blob: 959be0cf8e4556d95ce97dbaedaaf9c4ca037166 [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 Moolenaara7e18d22018-02-11 14:29:49 +01004861 cleanup_jumplist(wp);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004862 for (i = 0; i < wp->w_jumplistlen; ++i)
4863 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004864 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4865 continue;
4866 if (wp->w_jumplist[i].fmark.fnum == 0)
4867 fname2fnum(&wp->w_jumplist[i]);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004868 if ((d = dict_alloc()) == NULL)
4869 return;
4870 if (list_append_dict(l, d) == FAIL)
4871 return;
4872 dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
4873 NULL);
4874 dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
4875 NULL);
4876# ifdef FEAT_VIRTUALEDIT
4877 dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
4878 NULL);
4879# endif
4880 dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004881 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaar4f505882018-02-10 21:06:32 +01004882 dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
4883 }
4884#endif
4885}
4886
4887/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004888 * "getline(lnum, [end])" function
4889 */
4890 static void
4891f_getline(typval_T *argvars, typval_T *rettv)
4892{
4893 linenr_T lnum;
4894 linenr_T end;
4895 int retlist;
4896
4897 lnum = get_tv_lnum(argvars);
4898 if (argvars[1].v_type == VAR_UNKNOWN)
4899 {
4900 end = 0;
4901 retlist = FALSE;
4902 }
4903 else
4904 {
4905 end = get_tv_lnum(&argvars[1]);
4906 retlist = TRUE;
4907 }
4908
4909 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4910}
4911
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004912#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004913 static void
4914get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4915{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004916 if (what_arg->v_type == VAR_UNKNOWN)
4917 {
4918 if (rettv_list_alloc(rettv) == OK)
4919 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004920 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004921 }
4922 else
4923 {
4924 if (rettv_dict_alloc(rettv) == OK)
4925 if (is_qf || (wp != NULL))
4926 {
4927 if (what_arg->v_type == VAR_DICT)
4928 {
4929 dict_T *d = what_arg->vval.v_dict;
4930
4931 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004932 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004933 }
4934 else
4935 EMSG(_(e_dictreq));
4936 }
4937 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004938}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004939#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004940
4941/*
4942 * "getloclist()" function
4943 */
4944 static void
4945f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4946{
4947#ifdef FEAT_QUICKFIX
4948 win_T *wp;
4949
4950 wp = find_win_by_nr(&argvars[0], NULL);
4951 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4952#endif
4953}
4954
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004955/*
4956 * "getmatches()" function
4957 */
4958 static void
4959f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4960{
4961#ifdef FEAT_SEARCH_EXTRA
4962 dict_T *dict;
4963 matchitem_T *cur = curwin->w_match_head;
4964 int i;
4965
4966 if (rettv_list_alloc(rettv) == OK)
4967 {
4968 while (cur != NULL)
4969 {
4970 dict = dict_alloc();
4971 if (dict == NULL)
4972 return;
4973 if (cur->match.regprog == NULL)
4974 {
4975 /* match added with matchaddpos() */
4976 for (i = 0; i < MAXPOSMATCH; ++i)
4977 {
4978 llpos_T *llpos;
4979 char buf[6];
4980 list_T *l;
4981
4982 llpos = &cur->pos.pos[i];
4983 if (llpos->lnum == 0)
4984 break;
4985 l = list_alloc();
4986 if (l == NULL)
4987 break;
4988 list_append_number(l, (varnumber_T)llpos->lnum);
4989 if (llpos->col > 0)
4990 {
4991 list_append_number(l, (varnumber_T)llpos->col);
4992 list_append_number(l, (varnumber_T)llpos->len);
4993 }
4994 sprintf(buf, "pos%d", i + 1);
4995 dict_add_list(dict, buf, l);
4996 }
4997 }
4998 else
4999 {
5000 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
5001 }
5002 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
5003 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
5004 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
5005# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5006 if (cur->conceal_char)
5007 {
5008 char_u buf[MB_MAXBYTES + 1];
5009
5010 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5011 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5012 }
5013# endif
5014 list_append_dict(rettv->vval.v_list, dict);
5015 cur = cur->next;
5016 }
5017 }
5018#endif
5019}
5020
5021/*
5022 * "getpid()" function
5023 */
5024 static void
5025f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5026{
5027 rettv->vval.v_number = mch_get_pid();
5028}
5029
5030 static void
5031getpos_both(
5032 typval_T *argvars,
5033 typval_T *rettv,
5034 int getcurpos)
5035{
5036 pos_T *fp;
5037 list_T *l;
5038 int fnum = -1;
5039
5040 if (rettv_list_alloc(rettv) == OK)
5041 {
5042 l = rettv->vval.v_list;
5043 if (getcurpos)
5044 fp = &curwin->w_cursor;
5045 else
5046 fp = var2fpos(&argvars[0], TRUE, &fnum);
5047 if (fnum != -1)
5048 list_append_number(l, (varnumber_T)fnum);
5049 else
5050 list_append_number(l, (varnumber_T)0);
5051 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5052 : (varnumber_T)0);
5053 list_append_number(l, (fp != NULL)
5054 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5055 : (varnumber_T)0);
5056 list_append_number(l,
5057#ifdef FEAT_VIRTUALEDIT
5058 (fp != NULL) ? (varnumber_T)fp->coladd :
5059#endif
5060 (varnumber_T)0);
5061 if (getcurpos)
5062 {
5063 update_curswant();
5064 list_append_number(l, curwin->w_curswant == MAXCOL ?
5065 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5066 }
5067 }
5068 else
5069 rettv->vval.v_number = FALSE;
5070}
5071
5072
5073/*
5074 * "getcurpos()" function
5075 */
5076 static void
5077f_getcurpos(typval_T *argvars, typval_T *rettv)
5078{
5079 getpos_both(argvars, rettv, TRUE);
5080}
5081
5082/*
5083 * "getpos(string)" function
5084 */
5085 static void
5086f_getpos(typval_T *argvars, typval_T *rettv)
5087{
5088 getpos_both(argvars, rettv, FALSE);
5089}
5090
5091/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005092 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005093 */
5094 static void
5095f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5096{
5097#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005098 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005099#endif
5100}
5101
5102/*
5103 * "getreg()" function
5104 */
5105 static void
5106f_getreg(typval_T *argvars, typval_T *rettv)
5107{
5108 char_u *strregname;
5109 int regname;
5110 int arg2 = FALSE;
5111 int return_list = FALSE;
5112 int error = FALSE;
5113
5114 if (argvars[0].v_type != VAR_UNKNOWN)
5115 {
5116 strregname = get_tv_string_chk(&argvars[0]);
5117 error = strregname == NULL;
5118 if (argvars[1].v_type != VAR_UNKNOWN)
5119 {
5120 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5121 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5122 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5123 }
5124 }
5125 else
5126 strregname = get_vim_var_str(VV_REG);
5127
5128 if (error)
5129 return;
5130
5131 regname = (strregname == NULL ? '"' : *strregname);
5132 if (regname == 0)
5133 regname = '"';
5134
5135 if (return_list)
5136 {
5137 rettv->v_type = VAR_LIST;
5138 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5139 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5140 if (rettv->vval.v_list == NULL)
5141 (void)rettv_list_alloc(rettv);
5142 else
5143 ++rettv->vval.v_list->lv_refcount;
5144 }
5145 else
5146 {
5147 rettv->v_type = VAR_STRING;
5148 rettv->vval.v_string = get_reg_contents(regname,
5149 arg2 ? GREG_EXPR_SRC : 0);
5150 }
5151}
5152
5153/*
5154 * "getregtype()" function
5155 */
5156 static void
5157f_getregtype(typval_T *argvars, typval_T *rettv)
5158{
5159 char_u *strregname;
5160 int regname;
5161 char_u buf[NUMBUFLEN + 2];
5162 long reglen = 0;
5163
5164 if (argvars[0].v_type != VAR_UNKNOWN)
5165 {
5166 strregname = get_tv_string_chk(&argvars[0]);
5167 if (strregname == NULL) /* type error; errmsg already given */
5168 {
5169 rettv->v_type = VAR_STRING;
5170 rettv->vval.v_string = NULL;
5171 return;
5172 }
5173 }
5174 else
5175 /* Default to v:register */
5176 strregname = get_vim_var_str(VV_REG);
5177
5178 regname = (strregname == NULL ? '"' : *strregname);
5179 if (regname == 0)
5180 regname = '"';
5181
5182 buf[0] = NUL;
5183 buf[1] = NUL;
5184 switch (get_reg_type(regname, &reglen))
5185 {
5186 case MLINE: buf[0] = 'V'; break;
5187 case MCHAR: buf[0] = 'v'; break;
5188 case MBLOCK:
5189 buf[0] = Ctrl_V;
5190 sprintf((char *)buf + 1, "%ld", reglen + 1);
5191 break;
5192 }
5193 rettv->v_type = VAR_STRING;
5194 rettv->vval.v_string = vim_strsave(buf);
5195}
5196
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005197/*
5198 * Returns information (variables, options, etc.) about a tab page
5199 * as a dictionary.
5200 */
5201 static dict_T *
5202get_tabpage_info(tabpage_T *tp, int tp_idx)
5203{
5204 win_T *wp;
5205 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005206 list_T *l;
5207
5208 dict = dict_alloc();
5209 if (dict == NULL)
5210 return NULL;
5211
Bram Moolenaar33928832016-08-18 21:22:04 +02005212 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005213
5214 l = list_alloc();
5215 if (l != NULL)
5216 {
5217 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5218 wp; wp = wp->w_next)
5219 list_append_number(l, (varnumber_T)wp->w_id);
5220 dict_add_list(dict, "windows", l);
5221 }
5222
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005223 /* Make a reference to tabpage variables */
5224 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005225
5226 return dict;
5227}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005228
5229/*
5230 * "gettabinfo()" function
5231 */
5232 static void
5233f_gettabinfo(typval_T *argvars, typval_T *rettv)
5234{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005235 tabpage_T *tp, *tparg = NULL;
5236 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005237 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005238
5239 if (rettv_list_alloc(rettv) != OK)
5240 return;
5241
5242 if (argvars[0].v_type != VAR_UNKNOWN)
5243 {
5244 /* Information about one tab page */
5245 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5246 if (tparg == NULL)
5247 return;
5248 }
5249
5250 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005251 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005252 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005253 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005254 if (tparg != NULL && tp != tparg)
5255 continue;
5256 d = get_tabpage_info(tp, tpnr);
5257 if (d != NULL)
5258 list_append_dict(rettv->vval.v_list, d);
5259 if (tparg != NULL)
5260 return;
5261 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005262}
5263
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005264/*
5265 * "gettabvar()" function
5266 */
5267 static void
5268f_gettabvar(typval_T *argvars, typval_T *rettv)
5269{
5270 win_T *oldcurwin;
5271 tabpage_T *tp, *oldtabpage;
5272 dictitem_T *v;
5273 char_u *varname;
5274 int done = FALSE;
5275
5276 rettv->v_type = VAR_STRING;
5277 rettv->vval.v_string = NULL;
5278
5279 varname = get_tv_string_chk(&argvars[1]);
5280 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5281 if (tp != NULL && varname != NULL)
5282 {
5283 /* Set tp to be our tabpage, temporarily. Also set the window to the
5284 * first window in the tabpage, otherwise the window is not valid. */
5285 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005286 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5287 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005288 {
5289 /* look up the variable */
5290 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5291 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5292 if (v != NULL)
5293 {
5294 copy_tv(&v->di_tv, rettv);
5295 done = TRUE;
5296 }
5297 }
5298
5299 /* restore previous notion of curwin */
5300 restore_win(oldcurwin, oldtabpage, TRUE);
5301 }
5302
5303 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5304 copy_tv(&argvars[2], rettv);
5305}
5306
5307/*
5308 * "gettabwinvar()" function
5309 */
5310 static void
5311f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5312{
5313 getwinvar(argvars, rettv, 1);
5314}
5315
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005316/*
5317 * Returns information about a window as a dictionary.
5318 */
5319 static dict_T *
5320get_win_info(win_T *wp, short tpnr, short winnr)
5321{
5322 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005323
5324 dict = dict_alloc();
5325 if (dict == NULL)
5326 return NULL;
5327
Bram Moolenaar33928832016-08-18 21:22:04 +02005328 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5329 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005330 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5331 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005332#ifdef FEAT_MENU
5333 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5334#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005335 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005336 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005337
Bram Moolenaar69905d12017-08-13 18:14:47 +02005338#ifdef FEAT_TERMINAL
5339 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5340#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005341#ifdef FEAT_QUICKFIX
5342 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5343 dict_add_nr_str(dict, "loclist",
5344 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5345#endif
5346
Bram Moolenaar30567352016-08-27 21:25:44 +02005347 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005348 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005349
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005350 return dict;
5351}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005352
5353/*
5354 * "getwininfo()" function
5355 */
5356 static void
5357f_getwininfo(typval_T *argvars, typval_T *rettv)
5358{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005359 tabpage_T *tp;
5360 win_T *wp = NULL, *wparg = NULL;
5361 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005362 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005363
5364 if (rettv_list_alloc(rettv) != OK)
5365 return;
5366
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005367 if (argvars[0].v_type != VAR_UNKNOWN)
5368 {
5369 wparg = win_id2wp(argvars);
5370 if (wparg == NULL)
5371 return;
5372 }
5373
5374 /* Collect information about either all the windows across all the tab
5375 * pages or one particular window.
5376 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005377 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005378 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005379 tabnr++;
5380 winnr = 0;
5381 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005382 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005383 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005384 if (wparg != NULL && wp != wparg)
5385 continue;
5386 d = get_win_info(wp, tabnr, winnr);
5387 if (d != NULL)
5388 list_append_dict(rettv->vval.v_list, d);
5389 if (wparg != NULL)
5390 /* found information about a specific window */
5391 return;
5392 }
5393 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005394}
5395
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005396/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005397 * "win_findbuf()" function
5398 */
5399 static void
5400f_win_findbuf(typval_T *argvars, typval_T *rettv)
5401{
5402 if (rettv_list_alloc(rettv) != FAIL)
5403 win_findbuf(argvars, rettv->vval.v_list);
5404}
5405
5406/*
5407 * "win_getid()" function
5408 */
5409 static void
5410f_win_getid(typval_T *argvars, typval_T *rettv)
5411{
5412 rettv->vval.v_number = win_getid(argvars);
5413}
5414
5415/*
5416 * "win_gotoid()" function
5417 */
5418 static void
5419f_win_gotoid(typval_T *argvars, typval_T *rettv)
5420{
5421 rettv->vval.v_number = win_gotoid(argvars);
5422}
5423
5424/*
5425 * "win_id2tabwin()" function
5426 */
5427 static void
5428f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5429{
5430 if (rettv_list_alloc(rettv) != FAIL)
5431 win_id2tabwin(argvars, rettv->vval.v_list);
5432}
5433
5434/*
5435 * "win_id2win()" function
5436 */
5437 static void
5438f_win_id2win(typval_T *argvars, typval_T *rettv)
5439{
5440 rettv->vval.v_number = win_id2win(argvars);
5441}
5442
5443/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005444 * "win_screenpos()" function
5445 */
5446 static void
5447f_win_screenpos(typval_T *argvars, typval_T *rettv)
5448{
5449 win_T *wp;
5450
5451 if (rettv_list_alloc(rettv) == FAIL)
5452 return;
5453
5454 wp = find_win_by_nr(&argvars[0], NULL);
5455 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5456 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5457}
5458
5459/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005460 * "getwinposx()" function
5461 */
5462 static void
5463f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5464{
5465 rettv->vval.v_number = -1;
5466#ifdef FEAT_GUI
5467 if (gui.in_use)
5468 {
5469 int x, y;
5470
5471 if (gui_mch_get_winpos(&x, &y) == OK)
5472 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005473 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005474 }
5475#endif
5476#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5477 {
5478 int x, y;
5479
5480 if (term_get_winpos(&x, &y) == OK)
5481 rettv->vval.v_number = x;
5482 }
5483#endif
5484}
5485
5486/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005487 * "getwinposy()" function
5488 */
5489 static void
5490f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5491{
5492 rettv->vval.v_number = -1;
5493#ifdef FEAT_GUI
5494 if (gui.in_use)
5495 {
5496 int x, y;
5497
5498 if (gui_mch_get_winpos(&x, &y) == OK)
5499 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005500 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005501 }
5502#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005503#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5504 {
5505 int x, y;
5506
5507 if (term_get_winpos(&x, &y) == OK)
5508 rettv->vval.v_number = y;
5509 }
5510#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005511}
5512
5513/*
5514 * "getwinvar()" function
5515 */
5516 static void
5517f_getwinvar(typval_T *argvars, typval_T *rettv)
5518{
5519 getwinvar(argvars, rettv, 0);
5520}
5521
5522/*
5523 * "glob()" function
5524 */
5525 static void
5526f_glob(typval_T *argvars, typval_T *rettv)
5527{
5528 int options = WILD_SILENT|WILD_USE_NL;
5529 expand_T xpc;
5530 int error = FALSE;
5531
5532 /* When the optional second argument is non-zero, don't remove matches
5533 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5534 rettv->v_type = VAR_STRING;
5535 if (argvars[1].v_type != VAR_UNKNOWN)
5536 {
5537 if (get_tv_number_chk(&argvars[1], &error))
5538 options |= WILD_KEEP_ALL;
5539 if (argvars[2].v_type != VAR_UNKNOWN)
5540 {
5541 if (get_tv_number_chk(&argvars[2], &error))
5542 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005543 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005544 }
5545 if (argvars[3].v_type != VAR_UNKNOWN
5546 && get_tv_number_chk(&argvars[3], &error))
5547 options |= WILD_ALLLINKS;
5548 }
5549 }
5550 if (!error)
5551 {
5552 ExpandInit(&xpc);
5553 xpc.xp_context = EXPAND_FILES;
5554 if (p_wic)
5555 options += WILD_ICASE;
5556 if (rettv->v_type == VAR_STRING)
5557 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5558 NULL, options, WILD_ALL);
5559 else if (rettv_list_alloc(rettv) != FAIL)
5560 {
5561 int i;
5562
5563 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5564 NULL, options, WILD_ALL_KEEP);
5565 for (i = 0; i < xpc.xp_numfiles; i++)
5566 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5567
5568 ExpandCleanup(&xpc);
5569 }
5570 }
5571 else
5572 rettv->vval.v_string = NULL;
5573}
5574
5575/*
5576 * "globpath()" function
5577 */
5578 static void
5579f_globpath(typval_T *argvars, typval_T *rettv)
5580{
5581 int flags = 0;
5582 char_u buf1[NUMBUFLEN];
5583 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5584 int error = FALSE;
5585 garray_T ga;
5586 int i;
5587
5588 /* When the optional second argument is non-zero, don't remove matches
5589 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5590 rettv->v_type = VAR_STRING;
5591 if (argvars[2].v_type != VAR_UNKNOWN)
5592 {
5593 if (get_tv_number_chk(&argvars[2], &error))
5594 flags |= WILD_KEEP_ALL;
5595 if (argvars[3].v_type != VAR_UNKNOWN)
5596 {
5597 if (get_tv_number_chk(&argvars[3], &error))
5598 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005599 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005600 }
5601 if (argvars[4].v_type != VAR_UNKNOWN
5602 && get_tv_number_chk(&argvars[4], &error))
5603 flags |= WILD_ALLLINKS;
5604 }
5605 }
5606 if (file != NULL && !error)
5607 {
5608 ga_init2(&ga, (int)sizeof(char_u *), 10);
5609 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5610 if (rettv->v_type == VAR_STRING)
5611 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5612 else if (rettv_list_alloc(rettv) != FAIL)
5613 for (i = 0; i < ga.ga_len; ++i)
5614 list_append_string(rettv->vval.v_list,
5615 ((char_u **)(ga.ga_data))[i], -1);
5616 ga_clear_strings(&ga);
5617 }
5618 else
5619 rettv->vval.v_string = NULL;
5620}
5621
5622/*
5623 * "glob2regpat()" function
5624 */
5625 static void
5626f_glob2regpat(typval_T *argvars, typval_T *rettv)
5627{
5628 char_u *pat = get_tv_string_chk(&argvars[0]);
5629
5630 rettv->v_type = VAR_STRING;
5631 rettv->vval.v_string = (pat == NULL)
5632 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5633}
5634
5635/* for VIM_VERSION_ defines */
5636#include "version.h"
5637
5638/*
5639 * "has()" function
5640 */
5641 static void
5642f_has(typval_T *argvars, typval_T *rettv)
5643{
5644 int i;
5645 char_u *name;
5646 int n = FALSE;
5647 static char *(has_list[]) =
5648 {
5649#ifdef AMIGA
5650 "amiga",
5651# ifdef FEAT_ARP
5652 "arp",
5653# endif
5654#endif
5655#ifdef __BEOS__
5656 "beos",
5657#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005658#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005659 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5660 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005661# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005662 "macunix", /* Mac OS X, with the darwin feature */
5663 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005664# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005665#endif
5666#ifdef __QNX__
5667 "qnx",
5668#endif
5669#ifdef UNIX
5670 "unix",
5671#endif
5672#ifdef VMS
5673 "vms",
5674#endif
5675#ifdef WIN32
5676 "win32",
5677#endif
5678#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5679 "win32unix",
5680#endif
5681#if defined(WIN64) || defined(_WIN64)
5682 "win64",
5683#endif
5684#ifdef EBCDIC
5685 "ebcdic",
5686#endif
5687#ifndef CASE_INSENSITIVE_FILENAME
5688 "fname_case",
5689#endif
5690#ifdef HAVE_ACL
5691 "acl",
5692#endif
5693#ifdef FEAT_ARABIC
5694 "arabic",
5695#endif
5696#ifdef FEAT_AUTOCMD
5697 "autocmd",
5698#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005699#ifdef FEAT_AUTOSERVERNAME
5700 "autoservername",
5701#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005702#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005703 "balloon_eval",
5704# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5705 "balloon_multiline",
5706# endif
5707#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005708#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005709 "balloon_eval_term",
5710#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005711#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5712 "builtin_terms",
5713# ifdef ALL_BUILTIN_TCAPS
5714 "all_builtin_terms",
5715# endif
5716#endif
5717#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5718 || defined(FEAT_GUI_W32) \
5719 || defined(FEAT_GUI_MOTIF))
5720 "browsefilter",
5721#endif
5722#ifdef FEAT_BYTEOFF
5723 "byte_offset",
5724#endif
5725#ifdef FEAT_JOB_CHANNEL
5726 "channel",
5727#endif
5728#ifdef FEAT_CINDENT
5729 "cindent",
5730#endif
5731#ifdef FEAT_CLIENTSERVER
5732 "clientserver",
5733#endif
5734#ifdef FEAT_CLIPBOARD
5735 "clipboard",
5736#endif
5737#ifdef FEAT_CMDL_COMPL
5738 "cmdline_compl",
5739#endif
5740#ifdef FEAT_CMDHIST
5741 "cmdline_hist",
5742#endif
5743#ifdef FEAT_COMMENTS
5744 "comments",
5745#endif
5746#ifdef FEAT_CONCEAL
5747 "conceal",
5748#endif
5749#ifdef FEAT_CRYPT
5750 "cryptv",
5751 "crypt-blowfish",
5752 "crypt-blowfish2",
5753#endif
5754#ifdef FEAT_CSCOPE
5755 "cscope",
5756#endif
5757#ifdef FEAT_CURSORBIND
5758 "cursorbind",
5759#endif
5760#ifdef CURSOR_SHAPE
5761 "cursorshape",
5762#endif
5763#ifdef DEBUG
5764 "debug",
5765#endif
5766#ifdef FEAT_CON_DIALOG
5767 "dialog_con",
5768#endif
5769#ifdef FEAT_GUI_DIALOG
5770 "dialog_gui",
5771#endif
5772#ifdef FEAT_DIFF
5773 "diff",
5774#endif
5775#ifdef FEAT_DIGRAPHS
5776 "digraphs",
5777#endif
5778#ifdef FEAT_DIRECTX
5779 "directx",
5780#endif
5781#ifdef FEAT_DND
5782 "dnd",
5783#endif
5784#ifdef FEAT_EMACS_TAGS
5785 "emacs_tags",
5786#endif
5787 "eval", /* always present, of course! */
5788 "ex_extra", /* graduated feature */
5789#ifdef FEAT_SEARCH_EXTRA
5790 "extra_search",
5791#endif
5792#ifdef FEAT_FKMAP
5793 "farsi",
5794#endif
5795#ifdef FEAT_SEARCHPATH
5796 "file_in_path",
5797#endif
5798#ifdef FEAT_FILTERPIPE
5799 "filterpipe",
5800#endif
5801#ifdef FEAT_FIND_ID
5802 "find_in_path",
5803#endif
5804#ifdef FEAT_FLOAT
5805 "float",
5806#endif
5807#ifdef FEAT_FOLDING
5808 "folding",
5809#endif
5810#ifdef FEAT_FOOTER
5811 "footer",
5812#endif
5813#if !defined(USE_SYSTEM) && defined(UNIX)
5814 "fork",
5815#endif
5816#ifdef FEAT_GETTEXT
5817 "gettext",
5818#endif
5819#ifdef FEAT_GUI
5820 "gui",
5821#endif
5822#ifdef FEAT_GUI_ATHENA
5823# ifdef FEAT_GUI_NEXTAW
5824 "gui_neXtaw",
5825# else
5826 "gui_athena",
5827# endif
5828#endif
5829#ifdef FEAT_GUI_GTK
5830 "gui_gtk",
5831# ifdef USE_GTK3
5832 "gui_gtk3",
5833# else
5834 "gui_gtk2",
5835# endif
5836#endif
5837#ifdef FEAT_GUI_GNOME
5838 "gui_gnome",
5839#endif
5840#ifdef FEAT_GUI_MAC
5841 "gui_mac",
5842#endif
5843#ifdef FEAT_GUI_MOTIF
5844 "gui_motif",
5845#endif
5846#ifdef FEAT_GUI_PHOTON
5847 "gui_photon",
5848#endif
5849#ifdef FEAT_GUI_W32
5850 "gui_win32",
5851#endif
5852#ifdef FEAT_HANGULIN
5853 "hangul_input",
5854#endif
5855#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5856 "iconv",
5857#endif
5858#ifdef FEAT_INS_EXPAND
5859 "insert_expand",
5860#endif
5861#ifdef FEAT_JOB_CHANNEL
5862 "job",
5863#endif
5864#ifdef FEAT_JUMPLIST
5865 "jumplist",
5866#endif
5867#ifdef FEAT_KEYMAP
5868 "keymap",
5869#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005870 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005871#ifdef FEAT_LANGMAP
5872 "langmap",
5873#endif
5874#ifdef FEAT_LIBCALL
5875 "libcall",
5876#endif
5877#ifdef FEAT_LINEBREAK
5878 "linebreak",
5879#endif
5880#ifdef FEAT_LISP
5881 "lispindent",
5882#endif
5883#ifdef FEAT_LISTCMDS
5884 "listcmds",
5885#endif
5886#ifdef FEAT_LOCALMAP
5887 "localmap",
5888#endif
5889#ifdef FEAT_LUA
5890# ifndef DYNAMIC_LUA
5891 "lua",
5892# endif
5893#endif
5894#ifdef FEAT_MENU
5895 "menu",
5896#endif
5897#ifdef FEAT_SESSION
5898 "mksession",
5899#endif
5900#ifdef FEAT_MODIFY_FNAME
5901 "modify_fname",
5902#endif
5903#ifdef FEAT_MOUSE
5904 "mouse",
5905#endif
5906#ifdef FEAT_MOUSESHAPE
5907 "mouseshape",
5908#endif
5909#if defined(UNIX) || defined(VMS)
5910# ifdef FEAT_MOUSE_DEC
5911 "mouse_dec",
5912# endif
5913# ifdef FEAT_MOUSE_GPM
5914 "mouse_gpm",
5915# endif
5916# ifdef FEAT_MOUSE_JSB
5917 "mouse_jsbterm",
5918# endif
5919# ifdef FEAT_MOUSE_NET
5920 "mouse_netterm",
5921# endif
5922# ifdef FEAT_MOUSE_PTERM
5923 "mouse_pterm",
5924# endif
5925# ifdef FEAT_MOUSE_SGR
5926 "mouse_sgr",
5927# endif
5928# ifdef FEAT_SYSMOUSE
5929 "mouse_sysmouse",
5930# endif
5931# ifdef FEAT_MOUSE_URXVT
5932 "mouse_urxvt",
5933# endif
5934# ifdef FEAT_MOUSE_XTERM
5935 "mouse_xterm",
5936# endif
5937#endif
5938#ifdef FEAT_MBYTE
5939 "multi_byte",
5940#endif
5941#ifdef FEAT_MBYTE_IME
5942 "multi_byte_ime",
5943#endif
5944#ifdef FEAT_MULTI_LANG
5945 "multi_lang",
5946#endif
5947#ifdef FEAT_MZSCHEME
5948#ifndef DYNAMIC_MZSCHEME
5949 "mzscheme",
5950#endif
5951#endif
5952#ifdef FEAT_NUM64
5953 "num64",
5954#endif
5955#ifdef FEAT_OLE
5956 "ole",
5957#endif
5958 "packages",
5959#ifdef FEAT_PATH_EXTRA
5960 "path_extra",
5961#endif
5962#ifdef FEAT_PERL
5963#ifndef DYNAMIC_PERL
5964 "perl",
5965#endif
5966#endif
5967#ifdef FEAT_PERSISTENT_UNDO
5968 "persistent_undo",
5969#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005970#if defined(FEAT_PYTHON)
5971 "python_compiled",
5972# if defined(DYNAMIC_PYTHON)
5973 "python_dynamic",
5974# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005975 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005976 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005977# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005978#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005979#if defined(FEAT_PYTHON3)
5980 "python3_compiled",
5981# if defined(DYNAMIC_PYTHON3)
5982 "python3_dynamic",
5983# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005984 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005985 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005986# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005987#endif
5988#ifdef FEAT_POSTSCRIPT
5989 "postscript",
5990#endif
5991#ifdef FEAT_PRINTER
5992 "printer",
5993#endif
5994#ifdef FEAT_PROFILE
5995 "profile",
5996#endif
5997#ifdef FEAT_RELTIME
5998 "reltime",
5999#endif
6000#ifdef FEAT_QUICKFIX
6001 "quickfix",
6002#endif
6003#ifdef FEAT_RIGHTLEFT
6004 "rightleft",
6005#endif
6006#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6007 "ruby",
6008#endif
6009#ifdef FEAT_SCROLLBIND
6010 "scrollbind",
6011#endif
6012#ifdef FEAT_CMDL_INFO
6013 "showcmd",
6014 "cmdline_info",
6015#endif
6016#ifdef FEAT_SIGNS
6017 "signs",
6018#endif
6019#ifdef FEAT_SMARTINDENT
6020 "smartindent",
6021#endif
6022#ifdef STARTUPTIME
6023 "startuptime",
6024#endif
6025#ifdef FEAT_STL_OPT
6026 "statusline",
6027#endif
6028#ifdef FEAT_SUN_WORKSHOP
6029 "sun_workshop",
6030#endif
6031#ifdef FEAT_NETBEANS_INTG
6032 "netbeans_intg",
6033#endif
6034#ifdef FEAT_SPELL
6035 "spell",
6036#endif
6037#ifdef FEAT_SYN_HL
6038 "syntax",
6039#endif
6040#if defined(USE_SYSTEM) || !defined(UNIX)
6041 "system",
6042#endif
6043#ifdef FEAT_TAG_BINS
6044 "tag_binary",
6045#endif
6046#ifdef FEAT_TAG_OLDSTATIC
6047 "tag_old_static",
6048#endif
6049#ifdef FEAT_TAG_ANYWHITE
6050 "tag_any_white",
6051#endif
6052#ifdef FEAT_TCL
6053# ifndef DYNAMIC_TCL
6054 "tcl",
6055# endif
6056#endif
6057#ifdef FEAT_TERMGUICOLORS
6058 "termguicolors",
6059#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006060#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006061 "terminal",
6062#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006063#ifdef TERMINFO
6064 "terminfo",
6065#endif
6066#ifdef FEAT_TERMRESPONSE
6067 "termresponse",
6068#endif
6069#ifdef FEAT_TEXTOBJ
6070 "textobjects",
6071#endif
6072#ifdef HAVE_TGETENT
6073 "tgetent",
6074#endif
6075#ifdef FEAT_TIMERS
6076 "timers",
6077#endif
6078#ifdef FEAT_TITLE
6079 "title",
6080#endif
6081#ifdef FEAT_TOOLBAR
6082 "toolbar",
6083#endif
6084#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6085 "unnamedplus",
6086#endif
6087#ifdef FEAT_USR_CMDS
6088 "user-commands", /* was accidentally included in 5.4 */
6089 "user_commands",
6090#endif
6091#ifdef FEAT_VIMINFO
6092 "viminfo",
6093#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006094 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006095#ifdef FEAT_VIRTUALEDIT
6096 "virtualedit",
6097#endif
6098 "visual",
6099#ifdef FEAT_VISUALEXTRA
6100 "visualextra",
6101#endif
6102#ifdef FEAT_VREPLACE
6103 "vreplace",
6104#endif
6105#ifdef FEAT_WILDIGN
6106 "wildignore",
6107#endif
6108#ifdef FEAT_WILDMENU
6109 "wildmenu",
6110#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006111 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006112#ifdef FEAT_WAK
6113 "winaltkeys",
6114#endif
6115#ifdef FEAT_WRITEBACKUP
6116 "writebackup",
6117#endif
6118#ifdef FEAT_XIM
6119 "xim",
6120#endif
6121#ifdef FEAT_XFONTSET
6122 "xfontset",
6123#endif
6124#ifdef FEAT_XPM_W32
6125 "xpm",
6126 "xpm_w32", /* for backward compatibility */
6127#else
6128# if defined(HAVE_XPM)
6129 "xpm",
6130# endif
6131#endif
6132#ifdef USE_XSMP
6133 "xsmp",
6134#endif
6135#ifdef USE_XSMP_INTERACT
6136 "xsmp_interact",
6137#endif
6138#ifdef FEAT_XCLIPBOARD
6139 "xterm_clipboard",
6140#endif
6141#ifdef FEAT_XTERM_SAVE
6142 "xterm_save",
6143#endif
6144#if defined(UNIX) && defined(FEAT_X11)
6145 "X11",
6146#endif
6147 NULL
6148 };
6149
6150 name = get_tv_string(&argvars[0]);
6151 for (i = 0; has_list[i] != NULL; ++i)
6152 if (STRICMP(name, has_list[i]) == 0)
6153 {
6154 n = TRUE;
6155 break;
6156 }
6157
6158 if (n == FALSE)
6159 {
6160 if (STRNICMP(name, "patch", 5) == 0)
6161 {
6162 if (name[5] == '-'
6163 && STRLEN(name) >= 11
6164 && vim_isdigit(name[6])
6165 && vim_isdigit(name[8])
6166 && vim_isdigit(name[10]))
6167 {
6168 int major = atoi((char *)name + 6);
6169 int minor = atoi((char *)name + 8);
6170
6171 /* Expect "patch-9.9.01234". */
6172 n = (major < VIM_VERSION_MAJOR
6173 || (major == VIM_VERSION_MAJOR
6174 && (minor < VIM_VERSION_MINOR
6175 || (minor == VIM_VERSION_MINOR
6176 && has_patch(atoi((char *)name + 10))))));
6177 }
6178 else
6179 n = has_patch(atoi((char *)name + 5));
6180 }
6181 else if (STRICMP(name, "vim_starting") == 0)
6182 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006183 else if (STRICMP(name, "ttyin") == 0)
6184 n = mch_input_isatty();
6185 else if (STRICMP(name, "ttyout") == 0)
6186 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006187#ifdef FEAT_MBYTE
6188 else if (STRICMP(name, "multi_byte_encoding") == 0)
6189 n = has_mbyte;
6190#endif
6191#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6192 else if (STRICMP(name, "balloon_multiline") == 0)
6193 n = multiline_balloon_available();
6194#endif
6195#ifdef DYNAMIC_TCL
6196 else if (STRICMP(name, "tcl") == 0)
6197 n = tcl_enabled(FALSE);
6198#endif
6199#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6200 else if (STRICMP(name, "iconv") == 0)
6201 n = iconv_enabled(FALSE);
6202#endif
6203#ifdef DYNAMIC_LUA
6204 else if (STRICMP(name, "lua") == 0)
6205 n = lua_enabled(FALSE);
6206#endif
6207#ifdef DYNAMIC_MZSCHEME
6208 else if (STRICMP(name, "mzscheme") == 0)
6209 n = mzscheme_enabled(FALSE);
6210#endif
6211#ifdef DYNAMIC_RUBY
6212 else if (STRICMP(name, "ruby") == 0)
6213 n = ruby_enabled(FALSE);
6214#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006215#ifdef DYNAMIC_PYTHON
6216 else if (STRICMP(name, "python") == 0)
6217 n = python_enabled(FALSE);
6218#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006219#ifdef DYNAMIC_PYTHON3
6220 else if (STRICMP(name, "python3") == 0)
6221 n = python3_enabled(FALSE);
6222#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006223#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6224 else if (STRICMP(name, "pythonx") == 0)
6225 {
6226# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6227 if (p_pyx == 0)
6228 n = python3_enabled(FALSE) || python_enabled(FALSE);
6229 else if (p_pyx == 3)
6230 n = python3_enabled(FALSE);
6231 else if (p_pyx == 2)
6232 n = python_enabled(FALSE);
6233# elif defined(DYNAMIC_PYTHON)
6234 n = python_enabled(FALSE);
6235# elif defined(DYNAMIC_PYTHON3)
6236 n = python3_enabled(FALSE);
6237# endif
6238 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006239#endif
6240#ifdef DYNAMIC_PERL
6241 else if (STRICMP(name, "perl") == 0)
6242 n = perl_enabled(FALSE);
6243#endif
6244#ifdef FEAT_GUI
6245 else if (STRICMP(name, "gui_running") == 0)
6246 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247# ifdef FEAT_BROWSE
6248 else if (STRICMP(name, "browse") == 0)
6249 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6250# endif
6251#endif
6252#ifdef FEAT_SYN_HL
6253 else if (STRICMP(name, "syntax_items") == 0)
6254 n = syntax_present(curwin);
6255#endif
6256#if defined(WIN3264)
6257 else if (STRICMP(name, "win95") == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006258 n = FALSE; /* Win9x is no more supported. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006259#endif
6260#ifdef FEAT_NETBEANS_INTG
6261 else if (STRICMP(name, "netbeans_enabled") == 0)
6262 n = netbeans_active();
6263#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006264#if defined(FEAT_TERMINAL) && defined(WIN3264)
6265 else if (STRICMP(name, "terminal") == 0)
6266 n = terminal_enabled();
6267#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006268 }
6269
6270 rettv->vval.v_number = n;
6271}
6272
6273/*
6274 * "has_key()" function
6275 */
6276 static void
6277f_has_key(typval_T *argvars, typval_T *rettv)
6278{
6279 if (argvars[0].v_type != VAR_DICT)
6280 {
6281 EMSG(_(e_dictreq));
6282 return;
6283 }
6284 if (argvars[0].vval.v_dict == NULL)
6285 return;
6286
6287 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6288 get_tv_string(&argvars[1]), -1) != NULL;
6289}
6290
6291/*
6292 * "haslocaldir()" function
6293 */
6294 static void
6295f_haslocaldir(typval_T *argvars, typval_T *rettv)
6296{
6297 win_T *wp = NULL;
6298
6299 wp = find_tabwin(&argvars[0], &argvars[1]);
6300 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6301}
6302
6303/*
6304 * "hasmapto()" function
6305 */
6306 static void
6307f_hasmapto(typval_T *argvars, typval_T *rettv)
6308{
6309 char_u *name;
6310 char_u *mode;
6311 char_u buf[NUMBUFLEN];
6312 int abbr = FALSE;
6313
6314 name = get_tv_string(&argvars[0]);
6315 if (argvars[1].v_type == VAR_UNKNOWN)
6316 mode = (char_u *)"nvo";
6317 else
6318 {
6319 mode = get_tv_string_buf(&argvars[1], buf);
6320 if (argvars[2].v_type != VAR_UNKNOWN)
6321 abbr = (int)get_tv_number(&argvars[2]);
6322 }
6323
6324 if (map_to_exists(name, mode, abbr))
6325 rettv->vval.v_number = TRUE;
6326 else
6327 rettv->vval.v_number = FALSE;
6328}
6329
6330/*
6331 * "histadd()" function
6332 */
6333 static void
6334f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6335{
6336#ifdef FEAT_CMDHIST
6337 int histype;
6338 char_u *str;
6339 char_u buf[NUMBUFLEN];
6340#endif
6341
6342 rettv->vval.v_number = FALSE;
6343 if (check_restricted() || check_secure())
6344 return;
6345#ifdef FEAT_CMDHIST
6346 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6347 histype = str != NULL ? get_histtype(str) : -1;
6348 if (histype >= 0)
6349 {
6350 str = get_tv_string_buf(&argvars[1], buf);
6351 if (*str != NUL)
6352 {
6353 init_history();
6354 add_to_history(histype, str, FALSE, NUL);
6355 rettv->vval.v_number = TRUE;
6356 return;
6357 }
6358 }
6359#endif
6360}
6361
6362/*
6363 * "histdel()" function
6364 */
6365 static void
6366f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6367{
6368#ifdef FEAT_CMDHIST
6369 int n;
6370 char_u buf[NUMBUFLEN];
6371 char_u *str;
6372
6373 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6374 if (str == NULL)
6375 n = 0;
6376 else if (argvars[1].v_type == VAR_UNKNOWN)
6377 /* only one argument: clear entire history */
6378 n = clr_history(get_histtype(str));
6379 else if (argvars[1].v_type == VAR_NUMBER)
6380 /* index given: remove that entry */
6381 n = del_history_idx(get_histtype(str),
6382 (int)get_tv_number(&argvars[1]));
6383 else
6384 /* string given: remove all matching entries */
6385 n = del_history_entry(get_histtype(str),
6386 get_tv_string_buf(&argvars[1], buf));
6387 rettv->vval.v_number = n;
6388#endif
6389}
6390
6391/*
6392 * "histget()" function
6393 */
6394 static void
6395f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6396{
6397#ifdef FEAT_CMDHIST
6398 int type;
6399 int idx;
6400 char_u *str;
6401
6402 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6403 if (str == NULL)
6404 rettv->vval.v_string = NULL;
6405 else
6406 {
6407 type = get_histtype(str);
6408 if (argvars[1].v_type == VAR_UNKNOWN)
6409 idx = get_history_idx(type);
6410 else
6411 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6412 /* -1 on type error */
6413 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6414 }
6415#else
6416 rettv->vval.v_string = NULL;
6417#endif
6418 rettv->v_type = VAR_STRING;
6419}
6420
6421/*
6422 * "histnr()" function
6423 */
6424 static void
6425f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6426{
6427 int i;
6428
6429#ifdef FEAT_CMDHIST
6430 char_u *history = get_tv_string_chk(&argvars[0]);
6431
6432 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6433 if (i >= HIST_CMD && i < HIST_COUNT)
6434 i = get_history_idx(i);
6435 else
6436#endif
6437 i = -1;
6438 rettv->vval.v_number = i;
6439}
6440
6441/*
6442 * "highlightID(name)" function
6443 */
6444 static void
6445f_hlID(typval_T *argvars, typval_T *rettv)
6446{
6447 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6448}
6449
6450/*
6451 * "highlight_exists()" function
6452 */
6453 static void
6454f_hlexists(typval_T *argvars, typval_T *rettv)
6455{
6456 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6457}
6458
6459/*
6460 * "hostname()" function
6461 */
6462 static void
6463f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6464{
6465 char_u hostname[256];
6466
6467 mch_get_host_name(hostname, 256);
6468 rettv->v_type = VAR_STRING;
6469 rettv->vval.v_string = vim_strsave(hostname);
6470}
6471
6472/*
6473 * iconv() function
6474 */
6475 static void
6476f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6477{
6478#ifdef FEAT_MBYTE
6479 char_u buf1[NUMBUFLEN];
6480 char_u buf2[NUMBUFLEN];
6481 char_u *from, *to, *str;
6482 vimconv_T vimconv;
6483#endif
6484
6485 rettv->v_type = VAR_STRING;
6486 rettv->vval.v_string = NULL;
6487
6488#ifdef FEAT_MBYTE
6489 str = get_tv_string(&argvars[0]);
6490 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6491 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6492 vimconv.vc_type = CONV_NONE;
6493 convert_setup(&vimconv, from, to);
6494
6495 /* If the encodings are equal, no conversion needed. */
6496 if (vimconv.vc_type == CONV_NONE)
6497 rettv->vval.v_string = vim_strsave(str);
6498 else
6499 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6500
6501 convert_setup(&vimconv, NULL, NULL);
6502 vim_free(from);
6503 vim_free(to);
6504#endif
6505}
6506
6507/*
6508 * "indent()" function
6509 */
6510 static void
6511f_indent(typval_T *argvars, typval_T *rettv)
6512{
6513 linenr_T lnum;
6514
6515 lnum = get_tv_lnum(argvars);
6516 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6517 rettv->vval.v_number = get_indent_lnum(lnum);
6518 else
6519 rettv->vval.v_number = -1;
6520}
6521
6522/*
6523 * "index()" function
6524 */
6525 static void
6526f_index(typval_T *argvars, typval_T *rettv)
6527{
6528 list_T *l;
6529 listitem_T *item;
6530 long idx = 0;
6531 int ic = FALSE;
6532
6533 rettv->vval.v_number = -1;
6534 if (argvars[0].v_type != VAR_LIST)
6535 {
6536 EMSG(_(e_listreq));
6537 return;
6538 }
6539 l = argvars[0].vval.v_list;
6540 if (l != NULL)
6541 {
6542 item = l->lv_first;
6543 if (argvars[2].v_type != VAR_UNKNOWN)
6544 {
6545 int error = FALSE;
6546
6547 /* Start at specified item. Use the cached index that list_find()
6548 * sets, so that a negative number also works. */
6549 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6550 idx = l->lv_idx;
6551 if (argvars[3].v_type != VAR_UNKNOWN)
6552 ic = (int)get_tv_number_chk(&argvars[3], &error);
6553 if (error)
6554 item = NULL;
6555 }
6556
6557 for ( ; item != NULL; item = item->li_next, ++idx)
6558 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6559 {
6560 rettv->vval.v_number = idx;
6561 break;
6562 }
6563 }
6564}
6565
6566static int inputsecret_flag = 0;
6567
6568/*
6569 * "input()" function
6570 * Also handles inputsecret() when inputsecret is set.
6571 */
6572 static void
6573f_input(typval_T *argvars, typval_T *rettv)
6574{
6575 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6576}
6577
6578/*
6579 * "inputdialog()" function
6580 */
6581 static void
6582f_inputdialog(typval_T *argvars, typval_T *rettv)
6583{
6584#if defined(FEAT_GUI_TEXTDIALOG)
6585 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6586 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6587 {
6588 char_u *message;
6589 char_u buf[NUMBUFLEN];
6590 char_u *defstr = (char_u *)"";
6591
6592 message = get_tv_string_chk(&argvars[0]);
6593 if (argvars[1].v_type != VAR_UNKNOWN
6594 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6595 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6596 else
6597 IObuff[0] = NUL;
6598 if (message != NULL && defstr != NULL
6599 && do_dialog(VIM_QUESTION, NULL, message,
6600 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6601 rettv->vval.v_string = vim_strsave(IObuff);
6602 else
6603 {
6604 if (message != NULL && defstr != NULL
6605 && argvars[1].v_type != VAR_UNKNOWN
6606 && argvars[2].v_type != VAR_UNKNOWN)
6607 rettv->vval.v_string = vim_strsave(
6608 get_tv_string_buf(&argvars[2], buf));
6609 else
6610 rettv->vval.v_string = NULL;
6611 }
6612 rettv->v_type = VAR_STRING;
6613 }
6614 else
6615#endif
6616 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6617}
6618
6619/*
6620 * "inputlist()" function
6621 */
6622 static void
6623f_inputlist(typval_T *argvars, typval_T *rettv)
6624{
6625 listitem_T *li;
6626 int selected;
6627 int mouse_used;
6628
6629#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006630 /* While starting up, there is no place to enter text. When running tests
6631 * with --not-a-term we assume feedkeys() will be used. */
6632 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006633 return;
6634#endif
6635 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6636 {
6637 EMSG2(_(e_listarg), "inputlist()");
6638 return;
6639 }
6640
6641 msg_start();
6642 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6643 lines_left = Rows; /* avoid more prompt */
6644 msg_scroll = TRUE;
6645 msg_clr_eos();
6646
6647 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6648 {
6649 msg_puts(get_tv_string(&li->li_tv));
6650 msg_putchar('\n');
6651 }
6652
6653 /* Ask for choice. */
6654 selected = prompt_for_number(&mouse_used);
6655 if (mouse_used)
6656 selected -= lines_left;
6657
6658 rettv->vval.v_number = selected;
6659}
6660
6661
6662static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6663
6664/*
6665 * "inputrestore()" function
6666 */
6667 static void
6668f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6669{
6670 if (ga_userinput.ga_len > 0)
6671 {
6672 --ga_userinput.ga_len;
6673 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6674 + ga_userinput.ga_len);
6675 /* default return is zero == OK */
6676 }
6677 else if (p_verbose > 1)
6678 {
6679 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6680 rettv->vval.v_number = 1; /* Failed */
6681 }
6682}
6683
6684/*
6685 * "inputsave()" function
6686 */
6687 static void
6688f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6689{
6690 /* Add an entry to the stack of typeahead storage. */
6691 if (ga_grow(&ga_userinput, 1) == OK)
6692 {
6693 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6694 + ga_userinput.ga_len);
6695 ++ga_userinput.ga_len;
6696 /* default return is zero == OK */
6697 }
6698 else
6699 rettv->vval.v_number = 1; /* Failed */
6700}
6701
6702/*
6703 * "inputsecret()" function
6704 */
6705 static void
6706f_inputsecret(typval_T *argvars, typval_T *rettv)
6707{
6708 ++cmdline_star;
6709 ++inputsecret_flag;
6710 f_input(argvars, rettv);
6711 --cmdline_star;
6712 --inputsecret_flag;
6713}
6714
6715/*
6716 * "insert()" function
6717 */
6718 static void
6719f_insert(typval_T *argvars, typval_T *rettv)
6720{
6721 long before = 0;
6722 listitem_T *item;
6723 list_T *l;
6724 int error = FALSE;
6725
6726 if (argvars[0].v_type != VAR_LIST)
6727 EMSG2(_(e_listarg), "insert()");
6728 else if ((l = argvars[0].vval.v_list) != NULL
6729 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6730 {
6731 if (argvars[2].v_type != VAR_UNKNOWN)
6732 before = (long)get_tv_number_chk(&argvars[2], &error);
6733 if (error)
6734 return; /* type error; errmsg already given */
6735
6736 if (before == l->lv_len)
6737 item = NULL;
6738 else
6739 {
6740 item = list_find(l, before);
6741 if (item == NULL)
6742 {
6743 EMSGN(_(e_listidx), before);
6744 l = NULL;
6745 }
6746 }
6747 if (l != NULL)
6748 {
6749 list_insert_tv(l, &argvars[1], item);
6750 copy_tv(&argvars[0], rettv);
6751 }
6752 }
6753}
6754
6755/*
6756 * "invert(expr)" function
6757 */
6758 static void
6759f_invert(typval_T *argvars, typval_T *rettv)
6760{
6761 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6762}
6763
6764/*
6765 * "isdirectory()" function
6766 */
6767 static void
6768f_isdirectory(typval_T *argvars, typval_T *rettv)
6769{
6770 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6771}
6772
6773/*
6774 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6775 * or it refers to a List or Dictionary that is locked.
6776 */
6777 static int
6778tv_islocked(typval_T *tv)
6779{
6780 return (tv->v_lock & VAR_LOCKED)
6781 || (tv->v_type == VAR_LIST
6782 && tv->vval.v_list != NULL
6783 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6784 || (tv->v_type == VAR_DICT
6785 && tv->vval.v_dict != NULL
6786 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6787}
6788
6789/*
6790 * "islocked()" function
6791 */
6792 static void
6793f_islocked(typval_T *argvars, typval_T *rettv)
6794{
6795 lval_T lv;
6796 char_u *end;
6797 dictitem_T *di;
6798
6799 rettv->vval.v_number = -1;
6800 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006801 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006802 if (end != NULL && lv.ll_name != NULL)
6803 {
6804 if (*end != NUL)
6805 EMSG(_(e_trailing));
6806 else
6807 {
6808 if (lv.ll_tv == NULL)
6809 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006810 di = find_var(lv.ll_name, NULL, TRUE);
6811 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006812 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006813 /* Consider a variable locked when:
6814 * 1. the variable itself is locked
6815 * 2. the value of the variable is locked.
6816 * 3. the List or Dict value is locked.
6817 */
6818 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6819 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006820 }
6821 }
6822 else if (lv.ll_range)
6823 EMSG(_("E786: Range not allowed"));
6824 else if (lv.ll_newkey != NULL)
6825 EMSG2(_(e_dictkey), lv.ll_newkey);
6826 else if (lv.ll_list != NULL)
6827 /* List item. */
6828 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6829 else
6830 /* Dictionary item. */
6831 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6832 }
6833 }
6834
6835 clear_lval(&lv);
6836}
6837
6838#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6839/*
6840 * "isnan()" function
6841 */
6842 static void
6843f_isnan(typval_T *argvars, typval_T *rettv)
6844{
6845 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6846 && isnan(argvars[0].vval.v_float);
6847}
6848#endif
6849
6850/*
6851 * "items(dict)" function
6852 */
6853 static void
6854f_items(typval_T *argvars, typval_T *rettv)
6855{
6856 dict_list(argvars, rettv, 2);
6857}
6858
6859#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6860/*
6861 * Get the job from the argument.
6862 * Returns NULL if the job is invalid.
6863 */
6864 static job_T *
6865get_job_arg(typval_T *tv)
6866{
6867 job_T *job;
6868
6869 if (tv->v_type != VAR_JOB)
6870 {
6871 EMSG2(_(e_invarg2), get_tv_string(tv));
6872 return NULL;
6873 }
6874 job = tv->vval.v_job;
6875
6876 if (job == NULL)
6877 EMSG(_("E916: not a valid job"));
6878 return job;
6879}
6880
6881/*
6882 * "job_getchannel()" function
6883 */
6884 static void
6885f_job_getchannel(typval_T *argvars, typval_T *rettv)
6886{
6887 job_T *job = get_job_arg(&argvars[0]);
6888
6889 if (job != NULL)
6890 {
6891 rettv->v_type = VAR_CHANNEL;
6892 rettv->vval.v_channel = job->jv_channel;
6893 if (job->jv_channel != NULL)
6894 ++job->jv_channel->ch_refcount;
6895 }
6896}
6897
6898/*
6899 * "job_info()" function
6900 */
6901 static void
6902f_job_info(typval_T *argvars, typval_T *rettv)
6903{
6904 job_T *job = get_job_arg(&argvars[0]);
6905
6906 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6907 job_info(job, rettv->vval.v_dict);
6908}
6909
6910/*
6911 * "job_setoptions()" function
6912 */
6913 static void
6914f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6915{
6916 job_T *job = get_job_arg(&argvars[0]);
6917 jobopt_T opt;
6918
6919 if (job == NULL)
6920 return;
6921 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02006922 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923 job_set_options(job, &opt);
6924 free_job_options(&opt);
6925}
6926
6927/*
6928 * "job_start()" function
6929 */
6930 static void
6931f_job_start(typval_T *argvars, typval_T *rettv)
6932{
6933 rettv->v_type = VAR_JOB;
6934 if (check_restricted() || check_secure())
6935 return;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006936 rettv->vval.v_job = job_start(argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006937}
6938
6939/*
6940 * "job_status()" function
6941 */
6942 static void
6943f_job_status(typval_T *argvars, typval_T *rettv)
6944{
6945 job_T *job = get_job_arg(&argvars[0]);
6946
6947 if (job != NULL)
6948 {
6949 rettv->v_type = VAR_STRING;
6950 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6951 }
6952}
6953
6954/*
6955 * "job_stop()" function
6956 */
6957 static void
6958f_job_stop(typval_T *argvars, typval_T *rettv)
6959{
6960 job_T *job = get_job_arg(&argvars[0]);
6961
6962 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006963 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006964}
6965#endif
6966
6967/*
6968 * "join()" function
6969 */
6970 static void
6971f_join(typval_T *argvars, typval_T *rettv)
6972{
6973 garray_T ga;
6974 char_u *sep;
6975
6976 if (argvars[0].v_type != VAR_LIST)
6977 {
6978 EMSG(_(e_listreq));
6979 return;
6980 }
6981 if (argvars[0].vval.v_list == NULL)
6982 return;
6983 if (argvars[1].v_type == VAR_UNKNOWN)
6984 sep = (char_u *)" ";
6985 else
6986 sep = get_tv_string_chk(&argvars[1]);
6987
6988 rettv->v_type = VAR_STRING;
6989
6990 if (sep != NULL)
6991 {
6992 ga_init2(&ga, (int)sizeof(char), 80);
6993 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
6994 ga_append(&ga, NUL);
6995 rettv->vval.v_string = (char_u *)ga.ga_data;
6996 }
6997 else
6998 rettv->vval.v_string = NULL;
6999}
7000
7001/*
7002 * "js_decode()" function
7003 */
7004 static void
7005f_js_decode(typval_T *argvars, typval_T *rettv)
7006{
7007 js_read_T reader;
7008
7009 reader.js_buf = get_tv_string(&argvars[0]);
7010 reader.js_fill = NULL;
7011 reader.js_used = 0;
7012 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7013 EMSG(_(e_invarg));
7014}
7015
7016/*
7017 * "js_encode()" function
7018 */
7019 static void
7020f_js_encode(typval_T *argvars, typval_T *rettv)
7021{
7022 rettv->v_type = VAR_STRING;
7023 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7024}
7025
7026/*
7027 * "json_decode()" function
7028 */
7029 static void
7030f_json_decode(typval_T *argvars, typval_T *rettv)
7031{
7032 js_read_T reader;
7033
7034 reader.js_buf = get_tv_string(&argvars[0]);
7035 reader.js_fill = NULL;
7036 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007037 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007038}
7039
7040/*
7041 * "json_encode()" function
7042 */
7043 static void
7044f_json_encode(typval_T *argvars, typval_T *rettv)
7045{
7046 rettv->v_type = VAR_STRING;
7047 rettv->vval.v_string = json_encode(&argvars[0], 0);
7048}
7049
7050/*
7051 * "keys()" function
7052 */
7053 static void
7054f_keys(typval_T *argvars, typval_T *rettv)
7055{
7056 dict_list(argvars, rettv, 0);
7057}
7058
7059/*
7060 * "last_buffer_nr()" function.
7061 */
7062 static void
7063f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7064{
7065 int n = 0;
7066 buf_T *buf;
7067
Bram Moolenaar29323592016-07-24 22:04:11 +02007068 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007069 if (n < buf->b_fnum)
7070 n = buf->b_fnum;
7071
7072 rettv->vval.v_number = n;
7073}
7074
7075/*
7076 * "len()" function
7077 */
7078 static void
7079f_len(typval_T *argvars, typval_T *rettv)
7080{
7081 switch (argvars[0].v_type)
7082 {
7083 case VAR_STRING:
7084 case VAR_NUMBER:
7085 rettv->vval.v_number = (varnumber_T)STRLEN(
7086 get_tv_string(&argvars[0]));
7087 break;
7088 case VAR_LIST:
7089 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7090 break;
7091 case VAR_DICT:
7092 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7093 break;
7094 case VAR_UNKNOWN:
7095 case VAR_SPECIAL:
7096 case VAR_FLOAT:
7097 case VAR_FUNC:
7098 case VAR_PARTIAL:
7099 case VAR_JOB:
7100 case VAR_CHANNEL:
7101 EMSG(_("E701: Invalid type for len()"));
7102 break;
7103 }
7104}
7105
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007106 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007107libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007108{
7109#ifdef FEAT_LIBCALL
7110 char_u *string_in;
7111 char_u **string_result;
7112 int nr_result;
7113#endif
7114
7115 rettv->v_type = type;
7116 if (type != VAR_NUMBER)
7117 rettv->vval.v_string = NULL;
7118
7119 if (check_restricted() || check_secure())
7120 return;
7121
7122#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007123 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007124 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7125 {
7126 string_in = NULL;
7127 if (argvars[2].v_type == VAR_STRING)
7128 string_in = argvars[2].vval.v_string;
7129 if (type == VAR_NUMBER)
7130 string_result = NULL;
7131 else
7132 string_result = &rettv->vval.v_string;
7133 if (mch_libcall(argvars[0].vval.v_string,
7134 argvars[1].vval.v_string,
7135 string_in,
7136 argvars[2].vval.v_number,
7137 string_result,
7138 &nr_result) == OK
7139 && type == VAR_NUMBER)
7140 rettv->vval.v_number = nr_result;
7141 }
7142#endif
7143}
7144
7145/*
7146 * "libcall()" function
7147 */
7148 static void
7149f_libcall(typval_T *argvars, typval_T *rettv)
7150{
7151 libcall_common(argvars, rettv, VAR_STRING);
7152}
7153
7154/*
7155 * "libcallnr()" function
7156 */
7157 static void
7158f_libcallnr(typval_T *argvars, typval_T *rettv)
7159{
7160 libcall_common(argvars, rettv, VAR_NUMBER);
7161}
7162
7163/*
7164 * "line(string)" function
7165 */
7166 static void
7167f_line(typval_T *argvars, typval_T *rettv)
7168{
7169 linenr_T lnum = 0;
7170 pos_T *fp;
7171 int fnum;
7172
7173 fp = var2fpos(&argvars[0], TRUE, &fnum);
7174 if (fp != NULL)
7175 lnum = fp->lnum;
7176 rettv->vval.v_number = lnum;
7177}
7178
7179/*
7180 * "line2byte(lnum)" function
7181 */
7182 static void
7183f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7184{
7185#ifndef FEAT_BYTEOFF
7186 rettv->vval.v_number = -1;
7187#else
7188 linenr_T lnum;
7189
7190 lnum = get_tv_lnum(argvars);
7191 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7192 rettv->vval.v_number = -1;
7193 else
7194 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7195 if (rettv->vval.v_number >= 0)
7196 ++rettv->vval.v_number;
7197#endif
7198}
7199
7200/*
7201 * "lispindent(lnum)" function
7202 */
7203 static void
7204f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7205{
7206#ifdef FEAT_LISP
7207 pos_T pos;
7208 linenr_T lnum;
7209
7210 pos = curwin->w_cursor;
7211 lnum = get_tv_lnum(argvars);
7212 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7213 {
7214 curwin->w_cursor.lnum = lnum;
7215 rettv->vval.v_number = get_lisp_indent();
7216 curwin->w_cursor = pos;
7217 }
7218 else
7219#endif
7220 rettv->vval.v_number = -1;
7221}
7222
7223/*
7224 * "localtime()" function
7225 */
7226 static void
7227f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7228{
7229 rettv->vval.v_number = (varnumber_T)time(NULL);
7230}
7231
7232static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7233
7234 static void
7235get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7236{
7237 char_u *keys;
7238 char_u *which;
7239 char_u buf[NUMBUFLEN];
7240 char_u *keys_buf = NULL;
7241 char_u *rhs;
7242 int mode;
7243 int abbr = FALSE;
7244 int get_dict = FALSE;
7245 mapblock_T *mp;
7246 int buffer_local;
7247
7248 /* return empty string for failure */
7249 rettv->v_type = VAR_STRING;
7250 rettv->vval.v_string = NULL;
7251
7252 keys = get_tv_string(&argvars[0]);
7253 if (*keys == NUL)
7254 return;
7255
7256 if (argvars[1].v_type != VAR_UNKNOWN)
7257 {
7258 which = get_tv_string_buf_chk(&argvars[1], buf);
7259 if (argvars[2].v_type != VAR_UNKNOWN)
7260 {
7261 abbr = (int)get_tv_number(&argvars[2]);
7262 if (argvars[3].v_type != VAR_UNKNOWN)
7263 get_dict = (int)get_tv_number(&argvars[3]);
7264 }
7265 }
7266 else
7267 which = (char_u *)"";
7268 if (which == NULL)
7269 return;
7270
7271 mode = get_map_mode(&which, 0);
7272
7273 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7274 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7275 vim_free(keys_buf);
7276
7277 if (!get_dict)
7278 {
7279 /* Return a string. */
7280 if (rhs != NULL)
7281 rettv->vval.v_string = str2special_save(rhs, FALSE);
7282
7283 }
7284 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7285 {
7286 /* Return a dictionary. */
7287 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7288 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7289 dict_T *dict = rettv->vval.v_dict;
7290
7291 dict_add_nr_str(dict, "lhs", 0L, lhs);
7292 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7293 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7294 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7295 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7296 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7297 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7298 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7299 dict_add_nr_str(dict, "mode", 0L, mapmode);
7300
7301 vim_free(lhs);
7302 vim_free(mapmode);
7303 }
7304}
7305
7306#ifdef FEAT_FLOAT
7307/*
7308 * "log()" function
7309 */
7310 static void
7311f_log(typval_T *argvars, typval_T *rettv)
7312{
7313 float_T f = 0.0;
7314
7315 rettv->v_type = VAR_FLOAT;
7316 if (get_float_arg(argvars, &f) == OK)
7317 rettv->vval.v_float = log(f);
7318 else
7319 rettv->vval.v_float = 0.0;
7320}
7321
7322/*
7323 * "log10()" function
7324 */
7325 static void
7326f_log10(typval_T *argvars, typval_T *rettv)
7327{
7328 float_T f = 0.0;
7329
7330 rettv->v_type = VAR_FLOAT;
7331 if (get_float_arg(argvars, &f) == OK)
7332 rettv->vval.v_float = log10(f);
7333 else
7334 rettv->vval.v_float = 0.0;
7335}
7336#endif
7337
7338#ifdef FEAT_LUA
7339/*
7340 * "luaeval()" function
7341 */
7342 static void
7343f_luaeval(typval_T *argvars, typval_T *rettv)
7344{
7345 char_u *str;
7346 char_u buf[NUMBUFLEN];
7347
7348 str = get_tv_string_buf(&argvars[0], buf);
7349 do_luaeval(str, argvars + 1, rettv);
7350}
7351#endif
7352
7353/*
7354 * "map()" function
7355 */
7356 static void
7357f_map(typval_T *argvars, typval_T *rettv)
7358{
7359 filter_map(argvars, rettv, TRUE);
7360}
7361
7362/*
7363 * "maparg()" function
7364 */
7365 static void
7366f_maparg(typval_T *argvars, typval_T *rettv)
7367{
7368 get_maparg(argvars, rettv, TRUE);
7369}
7370
7371/*
7372 * "mapcheck()" function
7373 */
7374 static void
7375f_mapcheck(typval_T *argvars, typval_T *rettv)
7376{
7377 get_maparg(argvars, rettv, FALSE);
7378}
7379
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007380typedef enum
7381{
7382 MATCH_END, /* matchend() */
7383 MATCH_MATCH, /* match() */
7384 MATCH_STR, /* matchstr() */
7385 MATCH_LIST, /* matchlist() */
7386 MATCH_POS /* matchstrpos() */
7387} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007388
7389 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007390find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007391{
7392 char_u *str = NULL;
7393 long len = 0;
7394 char_u *expr = NULL;
7395 char_u *pat;
7396 regmatch_T regmatch;
7397 char_u patbuf[NUMBUFLEN];
7398 char_u strbuf[NUMBUFLEN];
7399 char_u *save_cpo;
7400 long start = 0;
7401 long nth = 1;
7402 colnr_T startcol = 0;
7403 int match = 0;
7404 list_T *l = NULL;
7405 listitem_T *li = NULL;
7406 long idx = 0;
7407 char_u *tofree = NULL;
7408
7409 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7410 save_cpo = p_cpo;
7411 p_cpo = (char_u *)"";
7412
7413 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007414 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007415 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007416 /* type MATCH_LIST: return empty list when there are no matches.
7417 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007418 if (rettv_list_alloc(rettv) == FAIL)
7419 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007420 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007421 && (list_append_string(rettv->vval.v_list,
7422 (char_u *)"", 0) == FAIL
7423 || list_append_number(rettv->vval.v_list,
7424 (varnumber_T)-1) == FAIL
7425 || list_append_number(rettv->vval.v_list,
7426 (varnumber_T)-1) == FAIL
7427 || list_append_number(rettv->vval.v_list,
7428 (varnumber_T)-1) == FAIL))
7429 {
7430 list_free(rettv->vval.v_list);
7431 rettv->vval.v_list = NULL;
7432 goto theend;
7433 }
7434 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007435 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007436 {
7437 rettv->v_type = VAR_STRING;
7438 rettv->vval.v_string = NULL;
7439 }
7440
7441 if (argvars[0].v_type == VAR_LIST)
7442 {
7443 if ((l = argvars[0].vval.v_list) == NULL)
7444 goto theend;
7445 li = l->lv_first;
7446 }
7447 else
7448 {
7449 expr = str = get_tv_string(&argvars[0]);
7450 len = (long)STRLEN(str);
7451 }
7452
7453 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7454 if (pat == NULL)
7455 goto theend;
7456
7457 if (argvars[2].v_type != VAR_UNKNOWN)
7458 {
7459 int error = FALSE;
7460
7461 start = (long)get_tv_number_chk(&argvars[2], &error);
7462 if (error)
7463 goto theend;
7464 if (l != NULL)
7465 {
7466 li = list_find(l, start);
7467 if (li == NULL)
7468 goto theend;
7469 idx = l->lv_idx; /* use the cached index */
7470 }
7471 else
7472 {
7473 if (start < 0)
7474 start = 0;
7475 if (start > len)
7476 goto theend;
7477 /* When "count" argument is there ignore matches before "start",
7478 * otherwise skip part of the string. Differs when pattern is "^"
7479 * or "\<". */
7480 if (argvars[3].v_type != VAR_UNKNOWN)
7481 startcol = start;
7482 else
7483 {
7484 str += start;
7485 len -= start;
7486 }
7487 }
7488
7489 if (argvars[3].v_type != VAR_UNKNOWN)
7490 nth = (long)get_tv_number_chk(&argvars[3], &error);
7491 if (error)
7492 goto theend;
7493 }
7494
7495 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7496 if (regmatch.regprog != NULL)
7497 {
7498 regmatch.rm_ic = p_ic;
7499
7500 for (;;)
7501 {
7502 if (l != NULL)
7503 {
7504 if (li == NULL)
7505 {
7506 match = FALSE;
7507 break;
7508 }
7509 vim_free(tofree);
7510 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7511 if (str == NULL)
7512 break;
7513 }
7514
7515 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7516
7517 if (match && --nth <= 0)
7518 break;
7519 if (l == NULL && !match)
7520 break;
7521
7522 /* Advance to just after the match. */
7523 if (l != NULL)
7524 {
7525 li = li->li_next;
7526 ++idx;
7527 }
7528 else
7529 {
7530#ifdef FEAT_MBYTE
7531 startcol = (colnr_T)(regmatch.startp[0]
7532 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7533#else
7534 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7535#endif
7536 if (startcol > (colnr_T)len
7537 || str + startcol <= regmatch.startp[0])
7538 {
7539 match = FALSE;
7540 break;
7541 }
7542 }
7543 }
7544
7545 if (match)
7546 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007547 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007548 {
7549 listitem_T *li1 = rettv->vval.v_list->lv_first;
7550 listitem_T *li2 = li1->li_next;
7551 listitem_T *li3 = li2->li_next;
7552 listitem_T *li4 = li3->li_next;
7553
7554 vim_free(li1->li_tv.vval.v_string);
7555 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7556 (int)(regmatch.endp[0] - regmatch.startp[0]));
7557 li3->li_tv.vval.v_number =
7558 (varnumber_T)(regmatch.startp[0] - expr);
7559 li4->li_tv.vval.v_number =
7560 (varnumber_T)(regmatch.endp[0] - expr);
7561 if (l != NULL)
7562 li2->li_tv.vval.v_number = (varnumber_T)idx;
7563 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007564 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007565 {
7566 int i;
7567
7568 /* return list with matched string and submatches */
7569 for (i = 0; i < NSUBEXP; ++i)
7570 {
7571 if (regmatch.endp[i] == NULL)
7572 {
7573 if (list_append_string(rettv->vval.v_list,
7574 (char_u *)"", 0) == FAIL)
7575 break;
7576 }
7577 else if (list_append_string(rettv->vval.v_list,
7578 regmatch.startp[i],
7579 (int)(regmatch.endp[i] - regmatch.startp[i]))
7580 == FAIL)
7581 break;
7582 }
7583 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007584 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007585 {
7586 /* return matched string */
7587 if (l != NULL)
7588 copy_tv(&li->li_tv, rettv);
7589 else
7590 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7591 (int)(regmatch.endp[0] - regmatch.startp[0]));
7592 }
7593 else if (l != NULL)
7594 rettv->vval.v_number = idx;
7595 else
7596 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007597 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598 rettv->vval.v_number =
7599 (varnumber_T)(regmatch.startp[0] - str);
7600 else
7601 rettv->vval.v_number =
7602 (varnumber_T)(regmatch.endp[0] - str);
7603 rettv->vval.v_number += (varnumber_T)(str - expr);
7604 }
7605 }
7606 vim_regfree(regmatch.regprog);
7607 }
7608
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007609theend:
7610 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007611 /* matchstrpos() without a list: drop the second item. */
7612 listitem_remove(rettv->vval.v_list,
7613 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007614 vim_free(tofree);
7615 p_cpo = save_cpo;
7616}
7617
7618/*
7619 * "match()" function
7620 */
7621 static void
7622f_match(typval_T *argvars, typval_T *rettv)
7623{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007624 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007625}
7626
7627/*
7628 * "matchadd()" function
7629 */
7630 static void
7631f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7632{
7633#ifdef FEAT_SEARCH_EXTRA
7634 char_u buf[NUMBUFLEN];
7635 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7636 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7637 int prio = 10; /* default priority */
7638 int id = -1;
7639 int error = FALSE;
7640 char_u *conceal_char = NULL;
7641
7642 rettv->vval.v_number = -1;
7643
7644 if (grp == NULL || pat == NULL)
7645 return;
7646 if (argvars[2].v_type != VAR_UNKNOWN)
7647 {
7648 prio = (int)get_tv_number_chk(&argvars[2], &error);
7649 if (argvars[3].v_type != VAR_UNKNOWN)
7650 {
7651 id = (int)get_tv_number_chk(&argvars[3], &error);
7652 if (argvars[4].v_type != VAR_UNKNOWN)
7653 {
7654 if (argvars[4].v_type != VAR_DICT)
7655 {
7656 EMSG(_(e_dictreq));
7657 return;
7658 }
7659 if (dict_find(argvars[4].vval.v_dict,
7660 (char_u *)"conceal", -1) != NULL)
7661 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7662 (char_u *)"conceal", FALSE);
7663 }
7664 }
7665 }
7666 if (error == TRUE)
7667 return;
7668 if (id >= 1 && id <= 3)
7669 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007670 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007671 return;
7672 }
7673
7674 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7675 conceal_char);
7676#endif
7677}
7678
7679/*
7680 * "matchaddpos()" function
7681 */
7682 static void
7683f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7684{
7685#ifdef FEAT_SEARCH_EXTRA
7686 char_u buf[NUMBUFLEN];
7687 char_u *group;
7688 int prio = 10;
7689 int id = -1;
7690 int error = FALSE;
7691 list_T *l;
7692 char_u *conceal_char = NULL;
7693
7694 rettv->vval.v_number = -1;
7695
7696 group = get_tv_string_buf_chk(&argvars[0], buf);
7697 if (group == NULL)
7698 return;
7699
7700 if (argvars[1].v_type != VAR_LIST)
7701 {
7702 EMSG2(_(e_listarg), "matchaddpos()");
7703 return;
7704 }
7705 l = argvars[1].vval.v_list;
7706 if (l == NULL)
7707 return;
7708
7709 if (argvars[2].v_type != VAR_UNKNOWN)
7710 {
7711 prio = (int)get_tv_number_chk(&argvars[2], &error);
7712 if (argvars[3].v_type != VAR_UNKNOWN)
7713 {
7714 id = (int)get_tv_number_chk(&argvars[3], &error);
7715 if (argvars[4].v_type != VAR_UNKNOWN)
7716 {
7717 if (argvars[4].v_type != VAR_DICT)
7718 {
7719 EMSG(_(e_dictreq));
7720 return;
7721 }
7722 if (dict_find(argvars[4].vval.v_dict,
7723 (char_u *)"conceal", -1) != NULL)
7724 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7725 (char_u *)"conceal", FALSE);
7726 }
7727 }
7728 }
7729 if (error == TRUE)
7730 return;
7731
7732 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7733 if (id == 1 || id == 2)
7734 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007735 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007736 return;
7737 }
7738
7739 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7740 conceal_char);
7741#endif
7742}
7743
7744/*
7745 * "matcharg()" function
7746 */
7747 static void
7748f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7749{
7750 if (rettv_list_alloc(rettv) == OK)
7751 {
7752#ifdef FEAT_SEARCH_EXTRA
7753 int id = (int)get_tv_number(&argvars[0]);
7754 matchitem_T *m;
7755
7756 if (id >= 1 && id <= 3)
7757 {
7758 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7759 {
7760 list_append_string(rettv->vval.v_list,
7761 syn_id2name(m->hlg_id), -1);
7762 list_append_string(rettv->vval.v_list, m->pattern, -1);
7763 }
7764 else
7765 {
7766 list_append_string(rettv->vval.v_list, NULL, -1);
7767 list_append_string(rettv->vval.v_list, NULL, -1);
7768 }
7769 }
7770#endif
7771 }
7772}
7773
7774/*
7775 * "matchdelete()" function
7776 */
7777 static void
7778f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7779{
7780#ifdef FEAT_SEARCH_EXTRA
7781 rettv->vval.v_number = match_delete(curwin,
7782 (int)get_tv_number(&argvars[0]), TRUE);
7783#endif
7784}
7785
7786/*
7787 * "matchend()" function
7788 */
7789 static void
7790f_matchend(typval_T *argvars, typval_T *rettv)
7791{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007792 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007793}
7794
7795/*
7796 * "matchlist()" function
7797 */
7798 static void
7799f_matchlist(typval_T *argvars, typval_T *rettv)
7800{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007801 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007802}
7803
7804/*
7805 * "matchstr()" function
7806 */
7807 static void
7808f_matchstr(typval_T *argvars, typval_T *rettv)
7809{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007810 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007811}
7812
7813/*
7814 * "matchstrpos()" function
7815 */
7816 static void
7817f_matchstrpos(typval_T *argvars, typval_T *rettv)
7818{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007819 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007820}
7821
7822static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7823
7824 static void
7825max_min(typval_T *argvars, typval_T *rettv, int domax)
7826{
7827 varnumber_T n = 0;
7828 varnumber_T i;
7829 int error = FALSE;
7830
7831 if (argvars[0].v_type == VAR_LIST)
7832 {
7833 list_T *l;
7834 listitem_T *li;
7835
7836 l = argvars[0].vval.v_list;
7837 if (l != NULL)
7838 {
7839 li = l->lv_first;
7840 if (li != NULL)
7841 {
7842 n = get_tv_number_chk(&li->li_tv, &error);
7843 for (;;)
7844 {
7845 li = li->li_next;
7846 if (li == NULL)
7847 break;
7848 i = get_tv_number_chk(&li->li_tv, &error);
7849 if (domax ? i > n : i < n)
7850 n = i;
7851 }
7852 }
7853 }
7854 }
7855 else if (argvars[0].v_type == VAR_DICT)
7856 {
7857 dict_T *d;
7858 int first = TRUE;
7859 hashitem_T *hi;
7860 int todo;
7861
7862 d = argvars[0].vval.v_dict;
7863 if (d != NULL)
7864 {
7865 todo = (int)d->dv_hashtab.ht_used;
7866 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7867 {
7868 if (!HASHITEM_EMPTY(hi))
7869 {
7870 --todo;
7871 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7872 if (first)
7873 {
7874 n = i;
7875 first = FALSE;
7876 }
7877 else if (domax ? i > n : i < n)
7878 n = i;
7879 }
7880 }
7881 }
7882 }
7883 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007884 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007885 rettv->vval.v_number = error ? 0 : n;
7886}
7887
7888/*
7889 * "max()" function
7890 */
7891 static void
7892f_max(typval_T *argvars, typval_T *rettv)
7893{
7894 max_min(argvars, rettv, TRUE);
7895}
7896
7897/*
7898 * "min()" function
7899 */
7900 static void
7901f_min(typval_T *argvars, typval_T *rettv)
7902{
7903 max_min(argvars, rettv, FALSE);
7904}
7905
7906static int mkdir_recurse(char_u *dir, int prot);
7907
7908/*
7909 * Create the directory in which "dir" is located, and higher levels when
7910 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007911 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007912 */
7913 static int
7914mkdir_recurse(char_u *dir, int prot)
7915{
7916 char_u *p;
7917 char_u *updir;
7918 int r = FAIL;
7919
7920 /* Get end of directory name in "dir".
7921 * We're done when it's "/" or "c:/". */
7922 p = gettail_sep(dir);
7923 if (p <= get_past_head(dir))
7924 return OK;
7925
7926 /* If the directory exists we're done. Otherwise: create it.*/
7927 updir = vim_strnsave(dir, (int)(p - dir));
7928 if (updir == NULL)
7929 return FAIL;
7930 if (mch_isdir(updir))
7931 r = OK;
7932 else if (mkdir_recurse(updir, prot) == OK)
7933 r = vim_mkdir_emsg(updir, prot);
7934 vim_free(updir);
7935 return r;
7936}
7937
7938#ifdef vim_mkdir
7939/*
7940 * "mkdir()" function
7941 */
7942 static void
7943f_mkdir(typval_T *argvars, typval_T *rettv)
7944{
7945 char_u *dir;
7946 char_u buf[NUMBUFLEN];
7947 int prot = 0755;
7948
7949 rettv->vval.v_number = FAIL;
7950 if (check_restricted() || check_secure())
7951 return;
7952
7953 dir = get_tv_string_buf(&argvars[0], buf);
7954 if (*dir == NUL)
7955 rettv->vval.v_number = FAIL;
7956 else
7957 {
7958 if (*gettail(dir) == NUL)
7959 /* remove trailing slashes */
7960 *gettail_sep(dir) = NUL;
7961
7962 if (argvars[1].v_type != VAR_UNKNOWN)
7963 {
7964 if (argvars[2].v_type != VAR_UNKNOWN)
7965 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7966 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7967 mkdir_recurse(dir, prot);
7968 }
7969 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7970 }
7971}
7972#endif
7973
7974/*
7975 * "mode()" function
7976 */
7977 static void
7978f_mode(typval_T *argvars, typval_T *rettv)
7979{
7980 char_u buf[3];
7981
7982 buf[1] = NUL;
7983 buf[2] = NUL;
7984
7985 if (time_for_testing == 93784)
7986 {
7987 /* Testing the two-character code. */
7988 buf[0] = 'x';
7989 buf[1] = '!';
7990 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02007991#ifdef FEAT_TERMINAL
7992 else if (term_use_loop())
7993 buf[0] = 't';
7994#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007995 else if (VIsual_active)
7996 {
7997 if (VIsual_select)
7998 buf[0] = VIsual_mode + 's' - 'v';
7999 else
8000 buf[0] = VIsual_mode;
8001 }
8002 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8003 || State == CONFIRM)
8004 {
8005 buf[0] = 'r';
8006 if (State == ASKMORE)
8007 buf[1] = 'm';
8008 else if (State == CONFIRM)
8009 buf[1] = '?';
8010 }
8011 else if (State == EXTERNCMD)
8012 buf[0] = '!';
8013 else if (State & INSERT)
8014 {
8015#ifdef FEAT_VREPLACE
8016 if (State & VREPLACE_FLAG)
8017 {
8018 buf[0] = 'R';
8019 buf[1] = 'v';
8020 }
8021 else
8022#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008023 {
8024 if (State & REPLACE_FLAG)
8025 buf[0] = 'R';
8026 else
8027 buf[0] = 'i';
8028#ifdef FEAT_INS_EXPAND
8029 if (ins_compl_active())
8030 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008031 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008032 buf[1] = 'x';
8033#endif
8034 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008035 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008036 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008037 {
8038 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008039 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008040 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008041 else if (exmode_active == EXMODE_NORMAL)
8042 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008043 }
8044 else
8045 {
8046 buf[0] = 'n';
8047 if (finish_op)
8048 buf[1] = 'o';
8049 }
8050
8051 /* Clear out the minor mode when the argument is not a non-zero number or
8052 * non-empty string. */
8053 if (!non_zero_arg(&argvars[0]))
8054 buf[1] = NUL;
8055
8056 rettv->vval.v_string = vim_strsave(buf);
8057 rettv->v_type = VAR_STRING;
8058}
8059
8060#if defined(FEAT_MZSCHEME) || defined(PROTO)
8061/*
8062 * "mzeval()" function
8063 */
8064 static void
8065f_mzeval(typval_T *argvars, typval_T *rettv)
8066{
8067 char_u *str;
8068 char_u buf[NUMBUFLEN];
8069
8070 str = get_tv_string_buf(&argvars[0], buf);
8071 do_mzeval(str, rettv);
8072}
8073
8074 void
8075mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8076{
8077 typval_T argvars[3];
8078
8079 argvars[0].v_type = VAR_STRING;
8080 argvars[0].vval.v_string = name;
8081 copy_tv(args, &argvars[1]);
8082 argvars[2].v_type = VAR_UNKNOWN;
8083 f_call(argvars, rettv);
8084 clear_tv(&argvars[1]);
8085}
8086#endif
8087
8088/*
8089 * "nextnonblank()" function
8090 */
8091 static void
8092f_nextnonblank(typval_T *argvars, typval_T *rettv)
8093{
8094 linenr_T lnum;
8095
8096 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8097 {
8098 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8099 {
8100 lnum = 0;
8101 break;
8102 }
8103 if (*skipwhite(ml_get(lnum)) != NUL)
8104 break;
8105 }
8106 rettv->vval.v_number = lnum;
8107}
8108
8109/*
8110 * "nr2char()" function
8111 */
8112 static void
8113f_nr2char(typval_T *argvars, typval_T *rettv)
8114{
8115 char_u buf[NUMBUFLEN];
8116
8117#ifdef FEAT_MBYTE
8118 if (has_mbyte)
8119 {
8120 int utf8 = 0;
8121
8122 if (argvars[1].v_type != VAR_UNKNOWN)
8123 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8124 if (utf8)
8125 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8126 else
8127 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8128 }
8129 else
8130#endif
8131 {
8132 buf[0] = (char_u)get_tv_number(&argvars[0]);
8133 buf[1] = NUL;
8134 }
8135 rettv->v_type = VAR_STRING;
8136 rettv->vval.v_string = vim_strsave(buf);
8137}
8138
8139/*
8140 * "or(expr, expr)" function
8141 */
8142 static void
8143f_or(typval_T *argvars, typval_T *rettv)
8144{
8145 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8146 | get_tv_number_chk(&argvars[1], NULL);
8147}
8148
8149/*
8150 * "pathshorten()" function
8151 */
8152 static void
8153f_pathshorten(typval_T *argvars, typval_T *rettv)
8154{
8155 char_u *p;
8156
8157 rettv->v_type = VAR_STRING;
8158 p = get_tv_string_chk(&argvars[0]);
8159 if (p == NULL)
8160 rettv->vval.v_string = NULL;
8161 else
8162 {
8163 p = vim_strsave(p);
8164 rettv->vval.v_string = p;
8165 if (p != NULL)
8166 shorten_dir(p);
8167 }
8168}
8169
8170#ifdef FEAT_PERL
8171/*
8172 * "perleval()" function
8173 */
8174 static void
8175f_perleval(typval_T *argvars, typval_T *rettv)
8176{
8177 char_u *str;
8178 char_u buf[NUMBUFLEN];
8179
8180 str = get_tv_string_buf(&argvars[0], buf);
8181 do_perleval(str, rettv);
8182}
8183#endif
8184
8185#ifdef FEAT_FLOAT
8186/*
8187 * "pow()" function
8188 */
8189 static void
8190f_pow(typval_T *argvars, typval_T *rettv)
8191{
8192 float_T fx = 0.0, fy = 0.0;
8193
8194 rettv->v_type = VAR_FLOAT;
8195 if (get_float_arg(argvars, &fx) == OK
8196 && get_float_arg(&argvars[1], &fy) == OK)
8197 rettv->vval.v_float = pow(fx, fy);
8198 else
8199 rettv->vval.v_float = 0.0;
8200}
8201#endif
8202
8203/*
8204 * "prevnonblank()" function
8205 */
8206 static void
8207f_prevnonblank(typval_T *argvars, typval_T *rettv)
8208{
8209 linenr_T lnum;
8210
8211 lnum = get_tv_lnum(argvars);
8212 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8213 lnum = 0;
8214 else
8215 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8216 --lnum;
8217 rettv->vval.v_number = lnum;
8218}
8219
8220/* This dummy va_list is here because:
8221 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8222 * - locally in the function results in a "used before set" warning
8223 * - using va_start() to initialize it gives "function with fixed args" error */
8224static va_list ap;
8225
8226/*
8227 * "printf()" function
8228 */
8229 static void
8230f_printf(typval_T *argvars, typval_T *rettv)
8231{
8232 char_u buf[NUMBUFLEN];
8233 int len;
8234 char_u *s;
8235 int saved_did_emsg = did_emsg;
8236 char *fmt;
8237
8238 rettv->v_type = VAR_STRING;
8239 rettv->vval.v_string = NULL;
8240
8241 /* Get the required length, allocate the buffer and do it for real. */
8242 did_emsg = FALSE;
8243 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008244 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008245 if (!did_emsg)
8246 {
8247 s = alloc(len + 1);
8248 if (s != NULL)
8249 {
8250 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008251 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8252 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008253 }
8254 }
8255 did_emsg |= saved_did_emsg;
8256}
8257
8258/*
8259 * "pumvisible()" function
8260 */
8261 static void
8262f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8263{
8264#ifdef FEAT_INS_EXPAND
8265 if (pum_visible())
8266 rettv->vval.v_number = 1;
8267#endif
8268}
8269
8270#ifdef FEAT_PYTHON3
8271/*
8272 * "py3eval()" function
8273 */
8274 static void
8275f_py3eval(typval_T *argvars, typval_T *rettv)
8276{
8277 char_u *str;
8278 char_u buf[NUMBUFLEN];
8279
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008280 if (p_pyx == 0)
8281 p_pyx = 3;
8282
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008283 str = get_tv_string_buf(&argvars[0], buf);
8284 do_py3eval(str, rettv);
8285}
8286#endif
8287
8288#ifdef FEAT_PYTHON
8289/*
8290 * "pyeval()" function
8291 */
8292 static void
8293f_pyeval(typval_T *argvars, typval_T *rettv)
8294{
8295 char_u *str;
8296 char_u buf[NUMBUFLEN];
8297
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008298 if (p_pyx == 0)
8299 p_pyx = 2;
8300
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008301 str = get_tv_string_buf(&argvars[0], buf);
8302 do_pyeval(str, rettv);
8303}
8304#endif
8305
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008306#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8307/*
8308 * "pyxeval()" function
8309 */
8310 static void
8311f_pyxeval(typval_T *argvars, typval_T *rettv)
8312{
8313# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8314 init_pyxversion();
8315 if (p_pyx == 2)
8316 f_pyeval(argvars, rettv);
8317 else
8318 f_py3eval(argvars, rettv);
8319# elif defined(FEAT_PYTHON)
8320 f_pyeval(argvars, rettv);
8321# elif defined(FEAT_PYTHON3)
8322 f_py3eval(argvars, rettv);
8323# endif
8324}
8325#endif
8326
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008327/*
8328 * "range()" function
8329 */
8330 static void
8331f_range(typval_T *argvars, typval_T *rettv)
8332{
8333 varnumber_T start;
8334 varnumber_T end;
8335 varnumber_T stride = 1;
8336 varnumber_T i;
8337 int error = FALSE;
8338
8339 start = get_tv_number_chk(&argvars[0], &error);
8340 if (argvars[1].v_type == VAR_UNKNOWN)
8341 {
8342 end = start - 1;
8343 start = 0;
8344 }
8345 else
8346 {
8347 end = get_tv_number_chk(&argvars[1], &error);
8348 if (argvars[2].v_type != VAR_UNKNOWN)
8349 stride = get_tv_number_chk(&argvars[2], &error);
8350 }
8351
8352 if (error)
8353 return; /* type error; errmsg already given */
8354 if (stride == 0)
8355 EMSG(_("E726: Stride is zero"));
8356 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8357 EMSG(_("E727: Start past end"));
8358 else
8359 {
8360 if (rettv_list_alloc(rettv) == OK)
8361 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8362 if (list_append_number(rettv->vval.v_list,
8363 (varnumber_T)i) == FAIL)
8364 break;
8365 }
8366}
8367
8368/*
8369 * "readfile()" function
8370 */
8371 static void
8372f_readfile(typval_T *argvars, typval_T *rettv)
8373{
8374 int binary = FALSE;
8375 int failed = FALSE;
8376 char_u *fname;
8377 FILE *fd;
8378 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8379 int io_size = sizeof(buf);
8380 int readlen; /* size of last fread() */
8381 char_u *prev = NULL; /* previously read bytes, if any */
8382 long prevlen = 0; /* length of data in prev */
8383 long prevsize = 0; /* size of prev buffer */
8384 long maxline = MAXLNUM;
8385 long cnt = 0;
8386 char_u *p; /* position in buf */
8387 char_u *start; /* start of current line */
8388
8389 if (argvars[1].v_type != VAR_UNKNOWN)
8390 {
8391 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8392 binary = TRUE;
8393 if (argvars[2].v_type != VAR_UNKNOWN)
8394 maxline = (long)get_tv_number(&argvars[2]);
8395 }
8396
8397 if (rettv_list_alloc(rettv) == FAIL)
8398 return;
8399
8400 /* Always open the file in binary mode, library functions have a mind of
8401 * their own about CR-LF conversion. */
8402 fname = get_tv_string(&argvars[0]);
8403 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8404 {
8405 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8406 return;
8407 }
8408
8409 while (cnt < maxline || maxline < 0)
8410 {
8411 readlen = (int)fread(buf, 1, io_size, fd);
8412
8413 /* This for loop processes what was read, but is also entered at end
8414 * of file so that either:
8415 * - an incomplete line gets written
8416 * - a "binary" file gets an empty line at the end if it ends in a
8417 * newline. */
8418 for (p = buf, start = buf;
8419 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8420 ++p)
8421 {
8422 if (*p == '\n' || readlen <= 0)
8423 {
8424 listitem_T *li;
8425 char_u *s = NULL;
8426 long_u len = p - start;
8427
8428 /* Finished a line. Remove CRs before NL. */
8429 if (readlen > 0 && !binary)
8430 {
8431 while (len > 0 && start[len - 1] == '\r')
8432 --len;
8433 /* removal may cross back to the "prev" string */
8434 if (len == 0)
8435 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8436 --prevlen;
8437 }
8438 if (prevlen == 0)
8439 s = vim_strnsave(start, (int)len);
8440 else
8441 {
8442 /* Change "prev" buffer to be the right size. This way
8443 * the bytes are only copied once, and very long lines are
8444 * allocated only once. */
8445 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8446 {
8447 mch_memmove(s + prevlen, start, len);
8448 s[prevlen + len] = NUL;
8449 prev = NULL; /* the list will own the string */
8450 prevlen = prevsize = 0;
8451 }
8452 }
8453 if (s == NULL)
8454 {
8455 do_outofmem_msg((long_u) prevlen + len + 1);
8456 failed = TRUE;
8457 break;
8458 }
8459
8460 if ((li = listitem_alloc()) == NULL)
8461 {
8462 vim_free(s);
8463 failed = TRUE;
8464 break;
8465 }
8466 li->li_tv.v_type = VAR_STRING;
8467 li->li_tv.v_lock = 0;
8468 li->li_tv.vval.v_string = s;
8469 list_append(rettv->vval.v_list, li);
8470
8471 start = p + 1; /* step over newline */
8472 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8473 break;
8474 }
8475 else if (*p == NUL)
8476 *p = '\n';
8477#ifdef FEAT_MBYTE
8478 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8479 * when finding the BF and check the previous two bytes. */
8480 else if (*p == 0xbf && enc_utf8 && !binary)
8481 {
8482 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8483 * + 1, these may be in the "prev" string. */
8484 char_u back1 = p >= buf + 1 ? p[-1]
8485 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8486 char_u back2 = p >= buf + 2 ? p[-2]
8487 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8488 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8489
8490 if (back2 == 0xef && back1 == 0xbb)
8491 {
8492 char_u *dest = p - 2;
8493
8494 /* Usually a BOM is at the beginning of a file, and so at
8495 * the beginning of a line; then we can just step over it.
8496 */
8497 if (start == dest)
8498 start = p + 1;
8499 else
8500 {
8501 /* have to shuffle buf to close gap */
8502 int adjust_prevlen = 0;
8503
8504 if (dest < buf)
8505 {
8506 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8507 dest = buf;
8508 }
8509 if (readlen > p - buf + 1)
8510 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8511 readlen -= 3 - adjust_prevlen;
8512 prevlen -= adjust_prevlen;
8513 p = dest - 1;
8514 }
8515 }
8516 }
8517#endif
8518 } /* for */
8519
8520 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8521 break;
8522 if (start < p)
8523 {
8524 /* There's part of a line in buf, store it in "prev". */
8525 if (p - start + prevlen >= prevsize)
8526 {
8527 /* need bigger "prev" buffer */
8528 char_u *newprev;
8529
8530 /* A common use case is ordinary text files and "prev" gets a
8531 * fragment of a line, so the first allocation is made
8532 * small, to avoid repeatedly 'allocing' large and
8533 * 'reallocing' small. */
8534 if (prevsize == 0)
8535 prevsize = (long)(p - start);
8536 else
8537 {
8538 long grow50pc = (prevsize * 3) / 2;
8539 long growmin = (long)((p - start) * 2 + prevlen);
8540 prevsize = grow50pc > growmin ? grow50pc : growmin;
8541 }
8542 newprev = prev == NULL ? alloc(prevsize)
8543 : vim_realloc(prev, prevsize);
8544 if (newprev == NULL)
8545 {
8546 do_outofmem_msg((long_u)prevsize);
8547 failed = TRUE;
8548 break;
8549 }
8550 prev = newprev;
8551 }
8552 /* Add the line part to end of "prev". */
8553 mch_memmove(prev + prevlen, start, p - start);
8554 prevlen += (long)(p - start);
8555 }
8556 } /* while */
8557
8558 /*
8559 * For a negative line count use only the lines at the end of the file,
8560 * free the rest.
8561 */
8562 if (!failed && maxline < 0)
8563 while (cnt > -maxline)
8564 {
8565 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8566 --cnt;
8567 }
8568
8569 if (failed)
8570 {
8571 list_free(rettv->vval.v_list);
8572 /* readfile doc says an empty list is returned on error */
8573 rettv->vval.v_list = list_alloc();
8574 }
8575
8576 vim_free(prev);
8577 fclose(fd);
8578}
8579
8580#if defined(FEAT_RELTIME)
8581static int list2proftime(typval_T *arg, proftime_T *tm);
8582
8583/*
8584 * Convert a List to proftime_T.
8585 * Return FAIL when there is something wrong.
8586 */
8587 static int
8588list2proftime(typval_T *arg, proftime_T *tm)
8589{
8590 long n1, n2;
8591 int error = FALSE;
8592
8593 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8594 || arg->vval.v_list->lv_len != 2)
8595 return FAIL;
8596 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8597 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8598# ifdef WIN3264
8599 tm->HighPart = n1;
8600 tm->LowPart = n2;
8601# else
8602 tm->tv_sec = n1;
8603 tm->tv_usec = n2;
8604# endif
8605 return error ? FAIL : OK;
8606}
8607#endif /* FEAT_RELTIME */
8608
8609/*
8610 * "reltime()" function
8611 */
8612 static void
8613f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8614{
8615#ifdef FEAT_RELTIME
8616 proftime_T res;
8617 proftime_T start;
8618
8619 if (argvars[0].v_type == VAR_UNKNOWN)
8620 {
8621 /* No arguments: get current time. */
8622 profile_start(&res);
8623 }
8624 else if (argvars[1].v_type == VAR_UNKNOWN)
8625 {
8626 if (list2proftime(&argvars[0], &res) == FAIL)
8627 return;
8628 profile_end(&res);
8629 }
8630 else
8631 {
8632 /* Two arguments: compute the difference. */
8633 if (list2proftime(&argvars[0], &start) == FAIL
8634 || list2proftime(&argvars[1], &res) == FAIL)
8635 return;
8636 profile_sub(&res, &start);
8637 }
8638
8639 if (rettv_list_alloc(rettv) == OK)
8640 {
8641 long n1, n2;
8642
8643# ifdef WIN3264
8644 n1 = res.HighPart;
8645 n2 = res.LowPart;
8646# else
8647 n1 = res.tv_sec;
8648 n2 = res.tv_usec;
8649# endif
8650 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8651 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8652 }
8653#endif
8654}
8655
8656#ifdef FEAT_FLOAT
8657/*
8658 * "reltimefloat()" function
8659 */
8660 static void
8661f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8662{
8663# ifdef FEAT_RELTIME
8664 proftime_T tm;
8665# endif
8666
8667 rettv->v_type = VAR_FLOAT;
8668 rettv->vval.v_float = 0;
8669# ifdef FEAT_RELTIME
8670 if (list2proftime(&argvars[0], &tm) == OK)
8671 rettv->vval.v_float = profile_float(&tm);
8672# endif
8673}
8674#endif
8675
8676/*
8677 * "reltimestr()" function
8678 */
8679 static void
8680f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8681{
8682#ifdef FEAT_RELTIME
8683 proftime_T tm;
8684#endif
8685
8686 rettv->v_type = VAR_STRING;
8687 rettv->vval.v_string = NULL;
8688#ifdef FEAT_RELTIME
8689 if (list2proftime(&argvars[0], &tm) == OK)
8690 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8691#endif
8692}
8693
8694#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8695static void make_connection(void);
8696static int check_connection(void);
8697
8698 static void
8699make_connection(void)
8700{
8701 if (X_DISPLAY == NULL
8702# ifdef FEAT_GUI
8703 && !gui.in_use
8704# endif
8705 )
8706 {
8707 x_force_connect = TRUE;
8708 setup_term_clip();
8709 x_force_connect = FALSE;
8710 }
8711}
8712
8713 static int
8714check_connection(void)
8715{
8716 make_connection();
8717 if (X_DISPLAY == NULL)
8718 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008719 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008720 return FAIL;
8721 }
8722 return OK;
8723}
8724#endif
8725
8726#ifdef FEAT_CLIENTSERVER
8727 static void
8728remote_common(typval_T *argvars, typval_T *rettv, int expr)
8729{
8730 char_u *server_name;
8731 char_u *keys;
8732 char_u *r = NULL;
8733 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008734 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008735# ifdef WIN32
8736 HWND w;
8737# else
8738 Window w;
8739# endif
8740
8741 if (check_restricted() || check_secure())
8742 return;
8743
8744# ifdef FEAT_X11
8745 if (check_connection() == FAIL)
8746 return;
8747# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008748 if (argvars[2].v_type != VAR_UNKNOWN
8749 && argvars[3].v_type != VAR_UNKNOWN)
8750 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008751
8752 server_name = get_tv_string_chk(&argvars[0]);
8753 if (server_name == NULL)
8754 return; /* type error; errmsg already given */
8755 keys = get_tv_string_buf(&argvars[1], buf);
8756# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008757 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008758# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008759 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8760 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008761# endif
8762 {
8763 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008764 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008765 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008766 vim_free(r);
8767 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008768 else
8769 EMSG2(_("E241: Unable to send to %s"), server_name);
8770 return;
8771 }
8772
8773 rettv->vval.v_string = r;
8774
8775 if (argvars[2].v_type != VAR_UNKNOWN)
8776 {
8777 dictitem_T v;
8778 char_u str[30];
8779 char_u *idvar;
8780
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008781 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008782 if (idvar != NULL && *idvar != NUL)
8783 {
8784 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8785 v.di_tv.v_type = VAR_STRING;
8786 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008787 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008788 vim_free(v.di_tv.vval.v_string);
8789 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008790 }
8791}
8792#endif
8793
8794/*
8795 * "remote_expr()" function
8796 */
8797 static void
8798f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8799{
8800 rettv->v_type = VAR_STRING;
8801 rettv->vval.v_string = NULL;
8802#ifdef FEAT_CLIENTSERVER
8803 remote_common(argvars, rettv, TRUE);
8804#endif
8805}
8806
8807/*
8808 * "remote_foreground()" function
8809 */
8810 static void
8811f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8812{
8813#ifdef FEAT_CLIENTSERVER
8814# ifdef WIN32
8815 /* On Win32 it's done in this application. */
8816 {
8817 char_u *server_name = get_tv_string_chk(&argvars[0]);
8818
8819 if (server_name != NULL)
8820 serverForeground(server_name);
8821 }
8822# else
8823 /* Send a foreground() expression to the server. */
8824 argvars[1].v_type = VAR_STRING;
8825 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8826 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008827 rettv->v_type = VAR_STRING;
8828 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008829 remote_common(argvars, rettv, TRUE);
8830 vim_free(argvars[1].vval.v_string);
8831# endif
8832#endif
8833}
8834
8835 static void
8836f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8837{
8838#ifdef FEAT_CLIENTSERVER
8839 dictitem_T v;
8840 char_u *s = NULL;
8841# ifdef WIN32
8842 long_u n = 0;
8843# endif
8844 char_u *serverid;
8845
8846 if (check_restricted() || check_secure())
8847 {
8848 rettv->vval.v_number = -1;
8849 return;
8850 }
8851 serverid = get_tv_string_chk(&argvars[0]);
8852 if (serverid == NULL)
8853 {
8854 rettv->vval.v_number = -1;
8855 return; /* type error; errmsg already given */
8856 }
8857# ifdef WIN32
8858 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8859 if (n == 0)
8860 rettv->vval.v_number = -1;
8861 else
8862 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008863 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008864 rettv->vval.v_number = (s != NULL);
8865 }
8866# else
8867 if (check_connection() == FAIL)
8868 return;
8869
8870 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8871 serverStrToWin(serverid), &s);
8872# endif
8873
8874 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8875 {
8876 char_u *retvar;
8877
8878 v.di_tv.v_type = VAR_STRING;
8879 v.di_tv.vval.v_string = vim_strsave(s);
8880 retvar = get_tv_string_chk(&argvars[1]);
8881 if (retvar != NULL)
8882 set_var(retvar, &v.di_tv, FALSE);
8883 vim_free(v.di_tv.vval.v_string);
8884 }
8885#else
8886 rettv->vval.v_number = -1;
8887#endif
8888}
8889
8890 static void
8891f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8892{
8893 char_u *r = NULL;
8894
8895#ifdef FEAT_CLIENTSERVER
8896 char_u *serverid = get_tv_string_chk(&argvars[0]);
8897
8898 if (serverid != NULL && !check_restricted() && !check_secure())
8899 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008900 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008901# ifdef WIN32
8902 /* The server's HWND is encoded in the 'id' parameter */
8903 long_u n = 0;
8904# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008905
8906 if (argvars[1].v_type != VAR_UNKNOWN)
8907 timeout = get_tv_number(&argvars[1]);
8908
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008909# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008910 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8911 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008912 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008913 if (r == NULL)
8914# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008915 if (check_connection() == FAIL
8916 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8917 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008918# endif
8919 EMSG(_("E277: Unable to read a server reply"));
8920 }
8921#endif
8922 rettv->v_type = VAR_STRING;
8923 rettv->vval.v_string = r;
8924}
8925
8926/*
8927 * "remote_send()" function
8928 */
8929 static void
8930f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8931{
8932 rettv->v_type = VAR_STRING;
8933 rettv->vval.v_string = NULL;
8934#ifdef FEAT_CLIENTSERVER
8935 remote_common(argvars, rettv, FALSE);
8936#endif
8937}
8938
8939/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008940 * "remote_startserver()" function
8941 */
8942 static void
8943f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8944{
8945#ifdef FEAT_CLIENTSERVER
8946 char_u *server = get_tv_string_chk(&argvars[0]);
8947
8948 if (server == NULL)
8949 return; /* type error; errmsg already given */
8950 if (serverName != NULL)
8951 EMSG(_("E941: already started a server"));
8952 else
8953 {
8954# ifdef FEAT_X11
8955 if (check_connection() == OK)
8956 serverRegisterName(X_DISPLAY, server);
8957# else
8958 serverSetName(server);
8959# endif
8960 }
8961#else
8962 EMSG(_("E942: +clientserver feature not available"));
8963#endif
8964}
8965
8966/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008967 * "remove()" function
8968 */
8969 static void
8970f_remove(typval_T *argvars, typval_T *rettv)
8971{
8972 list_T *l;
8973 listitem_T *item, *item2;
8974 listitem_T *li;
8975 long idx;
8976 long end;
8977 char_u *key;
8978 dict_T *d;
8979 dictitem_T *di;
8980 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8981
8982 if (argvars[0].v_type == VAR_DICT)
8983 {
8984 if (argvars[2].v_type != VAR_UNKNOWN)
8985 EMSG2(_(e_toomanyarg), "remove()");
8986 else if ((d = argvars[0].vval.v_dict) != NULL
8987 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
8988 {
8989 key = get_tv_string_chk(&argvars[1]);
8990 if (key != NULL)
8991 {
8992 di = dict_find(d, key, -1);
8993 if (di == NULL)
8994 EMSG2(_(e_dictkey), key);
8995 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
8996 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
8997 {
8998 *rettv = di->di_tv;
8999 init_tv(&di->di_tv);
9000 dictitem_remove(d, di);
9001 }
9002 }
9003 }
9004 }
9005 else if (argvars[0].v_type != VAR_LIST)
9006 EMSG2(_(e_listdictarg), "remove()");
9007 else if ((l = argvars[0].vval.v_list) != NULL
9008 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9009 {
9010 int error = FALSE;
9011
9012 idx = (long)get_tv_number_chk(&argvars[1], &error);
9013 if (error)
9014 ; /* type error: do nothing, errmsg already given */
9015 else if ((item = list_find(l, idx)) == NULL)
9016 EMSGN(_(e_listidx), idx);
9017 else
9018 {
9019 if (argvars[2].v_type == VAR_UNKNOWN)
9020 {
9021 /* Remove one item, return its value. */
9022 vimlist_remove(l, item, item);
9023 *rettv = item->li_tv;
9024 vim_free(item);
9025 }
9026 else
9027 {
9028 /* Remove range of items, return list with values. */
9029 end = (long)get_tv_number_chk(&argvars[2], &error);
9030 if (error)
9031 ; /* type error: do nothing */
9032 else if ((item2 = list_find(l, end)) == NULL)
9033 EMSGN(_(e_listidx), end);
9034 else
9035 {
9036 int cnt = 0;
9037
9038 for (li = item; li != NULL; li = li->li_next)
9039 {
9040 ++cnt;
9041 if (li == item2)
9042 break;
9043 }
9044 if (li == NULL) /* didn't find "item2" after "item" */
9045 EMSG(_(e_invrange));
9046 else
9047 {
9048 vimlist_remove(l, item, item2);
9049 if (rettv_list_alloc(rettv) == OK)
9050 {
9051 l = rettv->vval.v_list;
9052 l->lv_first = item;
9053 l->lv_last = item2;
9054 item->li_prev = NULL;
9055 item2->li_next = NULL;
9056 l->lv_len = cnt;
9057 }
9058 }
9059 }
9060 }
9061 }
9062 }
9063}
9064
9065/*
9066 * "rename({from}, {to})" function
9067 */
9068 static void
9069f_rename(typval_T *argvars, typval_T *rettv)
9070{
9071 char_u buf[NUMBUFLEN];
9072
9073 if (check_restricted() || check_secure())
9074 rettv->vval.v_number = -1;
9075 else
9076 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9077 get_tv_string_buf(&argvars[1], buf));
9078}
9079
9080/*
9081 * "repeat()" function
9082 */
9083 static void
9084f_repeat(typval_T *argvars, typval_T *rettv)
9085{
9086 char_u *p;
9087 int n;
9088 int slen;
9089 int len;
9090 char_u *r;
9091 int i;
9092
9093 n = (int)get_tv_number(&argvars[1]);
9094 if (argvars[0].v_type == VAR_LIST)
9095 {
9096 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9097 while (n-- > 0)
9098 if (list_extend(rettv->vval.v_list,
9099 argvars[0].vval.v_list, NULL) == FAIL)
9100 break;
9101 }
9102 else
9103 {
9104 p = get_tv_string(&argvars[0]);
9105 rettv->v_type = VAR_STRING;
9106 rettv->vval.v_string = NULL;
9107
9108 slen = (int)STRLEN(p);
9109 len = slen * n;
9110 if (len <= 0)
9111 return;
9112
9113 r = alloc(len + 1);
9114 if (r != NULL)
9115 {
9116 for (i = 0; i < n; i++)
9117 mch_memmove(r + i * slen, p, (size_t)slen);
9118 r[len] = NUL;
9119 }
9120
9121 rettv->vval.v_string = r;
9122 }
9123}
9124
9125/*
9126 * "resolve()" function
9127 */
9128 static void
9129f_resolve(typval_T *argvars, typval_T *rettv)
9130{
9131 char_u *p;
9132#ifdef HAVE_READLINK
9133 char_u *buf = NULL;
9134#endif
9135
9136 p = get_tv_string(&argvars[0]);
9137#ifdef FEAT_SHORTCUT
9138 {
9139 char_u *v = NULL;
9140
9141 v = mch_resolve_shortcut(p);
9142 if (v != NULL)
9143 rettv->vval.v_string = v;
9144 else
9145 rettv->vval.v_string = vim_strsave(p);
9146 }
9147#else
9148# ifdef HAVE_READLINK
9149 {
9150 char_u *cpy;
9151 int len;
9152 char_u *remain = NULL;
9153 char_u *q;
9154 int is_relative_to_current = FALSE;
9155 int has_trailing_pathsep = FALSE;
9156 int limit = 100;
9157
9158 p = vim_strsave(p);
9159
9160 if (p[0] == '.' && (vim_ispathsep(p[1])
9161 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9162 is_relative_to_current = TRUE;
9163
9164 len = STRLEN(p);
9165 if (len > 0 && after_pathsep(p, p + len))
9166 {
9167 has_trailing_pathsep = TRUE;
9168 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9169 }
9170
9171 q = getnextcomp(p);
9172 if (*q != NUL)
9173 {
9174 /* Separate the first path component in "p", and keep the
9175 * remainder (beginning with the path separator). */
9176 remain = vim_strsave(q - 1);
9177 q[-1] = NUL;
9178 }
9179
9180 buf = alloc(MAXPATHL + 1);
9181 if (buf == NULL)
9182 goto fail;
9183
9184 for (;;)
9185 {
9186 for (;;)
9187 {
9188 len = readlink((char *)p, (char *)buf, MAXPATHL);
9189 if (len <= 0)
9190 break;
9191 buf[len] = NUL;
9192
9193 if (limit-- == 0)
9194 {
9195 vim_free(p);
9196 vim_free(remain);
9197 EMSG(_("E655: Too many symbolic links (cycle?)"));
9198 rettv->vval.v_string = NULL;
9199 goto fail;
9200 }
9201
9202 /* Ensure that the result will have a trailing path separator
9203 * if the argument has one. */
9204 if (remain == NULL && has_trailing_pathsep)
9205 add_pathsep(buf);
9206
9207 /* Separate the first path component in the link value and
9208 * concatenate the remainders. */
9209 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9210 if (*q != NUL)
9211 {
9212 if (remain == NULL)
9213 remain = vim_strsave(q - 1);
9214 else
9215 {
9216 cpy = concat_str(q - 1, remain);
9217 if (cpy != NULL)
9218 {
9219 vim_free(remain);
9220 remain = cpy;
9221 }
9222 }
9223 q[-1] = NUL;
9224 }
9225
9226 q = gettail(p);
9227 if (q > p && *q == NUL)
9228 {
9229 /* Ignore trailing path separator. */
9230 q[-1] = NUL;
9231 q = gettail(p);
9232 }
9233 if (q > p && !mch_isFullName(buf))
9234 {
9235 /* symlink is relative to directory of argument */
9236 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9237 if (cpy != NULL)
9238 {
9239 STRCPY(cpy, p);
9240 STRCPY(gettail(cpy), buf);
9241 vim_free(p);
9242 p = cpy;
9243 }
9244 }
9245 else
9246 {
9247 vim_free(p);
9248 p = vim_strsave(buf);
9249 }
9250 }
9251
9252 if (remain == NULL)
9253 break;
9254
9255 /* Append the first path component of "remain" to "p". */
9256 q = getnextcomp(remain + 1);
9257 len = q - remain - (*q != NUL);
9258 cpy = vim_strnsave(p, STRLEN(p) + len);
9259 if (cpy != NULL)
9260 {
9261 STRNCAT(cpy, remain, len);
9262 vim_free(p);
9263 p = cpy;
9264 }
9265 /* Shorten "remain". */
9266 if (*q != NUL)
9267 STRMOVE(remain, q - 1);
9268 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009269 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009270 }
9271
9272 /* If the result is a relative path name, make it explicitly relative to
9273 * the current directory if and only if the argument had this form. */
9274 if (!vim_ispathsep(*p))
9275 {
9276 if (is_relative_to_current
9277 && *p != NUL
9278 && !(p[0] == '.'
9279 && (p[1] == NUL
9280 || vim_ispathsep(p[1])
9281 || (p[1] == '.'
9282 && (p[2] == NUL
9283 || vim_ispathsep(p[2]))))))
9284 {
9285 /* Prepend "./". */
9286 cpy = concat_str((char_u *)"./", p);
9287 if (cpy != NULL)
9288 {
9289 vim_free(p);
9290 p = cpy;
9291 }
9292 }
9293 else if (!is_relative_to_current)
9294 {
9295 /* Strip leading "./". */
9296 q = p;
9297 while (q[0] == '.' && vim_ispathsep(q[1]))
9298 q += 2;
9299 if (q > p)
9300 STRMOVE(p, p + 2);
9301 }
9302 }
9303
9304 /* Ensure that the result will have no trailing path separator
9305 * if the argument had none. But keep "/" or "//". */
9306 if (!has_trailing_pathsep)
9307 {
9308 q = p + STRLEN(p);
9309 if (after_pathsep(p, q))
9310 *gettail_sep(p) = NUL;
9311 }
9312
9313 rettv->vval.v_string = p;
9314 }
9315# else
9316 rettv->vval.v_string = vim_strsave(p);
9317# endif
9318#endif
9319
9320 simplify_filename(rettv->vval.v_string);
9321
9322#ifdef HAVE_READLINK
9323fail:
9324 vim_free(buf);
9325#endif
9326 rettv->v_type = VAR_STRING;
9327}
9328
9329/*
9330 * "reverse({list})" function
9331 */
9332 static void
9333f_reverse(typval_T *argvars, typval_T *rettv)
9334{
9335 list_T *l;
9336 listitem_T *li, *ni;
9337
9338 if (argvars[0].v_type != VAR_LIST)
9339 EMSG2(_(e_listarg), "reverse()");
9340 else if ((l = argvars[0].vval.v_list) != NULL
9341 && !tv_check_lock(l->lv_lock,
9342 (char_u *)N_("reverse() argument"), TRUE))
9343 {
9344 li = l->lv_last;
9345 l->lv_first = l->lv_last = NULL;
9346 l->lv_len = 0;
9347 while (li != NULL)
9348 {
9349 ni = li->li_prev;
9350 list_append(l, li);
9351 li = ni;
9352 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009353 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009354 l->lv_idx = l->lv_len - l->lv_idx - 1;
9355 }
9356}
9357
9358#define SP_NOMOVE 0x01 /* don't move cursor */
9359#define SP_REPEAT 0x02 /* repeat to find outer pair */
9360#define SP_RETCOUNT 0x04 /* return matchcount */
9361#define SP_SETPCMARK 0x08 /* set previous context mark */
9362#define SP_START 0x10 /* accept match at start position */
9363#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9364#define SP_END 0x40 /* leave cursor at end of match */
9365#define SP_COLUMN 0x80 /* start at cursor column */
9366
9367static int get_search_arg(typval_T *varp, int *flagsp);
9368
9369/*
9370 * Get flags for a search function.
9371 * Possibly sets "p_ws".
9372 * Returns BACKWARD, FORWARD or zero (for an error).
9373 */
9374 static int
9375get_search_arg(typval_T *varp, int *flagsp)
9376{
9377 int dir = FORWARD;
9378 char_u *flags;
9379 char_u nbuf[NUMBUFLEN];
9380 int mask;
9381
9382 if (varp->v_type != VAR_UNKNOWN)
9383 {
9384 flags = get_tv_string_buf_chk(varp, nbuf);
9385 if (flags == NULL)
9386 return 0; /* type error; errmsg already given */
9387 while (*flags != NUL)
9388 {
9389 switch (*flags)
9390 {
9391 case 'b': dir = BACKWARD; break;
9392 case 'w': p_ws = TRUE; break;
9393 case 'W': p_ws = FALSE; break;
9394 default: mask = 0;
9395 if (flagsp != NULL)
9396 switch (*flags)
9397 {
9398 case 'c': mask = SP_START; break;
9399 case 'e': mask = SP_END; break;
9400 case 'm': mask = SP_RETCOUNT; break;
9401 case 'n': mask = SP_NOMOVE; break;
9402 case 'p': mask = SP_SUBPAT; break;
9403 case 'r': mask = SP_REPEAT; break;
9404 case 's': mask = SP_SETPCMARK; break;
9405 case 'z': mask = SP_COLUMN; break;
9406 }
9407 if (mask == 0)
9408 {
9409 EMSG2(_(e_invarg2), flags);
9410 dir = 0;
9411 }
9412 else
9413 *flagsp |= mask;
9414 }
9415 if (dir == 0)
9416 break;
9417 ++flags;
9418 }
9419 }
9420 return dir;
9421}
9422
9423/*
9424 * Shared by search() and searchpos() functions.
9425 */
9426 static int
9427search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9428{
9429 int flags;
9430 char_u *pat;
9431 pos_T pos;
9432 pos_T save_cursor;
9433 int save_p_ws = p_ws;
9434 int dir;
9435 int retval = 0; /* default: FAIL */
9436 long lnum_stop = 0;
9437 proftime_T tm;
9438#ifdef FEAT_RELTIME
9439 long time_limit = 0;
9440#endif
9441 int options = SEARCH_KEEP;
9442 int subpatnum;
9443
9444 pat = get_tv_string(&argvars[0]);
9445 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9446 if (dir == 0)
9447 goto theend;
9448 flags = *flagsp;
9449 if (flags & SP_START)
9450 options |= SEARCH_START;
9451 if (flags & SP_END)
9452 options |= SEARCH_END;
9453 if (flags & SP_COLUMN)
9454 options |= SEARCH_COL;
9455
9456 /* Optional arguments: line number to stop searching and timeout. */
9457 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9458 {
9459 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9460 if (lnum_stop < 0)
9461 goto theend;
9462#ifdef FEAT_RELTIME
9463 if (argvars[3].v_type != VAR_UNKNOWN)
9464 {
9465 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9466 if (time_limit < 0)
9467 goto theend;
9468 }
9469#endif
9470 }
9471
9472#ifdef FEAT_RELTIME
9473 /* Set the time limit, if there is one. */
9474 profile_setlimit(time_limit, &tm);
9475#endif
9476
9477 /*
9478 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9479 * Check to make sure only those flags are set.
9480 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9481 * flags cannot be set. Check for that condition also.
9482 */
9483 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9484 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9485 {
9486 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9487 goto theend;
9488 }
9489
9490 pos = save_cursor = curwin->w_cursor;
9491 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009492 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009493 if (subpatnum != FAIL)
9494 {
9495 if (flags & SP_SUBPAT)
9496 retval = subpatnum;
9497 else
9498 retval = pos.lnum;
9499 if (flags & SP_SETPCMARK)
9500 setpcmark();
9501 curwin->w_cursor = pos;
9502 if (match_pos != NULL)
9503 {
9504 /* Store the match cursor position */
9505 match_pos->lnum = pos.lnum;
9506 match_pos->col = pos.col + 1;
9507 }
9508 /* "/$" will put the cursor after the end of the line, may need to
9509 * correct that here */
9510 check_cursor();
9511 }
9512
9513 /* If 'n' flag is used: restore cursor position. */
9514 if (flags & SP_NOMOVE)
9515 curwin->w_cursor = save_cursor;
9516 else
9517 curwin->w_set_curswant = TRUE;
9518theend:
9519 p_ws = save_p_ws;
9520
9521 return retval;
9522}
9523
9524#ifdef FEAT_FLOAT
9525
9526/*
9527 * round() is not in C90, use ceil() or floor() instead.
9528 */
9529 float_T
9530vim_round(float_T f)
9531{
9532 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9533}
9534
9535/*
9536 * "round({float})" function
9537 */
9538 static void
9539f_round(typval_T *argvars, typval_T *rettv)
9540{
9541 float_T f = 0.0;
9542
9543 rettv->v_type = VAR_FLOAT;
9544 if (get_float_arg(argvars, &f) == OK)
9545 rettv->vval.v_float = vim_round(f);
9546 else
9547 rettv->vval.v_float = 0.0;
9548}
9549#endif
9550
9551/*
9552 * "screenattr()" function
9553 */
9554 static void
9555f_screenattr(typval_T *argvars, typval_T *rettv)
9556{
9557 int row;
9558 int col;
9559 int c;
9560
9561 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9562 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9563 if (row < 0 || row >= screen_Rows
9564 || col < 0 || col >= screen_Columns)
9565 c = -1;
9566 else
9567 c = ScreenAttrs[LineOffset[row] + col];
9568 rettv->vval.v_number = c;
9569}
9570
9571/*
9572 * "screenchar()" function
9573 */
9574 static void
9575f_screenchar(typval_T *argvars, typval_T *rettv)
9576{
9577 int row;
9578 int col;
9579 int off;
9580 int c;
9581
9582 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9583 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9584 if (row < 0 || row >= screen_Rows
9585 || col < 0 || col >= screen_Columns)
9586 c = -1;
9587 else
9588 {
9589 off = LineOffset[row] + col;
9590#ifdef FEAT_MBYTE
9591 if (enc_utf8 && ScreenLinesUC[off] != 0)
9592 c = ScreenLinesUC[off];
9593 else
9594#endif
9595 c = ScreenLines[off];
9596 }
9597 rettv->vval.v_number = c;
9598}
9599
9600/*
9601 * "screencol()" function
9602 *
9603 * First column is 1 to be consistent with virtcol().
9604 */
9605 static void
9606f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9607{
9608 rettv->vval.v_number = screen_screencol() + 1;
9609}
9610
9611/*
9612 * "screenrow()" function
9613 */
9614 static void
9615f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9616{
9617 rettv->vval.v_number = screen_screenrow() + 1;
9618}
9619
9620/*
9621 * "search()" function
9622 */
9623 static void
9624f_search(typval_T *argvars, typval_T *rettv)
9625{
9626 int flags = 0;
9627
9628 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9629}
9630
9631/*
9632 * "searchdecl()" function
9633 */
9634 static void
9635f_searchdecl(typval_T *argvars, typval_T *rettv)
9636{
9637 int locally = 1;
9638 int thisblock = 0;
9639 int error = FALSE;
9640 char_u *name;
9641
9642 rettv->vval.v_number = 1; /* default: FAIL */
9643
9644 name = get_tv_string_chk(&argvars[0]);
9645 if (argvars[1].v_type != VAR_UNKNOWN)
9646 {
9647 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9648 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9649 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9650 }
9651 if (!error && name != NULL)
9652 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9653 locally, thisblock, SEARCH_KEEP) == FAIL;
9654}
9655
9656/*
9657 * Used by searchpair() and searchpairpos()
9658 */
9659 static int
9660searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9661{
9662 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009663 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009664 int save_p_ws = p_ws;
9665 int dir;
9666 int flags = 0;
9667 char_u nbuf1[NUMBUFLEN];
9668 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009669 int retval = 0; /* default: FAIL */
9670 long lnum_stop = 0;
9671 long time_limit = 0;
9672
9673 /* Get the three pattern arguments: start, middle, end. */
9674 spat = get_tv_string_chk(&argvars[0]);
9675 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9676 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9677 if (spat == NULL || mpat == NULL || epat == NULL)
9678 goto theend; /* type error */
9679
9680 /* Handle the optional fourth argument: flags */
9681 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9682 if (dir == 0)
9683 goto theend;
9684
9685 /* Don't accept SP_END or SP_SUBPAT.
9686 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9687 */
9688 if ((flags & (SP_END | SP_SUBPAT)) != 0
9689 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9690 {
9691 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9692 goto theend;
9693 }
9694
9695 /* Using 'r' implies 'W', otherwise it doesn't work. */
9696 if (flags & SP_REPEAT)
9697 p_ws = FALSE;
9698
9699 /* Optional fifth argument: skip expression */
9700 if (argvars[3].v_type == VAR_UNKNOWN
9701 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009702 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009703 else
9704 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009705 skip = &argvars[4];
9706 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9707 && skip->v_type != VAR_STRING)
9708 {
9709 /* Type error */
9710 goto theend;
9711 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009712 if (argvars[5].v_type != VAR_UNKNOWN)
9713 {
9714 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9715 if (lnum_stop < 0)
9716 goto theend;
9717#ifdef FEAT_RELTIME
9718 if (argvars[6].v_type != VAR_UNKNOWN)
9719 {
9720 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9721 if (time_limit < 0)
9722 goto theend;
9723 }
9724#endif
9725 }
9726 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009727
9728 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9729 match_pos, lnum_stop, time_limit);
9730
9731theend:
9732 p_ws = save_p_ws;
9733
9734 return retval;
9735}
9736
9737/*
9738 * "searchpair()" function
9739 */
9740 static void
9741f_searchpair(typval_T *argvars, typval_T *rettv)
9742{
9743 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9744}
9745
9746/*
9747 * "searchpairpos()" function
9748 */
9749 static void
9750f_searchpairpos(typval_T *argvars, typval_T *rettv)
9751{
9752 pos_T match_pos;
9753 int lnum = 0;
9754 int col = 0;
9755
9756 if (rettv_list_alloc(rettv) == FAIL)
9757 return;
9758
9759 if (searchpair_cmn(argvars, &match_pos) > 0)
9760 {
9761 lnum = match_pos.lnum;
9762 col = match_pos.col;
9763 }
9764
9765 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9766 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9767}
9768
9769/*
9770 * Search for a start/middle/end thing.
9771 * Used by searchpair(), see its documentation for the details.
9772 * Returns 0 or -1 for no match,
9773 */
9774 long
9775do_searchpair(
9776 char_u *spat, /* start pattern */
9777 char_u *mpat, /* middle pattern */
9778 char_u *epat, /* end pattern */
9779 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009780 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009781 int flags, /* SP_SETPCMARK and other SP_ values */
9782 pos_T *match_pos,
9783 linenr_T lnum_stop, /* stop at this line if not zero */
9784 long time_limit UNUSED) /* stop after this many msec */
9785{
9786 char_u *save_cpo;
9787 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9788 long retval = 0;
9789 pos_T pos;
9790 pos_T firstpos;
9791 pos_T foundpos;
9792 pos_T save_cursor;
9793 pos_T save_pos;
9794 int n;
9795 int r;
9796 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009797 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009798 int err;
9799 int options = SEARCH_KEEP;
9800 proftime_T tm;
9801
9802 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9803 save_cpo = p_cpo;
9804 p_cpo = empty_option;
9805
9806#ifdef FEAT_RELTIME
9807 /* Set the time limit, if there is one. */
9808 profile_setlimit(time_limit, &tm);
9809#endif
9810
9811 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9812 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009813 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9814 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009815 if (pat2 == NULL || pat3 == NULL)
9816 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009817 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009818 if (*mpat == NUL)
9819 STRCPY(pat3, pat2);
9820 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009821 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009822 spat, epat, mpat);
9823 if (flags & SP_START)
9824 options |= SEARCH_START;
9825
Bram Moolenaar48570482017-10-30 21:48:41 +01009826 if (skip != NULL)
9827 {
9828 /* Empty string means to not use the skip expression. */
9829 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9830 use_skip = skip->vval.v_string != NULL
9831 && *skip->vval.v_string != NUL;
9832 }
9833
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009834 save_cursor = curwin->w_cursor;
9835 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009836 CLEAR_POS(&firstpos);
9837 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009838 pat = pat3;
9839 for (;;)
9840 {
9841 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009842 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009843 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009844 /* didn't find it or found the first match again: FAIL */
9845 break;
9846
9847 if (firstpos.lnum == 0)
9848 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009849 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009850 {
9851 /* Found the same position again. Can happen with a pattern that
9852 * has "\zs" at the end and searching backwards. Advance one
9853 * character and try again. */
9854 if (dir == BACKWARD)
9855 decl(&pos);
9856 else
9857 incl(&pos);
9858 }
9859 foundpos = pos;
9860
9861 /* clear the start flag to avoid getting stuck here */
9862 options &= ~SEARCH_START;
9863
9864 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009865 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009866 {
9867 save_pos = curwin->w_cursor;
9868 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009869 err = FALSE;
9870 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009871 curwin->w_cursor = save_pos;
9872 if (err)
9873 {
9874 /* Evaluating {skip} caused an error, break here. */
9875 curwin->w_cursor = save_cursor;
9876 retval = -1;
9877 break;
9878 }
9879 if (r)
9880 continue;
9881 }
9882
9883 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9884 {
9885 /* Found end when searching backwards or start when searching
9886 * forward: nested pair. */
9887 ++nest;
9888 pat = pat2; /* nested, don't search for middle */
9889 }
9890 else
9891 {
9892 /* Found end when searching forward or start when searching
9893 * backward: end of (nested) pair; or found middle in outer pair. */
9894 if (--nest == 1)
9895 pat = pat3; /* outer level, search for middle */
9896 }
9897
9898 if (nest == 0)
9899 {
9900 /* Found the match: return matchcount or line number. */
9901 if (flags & SP_RETCOUNT)
9902 ++retval;
9903 else
9904 retval = pos.lnum;
9905 if (flags & SP_SETPCMARK)
9906 setpcmark();
9907 curwin->w_cursor = pos;
9908 if (!(flags & SP_REPEAT))
9909 break;
9910 nest = 1; /* search for next unmatched */
9911 }
9912 }
9913
9914 if (match_pos != NULL)
9915 {
9916 /* Store the match cursor position */
9917 match_pos->lnum = curwin->w_cursor.lnum;
9918 match_pos->col = curwin->w_cursor.col + 1;
9919 }
9920
9921 /* If 'n' flag is used or search failed: restore cursor position. */
9922 if ((flags & SP_NOMOVE) || retval == 0)
9923 curwin->w_cursor = save_cursor;
9924
9925theend:
9926 vim_free(pat2);
9927 vim_free(pat3);
9928 if (p_cpo == empty_option)
9929 p_cpo = save_cpo;
9930 else
9931 /* Darn, evaluating the {skip} expression changed the value. */
9932 free_string_option(save_cpo);
9933
9934 return retval;
9935}
9936
9937/*
9938 * "searchpos()" function
9939 */
9940 static void
9941f_searchpos(typval_T *argvars, typval_T *rettv)
9942{
9943 pos_T match_pos;
9944 int lnum = 0;
9945 int col = 0;
9946 int n;
9947 int flags = 0;
9948
9949 if (rettv_list_alloc(rettv) == FAIL)
9950 return;
9951
9952 n = search_cmn(argvars, &match_pos, &flags);
9953 if (n > 0)
9954 {
9955 lnum = match_pos.lnum;
9956 col = match_pos.col;
9957 }
9958
9959 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9960 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9961 if (flags & SP_SUBPAT)
9962 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9963}
9964
9965 static void
9966f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9967{
9968#ifdef FEAT_CLIENTSERVER
9969 char_u buf[NUMBUFLEN];
9970 char_u *server = get_tv_string_chk(&argvars[0]);
9971 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9972
9973 rettv->vval.v_number = -1;
9974 if (server == NULL || reply == NULL)
9975 return;
9976 if (check_restricted() || check_secure())
9977 return;
9978# ifdef FEAT_X11
9979 if (check_connection() == FAIL)
9980 return;
9981# endif
9982
9983 if (serverSendReply(server, reply) < 0)
9984 {
9985 EMSG(_("E258: Unable to send to client"));
9986 return;
9987 }
9988 rettv->vval.v_number = 0;
9989#else
9990 rettv->vval.v_number = -1;
9991#endif
9992}
9993
9994 static void
9995f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9996{
9997 char_u *r = NULL;
9998
9999#ifdef FEAT_CLIENTSERVER
10000# ifdef WIN32
10001 r = serverGetVimNames();
10002# else
10003 make_connection();
10004 if (X_DISPLAY != NULL)
10005 r = serverGetVimNames(X_DISPLAY);
10006# endif
10007#endif
10008 rettv->v_type = VAR_STRING;
10009 rettv->vval.v_string = r;
10010}
10011
10012/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010013 * Set line or list of lines in buffer "buf".
10014 */
10015 static void
10016set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
10017{
10018 char_u *line = NULL;
10019 list_T *l = NULL;
10020 listitem_T *li = NULL;
10021 long added = 0;
10022 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010023 buf_T *curbuf_save = NULL;
10024 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010025 int is_curbuf = buf == curbuf;
10026
Bram Moolenaar9d954202017-09-04 20:34:19 +020010027 /* When using the current buffer ml_mfp will be set if needed. Useful when
10028 * setline() is used on startup. For other buffers the buffer must be
10029 * loaded. */
10030 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010031 {
10032 rettv->vval.v_number = 1; /* FAIL */
10033 return;
10034 }
10035
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010036 if (!is_curbuf)
10037 {
10038 wininfo_T *wip;
10039
10040 curbuf_save = curbuf;
10041 curwin_save = curwin;
10042 curbuf = buf;
10043 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10044 {
10045 if (wip->wi_win != NULL)
10046 {
10047 curwin = wip->wi_win;
10048 break;
10049 }
10050 }
10051 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010052
10053 lcount = curbuf->b_ml.ml_line_count;
10054
10055 if (lines->v_type == VAR_LIST)
10056 {
10057 l = lines->vval.v_list;
10058 li = l->lv_first;
10059 }
10060 else
10061 line = get_tv_string_chk(lines);
10062
10063 /* default result is zero == OK */
10064 for (;;)
10065 {
10066 if (l != NULL)
10067 {
10068 /* list argument, get next string */
10069 if (li == NULL)
10070 break;
10071 line = get_tv_string_chk(&li->li_tv);
10072 li = li->li_next;
10073 }
10074
10075 rettv->vval.v_number = 1; /* FAIL */
10076 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10077 break;
10078
10079 /* When coming here from Insert mode, sync undo, so that this can be
10080 * undone separately from what was previously inserted. */
10081 if (u_sync_once == 2)
10082 {
10083 u_sync_once = 1; /* notify that u_sync() was called */
10084 u_sync(TRUE);
10085 }
10086
10087 if (lnum <= curbuf->b_ml.ml_line_count)
10088 {
10089 /* existing line, replace it */
10090 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10091 {
10092 changed_bytes(lnum, 0);
10093 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10094 check_cursor_col();
10095 rettv->vval.v_number = 0; /* OK */
10096 }
10097 }
10098 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10099 {
10100 /* lnum is one past the last line, append the line */
10101 ++added;
10102 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10103 rettv->vval.v_number = 0; /* OK */
10104 }
10105
10106 if (l == NULL) /* only one string argument */
10107 break;
10108 ++lnum;
10109 }
10110
10111 if (added > 0)
10112 appended_lines_mark(lcount, added);
10113
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010114 if (!is_curbuf)
10115 {
10116 curbuf = curbuf_save;
10117 curwin = curwin_save;
10118 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010119}
10120
10121/*
10122 * "setbufline()" function
10123 */
10124 static void
10125f_setbufline(argvars, rettv)
10126 typval_T *argvars;
10127 typval_T *rettv;
10128{
10129 linenr_T lnum;
10130 buf_T *buf;
10131
10132 buf = get_buf_tv(&argvars[0], FALSE);
10133 if (buf == NULL)
10134 rettv->vval.v_number = 1; /* FAIL */
10135 else
10136 {
10137 lnum = get_tv_lnum_buf(&argvars[1], buf);
10138
10139 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10140 }
10141}
10142
10143/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010144 * "setbufvar()" function
10145 */
10146 static void
10147f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10148{
10149 buf_T *buf;
10150 char_u *varname, *bufvarname;
10151 typval_T *varp;
10152 char_u nbuf[NUMBUFLEN];
10153
10154 if (check_restricted() || check_secure())
10155 return;
10156 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10157 varname = get_tv_string_chk(&argvars[1]);
10158 buf = get_buf_tv(&argvars[0], FALSE);
10159 varp = &argvars[2];
10160
10161 if (buf != NULL && varname != NULL && varp != NULL)
10162 {
10163 if (*varname == '&')
10164 {
10165 long numval;
10166 char_u *strval;
10167 int error = FALSE;
10168 aco_save_T aco;
10169
10170 /* set curbuf to be our buf, temporarily */
10171 aucmd_prepbuf(&aco, buf);
10172
10173 ++varname;
10174 numval = (long)get_tv_number_chk(varp, &error);
10175 strval = get_tv_string_buf_chk(varp, nbuf);
10176 if (!error && strval != NULL)
10177 set_option_value(varname, numval, strval, OPT_LOCAL);
10178
10179 /* reset notion of buffer */
10180 aucmd_restbuf(&aco);
10181 }
10182 else
10183 {
10184 buf_T *save_curbuf = curbuf;
10185
10186 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10187 if (bufvarname != NULL)
10188 {
10189 curbuf = buf;
10190 STRCPY(bufvarname, "b:");
10191 STRCPY(bufvarname + 2, varname);
10192 set_var(bufvarname, varp, TRUE);
10193 vim_free(bufvarname);
10194 curbuf = save_curbuf;
10195 }
10196 }
10197 }
10198}
10199
10200 static void
10201f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10202{
10203 dict_T *d;
10204 dictitem_T *di;
10205 char_u *csearch;
10206
10207 if (argvars[0].v_type != VAR_DICT)
10208 {
10209 EMSG(_(e_dictreq));
10210 return;
10211 }
10212
10213 if ((d = argvars[0].vval.v_dict) != NULL)
10214 {
10215 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10216 if (csearch != NULL)
10217 {
10218#ifdef FEAT_MBYTE
10219 if (enc_utf8)
10220 {
10221 int pcc[MAX_MCO];
10222 int c = utfc_ptr2char(csearch, pcc);
10223
10224 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10225 }
10226 else
10227#endif
10228 set_last_csearch(PTR2CHAR(csearch),
10229 csearch, MB_PTR2LEN(csearch));
10230 }
10231
10232 di = dict_find(d, (char_u *)"forward", -1);
10233 if (di != NULL)
10234 set_csearch_direction((int)get_tv_number(&di->di_tv)
10235 ? FORWARD : BACKWARD);
10236
10237 di = dict_find(d, (char_u *)"until", -1);
10238 if (di != NULL)
10239 set_csearch_until(!!get_tv_number(&di->di_tv));
10240 }
10241}
10242
10243/*
10244 * "setcmdpos()" function
10245 */
10246 static void
10247f_setcmdpos(typval_T *argvars, typval_T *rettv)
10248{
10249 int pos = (int)get_tv_number(&argvars[0]) - 1;
10250
10251 if (pos >= 0)
10252 rettv->vval.v_number = set_cmdline_pos(pos);
10253}
10254
10255/*
10256 * "setfperm({fname}, {mode})" function
10257 */
10258 static void
10259f_setfperm(typval_T *argvars, typval_T *rettv)
10260{
10261 char_u *fname;
10262 char_u modebuf[NUMBUFLEN];
10263 char_u *mode_str;
10264 int i;
10265 int mask;
10266 int mode = 0;
10267
10268 rettv->vval.v_number = 0;
10269 fname = get_tv_string_chk(&argvars[0]);
10270 if (fname == NULL)
10271 return;
10272 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10273 if (mode_str == NULL)
10274 return;
10275 if (STRLEN(mode_str) != 9)
10276 {
10277 EMSG2(_(e_invarg2), mode_str);
10278 return;
10279 }
10280
10281 mask = 1;
10282 for (i = 8; i >= 0; --i)
10283 {
10284 if (mode_str[i] != '-')
10285 mode |= mask;
10286 mask = mask << 1;
10287 }
10288 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10289}
10290
10291/*
10292 * "setline()" function
10293 */
10294 static void
10295f_setline(typval_T *argvars, typval_T *rettv)
10296{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010297 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010298
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010299 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010300}
10301
Bram Moolenaard823fa92016-08-12 16:29:27 +020010302static 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 +020010303
10304/*
10305 * Used by "setqflist()" and "setloclist()" functions
10306 */
10307 static void
10308set_qf_ll_list(
10309 win_T *wp UNUSED,
10310 typval_T *list_arg UNUSED,
10311 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010312 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010313 typval_T *rettv)
10314{
10315#ifdef FEAT_QUICKFIX
10316 static char *e_invact = N_("E927: Invalid action: '%s'");
10317 char_u *act;
10318 int action = 0;
10319#endif
10320
10321 rettv->vval.v_number = -1;
10322
10323#ifdef FEAT_QUICKFIX
10324 if (list_arg->v_type != VAR_LIST)
10325 EMSG(_(e_listreq));
10326 else
10327 {
10328 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010329 dict_T *d = NULL;
10330 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010331
10332 if (action_arg->v_type == VAR_STRING)
10333 {
10334 act = get_tv_string_chk(action_arg);
10335 if (act == NULL)
10336 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010337 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10338 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010339 action = *act;
10340 else
10341 EMSG2(_(e_invact), act);
10342 }
10343 else if (action_arg->v_type == VAR_UNKNOWN)
10344 action = ' ';
10345 else
10346 EMSG(_(e_stringreq));
10347
Bram Moolenaard823fa92016-08-12 16:29:27 +020010348 if (action_arg->v_type != VAR_UNKNOWN
10349 && what_arg->v_type != VAR_UNKNOWN)
10350 {
10351 if (what_arg->v_type == VAR_DICT)
10352 d = what_arg->vval.v_dict;
10353 else
10354 {
10355 EMSG(_(e_dictreq));
10356 valid_dict = FALSE;
10357 }
10358 }
10359
10360 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10361 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010362 rettv->vval.v_number = 0;
10363 }
10364#endif
10365}
10366
10367/*
10368 * "setloclist()" function
10369 */
10370 static void
10371f_setloclist(typval_T *argvars, typval_T *rettv)
10372{
10373 win_T *win;
10374
10375 rettv->vval.v_number = -1;
10376
10377 win = find_win_by_nr(&argvars[0], NULL);
10378 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010379 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010380}
10381
10382/*
10383 * "setmatches()" function
10384 */
10385 static void
10386f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10387{
10388#ifdef FEAT_SEARCH_EXTRA
10389 list_T *l;
10390 listitem_T *li;
10391 dict_T *d;
10392 list_T *s = NULL;
10393
10394 rettv->vval.v_number = -1;
10395 if (argvars[0].v_type != VAR_LIST)
10396 {
10397 EMSG(_(e_listreq));
10398 return;
10399 }
10400 if ((l = argvars[0].vval.v_list) != NULL)
10401 {
10402
10403 /* To some extent make sure that we are dealing with a list from
10404 * "getmatches()". */
10405 li = l->lv_first;
10406 while (li != NULL)
10407 {
10408 if (li->li_tv.v_type != VAR_DICT
10409 || (d = li->li_tv.vval.v_dict) == NULL)
10410 {
10411 EMSG(_(e_invarg));
10412 return;
10413 }
10414 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10415 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10416 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10417 && dict_find(d, (char_u *)"priority", -1) != NULL
10418 && dict_find(d, (char_u *)"id", -1) != NULL))
10419 {
10420 EMSG(_(e_invarg));
10421 return;
10422 }
10423 li = li->li_next;
10424 }
10425
10426 clear_matches(curwin);
10427 li = l->lv_first;
10428 while (li != NULL)
10429 {
10430 int i = 0;
10431 char_u buf[5];
10432 dictitem_T *di;
10433 char_u *group;
10434 int priority;
10435 int id;
10436 char_u *conceal;
10437
10438 d = li->li_tv.vval.v_dict;
10439 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10440 {
10441 if (s == NULL)
10442 {
10443 s = list_alloc();
10444 if (s == NULL)
10445 return;
10446 }
10447
10448 /* match from matchaddpos() */
10449 for (i = 1; i < 9; i++)
10450 {
10451 sprintf((char *)buf, (char *)"pos%d", i);
10452 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10453 {
10454 if (di->di_tv.v_type != VAR_LIST)
10455 return;
10456
10457 list_append_tv(s, &di->di_tv);
10458 s->lv_refcount++;
10459 }
10460 else
10461 break;
10462 }
10463 }
10464
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010465 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010466 priority = (int)get_dict_number(d, (char_u *)"priority");
10467 id = (int)get_dict_number(d, (char_u *)"id");
10468 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010469 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010470 : NULL;
10471 if (i == 0)
10472 {
10473 match_add(curwin, group,
10474 get_dict_string(d, (char_u *)"pattern", FALSE),
10475 priority, id, NULL, conceal);
10476 }
10477 else
10478 {
10479 match_add(curwin, group, NULL, priority, id, s, conceal);
10480 list_unref(s);
10481 s = NULL;
10482 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010483 vim_free(group);
10484 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010485
10486 li = li->li_next;
10487 }
10488 rettv->vval.v_number = 0;
10489 }
10490#endif
10491}
10492
10493/*
10494 * "setpos()" function
10495 */
10496 static void
10497f_setpos(typval_T *argvars, typval_T *rettv)
10498{
10499 pos_T pos;
10500 int fnum;
10501 char_u *name;
10502 colnr_T curswant = -1;
10503
10504 rettv->vval.v_number = -1;
10505 name = get_tv_string_chk(argvars);
10506 if (name != NULL)
10507 {
10508 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10509 {
10510 if (--pos.col < 0)
10511 pos.col = 0;
10512 if (name[0] == '.' && name[1] == NUL)
10513 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010514 /* set cursor; "fnum" is ignored */
10515 curwin->w_cursor = pos;
10516 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010517 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010518 curwin->w_curswant = curswant - 1;
10519 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010520 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010521 check_cursor();
10522 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010523 }
10524 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10525 {
10526 /* set mark */
10527 if (setmark_pos(name[1], &pos, fnum) == OK)
10528 rettv->vval.v_number = 0;
10529 }
10530 else
10531 EMSG(_(e_invarg));
10532 }
10533 }
10534}
10535
10536/*
10537 * "setqflist()" function
10538 */
10539 static void
10540f_setqflist(typval_T *argvars, typval_T *rettv)
10541{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010542 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010543}
10544
10545/*
10546 * "setreg()" function
10547 */
10548 static void
10549f_setreg(typval_T *argvars, typval_T *rettv)
10550{
10551 int regname;
10552 char_u *strregname;
10553 char_u *stropt;
10554 char_u *strval;
10555 int append;
10556 char_u yank_type;
10557 long block_len;
10558
10559 block_len = -1;
10560 yank_type = MAUTO;
10561 append = FALSE;
10562
10563 strregname = get_tv_string_chk(argvars);
10564 rettv->vval.v_number = 1; /* FAIL is default */
10565
10566 if (strregname == NULL)
10567 return; /* type error; errmsg already given */
10568 regname = *strregname;
10569 if (regname == 0 || regname == '@')
10570 regname = '"';
10571
10572 if (argvars[2].v_type != VAR_UNKNOWN)
10573 {
10574 stropt = get_tv_string_chk(&argvars[2]);
10575 if (stropt == NULL)
10576 return; /* type error */
10577 for (; *stropt != NUL; ++stropt)
10578 switch (*stropt)
10579 {
10580 case 'a': case 'A': /* append */
10581 append = TRUE;
10582 break;
10583 case 'v': case 'c': /* character-wise selection */
10584 yank_type = MCHAR;
10585 break;
10586 case 'V': case 'l': /* line-wise selection */
10587 yank_type = MLINE;
10588 break;
10589 case 'b': case Ctrl_V: /* block-wise selection */
10590 yank_type = MBLOCK;
10591 if (VIM_ISDIGIT(stropt[1]))
10592 {
10593 ++stropt;
10594 block_len = getdigits(&stropt) - 1;
10595 --stropt;
10596 }
10597 break;
10598 }
10599 }
10600
10601 if (argvars[1].v_type == VAR_LIST)
10602 {
10603 char_u **lstval;
10604 char_u **allocval;
10605 char_u buf[NUMBUFLEN];
10606 char_u **curval;
10607 char_u **curallocval;
10608 list_T *ll = argvars[1].vval.v_list;
10609 listitem_T *li;
10610 int len;
10611
10612 /* If the list is NULL handle like an empty list. */
10613 len = ll == NULL ? 0 : ll->lv_len;
10614
10615 /* First half: use for pointers to result lines; second half: use for
10616 * pointers to allocated copies. */
10617 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10618 if (lstval == NULL)
10619 return;
10620 curval = lstval;
10621 allocval = lstval + len + 2;
10622 curallocval = allocval;
10623
10624 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10625 li = li->li_next)
10626 {
10627 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10628 if (strval == NULL)
10629 goto free_lstval;
10630 if (strval == buf)
10631 {
10632 /* Need to make a copy, next get_tv_string_buf_chk() will
10633 * overwrite the string. */
10634 strval = vim_strsave(buf);
10635 if (strval == NULL)
10636 goto free_lstval;
10637 *curallocval++ = strval;
10638 }
10639 *curval++ = strval;
10640 }
10641 *curval++ = NULL;
10642
10643 write_reg_contents_lst(regname, lstval, -1,
10644 append, yank_type, block_len);
10645free_lstval:
10646 while (curallocval > allocval)
10647 vim_free(*--curallocval);
10648 vim_free(lstval);
10649 }
10650 else
10651 {
10652 strval = get_tv_string_chk(&argvars[1]);
10653 if (strval == NULL)
10654 return;
10655 write_reg_contents_ex(regname, strval, -1,
10656 append, yank_type, block_len);
10657 }
10658 rettv->vval.v_number = 0;
10659}
10660
10661/*
10662 * "settabvar()" function
10663 */
10664 static void
10665f_settabvar(typval_T *argvars, typval_T *rettv)
10666{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010667 tabpage_T *save_curtab;
10668 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010669 char_u *varname, *tabvarname;
10670 typval_T *varp;
10671
10672 rettv->vval.v_number = 0;
10673
10674 if (check_restricted() || check_secure())
10675 return;
10676
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010677 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010678 varname = get_tv_string_chk(&argvars[1]);
10679 varp = &argvars[2];
10680
Bram Moolenaar4033c552017-09-16 20:54:51 +020010681 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010682 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010683 save_curtab = curtab;
10684 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010685
10686 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10687 if (tabvarname != NULL)
10688 {
10689 STRCPY(tabvarname, "t:");
10690 STRCPY(tabvarname + 2, varname);
10691 set_var(tabvarname, varp, TRUE);
10692 vim_free(tabvarname);
10693 }
10694
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010695 /* Restore current tabpage */
10696 if (valid_tabpage(save_curtab))
10697 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010698 }
10699}
10700
10701/*
10702 * "settabwinvar()" function
10703 */
10704 static void
10705f_settabwinvar(typval_T *argvars, typval_T *rettv)
10706{
10707 setwinvar(argvars, rettv, 1);
10708}
10709
10710/*
10711 * "setwinvar()" function
10712 */
10713 static void
10714f_setwinvar(typval_T *argvars, typval_T *rettv)
10715{
10716 setwinvar(argvars, rettv, 0);
10717}
10718
10719#ifdef FEAT_CRYPT
10720/*
10721 * "sha256({string})" function
10722 */
10723 static void
10724f_sha256(typval_T *argvars, typval_T *rettv)
10725{
10726 char_u *p;
10727
10728 p = get_tv_string(&argvars[0]);
10729 rettv->vval.v_string = vim_strsave(
10730 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10731 rettv->v_type = VAR_STRING;
10732}
10733#endif /* FEAT_CRYPT */
10734
10735/*
10736 * "shellescape({string})" function
10737 */
10738 static void
10739f_shellescape(typval_T *argvars, typval_T *rettv)
10740{
Bram Moolenaar20615522017-06-05 18:46:26 +020010741 int do_special = non_zero_arg(&argvars[1]);
10742
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010743 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010744 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010745 rettv->v_type = VAR_STRING;
10746}
10747
10748/*
10749 * shiftwidth() function
10750 */
10751 static void
10752f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10753{
10754 rettv->vval.v_number = get_sw_value(curbuf);
10755}
10756
10757/*
10758 * "simplify()" function
10759 */
10760 static void
10761f_simplify(typval_T *argvars, typval_T *rettv)
10762{
10763 char_u *p;
10764
10765 p = get_tv_string(&argvars[0]);
10766 rettv->vval.v_string = vim_strsave(p);
10767 simplify_filename(rettv->vval.v_string); /* simplify in place */
10768 rettv->v_type = VAR_STRING;
10769}
10770
10771#ifdef FEAT_FLOAT
10772/*
10773 * "sin()" function
10774 */
10775 static void
10776f_sin(typval_T *argvars, typval_T *rettv)
10777{
10778 float_T f = 0.0;
10779
10780 rettv->v_type = VAR_FLOAT;
10781 if (get_float_arg(argvars, &f) == OK)
10782 rettv->vval.v_float = sin(f);
10783 else
10784 rettv->vval.v_float = 0.0;
10785}
10786
10787/*
10788 * "sinh()" function
10789 */
10790 static void
10791f_sinh(typval_T *argvars, typval_T *rettv)
10792{
10793 float_T f = 0.0;
10794
10795 rettv->v_type = VAR_FLOAT;
10796 if (get_float_arg(argvars, &f) == OK)
10797 rettv->vval.v_float = sinh(f);
10798 else
10799 rettv->vval.v_float = 0.0;
10800}
10801#endif
10802
10803static int
10804#ifdef __BORLANDC__
10805 _RTLENTRYF
10806#endif
10807 item_compare(const void *s1, const void *s2);
10808static int
10809#ifdef __BORLANDC__
10810 _RTLENTRYF
10811#endif
10812 item_compare2(const void *s1, const void *s2);
10813
10814/* struct used in the array that's given to qsort() */
10815typedef struct
10816{
10817 listitem_T *item;
10818 int idx;
10819} sortItem_T;
10820
10821/* struct storing information about current sort */
10822typedef struct
10823{
10824 int item_compare_ic;
10825 int item_compare_numeric;
10826 int item_compare_numbers;
10827#ifdef FEAT_FLOAT
10828 int item_compare_float;
10829#endif
10830 char_u *item_compare_func;
10831 partial_T *item_compare_partial;
10832 dict_T *item_compare_selfdict;
10833 int item_compare_func_err;
10834 int item_compare_keep_zero;
10835} sortinfo_T;
10836static sortinfo_T *sortinfo = NULL;
10837static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10838#define ITEM_COMPARE_FAIL 999
10839
10840/*
10841 * Compare functions for f_sort() and f_uniq() below.
10842 */
10843 static int
10844#ifdef __BORLANDC__
10845_RTLENTRYF
10846#endif
10847item_compare(const void *s1, const void *s2)
10848{
10849 sortItem_T *si1, *si2;
10850 typval_T *tv1, *tv2;
10851 char_u *p1, *p2;
10852 char_u *tofree1 = NULL, *tofree2 = NULL;
10853 int res;
10854 char_u numbuf1[NUMBUFLEN];
10855 char_u numbuf2[NUMBUFLEN];
10856
10857 si1 = (sortItem_T *)s1;
10858 si2 = (sortItem_T *)s2;
10859 tv1 = &si1->item->li_tv;
10860 tv2 = &si2->item->li_tv;
10861
10862 if (sortinfo->item_compare_numbers)
10863 {
10864 varnumber_T v1 = get_tv_number(tv1);
10865 varnumber_T v2 = get_tv_number(tv2);
10866
10867 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10868 }
10869
10870#ifdef FEAT_FLOAT
10871 if (sortinfo->item_compare_float)
10872 {
10873 float_T v1 = get_tv_float(tv1);
10874 float_T v2 = get_tv_float(tv2);
10875
10876 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10877 }
10878#endif
10879
10880 /* tv2string() puts quotes around a string and allocates memory. Don't do
10881 * that for string variables. Use a single quote when comparing with a
10882 * non-string to do what the docs promise. */
10883 if (tv1->v_type == VAR_STRING)
10884 {
10885 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10886 p1 = (char_u *)"'";
10887 else
10888 p1 = tv1->vval.v_string;
10889 }
10890 else
10891 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10892 if (tv2->v_type == VAR_STRING)
10893 {
10894 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10895 p2 = (char_u *)"'";
10896 else
10897 p2 = tv2->vval.v_string;
10898 }
10899 else
10900 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10901 if (p1 == NULL)
10902 p1 = (char_u *)"";
10903 if (p2 == NULL)
10904 p2 = (char_u *)"";
10905 if (!sortinfo->item_compare_numeric)
10906 {
10907 if (sortinfo->item_compare_ic)
10908 res = STRICMP(p1, p2);
10909 else
10910 res = STRCMP(p1, p2);
10911 }
10912 else
10913 {
10914 double n1, n2;
10915 n1 = strtod((char *)p1, (char **)&p1);
10916 n2 = strtod((char *)p2, (char **)&p2);
10917 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10918 }
10919
10920 /* When the result would be zero, compare the item indexes. Makes the
10921 * sort stable. */
10922 if (res == 0 && !sortinfo->item_compare_keep_zero)
10923 res = si1->idx > si2->idx ? 1 : -1;
10924
10925 vim_free(tofree1);
10926 vim_free(tofree2);
10927 return res;
10928}
10929
10930 static int
10931#ifdef __BORLANDC__
10932_RTLENTRYF
10933#endif
10934item_compare2(const void *s1, const void *s2)
10935{
10936 sortItem_T *si1, *si2;
10937 int res;
10938 typval_T rettv;
10939 typval_T argv[3];
10940 int dummy;
10941 char_u *func_name;
10942 partial_T *partial = sortinfo->item_compare_partial;
10943
10944 /* shortcut after failure in previous call; compare all items equal */
10945 if (sortinfo->item_compare_func_err)
10946 return 0;
10947
10948 si1 = (sortItem_T *)s1;
10949 si2 = (sortItem_T *)s2;
10950
10951 if (partial == NULL)
10952 func_name = sortinfo->item_compare_func;
10953 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010954 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010955
10956 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10957 * in the copy without changing the original list items. */
10958 copy_tv(&si1->item->li_tv, &argv[0]);
10959 copy_tv(&si2->item->li_tv, &argv[1]);
10960
10961 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10962 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010963 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010964 partial, sortinfo->item_compare_selfdict);
10965 clear_tv(&argv[0]);
10966 clear_tv(&argv[1]);
10967
10968 if (res == FAIL)
10969 res = ITEM_COMPARE_FAIL;
10970 else
10971 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10972 if (sortinfo->item_compare_func_err)
10973 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10974 clear_tv(&rettv);
10975
10976 /* When the result would be zero, compare the pointers themselves. Makes
10977 * the sort stable. */
10978 if (res == 0 && !sortinfo->item_compare_keep_zero)
10979 res = si1->idx > si2->idx ? 1 : -1;
10980
10981 return res;
10982}
10983
10984/*
10985 * "sort({list})" function
10986 */
10987 static void
10988do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
10989{
10990 list_T *l;
10991 listitem_T *li;
10992 sortItem_T *ptrs;
10993 sortinfo_T *old_sortinfo;
10994 sortinfo_T info;
10995 long len;
10996 long i;
10997
10998 /* Pointer to current info struct used in compare function. Save and
10999 * restore the current one for nested calls. */
11000 old_sortinfo = sortinfo;
11001 sortinfo = &info;
11002
11003 if (argvars[0].v_type != VAR_LIST)
11004 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11005 else
11006 {
11007 l = argvars[0].vval.v_list;
11008 if (l == NULL || tv_check_lock(l->lv_lock,
11009 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11010 TRUE))
11011 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011012 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011013
11014 len = list_len(l);
11015 if (len <= 1)
11016 goto theend; /* short list sorts pretty quickly */
11017
11018 info.item_compare_ic = FALSE;
11019 info.item_compare_numeric = FALSE;
11020 info.item_compare_numbers = FALSE;
11021#ifdef FEAT_FLOAT
11022 info.item_compare_float = FALSE;
11023#endif
11024 info.item_compare_func = NULL;
11025 info.item_compare_partial = NULL;
11026 info.item_compare_selfdict = NULL;
11027 if (argvars[1].v_type != VAR_UNKNOWN)
11028 {
11029 /* optional second argument: {func} */
11030 if (argvars[1].v_type == VAR_FUNC)
11031 info.item_compare_func = argvars[1].vval.v_string;
11032 else if (argvars[1].v_type == VAR_PARTIAL)
11033 info.item_compare_partial = argvars[1].vval.v_partial;
11034 else
11035 {
11036 int error = FALSE;
11037
11038 i = (long)get_tv_number_chk(&argvars[1], &error);
11039 if (error)
11040 goto theend; /* type error; errmsg already given */
11041 if (i == 1)
11042 info.item_compare_ic = TRUE;
11043 else if (argvars[1].v_type != VAR_NUMBER)
11044 info.item_compare_func = get_tv_string(&argvars[1]);
11045 else if (i != 0)
11046 {
11047 EMSG(_(e_invarg));
11048 goto theend;
11049 }
11050 if (info.item_compare_func != NULL)
11051 {
11052 if (*info.item_compare_func == NUL)
11053 {
11054 /* empty string means default sort */
11055 info.item_compare_func = NULL;
11056 }
11057 else if (STRCMP(info.item_compare_func, "n") == 0)
11058 {
11059 info.item_compare_func = NULL;
11060 info.item_compare_numeric = TRUE;
11061 }
11062 else if (STRCMP(info.item_compare_func, "N") == 0)
11063 {
11064 info.item_compare_func = NULL;
11065 info.item_compare_numbers = TRUE;
11066 }
11067#ifdef FEAT_FLOAT
11068 else if (STRCMP(info.item_compare_func, "f") == 0)
11069 {
11070 info.item_compare_func = NULL;
11071 info.item_compare_float = TRUE;
11072 }
11073#endif
11074 else if (STRCMP(info.item_compare_func, "i") == 0)
11075 {
11076 info.item_compare_func = NULL;
11077 info.item_compare_ic = TRUE;
11078 }
11079 }
11080 }
11081
11082 if (argvars[2].v_type != VAR_UNKNOWN)
11083 {
11084 /* optional third argument: {dict} */
11085 if (argvars[2].v_type != VAR_DICT)
11086 {
11087 EMSG(_(e_dictreq));
11088 goto theend;
11089 }
11090 info.item_compare_selfdict = argvars[2].vval.v_dict;
11091 }
11092 }
11093
11094 /* Make an array with each entry pointing to an item in the List. */
11095 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11096 if (ptrs == NULL)
11097 goto theend;
11098
11099 i = 0;
11100 if (sort)
11101 {
11102 /* sort(): ptrs will be the list to sort */
11103 for (li = l->lv_first; li != NULL; li = li->li_next)
11104 {
11105 ptrs[i].item = li;
11106 ptrs[i].idx = i;
11107 ++i;
11108 }
11109
11110 info.item_compare_func_err = FALSE;
11111 info.item_compare_keep_zero = FALSE;
11112 /* test the compare function */
11113 if ((info.item_compare_func != NULL
11114 || info.item_compare_partial != NULL)
11115 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11116 == ITEM_COMPARE_FAIL)
11117 EMSG(_("E702: Sort compare function failed"));
11118 else
11119 {
11120 /* Sort the array with item pointers. */
11121 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11122 info.item_compare_func == NULL
11123 && info.item_compare_partial == NULL
11124 ? item_compare : item_compare2);
11125
11126 if (!info.item_compare_func_err)
11127 {
11128 /* Clear the List and append the items in sorted order. */
11129 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11130 l->lv_len = 0;
11131 for (i = 0; i < len; ++i)
11132 list_append(l, ptrs[i].item);
11133 }
11134 }
11135 }
11136 else
11137 {
11138 int (*item_compare_func_ptr)(const void *, const void *);
11139
11140 /* f_uniq(): ptrs will be a stack of items to remove */
11141 info.item_compare_func_err = FALSE;
11142 info.item_compare_keep_zero = TRUE;
11143 item_compare_func_ptr = info.item_compare_func != NULL
11144 || info.item_compare_partial != NULL
11145 ? item_compare2 : item_compare;
11146
11147 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11148 li = li->li_next)
11149 {
11150 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11151 == 0)
11152 ptrs[i++].item = li;
11153 if (info.item_compare_func_err)
11154 {
11155 EMSG(_("E882: Uniq compare function failed"));
11156 break;
11157 }
11158 }
11159
11160 if (!info.item_compare_func_err)
11161 {
11162 while (--i >= 0)
11163 {
11164 li = ptrs[i].item->li_next;
11165 ptrs[i].item->li_next = li->li_next;
11166 if (li->li_next != NULL)
11167 li->li_next->li_prev = ptrs[i].item;
11168 else
11169 l->lv_last = ptrs[i].item;
11170 list_fix_watch(l, li);
11171 listitem_free(li);
11172 l->lv_len--;
11173 }
11174 }
11175 }
11176
11177 vim_free(ptrs);
11178 }
11179theend:
11180 sortinfo = old_sortinfo;
11181}
11182
11183/*
11184 * "sort({list})" function
11185 */
11186 static void
11187f_sort(typval_T *argvars, typval_T *rettv)
11188{
11189 do_sort_uniq(argvars, rettv, TRUE);
11190}
11191
11192/*
11193 * "uniq({list})" function
11194 */
11195 static void
11196f_uniq(typval_T *argvars, typval_T *rettv)
11197{
11198 do_sort_uniq(argvars, rettv, FALSE);
11199}
11200
11201/*
11202 * "soundfold({word})" function
11203 */
11204 static void
11205f_soundfold(typval_T *argvars, typval_T *rettv)
11206{
11207 char_u *s;
11208
11209 rettv->v_type = VAR_STRING;
11210 s = get_tv_string(&argvars[0]);
11211#ifdef FEAT_SPELL
11212 rettv->vval.v_string = eval_soundfold(s);
11213#else
11214 rettv->vval.v_string = vim_strsave(s);
11215#endif
11216}
11217
11218/*
11219 * "spellbadword()" function
11220 */
11221 static void
11222f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11223{
11224 char_u *word = (char_u *)"";
11225 hlf_T attr = HLF_COUNT;
11226 int len = 0;
11227
11228 if (rettv_list_alloc(rettv) == FAIL)
11229 return;
11230
11231#ifdef FEAT_SPELL
11232 if (argvars[0].v_type == VAR_UNKNOWN)
11233 {
11234 /* Find the start and length of the badly spelled word. */
11235 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11236 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011237 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011238 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011239 curwin->w_set_curswant = TRUE;
11240 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011241 }
11242 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11243 {
11244 char_u *str = get_tv_string_chk(&argvars[0]);
11245 int capcol = -1;
11246
11247 if (str != NULL)
11248 {
11249 /* Check the argument for spelling. */
11250 while (*str != NUL)
11251 {
11252 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11253 if (attr != HLF_COUNT)
11254 {
11255 word = str;
11256 break;
11257 }
11258 str += len;
11259 }
11260 }
11261 }
11262#endif
11263
11264 list_append_string(rettv->vval.v_list, word, len);
11265 list_append_string(rettv->vval.v_list, (char_u *)(
11266 attr == HLF_SPB ? "bad" :
11267 attr == HLF_SPR ? "rare" :
11268 attr == HLF_SPL ? "local" :
11269 attr == HLF_SPC ? "caps" :
11270 ""), -1);
11271}
11272
11273/*
11274 * "spellsuggest()" function
11275 */
11276 static void
11277f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11278{
11279#ifdef FEAT_SPELL
11280 char_u *str;
11281 int typeerr = FALSE;
11282 int maxcount;
11283 garray_T ga;
11284 int i;
11285 listitem_T *li;
11286 int need_capital = FALSE;
11287#endif
11288
11289 if (rettv_list_alloc(rettv) == FAIL)
11290 return;
11291
11292#ifdef FEAT_SPELL
11293 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11294 {
11295 str = get_tv_string(&argvars[0]);
11296 if (argvars[1].v_type != VAR_UNKNOWN)
11297 {
11298 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11299 if (maxcount <= 0)
11300 return;
11301 if (argvars[2].v_type != VAR_UNKNOWN)
11302 {
11303 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11304 if (typeerr)
11305 return;
11306 }
11307 }
11308 else
11309 maxcount = 25;
11310
11311 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11312
11313 for (i = 0; i < ga.ga_len; ++i)
11314 {
11315 str = ((char_u **)ga.ga_data)[i];
11316
11317 li = listitem_alloc();
11318 if (li == NULL)
11319 vim_free(str);
11320 else
11321 {
11322 li->li_tv.v_type = VAR_STRING;
11323 li->li_tv.v_lock = 0;
11324 li->li_tv.vval.v_string = str;
11325 list_append(rettv->vval.v_list, li);
11326 }
11327 }
11328 ga_clear(&ga);
11329 }
11330#endif
11331}
11332
11333 static void
11334f_split(typval_T *argvars, typval_T *rettv)
11335{
11336 char_u *str;
11337 char_u *end;
11338 char_u *pat = NULL;
11339 regmatch_T regmatch;
11340 char_u patbuf[NUMBUFLEN];
11341 char_u *save_cpo;
11342 int match;
11343 colnr_T col = 0;
11344 int keepempty = FALSE;
11345 int typeerr = FALSE;
11346
11347 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11348 save_cpo = p_cpo;
11349 p_cpo = (char_u *)"";
11350
11351 str = get_tv_string(&argvars[0]);
11352 if (argvars[1].v_type != VAR_UNKNOWN)
11353 {
11354 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11355 if (pat == NULL)
11356 typeerr = TRUE;
11357 if (argvars[2].v_type != VAR_UNKNOWN)
11358 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11359 }
11360 if (pat == NULL || *pat == NUL)
11361 pat = (char_u *)"[\\x01- ]\\+";
11362
11363 if (rettv_list_alloc(rettv) == FAIL)
11364 return;
11365 if (typeerr)
11366 return;
11367
11368 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11369 if (regmatch.regprog != NULL)
11370 {
11371 regmatch.rm_ic = FALSE;
11372 while (*str != NUL || keepempty)
11373 {
11374 if (*str == NUL)
11375 match = FALSE; /* empty item at the end */
11376 else
11377 match = vim_regexec_nl(&regmatch, str, col);
11378 if (match)
11379 end = regmatch.startp[0];
11380 else
11381 end = str + STRLEN(str);
11382 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11383 && *str != NUL && match && end < regmatch.endp[0]))
11384 {
11385 if (list_append_string(rettv->vval.v_list, str,
11386 (int)(end - str)) == FAIL)
11387 break;
11388 }
11389 if (!match)
11390 break;
11391 /* Advance to just after the match. */
11392 if (regmatch.endp[0] > str)
11393 col = 0;
11394 else
11395 {
11396 /* Don't get stuck at the same match. */
11397#ifdef FEAT_MBYTE
11398 col = (*mb_ptr2len)(regmatch.endp[0]);
11399#else
11400 col = 1;
11401#endif
11402 }
11403 str = regmatch.endp[0];
11404 }
11405
11406 vim_regfree(regmatch.regprog);
11407 }
11408
11409 p_cpo = save_cpo;
11410}
11411
11412#ifdef FEAT_FLOAT
11413/*
11414 * "sqrt()" function
11415 */
11416 static void
11417f_sqrt(typval_T *argvars, typval_T *rettv)
11418{
11419 float_T f = 0.0;
11420
11421 rettv->v_type = VAR_FLOAT;
11422 if (get_float_arg(argvars, &f) == OK)
11423 rettv->vval.v_float = sqrt(f);
11424 else
11425 rettv->vval.v_float = 0.0;
11426}
11427
11428/*
11429 * "str2float()" function
11430 */
11431 static void
11432f_str2float(typval_T *argvars, typval_T *rettv)
11433{
11434 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011435 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011436
Bram Moolenaar08243d22017-01-10 16:12:29 +010011437 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011438 p = skipwhite(p + 1);
11439 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011440 if (isneg)
11441 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011442 rettv->v_type = VAR_FLOAT;
11443}
11444#endif
11445
11446/*
11447 * "str2nr()" function
11448 */
11449 static void
11450f_str2nr(typval_T *argvars, typval_T *rettv)
11451{
11452 int base = 10;
11453 char_u *p;
11454 varnumber_T n;
11455 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011456 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011457
11458 if (argvars[1].v_type != VAR_UNKNOWN)
11459 {
11460 base = (int)get_tv_number(&argvars[1]);
11461 if (base != 2 && base != 8 && base != 10 && base != 16)
11462 {
11463 EMSG(_(e_invarg));
11464 return;
11465 }
11466 }
11467
11468 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011469 isneg = (*p == '-');
11470 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011471 p = skipwhite(p + 1);
11472 switch (base)
11473 {
11474 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11475 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11476 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11477 default: what = 0;
11478 }
11479 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011480 if (isneg)
11481 rettv->vval.v_number = -n;
11482 else
11483 rettv->vval.v_number = n;
11484
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011485}
11486
11487#ifdef HAVE_STRFTIME
11488/*
11489 * "strftime({format}[, {time}])" function
11490 */
11491 static void
11492f_strftime(typval_T *argvars, typval_T *rettv)
11493{
11494 char_u result_buf[256];
11495 struct tm *curtime;
11496 time_t seconds;
11497 char_u *p;
11498
11499 rettv->v_type = VAR_STRING;
11500
11501 p = get_tv_string(&argvars[0]);
11502 if (argvars[1].v_type == VAR_UNKNOWN)
11503 seconds = time(NULL);
11504 else
11505 seconds = (time_t)get_tv_number(&argvars[1]);
11506 curtime = localtime(&seconds);
11507 /* MSVC returns NULL for an invalid value of seconds. */
11508 if (curtime == NULL)
11509 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11510 else
11511 {
11512# ifdef FEAT_MBYTE
11513 vimconv_T conv;
11514 char_u *enc;
11515
11516 conv.vc_type = CONV_NONE;
11517 enc = enc_locale();
11518 convert_setup(&conv, p_enc, enc);
11519 if (conv.vc_type != CONV_NONE)
11520 p = string_convert(&conv, p, NULL);
11521# endif
11522 if (p != NULL)
11523 (void)strftime((char *)result_buf, sizeof(result_buf),
11524 (char *)p, curtime);
11525 else
11526 result_buf[0] = NUL;
11527
11528# ifdef FEAT_MBYTE
11529 if (conv.vc_type != CONV_NONE)
11530 vim_free(p);
11531 convert_setup(&conv, enc, p_enc);
11532 if (conv.vc_type != CONV_NONE)
11533 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11534 else
11535# endif
11536 rettv->vval.v_string = vim_strsave(result_buf);
11537
11538# ifdef FEAT_MBYTE
11539 /* Release conversion descriptors */
11540 convert_setup(&conv, NULL, NULL);
11541 vim_free(enc);
11542# endif
11543 }
11544}
11545#endif
11546
11547/*
11548 * "strgetchar()" function
11549 */
11550 static void
11551f_strgetchar(typval_T *argvars, typval_T *rettv)
11552{
11553 char_u *str;
11554 int len;
11555 int error = FALSE;
11556 int charidx;
11557
11558 rettv->vval.v_number = -1;
11559 str = get_tv_string_chk(&argvars[0]);
11560 if (str == NULL)
11561 return;
11562 len = (int)STRLEN(str);
11563 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11564 if (error)
11565 return;
11566#ifdef FEAT_MBYTE
11567 {
11568 int byteidx = 0;
11569
11570 while (charidx >= 0 && byteidx < len)
11571 {
11572 if (charidx == 0)
11573 {
11574 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11575 break;
11576 }
11577 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011578 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011579 }
11580 }
11581#else
11582 if (charidx < len)
11583 rettv->vval.v_number = str[charidx];
11584#endif
11585}
11586
11587/*
11588 * "stridx()" function
11589 */
11590 static void
11591f_stridx(typval_T *argvars, typval_T *rettv)
11592{
11593 char_u buf[NUMBUFLEN];
11594 char_u *needle;
11595 char_u *haystack;
11596 char_u *save_haystack;
11597 char_u *pos;
11598 int start_idx;
11599
11600 needle = get_tv_string_chk(&argvars[1]);
11601 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11602 rettv->vval.v_number = -1;
11603 if (needle == NULL || haystack == NULL)
11604 return; /* type error; errmsg already given */
11605
11606 if (argvars[2].v_type != VAR_UNKNOWN)
11607 {
11608 int error = FALSE;
11609
11610 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11611 if (error || start_idx >= (int)STRLEN(haystack))
11612 return;
11613 if (start_idx >= 0)
11614 haystack += start_idx;
11615 }
11616
11617 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11618 if (pos != NULL)
11619 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11620}
11621
11622/*
11623 * "string()" function
11624 */
11625 static void
11626f_string(typval_T *argvars, typval_T *rettv)
11627{
11628 char_u *tofree;
11629 char_u numbuf[NUMBUFLEN];
11630
11631 rettv->v_type = VAR_STRING;
11632 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11633 get_copyID());
11634 /* Make a copy if we have a value but it's not in allocated memory. */
11635 if (rettv->vval.v_string != NULL && tofree == NULL)
11636 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11637}
11638
11639/*
11640 * "strlen()" function
11641 */
11642 static void
11643f_strlen(typval_T *argvars, typval_T *rettv)
11644{
11645 rettv->vval.v_number = (varnumber_T)(STRLEN(
11646 get_tv_string(&argvars[0])));
11647}
11648
11649/*
11650 * "strchars()" function
11651 */
11652 static void
11653f_strchars(typval_T *argvars, typval_T *rettv)
11654{
11655 char_u *s = get_tv_string(&argvars[0]);
11656 int skipcc = 0;
11657#ifdef FEAT_MBYTE
11658 varnumber_T len = 0;
11659 int (*func_mb_ptr2char_adv)(char_u **pp);
11660#endif
11661
11662 if (argvars[1].v_type != VAR_UNKNOWN)
11663 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11664 if (skipcc < 0 || skipcc > 1)
11665 EMSG(_(e_invarg));
11666 else
11667 {
11668#ifdef FEAT_MBYTE
11669 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11670 while (*s != NUL)
11671 {
11672 func_mb_ptr2char_adv(&s);
11673 ++len;
11674 }
11675 rettv->vval.v_number = len;
11676#else
11677 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11678#endif
11679 }
11680}
11681
11682/*
11683 * "strdisplaywidth()" function
11684 */
11685 static void
11686f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11687{
11688 char_u *s = get_tv_string(&argvars[0]);
11689 int col = 0;
11690
11691 if (argvars[1].v_type != VAR_UNKNOWN)
11692 col = (int)get_tv_number(&argvars[1]);
11693
11694 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11695}
11696
11697/*
11698 * "strwidth()" function
11699 */
11700 static void
11701f_strwidth(typval_T *argvars, typval_T *rettv)
11702{
11703 char_u *s = get_tv_string(&argvars[0]);
11704
11705 rettv->vval.v_number = (varnumber_T)(
11706#ifdef FEAT_MBYTE
11707 mb_string2cells(s, -1)
11708#else
11709 STRLEN(s)
11710#endif
11711 );
11712}
11713
11714/*
11715 * "strcharpart()" function
11716 */
11717 static void
11718f_strcharpart(typval_T *argvars, typval_T *rettv)
11719{
11720#ifdef FEAT_MBYTE
11721 char_u *p;
11722 int nchar;
11723 int nbyte = 0;
11724 int charlen;
11725 int len = 0;
11726 int slen;
11727 int error = FALSE;
11728
11729 p = get_tv_string(&argvars[0]);
11730 slen = (int)STRLEN(p);
11731
11732 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11733 if (!error)
11734 {
11735 if (nchar > 0)
11736 while (nchar > 0 && nbyte < slen)
11737 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011738 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011739 --nchar;
11740 }
11741 else
11742 nbyte = nchar;
11743 if (argvars[2].v_type != VAR_UNKNOWN)
11744 {
11745 charlen = (int)get_tv_number(&argvars[2]);
11746 while (charlen > 0 && nbyte + len < slen)
11747 {
11748 int off = nbyte + len;
11749
11750 if (off < 0)
11751 len += 1;
11752 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011753 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011754 --charlen;
11755 }
11756 }
11757 else
11758 len = slen - nbyte; /* default: all bytes that are available. */
11759 }
11760
11761 /*
11762 * Only return the overlap between the specified part and the actual
11763 * string.
11764 */
11765 if (nbyte < 0)
11766 {
11767 len += nbyte;
11768 nbyte = 0;
11769 }
11770 else if (nbyte > slen)
11771 nbyte = slen;
11772 if (len < 0)
11773 len = 0;
11774 else if (nbyte + len > slen)
11775 len = slen - nbyte;
11776
11777 rettv->v_type = VAR_STRING;
11778 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11779#else
11780 f_strpart(argvars, rettv);
11781#endif
11782}
11783
11784/*
11785 * "strpart()" function
11786 */
11787 static void
11788f_strpart(typval_T *argvars, typval_T *rettv)
11789{
11790 char_u *p;
11791 int n;
11792 int len;
11793 int slen;
11794 int error = FALSE;
11795
11796 p = get_tv_string(&argvars[0]);
11797 slen = (int)STRLEN(p);
11798
11799 n = (int)get_tv_number_chk(&argvars[1], &error);
11800 if (error)
11801 len = 0;
11802 else if (argvars[2].v_type != VAR_UNKNOWN)
11803 len = (int)get_tv_number(&argvars[2]);
11804 else
11805 len = slen - n; /* default len: all bytes that are available. */
11806
11807 /*
11808 * Only return the overlap between the specified part and the actual
11809 * string.
11810 */
11811 if (n < 0)
11812 {
11813 len += n;
11814 n = 0;
11815 }
11816 else if (n > slen)
11817 n = slen;
11818 if (len < 0)
11819 len = 0;
11820 else if (n + len > slen)
11821 len = slen - n;
11822
11823 rettv->v_type = VAR_STRING;
11824 rettv->vval.v_string = vim_strnsave(p + n, len);
11825}
11826
11827/*
11828 * "strridx()" function
11829 */
11830 static void
11831f_strridx(typval_T *argvars, typval_T *rettv)
11832{
11833 char_u buf[NUMBUFLEN];
11834 char_u *needle;
11835 char_u *haystack;
11836 char_u *rest;
11837 char_u *lastmatch = NULL;
11838 int haystack_len, end_idx;
11839
11840 needle = get_tv_string_chk(&argvars[1]);
11841 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11842
11843 rettv->vval.v_number = -1;
11844 if (needle == NULL || haystack == NULL)
11845 return; /* type error; errmsg already given */
11846
11847 haystack_len = (int)STRLEN(haystack);
11848 if (argvars[2].v_type != VAR_UNKNOWN)
11849 {
11850 /* Third argument: upper limit for index */
11851 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11852 if (end_idx < 0)
11853 return; /* can never find a match */
11854 }
11855 else
11856 end_idx = haystack_len;
11857
11858 if (*needle == NUL)
11859 {
11860 /* Empty string matches past the end. */
11861 lastmatch = haystack + end_idx;
11862 }
11863 else
11864 {
11865 for (rest = haystack; *rest != '\0'; ++rest)
11866 {
11867 rest = (char_u *)strstr((char *)rest, (char *)needle);
11868 if (rest == NULL || rest > haystack + end_idx)
11869 break;
11870 lastmatch = rest;
11871 }
11872 }
11873
11874 if (lastmatch == NULL)
11875 rettv->vval.v_number = -1;
11876 else
11877 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11878}
11879
11880/*
11881 * "strtrans()" function
11882 */
11883 static void
11884f_strtrans(typval_T *argvars, typval_T *rettv)
11885{
11886 rettv->v_type = VAR_STRING;
11887 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11888}
11889
11890/*
11891 * "submatch()" function
11892 */
11893 static void
11894f_submatch(typval_T *argvars, typval_T *rettv)
11895{
11896 int error = FALSE;
11897 int no;
11898 int retList = 0;
11899
11900 no = (int)get_tv_number_chk(&argvars[0], &error);
11901 if (error)
11902 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011903 if (no < 0 || no >= NSUBEXP)
11904 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010011905 EMSGN(_("E935: invalid submatch number: %d"), no);
11906 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011907 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011908 if (argvars[1].v_type != VAR_UNKNOWN)
11909 retList = (int)get_tv_number_chk(&argvars[1], &error);
11910 if (error)
11911 return;
11912
11913 if (retList == 0)
11914 {
11915 rettv->v_type = VAR_STRING;
11916 rettv->vval.v_string = reg_submatch(no);
11917 }
11918 else
11919 {
11920 rettv->v_type = VAR_LIST;
11921 rettv->vval.v_list = reg_submatch_list(no);
11922 }
11923}
11924
11925/*
11926 * "substitute()" function
11927 */
11928 static void
11929f_substitute(typval_T *argvars, typval_T *rettv)
11930{
11931 char_u patbuf[NUMBUFLEN];
11932 char_u subbuf[NUMBUFLEN];
11933 char_u flagsbuf[NUMBUFLEN];
11934
11935 char_u *str = get_tv_string_chk(&argvars[0]);
11936 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011937 char_u *sub = NULL;
11938 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011939 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11940
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011941 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11942 expr = &argvars[2];
11943 else
11944 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11945
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011946 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011947 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11948 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011949 rettv->vval.v_string = NULL;
11950 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011951 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011952}
11953
11954/*
11955 * "synID(lnum, col, trans)" function
11956 */
11957 static void
11958f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11959{
11960 int id = 0;
11961#ifdef FEAT_SYN_HL
11962 linenr_T lnum;
11963 colnr_T col;
11964 int trans;
11965 int transerr = FALSE;
11966
11967 lnum = get_tv_lnum(argvars); /* -1 on type error */
11968 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11969 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
11970
11971 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11972 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11973 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11974#endif
11975
11976 rettv->vval.v_number = id;
11977}
11978
11979/*
11980 * "synIDattr(id, what [, mode])" function
11981 */
11982 static void
11983f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11984{
11985 char_u *p = NULL;
11986#ifdef FEAT_SYN_HL
11987 int id;
11988 char_u *what;
11989 char_u *mode;
11990 char_u modebuf[NUMBUFLEN];
11991 int modec;
11992
11993 id = (int)get_tv_number(&argvars[0]);
11994 what = get_tv_string(&argvars[1]);
11995 if (argvars[2].v_type != VAR_UNKNOWN)
11996 {
11997 mode = get_tv_string_buf(&argvars[2], modebuf);
11998 modec = TOLOWER_ASC(mode[0]);
11999 if (modec != 't' && modec != 'c' && modec != 'g')
12000 modec = 0; /* replace invalid with current */
12001 }
12002 else
12003 {
12004#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12005 if (USE_24BIT)
12006 modec = 'g';
12007 else
12008#endif
12009 if (t_colors > 1)
12010 modec = 'c';
12011 else
12012 modec = 't';
12013 }
12014
12015
12016 switch (TOLOWER_ASC(what[0]))
12017 {
12018 case 'b':
12019 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12020 p = highlight_color(id, what, modec);
12021 else /* bold */
12022 p = highlight_has_attr(id, HL_BOLD, modec);
12023 break;
12024
12025 case 'f': /* fg[#] or font */
12026 p = highlight_color(id, what, modec);
12027 break;
12028
12029 case 'i':
12030 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12031 p = highlight_has_attr(id, HL_INVERSE, modec);
12032 else /* italic */
12033 p = highlight_has_attr(id, HL_ITALIC, modec);
12034 break;
12035
12036 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012037 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012038 break;
12039
12040 case 'r': /* reverse */
12041 p = highlight_has_attr(id, HL_INVERSE, modec);
12042 break;
12043
12044 case 's':
12045 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12046 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012047 /* strikeout */
12048 else if (TOLOWER_ASC(what[1]) == 't' &&
12049 TOLOWER_ASC(what[2]) == 'r')
12050 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012051 else /* standout */
12052 p = highlight_has_attr(id, HL_STANDOUT, modec);
12053 break;
12054
12055 case 'u':
12056 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12057 /* underline */
12058 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12059 else
12060 /* undercurl */
12061 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12062 break;
12063 }
12064
12065 if (p != NULL)
12066 p = vim_strsave(p);
12067#endif
12068 rettv->v_type = VAR_STRING;
12069 rettv->vval.v_string = p;
12070}
12071
12072/*
12073 * "synIDtrans(id)" function
12074 */
12075 static void
12076f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12077{
12078 int id;
12079
12080#ifdef FEAT_SYN_HL
12081 id = (int)get_tv_number(&argvars[0]);
12082
12083 if (id > 0)
12084 id = syn_get_final_id(id);
12085 else
12086#endif
12087 id = 0;
12088
12089 rettv->vval.v_number = id;
12090}
12091
12092/*
12093 * "synconcealed(lnum, col)" function
12094 */
12095 static void
12096f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12097{
12098#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12099 linenr_T lnum;
12100 colnr_T col;
12101 int syntax_flags = 0;
12102 int cchar;
12103 int matchid = 0;
12104 char_u str[NUMBUFLEN];
12105#endif
12106
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012107 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012108
12109#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12110 lnum = get_tv_lnum(argvars); /* -1 on type error */
12111 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12112
12113 vim_memset(str, NUL, sizeof(str));
12114
12115 if (rettv_list_alloc(rettv) != FAIL)
12116 {
12117 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12118 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12119 && curwin->w_p_cole > 0)
12120 {
12121 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12122 syntax_flags = get_syntax_info(&matchid);
12123
12124 /* get the conceal character */
12125 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12126 {
12127 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012128 if (cchar == NUL && curwin->w_p_cole == 1)
12129 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012130 if (cchar != NUL)
12131 {
12132# ifdef FEAT_MBYTE
12133 if (has_mbyte)
12134 (*mb_char2bytes)(cchar, str);
12135 else
12136# endif
12137 str[0] = cchar;
12138 }
12139 }
12140 }
12141
12142 list_append_number(rettv->vval.v_list,
12143 (syntax_flags & HL_CONCEAL) != 0);
12144 /* -1 to auto-determine strlen */
12145 list_append_string(rettv->vval.v_list, str, -1);
12146 list_append_number(rettv->vval.v_list, matchid);
12147 }
12148#endif
12149}
12150
12151/*
12152 * "synstack(lnum, col)" function
12153 */
12154 static void
12155f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12156{
12157#ifdef FEAT_SYN_HL
12158 linenr_T lnum;
12159 colnr_T col;
12160 int i;
12161 int id;
12162#endif
12163
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012164 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012165
12166#ifdef FEAT_SYN_HL
12167 lnum = get_tv_lnum(argvars); /* -1 on type error */
12168 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12169
12170 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12171 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12172 && rettv_list_alloc(rettv) != FAIL)
12173 {
12174 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12175 for (i = 0; ; ++i)
12176 {
12177 id = syn_get_stack_item(i);
12178 if (id < 0)
12179 break;
12180 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12181 break;
12182 }
12183 }
12184#endif
12185}
12186
12187 static void
12188get_cmd_output_as_rettv(
12189 typval_T *argvars,
12190 typval_T *rettv,
12191 int retlist)
12192{
12193 char_u *res = NULL;
12194 char_u *p;
12195 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012196 int err = FALSE;
12197 FILE *fd;
12198 list_T *list = NULL;
12199 int flags = SHELL_SILENT;
12200
12201 rettv->v_type = VAR_STRING;
12202 rettv->vval.v_string = NULL;
12203 if (check_restricted() || check_secure())
12204 goto errret;
12205
12206 if (argvars[1].v_type != VAR_UNKNOWN)
12207 {
12208 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012209 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012210 * command.
12211 */
12212 if ((infile = vim_tempname('i', TRUE)) == NULL)
12213 {
12214 EMSG(_(e_notmp));
12215 goto errret;
12216 }
12217
12218 fd = mch_fopen((char *)infile, WRITEBIN);
12219 if (fd == NULL)
12220 {
12221 EMSG2(_(e_notopen), infile);
12222 goto errret;
12223 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012224 if (argvars[1].v_type == VAR_NUMBER)
12225 {
12226 linenr_T lnum;
12227 buf_T *buf;
12228
12229 buf = buflist_findnr(argvars[1].vval.v_number);
12230 if (buf == NULL)
12231 {
12232 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012233 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012234 goto errret;
12235 }
12236
12237 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12238 {
12239 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12240 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12241 {
12242 err = TRUE;
12243 break;
12244 }
12245 if (putc(NL, fd) == EOF)
12246 {
12247 err = TRUE;
12248 break;
12249 }
12250 }
12251 }
12252 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012253 {
12254 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12255 err = TRUE;
12256 }
12257 else
12258 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012259 size_t len;
12260 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012261
12262 p = get_tv_string_buf_chk(&argvars[1], buf);
12263 if (p == NULL)
12264 {
12265 fclose(fd);
12266 goto errret; /* type error; errmsg already given */
12267 }
12268 len = STRLEN(p);
12269 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12270 err = TRUE;
12271 }
12272 if (fclose(fd) != 0)
12273 err = TRUE;
12274 if (err)
12275 {
12276 EMSG(_("E677: Error writing temp file"));
12277 goto errret;
12278 }
12279 }
12280
12281 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12282 * echoes typeahead, that messes up the display. */
12283 if (!msg_silent)
12284 flags += SHELL_COOKED;
12285
12286 if (retlist)
12287 {
12288 int len;
12289 listitem_T *li;
12290 char_u *s = NULL;
12291 char_u *start;
12292 char_u *end;
12293 int i;
12294
12295 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12296 if (res == NULL)
12297 goto errret;
12298
12299 list = list_alloc();
12300 if (list == NULL)
12301 goto errret;
12302
12303 for (i = 0; i < len; ++i)
12304 {
12305 start = res + i;
12306 while (i < len && res[i] != NL)
12307 ++i;
12308 end = res + i;
12309
12310 s = alloc((unsigned)(end - start + 1));
12311 if (s == NULL)
12312 goto errret;
12313
12314 for (p = s; start < end; ++p, ++start)
12315 *p = *start == NUL ? NL : *start;
12316 *p = NUL;
12317
12318 li = listitem_alloc();
12319 if (li == NULL)
12320 {
12321 vim_free(s);
12322 goto errret;
12323 }
12324 li->li_tv.v_type = VAR_STRING;
12325 li->li_tv.v_lock = 0;
12326 li->li_tv.vval.v_string = s;
12327 list_append(list, li);
12328 }
12329
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012330 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012331 list = NULL;
12332 }
12333 else
12334 {
12335 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12336#ifdef USE_CR
12337 /* translate <CR> into <NL> */
12338 if (res != NULL)
12339 {
12340 char_u *s;
12341
12342 for (s = res; *s; ++s)
12343 {
12344 if (*s == CAR)
12345 *s = NL;
12346 }
12347 }
12348#else
12349# ifdef USE_CRNL
12350 /* translate <CR><NL> into <NL> */
12351 if (res != NULL)
12352 {
12353 char_u *s, *d;
12354
12355 d = res;
12356 for (s = res; *s; ++s)
12357 {
12358 if (s[0] == CAR && s[1] == NL)
12359 ++s;
12360 *d++ = *s;
12361 }
12362 *d = NUL;
12363 }
12364# endif
12365#endif
12366 rettv->vval.v_string = res;
12367 res = NULL;
12368 }
12369
12370errret:
12371 if (infile != NULL)
12372 {
12373 mch_remove(infile);
12374 vim_free(infile);
12375 }
12376 if (res != NULL)
12377 vim_free(res);
12378 if (list != NULL)
12379 list_free(list);
12380}
12381
12382/*
12383 * "system()" function
12384 */
12385 static void
12386f_system(typval_T *argvars, typval_T *rettv)
12387{
12388 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12389}
12390
12391/*
12392 * "systemlist()" function
12393 */
12394 static void
12395f_systemlist(typval_T *argvars, typval_T *rettv)
12396{
12397 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12398}
12399
12400/*
12401 * "tabpagebuflist()" function
12402 */
12403 static void
12404f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12405{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012406 tabpage_T *tp;
12407 win_T *wp = NULL;
12408
12409 if (argvars[0].v_type == VAR_UNKNOWN)
12410 wp = firstwin;
12411 else
12412 {
12413 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12414 if (tp != NULL)
12415 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12416 }
12417 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12418 {
12419 for (; wp != NULL; wp = wp->w_next)
12420 if (list_append_number(rettv->vval.v_list,
12421 wp->w_buffer->b_fnum) == FAIL)
12422 break;
12423 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012424}
12425
12426
12427/*
12428 * "tabpagenr()" function
12429 */
12430 static void
12431f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12432{
12433 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012434 char_u *arg;
12435
12436 if (argvars[0].v_type != VAR_UNKNOWN)
12437 {
12438 arg = get_tv_string_chk(&argvars[0]);
12439 nr = 0;
12440 if (arg != NULL)
12441 {
12442 if (STRCMP(arg, "$") == 0)
12443 nr = tabpage_index(NULL) - 1;
12444 else
12445 EMSG2(_(e_invexpr2), arg);
12446 }
12447 }
12448 else
12449 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012450 rettv->vval.v_number = nr;
12451}
12452
12453
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012454static int get_winnr(tabpage_T *tp, typval_T *argvar);
12455
12456/*
12457 * Common code for tabpagewinnr() and winnr().
12458 */
12459 static int
12460get_winnr(tabpage_T *tp, typval_T *argvar)
12461{
12462 win_T *twin;
12463 int nr = 1;
12464 win_T *wp;
12465 char_u *arg;
12466
12467 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12468 if (argvar->v_type != VAR_UNKNOWN)
12469 {
12470 arg = get_tv_string_chk(argvar);
12471 if (arg == NULL)
12472 nr = 0; /* type error; errmsg already given */
12473 else if (STRCMP(arg, "$") == 0)
12474 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12475 else if (STRCMP(arg, "#") == 0)
12476 {
12477 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12478 if (twin == NULL)
12479 nr = 0;
12480 }
12481 else
12482 {
12483 EMSG2(_(e_invexpr2), arg);
12484 nr = 0;
12485 }
12486 }
12487
12488 if (nr > 0)
12489 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12490 wp != twin; wp = wp->w_next)
12491 {
12492 if (wp == NULL)
12493 {
12494 /* didn't find it in this tabpage */
12495 nr = 0;
12496 break;
12497 }
12498 ++nr;
12499 }
12500 return nr;
12501}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012502
12503/*
12504 * "tabpagewinnr()" function
12505 */
12506 static void
12507f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12508{
12509 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012510 tabpage_T *tp;
12511
12512 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12513 if (tp == NULL)
12514 nr = 0;
12515 else
12516 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012517 rettv->vval.v_number = nr;
12518}
12519
12520
12521/*
12522 * "tagfiles()" function
12523 */
12524 static void
12525f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12526{
12527 char_u *fname;
12528 tagname_T tn;
12529 int first;
12530
12531 if (rettv_list_alloc(rettv) == FAIL)
12532 return;
12533 fname = alloc(MAXPATHL);
12534 if (fname == NULL)
12535 return;
12536
12537 for (first = TRUE; ; first = FALSE)
12538 if (get_tagfname(&tn, first, fname) == FAIL
12539 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12540 break;
12541 tagname_free(&tn);
12542 vim_free(fname);
12543}
12544
12545/*
12546 * "taglist()" function
12547 */
12548 static void
12549f_taglist(typval_T *argvars, typval_T *rettv)
12550{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012551 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012552 char_u *tag_pattern;
12553
12554 tag_pattern = get_tv_string(&argvars[0]);
12555
12556 rettv->vval.v_number = FALSE;
12557 if (*tag_pattern == NUL)
12558 return;
12559
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012560 if (argvars[1].v_type != VAR_UNKNOWN)
12561 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012562 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012563 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012564}
12565
12566/*
12567 * "tempname()" function
12568 */
12569 static void
12570f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12571{
12572 static int x = 'A';
12573
12574 rettv->v_type = VAR_STRING;
12575 rettv->vval.v_string = vim_tempname(x, FALSE);
12576
12577 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12578 * names. Skip 'I' and 'O', they are used for shell redirection. */
12579 do
12580 {
12581 if (x == 'Z')
12582 x = '0';
12583 else if (x == '9')
12584 x = 'A';
12585 else
12586 {
12587#ifdef EBCDIC
12588 if (x == 'I')
12589 x = 'J';
12590 else if (x == 'R')
12591 x = 'S';
12592 else
12593#endif
12594 ++x;
12595 }
12596 } while (x == 'I' || x == 'O');
12597}
12598
12599#ifdef FEAT_FLOAT
12600/*
12601 * "tan()" function
12602 */
12603 static void
12604f_tan(typval_T *argvars, typval_T *rettv)
12605{
12606 float_T f = 0.0;
12607
12608 rettv->v_type = VAR_FLOAT;
12609 if (get_float_arg(argvars, &f) == OK)
12610 rettv->vval.v_float = tan(f);
12611 else
12612 rettv->vval.v_float = 0.0;
12613}
12614
12615/*
12616 * "tanh()" function
12617 */
12618 static void
12619f_tanh(typval_T *argvars, typval_T *rettv)
12620{
12621 float_T f = 0.0;
12622
12623 rettv->v_type = VAR_FLOAT;
12624 if (get_float_arg(argvars, &f) == OK)
12625 rettv->vval.v_float = tanh(f);
12626 else
12627 rettv->vval.v_float = 0.0;
12628}
12629#endif
12630
12631/*
12632 * "test_alloc_fail(id, countdown, repeat)" function
12633 */
12634 static void
12635f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12636{
12637 if (argvars[0].v_type != VAR_NUMBER
12638 || argvars[0].vval.v_number <= 0
12639 || argvars[1].v_type != VAR_NUMBER
12640 || argvars[1].vval.v_number < 0
12641 || argvars[2].v_type != VAR_NUMBER)
12642 EMSG(_(e_invarg));
12643 else
12644 {
12645 alloc_fail_id = argvars[0].vval.v_number;
12646 if (alloc_fail_id >= aid_last)
12647 EMSG(_(e_invarg));
12648 alloc_fail_countdown = argvars[1].vval.v_number;
12649 alloc_fail_repeat = argvars[2].vval.v_number;
12650 did_outofmem_msg = FALSE;
12651 }
12652}
12653
12654/*
12655 * "test_autochdir()"
12656 */
12657 static void
12658f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12659{
12660#if defined(FEAT_AUTOCHDIR)
12661 test_autochdir = TRUE;
12662#endif
12663}
12664
12665/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012666 * "test_feedinput()"
12667 */
12668 static void
12669f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12670{
12671#ifdef USE_INPUT_BUF
12672 char_u *val = get_tv_string_chk(&argvars[0]);
12673
12674 if (val != NULL)
12675 {
12676 trash_input_buf();
12677 add_to_input_buf_csi(val, (int)STRLEN(val));
12678 }
12679#endif
12680}
12681
12682/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012683 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012684 */
12685 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012686f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012687{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012688 char_u *name = (char_u *)"";
12689 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012690 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012691
12692 if (argvars[0].v_type != VAR_STRING
12693 || (argvars[1].v_type) != VAR_NUMBER)
12694 EMSG(_(e_invarg));
12695 else
12696 {
12697 name = get_tv_string_chk(&argvars[0]);
12698 val = (int)get_tv_number(&argvars[1]);
12699
12700 if (STRCMP(name, (char_u *)"redraw") == 0)
12701 disable_redraw_for_testing = val;
12702 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12703 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012704 else if (STRCMP(name, (char_u *)"starting") == 0)
12705 {
12706 if (val)
12707 {
12708 if (save_starting < 0)
12709 save_starting = starting;
12710 starting = 0;
12711 }
12712 else
12713 {
12714 starting = save_starting;
12715 save_starting = -1;
12716 }
12717 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012718 else if (STRCMP(name, (char_u *)"ALL") == 0)
12719 {
12720 disable_char_avail_for_testing = FALSE;
12721 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012722 if (save_starting >= 0)
12723 {
12724 starting = save_starting;
12725 save_starting = -1;
12726 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012727 }
12728 else
12729 EMSG2(_(e_invarg2), name);
12730 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012731}
12732
12733/*
12734 * "test_garbagecollect_now()" function
12735 */
12736 static void
12737f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12738{
12739 /* This is dangerous, any Lists and Dicts used internally may be freed
12740 * while still in use. */
12741 garbage_collect(TRUE);
12742}
12743
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012744/*
12745 * "test_ignore_error()" function
12746 */
12747 static void
12748f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12749{
12750 ignore_error_for_testing(get_tv_string(&argvars[0]));
12751}
12752
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012753#ifdef FEAT_JOB_CHANNEL
12754 static void
12755f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12756{
12757 rettv->v_type = VAR_CHANNEL;
12758 rettv->vval.v_channel = NULL;
12759}
12760#endif
12761
12762 static void
12763f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12764{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012765 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012766}
12767
12768#ifdef FEAT_JOB_CHANNEL
12769 static void
12770f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12771{
12772 rettv->v_type = VAR_JOB;
12773 rettv->vval.v_job = NULL;
12774}
12775#endif
12776
12777 static void
12778f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12779{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012780 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012781}
12782
12783 static void
12784f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12785{
12786 rettv->v_type = VAR_PARTIAL;
12787 rettv->vval.v_partial = NULL;
12788}
12789
12790 static void
12791f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12792{
12793 rettv->v_type = VAR_STRING;
12794 rettv->vval.v_string = NULL;
12795}
12796
12797 static void
12798f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12799{
12800 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12801}
12802
12803#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12804/*
12805 * Get a callback from "arg". It can be a Funcref or a function name.
12806 * When "arg" is zero return an empty string.
12807 * Return NULL for an invalid argument.
12808 */
12809 char_u *
12810get_callback(typval_T *arg, partial_T **pp)
12811{
12812 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12813 {
12814 *pp = arg->vval.v_partial;
12815 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012816 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012817 }
12818 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012819 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012820 {
12821 func_ref(arg->vval.v_string);
12822 return arg->vval.v_string;
12823 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012824 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12825 return (char_u *)"";
12826 EMSG(_("E921: Invalid callback argument"));
12827 return NULL;
12828}
12829
12830/*
12831 * Unref/free "callback" and "partial" retured by get_callback().
12832 */
12833 void
12834free_callback(char_u *callback, partial_T *partial)
12835{
12836 if (partial != NULL)
12837 partial_unref(partial);
12838 else if (callback != NULL)
12839 {
12840 func_unref(callback);
12841 vim_free(callback);
12842 }
12843}
12844#endif
12845
12846#ifdef FEAT_TIMERS
12847/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012848 * "timer_info([timer])" function
12849 */
12850 static void
12851f_timer_info(typval_T *argvars, typval_T *rettv)
12852{
12853 timer_T *timer = NULL;
12854
12855 if (rettv_list_alloc(rettv) != OK)
12856 return;
12857 if (argvars[0].v_type != VAR_UNKNOWN)
12858 {
12859 if (argvars[0].v_type != VAR_NUMBER)
12860 EMSG(_(e_number_exp));
12861 else
12862 {
12863 timer = find_timer((int)get_tv_number(&argvars[0]));
12864 if (timer != NULL)
12865 add_timer_info(rettv, timer);
12866 }
12867 }
12868 else
12869 add_timer_info_all(rettv);
12870}
12871
12872/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012873 * "timer_pause(timer, paused)" function
12874 */
12875 static void
12876f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12877{
12878 timer_T *timer = NULL;
12879 int paused = (int)get_tv_number(&argvars[1]);
12880
12881 if (argvars[0].v_type != VAR_NUMBER)
12882 EMSG(_(e_number_exp));
12883 else
12884 {
12885 timer = find_timer((int)get_tv_number(&argvars[0]));
12886 if (timer != NULL)
12887 timer->tr_paused = paused;
12888 }
12889}
12890
12891/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012892 * "timer_start(time, callback [, options])" function
12893 */
12894 static void
12895f_timer_start(typval_T *argvars, typval_T *rettv)
12896{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012897 long msec = (long)get_tv_number(&argvars[0]);
12898 timer_T *timer;
12899 int repeat = 0;
12900 char_u *callback;
12901 dict_T *dict;
12902 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012903
Bram Moolenaar75537a92016-09-05 22:45:28 +020012904 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012905 if (check_secure())
12906 return;
12907 if (argvars[2].v_type != VAR_UNKNOWN)
12908 {
12909 if (argvars[2].v_type != VAR_DICT
12910 || (dict = argvars[2].vval.v_dict) == NULL)
12911 {
12912 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12913 return;
12914 }
12915 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12916 repeat = get_dict_number(dict, (char_u *)"repeat");
12917 }
12918
Bram Moolenaar75537a92016-09-05 22:45:28 +020012919 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012920 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012921 return;
12922
12923 timer = create_timer(msec, repeat);
12924 if (timer == NULL)
12925 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012926 else
12927 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012928 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012929 timer->tr_callback = vim_strsave(callback);
12930 else
12931 /* pointer into the partial */
12932 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012933 timer->tr_partial = partial;
12934 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012935 }
12936}
12937
12938/*
12939 * "timer_stop(timer)" function
12940 */
12941 static void
12942f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12943{
12944 timer_T *timer;
12945
12946 if (argvars[0].v_type != VAR_NUMBER)
12947 {
12948 EMSG(_(e_number_exp));
12949 return;
12950 }
12951 timer = find_timer((int)get_tv_number(&argvars[0]));
12952 if (timer != NULL)
12953 stop_timer(timer);
12954}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012955
12956/*
12957 * "timer_stopall()" function
12958 */
12959 static void
12960f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12961{
12962 stop_all_timers();
12963}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012964#endif
12965
12966/*
12967 * "tolower(string)" function
12968 */
12969 static void
12970f_tolower(typval_T *argvars, typval_T *rettv)
12971{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012972 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010012973 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012974}
12975
12976/*
12977 * "toupper(string)" function
12978 */
12979 static void
12980f_toupper(typval_T *argvars, typval_T *rettv)
12981{
12982 rettv->v_type = VAR_STRING;
12983 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
12984}
12985
12986/*
12987 * "tr(string, fromstr, tostr)" function
12988 */
12989 static void
12990f_tr(typval_T *argvars, typval_T *rettv)
12991{
12992 char_u *in_str;
12993 char_u *fromstr;
12994 char_u *tostr;
12995 char_u *p;
12996#ifdef FEAT_MBYTE
12997 int inlen;
12998 int fromlen;
12999 int tolen;
13000 int idx;
13001 char_u *cpstr;
13002 int cplen;
13003 int first = TRUE;
13004#endif
13005 char_u buf[NUMBUFLEN];
13006 char_u buf2[NUMBUFLEN];
13007 garray_T ga;
13008
13009 in_str = get_tv_string(&argvars[0]);
13010 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13011 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13012
13013 /* Default return value: empty string. */
13014 rettv->v_type = VAR_STRING;
13015 rettv->vval.v_string = NULL;
13016 if (fromstr == NULL || tostr == NULL)
13017 return; /* type error; errmsg already given */
13018 ga_init2(&ga, (int)sizeof(char), 80);
13019
13020#ifdef FEAT_MBYTE
13021 if (!has_mbyte)
13022#endif
13023 /* not multi-byte: fromstr and tostr must be the same length */
13024 if (STRLEN(fromstr) != STRLEN(tostr))
13025 {
13026#ifdef FEAT_MBYTE
13027error:
13028#endif
13029 EMSG2(_(e_invarg2), fromstr);
13030 ga_clear(&ga);
13031 return;
13032 }
13033
13034 /* fromstr and tostr have to contain the same number of chars */
13035 while (*in_str != NUL)
13036 {
13037#ifdef FEAT_MBYTE
13038 if (has_mbyte)
13039 {
13040 inlen = (*mb_ptr2len)(in_str);
13041 cpstr = in_str;
13042 cplen = inlen;
13043 idx = 0;
13044 for (p = fromstr; *p != NUL; p += fromlen)
13045 {
13046 fromlen = (*mb_ptr2len)(p);
13047 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13048 {
13049 for (p = tostr; *p != NUL; p += tolen)
13050 {
13051 tolen = (*mb_ptr2len)(p);
13052 if (idx-- == 0)
13053 {
13054 cplen = tolen;
13055 cpstr = p;
13056 break;
13057 }
13058 }
13059 if (*p == NUL) /* tostr is shorter than fromstr */
13060 goto error;
13061 break;
13062 }
13063 ++idx;
13064 }
13065
13066 if (first && cpstr == in_str)
13067 {
13068 /* Check that fromstr and tostr have the same number of
13069 * (multi-byte) characters. Done only once when a character
13070 * of in_str doesn't appear in fromstr. */
13071 first = FALSE;
13072 for (p = tostr; *p != NUL; p += tolen)
13073 {
13074 tolen = (*mb_ptr2len)(p);
13075 --idx;
13076 }
13077 if (idx != 0)
13078 goto error;
13079 }
13080
13081 (void)ga_grow(&ga, cplen);
13082 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13083 ga.ga_len += cplen;
13084
13085 in_str += inlen;
13086 }
13087 else
13088#endif
13089 {
13090 /* When not using multi-byte chars we can do it faster. */
13091 p = vim_strchr(fromstr, *in_str);
13092 if (p != NULL)
13093 ga_append(&ga, tostr[p - fromstr]);
13094 else
13095 ga_append(&ga, *in_str);
13096 ++in_str;
13097 }
13098 }
13099
13100 /* add a terminating NUL */
13101 (void)ga_grow(&ga, 1);
13102 ga_append(&ga, NUL);
13103
13104 rettv->vval.v_string = ga.ga_data;
13105}
13106
13107#ifdef FEAT_FLOAT
13108/*
13109 * "trunc({float})" function
13110 */
13111 static void
13112f_trunc(typval_T *argvars, typval_T *rettv)
13113{
13114 float_T f = 0.0;
13115
13116 rettv->v_type = VAR_FLOAT;
13117 if (get_float_arg(argvars, &f) == OK)
13118 /* trunc() is not in C90, use floor() or ceil() instead. */
13119 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13120 else
13121 rettv->vval.v_float = 0.0;
13122}
13123#endif
13124
13125/*
13126 * "type(expr)" function
13127 */
13128 static void
13129f_type(typval_T *argvars, typval_T *rettv)
13130{
13131 int n = -1;
13132
13133 switch (argvars[0].v_type)
13134 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013135 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13136 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013137 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013138 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13139 case VAR_LIST: n = VAR_TYPE_LIST; break;
13140 case VAR_DICT: n = VAR_TYPE_DICT; break;
13141 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013142 case VAR_SPECIAL:
13143 if (argvars[0].vval.v_number == VVAL_FALSE
13144 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013145 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013146 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013147 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013148 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013149 case VAR_JOB: n = VAR_TYPE_JOB; break;
13150 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013151 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013152 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013153 n = -1;
13154 break;
13155 }
13156 rettv->vval.v_number = n;
13157}
13158
13159/*
13160 * "undofile(name)" function
13161 */
13162 static void
13163f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13164{
13165 rettv->v_type = VAR_STRING;
13166#ifdef FEAT_PERSISTENT_UNDO
13167 {
13168 char_u *fname = get_tv_string(&argvars[0]);
13169
13170 if (*fname == NUL)
13171 {
13172 /* If there is no file name there will be no undo file. */
13173 rettv->vval.v_string = NULL;
13174 }
13175 else
13176 {
13177 char_u *ffname = FullName_save(fname, FALSE);
13178
13179 if (ffname != NULL)
13180 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13181 vim_free(ffname);
13182 }
13183 }
13184#else
13185 rettv->vval.v_string = NULL;
13186#endif
13187}
13188
13189/*
13190 * "undotree()" function
13191 */
13192 static void
13193f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13194{
13195 if (rettv_dict_alloc(rettv) == OK)
13196 {
13197 dict_T *dict = rettv->vval.v_dict;
13198 list_T *list;
13199
13200 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13201 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13202 dict_add_nr_str(dict, "save_last",
13203 (long)curbuf->b_u_save_nr_last, NULL);
13204 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13205 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13206 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13207
13208 list = list_alloc();
13209 if (list != NULL)
13210 {
13211 u_eval_tree(curbuf->b_u_oldhead, list);
13212 dict_add_list(dict, "entries", list);
13213 }
13214 }
13215}
13216
13217/*
13218 * "values(dict)" function
13219 */
13220 static void
13221f_values(typval_T *argvars, typval_T *rettv)
13222{
13223 dict_list(argvars, rettv, 1);
13224}
13225
13226/*
13227 * "virtcol(string)" function
13228 */
13229 static void
13230f_virtcol(typval_T *argvars, typval_T *rettv)
13231{
13232 colnr_T vcol = 0;
13233 pos_T *fp;
13234 int fnum = curbuf->b_fnum;
13235
13236 fp = var2fpos(&argvars[0], FALSE, &fnum);
13237 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13238 && fnum == curbuf->b_fnum)
13239 {
13240 getvvcol(curwin, fp, NULL, NULL, &vcol);
13241 ++vcol;
13242 }
13243
13244 rettv->vval.v_number = vcol;
13245}
13246
13247/*
13248 * "visualmode()" function
13249 */
13250 static void
13251f_visualmode(typval_T *argvars, typval_T *rettv)
13252{
13253 char_u str[2];
13254
13255 rettv->v_type = VAR_STRING;
13256 str[0] = curbuf->b_visual_mode_eval;
13257 str[1] = NUL;
13258 rettv->vval.v_string = vim_strsave(str);
13259
13260 /* A non-zero number or non-empty string argument: reset mode. */
13261 if (non_zero_arg(&argvars[0]))
13262 curbuf->b_visual_mode_eval = NUL;
13263}
13264
13265/*
13266 * "wildmenumode()" function
13267 */
13268 static void
13269f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13270{
13271#ifdef FEAT_WILDMENU
13272 if (wild_menu_showing)
13273 rettv->vval.v_number = 1;
13274#endif
13275}
13276
13277/*
13278 * "winbufnr(nr)" function
13279 */
13280 static void
13281f_winbufnr(typval_T *argvars, typval_T *rettv)
13282{
13283 win_T *wp;
13284
13285 wp = find_win_by_nr(&argvars[0], NULL);
13286 if (wp == NULL)
13287 rettv->vval.v_number = -1;
13288 else
13289 rettv->vval.v_number = wp->w_buffer->b_fnum;
13290}
13291
13292/*
13293 * "wincol()" function
13294 */
13295 static void
13296f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13297{
13298 validate_cursor();
13299 rettv->vval.v_number = curwin->w_wcol + 1;
13300}
13301
13302/*
13303 * "winheight(nr)" function
13304 */
13305 static void
13306f_winheight(typval_T *argvars, typval_T *rettv)
13307{
13308 win_T *wp;
13309
13310 wp = find_win_by_nr(&argvars[0], NULL);
13311 if (wp == NULL)
13312 rettv->vval.v_number = -1;
13313 else
13314 rettv->vval.v_number = wp->w_height;
13315}
13316
13317/*
13318 * "winline()" function
13319 */
13320 static void
13321f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13322{
13323 validate_cursor();
13324 rettv->vval.v_number = curwin->w_wrow + 1;
13325}
13326
13327/*
13328 * "winnr()" function
13329 */
13330 static void
13331f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13332{
13333 int nr = 1;
13334
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013335 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013336 rettv->vval.v_number = nr;
13337}
13338
13339/*
13340 * "winrestcmd()" function
13341 */
13342 static void
13343f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13344{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013345 win_T *wp;
13346 int winnr = 1;
13347 garray_T ga;
13348 char_u buf[50];
13349
13350 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013351 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013352 {
13353 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13354 ga_concat(&ga, buf);
13355 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13356 ga_concat(&ga, buf);
13357 ++winnr;
13358 }
13359 ga_append(&ga, NUL);
13360
13361 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013362 rettv->v_type = VAR_STRING;
13363}
13364
13365/*
13366 * "winrestview()" function
13367 */
13368 static void
13369f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13370{
13371 dict_T *dict;
13372
13373 if (argvars[0].v_type != VAR_DICT
13374 || (dict = argvars[0].vval.v_dict) == NULL)
13375 EMSG(_(e_invarg));
13376 else
13377 {
13378 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13379 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13380 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13381 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13382#ifdef FEAT_VIRTUALEDIT
13383 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13384 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13385#endif
13386 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13387 {
13388 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13389 curwin->w_set_curswant = FALSE;
13390 }
13391
13392 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13393 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13394#ifdef FEAT_DIFF
13395 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13396 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13397#endif
13398 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13399 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13400 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13401 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13402
13403 check_cursor();
13404 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013405 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013406 changed_window_setting();
13407
13408 if (curwin->w_topline <= 0)
13409 curwin->w_topline = 1;
13410 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13411 curwin->w_topline = curbuf->b_ml.ml_line_count;
13412#ifdef FEAT_DIFF
13413 check_topfill(curwin, TRUE);
13414#endif
13415 }
13416}
13417
13418/*
13419 * "winsaveview()" function
13420 */
13421 static void
13422f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13423{
13424 dict_T *dict;
13425
13426 if (rettv_dict_alloc(rettv) == FAIL)
13427 return;
13428 dict = rettv->vval.v_dict;
13429
13430 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13431 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13432#ifdef FEAT_VIRTUALEDIT
13433 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13434#endif
13435 update_curswant();
13436 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13437
13438 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13439#ifdef FEAT_DIFF
13440 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13441#endif
13442 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13443 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13444}
13445
13446/*
13447 * "winwidth(nr)" function
13448 */
13449 static void
13450f_winwidth(typval_T *argvars, typval_T *rettv)
13451{
13452 win_T *wp;
13453
13454 wp = find_win_by_nr(&argvars[0], NULL);
13455 if (wp == NULL)
13456 rettv->vval.v_number = -1;
13457 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013458 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013459}
13460
13461/*
13462 * "wordcount()" function
13463 */
13464 static void
13465f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13466{
13467 if (rettv_dict_alloc(rettv) == FAIL)
13468 return;
13469 cursor_pos_info(rettv->vval.v_dict);
13470}
13471
13472/*
13473 * "writefile()" function
13474 */
13475 static void
13476f_writefile(typval_T *argvars, typval_T *rettv)
13477{
13478 int binary = FALSE;
13479 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013480#ifdef HAVE_FSYNC
13481 int do_fsync = p_fs;
13482#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013483 char_u *fname;
13484 FILE *fd;
13485 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013486 listitem_T *li;
13487 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013488
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013489 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013490 if (check_restricted() || check_secure())
13491 return;
13492
13493 if (argvars[0].v_type != VAR_LIST)
13494 {
13495 EMSG2(_(e_listarg), "writefile()");
13496 return;
13497 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013498 list = argvars[0].vval.v_list;
13499 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013500 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013501 for (li = list->lv_first; li != NULL; li = li->li_next)
13502 if (get_tv_string_chk(&li->li_tv) == NULL)
13503 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013504
13505 if (argvars[2].v_type != VAR_UNKNOWN)
13506 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013507 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13508
13509 if (arg2 == NULL)
13510 return;
13511 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013512 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013513 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013514 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013515#ifdef HAVE_FSYNC
13516 if (vim_strchr(arg2, 's') != NULL)
13517 do_fsync = TRUE;
13518 else if (vim_strchr(arg2, 'S') != NULL)
13519 do_fsync = FALSE;
13520#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013521 }
13522
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013523 fname = get_tv_string_chk(&argvars[1]);
13524 if (fname == NULL)
13525 return;
13526
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013527 /* Always open the file in binary mode, library functions have a mind of
13528 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013529 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13530 append ? APPENDBIN : WRITEBIN)) == NULL)
13531 {
13532 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13533 ret = -1;
13534 }
13535 else
13536 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013537 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013538 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013539#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013540 else if (do_fsync)
13541 /* Ignore the error, the user wouldn't know what to do about it.
13542 * May happen for a device. */
13543 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013544#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013545 fclose(fd);
13546 }
13547
13548 rettv->vval.v_number = ret;
13549}
13550
13551/*
13552 * "xor(expr, expr)" function
13553 */
13554 static void
13555f_xor(typval_T *argvars, typval_T *rettv)
13556{
13557 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13558 ^ get_tv_number_chk(&argvars[1], NULL);
13559}
13560
13561
13562#endif /* FEAT_EVAL */