blob: 7ea106bf65f0705c672b97820775149954a96dc3 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
32#ifdef FEAT_QUICKFIX
33static char *e_stringreq = N_("E928: String required");
34#endif
35
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
43static void f_argc(typval_T *argvars, typval_T *rettv);
44static void f_argidx(typval_T *argvars, typval_T *rettv);
45static void f_arglistid(typval_T *argvars, typval_T *rettv);
46static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010047static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_assert_equal(typval_T *argvars, typval_T *rettv);
49static void f_assert_exception(typval_T *argvars, typval_T *rettv);
50static void f_assert_fails(typval_T *argvars, typval_T *rettv);
51static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020052static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020053static void f_assert_match(typval_T *argvars, typval_T *rettv);
54static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
55static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010056static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_assert_true(typval_T *argvars, typval_T *rettv);
58#ifdef FEAT_FLOAT
59static void f_asin(typval_T *argvars, typval_T *rettv);
60static void f_atan(typval_T *argvars, typval_T *rettv);
61static void f_atan2(typval_T *argvars, typval_T *rettv);
62#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010063#ifdef FEAT_BEVAL
64static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010065# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010066static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010067# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010068#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020069static void f_browse(typval_T *argvars, typval_T *rettv);
70static void f_browsedir(typval_T *argvars, typval_T *rettv);
71static void f_bufexists(typval_T *argvars, typval_T *rettv);
72static void f_buflisted(typval_T *argvars, typval_T *rettv);
73static void f_bufloaded(typval_T *argvars, typval_T *rettv);
74static void f_bufname(typval_T *argvars, typval_T *rettv);
75static void f_bufnr(typval_T *argvars, typval_T *rettv);
76static void f_bufwinid(typval_T *argvars, typval_T *rettv);
77static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
78static void f_byte2line(typval_T *argvars, typval_T *rettv);
79static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
80static void f_byteidx(typval_T *argvars, typval_T *rettv);
81static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
82static void f_call(typval_T *argvars, typval_T *rettv);
83#ifdef FEAT_FLOAT
84static void f_ceil(typval_T *argvars, typval_T *rettv);
85#endif
86#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010087static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020088static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020089static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
91static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
92static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
93static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
94static void f_ch_info(typval_T *argvars, typval_T *rettv);
95static void f_ch_log(typval_T *argvars, typval_T *rettv);
96static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
97static void f_ch_open(typval_T *argvars, typval_T *rettv);
98static void f_ch_read(typval_T *argvars, typval_T *rettv);
99static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
100static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
101static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
102static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
103static void f_ch_status(typval_T *argvars, typval_T *rettv);
104#endif
105static void f_changenr(typval_T *argvars, typval_T *rettv);
106static void f_char2nr(typval_T *argvars, typval_T *rettv);
107static void f_cindent(typval_T *argvars, typval_T *rettv);
108static void f_clearmatches(typval_T *argvars, typval_T *rettv);
109static void f_col(typval_T *argvars, typval_T *rettv);
110#if defined(FEAT_INS_EXPAND)
111static void f_complete(typval_T *argvars, typval_T *rettv);
112static void f_complete_add(typval_T *argvars, typval_T *rettv);
113static void f_complete_check(typval_T *argvars, typval_T *rettv);
114#endif
115static void f_confirm(typval_T *argvars, typval_T *rettv);
116static void f_copy(typval_T *argvars, typval_T *rettv);
117#ifdef FEAT_FLOAT
118static void f_cos(typval_T *argvars, typval_T *rettv);
119static void f_cosh(typval_T *argvars, typval_T *rettv);
120#endif
121static void f_count(typval_T *argvars, typval_T *rettv);
122static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
123static void f_cursor(typval_T *argsvars, typval_T *rettv);
124static void f_deepcopy(typval_T *argvars, typval_T *rettv);
125static void f_delete(typval_T *argvars, typval_T *rettv);
126static void f_did_filetype(typval_T *argvars, typval_T *rettv);
127static void f_diff_filler(typval_T *argvars, typval_T *rettv);
128static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
129static void f_empty(typval_T *argvars, typval_T *rettv);
130static void f_escape(typval_T *argvars, typval_T *rettv);
131static void f_eval(typval_T *argvars, typval_T *rettv);
132static void f_eventhandler(typval_T *argvars, typval_T *rettv);
133static void f_executable(typval_T *argvars, typval_T *rettv);
134static void f_execute(typval_T *argvars, typval_T *rettv);
135static void f_exepath(typval_T *argvars, typval_T *rettv);
136static void f_exists(typval_T *argvars, typval_T *rettv);
137#ifdef FEAT_FLOAT
138static void f_exp(typval_T *argvars, typval_T *rettv);
139#endif
140static void f_expand(typval_T *argvars, typval_T *rettv);
141static void f_extend(typval_T *argvars, typval_T *rettv);
142static void f_feedkeys(typval_T *argvars, typval_T *rettv);
143static void f_filereadable(typval_T *argvars, typval_T *rettv);
144static void f_filewritable(typval_T *argvars, typval_T *rettv);
145static void f_filter(typval_T *argvars, typval_T *rettv);
146static void f_finddir(typval_T *argvars, typval_T *rettv);
147static void f_findfile(typval_T *argvars, typval_T *rettv);
148#ifdef FEAT_FLOAT
149static void f_float2nr(typval_T *argvars, typval_T *rettv);
150static void f_floor(typval_T *argvars, typval_T *rettv);
151static void f_fmod(typval_T *argvars, typval_T *rettv);
152#endif
153static void f_fnameescape(typval_T *argvars, typval_T *rettv);
154static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
155static void f_foldclosed(typval_T *argvars, typval_T *rettv);
156static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
157static void f_foldlevel(typval_T *argvars, typval_T *rettv);
158static void f_foldtext(typval_T *argvars, typval_T *rettv);
159static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
160static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200161static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200162static void f_function(typval_T *argvars, typval_T *rettv);
163static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
164static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200165static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200166static void f_getbufline(typval_T *argvars, typval_T *rettv);
167static void f_getbufvar(typval_T *argvars, typval_T *rettv);
168static void f_getchar(typval_T *argvars, typval_T *rettv);
169static void f_getcharmod(typval_T *argvars, typval_T *rettv);
170static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
171static void f_getcmdline(typval_T *argvars, typval_T *rettv);
172#if defined(FEAT_CMDL_COMPL)
173static void f_getcompletion(typval_T *argvars, typval_T *rettv);
174#endif
175static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
176static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
177static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
178static void f_getcwd(typval_T *argvars, typval_T *rettv);
179static void f_getfontname(typval_T *argvars, typval_T *rettv);
180static void f_getfperm(typval_T *argvars, typval_T *rettv);
181static void f_getfsize(typval_T *argvars, typval_T *rettv);
182static void f_getftime(typval_T *argvars, typval_T *rettv);
183static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100184static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200185static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200186static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200187static void f_getmatches(typval_T *argvars, typval_T *rettv);
188static void f_getpid(typval_T *argvars, typval_T *rettv);
189static void f_getcurpos(typval_T *argvars, typval_T *rettv);
190static void f_getpos(typval_T *argvars, typval_T *rettv);
191static void f_getqflist(typval_T *argvars, typval_T *rettv);
192static void f_getreg(typval_T *argvars, typval_T *rettv);
193static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200194static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200195static void f_gettabvar(typval_T *argvars, typval_T *rettv);
196static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200197static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200198static void f_getwinposx(typval_T *argvars, typval_T *rettv);
199static void f_getwinposy(typval_T *argvars, typval_T *rettv);
200static void f_getwinvar(typval_T *argvars, typval_T *rettv);
201static void f_glob(typval_T *argvars, typval_T *rettv);
202static void f_globpath(typval_T *argvars, typval_T *rettv);
203static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
204static void f_has(typval_T *argvars, typval_T *rettv);
205static void f_has_key(typval_T *argvars, typval_T *rettv);
206static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
207static void f_hasmapto(typval_T *argvars, typval_T *rettv);
208static void f_histadd(typval_T *argvars, typval_T *rettv);
209static void f_histdel(typval_T *argvars, typval_T *rettv);
210static void f_histget(typval_T *argvars, typval_T *rettv);
211static void f_histnr(typval_T *argvars, typval_T *rettv);
212static void f_hlID(typval_T *argvars, typval_T *rettv);
213static void f_hlexists(typval_T *argvars, typval_T *rettv);
214static void f_hostname(typval_T *argvars, typval_T *rettv);
215static void f_iconv(typval_T *argvars, typval_T *rettv);
216static void f_indent(typval_T *argvars, typval_T *rettv);
217static void f_index(typval_T *argvars, typval_T *rettv);
218static void f_input(typval_T *argvars, typval_T *rettv);
219static void f_inputdialog(typval_T *argvars, typval_T *rettv);
220static void f_inputlist(typval_T *argvars, typval_T *rettv);
221static void f_inputrestore(typval_T *argvars, typval_T *rettv);
222static void f_inputsave(typval_T *argvars, typval_T *rettv);
223static void f_inputsecret(typval_T *argvars, typval_T *rettv);
224static void f_insert(typval_T *argvars, typval_T *rettv);
225static void f_invert(typval_T *argvars, typval_T *rettv);
226static void f_isdirectory(typval_T *argvars, typval_T *rettv);
227static void f_islocked(typval_T *argvars, typval_T *rettv);
228#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
229static void f_isnan(typval_T *argvars, typval_T *rettv);
230#endif
231static void f_items(typval_T *argvars, typval_T *rettv);
232#ifdef FEAT_JOB_CHANNEL
233static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
234static void f_job_info(typval_T *argvars, typval_T *rettv);
235static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
236static void f_job_start(typval_T *argvars, typval_T *rettv);
237static void f_job_stop(typval_T *argvars, typval_T *rettv);
238static void f_job_status(typval_T *argvars, typval_T *rettv);
239#endif
240static void f_join(typval_T *argvars, typval_T *rettv);
241static void f_js_decode(typval_T *argvars, typval_T *rettv);
242static void f_js_encode(typval_T *argvars, typval_T *rettv);
243static void f_json_decode(typval_T *argvars, typval_T *rettv);
244static void f_json_encode(typval_T *argvars, typval_T *rettv);
245static void f_keys(typval_T *argvars, typval_T *rettv);
246static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
247static void f_len(typval_T *argvars, typval_T *rettv);
248static void f_libcall(typval_T *argvars, typval_T *rettv);
249static void f_libcallnr(typval_T *argvars, typval_T *rettv);
250static void f_line(typval_T *argvars, typval_T *rettv);
251static void f_line2byte(typval_T *argvars, typval_T *rettv);
252static void f_lispindent(typval_T *argvars, typval_T *rettv);
253static void f_localtime(typval_T *argvars, typval_T *rettv);
254#ifdef FEAT_FLOAT
255static void f_log(typval_T *argvars, typval_T *rettv);
256static void f_log10(typval_T *argvars, typval_T *rettv);
257#endif
258#ifdef FEAT_LUA
259static void f_luaeval(typval_T *argvars, typval_T *rettv);
260#endif
261static void f_map(typval_T *argvars, typval_T *rettv);
262static void f_maparg(typval_T *argvars, typval_T *rettv);
263static void f_mapcheck(typval_T *argvars, typval_T *rettv);
264static void f_match(typval_T *argvars, typval_T *rettv);
265static void f_matchadd(typval_T *argvars, typval_T *rettv);
266static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
267static void f_matcharg(typval_T *argvars, typval_T *rettv);
268static void f_matchdelete(typval_T *argvars, typval_T *rettv);
269static void f_matchend(typval_T *argvars, typval_T *rettv);
270static void f_matchlist(typval_T *argvars, typval_T *rettv);
271static void f_matchstr(typval_T *argvars, typval_T *rettv);
272static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
273static void f_max(typval_T *argvars, typval_T *rettv);
274static void f_min(typval_T *argvars, typval_T *rettv);
275#ifdef vim_mkdir
276static void f_mkdir(typval_T *argvars, typval_T *rettv);
277#endif
278static void f_mode(typval_T *argvars, typval_T *rettv);
279#ifdef FEAT_MZSCHEME
280static void f_mzeval(typval_T *argvars, typval_T *rettv);
281#endif
282static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
283static void f_nr2char(typval_T *argvars, typval_T *rettv);
284static void f_or(typval_T *argvars, typval_T *rettv);
285static void f_pathshorten(typval_T *argvars, typval_T *rettv);
286#ifdef FEAT_PERL
287static void f_perleval(typval_T *argvars, typval_T *rettv);
288#endif
289#ifdef FEAT_FLOAT
290static void f_pow(typval_T *argvars, typval_T *rettv);
291#endif
292static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
293static void f_printf(typval_T *argvars, typval_T *rettv);
294static void f_pumvisible(typval_T *argvars, typval_T *rettv);
295#ifdef FEAT_PYTHON3
296static void f_py3eval(typval_T *argvars, typval_T *rettv);
297#endif
298#ifdef FEAT_PYTHON
299static void f_pyeval(typval_T *argvars, typval_T *rettv);
300#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100301#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
302static void f_pyxeval(typval_T *argvars, typval_T *rettv);
303#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200304static void f_range(typval_T *argvars, typval_T *rettv);
305static void f_readfile(typval_T *argvars, typval_T *rettv);
306static void f_reltime(typval_T *argvars, typval_T *rettv);
307#ifdef FEAT_FLOAT
308static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
309#endif
310static void f_reltimestr(typval_T *argvars, typval_T *rettv);
311static void f_remote_expr(typval_T *argvars, typval_T *rettv);
312static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
313static void f_remote_peek(typval_T *argvars, typval_T *rettv);
314static void f_remote_read(typval_T *argvars, typval_T *rettv);
315static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100316static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200317static void f_remove(typval_T *argvars, typval_T *rettv);
318static void f_rename(typval_T *argvars, typval_T *rettv);
319static void f_repeat(typval_T *argvars, typval_T *rettv);
320static void f_resolve(typval_T *argvars, typval_T *rettv);
321static void f_reverse(typval_T *argvars, typval_T *rettv);
322#ifdef FEAT_FLOAT
323static void f_round(typval_T *argvars, typval_T *rettv);
324#endif
325static void f_screenattr(typval_T *argvars, typval_T *rettv);
326static void f_screenchar(typval_T *argvars, typval_T *rettv);
327static void f_screencol(typval_T *argvars, typval_T *rettv);
328static void f_screenrow(typval_T *argvars, typval_T *rettv);
329static void f_search(typval_T *argvars, typval_T *rettv);
330static void f_searchdecl(typval_T *argvars, typval_T *rettv);
331static void f_searchpair(typval_T *argvars, typval_T *rettv);
332static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
333static void f_searchpos(typval_T *argvars, typval_T *rettv);
334static void f_server2client(typval_T *argvars, typval_T *rettv);
335static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200336static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200337static void f_setbufvar(typval_T *argvars, typval_T *rettv);
338static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
339static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
340static void f_setfperm(typval_T *argvars, typval_T *rettv);
341static void f_setline(typval_T *argvars, typval_T *rettv);
342static void f_setloclist(typval_T *argvars, typval_T *rettv);
343static void f_setmatches(typval_T *argvars, typval_T *rettv);
344static void f_setpos(typval_T *argvars, typval_T *rettv);
345static void f_setqflist(typval_T *argvars, typval_T *rettv);
346static void f_setreg(typval_T *argvars, typval_T *rettv);
347static void f_settabvar(typval_T *argvars, typval_T *rettv);
348static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
349static void f_setwinvar(typval_T *argvars, typval_T *rettv);
350#ifdef FEAT_CRYPT
351static void f_sha256(typval_T *argvars, typval_T *rettv);
352#endif /* FEAT_CRYPT */
353static void f_shellescape(typval_T *argvars, typval_T *rettv);
354static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
355static void f_simplify(typval_T *argvars, typval_T *rettv);
356#ifdef FEAT_FLOAT
357static void f_sin(typval_T *argvars, typval_T *rettv);
358static void f_sinh(typval_T *argvars, typval_T *rettv);
359#endif
360static void f_sort(typval_T *argvars, typval_T *rettv);
361static void f_soundfold(typval_T *argvars, typval_T *rettv);
362static void f_spellbadword(typval_T *argvars, typval_T *rettv);
363static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
364static void f_split(typval_T *argvars, typval_T *rettv);
365#ifdef FEAT_FLOAT
366static void f_sqrt(typval_T *argvars, typval_T *rettv);
367static void f_str2float(typval_T *argvars, typval_T *rettv);
368#endif
369static void f_str2nr(typval_T *argvars, typval_T *rettv);
370static void f_strchars(typval_T *argvars, typval_T *rettv);
371#ifdef HAVE_STRFTIME
372static void f_strftime(typval_T *argvars, typval_T *rettv);
373#endif
374static void f_strgetchar(typval_T *argvars, typval_T *rettv);
375static void f_stridx(typval_T *argvars, typval_T *rettv);
376static void f_string(typval_T *argvars, typval_T *rettv);
377static void f_strlen(typval_T *argvars, typval_T *rettv);
378static void f_strcharpart(typval_T *argvars, typval_T *rettv);
379static void f_strpart(typval_T *argvars, typval_T *rettv);
380static void f_strridx(typval_T *argvars, typval_T *rettv);
381static void f_strtrans(typval_T *argvars, typval_T *rettv);
382static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
383static void f_strwidth(typval_T *argvars, typval_T *rettv);
384static void f_submatch(typval_T *argvars, typval_T *rettv);
385static void f_substitute(typval_T *argvars, typval_T *rettv);
386static void f_synID(typval_T *argvars, typval_T *rettv);
387static void f_synIDattr(typval_T *argvars, typval_T *rettv);
388static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
389static void f_synstack(typval_T *argvars, typval_T *rettv);
390static void f_synconcealed(typval_T *argvars, typval_T *rettv);
391static void f_system(typval_T *argvars, typval_T *rettv);
392static void f_systemlist(typval_T *argvars, typval_T *rettv);
393static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
394static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
395static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
396static void f_taglist(typval_T *argvars, typval_T *rettv);
397static void f_tagfiles(typval_T *argvars, typval_T *rettv);
398static void f_tempname(typval_T *argvars, typval_T *rettv);
399static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
400static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200401static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100402static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200403static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100404static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200405#ifdef FEAT_JOB_CHANNEL
406static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
407#endif
408static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
409#ifdef FEAT_JOB_CHANNEL
410static void f_test_null_job(typval_T *argvars, typval_T *rettv);
411#endif
412static void f_test_null_list(typval_T *argvars, typval_T *rettv);
413static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
414static void f_test_null_string(typval_T *argvars, typval_T *rettv);
415static void f_test_settime(typval_T *argvars, typval_T *rettv);
416#ifdef FEAT_FLOAT
417static void f_tan(typval_T *argvars, typval_T *rettv);
418static void f_tanh(typval_T *argvars, typval_T *rettv);
419#endif
420#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200421static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200422static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200423static void f_timer_start(typval_T *argvars, typval_T *rettv);
424static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200425static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200426#endif
427static void f_tolower(typval_T *argvars, typval_T *rettv);
428static void f_toupper(typval_T *argvars, typval_T *rettv);
429static void f_tr(typval_T *argvars, typval_T *rettv);
430#ifdef FEAT_FLOAT
431static void f_trunc(typval_T *argvars, typval_T *rettv);
432#endif
433static void f_type(typval_T *argvars, typval_T *rettv);
434static void f_undofile(typval_T *argvars, typval_T *rettv);
435static void f_undotree(typval_T *argvars, typval_T *rettv);
436static void f_uniq(typval_T *argvars, typval_T *rettv);
437static void f_values(typval_T *argvars, typval_T *rettv);
438static void f_virtcol(typval_T *argvars, typval_T *rettv);
439static void f_visualmode(typval_T *argvars, typval_T *rettv);
440static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
441static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
442static void f_win_getid(typval_T *argvars, typval_T *rettv);
443static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
444static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
445static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100446static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200447static void f_winbufnr(typval_T *argvars, typval_T *rettv);
448static void f_wincol(typval_T *argvars, typval_T *rettv);
449static void f_winheight(typval_T *argvars, typval_T *rettv);
450static void f_winline(typval_T *argvars, typval_T *rettv);
451static void f_winnr(typval_T *argvars, typval_T *rettv);
452static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
453static void f_winrestview(typval_T *argvars, typval_T *rettv);
454static void f_winsaveview(typval_T *argvars, typval_T *rettv);
455static void f_winwidth(typval_T *argvars, typval_T *rettv);
456static void f_writefile(typval_T *argvars, typval_T *rettv);
457static void f_wordcount(typval_T *argvars, typval_T *rettv);
458static void f_xor(typval_T *argvars, typval_T *rettv);
459
460/*
461 * Array with names and number of arguments of all internal functions
462 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
463 */
464static struct fst
465{
466 char *f_name; /* function name */
467 char f_min_argc; /* minimal number of arguments */
468 char f_max_argc; /* maximal number of arguments */
469 void (*f_func)(typval_T *args, typval_T *rvar);
470 /* implementation of function */
471} functions[] =
472{
473#ifdef FEAT_FLOAT
474 {"abs", 1, 1, f_abs},
475 {"acos", 1, 1, f_acos}, /* WJMc */
476#endif
477 {"add", 2, 2, f_add},
478 {"and", 2, 2, f_and},
479 {"append", 2, 2, f_append},
480 {"argc", 0, 0, f_argc},
481 {"argidx", 0, 0, f_argidx},
482 {"arglistid", 0, 2, f_arglistid},
483 {"argv", 0, 1, f_argv},
484#ifdef FEAT_FLOAT
485 {"asin", 1, 1, f_asin}, /* WJMc */
486#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100487 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200488 {"assert_equal", 2, 3, f_assert_equal},
489 {"assert_exception", 1, 2, f_assert_exception},
490 {"assert_fails", 1, 2, f_assert_fails},
491 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100492 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200493 {"assert_match", 2, 3, f_assert_match},
494 {"assert_notequal", 2, 3, f_assert_notequal},
495 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100496 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200497 {"assert_true", 1, 2, f_assert_true},
498#ifdef FEAT_FLOAT
499 {"atan", 1, 1, f_atan},
500 {"atan2", 2, 2, f_atan2},
501#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100502#ifdef FEAT_BEVAL
503 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100504# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100505 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100506# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100507#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200508 {"browse", 4, 4, f_browse},
509 {"browsedir", 2, 2, f_browsedir},
510 {"bufexists", 1, 1, f_bufexists},
511 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
512 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
513 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
514 {"buflisted", 1, 1, f_buflisted},
515 {"bufloaded", 1, 1, f_bufloaded},
516 {"bufname", 1, 1, f_bufname},
517 {"bufnr", 1, 2, f_bufnr},
518 {"bufwinid", 1, 1, f_bufwinid},
519 {"bufwinnr", 1, 1, f_bufwinnr},
520 {"byte2line", 1, 1, f_byte2line},
521 {"byteidx", 2, 2, f_byteidx},
522 {"byteidxcomp", 2, 2, f_byteidxcomp},
523 {"call", 2, 3, f_call},
524#ifdef FEAT_FLOAT
525 {"ceil", 1, 1, f_ceil},
526#endif
527#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100528 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200529 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200530 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200531 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
532 {"ch_evalraw", 2, 3, f_ch_evalraw},
533 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
534 {"ch_getjob", 1, 1, f_ch_getjob},
535 {"ch_info", 1, 1, f_ch_info},
536 {"ch_log", 1, 2, f_ch_log},
537 {"ch_logfile", 1, 2, f_ch_logfile},
538 {"ch_open", 1, 2, f_ch_open},
539 {"ch_read", 1, 2, f_ch_read},
540 {"ch_readraw", 1, 2, f_ch_readraw},
541 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
542 {"ch_sendraw", 2, 3, f_ch_sendraw},
543 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200544 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200545#endif
546 {"changenr", 0, 0, f_changenr},
547 {"char2nr", 1, 2, f_char2nr},
548 {"cindent", 1, 1, f_cindent},
549 {"clearmatches", 0, 0, f_clearmatches},
550 {"col", 1, 1, f_col},
551#if defined(FEAT_INS_EXPAND)
552 {"complete", 2, 2, f_complete},
553 {"complete_add", 1, 1, f_complete_add},
554 {"complete_check", 0, 0, f_complete_check},
555#endif
556 {"confirm", 1, 4, f_confirm},
557 {"copy", 1, 1, f_copy},
558#ifdef FEAT_FLOAT
559 {"cos", 1, 1, f_cos},
560 {"cosh", 1, 1, f_cosh},
561#endif
562 {"count", 2, 4, f_count},
563 {"cscope_connection",0,3, f_cscope_connection},
564 {"cursor", 1, 3, f_cursor},
565 {"deepcopy", 1, 2, f_deepcopy},
566 {"delete", 1, 2, f_delete},
567 {"did_filetype", 0, 0, f_did_filetype},
568 {"diff_filler", 1, 1, f_diff_filler},
569 {"diff_hlID", 2, 2, f_diff_hlID},
570 {"empty", 1, 1, f_empty},
571 {"escape", 2, 2, f_escape},
572 {"eval", 1, 1, f_eval},
573 {"eventhandler", 0, 0, f_eventhandler},
574 {"executable", 1, 1, f_executable},
575 {"execute", 1, 2, f_execute},
576 {"exepath", 1, 1, f_exepath},
577 {"exists", 1, 1, f_exists},
578#ifdef FEAT_FLOAT
579 {"exp", 1, 1, f_exp},
580#endif
581 {"expand", 1, 3, f_expand},
582 {"extend", 2, 3, f_extend},
583 {"feedkeys", 1, 2, f_feedkeys},
584 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
585 {"filereadable", 1, 1, f_filereadable},
586 {"filewritable", 1, 1, f_filewritable},
587 {"filter", 2, 2, f_filter},
588 {"finddir", 1, 3, f_finddir},
589 {"findfile", 1, 3, f_findfile},
590#ifdef FEAT_FLOAT
591 {"float2nr", 1, 1, f_float2nr},
592 {"floor", 1, 1, f_floor},
593 {"fmod", 2, 2, f_fmod},
594#endif
595 {"fnameescape", 1, 1, f_fnameescape},
596 {"fnamemodify", 2, 2, f_fnamemodify},
597 {"foldclosed", 1, 1, f_foldclosed},
598 {"foldclosedend", 1, 1, f_foldclosedend},
599 {"foldlevel", 1, 1, f_foldlevel},
600 {"foldtext", 0, 0, f_foldtext},
601 {"foldtextresult", 1, 1, f_foldtextresult},
602 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200603 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200604 {"function", 1, 3, f_function},
605 {"garbagecollect", 0, 1, f_garbagecollect},
606 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200607 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200608 {"getbufline", 2, 3, f_getbufline},
609 {"getbufvar", 2, 3, f_getbufvar},
610 {"getchar", 0, 1, f_getchar},
611 {"getcharmod", 0, 0, f_getcharmod},
612 {"getcharsearch", 0, 0, f_getcharsearch},
613 {"getcmdline", 0, 0, f_getcmdline},
614 {"getcmdpos", 0, 0, f_getcmdpos},
615 {"getcmdtype", 0, 0, f_getcmdtype},
616 {"getcmdwintype", 0, 0, f_getcmdwintype},
617#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200618 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200619#endif
620 {"getcurpos", 0, 0, f_getcurpos},
621 {"getcwd", 0, 2, f_getcwd},
622 {"getfontname", 0, 1, f_getfontname},
623 {"getfperm", 1, 1, f_getfperm},
624 {"getfsize", 1, 1, f_getfsize},
625 {"getftime", 1, 1, f_getftime},
626 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100627 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200628 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200629 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200630 {"getmatches", 0, 0, f_getmatches},
631 {"getpid", 0, 0, f_getpid},
632 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200633 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200634 {"getreg", 0, 3, f_getreg},
635 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200636 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200637 {"gettabvar", 2, 3, f_gettabvar},
638 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200639 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200640 {"getwinposx", 0, 0, f_getwinposx},
641 {"getwinposy", 0, 0, f_getwinposy},
642 {"getwinvar", 2, 3, f_getwinvar},
643 {"glob", 1, 4, f_glob},
644 {"glob2regpat", 1, 1, f_glob2regpat},
645 {"globpath", 2, 5, f_globpath},
646 {"has", 1, 1, f_has},
647 {"has_key", 2, 2, f_has_key},
648 {"haslocaldir", 0, 2, f_haslocaldir},
649 {"hasmapto", 1, 3, f_hasmapto},
650 {"highlightID", 1, 1, f_hlID}, /* obsolete */
651 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
652 {"histadd", 2, 2, f_histadd},
653 {"histdel", 1, 2, f_histdel},
654 {"histget", 1, 2, f_histget},
655 {"histnr", 1, 1, f_histnr},
656 {"hlID", 1, 1, f_hlID},
657 {"hlexists", 1, 1, f_hlexists},
658 {"hostname", 0, 0, f_hostname},
659 {"iconv", 3, 3, f_iconv},
660 {"indent", 1, 1, f_indent},
661 {"index", 2, 4, f_index},
662 {"input", 1, 3, f_input},
663 {"inputdialog", 1, 3, f_inputdialog},
664 {"inputlist", 1, 1, f_inputlist},
665 {"inputrestore", 0, 0, f_inputrestore},
666 {"inputsave", 0, 0, f_inputsave},
667 {"inputsecret", 1, 2, f_inputsecret},
668 {"insert", 2, 3, f_insert},
669 {"invert", 1, 1, f_invert},
670 {"isdirectory", 1, 1, f_isdirectory},
671 {"islocked", 1, 1, f_islocked},
672#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
673 {"isnan", 1, 1, f_isnan},
674#endif
675 {"items", 1, 1, f_items},
676#ifdef FEAT_JOB_CHANNEL
677 {"job_getchannel", 1, 1, f_job_getchannel},
678 {"job_info", 1, 1, f_job_info},
679 {"job_setoptions", 2, 2, f_job_setoptions},
680 {"job_start", 1, 2, f_job_start},
681 {"job_status", 1, 1, f_job_status},
682 {"job_stop", 1, 2, f_job_stop},
683#endif
684 {"join", 1, 2, f_join},
685 {"js_decode", 1, 1, f_js_decode},
686 {"js_encode", 1, 1, f_js_encode},
687 {"json_decode", 1, 1, f_json_decode},
688 {"json_encode", 1, 1, f_json_encode},
689 {"keys", 1, 1, f_keys},
690 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
691 {"len", 1, 1, f_len},
692 {"libcall", 3, 3, f_libcall},
693 {"libcallnr", 3, 3, f_libcallnr},
694 {"line", 1, 1, f_line},
695 {"line2byte", 1, 1, f_line2byte},
696 {"lispindent", 1, 1, f_lispindent},
697 {"localtime", 0, 0, f_localtime},
698#ifdef FEAT_FLOAT
699 {"log", 1, 1, f_log},
700 {"log10", 1, 1, f_log10},
701#endif
702#ifdef FEAT_LUA
703 {"luaeval", 1, 2, f_luaeval},
704#endif
705 {"map", 2, 2, f_map},
706 {"maparg", 1, 4, f_maparg},
707 {"mapcheck", 1, 3, f_mapcheck},
708 {"match", 2, 4, f_match},
709 {"matchadd", 2, 5, f_matchadd},
710 {"matchaddpos", 2, 5, f_matchaddpos},
711 {"matcharg", 1, 1, f_matcharg},
712 {"matchdelete", 1, 1, f_matchdelete},
713 {"matchend", 2, 4, f_matchend},
714 {"matchlist", 2, 4, f_matchlist},
715 {"matchstr", 2, 4, f_matchstr},
716 {"matchstrpos", 2, 4, f_matchstrpos},
717 {"max", 1, 1, f_max},
718 {"min", 1, 1, f_min},
719#ifdef vim_mkdir
720 {"mkdir", 1, 3, f_mkdir},
721#endif
722 {"mode", 0, 1, f_mode},
723#ifdef FEAT_MZSCHEME
724 {"mzeval", 1, 1, f_mzeval},
725#endif
726 {"nextnonblank", 1, 1, f_nextnonblank},
727 {"nr2char", 1, 2, f_nr2char},
728 {"or", 2, 2, f_or},
729 {"pathshorten", 1, 1, f_pathshorten},
730#ifdef FEAT_PERL
731 {"perleval", 1, 1, f_perleval},
732#endif
733#ifdef FEAT_FLOAT
734 {"pow", 2, 2, f_pow},
735#endif
736 {"prevnonblank", 1, 1, f_prevnonblank},
737 {"printf", 2, 19, f_printf},
738 {"pumvisible", 0, 0, f_pumvisible},
739#ifdef FEAT_PYTHON3
740 {"py3eval", 1, 1, f_py3eval},
741#endif
742#ifdef FEAT_PYTHON
743 {"pyeval", 1, 1, f_pyeval},
744#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100745#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
746 {"pyxeval", 1, 1, f_pyxeval},
747#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200748 {"range", 1, 3, f_range},
749 {"readfile", 1, 3, f_readfile},
750 {"reltime", 0, 2, f_reltime},
751#ifdef FEAT_FLOAT
752 {"reltimefloat", 1, 1, f_reltimefloat},
753#endif
754 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100755 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200756 {"remote_foreground", 1, 1, f_remote_foreground},
757 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100758 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200759 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100760 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200761 {"remove", 2, 3, f_remove},
762 {"rename", 2, 2, f_rename},
763 {"repeat", 2, 2, f_repeat},
764 {"resolve", 1, 1, f_resolve},
765 {"reverse", 1, 1, f_reverse},
766#ifdef FEAT_FLOAT
767 {"round", 1, 1, f_round},
768#endif
769 {"screenattr", 2, 2, f_screenattr},
770 {"screenchar", 2, 2, f_screenchar},
771 {"screencol", 0, 0, f_screencol},
772 {"screenrow", 0, 0, f_screenrow},
773 {"search", 1, 4, f_search},
774 {"searchdecl", 1, 3, f_searchdecl},
775 {"searchpair", 3, 7, f_searchpair},
776 {"searchpairpos", 3, 7, f_searchpairpos},
777 {"searchpos", 1, 4, f_searchpos},
778 {"server2client", 2, 2, f_server2client},
779 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200780 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200781 {"setbufvar", 3, 3, f_setbufvar},
782 {"setcharsearch", 1, 1, f_setcharsearch},
783 {"setcmdpos", 1, 1, f_setcmdpos},
784 {"setfperm", 2, 2, f_setfperm},
785 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200786 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200787 {"setmatches", 1, 1, f_setmatches},
788 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200789 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200790 {"setreg", 2, 3, f_setreg},
791 {"settabvar", 3, 3, f_settabvar},
792 {"settabwinvar", 4, 4, f_settabwinvar},
793 {"setwinvar", 3, 3, f_setwinvar},
794#ifdef FEAT_CRYPT
795 {"sha256", 1, 1, f_sha256},
796#endif
797 {"shellescape", 1, 2, f_shellescape},
798 {"shiftwidth", 0, 0, f_shiftwidth},
799 {"simplify", 1, 1, f_simplify},
800#ifdef FEAT_FLOAT
801 {"sin", 1, 1, f_sin},
802 {"sinh", 1, 1, f_sinh},
803#endif
804 {"sort", 1, 3, f_sort},
805 {"soundfold", 1, 1, f_soundfold},
806 {"spellbadword", 0, 1, f_spellbadword},
807 {"spellsuggest", 1, 3, f_spellsuggest},
808 {"split", 1, 3, f_split},
809#ifdef FEAT_FLOAT
810 {"sqrt", 1, 1, f_sqrt},
811 {"str2float", 1, 1, f_str2float},
812#endif
813 {"str2nr", 1, 2, f_str2nr},
814 {"strcharpart", 2, 3, f_strcharpart},
815 {"strchars", 1, 2, f_strchars},
816 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
817#ifdef HAVE_STRFTIME
818 {"strftime", 1, 2, f_strftime},
819#endif
820 {"strgetchar", 2, 2, f_strgetchar},
821 {"stridx", 2, 3, f_stridx},
822 {"string", 1, 1, f_string},
823 {"strlen", 1, 1, f_strlen},
824 {"strpart", 2, 3, f_strpart},
825 {"strridx", 2, 3, f_strridx},
826 {"strtrans", 1, 1, f_strtrans},
827 {"strwidth", 1, 1, f_strwidth},
828 {"submatch", 1, 2, f_submatch},
829 {"substitute", 4, 4, f_substitute},
830 {"synID", 3, 3, f_synID},
831 {"synIDattr", 2, 3, f_synIDattr},
832 {"synIDtrans", 1, 1, f_synIDtrans},
833 {"synconcealed", 2, 2, f_synconcealed},
834 {"synstack", 2, 2, f_synstack},
835 {"system", 1, 2, f_system},
836 {"systemlist", 1, 2, f_systemlist},
837 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
838 {"tabpagenr", 0, 1, f_tabpagenr},
839 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
840 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100841 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200842#ifdef FEAT_FLOAT
843 {"tan", 1, 1, f_tan},
844 {"tanh", 1, 1, f_tanh},
845#endif
846 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200847#ifdef FEAT_TERMINAL
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200848 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200849 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200850 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200851 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200852 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200853 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200854 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200855 {"term_getstatus", 1, 1, f_term_getstatus},
856 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200857 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200858 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200859 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200860 {"term_sendkeys", 2, 2, f_term_sendkeys},
861 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200862 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200863#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200864 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
865 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200866 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200867 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100868 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200869#ifdef FEAT_JOB_CHANNEL
870 {"test_null_channel", 0, 0, f_test_null_channel},
871#endif
872 {"test_null_dict", 0, 0, f_test_null_dict},
873#ifdef FEAT_JOB_CHANNEL
874 {"test_null_job", 0, 0, f_test_null_job},
875#endif
876 {"test_null_list", 0, 0, f_test_null_list},
877 {"test_null_partial", 0, 0, f_test_null_partial},
878 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100879 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200880 {"test_settime", 1, 1, f_test_settime},
881#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200882 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200883 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200884 {"timer_start", 2, 3, f_timer_start},
885 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200886 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200887#endif
888 {"tolower", 1, 1, f_tolower},
889 {"toupper", 1, 1, f_toupper},
890 {"tr", 3, 3, f_tr},
891#ifdef FEAT_FLOAT
892 {"trunc", 1, 1, f_trunc},
893#endif
894 {"type", 1, 1, f_type},
895 {"undofile", 1, 1, f_undofile},
896 {"undotree", 0, 0, f_undotree},
897 {"uniq", 1, 3, f_uniq},
898 {"values", 1, 1, f_values},
899 {"virtcol", 1, 1, f_virtcol},
900 {"visualmode", 0, 1, f_visualmode},
901 {"wildmenumode", 0, 0, f_wildmenumode},
902 {"win_findbuf", 1, 1, f_win_findbuf},
903 {"win_getid", 0, 2, f_win_getid},
904 {"win_gotoid", 1, 1, f_win_gotoid},
905 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
906 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100907 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200908 {"winbufnr", 1, 1, f_winbufnr},
909 {"wincol", 0, 0, f_wincol},
910 {"winheight", 1, 1, f_winheight},
911 {"winline", 0, 0, f_winline},
912 {"winnr", 0, 1, f_winnr},
913 {"winrestcmd", 0, 0, f_winrestcmd},
914 {"winrestview", 1, 1, f_winrestview},
915 {"winsaveview", 0, 0, f_winsaveview},
916 {"winwidth", 1, 1, f_winwidth},
917 {"wordcount", 0, 0, f_wordcount},
918 {"writefile", 2, 3, f_writefile},
919 {"xor", 2, 2, f_xor},
920};
921
922#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
923
924/*
925 * Function given to ExpandGeneric() to obtain the list of internal
926 * or user defined function names.
927 */
928 char_u *
929get_function_name(expand_T *xp, int idx)
930{
931 static int intidx = -1;
932 char_u *name;
933
934 if (idx == 0)
935 intidx = -1;
936 if (intidx < 0)
937 {
938 name = get_user_func_name(xp, idx);
939 if (name != NULL)
940 return name;
941 }
942 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
943 {
944 STRCPY(IObuff, functions[intidx].f_name);
945 STRCAT(IObuff, "(");
946 if (functions[intidx].f_max_argc == 0)
947 STRCAT(IObuff, ")");
948 return IObuff;
949 }
950
951 return NULL;
952}
953
954/*
955 * Function given to ExpandGeneric() to obtain the list of internal or
956 * user defined variable or function names.
957 */
958 char_u *
959get_expr_name(expand_T *xp, int idx)
960{
961 static int intidx = -1;
962 char_u *name;
963
964 if (idx == 0)
965 intidx = -1;
966 if (intidx < 0)
967 {
968 name = get_function_name(xp, idx);
969 if (name != NULL)
970 return name;
971 }
972 return get_user_var_name(xp, ++intidx);
973}
974
975#endif /* FEAT_CMDL_COMPL */
976
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200977/*
978 * Find internal function in table above.
979 * Return index, or -1 if not found
980 */
981 int
982find_internal_func(
983 char_u *name) /* name of the function */
984{
985 int first = 0;
986 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
987 int cmp;
988 int x;
989
990 /*
991 * Find the function name in the table. Binary search.
992 */
993 while (first <= last)
994 {
995 x = first + ((unsigned)(last - first) >> 1);
996 cmp = STRCMP(name, functions[x].f_name);
997 if (cmp < 0)
998 last = x - 1;
999 else if (cmp > 0)
1000 first = x + 1;
1001 else
1002 return x;
1003 }
1004 return -1;
1005}
1006
1007 int
1008call_internal_func(
1009 char_u *name,
1010 int argcount,
1011 typval_T *argvars,
1012 typval_T *rettv)
1013{
1014 int i;
1015
1016 i = find_internal_func(name);
1017 if (i < 0)
1018 return ERROR_UNKNOWN;
1019 if (argcount < functions[i].f_min_argc)
1020 return ERROR_TOOFEW;
1021 if (argcount > functions[i].f_max_argc)
1022 return ERROR_TOOMANY;
1023 argvars[argcount].v_type = VAR_UNKNOWN;
1024 functions[i].f_func(argvars, rettv);
1025 return ERROR_NONE;
1026}
1027
1028/*
1029 * Return TRUE for a non-zero Number and a non-empty String.
1030 */
1031 static int
1032non_zero_arg(typval_T *argvars)
1033{
1034 return ((argvars[0].v_type == VAR_NUMBER
1035 && argvars[0].vval.v_number != 0)
1036 || (argvars[0].v_type == VAR_SPECIAL
1037 && argvars[0].vval.v_number == VVAL_TRUE)
1038 || (argvars[0].v_type == VAR_STRING
1039 && argvars[0].vval.v_string != NULL
1040 && *argvars[0].vval.v_string != NUL));
1041}
1042
1043/*
1044 * Get the lnum from the first argument.
1045 * Also accepts ".", "$", etc., but that only works for the current buffer.
1046 * Returns -1 on error.
1047 */
1048 static linenr_T
1049get_tv_lnum(typval_T *argvars)
1050{
1051 typval_T rettv;
1052 linenr_T lnum;
1053
1054 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1055 if (lnum == 0) /* no valid number, try using line() */
1056 {
1057 rettv.v_type = VAR_NUMBER;
1058 f_line(argvars, &rettv);
1059 lnum = (linenr_T)rettv.vval.v_number;
1060 clear_tv(&rettv);
1061 }
1062 return lnum;
1063}
1064
1065#ifdef FEAT_FLOAT
1066static int get_float_arg(typval_T *argvars, float_T *f);
1067
1068/*
1069 * Get the float value of "argvars[0]" into "f".
1070 * Returns FAIL when the argument is not a Number or Float.
1071 */
1072 static int
1073get_float_arg(typval_T *argvars, float_T *f)
1074{
1075 if (argvars[0].v_type == VAR_FLOAT)
1076 {
1077 *f = argvars[0].vval.v_float;
1078 return OK;
1079 }
1080 if (argvars[0].v_type == VAR_NUMBER)
1081 {
1082 *f = (float_T)argvars[0].vval.v_number;
1083 return OK;
1084 }
1085 EMSG(_("E808: Number or Float required"));
1086 return FAIL;
1087}
1088
1089/*
1090 * "abs(expr)" function
1091 */
1092 static void
1093f_abs(typval_T *argvars, typval_T *rettv)
1094{
1095 if (argvars[0].v_type == VAR_FLOAT)
1096 {
1097 rettv->v_type = VAR_FLOAT;
1098 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1099 }
1100 else
1101 {
1102 varnumber_T n;
1103 int error = FALSE;
1104
1105 n = get_tv_number_chk(&argvars[0], &error);
1106 if (error)
1107 rettv->vval.v_number = -1;
1108 else if (n > 0)
1109 rettv->vval.v_number = n;
1110 else
1111 rettv->vval.v_number = -n;
1112 }
1113}
1114
1115/*
1116 * "acos()" function
1117 */
1118 static void
1119f_acos(typval_T *argvars, typval_T *rettv)
1120{
1121 float_T f = 0.0;
1122
1123 rettv->v_type = VAR_FLOAT;
1124 if (get_float_arg(argvars, &f) == OK)
1125 rettv->vval.v_float = acos(f);
1126 else
1127 rettv->vval.v_float = 0.0;
1128}
1129#endif
1130
1131/*
1132 * "add(list, item)" function
1133 */
1134 static void
1135f_add(typval_T *argvars, typval_T *rettv)
1136{
1137 list_T *l;
1138
1139 rettv->vval.v_number = 1; /* Default: Failed */
1140 if (argvars[0].v_type == VAR_LIST)
1141 {
1142 if ((l = argvars[0].vval.v_list) != NULL
1143 && !tv_check_lock(l->lv_lock,
1144 (char_u *)N_("add() argument"), TRUE)
1145 && list_append_tv(l, &argvars[1]) == OK)
1146 copy_tv(&argvars[0], rettv);
1147 }
1148 else
1149 EMSG(_(e_listreq));
1150}
1151
1152/*
1153 * "and(expr, expr)" function
1154 */
1155 static void
1156f_and(typval_T *argvars, typval_T *rettv)
1157{
1158 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1159 & get_tv_number_chk(&argvars[1], NULL);
1160}
1161
1162/*
1163 * "append(lnum, string/list)" function
1164 */
1165 static void
1166f_append(typval_T *argvars, typval_T *rettv)
1167{
1168 long lnum;
1169 char_u *line;
1170 list_T *l = NULL;
1171 listitem_T *li = NULL;
1172 typval_T *tv;
1173 long added = 0;
1174
1175 /* When coming here from Insert mode, sync undo, so that this can be
1176 * undone separately from what was previously inserted. */
1177 if (u_sync_once == 2)
1178 {
1179 u_sync_once = 1; /* notify that u_sync() was called */
1180 u_sync(TRUE);
1181 }
1182
1183 lnum = get_tv_lnum(argvars);
1184 if (lnum >= 0
1185 && lnum <= curbuf->b_ml.ml_line_count
1186 && u_save(lnum, lnum + 1) == OK)
1187 {
1188 if (argvars[1].v_type == VAR_LIST)
1189 {
1190 l = argvars[1].vval.v_list;
1191 if (l == NULL)
1192 return;
1193 li = l->lv_first;
1194 }
1195 for (;;)
1196 {
1197 if (l == NULL)
1198 tv = &argvars[1]; /* append a string */
1199 else if (li == NULL)
1200 break; /* end of list */
1201 else
1202 tv = &li->li_tv; /* append item from list */
1203 line = get_tv_string_chk(tv);
1204 if (line == NULL) /* type error */
1205 {
1206 rettv->vval.v_number = 1; /* Failed */
1207 break;
1208 }
1209 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1210 ++added;
1211 if (l == NULL)
1212 break;
1213 li = li->li_next;
1214 }
1215
1216 appended_lines_mark(lnum, added);
1217 if (curwin->w_cursor.lnum > lnum)
1218 curwin->w_cursor.lnum += added;
1219 }
1220 else
1221 rettv->vval.v_number = 1; /* Failed */
1222}
1223
1224/*
1225 * "argc()" function
1226 */
1227 static void
1228f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1229{
1230 rettv->vval.v_number = ARGCOUNT;
1231}
1232
1233/*
1234 * "argidx()" function
1235 */
1236 static void
1237f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1238{
1239 rettv->vval.v_number = curwin->w_arg_idx;
1240}
1241
1242/*
1243 * "arglistid()" function
1244 */
1245 static void
1246f_arglistid(typval_T *argvars, typval_T *rettv)
1247{
1248 win_T *wp;
1249
1250 rettv->vval.v_number = -1;
1251 wp = find_tabwin(&argvars[0], &argvars[1]);
1252 if (wp != NULL)
1253 rettv->vval.v_number = wp->w_alist->id;
1254}
1255
1256/*
1257 * "argv(nr)" function
1258 */
1259 static void
1260f_argv(typval_T *argvars, typval_T *rettv)
1261{
1262 int idx;
1263
1264 if (argvars[0].v_type != VAR_UNKNOWN)
1265 {
1266 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1267 if (idx >= 0 && idx < ARGCOUNT)
1268 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1269 else
1270 rettv->vval.v_string = NULL;
1271 rettv->v_type = VAR_STRING;
1272 }
1273 else if (rettv_list_alloc(rettv) == OK)
1274 for (idx = 0; idx < ARGCOUNT; ++idx)
1275 list_append_string(rettv->vval.v_list,
1276 alist_name(&ARGLIST[idx]), -1);
1277}
1278
1279/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001280 * "assert_beeps(cmd [, error])" function
1281 */
1282 static void
1283f_assert_beeps(typval_T *argvars, typval_T *rettv UNUSED)
1284{
1285 assert_beeps(argvars);
1286}
1287
1288/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001289 * "assert_equal(expected, actual[, msg])" function
1290 */
1291 static void
1292f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1293{
1294 assert_equal_common(argvars, ASSERT_EQUAL);
1295}
1296
1297/*
1298 * "assert_notequal(expected, actual[, msg])" function
1299 */
1300 static void
1301f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1302{
1303 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1304}
1305
1306/*
1307 * "assert_exception(string[, msg])" function
1308 */
1309 static void
1310f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1311{
1312 assert_exception(argvars);
1313}
1314
1315/*
1316 * "assert_fails(cmd [, error])" function
1317 */
1318 static void
1319f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1320{
1321 assert_fails(argvars);
1322}
1323
1324/*
1325 * "assert_false(actual[, msg])" function
1326 */
1327 static void
1328f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1329{
1330 assert_bool(argvars, FALSE);
1331}
1332
1333/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001334 * "assert_inrange(lower, upper[, msg])" function
1335 */
1336 static void
1337f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1338{
1339 assert_inrange(argvars);
1340}
1341
1342/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001343 * "assert_match(pattern, actual[, msg])" function
1344 */
1345 static void
1346f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1347{
1348 assert_match_common(argvars, ASSERT_MATCH);
1349}
1350
1351/*
1352 * "assert_notmatch(pattern, actual[, msg])" function
1353 */
1354 static void
1355f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1356{
1357 assert_match_common(argvars, ASSERT_NOTMATCH);
1358}
1359
1360/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001361 * "assert_report(msg)" function
1362 */
1363 static void
1364f_assert_report(typval_T *argvars, typval_T *rettv UNUSED)
1365{
1366 assert_report(argvars);
1367}
1368
1369/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001370 * "assert_true(actual[, msg])" function
1371 */
1372 static void
1373f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1374{
1375 assert_bool(argvars, TRUE);
1376}
1377
1378#ifdef FEAT_FLOAT
1379/*
1380 * "asin()" function
1381 */
1382 static void
1383f_asin(typval_T *argvars, typval_T *rettv)
1384{
1385 float_T f = 0.0;
1386
1387 rettv->v_type = VAR_FLOAT;
1388 if (get_float_arg(argvars, &f) == OK)
1389 rettv->vval.v_float = asin(f);
1390 else
1391 rettv->vval.v_float = 0.0;
1392}
1393
1394/*
1395 * "atan()" function
1396 */
1397 static void
1398f_atan(typval_T *argvars, typval_T *rettv)
1399{
1400 float_T f = 0.0;
1401
1402 rettv->v_type = VAR_FLOAT;
1403 if (get_float_arg(argvars, &f) == OK)
1404 rettv->vval.v_float = atan(f);
1405 else
1406 rettv->vval.v_float = 0.0;
1407}
1408
1409/*
1410 * "atan2()" function
1411 */
1412 static void
1413f_atan2(typval_T *argvars, typval_T *rettv)
1414{
1415 float_T fx = 0.0, fy = 0.0;
1416
1417 rettv->v_type = VAR_FLOAT;
1418 if (get_float_arg(argvars, &fx) == OK
1419 && get_float_arg(&argvars[1], &fy) == OK)
1420 rettv->vval.v_float = atan2(fx, fy);
1421 else
1422 rettv->vval.v_float = 0.0;
1423}
1424#endif
1425
1426/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001427 * "balloon_show()" function
1428 */
1429#ifdef FEAT_BEVAL
1430 static void
1431f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1432{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001433 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001434 {
1435 if (argvars[0].v_type == VAR_LIST
1436# ifdef FEAT_GUI
1437 && !gui.in_use
1438# endif
1439 )
1440 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1441 else
1442 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1443 }
1444}
1445
Bram Moolenaar669a8282017-11-19 20:13:05 +01001446# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001447 static void
1448f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1449{
1450 if (rettv_list_alloc(rettv) == OK)
1451 {
1452 char_u *msg = get_tv_string_chk(&argvars[0]);
1453
1454 if (msg != NULL)
1455 {
1456 pumitem_T *array;
1457 int size = split_message(msg, &array);
1458 int i;
1459
1460 /* Skip the first and last item, they are always empty. */
1461 for (i = 1; i < size - 1; ++i)
1462 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001463 while (size > 0)
1464 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001465 vim_free(array);
1466 }
1467 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001468}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001469# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001470#endif
1471
1472/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001473 * "browse(save, title, initdir, default)" function
1474 */
1475 static void
1476f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1477{
1478#ifdef FEAT_BROWSE
1479 int save;
1480 char_u *title;
1481 char_u *initdir;
1482 char_u *defname;
1483 char_u buf[NUMBUFLEN];
1484 char_u buf2[NUMBUFLEN];
1485 int error = FALSE;
1486
1487 save = (int)get_tv_number_chk(&argvars[0], &error);
1488 title = get_tv_string_chk(&argvars[1]);
1489 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1490 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1491
1492 if (error || title == NULL || initdir == NULL || defname == NULL)
1493 rettv->vval.v_string = NULL;
1494 else
1495 rettv->vval.v_string =
1496 do_browse(save ? BROWSE_SAVE : 0,
1497 title, defname, NULL, initdir, NULL, curbuf);
1498#else
1499 rettv->vval.v_string = NULL;
1500#endif
1501 rettv->v_type = VAR_STRING;
1502}
1503
1504/*
1505 * "browsedir(title, initdir)" function
1506 */
1507 static void
1508f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1509{
1510#ifdef FEAT_BROWSE
1511 char_u *title;
1512 char_u *initdir;
1513 char_u buf[NUMBUFLEN];
1514
1515 title = get_tv_string_chk(&argvars[0]);
1516 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1517
1518 if (title == NULL || initdir == NULL)
1519 rettv->vval.v_string = NULL;
1520 else
1521 rettv->vval.v_string = do_browse(BROWSE_DIR,
1522 title, NULL, NULL, initdir, NULL, curbuf);
1523#else
1524 rettv->vval.v_string = NULL;
1525#endif
1526 rettv->v_type = VAR_STRING;
1527}
1528
1529static buf_T *find_buffer(typval_T *avar);
1530
1531/*
1532 * Find a buffer by number or exact name.
1533 */
1534 static buf_T *
1535find_buffer(typval_T *avar)
1536{
1537 buf_T *buf = NULL;
1538
1539 if (avar->v_type == VAR_NUMBER)
1540 buf = buflist_findnr((int)avar->vval.v_number);
1541 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1542 {
1543 buf = buflist_findname_exp(avar->vval.v_string);
1544 if (buf == NULL)
1545 {
1546 /* No full path name match, try a match with a URL or a "nofile"
1547 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001548 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001549 if (buf->b_fname != NULL
1550 && (path_with_url(buf->b_fname)
1551#ifdef FEAT_QUICKFIX
1552 || bt_nofile(buf)
1553#endif
1554 )
1555 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1556 break;
1557 }
1558 }
1559 return buf;
1560}
1561
1562/*
1563 * "bufexists(expr)" function
1564 */
1565 static void
1566f_bufexists(typval_T *argvars, typval_T *rettv)
1567{
1568 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1569}
1570
1571/*
1572 * "buflisted(expr)" function
1573 */
1574 static void
1575f_buflisted(typval_T *argvars, typval_T *rettv)
1576{
1577 buf_T *buf;
1578
1579 buf = find_buffer(&argvars[0]);
1580 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1581}
1582
1583/*
1584 * "bufloaded(expr)" function
1585 */
1586 static void
1587f_bufloaded(typval_T *argvars, typval_T *rettv)
1588{
1589 buf_T *buf;
1590
1591 buf = find_buffer(&argvars[0]);
1592 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1593}
1594
1595 buf_T *
1596buflist_find_by_name(char_u *name, int curtab_only)
1597{
1598 int save_magic;
1599 char_u *save_cpo;
1600 buf_T *buf;
1601
1602 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1603 save_magic = p_magic;
1604 p_magic = TRUE;
1605 save_cpo = p_cpo;
1606 p_cpo = (char_u *)"";
1607
1608 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1609 TRUE, FALSE, curtab_only));
1610
1611 p_magic = save_magic;
1612 p_cpo = save_cpo;
1613 return buf;
1614}
1615
1616/*
1617 * Get buffer by number or pattern.
1618 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001619 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001620get_buf_tv(typval_T *tv, int curtab_only)
1621{
1622 char_u *name = tv->vval.v_string;
1623 buf_T *buf;
1624
1625 if (tv->v_type == VAR_NUMBER)
1626 return buflist_findnr((int)tv->vval.v_number);
1627 if (tv->v_type != VAR_STRING)
1628 return NULL;
1629 if (name == NULL || *name == NUL)
1630 return curbuf;
1631 if (name[0] == '$' && name[1] == NUL)
1632 return lastbuf;
1633
1634 buf = buflist_find_by_name(name, curtab_only);
1635
1636 /* If not found, try expanding the name, like done for bufexists(). */
1637 if (buf == NULL)
1638 buf = find_buffer(tv);
1639
1640 return buf;
1641}
1642
1643/*
1644 * "bufname(expr)" function
1645 */
1646 static void
1647f_bufname(typval_T *argvars, typval_T *rettv)
1648{
1649 buf_T *buf;
1650
1651 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1652 ++emsg_off;
1653 buf = get_buf_tv(&argvars[0], FALSE);
1654 rettv->v_type = VAR_STRING;
1655 if (buf != NULL && buf->b_fname != NULL)
1656 rettv->vval.v_string = vim_strsave(buf->b_fname);
1657 else
1658 rettv->vval.v_string = NULL;
1659 --emsg_off;
1660}
1661
1662/*
1663 * "bufnr(expr)" function
1664 */
1665 static void
1666f_bufnr(typval_T *argvars, typval_T *rettv)
1667{
1668 buf_T *buf;
1669 int error = FALSE;
1670 char_u *name;
1671
1672 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1673 ++emsg_off;
1674 buf = get_buf_tv(&argvars[0], FALSE);
1675 --emsg_off;
1676
1677 /* If the buffer isn't found and the second argument is not zero create a
1678 * new buffer. */
1679 if (buf == NULL
1680 && argvars[1].v_type != VAR_UNKNOWN
1681 && get_tv_number_chk(&argvars[1], &error) != 0
1682 && !error
1683 && (name = get_tv_string_chk(&argvars[0])) != NULL
1684 && !error)
1685 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1686
1687 if (buf != NULL)
1688 rettv->vval.v_number = buf->b_fnum;
1689 else
1690 rettv->vval.v_number = -1;
1691}
1692
1693 static void
1694buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1695{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001696 win_T *wp;
1697 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001698 buf_T *buf;
1699
1700 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1701 ++emsg_off;
1702 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001703 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001704 {
1705 ++winnr;
1706 if (wp->w_buffer == buf)
1707 break;
1708 }
1709 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001710 --emsg_off;
1711}
1712
1713/*
1714 * "bufwinid(nr)" function
1715 */
1716 static void
1717f_bufwinid(typval_T *argvars, typval_T *rettv)
1718{
1719 buf_win_common(argvars, rettv, FALSE);
1720}
1721
1722/*
1723 * "bufwinnr(nr)" function
1724 */
1725 static void
1726f_bufwinnr(typval_T *argvars, typval_T *rettv)
1727{
1728 buf_win_common(argvars, rettv, TRUE);
1729}
1730
1731/*
1732 * "byte2line(byte)" function
1733 */
1734 static void
1735f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1736{
1737#ifndef FEAT_BYTEOFF
1738 rettv->vval.v_number = -1;
1739#else
1740 long boff = 0;
1741
1742 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1743 if (boff < 0)
1744 rettv->vval.v_number = -1;
1745 else
1746 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1747 (linenr_T)0, &boff);
1748#endif
1749}
1750
1751 static void
1752byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1753{
1754#ifdef FEAT_MBYTE
1755 char_u *t;
1756#endif
1757 char_u *str;
1758 varnumber_T idx;
1759
1760 str = get_tv_string_chk(&argvars[0]);
1761 idx = get_tv_number_chk(&argvars[1], NULL);
1762 rettv->vval.v_number = -1;
1763 if (str == NULL || idx < 0)
1764 return;
1765
1766#ifdef FEAT_MBYTE
1767 t = str;
1768 for ( ; idx > 0; idx--)
1769 {
1770 if (*t == NUL) /* EOL reached */
1771 return;
1772 if (enc_utf8 && comp)
1773 t += utf_ptr2len(t);
1774 else
1775 t += (*mb_ptr2len)(t);
1776 }
1777 rettv->vval.v_number = (varnumber_T)(t - str);
1778#else
1779 if ((size_t)idx <= STRLEN(str))
1780 rettv->vval.v_number = idx;
1781#endif
1782}
1783
1784/*
1785 * "byteidx()" function
1786 */
1787 static void
1788f_byteidx(typval_T *argvars, typval_T *rettv)
1789{
1790 byteidx(argvars, rettv, FALSE);
1791}
1792
1793/*
1794 * "byteidxcomp()" function
1795 */
1796 static void
1797f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1798{
1799 byteidx(argvars, rettv, TRUE);
1800}
1801
1802/*
1803 * "call(func, arglist [, dict])" function
1804 */
1805 static void
1806f_call(typval_T *argvars, typval_T *rettv)
1807{
1808 char_u *func;
1809 partial_T *partial = NULL;
1810 dict_T *selfdict = NULL;
1811
1812 if (argvars[1].v_type != VAR_LIST)
1813 {
1814 EMSG(_(e_listreq));
1815 return;
1816 }
1817 if (argvars[1].vval.v_list == NULL)
1818 return;
1819
1820 if (argvars[0].v_type == VAR_FUNC)
1821 func = argvars[0].vval.v_string;
1822 else if (argvars[0].v_type == VAR_PARTIAL)
1823 {
1824 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001825 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001826 }
1827 else
1828 func = get_tv_string(&argvars[0]);
1829 if (*func == NUL)
1830 return; /* type error or empty name */
1831
1832 if (argvars[2].v_type != VAR_UNKNOWN)
1833 {
1834 if (argvars[2].v_type != VAR_DICT)
1835 {
1836 EMSG(_(e_dictreq));
1837 return;
1838 }
1839 selfdict = argvars[2].vval.v_dict;
1840 }
1841
1842 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1843}
1844
1845#ifdef FEAT_FLOAT
1846/*
1847 * "ceil({float})" function
1848 */
1849 static void
1850f_ceil(typval_T *argvars, typval_T *rettv)
1851{
1852 float_T f = 0.0;
1853
1854 rettv->v_type = VAR_FLOAT;
1855 if (get_float_arg(argvars, &f) == OK)
1856 rettv->vval.v_float = ceil(f);
1857 else
1858 rettv->vval.v_float = 0.0;
1859}
1860#endif
1861
1862#ifdef FEAT_JOB_CHANNEL
1863/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001864 * "ch_canread()" function
1865 */
1866 static void
1867f_ch_canread(typval_T *argvars, typval_T *rettv)
1868{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001869 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001870
1871 rettv->vval.v_number = 0;
1872 if (channel != NULL)
1873 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1874 || channel_has_readahead(channel, PART_OUT)
1875 || channel_has_readahead(channel, PART_ERR);
1876}
1877
1878/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001879 * "ch_close()" function
1880 */
1881 static void
1882f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1883{
1884 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1885
1886 if (channel != NULL)
1887 {
1888 channel_close(channel, FALSE);
1889 channel_clear(channel);
1890 }
1891}
1892
1893/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001894 * "ch_close()" function
1895 */
1896 static void
1897f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1898{
1899 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1900
1901 if (channel != NULL)
1902 channel_close_in(channel);
1903}
1904
1905/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001906 * "ch_getbufnr()" function
1907 */
1908 static void
1909f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1910{
1911 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1912
1913 rettv->vval.v_number = -1;
1914 if (channel != NULL)
1915 {
1916 char_u *what = get_tv_string(&argvars[1]);
1917 int part;
1918
1919 if (STRCMP(what, "err") == 0)
1920 part = PART_ERR;
1921 else if (STRCMP(what, "out") == 0)
1922 part = PART_OUT;
1923 else if (STRCMP(what, "in") == 0)
1924 part = PART_IN;
1925 else
1926 part = PART_SOCK;
1927 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1928 rettv->vval.v_number =
1929 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1930 }
1931}
1932
1933/*
1934 * "ch_getjob()" function
1935 */
1936 static void
1937f_ch_getjob(typval_T *argvars, typval_T *rettv)
1938{
1939 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1940
1941 if (channel != NULL)
1942 {
1943 rettv->v_type = VAR_JOB;
1944 rettv->vval.v_job = channel->ch_job;
1945 if (channel->ch_job != NULL)
1946 ++channel->ch_job->jv_refcount;
1947 }
1948}
1949
1950/*
1951 * "ch_info()" function
1952 */
1953 static void
1954f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1955{
1956 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1957
1958 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1959 channel_info(channel, rettv->vval.v_dict);
1960}
1961
1962/*
1963 * "ch_log()" function
1964 */
1965 static void
1966f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1967{
1968 char_u *msg = get_tv_string(&argvars[0]);
1969 channel_T *channel = NULL;
1970
1971 if (argvars[1].v_type != VAR_UNKNOWN)
1972 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1973
1974 ch_log(channel, (char *)msg);
1975}
1976
1977/*
1978 * "ch_logfile()" function
1979 */
1980 static void
1981f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
1982{
1983 char_u *fname;
1984 char_u *opt = (char_u *)"";
1985 char_u buf[NUMBUFLEN];
1986
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02001987 /* Don't open a file in restricted mode. */
1988 if (check_restricted() || check_secure())
1989 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001990 fname = get_tv_string(&argvars[0]);
1991 if (argvars[1].v_type == VAR_STRING)
1992 opt = get_tv_string_buf(&argvars[1], buf);
1993 ch_logfile(fname, opt);
1994}
1995
1996/*
1997 * "ch_open()" function
1998 */
1999 static void
2000f_ch_open(typval_T *argvars, typval_T *rettv)
2001{
2002 rettv->v_type = VAR_CHANNEL;
2003 if (check_restricted() || check_secure())
2004 return;
2005 rettv->vval.v_channel = channel_open_func(argvars);
2006}
2007
2008/*
2009 * "ch_read()" function
2010 */
2011 static void
2012f_ch_read(typval_T *argvars, typval_T *rettv)
2013{
2014 common_channel_read(argvars, rettv, FALSE);
2015}
2016
2017/*
2018 * "ch_readraw()" function
2019 */
2020 static void
2021f_ch_readraw(typval_T *argvars, typval_T *rettv)
2022{
2023 common_channel_read(argvars, rettv, TRUE);
2024}
2025
2026/*
2027 * "ch_evalexpr()" function
2028 */
2029 static void
2030f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2031{
2032 ch_expr_common(argvars, rettv, TRUE);
2033}
2034
2035/*
2036 * "ch_sendexpr()" function
2037 */
2038 static void
2039f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2040{
2041 ch_expr_common(argvars, rettv, FALSE);
2042}
2043
2044/*
2045 * "ch_evalraw()" function
2046 */
2047 static void
2048f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2049{
2050 ch_raw_common(argvars, rettv, TRUE);
2051}
2052
2053/*
2054 * "ch_sendraw()" function
2055 */
2056 static void
2057f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2058{
2059 ch_raw_common(argvars, rettv, FALSE);
2060}
2061
2062/*
2063 * "ch_setoptions()" function
2064 */
2065 static void
2066f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2067{
2068 channel_T *channel;
2069 jobopt_T opt;
2070
2071 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2072 if (channel == NULL)
2073 return;
2074 clear_job_options(&opt);
2075 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002076 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002077 channel_set_options(channel, &opt);
2078 free_job_options(&opt);
2079}
2080
2081/*
2082 * "ch_status()" function
2083 */
2084 static void
2085f_ch_status(typval_T *argvars, typval_T *rettv)
2086{
2087 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002088 jobopt_T opt;
2089 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002090
2091 /* return an empty string by default */
2092 rettv->v_type = VAR_STRING;
2093 rettv->vval.v_string = NULL;
2094
2095 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002096
2097 if (argvars[1].v_type != VAR_UNKNOWN)
2098 {
2099 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002100 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002101 && (opt.jo_set & JO_PART))
2102 part = opt.jo_part;
2103 }
2104
2105 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002106}
2107#endif
2108
2109/*
2110 * "changenr()" function
2111 */
2112 static void
2113f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2114{
2115 rettv->vval.v_number = curbuf->b_u_seq_cur;
2116}
2117
2118/*
2119 * "char2nr(string)" function
2120 */
2121 static void
2122f_char2nr(typval_T *argvars, typval_T *rettv)
2123{
2124#ifdef FEAT_MBYTE
2125 if (has_mbyte)
2126 {
2127 int utf8 = 0;
2128
2129 if (argvars[1].v_type != VAR_UNKNOWN)
2130 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2131
2132 if (utf8)
2133 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2134 else
2135 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2136 }
2137 else
2138#endif
2139 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2140}
2141
2142/*
2143 * "cindent(lnum)" function
2144 */
2145 static void
2146f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2147{
2148#ifdef FEAT_CINDENT
2149 pos_T pos;
2150 linenr_T lnum;
2151
2152 pos = curwin->w_cursor;
2153 lnum = get_tv_lnum(argvars);
2154 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2155 {
2156 curwin->w_cursor.lnum = lnum;
2157 rettv->vval.v_number = get_c_indent();
2158 curwin->w_cursor = pos;
2159 }
2160 else
2161#endif
2162 rettv->vval.v_number = -1;
2163}
2164
2165/*
2166 * "clearmatches()" function
2167 */
2168 static void
2169f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2170{
2171#ifdef FEAT_SEARCH_EXTRA
2172 clear_matches(curwin);
2173#endif
2174}
2175
2176/*
2177 * "col(string)" function
2178 */
2179 static void
2180f_col(typval_T *argvars, typval_T *rettv)
2181{
2182 colnr_T col = 0;
2183 pos_T *fp;
2184 int fnum = curbuf->b_fnum;
2185
2186 fp = var2fpos(&argvars[0], FALSE, &fnum);
2187 if (fp != NULL && fnum == curbuf->b_fnum)
2188 {
2189 if (fp->col == MAXCOL)
2190 {
2191 /* '> can be MAXCOL, get the length of the line then */
2192 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2193 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2194 else
2195 col = MAXCOL;
2196 }
2197 else
2198 {
2199 col = fp->col + 1;
2200#ifdef FEAT_VIRTUALEDIT
2201 /* col(".") when the cursor is on the NUL at the end of the line
2202 * because of "coladd" can be seen as an extra column. */
2203 if (virtual_active() && fp == &curwin->w_cursor)
2204 {
2205 char_u *p = ml_get_cursor();
2206
2207 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2208 curwin->w_virtcol - curwin->w_cursor.coladd))
2209 {
2210# ifdef FEAT_MBYTE
2211 int l;
2212
2213 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2214 col += l;
2215# else
2216 if (*p != NUL && p[1] == NUL)
2217 ++col;
2218# endif
2219 }
2220 }
2221#endif
2222 }
2223 }
2224 rettv->vval.v_number = col;
2225}
2226
2227#if defined(FEAT_INS_EXPAND)
2228/*
2229 * "complete()" function
2230 */
2231 static void
2232f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2233{
2234 int startcol;
2235
2236 if ((State & INSERT) == 0)
2237 {
2238 EMSG(_("E785: complete() can only be used in Insert mode"));
2239 return;
2240 }
2241
2242 /* Check for undo allowed here, because if something was already inserted
2243 * the line was already saved for undo and this check isn't done. */
2244 if (!undo_allowed())
2245 return;
2246
2247 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2248 {
2249 EMSG(_(e_invarg));
2250 return;
2251 }
2252
2253 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2254 if (startcol <= 0)
2255 return;
2256
2257 set_completion(startcol - 1, argvars[1].vval.v_list);
2258}
2259
2260/*
2261 * "complete_add()" function
2262 */
2263 static void
2264f_complete_add(typval_T *argvars, typval_T *rettv)
2265{
2266 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2267}
2268
2269/*
2270 * "complete_check()" function
2271 */
2272 static void
2273f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2274{
2275 int saved = RedrawingDisabled;
2276
2277 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002278 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002279 rettv->vval.v_number = compl_interrupted;
2280 RedrawingDisabled = saved;
2281}
2282#endif
2283
2284/*
2285 * "confirm(message, buttons[, default [, type]])" function
2286 */
2287 static void
2288f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2289{
2290#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2291 char_u *message;
2292 char_u *buttons = NULL;
2293 char_u buf[NUMBUFLEN];
2294 char_u buf2[NUMBUFLEN];
2295 int def = 1;
2296 int type = VIM_GENERIC;
2297 char_u *typestr;
2298 int error = FALSE;
2299
2300 message = get_tv_string_chk(&argvars[0]);
2301 if (message == NULL)
2302 error = TRUE;
2303 if (argvars[1].v_type != VAR_UNKNOWN)
2304 {
2305 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2306 if (buttons == NULL)
2307 error = TRUE;
2308 if (argvars[2].v_type != VAR_UNKNOWN)
2309 {
2310 def = (int)get_tv_number_chk(&argvars[2], &error);
2311 if (argvars[3].v_type != VAR_UNKNOWN)
2312 {
2313 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2314 if (typestr == NULL)
2315 error = TRUE;
2316 else
2317 {
2318 switch (TOUPPER_ASC(*typestr))
2319 {
2320 case 'E': type = VIM_ERROR; break;
2321 case 'Q': type = VIM_QUESTION; break;
2322 case 'I': type = VIM_INFO; break;
2323 case 'W': type = VIM_WARNING; break;
2324 case 'G': type = VIM_GENERIC; break;
2325 }
2326 }
2327 }
2328 }
2329 }
2330
2331 if (buttons == NULL || *buttons == NUL)
2332 buttons = (char_u *)_("&Ok");
2333
2334 if (!error)
2335 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2336 def, NULL, FALSE);
2337#endif
2338}
2339
2340/*
2341 * "copy()" function
2342 */
2343 static void
2344f_copy(typval_T *argvars, typval_T *rettv)
2345{
2346 item_copy(&argvars[0], rettv, FALSE, 0);
2347}
2348
2349#ifdef FEAT_FLOAT
2350/*
2351 * "cos()" function
2352 */
2353 static void
2354f_cos(typval_T *argvars, typval_T *rettv)
2355{
2356 float_T f = 0.0;
2357
2358 rettv->v_type = VAR_FLOAT;
2359 if (get_float_arg(argvars, &f) == OK)
2360 rettv->vval.v_float = cos(f);
2361 else
2362 rettv->vval.v_float = 0.0;
2363}
2364
2365/*
2366 * "cosh()" function
2367 */
2368 static void
2369f_cosh(typval_T *argvars, typval_T *rettv)
2370{
2371 float_T f = 0.0;
2372
2373 rettv->v_type = VAR_FLOAT;
2374 if (get_float_arg(argvars, &f) == OK)
2375 rettv->vval.v_float = cosh(f);
2376 else
2377 rettv->vval.v_float = 0.0;
2378}
2379#endif
2380
2381/*
2382 * "count()" function
2383 */
2384 static void
2385f_count(typval_T *argvars, typval_T *rettv)
2386{
2387 long n = 0;
2388 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002389 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002390
Bram Moolenaar9966b212017-07-28 16:46:57 +02002391 if (argvars[2].v_type != VAR_UNKNOWN)
2392 ic = (int)get_tv_number_chk(&argvars[2], &error);
2393
2394 if (argvars[0].v_type == VAR_STRING)
2395 {
2396 char_u *expr = get_tv_string_chk(&argvars[1]);
2397 char_u *p = argvars[0].vval.v_string;
2398 char_u *next;
2399
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002400 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002401 {
2402 if (ic)
2403 {
2404 size_t len = STRLEN(expr);
2405
2406 while (*p != NUL)
2407 {
2408 if (MB_STRNICMP(p, expr, len) == 0)
2409 {
2410 ++n;
2411 p += len;
2412 }
2413 else
2414 MB_PTR_ADV(p);
2415 }
2416 }
2417 else
2418 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2419 != NULL)
2420 {
2421 ++n;
2422 p = next + STRLEN(expr);
2423 }
2424 }
2425
2426 }
2427 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002428 {
2429 listitem_T *li;
2430 list_T *l;
2431 long idx;
2432
2433 if ((l = argvars[0].vval.v_list) != NULL)
2434 {
2435 li = l->lv_first;
2436 if (argvars[2].v_type != VAR_UNKNOWN)
2437 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002438 if (argvars[3].v_type != VAR_UNKNOWN)
2439 {
2440 idx = (long)get_tv_number_chk(&argvars[3], &error);
2441 if (!error)
2442 {
2443 li = list_find(l, idx);
2444 if (li == NULL)
2445 EMSGN(_(e_listidx), idx);
2446 }
2447 }
2448 if (error)
2449 li = NULL;
2450 }
2451
2452 for ( ; li != NULL; li = li->li_next)
2453 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2454 ++n;
2455 }
2456 }
2457 else if (argvars[0].v_type == VAR_DICT)
2458 {
2459 int todo;
2460 dict_T *d;
2461 hashitem_T *hi;
2462
2463 if ((d = argvars[0].vval.v_dict) != NULL)
2464 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002465 if (argvars[2].v_type != VAR_UNKNOWN)
2466 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002467 if (argvars[3].v_type != VAR_UNKNOWN)
2468 EMSG(_(e_invarg));
2469 }
2470
2471 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2472 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2473 {
2474 if (!HASHITEM_EMPTY(hi))
2475 {
2476 --todo;
2477 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2478 ++n;
2479 }
2480 }
2481 }
2482 }
2483 else
2484 EMSG2(_(e_listdictarg), "count()");
2485 rettv->vval.v_number = n;
2486}
2487
2488/*
2489 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2490 *
2491 * Checks the existence of a cscope connection.
2492 */
2493 static void
2494f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2495{
2496#ifdef FEAT_CSCOPE
2497 int num = 0;
2498 char_u *dbpath = NULL;
2499 char_u *prepend = NULL;
2500 char_u buf[NUMBUFLEN];
2501
2502 if (argvars[0].v_type != VAR_UNKNOWN
2503 && argvars[1].v_type != VAR_UNKNOWN)
2504 {
2505 num = (int)get_tv_number(&argvars[0]);
2506 dbpath = get_tv_string(&argvars[1]);
2507 if (argvars[2].v_type != VAR_UNKNOWN)
2508 prepend = get_tv_string_buf(&argvars[2], buf);
2509 }
2510
2511 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2512#endif
2513}
2514
2515/*
2516 * "cursor(lnum, col)" function, or
2517 * "cursor(list)"
2518 *
2519 * Moves the cursor to the specified line and column.
2520 * Returns 0 when the position could be set, -1 otherwise.
2521 */
2522 static void
2523f_cursor(typval_T *argvars, typval_T *rettv)
2524{
2525 long line, col;
2526#ifdef FEAT_VIRTUALEDIT
2527 long coladd = 0;
2528#endif
2529 int set_curswant = TRUE;
2530
2531 rettv->vval.v_number = -1;
2532 if (argvars[1].v_type == VAR_UNKNOWN)
2533 {
2534 pos_T pos;
2535 colnr_T curswant = -1;
2536
2537 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2538 {
2539 EMSG(_(e_invarg));
2540 return;
2541 }
2542 line = pos.lnum;
2543 col = pos.col;
2544#ifdef FEAT_VIRTUALEDIT
2545 coladd = pos.coladd;
2546#endif
2547 if (curswant >= 0)
2548 {
2549 curwin->w_curswant = curswant - 1;
2550 set_curswant = FALSE;
2551 }
2552 }
2553 else
2554 {
2555 line = get_tv_lnum(argvars);
2556 col = (long)get_tv_number_chk(&argvars[1], NULL);
2557#ifdef FEAT_VIRTUALEDIT
2558 if (argvars[2].v_type != VAR_UNKNOWN)
2559 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2560#endif
2561 }
2562 if (line < 0 || col < 0
2563#ifdef FEAT_VIRTUALEDIT
2564 || coladd < 0
2565#endif
2566 )
2567 return; /* type error; errmsg already given */
2568 if (line > 0)
2569 curwin->w_cursor.lnum = line;
2570 if (col > 0)
2571 curwin->w_cursor.col = col - 1;
2572#ifdef FEAT_VIRTUALEDIT
2573 curwin->w_cursor.coladd = coladd;
2574#endif
2575
2576 /* Make sure the cursor is in a valid position. */
2577 check_cursor();
2578#ifdef FEAT_MBYTE
2579 /* Correct cursor for multi-byte character. */
2580 if (has_mbyte)
2581 mb_adjust_cursor();
2582#endif
2583
2584 curwin->w_set_curswant = set_curswant;
2585 rettv->vval.v_number = 0;
2586}
2587
2588/*
2589 * "deepcopy()" function
2590 */
2591 static void
2592f_deepcopy(typval_T *argvars, typval_T *rettv)
2593{
2594 int noref = 0;
2595 int copyID;
2596
2597 if (argvars[1].v_type != VAR_UNKNOWN)
2598 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2599 if (noref < 0 || noref > 1)
2600 EMSG(_(e_invarg));
2601 else
2602 {
2603 copyID = get_copyID();
2604 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2605 }
2606}
2607
2608/*
2609 * "delete()" function
2610 */
2611 static void
2612f_delete(typval_T *argvars, typval_T *rettv)
2613{
2614 char_u nbuf[NUMBUFLEN];
2615 char_u *name;
2616 char_u *flags;
2617
2618 rettv->vval.v_number = -1;
2619 if (check_restricted() || check_secure())
2620 return;
2621
2622 name = get_tv_string(&argvars[0]);
2623 if (name == NULL || *name == NUL)
2624 {
2625 EMSG(_(e_invarg));
2626 return;
2627 }
2628
2629 if (argvars[1].v_type != VAR_UNKNOWN)
2630 flags = get_tv_string_buf(&argvars[1], nbuf);
2631 else
2632 flags = (char_u *)"";
2633
2634 if (*flags == NUL)
2635 /* delete a file */
2636 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2637 else if (STRCMP(flags, "d") == 0)
2638 /* delete an empty directory */
2639 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2640 else if (STRCMP(flags, "rf") == 0)
2641 /* delete a directory recursively */
2642 rettv->vval.v_number = delete_recursive(name);
2643 else
2644 EMSG2(_(e_invexpr2), flags);
2645}
2646
2647/*
2648 * "did_filetype()" function
2649 */
2650 static void
2651f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2652{
2653#ifdef FEAT_AUTOCMD
2654 rettv->vval.v_number = did_filetype;
2655#endif
2656}
2657
2658/*
2659 * "diff_filler()" function
2660 */
2661 static void
2662f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2663{
2664#ifdef FEAT_DIFF
2665 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2666#endif
2667}
2668
2669/*
2670 * "diff_hlID()" function
2671 */
2672 static void
2673f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2674{
2675#ifdef FEAT_DIFF
2676 linenr_T lnum = get_tv_lnum(argvars);
2677 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002678 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002679 static int fnum = 0;
2680 static int change_start = 0;
2681 static int change_end = 0;
2682 static hlf_T hlID = (hlf_T)0;
2683 int filler_lines;
2684 int col;
2685
2686 if (lnum < 0) /* ignore type error in {lnum} arg */
2687 lnum = 0;
2688 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002689 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002690 || fnum != curbuf->b_fnum)
2691 {
2692 /* New line, buffer, change: need to get the values. */
2693 filler_lines = diff_check(curwin, lnum);
2694 if (filler_lines < 0)
2695 {
2696 if (filler_lines == -1)
2697 {
2698 change_start = MAXCOL;
2699 change_end = -1;
2700 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2701 hlID = HLF_ADD; /* added line */
2702 else
2703 hlID = HLF_CHD; /* changed line */
2704 }
2705 else
2706 hlID = HLF_ADD; /* added line */
2707 }
2708 else
2709 hlID = (hlf_T)0;
2710 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002711 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002712 fnum = curbuf->b_fnum;
2713 }
2714
2715 if (hlID == HLF_CHD || hlID == HLF_TXD)
2716 {
2717 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2718 if (col >= change_start && col <= change_end)
2719 hlID = HLF_TXD; /* changed text */
2720 else
2721 hlID = HLF_CHD; /* changed line */
2722 }
2723 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2724#endif
2725}
2726
2727/*
2728 * "empty({expr})" function
2729 */
2730 static void
2731f_empty(typval_T *argvars, typval_T *rettv)
2732{
2733 int n = FALSE;
2734
2735 switch (argvars[0].v_type)
2736 {
2737 case VAR_STRING:
2738 case VAR_FUNC:
2739 n = argvars[0].vval.v_string == NULL
2740 || *argvars[0].vval.v_string == NUL;
2741 break;
2742 case VAR_PARTIAL:
2743 n = FALSE;
2744 break;
2745 case VAR_NUMBER:
2746 n = argvars[0].vval.v_number == 0;
2747 break;
2748 case VAR_FLOAT:
2749#ifdef FEAT_FLOAT
2750 n = argvars[0].vval.v_float == 0.0;
2751 break;
2752#endif
2753 case VAR_LIST:
2754 n = argvars[0].vval.v_list == NULL
2755 || argvars[0].vval.v_list->lv_first == NULL;
2756 break;
2757 case VAR_DICT:
2758 n = argvars[0].vval.v_dict == NULL
2759 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2760 break;
2761 case VAR_SPECIAL:
2762 n = argvars[0].vval.v_number != VVAL_TRUE;
2763 break;
2764
2765 case VAR_JOB:
2766#ifdef FEAT_JOB_CHANNEL
2767 n = argvars[0].vval.v_job == NULL
2768 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2769 break;
2770#endif
2771 case VAR_CHANNEL:
2772#ifdef FEAT_JOB_CHANNEL
2773 n = argvars[0].vval.v_channel == NULL
2774 || !channel_is_open(argvars[0].vval.v_channel);
2775 break;
2776#endif
2777 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002778 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002779 n = TRUE;
2780 break;
2781 }
2782
2783 rettv->vval.v_number = n;
2784}
2785
2786/*
2787 * "escape({string}, {chars})" function
2788 */
2789 static void
2790f_escape(typval_T *argvars, typval_T *rettv)
2791{
2792 char_u buf[NUMBUFLEN];
2793
2794 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2795 get_tv_string_buf(&argvars[1], buf));
2796 rettv->v_type = VAR_STRING;
2797}
2798
2799/*
2800 * "eval()" function
2801 */
2802 static void
2803f_eval(typval_T *argvars, typval_T *rettv)
2804{
2805 char_u *s, *p;
2806
2807 s = get_tv_string_chk(&argvars[0]);
2808 if (s != NULL)
2809 s = skipwhite(s);
2810
2811 p = s;
2812 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2813 {
2814 if (p != NULL && !aborting())
2815 EMSG2(_(e_invexpr2), p);
2816 need_clr_eos = FALSE;
2817 rettv->v_type = VAR_NUMBER;
2818 rettv->vval.v_number = 0;
2819 }
2820 else if (*s != NUL)
2821 EMSG(_(e_trailing));
2822}
2823
2824/*
2825 * "eventhandler()" function
2826 */
2827 static void
2828f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2829{
2830 rettv->vval.v_number = vgetc_busy;
2831}
2832
2833/*
2834 * "executable()" function
2835 */
2836 static void
2837f_executable(typval_T *argvars, typval_T *rettv)
2838{
2839 char_u *name = get_tv_string(&argvars[0]);
2840
2841 /* Check in $PATH and also check directly if there is a directory name. */
2842 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2843 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2844}
2845
2846static garray_T redir_execute_ga;
2847
2848/*
2849 * Append "value[value_len]" to the execute() output.
2850 */
2851 void
2852execute_redir_str(char_u *value, int value_len)
2853{
2854 int len;
2855
2856 if (value_len == -1)
2857 len = (int)STRLEN(value); /* Append the entire string */
2858 else
2859 len = value_len; /* Append only "value_len" characters */
2860 if (ga_grow(&redir_execute_ga, len) == OK)
2861 {
2862 mch_memmove((char *)redir_execute_ga.ga_data
2863 + redir_execute_ga.ga_len, value, len);
2864 redir_execute_ga.ga_len += len;
2865 }
2866}
2867
2868/*
2869 * Get next line from a list.
2870 * Called by do_cmdline() to get the next line.
2871 * Returns allocated string, or NULL for end of function.
2872 */
2873
2874 static char_u *
2875get_list_line(
2876 int c UNUSED,
2877 void *cookie,
2878 int indent UNUSED)
2879{
2880 listitem_T **p = (listitem_T **)cookie;
2881 listitem_T *item = *p;
2882 char_u buf[NUMBUFLEN];
2883 char_u *s;
2884
2885 if (item == NULL)
2886 return NULL;
2887 s = get_tv_string_buf_chk(&item->li_tv, buf);
2888 *p = item->li_next;
2889 return s == NULL ? NULL : vim_strsave(s);
2890}
2891
2892/*
2893 * "execute()" function
2894 */
2895 static void
2896f_execute(typval_T *argvars, typval_T *rettv)
2897{
2898 char_u *cmd = NULL;
2899 list_T *list = NULL;
2900 int save_msg_silent = msg_silent;
2901 int save_emsg_silent = emsg_silent;
2902 int save_emsg_noredir = emsg_noredir;
2903 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002904 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002905 garray_T save_ga;
2906
2907 rettv->vval.v_string = NULL;
2908 rettv->v_type = VAR_STRING;
2909
2910 if (argvars[0].v_type == VAR_LIST)
2911 {
2912 list = argvars[0].vval.v_list;
2913 if (list == NULL || list->lv_first == NULL)
2914 /* empty list, no commands, empty output */
2915 return;
2916 ++list->lv_refcount;
2917 }
2918 else
2919 {
2920 cmd = get_tv_string_chk(&argvars[0]);
2921 if (cmd == NULL)
2922 return;
2923 }
2924
2925 if (argvars[1].v_type != VAR_UNKNOWN)
2926 {
2927 char_u buf[NUMBUFLEN];
2928 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2929
2930 if (s == NULL)
2931 return;
2932 if (STRNCMP(s, "silent", 6) == 0)
2933 ++msg_silent;
2934 if (STRCMP(s, "silent!") == 0)
2935 {
2936 emsg_silent = TRUE;
2937 emsg_noredir = TRUE;
2938 }
2939 }
2940 else
2941 ++msg_silent;
2942
2943 if (redir_execute)
2944 save_ga = redir_execute_ga;
2945 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2946 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002947 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002948
2949 if (cmd != NULL)
2950 do_cmdline_cmd(cmd);
2951 else
2952 {
2953 listitem_T *item = list->lv_first;
2954
2955 do_cmdline(NULL, get_list_line, (void *)&item,
2956 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2957 --list->lv_refcount;
2958 }
2959
Bram Moolenaard297f352017-01-29 20:31:21 +01002960 /* Need to append a NUL to the result. */
2961 if (ga_grow(&redir_execute_ga, 1) == OK)
2962 {
2963 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2964 rettv->vval.v_string = redir_execute_ga.ga_data;
2965 }
2966 else
2967 {
2968 ga_clear(&redir_execute_ga);
2969 rettv->vval.v_string = NULL;
2970 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971 msg_silent = save_msg_silent;
2972 emsg_silent = save_emsg_silent;
2973 emsg_noredir = save_emsg_noredir;
2974
2975 redir_execute = save_redir_execute;
2976 if (redir_execute)
2977 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002978 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002979
2980 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
2981 * line. Put it back in the first column. */
2982 msg_col = 0;
2983}
2984
2985/*
2986 * "exepath()" function
2987 */
2988 static void
2989f_exepath(typval_T *argvars, typval_T *rettv)
2990{
2991 char_u *p = NULL;
2992
2993 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
2994 rettv->v_type = VAR_STRING;
2995 rettv->vval.v_string = p;
2996}
2997
2998/*
2999 * "exists()" function
3000 */
3001 static void
3002f_exists(typval_T *argvars, typval_T *rettv)
3003{
3004 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003005 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003006
3007 p = get_tv_string(&argvars[0]);
3008 if (*p == '$') /* environment variable */
3009 {
3010 /* first try "normal" environment variables (fast) */
3011 if (mch_getenv(p + 1) != NULL)
3012 n = TRUE;
3013 else
3014 {
3015 /* try expanding things like $VIM and ${HOME} */
3016 p = expand_env_save(p);
3017 if (p != NULL && *p != '$')
3018 n = TRUE;
3019 vim_free(p);
3020 }
3021 }
3022 else if (*p == '&' || *p == '+') /* option */
3023 {
3024 n = (get_option_tv(&p, NULL, TRUE) == OK);
3025 if (*skipwhite(p) != NUL)
3026 n = FALSE; /* trailing garbage */
3027 }
3028 else if (*p == '*') /* internal or user defined function */
3029 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003030 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003031 }
3032 else if (*p == ':')
3033 {
3034 n = cmd_exists(p + 1);
3035 }
3036 else if (*p == '#')
3037 {
3038#ifdef FEAT_AUTOCMD
3039 if (p[1] == '#')
3040 n = autocmd_supported(p + 2);
3041 else
3042 n = au_exists(p + 1);
3043#endif
3044 }
3045 else /* internal variable */
3046 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003047 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003048 }
3049
3050 rettv->vval.v_number = n;
3051}
3052
3053#ifdef FEAT_FLOAT
3054/*
3055 * "exp()" function
3056 */
3057 static void
3058f_exp(typval_T *argvars, typval_T *rettv)
3059{
3060 float_T f = 0.0;
3061
3062 rettv->v_type = VAR_FLOAT;
3063 if (get_float_arg(argvars, &f) == OK)
3064 rettv->vval.v_float = exp(f);
3065 else
3066 rettv->vval.v_float = 0.0;
3067}
3068#endif
3069
3070/*
3071 * "expand()" function
3072 */
3073 static void
3074f_expand(typval_T *argvars, typval_T *rettv)
3075{
3076 char_u *s;
3077 int len;
3078 char_u *errormsg;
3079 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3080 expand_T xpc;
3081 int error = FALSE;
3082 char_u *result;
3083
3084 rettv->v_type = VAR_STRING;
3085 if (argvars[1].v_type != VAR_UNKNOWN
3086 && argvars[2].v_type != VAR_UNKNOWN
3087 && get_tv_number_chk(&argvars[2], &error)
3088 && !error)
3089 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003090 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003091 }
3092
3093 s = get_tv_string(&argvars[0]);
3094 if (*s == '%' || *s == '#' || *s == '<')
3095 {
3096 ++emsg_off;
3097 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3098 --emsg_off;
3099 if (rettv->v_type == VAR_LIST)
3100 {
3101 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3102 list_append_string(rettv->vval.v_list, result, -1);
3103 else
3104 vim_free(result);
3105 }
3106 else
3107 rettv->vval.v_string = result;
3108 }
3109 else
3110 {
3111 /* When the optional second argument is non-zero, don't remove matches
3112 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3113 if (argvars[1].v_type != VAR_UNKNOWN
3114 && get_tv_number_chk(&argvars[1], &error))
3115 options |= WILD_KEEP_ALL;
3116 if (!error)
3117 {
3118 ExpandInit(&xpc);
3119 xpc.xp_context = EXPAND_FILES;
3120 if (p_wic)
3121 options += WILD_ICASE;
3122 if (rettv->v_type == VAR_STRING)
3123 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3124 options, WILD_ALL);
3125 else if (rettv_list_alloc(rettv) != FAIL)
3126 {
3127 int i;
3128
3129 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3130 for (i = 0; i < xpc.xp_numfiles; i++)
3131 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3132 ExpandCleanup(&xpc);
3133 }
3134 }
3135 else
3136 rettv->vval.v_string = NULL;
3137 }
3138}
3139
3140/*
3141 * "extend(list, list [, idx])" function
3142 * "extend(dict, dict [, action])" function
3143 */
3144 static void
3145f_extend(typval_T *argvars, typval_T *rettv)
3146{
3147 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3148
3149 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3150 {
3151 list_T *l1, *l2;
3152 listitem_T *item;
3153 long before;
3154 int error = FALSE;
3155
3156 l1 = argvars[0].vval.v_list;
3157 l2 = argvars[1].vval.v_list;
3158 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3159 && l2 != NULL)
3160 {
3161 if (argvars[2].v_type != VAR_UNKNOWN)
3162 {
3163 before = (long)get_tv_number_chk(&argvars[2], &error);
3164 if (error)
3165 return; /* type error; errmsg already given */
3166
3167 if (before == l1->lv_len)
3168 item = NULL;
3169 else
3170 {
3171 item = list_find(l1, before);
3172 if (item == NULL)
3173 {
3174 EMSGN(_(e_listidx), before);
3175 return;
3176 }
3177 }
3178 }
3179 else
3180 item = NULL;
3181 list_extend(l1, l2, item);
3182
3183 copy_tv(&argvars[0], rettv);
3184 }
3185 }
3186 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3187 {
3188 dict_T *d1, *d2;
3189 char_u *action;
3190 int i;
3191
3192 d1 = argvars[0].vval.v_dict;
3193 d2 = argvars[1].vval.v_dict;
3194 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3195 && d2 != NULL)
3196 {
3197 /* Check the third argument. */
3198 if (argvars[2].v_type != VAR_UNKNOWN)
3199 {
3200 static char *(av[]) = {"keep", "force", "error"};
3201
3202 action = get_tv_string_chk(&argvars[2]);
3203 if (action == NULL)
3204 return; /* type error; errmsg already given */
3205 for (i = 0; i < 3; ++i)
3206 if (STRCMP(action, av[i]) == 0)
3207 break;
3208 if (i == 3)
3209 {
3210 EMSG2(_(e_invarg2), action);
3211 return;
3212 }
3213 }
3214 else
3215 action = (char_u *)"force";
3216
3217 dict_extend(d1, d2, action);
3218
3219 copy_tv(&argvars[0], rettv);
3220 }
3221 }
3222 else
3223 EMSG2(_(e_listdictarg), "extend()");
3224}
3225
3226/*
3227 * "feedkeys()" function
3228 */
3229 static void
3230f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3231{
3232 int remap = TRUE;
3233 int insert = FALSE;
3234 char_u *keys, *flags;
3235 char_u nbuf[NUMBUFLEN];
3236 int typed = FALSE;
3237 int execute = FALSE;
3238 int dangerous = FALSE;
3239 char_u *keys_esc;
3240
3241 /* This is not allowed in the sandbox. If the commands would still be
3242 * executed in the sandbox it would be OK, but it probably happens later,
3243 * when "sandbox" is no longer set. */
3244 if (check_secure())
3245 return;
3246
3247 keys = get_tv_string(&argvars[0]);
3248
3249 if (argvars[1].v_type != VAR_UNKNOWN)
3250 {
3251 flags = get_tv_string_buf(&argvars[1], nbuf);
3252 for ( ; *flags != NUL; ++flags)
3253 {
3254 switch (*flags)
3255 {
3256 case 'n': remap = FALSE; break;
3257 case 'm': remap = TRUE; break;
3258 case 't': typed = TRUE; break;
3259 case 'i': insert = TRUE; break;
3260 case 'x': execute = TRUE; break;
3261 case '!': dangerous = TRUE; break;
3262 }
3263 }
3264 }
3265
3266 if (*keys != NUL || execute)
3267 {
3268 /* Need to escape K_SPECIAL and CSI before putting the string in the
3269 * typeahead buffer. */
3270 keys_esc = vim_strsave_escape_csi(keys);
3271 if (keys_esc != NULL)
3272 {
3273 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3274 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3275 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003276 if (vgetc_busy
3277#ifdef FEAT_TIMERS
3278 || timer_busy
3279#endif
3280 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003281 typebuf_was_filled = TRUE;
3282 if (execute)
3283 {
3284 int save_msg_scroll = msg_scroll;
3285
3286 /* Avoid a 1 second delay when the keys start Insert mode. */
3287 msg_scroll = FALSE;
3288
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02003289#ifdef FEAT_TERMINAL
3290 if (term_use_loop())
3291 terminal_loop(FALSE);
3292 else
3293#endif
3294 {
3295 if (!dangerous)
3296 ++ex_normal_busy;
3297 exec_normal(TRUE);
3298 if (!dangerous)
3299 --ex_normal_busy;
3300 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003301 msg_scroll |= save_msg_scroll;
3302 }
3303 }
3304 }
3305}
3306
3307/*
3308 * "filereadable()" function
3309 */
3310 static void
3311f_filereadable(typval_T *argvars, typval_T *rettv)
3312{
3313 int fd;
3314 char_u *p;
3315 int n;
3316
3317#ifndef O_NONBLOCK
3318# define O_NONBLOCK 0
3319#endif
3320 p = get_tv_string(&argvars[0]);
3321 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3322 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3323 {
3324 n = TRUE;
3325 close(fd);
3326 }
3327 else
3328 n = FALSE;
3329
3330 rettv->vval.v_number = n;
3331}
3332
3333/*
3334 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3335 * rights to write into.
3336 */
3337 static void
3338f_filewritable(typval_T *argvars, typval_T *rettv)
3339{
3340 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3341}
3342
3343 static void
3344findfilendir(
3345 typval_T *argvars UNUSED,
3346 typval_T *rettv,
3347 int find_what UNUSED)
3348{
3349#ifdef FEAT_SEARCHPATH
3350 char_u *fname;
3351 char_u *fresult = NULL;
3352 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3353 char_u *p;
3354 char_u pathbuf[NUMBUFLEN];
3355 int count = 1;
3356 int first = TRUE;
3357 int error = FALSE;
3358#endif
3359
3360 rettv->vval.v_string = NULL;
3361 rettv->v_type = VAR_STRING;
3362
3363#ifdef FEAT_SEARCHPATH
3364 fname = get_tv_string(&argvars[0]);
3365
3366 if (argvars[1].v_type != VAR_UNKNOWN)
3367 {
3368 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3369 if (p == NULL)
3370 error = TRUE;
3371 else
3372 {
3373 if (*p != NUL)
3374 path = p;
3375
3376 if (argvars[2].v_type != VAR_UNKNOWN)
3377 count = (int)get_tv_number_chk(&argvars[2], &error);
3378 }
3379 }
3380
3381 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3382 error = TRUE;
3383
3384 if (*fname != NUL && !error)
3385 {
3386 do
3387 {
3388 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3389 vim_free(fresult);
3390 fresult = find_file_in_path_option(first ? fname : NULL,
3391 first ? (int)STRLEN(fname) : 0,
3392 0, first, path,
3393 find_what,
3394 curbuf->b_ffname,
3395 find_what == FINDFILE_DIR
3396 ? (char_u *)"" : curbuf->b_p_sua);
3397 first = FALSE;
3398
3399 if (fresult != NULL && rettv->v_type == VAR_LIST)
3400 list_append_string(rettv->vval.v_list, fresult, -1);
3401
3402 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3403 }
3404
3405 if (rettv->v_type == VAR_STRING)
3406 rettv->vval.v_string = fresult;
3407#endif
3408}
3409
3410/*
3411 * "filter()" function
3412 */
3413 static void
3414f_filter(typval_T *argvars, typval_T *rettv)
3415{
3416 filter_map(argvars, rettv, FALSE);
3417}
3418
3419/*
3420 * "finddir({fname}[, {path}[, {count}]])" function
3421 */
3422 static void
3423f_finddir(typval_T *argvars, typval_T *rettv)
3424{
3425 findfilendir(argvars, rettv, FINDFILE_DIR);
3426}
3427
3428/*
3429 * "findfile({fname}[, {path}[, {count}]])" function
3430 */
3431 static void
3432f_findfile(typval_T *argvars, typval_T *rettv)
3433{
3434 findfilendir(argvars, rettv, FINDFILE_FILE);
3435}
3436
3437#ifdef FEAT_FLOAT
3438/*
3439 * "float2nr({float})" function
3440 */
3441 static void
3442f_float2nr(typval_T *argvars, typval_T *rettv)
3443{
3444 float_T f = 0.0;
3445
3446 if (get_float_arg(argvars, &f) == OK)
3447 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003448 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003449 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003450 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003451 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003452 else
3453 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003454 }
3455}
3456
3457/*
3458 * "floor({float})" function
3459 */
3460 static void
3461f_floor(typval_T *argvars, typval_T *rettv)
3462{
3463 float_T f = 0.0;
3464
3465 rettv->v_type = VAR_FLOAT;
3466 if (get_float_arg(argvars, &f) == OK)
3467 rettv->vval.v_float = floor(f);
3468 else
3469 rettv->vval.v_float = 0.0;
3470}
3471
3472/*
3473 * "fmod()" function
3474 */
3475 static void
3476f_fmod(typval_T *argvars, typval_T *rettv)
3477{
3478 float_T fx = 0.0, fy = 0.0;
3479
3480 rettv->v_type = VAR_FLOAT;
3481 if (get_float_arg(argvars, &fx) == OK
3482 && get_float_arg(&argvars[1], &fy) == OK)
3483 rettv->vval.v_float = fmod(fx, fy);
3484 else
3485 rettv->vval.v_float = 0.0;
3486}
3487#endif
3488
3489/*
3490 * "fnameescape({string})" function
3491 */
3492 static void
3493f_fnameescape(typval_T *argvars, typval_T *rettv)
3494{
3495 rettv->vval.v_string = vim_strsave_fnameescape(
3496 get_tv_string(&argvars[0]), FALSE);
3497 rettv->v_type = VAR_STRING;
3498}
3499
3500/*
3501 * "fnamemodify({fname}, {mods})" function
3502 */
3503 static void
3504f_fnamemodify(typval_T *argvars, typval_T *rettv)
3505{
3506 char_u *fname;
3507 char_u *mods;
3508 int usedlen = 0;
3509 int len;
3510 char_u *fbuf = NULL;
3511 char_u buf[NUMBUFLEN];
3512
3513 fname = get_tv_string_chk(&argvars[0]);
3514 mods = get_tv_string_buf_chk(&argvars[1], buf);
3515 if (fname == NULL || mods == NULL)
3516 fname = NULL;
3517 else
3518 {
3519 len = (int)STRLEN(fname);
3520 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3521 }
3522
3523 rettv->v_type = VAR_STRING;
3524 if (fname == NULL)
3525 rettv->vval.v_string = NULL;
3526 else
3527 rettv->vval.v_string = vim_strnsave(fname, len);
3528 vim_free(fbuf);
3529}
3530
3531static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3532
3533/*
3534 * "foldclosed()" function
3535 */
3536 static void
3537foldclosed_both(
3538 typval_T *argvars UNUSED,
3539 typval_T *rettv,
3540 int end UNUSED)
3541{
3542#ifdef FEAT_FOLDING
3543 linenr_T lnum;
3544 linenr_T first, last;
3545
3546 lnum = get_tv_lnum(argvars);
3547 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3548 {
3549 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3550 {
3551 if (end)
3552 rettv->vval.v_number = (varnumber_T)last;
3553 else
3554 rettv->vval.v_number = (varnumber_T)first;
3555 return;
3556 }
3557 }
3558#endif
3559 rettv->vval.v_number = -1;
3560}
3561
3562/*
3563 * "foldclosed()" function
3564 */
3565 static void
3566f_foldclosed(typval_T *argvars, typval_T *rettv)
3567{
3568 foldclosed_both(argvars, rettv, FALSE);
3569}
3570
3571/*
3572 * "foldclosedend()" function
3573 */
3574 static void
3575f_foldclosedend(typval_T *argvars, typval_T *rettv)
3576{
3577 foldclosed_both(argvars, rettv, TRUE);
3578}
3579
3580/*
3581 * "foldlevel()" function
3582 */
3583 static void
3584f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3585{
3586#ifdef FEAT_FOLDING
3587 linenr_T lnum;
3588
3589 lnum = get_tv_lnum(argvars);
3590 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3591 rettv->vval.v_number = foldLevel(lnum);
3592#endif
3593}
3594
3595/*
3596 * "foldtext()" function
3597 */
3598 static void
3599f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3600{
3601#ifdef FEAT_FOLDING
3602 linenr_T foldstart;
3603 linenr_T foldend;
3604 char_u *dashes;
3605 linenr_T lnum;
3606 char_u *s;
3607 char_u *r;
3608 int len;
3609 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003610 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003611#endif
3612
3613 rettv->v_type = VAR_STRING;
3614 rettv->vval.v_string = NULL;
3615#ifdef FEAT_FOLDING
3616 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3617 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3618 dashes = get_vim_var_str(VV_FOLDDASHES);
3619 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3620 && dashes != NULL)
3621 {
3622 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003623 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003624 if (!linewhite(lnum))
3625 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003626
3627 /* Find interesting text in this line. */
3628 s = skipwhite(ml_get(lnum));
3629 /* skip C comment-start */
3630 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3631 {
3632 s = skipwhite(s + 2);
3633 if (*skipwhite(s) == NUL
3634 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3635 {
3636 s = skipwhite(ml_get(lnum + 1));
3637 if (*s == '*')
3638 s = skipwhite(s + 1);
3639 }
3640 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003641 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003642 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003643 r = alloc((unsigned)(STRLEN(txt)
3644 + STRLEN(dashes) /* for %s */
3645 + 20 /* for %3ld */
3646 + STRLEN(s))); /* concatenated */
3647 if (r != NULL)
3648 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003649 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003650 len = (int)STRLEN(r);
3651 STRCAT(r, s);
3652 /* remove 'foldmarker' and 'commentstring' */
3653 foldtext_cleanup(r + len);
3654 rettv->vval.v_string = r;
3655 }
3656 }
3657#endif
3658}
3659
3660/*
3661 * "foldtextresult(lnum)" function
3662 */
3663 static void
3664f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3665{
3666#ifdef FEAT_FOLDING
3667 linenr_T lnum;
3668 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003669 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003670 foldinfo_T foldinfo;
3671 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003672 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003673#endif
3674
3675 rettv->v_type = VAR_STRING;
3676 rettv->vval.v_string = NULL;
3677#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003678 if (entered)
3679 return; /* reject recursive use */
3680 entered = TRUE;
3681
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003682 lnum = get_tv_lnum(argvars);
3683 /* treat illegal types and illegal string values for {lnum} the same */
3684 if (lnum < 0)
3685 lnum = 0;
3686 fold_count = foldedCount(curwin, lnum, &foldinfo);
3687 if (fold_count > 0)
3688 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003689 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3690 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003691 if (text == buf)
3692 text = vim_strsave(text);
3693 rettv->vval.v_string = text;
3694 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003695
3696 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003697#endif
3698}
3699
3700/*
3701 * "foreground()" function
3702 */
3703 static void
3704f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3705{
3706#ifdef FEAT_GUI
3707 if (gui.in_use)
3708 gui_mch_set_foreground();
3709#else
3710# ifdef WIN32
3711 win32_set_foreground();
3712# endif
3713#endif
3714}
3715
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003716 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003717common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003718{
3719 char_u *s;
3720 char_u *name;
3721 int use_string = FALSE;
3722 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003723 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003724
3725 if (argvars[0].v_type == VAR_FUNC)
3726 {
3727 /* function(MyFunc, [arg], dict) */
3728 s = argvars[0].vval.v_string;
3729 }
3730 else if (argvars[0].v_type == VAR_PARTIAL
3731 && argvars[0].vval.v_partial != NULL)
3732 {
3733 /* function(dict.MyFunc, [arg]) */
3734 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003735 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003736 }
3737 else
3738 {
3739 /* function('MyFunc', [arg], dict) */
3740 s = get_tv_string(&argvars[0]);
3741 use_string = TRUE;
3742 }
3743
Bram Moolenaar843b8842016-08-21 14:36:15 +02003744 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003745 {
3746 name = s;
3747 trans_name = trans_function_name(&name, FALSE,
3748 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3749 if (*name != NUL)
3750 s = NULL;
3751 }
3752
Bram Moolenaar843b8842016-08-21 14:36:15 +02003753 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3754 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003755 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003756 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003757 else if (trans_name != NULL && (is_funcref
3758 ? find_func(trans_name) == NULL
3759 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003760 EMSG2(_("E700: Unknown function: %s"), s);
3761 else
3762 {
3763 int dict_idx = 0;
3764 int arg_idx = 0;
3765 list_T *list = NULL;
3766
3767 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3768 {
3769 char sid_buf[25];
3770 int off = *s == 's' ? 2 : 5;
3771
3772 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3773 * also be called from another script. Using trans_function_name()
3774 * would also work, but some plugins depend on the name being
3775 * printable text. */
3776 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3777 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3778 if (name != NULL)
3779 {
3780 STRCPY(name, sid_buf);
3781 STRCAT(name, s + off);
3782 }
3783 }
3784 else
3785 name = vim_strsave(s);
3786
3787 if (argvars[1].v_type != VAR_UNKNOWN)
3788 {
3789 if (argvars[2].v_type != VAR_UNKNOWN)
3790 {
3791 /* function(name, [args], dict) */
3792 arg_idx = 1;
3793 dict_idx = 2;
3794 }
3795 else if (argvars[1].v_type == VAR_DICT)
3796 /* function(name, dict) */
3797 dict_idx = 1;
3798 else
3799 /* function(name, [args]) */
3800 arg_idx = 1;
3801 if (dict_idx > 0)
3802 {
3803 if (argvars[dict_idx].v_type != VAR_DICT)
3804 {
3805 EMSG(_("E922: expected a dict"));
3806 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003807 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003808 }
3809 if (argvars[dict_idx].vval.v_dict == NULL)
3810 dict_idx = 0;
3811 }
3812 if (arg_idx > 0)
3813 {
3814 if (argvars[arg_idx].v_type != VAR_LIST)
3815 {
3816 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3817 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003818 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003819 }
3820 list = argvars[arg_idx].vval.v_list;
3821 if (list == NULL || list->lv_len == 0)
3822 arg_idx = 0;
3823 }
3824 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003825 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003826 {
3827 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3828
3829 /* result is a VAR_PARTIAL */
3830 if (pt == NULL)
3831 vim_free(name);
3832 else
3833 {
3834 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3835 {
3836 listitem_T *li;
3837 int i = 0;
3838 int arg_len = 0;
3839 int lv_len = 0;
3840
3841 if (arg_pt != NULL)
3842 arg_len = arg_pt->pt_argc;
3843 if (list != NULL)
3844 lv_len = list->lv_len;
3845 pt->pt_argc = arg_len + lv_len;
3846 pt->pt_argv = (typval_T *)alloc(
3847 sizeof(typval_T) * pt->pt_argc);
3848 if (pt->pt_argv == NULL)
3849 {
3850 vim_free(pt);
3851 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003852 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003853 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003854 for (i = 0; i < arg_len; i++)
3855 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3856 if (lv_len > 0)
3857 for (li = list->lv_first; li != NULL;
3858 li = li->li_next)
3859 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003860 }
3861
3862 /* For "function(dict.func, [], dict)" and "func" is a partial
3863 * use "dict". That is backwards compatible. */
3864 if (dict_idx > 0)
3865 {
3866 /* The dict is bound explicitly, pt_auto is FALSE. */
3867 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3868 ++pt->pt_dict->dv_refcount;
3869 }
3870 else if (arg_pt != NULL)
3871 {
3872 /* If the dict was bound automatically the result is also
3873 * bound automatically. */
3874 pt->pt_dict = arg_pt->pt_dict;
3875 pt->pt_auto = arg_pt->pt_auto;
3876 if (pt->pt_dict != NULL)
3877 ++pt->pt_dict->dv_refcount;
3878 }
3879
3880 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003881 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3882 {
3883 pt->pt_func = arg_pt->pt_func;
3884 func_ptr_ref(pt->pt_func);
3885 vim_free(name);
3886 }
3887 else if (is_funcref)
3888 {
3889 pt->pt_func = find_func(trans_name);
3890 func_ptr_ref(pt->pt_func);
3891 vim_free(name);
3892 }
3893 else
3894 {
3895 pt->pt_name = name;
3896 func_ref(name);
3897 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003898 }
3899 rettv->v_type = VAR_PARTIAL;
3900 rettv->vval.v_partial = pt;
3901 }
3902 else
3903 {
3904 /* result is a VAR_FUNC */
3905 rettv->v_type = VAR_FUNC;
3906 rettv->vval.v_string = name;
3907 func_ref(name);
3908 }
3909 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003910theend:
3911 vim_free(trans_name);
3912}
3913
3914/*
3915 * "funcref()" function
3916 */
3917 static void
3918f_funcref(typval_T *argvars, typval_T *rettv)
3919{
3920 common_function(argvars, rettv, TRUE);
3921}
3922
3923/*
3924 * "function()" function
3925 */
3926 static void
3927f_function(typval_T *argvars, typval_T *rettv)
3928{
3929 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003930}
3931
3932/*
3933 * "garbagecollect()" function
3934 */
3935 static void
3936f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3937{
3938 /* This is postponed until we are back at the toplevel, because we may be
3939 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3940 want_garbage_collect = TRUE;
3941
3942 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3943 garbage_collect_at_exit = TRUE;
3944}
3945
3946/*
3947 * "get()" function
3948 */
3949 static void
3950f_get(typval_T *argvars, typval_T *rettv)
3951{
3952 listitem_T *li;
3953 list_T *l;
3954 dictitem_T *di;
3955 dict_T *d;
3956 typval_T *tv = NULL;
3957
3958 if (argvars[0].v_type == VAR_LIST)
3959 {
3960 if ((l = argvars[0].vval.v_list) != NULL)
3961 {
3962 int error = FALSE;
3963
3964 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3965 if (!error && li != NULL)
3966 tv = &li->li_tv;
3967 }
3968 }
3969 else if (argvars[0].v_type == VAR_DICT)
3970 {
3971 if ((d = argvars[0].vval.v_dict) != NULL)
3972 {
3973 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3974 if (di != NULL)
3975 tv = &di->di_tv;
3976 }
3977 }
3978 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3979 {
3980 partial_T *pt;
3981 partial_T fref_pt;
3982
3983 if (argvars[0].v_type == VAR_PARTIAL)
3984 pt = argvars[0].vval.v_partial;
3985 else
3986 {
3987 vim_memset(&fref_pt, 0, sizeof(fref_pt));
3988 fref_pt.pt_name = argvars[0].vval.v_string;
3989 pt = &fref_pt;
3990 }
3991
3992 if (pt != NULL)
3993 {
3994 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003995 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003996
3997 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3998 {
3999 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004000 n = partial_name(pt);
4001 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004002 rettv->vval.v_string = NULL;
4003 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004004 {
4005 rettv->vval.v_string = vim_strsave(n);
4006 if (rettv->v_type == VAR_FUNC)
4007 func_ref(rettv->vval.v_string);
4008 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004009 }
4010 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004011 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004012 else if (STRCMP(what, "args") == 0)
4013 {
4014 rettv->v_type = VAR_LIST;
4015 if (rettv_list_alloc(rettv) == OK)
4016 {
4017 int i;
4018
4019 for (i = 0; i < pt->pt_argc; ++i)
4020 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4021 }
4022 }
4023 else
4024 EMSG2(_(e_invarg2), what);
4025 return;
4026 }
4027 }
4028 else
4029 EMSG2(_(e_listdictarg), "get()");
4030
4031 if (tv == NULL)
4032 {
4033 if (argvars[2].v_type != VAR_UNKNOWN)
4034 copy_tv(&argvars[2], rettv);
4035 }
4036 else
4037 copy_tv(tv, rettv);
4038}
4039
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004040#ifdef FEAT_SIGNS
4041/*
4042 * Returns information about signs placed in a buffer as list of dicts.
4043 */
4044 static void
4045get_buffer_signs(buf_T *buf, list_T *l)
4046{
4047 signlist_T *sign;
4048
4049 for (sign = buf->b_signlist; sign; sign = sign->next)
4050 {
4051 dict_T *d = dict_alloc();
4052
4053 if (d != NULL)
4054 {
4055 dict_add_nr_str(d, "id", sign->id, NULL);
4056 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004057 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004058
4059 list_append_dict(l, d);
4060 }
4061 }
4062}
4063#endif
4064
4065/*
4066 * Returns buffer options, variables and other attributes in a dictionary.
4067 */
4068 static dict_T *
4069get_buffer_info(buf_T *buf)
4070{
4071 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004072 tabpage_T *tp;
4073 win_T *wp;
4074 list_T *windows;
4075
4076 dict = dict_alloc();
4077 if (dict == NULL)
4078 return NULL;
4079
Bram Moolenaar33928832016-08-18 21:22:04 +02004080 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004081 dict_add_nr_str(dict, "name", 0L,
4082 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004083 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4084 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004085 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4086 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4087 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004088 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004089 dict_add_nr_str(dict, "hidden",
4090 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4091 NULL);
4092
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004093 /* Get a reference to buffer variables */
4094 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004095
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004096 /* List of windows displaying this buffer */
4097 windows = list_alloc();
4098 if (windows != NULL)
4099 {
4100 FOR_ALL_TAB_WINDOWS(tp, wp)
4101 if (wp->w_buffer == buf)
4102 list_append_number(windows, (varnumber_T)wp->w_id);
4103 dict_add_list(dict, "windows", windows);
4104 }
4105
4106#ifdef FEAT_SIGNS
4107 if (buf->b_signlist != NULL)
4108 {
4109 /* List of signs placed in this buffer */
4110 list_T *signs = list_alloc();
4111 if (signs != NULL)
4112 {
4113 get_buffer_signs(buf, signs);
4114 dict_add_list(dict, "signs", signs);
4115 }
4116 }
4117#endif
4118
4119 return dict;
4120}
4121
4122/*
4123 * "getbufinfo()" function
4124 */
4125 static void
4126f_getbufinfo(typval_T *argvars, typval_T *rettv)
4127{
4128 buf_T *buf = NULL;
4129 buf_T *argbuf = NULL;
4130 dict_T *d;
4131 int filtered = FALSE;
4132 int sel_buflisted = FALSE;
4133 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004134 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004135
4136 if (rettv_list_alloc(rettv) != OK)
4137 return;
4138
4139 /* List of all the buffers or selected buffers */
4140 if (argvars[0].v_type == VAR_DICT)
4141 {
4142 dict_T *sel_d = argvars[0].vval.v_dict;
4143
4144 if (sel_d != NULL)
4145 {
4146 dictitem_T *di;
4147
4148 filtered = TRUE;
4149
4150 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4151 if (di != NULL && get_tv_number(&di->di_tv))
4152 sel_buflisted = TRUE;
4153
4154 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4155 if (di != NULL && get_tv_number(&di->di_tv))
4156 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004157
4158 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4159 if (di != NULL && get_tv_number(&di->di_tv))
4160 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004161 }
4162 }
4163 else if (argvars[0].v_type != VAR_UNKNOWN)
4164 {
4165 /* Information about one buffer. Argument specifies the buffer */
4166 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4167 ++emsg_off;
4168 argbuf = get_buf_tv(&argvars[0], FALSE);
4169 --emsg_off;
4170 if (argbuf == NULL)
4171 return;
4172 }
4173
4174 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004175 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004176 {
4177 if (argbuf != NULL && argbuf != buf)
4178 continue;
4179 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004180 || (sel_buflisted && !buf->b_p_bl)
4181 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004182 continue;
4183
4184 d = get_buffer_info(buf);
4185 if (d != NULL)
4186 list_append_dict(rettv->vval.v_list, d);
4187 if (argbuf != NULL)
4188 return;
4189 }
4190}
4191
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004192static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4193
4194/*
4195 * Get line or list of lines from buffer "buf" into "rettv".
4196 * Return a range (from start to end) of lines in rettv from the specified
4197 * buffer.
4198 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4199 */
4200 static void
4201get_buffer_lines(
4202 buf_T *buf,
4203 linenr_T start,
4204 linenr_T end,
4205 int retlist,
4206 typval_T *rettv)
4207{
4208 char_u *p;
4209
4210 rettv->v_type = VAR_STRING;
4211 rettv->vval.v_string = NULL;
4212 if (retlist && rettv_list_alloc(rettv) == FAIL)
4213 return;
4214
4215 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4216 return;
4217
4218 if (!retlist)
4219 {
4220 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4221 p = ml_get_buf(buf, start, FALSE);
4222 else
4223 p = (char_u *)"";
4224 rettv->vval.v_string = vim_strsave(p);
4225 }
4226 else
4227 {
4228 if (end < start)
4229 return;
4230
4231 if (start < 1)
4232 start = 1;
4233 if (end > buf->b_ml.ml_line_count)
4234 end = buf->b_ml.ml_line_count;
4235 while (start <= end)
4236 if (list_append_string(rettv->vval.v_list,
4237 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4238 break;
4239 }
4240}
4241
4242/*
4243 * Get the lnum from the first argument.
4244 * Also accepts "$", then "buf" is used.
4245 * Returns 0 on error.
4246 */
4247 static linenr_T
4248get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4249{
4250 if (argvars[0].v_type == VAR_STRING
4251 && argvars[0].vval.v_string != NULL
4252 && argvars[0].vval.v_string[0] == '$'
4253 && buf != NULL)
4254 return buf->b_ml.ml_line_count;
4255 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4256}
4257
4258/*
4259 * "getbufline()" function
4260 */
4261 static void
4262f_getbufline(typval_T *argvars, typval_T *rettv)
4263{
4264 linenr_T lnum;
4265 linenr_T end;
4266 buf_T *buf;
4267
4268 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4269 ++emsg_off;
4270 buf = get_buf_tv(&argvars[0], FALSE);
4271 --emsg_off;
4272
4273 lnum = get_tv_lnum_buf(&argvars[1], buf);
4274 if (argvars[2].v_type == VAR_UNKNOWN)
4275 end = lnum;
4276 else
4277 end = get_tv_lnum_buf(&argvars[2], buf);
4278
4279 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4280}
4281
4282/*
4283 * "getbufvar()" function
4284 */
4285 static void
4286f_getbufvar(typval_T *argvars, typval_T *rettv)
4287{
4288 buf_T *buf;
4289 buf_T *save_curbuf;
4290 char_u *varname;
4291 dictitem_T *v;
4292 int done = FALSE;
4293
4294 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4295 varname = get_tv_string_chk(&argvars[1]);
4296 ++emsg_off;
4297 buf = get_buf_tv(&argvars[0], FALSE);
4298
4299 rettv->v_type = VAR_STRING;
4300 rettv->vval.v_string = NULL;
4301
4302 if (buf != NULL && varname != NULL)
4303 {
4304 /* set curbuf to be our buf, temporarily */
4305 save_curbuf = curbuf;
4306 curbuf = buf;
4307
Bram Moolenaar30567352016-08-27 21:25:44 +02004308 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004309 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004310 if (varname[1] == NUL)
4311 {
4312 /* get all buffer-local options in a dict */
4313 dict_T *opts = get_winbuf_options(TRUE);
4314
4315 if (opts != NULL)
4316 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004317 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004318 done = TRUE;
4319 }
4320 }
4321 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4322 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004323 done = TRUE;
4324 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004325 else
4326 {
4327 /* Look up the variable. */
4328 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4329 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4330 'b', varname, FALSE);
4331 if (v != NULL)
4332 {
4333 copy_tv(&v->di_tv, rettv);
4334 done = TRUE;
4335 }
4336 }
4337
4338 /* restore previous notion of curbuf */
4339 curbuf = save_curbuf;
4340 }
4341
4342 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4343 /* use the default value */
4344 copy_tv(&argvars[2], rettv);
4345
4346 --emsg_off;
4347}
4348
4349/*
4350 * "getchar()" function
4351 */
4352 static void
4353f_getchar(typval_T *argvars, typval_T *rettv)
4354{
4355 varnumber_T n;
4356 int error = FALSE;
4357
4358 /* Position the cursor. Needed after a message that ends in a space. */
4359 windgoto(msg_row, msg_col);
4360
4361 ++no_mapping;
4362 ++allow_keys;
4363 for (;;)
4364 {
4365 if (argvars[0].v_type == VAR_UNKNOWN)
4366 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004367 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004368 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4369 /* getchar(1): only check if char avail */
4370 n = vpeekc_any();
4371 else if (error || vpeekc_any() == NUL)
4372 /* illegal argument or getchar(0) and no char avail: return zero */
4373 n = 0;
4374 else
4375 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004376 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004377
4378 if (n == K_IGNORE)
4379 continue;
4380 break;
4381 }
4382 --no_mapping;
4383 --allow_keys;
4384
4385 set_vim_var_nr(VV_MOUSE_WIN, 0);
4386 set_vim_var_nr(VV_MOUSE_WINID, 0);
4387 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4388 set_vim_var_nr(VV_MOUSE_COL, 0);
4389
4390 rettv->vval.v_number = n;
4391 if (IS_SPECIAL(n) || mod_mask != 0)
4392 {
4393 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4394 int i = 0;
4395
4396 /* Turn a special key into three bytes, plus modifier. */
4397 if (mod_mask != 0)
4398 {
4399 temp[i++] = K_SPECIAL;
4400 temp[i++] = KS_MODIFIER;
4401 temp[i++] = mod_mask;
4402 }
4403 if (IS_SPECIAL(n))
4404 {
4405 temp[i++] = K_SPECIAL;
4406 temp[i++] = K_SECOND(n);
4407 temp[i++] = K_THIRD(n);
4408 }
4409#ifdef FEAT_MBYTE
4410 else if (has_mbyte)
4411 i += (*mb_char2bytes)(n, temp + i);
4412#endif
4413 else
4414 temp[i++] = n;
4415 temp[i++] = NUL;
4416 rettv->v_type = VAR_STRING;
4417 rettv->vval.v_string = vim_strsave(temp);
4418
4419#ifdef FEAT_MOUSE
4420 if (is_mouse_key(n))
4421 {
4422 int row = mouse_row;
4423 int col = mouse_col;
4424 win_T *win;
4425 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004426 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004427 int winnr = 1;
4428
4429 if (row >= 0 && col >= 0)
4430 {
4431 /* Find the window at the mouse coordinates and compute the
4432 * text position. */
4433 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004434 if (win == NULL)
4435 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004436 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004437 for (wp = firstwin; wp != win; wp = wp->w_next)
4438 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004439 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4440 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4441 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4442 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4443 }
4444 }
4445#endif
4446 }
4447}
4448
4449/*
4450 * "getcharmod()" function
4451 */
4452 static void
4453f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4454{
4455 rettv->vval.v_number = mod_mask;
4456}
4457
4458/*
4459 * "getcharsearch()" function
4460 */
4461 static void
4462f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4463{
4464 if (rettv_dict_alloc(rettv) != FAIL)
4465 {
4466 dict_T *dict = rettv->vval.v_dict;
4467
4468 dict_add_nr_str(dict, "char", 0L, last_csearch());
4469 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4470 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4471 }
4472}
4473
4474/*
4475 * "getcmdline()" function
4476 */
4477 static void
4478f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4479{
4480 rettv->v_type = VAR_STRING;
4481 rettv->vval.v_string = get_cmdline_str();
4482}
4483
4484/*
4485 * "getcmdpos()" function
4486 */
4487 static void
4488f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4489{
4490 rettv->vval.v_number = get_cmdline_pos() + 1;
4491}
4492
4493/*
4494 * "getcmdtype()" function
4495 */
4496 static void
4497f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4498{
4499 rettv->v_type = VAR_STRING;
4500 rettv->vval.v_string = alloc(2);
4501 if (rettv->vval.v_string != NULL)
4502 {
4503 rettv->vval.v_string[0] = get_cmdline_type();
4504 rettv->vval.v_string[1] = NUL;
4505 }
4506}
4507
4508/*
4509 * "getcmdwintype()" function
4510 */
4511 static void
4512f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4513{
4514 rettv->v_type = VAR_STRING;
4515 rettv->vval.v_string = NULL;
4516#ifdef FEAT_CMDWIN
4517 rettv->vval.v_string = alloc(2);
4518 if (rettv->vval.v_string != NULL)
4519 {
4520 rettv->vval.v_string[0] = cmdwin_type;
4521 rettv->vval.v_string[1] = NUL;
4522 }
4523#endif
4524}
4525
4526#if defined(FEAT_CMDL_COMPL)
4527/*
4528 * "getcompletion()" function
4529 */
4530 static void
4531f_getcompletion(typval_T *argvars, typval_T *rettv)
4532{
4533 char_u *pat;
4534 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004535 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004536 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4537 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004538
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004539 if (argvars[2].v_type != VAR_UNKNOWN)
4540 filtered = get_tv_number_chk(&argvars[2], NULL);
4541
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004542 if (p_wic)
4543 options |= WILD_ICASE;
4544
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004545 /* For filtered results, 'wildignore' is used */
4546 if (!filtered)
4547 options |= WILD_KEEP_ALL;
4548
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004549 ExpandInit(&xpc);
4550 xpc.xp_pattern = get_tv_string(&argvars[0]);
4551 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4552 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4553 if (xpc.xp_context == EXPAND_NOTHING)
4554 {
4555 if (argvars[1].v_type == VAR_STRING)
4556 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4557 else
4558 EMSG(_(e_invarg));
4559 return;
4560 }
4561
4562# if defined(FEAT_MENU)
4563 if (xpc.xp_context == EXPAND_MENUS)
4564 {
4565 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4566 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4567 }
4568# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004569#ifdef FEAT_CSCOPE
4570 if (xpc.xp_context == EXPAND_CSCOPE)
4571 {
4572 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4573 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4574 }
4575#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004576#ifdef FEAT_SIGNS
4577 if (xpc.xp_context == EXPAND_SIGN)
4578 {
4579 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4580 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4581 }
4582#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004583
4584 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4585 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4586 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004587 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004588
4589 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4590
4591 for (i = 0; i < xpc.xp_numfiles; i++)
4592 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4593 }
4594 vim_free(pat);
4595 ExpandCleanup(&xpc);
4596}
4597#endif
4598
4599/*
4600 * "getcwd()" function
4601 */
4602 static void
4603f_getcwd(typval_T *argvars, typval_T *rettv)
4604{
4605 win_T *wp = NULL;
4606 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004607 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004608
4609 rettv->v_type = VAR_STRING;
4610 rettv->vval.v_string = NULL;
4611
Bram Moolenaar54591292018-02-09 20:53:59 +01004612 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4613 global = TRUE;
4614 else
4615 wp = find_tabwin(&argvars[0], &argvars[1]);
4616
4617 if (wp != NULL && wp->w_localdir != NULL)
4618 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4619 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004620 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004621 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004622 rettv->vval.v_string = vim_strsave(globaldir);
4623 else
4624 {
4625 cwd = alloc(MAXPATHL);
4626 if (cwd != NULL)
4627 {
4628 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4629 rettv->vval.v_string = vim_strsave(cwd);
4630 vim_free(cwd);
4631 }
4632 }
4633#ifdef BACKSLASH_IN_FILENAME
4634 if (rettv->vval.v_string != NULL)
4635 slash_adjust(rettv->vval.v_string);
4636#endif
4637 }
4638}
4639
4640/*
4641 * "getfontname()" function
4642 */
4643 static void
4644f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4645{
4646 rettv->v_type = VAR_STRING;
4647 rettv->vval.v_string = NULL;
4648#ifdef FEAT_GUI
4649 if (gui.in_use)
4650 {
4651 GuiFont font;
4652 char_u *name = NULL;
4653
4654 if (argvars[0].v_type == VAR_UNKNOWN)
4655 {
4656 /* Get the "Normal" font. Either the name saved by
4657 * hl_set_font_name() or from the font ID. */
4658 font = gui.norm_font;
4659 name = hl_get_font_name();
4660 }
4661 else
4662 {
4663 name = get_tv_string(&argvars[0]);
4664 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4665 return;
4666 font = gui_mch_get_font(name, FALSE);
4667 if (font == NOFONT)
4668 return; /* Invalid font name, return empty string. */
4669 }
4670 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4671 if (argvars[0].v_type != VAR_UNKNOWN)
4672 gui_mch_free_font(font);
4673 }
4674#endif
4675}
4676
4677/*
4678 * "getfperm({fname})" function
4679 */
4680 static void
4681f_getfperm(typval_T *argvars, typval_T *rettv)
4682{
4683 char_u *fname;
4684 stat_T st;
4685 char_u *perm = NULL;
4686 char_u flags[] = "rwx";
4687 int i;
4688
4689 fname = get_tv_string(&argvars[0]);
4690
4691 rettv->v_type = VAR_STRING;
4692 if (mch_stat((char *)fname, &st) >= 0)
4693 {
4694 perm = vim_strsave((char_u *)"---------");
4695 if (perm != NULL)
4696 {
4697 for (i = 0; i < 9; i++)
4698 {
4699 if (st.st_mode & (1 << (8 - i)))
4700 perm[i] = flags[i % 3];
4701 }
4702 }
4703 }
4704 rettv->vval.v_string = perm;
4705}
4706
4707/*
4708 * "getfsize({fname})" function
4709 */
4710 static void
4711f_getfsize(typval_T *argvars, typval_T *rettv)
4712{
4713 char_u *fname;
4714 stat_T st;
4715
4716 fname = get_tv_string(&argvars[0]);
4717
4718 rettv->v_type = VAR_NUMBER;
4719
4720 if (mch_stat((char *)fname, &st) >= 0)
4721 {
4722 if (mch_isdir(fname))
4723 rettv->vval.v_number = 0;
4724 else
4725 {
4726 rettv->vval.v_number = (varnumber_T)st.st_size;
4727
4728 /* non-perfect check for overflow */
4729 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4730 rettv->vval.v_number = -2;
4731 }
4732 }
4733 else
4734 rettv->vval.v_number = -1;
4735}
4736
4737/*
4738 * "getftime({fname})" function
4739 */
4740 static void
4741f_getftime(typval_T *argvars, typval_T *rettv)
4742{
4743 char_u *fname;
4744 stat_T st;
4745
4746 fname = get_tv_string(&argvars[0]);
4747
4748 if (mch_stat((char *)fname, &st) >= 0)
4749 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4750 else
4751 rettv->vval.v_number = -1;
4752}
4753
4754/*
4755 * "getftype({fname})" function
4756 */
4757 static void
4758f_getftype(typval_T *argvars, typval_T *rettv)
4759{
4760 char_u *fname;
4761 stat_T st;
4762 char_u *type = NULL;
4763 char *t;
4764
4765 fname = get_tv_string(&argvars[0]);
4766
4767 rettv->v_type = VAR_STRING;
4768 if (mch_lstat((char *)fname, &st) >= 0)
4769 {
4770#ifdef S_ISREG
4771 if (S_ISREG(st.st_mode))
4772 t = "file";
4773 else if (S_ISDIR(st.st_mode))
4774 t = "dir";
4775# ifdef S_ISLNK
4776 else if (S_ISLNK(st.st_mode))
4777 t = "link";
4778# endif
4779# ifdef S_ISBLK
4780 else if (S_ISBLK(st.st_mode))
4781 t = "bdev";
4782# endif
4783# ifdef S_ISCHR
4784 else if (S_ISCHR(st.st_mode))
4785 t = "cdev";
4786# endif
4787# ifdef S_ISFIFO
4788 else if (S_ISFIFO(st.st_mode))
4789 t = "fifo";
4790# endif
4791# ifdef S_ISSOCK
4792 else if (S_ISSOCK(st.st_mode))
4793 t = "fifo";
4794# endif
4795 else
4796 t = "other";
4797#else
4798# ifdef S_IFMT
4799 switch (st.st_mode & S_IFMT)
4800 {
4801 case S_IFREG: t = "file"; break;
4802 case S_IFDIR: t = "dir"; break;
4803# ifdef S_IFLNK
4804 case S_IFLNK: t = "link"; break;
4805# endif
4806# ifdef S_IFBLK
4807 case S_IFBLK: t = "bdev"; break;
4808# endif
4809# ifdef S_IFCHR
4810 case S_IFCHR: t = "cdev"; break;
4811# endif
4812# ifdef S_IFIFO
4813 case S_IFIFO: t = "fifo"; break;
4814# endif
4815# ifdef S_IFSOCK
4816 case S_IFSOCK: t = "socket"; break;
4817# endif
4818 default: t = "other";
4819 }
4820# else
4821 if (mch_isdir(fname))
4822 t = "dir";
4823 else
4824 t = "file";
4825# endif
4826#endif
4827 type = vim_strsave((char_u *)t);
4828 }
4829 rettv->vval.v_string = type;
4830}
4831
4832/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004833 * "getjumplist()" function
4834 */
4835 static void
4836f_getjumplist(typval_T *argvars, typval_T *rettv)
4837{
4838#ifdef FEAT_JUMPLIST
4839 win_T *wp;
4840 int i;
4841 list_T *l;
4842 dict_T *d;
4843#endif
4844
4845 if (rettv_list_alloc(rettv) != OK)
4846 return;
4847
4848#ifdef FEAT_JUMPLIST
4849 wp = find_tabwin(&argvars[0], &argvars[1]);
4850 if (wp == NULL)
4851 return;
4852
4853 l = list_alloc();
4854 if (l == NULL)
4855 return;
4856
4857 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4858 return;
4859 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4860
Bram Moolenaar48679742018-02-13 13:33:29 +01004861 cleanup_jumplist(wp, TRUE);
4862
Bram Moolenaar4f505882018-02-10 21:06:32 +01004863 for (i = 0; i < wp->w_jumplistlen; ++i)
4864 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004865 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4866 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004867 if ((d = dict_alloc()) == NULL)
4868 return;
4869 if (list_append_dict(l, d) == FAIL)
4870 return;
4871 dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
4872 NULL);
4873 dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
4874 NULL);
4875# ifdef FEAT_VIRTUALEDIT
4876 dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
4877 NULL);
4878# endif
4879 dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004880 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaar4f505882018-02-10 21:06:32 +01004881 dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
4882 }
4883#endif
4884}
4885
4886/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004887 * "getline(lnum, [end])" function
4888 */
4889 static void
4890f_getline(typval_T *argvars, typval_T *rettv)
4891{
4892 linenr_T lnum;
4893 linenr_T end;
4894 int retlist;
4895
4896 lnum = get_tv_lnum(argvars);
4897 if (argvars[1].v_type == VAR_UNKNOWN)
4898 {
4899 end = 0;
4900 retlist = FALSE;
4901 }
4902 else
4903 {
4904 end = get_tv_lnum(&argvars[1]);
4905 retlist = TRUE;
4906 }
4907
4908 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4909}
4910
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004911#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004912 static void
4913get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4914{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004915 if (what_arg->v_type == VAR_UNKNOWN)
4916 {
4917 if (rettv_list_alloc(rettv) == OK)
4918 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004919 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004920 }
4921 else
4922 {
4923 if (rettv_dict_alloc(rettv) == OK)
4924 if (is_qf || (wp != NULL))
4925 {
4926 if (what_arg->v_type == VAR_DICT)
4927 {
4928 dict_T *d = what_arg->vval.v_dict;
4929
4930 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004931 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004932 }
4933 else
4934 EMSG(_(e_dictreq));
4935 }
4936 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004937}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004938#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004939
4940/*
4941 * "getloclist()" function
4942 */
4943 static void
4944f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4945{
4946#ifdef FEAT_QUICKFIX
4947 win_T *wp;
4948
4949 wp = find_win_by_nr(&argvars[0], NULL);
4950 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4951#endif
4952}
4953
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004954/*
4955 * "getmatches()" function
4956 */
4957 static void
4958f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4959{
4960#ifdef FEAT_SEARCH_EXTRA
4961 dict_T *dict;
4962 matchitem_T *cur = curwin->w_match_head;
4963 int i;
4964
4965 if (rettv_list_alloc(rettv) == OK)
4966 {
4967 while (cur != NULL)
4968 {
4969 dict = dict_alloc();
4970 if (dict == NULL)
4971 return;
4972 if (cur->match.regprog == NULL)
4973 {
4974 /* match added with matchaddpos() */
4975 for (i = 0; i < MAXPOSMATCH; ++i)
4976 {
4977 llpos_T *llpos;
4978 char buf[6];
4979 list_T *l;
4980
4981 llpos = &cur->pos.pos[i];
4982 if (llpos->lnum == 0)
4983 break;
4984 l = list_alloc();
4985 if (l == NULL)
4986 break;
4987 list_append_number(l, (varnumber_T)llpos->lnum);
4988 if (llpos->col > 0)
4989 {
4990 list_append_number(l, (varnumber_T)llpos->col);
4991 list_append_number(l, (varnumber_T)llpos->len);
4992 }
4993 sprintf(buf, "pos%d", i + 1);
4994 dict_add_list(dict, buf, l);
4995 }
4996 }
4997 else
4998 {
4999 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
5000 }
5001 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
5002 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
5003 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
5004# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5005 if (cur->conceal_char)
5006 {
5007 char_u buf[MB_MAXBYTES + 1];
5008
5009 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5010 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5011 }
5012# endif
5013 list_append_dict(rettv->vval.v_list, dict);
5014 cur = cur->next;
5015 }
5016 }
5017#endif
5018}
5019
5020/*
5021 * "getpid()" function
5022 */
5023 static void
5024f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5025{
5026 rettv->vval.v_number = mch_get_pid();
5027}
5028
5029 static void
5030getpos_both(
5031 typval_T *argvars,
5032 typval_T *rettv,
5033 int getcurpos)
5034{
5035 pos_T *fp;
5036 list_T *l;
5037 int fnum = -1;
5038
5039 if (rettv_list_alloc(rettv) == OK)
5040 {
5041 l = rettv->vval.v_list;
5042 if (getcurpos)
5043 fp = &curwin->w_cursor;
5044 else
5045 fp = var2fpos(&argvars[0], TRUE, &fnum);
5046 if (fnum != -1)
5047 list_append_number(l, (varnumber_T)fnum);
5048 else
5049 list_append_number(l, (varnumber_T)0);
5050 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5051 : (varnumber_T)0);
5052 list_append_number(l, (fp != NULL)
5053 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5054 : (varnumber_T)0);
5055 list_append_number(l,
5056#ifdef FEAT_VIRTUALEDIT
5057 (fp != NULL) ? (varnumber_T)fp->coladd :
5058#endif
5059 (varnumber_T)0);
5060 if (getcurpos)
5061 {
5062 update_curswant();
5063 list_append_number(l, curwin->w_curswant == MAXCOL ?
5064 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5065 }
5066 }
5067 else
5068 rettv->vval.v_number = FALSE;
5069}
5070
5071
5072/*
5073 * "getcurpos()" function
5074 */
5075 static void
5076f_getcurpos(typval_T *argvars, typval_T *rettv)
5077{
5078 getpos_both(argvars, rettv, TRUE);
5079}
5080
5081/*
5082 * "getpos(string)" function
5083 */
5084 static void
5085f_getpos(typval_T *argvars, typval_T *rettv)
5086{
5087 getpos_both(argvars, rettv, FALSE);
5088}
5089
5090/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005091 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005092 */
5093 static void
5094f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5095{
5096#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005097 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005098#endif
5099}
5100
5101/*
5102 * "getreg()" function
5103 */
5104 static void
5105f_getreg(typval_T *argvars, typval_T *rettv)
5106{
5107 char_u *strregname;
5108 int regname;
5109 int arg2 = FALSE;
5110 int return_list = FALSE;
5111 int error = FALSE;
5112
5113 if (argvars[0].v_type != VAR_UNKNOWN)
5114 {
5115 strregname = get_tv_string_chk(&argvars[0]);
5116 error = strregname == NULL;
5117 if (argvars[1].v_type != VAR_UNKNOWN)
5118 {
5119 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5120 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5121 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5122 }
5123 }
5124 else
5125 strregname = get_vim_var_str(VV_REG);
5126
5127 if (error)
5128 return;
5129
5130 regname = (strregname == NULL ? '"' : *strregname);
5131 if (regname == 0)
5132 regname = '"';
5133
5134 if (return_list)
5135 {
5136 rettv->v_type = VAR_LIST;
5137 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5138 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5139 if (rettv->vval.v_list == NULL)
5140 (void)rettv_list_alloc(rettv);
5141 else
5142 ++rettv->vval.v_list->lv_refcount;
5143 }
5144 else
5145 {
5146 rettv->v_type = VAR_STRING;
5147 rettv->vval.v_string = get_reg_contents(regname,
5148 arg2 ? GREG_EXPR_SRC : 0);
5149 }
5150}
5151
5152/*
5153 * "getregtype()" function
5154 */
5155 static void
5156f_getregtype(typval_T *argvars, typval_T *rettv)
5157{
5158 char_u *strregname;
5159 int regname;
5160 char_u buf[NUMBUFLEN + 2];
5161 long reglen = 0;
5162
5163 if (argvars[0].v_type != VAR_UNKNOWN)
5164 {
5165 strregname = get_tv_string_chk(&argvars[0]);
5166 if (strregname == NULL) /* type error; errmsg already given */
5167 {
5168 rettv->v_type = VAR_STRING;
5169 rettv->vval.v_string = NULL;
5170 return;
5171 }
5172 }
5173 else
5174 /* Default to v:register */
5175 strregname = get_vim_var_str(VV_REG);
5176
5177 regname = (strregname == NULL ? '"' : *strregname);
5178 if (regname == 0)
5179 regname = '"';
5180
5181 buf[0] = NUL;
5182 buf[1] = NUL;
5183 switch (get_reg_type(regname, &reglen))
5184 {
5185 case MLINE: buf[0] = 'V'; break;
5186 case MCHAR: buf[0] = 'v'; break;
5187 case MBLOCK:
5188 buf[0] = Ctrl_V;
5189 sprintf((char *)buf + 1, "%ld", reglen + 1);
5190 break;
5191 }
5192 rettv->v_type = VAR_STRING;
5193 rettv->vval.v_string = vim_strsave(buf);
5194}
5195
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005196/*
5197 * Returns information (variables, options, etc.) about a tab page
5198 * as a dictionary.
5199 */
5200 static dict_T *
5201get_tabpage_info(tabpage_T *tp, int tp_idx)
5202{
5203 win_T *wp;
5204 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005205 list_T *l;
5206
5207 dict = dict_alloc();
5208 if (dict == NULL)
5209 return NULL;
5210
Bram Moolenaar33928832016-08-18 21:22:04 +02005211 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005212
5213 l = list_alloc();
5214 if (l != NULL)
5215 {
5216 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5217 wp; wp = wp->w_next)
5218 list_append_number(l, (varnumber_T)wp->w_id);
5219 dict_add_list(dict, "windows", l);
5220 }
5221
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005222 /* Make a reference to tabpage variables */
5223 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005224
5225 return dict;
5226}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005227
5228/*
5229 * "gettabinfo()" function
5230 */
5231 static void
5232f_gettabinfo(typval_T *argvars, typval_T *rettv)
5233{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005234 tabpage_T *tp, *tparg = NULL;
5235 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005236 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005237
5238 if (rettv_list_alloc(rettv) != OK)
5239 return;
5240
5241 if (argvars[0].v_type != VAR_UNKNOWN)
5242 {
5243 /* Information about one tab page */
5244 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5245 if (tparg == NULL)
5246 return;
5247 }
5248
5249 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005250 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005251 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005252 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005253 if (tparg != NULL && tp != tparg)
5254 continue;
5255 d = get_tabpage_info(tp, tpnr);
5256 if (d != NULL)
5257 list_append_dict(rettv->vval.v_list, d);
5258 if (tparg != NULL)
5259 return;
5260 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005261}
5262
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005263/*
5264 * "gettabvar()" function
5265 */
5266 static void
5267f_gettabvar(typval_T *argvars, typval_T *rettv)
5268{
5269 win_T *oldcurwin;
5270 tabpage_T *tp, *oldtabpage;
5271 dictitem_T *v;
5272 char_u *varname;
5273 int done = FALSE;
5274
5275 rettv->v_type = VAR_STRING;
5276 rettv->vval.v_string = NULL;
5277
5278 varname = get_tv_string_chk(&argvars[1]);
5279 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5280 if (tp != NULL && varname != NULL)
5281 {
5282 /* Set tp to be our tabpage, temporarily. Also set the window to the
5283 * first window in the tabpage, otherwise the window is not valid. */
5284 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005285 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5286 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005287 {
5288 /* look up the variable */
5289 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5290 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5291 if (v != NULL)
5292 {
5293 copy_tv(&v->di_tv, rettv);
5294 done = TRUE;
5295 }
5296 }
5297
5298 /* restore previous notion of curwin */
5299 restore_win(oldcurwin, oldtabpage, TRUE);
5300 }
5301
5302 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5303 copy_tv(&argvars[2], rettv);
5304}
5305
5306/*
5307 * "gettabwinvar()" function
5308 */
5309 static void
5310f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5311{
5312 getwinvar(argvars, rettv, 1);
5313}
5314
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005315/*
5316 * Returns information about a window as a dictionary.
5317 */
5318 static dict_T *
5319get_win_info(win_T *wp, short tpnr, short winnr)
5320{
5321 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005322
5323 dict = dict_alloc();
5324 if (dict == NULL)
5325 return NULL;
5326
Bram Moolenaar33928832016-08-18 21:22:04 +02005327 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5328 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005329 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5330 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005331#ifdef FEAT_MENU
5332 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5333#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005334 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005335 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005336
Bram Moolenaar69905d12017-08-13 18:14:47 +02005337#ifdef FEAT_TERMINAL
5338 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5339#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005340#ifdef FEAT_QUICKFIX
5341 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5342 dict_add_nr_str(dict, "loclist",
5343 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5344#endif
5345
Bram Moolenaar30567352016-08-27 21:25:44 +02005346 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005347 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005348
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005349 return dict;
5350}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005351
5352/*
5353 * "getwininfo()" function
5354 */
5355 static void
5356f_getwininfo(typval_T *argvars, typval_T *rettv)
5357{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005358 tabpage_T *tp;
5359 win_T *wp = NULL, *wparg = NULL;
5360 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005361 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005362
5363 if (rettv_list_alloc(rettv) != OK)
5364 return;
5365
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005366 if (argvars[0].v_type != VAR_UNKNOWN)
5367 {
5368 wparg = win_id2wp(argvars);
5369 if (wparg == NULL)
5370 return;
5371 }
5372
5373 /* Collect information about either all the windows across all the tab
5374 * pages or one particular window.
5375 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005376 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005377 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005378 tabnr++;
5379 winnr = 0;
5380 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005381 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005382 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005383 if (wparg != NULL && wp != wparg)
5384 continue;
5385 d = get_win_info(wp, tabnr, winnr);
5386 if (d != NULL)
5387 list_append_dict(rettv->vval.v_list, d);
5388 if (wparg != NULL)
5389 /* found information about a specific window */
5390 return;
5391 }
5392 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005393}
5394
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005395/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005396 * "win_findbuf()" function
5397 */
5398 static void
5399f_win_findbuf(typval_T *argvars, typval_T *rettv)
5400{
5401 if (rettv_list_alloc(rettv) != FAIL)
5402 win_findbuf(argvars, rettv->vval.v_list);
5403}
5404
5405/*
5406 * "win_getid()" function
5407 */
5408 static void
5409f_win_getid(typval_T *argvars, typval_T *rettv)
5410{
5411 rettv->vval.v_number = win_getid(argvars);
5412}
5413
5414/*
5415 * "win_gotoid()" function
5416 */
5417 static void
5418f_win_gotoid(typval_T *argvars, typval_T *rettv)
5419{
5420 rettv->vval.v_number = win_gotoid(argvars);
5421}
5422
5423/*
5424 * "win_id2tabwin()" function
5425 */
5426 static void
5427f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5428{
5429 if (rettv_list_alloc(rettv) != FAIL)
5430 win_id2tabwin(argvars, rettv->vval.v_list);
5431}
5432
5433/*
5434 * "win_id2win()" function
5435 */
5436 static void
5437f_win_id2win(typval_T *argvars, typval_T *rettv)
5438{
5439 rettv->vval.v_number = win_id2win(argvars);
5440}
5441
5442/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005443 * "win_screenpos()" function
5444 */
5445 static void
5446f_win_screenpos(typval_T *argvars, typval_T *rettv)
5447{
5448 win_T *wp;
5449
5450 if (rettv_list_alloc(rettv) == FAIL)
5451 return;
5452
5453 wp = find_win_by_nr(&argvars[0], NULL);
5454 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5455 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5456}
5457
5458/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005459 * "getwinposx()" function
5460 */
5461 static void
5462f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5463{
5464 rettv->vval.v_number = -1;
5465#ifdef FEAT_GUI
5466 if (gui.in_use)
5467 {
5468 int x, y;
5469
5470 if (gui_mch_get_winpos(&x, &y) == OK)
5471 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005472 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005473 }
5474#endif
5475#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5476 {
5477 int x, y;
5478
5479 if (term_get_winpos(&x, &y) == OK)
5480 rettv->vval.v_number = x;
5481 }
5482#endif
5483}
5484
5485/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005486 * "getwinposy()" function
5487 */
5488 static void
5489f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5490{
5491 rettv->vval.v_number = -1;
5492#ifdef FEAT_GUI
5493 if (gui.in_use)
5494 {
5495 int x, y;
5496
5497 if (gui_mch_get_winpos(&x, &y) == OK)
5498 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005499 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005500 }
5501#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005502#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5503 {
5504 int x, y;
5505
5506 if (term_get_winpos(&x, &y) == OK)
5507 rettv->vval.v_number = y;
5508 }
5509#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005510}
5511
5512/*
5513 * "getwinvar()" function
5514 */
5515 static void
5516f_getwinvar(typval_T *argvars, typval_T *rettv)
5517{
5518 getwinvar(argvars, rettv, 0);
5519}
5520
5521/*
5522 * "glob()" function
5523 */
5524 static void
5525f_glob(typval_T *argvars, typval_T *rettv)
5526{
5527 int options = WILD_SILENT|WILD_USE_NL;
5528 expand_T xpc;
5529 int error = FALSE;
5530
5531 /* When the optional second argument is non-zero, don't remove matches
5532 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5533 rettv->v_type = VAR_STRING;
5534 if (argvars[1].v_type != VAR_UNKNOWN)
5535 {
5536 if (get_tv_number_chk(&argvars[1], &error))
5537 options |= WILD_KEEP_ALL;
5538 if (argvars[2].v_type != VAR_UNKNOWN)
5539 {
5540 if (get_tv_number_chk(&argvars[2], &error))
5541 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005542 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005543 }
5544 if (argvars[3].v_type != VAR_UNKNOWN
5545 && get_tv_number_chk(&argvars[3], &error))
5546 options |= WILD_ALLLINKS;
5547 }
5548 }
5549 if (!error)
5550 {
5551 ExpandInit(&xpc);
5552 xpc.xp_context = EXPAND_FILES;
5553 if (p_wic)
5554 options += WILD_ICASE;
5555 if (rettv->v_type == VAR_STRING)
5556 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5557 NULL, options, WILD_ALL);
5558 else if (rettv_list_alloc(rettv) != FAIL)
5559 {
5560 int i;
5561
5562 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5563 NULL, options, WILD_ALL_KEEP);
5564 for (i = 0; i < xpc.xp_numfiles; i++)
5565 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5566
5567 ExpandCleanup(&xpc);
5568 }
5569 }
5570 else
5571 rettv->vval.v_string = NULL;
5572}
5573
5574/*
5575 * "globpath()" function
5576 */
5577 static void
5578f_globpath(typval_T *argvars, typval_T *rettv)
5579{
5580 int flags = 0;
5581 char_u buf1[NUMBUFLEN];
5582 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5583 int error = FALSE;
5584 garray_T ga;
5585 int i;
5586
5587 /* When the optional second argument is non-zero, don't remove matches
5588 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5589 rettv->v_type = VAR_STRING;
5590 if (argvars[2].v_type != VAR_UNKNOWN)
5591 {
5592 if (get_tv_number_chk(&argvars[2], &error))
5593 flags |= WILD_KEEP_ALL;
5594 if (argvars[3].v_type != VAR_UNKNOWN)
5595 {
5596 if (get_tv_number_chk(&argvars[3], &error))
5597 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005598 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005599 }
5600 if (argvars[4].v_type != VAR_UNKNOWN
5601 && get_tv_number_chk(&argvars[4], &error))
5602 flags |= WILD_ALLLINKS;
5603 }
5604 }
5605 if (file != NULL && !error)
5606 {
5607 ga_init2(&ga, (int)sizeof(char_u *), 10);
5608 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5609 if (rettv->v_type == VAR_STRING)
5610 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5611 else if (rettv_list_alloc(rettv) != FAIL)
5612 for (i = 0; i < ga.ga_len; ++i)
5613 list_append_string(rettv->vval.v_list,
5614 ((char_u **)(ga.ga_data))[i], -1);
5615 ga_clear_strings(&ga);
5616 }
5617 else
5618 rettv->vval.v_string = NULL;
5619}
5620
5621/*
5622 * "glob2regpat()" function
5623 */
5624 static void
5625f_glob2regpat(typval_T *argvars, typval_T *rettv)
5626{
5627 char_u *pat = get_tv_string_chk(&argvars[0]);
5628
5629 rettv->v_type = VAR_STRING;
5630 rettv->vval.v_string = (pat == NULL)
5631 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5632}
5633
5634/* for VIM_VERSION_ defines */
5635#include "version.h"
5636
5637/*
5638 * "has()" function
5639 */
5640 static void
5641f_has(typval_T *argvars, typval_T *rettv)
5642{
5643 int i;
5644 char_u *name;
5645 int n = FALSE;
5646 static char *(has_list[]) =
5647 {
5648#ifdef AMIGA
5649 "amiga",
5650# ifdef FEAT_ARP
5651 "arp",
5652# endif
5653#endif
5654#ifdef __BEOS__
5655 "beos",
5656#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005657#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005658 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5659 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005660# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005661 "macunix", /* Mac OS X, with the darwin feature */
5662 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005663# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005664#endif
5665#ifdef __QNX__
5666 "qnx",
5667#endif
5668#ifdef UNIX
5669 "unix",
5670#endif
5671#ifdef VMS
5672 "vms",
5673#endif
5674#ifdef WIN32
5675 "win32",
5676#endif
5677#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5678 "win32unix",
5679#endif
5680#if defined(WIN64) || defined(_WIN64)
5681 "win64",
5682#endif
5683#ifdef EBCDIC
5684 "ebcdic",
5685#endif
5686#ifndef CASE_INSENSITIVE_FILENAME
5687 "fname_case",
5688#endif
5689#ifdef HAVE_ACL
5690 "acl",
5691#endif
5692#ifdef FEAT_ARABIC
5693 "arabic",
5694#endif
5695#ifdef FEAT_AUTOCMD
5696 "autocmd",
5697#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005698#ifdef FEAT_AUTOSERVERNAME
5699 "autoservername",
5700#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005701#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005702 "balloon_eval",
5703# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5704 "balloon_multiline",
5705# endif
5706#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005707#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005708 "balloon_eval_term",
5709#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005710#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5711 "builtin_terms",
5712# ifdef ALL_BUILTIN_TCAPS
5713 "all_builtin_terms",
5714# endif
5715#endif
5716#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5717 || defined(FEAT_GUI_W32) \
5718 || defined(FEAT_GUI_MOTIF))
5719 "browsefilter",
5720#endif
5721#ifdef FEAT_BYTEOFF
5722 "byte_offset",
5723#endif
5724#ifdef FEAT_JOB_CHANNEL
5725 "channel",
5726#endif
5727#ifdef FEAT_CINDENT
5728 "cindent",
5729#endif
5730#ifdef FEAT_CLIENTSERVER
5731 "clientserver",
5732#endif
5733#ifdef FEAT_CLIPBOARD
5734 "clipboard",
5735#endif
5736#ifdef FEAT_CMDL_COMPL
5737 "cmdline_compl",
5738#endif
5739#ifdef FEAT_CMDHIST
5740 "cmdline_hist",
5741#endif
5742#ifdef FEAT_COMMENTS
5743 "comments",
5744#endif
5745#ifdef FEAT_CONCEAL
5746 "conceal",
5747#endif
5748#ifdef FEAT_CRYPT
5749 "cryptv",
5750 "crypt-blowfish",
5751 "crypt-blowfish2",
5752#endif
5753#ifdef FEAT_CSCOPE
5754 "cscope",
5755#endif
5756#ifdef FEAT_CURSORBIND
5757 "cursorbind",
5758#endif
5759#ifdef CURSOR_SHAPE
5760 "cursorshape",
5761#endif
5762#ifdef DEBUG
5763 "debug",
5764#endif
5765#ifdef FEAT_CON_DIALOG
5766 "dialog_con",
5767#endif
5768#ifdef FEAT_GUI_DIALOG
5769 "dialog_gui",
5770#endif
5771#ifdef FEAT_DIFF
5772 "diff",
5773#endif
5774#ifdef FEAT_DIGRAPHS
5775 "digraphs",
5776#endif
5777#ifdef FEAT_DIRECTX
5778 "directx",
5779#endif
5780#ifdef FEAT_DND
5781 "dnd",
5782#endif
5783#ifdef FEAT_EMACS_TAGS
5784 "emacs_tags",
5785#endif
5786 "eval", /* always present, of course! */
5787 "ex_extra", /* graduated feature */
5788#ifdef FEAT_SEARCH_EXTRA
5789 "extra_search",
5790#endif
5791#ifdef FEAT_FKMAP
5792 "farsi",
5793#endif
5794#ifdef FEAT_SEARCHPATH
5795 "file_in_path",
5796#endif
5797#ifdef FEAT_FILTERPIPE
5798 "filterpipe",
5799#endif
5800#ifdef FEAT_FIND_ID
5801 "find_in_path",
5802#endif
5803#ifdef FEAT_FLOAT
5804 "float",
5805#endif
5806#ifdef FEAT_FOLDING
5807 "folding",
5808#endif
5809#ifdef FEAT_FOOTER
5810 "footer",
5811#endif
5812#if !defined(USE_SYSTEM) && defined(UNIX)
5813 "fork",
5814#endif
5815#ifdef FEAT_GETTEXT
5816 "gettext",
5817#endif
5818#ifdef FEAT_GUI
5819 "gui",
5820#endif
5821#ifdef FEAT_GUI_ATHENA
5822# ifdef FEAT_GUI_NEXTAW
5823 "gui_neXtaw",
5824# else
5825 "gui_athena",
5826# endif
5827#endif
5828#ifdef FEAT_GUI_GTK
5829 "gui_gtk",
5830# ifdef USE_GTK3
5831 "gui_gtk3",
5832# else
5833 "gui_gtk2",
5834# endif
5835#endif
5836#ifdef FEAT_GUI_GNOME
5837 "gui_gnome",
5838#endif
5839#ifdef FEAT_GUI_MAC
5840 "gui_mac",
5841#endif
5842#ifdef FEAT_GUI_MOTIF
5843 "gui_motif",
5844#endif
5845#ifdef FEAT_GUI_PHOTON
5846 "gui_photon",
5847#endif
5848#ifdef FEAT_GUI_W32
5849 "gui_win32",
5850#endif
5851#ifdef FEAT_HANGULIN
5852 "hangul_input",
5853#endif
5854#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5855 "iconv",
5856#endif
5857#ifdef FEAT_INS_EXPAND
5858 "insert_expand",
5859#endif
5860#ifdef FEAT_JOB_CHANNEL
5861 "job",
5862#endif
5863#ifdef FEAT_JUMPLIST
5864 "jumplist",
5865#endif
5866#ifdef FEAT_KEYMAP
5867 "keymap",
5868#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005869 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005870#ifdef FEAT_LANGMAP
5871 "langmap",
5872#endif
5873#ifdef FEAT_LIBCALL
5874 "libcall",
5875#endif
5876#ifdef FEAT_LINEBREAK
5877 "linebreak",
5878#endif
5879#ifdef FEAT_LISP
5880 "lispindent",
5881#endif
5882#ifdef FEAT_LISTCMDS
5883 "listcmds",
5884#endif
5885#ifdef FEAT_LOCALMAP
5886 "localmap",
5887#endif
5888#ifdef FEAT_LUA
5889# ifndef DYNAMIC_LUA
5890 "lua",
5891# endif
5892#endif
5893#ifdef FEAT_MENU
5894 "menu",
5895#endif
5896#ifdef FEAT_SESSION
5897 "mksession",
5898#endif
5899#ifdef FEAT_MODIFY_FNAME
5900 "modify_fname",
5901#endif
5902#ifdef FEAT_MOUSE
5903 "mouse",
5904#endif
5905#ifdef FEAT_MOUSESHAPE
5906 "mouseshape",
5907#endif
5908#if defined(UNIX) || defined(VMS)
5909# ifdef FEAT_MOUSE_DEC
5910 "mouse_dec",
5911# endif
5912# ifdef FEAT_MOUSE_GPM
5913 "mouse_gpm",
5914# endif
5915# ifdef FEAT_MOUSE_JSB
5916 "mouse_jsbterm",
5917# endif
5918# ifdef FEAT_MOUSE_NET
5919 "mouse_netterm",
5920# endif
5921# ifdef FEAT_MOUSE_PTERM
5922 "mouse_pterm",
5923# endif
5924# ifdef FEAT_MOUSE_SGR
5925 "mouse_sgr",
5926# endif
5927# ifdef FEAT_SYSMOUSE
5928 "mouse_sysmouse",
5929# endif
5930# ifdef FEAT_MOUSE_URXVT
5931 "mouse_urxvt",
5932# endif
5933# ifdef FEAT_MOUSE_XTERM
5934 "mouse_xterm",
5935# endif
5936#endif
5937#ifdef FEAT_MBYTE
5938 "multi_byte",
5939#endif
5940#ifdef FEAT_MBYTE_IME
5941 "multi_byte_ime",
5942#endif
5943#ifdef FEAT_MULTI_LANG
5944 "multi_lang",
5945#endif
5946#ifdef FEAT_MZSCHEME
5947#ifndef DYNAMIC_MZSCHEME
5948 "mzscheme",
5949#endif
5950#endif
5951#ifdef FEAT_NUM64
5952 "num64",
5953#endif
5954#ifdef FEAT_OLE
5955 "ole",
5956#endif
5957 "packages",
5958#ifdef FEAT_PATH_EXTRA
5959 "path_extra",
5960#endif
5961#ifdef FEAT_PERL
5962#ifndef DYNAMIC_PERL
5963 "perl",
5964#endif
5965#endif
5966#ifdef FEAT_PERSISTENT_UNDO
5967 "persistent_undo",
5968#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005969#if defined(FEAT_PYTHON)
5970 "python_compiled",
5971# if defined(DYNAMIC_PYTHON)
5972 "python_dynamic",
5973# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005974 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005975 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005976# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005977#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005978#if defined(FEAT_PYTHON3)
5979 "python3_compiled",
5980# if defined(DYNAMIC_PYTHON3)
5981 "python3_dynamic",
5982# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005983 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005984 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005985# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005986#endif
5987#ifdef FEAT_POSTSCRIPT
5988 "postscript",
5989#endif
5990#ifdef FEAT_PRINTER
5991 "printer",
5992#endif
5993#ifdef FEAT_PROFILE
5994 "profile",
5995#endif
5996#ifdef FEAT_RELTIME
5997 "reltime",
5998#endif
5999#ifdef FEAT_QUICKFIX
6000 "quickfix",
6001#endif
6002#ifdef FEAT_RIGHTLEFT
6003 "rightleft",
6004#endif
6005#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6006 "ruby",
6007#endif
6008#ifdef FEAT_SCROLLBIND
6009 "scrollbind",
6010#endif
6011#ifdef FEAT_CMDL_INFO
6012 "showcmd",
6013 "cmdline_info",
6014#endif
6015#ifdef FEAT_SIGNS
6016 "signs",
6017#endif
6018#ifdef FEAT_SMARTINDENT
6019 "smartindent",
6020#endif
6021#ifdef STARTUPTIME
6022 "startuptime",
6023#endif
6024#ifdef FEAT_STL_OPT
6025 "statusline",
6026#endif
6027#ifdef FEAT_SUN_WORKSHOP
6028 "sun_workshop",
6029#endif
6030#ifdef FEAT_NETBEANS_INTG
6031 "netbeans_intg",
6032#endif
6033#ifdef FEAT_SPELL
6034 "spell",
6035#endif
6036#ifdef FEAT_SYN_HL
6037 "syntax",
6038#endif
6039#if defined(USE_SYSTEM) || !defined(UNIX)
6040 "system",
6041#endif
6042#ifdef FEAT_TAG_BINS
6043 "tag_binary",
6044#endif
6045#ifdef FEAT_TAG_OLDSTATIC
6046 "tag_old_static",
6047#endif
6048#ifdef FEAT_TAG_ANYWHITE
6049 "tag_any_white",
6050#endif
6051#ifdef FEAT_TCL
6052# ifndef DYNAMIC_TCL
6053 "tcl",
6054# endif
6055#endif
6056#ifdef FEAT_TERMGUICOLORS
6057 "termguicolors",
6058#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006059#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006060 "terminal",
6061#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006062#ifdef TERMINFO
6063 "terminfo",
6064#endif
6065#ifdef FEAT_TERMRESPONSE
6066 "termresponse",
6067#endif
6068#ifdef FEAT_TEXTOBJ
6069 "textobjects",
6070#endif
6071#ifdef HAVE_TGETENT
6072 "tgetent",
6073#endif
6074#ifdef FEAT_TIMERS
6075 "timers",
6076#endif
6077#ifdef FEAT_TITLE
6078 "title",
6079#endif
6080#ifdef FEAT_TOOLBAR
6081 "toolbar",
6082#endif
6083#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6084 "unnamedplus",
6085#endif
6086#ifdef FEAT_USR_CMDS
6087 "user-commands", /* was accidentally included in 5.4 */
6088 "user_commands",
6089#endif
6090#ifdef FEAT_VIMINFO
6091 "viminfo",
6092#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006093 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006094#ifdef FEAT_VIRTUALEDIT
6095 "virtualedit",
6096#endif
6097 "visual",
6098#ifdef FEAT_VISUALEXTRA
6099 "visualextra",
6100#endif
6101#ifdef FEAT_VREPLACE
6102 "vreplace",
6103#endif
6104#ifdef FEAT_WILDIGN
6105 "wildignore",
6106#endif
6107#ifdef FEAT_WILDMENU
6108 "wildmenu",
6109#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006110 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006111#ifdef FEAT_WAK
6112 "winaltkeys",
6113#endif
6114#ifdef FEAT_WRITEBACKUP
6115 "writebackup",
6116#endif
6117#ifdef FEAT_XIM
6118 "xim",
6119#endif
6120#ifdef FEAT_XFONTSET
6121 "xfontset",
6122#endif
6123#ifdef FEAT_XPM_W32
6124 "xpm",
6125 "xpm_w32", /* for backward compatibility */
6126#else
6127# if defined(HAVE_XPM)
6128 "xpm",
6129# endif
6130#endif
6131#ifdef USE_XSMP
6132 "xsmp",
6133#endif
6134#ifdef USE_XSMP_INTERACT
6135 "xsmp_interact",
6136#endif
6137#ifdef FEAT_XCLIPBOARD
6138 "xterm_clipboard",
6139#endif
6140#ifdef FEAT_XTERM_SAVE
6141 "xterm_save",
6142#endif
6143#if defined(UNIX) && defined(FEAT_X11)
6144 "X11",
6145#endif
6146 NULL
6147 };
6148
6149 name = get_tv_string(&argvars[0]);
6150 for (i = 0; has_list[i] != NULL; ++i)
6151 if (STRICMP(name, has_list[i]) == 0)
6152 {
6153 n = TRUE;
6154 break;
6155 }
6156
6157 if (n == FALSE)
6158 {
6159 if (STRNICMP(name, "patch", 5) == 0)
6160 {
6161 if (name[5] == '-'
6162 && STRLEN(name) >= 11
6163 && vim_isdigit(name[6])
6164 && vim_isdigit(name[8])
6165 && vim_isdigit(name[10]))
6166 {
6167 int major = atoi((char *)name + 6);
6168 int minor = atoi((char *)name + 8);
6169
6170 /* Expect "patch-9.9.01234". */
6171 n = (major < VIM_VERSION_MAJOR
6172 || (major == VIM_VERSION_MAJOR
6173 && (minor < VIM_VERSION_MINOR
6174 || (minor == VIM_VERSION_MINOR
6175 && has_patch(atoi((char *)name + 10))))));
6176 }
6177 else
6178 n = has_patch(atoi((char *)name + 5));
6179 }
6180 else if (STRICMP(name, "vim_starting") == 0)
6181 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006182 else if (STRICMP(name, "ttyin") == 0)
6183 n = mch_input_isatty();
6184 else if (STRICMP(name, "ttyout") == 0)
6185 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006186#ifdef FEAT_MBYTE
6187 else if (STRICMP(name, "multi_byte_encoding") == 0)
6188 n = has_mbyte;
6189#endif
6190#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6191 else if (STRICMP(name, "balloon_multiline") == 0)
6192 n = multiline_balloon_available();
6193#endif
6194#ifdef DYNAMIC_TCL
6195 else if (STRICMP(name, "tcl") == 0)
6196 n = tcl_enabled(FALSE);
6197#endif
6198#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6199 else if (STRICMP(name, "iconv") == 0)
6200 n = iconv_enabled(FALSE);
6201#endif
6202#ifdef DYNAMIC_LUA
6203 else if (STRICMP(name, "lua") == 0)
6204 n = lua_enabled(FALSE);
6205#endif
6206#ifdef DYNAMIC_MZSCHEME
6207 else if (STRICMP(name, "mzscheme") == 0)
6208 n = mzscheme_enabled(FALSE);
6209#endif
6210#ifdef DYNAMIC_RUBY
6211 else if (STRICMP(name, "ruby") == 0)
6212 n = ruby_enabled(FALSE);
6213#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006214#ifdef DYNAMIC_PYTHON
6215 else if (STRICMP(name, "python") == 0)
6216 n = python_enabled(FALSE);
6217#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006218#ifdef DYNAMIC_PYTHON3
6219 else if (STRICMP(name, "python3") == 0)
6220 n = python3_enabled(FALSE);
6221#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006222#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6223 else if (STRICMP(name, "pythonx") == 0)
6224 {
6225# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6226 if (p_pyx == 0)
6227 n = python3_enabled(FALSE) || python_enabled(FALSE);
6228 else if (p_pyx == 3)
6229 n = python3_enabled(FALSE);
6230 else if (p_pyx == 2)
6231 n = python_enabled(FALSE);
6232# elif defined(DYNAMIC_PYTHON)
6233 n = python_enabled(FALSE);
6234# elif defined(DYNAMIC_PYTHON3)
6235 n = python3_enabled(FALSE);
6236# endif
6237 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006238#endif
6239#ifdef DYNAMIC_PERL
6240 else if (STRICMP(name, "perl") == 0)
6241 n = perl_enabled(FALSE);
6242#endif
6243#ifdef FEAT_GUI
6244 else if (STRICMP(name, "gui_running") == 0)
6245 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006246# ifdef FEAT_BROWSE
6247 else if (STRICMP(name, "browse") == 0)
6248 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6249# endif
6250#endif
6251#ifdef FEAT_SYN_HL
6252 else if (STRICMP(name, "syntax_items") == 0)
6253 n = syntax_present(curwin);
6254#endif
6255#if defined(WIN3264)
6256 else if (STRICMP(name, "win95") == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006257 n = FALSE; /* Win9x is no more supported. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006258#endif
6259#ifdef FEAT_NETBEANS_INTG
6260 else if (STRICMP(name, "netbeans_enabled") == 0)
6261 n = netbeans_active();
6262#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006263#if defined(FEAT_TERMINAL) && defined(WIN3264)
6264 else if (STRICMP(name, "terminal") == 0)
6265 n = terminal_enabled();
6266#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006267 }
6268
6269 rettv->vval.v_number = n;
6270}
6271
6272/*
6273 * "has_key()" function
6274 */
6275 static void
6276f_has_key(typval_T *argvars, typval_T *rettv)
6277{
6278 if (argvars[0].v_type != VAR_DICT)
6279 {
6280 EMSG(_(e_dictreq));
6281 return;
6282 }
6283 if (argvars[0].vval.v_dict == NULL)
6284 return;
6285
6286 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6287 get_tv_string(&argvars[1]), -1) != NULL;
6288}
6289
6290/*
6291 * "haslocaldir()" function
6292 */
6293 static void
6294f_haslocaldir(typval_T *argvars, typval_T *rettv)
6295{
6296 win_T *wp = NULL;
6297
6298 wp = find_tabwin(&argvars[0], &argvars[1]);
6299 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6300}
6301
6302/*
6303 * "hasmapto()" function
6304 */
6305 static void
6306f_hasmapto(typval_T *argvars, typval_T *rettv)
6307{
6308 char_u *name;
6309 char_u *mode;
6310 char_u buf[NUMBUFLEN];
6311 int abbr = FALSE;
6312
6313 name = get_tv_string(&argvars[0]);
6314 if (argvars[1].v_type == VAR_UNKNOWN)
6315 mode = (char_u *)"nvo";
6316 else
6317 {
6318 mode = get_tv_string_buf(&argvars[1], buf);
6319 if (argvars[2].v_type != VAR_UNKNOWN)
6320 abbr = (int)get_tv_number(&argvars[2]);
6321 }
6322
6323 if (map_to_exists(name, mode, abbr))
6324 rettv->vval.v_number = TRUE;
6325 else
6326 rettv->vval.v_number = FALSE;
6327}
6328
6329/*
6330 * "histadd()" function
6331 */
6332 static void
6333f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6334{
6335#ifdef FEAT_CMDHIST
6336 int histype;
6337 char_u *str;
6338 char_u buf[NUMBUFLEN];
6339#endif
6340
6341 rettv->vval.v_number = FALSE;
6342 if (check_restricted() || check_secure())
6343 return;
6344#ifdef FEAT_CMDHIST
6345 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6346 histype = str != NULL ? get_histtype(str) : -1;
6347 if (histype >= 0)
6348 {
6349 str = get_tv_string_buf(&argvars[1], buf);
6350 if (*str != NUL)
6351 {
6352 init_history();
6353 add_to_history(histype, str, FALSE, NUL);
6354 rettv->vval.v_number = TRUE;
6355 return;
6356 }
6357 }
6358#endif
6359}
6360
6361/*
6362 * "histdel()" function
6363 */
6364 static void
6365f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6366{
6367#ifdef FEAT_CMDHIST
6368 int n;
6369 char_u buf[NUMBUFLEN];
6370 char_u *str;
6371
6372 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6373 if (str == NULL)
6374 n = 0;
6375 else if (argvars[1].v_type == VAR_UNKNOWN)
6376 /* only one argument: clear entire history */
6377 n = clr_history(get_histtype(str));
6378 else if (argvars[1].v_type == VAR_NUMBER)
6379 /* index given: remove that entry */
6380 n = del_history_idx(get_histtype(str),
6381 (int)get_tv_number(&argvars[1]));
6382 else
6383 /* string given: remove all matching entries */
6384 n = del_history_entry(get_histtype(str),
6385 get_tv_string_buf(&argvars[1], buf));
6386 rettv->vval.v_number = n;
6387#endif
6388}
6389
6390/*
6391 * "histget()" function
6392 */
6393 static void
6394f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6395{
6396#ifdef FEAT_CMDHIST
6397 int type;
6398 int idx;
6399 char_u *str;
6400
6401 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6402 if (str == NULL)
6403 rettv->vval.v_string = NULL;
6404 else
6405 {
6406 type = get_histtype(str);
6407 if (argvars[1].v_type == VAR_UNKNOWN)
6408 idx = get_history_idx(type);
6409 else
6410 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6411 /* -1 on type error */
6412 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6413 }
6414#else
6415 rettv->vval.v_string = NULL;
6416#endif
6417 rettv->v_type = VAR_STRING;
6418}
6419
6420/*
6421 * "histnr()" function
6422 */
6423 static void
6424f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6425{
6426 int i;
6427
6428#ifdef FEAT_CMDHIST
6429 char_u *history = get_tv_string_chk(&argvars[0]);
6430
6431 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6432 if (i >= HIST_CMD && i < HIST_COUNT)
6433 i = get_history_idx(i);
6434 else
6435#endif
6436 i = -1;
6437 rettv->vval.v_number = i;
6438}
6439
6440/*
6441 * "highlightID(name)" function
6442 */
6443 static void
6444f_hlID(typval_T *argvars, typval_T *rettv)
6445{
6446 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6447}
6448
6449/*
6450 * "highlight_exists()" function
6451 */
6452 static void
6453f_hlexists(typval_T *argvars, typval_T *rettv)
6454{
6455 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6456}
6457
6458/*
6459 * "hostname()" function
6460 */
6461 static void
6462f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6463{
6464 char_u hostname[256];
6465
6466 mch_get_host_name(hostname, 256);
6467 rettv->v_type = VAR_STRING;
6468 rettv->vval.v_string = vim_strsave(hostname);
6469}
6470
6471/*
6472 * iconv() function
6473 */
6474 static void
6475f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6476{
6477#ifdef FEAT_MBYTE
6478 char_u buf1[NUMBUFLEN];
6479 char_u buf2[NUMBUFLEN];
6480 char_u *from, *to, *str;
6481 vimconv_T vimconv;
6482#endif
6483
6484 rettv->v_type = VAR_STRING;
6485 rettv->vval.v_string = NULL;
6486
6487#ifdef FEAT_MBYTE
6488 str = get_tv_string(&argvars[0]);
6489 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6490 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6491 vimconv.vc_type = CONV_NONE;
6492 convert_setup(&vimconv, from, to);
6493
6494 /* If the encodings are equal, no conversion needed. */
6495 if (vimconv.vc_type == CONV_NONE)
6496 rettv->vval.v_string = vim_strsave(str);
6497 else
6498 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6499
6500 convert_setup(&vimconv, NULL, NULL);
6501 vim_free(from);
6502 vim_free(to);
6503#endif
6504}
6505
6506/*
6507 * "indent()" function
6508 */
6509 static void
6510f_indent(typval_T *argvars, typval_T *rettv)
6511{
6512 linenr_T lnum;
6513
6514 lnum = get_tv_lnum(argvars);
6515 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6516 rettv->vval.v_number = get_indent_lnum(lnum);
6517 else
6518 rettv->vval.v_number = -1;
6519}
6520
6521/*
6522 * "index()" function
6523 */
6524 static void
6525f_index(typval_T *argvars, typval_T *rettv)
6526{
6527 list_T *l;
6528 listitem_T *item;
6529 long idx = 0;
6530 int ic = FALSE;
6531
6532 rettv->vval.v_number = -1;
6533 if (argvars[0].v_type != VAR_LIST)
6534 {
6535 EMSG(_(e_listreq));
6536 return;
6537 }
6538 l = argvars[0].vval.v_list;
6539 if (l != NULL)
6540 {
6541 item = l->lv_first;
6542 if (argvars[2].v_type != VAR_UNKNOWN)
6543 {
6544 int error = FALSE;
6545
6546 /* Start at specified item. Use the cached index that list_find()
6547 * sets, so that a negative number also works. */
6548 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6549 idx = l->lv_idx;
6550 if (argvars[3].v_type != VAR_UNKNOWN)
6551 ic = (int)get_tv_number_chk(&argvars[3], &error);
6552 if (error)
6553 item = NULL;
6554 }
6555
6556 for ( ; item != NULL; item = item->li_next, ++idx)
6557 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6558 {
6559 rettv->vval.v_number = idx;
6560 break;
6561 }
6562 }
6563}
6564
6565static int inputsecret_flag = 0;
6566
6567/*
6568 * "input()" function
6569 * Also handles inputsecret() when inputsecret is set.
6570 */
6571 static void
6572f_input(typval_T *argvars, typval_T *rettv)
6573{
6574 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6575}
6576
6577/*
6578 * "inputdialog()" function
6579 */
6580 static void
6581f_inputdialog(typval_T *argvars, typval_T *rettv)
6582{
6583#if defined(FEAT_GUI_TEXTDIALOG)
6584 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6585 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6586 {
6587 char_u *message;
6588 char_u buf[NUMBUFLEN];
6589 char_u *defstr = (char_u *)"";
6590
6591 message = get_tv_string_chk(&argvars[0]);
6592 if (argvars[1].v_type != VAR_UNKNOWN
6593 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6594 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6595 else
6596 IObuff[0] = NUL;
6597 if (message != NULL && defstr != NULL
6598 && do_dialog(VIM_QUESTION, NULL, message,
6599 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6600 rettv->vval.v_string = vim_strsave(IObuff);
6601 else
6602 {
6603 if (message != NULL && defstr != NULL
6604 && argvars[1].v_type != VAR_UNKNOWN
6605 && argvars[2].v_type != VAR_UNKNOWN)
6606 rettv->vval.v_string = vim_strsave(
6607 get_tv_string_buf(&argvars[2], buf));
6608 else
6609 rettv->vval.v_string = NULL;
6610 }
6611 rettv->v_type = VAR_STRING;
6612 }
6613 else
6614#endif
6615 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6616}
6617
6618/*
6619 * "inputlist()" function
6620 */
6621 static void
6622f_inputlist(typval_T *argvars, typval_T *rettv)
6623{
6624 listitem_T *li;
6625 int selected;
6626 int mouse_used;
6627
6628#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006629 /* While starting up, there is no place to enter text. When running tests
6630 * with --not-a-term we assume feedkeys() will be used. */
6631 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006632 return;
6633#endif
6634 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6635 {
6636 EMSG2(_(e_listarg), "inputlist()");
6637 return;
6638 }
6639
6640 msg_start();
6641 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6642 lines_left = Rows; /* avoid more prompt */
6643 msg_scroll = TRUE;
6644 msg_clr_eos();
6645
6646 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6647 {
6648 msg_puts(get_tv_string(&li->li_tv));
6649 msg_putchar('\n');
6650 }
6651
6652 /* Ask for choice. */
6653 selected = prompt_for_number(&mouse_used);
6654 if (mouse_used)
6655 selected -= lines_left;
6656
6657 rettv->vval.v_number = selected;
6658}
6659
6660
6661static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6662
6663/*
6664 * "inputrestore()" function
6665 */
6666 static void
6667f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6668{
6669 if (ga_userinput.ga_len > 0)
6670 {
6671 --ga_userinput.ga_len;
6672 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6673 + ga_userinput.ga_len);
6674 /* default return is zero == OK */
6675 }
6676 else if (p_verbose > 1)
6677 {
6678 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6679 rettv->vval.v_number = 1; /* Failed */
6680 }
6681}
6682
6683/*
6684 * "inputsave()" function
6685 */
6686 static void
6687f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6688{
6689 /* Add an entry to the stack of typeahead storage. */
6690 if (ga_grow(&ga_userinput, 1) == OK)
6691 {
6692 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6693 + ga_userinput.ga_len);
6694 ++ga_userinput.ga_len;
6695 /* default return is zero == OK */
6696 }
6697 else
6698 rettv->vval.v_number = 1; /* Failed */
6699}
6700
6701/*
6702 * "inputsecret()" function
6703 */
6704 static void
6705f_inputsecret(typval_T *argvars, typval_T *rettv)
6706{
6707 ++cmdline_star;
6708 ++inputsecret_flag;
6709 f_input(argvars, rettv);
6710 --cmdline_star;
6711 --inputsecret_flag;
6712}
6713
6714/*
6715 * "insert()" function
6716 */
6717 static void
6718f_insert(typval_T *argvars, typval_T *rettv)
6719{
6720 long before = 0;
6721 listitem_T *item;
6722 list_T *l;
6723 int error = FALSE;
6724
6725 if (argvars[0].v_type != VAR_LIST)
6726 EMSG2(_(e_listarg), "insert()");
6727 else if ((l = argvars[0].vval.v_list) != NULL
6728 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6729 {
6730 if (argvars[2].v_type != VAR_UNKNOWN)
6731 before = (long)get_tv_number_chk(&argvars[2], &error);
6732 if (error)
6733 return; /* type error; errmsg already given */
6734
6735 if (before == l->lv_len)
6736 item = NULL;
6737 else
6738 {
6739 item = list_find(l, before);
6740 if (item == NULL)
6741 {
6742 EMSGN(_(e_listidx), before);
6743 l = NULL;
6744 }
6745 }
6746 if (l != NULL)
6747 {
6748 list_insert_tv(l, &argvars[1], item);
6749 copy_tv(&argvars[0], rettv);
6750 }
6751 }
6752}
6753
6754/*
6755 * "invert(expr)" function
6756 */
6757 static void
6758f_invert(typval_T *argvars, typval_T *rettv)
6759{
6760 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6761}
6762
6763/*
6764 * "isdirectory()" function
6765 */
6766 static void
6767f_isdirectory(typval_T *argvars, typval_T *rettv)
6768{
6769 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6770}
6771
6772/*
6773 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6774 * or it refers to a List or Dictionary that is locked.
6775 */
6776 static int
6777tv_islocked(typval_T *tv)
6778{
6779 return (tv->v_lock & VAR_LOCKED)
6780 || (tv->v_type == VAR_LIST
6781 && tv->vval.v_list != NULL
6782 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6783 || (tv->v_type == VAR_DICT
6784 && tv->vval.v_dict != NULL
6785 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6786}
6787
6788/*
6789 * "islocked()" function
6790 */
6791 static void
6792f_islocked(typval_T *argvars, typval_T *rettv)
6793{
6794 lval_T lv;
6795 char_u *end;
6796 dictitem_T *di;
6797
6798 rettv->vval.v_number = -1;
6799 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006800 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006801 if (end != NULL && lv.ll_name != NULL)
6802 {
6803 if (*end != NUL)
6804 EMSG(_(e_trailing));
6805 else
6806 {
6807 if (lv.ll_tv == NULL)
6808 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006809 di = find_var(lv.ll_name, NULL, TRUE);
6810 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006811 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006812 /* Consider a variable locked when:
6813 * 1. the variable itself is locked
6814 * 2. the value of the variable is locked.
6815 * 3. the List or Dict value is locked.
6816 */
6817 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6818 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006819 }
6820 }
6821 else if (lv.ll_range)
6822 EMSG(_("E786: Range not allowed"));
6823 else if (lv.ll_newkey != NULL)
6824 EMSG2(_(e_dictkey), lv.ll_newkey);
6825 else if (lv.ll_list != NULL)
6826 /* List item. */
6827 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6828 else
6829 /* Dictionary item. */
6830 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6831 }
6832 }
6833
6834 clear_lval(&lv);
6835}
6836
6837#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6838/*
6839 * "isnan()" function
6840 */
6841 static void
6842f_isnan(typval_T *argvars, typval_T *rettv)
6843{
6844 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6845 && isnan(argvars[0].vval.v_float);
6846}
6847#endif
6848
6849/*
6850 * "items(dict)" function
6851 */
6852 static void
6853f_items(typval_T *argvars, typval_T *rettv)
6854{
6855 dict_list(argvars, rettv, 2);
6856}
6857
6858#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6859/*
6860 * Get the job from the argument.
6861 * Returns NULL if the job is invalid.
6862 */
6863 static job_T *
6864get_job_arg(typval_T *tv)
6865{
6866 job_T *job;
6867
6868 if (tv->v_type != VAR_JOB)
6869 {
6870 EMSG2(_(e_invarg2), get_tv_string(tv));
6871 return NULL;
6872 }
6873 job = tv->vval.v_job;
6874
6875 if (job == NULL)
6876 EMSG(_("E916: not a valid job"));
6877 return job;
6878}
6879
6880/*
6881 * "job_getchannel()" function
6882 */
6883 static void
6884f_job_getchannel(typval_T *argvars, typval_T *rettv)
6885{
6886 job_T *job = get_job_arg(&argvars[0]);
6887
6888 if (job != NULL)
6889 {
6890 rettv->v_type = VAR_CHANNEL;
6891 rettv->vval.v_channel = job->jv_channel;
6892 if (job->jv_channel != NULL)
6893 ++job->jv_channel->ch_refcount;
6894 }
6895}
6896
6897/*
6898 * "job_info()" function
6899 */
6900 static void
6901f_job_info(typval_T *argvars, typval_T *rettv)
6902{
6903 job_T *job = get_job_arg(&argvars[0]);
6904
6905 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6906 job_info(job, rettv->vval.v_dict);
6907}
6908
6909/*
6910 * "job_setoptions()" function
6911 */
6912 static void
6913f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6914{
6915 job_T *job = get_job_arg(&argvars[0]);
6916 jobopt_T opt;
6917
6918 if (job == NULL)
6919 return;
6920 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02006921 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006922 job_set_options(job, &opt);
6923 free_job_options(&opt);
6924}
6925
6926/*
6927 * "job_start()" function
6928 */
6929 static void
6930f_job_start(typval_T *argvars, typval_T *rettv)
6931{
6932 rettv->v_type = VAR_JOB;
6933 if (check_restricted() || check_secure())
6934 return;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006935 rettv->vval.v_job = job_start(argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006936}
6937
6938/*
6939 * "job_status()" function
6940 */
6941 static void
6942f_job_status(typval_T *argvars, typval_T *rettv)
6943{
6944 job_T *job = get_job_arg(&argvars[0]);
6945
6946 if (job != NULL)
6947 {
6948 rettv->v_type = VAR_STRING;
6949 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6950 }
6951}
6952
6953/*
6954 * "job_stop()" function
6955 */
6956 static void
6957f_job_stop(typval_T *argvars, typval_T *rettv)
6958{
6959 job_T *job = get_job_arg(&argvars[0]);
6960
6961 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006962 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006963}
6964#endif
6965
6966/*
6967 * "join()" function
6968 */
6969 static void
6970f_join(typval_T *argvars, typval_T *rettv)
6971{
6972 garray_T ga;
6973 char_u *sep;
6974
6975 if (argvars[0].v_type != VAR_LIST)
6976 {
6977 EMSG(_(e_listreq));
6978 return;
6979 }
6980 if (argvars[0].vval.v_list == NULL)
6981 return;
6982 if (argvars[1].v_type == VAR_UNKNOWN)
6983 sep = (char_u *)" ";
6984 else
6985 sep = get_tv_string_chk(&argvars[1]);
6986
6987 rettv->v_type = VAR_STRING;
6988
6989 if (sep != NULL)
6990 {
6991 ga_init2(&ga, (int)sizeof(char), 80);
6992 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
6993 ga_append(&ga, NUL);
6994 rettv->vval.v_string = (char_u *)ga.ga_data;
6995 }
6996 else
6997 rettv->vval.v_string = NULL;
6998}
6999
7000/*
7001 * "js_decode()" function
7002 */
7003 static void
7004f_js_decode(typval_T *argvars, typval_T *rettv)
7005{
7006 js_read_T reader;
7007
7008 reader.js_buf = get_tv_string(&argvars[0]);
7009 reader.js_fill = NULL;
7010 reader.js_used = 0;
7011 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7012 EMSG(_(e_invarg));
7013}
7014
7015/*
7016 * "js_encode()" function
7017 */
7018 static void
7019f_js_encode(typval_T *argvars, typval_T *rettv)
7020{
7021 rettv->v_type = VAR_STRING;
7022 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7023}
7024
7025/*
7026 * "json_decode()" function
7027 */
7028 static void
7029f_json_decode(typval_T *argvars, typval_T *rettv)
7030{
7031 js_read_T reader;
7032
7033 reader.js_buf = get_tv_string(&argvars[0]);
7034 reader.js_fill = NULL;
7035 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007036 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007037}
7038
7039/*
7040 * "json_encode()" function
7041 */
7042 static void
7043f_json_encode(typval_T *argvars, typval_T *rettv)
7044{
7045 rettv->v_type = VAR_STRING;
7046 rettv->vval.v_string = json_encode(&argvars[0], 0);
7047}
7048
7049/*
7050 * "keys()" function
7051 */
7052 static void
7053f_keys(typval_T *argvars, typval_T *rettv)
7054{
7055 dict_list(argvars, rettv, 0);
7056}
7057
7058/*
7059 * "last_buffer_nr()" function.
7060 */
7061 static void
7062f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7063{
7064 int n = 0;
7065 buf_T *buf;
7066
Bram Moolenaar29323592016-07-24 22:04:11 +02007067 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007068 if (n < buf->b_fnum)
7069 n = buf->b_fnum;
7070
7071 rettv->vval.v_number = n;
7072}
7073
7074/*
7075 * "len()" function
7076 */
7077 static void
7078f_len(typval_T *argvars, typval_T *rettv)
7079{
7080 switch (argvars[0].v_type)
7081 {
7082 case VAR_STRING:
7083 case VAR_NUMBER:
7084 rettv->vval.v_number = (varnumber_T)STRLEN(
7085 get_tv_string(&argvars[0]));
7086 break;
7087 case VAR_LIST:
7088 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7089 break;
7090 case VAR_DICT:
7091 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7092 break;
7093 case VAR_UNKNOWN:
7094 case VAR_SPECIAL:
7095 case VAR_FLOAT:
7096 case VAR_FUNC:
7097 case VAR_PARTIAL:
7098 case VAR_JOB:
7099 case VAR_CHANNEL:
7100 EMSG(_("E701: Invalid type for len()"));
7101 break;
7102 }
7103}
7104
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007105 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007106libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007107{
7108#ifdef FEAT_LIBCALL
7109 char_u *string_in;
7110 char_u **string_result;
7111 int nr_result;
7112#endif
7113
7114 rettv->v_type = type;
7115 if (type != VAR_NUMBER)
7116 rettv->vval.v_string = NULL;
7117
7118 if (check_restricted() || check_secure())
7119 return;
7120
7121#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007122 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007123 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7124 {
7125 string_in = NULL;
7126 if (argvars[2].v_type == VAR_STRING)
7127 string_in = argvars[2].vval.v_string;
7128 if (type == VAR_NUMBER)
7129 string_result = NULL;
7130 else
7131 string_result = &rettv->vval.v_string;
7132 if (mch_libcall(argvars[0].vval.v_string,
7133 argvars[1].vval.v_string,
7134 string_in,
7135 argvars[2].vval.v_number,
7136 string_result,
7137 &nr_result) == OK
7138 && type == VAR_NUMBER)
7139 rettv->vval.v_number = nr_result;
7140 }
7141#endif
7142}
7143
7144/*
7145 * "libcall()" function
7146 */
7147 static void
7148f_libcall(typval_T *argvars, typval_T *rettv)
7149{
7150 libcall_common(argvars, rettv, VAR_STRING);
7151}
7152
7153/*
7154 * "libcallnr()" function
7155 */
7156 static void
7157f_libcallnr(typval_T *argvars, typval_T *rettv)
7158{
7159 libcall_common(argvars, rettv, VAR_NUMBER);
7160}
7161
7162/*
7163 * "line(string)" function
7164 */
7165 static void
7166f_line(typval_T *argvars, typval_T *rettv)
7167{
7168 linenr_T lnum = 0;
7169 pos_T *fp;
7170 int fnum;
7171
7172 fp = var2fpos(&argvars[0], TRUE, &fnum);
7173 if (fp != NULL)
7174 lnum = fp->lnum;
7175 rettv->vval.v_number = lnum;
7176}
7177
7178/*
7179 * "line2byte(lnum)" function
7180 */
7181 static void
7182f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7183{
7184#ifndef FEAT_BYTEOFF
7185 rettv->vval.v_number = -1;
7186#else
7187 linenr_T lnum;
7188
7189 lnum = get_tv_lnum(argvars);
7190 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7191 rettv->vval.v_number = -1;
7192 else
7193 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7194 if (rettv->vval.v_number >= 0)
7195 ++rettv->vval.v_number;
7196#endif
7197}
7198
7199/*
7200 * "lispindent(lnum)" function
7201 */
7202 static void
7203f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7204{
7205#ifdef FEAT_LISP
7206 pos_T pos;
7207 linenr_T lnum;
7208
7209 pos = curwin->w_cursor;
7210 lnum = get_tv_lnum(argvars);
7211 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7212 {
7213 curwin->w_cursor.lnum = lnum;
7214 rettv->vval.v_number = get_lisp_indent();
7215 curwin->w_cursor = pos;
7216 }
7217 else
7218#endif
7219 rettv->vval.v_number = -1;
7220}
7221
7222/*
7223 * "localtime()" function
7224 */
7225 static void
7226f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7227{
7228 rettv->vval.v_number = (varnumber_T)time(NULL);
7229}
7230
7231static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7232
7233 static void
7234get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7235{
7236 char_u *keys;
7237 char_u *which;
7238 char_u buf[NUMBUFLEN];
7239 char_u *keys_buf = NULL;
7240 char_u *rhs;
7241 int mode;
7242 int abbr = FALSE;
7243 int get_dict = FALSE;
7244 mapblock_T *mp;
7245 int buffer_local;
7246
7247 /* return empty string for failure */
7248 rettv->v_type = VAR_STRING;
7249 rettv->vval.v_string = NULL;
7250
7251 keys = get_tv_string(&argvars[0]);
7252 if (*keys == NUL)
7253 return;
7254
7255 if (argvars[1].v_type != VAR_UNKNOWN)
7256 {
7257 which = get_tv_string_buf_chk(&argvars[1], buf);
7258 if (argvars[2].v_type != VAR_UNKNOWN)
7259 {
7260 abbr = (int)get_tv_number(&argvars[2]);
7261 if (argvars[3].v_type != VAR_UNKNOWN)
7262 get_dict = (int)get_tv_number(&argvars[3]);
7263 }
7264 }
7265 else
7266 which = (char_u *)"";
7267 if (which == NULL)
7268 return;
7269
7270 mode = get_map_mode(&which, 0);
7271
7272 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7273 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7274 vim_free(keys_buf);
7275
7276 if (!get_dict)
7277 {
7278 /* Return a string. */
7279 if (rhs != NULL)
7280 rettv->vval.v_string = str2special_save(rhs, FALSE);
7281
7282 }
7283 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7284 {
7285 /* Return a dictionary. */
7286 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7287 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7288 dict_T *dict = rettv->vval.v_dict;
7289
7290 dict_add_nr_str(dict, "lhs", 0L, lhs);
7291 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7292 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7293 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7294 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7295 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7296 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7297 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7298 dict_add_nr_str(dict, "mode", 0L, mapmode);
7299
7300 vim_free(lhs);
7301 vim_free(mapmode);
7302 }
7303}
7304
7305#ifdef FEAT_FLOAT
7306/*
7307 * "log()" function
7308 */
7309 static void
7310f_log(typval_T *argvars, typval_T *rettv)
7311{
7312 float_T f = 0.0;
7313
7314 rettv->v_type = VAR_FLOAT;
7315 if (get_float_arg(argvars, &f) == OK)
7316 rettv->vval.v_float = log(f);
7317 else
7318 rettv->vval.v_float = 0.0;
7319}
7320
7321/*
7322 * "log10()" function
7323 */
7324 static void
7325f_log10(typval_T *argvars, typval_T *rettv)
7326{
7327 float_T f = 0.0;
7328
7329 rettv->v_type = VAR_FLOAT;
7330 if (get_float_arg(argvars, &f) == OK)
7331 rettv->vval.v_float = log10(f);
7332 else
7333 rettv->vval.v_float = 0.0;
7334}
7335#endif
7336
7337#ifdef FEAT_LUA
7338/*
7339 * "luaeval()" function
7340 */
7341 static void
7342f_luaeval(typval_T *argvars, typval_T *rettv)
7343{
7344 char_u *str;
7345 char_u buf[NUMBUFLEN];
7346
7347 str = get_tv_string_buf(&argvars[0], buf);
7348 do_luaeval(str, argvars + 1, rettv);
7349}
7350#endif
7351
7352/*
7353 * "map()" function
7354 */
7355 static void
7356f_map(typval_T *argvars, typval_T *rettv)
7357{
7358 filter_map(argvars, rettv, TRUE);
7359}
7360
7361/*
7362 * "maparg()" function
7363 */
7364 static void
7365f_maparg(typval_T *argvars, typval_T *rettv)
7366{
7367 get_maparg(argvars, rettv, TRUE);
7368}
7369
7370/*
7371 * "mapcheck()" function
7372 */
7373 static void
7374f_mapcheck(typval_T *argvars, typval_T *rettv)
7375{
7376 get_maparg(argvars, rettv, FALSE);
7377}
7378
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007379typedef enum
7380{
7381 MATCH_END, /* matchend() */
7382 MATCH_MATCH, /* match() */
7383 MATCH_STR, /* matchstr() */
7384 MATCH_LIST, /* matchlist() */
7385 MATCH_POS /* matchstrpos() */
7386} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007387
7388 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007389find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007390{
7391 char_u *str = NULL;
7392 long len = 0;
7393 char_u *expr = NULL;
7394 char_u *pat;
7395 regmatch_T regmatch;
7396 char_u patbuf[NUMBUFLEN];
7397 char_u strbuf[NUMBUFLEN];
7398 char_u *save_cpo;
7399 long start = 0;
7400 long nth = 1;
7401 colnr_T startcol = 0;
7402 int match = 0;
7403 list_T *l = NULL;
7404 listitem_T *li = NULL;
7405 long idx = 0;
7406 char_u *tofree = NULL;
7407
7408 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7409 save_cpo = p_cpo;
7410 p_cpo = (char_u *)"";
7411
7412 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007413 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007414 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007415 /* type MATCH_LIST: return empty list when there are no matches.
7416 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007417 if (rettv_list_alloc(rettv) == FAIL)
7418 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007419 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007420 && (list_append_string(rettv->vval.v_list,
7421 (char_u *)"", 0) == FAIL
7422 || list_append_number(rettv->vval.v_list,
7423 (varnumber_T)-1) == FAIL
7424 || list_append_number(rettv->vval.v_list,
7425 (varnumber_T)-1) == FAIL
7426 || list_append_number(rettv->vval.v_list,
7427 (varnumber_T)-1) == FAIL))
7428 {
7429 list_free(rettv->vval.v_list);
7430 rettv->vval.v_list = NULL;
7431 goto theend;
7432 }
7433 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007434 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007435 {
7436 rettv->v_type = VAR_STRING;
7437 rettv->vval.v_string = NULL;
7438 }
7439
7440 if (argvars[0].v_type == VAR_LIST)
7441 {
7442 if ((l = argvars[0].vval.v_list) == NULL)
7443 goto theend;
7444 li = l->lv_first;
7445 }
7446 else
7447 {
7448 expr = str = get_tv_string(&argvars[0]);
7449 len = (long)STRLEN(str);
7450 }
7451
7452 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7453 if (pat == NULL)
7454 goto theend;
7455
7456 if (argvars[2].v_type != VAR_UNKNOWN)
7457 {
7458 int error = FALSE;
7459
7460 start = (long)get_tv_number_chk(&argvars[2], &error);
7461 if (error)
7462 goto theend;
7463 if (l != NULL)
7464 {
7465 li = list_find(l, start);
7466 if (li == NULL)
7467 goto theend;
7468 idx = l->lv_idx; /* use the cached index */
7469 }
7470 else
7471 {
7472 if (start < 0)
7473 start = 0;
7474 if (start > len)
7475 goto theend;
7476 /* When "count" argument is there ignore matches before "start",
7477 * otherwise skip part of the string. Differs when pattern is "^"
7478 * or "\<". */
7479 if (argvars[3].v_type != VAR_UNKNOWN)
7480 startcol = start;
7481 else
7482 {
7483 str += start;
7484 len -= start;
7485 }
7486 }
7487
7488 if (argvars[3].v_type != VAR_UNKNOWN)
7489 nth = (long)get_tv_number_chk(&argvars[3], &error);
7490 if (error)
7491 goto theend;
7492 }
7493
7494 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7495 if (regmatch.regprog != NULL)
7496 {
7497 regmatch.rm_ic = p_ic;
7498
7499 for (;;)
7500 {
7501 if (l != NULL)
7502 {
7503 if (li == NULL)
7504 {
7505 match = FALSE;
7506 break;
7507 }
7508 vim_free(tofree);
7509 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7510 if (str == NULL)
7511 break;
7512 }
7513
7514 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7515
7516 if (match && --nth <= 0)
7517 break;
7518 if (l == NULL && !match)
7519 break;
7520
7521 /* Advance to just after the match. */
7522 if (l != NULL)
7523 {
7524 li = li->li_next;
7525 ++idx;
7526 }
7527 else
7528 {
7529#ifdef FEAT_MBYTE
7530 startcol = (colnr_T)(regmatch.startp[0]
7531 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7532#else
7533 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7534#endif
7535 if (startcol > (colnr_T)len
7536 || str + startcol <= regmatch.startp[0])
7537 {
7538 match = FALSE;
7539 break;
7540 }
7541 }
7542 }
7543
7544 if (match)
7545 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007546 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007547 {
7548 listitem_T *li1 = rettv->vval.v_list->lv_first;
7549 listitem_T *li2 = li1->li_next;
7550 listitem_T *li3 = li2->li_next;
7551 listitem_T *li4 = li3->li_next;
7552
7553 vim_free(li1->li_tv.vval.v_string);
7554 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7555 (int)(regmatch.endp[0] - regmatch.startp[0]));
7556 li3->li_tv.vval.v_number =
7557 (varnumber_T)(regmatch.startp[0] - expr);
7558 li4->li_tv.vval.v_number =
7559 (varnumber_T)(regmatch.endp[0] - expr);
7560 if (l != NULL)
7561 li2->li_tv.vval.v_number = (varnumber_T)idx;
7562 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007563 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007564 {
7565 int i;
7566
7567 /* return list with matched string and submatches */
7568 for (i = 0; i < NSUBEXP; ++i)
7569 {
7570 if (regmatch.endp[i] == NULL)
7571 {
7572 if (list_append_string(rettv->vval.v_list,
7573 (char_u *)"", 0) == FAIL)
7574 break;
7575 }
7576 else if (list_append_string(rettv->vval.v_list,
7577 regmatch.startp[i],
7578 (int)(regmatch.endp[i] - regmatch.startp[i]))
7579 == FAIL)
7580 break;
7581 }
7582 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007583 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007584 {
7585 /* return matched string */
7586 if (l != NULL)
7587 copy_tv(&li->li_tv, rettv);
7588 else
7589 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7590 (int)(regmatch.endp[0] - regmatch.startp[0]));
7591 }
7592 else if (l != NULL)
7593 rettv->vval.v_number = idx;
7594 else
7595 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007596 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007597 rettv->vval.v_number =
7598 (varnumber_T)(regmatch.startp[0] - str);
7599 else
7600 rettv->vval.v_number =
7601 (varnumber_T)(regmatch.endp[0] - str);
7602 rettv->vval.v_number += (varnumber_T)(str - expr);
7603 }
7604 }
7605 vim_regfree(regmatch.regprog);
7606 }
7607
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007608theend:
7609 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007610 /* matchstrpos() without a list: drop the second item. */
7611 listitem_remove(rettv->vval.v_list,
7612 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007613 vim_free(tofree);
7614 p_cpo = save_cpo;
7615}
7616
7617/*
7618 * "match()" function
7619 */
7620 static void
7621f_match(typval_T *argvars, typval_T *rettv)
7622{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007623 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007624}
7625
7626/*
7627 * "matchadd()" function
7628 */
7629 static void
7630f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7631{
7632#ifdef FEAT_SEARCH_EXTRA
7633 char_u buf[NUMBUFLEN];
7634 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7635 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7636 int prio = 10; /* default priority */
7637 int id = -1;
7638 int error = FALSE;
7639 char_u *conceal_char = NULL;
7640
7641 rettv->vval.v_number = -1;
7642
7643 if (grp == NULL || pat == NULL)
7644 return;
7645 if (argvars[2].v_type != VAR_UNKNOWN)
7646 {
7647 prio = (int)get_tv_number_chk(&argvars[2], &error);
7648 if (argvars[3].v_type != VAR_UNKNOWN)
7649 {
7650 id = (int)get_tv_number_chk(&argvars[3], &error);
7651 if (argvars[4].v_type != VAR_UNKNOWN)
7652 {
7653 if (argvars[4].v_type != VAR_DICT)
7654 {
7655 EMSG(_(e_dictreq));
7656 return;
7657 }
7658 if (dict_find(argvars[4].vval.v_dict,
7659 (char_u *)"conceal", -1) != NULL)
7660 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7661 (char_u *)"conceal", FALSE);
7662 }
7663 }
7664 }
7665 if (error == TRUE)
7666 return;
7667 if (id >= 1 && id <= 3)
7668 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007669 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007670 return;
7671 }
7672
7673 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7674 conceal_char);
7675#endif
7676}
7677
7678/*
7679 * "matchaddpos()" function
7680 */
7681 static void
7682f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7683{
7684#ifdef FEAT_SEARCH_EXTRA
7685 char_u buf[NUMBUFLEN];
7686 char_u *group;
7687 int prio = 10;
7688 int id = -1;
7689 int error = FALSE;
7690 list_T *l;
7691 char_u *conceal_char = NULL;
7692
7693 rettv->vval.v_number = -1;
7694
7695 group = get_tv_string_buf_chk(&argvars[0], buf);
7696 if (group == NULL)
7697 return;
7698
7699 if (argvars[1].v_type != VAR_LIST)
7700 {
7701 EMSG2(_(e_listarg), "matchaddpos()");
7702 return;
7703 }
7704 l = argvars[1].vval.v_list;
7705 if (l == NULL)
7706 return;
7707
7708 if (argvars[2].v_type != VAR_UNKNOWN)
7709 {
7710 prio = (int)get_tv_number_chk(&argvars[2], &error);
7711 if (argvars[3].v_type != VAR_UNKNOWN)
7712 {
7713 id = (int)get_tv_number_chk(&argvars[3], &error);
7714 if (argvars[4].v_type != VAR_UNKNOWN)
7715 {
7716 if (argvars[4].v_type != VAR_DICT)
7717 {
7718 EMSG(_(e_dictreq));
7719 return;
7720 }
7721 if (dict_find(argvars[4].vval.v_dict,
7722 (char_u *)"conceal", -1) != NULL)
7723 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7724 (char_u *)"conceal", FALSE);
7725 }
7726 }
7727 }
7728 if (error == TRUE)
7729 return;
7730
7731 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7732 if (id == 1 || id == 2)
7733 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007734 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007735 return;
7736 }
7737
7738 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7739 conceal_char);
7740#endif
7741}
7742
7743/*
7744 * "matcharg()" function
7745 */
7746 static void
7747f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7748{
7749 if (rettv_list_alloc(rettv) == OK)
7750 {
7751#ifdef FEAT_SEARCH_EXTRA
7752 int id = (int)get_tv_number(&argvars[0]);
7753 matchitem_T *m;
7754
7755 if (id >= 1 && id <= 3)
7756 {
7757 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7758 {
7759 list_append_string(rettv->vval.v_list,
7760 syn_id2name(m->hlg_id), -1);
7761 list_append_string(rettv->vval.v_list, m->pattern, -1);
7762 }
7763 else
7764 {
7765 list_append_string(rettv->vval.v_list, NULL, -1);
7766 list_append_string(rettv->vval.v_list, NULL, -1);
7767 }
7768 }
7769#endif
7770 }
7771}
7772
7773/*
7774 * "matchdelete()" function
7775 */
7776 static void
7777f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7778{
7779#ifdef FEAT_SEARCH_EXTRA
7780 rettv->vval.v_number = match_delete(curwin,
7781 (int)get_tv_number(&argvars[0]), TRUE);
7782#endif
7783}
7784
7785/*
7786 * "matchend()" function
7787 */
7788 static void
7789f_matchend(typval_T *argvars, typval_T *rettv)
7790{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007791 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007792}
7793
7794/*
7795 * "matchlist()" function
7796 */
7797 static void
7798f_matchlist(typval_T *argvars, typval_T *rettv)
7799{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007800 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007801}
7802
7803/*
7804 * "matchstr()" function
7805 */
7806 static void
7807f_matchstr(typval_T *argvars, typval_T *rettv)
7808{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007809 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007810}
7811
7812/*
7813 * "matchstrpos()" function
7814 */
7815 static void
7816f_matchstrpos(typval_T *argvars, typval_T *rettv)
7817{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007818 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007819}
7820
7821static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7822
7823 static void
7824max_min(typval_T *argvars, typval_T *rettv, int domax)
7825{
7826 varnumber_T n = 0;
7827 varnumber_T i;
7828 int error = FALSE;
7829
7830 if (argvars[0].v_type == VAR_LIST)
7831 {
7832 list_T *l;
7833 listitem_T *li;
7834
7835 l = argvars[0].vval.v_list;
7836 if (l != NULL)
7837 {
7838 li = l->lv_first;
7839 if (li != NULL)
7840 {
7841 n = get_tv_number_chk(&li->li_tv, &error);
7842 for (;;)
7843 {
7844 li = li->li_next;
7845 if (li == NULL)
7846 break;
7847 i = get_tv_number_chk(&li->li_tv, &error);
7848 if (domax ? i > n : i < n)
7849 n = i;
7850 }
7851 }
7852 }
7853 }
7854 else if (argvars[0].v_type == VAR_DICT)
7855 {
7856 dict_T *d;
7857 int first = TRUE;
7858 hashitem_T *hi;
7859 int todo;
7860
7861 d = argvars[0].vval.v_dict;
7862 if (d != NULL)
7863 {
7864 todo = (int)d->dv_hashtab.ht_used;
7865 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7866 {
7867 if (!HASHITEM_EMPTY(hi))
7868 {
7869 --todo;
7870 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7871 if (first)
7872 {
7873 n = i;
7874 first = FALSE;
7875 }
7876 else if (domax ? i > n : i < n)
7877 n = i;
7878 }
7879 }
7880 }
7881 }
7882 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007883 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007884 rettv->vval.v_number = error ? 0 : n;
7885}
7886
7887/*
7888 * "max()" function
7889 */
7890 static void
7891f_max(typval_T *argvars, typval_T *rettv)
7892{
7893 max_min(argvars, rettv, TRUE);
7894}
7895
7896/*
7897 * "min()" function
7898 */
7899 static void
7900f_min(typval_T *argvars, typval_T *rettv)
7901{
7902 max_min(argvars, rettv, FALSE);
7903}
7904
7905static int mkdir_recurse(char_u *dir, int prot);
7906
7907/*
7908 * Create the directory in which "dir" is located, and higher levels when
7909 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007910 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007911 */
7912 static int
7913mkdir_recurse(char_u *dir, int prot)
7914{
7915 char_u *p;
7916 char_u *updir;
7917 int r = FAIL;
7918
7919 /* Get end of directory name in "dir".
7920 * We're done when it's "/" or "c:/". */
7921 p = gettail_sep(dir);
7922 if (p <= get_past_head(dir))
7923 return OK;
7924
7925 /* If the directory exists we're done. Otherwise: create it.*/
7926 updir = vim_strnsave(dir, (int)(p - dir));
7927 if (updir == NULL)
7928 return FAIL;
7929 if (mch_isdir(updir))
7930 r = OK;
7931 else if (mkdir_recurse(updir, prot) == OK)
7932 r = vim_mkdir_emsg(updir, prot);
7933 vim_free(updir);
7934 return r;
7935}
7936
7937#ifdef vim_mkdir
7938/*
7939 * "mkdir()" function
7940 */
7941 static void
7942f_mkdir(typval_T *argvars, typval_T *rettv)
7943{
7944 char_u *dir;
7945 char_u buf[NUMBUFLEN];
7946 int prot = 0755;
7947
7948 rettv->vval.v_number = FAIL;
7949 if (check_restricted() || check_secure())
7950 return;
7951
7952 dir = get_tv_string_buf(&argvars[0], buf);
7953 if (*dir == NUL)
7954 rettv->vval.v_number = FAIL;
7955 else
7956 {
7957 if (*gettail(dir) == NUL)
7958 /* remove trailing slashes */
7959 *gettail_sep(dir) = NUL;
7960
7961 if (argvars[1].v_type != VAR_UNKNOWN)
7962 {
7963 if (argvars[2].v_type != VAR_UNKNOWN)
7964 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7965 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7966 mkdir_recurse(dir, prot);
7967 }
7968 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7969 }
7970}
7971#endif
7972
7973/*
7974 * "mode()" function
7975 */
7976 static void
7977f_mode(typval_T *argvars, typval_T *rettv)
7978{
7979 char_u buf[3];
7980
7981 buf[1] = NUL;
7982 buf[2] = NUL;
7983
7984 if (time_for_testing == 93784)
7985 {
7986 /* Testing the two-character code. */
7987 buf[0] = 'x';
7988 buf[1] = '!';
7989 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02007990#ifdef FEAT_TERMINAL
7991 else if (term_use_loop())
7992 buf[0] = 't';
7993#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007994 else if (VIsual_active)
7995 {
7996 if (VIsual_select)
7997 buf[0] = VIsual_mode + 's' - 'v';
7998 else
7999 buf[0] = VIsual_mode;
8000 }
8001 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8002 || State == CONFIRM)
8003 {
8004 buf[0] = 'r';
8005 if (State == ASKMORE)
8006 buf[1] = 'm';
8007 else if (State == CONFIRM)
8008 buf[1] = '?';
8009 }
8010 else if (State == EXTERNCMD)
8011 buf[0] = '!';
8012 else if (State & INSERT)
8013 {
8014#ifdef FEAT_VREPLACE
8015 if (State & VREPLACE_FLAG)
8016 {
8017 buf[0] = 'R';
8018 buf[1] = 'v';
8019 }
8020 else
8021#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008022 {
8023 if (State & REPLACE_FLAG)
8024 buf[0] = 'R';
8025 else
8026 buf[0] = 'i';
8027#ifdef FEAT_INS_EXPAND
8028 if (ins_compl_active())
8029 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008030 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008031 buf[1] = 'x';
8032#endif
8033 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008034 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008035 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036 {
8037 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008038 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008039 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008040 else if (exmode_active == EXMODE_NORMAL)
8041 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008042 }
8043 else
8044 {
8045 buf[0] = 'n';
8046 if (finish_op)
8047 buf[1] = 'o';
8048 }
8049
8050 /* Clear out the minor mode when the argument is not a non-zero number or
8051 * non-empty string. */
8052 if (!non_zero_arg(&argvars[0]))
8053 buf[1] = NUL;
8054
8055 rettv->vval.v_string = vim_strsave(buf);
8056 rettv->v_type = VAR_STRING;
8057}
8058
8059#if defined(FEAT_MZSCHEME) || defined(PROTO)
8060/*
8061 * "mzeval()" function
8062 */
8063 static void
8064f_mzeval(typval_T *argvars, typval_T *rettv)
8065{
8066 char_u *str;
8067 char_u buf[NUMBUFLEN];
8068
8069 str = get_tv_string_buf(&argvars[0], buf);
8070 do_mzeval(str, rettv);
8071}
8072
8073 void
8074mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8075{
8076 typval_T argvars[3];
8077
8078 argvars[0].v_type = VAR_STRING;
8079 argvars[0].vval.v_string = name;
8080 copy_tv(args, &argvars[1]);
8081 argvars[2].v_type = VAR_UNKNOWN;
8082 f_call(argvars, rettv);
8083 clear_tv(&argvars[1]);
8084}
8085#endif
8086
8087/*
8088 * "nextnonblank()" function
8089 */
8090 static void
8091f_nextnonblank(typval_T *argvars, typval_T *rettv)
8092{
8093 linenr_T lnum;
8094
8095 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8096 {
8097 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8098 {
8099 lnum = 0;
8100 break;
8101 }
8102 if (*skipwhite(ml_get(lnum)) != NUL)
8103 break;
8104 }
8105 rettv->vval.v_number = lnum;
8106}
8107
8108/*
8109 * "nr2char()" function
8110 */
8111 static void
8112f_nr2char(typval_T *argvars, typval_T *rettv)
8113{
8114 char_u buf[NUMBUFLEN];
8115
8116#ifdef FEAT_MBYTE
8117 if (has_mbyte)
8118 {
8119 int utf8 = 0;
8120
8121 if (argvars[1].v_type != VAR_UNKNOWN)
8122 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8123 if (utf8)
8124 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8125 else
8126 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8127 }
8128 else
8129#endif
8130 {
8131 buf[0] = (char_u)get_tv_number(&argvars[0]);
8132 buf[1] = NUL;
8133 }
8134 rettv->v_type = VAR_STRING;
8135 rettv->vval.v_string = vim_strsave(buf);
8136}
8137
8138/*
8139 * "or(expr, expr)" function
8140 */
8141 static void
8142f_or(typval_T *argvars, typval_T *rettv)
8143{
8144 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8145 | get_tv_number_chk(&argvars[1], NULL);
8146}
8147
8148/*
8149 * "pathshorten()" function
8150 */
8151 static void
8152f_pathshorten(typval_T *argvars, typval_T *rettv)
8153{
8154 char_u *p;
8155
8156 rettv->v_type = VAR_STRING;
8157 p = get_tv_string_chk(&argvars[0]);
8158 if (p == NULL)
8159 rettv->vval.v_string = NULL;
8160 else
8161 {
8162 p = vim_strsave(p);
8163 rettv->vval.v_string = p;
8164 if (p != NULL)
8165 shorten_dir(p);
8166 }
8167}
8168
8169#ifdef FEAT_PERL
8170/*
8171 * "perleval()" function
8172 */
8173 static void
8174f_perleval(typval_T *argvars, typval_T *rettv)
8175{
8176 char_u *str;
8177 char_u buf[NUMBUFLEN];
8178
8179 str = get_tv_string_buf(&argvars[0], buf);
8180 do_perleval(str, rettv);
8181}
8182#endif
8183
8184#ifdef FEAT_FLOAT
8185/*
8186 * "pow()" function
8187 */
8188 static void
8189f_pow(typval_T *argvars, typval_T *rettv)
8190{
8191 float_T fx = 0.0, fy = 0.0;
8192
8193 rettv->v_type = VAR_FLOAT;
8194 if (get_float_arg(argvars, &fx) == OK
8195 && get_float_arg(&argvars[1], &fy) == OK)
8196 rettv->vval.v_float = pow(fx, fy);
8197 else
8198 rettv->vval.v_float = 0.0;
8199}
8200#endif
8201
8202/*
8203 * "prevnonblank()" function
8204 */
8205 static void
8206f_prevnonblank(typval_T *argvars, typval_T *rettv)
8207{
8208 linenr_T lnum;
8209
8210 lnum = get_tv_lnum(argvars);
8211 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8212 lnum = 0;
8213 else
8214 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8215 --lnum;
8216 rettv->vval.v_number = lnum;
8217}
8218
8219/* This dummy va_list is here because:
8220 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8221 * - locally in the function results in a "used before set" warning
8222 * - using va_start() to initialize it gives "function with fixed args" error */
8223static va_list ap;
8224
8225/*
8226 * "printf()" function
8227 */
8228 static void
8229f_printf(typval_T *argvars, typval_T *rettv)
8230{
8231 char_u buf[NUMBUFLEN];
8232 int len;
8233 char_u *s;
8234 int saved_did_emsg = did_emsg;
8235 char *fmt;
8236
8237 rettv->v_type = VAR_STRING;
8238 rettv->vval.v_string = NULL;
8239
8240 /* Get the required length, allocate the buffer and do it for real. */
8241 did_emsg = FALSE;
8242 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008243 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008244 if (!did_emsg)
8245 {
8246 s = alloc(len + 1);
8247 if (s != NULL)
8248 {
8249 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008250 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8251 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008252 }
8253 }
8254 did_emsg |= saved_did_emsg;
8255}
8256
8257/*
8258 * "pumvisible()" function
8259 */
8260 static void
8261f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8262{
8263#ifdef FEAT_INS_EXPAND
8264 if (pum_visible())
8265 rettv->vval.v_number = 1;
8266#endif
8267}
8268
8269#ifdef FEAT_PYTHON3
8270/*
8271 * "py3eval()" function
8272 */
8273 static void
8274f_py3eval(typval_T *argvars, typval_T *rettv)
8275{
8276 char_u *str;
8277 char_u buf[NUMBUFLEN];
8278
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008279 if (p_pyx == 0)
8280 p_pyx = 3;
8281
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008282 str = get_tv_string_buf(&argvars[0], buf);
8283 do_py3eval(str, rettv);
8284}
8285#endif
8286
8287#ifdef FEAT_PYTHON
8288/*
8289 * "pyeval()" function
8290 */
8291 static void
8292f_pyeval(typval_T *argvars, typval_T *rettv)
8293{
8294 char_u *str;
8295 char_u buf[NUMBUFLEN];
8296
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008297 if (p_pyx == 0)
8298 p_pyx = 2;
8299
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008300 str = get_tv_string_buf(&argvars[0], buf);
8301 do_pyeval(str, rettv);
8302}
8303#endif
8304
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008305#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8306/*
8307 * "pyxeval()" function
8308 */
8309 static void
8310f_pyxeval(typval_T *argvars, typval_T *rettv)
8311{
8312# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8313 init_pyxversion();
8314 if (p_pyx == 2)
8315 f_pyeval(argvars, rettv);
8316 else
8317 f_py3eval(argvars, rettv);
8318# elif defined(FEAT_PYTHON)
8319 f_pyeval(argvars, rettv);
8320# elif defined(FEAT_PYTHON3)
8321 f_py3eval(argvars, rettv);
8322# endif
8323}
8324#endif
8325
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008326/*
8327 * "range()" function
8328 */
8329 static void
8330f_range(typval_T *argvars, typval_T *rettv)
8331{
8332 varnumber_T start;
8333 varnumber_T end;
8334 varnumber_T stride = 1;
8335 varnumber_T i;
8336 int error = FALSE;
8337
8338 start = get_tv_number_chk(&argvars[0], &error);
8339 if (argvars[1].v_type == VAR_UNKNOWN)
8340 {
8341 end = start - 1;
8342 start = 0;
8343 }
8344 else
8345 {
8346 end = get_tv_number_chk(&argvars[1], &error);
8347 if (argvars[2].v_type != VAR_UNKNOWN)
8348 stride = get_tv_number_chk(&argvars[2], &error);
8349 }
8350
8351 if (error)
8352 return; /* type error; errmsg already given */
8353 if (stride == 0)
8354 EMSG(_("E726: Stride is zero"));
8355 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8356 EMSG(_("E727: Start past end"));
8357 else
8358 {
8359 if (rettv_list_alloc(rettv) == OK)
8360 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8361 if (list_append_number(rettv->vval.v_list,
8362 (varnumber_T)i) == FAIL)
8363 break;
8364 }
8365}
8366
8367/*
8368 * "readfile()" function
8369 */
8370 static void
8371f_readfile(typval_T *argvars, typval_T *rettv)
8372{
8373 int binary = FALSE;
8374 int failed = FALSE;
8375 char_u *fname;
8376 FILE *fd;
8377 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8378 int io_size = sizeof(buf);
8379 int readlen; /* size of last fread() */
8380 char_u *prev = NULL; /* previously read bytes, if any */
8381 long prevlen = 0; /* length of data in prev */
8382 long prevsize = 0; /* size of prev buffer */
8383 long maxline = MAXLNUM;
8384 long cnt = 0;
8385 char_u *p; /* position in buf */
8386 char_u *start; /* start of current line */
8387
8388 if (argvars[1].v_type != VAR_UNKNOWN)
8389 {
8390 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8391 binary = TRUE;
8392 if (argvars[2].v_type != VAR_UNKNOWN)
8393 maxline = (long)get_tv_number(&argvars[2]);
8394 }
8395
8396 if (rettv_list_alloc(rettv) == FAIL)
8397 return;
8398
8399 /* Always open the file in binary mode, library functions have a mind of
8400 * their own about CR-LF conversion. */
8401 fname = get_tv_string(&argvars[0]);
8402 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8403 {
8404 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8405 return;
8406 }
8407
8408 while (cnt < maxline || maxline < 0)
8409 {
8410 readlen = (int)fread(buf, 1, io_size, fd);
8411
8412 /* This for loop processes what was read, but is also entered at end
8413 * of file so that either:
8414 * - an incomplete line gets written
8415 * - a "binary" file gets an empty line at the end if it ends in a
8416 * newline. */
8417 for (p = buf, start = buf;
8418 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8419 ++p)
8420 {
8421 if (*p == '\n' || readlen <= 0)
8422 {
8423 listitem_T *li;
8424 char_u *s = NULL;
8425 long_u len = p - start;
8426
8427 /* Finished a line. Remove CRs before NL. */
8428 if (readlen > 0 && !binary)
8429 {
8430 while (len > 0 && start[len - 1] == '\r')
8431 --len;
8432 /* removal may cross back to the "prev" string */
8433 if (len == 0)
8434 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8435 --prevlen;
8436 }
8437 if (prevlen == 0)
8438 s = vim_strnsave(start, (int)len);
8439 else
8440 {
8441 /* Change "prev" buffer to be the right size. This way
8442 * the bytes are only copied once, and very long lines are
8443 * allocated only once. */
8444 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8445 {
8446 mch_memmove(s + prevlen, start, len);
8447 s[prevlen + len] = NUL;
8448 prev = NULL; /* the list will own the string */
8449 prevlen = prevsize = 0;
8450 }
8451 }
8452 if (s == NULL)
8453 {
8454 do_outofmem_msg((long_u) prevlen + len + 1);
8455 failed = TRUE;
8456 break;
8457 }
8458
8459 if ((li = listitem_alloc()) == NULL)
8460 {
8461 vim_free(s);
8462 failed = TRUE;
8463 break;
8464 }
8465 li->li_tv.v_type = VAR_STRING;
8466 li->li_tv.v_lock = 0;
8467 li->li_tv.vval.v_string = s;
8468 list_append(rettv->vval.v_list, li);
8469
8470 start = p + 1; /* step over newline */
8471 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8472 break;
8473 }
8474 else if (*p == NUL)
8475 *p = '\n';
8476#ifdef FEAT_MBYTE
8477 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8478 * when finding the BF and check the previous two bytes. */
8479 else if (*p == 0xbf && enc_utf8 && !binary)
8480 {
8481 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8482 * + 1, these may be in the "prev" string. */
8483 char_u back1 = p >= buf + 1 ? p[-1]
8484 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8485 char_u back2 = p >= buf + 2 ? p[-2]
8486 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8487 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8488
8489 if (back2 == 0xef && back1 == 0xbb)
8490 {
8491 char_u *dest = p - 2;
8492
8493 /* Usually a BOM is at the beginning of a file, and so at
8494 * the beginning of a line; then we can just step over it.
8495 */
8496 if (start == dest)
8497 start = p + 1;
8498 else
8499 {
8500 /* have to shuffle buf to close gap */
8501 int adjust_prevlen = 0;
8502
8503 if (dest < buf)
8504 {
8505 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8506 dest = buf;
8507 }
8508 if (readlen > p - buf + 1)
8509 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8510 readlen -= 3 - adjust_prevlen;
8511 prevlen -= adjust_prevlen;
8512 p = dest - 1;
8513 }
8514 }
8515 }
8516#endif
8517 } /* for */
8518
8519 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8520 break;
8521 if (start < p)
8522 {
8523 /* There's part of a line in buf, store it in "prev". */
8524 if (p - start + prevlen >= prevsize)
8525 {
8526 /* need bigger "prev" buffer */
8527 char_u *newprev;
8528
8529 /* A common use case is ordinary text files and "prev" gets a
8530 * fragment of a line, so the first allocation is made
8531 * small, to avoid repeatedly 'allocing' large and
8532 * 'reallocing' small. */
8533 if (prevsize == 0)
8534 prevsize = (long)(p - start);
8535 else
8536 {
8537 long grow50pc = (prevsize * 3) / 2;
8538 long growmin = (long)((p - start) * 2 + prevlen);
8539 prevsize = grow50pc > growmin ? grow50pc : growmin;
8540 }
8541 newprev = prev == NULL ? alloc(prevsize)
8542 : vim_realloc(prev, prevsize);
8543 if (newprev == NULL)
8544 {
8545 do_outofmem_msg((long_u)prevsize);
8546 failed = TRUE;
8547 break;
8548 }
8549 prev = newprev;
8550 }
8551 /* Add the line part to end of "prev". */
8552 mch_memmove(prev + prevlen, start, p - start);
8553 prevlen += (long)(p - start);
8554 }
8555 } /* while */
8556
8557 /*
8558 * For a negative line count use only the lines at the end of the file,
8559 * free the rest.
8560 */
8561 if (!failed && maxline < 0)
8562 while (cnt > -maxline)
8563 {
8564 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8565 --cnt;
8566 }
8567
8568 if (failed)
8569 {
8570 list_free(rettv->vval.v_list);
8571 /* readfile doc says an empty list is returned on error */
8572 rettv->vval.v_list = list_alloc();
8573 }
8574
8575 vim_free(prev);
8576 fclose(fd);
8577}
8578
8579#if defined(FEAT_RELTIME)
8580static int list2proftime(typval_T *arg, proftime_T *tm);
8581
8582/*
8583 * Convert a List to proftime_T.
8584 * Return FAIL when there is something wrong.
8585 */
8586 static int
8587list2proftime(typval_T *arg, proftime_T *tm)
8588{
8589 long n1, n2;
8590 int error = FALSE;
8591
8592 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8593 || arg->vval.v_list->lv_len != 2)
8594 return FAIL;
8595 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8596 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8597# ifdef WIN3264
8598 tm->HighPart = n1;
8599 tm->LowPart = n2;
8600# else
8601 tm->tv_sec = n1;
8602 tm->tv_usec = n2;
8603# endif
8604 return error ? FAIL : OK;
8605}
8606#endif /* FEAT_RELTIME */
8607
8608/*
8609 * "reltime()" function
8610 */
8611 static void
8612f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8613{
8614#ifdef FEAT_RELTIME
8615 proftime_T res;
8616 proftime_T start;
8617
8618 if (argvars[0].v_type == VAR_UNKNOWN)
8619 {
8620 /* No arguments: get current time. */
8621 profile_start(&res);
8622 }
8623 else if (argvars[1].v_type == VAR_UNKNOWN)
8624 {
8625 if (list2proftime(&argvars[0], &res) == FAIL)
8626 return;
8627 profile_end(&res);
8628 }
8629 else
8630 {
8631 /* Two arguments: compute the difference. */
8632 if (list2proftime(&argvars[0], &start) == FAIL
8633 || list2proftime(&argvars[1], &res) == FAIL)
8634 return;
8635 profile_sub(&res, &start);
8636 }
8637
8638 if (rettv_list_alloc(rettv) == OK)
8639 {
8640 long n1, n2;
8641
8642# ifdef WIN3264
8643 n1 = res.HighPart;
8644 n2 = res.LowPart;
8645# else
8646 n1 = res.tv_sec;
8647 n2 = res.tv_usec;
8648# endif
8649 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8650 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8651 }
8652#endif
8653}
8654
8655#ifdef FEAT_FLOAT
8656/*
8657 * "reltimefloat()" function
8658 */
8659 static void
8660f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8661{
8662# ifdef FEAT_RELTIME
8663 proftime_T tm;
8664# endif
8665
8666 rettv->v_type = VAR_FLOAT;
8667 rettv->vval.v_float = 0;
8668# ifdef FEAT_RELTIME
8669 if (list2proftime(&argvars[0], &tm) == OK)
8670 rettv->vval.v_float = profile_float(&tm);
8671# endif
8672}
8673#endif
8674
8675/*
8676 * "reltimestr()" function
8677 */
8678 static void
8679f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8680{
8681#ifdef FEAT_RELTIME
8682 proftime_T tm;
8683#endif
8684
8685 rettv->v_type = VAR_STRING;
8686 rettv->vval.v_string = NULL;
8687#ifdef FEAT_RELTIME
8688 if (list2proftime(&argvars[0], &tm) == OK)
8689 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8690#endif
8691}
8692
8693#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8694static void make_connection(void);
8695static int check_connection(void);
8696
8697 static void
8698make_connection(void)
8699{
8700 if (X_DISPLAY == NULL
8701# ifdef FEAT_GUI
8702 && !gui.in_use
8703# endif
8704 )
8705 {
8706 x_force_connect = TRUE;
8707 setup_term_clip();
8708 x_force_connect = FALSE;
8709 }
8710}
8711
8712 static int
8713check_connection(void)
8714{
8715 make_connection();
8716 if (X_DISPLAY == NULL)
8717 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008718 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008719 return FAIL;
8720 }
8721 return OK;
8722}
8723#endif
8724
8725#ifdef FEAT_CLIENTSERVER
8726 static void
8727remote_common(typval_T *argvars, typval_T *rettv, int expr)
8728{
8729 char_u *server_name;
8730 char_u *keys;
8731 char_u *r = NULL;
8732 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008733 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008734# ifdef WIN32
8735 HWND w;
8736# else
8737 Window w;
8738# endif
8739
8740 if (check_restricted() || check_secure())
8741 return;
8742
8743# ifdef FEAT_X11
8744 if (check_connection() == FAIL)
8745 return;
8746# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008747 if (argvars[2].v_type != VAR_UNKNOWN
8748 && argvars[3].v_type != VAR_UNKNOWN)
8749 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008750
8751 server_name = get_tv_string_chk(&argvars[0]);
8752 if (server_name == NULL)
8753 return; /* type error; errmsg already given */
8754 keys = get_tv_string_buf(&argvars[1], buf);
8755# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008756 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008758 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8759 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008760# endif
8761 {
8762 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008763 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008764 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008765 vim_free(r);
8766 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008767 else
8768 EMSG2(_("E241: Unable to send to %s"), server_name);
8769 return;
8770 }
8771
8772 rettv->vval.v_string = r;
8773
8774 if (argvars[2].v_type != VAR_UNKNOWN)
8775 {
8776 dictitem_T v;
8777 char_u str[30];
8778 char_u *idvar;
8779
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008780 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008781 if (idvar != NULL && *idvar != NUL)
8782 {
8783 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8784 v.di_tv.v_type = VAR_STRING;
8785 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008786 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008787 vim_free(v.di_tv.vval.v_string);
8788 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008789 }
8790}
8791#endif
8792
8793/*
8794 * "remote_expr()" function
8795 */
8796 static void
8797f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8798{
8799 rettv->v_type = VAR_STRING;
8800 rettv->vval.v_string = NULL;
8801#ifdef FEAT_CLIENTSERVER
8802 remote_common(argvars, rettv, TRUE);
8803#endif
8804}
8805
8806/*
8807 * "remote_foreground()" function
8808 */
8809 static void
8810f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8811{
8812#ifdef FEAT_CLIENTSERVER
8813# ifdef WIN32
8814 /* On Win32 it's done in this application. */
8815 {
8816 char_u *server_name = get_tv_string_chk(&argvars[0]);
8817
8818 if (server_name != NULL)
8819 serverForeground(server_name);
8820 }
8821# else
8822 /* Send a foreground() expression to the server. */
8823 argvars[1].v_type = VAR_STRING;
8824 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8825 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008826 rettv->v_type = VAR_STRING;
8827 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008828 remote_common(argvars, rettv, TRUE);
8829 vim_free(argvars[1].vval.v_string);
8830# endif
8831#endif
8832}
8833
8834 static void
8835f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8836{
8837#ifdef FEAT_CLIENTSERVER
8838 dictitem_T v;
8839 char_u *s = NULL;
8840# ifdef WIN32
8841 long_u n = 0;
8842# endif
8843 char_u *serverid;
8844
8845 if (check_restricted() || check_secure())
8846 {
8847 rettv->vval.v_number = -1;
8848 return;
8849 }
8850 serverid = get_tv_string_chk(&argvars[0]);
8851 if (serverid == NULL)
8852 {
8853 rettv->vval.v_number = -1;
8854 return; /* type error; errmsg already given */
8855 }
8856# ifdef WIN32
8857 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8858 if (n == 0)
8859 rettv->vval.v_number = -1;
8860 else
8861 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008862 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008863 rettv->vval.v_number = (s != NULL);
8864 }
8865# else
8866 if (check_connection() == FAIL)
8867 return;
8868
8869 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8870 serverStrToWin(serverid), &s);
8871# endif
8872
8873 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8874 {
8875 char_u *retvar;
8876
8877 v.di_tv.v_type = VAR_STRING;
8878 v.di_tv.vval.v_string = vim_strsave(s);
8879 retvar = get_tv_string_chk(&argvars[1]);
8880 if (retvar != NULL)
8881 set_var(retvar, &v.di_tv, FALSE);
8882 vim_free(v.di_tv.vval.v_string);
8883 }
8884#else
8885 rettv->vval.v_number = -1;
8886#endif
8887}
8888
8889 static void
8890f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8891{
8892 char_u *r = NULL;
8893
8894#ifdef FEAT_CLIENTSERVER
8895 char_u *serverid = get_tv_string_chk(&argvars[0]);
8896
8897 if (serverid != NULL && !check_restricted() && !check_secure())
8898 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008899 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008900# ifdef WIN32
8901 /* The server's HWND is encoded in the 'id' parameter */
8902 long_u n = 0;
8903# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008904
8905 if (argvars[1].v_type != VAR_UNKNOWN)
8906 timeout = get_tv_number(&argvars[1]);
8907
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008908# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008909 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8910 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008911 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008912 if (r == NULL)
8913# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008914 if (check_connection() == FAIL
8915 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8916 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008917# endif
8918 EMSG(_("E277: Unable to read a server reply"));
8919 }
8920#endif
8921 rettv->v_type = VAR_STRING;
8922 rettv->vval.v_string = r;
8923}
8924
8925/*
8926 * "remote_send()" function
8927 */
8928 static void
8929f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8930{
8931 rettv->v_type = VAR_STRING;
8932 rettv->vval.v_string = NULL;
8933#ifdef FEAT_CLIENTSERVER
8934 remote_common(argvars, rettv, FALSE);
8935#endif
8936}
8937
8938/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008939 * "remote_startserver()" function
8940 */
8941 static void
8942f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8943{
8944#ifdef FEAT_CLIENTSERVER
8945 char_u *server = get_tv_string_chk(&argvars[0]);
8946
8947 if (server == NULL)
8948 return; /* type error; errmsg already given */
8949 if (serverName != NULL)
8950 EMSG(_("E941: already started a server"));
8951 else
8952 {
8953# ifdef FEAT_X11
8954 if (check_connection() == OK)
8955 serverRegisterName(X_DISPLAY, server);
8956# else
8957 serverSetName(server);
8958# endif
8959 }
8960#else
8961 EMSG(_("E942: +clientserver feature not available"));
8962#endif
8963}
8964
8965/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008966 * "remove()" function
8967 */
8968 static void
8969f_remove(typval_T *argvars, typval_T *rettv)
8970{
8971 list_T *l;
8972 listitem_T *item, *item2;
8973 listitem_T *li;
8974 long idx;
8975 long end;
8976 char_u *key;
8977 dict_T *d;
8978 dictitem_T *di;
8979 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8980
8981 if (argvars[0].v_type == VAR_DICT)
8982 {
8983 if (argvars[2].v_type != VAR_UNKNOWN)
8984 EMSG2(_(e_toomanyarg), "remove()");
8985 else if ((d = argvars[0].vval.v_dict) != NULL
8986 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
8987 {
8988 key = get_tv_string_chk(&argvars[1]);
8989 if (key != NULL)
8990 {
8991 di = dict_find(d, key, -1);
8992 if (di == NULL)
8993 EMSG2(_(e_dictkey), key);
8994 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
8995 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
8996 {
8997 *rettv = di->di_tv;
8998 init_tv(&di->di_tv);
8999 dictitem_remove(d, di);
9000 }
9001 }
9002 }
9003 }
9004 else if (argvars[0].v_type != VAR_LIST)
9005 EMSG2(_(e_listdictarg), "remove()");
9006 else if ((l = argvars[0].vval.v_list) != NULL
9007 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9008 {
9009 int error = FALSE;
9010
9011 idx = (long)get_tv_number_chk(&argvars[1], &error);
9012 if (error)
9013 ; /* type error: do nothing, errmsg already given */
9014 else if ((item = list_find(l, idx)) == NULL)
9015 EMSGN(_(e_listidx), idx);
9016 else
9017 {
9018 if (argvars[2].v_type == VAR_UNKNOWN)
9019 {
9020 /* Remove one item, return its value. */
9021 vimlist_remove(l, item, item);
9022 *rettv = item->li_tv;
9023 vim_free(item);
9024 }
9025 else
9026 {
9027 /* Remove range of items, return list with values. */
9028 end = (long)get_tv_number_chk(&argvars[2], &error);
9029 if (error)
9030 ; /* type error: do nothing */
9031 else if ((item2 = list_find(l, end)) == NULL)
9032 EMSGN(_(e_listidx), end);
9033 else
9034 {
9035 int cnt = 0;
9036
9037 for (li = item; li != NULL; li = li->li_next)
9038 {
9039 ++cnt;
9040 if (li == item2)
9041 break;
9042 }
9043 if (li == NULL) /* didn't find "item2" after "item" */
9044 EMSG(_(e_invrange));
9045 else
9046 {
9047 vimlist_remove(l, item, item2);
9048 if (rettv_list_alloc(rettv) == OK)
9049 {
9050 l = rettv->vval.v_list;
9051 l->lv_first = item;
9052 l->lv_last = item2;
9053 item->li_prev = NULL;
9054 item2->li_next = NULL;
9055 l->lv_len = cnt;
9056 }
9057 }
9058 }
9059 }
9060 }
9061 }
9062}
9063
9064/*
9065 * "rename({from}, {to})" function
9066 */
9067 static void
9068f_rename(typval_T *argvars, typval_T *rettv)
9069{
9070 char_u buf[NUMBUFLEN];
9071
9072 if (check_restricted() || check_secure())
9073 rettv->vval.v_number = -1;
9074 else
9075 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9076 get_tv_string_buf(&argvars[1], buf));
9077}
9078
9079/*
9080 * "repeat()" function
9081 */
9082 static void
9083f_repeat(typval_T *argvars, typval_T *rettv)
9084{
9085 char_u *p;
9086 int n;
9087 int slen;
9088 int len;
9089 char_u *r;
9090 int i;
9091
9092 n = (int)get_tv_number(&argvars[1]);
9093 if (argvars[0].v_type == VAR_LIST)
9094 {
9095 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9096 while (n-- > 0)
9097 if (list_extend(rettv->vval.v_list,
9098 argvars[0].vval.v_list, NULL) == FAIL)
9099 break;
9100 }
9101 else
9102 {
9103 p = get_tv_string(&argvars[0]);
9104 rettv->v_type = VAR_STRING;
9105 rettv->vval.v_string = NULL;
9106
9107 slen = (int)STRLEN(p);
9108 len = slen * n;
9109 if (len <= 0)
9110 return;
9111
9112 r = alloc(len + 1);
9113 if (r != NULL)
9114 {
9115 for (i = 0; i < n; i++)
9116 mch_memmove(r + i * slen, p, (size_t)slen);
9117 r[len] = NUL;
9118 }
9119
9120 rettv->vval.v_string = r;
9121 }
9122}
9123
9124/*
9125 * "resolve()" function
9126 */
9127 static void
9128f_resolve(typval_T *argvars, typval_T *rettv)
9129{
9130 char_u *p;
9131#ifdef HAVE_READLINK
9132 char_u *buf = NULL;
9133#endif
9134
9135 p = get_tv_string(&argvars[0]);
9136#ifdef FEAT_SHORTCUT
9137 {
9138 char_u *v = NULL;
9139
9140 v = mch_resolve_shortcut(p);
9141 if (v != NULL)
9142 rettv->vval.v_string = v;
9143 else
9144 rettv->vval.v_string = vim_strsave(p);
9145 }
9146#else
9147# ifdef HAVE_READLINK
9148 {
9149 char_u *cpy;
9150 int len;
9151 char_u *remain = NULL;
9152 char_u *q;
9153 int is_relative_to_current = FALSE;
9154 int has_trailing_pathsep = FALSE;
9155 int limit = 100;
9156
9157 p = vim_strsave(p);
9158
9159 if (p[0] == '.' && (vim_ispathsep(p[1])
9160 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9161 is_relative_to_current = TRUE;
9162
9163 len = STRLEN(p);
9164 if (len > 0 && after_pathsep(p, p + len))
9165 {
9166 has_trailing_pathsep = TRUE;
9167 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9168 }
9169
9170 q = getnextcomp(p);
9171 if (*q != NUL)
9172 {
9173 /* Separate the first path component in "p", and keep the
9174 * remainder (beginning with the path separator). */
9175 remain = vim_strsave(q - 1);
9176 q[-1] = NUL;
9177 }
9178
9179 buf = alloc(MAXPATHL + 1);
9180 if (buf == NULL)
9181 goto fail;
9182
9183 for (;;)
9184 {
9185 for (;;)
9186 {
9187 len = readlink((char *)p, (char *)buf, MAXPATHL);
9188 if (len <= 0)
9189 break;
9190 buf[len] = NUL;
9191
9192 if (limit-- == 0)
9193 {
9194 vim_free(p);
9195 vim_free(remain);
9196 EMSG(_("E655: Too many symbolic links (cycle?)"));
9197 rettv->vval.v_string = NULL;
9198 goto fail;
9199 }
9200
9201 /* Ensure that the result will have a trailing path separator
9202 * if the argument has one. */
9203 if (remain == NULL && has_trailing_pathsep)
9204 add_pathsep(buf);
9205
9206 /* Separate the first path component in the link value and
9207 * concatenate the remainders. */
9208 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9209 if (*q != NUL)
9210 {
9211 if (remain == NULL)
9212 remain = vim_strsave(q - 1);
9213 else
9214 {
9215 cpy = concat_str(q - 1, remain);
9216 if (cpy != NULL)
9217 {
9218 vim_free(remain);
9219 remain = cpy;
9220 }
9221 }
9222 q[-1] = NUL;
9223 }
9224
9225 q = gettail(p);
9226 if (q > p && *q == NUL)
9227 {
9228 /* Ignore trailing path separator. */
9229 q[-1] = NUL;
9230 q = gettail(p);
9231 }
9232 if (q > p && !mch_isFullName(buf))
9233 {
9234 /* symlink is relative to directory of argument */
9235 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9236 if (cpy != NULL)
9237 {
9238 STRCPY(cpy, p);
9239 STRCPY(gettail(cpy), buf);
9240 vim_free(p);
9241 p = cpy;
9242 }
9243 }
9244 else
9245 {
9246 vim_free(p);
9247 p = vim_strsave(buf);
9248 }
9249 }
9250
9251 if (remain == NULL)
9252 break;
9253
9254 /* Append the first path component of "remain" to "p". */
9255 q = getnextcomp(remain + 1);
9256 len = q - remain - (*q != NUL);
9257 cpy = vim_strnsave(p, STRLEN(p) + len);
9258 if (cpy != NULL)
9259 {
9260 STRNCAT(cpy, remain, len);
9261 vim_free(p);
9262 p = cpy;
9263 }
9264 /* Shorten "remain". */
9265 if (*q != NUL)
9266 STRMOVE(remain, q - 1);
9267 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009268 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009269 }
9270
9271 /* If the result is a relative path name, make it explicitly relative to
9272 * the current directory if and only if the argument had this form. */
9273 if (!vim_ispathsep(*p))
9274 {
9275 if (is_relative_to_current
9276 && *p != NUL
9277 && !(p[0] == '.'
9278 && (p[1] == NUL
9279 || vim_ispathsep(p[1])
9280 || (p[1] == '.'
9281 && (p[2] == NUL
9282 || vim_ispathsep(p[2]))))))
9283 {
9284 /* Prepend "./". */
9285 cpy = concat_str((char_u *)"./", p);
9286 if (cpy != NULL)
9287 {
9288 vim_free(p);
9289 p = cpy;
9290 }
9291 }
9292 else if (!is_relative_to_current)
9293 {
9294 /* Strip leading "./". */
9295 q = p;
9296 while (q[0] == '.' && vim_ispathsep(q[1]))
9297 q += 2;
9298 if (q > p)
9299 STRMOVE(p, p + 2);
9300 }
9301 }
9302
9303 /* Ensure that the result will have no trailing path separator
9304 * if the argument had none. But keep "/" or "//". */
9305 if (!has_trailing_pathsep)
9306 {
9307 q = p + STRLEN(p);
9308 if (after_pathsep(p, q))
9309 *gettail_sep(p) = NUL;
9310 }
9311
9312 rettv->vval.v_string = p;
9313 }
9314# else
9315 rettv->vval.v_string = vim_strsave(p);
9316# endif
9317#endif
9318
9319 simplify_filename(rettv->vval.v_string);
9320
9321#ifdef HAVE_READLINK
9322fail:
9323 vim_free(buf);
9324#endif
9325 rettv->v_type = VAR_STRING;
9326}
9327
9328/*
9329 * "reverse({list})" function
9330 */
9331 static void
9332f_reverse(typval_T *argvars, typval_T *rettv)
9333{
9334 list_T *l;
9335 listitem_T *li, *ni;
9336
9337 if (argvars[0].v_type != VAR_LIST)
9338 EMSG2(_(e_listarg), "reverse()");
9339 else if ((l = argvars[0].vval.v_list) != NULL
9340 && !tv_check_lock(l->lv_lock,
9341 (char_u *)N_("reverse() argument"), TRUE))
9342 {
9343 li = l->lv_last;
9344 l->lv_first = l->lv_last = NULL;
9345 l->lv_len = 0;
9346 while (li != NULL)
9347 {
9348 ni = li->li_prev;
9349 list_append(l, li);
9350 li = ni;
9351 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009352 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009353 l->lv_idx = l->lv_len - l->lv_idx - 1;
9354 }
9355}
9356
9357#define SP_NOMOVE 0x01 /* don't move cursor */
9358#define SP_REPEAT 0x02 /* repeat to find outer pair */
9359#define SP_RETCOUNT 0x04 /* return matchcount */
9360#define SP_SETPCMARK 0x08 /* set previous context mark */
9361#define SP_START 0x10 /* accept match at start position */
9362#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9363#define SP_END 0x40 /* leave cursor at end of match */
9364#define SP_COLUMN 0x80 /* start at cursor column */
9365
9366static int get_search_arg(typval_T *varp, int *flagsp);
9367
9368/*
9369 * Get flags for a search function.
9370 * Possibly sets "p_ws".
9371 * Returns BACKWARD, FORWARD or zero (for an error).
9372 */
9373 static int
9374get_search_arg(typval_T *varp, int *flagsp)
9375{
9376 int dir = FORWARD;
9377 char_u *flags;
9378 char_u nbuf[NUMBUFLEN];
9379 int mask;
9380
9381 if (varp->v_type != VAR_UNKNOWN)
9382 {
9383 flags = get_tv_string_buf_chk(varp, nbuf);
9384 if (flags == NULL)
9385 return 0; /* type error; errmsg already given */
9386 while (*flags != NUL)
9387 {
9388 switch (*flags)
9389 {
9390 case 'b': dir = BACKWARD; break;
9391 case 'w': p_ws = TRUE; break;
9392 case 'W': p_ws = FALSE; break;
9393 default: mask = 0;
9394 if (flagsp != NULL)
9395 switch (*flags)
9396 {
9397 case 'c': mask = SP_START; break;
9398 case 'e': mask = SP_END; break;
9399 case 'm': mask = SP_RETCOUNT; break;
9400 case 'n': mask = SP_NOMOVE; break;
9401 case 'p': mask = SP_SUBPAT; break;
9402 case 'r': mask = SP_REPEAT; break;
9403 case 's': mask = SP_SETPCMARK; break;
9404 case 'z': mask = SP_COLUMN; break;
9405 }
9406 if (mask == 0)
9407 {
9408 EMSG2(_(e_invarg2), flags);
9409 dir = 0;
9410 }
9411 else
9412 *flagsp |= mask;
9413 }
9414 if (dir == 0)
9415 break;
9416 ++flags;
9417 }
9418 }
9419 return dir;
9420}
9421
9422/*
9423 * Shared by search() and searchpos() functions.
9424 */
9425 static int
9426search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9427{
9428 int flags;
9429 char_u *pat;
9430 pos_T pos;
9431 pos_T save_cursor;
9432 int save_p_ws = p_ws;
9433 int dir;
9434 int retval = 0; /* default: FAIL */
9435 long lnum_stop = 0;
9436 proftime_T tm;
9437#ifdef FEAT_RELTIME
9438 long time_limit = 0;
9439#endif
9440 int options = SEARCH_KEEP;
9441 int subpatnum;
9442
9443 pat = get_tv_string(&argvars[0]);
9444 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9445 if (dir == 0)
9446 goto theend;
9447 flags = *flagsp;
9448 if (flags & SP_START)
9449 options |= SEARCH_START;
9450 if (flags & SP_END)
9451 options |= SEARCH_END;
9452 if (flags & SP_COLUMN)
9453 options |= SEARCH_COL;
9454
9455 /* Optional arguments: line number to stop searching and timeout. */
9456 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9457 {
9458 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9459 if (lnum_stop < 0)
9460 goto theend;
9461#ifdef FEAT_RELTIME
9462 if (argvars[3].v_type != VAR_UNKNOWN)
9463 {
9464 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9465 if (time_limit < 0)
9466 goto theend;
9467 }
9468#endif
9469 }
9470
9471#ifdef FEAT_RELTIME
9472 /* Set the time limit, if there is one. */
9473 profile_setlimit(time_limit, &tm);
9474#endif
9475
9476 /*
9477 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9478 * Check to make sure only those flags are set.
9479 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9480 * flags cannot be set. Check for that condition also.
9481 */
9482 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9483 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9484 {
9485 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9486 goto theend;
9487 }
9488
9489 pos = save_cursor = curwin->w_cursor;
9490 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009491 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009492 if (subpatnum != FAIL)
9493 {
9494 if (flags & SP_SUBPAT)
9495 retval = subpatnum;
9496 else
9497 retval = pos.lnum;
9498 if (flags & SP_SETPCMARK)
9499 setpcmark();
9500 curwin->w_cursor = pos;
9501 if (match_pos != NULL)
9502 {
9503 /* Store the match cursor position */
9504 match_pos->lnum = pos.lnum;
9505 match_pos->col = pos.col + 1;
9506 }
9507 /* "/$" will put the cursor after the end of the line, may need to
9508 * correct that here */
9509 check_cursor();
9510 }
9511
9512 /* If 'n' flag is used: restore cursor position. */
9513 if (flags & SP_NOMOVE)
9514 curwin->w_cursor = save_cursor;
9515 else
9516 curwin->w_set_curswant = TRUE;
9517theend:
9518 p_ws = save_p_ws;
9519
9520 return retval;
9521}
9522
9523#ifdef FEAT_FLOAT
9524
9525/*
9526 * round() is not in C90, use ceil() or floor() instead.
9527 */
9528 float_T
9529vim_round(float_T f)
9530{
9531 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9532}
9533
9534/*
9535 * "round({float})" function
9536 */
9537 static void
9538f_round(typval_T *argvars, typval_T *rettv)
9539{
9540 float_T f = 0.0;
9541
9542 rettv->v_type = VAR_FLOAT;
9543 if (get_float_arg(argvars, &f) == OK)
9544 rettv->vval.v_float = vim_round(f);
9545 else
9546 rettv->vval.v_float = 0.0;
9547}
9548#endif
9549
9550/*
9551 * "screenattr()" function
9552 */
9553 static void
9554f_screenattr(typval_T *argvars, typval_T *rettv)
9555{
9556 int row;
9557 int col;
9558 int c;
9559
9560 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9561 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9562 if (row < 0 || row >= screen_Rows
9563 || col < 0 || col >= screen_Columns)
9564 c = -1;
9565 else
9566 c = ScreenAttrs[LineOffset[row] + col];
9567 rettv->vval.v_number = c;
9568}
9569
9570/*
9571 * "screenchar()" function
9572 */
9573 static void
9574f_screenchar(typval_T *argvars, typval_T *rettv)
9575{
9576 int row;
9577 int col;
9578 int off;
9579 int c;
9580
9581 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9582 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9583 if (row < 0 || row >= screen_Rows
9584 || col < 0 || col >= screen_Columns)
9585 c = -1;
9586 else
9587 {
9588 off = LineOffset[row] + col;
9589#ifdef FEAT_MBYTE
9590 if (enc_utf8 && ScreenLinesUC[off] != 0)
9591 c = ScreenLinesUC[off];
9592 else
9593#endif
9594 c = ScreenLines[off];
9595 }
9596 rettv->vval.v_number = c;
9597}
9598
9599/*
9600 * "screencol()" function
9601 *
9602 * First column is 1 to be consistent with virtcol().
9603 */
9604 static void
9605f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9606{
9607 rettv->vval.v_number = screen_screencol() + 1;
9608}
9609
9610/*
9611 * "screenrow()" function
9612 */
9613 static void
9614f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9615{
9616 rettv->vval.v_number = screen_screenrow() + 1;
9617}
9618
9619/*
9620 * "search()" function
9621 */
9622 static void
9623f_search(typval_T *argvars, typval_T *rettv)
9624{
9625 int flags = 0;
9626
9627 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9628}
9629
9630/*
9631 * "searchdecl()" function
9632 */
9633 static void
9634f_searchdecl(typval_T *argvars, typval_T *rettv)
9635{
9636 int locally = 1;
9637 int thisblock = 0;
9638 int error = FALSE;
9639 char_u *name;
9640
9641 rettv->vval.v_number = 1; /* default: FAIL */
9642
9643 name = get_tv_string_chk(&argvars[0]);
9644 if (argvars[1].v_type != VAR_UNKNOWN)
9645 {
9646 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9647 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9648 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9649 }
9650 if (!error && name != NULL)
9651 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9652 locally, thisblock, SEARCH_KEEP) == FAIL;
9653}
9654
9655/*
9656 * Used by searchpair() and searchpairpos()
9657 */
9658 static int
9659searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9660{
9661 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009662 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009663 int save_p_ws = p_ws;
9664 int dir;
9665 int flags = 0;
9666 char_u nbuf1[NUMBUFLEN];
9667 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009668 int retval = 0; /* default: FAIL */
9669 long lnum_stop = 0;
9670 long time_limit = 0;
9671
9672 /* Get the three pattern arguments: start, middle, end. */
9673 spat = get_tv_string_chk(&argvars[0]);
9674 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9675 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9676 if (spat == NULL || mpat == NULL || epat == NULL)
9677 goto theend; /* type error */
9678
9679 /* Handle the optional fourth argument: flags */
9680 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9681 if (dir == 0)
9682 goto theend;
9683
9684 /* Don't accept SP_END or SP_SUBPAT.
9685 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9686 */
9687 if ((flags & (SP_END | SP_SUBPAT)) != 0
9688 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9689 {
9690 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9691 goto theend;
9692 }
9693
9694 /* Using 'r' implies 'W', otherwise it doesn't work. */
9695 if (flags & SP_REPEAT)
9696 p_ws = FALSE;
9697
9698 /* Optional fifth argument: skip expression */
9699 if (argvars[3].v_type == VAR_UNKNOWN
9700 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009701 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009702 else
9703 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009704 skip = &argvars[4];
9705 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9706 && skip->v_type != VAR_STRING)
9707 {
9708 /* Type error */
9709 goto theend;
9710 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009711 if (argvars[5].v_type != VAR_UNKNOWN)
9712 {
9713 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9714 if (lnum_stop < 0)
9715 goto theend;
9716#ifdef FEAT_RELTIME
9717 if (argvars[6].v_type != VAR_UNKNOWN)
9718 {
9719 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9720 if (time_limit < 0)
9721 goto theend;
9722 }
9723#endif
9724 }
9725 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009726
9727 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9728 match_pos, lnum_stop, time_limit);
9729
9730theend:
9731 p_ws = save_p_ws;
9732
9733 return retval;
9734}
9735
9736/*
9737 * "searchpair()" function
9738 */
9739 static void
9740f_searchpair(typval_T *argvars, typval_T *rettv)
9741{
9742 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9743}
9744
9745/*
9746 * "searchpairpos()" function
9747 */
9748 static void
9749f_searchpairpos(typval_T *argvars, typval_T *rettv)
9750{
9751 pos_T match_pos;
9752 int lnum = 0;
9753 int col = 0;
9754
9755 if (rettv_list_alloc(rettv) == FAIL)
9756 return;
9757
9758 if (searchpair_cmn(argvars, &match_pos) > 0)
9759 {
9760 lnum = match_pos.lnum;
9761 col = match_pos.col;
9762 }
9763
9764 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9765 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9766}
9767
9768/*
9769 * Search for a start/middle/end thing.
9770 * Used by searchpair(), see its documentation for the details.
9771 * Returns 0 or -1 for no match,
9772 */
9773 long
9774do_searchpair(
9775 char_u *spat, /* start pattern */
9776 char_u *mpat, /* middle pattern */
9777 char_u *epat, /* end pattern */
9778 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009779 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009780 int flags, /* SP_SETPCMARK and other SP_ values */
9781 pos_T *match_pos,
9782 linenr_T lnum_stop, /* stop at this line if not zero */
9783 long time_limit UNUSED) /* stop after this many msec */
9784{
9785 char_u *save_cpo;
9786 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9787 long retval = 0;
9788 pos_T pos;
9789 pos_T firstpos;
9790 pos_T foundpos;
9791 pos_T save_cursor;
9792 pos_T save_pos;
9793 int n;
9794 int r;
9795 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009796 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009797 int err;
9798 int options = SEARCH_KEEP;
9799 proftime_T tm;
9800
9801 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9802 save_cpo = p_cpo;
9803 p_cpo = empty_option;
9804
9805#ifdef FEAT_RELTIME
9806 /* Set the time limit, if there is one. */
9807 profile_setlimit(time_limit, &tm);
9808#endif
9809
9810 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9811 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009812 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9813 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009814 if (pat2 == NULL || pat3 == NULL)
9815 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009816 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009817 if (*mpat == NUL)
9818 STRCPY(pat3, pat2);
9819 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009820 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009821 spat, epat, mpat);
9822 if (flags & SP_START)
9823 options |= SEARCH_START;
9824
Bram Moolenaar48570482017-10-30 21:48:41 +01009825 if (skip != NULL)
9826 {
9827 /* Empty string means to not use the skip expression. */
9828 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9829 use_skip = skip->vval.v_string != NULL
9830 && *skip->vval.v_string != NUL;
9831 }
9832
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009833 save_cursor = curwin->w_cursor;
9834 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009835 CLEAR_POS(&firstpos);
9836 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009837 pat = pat3;
9838 for (;;)
9839 {
9840 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009841 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009842 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009843 /* didn't find it or found the first match again: FAIL */
9844 break;
9845
9846 if (firstpos.lnum == 0)
9847 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009848 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009849 {
9850 /* Found the same position again. Can happen with a pattern that
9851 * has "\zs" at the end and searching backwards. Advance one
9852 * character and try again. */
9853 if (dir == BACKWARD)
9854 decl(&pos);
9855 else
9856 incl(&pos);
9857 }
9858 foundpos = pos;
9859
9860 /* clear the start flag to avoid getting stuck here */
9861 options &= ~SEARCH_START;
9862
9863 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009864 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009865 {
9866 save_pos = curwin->w_cursor;
9867 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009868 err = FALSE;
9869 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009870 curwin->w_cursor = save_pos;
9871 if (err)
9872 {
9873 /* Evaluating {skip} caused an error, break here. */
9874 curwin->w_cursor = save_cursor;
9875 retval = -1;
9876 break;
9877 }
9878 if (r)
9879 continue;
9880 }
9881
9882 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9883 {
9884 /* Found end when searching backwards or start when searching
9885 * forward: nested pair. */
9886 ++nest;
9887 pat = pat2; /* nested, don't search for middle */
9888 }
9889 else
9890 {
9891 /* Found end when searching forward or start when searching
9892 * backward: end of (nested) pair; or found middle in outer pair. */
9893 if (--nest == 1)
9894 pat = pat3; /* outer level, search for middle */
9895 }
9896
9897 if (nest == 0)
9898 {
9899 /* Found the match: return matchcount or line number. */
9900 if (flags & SP_RETCOUNT)
9901 ++retval;
9902 else
9903 retval = pos.lnum;
9904 if (flags & SP_SETPCMARK)
9905 setpcmark();
9906 curwin->w_cursor = pos;
9907 if (!(flags & SP_REPEAT))
9908 break;
9909 nest = 1; /* search for next unmatched */
9910 }
9911 }
9912
9913 if (match_pos != NULL)
9914 {
9915 /* Store the match cursor position */
9916 match_pos->lnum = curwin->w_cursor.lnum;
9917 match_pos->col = curwin->w_cursor.col + 1;
9918 }
9919
9920 /* If 'n' flag is used or search failed: restore cursor position. */
9921 if ((flags & SP_NOMOVE) || retval == 0)
9922 curwin->w_cursor = save_cursor;
9923
9924theend:
9925 vim_free(pat2);
9926 vim_free(pat3);
9927 if (p_cpo == empty_option)
9928 p_cpo = save_cpo;
9929 else
9930 /* Darn, evaluating the {skip} expression changed the value. */
9931 free_string_option(save_cpo);
9932
9933 return retval;
9934}
9935
9936/*
9937 * "searchpos()" function
9938 */
9939 static void
9940f_searchpos(typval_T *argvars, typval_T *rettv)
9941{
9942 pos_T match_pos;
9943 int lnum = 0;
9944 int col = 0;
9945 int n;
9946 int flags = 0;
9947
9948 if (rettv_list_alloc(rettv) == FAIL)
9949 return;
9950
9951 n = search_cmn(argvars, &match_pos, &flags);
9952 if (n > 0)
9953 {
9954 lnum = match_pos.lnum;
9955 col = match_pos.col;
9956 }
9957
9958 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9959 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9960 if (flags & SP_SUBPAT)
9961 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9962}
9963
9964 static void
9965f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9966{
9967#ifdef FEAT_CLIENTSERVER
9968 char_u buf[NUMBUFLEN];
9969 char_u *server = get_tv_string_chk(&argvars[0]);
9970 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9971
9972 rettv->vval.v_number = -1;
9973 if (server == NULL || reply == NULL)
9974 return;
9975 if (check_restricted() || check_secure())
9976 return;
9977# ifdef FEAT_X11
9978 if (check_connection() == FAIL)
9979 return;
9980# endif
9981
9982 if (serverSendReply(server, reply) < 0)
9983 {
9984 EMSG(_("E258: Unable to send to client"));
9985 return;
9986 }
9987 rettv->vval.v_number = 0;
9988#else
9989 rettv->vval.v_number = -1;
9990#endif
9991}
9992
9993 static void
9994f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9995{
9996 char_u *r = NULL;
9997
9998#ifdef FEAT_CLIENTSERVER
9999# ifdef WIN32
10000 r = serverGetVimNames();
10001# else
10002 make_connection();
10003 if (X_DISPLAY != NULL)
10004 r = serverGetVimNames(X_DISPLAY);
10005# endif
10006#endif
10007 rettv->v_type = VAR_STRING;
10008 rettv->vval.v_string = r;
10009}
10010
10011/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010012 * Set line or list of lines in buffer "buf".
10013 */
10014 static void
10015set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
10016{
10017 char_u *line = NULL;
10018 list_T *l = NULL;
10019 listitem_T *li = NULL;
10020 long added = 0;
10021 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010022 buf_T *curbuf_save = NULL;
10023 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010024 int is_curbuf = buf == curbuf;
10025
Bram Moolenaar9d954202017-09-04 20:34:19 +020010026 /* When using the current buffer ml_mfp will be set if needed. Useful when
10027 * setline() is used on startup. For other buffers the buffer must be
10028 * loaded. */
10029 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010030 {
10031 rettv->vval.v_number = 1; /* FAIL */
10032 return;
10033 }
10034
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010035 if (!is_curbuf)
10036 {
10037 wininfo_T *wip;
10038
10039 curbuf_save = curbuf;
10040 curwin_save = curwin;
10041 curbuf = buf;
10042 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10043 {
10044 if (wip->wi_win != NULL)
10045 {
10046 curwin = wip->wi_win;
10047 break;
10048 }
10049 }
10050 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010051
10052 lcount = curbuf->b_ml.ml_line_count;
10053
10054 if (lines->v_type == VAR_LIST)
10055 {
10056 l = lines->vval.v_list;
10057 li = l->lv_first;
10058 }
10059 else
10060 line = get_tv_string_chk(lines);
10061
10062 /* default result is zero == OK */
10063 for (;;)
10064 {
10065 if (l != NULL)
10066 {
10067 /* list argument, get next string */
10068 if (li == NULL)
10069 break;
10070 line = get_tv_string_chk(&li->li_tv);
10071 li = li->li_next;
10072 }
10073
10074 rettv->vval.v_number = 1; /* FAIL */
10075 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10076 break;
10077
10078 /* When coming here from Insert mode, sync undo, so that this can be
10079 * undone separately from what was previously inserted. */
10080 if (u_sync_once == 2)
10081 {
10082 u_sync_once = 1; /* notify that u_sync() was called */
10083 u_sync(TRUE);
10084 }
10085
10086 if (lnum <= curbuf->b_ml.ml_line_count)
10087 {
10088 /* existing line, replace it */
10089 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10090 {
10091 changed_bytes(lnum, 0);
10092 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10093 check_cursor_col();
10094 rettv->vval.v_number = 0; /* OK */
10095 }
10096 }
10097 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10098 {
10099 /* lnum is one past the last line, append the line */
10100 ++added;
10101 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10102 rettv->vval.v_number = 0; /* OK */
10103 }
10104
10105 if (l == NULL) /* only one string argument */
10106 break;
10107 ++lnum;
10108 }
10109
10110 if (added > 0)
10111 appended_lines_mark(lcount, added);
10112
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010113 if (!is_curbuf)
10114 {
10115 curbuf = curbuf_save;
10116 curwin = curwin_save;
10117 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010118}
10119
10120/*
10121 * "setbufline()" function
10122 */
10123 static void
10124f_setbufline(argvars, rettv)
10125 typval_T *argvars;
10126 typval_T *rettv;
10127{
10128 linenr_T lnum;
10129 buf_T *buf;
10130
10131 buf = get_buf_tv(&argvars[0], FALSE);
10132 if (buf == NULL)
10133 rettv->vval.v_number = 1; /* FAIL */
10134 else
10135 {
10136 lnum = get_tv_lnum_buf(&argvars[1], buf);
10137
10138 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10139 }
10140}
10141
10142/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010143 * "setbufvar()" function
10144 */
10145 static void
10146f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10147{
10148 buf_T *buf;
10149 char_u *varname, *bufvarname;
10150 typval_T *varp;
10151 char_u nbuf[NUMBUFLEN];
10152
10153 if (check_restricted() || check_secure())
10154 return;
10155 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10156 varname = get_tv_string_chk(&argvars[1]);
10157 buf = get_buf_tv(&argvars[0], FALSE);
10158 varp = &argvars[2];
10159
10160 if (buf != NULL && varname != NULL && varp != NULL)
10161 {
10162 if (*varname == '&')
10163 {
10164 long numval;
10165 char_u *strval;
10166 int error = FALSE;
10167 aco_save_T aco;
10168
10169 /* set curbuf to be our buf, temporarily */
10170 aucmd_prepbuf(&aco, buf);
10171
10172 ++varname;
10173 numval = (long)get_tv_number_chk(varp, &error);
10174 strval = get_tv_string_buf_chk(varp, nbuf);
10175 if (!error && strval != NULL)
10176 set_option_value(varname, numval, strval, OPT_LOCAL);
10177
10178 /* reset notion of buffer */
10179 aucmd_restbuf(&aco);
10180 }
10181 else
10182 {
10183 buf_T *save_curbuf = curbuf;
10184
10185 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10186 if (bufvarname != NULL)
10187 {
10188 curbuf = buf;
10189 STRCPY(bufvarname, "b:");
10190 STRCPY(bufvarname + 2, varname);
10191 set_var(bufvarname, varp, TRUE);
10192 vim_free(bufvarname);
10193 curbuf = save_curbuf;
10194 }
10195 }
10196 }
10197}
10198
10199 static void
10200f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10201{
10202 dict_T *d;
10203 dictitem_T *di;
10204 char_u *csearch;
10205
10206 if (argvars[0].v_type != VAR_DICT)
10207 {
10208 EMSG(_(e_dictreq));
10209 return;
10210 }
10211
10212 if ((d = argvars[0].vval.v_dict) != NULL)
10213 {
10214 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10215 if (csearch != NULL)
10216 {
10217#ifdef FEAT_MBYTE
10218 if (enc_utf8)
10219 {
10220 int pcc[MAX_MCO];
10221 int c = utfc_ptr2char(csearch, pcc);
10222
10223 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10224 }
10225 else
10226#endif
10227 set_last_csearch(PTR2CHAR(csearch),
10228 csearch, MB_PTR2LEN(csearch));
10229 }
10230
10231 di = dict_find(d, (char_u *)"forward", -1);
10232 if (di != NULL)
10233 set_csearch_direction((int)get_tv_number(&di->di_tv)
10234 ? FORWARD : BACKWARD);
10235
10236 di = dict_find(d, (char_u *)"until", -1);
10237 if (di != NULL)
10238 set_csearch_until(!!get_tv_number(&di->di_tv));
10239 }
10240}
10241
10242/*
10243 * "setcmdpos()" function
10244 */
10245 static void
10246f_setcmdpos(typval_T *argvars, typval_T *rettv)
10247{
10248 int pos = (int)get_tv_number(&argvars[0]) - 1;
10249
10250 if (pos >= 0)
10251 rettv->vval.v_number = set_cmdline_pos(pos);
10252}
10253
10254/*
10255 * "setfperm({fname}, {mode})" function
10256 */
10257 static void
10258f_setfperm(typval_T *argvars, typval_T *rettv)
10259{
10260 char_u *fname;
10261 char_u modebuf[NUMBUFLEN];
10262 char_u *mode_str;
10263 int i;
10264 int mask;
10265 int mode = 0;
10266
10267 rettv->vval.v_number = 0;
10268 fname = get_tv_string_chk(&argvars[0]);
10269 if (fname == NULL)
10270 return;
10271 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10272 if (mode_str == NULL)
10273 return;
10274 if (STRLEN(mode_str) != 9)
10275 {
10276 EMSG2(_(e_invarg2), mode_str);
10277 return;
10278 }
10279
10280 mask = 1;
10281 for (i = 8; i >= 0; --i)
10282 {
10283 if (mode_str[i] != '-')
10284 mode |= mask;
10285 mask = mask << 1;
10286 }
10287 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10288}
10289
10290/*
10291 * "setline()" function
10292 */
10293 static void
10294f_setline(typval_T *argvars, typval_T *rettv)
10295{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010296 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010297
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010298 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010299}
10300
Bram Moolenaard823fa92016-08-12 16:29:27 +020010301static 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 +020010302
10303/*
10304 * Used by "setqflist()" and "setloclist()" functions
10305 */
10306 static void
10307set_qf_ll_list(
10308 win_T *wp UNUSED,
10309 typval_T *list_arg UNUSED,
10310 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010311 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010312 typval_T *rettv)
10313{
10314#ifdef FEAT_QUICKFIX
10315 static char *e_invact = N_("E927: Invalid action: '%s'");
10316 char_u *act;
10317 int action = 0;
10318#endif
10319
10320 rettv->vval.v_number = -1;
10321
10322#ifdef FEAT_QUICKFIX
10323 if (list_arg->v_type != VAR_LIST)
10324 EMSG(_(e_listreq));
10325 else
10326 {
10327 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010328 dict_T *d = NULL;
10329 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010330
10331 if (action_arg->v_type == VAR_STRING)
10332 {
10333 act = get_tv_string_chk(action_arg);
10334 if (act == NULL)
10335 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010336 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10337 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010338 action = *act;
10339 else
10340 EMSG2(_(e_invact), act);
10341 }
10342 else if (action_arg->v_type == VAR_UNKNOWN)
10343 action = ' ';
10344 else
10345 EMSG(_(e_stringreq));
10346
Bram Moolenaard823fa92016-08-12 16:29:27 +020010347 if (action_arg->v_type != VAR_UNKNOWN
10348 && what_arg->v_type != VAR_UNKNOWN)
10349 {
10350 if (what_arg->v_type == VAR_DICT)
10351 d = what_arg->vval.v_dict;
10352 else
10353 {
10354 EMSG(_(e_dictreq));
10355 valid_dict = FALSE;
10356 }
10357 }
10358
10359 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10360 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010361 rettv->vval.v_number = 0;
10362 }
10363#endif
10364}
10365
10366/*
10367 * "setloclist()" function
10368 */
10369 static void
10370f_setloclist(typval_T *argvars, typval_T *rettv)
10371{
10372 win_T *win;
10373
10374 rettv->vval.v_number = -1;
10375
10376 win = find_win_by_nr(&argvars[0], NULL);
10377 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010378 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010379}
10380
10381/*
10382 * "setmatches()" function
10383 */
10384 static void
10385f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10386{
10387#ifdef FEAT_SEARCH_EXTRA
10388 list_T *l;
10389 listitem_T *li;
10390 dict_T *d;
10391 list_T *s = NULL;
10392
10393 rettv->vval.v_number = -1;
10394 if (argvars[0].v_type != VAR_LIST)
10395 {
10396 EMSG(_(e_listreq));
10397 return;
10398 }
10399 if ((l = argvars[0].vval.v_list) != NULL)
10400 {
10401
10402 /* To some extent make sure that we are dealing with a list from
10403 * "getmatches()". */
10404 li = l->lv_first;
10405 while (li != NULL)
10406 {
10407 if (li->li_tv.v_type != VAR_DICT
10408 || (d = li->li_tv.vval.v_dict) == NULL)
10409 {
10410 EMSG(_(e_invarg));
10411 return;
10412 }
10413 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10414 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10415 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10416 && dict_find(d, (char_u *)"priority", -1) != NULL
10417 && dict_find(d, (char_u *)"id", -1) != NULL))
10418 {
10419 EMSG(_(e_invarg));
10420 return;
10421 }
10422 li = li->li_next;
10423 }
10424
10425 clear_matches(curwin);
10426 li = l->lv_first;
10427 while (li != NULL)
10428 {
10429 int i = 0;
10430 char_u buf[5];
10431 dictitem_T *di;
10432 char_u *group;
10433 int priority;
10434 int id;
10435 char_u *conceal;
10436
10437 d = li->li_tv.vval.v_dict;
10438 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10439 {
10440 if (s == NULL)
10441 {
10442 s = list_alloc();
10443 if (s == NULL)
10444 return;
10445 }
10446
10447 /* match from matchaddpos() */
10448 for (i = 1; i < 9; i++)
10449 {
10450 sprintf((char *)buf, (char *)"pos%d", i);
10451 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10452 {
10453 if (di->di_tv.v_type != VAR_LIST)
10454 return;
10455
10456 list_append_tv(s, &di->di_tv);
10457 s->lv_refcount++;
10458 }
10459 else
10460 break;
10461 }
10462 }
10463
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010464 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010465 priority = (int)get_dict_number(d, (char_u *)"priority");
10466 id = (int)get_dict_number(d, (char_u *)"id");
10467 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010468 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010469 : NULL;
10470 if (i == 0)
10471 {
10472 match_add(curwin, group,
10473 get_dict_string(d, (char_u *)"pattern", FALSE),
10474 priority, id, NULL, conceal);
10475 }
10476 else
10477 {
10478 match_add(curwin, group, NULL, priority, id, s, conceal);
10479 list_unref(s);
10480 s = NULL;
10481 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010482 vim_free(group);
10483 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010484
10485 li = li->li_next;
10486 }
10487 rettv->vval.v_number = 0;
10488 }
10489#endif
10490}
10491
10492/*
10493 * "setpos()" function
10494 */
10495 static void
10496f_setpos(typval_T *argvars, typval_T *rettv)
10497{
10498 pos_T pos;
10499 int fnum;
10500 char_u *name;
10501 colnr_T curswant = -1;
10502
10503 rettv->vval.v_number = -1;
10504 name = get_tv_string_chk(argvars);
10505 if (name != NULL)
10506 {
10507 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10508 {
10509 if (--pos.col < 0)
10510 pos.col = 0;
10511 if (name[0] == '.' && name[1] == NUL)
10512 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010513 /* set cursor; "fnum" is ignored */
10514 curwin->w_cursor = pos;
10515 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010516 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010517 curwin->w_curswant = curswant - 1;
10518 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010519 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010520 check_cursor();
10521 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010522 }
10523 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10524 {
10525 /* set mark */
10526 if (setmark_pos(name[1], &pos, fnum) == OK)
10527 rettv->vval.v_number = 0;
10528 }
10529 else
10530 EMSG(_(e_invarg));
10531 }
10532 }
10533}
10534
10535/*
10536 * "setqflist()" function
10537 */
10538 static void
10539f_setqflist(typval_T *argvars, typval_T *rettv)
10540{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010541 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010542}
10543
10544/*
10545 * "setreg()" function
10546 */
10547 static void
10548f_setreg(typval_T *argvars, typval_T *rettv)
10549{
10550 int regname;
10551 char_u *strregname;
10552 char_u *stropt;
10553 char_u *strval;
10554 int append;
10555 char_u yank_type;
10556 long block_len;
10557
10558 block_len = -1;
10559 yank_type = MAUTO;
10560 append = FALSE;
10561
10562 strregname = get_tv_string_chk(argvars);
10563 rettv->vval.v_number = 1; /* FAIL is default */
10564
10565 if (strregname == NULL)
10566 return; /* type error; errmsg already given */
10567 regname = *strregname;
10568 if (regname == 0 || regname == '@')
10569 regname = '"';
10570
10571 if (argvars[2].v_type != VAR_UNKNOWN)
10572 {
10573 stropt = get_tv_string_chk(&argvars[2]);
10574 if (stropt == NULL)
10575 return; /* type error */
10576 for (; *stropt != NUL; ++stropt)
10577 switch (*stropt)
10578 {
10579 case 'a': case 'A': /* append */
10580 append = TRUE;
10581 break;
10582 case 'v': case 'c': /* character-wise selection */
10583 yank_type = MCHAR;
10584 break;
10585 case 'V': case 'l': /* line-wise selection */
10586 yank_type = MLINE;
10587 break;
10588 case 'b': case Ctrl_V: /* block-wise selection */
10589 yank_type = MBLOCK;
10590 if (VIM_ISDIGIT(stropt[1]))
10591 {
10592 ++stropt;
10593 block_len = getdigits(&stropt) - 1;
10594 --stropt;
10595 }
10596 break;
10597 }
10598 }
10599
10600 if (argvars[1].v_type == VAR_LIST)
10601 {
10602 char_u **lstval;
10603 char_u **allocval;
10604 char_u buf[NUMBUFLEN];
10605 char_u **curval;
10606 char_u **curallocval;
10607 list_T *ll = argvars[1].vval.v_list;
10608 listitem_T *li;
10609 int len;
10610
10611 /* If the list is NULL handle like an empty list. */
10612 len = ll == NULL ? 0 : ll->lv_len;
10613
10614 /* First half: use for pointers to result lines; second half: use for
10615 * pointers to allocated copies. */
10616 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10617 if (lstval == NULL)
10618 return;
10619 curval = lstval;
10620 allocval = lstval + len + 2;
10621 curallocval = allocval;
10622
10623 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10624 li = li->li_next)
10625 {
10626 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10627 if (strval == NULL)
10628 goto free_lstval;
10629 if (strval == buf)
10630 {
10631 /* Need to make a copy, next get_tv_string_buf_chk() will
10632 * overwrite the string. */
10633 strval = vim_strsave(buf);
10634 if (strval == NULL)
10635 goto free_lstval;
10636 *curallocval++ = strval;
10637 }
10638 *curval++ = strval;
10639 }
10640 *curval++ = NULL;
10641
10642 write_reg_contents_lst(regname, lstval, -1,
10643 append, yank_type, block_len);
10644free_lstval:
10645 while (curallocval > allocval)
10646 vim_free(*--curallocval);
10647 vim_free(lstval);
10648 }
10649 else
10650 {
10651 strval = get_tv_string_chk(&argvars[1]);
10652 if (strval == NULL)
10653 return;
10654 write_reg_contents_ex(regname, strval, -1,
10655 append, yank_type, block_len);
10656 }
10657 rettv->vval.v_number = 0;
10658}
10659
10660/*
10661 * "settabvar()" function
10662 */
10663 static void
10664f_settabvar(typval_T *argvars, typval_T *rettv)
10665{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010666 tabpage_T *save_curtab;
10667 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010668 char_u *varname, *tabvarname;
10669 typval_T *varp;
10670
10671 rettv->vval.v_number = 0;
10672
10673 if (check_restricted() || check_secure())
10674 return;
10675
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010676 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010677 varname = get_tv_string_chk(&argvars[1]);
10678 varp = &argvars[2];
10679
Bram Moolenaar4033c552017-09-16 20:54:51 +020010680 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010681 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010682 save_curtab = curtab;
10683 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010684
10685 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10686 if (tabvarname != NULL)
10687 {
10688 STRCPY(tabvarname, "t:");
10689 STRCPY(tabvarname + 2, varname);
10690 set_var(tabvarname, varp, TRUE);
10691 vim_free(tabvarname);
10692 }
10693
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010694 /* Restore current tabpage */
10695 if (valid_tabpage(save_curtab))
10696 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010697 }
10698}
10699
10700/*
10701 * "settabwinvar()" function
10702 */
10703 static void
10704f_settabwinvar(typval_T *argvars, typval_T *rettv)
10705{
10706 setwinvar(argvars, rettv, 1);
10707}
10708
10709/*
10710 * "setwinvar()" function
10711 */
10712 static void
10713f_setwinvar(typval_T *argvars, typval_T *rettv)
10714{
10715 setwinvar(argvars, rettv, 0);
10716}
10717
10718#ifdef FEAT_CRYPT
10719/*
10720 * "sha256({string})" function
10721 */
10722 static void
10723f_sha256(typval_T *argvars, typval_T *rettv)
10724{
10725 char_u *p;
10726
10727 p = get_tv_string(&argvars[0]);
10728 rettv->vval.v_string = vim_strsave(
10729 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10730 rettv->v_type = VAR_STRING;
10731}
10732#endif /* FEAT_CRYPT */
10733
10734/*
10735 * "shellescape({string})" function
10736 */
10737 static void
10738f_shellescape(typval_T *argvars, typval_T *rettv)
10739{
Bram Moolenaar20615522017-06-05 18:46:26 +020010740 int do_special = non_zero_arg(&argvars[1]);
10741
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010742 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010743 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010744 rettv->v_type = VAR_STRING;
10745}
10746
10747/*
10748 * shiftwidth() function
10749 */
10750 static void
10751f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10752{
10753 rettv->vval.v_number = get_sw_value(curbuf);
10754}
10755
10756/*
10757 * "simplify()" function
10758 */
10759 static void
10760f_simplify(typval_T *argvars, typval_T *rettv)
10761{
10762 char_u *p;
10763
10764 p = get_tv_string(&argvars[0]);
10765 rettv->vval.v_string = vim_strsave(p);
10766 simplify_filename(rettv->vval.v_string); /* simplify in place */
10767 rettv->v_type = VAR_STRING;
10768}
10769
10770#ifdef FEAT_FLOAT
10771/*
10772 * "sin()" function
10773 */
10774 static void
10775f_sin(typval_T *argvars, typval_T *rettv)
10776{
10777 float_T f = 0.0;
10778
10779 rettv->v_type = VAR_FLOAT;
10780 if (get_float_arg(argvars, &f) == OK)
10781 rettv->vval.v_float = sin(f);
10782 else
10783 rettv->vval.v_float = 0.0;
10784}
10785
10786/*
10787 * "sinh()" function
10788 */
10789 static void
10790f_sinh(typval_T *argvars, typval_T *rettv)
10791{
10792 float_T f = 0.0;
10793
10794 rettv->v_type = VAR_FLOAT;
10795 if (get_float_arg(argvars, &f) == OK)
10796 rettv->vval.v_float = sinh(f);
10797 else
10798 rettv->vval.v_float = 0.0;
10799}
10800#endif
10801
10802static int
10803#ifdef __BORLANDC__
10804 _RTLENTRYF
10805#endif
10806 item_compare(const void *s1, const void *s2);
10807static int
10808#ifdef __BORLANDC__
10809 _RTLENTRYF
10810#endif
10811 item_compare2(const void *s1, const void *s2);
10812
10813/* struct used in the array that's given to qsort() */
10814typedef struct
10815{
10816 listitem_T *item;
10817 int idx;
10818} sortItem_T;
10819
10820/* struct storing information about current sort */
10821typedef struct
10822{
10823 int item_compare_ic;
10824 int item_compare_numeric;
10825 int item_compare_numbers;
10826#ifdef FEAT_FLOAT
10827 int item_compare_float;
10828#endif
10829 char_u *item_compare_func;
10830 partial_T *item_compare_partial;
10831 dict_T *item_compare_selfdict;
10832 int item_compare_func_err;
10833 int item_compare_keep_zero;
10834} sortinfo_T;
10835static sortinfo_T *sortinfo = NULL;
10836static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10837#define ITEM_COMPARE_FAIL 999
10838
10839/*
10840 * Compare functions for f_sort() and f_uniq() below.
10841 */
10842 static int
10843#ifdef __BORLANDC__
10844_RTLENTRYF
10845#endif
10846item_compare(const void *s1, const void *s2)
10847{
10848 sortItem_T *si1, *si2;
10849 typval_T *tv1, *tv2;
10850 char_u *p1, *p2;
10851 char_u *tofree1 = NULL, *tofree2 = NULL;
10852 int res;
10853 char_u numbuf1[NUMBUFLEN];
10854 char_u numbuf2[NUMBUFLEN];
10855
10856 si1 = (sortItem_T *)s1;
10857 si2 = (sortItem_T *)s2;
10858 tv1 = &si1->item->li_tv;
10859 tv2 = &si2->item->li_tv;
10860
10861 if (sortinfo->item_compare_numbers)
10862 {
10863 varnumber_T v1 = get_tv_number(tv1);
10864 varnumber_T v2 = get_tv_number(tv2);
10865
10866 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10867 }
10868
10869#ifdef FEAT_FLOAT
10870 if (sortinfo->item_compare_float)
10871 {
10872 float_T v1 = get_tv_float(tv1);
10873 float_T v2 = get_tv_float(tv2);
10874
10875 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10876 }
10877#endif
10878
10879 /* tv2string() puts quotes around a string and allocates memory. Don't do
10880 * that for string variables. Use a single quote when comparing with a
10881 * non-string to do what the docs promise. */
10882 if (tv1->v_type == VAR_STRING)
10883 {
10884 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10885 p1 = (char_u *)"'";
10886 else
10887 p1 = tv1->vval.v_string;
10888 }
10889 else
10890 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10891 if (tv2->v_type == VAR_STRING)
10892 {
10893 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10894 p2 = (char_u *)"'";
10895 else
10896 p2 = tv2->vval.v_string;
10897 }
10898 else
10899 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10900 if (p1 == NULL)
10901 p1 = (char_u *)"";
10902 if (p2 == NULL)
10903 p2 = (char_u *)"";
10904 if (!sortinfo->item_compare_numeric)
10905 {
10906 if (sortinfo->item_compare_ic)
10907 res = STRICMP(p1, p2);
10908 else
10909 res = STRCMP(p1, p2);
10910 }
10911 else
10912 {
10913 double n1, n2;
10914 n1 = strtod((char *)p1, (char **)&p1);
10915 n2 = strtod((char *)p2, (char **)&p2);
10916 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10917 }
10918
10919 /* When the result would be zero, compare the item indexes. Makes the
10920 * sort stable. */
10921 if (res == 0 && !sortinfo->item_compare_keep_zero)
10922 res = si1->idx > si2->idx ? 1 : -1;
10923
10924 vim_free(tofree1);
10925 vim_free(tofree2);
10926 return res;
10927}
10928
10929 static int
10930#ifdef __BORLANDC__
10931_RTLENTRYF
10932#endif
10933item_compare2(const void *s1, const void *s2)
10934{
10935 sortItem_T *si1, *si2;
10936 int res;
10937 typval_T rettv;
10938 typval_T argv[3];
10939 int dummy;
10940 char_u *func_name;
10941 partial_T *partial = sortinfo->item_compare_partial;
10942
10943 /* shortcut after failure in previous call; compare all items equal */
10944 if (sortinfo->item_compare_func_err)
10945 return 0;
10946
10947 si1 = (sortItem_T *)s1;
10948 si2 = (sortItem_T *)s2;
10949
10950 if (partial == NULL)
10951 func_name = sortinfo->item_compare_func;
10952 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010953 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010954
10955 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10956 * in the copy without changing the original list items. */
10957 copy_tv(&si1->item->li_tv, &argv[0]);
10958 copy_tv(&si2->item->li_tv, &argv[1]);
10959
10960 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10961 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010962 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010963 partial, sortinfo->item_compare_selfdict);
10964 clear_tv(&argv[0]);
10965 clear_tv(&argv[1]);
10966
10967 if (res == FAIL)
10968 res = ITEM_COMPARE_FAIL;
10969 else
10970 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10971 if (sortinfo->item_compare_func_err)
10972 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10973 clear_tv(&rettv);
10974
10975 /* When the result would be zero, compare the pointers themselves. Makes
10976 * the sort stable. */
10977 if (res == 0 && !sortinfo->item_compare_keep_zero)
10978 res = si1->idx > si2->idx ? 1 : -1;
10979
10980 return res;
10981}
10982
10983/*
10984 * "sort({list})" function
10985 */
10986 static void
10987do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
10988{
10989 list_T *l;
10990 listitem_T *li;
10991 sortItem_T *ptrs;
10992 sortinfo_T *old_sortinfo;
10993 sortinfo_T info;
10994 long len;
10995 long i;
10996
10997 /* Pointer to current info struct used in compare function. Save and
10998 * restore the current one for nested calls. */
10999 old_sortinfo = sortinfo;
11000 sortinfo = &info;
11001
11002 if (argvars[0].v_type != VAR_LIST)
11003 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11004 else
11005 {
11006 l = argvars[0].vval.v_list;
11007 if (l == NULL || tv_check_lock(l->lv_lock,
11008 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11009 TRUE))
11010 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011011 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011012
11013 len = list_len(l);
11014 if (len <= 1)
11015 goto theend; /* short list sorts pretty quickly */
11016
11017 info.item_compare_ic = FALSE;
11018 info.item_compare_numeric = FALSE;
11019 info.item_compare_numbers = FALSE;
11020#ifdef FEAT_FLOAT
11021 info.item_compare_float = FALSE;
11022#endif
11023 info.item_compare_func = NULL;
11024 info.item_compare_partial = NULL;
11025 info.item_compare_selfdict = NULL;
11026 if (argvars[1].v_type != VAR_UNKNOWN)
11027 {
11028 /* optional second argument: {func} */
11029 if (argvars[1].v_type == VAR_FUNC)
11030 info.item_compare_func = argvars[1].vval.v_string;
11031 else if (argvars[1].v_type == VAR_PARTIAL)
11032 info.item_compare_partial = argvars[1].vval.v_partial;
11033 else
11034 {
11035 int error = FALSE;
11036
11037 i = (long)get_tv_number_chk(&argvars[1], &error);
11038 if (error)
11039 goto theend; /* type error; errmsg already given */
11040 if (i == 1)
11041 info.item_compare_ic = TRUE;
11042 else if (argvars[1].v_type != VAR_NUMBER)
11043 info.item_compare_func = get_tv_string(&argvars[1]);
11044 else if (i != 0)
11045 {
11046 EMSG(_(e_invarg));
11047 goto theend;
11048 }
11049 if (info.item_compare_func != NULL)
11050 {
11051 if (*info.item_compare_func == NUL)
11052 {
11053 /* empty string means default sort */
11054 info.item_compare_func = NULL;
11055 }
11056 else if (STRCMP(info.item_compare_func, "n") == 0)
11057 {
11058 info.item_compare_func = NULL;
11059 info.item_compare_numeric = TRUE;
11060 }
11061 else if (STRCMP(info.item_compare_func, "N") == 0)
11062 {
11063 info.item_compare_func = NULL;
11064 info.item_compare_numbers = TRUE;
11065 }
11066#ifdef FEAT_FLOAT
11067 else if (STRCMP(info.item_compare_func, "f") == 0)
11068 {
11069 info.item_compare_func = NULL;
11070 info.item_compare_float = TRUE;
11071 }
11072#endif
11073 else if (STRCMP(info.item_compare_func, "i") == 0)
11074 {
11075 info.item_compare_func = NULL;
11076 info.item_compare_ic = TRUE;
11077 }
11078 }
11079 }
11080
11081 if (argvars[2].v_type != VAR_UNKNOWN)
11082 {
11083 /* optional third argument: {dict} */
11084 if (argvars[2].v_type != VAR_DICT)
11085 {
11086 EMSG(_(e_dictreq));
11087 goto theend;
11088 }
11089 info.item_compare_selfdict = argvars[2].vval.v_dict;
11090 }
11091 }
11092
11093 /* Make an array with each entry pointing to an item in the List. */
11094 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11095 if (ptrs == NULL)
11096 goto theend;
11097
11098 i = 0;
11099 if (sort)
11100 {
11101 /* sort(): ptrs will be the list to sort */
11102 for (li = l->lv_first; li != NULL; li = li->li_next)
11103 {
11104 ptrs[i].item = li;
11105 ptrs[i].idx = i;
11106 ++i;
11107 }
11108
11109 info.item_compare_func_err = FALSE;
11110 info.item_compare_keep_zero = FALSE;
11111 /* test the compare function */
11112 if ((info.item_compare_func != NULL
11113 || info.item_compare_partial != NULL)
11114 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11115 == ITEM_COMPARE_FAIL)
11116 EMSG(_("E702: Sort compare function failed"));
11117 else
11118 {
11119 /* Sort the array with item pointers. */
11120 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11121 info.item_compare_func == NULL
11122 && info.item_compare_partial == NULL
11123 ? item_compare : item_compare2);
11124
11125 if (!info.item_compare_func_err)
11126 {
11127 /* Clear the List and append the items in sorted order. */
11128 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11129 l->lv_len = 0;
11130 for (i = 0; i < len; ++i)
11131 list_append(l, ptrs[i].item);
11132 }
11133 }
11134 }
11135 else
11136 {
11137 int (*item_compare_func_ptr)(const void *, const void *);
11138
11139 /* f_uniq(): ptrs will be a stack of items to remove */
11140 info.item_compare_func_err = FALSE;
11141 info.item_compare_keep_zero = TRUE;
11142 item_compare_func_ptr = info.item_compare_func != NULL
11143 || info.item_compare_partial != NULL
11144 ? item_compare2 : item_compare;
11145
11146 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11147 li = li->li_next)
11148 {
11149 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11150 == 0)
11151 ptrs[i++].item = li;
11152 if (info.item_compare_func_err)
11153 {
11154 EMSG(_("E882: Uniq compare function failed"));
11155 break;
11156 }
11157 }
11158
11159 if (!info.item_compare_func_err)
11160 {
11161 while (--i >= 0)
11162 {
11163 li = ptrs[i].item->li_next;
11164 ptrs[i].item->li_next = li->li_next;
11165 if (li->li_next != NULL)
11166 li->li_next->li_prev = ptrs[i].item;
11167 else
11168 l->lv_last = ptrs[i].item;
11169 list_fix_watch(l, li);
11170 listitem_free(li);
11171 l->lv_len--;
11172 }
11173 }
11174 }
11175
11176 vim_free(ptrs);
11177 }
11178theend:
11179 sortinfo = old_sortinfo;
11180}
11181
11182/*
11183 * "sort({list})" function
11184 */
11185 static void
11186f_sort(typval_T *argvars, typval_T *rettv)
11187{
11188 do_sort_uniq(argvars, rettv, TRUE);
11189}
11190
11191/*
11192 * "uniq({list})" function
11193 */
11194 static void
11195f_uniq(typval_T *argvars, typval_T *rettv)
11196{
11197 do_sort_uniq(argvars, rettv, FALSE);
11198}
11199
11200/*
11201 * "soundfold({word})" function
11202 */
11203 static void
11204f_soundfold(typval_T *argvars, typval_T *rettv)
11205{
11206 char_u *s;
11207
11208 rettv->v_type = VAR_STRING;
11209 s = get_tv_string(&argvars[0]);
11210#ifdef FEAT_SPELL
11211 rettv->vval.v_string = eval_soundfold(s);
11212#else
11213 rettv->vval.v_string = vim_strsave(s);
11214#endif
11215}
11216
11217/*
11218 * "spellbadword()" function
11219 */
11220 static void
11221f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11222{
11223 char_u *word = (char_u *)"";
11224 hlf_T attr = HLF_COUNT;
11225 int len = 0;
11226
11227 if (rettv_list_alloc(rettv) == FAIL)
11228 return;
11229
11230#ifdef FEAT_SPELL
11231 if (argvars[0].v_type == VAR_UNKNOWN)
11232 {
11233 /* Find the start and length of the badly spelled word. */
11234 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11235 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011236 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011237 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011238 curwin->w_set_curswant = TRUE;
11239 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011240 }
11241 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11242 {
11243 char_u *str = get_tv_string_chk(&argvars[0]);
11244 int capcol = -1;
11245
11246 if (str != NULL)
11247 {
11248 /* Check the argument for spelling. */
11249 while (*str != NUL)
11250 {
11251 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11252 if (attr != HLF_COUNT)
11253 {
11254 word = str;
11255 break;
11256 }
11257 str += len;
11258 }
11259 }
11260 }
11261#endif
11262
11263 list_append_string(rettv->vval.v_list, word, len);
11264 list_append_string(rettv->vval.v_list, (char_u *)(
11265 attr == HLF_SPB ? "bad" :
11266 attr == HLF_SPR ? "rare" :
11267 attr == HLF_SPL ? "local" :
11268 attr == HLF_SPC ? "caps" :
11269 ""), -1);
11270}
11271
11272/*
11273 * "spellsuggest()" function
11274 */
11275 static void
11276f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11277{
11278#ifdef FEAT_SPELL
11279 char_u *str;
11280 int typeerr = FALSE;
11281 int maxcount;
11282 garray_T ga;
11283 int i;
11284 listitem_T *li;
11285 int need_capital = FALSE;
11286#endif
11287
11288 if (rettv_list_alloc(rettv) == FAIL)
11289 return;
11290
11291#ifdef FEAT_SPELL
11292 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11293 {
11294 str = get_tv_string(&argvars[0]);
11295 if (argvars[1].v_type != VAR_UNKNOWN)
11296 {
11297 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11298 if (maxcount <= 0)
11299 return;
11300 if (argvars[2].v_type != VAR_UNKNOWN)
11301 {
11302 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11303 if (typeerr)
11304 return;
11305 }
11306 }
11307 else
11308 maxcount = 25;
11309
11310 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11311
11312 for (i = 0; i < ga.ga_len; ++i)
11313 {
11314 str = ((char_u **)ga.ga_data)[i];
11315
11316 li = listitem_alloc();
11317 if (li == NULL)
11318 vim_free(str);
11319 else
11320 {
11321 li->li_tv.v_type = VAR_STRING;
11322 li->li_tv.v_lock = 0;
11323 li->li_tv.vval.v_string = str;
11324 list_append(rettv->vval.v_list, li);
11325 }
11326 }
11327 ga_clear(&ga);
11328 }
11329#endif
11330}
11331
11332 static void
11333f_split(typval_T *argvars, typval_T *rettv)
11334{
11335 char_u *str;
11336 char_u *end;
11337 char_u *pat = NULL;
11338 regmatch_T regmatch;
11339 char_u patbuf[NUMBUFLEN];
11340 char_u *save_cpo;
11341 int match;
11342 colnr_T col = 0;
11343 int keepempty = FALSE;
11344 int typeerr = FALSE;
11345
11346 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11347 save_cpo = p_cpo;
11348 p_cpo = (char_u *)"";
11349
11350 str = get_tv_string(&argvars[0]);
11351 if (argvars[1].v_type != VAR_UNKNOWN)
11352 {
11353 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11354 if (pat == NULL)
11355 typeerr = TRUE;
11356 if (argvars[2].v_type != VAR_UNKNOWN)
11357 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11358 }
11359 if (pat == NULL || *pat == NUL)
11360 pat = (char_u *)"[\\x01- ]\\+";
11361
11362 if (rettv_list_alloc(rettv) == FAIL)
11363 return;
11364 if (typeerr)
11365 return;
11366
11367 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11368 if (regmatch.regprog != NULL)
11369 {
11370 regmatch.rm_ic = FALSE;
11371 while (*str != NUL || keepempty)
11372 {
11373 if (*str == NUL)
11374 match = FALSE; /* empty item at the end */
11375 else
11376 match = vim_regexec_nl(&regmatch, str, col);
11377 if (match)
11378 end = regmatch.startp[0];
11379 else
11380 end = str + STRLEN(str);
11381 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11382 && *str != NUL && match && end < regmatch.endp[0]))
11383 {
11384 if (list_append_string(rettv->vval.v_list, str,
11385 (int)(end - str)) == FAIL)
11386 break;
11387 }
11388 if (!match)
11389 break;
11390 /* Advance to just after the match. */
11391 if (regmatch.endp[0] > str)
11392 col = 0;
11393 else
11394 {
11395 /* Don't get stuck at the same match. */
11396#ifdef FEAT_MBYTE
11397 col = (*mb_ptr2len)(regmatch.endp[0]);
11398#else
11399 col = 1;
11400#endif
11401 }
11402 str = regmatch.endp[0];
11403 }
11404
11405 vim_regfree(regmatch.regprog);
11406 }
11407
11408 p_cpo = save_cpo;
11409}
11410
11411#ifdef FEAT_FLOAT
11412/*
11413 * "sqrt()" function
11414 */
11415 static void
11416f_sqrt(typval_T *argvars, typval_T *rettv)
11417{
11418 float_T f = 0.0;
11419
11420 rettv->v_type = VAR_FLOAT;
11421 if (get_float_arg(argvars, &f) == OK)
11422 rettv->vval.v_float = sqrt(f);
11423 else
11424 rettv->vval.v_float = 0.0;
11425}
11426
11427/*
11428 * "str2float()" function
11429 */
11430 static void
11431f_str2float(typval_T *argvars, typval_T *rettv)
11432{
11433 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011434 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011435
Bram Moolenaar08243d22017-01-10 16:12:29 +010011436 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011437 p = skipwhite(p + 1);
11438 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011439 if (isneg)
11440 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011441 rettv->v_type = VAR_FLOAT;
11442}
11443#endif
11444
11445/*
11446 * "str2nr()" function
11447 */
11448 static void
11449f_str2nr(typval_T *argvars, typval_T *rettv)
11450{
11451 int base = 10;
11452 char_u *p;
11453 varnumber_T n;
11454 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011455 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011456
11457 if (argvars[1].v_type != VAR_UNKNOWN)
11458 {
11459 base = (int)get_tv_number(&argvars[1]);
11460 if (base != 2 && base != 8 && base != 10 && base != 16)
11461 {
11462 EMSG(_(e_invarg));
11463 return;
11464 }
11465 }
11466
11467 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011468 isneg = (*p == '-');
11469 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011470 p = skipwhite(p + 1);
11471 switch (base)
11472 {
11473 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11474 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11475 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11476 default: what = 0;
11477 }
11478 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011479 if (isneg)
11480 rettv->vval.v_number = -n;
11481 else
11482 rettv->vval.v_number = n;
11483
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011484}
11485
11486#ifdef HAVE_STRFTIME
11487/*
11488 * "strftime({format}[, {time}])" function
11489 */
11490 static void
11491f_strftime(typval_T *argvars, typval_T *rettv)
11492{
11493 char_u result_buf[256];
11494 struct tm *curtime;
11495 time_t seconds;
11496 char_u *p;
11497
11498 rettv->v_type = VAR_STRING;
11499
11500 p = get_tv_string(&argvars[0]);
11501 if (argvars[1].v_type == VAR_UNKNOWN)
11502 seconds = time(NULL);
11503 else
11504 seconds = (time_t)get_tv_number(&argvars[1]);
11505 curtime = localtime(&seconds);
11506 /* MSVC returns NULL for an invalid value of seconds. */
11507 if (curtime == NULL)
11508 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11509 else
11510 {
11511# ifdef FEAT_MBYTE
11512 vimconv_T conv;
11513 char_u *enc;
11514
11515 conv.vc_type = CONV_NONE;
11516 enc = enc_locale();
11517 convert_setup(&conv, p_enc, enc);
11518 if (conv.vc_type != CONV_NONE)
11519 p = string_convert(&conv, p, NULL);
11520# endif
11521 if (p != NULL)
11522 (void)strftime((char *)result_buf, sizeof(result_buf),
11523 (char *)p, curtime);
11524 else
11525 result_buf[0] = NUL;
11526
11527# ifdef FEAT_MBYTE
11528 if (conv.vc_type != CONV_NONE)
11529 vim_free(p);
11530 convert_setup(&conv, enc, p_enc);
11531 if (conv.vc_type != CONV_NONE)
11532 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11533 else
11534# endif
11535 rettv->vval.v_string = vim_strsave(result_buf);
11536
11537# ifdef FEAT_MBYTE
11538 /* Release conversion descriptors */
11539 convert_setup(&conv, NULL, NULL);
11540 vim_free(enc);
11541# endif
11542 }
11543}
11544#endif
11545
11546/*
11547 * "strgetchar()" function
11548 */
11549 static void
11550f_strgetchar(typval_T *argvars, typval_T *rettv)
11551{
11552 char_u *str;
11553 int len;
11554 int error = FALSE;
11555 int charidx;
11556
11557 rettv->vval.v_number = -1;
11558 str = get_tv_string_chk(&argvars[0]);
11559 if (str == NULL)
11560 return;
11561 len = (int)STRLEN(str);
11562 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11563 if (error)
11564 return;
11565#ifdef FEAT_MBYTE
11566 {
11567 int byteidx = 0;
11568
11569 while (charidx >= 0 && byteidx < len)
11570 {
11571 if (charidx == 0)
11572 {
11573 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11574 break;
11575 }
11576 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011577 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011578 }
11579 }
11580#else
11581 if (charidx < len)
11582 rettv->vval.v_number = str[charidx];
11583#endif
11584}
11585
11586/*
11587 * "stridx()" function
11588 */
11589 static void
11590f_stridx(typval_T *argvars, typval_T *rettv)
11591{
11592 char_u buf[NUMBUFLEN];
11593 char_u *needle;
11594 char_u *haystack;
11595 char_u *save_haystack;
11596 char_u *pos;
11597 int start_idx;
11598
11599 needle = get_tv_string_chk(&argvars[1]);
11600 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11601 rettv->vval.v_number = -1;
11602 if (needle == NULL || haystack == NULL)
11603 return; /* type error; errmsg already given */
11604
11605 if (argvars[2].v_type != VAR_UNKNOWN)
11606 {
11607 int error = FALSE;
11608
11609 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11610 if (error || start_idx >= (int)STRLEN(haystack))
11611 return;
11612 if (start_idx >= 0)
11613 haystack += start_idx;
11614 }
11615
11616 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11617 if (pos != NULL)
11618 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11619}
11620
11621/*
11622 * "string()" function
11623 */
11624 static void
11625f_string(typval_T *argvars, typval_T *rettv)
11626{
11627 char_u *tofree;
11628 char_u numbuf[NUMBUFLEN];
11629
11630 rettv->v_type = VAR_STRING;
11631 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11632 get_copyID());
11633 /* Make a copy if we have a value but it's not in allocated memory. */
11634 if (rettv->vval.v_string != NULL && tofree == NULL)
11635 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11636}
11637
11638/*
11639 * "strlen()" function
11640 */
11641 static void
11642f_strlen(typval_T *argvars, typval_T *rettv)
11643{
11644 rettv->vval.v_number = (varnumber_T)(STRLEN(
11645 get_tv_string(&argvars[0])));
11646}
11647
11648/*
11649 * "strchars()" function
11650 */
11651 static void
11652f_strchars(typval_T *argvars, typval_T *rettv)
11653{
11654 char_u *s = get_tv_string(&argvars[0]);
11655 int skipcc = 0;
11656#ifdef FEAT_MBYTE
11657 varnumber_T len = 0;
11658 int (*func_mb_ptr2char_adv)(char_u **pp);
11659#endif
11660
11661 if (argvars[1].v_type != VAR_UNKNOWN)
11662 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11663 if (skipcc < 0 || skipcc > 1)
11664 EMSG(_(e_invarg));
11665 else
11666 {
11667#ifdef FEAT_MBYTE
11668 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11669 while (*s != NUL)
11670 {
11671 func_mb_ptr2char_adv(&s);
11672 ++len;
11673 }
11674 rettv->vval.v_number = len;
11675#else
11676 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11677#endif
11678 }
11679}
11680
11681/*
11682 * "strdisplaywidth()" function
11683 */
11684 static void
11685f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11686{
11687 char_u *s = get_tv_string(&argvars[0]);
11688 int col = 0;
11689
11690 if (argvars[1].v_type != VAR_UNKNOWN)
11691 col = (int)get_tv_number(&argvars[1]);
11692
11693 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11694}
11695
11696/*
11697 * "strwidth()" function
11698 */
11699 static void
11700f_strwidth(typval_T *argvars, typval_T *rettv)
11701{
11702 char_u *s = get_tv_string(&argvars[0]);
11703
11704 rettv->vval.v_number = (varnumber_T)(
11705#ifdef FEAT_MBYTE
11706 mb_string2cells(s, -1)
11707#else
11708 STRLEN(s)
11709#endif
11710 );
11711}
11712
11713/*
11714 * "strcharpart()" function
11715 */
11716 static void
11717f_strcharpart(typval_T *argvars, typval_T *rettv)
11718{
11719#ifdef FEAT_MBYTE
11720 char_u *p;
11721 int nchar;
11722 int nbyte = 0;
11723 int charlen;
11724 int len = 0;
11725 int slen;
11726 int error = FALSE;
11727
11728 p = get_tv_string(&argvars[0]);
11729 slen = (int)STRLEN(p);
11730
11731 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11732 if (!error)
11733 {
11734 if (nchar > 0)
11735 while (nchar > 0 && nbyte < slen)
11736 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011737 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011738 --nchar;
11739 }
11740 else
11741 nbyte = nchar;
11742 if (argvars[2].v_type != VAR_UNKNOWN)
11743 {
11744 charlen = (int)get_tv_number(&argvars[2]);
11745 while (charlen > 0 && nbyte + len < slen)
11746 {
11747 int off = nbyte + len;
11748
11749 if (off < 0)
11750 len += 1;
11751 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011752 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011753 --charlen;
11754 }
11755 }
11756 else
11757 len = slen - nbyte; /* default: all bytes that are available. */
11758 }
11759
11760 /*
11761 * Only return the overlap between the specified part and the actual
11762 * string.
11763 */
11764 if (nbyte < 0)
11765 {
11766 len += nbyte;
11767 nbyte = 0;
11768 }
11769 else if (nbyte > slen)
11770 nbyte = slen;
11771 if (len < 0)
11772 len = 0;
11773 else if (nbyte + len > slen)
11774 len = slen - nbyte;
11775
11776 rettv->v_type = VAR_STRING;
11777 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11778#else
11779 f_strpart(argvars, rettv);
11780#endif
11781}
11782
11783/*
11784 * "strpart()" function
11785 */
11786 static void
11787f_strpart(typval_T *argvars, typval_T *rettv)
11788{
11789 char_u *p;
11790 int n;
11791 int len;
11792 int slen;
11793 int error = FALSE;
11794
11795 p = get_tv_string(&argvars[0]);
11796 slen = (int)STRLEN(p);
11797
11798 n = (int)get_tv_number_chk(&argvars[1], &error);
11799 if (error)
11800 len = 0;
11801 else if (argvars[2].v_type != VAR_UNKNOWN)
11802 len = (int)get_tv_number(&argvars[2]);
11803 else
11804 len = slen - n; /* default len: all bytes that are available. */
11805
11806 /*
11807 * Only return the overlap between the specified part and the actual
11808 * string.
11809 */
11810 if (n < 0)
11811 {
11812 len += n;
11813 n = 0;
11814 }
11815 else if (n > slen)
11816 n = slen;
11817 if (len < 0)
11818 len = 0;
11819 else if (n + len > slen)
11820 len = slen - n;
11821
11822 rettv->v_type = VAR_STRING;
11823 rettv->vval.v_string = vim_strnsave(p + n, len);
11824}
11825
11826/*
11827 * "strridx()" function
11828 */
11829 static void
11830f_strridx(typval_T *argvars, typval_T *rettv)
11831{
11832 char_u buf[NUMBUFLEN];
11833 char_u *needle;
11834 char_u *haystack;
11835 char_u *rest;
11836 char_u *lastmatch = NULL;
11837 int haystack_len, end_idx;
11838
11839 needle = get_tv_string_chk(&argvars[1]);
11840 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11841
11842 rettv->vval.v_number = -1;
11843 if (needle == NULL || haystack == NULL)
11844 return; /* type error; errmsg already given */
11845
11846 haystack_len = (int)STRLEN(haystack);
11847 if (argvars[2].v_type != VAR_UNKNOWN)
11848 {
11849 /* Third argument: upper limit for index */
11850 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11851 if (end_idx < 0)
11852 return; /* can never find a match */
11853 }
11854 else
11855 end_idx = haystack_len;
11856
11857 if (*needle == NUL)
11858 {
11859 /* Empty string matches past the end. */
11860 lastmatch = haystack + end_idx;
11861 }
11862 else
11863 {
11864 for (rest = haystack; *rest != '\0'; ++rest)
11865 {
11866 rest = (char_u *)strstr((char *)rest, (char *)needle);
11867 if (rest == NULL || rest > haystack + end_idx)
11868 break;
11869 lastmatch = rest;
11870 }
11871 }
11872
11873 if (lastmatch == NULL)
11874 rettv->vval.v_number = -1;
11875 else
11876 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11877}
11878
11879/*
11880 * "strtrans()" function
11881 */
11882 static void
11883f_strtrans(typval_T *argvars, typval_T *rettv)
11884{
11885 rettv->v_type = VAR_STRING;
11886 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11887}
11888
11889/*
11890 * "submatch()" function
11891 */
11892 static void
11893f_submatch(typval_T *argvars, typval_T *rettv)
11894{
11895 int error = FALSE;
11896 int no;
11897 int retList = 0;
11898
11899 no = (int)get_tv_number_chk(&argvars[0], &error);
11900 if (error)
11901 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011902 if (no < 0 || no >= NSUBEXP)
11903 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010011904 EMSGN(_("E935: invalid submatch number: %d"), no);
11905 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011906 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011907 if (argvars[1].v_type != VAR_UNKNOWN)
11908 retList = (int)get_tv_number_chk(&argvars[1], &error);
11909 if (error)
11910 return;
11911
11912 if (retList == 0)
11913 {
11914 rettv->v_type = VAR_STRING;
11915 rettv->vval.v_string = reg_submatch(no);
11916 }
11917 else
11918 {
11919 rettv->v_type = VAR_LIST;
11920 rettv->vval.v_list = reg_submatch_list(no);
11921 }
11922}
11923
11924/*
11925 * "substitute()" function
11926 */
11927 static void
11928f_substitute(typval_T *argvars, typval_T *rettv)
11929{
11930 char_u patbuf[NUMBUFLEN];
11931 char_u subbuf[NUMBUFLEN];
11932 char_u flagsbuf[NUMBUFLEN];
11933
11934 char_u *str = get_tv_string_chk(&argvars[0]);
11935 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011936 char_u *sub = NULL;
11937 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011938 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11939
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011940 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11941 expr = &argvars[2];
11942 else
11943 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11944
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011945 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011946 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11947 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011948 rettv->vval.v_string = NULL;
11949 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011950 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011951}
11952
11953/*
11954 * "synID(lnum, col, trans)" function
11955 */
11956 static void
11957f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11958{
11959 int id = 0;
11960#ifdef FEAT_SYN_HL
11961 linenr_T lnum;
11962 colnr_T col;
11963 int trans;
11964 int transerr = FALSE;
11965
11966 lnum = get_tv_lnum(argvars); /* -1 on type error */
11967 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11968 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
11969
11970 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11971 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11972 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11973#endif
11974
11975 rettv->vval.v_number = id;
11976}
11977
11978/*
11979 * "synIDattr(id, what [, mode])" function
11980 */
11981 static void
11982f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11983{
11984 char_u *p = NULL;
11985#ifdef FEAT_SYN_HL
11986 int id;
11987 char_u *what;
11988 char_u *mode;
11989 char_u modebuf[NUMBUFLEN];
11990 int modec;
11991
11992 id = (int)get_tv_number(&argvars[0]);
11993 what = get_tv_string(&argvars[1]);
11994 if (argvars[2].v_type != VAR_UNKNOWN)
11995 {
11996 mode = get_tv_string_buf(&argvars[2], modebuf);
11997 modec = TOLOWER_ASC(mode[0]);
11998 if (modec != 't' && modec != 'c' && modec != 'g')
11999 modec = 0; /* replace invalid with current */
12000 }
12001 else
12002 {
12003#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12004 if (USE_24BIT)
12005 modec = 'g';
12006 else
12007#endif
12008 if (t_colors > 1)
12009 modec = 'c';
12010 else
12011 modec = 't';
12012 }
12013
12014
12015 switch (TOLOWER_ASC(what[0]))
12016 {
12017 case 'b':
12018 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12019 p = highlight_color(id, what, modec);
12020 else /* bold */
12021 p = highlight_has_attr(id, HL_BOLD, modec);
12022 break;
12023
12024 case 'f': /* fg[#] or font */
12025 p = highlight_color(id, what, modec);
12026 break;
12027
12028 case 'i':
12029 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12030 p = highlight_has_attr(id, HL_INVERSE, modec);
12031 else /* italic */
12032 p = highlight_has_attr(id, HL_ITALIC, modec);
12033 break;
12034
12035 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012036 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012037 break;
12038
12039 case 'r': /* reverse */
12040 p = highlight_has_attr(id, HL_INVERSE, modec);
12041 break;
12042
12043 case 's':
12044 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12045 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012046 /* strikeout */
12047 else if (TOLOWER_ASC(what[1]) == 't' &&
12048 TOLOWER_ASC(what[2]) == 'r')
12049 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012050 else /* standout */
12051 p = highlight_has_attr(id, HL_STANDOUT, modec);
12052 break;
12053
12054 case 'u':
12055 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12056 /* underline */
12057 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12058 else
12059 /* undercurl */
12060 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12061 break;
12062 }
12063
12064 if (p != NULL)
12065 p = vim_strsave(p);
12066#endif
12067 rettv->v_type = VAR_STRING;
12068 rettv->vval.v_string = p;
12069}
12070
12071/*
12072 * "synIDtrans(id)" function
12073 */
12074 static void
12075f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12076{
12077 int id;
12078
12079#ifdef FEAT_SYN_HL
12080 id = (int)get_tv_number(&argvars[0]);
12081
12082 if (id > 0)
12083 id = syn_get_final_id(id);
12084 else
12085#endif
12086 id = 0;
12087
12088 rettv->vval.v_number = id;
12089}
12090
12091/*
12092 * "synconcealed(lnum, col)" function
12093 */
12094 static void
12095f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12096{
12097#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12098 linenr_T lnum;
12099 colnr_T col;
12100 int syntax_flags = 0;
12101 int cchar;
12102 int matchid = 0;
12103 char_u str[NUMBUFLEN];
12104#endif
12105
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012106 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012107
12108#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12109 lnum = get_tv_lnum(argvars); /* -1 on type error */
12110 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12111
12112 vim_memset(str, NUL, sizeof(str));
12113
12114 if (rettv_list_alloc(rettv) != FAIL)
12115 {
12116 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12117 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12118 && curwin->w_p_cole > 0)
12119 {
12120 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12121 syntax_flags = get_syntax_info(&matchid);
12122
12123 /* get the conceal character */
12124 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12125 {
12126 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012127 if (cchar == NUL && curwin->w_p_cole == 1)
12128 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012129 if (cchar != NUL)
12130 {
12131# ifdef FEAT_MBYTE
12132 if (has_mbyte)
12133 (*mb_char2bytes)(cchar, str);
12134 else
12135# endif
12136 str[0] = cchar;
12137 }
12138 }
12139 }
12140
12141 list_append_number(rettv->vval.v_list,
12142 (syntax_flags & HL_CONCEAL) != 0);
12143 /* -1 to auto-determine strlen */
12144 list_append_string(rettv->vval.v_list, str, -1);
12145 list_append_number(rettv->vval.v_list, matchid);
12146 }
12147#endif
12148}
12149
12150/*
12151 * "synstack(lnum, col)" function
12152 */
12153 static void
12154f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12155{
12156#ifdef FEAT_SYN_HL
12157 linenr_T lnum;
12158 colnr_T col;
12159 int i;
12160 int id;
12161#endif
12162
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012163 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012164
12165#ifdef FEAT_SYN_HL
12166 lnum = get_tv_lnum(argvars); /* -1 on type error */
12167 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12168
12169 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12170 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12171 && rettv_list_alloc(rettv) != FAIL)
12172 {
12173 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12174 for (i = 0; ; ++i)
12175 {
12176 id = syn_get_stack_item(i);
12177 if (id < 0)
12178 break;
12179 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12180 break;
12181 }
12182 }
12183#endif
12184}
12185
12186 static void
12187get_cmd_output_as_rettv(
12188 typval_T *argvars,
12189 typval_T *rettv,
12190 int retlist)
12191{
12192 char_u *res = NULL;
12193 char_u *p;
12194 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012195 int err = FALSE;
12196 FILE *fd;
12197 list_T *list = NULL;
12198 int flags = SHELL_SILENT;
12199
12200 rettv->v_type = VAR_STRING;
12201 rettv->vval.v_string = NULL;
12202 if (check_restricted() || check_secure())
12203 goto errret;
12204
12205 if (argvars[1].v_type != VAR_UNKNOWN)
12206 {
12207 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012208 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012209 * command.
12210 */
12211 if ((infile = vim_tempname('i', TRUE)) == NULL)
12212 {
12213 EMSG(_(e_notmp));
12214 goto errret;
12215 }
12216
12217 fd = mch_fopen((char *)infile, WRITEBIN);
12218 if (fd == NULL)
12219 {
12220 EMSG2(_(e_notopen), infile);
12221 goto errret;
12222 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012223 if (argvars[1].v_type == VAR_NUMBER)
12224 {
12225 linenr_T lnum;
12226 buf_T *buf;
12227
12228 buf = buflist_findnr(argvars[1].vval.v_number);
12229 if (buf == NULL)
12230 {
12231 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012232 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012233 goto errret;
12234 }
12235
12236 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12237 {
12238 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12239 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12240 {
12241 err = TRUE;
12242 break;
12243 }
12244 if (putc(NL, fd) == EOF)
12245 {
12246 err = TRUE;
12247 break;
12248 }
12249 }
12250 }
12251 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012252 {
12253 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12254 err = TRUE;
12255 }
12256 else
12257 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012258 size_t len;
12259 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012260
12261 p = get_tv_string_buf_chk(&argvars[1], buf);
12262 if (p == NULL)
12263 {
12264 fclose(fd);
12265 goto errret; /* type error; errmsg already given */
12266 }
12267 len = STRLEN(p);
12268 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12269 err = TRUE;
12270 }
12271 if (fclose(fd) != 0)
12272 err = TRUE;
12273 if (err)
12274 {
12275 EMSG(_("E677: Error writing temp file"));
12276 goto errret;
12277 }
12278 }
12279
12280 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12281 * echoes typeahead, that messes up the display. */
12282 if (!msg_silent)
12283 flags += SHELL_COOKED;
12284
12285 if (retlist)
12286 {
12287 int len;
12288 listitem_T *li;
12289 char_u *s = NULL;
12290 char_u *start;
12291 char_u *end;
12292 int i;
12293
12294 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12295 if (res == NULL)
12296 goto errret;
12297
12298 list = list_alloc();
12299 if (list == NULL)
12300 goto errret;
12301
12302 for (i = 0; i < len; ++i)
12303 {
12304 start = res + i;
12305 while (i < len && res[i] != NL)
12306 ++i;
12307 end = res + i;
12308
12309 s = alloc((unsigned)(end - start + 1));
12310 if (s == NULL)
12311 goto errret;
12312
12313 for (p = s; start < end; ++p, ++start)
12314 *p = *start == NUL ? NL : *start;
12315 *p = NUL;
12316
12317 li = listitem_alloc();
12318 if (li == NULL)
12319 {
12320 vim_free(s);
12321 goto errret;
12322 }
12323 li->li_tv.v_type = VAR_STRING;
12324 li->li_tv.v_lock = 0;
12325 li->li_tv.vval.v_string = s;
12326 list_append(list, li);
12327 }
12328
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012329 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012330 list = NULL;
12331 }
12332 else
12333 {
12334 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12335#ifdef USE_CR
12336 /* translate <CR> into <NL> */
12337 if (res != NULL)
12338 {
12339 char_u *s;
12340
12341 for (s = res; *s; ++s)
12342 {
12343 if (*s == CAR)
12344 *s = NL;
12345 }
12346 }
12347#else
12348# ifdef USE_CRNL
12349 /* translate <CR><NL> into <NL> */
12350 if (res != NULL)
12351 {
12352 char_u *s, *d;
12353
12354 d = res;
12355 for (s = res; *s; ++s)
12356 {
12357 if (s[0] == CAR && s[1] == NL)
12358 ++s;
12359 *d++ = *s;
12360 }
12361 *d = NUL;
12362 }
12363# endif
12364#endif
12365 rettv->vval.v_string = res;
12366 res = NULL;
12367 }
12368
12369errret:
12370 if (infile != NULL)
12371 {
12372 mch_remove(infile);
12373 vim_free(infile);
12374 }
12375 if (res != NULL)
12376 vim_free(res);
12377 if (list != NULL)
12378 list_free(list);
12379}
12380
12381/*
12382 * "system()" function
12383 */
12384 static void
12385f_system(typval_T *argvars, typval_T *rettv)
12386{
12387 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12388}
12389
12390/*
12391 * "systemlist()" function
12392 */
12393 static void
12394f_systemlist(typval_T *argvars, typval_T *rettv)
12395{
12396 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12397}
12398
12399/*
12400 * "tabpagebuflist()" function
12401 */
12402 static void
12403f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12404{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012405 tabpage_T *tp;
12406 win_T *wp = NULL;
12407
12408 if (argvars[0].v_type == VAR_UNKNOWN)
12409 wp = firstwin;
12410 else
12411 {
12412 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12413 if (tp != NULL)
12414 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12415 }
12416 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12417 {
12418 for (; wp != NULL; wp = wp->w_next)
12419 if (list_append_number(rettv->vval.v_list,
12420 wp->w_buffer->b_fnum) == FAIL)
12421 break;
12422 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012423}
12424
12425
12426/*
12427 * "tabpagenr()" function
12428 */
12429 static void
12430f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12431{
12432 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012433 char_u *arg;
12434
12435 if (argvars[0].v_type != VAR_UNKNOWN)
12436 {
12437 arg = get_tv_string_chk(&argvars[0]);
12438 nr = 0;
12439 if (arg != NULL)
12440 {
12441 if (STRCMP(arg, "$") == 0)
12442 nr = tabpage_index(NULL) - 1;
12443 else
12444 EMSG2(_(e_invexpr2), arg);
12445 }
12446 }
12447 else
12448 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012449 rettv->vval.v_number = nr;
12450}
12451
12452
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012453static int get_winnr(tabpage_T *tp, typval_T *argvar);
12454
12455/*
12456 * Common code for tabpagewinnr() and winnr().
12457 */
12458 static int
12459get_winnr(tabpage_T *tp, typval_T *argvar)
12460{
12461 win_T *twin;
12462 int nr = 1;
12463 win_T *wp;
12464 char_u *arg;
12465
12466 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12467 if (argvar->v_type != VAR_UNKNOWN)
12468 {
12469 arg = get_tv_string_chk(argvar);
12470 if (arg == NULL)
12471 nr = 0; /* type error; errmsg already given */
12472 else if (STRCMP(arg, "$") == 0)
12473 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12474 else if (STRCMP(arg, "#") == 0)
12475 {
12476 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12477 if (twin == NULL)
12478 nr = 0;
12479 }
12480 else
12481 {
12482 EMSG2(_(e_invexpr2), arg);
12483 nr = 0;
12484 }
12485 }
12486
12487 if (nr > 0)
12488 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12489 wp != twin; wp = wp->w_next)
12490 {
12491 if (wp == NULL)
12492 {
12493 /* didn't find it in this tabpage */
12494 nr = 0;
12495 break;
12496 }
12497 ++nr;
12498 }
12499 return nr;
12500}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012501
12502/*
12503 * "tabpagewinnr()" function
12504 */
12505 static void
12506f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12507{
12508 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012509 tabpage_T *tp;
12510
12511 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12512 if (tp == NULL)
12513 nr = 0;
12514 else
12515 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012516 rettv->vval.v_number = nr;
12517}
12518
12519
12520/*
12521 * "tagfiles()" function
12522 */
12523 static void
12524f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12525{
12526 char_u *fname;
12527 tagname_T tn;
12528 int first;
12529
12530 if (rettv_list_alloc(rettv) == FAIL)
12531 return;
12532 fname = alloc(MAXPATHL);
12533 if (fname == NULL)
12534 return;
12535
12536 for (first = TRUE; ; first = FALSE)
12537 if (get_tagfname(&tn, first, fname) == FAIL
12538 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12539 break;
12540 tagname_free(&tn);
12541 vim_free(fname);
12542}
12543
12544/*
12545 * "taglist()" function
12546 */
12547 static void
12548f_taglist(typval_T *argvars, typval_T *rettv)
12549{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012550 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012551 char_u *tag_pattern;
12552
12553 tag_pattern = get_tv_string(&argvars[0]);
12554
12555 rettv->vval.v_number = FALSE;
12556 if (*tag_pattern == NUL)
12557 return;
12558
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012559 if (argvars[1].v_type != VAR_UNKNOWN)
12560 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012561 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012562 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012563}
12564
12565/*
12566 * "tempname()" function
12567 */
12568 static void
12569f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12570{
12571 static int x = 'A';
12572
12573 rettv->v_type = VAR_STRING;
12574 rettv->vval.v_string = vim_tempname(x, FALSE);
12575
12576 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12577 * names. Skip 'I' and 'O', they are used for shell redirection. */
12578 do
12579 {
12580 if (x == 'Z')
12581 x = '0';
12582 else if (x == '9')
12583 x = 'A';
12584 else
12585 {
12586#ifdef EBCDIC
12587 if (x == 'I')
12588 x = 'J';
12589 else if (x == 'R')
12590 x = 'S';
12591 else
12592#endif
12593 ++x;
12594 }
12595 } while (x == 'I' || x == 'O');
12596}
12597
12598#ifdef FEAT_FLOAT
12599/*
12600 * "tan()" function
12601 */
12602 static void
12603f_tan(typval_T *argvars, typval_T *rettv)
12604{
12605 float_T f = 0.0;
12606
12607 rettv->v_type = VAR_FLOAT;
12608 if (get_float_arg(argvars, &f) == OK)
12609 rettv->vval.v_float = tan(f);
12610 else
12611 rettv->vval.v_float = 0.0;
12612}
12613
12614/*
12615 * "tanh()" function
12616 */
12617 static void
12618f_tanh(typval_T *argvars, typval_T *rettv)
12619{
12620 float_T f = 0.0;
12621
12622 rettv->v_type = VAR_FLOAT;
12623 if (get_float_arg(argvars, &f) == OK)
12624 rettv->vval.v_float = tanh(f);
12625 else
12626 rettv->vval.v_float = 0.0;
12627}
12628#endif
12629
12630/*
12631 * "test_alloc_fail(id, countdown, repeat)" function
12632 */
12633 static void
12634f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12635{
12636 if (argvars[0].v_type != VAR_NUMBER
12637 || argvars[0].vval.v_number <= 0
12638 || argvars[1].v_type != VAR_NUMBER
12639 || argvars[1].vval.v_number < 0
12640 || argvars[2].v_type != VAR_NUMBER)
12641 EMSG(_(e_invarg));
12642 else
12643 {
12644 alloc_fail_id = argvars[0].vval.v_number;
12645 if (alloc_fail_id >= aid_last)
12646 EMSG(_(e_invarg));
12647 alloc_fail_countdown = argvars[1].vval.v_number;
12648 alloc_fail_repeat = argvars[2].vval.v_number;
12649 did_outofmem_msg = FALSE;
12650 }
12651}
12652
12653/*
12654 * "test_autochdir()"
12655 */
12656 static void
12657f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12658{
12659#if defined(FEAT_AUTOCHDIR)
12660 test_autochdir = TRUE;
12661#endif
12662}
12663
12664/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012665 * "test_feedinput()"
12666 */
12667 static void
12668f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12669{
12670#ifdef USE_INPUT_BUF
12671 char_u *val = get_tv_string_chk(&argvars[0]);
12672
12673 if (val != NULL)
12674 {
12675 trash_input_buf();
12676 add_to_input_buf_csi(val, (int)STRLEN(val));
12677 }
12678#endif
12679}
12680
12681/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012682 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012683 */
12684 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012685f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012686{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012687 char_u *name = (char_u *)"";
12688 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012689 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012690
12691 if (argvars[0].v_type != VAR_STRING
12692 || (argvars[1].v_type) != VAR_NUMBER)
12693 EMSG(_(e_invarg));
12694 else
12695 {
12696 name = get_tv_string_chk(&argvars[0]);
12697 val = (int)get_tv_number(&argvars[1]);
12698
12699 if (STRCMP(name, (char_u *)"redraw") == 0)
12700 disable_redraw_for_testing = val;
12701 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12702 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012703 else if (STRCMP(name, (char_u *)"starting") == 0)
12704 {
12705 if (val)
12706 {
12707 if (save_starting < 0)
12708 save_starting = starting;
12709 starting = 0;
12710 }
12711 else
12712 {
12713 starting = save_starting;
12714 save_starting = -1;
12715 }
12716 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012717 else if (STRCMP(name, (char_u *)"ALL") == 0)
12718 {
12719 disable_char_avail_for_testing = FALSE;
12720 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012721 if (save_starting >= 0)
12722 {
12723 starting = save_starting;
12724 save_starting = -1;
12725 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012726 }
12727 else
12728 EMSG2(_(e_invarg2), name);
12729 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012730}
12731
12732/*
12733 * "test_garbagecollect_now()" function
12734 */
12735 static void
12736f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12737{
12738 /* This is dangerous, any Lists and Dicts used internally may be freed
12739 * while still in use. */
12740 garbage_collect(TRUE);
12741}
12742
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012743/*
12744 * "test_ignore_error()" function
12745 */
12746 static void
12747f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12748{
12749 ignore_error_for_testing(get_tv_string(&argvars[0]));
12750}
12751
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012752#ifdef FEAT_JOB_CHANNEL
12753 static void
12754f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12755{
12756 rettv->v_type = VAR_CHANNEL;
12757 rettv->vval.v_channel = NULL;
12758}
12759#endif
12760
12761 static void
12762f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12763{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012764 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012765}
12766
12767#ifdef FEAT_JOB_CHANNEL
12768 static void
12769f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12770{
12771 rettv->v_type = VAR_JOB;
12772 rettv->vval.v_job = NULL;
12773}
12774#endif
12775
12776 static void
12777f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12778{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012779 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012780}
12781
12782 static void
12783f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12784{
12785 rettv->v_type = VAR_PARTIAL;
12786 rettv->vval.v_partial = NULL;
12787}
12788
12789 static void
12790f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12791{
12792 rettv->v_type = VAR_STRING;
12793 rettv->vval.v_string = NULL;
12794}
12795
12796 static void
12797f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12798{
12799 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12800}
12801
12802#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12803/*
12804 * Get a callback from "arg". It can be a Funcref or a function name.
12805 * When "arg" is zero return an empty string.
12806 * Return NULL for an invalid argument.
12807 */
12808 char_u *
12809get_callback(typval_T *arg, partial_T **pp)
12810{
12811 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12812 {
12813 *pp = arg->vval.v_partial;
12814 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012815 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012816 }
12817 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012818 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012819 {
12820 func_ref(arg->vval.v_string);
12821 return arg->vval.v_string;
12822 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012823 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12824 return (char_u *)"";
12825 EMSG(_("E921: Invalid callback argument"));
12826 return NULL;
12827}
12828
12829/*
12830 * Unref/free "callback" and "partial" retured by get_callback().
12831 */
12832 void
12833free_callback(char_u *callback, partial_T *partial)
12834{
12835 if (partial != NULL)
12836 partial_unref(partial);
12837 else if (callback != NULL)
12838 {
12839 func_unref(callback);
12840 vim_free(callback);
12841 }
12842}
12843#endif
12844
12845#ifdef FEAT_TIMERS
12846/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012847 * "timer_info([timer])" function
12848 */
12849 static void
12850f_timer_info(typval_T *argvars, typval_T *rettv)
12851{
12852 timer_T *timer = NULL;
12853
12854 if (rettv_list_alloc(rettv) != OK)
12855 return;
12856 if (argvars[0].v_type != VAR_UNKNOWN)
12857 {
12858 if (argvars[0].v_type != VAR_NUMBER)
12859 EMSG(_(e_number_exp));
12860 else
12861 {
12862 timer = find_timer((int)get_tv_number(&argvars[0]));
12863 if (timer != NULL)
12864 add_timer_info(rettv, timer);
12865 }
12866 }
12867 else
12868 add_timer_info_all(rettv);
12869}
12870
12871/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012872 * "timer_pause(timer, paused)" function
12873 */
12874 static void
12875f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12876{
12877 timer_T *timer = NULL;
12878 int paused = (int)get_tv_number(&argvars[1]);
12879
12880 if (argvars[0].v_type != VAR_NUMBER)
12881 EMSG(_(e_number_exp));
12882 else
12883 {
12884 timer = find_timer((int)get_tv_number(&argvars[0]));
12885 if (timer != NULL)
12886 timer->tr_paused = paused;
12887 }
12888}
12889
12890/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012891 * "timer_start(time, callback [, options])" function
12892 */
12893 static void
12894f_timer_start(typval_T *argvars, typval_T *rettv)
12895{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012896 long msec = (long)get_tv_number(&argvars[0]);
12897 timer_T *timer;
12898 int repeat = 0;
12899 char_u *callback;
12900 dict_T *dict;
12901 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012902
Bram Moolenaar75537a92016-09-05 22:45:28 +020012903 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012904 if (check_secure())
12905 return;
12906 if (argvars[2].v_type != VAR_UNKNOWN)
12907 {
12908 if (argvars[2].v_type != VAR_DICT
12909 || (dict = argvars[2].vval.v_dict) == NULL)
12910 {
12911 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12912 return;
12913 }
12914 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12915 repeat = get_dict_number(dict, (char_u *)"repeat");
12916 }
12917
Bram Moolenaar75537a92016-09-05 22:45:28 +020012918 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012919 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012920 return;
12921
12922 timer = create_timer(msec, repeat);
12923 if (timer == NULL)
12924 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012925 else
12926 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012927 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012928 timer->tr_callback = vim_strsave(callback);
12929 else
12930 /* pointer into the partial */
12931 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012932 timer->tr_partial = partial;
12933 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012934 }
12935}
12936
12937/*
12938 * "timer_stop(timer)" function
12939 */
12940 static void
12941f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12942{
12943 timer_T *timer;
12944
12945 if (argvars[0].v_type != VAR_NUMBER)
12946 {
12947 EMSG(_(e_number_exp));
12948 return;
12949 }
12950 timer = find_timer((int)get_tv_number(&argvars[0]));
12951 if (timer != NULL)
12952 stop_timer(timer);
12953}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012954
12955/*
12956 * "timer_stopall()" function
12957 */
12958 static void
12959f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12960{
12961 stop_all_timers();
12962}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012963#endif
12964
12965/*
12966 * "tolower(string)" function
12967 */
12968 static void
12969f_tolower(typval_T *argvars, typval_T *rettv)
12970{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012971 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010012972 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012973}
12974
12975/*
12976 * "toupper(string)" function
12977 */
12978 static void
12979f_toupper(typval_T *argvars, typval_T *rettv)
12980{
12981 rettv->v_type = VAR_STRING;
12982 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
12983}
12984
12985/*
12986 * "tr(string, fromstr, tostr)" function
12987 */
12988 static void
12989f_tr(typval_T *argvars, typval_T *rettv)
12990{
12991 char_u *in_str;
12992 char_u *fromstr;
12993 char_u *tostr;
12994 char_u *p;
12995#ifdef FEAT_MBYTE
12996 int inlen;
12997 int fromlen;
12998 int tolen;
12999 int idx;
13000 char_u *cpstr;
13001 int cplen;
13002 int first = TRUE;
13003#endif
13004 char_u buf[NUMBUFLEN];
13005 char_u buf2[NUMBUFLEN];
13006 garray_T ga;
13007
13008 in_str = get_tv_string(&argvars[0]);
13009 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13010 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13011
13012 /* Default return value: empty string. */
13013 rettv->v_type = VAR_STRING;
13014 rettv->vval.v_string = NULL;
13015 if (fromstr == NULL || tostr == NULL)
13016 return; /* type error; errmsg already given */
13017 ga_init2(&ga, (int)sizeof(char), 80);
13018
13019#ifdef FEAT_MBYTE
13020 if (!has_mbyte)
13021#endif
13022 /* not multi-byte: fromstr and tostr must be the same length */
13023 if (STRLEN(fromstr) != STRLEN(tostr))
13024 {
13025#ifdef FEAT_MBYTE
13026error:
13027#endif
13028 EMSG2(_(e_invarg2), fromstr);
13029 ga_clear(&ga);
13030 return;
13031 }
13032
13033 /* fromstr and tostr have to contain the same number of chars */
13034 while (*in_str != NUL)
13035 {
13036#ifdef FEAT_MBYTE
13037 if (has_mbyte)
13038 {
13039 inlen = (*mb_ptr2len)(in_str);
13040 cpstr = in_str;
13041 cplen = inlen;
13042 idx = 0;
13043 for (p = fromstr; *p != NUL; p += fromlen)
13044 {
13045 fromlen = (*mb_ptr2len)(p);
13046 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13047 {
13048 for (p = tostr; *p != NUL; p += tolen)
13049 {
13050 tolen = (*mb_ptr2len)(p);
13051 if (idx-- == 0)
13052 {
13053 cplen = tolen;
13054 cpstr = p;
13055 break;
13056 }
13057 }
13058 if (*p == NUL) /* tostr is shorter than fromstr */
13059 goto error;
13060 break;
13061 }
13062 ++idx;
13063 }
13064
13065 if (first && cpstr == in_str)
13066 {
13067 /* Check that fromstr and tostr have the same number of
13068 * (multi-byte) characters. Done only once when a character
13069 * of in_str doesn't appear in fromstr. */
13070 first = FALSE;
13071 for (p = tostr; *p != NUL; p += tolen)
13072 {
13073 tolen = (*mb_ptr2len)(p);
13074 --idx;
13075 }
13076 if (idx != 0)
13077 goto error;
13078 }
13079
13080 (void)ga_grow(&ga, cplen);
13081 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13082 ga.ga_len += cplen;
13083
13084 in_str += inlen;
13085 }
13086 else
13087#endif
13088 {
13089 /* When not using multi-byte chars we can do it faster. */
13090 p = vim_strchr(fromstr, *in_str);
13091 if (p != NULL)
13092 ga_append(&ga, tostr[p - fromstr]);
13093 else
13094 ga_append(&ga, *in_str);
13095 ++in_str;
13096 }
13097 }
13098
13099 /* add a terminating NUL */
13100 (void)ga_grow(&ga, 1);
13101 ga_append(&ga, NUL);
13102
13103 rettv->vval.v_string = ga.ga_data;
13104}
13105
13106#ifdef FEAT_FLOAT
13107/*
13108 * "trunc({float})" function
13109 */
13110 static void
13111f_trunc(typval_T *argvars, typval_T *rettv)
13112{
13113 float_T f = 0.0;
13114
13115 rettv->v_type = VAR_FLOAT;
13116 if (get_float_arg(argvars, &f) == OK)
13117 /* trunc() is not in C90, use floor() or ceil() instead. */
13118 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13119 else
13120 rettv->vval.v_float = 0.0;
13121}
13122#endif
13123
13124/*
13125 * "type(expr)" function
13126 */
13127 static void
13128f_type(typval_T *argvars, typval_T *rettv)
13129{
13130 int n = -1;
13131
13132 switch (argvars[0].v_type)
13133 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013134 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13135 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013136 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013137 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13138 case VAR_LIST: n = VAR_TYPE_LIST; break;
13139 case VAR_DICT: n = VAR_TYPE_DICT; break;
13140 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013141 case VAR_SPECIAL:
13142 if (argvars[0].vval.v_number == VVAL_FALSE
13143 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013144 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013145 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013146 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013147 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013148 case VAR_JOB: n = VAR_TYPE_JOB; break;
13149 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013150 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013151 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013152 n = -1;
13153 break;
13154 }
13155 rettv->vval.v_number = n;
13156}
13157
13158/*
13159 * "undofile(name)" function
13160 */
13161 static void
13162f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13163{
13164 rettv->v_type = VAR_STRING;
13165#ifdef FEAT_PERSISTENT_UNDO
13166 {
13167 char_u *fname = get_tv_string(&argvars[0]);
13168
13169 if (*fname == NUL)
13170 {
13171 /* If there is no file name there will be no undo file. */
13172 rettv->vval.v_string = NULL;
13173 }
13174 else
13175 {
13176 char_u *ffname = FullName_save(fname, FALSE);
13177
13178 if (ffname != NULL)
13179 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13180 vim_free(ffname);
13181 }
13182 }
13183#else
13184 rettv->vval.v_string = NULL;
13185#endif
13186}
13187
13188/*
13189 * "undotree()" function
13190 */
13191 static void
13192f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13193{
13194 if (rettv_dict_alloc(rettv) == OK)
13195 {
13196 dict_T *dict = rettv->vval.v_dict;
13197 list_T *list;
13198
13199 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13200 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13201 dict_add_nr_str(dict, "save_last",
13202 (long)curbuf->b_u_save_nr_last, NULL);
13203 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13204 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13205 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13206
13207 list = list_alloc();
13208 if (list != NULL)
13209 {
13210 u_eval_tree(curbuf->b_u_oldhead, list);
13211 dict_add_list(dict, "entries", list);
13212 }
13213 }
13214}
13215
13216/*
13217 * "values(dict)" function
13218 */
13219 static void
13220f_values(typval_T *argvars, typval_T *rettv)
13221{
13222 dict_list(argvars, rettv, 1);
13223}
13224
13225/*
13226 * "virtcol(string)" function
13227 */
13228 static void
13229f_virtcol(typval_T *argvars, typval_T *rettv)
13230{
13231 colnr_T vcol = 0;
13232 pos_T *fp;
13233 int fnum = curbuf->b_fnum;
13234
13235 fp = var2fpos(&argvars[0], FALSE, &fnum);
13236 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13237 && fnum == curbuf->b_fnum)
13238 {
13239 getvvcol(curwin, fp, NULL, NULL, &vcol);
13240 ++vcol;
13241 }
13242
13243 rettv->vval.v_number = vcol;
13244}
13245
13246/*
13247 * "visualmode()" function
13248 */
13249 static void
13250f_visualmode(typval_T *argvars, typval_T *rettv)
13251{
13252 char_u str[2];
13253
13254 rettv->v_type = VAR_STRING;
13255 str[0] = curbuf->b_visual_mode_eval;
13256 str[1] = NUL;
13257 rettv->vval.v_string = vim_strsave(str);
13258
13259 /* A non-zero number or non-empty string argument: reset mode. */
13260 if (non_zero_arg(&argvars[0]))
13261 curbuf->b_visual_mode_eval = NUL;
13262}
13263
13264/*
13265 * "wildmenumode()" function
13266 */
13267 static void
13268f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13269{
13270#ifdef FEAT_WILDMENU
13271 if (wild_menu_showing)
13272 rettv->vval.v_number = 1;
13273#endif
13274}
13275
13276/*
13277 * "winbufnr(nr)" function
13278 */
13279 static void
13280f_winbufnr(typval_T *argvars, typval_T *rettv)
13281{
13282 win_T *wp;
13283
13284 wp = find_win_by_nr(&argvars[0], NULL);
13285 if (wp == NULL)
13286 rettv->vval.v_number = -1;
13287 else
13288 rettv->vval.v_number = wp->w_buffer->b_fnum;
13289}
13290
13291/*
13292 * "wincol()" function
13293 */
13294 static void
13295f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13296{
13297 validate_cursor();
13298 rettv->vval.v_number = curwin->w_wcol + 1;
13299}
13300
13301/*
13302 * "winheight(nr)" function
13303 */
13304 static void
13305f_winheight(typval_T *argvars, typval_T *rettv)
13306{
13307 win_T *wp;
13308
13309 wp = find_win_by_nr(&argvars[0], NULL);
13310 if (wp == NULL)
13311 rettv->vval.v_number = -1;
13312 else
13313 rettv->vval.v_number = wp->w_height;
13314}
13315
13316/*
13317 * "winline()" function
13318 */
13319 static void
13320f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13321{
13322 validate_cursor();
13323 rettv->vval.v_number = curwin->w_wrow + 1;
13324}
13325
13326/*
13327 * "winnr()" function
13328 */
13329 static void
13330f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13331{
13332 int nr = 1;
13333
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013334 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013335 rettv->vval.v_number = nr;
13336}
13337
13338/*
13339 * "winrestcmd()" function
13340 */
13341 static void
13342f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13343{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013344 win_T *wp;
13345 int winnr = 1;
13346 garray_T ga;
13347 char_u buf[50];
13348
13349 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013350 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013351 {
13352 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13353 ga_concat(&ga, buf);
13354 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13355 ga_concat(&ga, buf);
13356 ++winnr;
13357 }
13358 ga_append(&ga, NUL);
13359
13360 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013361 rettv->v_type = VAR_STRING;
13362}
13363
13364/*
13365 * "winrestview()" function
13366 */
13367 static void
13368f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13369{
13370 dict_T *dict;
13371
13372 if (argvars[0].v_type != VAR_DICT
13373 || (dict = argvars[0].vval.v_dict) == NULL)
13374 EMSG(_(e_invarg));
13375 else
13376 {
13377 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13378 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13379 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13380 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13381#ifdef FEAT_VIRTUALEDIT
13382 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13383 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13384#endif
13385 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13386 {
13387 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13388 curwin->w_set_curswant = FALSE;
13389 }
13390
13391 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13392 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13393#ifdef FEAT_DIFF
13394 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13395 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13396#endif
13397 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13398 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13399 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13400 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13401
13402 check_cursor();
13403 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013404 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013405 changed_window_setting();
13406
13407 if (curwin->w_topline <= 0)
13408 curwin->w_topline = 1;
13409 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13410 curwin->w_topline = curbuf->b_ml.ml_line_count;
13411#ifdef FEAT_DIFF
13412 check_topfill(curwin, TRUE);
13413#endif
13414 }
13415}
13416
13417/*
13418 * "winsaveview()" function
13419 */
13420 static void
13421f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13422{
13423 dict_T *dict;
13424
13425 if (rettv_dict_alloc(rettv) == FAIL)
13426 return;
13427 dict = rettv->vval.v_dict;
13428
13429 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13430 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13431#ifdef FEAT_VIRTUALEDIT
13432 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13433#endif
13434 update_curswant();
13435 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13436
13437 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13438#ifdef FEAT_DIFF
13439 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13440#endif
13441 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13442 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13443}
13444
13445/*
13446 * "winwidth(nr)" function
13447 */
13448 static void
13449f_winwidth(typval_T *argvars, typval_T *rettv)
13450{
13451 win_T *wp;
13452
13453 wp = find_win_by_nr(&argvars[0], NULL);
13454 if (wp == NULL)
13455 rettv->vval.v_number = -1;
13456 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013457 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013458}
13459
13460/*
13461 * "wordcount()" function
13462 */
13463 static void
13464f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13465{
13466 if (rettv_dict_alloc(rettv) == FAIL)
13467 return;
13468 cursor_pos_info(rettv->vval.v_dict);
13469}
13470
13471/*
13472 * "writefile()" function
13473 */
13474 static void
13475f_writefile(typval_T *argvars, typval_T *rettv)
13476{
13477 int binary = FALSE;
13478 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013479#ifdef HAVE_FSYNC
13480 int do_fsync = p_fs;
13481#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013482 char_u *fname;
13483 FILE *fd;
13484 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013485 listitem_T *li;
13486 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013487
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013488 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013489 if (check_restricted() || check_secure())
13490 return;
13491
13492 if (argvars[0].v_type != VAR_LIST)
13493 {
13494 EMSG2(_(e_listarg), "writefile()");
13495 return;
13496 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013497 list = argvars[0].vval.v_list;
13498 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013499 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013500 for (li = list->lv_first; li != NULL; li = li->li_next)
13501 if (get_tv_string_chk(&li->li_tv) == NULL)
13502 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013503
13504 if (argvars[2].v_type != VAR_UNKNOWN)
13505 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013506 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13507
13508 if (arg2 == NULL)
13509 return;
13510 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013511 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013512 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013513 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013514#ifdef HAVE_FSYNC
13515 if (vim_strchr(arg2, 's') != NULL)
13516 do_fsync = TRUE;
13517 else if (vim_strchr(arg2, 'S') != NULL)
13518 do_fsync = FALSE;
13519#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013520 }
13521
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013522 fname = get_tv_string_chk(&argvars[1]);
13523 if (fname == NULL)
13524 return;
13525
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013526 /* Always open the file in binary mode, library functions have a mind of
13527 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013528 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13529 append ? APPENDBIN : WRITEBIN)) == NULL)
13530 {
13531 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13532 ret = -1;
13533 }
13534 else
13535 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013536 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013537 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013538#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013539 else if (do_fsync)
13540 /* Ignore the error, the user wouldn't know what to do about it.
13541 * May happen for a device. */
13542 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013543#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013544 fclose(fd);
13545 }
13546
13547 rettv->vval.v_number = ret;
13548}
13549
13550/*
13551 * "xor(expr, expr)" function
13552 */
13553 static void
13554f_xor(typval_T *argvars, typval_T *rettv)
13555{
13556 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13557 ^ get_tv_number_chk(&argvars[1], NULL);
13558}
13559
13560
13561#endif /* FEAT_EVAL */