blob: daef9a8c9737efc1013b4e432df04c88c11f10ce [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);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100168static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200169static void f_getchar(typval_T *argvars, typval_T *rettv);
170static void f_getcharmod(typval_T *argvars, typval_T *rettv);
171static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
172static void f_getcmdline(typval_T *argvars, typval_T *rettv);
173#if defined(FEAT_CMDL_COMPL)
174static void f_getcompletion(typval_T *argvars, typval_T *rettv);
175#endif
176static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
177static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
178static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
179static void f_getcwd(typval_T *argvars, typval_T *rettv);
180static void f_getfontname(typval_T *argvars, typval_T *rettv);
181static void f_getfperm(typval_T *argvars, typval_T *rettv);
182static void f_getfsize(typval_T *argvars, typval_T *rettv);
183static void f_getftime(typval_T *argvars, typval_T *rettv);
184static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100185static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200186static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200187static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200188static void f_getmatches(typval_T *argvars, typval_T *rettv);
189static void f_getpid(typval_T *argvars, typval_T *rettv);
190static void f_getcurpos(typval_T *argvars, typval_T *rettv);
191static void f_getpos(typval_T *argvars, typval_T *rettv);
192static void f_getqflist(typval_T *argvars, typval_T *rettv);
193static void f_getreg(typval_T *argvars, typval_T *rettv);
194static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200195static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200196static void f_gettabvar(typval_T *argvars, typval_T *rettv);
197static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200198static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200199static void f_getwinposx(typval_T *argvars, typval_T *rettv);
200static void f_getwinposy(typval_T *argvars, typval_T *rettv);
201static void f_getwinvar(typval_T *argvars, typval_T *rettv);
202static void f_glob(typval_T *argvars, typval_T *rettv);
203static void f_globpath(typval_T *argvars, typval_T *rettv);
204static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
205static void f_has(typval_T *argvars, typval_T *rettv);
206static void f_has_key(typval_T *argvars, typval_T *rettv);
207static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
208static void f_hasmapto(typval_T *argvars, typval_T *rettv);
209static void f_histadd(typval_T *argvars, typval_T *rettv);
210static void f_histdel(typval_T *argvars, typval_T *rettv);
211static void f_histget(typval_T *argvars, typval_T *rettv);
212static void f_histnr(typval_T *argvars, typval_T *rettv);
213static void f_hlID(typval_T *argvars, typval_T *rettv);
214static void f_hlexists(typval_T *argvars, typval_T *rettv);
215static void f_hostname(typval_T *argvars, typval_T *rettv);
216static void f_iconv(typval_T *argvars, typval_T *rettv);
217static void f_indent(typval_T *argvars, typval_T *rettv);
218static void f_index(typval_T *argvars, typval_T *rettv);
219static void f_input(typval_T *argvars, typval_T *rettv);
220static void f_inputdialog(typval_T *argvars, typval_T *rettv);
221static void f_inputlist(typval_T *argvars, typval_T *rettv);
222static void f_inputrestore(typval_T *argvars, typval_T *rettv);
223static void f_inputsave(typval_T *argvars, typval_T *rettv);
224static void f_inputsecret(typval_T *argvars, typval_T *rettv);
225static void f_insert(typval_T *argvars, typval_T *rettv);
226static void f_invert(typval_T *argvars, typval_T *rettv);
227static void f_isdirectory(typval_T *argvars, typval_T *rettv);
228static void f_islocked(typval_T *argvars, typval_T *rettv);
229#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
230static void f_isnan(typval_T *argvars, typval_T *rettv);
231#endif
232static void f_items(typval_T *argvars, typval_T *rettv);
233#ifdef FEAT_JOB_CHANNEL
234static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
235static void f_job_info(typval_T *argvars, typval_T *rettv);
236static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
237static void f_job_start(typval_T *argvars, typval_T *rettv);
238static void f_job_stop(typval_T *argvars, typval_T *rettv);
239static void f_job_status(typval_T *argvars, typval_T *rettv);
240#endif
241static void f_join(typval_T *argvars, typval_T *rettv);
242static void f_js_decode(typval_T *argvars, typval_T *rettv);
243static void f_js_encode(typval_T *argvars, typval_T *rettv);
244static void f_json_decode(typval_T *argvars, typval_T *rettv);
245static void f_json_encode(typval_T *argvars, typval_T *rettv);
246static void f_keys(typval_T *argvars, typval_T *rettv);
247static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
248static void f_len(typval_T *argvars, typval_T *rettv);
249static void f_libcall(typval_T *argvars, typval_T *rettv);
250static void f_libcallnr(typval_T *argvars, typval_T *rettv);
251static void f_line(typval_T *argvars, typval_T *rettv);
252static void f_line2byte(typval_T *argvars, typval_T *rettv);
253static void f_lispindent(typval_T *argvars, typval_T *rettv);
254static void f_localtime(typval_T *argvars, typval_T *rettv);
255#ifdef FEAT_FLOAT
256static void f_log(typval_T *argvars, typval_T *rettv);
257static void f_log10(typval_T *argvars, typval_T *rettv);
258#endif
259#ifdef FEAT_LUA
260static void f_luaeval(typval_T *argvars, typval_T *rettv);
261#endif
262static void f_map(typval_T *argvars, typval_T *rettv);
263static void f_maparg(typval_T *argvars, typval_T *rettv);
264static void f_mapcheck(typval_T *argvars, typval_T *rettv);
265static void f_match(typval_T *argvars, typval_T *rettv);
266static void f_matchadd(typval_T *argvars, typval_T *rettv);
267static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
268static void f_matcharg(typval_T *argvars, typval_T *rettv);
269static void f_matchdelete(typval_T *argvars, typval_T *rettv);
270static void f_matchend(typval_T *argvars, typval_T *rettv);
271static void f_matchlist(typval_T *argvars, typval_T *rettv);
272static void f_matchstr(typval_T *argvars, typval_T *rettv);
273static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
274static void f_max(typval_T *argvars, typval_T *rettv);
275static void f_min(typval_T *argvars, typval_T *rettv);
276#ifdef vim_mkdir
277static void f_mkdir(typval_T *argvars, typval_T *rettv);
278#endif
279static void f_mode(typval_T *argvars, typval_T *rettv);
280#ifdef FEAT_MZSCHEME
281static void f_mzeval(typval_T *argvars, typval_T *rettv);
282#endif
283static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
284static void f_nr2char(typval_T *argvars, typval_T *rettv);
285static void f_or(typval_T *argvars, typval_T *rettv);
286static void f_pathshorten(typval_T *argvars, typval_T *rettv);
287#ifdef FEAT_PERL
288static void f_perleval(typval_T *argvars, typval_T *rettv);
289#endif
290#ifdef FEAT_FLOAT
291static void f_pow(typval_T *argvars, typval_T *rettv);
292#endif
293static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
294static void f_printf(typval_T *argvars, typval_T *rettv);
295static void f_pumvisible(typval_T *argvars, typval_T *rettv);
296#ifdef FEAT_PYTHON3
297static void f_py3eval(typval_T *argvars, typval_T *rettv);
298#endif
299#ifdef FEAT_PYTHON
300static void f_pyeval(typval_T *argvars, typval_T *rettv);
301#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100302#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
303static void f_pyxeval(typval_T *argvars, typval_T *rettv);
304#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200305static void f_range(typval_T *argvars, typval_T *rettv);
306static void f_readfile(typval_T *argvars, typval_T *rettv);
307static void f_reltime(typval_T *argvars, typval_T *rettv);
308#ifdef FEAT_FLOAT
309static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
310#endif
311static void f_reltimestr(typval_T *argvars, typval_T *rettv);
312static void f_remote_expr(typval_T *argvars, typval_T *rettv);
313static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
314static void f_remote_peek(typval_T *argvars, typval_T *rettv);
315static void f_remote_read(typval_T *argvars, typval_T *rettv);
316static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100317static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200318static void f_remove(typval_T *argvars, typval_T *rettv);
319static void f_rename(typval_T *argvars, typval_T *rettv);
320static void f_repeat(typval_T *argvars, typval_T *rettv);
321static void f_resolve(typval_T *argvars, typval_T *rettv);
322static void f_reverse(typval_T *argvars, typval_T *rettv);
323#ifdef FEAT_FLOAT
324static void f_round(typval_T *argvars, typval_T *rettv);
325#endif
326static void f_screenattr(typval_T *argvars, typval_T *rettv);
327static void f_screenchar(typval_T *argvars, typval_T *rettv);
328static void f_screencol(typval_T *argvars, typval_T *rettv);
329static void f_screenrow(typval_T *argvars, typval_T *rettv);
330static void f_search(typval_T *argvars, typval_T *rettv);
331static void f_searchdecl(typval_T *argvars, typval_T *rettv);
332static void f_searchpair(typval_T *argvars, typval_T *rettv);
333static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
334static void f_searchpos(typval_T *argvars, typval_T *rettv);
335static void f_server2client(typval_T *argvars, typval_T *rettv);
336static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200337static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200338static void f_setbufvar(typval_T *argvars, typval_T *rettv);
339static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
340static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
341static void f_setfperm(typval_T *argvars, typval_T *rettv);
342static void f_setline(typval_T *argvars, typval_T *rettv);
343static void f_setloclist(typval_T *argvars, typval_T *rettv);
344static void f_setmatches(typval_T *argvars, typval_T *rettv);
345static void f_setpos(typval_T *argvars, typval_T *rettv);
346static void f_setqflist(typval_T *argvars, typval_T *rettv);
347static void f_setreg(typval_T *argvars, typval_T *rettv);
348static void f_settabvar(typval_T *argvars, typval_T *rettv);
349static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
350static void f_setwinvar(typval_T *argvars, typval_T *rettv);
351#ifdef FEAT_CRYPT
352static void f_sha256(typval_T *argvars, typval_T *rettv);
353#endif /* FEAT_CRYPT */
354static void f_shellescape(typval_T *argvars, typval_T *rettv);
355static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
356static void f_simplify(typval_T *argvars, typval_T *rettv);
357#ifdef FEAT_FLOAT
358static void f_sin(typval_T *argvars, typval_T *rettv);
359static void f_sinh(typval_T *argvars, typval_T *rettv);
360#endif
361static void f_sort(typval_T *argvars, typval_T *rettv);
362static void f_soundfold(typval_T *argvars, typval_T *rettv);
363static void f_spellbadword(typval_T *argvars, typval_T *rettv);
364static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
365static void f_split(typval_T *argvars, typval_T *rettv);
366#ifdef FEAT_FLOAT
367static void f_sqrt(typval_T *argvars, typval_T *rettv);
368static void f_str2float(typval_T *argvars, typval_T *rettv);
369#endif
370static void f_str2nr(typval_T *argvars, typval_T *rettv);
371static void f_strchars(typval_T *argvars, typval_T *rettv);
372#ifdef HAVE_STRFTIME
373static void f_strftime(typval_T *argvars, typval_T *rettv);
374#endif
375static void f_strgetchar(typval_T *argvars, typval_T *rettv);
376static void f_stridx(typval_T *argvars, typval_T *rettv);
377static void f_string(typval_T *argvars, typval_T *rettv);
378static void f_strlen(typval_T *argvars, typval_T *rettv);
379static void f_strcharpart(typval_T *argvars, typval_T *rettv);
380static void f_strpart(typval_T *argvars, typval_T *rettv);
381static void f_strridx(typval_T *argvars, typval_T *rettv);
382static void f_strtrans(typval_T *argvars, typval_T *rettv);
383static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
384static void f_strwidth(typval_T *argvars, typval_T *rettv);
385static void f_submatch(typval_T *argvars, typval_T *rettv);
386static void f_substitute(typval_T *argvars, typval_T *rettv);
387static void f_synID(typval_T *argvars, typval_T *rettv);
388static void f_synIDattr(typval_T *argvars, typval_T *rettv);
389static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
390static void f_synstack(typval_T *argvars, typval_T *rettv);
391static void f_synconcealed(typval_T *argvars, typval_T *rettv);
392static void f_system(typval_T *argvars, typval_T *rettv);
393static void f_systemlist(typval_T *argvars, typval_T *rettv);
394static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
395static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
396static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
397static void f_taglist(typval_T *argvars, typval_T *rettv);
398static void f_tagfiles(typval_T *argvars, typval_T *rettv);
399static void f_tempname(typval_T *argvars, typval_T *rettv);
400static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
401static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200402static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100403static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200404static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100405static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200406#ifdef FEAT_JOB_CHANNEL
407static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
408#endif
409static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
410#ifdef FEAT_JOB_CHANNEL
411static void f_test_null_job(typval_T *argvars, typval_T *rettv);
412#endif
413static void f_test_null_list(typval_T *argvars, typval_T *rettv);
414static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
415static void f_test_null_string(typval_T *argvars, typval_T *rettv);
416static void f_test_settime(typval_T *argvars, typval_T *rettv);
417#ifdef FEAT_FLOAT
418static void f_tan(typval_T *argvars, typval_T *rettv);
419static void f_tanh(typval_T *argvars, typval_T *rettv);
420#endif
421#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200422static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200423static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200424static void f_timer_start(typval_T *argvars, typval_T *rettv);
425static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200426static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200427#endif
428static void f_tolower(typval_T *argvars, typval_T *rettv);
429static void f_toupper(typval_T *argvars, typval_T *rettv);
430static void f_tr(typval_T *argvars, typval_T *rettv);
431#ifdef FEAT_FLOAT
432static void f_trunc(typval_T *argvars, typval_T *rettv);
433#endif
434static void f_type(typval_T *argvars, typval_T *rettv);
435static void f_undofile(typval_T *argvars, typval_T *rettv);
436static void f_undotree(typval_T *argvars, typval_T *rettv);
437static void f_uniq(typval_T *argvars, typval_T *rettv);
438static void f_values(typval_T *argvars, typval_T *rettv);
439static void f_virtcol(typval_T *argvars, typval_T *rettv);
440static void f_visualmode(typval_T *argvars, typval_T *rettv);
441static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
442static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
443static void f_win_getid(typval_T *argvars, typval_T *rettv);
444static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
445static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
446static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100447static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200448static void f_winbufnr(typval_T *argvars, typval_T *rettv);
449static void f_wincol(typval_T *argvars, typval_T *rettv);
450static void f_winheight(typval_T *argvars, typval_T *rettv);
451static void f_winline(typval_T *argvars, typval_T *rettv);
452static void f_winnr(typval_T *argvars, typval_T *rettv);
453static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
454static void f_winrestview(typval_T *argvars, typval_T *rettv);
455static void f_winsaveview(typval_T *argvars, typval_T *rettv);
456static void f_winwidth(typval_T *argvars, typval_T *rettv);
457static void f_writefile(typval_T *argvars, typval_T *rettv);
458static void f_wordcount(typval_T *argvars, typval_T *rettv);
459static void f_xor(typval_T *argvars, typval_T *rettv);
460
461/*
462 * Array with names and number of arguments of all internal functions
463 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
464 */
465static struct fst
466{
467 char *f_name; /* function name */
468 char f_min_argc; /* minimal number of arguments */
469 char f_max_argc; /* maximal number of arguments */
470 void (*f_func)(typval_T *args, typval_T *rvar);
471 /* implementation of function */
472} functions[] =
473{
474#ifdef FEAT_FLOAT
475 {"abs", 1, 1, f_abs},
476 {"acos", 1, 1, f_acos}, /* WJMc */
477#endif
478 {"add", 2, 2, f_add},
479 {"and", 2, 2, f_and},
480 {"append", 2, 2, f_append},
481 {"argc", 0, 0, f_argc},
482 {"argidx", 0, 0, f_argidx},
483 {"arglistid", 0, 2, f_arglistid},
484 {"argv", 0, 1, f_argv},
485#ifdef FEAT_FLOAT
486 {"asin", 1, 1, f_asin}, /* WJMc */
487#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100488 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200489 {"assert_equal", 2, 3, f_assert_equal},
490 {"assert_exception", 1, 2, f_assert_exception},
491 {"assert_fails", 1, 2, f_assert_fails},
492 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100493 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200494 {"assert_match", 2, 3, f_assert_match},
495 {"assert_notequal", 2, 3, f_assert_notequal},
496 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100497 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200498 {"assert_true", 1, 2, f_assert_true},
499#ifdef FEAT_FLOAT
500 {"atan", 1, 1, f_atan},
501 {"atan2", 2, 2, f_atan2},
502#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100503#ifdef FEAT_BEVAL
504 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100505# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100506 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100507# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100508#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200509 {"browse", 4, 4, f_browse},
510 {"browsedir", 2, 2, f_browsedir},
511 {"bufexists", 1, 1, f_bufexists},
512 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
513 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
514 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
515 {"buflisted", 1, 1, f_buflisted},
516 {"bufloaded", 1, 1, f_bufloaded},
517 {"bufname", 1, 1, f_bufname},
518 {"bufnr", 1, 2, f_bufnr},
519 {"bufwinid", 1, 1, f_bufwinid},
520 {"bufwinnr", 1, 1, f_bufwinnr},
521 {"byte2line", 1, 1, f_byte2line},
522 {"byteidx", 2, 2, f_byteidx},
523 {"byteidxcomp", 2, 2, f_byteidxcomp},
524 {"call", 2, 3, f_call},
525#ifdef FEAT_FLOAT
526 {"ceil", 1, 1, f_ceil},
527#endif
528#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100529 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200530 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200531 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200532 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
533 {"ch_evalraw", 2, 3, f_ch_evalraw},
534 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
535 {"ch_getjob", 1, 1, f_ch_getjob},
536 {"ch_info", 1, 1, f_ch_info},
537 {"ch_log", 1, 2, f_ch_log},
538 {"ch_logfile", 1, 2, f_ch_logfile},
539 {"ch_open", 1, 2, f_ch_open},
540 {"ch_read", 1, 2, f_ch_read},
541 {"ch_readraw", 1, 2, f_ch_readraw},
542 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
543 {"ch_sendraw", 2, 3, f_ch_sendraw},
544 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200545 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200546#endif
547 {"changenr", 0, 0, f_changenr},
548 {"char2nr", 1, 2, f_char2nr},
549 {"cindent", 1, 1, f_cindent},
550 {"clearmatches", 0, 0, f_clearmatches},
551 {"col", 1, 1, f_col},
552#if defined(FEAT_INS_EXPAND)
553 {"complete", 2, 2, f_complete},
554 {"complete_add", 1, 1, f_complete_add},
555 {"complete_check", 0, 0, f_complete_check},
556#endif
557 {"confirm", 1, 4, f_confirm},
558 {"copy", 1, 1, f_copy},
559#ifdef FEAT_FLOAT
560 {"cos", 1, 1, f_cos},
561 {"cosh", 1, 1, f_cosh},
562#endif
563 {"count", 2, 4, f_count},
564 {"cscope_connection",0,3, f_cscope_connection},
565 {"cursor", 1, 3, f_cursor},
566 {"deepcopy", 1, 2, f_deepcopy},
567 {"delete", 1, 2, f_delete},
568 {"did_filetype", 0, 0, f_did_filetype},
569 {"diff_filler", 1, 1, f_diff_filler},
570 {"diff_hlID", 2, 2, f_diff_hlID},
571 {"empty", 1, 1, f_empty},
572 {"escape", 2, 2, f_escape},
573 {"eval", 1, 1, f_eval},
574 {"eventhandler", 0, 0, f_eventhandler},
575 {"executable", 1, 1, f_executable},
576 {"execute", 1, 2, f_execute},
577 {"exepath", 1, 1, f_exepath},
578 {"exists", 1, 1, f_exists},
579#ifdef FEAT_FLOAT
580 {"exp", 1, 1, f_exp},
581#endif
582 {"expand", 1, 3, f_expand},
583 {"extend", 2, 3, f_extend},
584 {"feedkeys", 1, 2, f_feedkeys},
585 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
586 {"filereadable", 1, 1, f_filereadable},
587 {"filewritable", 1, 1, f_filewritable},
588 {"filter", 2, 2, f_filter},
589 {"finddir", 1, 3, f_finddir},
590 {"findfile", 1, 3, f_findfile},
591#ifdef FEAT_FLOAT
592 {"float2nr", 1, 1, f_float2nr},
593 {"floor", 1, 1, f_floor},
594 {"fmod", 2, 2, f_fmod},
595#endif
596 {"fnameescape", 1, 1, f_fnameescape},
597 {"fnamemodify", 2, 2, f_fnamemodify},
598 {"foldclosed", 1, 1, f_foldclosed},
599 {"foldclosedend", 1, 1, f_foldclosedend},
600 {"foldlevel", 1, 1, f_foldlevel},
601 {"foldtext", 0, 0, f_foldtext},
602 {"foldtextresult", 1, 1, f_foldtextresult},
603 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200604 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200605 {"function", 1, 3, f_function},
606 {"garbagecollect", 0, 1, f_garbagecollect},
607 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200608 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200609 {"getbufline", 2, 3, f_getbufline},
610 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100611 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200612 {"getchar", 0, 1, f_getchar},
613 {"getcharmod", 0, 0, f_getcharmod},
614 {"getcharsearch", 0, 0, f_getcharsearch},
615 {"getcmdline", 0, 0, f_getcmdline},
616 {"getcmdpos", 0, 0, f_getcmdpos},
617 {"getcmdtype", 0, 0, f_getcmdtype},
618 {"getcmdwintype", 0, 0, f_getcmdwintype},
619#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200620 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200621#endif
622 {"getcurpos", 0, 0, f_getcurpos},
623 {"getcwd", 0, 2, f_getcwd},
624 {"getfontname", 0, 1, f_getfontname},
625 {"getfperm", 1, 1, f_getfperm},
626 {"getfsize", 1, 1, f_getfsize},
627 {"getftime", 1, 1, f_getftime},
628 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100629 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200630 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200631 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200632 {"getmatches", 0, 0, f_getmatches},
633 {"getpid", 0, 0, f_getpid},
634 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200635 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636 {"getreg", 0, 3, f_getreg},
637 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200638 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200639 {"gettabvar", 2, 3, f_gettabvar},
640 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200641 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200642 {"getwinposx", 0, 0, f_getwinposx},
643 {"getwinposy", 0, 0, f_getwinposy},
644 {"getwinvar", 2, 3, f_getwinvar},
645 {"glob", 1, 4, f_glob},
646 {"glob2regpat", 1, 1, f_glob2regpat},
647 {"globpath", 2, 5, f_globpath},
648 {"has", 1, 1, f_has},
649 {"has_key", 2, 2, f_has_key},
650 {"haslocaldir", 0, 2, f_haslocaldir},
651 {"hasmapto", 1, 3, f_hasmapto},
652 {"highlightID", 1, 1, f_hlID}, /* obsolete */
653 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
654 {"histadd", 2, 2, f_histadd},
655 {"histdel", 1, 2, f_histdel},
656 {"histget", 1, 2, f_histget},
657 {"histnr", 1, 1, f_histnr},
658 {"hlID", 1, 1, f_hlID},
659 {"hlexists", 1, 1, f_hlexists},
660 {"hostname", 0, 0, f_hostname},
661 {"iconv", 3, 3, f_iconv},
662 {"indent", 1, 1, f_indent},
663 {"index", 2, 4, f_index},
664 {"input", 1, 3, f_input},
665 {"inputdialog", 1, 3, f_inputdialog},
666 {"inputlist", 1, 1, f_inputlist},
667 {"inputrestore", 0, 0, f_inputrestore},
668 {"inputsave", 0, 0, f_inputsave},
669 {"inputsecret", 1, 2, f_inputsecret},
670 {"insert", 2, 3, f_insert},
671 {"invert", 1, 1, f_invert},
672 {"isdirectory", 1, 1, f_isdirectory},
673 {"islocked", 1, 1, f_islocked},
674#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
675 {"isnan", 1, 1, f_isnan},
676#endif
677 {"items", 1, 1, f_items},
678#ifdef FEAT_JOB_CHANNEL
679 {"job_getchannel", 1, 1, f_job_getchannel},
680 {"job_info", 1, 1, f_job_info},
681 {"job_setoptions", 2, 2, f_job_setoptions},
682 {"job_start", 1, 2, f_job_start},
683 {"job_status", 1, 1, f_job_status},
684 {"job_stop", 1, 2, f_job_stop},
685#endif
686 {"join", 1, 2, f_join},
687 {"js_decode", 1, 1, f_js_decode},
688 {"js_encode", 1, 1, f_js_encode},
689 {"json_decode", 1, 1, f_json_decode},
690 {"json_encode", 1, 1, f_json_encode},
691 {"keys", 1, 1, f_keys},
692 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
693 {"len", 1, 1, f_len},
694 {"libcall", 3, 3, f_libcall},
695 {"libcallnr", 3, 3, f_libcallnr},
696 {"line", 1, 1, f_line},
697 {"line2byte", 1, 1, f_line2byte},
698 {"lispindent", 1, 1, f_lispindent},
699 {"localtime", 0, 0, f_localtime},
700#ifdef FEAT_FLOAT
701 {"log", 1, 1, f_log},
702 {"log10", 1, 1, f_log10},
703#endif
704#ifdef FEAT_LUA
705 {"luaeval", 1, 2, f_luaeval},
706#endif
707 {"map", 2, 2, f_map},
708 {"maparg", 1, 4, f_maparg},
709 {"mapcheck", 1, 3, f_mapcheck},
710 {"match", 2, 4, f_match},
711 {"matchadd", 2, 5, f_matchadd},
712 {"matchaddpos", 2, 5, f_matchaddpos},
713 {"matcharg", 1, 1, f_matcharg},
714 {"matchdelete", 1, 1, f_matchdelete},
715 {"matchend", 2, 4, f_matchend},
716 {"matchlist", 2, 4, f_matchlist},
717 {"matchstr", 2, 4, f_matchstr},
718 {"matchstrpos", 2, 4, f_matchstrpos},
719 {"max", 1, 1, f_max},
720 {"min", 1, 1, f_min},
721#ifdef vim_mkdir
722 {"mkdir", 1, 3, f_mkdir},
723#endif
724 {"mode", 0, 1, f_mode},
725#ifdef FEAT_MZSCHEME
726 {"mzeval", 1, 1, f_mzeval},
727#endif
728 {"nextnonblank", 1, 1, f_nextnonblank},
729 {"nr2char", 1, 2, f_nr2char},
730 {"or", 2, 2, f_or},
731 {"pathshorten", 1, 1, f_pathshorten},
732#ifdef FEAT_PERL
733 {"perleval", 1, 1, f_perleval},
734#endif
735#ifdef FEAT_FLOAT
736 {"pow", 2, 2, f_pow},
737#endif
738 {"prevnonblank", 1, 1, f_prevnonblank},
739 {"printf", 2, 19, f_printf},
740 {"pumvisible", 0, 0, f_pumvisible},
741#ifdef FEAT_PYTHON3
742 {"py3eval", 1, 1, f_py3eval},
743#endif
744#ifdef FEAT_PYTHON
745 {"pyeval", 1, 1, f_pyeval},
746#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100747#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
748 {"pyxeval", 1, 1, f_pyxeval},
749#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200750 {"range", 1, 3, f_range},
751 {"readfile", 1, 3, f_readfile},
752 {"reltime", 0, 2, f_reltime},
753#ifdef FEAT_FLOAT
754 {"reltimefloat", 1, 1, f_reltimefloat},
755#endif
756 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100757 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200758 {"remote_foreground", 1, 1, f_remote_foreground},
759 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100760 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200761 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100762 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200763 {"remove", 2, 3, f_remove},
764 {"rename", 2, 2, f_rename},
765 {"repeat", 2, 2, f_repeat},
766 {"resolve", 1, 1, f_resolve},
767 {"reverse", 1, 1, f_reverse},
768#ifdef FEAT_FLOAT
769 {"round", 1, 1, f_round},
770#endif
771 {"screenattr", 2, 2, f_screenattr},
772 {"screenchar", 2, 2, f_screenchar},
773 {"screencol", 0, 0, f_screencol},
774 {"screenrow", 0, 0, f_screenrow},
775 {"search", 1, 4, f_search},
776 {"searchdecl", 1, 3, f_searchdecl},
777 {"searchpair", 3, 7, f_searchpair},
778 {"searchpairpos", 3, 7, f_searchpairpos},
779 {"searchpos", 1, 4, f_searchpos},
780 {"server2client", 2, 2, f_server2client},
781 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200782 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200783 {"setbufvar", 3, 3, f_setbufvar},
784 {"setcharsearch", 1, 1, f_setcharsearch},
785 {"setcmdpos", 1, 1, f_setcmdpos},
786 {"setfperm", 2, 2, f_setfperm},
787 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200788 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200789 {"setmatches", 1, 1, f_setmatches},
790 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200791 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200792 {"setreg", 2, 3, f_setreg},
793 {"settabvar", 3, 3, f_settabvar},
794 {"settabwinvar", 4, 4, f_settabwinvar},
795 {"setwinvar", 3, 3, f_setwinvar},
796#ifdef FEAT_CRYPT
797 {"sha256", 1, 1, f_sha256},
798#endif
799 {"shellescape", 1, 2, f_shellescape},
800 {"shiftwidth", 0, 0, f_shiftwidth},
801 {"simplify", 1, 1, f_simplify},
802#ifdef FEAT_FLOAT
803 {"sin", 1, 1, f_sin},
804 {"sinh", 1, 1, f_sinh},
805#endif
806 {"sort", 1, 3, f_sort},
807 {"soundfold", 1, 1, f_soundfold},
808 {"spellbadword", 0, 1, f_spellbadword},
809 {"spellsuggest", 1, 3, f_spellsuggest},
810 {"split", 1, 3, f_split},
811#ifdef FEAT_FLOAT
812 {"sqrt", 1, 1, f_sqrt},
813 {"str2float", 1, 1, f_str2float},
814#endif
815 {"str2nr", 1, 2, f_str2nr},
816 {"strcharpart", 2, 3, f_strcharpart},
817 {"strchars", 1, 2, f_strchars},
818 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
819#ifdef HAVE_STRFTIME
820 {"strftime", 1, 2, f_strftime},
821#endif
822 {"strgetchar", 2, 2, f_strgetchar},
823 {"stridx", 2, 3, f_stridx},
824 {"string", 1, 1, f_string},
825 {"strlen", 1, 1, f_strlen},
826 {"strpart", 2, 3, f_strpart},
827 {"strridx", 2, 3, f_strridx},
828 {"strtrans", 1, 1, f_strtrans},
829 {"strwidth", 1, 1, f_strwidth},
830 {"submatch", 1, 2, f_submatch},
831 {"substitute", 4, 4, f_substitute},
832 {"synID", 3, 3, f_synID},
833 {"synIDattr", 2, 3, f_synIDattr},
834 {"synIDtrans", 1, 1, f_synIDtrans},
835 {"synconcealed", 2, 2, f_synconcealed},
836 {"synstack", 2, 2, f_synstack},
837 {"system", 1, 2, f_system},
838 {"systemlist", 1, 2, f_systemlist},
839 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
840 {"tabpagenr", 0, 1, f_tabpagenr},
841 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
842 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100843 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200844#ifdef FEAT_FLOAT
845 {"tan", 1, 1, f_tan},
846 {"tanh", 1, 1, f_tanh},
847#endif
848 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200849#ifdef FEAT_TERMINAL
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200850 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200851 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200852 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200853 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200854 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200855 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200856 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200857 {"term_getstatus", 1, 1, f_term_getstatus},
858 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200859 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200860 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200861 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200862 {"term_sendkeys", 2, 2, f_term_sendkeys},
863 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200864 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200865#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200866 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
867 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200868 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200869 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100870 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200871#ifdef FEAT_JOB_CHANNEL
872 {"test_null_channel", 0, 0, f_test_null_channel},
873#endif
874 {"test_null_dict", 0, 0, f_test_null_dict},
875#ifdef FEAT_JOB_CHANNEL
876 {"test_null_job", 0, 0, f_test_null_job},
877#endif
878 {"test_null_list", 0, 0, f_test_null_list},
879 {"test_null_partial", 0, 0, f_test_null_partial},
880 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100881 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200882 {"test_settime", 1, 1, f_test_settime},
883#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200884 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200885 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200886 {"timer_start", 2, 3, f_timer_start},
887 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200888 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200889#endif
890 {"tolower", 1, 1, f_tolower},
891 {"toupper", 1, 1, f_toupper},
892 {"tr", 3, 3, f_tr},
893#ifdef FEAT_FLOAT
894 {"trunc", 1, 1, f_trunc},
895#endif
896 {"type", 1, 1, f_type},
897 {"undofile", 1, 1, f_undofile},
898 {"undotree", 0, 0, f_undotree},
899 {"uniq", 1, 3, f_uniq},
900 {"values", 1, 1, f_values},
901 {"virtcol", 1, 1, f_virtcol},
902 {"visualmode", 0, 1, f_visualmode},
903 {"wildmenumode", 0, 0, f_wildmenumode},
904 {"win_findbuf", 1, 1, f_win_findbuf},
905 {"win_getid", 0, 2, f_win_getid},
906 {"win_gotoid", 1, 1, f_win_gotoid},
907 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
908 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100909 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200910 {"winbufnr", 1, 1, f_winbufnr},
911 {"wincol", 0, 0, f_wincol},
912 {"winheight", 1, 1, f_winheight},
913 {"winline", 0, 0, f_winline},
914 {"winnr", 0, 1, f_winnr},
915 {"winrestcmd", 0, 0, f_winrestcmd},
916 {"winrestview", 1, 1, f_winrestview},
917 {"winsaveview", 0, 0, f_winsaveview},
918 {"winwidth", 1, 1, f_winwidth},
919 {"wordcount", 0, 0, f_wordcount},
920 {"writefile", 2, 3, f_writefile},
921 {"xor", 2, 2, f_xor},
922};
923
924#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
925
926/*
927 * Function given to ExpandGeneric() to obtain the list of internal
928 * or user defined function names.
929 */
930 char_u *
931get_function_name(expand_T *xp, int idx)
932{
933 static int intidx = -1;
934 char_u *name;
935
936 if (idx == 0)
937 intidx = -1;
938 if (intidx < 0)
939 {
940 name = get_user_func_name(xp, idx);
941 if (name != NULL)
942 return name;
943 }
944 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
945 {
946 STRCPY(IObuff, functions[intidx].f_name);
947 STRCAT(IObuff, "(");
948 if (functions[intidx].f_max_argc == 0)
949 STRCAT(IObuff, ")");
950 return IObuff;
951 }
952
953 return NULL;
954}
955
956/*
957 * Function given to ExpandGeneric() to obtain the list of internal or
958 * user defined variable or function names.
959 */
960 char_u *
961get_expr_name(expand_T *xp, int idx)
962{
963 static int intidx = -1;
964 char_u *name;
965
966 if (idx == 0)
967 intidx = -1;
968 if (intidx < 0)
969 {
970 name = get_function_name(xp, idx);
971 if (name != NULL)
972 return name;
973 }
974 return get_user_var_name(xp, ++intidx);
975}
976
977#endif /* FEAT_CMDL_COMPL */
978
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200979/*
980 * Find internal function in table above.
981 * Return index, or -1 if not found
982 */
983 int
984find_internal_func(
985 char_u *name) /* name of the function */
986{
987 int first = 0;
988 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
989 int cmp;
990 int x;
991
992 /*
993 * Find the function name in the table. Binary search.
994 */
995 while (first <= last)
996 {
997 x = first + ((unsigned)(last - first) >> 1);
998 cmp = STRCMP(name, functions[x].f_name);
999 if (cmp < 0)
1000 last = x - 1;
1001 else if (cmp > 0)
1002 first = x + 1;
1003 else
1004 return x;
1005 }
1006 return -1;
1007}
1008
1009 int
1010call_internal_func(
1011 char_u *name,
1012 int argcount,
1013 typval_T *argvars,
1014 typval_T *rettv)
1015{
1016 int i;
1017
1018 i = find_internal_func(name);
1019 if (i < 0)
1020 return ERROR_UNKNOWN;
1021 if (argcount < functions[i].f_min_argc)
1022 return ERROR_TOOFEW;
1023 if (argcount > functions[i].f_max_argc)
1024 return ERROR_TOOMANY;
1025 argvars[argcount].v_type = VAR_UNKNOWN;
1026 functions[i].f_func(argvars, rettv);
1027 return ERROR_NONE;
1028}
1029
1030/*
1031 * Return TRUE for a non-zero Number and a non-empty String.
1032 */
1033 static int
1034non_zero_arg(typval_T *argvars)
1035{
1036 return ((argvars[0].v_type == VAR_NUMBER
1037 && argvars[0].vval.v_number != 0)
1038 || (argvars[0].v_type == VAR_SPECIAL
1039 && argvars[0].vval.v_number == VVAL_TRUE)
1040 || (argvars[0].v_type == VAR_STRING
1041 && argvars[0].vval.v_string != NULL
1042 && *argvars[0].vval.v_string != NUL));
1043}
1044
1045/*
1046 * Get the lnum from the first argument.
1047 * Also accepts ".", "$", etc., but that only works for the current buffer.
1048 * Returns -1 on error.
1049 */
1050 static linenr_T
1051get_tv_lnum(typval_T *argvars)
1052{
1053 typval_T rettv;
1054 linenr_T lnum;
1055
1056 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1057 if (lnum == 0) /* no valid number, try using line() */
1058 {
1059 rettv.v_type = VAR_NUMBER;
1060 f_line(argvars, &rettv);
1061 lnum = (linenr_T)rettv.vval.v_number;
1062 clear_tv(&rettv);
1063 }
1064 return lnum;
1065}
1066
1067#ifdef FEAT_FLOAT
1068static int get_float_arg(typval_T *argvars, float_T *f);
1069
1070/*
1071 * Get the float value of "argvars[0]" into "f".
1072 * Returns FAIL when the argument is not a Number or Float.
1073 */
1074 static int
1075get_float_arg(typval_T *argvars, float_T *f)
1076{
1077 if (argvars[0].v_type == VAR_FLOAT)
1078 {
1079 *f = argvars[0].vval.v_float;
1080 return OK;
1081 }
1082 if (argvars[0].v_type == VAR_NUMBER)
1083 {
1084 *f = (float_T)argvars[0].vval.v_number;
1085 return OK;
1086 }
1087 EMSG(_("E808: Number or Float required"));
1088 return FAIL;
1089}
1090
1091/*
1092 * "abs(expr)" function
1093 */
1094 static void
1095f_abs(typval_T *argvars, typval_T *rettv)
1096{
1097 if (argvars[0].v_type == VAR_FLOAT)
1098 {
1099 rettv->v_type = VAR_FLOAT;
1100 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1101 }
1102 else
1103 {
1104 varnumber_T n;
1105 int error = FALSE;
1106
1107 n = get_tv_number_chk(&argvars[0], &error);
1108 if (error)
1109 rettv->vval.v_number = -1;
1110 else if (n > 0)
1111 rettv->vval.v_number = n;
1112 else
1113 rettv->vval.v_number = -n;
1114 }
1115}
1116
1117/*
1118 * "acos()" function
1119 */
1120 static void
1121f_acos(typval_T *argvars, typval_T *rettv)
1122{
1123 float_T f = 0.0;
1124
1125 rettv->v_type = VAR_FLOAT;
1126 if (get_float_arg(argvars, &f) == OK)
1127 rettv->vval.v_float = acos(f);
1128 else
1129 rettv->vval.v_float = 0.0;
1130}
1131#endif
1132
1133/*
1134 * "add(list, item)" function
1135 */
1136 static void
1137f_add(typval_T *argvars, typval_T *rettv)
1138{
1139 list_T *l;
1140
1141 rettv->vval.v_number = 1; /* Default: Failed */
1142 if (argvars[0].v_type == VAR_LIST)
1143 {
1144 if ((l = argvars[0].vval.v_list) != NULL
1145 && !tv_check_lock(l->lv_lock,
1146 (char_u *)N_("add() argument"), TRUE)
1147 && list_append_tv(l, &argvars[1]) == OK)
1148 copy_tv(&argvars[0], rettv);
1149 }
1150 else
1151 EMSG(_(e_listreq));
1152}
1153
1154/*
1155 * "and(expr, expr)" function
1156 */
1157 static void
1158f_and(typval_T *argvars, typval_T *rettv)
1159{
1160 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1161 & get_tv_number_chk(&argvars[1], NULL);
1162}
1163
1164/*
1165 * "append(lnum, string/list)" function
1166 */
1167 static void
1168f_append(typval_T *argvars, typval_T *rettv)
1169{
1170 long lnum;
1171 char_u *line;
1172 list_T *l = NULL;
1173 listitem_T *li = NULL;
1174 typval_T *tv;
1175 long added = 0;
1176
1177 /* When coming here from Insert mode, sync undo, so that this can be
1178 * undone separately from what was previously inserted. */
1179 if (u_sync_once == 2)
1180 {
1181 u_sync_once = 1; /* notify that u_sync() was called */
1182 u_sync(TRUE);
1183 }
1184
1185 lnum = get_tv_lnum(argvars);
1186 if (lnum >= 0
1187 && lnum <= curbuf->b_ml.ml_line_count
1188 && u_save(lnum, lnum + 1) == OK)
1189 {
1190 if (argvars[1].v_type == VAR_LIST)
1191 {
1192 l = argvars[1].vval.v_list;
1193 if (l == NULL)
1194 return;
1195 li = l->lv_first;
1196 }
1197 for (;;)
1198 {
1199 if (l == NULL)
1200 tv = &argvars[1]; /* append a string */
1201 else if (li == NULL)
1202 break; /* end of list */
1203 else
1204 tv = &li->li_tv; /* append item from list */
1205 line = get_tv_string_chk(tv);
1206 if (line == NULL) /* type error */
1207 {
1208 rettv->vval.v_number = 1; /* Failed */
1209 break;
1210 }
1211 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1212 ++added;
1213 if (l == NULL)
1214 break;
1215 li = li->li_next;
1216 }
1217
1218 appended_lines_mark(lnum, added);
1219 if (curwin->w_cursor.lnum > lnum)
1220 curwin->w_cursor.lnum += added;
1221 }
1222 else
1223 rettv->vval.v_number = 1; /* Failed */
1224}
1225
1226/*
1227 * "argc()" function
1228 */
1229 static void
1230f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1231{
1232 rettv->vval.v_number = ARGCOUNT;
1233}
1234
1235/*
1236 * "argidx()" function
1237 */
1238 static void
1239f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1240{
1241 rettv->vval.v_number = curwin->w_arg_idx;
1242}
1243
1244/*
1245 * "arglistid()" function
1246 */
1247 static void
1248f_arglistid(typval_T *argvars, typval_T *rettv)
1249{
1250 win_T *wp;
1251
1252 rettv->vval.v_number = -1;
1253 wp = find_tabwin(&argvars[0], &argvars[1]);
1254 if (wp != NULL)
1255 rettv->vval.v_number = wp->w_alist->id;
1256}
1257
1258/*
1259 * "argv(nr)" function
1260 */
1261 static void
1262f_argv(typval_T *argvars, typval_T *rettv)
1263{
1264 int idx;
1265
1266 if (argvars[0].v_type != VAR_UNKNOWN)
1267 {
1268 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1269 if (idx >= 0 && idx < ARGCOUNT)
1270 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1271 else
1272 rettv->vval.v_string = NULL;
1273 rettv->v_type = VAR_STRING;
1274 }
1275 else if (rettv_list_alloc(rettv) == OK)
1276 for (idx = 0; idx < ARGCOUNT; ++idx)
1277 list_append_string(rettv->vval.v_list,
1278 alist_name(&ARGLIST[idx]), -1);
1279}
1280
1281/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001282 * "assert_beeps(cmd [, error])" function
1283 */
1284 static void
1285f_assert_beeps(typval_T *argvars, typval_T *rettv UNUSED)
1286{
1287 assert_beeps(argvars);
1288}
1289
1290/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001291 * "assert_equal(expected, actual[, msg])" function
1292 */
1293 static void
1294f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1295{
1296 assert_equal_common(argvars, ASSERT_EQUAL);
1297}
1298
1299/*
1300 * "assert_notequal(expected, actual[, msg])" function
1301 */
1302 static void
1303f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1304{
1305 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1306}
1307
1308/*
1309 * "assert_exception(string[, msg])" function
1310 */
1311 static void
1312f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1313{
1314 assert_exception(argvars);
1315}
1316
1317/*
1318 * "assert_fails(cmd [, error])" function
1319 */
1320 static void
1321f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1322{
1323 assert_fails(argvars);
1324}
1325
1326/*
1327 * "assert_false(actual[, msg])" function
1328 */
1329 static void
1330f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1331{
1332 assert_bool(argvars, FALSE);
1333}
1334
1335/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001336 * "assert_inrange(lower, upper[, msg])" function
1337 */
1338 static void
1339f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1340{
1341 assert_inrange(argvars);
1342}
1343
1344/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001345 * "assert_match(pattern, actual[, msg])" function
1346 */
1347 static void
1348f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1349{
1350 assert_match_common(argvars, ASSERT_MATCH);
1351}
1352
1353/*
1354 * "assert_notmatch(pattern, actual[, msg])" function
1355 */
1356 static void
1357f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1358{
1359 assert_match_common(argvars, ASSERT_NOTMATCH);
1360}
1361
1362/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001363 * "assert_report(msg)" function
1364 */
1365 static void
1366f_assert_report(typval_T *argvars, typval_T *rettv UNUSED)
1367{
1368 assert_report(argvars);
1369}
1370
1371/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001372 * "assert_true(actual[, msg])" function
1373 */
1374 static void
1375f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1376{
1377 assert_bool(argvars, TRUE);
1378}
1379
1380#ifdef FEAT_FLOAT
1381/*
1382 * "asin()" function
1383 */
1384 static void
1385f_asin(typval_T *argvars, typval_T *rettv)
1386{
1387 float_T f = 0.0;
1388
1389 rettv->v_type = VAR_FLOAT;
1390 if (get_float_arg(argvars, &f) == OK)
1391 rettv->vval.v_float = asin(f);
1392 else
1393 rettv->vval.v_float = 0.0;
1394}
1395
1396/*
1397 * "atan()" function
1398 */
1399 static void
1400f_atan(typval_T *argvars, typval_T *rettv)
1401{
1402 float_T f = 0.0;
1403
1404 rettv->v_type = VAR_FLOAT;
1405 if (get_float_arg(argvars, &f) == OK)
1406 rettv->vval.v_float = atan(f);
1407 else
1408 rettv->vval.v_float = 0.0;
1409}
1410
1411/*
1412 * "atan2()" function
1413 */
1414 static void
1415f_atan2(typval_T *argvars, typval_T *rettv)
1416{
1417 float_T fx = 0.0, fy = 0.0;
1418
1419 rettv->v_type = VAR_FLOAT;
1420 if (get_float_arg(argvars, &fx) == OK
1421 && get_float_arg(&argvars[1], &fy) == OK)
1422 rettv->vval.v_float = atan2(fx, fy);
1423 else
1424 rettv->vval.v_float = 0.0;
1425}
1426#endif
1427
1428/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001429 * "balloon_show()" function
1430 */
1431#ifdef FEAT_BEVAL
1432 static void
1433f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1434{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001435 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001436 {
1437 if (argvars[0].v_type == VAR_LIST
1438# ifdef FEAT_GUI
1439 && !gui.in_use
1440# endif
1441 )
1442 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1443 else
1444 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1445 }
1446}
1447
Bram Moolenaar669a8282017-11-19 20:13:05 +01001448# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001449 static void
1450f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1451{
1452 if (rettv_list_alloc(rettv) == OK)
1453 {
1454 char_u *msg = get_tv_string_chk(&argvars[0]);
1455
1456 if (msg != NULL)
1457 {
1458 pumitem_T *array;
1459 int size = split_message(msg, &array);
1460 int i;
1461
1462 /* Skip the first and last item, they are always empty. */
1463 for (i = 1; i < size - 1; ++i)
1464 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001465 while (size > 0)
1466 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001467 vim_free(array);
1468 }
1469 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001470}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001471# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001472#endif
1473
1474/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001475 * "browse(save, title, initdir, default)" function
1476 */
1477 static void
1478f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1479{
1480#ifdef FEAT_BROWSE
1481 int save;
1482 char_u *title;
1483 char_u *initdir;
1484 char_u *defname;
1485 char_u buf[NUMBUFLEN];
1486 char_u buf2[NUMBUFLEN];
1487 int error = FALSE;
1488
1489 save = (int)get_tv_number_chk(&argvars[0], &error);
1490 title = get_tv_string_chk(&argvars[1]);
1491 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1492 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1493
1494 if (error || title == NULL || initdir == NULL || defname == NULL)
1495 rettv->vval.v_string = NULL;
1496 else
1497 rettv->vval.v_string =
1498 do_browse(save ? BROWSE_SAVE : 0,
1499 title, defname, NULL, initdir, NULL, curbuf);
1500#else
1501 rettv->vval.v_string = NULL;
1502#endif
1503 rettv->v_type = VAR_STRING;
1504}
1505
1506/*
1507 * "browsedir(title, initdir)" function
1508 */
1509 static void
1510f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1511{
1512#ifdef FEAT_BROWSE
1513 char_u *title;
1514 char_u *initdir;
1515 char_u buf[NUMBUFLEN];
1516
1517 title = get_tv_string_chk(&argvars[0]);
1518 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1519
1520 if (title == NULL || initdir == NULL)
1521 rettv->vval.v_string = NULL;
1522 else
1523 rettv->vval.v_string = do_browse(BROWSE_DIR,
1524 title, NULL, NULL, initdir, NULL, curbuf);
1525#else
1526 rettv->vval.v_string = NULL;
1527#endif
1528 rettv->v_type = VAR_STRING;
1529}
1530
1531static buf_T *find_buffer(typval_T *avar);
1532
1533/*
1534 * Find a buffer by number or exact name.
1535 */
1536 static buf_T *
1537find_buffer(typval_T *avar)
1538{
1539 buf_T *buf = NULL;
1540
1541 if (avar->v_type == VAR_NUMBER)
1542 buf = buflist_findnr((int)avar->vval.v_number);
1543 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1544 {
1545 buf = buflist_findname_exp(avar->vval.v_string);
1546 if (buf == NULL)
1547 {
1548 /* No full path name match, try a match with a URL or a "nofile"
1549 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001550 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001551 if (buf->b_fname != NULL
1552 && (path_with_url(buf->b_fname)
1553#ifdef FEAT_QUICKFIX
1554 || bt_nofile(buf)
1555#endif
1556 )
1557 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1558 break;
1559 }
1560 }
1561 return buf;
1562}
1563
1564/*
1565 * "bufexists(expr)" function
1566 */
1567 static void
1568f_bufexists(typval_T *argvars, typval_T *rettv)
1569{
1570 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1571}
1572
1573/*
1574 * "buflisted(expr)" function
1575 */
1576 static void
1577f_buflisted(typval_T *argvars, typval_T *rettv)
1578{
1579 buf_T *buf;
1580
1581 buf = find_buffer(&argvars[0]);
1582 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1583}
1584
1585/*
1586 * "bufloaded(expr)" function
1587 */
1588 static void
1589f_bufloaded(typval_T *argvars, typval_T *rettv)
1590{
1591 buf_T *buf;
1592
1593 buf = find_buffer(&argvars[0]);
1594 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1595}
1596
1597 buf_T *
1598buflist_find_by_name(char_u *name, int curtab_only)
1599{
1600 int save_magic;
1601 char_u *save_cpo;
1602 buf_T *buf;
1603
1604 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1605 save_magic = p_magic;
1606 p_magic = TRUE;
1607 save_cpo = p_cpo;
1608 p_cpo = (char_u *)"";
1609
1610 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1611 TRUE, FALSE, curtab_only));
1612
1613 p_magic = save_magic;
1614 p_cpo = save_cpo;
1615 return buf;
1616}
1617
1618/*
1619 * Get buffer by number or pattern.
1620 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001621 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001622get_buf_tv(typval_T *tv, int curtab_only)
1623{
1624 char_u *name = tv->vval.v_string;
1625 buf_T *buf;
1626
1627 if (tv->v_type == VAR_NUMBER)
1628 return buflist_findnr((int)tv->vval.v_number);
1629 if (tv->v_type != VAR_STRING)
1630 return NULL;
1631 if (name == NULL || *name == NUL)
1632 return curbuf;
1633 if (name[0] == '$' && name[1] == NUL)
1634 return lastbuf;
1635
1636 buf = buflist_find_by_name(name, curtab_only);
1637
1638 /* If not found, try expanding the name, like done for bufexists(). */
1639 if (buf == NULL)
1640 buf = find_buffer(tv);
1641
1642 return buf;
1643}
1644
1645/*
1646 * "bufname(expr)" function
1647 */
1648 static void
1649f_bufname(typval_T *argvars, typval_T *rettv)
1650{
1651 buf_T *buf;
1652
1653 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1654 ++emsg_off;
1655 buf = get_buf_tv(&argvars[0], FALSE);
1656 rettv->v_type = VAR_STRING;
1657 if (buf != NULL && buf->b_fname != NULL)
1658 rettv->vval.v_string = vim_strsave(buf->b_fname);
1659 else
1660 rettv->vval.v_string = NULL;
1661 --emsg_off;
1662}
1663
1664/*
1665 * "bufnr(expr)" function
1666 */
1667 static void
1668f_bufnr(typval_T *argvars, typval_T *rettv)
1669{
1670 buf_T *buf;
1671 int error = FALSE;
1672 char_u *name;
1673
1674 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1675 ++emsg_off;
1676 buf = get_buf_tv(&argvars[0], FALSE);
1677 --emsg_off;
1678
1679 /* If the buffer isn't found and the second argument is not zero create a
1680 * new buffer. */
1681 if (buf == NULL
1682 && argvars[1].v_type != VAR_UNKNOWN
1683 && get_tv_number_chk(&argvars[1], &error) != 0
1684 && !error
1685 && (name = get_tv_string_chk(&argvars[0])) != NULL
1686 && !error)
1687 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1688
1689 if (buf != NULL)
1690 rettv->vval.v_number = buf->b_fnum;
1691 else
1692 rettv->vval.v_number = -1;
1693}
1694
1695 static void
1696buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1697{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001698 win_T *wp;
1699 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001700 buf_T *buf;
1701
1702 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1703 ++emsg_off;
1704 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001705 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001706 {
1707 ++winnr;
1708 if (wp->w_buffer == buf)
1709 break;
1710 }
1711 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001712 --emsg_off;
1713}
1714
1715/*
1716 * "bufwinid(nr)" function
1717 */
1718 static void
1719f_bufwinid(typval_T *argvars, typval_T *rettv)
1720{
1721 buf_win_common(argvars, rettv, FALSE);
1722}
1723
1724/*
1725 * "bufwinnr(nr)" function
1726 */
1727 static void
1728f_bufwinnr(typval_T *argvars, typval_T *rettv)
1729{
1730 buf_win_common(argvars, rettv, TRUE);
1731}
1732
1733/*
1734 * "byte2line(byte)" function
1735 */
1736 static void
1737f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1738{
1739#ifndef FEAT_BYTEOFF
1740 rettv->vval.v_number = -1;
1741#else
1742 long boff = 0;
1743
1744 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1745 if (boff < 0)
1746 rettv->vval.v_number = -1;
1747 else
1748 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1749 (linenr_T)0, &boff);
1750#endif
1751}
1752
1753 static void
1754byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1755{
1756#ifdef FEAT_MBYTE
1757 char_u *t;
1758#endif
1759 char_u *str;
1760 varnumber_T idx;
1761
1762 str = get_tv_string_chk(&argvars[0]);
1763 idx = get_tv_number_chk(&argvars[1], NULL);
1764 rettv->vval.v_number = -1;
1765 if (str == NULL || idx < 0)
1766 return;
1767
1768#ifdef FEAT_MBYTE
1769 t = str;
1770 for ( ; idx > 0; idx--)
1771 {
1772 if (*t == NUL) /* EOL reached */
1773 return;
1774 if (enc_utf8 && comp)
1775 t += utf_ptr2len(t);
1776 else
1777 t += (*mb_ptr2len)(t);
1778 }
1779 rettv->vval.v_number = (varnumber_T)(t - str);
1780#else
1781 if ((size_t)idx <= STRLEN(str))
1782 rettv->vval.v_number = idx;
1783#endif
1784}
1785
1786/*
1787 * "byteidx()" function
1788 */
1789 static void
1790f_byteidx(typval_T *argvars, typval_T *rettv)
1791{
1792 byteidx(argvars, rettv, FALSE);
1793}
1794
1795/*
1796 * "byteidxcomp()" function
1797 */
1798 static void
1799f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1800{
1801 byteidx(argvars, rettv, TRUE);
1802}
1803
1804/*
1805 * "call(func, arglist [, dict])" function
1806 */
1807 static void
1808f_call(typval_T *argvars, typval_T *rettv)
1809{
1810 char_u *func;
1811 partial_T *partial = NULL;
1812 dict_T *selfdict = NULL;
1813
1814 if (argvars[1].v_type != VAR_LIST)
1815 {
1816 EMSG(_(e_listreq));
1817 return;
1818 }
1819 if (argvars[1].vval.v_list == NULL)
1820 return;
1821
1822 if (argvars[0].v_type == VAR_FUNC)
1823 func = argvars[0].vval.v_string;
1824 else if (argvars[0].v_type == VAR_PARTIAL)
1825 {
1826 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001827 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001828 }
1829 else
1830 func = get_tv_string(&argvars[0]);
1831 if (*func == NUL)
1832 return; /* type error or empty name */
1833
1834 if (argvars[2].v_type != VAR_UNKNOWN)
1835 {
1836 if (argvars[2].v_type != VAR_DICT)
1837 {
1838 EMSG(_(e_dictreq));
1839 return;
1840 }
1841 selfdict = argvars[2].vval.v_dict;
1842 }
1843
1844 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1845}
1846
1847#ifdef FEAT_FLOAT
1848/*
1849 * "ceil({float})" function
1850 */
1851 static void
1852f_ceil(typval_T *argvars, typval_T *rettv)
1853{
1854 float_T f = 0.0;
1855
1856 rettv->v_type = VAR_FLOAT;
1857 if (get_float_arg(argvars, &f) == OK)
1858 rettv->vval.v_float = ceil(f);
1859 else
1860 rettv->vval.v_float = 0.0;
1861}
1862#endif
1863
1864#ifdef FEAT_JOB_CHANNEL
1865/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001866 * "ch_canread()" function
1867 */
1868 static void
1869f_ch_canread(typval_T *argvars, typval_T *rettv)
1870{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001871 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001872
1873 rettv->vval.v_number = 0;
1874 if (channel != NULL)
1875 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1876 || channel_has_readahead(channel, PART_OUT)
1877 || channel_has_readahead(channel, PART_ERR);
1878}
1879
1880/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001881 * "ch_close()" function
1882 */
1883 static void
1884f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1885{
1886 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1887
1888 if (channel != NULL)
1889 {
1890 channel_close(channel, FALSE);
1891 channel_clear(channel);
1892 }
1893}
1894
1895/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001896 * "ch_close()" function
1897 */
1898 static void
1899f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1900{
1901 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1902
1903 if (channel != NULL)
1904 channel_close_in(channel);
1905}
1906
1907/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001908 * "ch_getbufnr()" function
1909 */
1910 static void
1911f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1912{
1913 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1914
1915 rettv->vval.v_number = -1;
1916 if (channel != NULL)
1917 {
1918 char_u *what = get_tv_string(&argvars[1]);
1919 int part;
1920
1921 if (STRCMP(what, "err") == 0)
1922 part = PART_ERR;
1923 else if (STRCMP(what, "out") == 0)
1924 part = PART_OUT;
1925 else if (STRCMP(what, "in") == 0)
1926 part = PART_IN;
1927 else
1928 part = PART_SOCK;
1929 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1930 rettv->vval.v_number =
1931 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1932 }
1933}
1934
1935/*
1936 * "ch_getjob()" function
1937 */
1938 static void
1939f_ch_getjob(typval_T *argvars, typval_T *rettv)
1940{
1941 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1942
1943 if (channel != NULL)
1944 {
1945 rettv->v_type = VAR_JOB;
1946 rettv->vval.v_job = channel->ch_job;
1947 if (channel->ch_job != NULL)
1948 ++channel->ch_job->jv_refcount;
1949 }
1950}
1951
1952/*
1953 * "ch_info()" function
1954 */
1955 static void
1956f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1957{
1958 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1959
1960 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1961 channel_info(channel, rettv->vval.v_dict);
1962}
1963
1964/*
1965 * "ch_log()" function
1966 */
1967 static void
1968f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1969{
1970 char_u *msg = get_tv_string(&argvars[0]);
1971 channel_T *channel = NULL;
1972
1973 if (argvars[1].v_type != VAR_UNKNOWN)
1974 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1975
1976 ch_log(channel, (char *)msg);
1977}
1978
1979/*
1980 * "ch_logfile()" function
1981 */
1982 static void
1983f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
1984{
1985 char_u *fname;
1986 char_u *opt = (char_u *)"";
1987 char_u buf[NUMBUFLEN];
1988
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02001989 /* Don't open a file in restricted mode. */
1990 if (check_restricted() || check_secure())
1991 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001992 fname = get_tv_string(&argvars[0]);
1993 if (argvars[1].v_type == VAR_STRING)
1994 opt = get_tv_string_buf(&argvars[1], buf);
1995 ch_logfile(fname, opt);
1996}
1997
1998/*
1999 * "ch_open()" function
2000 */
2001 static void
2002f_ch_open(typval_T *argvars, typval_T *rettv)
2003{
2004 rettv->v_type = VAR_CHANNEL;
2005 if (check_restricted() || check_secure())
2006 return;
2007 rettv->vval.v_channel = channel_open_func(argvars);
2008}
2009
2010/*
2011 * "ch_read()" function
2012 */
2013 static void
2014f_ch_read(typval_T *argvars, typval_T *rettv)
2015{
2016 common_channel_read(argvars, rettv, FALSE);
2017}
2018
2019/*
2020 * "ch_readraw()" function
2021 */
2022 static void
2023f_ch_readraw(typval_T *argvars, typval_T *rettv)
2024{
2025 common_channel_read(argvars, rettv, TRUE);
2026}
2027
2028/*
2029 * "ch_evalexpr()" function
2030 */
2031 static void
2032f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2033{
2034 ch_expr_common(argvars, rettv, TRUE);
2035}
2036
2037/*
2038 * "ch_sendexpr()" function
2039 */
2040 static void
2041f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2042{
2043 ch_expr_common(argvars, rettv, FALSE);
2044}
2045
2046/*
2047 * "ch_evalraw()" function
2048 */
2049 static void
2050f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2051{
2052 ch_raw_common(argvars, rettv, TRUE);
2053}
2054
2055/*
2056 * "ch_sendraw()" function
2057 */
2058 static void
2059f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2060{
2061 ch_raw_common(argvars, rettv, FALSE);
2062}
2063
2064/*
2065 * "ch_setoptions()" function
2066 */
2067 static void
2068f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2069{
2070 channel_T *channel;
2071 jobopt_T opt;
2072
2073 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2074 if (channel == NULL)
2075 return;
2076 clear_job_options(&opt);
2077 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002078 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002079 channel_set_options(channel, &opt);
2080 free_job_options(&opt);
2081}
2082
2083/*
2084 * "ch_status()" function
2085 */
2086 static void
2087f_ch_status(typval_T *argvars, typval_T *rettv)
2088{
2089 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002090 jobopt_T opt;
2091 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002092
2093 /* return an empty string by default */
2094 rettv->v_type = VAR_STRING;
2095 rettv->vval.v_string = NULL;
2096
2097 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002098
2099 if (argvars[1].v_type != VAR_UNKNOWN)
2100 {
2101 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002102 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002103 && (opt.jo_set & JO_PART))
2104 part = opt.jo_part;
2105 }
2106
2107 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002108}
2109#endif
2110
2111/*
2112 * "changenr()" function
2113 */
2114 static void
2115f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2116{
2117 rettv->vval.v_number = curbuf->b_u_seq_cur;
2118}
2119
2120/*
2121 * "char2nr(string)" function
2122 */
2123 static void
2124f_char2nr(typval_T *argvars, typval_T *rettv)
2125{
2126#ifdef FEAT_MBYTE
2127 if (has_mbyte)
2128 {
2129 int utf8 = 0;
2130
2131 if (argvars[1].v_type != VAR_UNKNOWN)
2132 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2133
2134 if (utf8)
2135 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2136 else
2137 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2138 }
2139 else
2140#endif
2141 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2142}
2143
2144/*
2145 * "cindent(lnum)" function
2146 */
2147 static void
2148f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2149{
2150#ifdef FEAT_CINDENT
2151 pos_T pos;
2152 linenr_T lnum;
2153
2154 pos = curwin->w_cursor;
2155 lnum = get_tv_lnum(argvars);
2156 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2157 {
2158 curwin->w_cursor.lnum = lnum;
2159 rettv->vval.v_number = get_c_indent();
2160 curwin->w_cursor = pos;
2161 }
2162 else
2163#endif
2164 rettv->vval.v_number = -1;
2165}
2166
2167/*
2168 * "clearmatches()" function
2169 */
2170 static void
2171f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2172{
2173#ifdef FEAT_SEARCH_EXTRA
2174 clear_matches(curwin);
2175#endif
2176}
2177
2178/*
2179 * "col(string)" function
2180 */
2181 static void
2182f_col(typval_T *argvars, typval_T *rettv)
2183{
2184 colnr_T col = 0;
2185 pos_T *fp;
2186 int fnum = curbuf->b_fnum;
2187
2188 fp = var2fpos(&argvars[0], FALSE, &fnum);
2189 if (fp != NULL && fnum == curbuf->b_fnum)
2190 {
2191 if (fp->col == MAXCOL)
2192 {
2193 /* '> can be MAXCOL, get the length of the line then */
2194 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2195 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2196 else
2197 col = MAXCOL;
2198 }
2199 else
2200 {
2201 col = fp->col + 1;
2202#ifdef FEAT_VIRTUALEDIT
2203 /* col(".") when the cursor is on the NUL at the end of the line
2204 * because of "coladd" can be seen as an extra column. */
2205 if (virtual_active() && fp == &curwin->w_cursor)
2206 {
2207 char_u *p = ml_get_cursor();
2208
2209 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2210 curwin->w_virtcol - curwin->w_cursor.coladd))
2211 {
2212# ifdef FEAT_MBYTE
2213 int l;
2214
2215 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2216 col += l;
2217# else
2218 if (*p != NUL && p[1] == NUL)
2219 ++col;
2220# endif
2221 }
2222 }
2223#endif
2224 }
2225 }
2226 rettv->vval.v_number = col;
2227}
2228
2229#if defined(FEAT_INS_EXPAND)
2230/*
2231 * "complete()" function
2232 */
2233 static void
2234f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2235{
2236 int startcol;
2237
2238 if ((State & INSERT) == 0)
2239 {
2240 EMSG(_("E785: complete() can only be used in Insert mode"));
2241 return;
2242 }
2243
2244 /* Check for undo allowed here, because if something was already inserted
2245 * the line was already saved for undo and this check isn't done. */
2246 if (!undo_allowed())
2247 return;
2248
2249 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2250 {
2251 EMSG(_(e_invarg));
2252 return;
2253 }
2254
2255 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2256 if (startcol <= 0)
2257 return;
2258
2259 set_completion(startcol - 1, argvars[1].vval.v_list);
2260}
2261
2262/*
2263 * "complete_add()" function
2264 */
2265 static void
2266f_complete_add(typval_T *argvars, typval_T *rettv)
2267{
2268 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2269}
2270
2271/*
2272 * "complete_check()" function
2273 */
2274 static void
2275f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2276{
2277 int saved = RedrawingDisabled;
2278
2279 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002280 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002281 rettv->vval.v_number = compl_interrupted;
2282 RedrawingDisabled = saved;
2283}
2284#endif
2285
2286/*
2287 * "confirm(message, buttons[, default [, type]])" function
2288 */
2289 static void
2290f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2291{
2292#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2293 char_u *message;
2294 char_u *buttons = NULL;
2295 char_u buf[NUMBUFLEN];
2296 char_u buf2[NUMBUFLEN];
2297 int def = 1;
2298 int type = VIM_GENERIC;
2299 char_u *typestr;
2300 int error = FALSE;
2301
2302 message = get_tv_string_chk(&argvars[0]);
2303 if (message == NULL)
2304 error = TRUE;
2305 if (argvars[1].v_type != VAR_UNKNOWN)
2306 {
2307 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2308 if (buttons == NULL)
2309 error = TRUE;
2310 if (argvars[2].v_type != VAR_UNKNOWN)
2311 {
2312 def = (int)get_tv_number_chk(&argvars[2], &error);
2313 if (argvars[3].v_type != VAR_UNKNOWN)
2314 {
2315 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2316 if (typestr == NULL)
2317 error = TRUE;
2318 else
2319 {
2320 switch (TOUPPER_ASC(*typestr))
2321 {
2322 case 'E': type = VIM_ERROR; break;
2323 case 'Q': type = VIM_QUESTION; break;
2324 case 'I': type = VIM_INFO; break;
2325 case 'W': type = VIM_WARNING; break;
2326 case 'G': type = VIM_GENERIC; break;
2327 }
2328 }
2329 }
2330 }
2331 }
2332
2333 if (buttons == NULL || *buttons == NUL)
2334 buttons = (char_u *)_("&Ok");
2335
2336 if (!error)
2337 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2338 def, NULL, FALSE);
2339#endif
2340}
2341
2342/*
2343 * "copy()" function
2344 */
2345 static void
2346f_copy(typval_T *argvars, typval_T *rettv)
2347{
2348 item_copy(&argvars[0], rettv, FALSE, 0);
2349}
2350
2351#ifdef FEAT_FLOAT
2352/*
2353 * "cos()" function
2354 */
2355 static void
2356f_cos(typval_T *argvars, typval_T *rettv)
2357{
2358 float_T f = 0.0;
2359
2360 rettv->v_type = VAR_FLOAT;
2361 if (get_float_arg(argvars, &f) == OK)
2362 rettv->vval.v_float = cos(f);
2363 else
2364 rettv->vval.v_float = 0.0;
2365}
2366
2367/*
2368 * "cosh()" function
2369 */
2370 static void
2371f_cosh(typval_T *argvars, typval_T *rettv)
2372{
2373 float_T f = 0.0;
2374
2375 rettv->v_type = VAR_FLOAT;
2376 if (get_float_arg(argvars, &f) == OK)
2377 rettv->vval.v_float = cosh(f);
2378 else
2379 rettv->vval.v_float = 0.0;
2380}
2381#endif
2382
2383/*
2384 * "count()" function
2385 */
2386 static void
2387f_count(typval_T *argvars, typval_T *rettv)
2388{
2389 long n = 0;
2390 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002391 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002392
Bram Moolenaar9966b212017-07-28 16:46:57 +02002393 if (argvars[2].v_type != VAR_UNKNOWN)
2394 ic = (int)get_tv_number_chk(&argvars[2], &error);
2395
2396 if (argvars[0].v_type == VAR_STRING)
2397 {
2398 char_u *expr = get_tv_string_chk(&argvars[1]);
2399 char_u *p = argvars[0].vval.v_string;
2400 char_u *next;
2401
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002402 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002403 {
2404 if (ic)
2405 {
2406 size_t len = STRLEN(expr);
2407
2408 while (*p != NUL)
2409 {
2410 if (MB_STRNICMP(p, expr, len) == 0)
2411 {
2412 ++n;
2413 p += len;
2414 }
2415 else
2416 MB_PTR_ADV(p);
2417 }
2418 }
2419 else
2420 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2421 != NULL)
2422 {
2423 ++n;
2424 p = next + STRLEN(expr);
2425 }
2426 }
2427
2428 }
2429 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002430 {
2431 listitem_T *li;
2432 list_T *l;
2433 long idx;
2434
2435 if ((l = argvars[0].vval.v_list) != NULL)
2436 {
2437 li = l->lv_first;
2438 if (argvars[2].v_type != VAR_UNKNOWN)
2439 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002440 if (argvars[3].v_type != VAR_UNKNOWN)
2441 {
2442 idx = (long)get_tv_number_chk(&argvars[3], &error);
2443 if (!error)
2444 {
2445 li = list_find(l, idx);
2446 if (li == NULL)
2447 EMSGN(_(e_listidx), idx);
2448 }
2449 }
2450 if (error)
2451 li = NULL;
2452 }
2453
2454 for ( ; li != NULL; li = li->li_next)
2455 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2456 ++n;
2457 }
2458 }
2459 else if (argvars[0].v_type == VAR_DICT)
2460 {
2461 int todo;
2462 dict_T *d;
2463 hashitem_T *hi;
2464
2465 if ((d = argvars[0].vval.v_dict) != NULL)
2466 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002467 if (argvars[2].v_type != VAR_UNKNOWN)
2468 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002469 if (argvars[3].v_type != VAR_UNKNOWN)
2470 EMSG(_(e_invarg));
2471 }
2472
2473 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2474 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2475 {
2476 if (!HASHITEM_EMPTY(hi))
2477 {
2478 --todo;
2479 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2480 ++n;
2481 }
2482 }
2483 }
2484 }
2485 else
2486 EMSG2(_(e_listdictarg), "count()");
2487 rettv->vval.v_number = n;
2488}
2489
2490/*
2491 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2492 *
2493 * Checks the existence of a cscope connection.
2494 */
2495 static void
2496f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2497{
2498#ifdef FEAT_CSCOPE
2499 int num = 0;
2500 char_u *dbpath = NULL;
2501 char_u *prepend = NULL;
2502 char_u buf[NUMBUFLEN];
2503
2504 if (argvars[0].v_type != VAR_UNKNOWN
2505 && argvars[1].v_type != VAR_UNKNOWN)
2506 {
2507 num = (int)get_tv_number(&argvars[0]);
2508 dbpath = get_tv_string(&argvars[1]);
2509 if (argvars[2].v_type != VAR_UNKNOWN)
2510 prepend = get_tv_string_buf(&argvars[2], buf);
2511 }
2512
2513 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2514#endif
2515}
2516
2517/*
2518 * "cursor(lnum, col)" function, or
2519 * "cursor(list)"
2520 *
2521 * Moves the cursor to the specified line and column.
2522 * Returns 0 when the position could be set, -1 otherwise.
2523 */
2524 static void
2525f_cursor(typval_T *argvars, typval_T *rettv)
2526{
2527 long line, col;
2528#ifdef FEAT_VIRTUALEDIT
2529 long coladd = 0;
2530#endif
2531 int set_curswant = TRUE;
2532
2533 rettv->vval.v_number = -1;
2534 if (argvars[1].v_type == VAR_UNKNOWN)
2535 {
2536 pos_T pos;
2537 colnr_T curswant = -1;
2538
2539 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2540 {
2541 EMSG(_(e_invarg));
2542 return;
2543 }
2544 line = pos.lnum;
2545 col = pos.col;
2546#ifdef FEAT_VIRTUALEDIT
2547 coladd = pos.coladd;
2548#endif
2549 if (curswant >= 0)
2550 {
2551 curwin->w_curswant = curswant - 1;
2552 set_curswant = FALSE;
2553 }
2554 }
2555 else
2556 {
2557 line = get_tv_lnum(argvars);
2558 col = (long)get_tv_number_chk(&argvars[1], NULL);
2559#ifdef FEAT_VIRTUALEDIT
2560 if (argvars[2].v_type != VAR_UNKNOWN)
2561 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2562#endif
2563 }
2564 if (line < 0 || col < 0
2565#ifdef FEAT_VIRTUALEDIT
2566 || coladd < 0
2567#endif
2568 )
2569 return; /* type error; errmsg already given */
2570 if (line > 0)
2571 curwin->w_cursor.lnum = line;
2572 if (col > 0)
2573 curwin->w_cursor.col = col - 1;
2574#ifdef FEAT_VIRTUALEDIT
2575 curwin->w_cursor.coladd = coladd;
2576#endif
2577
2578 /* Make sure the cursor is in a valid position. */
2579 check_cursor();
2580#ifdef FEAT_MBYTE
2581 /* Correct cursor for multi-byte character. */
2582 if (has_mbyte)
2583 mb_adjust_cursor();
2584#endif
2585
2586 curwin->w_set_curswant = set_curswant;
2587 rettv->vval.v_number = 0;
2588}
2589
2590/*
2591 * "deepcopy()" function
2592 */
2593 static void
2594f_deepcopy(typval_T *argvars, typval_T *rettv)
2595{
2596 int noref = 0;
2597 int copyID;
2598
2599 if (argvars[1].v_type != VAR_UNKNOWN)
2600 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2601 if (noref < 0 || noref > 1)
2602 EMSG(_(e_invarg));
2603 else
2604 {
2605 copyID = get_copyID();
2606 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2607 }
2608}
2609
2610/*
2611 * "delete()" function
2612 */
2613 static void
2614f_delete(typval_T *argvars, typval_T *rettv)
2615{
2616 char_u nbuf[NUMBUFLEN];
2617 char_u *name;
2618 char_u *flags;
2619
2620 rettv->vval.v_number = -1;
2621 if (check_restricted() || check_secure())
2622 return;
2623
2624 name = get_tv_string(&argvars[0]);
2625 if (name == NULL || *name == NUL)
2626 {
2627 EMSG(_(e_invarg));
2628 return;
2629 }
2630
2631 if (argvars[1].v_type != VAR_UNKNOWN)
2632 flags = get_tv_string_buf(&argvars[1], nbuf);
2633 else
2634 flags = (char_u *)"";
2635
2636 if (*flags == NUL)
2637 /* delete a file */
2638 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2639 else if (STRCMP(flags, "d") == 0)
2640 /* delete an empty directory */
2641 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2642 else if (STRCMP(flags, "rf") == 0)
2643 /* delete a directory recursively */
2644 rettv->vval.v_number = delete_recursive(name);
2645 else
2646 EMSG2(_(e_invexpr2), flags);
2647}
2648
2649/*
2650 * "did_filetype()" function
2651 */
2652 static void
2653f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2654{
2655#ifdef FEAT_AUTOCMD
2656 rettv->vval.v_number = did_filetype;
2657#endif
2658}
2659
2660/*
2661 * "diff_filler()" function
2662 */
2663 static void
2664f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2665{
2666#ifdef FEAT_DIFF
2667 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2668#endif
2669}
2670
2671/*
2672 * "diff_hlID()" function
2673 */
2674 static void
2675f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2676{
2677#ifdef FEAT_DIFF
2678 linenr_T lnum = get_tv_lnum(argvars);
2679 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002680 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002681 static int fnum = 0;
2682 static int change_start = 0;
2683 static int change_end = 0;
2684 static hlf_T hlID = (hlf_T)0;
2685 int filler_lines;
2686 int col;
2687
2688 if (lnum < 0) /* ignore type error in {lnum} arg */
2689 lnum = 0;
2690 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002691 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002692 || fnum != curbuf->b_fnum)
2693 {
2694 /* New line, buffer, change: need to get the values. */
2695 filler_lines = diff_check(curwin, lnum);
2696 if (filler_lines < 0)
2697 {
2698 if (filler_lines == -1)
2699 {
2700 change_start = MAXCOL;
2701 change_end = -1;
2702 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2703 hlID = HLF_ADD; /* added line */
2704 else
2705 hlID = HLF_CHD; /* changed line */
2706 }
2707 else
2708 hlID = HLF_ADD; /* added line */
2709 }
2710 else
2711 hlID = (hlf_T)0;
2712 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002713 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002714 fnum = curbuf->b_fnum;
2715 }
2716
2717 if (hlID == HLF_CHD || hlID == HLF_TXD)
2718 {
2719 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2720 if (col >= change_start && col <= change_end)
2721 hlID = HLF_TXD; /* changed text */
2722 else
2723 hlID = HLF_CHD; /* changed line */
2724 }
2725 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2726#endif
2727}
2728
2729/*
2730 * "empty({expr})" function
2731 */
2732 static void
2733f_empty(typval_T *argvars, typval_T *rettv)
2734{
2735 int n = FALSE;
2736
2737 switch (argvars[0].v_type)
2738 {
2739 case VAR_STRING:
2740 case VAR_FUNC:
2741 n = argvars[0].vval.v_string == NULL
2742 || *argvars[0].vval.v_string == NUL;
2743 break;
2744 case VAR_PARTIAL:
2745 n = FALSE;
2746 break;
2747 case VAR_NUMBER:
2748 n = argvars[0].vval.v_number == 0;
2749 break;
2750 case VAR_FLOAT:
2751#ifdef FEAT_FLOAT
2752 n = argvars[0].vval.v_float == 0.0;
2753 break;
2754#endif
2755 case VAR_LIST:
2756 n = argvars[0].vval.v_list == NULL
2757 || argvars[0].vval.v_list->lv_first == NULL;
2758 break;
2759 case VAR_DICT:
2760 n = argvars[0].vval.v_dict == NULL
2761 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2762 break;
2763 case VAR_SPECIAL:
2764 n = argvars[0].vval.v_number != VVAL_TRUE;
2765 break;
2766
2767 case VAR_JOB:
2768#ifdef FEAT_JOB_CHANNEL
2769 n = argvars[0].vval.v_job == NULL
2770 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2771 break;
2772#endif
2773 case VAR_CHANNEL:
2774#ifdef FEAT_JOB_CHANNEL
2775 n = argvars[0].vval.v_channel == NULL
2776 || !channel_is_open(argvars[0].vval.v_channel);
2777 break;
2778#endif
2779 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002780 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002781 n = TRUE;
2782 break;
2783 }
2784
2785 rettv->vval.v_number = n;
2786}
2787
2788/*
2789 * "escape({string}, {chars})" function
2790 */
2791 static void
2792f_escape(typval_T *argvars, typval_T *rettv)
2793{
2794 char_u buf[NUMBUFLEN];
2795
2796 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2797 get_tv_string_buf(&argvars[1], buf));
2798 rettv->v_type = VAR_STRING;
2799}
2800
2801/*
2802 * "eval()" function
2803 */
2804 static void
2805f_eval(typval_T *argvars, typval_T *rettv)
2806{
2807 char_u *s, *p;
2808
2809 s = get_tv_string_chk(&argvars[0]);
2810 if (s != NULL)
2811 s = skipwhite(s);
2812
2813 p = s;
2814 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2815 {
2816 if (p != NULL && !aborting())
2817 EMSG2(_(e_invexpr2), p);
2818 need_clr_eos = FALSE;
2819 rettv->v_type = VAR_NUMBER;
2820 rettv->vval.v_number = 0;
2821 }
2822 else if (*s != NUL)
2823 EMSG(_(e_trailing));
2824}
2825
2826/*
2827 * "eventhandler()" function
2828 */
2829 static void
2830f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2831{
2832 rettv->vval.v_number = vgetc_busy;
2833}
2834
2835/*
2836 * "executable()" function
2837 */
2838 static void
2839f_executable(typval_T *argvars, typval_T *rettv)
2840{
2841 char_u *name = get_tv_string(&argvars[0]);
2842
2843 /* Check in $PATH and also check directly if there is a directory name. */
2844 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2845 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2846}
2847
2848static garray_T redir_execute_ga;
2849
2850/*
2851 * Append "value[value_len]" to the execute() output.
2852 */
2853 void
2854execute_redir_str(char_u *value, int value_len)
2855{
2856 int len;
2857
2858 if (value_len == -1)
2859 len = (int)STRLEN(value); /* Append the entire string */
2860 else
2861 len = value_len; /* Append only "value_len" characters */
2862 if (ga_grow(&redir_execute_ga, len) == OK)
2863 {
2864 mch_memmove((char *)redir_execute_ga.ga_data
2865 + redir_execute_ga.ga_len, value, len);
2866 redir_execute_ga.ga_len += len;
2867 }
2868}
2869
2870/*
2871 * Get next line from a list.
2872 * Called by do_cmdline() to get the next line.
2873 * Returns allocated string, or NULL for end of function.
2874 */
2875
2876 static char_u *
2877get_list_line(
2878 int c UNUSED,
2879 void *cookie,
2880 int indent UNUSED)
2881{
2882 listitem_T **p = (listitem_T **)cookie;
2883 listitem_T *item = *p;
2884 char_u buf[NUMBUFLEN];
2885 char_u *s;
2886
2887 if (item == NULL)
2888 return NULL;
2889 s = get_tv_string_buf_chk(&item->li_tv, buf);
2890 *p = item->li_next;
2891 return s == NULL ? NULL : vim_strsave(s);
2892}
2893
2894/*
2895 * "execute()" function
2896 */
2897 static void
2898f_execute(typval_T *argvars, typval_T *rettv)
2899{
2900 char_u *cmd = NULL;
2901 list_T *list = NULL;
2902 int save_msg_silent = msg_silent;
2903 int save_emsg_silent = emsg_silent;
2904 int save_emsg_noredir = emsg_noredir;
2905 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002906 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002907 garray_T save_ga;
2908
2909 rettv->vval.v_string = NULL;
2910 rettv->v_type = VAR_STRING;
2911
2912 if (argvars[0].v_type == VAR_LIST)
2913 {
2914 list = argvars[0].vval.v_list;
2915 if (list == NULL || list->lv_first == NULL)
2916 /* empty list, no commands, empty output */
2917 return;
2918 ++list->lv_refcount;
2919 }
2920 else
2921 {
2922 cmd = get_tv_string_chk(&argvars[0]);
2923 if (cmd == NULL)
2924 return;
2925 }
2926
2927 if (argvars[1].v_type != VAR_UNKNOWN)
2928 {
2929 char_u buf[NUMBUFLEN];
2930 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2931
2932 if (s == NULL)
2933 return;
2934 if (STRNCMP(s, "silent", 6) == 0)
2935 ++msg_silent;
2936 if (STRCMP(s, "silent!") == 0)
2937 {
2938 emsg_silent = TRUE;
2939 emsg_noredir = TRUE;
2940 }
2941 }
2942 else
2943 ++msg_silent;
2944
2945 if (redir_execute)
2946 save_ga = redir_execute_ga;
2947 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2948 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002949 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002950
2951 if (cmd != NULL)
2952 do_cmdline_cmd(cmd);
2953 else
2954 {
2955 listitem_T *item = list->lv_first;
2956
2957 do_cmdline(NULL, get_list_line, (void *)&item,
2958 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2959 --list->lv_refcount;
2960 }
2961
Bram Moolenaard297f352017-01-29 20:31:21 +01002962 /* Need to append a NUL to the result. */
2963 if (ga_grow(&redir_execute_ga, 1) == OK)
2964 {
2965 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2966 rettv->vval.v_string = redir_execute_ga.ga_data;
2967 }
2968 else
2969 {
2970 ga_clear(&redir_execute_ga);
2971 rettv->vval.v_string = NULL;
2972 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002973 msg_silent = save_msg_silent;
2974 emsg_silent = save_emsg_silent;
2975 emsg_noredir = save_emsg_noredir;
2976
2977 redir_execute = save_redir_execute;
2978 if (redir_execute)
2979 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002980 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002981
2982 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
2983 * line. Put it back in the first column. */
2984 msg_col = 0;
2985}
2986
2987/*
2988 * "exepath()" function
2989 */
2990 static void
2991f_exepath(typval_T *argvars, typval_T *rettv)
2992{
2993 char_u *p = NULL;
2994
2995 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
2996 rettv->v_type = VAR_STRING;
2997 rettv->vval.v_string = p;
2998}
2999
3000/*
3001 * "exists()" function
3002 */
3003 static void
3004f_exists(typval_T *argvars, typval_T *rettv)
3005{
3006 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003007 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003008
3009 p = get_tv_string(&argvars[0]);
3010 if (*p == '$') /* environment variable */
3011 {
3012 /* first try "normal" environment variables (fast) */
3013 if (mch_getenv(p + 1) != NULL)
3014 n = TRUE;
3015 else
3016 {
3017 /* try expanding things like $VIM and ${HOME} */
3018 p = expand_env_save(p);
3019 if (p != NULL && *p != '$')
3020 n = TRUE;
3021 vim_free(p);
3022 }
3023 }
3024 else if (*p == '&' || *p == '+') /* option */
3025 {
3026 n = (get_option_tv(&p, NULL, TRUE) == OK);
3027 if (*skipwhite(p) != NUL)
3028 n = FALSE; /* trailing garbage */
3029 }
3030 else if (*p == '*') /* internal or user defined function */
3031 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003032 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033 }
3034 else if (*p == ':')
3035 {
3036 n = cmd_exists(p + 1);
3037 }
3038 else if (*p == '#')
3039 {
3040#ifdef FEAT_AUTOCMD
3041 if (p[1] == '#')
3042 n = autocmd_supported(p + 2);
3043 else
3044 n = au_exists(p + 1);
3045#endif
3046 }
3047 else /* internal variable */
3048 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003049 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003050 }
3051
3052 rettv->vval.v_number = n;
3053}
3054
3055#ifdef FEAT_FLOAT
3056/*
3057 * "exp()" function
3058 */
3059 static void
3060f_exp(typval_T *argvars, typval_T *rettv)
3061{
3062 float_T f = 0.0;
3063
3064 rettv->v_type = VAR_FLOAT;
3065 if (get_float_arg(argvars, &f) == OK)
3066 rettv->vval.v_float = exp(f);
3067 else
3068 rettv->vval.v_float = 0.0;
3069}
3070#endif
3071
3072/*
3073 * "expand()" function
3074 */
3075 static void
3076f_expand(typval_T *argvars, typval_T *rettv)
3077{
3078 char_u *s;
3079 int len;
3080 char_u *errormsg;
3081 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3082 expand_T xpc;
3083 int error = FALSE;
3084 char_u *result;
3085
3086 rettv->v_type = VAR_STRING;
3087 if (argvars[1].v_type != VAR_UNKNOWN
3088 && argvars[2].v_type != VAR_UNKNOWN
3089 && get_tv_number_chk(&argvars[2], &error)
3090 && !error)
3091 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003092 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003093 }
3094
3095 s = get_tv_string(&argvars[0]);
3096 if (*s == '%' || *s == '#' || *s == '<')
3097 {
3098 ++emsg_off;
3099 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3100 --emsg_off;
3101 if (rettv->v_type == VAR_LIST)
3102 {
3103 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3104 list_append_string(rettv->vval.v_list, result, -1);
3105 else
3106 vim_free(result);
3107 }
3108 else
3109 rettv->vval.v_string = result;
3110 }
3111 else
3112 {
3113 /* When the optional second argument is non-zero, don't remove matches
3114 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3115 if (argvars[1].v_type != VAR_UNKNOWN
3116 && get_tv_number_chk(&argvars[1], &error))
3117 options |= WILD_KEEP_ALL;
3118 if (!error)
3119 {
3120 ExpandInit(&xpc);
3121 xpc.xp_context = EXPAND_FILES;
3122 if (p_wic)
3123 options += WILD_ICASE;
3124 if (rettv->v_type == VAR_STRING)
3125 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3126 options, WILD_ALL);
3127 else if (rettv_list_alloc(rettv) != FAIL)
3128 {
3129 int i;
3130
3131 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3132 for (i = 0; i < xpc.xp_numfiles; i++)
3133 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3134 ExpandCleanup(&xpc);
3135 }
3136 }
3137 else
3138 rettv->vval.v_string = NULL;
3139 }
3140}
3141
3142/*
3143 * "extend(list, list [, idx])" function
3144 * "extend(dict, dict [, action])" function
3145 */
3146 static void
3147f_extend(typval_T *argvars, typval_T *rettv)
3148{
3149 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3150
3151 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3152 {
3153 list_T *l1, *l2;
3154 listitem_T *item;
3155 long before;
3156 int error = FALSE;
3157
3158 l1 = argvars[0].vval.v_list;
3159 l2 = argvars[1].vval.v_list;
3160 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3161 && l2 != NULL)
3162 {
3163 if (argvars[2].v_type != VAR_UNKNOWN)
3164 {
3165 before = (long)get_tv_number_chk(&argvars[2], &error);
3166 if (error)
3167 return; /* type error; errmsg already given */
3168
3169 if (before == l1->lv_len)
3170 item = NULL;
3171 else
3172 {
3173 item = list_find(l1, before);
3174 if (item == NULL)
3175 {
3176 EMSGN(_(e_listidx), before);
3177 return;
3178 }
3179 }
3180 }
3181 else
3182 item = NULL;
3183 list_extend(l1, l2, item);
3184
3185 copy_tv(&argvars[0], rettv);
3186 }
3187 }
3188 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3189 {
3190 dict_T *d1, *d2;
3191 char_u *action;
3192 int i;
3193
3194 d1 = argvars[0].vval.v_dict;
3195 d2 = argvars[1].vval.v_dict;
3196 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3197 && d2 != NULL)
3198 {
3199 /* Check the third argument. */
3200 if (argvars[2].v_type != VAR_UNKNOWN)
3201 {
3202 static char *(av[]) = {"keep", "force", "error"};
3203
3204 action = get_tv_string_chk(&argvars[2]);
3205 if (action == NULL)
3206 return; /* type error; errmsg already given */
3207 for (i = 0; i < 3; ++i)
3208 if (STRCMP(action, av[i]) == 0)
3209 break;
3210 if (i == 3)
3211 {
3212 EMSG2(_(e_invarg2), action);
3213 return;
3214 }
3215 }
3216 else
3217 action = (char_u *)"force";
3218
3219 dict_extend(d1, d2, action);
3220
3221 copy_tv(&argvars[0], rettv);
3222 }
3223 }
3224 else
3225 EMSG2(_(e_listdictarg), "extend()");
3226}
3227
3228/*
3229 * "feedkeys()" function
3230 */
3231 static void
3232f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3233{
3234 int remap = TRUE;
3235 int insert = FALSE;
3236 char_u *keys, *flags;
3237 char_u nbuf[NUMBUFLEN];
3238 int typed = FALSE;
3239 int execute = FALSE;
3240 int dangerous = FALSE;
3241 char_u *keys_esc;
3242
3243 /* This is not allowed in the sandbox. If the commands would still be
3244 * executed in the sandbox it would be OK, but it probably happens later,
3245 * when "sandbox" is no longer set. */
3246 if (check_secure())
3247 return;
3248
3249 keys = get_tv_string(&argvars[0]);
3250
3251 if (argvars[1].v_type != VAR_UNKNOWN)
3252 {
3253 flags = get_tv_string_buf(&argvars[1], nbuf);
3254 for ( ; *flags != NUL; ++flags)
3255 {
3256 switch (*flags)
3257 {
3258 case 'n': remap = FALSE; break;
3259 case 'm': remap = TRUE; break;
3260 case 't': typed = TRUE; break;
3261 case 'i': insert = TRUE; break;
3262 case 'x': execute = TRUE; break;
3263 case '!': dangerous = TRUE; break;
3264 }
3265 }
3266 }
3267
3268 if (*keys != NUL || execute)
3269 {
3270 /* Need to escape K_SPECIAL and CSI before putting the string in the
3271 * typeahead buffer. */
3272 keys_esc = vim_strsave_escape_csi(keys);
3273 if (keys_esc != NULL)
3274 {
3275 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3276 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3277 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003278 if (vgetc_busy
3279#ifdef FEAT_TIMERS
3280 || timer_busy
3281#endif
3282 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003283 typebuf_was_filled = TRUE;
3284 if (execute)
3285 {
3286 int save_msg_scroll = msg_scroll;
3287
3288 /* Avoid a 1 second delay when the keys start Insert mode. */
3289 msg_scroll = FALSE;
3290
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02003291#ifdef FEAT_TERMINAL
3292 if (term_use_loop())
3293 terminal_loop(FALSE);
3294 else
3295#endif
3296 {
3297 if (!dangerous)
3298 ++ex_normal_busy;
3299 exec_normal(TRUE);
3300 if (!dangerous)
3301 --ex_normal_busy;
3302 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003303 msg_scroll |= save_msg_scroll;
3304 }
3305 }
3306 }
3307}
3308
3309/*
3310 * "filereadable()" function
3311 */
3312 static void
3313f_filereadable(typval_T *argvars, typval_T *rettv)
3314{
3315 int fd;
3316 char_u *p;
3317 int n;
3318
3319#ifndef O_NONBLOCK
3320# define O_NONBLOCK 0
3321#endif
3322 p = get_tv_string(&argvars[0]);
3323 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3324 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3325 {
3326 n = TRUE;
3327 close(fd);
3328 }
3329 else
3330 n = FALSE;
3331
3332 rettv->vval.v_number = n;
3333}
3334
3335/*
3336 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3337 * rights to write into.
3338 */
3339 static void
3340f_filewritable(typval_T *argvars, typval_T *rettv)
3341{
3342 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3343}
3344
3345 static void
3346findfilendir(
3347 typval_T *argvars UNUSED,
3348 typval_T *rettv,
3349 int find_what UNUSED)
3350{
3351#ifdef FEAT_SEARCHPATH
3352 char_u *fname;
3353 char_u *fresult = NULL;
3354 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3355 char_u *p;
3356 char_u pathbuf[NUMBUFLEN];
3357 int count = 1;
3358 int first = TRUE;
3359 int error = FALSE;
3360#endif
3361
3362 rettv->vval.v_string = NULL;
3363 rettv->v_type = VAR_STRING;
3364
3365#ifdef FEAT_SEARCHPATH
3366 fname = get_tv_string(&argvars[0]);
3367
3368 if (argvars[1].v_type != VAR_UNKNOWN)
3369 {
3370 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3371 if (p == NULL)
3372 error = TRUE;
3373 else
3374 {
3375 if (*p != NUL)
3376 path = p;
3377
3378 if (argvars[2].v_type != VAR_UNKNOWN)
3379 count = (int)get_tv_number_chk(&argvars[2], &error);
3380 }
3381 }
3382
3383 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3384 error = TRUE;
3385
3386 if (*fname != NUL && !error)
3387 {
3388 do
3389 {
3390 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3391 vim_free(fresult);
3392 fresult = find_file_in_path_option(first ? fname : NULL,
3393 first ? (int)STRLEN(fname) : 0,
3394 0, first, path,
3395 find_what,
3396 curbuf->b_ffname,
3397 find_what == FINDFILE_DIR
3398 ? (char_u *)"" : curbuf->b_p_sua);
3399 first = FALSE;
3400
3401 if (fresult != NULL && rettv->v_type == VAR_LIST)
3402 list_append_string(rettv->vval.v_list, fresult, -1);
3403
3404 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3405 }
3406
3407 if (rettv->v_type == VAR_STRING)
3408 rettv->vval.v_string = fresult;
3409#endif
3410}
3411
3412/*
3413 * "filter()" function
3414 */
3415 static void
3416f_filter(typval_T *argvars, typval_T *rettv)
3417{
3418 filter_map(argvars, rettv, FALSE);
3419}
3420
3421/*
3422 * "finddir({fname}[, {path}[, {count}]])" function
3423 */
3424 static void
3425f_finddir(typval_T *argvars, typval_T *rettv)
3426{
3427 findfilendir(argvars, rettv, FINDFILE_DIR);
3428}
3429
3430/*
3431 * "findfile({fname}[, {path}[, {count}]])" function
3432 */
3433 static void
3434f_findfile(typval_T *argvars, typval_T *rettv)
3435{
3436 findfilendir(argvars, rettv, FINDFILE_FILE);
3437}
3438
3439#ifdef FEAT_FLOAT
3440/*
3441 * "float2nr({float})" function
3442 */
3443 static void
3444f_float2nr(typval_T *argvars, typval_T *rettv)
3445{
3446 float_T f = 0.0;
3447
3448 if (get_float_arg(argvars, &f) == OK)
3449 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003450 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003451 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003452 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003453 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003454 else
3455 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003456 }
3457}
3458
3459/*
3460 * "floor({float})" function
3461 */
3462 static void
3463f_floor(typval_T *argvars, typval_T *rettv)
3464{
3465 float_T f = 0.0;
3466
3467 rettv->v_type = VAR_FLOAT;
3468 if (get_float_arg(argvars, &f) == OK)
3469 rettv->vval.v_float = floor(f);
3470 else
3471 rettv->vval.v_float = 0.0;
3472}
3473
3474/*
3475 * "fmod()" function
3476 */
3477 static void
3478f_fmod(typval_T *argvars, typval_T *rettv)
3479{
3480 float_T fx = 0.0, fy = 0.0;
3481
3482 rettv->v_type = VAR_FLOAT;
3483 if (get_float_arg(argvars, &fx) == OK
3484 && get_float_arg(&argvars[1], &fy) == OK)
3485 rettv->vval.v_float = fmod(fx, fy);
3486 else
3487 rettv->vval.v_float = 0.0;
3488}
3489#endif
3490
3491/*
3492 * "fnameescape({string})" function
3493 */
3494 static void
3495f_fnameescape(typval_T *argvars, typval_T *rettv)
3496{
3497 rettv->vval.v_string = vim_strsave_fnameescape(
3498 get_tv_string(&argvars[0]), FALSE);
3499 rettv->v_type = VAR_STRING;
3500}
3501
3502/*
3503 * "fnamemodify({fname}, {mods})" function
3504 */
3505 static void
3506f_fnamemodify(typval_T *argvars, typval_T *rettv)
3507{
3508 char_u *fname;
3509 char_u *mods;
3510 int usedlen = 0;
3511 int len;
3512 char_u *fbuf = NULL;
3513 char_u buf[NUMBUFLEN];
3514
3515 fname = get_tv_string_chk(&argvars[0]);
3516 mods = get_tv_string_buf_chk(&argvars[1], buf);
3517 if (fname == NULL || mods == NULL)
3518 fname = NULL;
3519 else
3520 {
3521 len = (int)STRLEN(fname);
3522 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3523 }
3524
3525 rettv->v_type = VAR_STRING;
3526 if (fname == NULL)
3527 rettv->vval.v_string = NULL;
3528 else
3529 rettv->vval.v_string = vim_strnsave(fname, len);
3530 vim_free(fbuf);
3531}
3532
3533static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3534
3535/*
3536 * "foldclosed()" function
3537 */
3538 static void
3539foldclosed_both(
3540 typval_T *argvars UNUSED,
3541 typval_T *rettv,
3542 int end UNUSED)
3543{
3544#ifdef FEAT_FOLDING
3545 linenr_T lnum;
3546 linenr_T first, last;
3547
3548 lnum = get_tv_lnum(argvars);
3549 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3550 {
3551 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3552 {
3553 if (end)
3554 rettv->vval.v_number = (varnumber_T)last;
3555 else
3556 rettv->vval.v_number = (varnumber_T)first;
3557 return;
3558 }
3559 }
3560#endif
3561 rettv->vval.v_number = -1;
3562}
3563
3564/*
3565 * "foldclosed()" function
3566 */
3567 static void
3568f_foldclosed(typval_T *argvars, typval_T *rettv)
3569{
3570 foldclosed_both(argvars, rettv, FALSE);
3571}
3572
3573/*
3574 * "foldclosedend()" function
3575 */
3576 static void
3577f_foldclosedend(typval_T *argvars, typval_T *rettv)
3578{
3579 foldclosed_both(argvars, rettv, TRUE);
3580}
3581
3582/*
3583 * "foldlevel()" function
3584 */
3585 static void
3586f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3587{
3588#ifdef FEAT_FOLDING
3589 linenr_T lnum;
3590
3591 lnum = get_tv_lnum(argvars);
3592 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3593 rettv->vval.v_number = foldLevel(lnum);
3594#endif
3595}
3596
3597/*
3598 * "foldtext()" function
3599 */
3600 static void
3601f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3602{
3603#ifdef FEAT_FOLDING
3604 linenr_T foldstart;
3605 linenr_T foldend;
3606 char_u *dashes;
3607 linenr_T lnum;
3608 char_u *s;
3609 char_u *r;
3610 int len;
3611 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003612 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003613#endif
3614
3615 rettv->v_type = VAR_STRING;
3616 rettv->vval.v_string = NULL;
3617#ifdef FEAT_FOLDING
3618 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3619 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3620 dashes = get_vim_var_str(VV_FOLDDASHES);
3621 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3622 && dashes != NULL)
3623 {
3624 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003625 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003626 if (!linewhite(lnum))
3627 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003628
3629 /* Find interesting text in this line. */
3630 s = skipwhite(ml_get(lnum));
3631 /* skip C comment-start */
3632 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3633 {
3634 s = skipwhite(s + 2);
3635 if (*skipwhite(s) == NUL
3636 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3637 {
3638 s = skipwhite(ml_get(lnum + 1));
3639 if (*s == '*')
3640 s = skipwhite(s + 1);
3641 }
3642 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003643 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003644 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003645 r = alloc((unsigned)(STRLEN(txt)
3646 + STRLEN(dashes) /* for %s */
3647 + 20 /* for %3ld */
3648 + STRLEN(s))); /* concatenated */
3649 if (r != NULL)
3650 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003651 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003652 len = (int)STRLEN(r);
3653 STRCAT(r, s);
3654 /* remove 'foldmarker' and 'commentstring' */
3655 foldtext_cleanup(r + len);
3656 rettv->vval.v_string = r;
3657 }
3658 }
3659#endif
3660}
3661
3662/*
3663 * "foldtextresult(lnum)" function
3664 */
3665 static void
3666f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3667{
3668#ifdef FEAT_FOLDING
3669 linenr_T lnum;
3670 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003671 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003672 foldinfo_T foldinfo;
3673 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003674 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003675#endif
3676
3677 rettv->v_type = VAR_STRING;
3678 rettv->vval.v_string = NULL;
3679#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003680 if (entered)
3681 return; /* reject recursive use */
3682 entered = TRUE;
3683
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003684 lnum = get_tv_lnum(argvars);
3685 /* treat illegal types and illegal string values for {lnum} the same */
3686 if (lnum < 0)
3687 lnum = 0;
3688 fold_count = foldedCount(curwin, lnum, &foldinfo);
3689 if (fold_count > 0)
3690 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003691 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3692 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003693 if (text == buf)
3694 text = vim_strsave(text);
3695 rettv->vval.v_string = text;
3696 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003697
3698 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003699#endif
3700}
3701
3702/*
3703 * "foreground()" function
3704 */
3705 static void
3706f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3707{
3708#ifdef FEAT_GUI
3709 if (gui.in_use)
3710 gui_mch_set_foreground();
3711#else
3712# ifdef WIN32
3713 win32_set_foreground();
3714# endif
3715#endif
3716}
3717
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003718 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003719common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003720{
3721 char_u *s;
3722 char_u *name;
3723 int use_string = FALSE;
3724 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003725 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003726
3727 if (argvars[0].v_type == VAR_FUNC)
3728 {
3729 /* function(MyFunc, [arg], dict) */
3730 s = argvars[0].vval.v_string;
3731 }
3732 else if (argvars[0].v_type == VAR_PARTIAL
3733 && argvars[0].vval.v_partial != NULL)
3734 {
3735 /* function(dict.MyFunc, [arg]) */
3736 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003737 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003738 }
3739 else
3740 {
3741 /* function('MyFunc', [arg], dict) */
3742 s = get_tv_string(&argvars[0]);
3743 use_string = TRUE;
3744 }
3745
Bram Moolenaar843b8842016-08-21 14:36:15 +02003746 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003747 {
3748 name = s;
3749 trans_name = trans_function_name(&name, FALSE,
3750 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3751 if (*name != NUL)
3752 s = NULL;
3753 }
3754
Bram Moolenaar843b8842016-08-21 14:36:15 +02003755 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3756 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003757 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003758 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003759 else if (trans_name != NULL && (is_funcref
3760 ? find_func(trans_name) == NULL
3761 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003762 EMSG2(_("E700: Unknown function: %s"), s);
3763 else
3764 {
3765 int dict_idx = 0;
3766 int arg_idx = 0;
3767 list_T *list = NULL;
3768
3769 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3770 {
3771 char sid_buf[25];
3772 int off = *s == 's' ? 2 : 5;
3773
3774 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3775 * also be called from another script. Using trans_function_name()
3776 * would also work, but some plugins depend on the name being
3777 * printable text. */
3778 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3779 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3780 if (name != NULL)
3781 {
3782 STRCPY(name, sid_buf);
3783 STRCAT(name, s + off);
3784 }
3785 }
3786 else
3787 name = vim_strsave(s);
3788
3789 if (argvars[1].v_type != VAR_UNKNOWN)
3790 {
3791 if (argvars[2].v_type != VAR_UNKNOWN)
3792 {
3793 /* function(name, [args], dict) */
3794 arg_idx = 1;
3795 dict_idx = 2;
3796 }
3797 else if (argvars[1].v_type == VAR_DICT)
3798 /* function(name, dict) */
3799 dict_idx = 1;
3800 else
3801 /* function(name, [args]) */
3802 arg_idx = 1;
3803 if (dict_idx > 0)
3804 {
3805 if (argvars[dict_idx].v_type != VAR_DICT)
3806 {
3807 EMSG(_("E922: expected a dict"));
3808 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003809 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003810 }
3811 if (argvars[dict_idx].vval.v_dict == NULL)
3812 dict_idx = 0;
3813 }
3814 if (arg_idx > 0)
3815 {
3816 if (argvars[arg_idx].v_type != VAR_LIST)
3817 {
3818 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3819 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003820 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003821 }
3822 list = argvars[arg_idx].vval.v_list;
3823 if (list == NULL || list->lv_len == 0)
3824 arg_idx = 0;
3825 }
3826 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003827 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003828 {
3829 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3830
3831 /* result is a VAR_PARTIAL */
3832 if (pt == NULL)
3833 vim_free(name);
3834 else
3835 {
3836 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3837 {
3838 listitem_T *li;
3839 int i = 0;
3840 int arg_len = 0;
3841 int lv_len = 0;
3842
3843 if (arg_pt != NULL)
3844 arg_len = arg_pt->pt_argc;
3845 if (list != NULL)
3846 lv_len = list->lv_len;
3847 pt->pt_argc = arg_len + lv_len;
3848 pt->pt_argv = (typval_T *)alloc(
3849 sizeof(typval_T) * pt->pt_argc);
3850 if (pt->pt_argv == NULL)
3851 {
3852 vim_free(pt);
3853 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003854 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003855 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003856 for (i = 0; i < arg_len; i++)
3857 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3858 if (lv_len > 0)
3859 for (li = list->lv_first; li != NULL;
3860 li = li->li_next)
3861 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003862 }
3863
3864 /* For "function(dict.func, [], dict)" and "func" is a partial
3865 * use "dict". That is backwards compatible. */
3866 if (dict_idx > 0)
3867 {
3868 /* The dict is bound explicitly, pt_auto is FALSE. */
3869 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3870 ++pt->pt_dict->dv_refcount;
3871 }
3872 else if (arg_pt != NULL)
3873 {
3874 /* If the dict was bound automatically the result is also
3875 * bound automatically. */
3876 pt->pt_dict = arg_pt->pt_dict;
3877 pt->pt_auto = arg_pt->pt_auto;
3878 if (pt->pt_dict != NULL)
3879 ++pt->pt_dict->dv_refcount;
3880 }
3881
3882 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003883 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3884 {
3885 pt->pt_func = arg_pt->pt_func;
3886 func_ptr_ref(pt->pt_func);
3887 vim_free(name);
3888 }
3889 else if (is_funcref)
3890 {
3891 pt->pt_func = find_func(trans_name);
3892 func_ptr_ref(pt->pt_func);
3893 vim_free(name);
3894 }
3895 else
3896 {
3897 pt->pt_name = name;
3898 func_ref(name);
3899 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003900 }
3901 rettv->v_type = VAR_PARTIAL;
3902 rettv->vval.v_partial = pt;
3903 }
3904 else
3905 {
3906 /* result is a VAR_FUNC */
3907 rettv->v_type = VAR_FUNC;
3908 rettv->vval.v_string = name;
3909 func_ref(name);
3910 }
3911 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003912theend:
3913 vim_free(trans_name);
3914}
3915
3916/*
3917 * "funcref()" function
3918 */
3919 static void
3920f_funcref(typval_T *argvars, typval_T *rettv)
3921{
3922 common_function(argvars, rettv, TRUE);
3923}
3924
3925/*
3926 * "function()" function
3927 */
3928 static void
3929f_function(typval_T *argvars, typval_T *rettv)
3930{
3931 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003932}
3933
3934/*
3935 * "garbagecollect()" function
3936 */
3937 static void
3938f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3939{
3940 /* This is postponed until we are back at the toplevel, because we may be
3941 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3942 want_garbage_collect = TRUE;
3943
3944 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3945 garbage_collect_at_exit = TRUE;
3946}
3947
3948/*
3949 * "get()" function
3950 */
3951 static void
3952f_get(typval_T *argvars, typval_T *rettv)
3953{
3954 listitem_T *li;
3955 list_T *l;
3956 dictitem_T *di;
3957 dict_T *d;
3958 typval_T *tv = NULL;
3959
3960 if (argvars[0].v_type == VAR_LIST)
3961 {
3962 if ((l = argvars[0].vval.v_list) != NULL)
3963 {
3964 int error = FALSE;
3965
3966 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3967 if (!error && li != NULL)
3968 tv = &li->li_tv;
3969 }
3970 }
3971 else if (argvars[0].v_type == VAR_DICT)
3972 {
3973 if ((d = argvars[0].vval.v_dict) != NULL)
3974 {
3975 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3976 if (di != NULL)
3977 tv = &di->di_tv;
3978 }
3979 }
3980 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3981 {
3982 partial_T *pt;
3983 partial_T fref_pt;
3984
3985 if (argvars[0].v_type == VAR_PARTIAL)
3986 pt = argvars[0].vval.v_partial;
3987 else
3988 {
3989 vim_memset(&fref_pt, 0, sizeof(fref_pt));
3990 fref_pt.pt_name = argvars[0].vval.v_string;
3991 pt = &fref_pt;
3992 }
3993
3994 if (pt != NULL)
3995 {
3996 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003997 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003998
3999 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4000 {
4001 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004002 n = partial_name(pt);
4003 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004004 rettv->vval.v_string = NULL;
4005 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004006 {
4007 rettv->vval.v_string = vim_strsave(n);
4008 if (rettv->v_type == VAR_FUNC)
4009 func_ref(rettv->vval.v_string);
4010 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004011 }
4012 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004013 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004014 else if (STRCMP(what, "args") == 0)
4015 {
4016 rettv->v_type = VAR_LIST;
4017 if (rettv_list_alloc(rettv) == OK)
4018 {
4019 int i;
4020
4021 for (i = 0; i < pt->pt_argc; ++i)
4022 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4023 }
4024 }
4025 else
4026 EMSG2(_(e_invarg2), what);
4027 return;
4028 }
4029 }
4030 else
4031 EMSG2(_(e_listdictarg), "get()");
4032
4033 if (tv == NULL)
4034 {
4035 if (argvars[2].v_type != VAR_UNKNOWN)
4036 copy_tv(&argvars[2], rettv);
4037 }
4038 else
4039 copy_tv(tv, rettv);
4040}
4041
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004042#ifdef FEAT_SIGNS
4043/*
4044 * Returns information about signs placed in a buffer as list of dicts.
4045 */
4046 static void
4047get_buffer_signs(buf_T *buf, list_T *l)
4048{
4049 signlist_T *sign;
4050
4051 for (sign = buf->b_signlist; sign; sign = sign->next)
4052 {
4053 dict_T *d = dict_alloc();
4054
4055 if (d != NULL)
4056 {
4057 dict_add_nr_str(d, "id", sign->id, NULL);
4058 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004059 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004060
4061 list_append_dict(l, d);
4062 }
4063 }
4064}
4065#endif
4066
4067/*
4068 * Returns buffer options, variables and other attributes in a dictionary.
4069 */
4070 static dict_T *
4071get_buffer_info(buf_T *buf)
4072{
4073 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004074 tabpage_T *tp;
4075 win_T *wp;
4076 list_T *windows;
4077
4078 dict = dict_alloc();
4079 if (dict == NULL)
4080 return NULL;
4081
Bram Moolenaar33928832016-08-18 21:22:04 +02004082 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004083 dict_add_nr_str(dict, "name", 0L,
4084 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004085 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4086 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004087 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4088 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4089 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004090 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004091 dict_add_nr_str(dict, "hidden",
4092 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4093 NULL);
4094
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004095 /* Get a reference to buffer variables */
4096 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004097
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004098 /* List of windows displaying this buffer */
4099 windows = list_alloc();
4100 if (windows != NULL)
4101 {
4102 FOR_ALL_TAB_WINDOWS(tp, wp)
4103 if (wp->w_buffer == buf)
4104 list_append_number(windows, (varnumber_T)wp->w_id);
4105 dict_add_list(dict, "windows", windows);
4106 }
4107
4108#ifdef FEAT_SIGNS
4109 if (buf->b_signlist != NULL)
4110 {
4111 /* List of signs placed in this buffer */
4112 list_T *signs = list_alloc();
4113 if (signs != NULL)
4114 {
4115 get_buffer_signs(buf, signs);
4116 dict_add_list(dict, "signs", signs);
4117 }
4118 }
4119#endif
4120
4121 return dict;
4122}
4123
4124/*
4125 * "getbufinfo()" function
4126 */
4127 static void
4128f_getbufinfo(typval_T *argvars, typval_T *rettv)
4129{
4130 buf_T *buf = NULL;
4131 buf_T *argbuf = NULL;
4132 dict_T *d;
4133 int filtered = FALSE;
4134 int sel_buflisted = FALSE;
4135 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004136 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004137
4138 if (rettv_list_alloc(rettv) != OK)
4139 return;
4140
4141 /* List of all the buffers or selected buffers */
4142 if (argvars[0].v_type == VAR_DICT)
4143 {
4144 dict_T *sel_d = argvars[0].vval.v_dict;
4145
4146 if (sel_d != NULL)
4147 {
4148 dictitem_T *di;
4149
4150 filtered = TRUE;
4151
4152 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4153 if (di != NULL && get_tv_number(&di->di_tv))
4154 sel_buflisted = TRUE;
4155
4156 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4157 if (di != NULL && get_tv_number(&di->di_tv))
4158 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004159
4160 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4161 if (di != NULL && get_tv_number(&di->di_tv))
4162 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004163 }
4164 }
4165 else if (argvars[0].v_type != VAR_UNKNOWN)
4166 {
4167 /* Information about one buffer. Argument specifies the buffer */
4168 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4169 ++emsg_off;
4170 argbuf = get_buf_tv(&argvars[0], FALSE);
4171 --emsg_off;
4172 if (argbuf == NULL)
4173 return;
4174 }
4175
4176 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004177 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004178 {
4179 if (argbuf != NULL && argbuf != buf)
4180 continue;
4181 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004182 || (sel_buflisted && !buf->b_p_bl)
4183 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004184 continue;
4185
4186 d = get_buffer_info(buf);
4187 if (d != NULL)
4188 list_append_dict(rettv->vval.v_list, d);
4189 if (argbuf != NULL)
4190 return;
4191 }
4192}
4193
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004194static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4195
4196/*
4197 * Get line or list of lines from buffer "buf" into "rettv".
4198 * Return a range (from start to end) of lines in rettv from the specified
4199 * buffer.
4200 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4201 */
4202 static void
4203get_buffer_lines(
4204 buf_T *buf,
4205 linenr_T start,
4206 linenr_T end,
4207 int retlist,
4208 typval_T *rettv)
4209{
4210 char_u *p;
4211
4212 rettv->v_type = VAR_STRING;
4213 rettv->vval.v_string = NULL;
4214 if (retlist && rettv_list_alloc(rettv) == FAIL)
4215 return;
4216
4217 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4218 return;
4219
4220 if (!retlist)
4221 {
4222 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4223 p = ml_get_buf(buf, start, FALSE);
4224 else
4225 p = (char_u *)"";
4226 rettv->vval.v_string = vim_strsave(p);
4227 }
4228 else
4229 {
4230 if (end < start)
4231 return;
4232
4233 if (start < 1)
4234 start = 1;
4235 if (end > buf->b_ml.ml_line_count)
4236 end = buf->b_ml.ml_line_count;
4237 while (start <= end)
4238 if (list_append_string(rettv->vval.v_list,
4239 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4240 break;
4241 }
4242}
4243
4244/*
4245 * Get the lnum from the first argument.
4246 * Also accepts "$", then "buf" is used.
4247 * Returns 0 on error.
4248 */
4249 static linenr_T
4250get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4251{
4252 if (argvars[0].v_type == VAR_STRING
4253 && argvars[0].vval.v_string != NULL
4254 && argvars[0].vval.v_string[0] == '$'
4255 && buf != NULL)
4256 return buf->b_ml.ml_line_count;
4257 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4258}
4259
4260/*
4261 * "getbufline()" function
4262 */
4263 static void
4264f_getbufline(typval_T *argvars, typval_T *rettv)
4265{
4266 linenr_T lnum;
4267 linenr_T end;
4268 buf_T *buf;
4269
4270 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4271 ++emsg_off;
4272 buf = get_buf_tv(&argvars[0], FALSE);
4273 --emsg_off;
4274
4275 lnum = get_tv_lnum_buf(&argvars[1], buf);
4276 if (argvars[2].v_type == VAR_UNKNOWN)
4277 end = lnum;
4278 else
4279 end = get_tv_lnum_buf(&argvars[2], buf);
4280
4281 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4282}
4283
4284/*
4285 * "getbufvar()" function
4286 */
4287 static void
4288f_getbufvar(typval_T *argvars, typval_T *rettv)
4289{
4290 buf_T *buf;
4291 buf_T *save_curbuf;
4292 char_u *varname;
4293 dictitem_T *v;
4294 int done = FALSE;
4295
4296 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4297 varname = get_tv_string_chk(&argvars[1]);
4298 ++emsg_off;
4299 buf = get_buf_tv(&argvars[0], FALSE);
4300
4301 rettv->v_type = VAR_STRING;
4302 rettv->vval.v_string = NULL;
4303
4304 if (buf != NULL && varname != NULL)
4305 {
4306 /* set curbuf to be our buf, temporarily */
4307 save_curbuf = curbuf;
4308 curbuf = buf;
4309
Bram Moolenaar30567352016-08-27 21:25:44 +02004310 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004311 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004312 if (varname[1] == NUL)
4313 {
4314 /* get all buffer-local options in a dict */
4315 dict_T *opts = get_winbuf_options(TRUE);
4316
4317 if (opts != NULL)
4318 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004319 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004320 done = TRUE;
4321 }
4322 }
4323 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4324 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004325 done = TRUE;
4326 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004327 else
4328 {
4329 /* Look up the variable. */
4330 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4331 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4332 'b', varname, FALSE);
4333 if (v != NULL)
4334 {
4335 copy_tv(&v->di_tv, rettv);
4336 done = TRUE;
4337 }
4338 }
4339
4340 /* restore previous notion of curbuf */
4341 curbuf = save_curbuf;
4342 }
4343
4344 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4345 /* use the default value */
4346 copy_tv(&argvars[2], rettv);
4347
4348 --emsg_off;
4349}
4350
4351/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004352 * "getchangelist()" function
4353 */
4354 static void
4355f_getchangelist(typval_T *argvars, typval_T *rettv)
4356{
4357#ifdef FEAT_JUMPLIST
4358 buf_T *buf;
4359 int i;
4360 list_T *l;
4361 dict_T *d;
4362#endif
4363
4364 if (rettv_list_alloc(rettv) != OK)
4365 return;
4366
4367#ifdef FEAT_JUMPLIST
4368 buf = find_buffer(&argvars[0]);
4369 if (buf == NULL)
4370 return;
4371
4372 l = list_alloc();
4373 if (l == NULL)
4374 return;
4375
4376 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4377 return;
4378 /*
4379 * The current window change list index tracks only the position in the
4380 * current buffer change list. For other buffers, use the change list
4381 * length as the current index.
4382 */
4383 list_append_number(rettv->vval.v_list,
4384 (varnumber_T)((buf == curwin->w_buffer)
4385 ? curwin->w_changelistidx : buf->b_changelistlen));
4386
4387 for (i = 0; i < buf->b_changelistlen; ++i)
4388 {
4389 if (buf->b_changelist[i].lnum == 0)
4390 continue;
4391 if ((d = dict_alloc()) == NULL)
4392 return;
4393 if (list_append_dict(l, d) == FAIL)
4394 return;
4395 dict_add_nr_str(d, "lnum", (long)buf->b_changelist[i].lnum, NULL);
4396 dict_add_nr_str(d, "col", (long)buf->b_changelist[i].col, NULL);
4397# ifdef FEAT_VIRTUALEDIT
4398 dict_add_nr_str(d, "coladd", (long)buf->b_changelist[i].coladd, NULL);
4399# endif
4400 }
4401#endif
4402}
4403/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004404 * "getchar()" function
4405 */
4406 static void
4407f_getchar(typval_T *argvars, typval_T *rettv)
4408{
4409 varnumber_T n;
4410 int error = FALSE;
4411
4412 /* Position the cursor. Needed after a message that ends in a space. */
4413 windgoto(msg_row, msg_col);
4414
4415 ++no_mapping;
4416 ++allow_keys;
4417 for (;;)
4418 {
4419 if (argvars[0].v_type == VAR_UNKNOWN)
4420 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004421 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004422 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4423 /* getchar(1): only check if char avail */
4424 n = vpeekc_any();
4425 else if (error || vpeekc_any() == NUL)
4426 /* illegal argument or getchar(0) and no char avail: return zero */
4427 n = 0;
4428 else
4429 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004430 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004431
4432 if (n == K_IGNORE)
4433 continue;
4434 break;
4435 }
4436 --no_mapping;
4437 --allow_keys;
4438
4439 set_vim_var_nr(VV_MOUSE_WIN, 0);
4440 set_vim_var_nr(VV_MOUSE_WINID, 0);
4441 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4442 set_vim_var_nr(VV_MOUSE_COL, 0);
4443
4444 rettv->vval.v_number = n;
4445 if (IS_SPECIAL(n) || mod_mask != 0)
4446 {
4447 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4448 int i = 0;
4449
4450 /* Turn a special key into three bytes, plus modifier. */
4451 if (mod_mask != 0)
4452 {
4453 temp[i++] = K_SPECIAL;
4454 temp[i++] = KS_MODIFIER;
4455 temp[i++] = mod_mask;
4456 }
4457 if (IS_SPECIAL(n))
4458 {
4459 temp[i++] = K_SPECIAL;
4460 temp[i++] = K_SECOND(n);
4461 temp[i++] = K_THIRD(n);
4462 }
4463#ifdef FEAT_MBYTE
4464 else if (has_mbyte)
4465 i += (*mb_char2bytes)(n, temp + i);
4466#endif
4467 else
4468 temp[i++] = n;
4469 temp[i++] = NUL;
4470 rettv->v_type = VAR_STRING;
4471 rettv->vval.v_string = vim_strsave(temp);
4472
4473#ifdef FEAT_MOUSE
4474 if (is_mouse_key(n))
4475 {
4476 int row = mouse_row;
4477 int col = mouse_col;
4478 win_T *win;
4479 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004480 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004481 int winnr = 1;
4482
4483 if (row >= 0 && col >= 0)
4484 {
4485 /* Find the window at the mouse coordinates and compute the
4486 * text position. */
4487 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004488 if (win == NULL)
4489 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004490 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004491 for (wp = firstwin; wp != win; wp = wp->w_next)
4492 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004493 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4494 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4495 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4496 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4497 }
4498 }
4499#endif
4500 }
4501}
4502
4503/*
4504 * "getcharmod()" function
4505 */
4506 static void
4507f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4508{
4509 rettv->vval.v_number = mod_mask;
4510}
4511
4512/*
4513 * "getcharsearch()" function
4514 */
4515 static void
4516f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4517{
4518 if (rettv_dict_alloc(rettv) != FAIL)
4519 {
4520 dict_T *dict = rettv->vval.v_dict;
4521
4522 dict_add_nr_str(dict, "char", 0L, last_csearch());
4523 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4524 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4525 }
4526}
4527
4528/*
4529 * "getcmdline()" function
4530 */
4531 static void
4532f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4533{
4534 rettv->v_type = VAR_STRING;
4535 rettv->vval.v_string = get_cmdline_str();
4536}
4537
4538/*
4539 * "getcmdpos()" function
4540 */
4541 static void
4542f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4543{
4544 rettv->vval.v_number = get_cmdline_pos() + 1;
4545}
4546
4547/*
4548 * "getcmdtype()" function
4549 */
4550 static void
4551f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4552{
4553 rettv->v_type = VAR_STRING;
4554 rettv->vval.v_string = alloc(2);
4555 if (rettv->vval.v_string != NULL)
4556 {
4557 rettv->vval.v_string[0] = get_cmdline_type();
4558 rettv->vval.v_string[1] = NUL;
4559 }
4560}
4561
4562/*
4563 * "getcmdwintype()" function
4564 */
4565 static void
4566f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4567{
4568 rettv->v_type = VAR_STRING;
4569 rettv->vval.v_string = NULL;
4570#ifdef FEAT_CMDWIN
4571 rettv->vval.v_string = alloc(2);
4572 if (rettv->vval.v_string != NULL)
4573 {
4574 rettv->vval.v_string[0] = cmdwin_type;
4575 rettv->vval.v_string[1] = NUL;
4576 }
4577#endif
4578}
4579
4580#if defined(FEAT_CMDL_COMPL)
4581/*
4582 * "getcompletion()" function
4583 */
4584 static void
4585f_getcompletion(typval_T *argvars, typval_T *rettv)
4586{
4587 char_u *pat;
4588 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004589 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004590 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4591 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004592
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004593 if (argvars[2].v_type != VAR_UNKNOWN)
4594 filtered = get_tv_number_chk(&argvars[2], NULL);
4595
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004596 if (p_wic)
4597 options |= WILD_ICASE;
4598
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004599 /* For filtered results, 'wildignore' is used */
4600 if (!filtered)
4601 options |= WILD_KEEP_ALL;
4602
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004603 ExpandInit(&xpc);
4604 xpc.xp_pattern = get_tv_string(&argvars[0]);
4605 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4606 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4607 if (xpc.xp_context == EXPAND_NOTHING)
4608 {
4609 if (argvars[1].v_type == VAR_STRING)
4610 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4611 else
4612 EMSG(_(e_invarg));
4613 return;
4614 }
4615
4616# if defined(FEAT_MENU)
4617 if (xpc.xp_context == EXPAND_MENUS)
4618 {
4619 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4620 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4621 }
4622# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004623#ifdef FEAT_CSCOPE
4624 if (xpc.xp_context == EXPAND_CSCOPE)
4625 {
4626 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4627 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4628 }
4629#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004630#ifdef FEAT_SIGNS
4631 if (xpc.xp_context == EXPAND_SIGN)
4632 {
4633 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4634 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4635 }
4636#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004637
4638 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4639 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4640 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004641 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004642
4643 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4644
4645 for (i = 0; i < xpc.xp_numfiles; i++)
4646 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4647 }
4648 vim_free(pat);
4649 ExpandCleanup(&xpc);
4650}
4651#endif
4652
4653/*
4654 * "getcwd()" function
4655 */
4656 static void
4657f_getcwd(typval_T *argvars, typval_T *rettv)
4658{
4659 win_T *wp = NULL;
4660 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004661 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004662
4663 rettv->v_type = VAR_STRING;
4664 rettv->vval.v_string = NULL;
4665
Bram Moolenaar54591292018-02-09 20:53:59 +01004666 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4667 global = TRUE;
4668 else
4669 wp = find_tabwin(&argvars[0], &argvars[1]);
4670
4671 if (wp != NULL && wp->w_localdir != NULL)
4672 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4673 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004674 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004675 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004676 rettv->vval.v_string = vim_strsave(globaldir);
4677 else
4678 {
4679 cwd = alloc(MAXPATHL);
4680 if (cwd != NULL)
4681 {
4682 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4683 rettv->vval.v_string = vim_strsave(cwd);
4684 vim_free(cwd);
4685 }
4686 }
4687#ifdef BACKSLASH_IN_FILENAME
4688 if (rettv->vval.v_string != NULL)
4689 slash_adjust(rettv->vval.v_string);
4690#endif
4691 }
4692}
4693
4694/*
4695 * "getfontname()" function
4696 */
4697 static void
4698f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4699{
4700 rettv->v_type = VAR_STRING;
4701 rettv->vval.v_string = NULL;
4702#ifdef FEAT_GUI
4703 if (gui.in_use)
4704 {
4705 GuiFont font;
4706 char_u *name = NULL;
4707
4708 if (argvars[0].v_type == VAR_UNKNOWN)
4709 {
4710 /* Get the "Normal" font. Either the name saved by
4711 * hl_set_font_name() or from the font ID. */
4712 font = gui.norm_font;
4713 name = hl_get_font_name();
4714 }
4715 else
4716 {
4717 name = get_tv_string(&argvars[0]);
4718 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4719 return;
4720 font = gui_mch_get_font(name, FALSE);
4721 if (font == NOFONT)
4722 return; /* Invalid font name, return empty string. */
4723 }
4724 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4725 if (argvars[0].v_type != VAR_UNKNOWN)
4726 gui_mch_free_font(font);
4727 }
4728#endif
4729}
4730
4731/*
4732 * "getfperm({fname})" function
4733 */
4734 static void
4735f_getfperm(typval_T *argvars, typval_T *rettv)
4736{
4737 char_u *fname;
4738 stat_T st;
4739 char_u *perm = NULL;
4740 char_u flags[] = "rwx";
4741 int i;
4742
4743 fname = get_tv_string(&argvars[0]);
4744
4745 rettv->v_type = VAR_STRING;
4746 if (mch_stat((char *)fname, &st) >= 0)
4747 {
4748 perm = vim_strsave((char_u *)"---------");
4749 if (perm != NULL)
4750 {
4751 for (i = 0; i < 9; i++)
4752 {
4753 if (st.st_mode & (1 << (8 - i)))
4754 perm[i] = flags[i % 3];
4755 }
4756 }
4757 }
4758 rettv->vval.v_string = perm;
4759}
4760
4761/*
4762 * "getfsize({fname})" function
4763 */
4764 static void
4765f_getfsize(typval_T *argvars, typval_T *rettv)
4766{
4767 char_u *fname;
4768 stat_T st;
4769
4770 fname = get_tv_string(&argvars[0]);
4771
4772 rettv->v_type = VAR_NUMBER;
4773
4774 if (mch_stat((char *)fname, &st) >= 0)
4775 {
4776 if (mch_isdir(fname))
4777 rettv->vval.v_number = 0;
4778 else
4779 {
4780 rettv->vval.v_number = (varnumber_T)st.st_size;
4781
4782 /* non-perfect check for overflow */
4783 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4784 rettv->vval.v_number = -2;
4785 }
4786 }
4787 else
4788 rettv->vval.v_number = -1;
4789}
4790
4791/*
4792 * "getftime({fname})" function
4793 */
4794 static void
4795f_getftime(typval_T *argvars, typval_T *rettv)
4796{
4797 char_u *fname;
4798 stat_T st;
4799
4800 fname = get_tv_string(&argvars[0]);
4801
4802 if (mch_stat((char *)fname, &st) >= 0)
4803 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4804 else
4805 rettv->vval.v_number = -1;
4806}
4807
4808/*
4809 * "getftype({fname})" function
4810 */
4811 static void
4812f_getftype(typval_T *argvars, typval_T *rettv)
4813{
4814 char_u *fname;
4815 stat_T st;
4816 char_u *type = NULL;
4817 char *t;
4818
4819 fname = get_tv_string(&argvars[0]);
4820
4821 rettv->v_type = VAR_STRING;
4822 if (mch_lstat((char *)fname, &st) >= 0)
4823 {
4824#ifdef S_ISREG
4825 if (S_ISREG(st.st_mode))
4826 t = "file";
4827 else if (S_ISDIR(st.st_mode))
4828 t = "dir";
4829# ifdef S_ISLNK
4830 else if (S_ISLNK(st.st_mode))
4831 t = "link";
4832# endif
4833# ifdef S_ISBLK
4834 else if (S_ISBLK(st.st_mode))
4835 t = "bdev";
4836# endif
4837# ifdef S_ISCHR
4838 else if (S_ISCHR(st.st_mode))
4839 t = "cdev";
4840# endif
4841# ifdef S_ISFIFO
4842 else if (S_ISFIFO(st.st_mode))
4843 t = "fifo";
4844# endif
4845# ifdef S_ISSOCK
4846 else if (S_ISSOCK(st.st_mode))
4847 t = "fifo";
4848# endif
4849 else
4850 t = "other";
4851#else
4852# ifdef S_IFMT
4853 switch (st.st_mode & S_IFMT)
4854 {
4855 case S_IFREG: t = "file"; break;
4856 case S_IFDIR: t = "dir"; break;
4857# ifdef S_IFLNK
4858 case S_IFLNK: t = "link"; break;
4859# endif
4860# ifdef S_IFBLK
4861 case S_IFBLK: t = "bdev"; break;
4862# endif
4863# ifdef S_IFCHR
4864 case S_IFCHR: t = "cdev"; break;
4865# endif
4866# ifdef S_IFIFO
4867 case S_IFIFO: t = "fifo"; break;
4868# endif
4869# ifdef S_IFSOCK
4870 case S_IFSOCK: t = "socket"; break;
4871# endif
4872 default: t = "other";
4873 }
4874# else
4875 if (mch_isdir(fname))
4876 t = "dir";
4877 else
4878 t = "file";
4879# endif
4880#endif
4881 type = vim_strsave((char_u *)t);
4882 }
4883 rettv->vval.v_string = type;
4884}
4885
4886/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004887 * "getjumplist()" function
4888 */
4889 static void
4890f_getjumplist(typval_T *argvars, typval_T *rettv)
4891{
4892#ifdef FEAT_JUMPLIST
4893 win_T *wp;
4894 int i;
4895 list_T *l;
4896 dict_T *d;
4897#endif
4898
4899 if (rettv_list_alloc(rettv) != OK)
4900 return;
4901
4902#ifdef FEAT_JUMPLIST
4903 wp = find_tabwin(&argvars[0], &argvars[1]);
4904 if (wp == NULL)
4905 return;
4906
4907 l = list_alloc();
4908 if (l == NULL)
4909 return;
4910
4911 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4912 return;
4913 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4914
Bram Moolenaar48679742018-02-13 13:33:29 +01004915 cleanup_jumplist(wp, TRUE);
4916
Bram Moolenaar4f505882018-02-10 21:06:32 +01004917 for (i = 0; i < wp->w_jumplistlen; ++i)
4918 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004919 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4920 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004921 if ((d = dict_alloc()) == NULL)
4922 return;
4923 if (list_append_dict(l, d) == FAIL)
4924 return;
4925 dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
4926 NULL);
4927 dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
4928 NULL);
4929# ifdef FEAT_VIRTUALEDIT
4930 dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
4931 NULL);
4932# endif
4933 dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004934 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaar4f505882018-02-10 21:06:32 +01004935 dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
4936 }
4937#endif
4938}
4939
4940/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004941 * "getline(lnum, [end])" function
4942 */
4943 static void
4944f_getline(typval_T *argvars, typval_T *rettv)
4945{
4946 linenr_T lnum;
4947 linenr_T end;
4948 int retlist;
4949
4950 lnum = get_tv_lnum(argvars);
4951 if (argvars[1].v_type == VAR_UNKNOWN)
4952 {
4953 end = 0;
4954 retlist = FALSE;
4955 }
4956 else
4957 {
4958 end = get_tv_lnum(&argvars[1]);
4959 retlist = TRUE;
4960 }
4961
4962 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4963}
4964
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004965#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004966 static void
4967get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4968{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004969 if (what_arg->v_type == VAR_UNKNOWN)
4970 {
4971 if (rettv_list_alloc(rettv) == OK)
4972 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004973 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004974 }
4975 else
4976 {
4977 if (rettv_dict_alloc(rettv) == OK)
4978 if (is_qf || (wp != NULL))
4979 {
4980 if (what_arg->v_type == VAR_DICT)
4981 {
4982 dict_T *d = what_arg->vval.v_dict;
4983
4984 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004985 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004986 }
4987 else
4988 EMSG(_(e_dictreq));
4989 }
4990 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004991}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004992#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004993
4994/*
4995 * "getloclist()" function
4996 */
4997 static void
4998f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4999{
5000#ifdef FEAT_QUICKFIX
5001 win_T *wp;
5002
5003 wp = find_win_by_nr(&argvars[0], NULL);
5004 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5005#endif
5006}
5007
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005008/*
5009 * "getmatches()" function
5010 */
5011 static void
5012f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5013{
5014#ifdef FEAT_SEARCH_EXTRA
5015 dict_T *dict;
5016 matchitem_T *cur = curwin->w_match_head;
5017 int i;
5018
5019 if (rettv_list_alloc(rettv) == OK)
5020 {
5021 while (cur != NULL)
5022 {
5023 dict = dict_alloc();
5024 if (dict == NULL)
5025 return;
5026 if (cur->match.regprog == NULL)
5027 {
5028 /* match added with matchaddpos() */
5029 for (i = 0; i < MAXPOSMATCH; ++i)
5030 {
5031 llpos_T *llpos;
5032 char buf[6];
5033 list_T *l;
5034
5035 llpos = &cur->pos.pos[i];
5036 if (llpos->lnum == 0)
5037 break;
5038 l = list_alloc();
5039 if (l == NULL)
5040 break;
5041 list_append_number(l, (varnumber_T)llpos->lnum);
5042 if (llpos->col > 0)
5043 {
5044 list_append_number(l, (varnumber_T)llpos->col);
5045 list_append_number(l, (varnumber_T)llpos->len);
5046 }
5047 sprintf(buf, "pos%d", i + 1);
5048 dict_add_list(dict, buf, l);
5049 }
5050 }
5051 else
5052 {
5053 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
5054 }
5055 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
5056 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
5057 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
5058# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5059 if (cur->conceal_char)
5060 {
5061 char_u buf[MB_MAXBYTES + 1];
5062
5063 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5064 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5065 }
5066# endif
5067 list_append_dict(rettv->vval.v_list, dict);
5068 cur = cur->next;
5069 }
5070 }
5071#endif
5072}
5073
5074/*
5075 * "getpid()" function
5076 */
5077 static void
5078f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5079{
5080 rettv->vval.v_number = mch_get_pid();
5081}
5082
5083 static void
5084getpos_both(
5085 typval_T *argvars,
5086 typval_T *rettv,
5087 int getcurpos)
5088{
5089 pos_T *fp;
5090 list_T *l;
5091 int fnum = -1;
5092
5093 if (rettv_list_alloc(rettv) == OK)
5094 {
5095 l = rettv->vval.v_list;
5096 if (getcurpos)
5097 fp = &curwin->w_cursor;
5098 else
5099 fp = var2fpos(&argvars[0], TRUE, &fnum);
5100 if (fnum != -1)
5101 list_append_number(l, (varnumber_T)fnum);
5102 else
5103 list_append_number(l, (varnumber_T)0);
5104 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5105 : (varnumber_T)0);
5106 list_append_number(l, (fp != NULL)
5107 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5108 : (varnumber_T)0);
5109 list_append_number(l,
5110#ifdef FEAT_VIRTUALEDIT
5111 (fp != NULL) ? (varnumber_T)fp->coladd :
5112#endif
5113 (varnumber_T)0);
5114 if (getcurpos)
5115 {
5116 update_curswant();
5117 list_append_number(l, curwin->w_curswant == MAXCOL ?
5118 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5119 }
5120 }
5121 else
5122 rettv->vval.v_number = FALSE;
5123}
5124
5125
5126/*
5127 * "getcurpos()" function
5128 */
5129 static void
5130f_getcurpos(typval_T *argvars, typval_T *rettv)
5131{
5132 getpos_both(argvars, rettv, TRUE);
5133}
5134
5135/*
5136 * "getpos(string)" function
5137 */
5138 static void
5139f_getpos(typval_T *argvars, typval_T *rettv)
5140{
5141 getpos_both(argvars, rettv, FALSE);
5142}
5143
5144/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005145 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005146 */
5147 static void
5148f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5149{
5150#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005151 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005152#endif
5153}
5154
5155/*
5156 * "getreg()" function
5157 */
5158 static void
5159f_getreg(typval_T *argvars, typval_T *rettv)
5160{
5161 char_u *strregname;
5162 int regname;
5163 int arg2 = FALSE;
5164 int return_list = FALSE;
5165 int error = FALSE;
5166
5167 if (argvars[0].v_type != VAR_UNKNOWN)
5168 {
5169 strregname = get_tv_string_chk(&argvars[0]);
5170 error = strregname == NULL;
5171 if (argvars[1].v_type != VAR_UNKNOWN)
5172 {
5173 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5174 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5175 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5176 }
5177 }
5178 else
5179 strregname = get_vim_var_str(VV_REG);
5180
5181 if (error)
5182 return;
5183
5184 regname = (strregname == NULL ? '"' : *strregname);
5185 if (regname == 0)
5186 regname = '"';
5187
5188 if (return_list)
5189 {
5190 rettv->v_type = VAR_LIST;
5191 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5192 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5193 if (rettv->vval.v_list == NULL)
5194 (void)rettv_list_alloc(rettv);
5195 else
5196 ++rettv->vval.v_list->lv_refcount;
5197 }
5198 else
5199 {
5200 rettv->v_type = VAR_STRING;
5201 rettv->vval.v_string = get_reg_contents(regname,
5202 arg2 ? GREG_EXPR_SRC : 0);
5203 }
5204}
5205
5206/*
5207 * "getregtype()" function
5208 */
5209 static void
5210f_getregtype(typval_T *argvars, typval_T *rettv)
5211{
5212 char_u *strregname;
5213 int regname;
5214 char_u buf[NUMBUFLEN + 2];
5215 long reglen = 0;
5216
5217 if (argvars[0].v_type != VAR_UNKNOWN)
5218 {
5219 strregname = get_tv_string_chk(&argvars[0]);
5220 if (strregname == NULL) /* type error; errmsg already given */
5221 {
5222 rettv->v_type = VAR_STRING;
5223 rettv->vval.v_string = NULL;
5224 return;
5225 }
5226 }
5227 else
5228 /* Default to v:register */
5229 strregname = get_vim_var_str(VV_REG);
5230
5231 regname = (strregname == NULL ? '"' : *strregname);
5232 if (regname == 0)
5233 regname = '"';
5234
5235 buf[0] = NUL;
5236 buf[1] = NUL;
5237 switch (get_reg_type(regname, &reglen))
5238 {
5239 case MLINE: buf[0] = 'V'; break;
5240 case MCHAR: buf[0] = 'v'; break;
5241 case MBLOCK:
5242 buf[0] = Ctrl_V;
5243 sprintf((char *)buf + 1, "%ld", reglen + 1);
5244 break;
5245 }
5246 rettv->v_type = VAR_STRING;
5247 rettv->vval.v_string = vim_strsave(buf);
5248}
5249
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005250/*
5251 * Returns information (variables, options, etc.) about a tab page
5252 * as a dictionary.
5253 */
5254 static dict_T *
5255get_tabpage_info(tabpage_T *tp, int tp_idx)
5256{
5257 win_T *wp;
5258 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005259 list_T *l;
5260
5261 dict = dict_alloc();
5262 if (dict == NULL)
5263 return NULL;
5264
Bram Moolenaar33928832016-08-18 21:22:04 +02005265 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005266
5267 l = list_alloc();
5268 if (l != NULL)
5269 {
5270 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5271 wp; wp = wp->w_next)
5272 list_append_number(l, (varnumber_T)wp->w_id);
5273 dict_add_list(dict, "windows", l);
5274 }
5275
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005276 /* Make a reference to tabpage variables */
5277 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005278
5279 return dict;
5280}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005281
5282/*
5283 * "gettabinfo()" function
5284 */
5285 static void
5286f_gettabinfo(typval_T *argvars, typval_T *rettv)
5287{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005288 tabpage_T *tp, *tparg = NULL;
5289 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005290 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005291
5292 if (rettv_list_alloc(rettv) != OK)
5293 return;
5294
5295 if (argvars[0].v_type != VAR_UNKNOWN)
5296 {
5297 /* Information about one tab page */
5298 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5299 if (tparg == NULL)
5300 return;
5301 }
5302
5303 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005304 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005305 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005306 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005307 if (tparg != NULL && tp != tparg)
5308 continue;
5309 d = get_tabpage_info(tp, tpnr);
5310 if (d != NULL)
5311 list_append_dict(rettv->vval.v_list, d);
5312 if (tparg != NULL)
5313 return;
5314 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005315}
5316
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005317/*
5318 * "gettabvar()" function
5319 */
5320 static void
5321f_gettabvar(typval_T *argvars, typval_T *rettv)
5322{
5323 win_T *oldcurwin;
5324 tabpage_T *tp, *oldtabpage;
5325 dictitem_T *v;
5326 char_u *varname;
5327 int done = FALSE;
5328
5329 rettv->v_type = VAR_STRING;
5330 rettv->vval.v_string = NULL;
5331
5332 varname = get_tv_string_chk(&argvars[1]);
5333 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5334 if (tp != NULL && varname != NULL)
5335 {
5336 /* Set tp to be our tabpage, temporarily. Also set the window to the
5337 * first window in the tabpage, otherwise the window is not valid. */
5338 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005339 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5340 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005341 {
5342 /* look up the variable */
5343 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5344 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5345 if (v != NULL)
5346 {
5347 copy_tv(&v->di_tv, rettv);
5348 done = TRUE;
5349 }
5350 }
5351
5352 /* restore previous notion of curwin */
5353 restore_win(oldcurwin, oldtabpage, TRUE);
5354 }
5355
5356 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5357 copy_tv(&argvars[2], rettv);
5358}
5359
5360/*
5361 * "gettabwinvar()" function
5362 */
5363 static void
5364f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5365{
5366 getwinvar(argvars, rettv, 1);
5367}
5368
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005369/*
5370 * Returns information about a window as a dictionary.
5371 */
5372 static dict_T *
5373get_win_info(win_T *wp, short tpnr, short winnr)
5374{
5375 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005376
5377 dict = dict_alloc();
5378 if (dict == NULL)
5379 return NULL;
5380
Bram Moolenaar33928832016-08-18 21:22:04 +02005381 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5382 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005383 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5384 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005385#ifdef FEAT_MENU
5386 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5387#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005388 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005389 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005390
Bram Moolenaar69905d12017-08-13 18:14:47 +02005391#ifdef FEAT_TERMINAL
5392 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5393#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005394#ifdef FEAT_QUICKFIX
5395 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5396 dict_add_nr_str(dict, "loclist",
5397 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5398#endif
5399
Bram Moolenaar30567352016-08-27 21:25:44 +02005400 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005401 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005402
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005403 return dict;
5404}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005405
5406/*
5407 * "getwininfo()" function
5408 */
5409 static void
5410f_getwininfo(typval_T *argvars, typval_T *rettv)
5411{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005412 tabpage_T *tp;
5413 win_T *wp = NULL, *wparg = NULL;
5414 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005415 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005416
5417 if (rettv_list_alloc(rettv) != OK)
5418 return;
5419
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005420 if (argvars[0].v_type != VAR_UNKNOWN)
5421 {
5422 wparg = win_id2wp(argvars);
5423 if (wparg == NULL)
5424 return;
5425 }
5426
5427 /* Collect information about either all the windows across all the tab
5428 * pages or one particular window.
5429 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005430 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005431 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005432 tabnr++;
5433 winnr = 0;
5434 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005435 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005436 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005437 if (wparg != NULL && wp != wparg)
5438 continue;
5439 d = get_win_info(wp, tabnr, winnr);
5440 if (d != NULL)
5441 list_append_dict(rettv->vval.v_list, d);
5442 if (wparg != NULL)
5443 /* found information about a specific window */
5444 return;
5445 }
5446 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005447}
5448
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005449/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005450 * "win_findbuf()" function
5451 */
5452 static void
5453f_win_findbuf(typval_T *argvars, typval_T *rettv)
5454{
5455 if (rettv_list_alloc(rettv) != FAIL)
5456 win_findbuf(argvars, rettv->vval.v_list);
5457}
5458
5459/*
5460 * "win_getid()" function
5461 */
5462 static void
5463f_win_getid(typval_T *argvars, typval_T *rettv)
5464{
5465 rettv->vval.v_number = win_getid(argvars);
5466}
5467
5468/*
5469 * "win_gotoid()" function
5470 */
5471 static void
5472f_win_gotoid(typval_T *argvars, typval_T *rettv)
5473{
5474 rettv->vval.v_number = win_gotoid(argvars);
5475}
5476
5477/*
5478 * "win_id2tabwin()" function
5479 */
5480 static void
5481f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5482{
5483 if (rettv_list_alloc(rettv) != FAIL)
5484 win_id2tabwin(argvars, rettv->vval.v_list);
5485}
5486
5487/*
5488 * "win_id2win()" function
5489 */
5490 static void
5491f_win_id2win(typval_T *argvars, typval_T *rettv)
5492{
5493 rettv->vval.v_number = win_id2win(argvars);
5494}
5495
5496/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005497 * "win_screenpos()" function
5498 */
5499 static void
5500f_win_screenpos(typval_T *argvars, typval_T *rettv)
5501{
5502 win_T *wp;
5503
5504 if (rettv_list_alloc(rettv) == FAIL)
5505 return;
5506
5507 wp = find_win_by_nr(&argvars[0], NULL);
5508 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5509 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5510}
5511
5512/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005513 * "getwinposx()" function
5514 */
5515 static void
5516f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5517{
5518 rettv->vval.v_number = -1;
5519#ifdef FEAT_GUI
5520 if (gui.in_use)
5521 {
5522 int x, y;
5523
5524 if (gui_mch_get_winpos(&x, &y) == OK)
5525 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005526 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005527 }
5528#endif
5529#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5530 {
5531 int x, y;
5532
5533 if (term_get_winpos(&x, &y) == OK)
5534 rettv->vval.v_number = x;
5535 }
5536#endif
5537}
5538
5539/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005540 * "getwinposy()" function
5541 */
5542 static void
5543f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5544{
5545 rettv->vval.v_number = -1;
5546#ifdef FEAT_GUI
5547 if (gui.in_use)
5548 {
5549 int x, y;
5550
5551 if (gui_mch_get_winpos(&x, &y) == OK)
5552 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005553 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005554 }
5555#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005556#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5557 {
5558 int x, y;
5559
5560 if (term_get_winpos(&x, &y) == OK)
5561 rettv->vval.v_number = y;
5562 }
5563#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005564}
5565
5566/*
5567 * "getwinvar()" function
5568 */
5569 static void
5570f_getwinvar(typval_T *argvars, typval_T *rettv)
5571{
5572 getwinvar(argvars, rettv, 0);
5573}
5574
5575/*
5576 * "glob()" function
5577 */
5578 static void
5579f_glob(typval_T *argvars, typval_T *rettv)
5580{
5581 int options = WILD_SILENT|WILD_USE_NL;
5582 expand_T xpc;
5583 int error = FALSE;
5584
5585 /* When the optional second argument is non-zero, don't remove matches
5586 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5587 rettv->v_type = VAR_STRING;
5588 if (argvars[1].v_type != VAR_UNKNOWN)
5589 {
5590 if (get_tv_number_chk(&argvars[1], &error))
5591 options |= WILD_KEEP_ALL;
5592 if (argvars[2].v_type != VAR_UNKNOWN)
5593 {
5594 if (get_tv_number_chk(&argvars[2], &error))
5595 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005596 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005597 }
5598 if (argvars[3].v_type != VAR_UNKNOWN
5599 && get_tv_number_chk(&argvars[3], &error))
5600 options |= WILD_ALLLINKS;
5601 }
5602 }
5603 if (!error)
5604 {
5605 ExpandInit(&xpc);
5606 xpc.xp_context = EXPAND_FILES;
5607 if (p_wic)
5608 options += WILD_ICASE;
5609 if (rettv->v_type == VAR_STRING)
5610 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5611 NULL, options, WILD_ALL);
5612 else if (rettv_list_alloc(rettv) != FAIL)
5613 {
5614 int i;
5615
5616 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5617 NULL, options, WILD_ALL_KEEP);
5618 for (i = 0; i < xpc.xp_numfiles; i++)
5619 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5620
5621 ExpandCleanup(&xpc);
5622 }
5623 }
5624 else
5625 rettv->vval.v_string = NULL;
5626}
5627
5628/*
5629 * "globpath()" function
5630 */
5631 static void
5632f_globpath(typval_T *argvars, typval_T *rettv)
5633{
5634 int flags = 0;
5635 char_u buf1[NUMBUFLEN];
5636 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5637 int error = FALSE;
5638 garray_T ga;
5639 int i;
5640
5641 /* When the optional second argument is non-zero, don't remove matches
5642 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5643 rettv->v_type = VAR_STRING;
5644 if (argvars[2].v_type != VAR_UNKNOWN)
5645 {
5646 if (get_tv_number_chk(&argvars[2], &error))
5647 flags |= WILD_KEEP_ALL;
5648 if (argvars[3].v_type != VAR_UNKNOWN)
5649 {
5650 if (get_tv_number_chk(&argvars[3], &error))
5651 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005652 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005653 }
5654 if (argvars[4].v_type != VAR_UNKNOWN
5655 && get_tv_number_chk(&argvars[4], &error))
5656 flags |= WILD_ALLLINKS;
5657 }
5658 }
5659 if (file != NULL && !error)
5660 {
5661 ga_init2(&ga, (int)sizeof(char_u *), 10);
5662 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5663 if (rettv->v_type == VAR_STRING)
5664 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5665 else if (rettv_list_alloc(rettv) != FAIL)
5666 for (i = 0; i < ga.ga_len; ++i)
5667 list_append_string(rettv->vval.v_list,
5668 ((char_u **)(ga.ga_data))[i], -1);
5669 ga_clear_strings(&ga);
5670 }
5671 else
5672 rettv->vval.v_string = NULL;
5673}
5674
5675/*
5676 * "glob2regpat()" function
5677 */
5678 static void
5679f_glob2regpat(typval_T *argvars, typval_T *rettv)
5680{
5681 char_u *pat = get_tv_string_chk(&argvars[0]);
5682
5683 rettv->v_type = VAR_STRING;
5684 rettv->vval.v_string = (pat == NULL)
5685 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5686}
5687
5688/* for VIM_VERSION_ defines */
5689#include "version.h"
5690
5691/*
5692 * "has()" function
5693 */
5694 static void
5695f_has(typval_T *argvars, typval_T *rettv)
5696{
5697 int i;
5698 char_u *name;
5699 int n = FALSE;
5700 static char *(has_list[]) =
5701 {
5702#ifdef AMIGA
5703 "amiga",
5704# ifdef FEAT_ARP
5705 "arp",
5706# endif
5707#endif
5708#ifdef __BEOS__
5709 "beos",
5710#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005711#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005712 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5713 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005714# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005715 "macunix", /* Mac OS X, with the darwin feature */
5716 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005717# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005718#endif
5719#ifdef __QNX__
5720 "qnx",
5721#endif
5722#ifdef UNIX
5723 "unix",
5724#endif
5725#ifdef VMS
5726 "vms",
5727#endif
5728#ifdef WIN32
5729 "win32",
5730#endif
5731#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5732 "win32unix",
5733#endif
5734#if defined(WIN64) || defined(_WIN64)
5735 "win64",
5736#endif
5737#ifdef EBCDIC
5738 "ebcdic",
5739#endif
5740#ifndef CASE_INSENSITIVE_FILENAME
5741 "fname_case",
5742#endif
5743#ifdef HAVE_ACL
5744 "acl",
5745#endif
5746#ifdef FEAT_ARABIC
5747 "arabic",
5748#endif
5749#ifdef FEAT_AUTOCMD
5750 "autocmd",
5751#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005752#ifdef FEAT_AUTOSERVERNAME
5753 "autoservername",
5754#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005755#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005756 "balloon_eval",
5757# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5758 "balloon_multiline",
5759# endif
5760#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005761#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005762 "balloon_eval_term",
5763#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005764#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5765 "builtin_terms",
5766# ifdef ALL_BUILTIN_TCAPS
5767 "all_builtin_terms",
5768# endif
5769#endif
5770#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5771 || defined(FEAT_GUI_W32) \
5772 || defined(FEAT_GUI_MOTIF))
5773 "browsefilter",
5774#endif
5775#ifdef FEAT_BYTEOFF
5776 "byte_offset",
5777#endif
5778#ifdef FEAT_JOB_CHANNEL
5779 "channel",
5780#endif
5781#ifdef FEAT_CINDENT
5782 "cindent",
5783#endif
5784#ifdef FEAT_CLIENTSERVER
5785 "clientserver",
5786#endif
5787#ifdef FEAT_CLIPBOARD
5788 "clipboard",
5789#endif
5790#ifdef FEAT_CMDL_COMPL
5791 "cmdline_compl",
5792#endif
5793#ifdef FEAT_CMDHIST
5794 "cmdline_hist",
5795#endif
5796#ifdef FEAT_COMMENTS
5797 "comments",
5798#endif
5799#ifdef FEAT_CONCEAL
5800 "conceal",
5801#endif
5802#ifdef FEAT_CRYPT
5803 "cryptv",
5804 "crypt-blowfish",
5805 "crypt-blowfish2",
5806#endif
5807#ifdef FEAT_CSCOPE
5808 "cscope",
5809#endif
5810#ifdef FEAT_CURSORBIND
5811 "cursorbind",
5812#endif
5813#ifdef CURSOR_SHAPE
5814 "cursorshape",
5815#endif
5816#ifdef DEBUG
5817 "debug",
5818#endif
5819#ifdef FEAT_CON_DIALOG
5820 "dialog_con",
5821#endif
5822#ifdef FEAT_GUI_DIALOG
5823 "dialog_gui",
5824#endif
5825#ifdef FEAT_DIFF
5826 "diff",
5827#endif
5828#ifdef FEAT_DIGRAPHS
5829 "digraphs",
5830#endif
5831#ifdef FEAT_DIRECTX
5832 "directx",
5833#endif
5834#ifdef FEAT_DND
5835 "dnd",
5836#endif
5837#ifdef FEAT_EMACS_TAGS
5838 "emacs_tags",
5839#endif
5840 "eval", /* always present, of course! */
5841 "ex_extra", /* graduated feature */
5842#ifdef FEAT_SEARCH_EXTRA
5843 "extra_search",
5844#endif
5845#ifdef FEAT_FKMAP
5846 "farsi",
5847#endif
5848#ifdef FEAT_SEARCHPATH
5849 "file_in_path",
5850#endif
5851#ifdef FEAT_FILTERPIPE
5852 "filterpipe",
5853#endif
5854#ifdef FEAT_FIND_ID
5855 "find_in_path",
5856#endif
5857#ifdef FEAT_FLOAT
5858 "float",
5859#endif
5860#ifdef FEAT_FOLDING
5861 "folding",
5862#endif
5863#ifdef FEAT_FOOTER
5864 "footer",
5865#endif
5866#if !defined(USE_SYSTEM) && defined(UNIX)
5867 "fork",
5868#endif
5869#ifdef FEAT_GETTEXT
5870 "gettext",
5871#endif
5872#ifdef FEAT_GUI
5873 "gui",
5874#endif
5875#ifdef FEAT_GUI_ATHENA
5876# ifdef FEAT_GUI_NEXTAW
5877 "gui_neXtaw",
5878# else
5879 "gui_athena",
5880# endif
5881#endif
5882#ifdef FEAT_GUI_GTK
5883 "gui_gtk",
5884# ifdef USE_GTK3
5885 "gui_gtk3",
5886# else
5887 "gui_gtk2",
5888# endif
5889#endif
5890#ifdef FEAT_GUI_GNOME
5891 "gui_gnome",
5892#endif
5893#ifdef FEAT_GUI_MAC
5894 "gui_mac",
5895#endif
5896#ifdef FEAT_GUI_MOTIF
5897 "gui_motif",
5898#endif
5899#ifdef FEAT_GUI_PHOTON
5900 "gui_photon",
5901#endif
5902#ifdef FEAT_GUI_W32
5903 "gui_win32",
5904#endif
5905#ifdef FEAT_HANGULIN
5906 "hangul_input",
5907#endif
5908#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5909 "iconv",
5910#endif
5911#ifdef FEAT_INS_EXPAND
5912 "insert_expand",
5913#endif
5914#ifdef FEAT_JOB_CHANNEL
5915 "job",
5916#endif
5917#ifdef FEAT_JUMPLIST
5918 "jumplist",
5919#endif
5920#ifdef FEAT_KEYMAP
5921 "keymap",
5922#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005923 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005924#ifdef FEAT_LANGMAP
5925 "langmap",
5926#endif
5927#ifdef FEAT_LIBCALL
5928 "libcall",
5929#endif
5930#ifdef FEAT_LINEBREAK
5931 "linebreak",
5932#endif
5933#ifdef FEAT_LISP
5934 "lispindent",
5935#endif
5936#ifdef FEAT_LISTCMDS
5937 "listcmds",
5938#endif
5939#ifdef FEAT_LOCALMAP
5940 "localmap",
5941#endif
5942#ifdef FEAT_LUA
5943# ifndef DYNAMIC_LUA
5944 "lua",
5945# endif
5946#endif
5947#ifdef FEAT_MENU
5948 "menu",
5949#endif
5950#ifdef FEAT_SESSION
5951 "mksession",
5952#endif
5953#ifdef FEAT_MODIFY_FNAME
5954 "modify_fname",
5955#endif
5956#ifdef FEAT_MOUSE
5957 "mouse",
5958#endif
5959#ifdef FEAT_MOUSESHAPE
5960 "mouseshape",
5961#endif
5962#if defined(UNIX) || defined(VMS)
5963# ifdef FEAT_MOUSE_DEC
5964 "mouse_dec",
5965# endif
5966# ifdef FEAT_MOUSE_GPM
5967 "mouse_gpm",
5968# endif
5969# ifdef FEAT_MOUSE_JSB
5970 "mouse_jsbterm",
5971# endif
5972# ifdef FEAT_MOUSE_NET
5973 "mouse_netterm",
5974# endif
5975# ifdef FEAT_MOUSE_PTERM
5976 "mouse_pterm",
5977# endif
5978# ifdef FEAT_MOUSE_SGR
5979 "mouse_sgr",
5980# endif
5981# ifdef FEAT_SYSMOUSE
5982 "mouse_sysmouse",
5983# endif
5984# ifdef FEAT_MOUSE_URXVT
5985 "mouse_urxvt",
5986# endif
5987# ifdef FEAT_MOUSE_XTERM
5988 "mouse_xterm",
5989# endif
5990#endif
5991#ifdef FEAT_MBYTE
5992 "multi_byte",
5993#endif
5994#ifdef FEAT_MBYTE_IME
5995 "multi_byte_ime",
5996#endif
5997#ifdef FEAT_MULTI_LANG
5998 "multi_lang",
5999#endif
6000#ifdef FEAT_MZSCHEME
6001#ifndef DYNAMIC_MZSCHEME
6002 "mzscheme",
6003#endif
6004#endif
6005#ifdef FEAT_NUM64
6006 "num64",
6007#endif
6008#ifdef FEAT_OLE
6009 "ole",
6010#endif
6011 "packages",
6012#ifdef FEAT_PATH_EXTRA
6013 "path_extra",
6014#endif
6015#ifdef FEAT_PERL
6016#ifndef DYNAMIC_PERL
6017 "perl",
6018#endif
6019#endif
6020#ifdef FEAT_PERSISTENT_UNDO
6021 "persistent_undo",
6022#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006023#if defined(FEAT_PYTHON)
6024 "python_compiled",
6025# if defined(DYNAMIC_PYTHON)
6026 "python_dynamic",
6027# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006028 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006029 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006030# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006031#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006032#if defined(FEAT_PYTHON3)
6033 "python3_compiled",
6034# if defined(DYNAMIC_PYTHON3)
6035 "python3_dynamic",
6036# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006037 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006038 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006039# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006040#endif
6041#ifdef FEAT_POSTSCRIPT
6042 "postscript",
6043#endif
6044#ifdef FEAT_PRINTER
6045 "printer",
6046#endif
6047#ifdef FEAT_PROFILE
6048 "profile",
6049#endif
6050#ifdef FEAT_RELTIME
6051 "reltime",
6052#endif
6053#ifdef FEAT_QUICKFIX
6054 "quickfix",
6055#endif
6056#ifdef FEAT_RIGHTLEFT
6057 "rightleft",
6058#endif
6059#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6060 "ruby",
6061#endif
6062#ifdef FEAT_SCROLLBIND
6063 "scrollbind",
6064#endif
6065#ifdef FEAT_CMDL_INFO
6066 "showcmd",
6067 "cmdline_info",
6068#endif
6069#ifdef FEAT_SIGNS
6070 "signs",
6071#endif
6072#ifdef FEAT_SMARTINDENT
6073 "smartindent",
6074#endif
6075#ifdef STARTUPTIME
6076 "startuptime",
6077#endif
6078#ifdef FEAT_STL_OPT
6079 "statusline",
6080#endif
6081#ifdef FEAT_SUN_WORKSHOP
6082 "sun_workshop",
6083#endif
6084#ifdef FEAT_NETBEANS_INTG
6085 "netbeans_intg",
6086#endif
6087#ifdef FEAT_SPELL
6088 "spell",
6089#endif
6090#ifdef FEAT_SYN_HL
6091 "syntax",
6092#endif
6093#if defined(USE_SYSTEM) || !defined(UNIX)
6094 "system",
6095#endif
6096#ifdef FEAT_TAG_BINS
6097 "tag_binary",
6098#endif
6099#ifdef FEAT_TAG_OLDSTATIC
6100 "tag_old_static",
6101#endif
6102#ifdef FEAT_TAG_ANYWHITE
6103 "tag_any_white",
6104#endif
6105#ifdef FEAT_TCL
6106# ifndef DYNAMIC_TCL
6107 "tcl",
6108# endif
6109#endif
6110#ifdef FEAT_TERMGUICOLORS
6111 "termguicolors",
6112#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006113#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006114 "terminal",
6115#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006116#ifdef TERMINFO
6117 "terminfo",
6118#endif
6119#ifdef FEAT_TERMRESPONSE
6120 "termresponse",
6121#endif
6122#ifdef FEAT_TEXTOBJ
6123 "textobjects",
6124#endif
6125#ifdef HAVE_TGETENT
6126 "tgetent",
6127#endif
6128#ifdef FEAT_TIMERS
6129 "timers",
6130#endif
6131#ifdef FEAT_TITLE
6132 "title",
6133#endif
6134#ifdef FEAT_TOOLBAR
6135 "toolbar",
6136#endif
6137#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6138 "unnamedplus",
6139#endif
6140#ifdef FEAT_USR_CMDS
6141 "user-commands", /* was accidentally included in 5.4 */
6142 "user_commands",
6143#endif
6144#ifdef FEAT_VIMINFO
6145 "viminfo",
6146#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006147 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006148#ifdef FEAT_VIRTUALEDIT
6149 "virtualedit",
6150#endif
6151 "visual",
6152#ifdef FEAT_VISUALEXTRA
6153 "visualextra",
6154#endif
6155#ifdef FEAT_VREPLACE
6156 "vreplace",
6157#endif
6158#ifdef FEAT_WILDIGN
6159 "wildignore",
6160#endif
6161#ifdef FEAT_WILDMENU
6162 "wildmenu",
6163#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006164 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006165#ifdef FEAT_WAK
6166 "winaltkeys",
6167#endif
6168#ifdef FEAT_WRITEBACKUP
6169 "writebackup",
6170#endif
6171#ifdef FEAT_XIM
6172 "xim",
6173#endif
6174#ifdef FEAT_XFONTSET
6175 "xfontset",
6176#endif
6177#ifdef FEAT_XPM_W32
6178 "xpm",
6179 "xpm_w32", /* for backward compatibility */
6180#else
6181# if defined(HAVE_XPM)
6182 "xpm",
6183# endif
6184#endif
6185#ifdef USE_XSMP
6186 "xsmp",
6187#endif
6188#ifdef USE_XSMP_INTERACT
6189 "xsmp_interact",
6190#endif
6191#ifdef FEAT_XCLIPBOARD
6192 "xterm_clipboard",
6193#endif
6194#ifdef FEAT_XTERM_SAVE
6195 "xterm_save",
6196#endif
6197#if defined(UNIX) && defined(FEAT_X11)
6198 "X11",
6199#endif
6200 NULL
6201 };
6202
6203 name = get_tv_string(&argvars[0]);
6204 for (i = 0; has_list[i] != NULL; ++i)
6205 if (STRICMP(name, has_list[i]) == 0)
6206 {
6207 n = TRUE;
6208 break;
6209 }
6210
6211 if (n == FALSE)
6212 {
6213 if (STRNICMP(name, "patch", 5) == 0)
6214 {
6215 if (name[5] == '-'
6216 && STRLEN(name) >= 11
6217 && vim_isdigit(name[6])
6218 && vim_isdigit(name[8])
6219 && vim_isdigit(name[10]))
6220 {
6221 int major = atoi((char *)name + 6);
6222 int minor = atoi((char *)name + 8);
6223
6224 /* Expect "patch-9.9.01234". */
6225 n = (major < VIM_VERSION_MAJOR
6226 || (major == VIM_VERSION_MAJOR
6227 && (minor < VIM_VERSION_MINOR
6228 || (minor == VIM_VERSION_MINOR
6229 && has_patch(atoi((char *)name + 10))))));
6230 }
6231 else
6232 n = has_patch(atoi((char *)name + 5));
6233 }
6234 else if (STRICMP(name, "vim_starting") == 0)
6235 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006236 else if (STRICMP(name, "ttyin") == 0)
6237 n = mch_input_isatty();
6238 else if (STRICMP(name, "ttyout") == 0)
6239 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006240#ifdef FEAT_MBYTE
6241 else if (STRICMP(name, "multi_byte_encoding") == 0)
6242 n = has_mbyte;
6243#endif
6244#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6245 else if (STRICMP(name, "balloon_multiline") == 0)
6246 n = multiline_balloon_available();
6247#endif
6248#ifdef DYNAMIC_TCL
6249 else if (STRICMP(name, "tcl") == 0)
6250 n = tcl_enabled(FALSE);
6251#endif
6252#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6253 else if (STRICMP(name, "iconv") == 0)
6254 n = iconv_enabled(FALSE);
6255#endif
6256#ifdef DYNAMIC_LUA
6257 else if (STRICMP(name, "lua") == 0)
6258 n = lua_enabled(FALSE);
6259#endif
6260#ifdef DYNAMIC_MZSCHEME
6261 else if (STRICMP(name, "mzscheme") == 0)
6262 n = mzscheme_enabled(FALSE);
6263#endif
6264#ifdef DYNAMIC_RUBY
6265 else if (STRICMP(name, "ruby") == 0)
6266 n = ruby_enabled(FALSE);
6267#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006268#ifdef DYNAMIC_PYTHON
6269 else if (STRICMP(name, "python") == 0)
6270 n = python_enabled(FALSE);
6271#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006272#ifdef DYNAMIC_PYTHON3
6273 else if (STRICMP(name, "python3") == 0)
6274 n = python3_enabled(FALSE);
6275#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006276#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6277 else if (STRICMP(name, "pythonx") == 0)
6278 {
6279# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6280 if (p_pyx == 0)
6281 n = python3_enabled(FALSE) || python_enabled(FALSE);
6282 else if (p_pyx == 3)
6283 n = python3_enabled(FALSE);
6284 else if (p_pyx == 2)
6285 n = python_enabled(FALSE);
6286# elif defined(DYNAMIC_PYTHON)
6287 n = python_enabled(FALSE);
6288# elif defined(DYNAMIC_PYTHON3)
6289 n = python3_enabled(FALSE);
6290# endif
6291 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006292#endif
6293#ifdef DYNAMIC_PERL
6294 else if (STRICMP(name, "perl") == 0)
6295 n = perl_enabled(FALSE);
6296#endif
6297#ifdef FEAT_GUI
6298 else if (STRICMP(name, "gui_running") == 0)
6299 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006300# ifdef FEAT_BROWSE
6301 else if (STRICMP(name, "browse") == 0)
6302 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6303# endif
6304#endif
6305#ifdef FEAT_SYN_HL
6306 else if (STRICMP(name, "syntax_items") == 0)
6307 n = syntax_present(curwin);
6308#endif
6309#if defined(WIN3264)
6310 else if (STRICMP(name, "win95") == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006311 n = FALSE; /* Win9x is no more supported. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006312#endif
6313#ifdef FEAT_NETBEANS_INTG
6314 else if (STRICMP(name, "netbeans_enabled") == 0)
6315 n = netbeans_active();
6316#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006317#if defined(FEAT_TERMINAL) && defined(WIN3264)
6318 else if (STRICMP(name, "terminal") == 0)
6319 n = terminal_enabled();
6320#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006321 }
6322
6323 rettv->vval.v_number = n;
6324}
6325
6326/*
6327 * "has_key()" function
6328 */
6329 static void
6330f_has_key(typval_T *argvars, typval_T *rettv)
6331{
6332 if (argvars[0].v_type != VAR_DICT)
6333 {
6334 EMSG(_(e_dictreq));
6335 return;
6336 }
6337 if (argvars[0].vval.v_dict == NULL)
6338 return;
6339
6340 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6341 get_tv_string(&argvars[1]), -1) != NULL;
6342}
6343
6344/*
6345 * "haslocaldir()" function
6346 */
6347 static void
6348f_haslocaldir(typval_T *argvars, typval_T *rettv)
6349{
6350 win_T *wp = NULL;
6351
6352 wp = find_tabwin(&argvars[0], &argvars[1]);
6353 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6354}
6355
6356/*
6357 * "hasmapto()" function
6358 */
6359 static void
6360f_hasmapto(typval_T *argvars, typval_T *rettv)
6361{
6362 char_u *name;
6363 char_u *mode;
6364 char_u buf[NUMBUFLEN];
6365 int abbr = FALSE;
6366
6367 name = get_tv_string(&argvars[0]);
6368 if (argvars[1].v_type == VAR_UNKNOWN)
6369 mode = (char_u *)"nvo";
6370 else
6371 {
6372 mode = get_tv_string_buf(&argvars[1], buf);
6373 if (argvars[2].v_type != VAR_UNKNOWN)
6374 abbr = (int)get_tv_number(&argvars[2]);
6375 }
6376
6377 if (map_to_exists(name, mode, abbr))
6378 rettv->vval.v_number = TRUE;
6379 else
6380 rettv->vval.v_number = FALSE;
6381}
6382
6383/*
6384 * "histadd()" function
6385 */
6386 static void
6387f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6388{
6389#ifdef FEAT_CMDHIST
6390 int histype;
6391 char_u *str;
6392 char_u buf[NUMBUFLEN];
6393#endif
6394
6395 rettv->vval.v_number = FALSE;
6396 if (check_restricted() || check_secure())
6397 return;
6398#ifdef FEAT_CMDHIST
6399 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6400 histype = str != NULL ? get_histtype(str) : -1;
6401 if (histype >= 0)
6402 {
6403 str = get_tv_string_buf(&argvars[1], buf);
6404 if (*str != NUL)
6405 {
6406 init_history();
6407 add_to_history(histype, str, FALSE, NUL);
6408 rettv->vval.v_number = TRUE;
6409 return;
6410 }
6411 }
6412#endif
6413}
6414
6415/*
6416 * "histdel()" function
6417 */
6418 static void
6419f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6420{
6421#ifdef FEAT_CMDHIST
6422 int n;
6423 char_u buf[NUMBUFLEN];
6424 char_u *str;
6425
6426 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6427 if (str == NULL)
6428 n = 0;
6429 else if (argvars[1].v_type == VAR_UNKNOWN)
6430 /* only one argument: clear entire history */
6431 n = clr_history(get_histtype(str));
6432 else if (argvars[1].v_type == VAR_NUMBER)
6433 /* index given: remove that entry */
6434 n = del_history_idx(get_histtype(str),
6435 (int)get_tv_number(&argvars[1]));
6436 else
6437 /* string given: remove all matching entries */
6438 n = del_history_entry(get_histtype(str),
6439 get_tv_string_buf(&argvars[1], buf));
6440 rettv->vval.v_number = n;
6441#endif
6442}
6443
6444/*
6445 * "histget()" function
6446 */
6447 static void
6448f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6449{
6450#ifdef FEAT_CMDHIST
6451 int type;
6452 int idx;
6453 char_u *str;
6454
6455 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6456 if (str == NULL)
6457 rettv->vval.v_string = NULL;
6458 else
6459 {
6460 type = get_histtype(str);
6461 if (argvars[1].v_type == VAR_UNKNOWN)
6462 idx = get_history_idx(type);
6463 else
6464 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6465 /* -1 on type error */
6466 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6467 }
6468#else
6469 rettv->vval.v_string = NULL;
6470#endif
6471 rettv->v_type = VAR_STRING;
6472}
6473
6474/*
6475 * "histnr()" function
6476 */
6477 static void
6478f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6479{
6480 int i;
6481
6482#ifdef FEAT_CMDHIST
6483 char_u *history = get_tv_string_chk(&argvars[0]);
6484
6485 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6486 if (i >= HIST_CMD && i < HIST_COUNT)
6487 i = get_history_idx(i);
6488 else
6489#endif
6490 i = -1;
6491 rettv->vval.v_number = i;
6492}
6493
6494/*
6495 * "highlightID(name)" function
6496 */
6497 static void
6498f_hlID(typval_T *argvars, typval_T *rettv)
6499{
6500 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6501}
6502
6503/*
6504 * "highlight_exists()" function
6505 */
6506 static void
6507f_hlexists(typval_T *argvars, typval_T *rettv)
6508{
6509 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6510}
6511
6512/*
6513 * "hostname()" function
6514 */
6515 static void
6516f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6517{
6518 char_u hostname[256];
6519
6520 mch_get_host_name(hostname, 256);
6521 rettv->v_type = VAR_STRING;
6522 rettv->vval.v_string = vim_strsave(hostname);
6523}
6524
6525/*
6526 * iconv() function
6527 */
6528 static void
6529f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6530{
6531#ifdef FEAT_MBYTE
6532 char_u buf1[NUMBUFLEN];
6533 char_u buf2[NUMBUFLEN];
6534 char_u *from, *to, *str;
6535 vimconv_T vimconv;
6536#endif
6537
6538 rettv->v_type = VAR_STRING;
6539 rettv->vval.v_string = NULL;
6540
6541#ifdef FEAT_MBYTE
6542 str = get_tv_string(&argvars[0]);
6543 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6544 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6545 vimconv.vc_type = CONV_NONE;
6546 convert_setup(&vimconv, from, to);
6547
6548 /* If the encodings are equal, no conversion needed. */
6549 if (vimconv.vc_type == CONV_NONE)
6550 rettv->vval.v_string = vim_strsave(str);
6551 else
6552 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6553
6554 convert_setup(&vimconv, NULL, NULL);
6555 vim_free(from);
6556 vim_free(to);
6557#endif
6558}
6559
6560/*
6561 * "indent()" function
6562 */
6563 static void
6564f_indent(typval_T *argvars, typval_T *rettv)
6565{
6566 linenr_T lnum;
6567
6568 lnum = get_tv_lnum(argvars);
6569 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6570 rettv->vval.v_number = get_indent_lnum(lnum);
6571 else
6572 rettv->vval.v_number = -1;
6573}
6574
6575/*
6576 * "index()" function
6577 */
6578 static void
6579f_index(typval_T *argvars, typval_T *rettv)
6580{
6581 list_T *l;
6582 listitem_T *item;
6583 long idx = 0;
6584 int ic = FALSE;
6585
6586 rettv->vval.v_number = -1;
6587 if (argvars[0].v_type != VAR_LIST)
6588 {
6589 EMSG(_(e_listreq));
6590 return;
6591 }
6592 l = argvars[0].vval.v_list;
6593 if (l != NULL)
6594 {
6595 item = l->lv_first;
6596 if (argvars[2].v_type != VAR_UNKNOWN)
6597 {
6598 int error = FALSE;
6599
6600 /* Start at specified item. Use the cached index that list_find()
6601 * sets, so that a negative number also works. */
6602 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6603 idx = l->lv_idx;
6604 if (argvars[3].v_type != VAR_UNKNOWN)
6605 ic = (int)get_tv_number_chk(&argvars[3], &error);
6606 if (error)
6607 item = NULL;
6608 }
6609
6610 for ( ; item != NULL; item = item->li_next, ++idx)
6611 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6612 {
6613 rettv->vval.v_number = idx;
6614 break;
6615 }
6616 }
6617}
6618
6619static int inputsecret_flag = 0;
6620
6621/*
6622 * "input()" function
6623 * Also handles inputsecret() when inputsecret is set.
6624 */
6625 static void
6626f_input(typval_T *argvars, typval_T *rettv)
6627{
6628 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6629}
6630
6631/*
6632 * "inputdialog()" function
6633 */
6634 static void
6635f_inputdialog(typval_T *argvars, typval_T *rettv)
6636{
6637#if defined(FEAT_GUI_TEXTDIALOG)
6638 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6639 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6640 {
6641 char_u *message;
6642 char_u buf[NUMBUFLEN];
6643 char_u *defstr = (char_u *)"";
6644
6645 message = get_tv_string_chk(&argvars[0]);
6646 if (argvars[1].v_type != VAR_UNKNOWN
6647 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6648 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6649 else
6650 IObuff[0] = NUL;
6651 if (message != NULL && defstr != NULL
6652 && do_dialog(VIM_QUESTION, NULL, message,
6653 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6654 rettv->vval.v_string = vim_strsave(IObuff);
6655 else
6656 {
6657 if (message != NULL && defstr != NULL
6658 && argvars[1].v_type != VAR_UNKNOWN
6659 && argvars[2].v_type != VAR_UNKNOWN)
6660 rettv->vval.v_string = vim_strsave(
6661 get_tv_string_buf(&argvars[2], buf));
6662 else
6663 rettv->vval.v_string = NULL;
6664 }
6665 rettv->v_type = VAR_STRING;
6666 }
6667 else
6668#endif
6669 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6670}
6671
6672/*
6673 * "inputlist()" function
6674 */
6675 static void
6676f_inputlist(typval_T *argvars, typval_T *rettv)
6677{
6678 listitem_T *li;
6679 int selected;
6680 int mouse_used;
6681
6682#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006683 /* While starting up, there is no place to enter text. When running tests
6684 * with --not-a-term we assume feedkeys() will be used. */
6685 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006686 return;
6687#endif
6688 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6689 {
6690 EMSG2(_(e_listarg), "inputlist()");
6691 return;
6692 }
6693
6694 msg_start();
6695 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6696 lines_left = Rows; /* avoid more prompt */
6697 msg_scroll = TRUE;
6698 msg_clr_eos();
6699
6700 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6701 {
6702 msg_puts(get_tv_string(&li->li_tv));
6703 msg_putchar('\n');
6704 }
6705
6706 /* Ask for choice. */
6707 selected = prompt_for_number(&mouse_used);
6708 if (mouse_used)
6709 selected -= lines_left;
6710
6711 rettv->vval.v_number = selected;
6712}
6713
6714
6715static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6716
6717/*
6718 * "inputrestore()" function
6719 */
6720 static void
6721f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6722{
6723 if (ga_userinput.ga_len > 0)
6724 {
6725 --ga_userinput.ga_len;
6726 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6727 + ga_userinput.ga_len);
6728 /* default return is zero == OK */
6729 }
6730 else if (p_verbose > 1)
6731 {
6732 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6733 rettv->vval.v_number = 1; /* Failed */
6734 }
6735}
6736
6737/*
6738 * "inputsave()" function
6739 */
6740 static void
6741f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6742{
6743 /* Add an entry to the stack of typeahead storage. */
6744 if (ga_grow(&ga_userinput, 1) == OK)
6745 {
6746 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6747 + ga_userinput.ga_len);
6748 ++ga_userinput.ga_len;
6749 /* default return is zero == OK */
6750 }
6751 else
6752 rettv->vval.v_number = 1; /* Failed */
6753}
6754
6755/*
6756 * "inputsecret()" function
6757 */
6758 static void
6759f_inputsecret(typval_T *argvars, typval_T *rettv)
6760{
6761 ++cmdline_star;
6762 ++inputsecret_flag;
6763 f_input(argvars, rettv);
6764 --cmdline_star;
6765 --inputsecret_flag;
6766}
6767
6768/*
6769 * "insert()" function
6770 */
6771 static void
6772f_insert(typval_T *argvars, typval_T *rettv)
6773{
6774 long before = 0;
6775 listitem_T *item;
6776 list_T *l;
6777 int error = FALSE;
6778
6779 if (argvars[0].v_type != VAR_LIST)
6780 EMSG2(_(e_listarg), "insert()");
6781 else if ((l = argvars[0].vval.v_list) != NULL
6782 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6783 {
6784 if (argvars[2].v_type != VAR_UNKNOWN)
6785 before = (long)get_tv_number_chk(&argvars[2], &error);
6786 if (error)
6787 return; /* type error; errmsg already given */
6788
6789 if (before == l->lv_len)
6790 item = NULL;
6791 else
6792 {
6793 item = list_find(l, before);
6794 if (item == NULL)
6795 {
6796 EMSGN(_(e_listidx), before);
6797 l = NULL;
6798 }
6799 }
6800 if (l != NULL)
6801 {
6802 list_insert_tv(l, &argvars[1], item);
6803 copy_tv(&argvars[0], rettv);
6804 }
6805 }
6806}
6807
6808/*
6809 * "invert(expr)" function
6810 */
6811 static void
6812f_invert(typval_T *argvars, typval_T *rettv)
6813{
6814 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6815}
6816
6817/*
6818 * "isdirectory()" function
6819 */
6820 static void
6821f_isdirectory(typval_T *argvars, typval_T *rettv)
6822{
6823 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6824}
6825
6826/*
6827 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6828 * or it refers to a List or Dictionary that is locked.
6829 */
6830 static int
6831tv_islocked(typval_T *tv)
6832{
6833 return (tv->v_lock & VAR_LOCKED)
6834 || (tv->v_type == VAR_LIST
6835 && tv->vval.v_list != NULL
6836 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6837 || (tv->v_type == VAR_DICT
6838 && tv->vval.v_dict != NULL
6839 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6840}
6841
6842/*
6843 * "islocked()" function
6844 */
6845 static void
6846f_islocked(typval_T *argvars, typval_T *rettv)
6847{
6848 lval_T lv;
6849 char_u *end;
6850 dictitem_T *di;
6851
6852 rettv->vval.v_number = -1;
6853 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006854 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006855 if (end != NULL && lv.ll_name != NULL)
6856 {
6857 if (*end != NUL)
6858 EMSG(_(e_trailing));
6859 else
6860 {
6861 if (lv.ll_tv == NULL)
6862 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006863 di = find_var(lv.ll_name, NULL, TRUE);
6864 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006865 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006866 /* Consider a variable locked when:
6867 * 1. the variable itself is locked
6868 * 2. the value of the variable is locked.
6869 * 3. the List or Dict value is locked.
6870 */
6871 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6872 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006873 }
6874 }
6875 else if (lv.ll_range)
6876 EMSG(_("E786: Range not allowed"));
6877 else if (lv.ll_newkey != NULL)
6878 EMSG2(_(e_dictkey), lv.ll_newkey);
6879 else if (lv.ll_list != NULL)
6880 /* List item. */
6881 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6882 else
6883 /* Dictionary item. */
6884 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6885 }
6886 }
6887
6888 clear_lval(&lv);
6889}
6890
6891#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6892/*
6893 * "isnan()" function
6894 */
6895 static void
6896f_isnan(typval_T *argvars, typval_T *rettv)
6897{
6898 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6899 && isnan(argvars[0].vval.v_float);
6900}
6901#endif
6902
6903/*
6904 * "items(dict)" function
6905 */
6906 static void
6907f_items(typval_T *argvars, typval_T *rettv)
6908{
6909 dict_list(argvars, rettv, 2);
6910}
6911
6912#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6913/*
6914 * Get the job from the argument.
6915 * Returns NULL if the job is invalid.
6916 */
6917 static job_T *
6918get_job_arg(typval_T *tv)
6919{
6920 job_T *job;
6921
6922 if (tv->v_type != VAR_JOB)
6923 {
6924 EMSG2(_(e_invarg2), get_tv_string(tv));
6925 return NULL;
6926 }
6927 job = tv->vval.v_job;
6928
6929 if (job == NULL)
6930 EMSG(_("E916: not a valid job"));
6931 return job;
6932}
6933
6934/*
6935 * "job_getchannel()" function
6936 */
6937 static void
6938f_job_getchannel(typval_T *argvars, typval_T *rettv)
6939{
6940 job_T *job = get_job_arg(&argvars[0]);
6941
6942 if (job != NULL)
6943 {
6944 rettv->v_type = VAR_CHANNEL;
6945 rettv->vval.v_channel = job->jv_channel;
6946 if (job->jv_channel != NULL)
6947 ++job->jv_channel->ch_refcount;
6948 }
6949}
6950
6951/*
6952 * "job_info()" function
6953 */
6954 static void
6955f_job_info(typval_T *argvars, typval_T *rettv)
6956{
6957 job_T *job = get_job_arg(&argvars[0]);
6958
6959 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6960 job_info(job, rettv->vval.v_dict);
6961}
6962
6963/*
6964 * "job_setoptions()" function
6965 */
6966 static void
6967f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6968{
6969 job_T *job = get_job_arg(&argvars[0]);
6970 jobopt_T opt;
6971
6972 if (job == NULL)
6973 return;
6974 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02006975 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006976 job_set_options(job, &opt);
6977 free_job_options(&opt);
6978}
6979
6980/*
6981 * "job_start()" function
6982 */
6983 static void
6984f_job_start(typval_T *argvars, typval_T *rettv)
6985{
6986 rettv->v_type = VAR_JOB;
6987 if (check_restricted() || check_secure())
6988 return;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006989 rettv->vval.v_job = job_start(argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006990}
6991
6992/*
6993 * "job_status()" function
6994 */
6995 static void
6996f_job_status(typval_T *argvars, typval_T *rettv)
6997{
6998 job_T *job = get_job_arg(&argvars[0]);
6999
7000 if (job != NULL)
7001 {
7002 rettv->v_type = VAR_STRING;
7003 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7004 }
7005}
7006
7007/*
7008 * "job_stop()" function
7009 */
7010 static void
7011f_job_stop(typval_T *argvars, typval_T *rettv)
7012{
7013 job_T *job = get_job_arg(&argvars[0]);
7014
7015 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007016 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007017}
7018#endif
7019
7020/*
7021 * "join()" function
7022 */
7023 static void
7024f_join(typval_T *argvars, typval_T *rettv)
7025{
7026 garray_T ga;
7027 char_u *sep;
7028
7029 if (argvars[0].v_type != VAR_LIST)
7030 {
7031 EMSG(_(e_listreq));
7032 return;
7033 }
7034 if (argvars[0].vval.v_list == NULL)
7035 return;
7036 if (argvars[1].v_type == VAR_UNKNOWN)
7037 sep = (char_u *)" ";
7038 else
7039 sep = get_tv_string_chk(&argvars[1]);
7040
7041 rettv->v_type = VAR_STRING;
7042
7043 if (sep != NULL)
7044 {
7045 ga_init2(&ga, (int)sizeof(char), 80);
7046 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7047 ga_append(&ga, NUL);
7048 rettv->vval.v_string = (char_u *)ga.ga_data;
7049 }
7050 else
7051 rettv->vval.v_string = NULL;
7052}
7053
7054/*
7055 * "js_decode()" function
7056 */
7057 static void
7058f_js_decode(typval_T *argvars, typval_T *rettv)
7059{
7060 js_read_T reader;
7061
7062 reader.js_buf = get_tv_string(&argvars[0]);
7063 reader.js_fill = NULL;
7064 reader.js_used = 0;
7065 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7066 EMSG(_(e_invarg));
7067}
7068
7069/*
7070 * "js_encode()" function
7071 */
7072 static void
7073f_js_encode(typval_T *argvars, typval_T *rettv)
7074{
7075 rettv->v_type = VAR_STRING;
7076 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7077}
7078
7079/*
7080 * "json_decode()" function
7081 */
7082 static void
7083f_json_decode(typval_T *argvars, typval_T *rettv)
7084{
7085 js_read_T reader;
7086
7087 reader.js_buf = get_tv_string(&argvars[0]);
7088 reader.js_fill = NULL;
7089 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007090 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007091}
7092
7093/*
7094 * "json_encode()" function
7095 */
7096 static void
7097f_json_encode(typval_T *argvars, typval_T *rettv)
7098{
7099 rettv->v_type = VAR_STRING;
7100 rettv->vval.v_string = json_encode(&argvars[0], 0);
7101}
7102
7103/*
7104 * "keys()" function
7105 */
7106 static void
7107f_keys(typval_T *argvars, typval_T *rettv)
7108{
7109 dict_list(argvars, rettv, 0);
7110}
7111
7112/*
7113 * "last_buffer_nr()" function.
7114 */
7115 static void
7116f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7117{
7118 int n = 0;
7119 buf_T *buf;
7120
Bram Moolenaar29323592016-07-24 22:04:11 +02007121 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007122 if (n < buf->b_fnum)
7123 n = buf->b_fnum;
7124
7125 rettv->vval.v_number = n;
7126}
7127
7128/*
7129 * "len()" function
7130 */
7131 static void
7132f_len(typval_T *argvars, typval_T *rettv)
7133{
7134 switch (argvars[0].v_type)
7135 {
7136 case VAR_STRING:
7137 case VAR_NUMBER:
7138 rettv->vval.v_number = (varnumber_T)STRLEN(
7139 get_tv_string(&argvars[0]));
7140 break;
7141 case VAR_LIST:
7142 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7143 break;
7144 case VAR_DICT:
7145 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7146 break;
7147 case VAR_UNKNOWN:
7148 case VAR_SPECIAL:
7149 case VAR_FLOAT:
7150 case VAR_FUNC:
7151 case VAR_PARTIAL:
7152 case VAR_JOB:
7153 case VAR_CHANNEL:
7154 EMSG(_("E701: Invalid type for len()"));
7155 break;
7156 }
7157}
7158
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007159 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007160libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007161{
7162#ifdef FEAT_LIBCALL
7163 char_u *string_in;
7164 char_u **string_result;
7165 int nr_result;
7166#endif
7167
7168 rettv->v_type = type;
7169 if (type != VAR_NUMBER)
7170 rettv->vval.v_string = NULL;
7171
7172 if (check_restricted() || check_secure())
7173 return;
7174
7175#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007176 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007177 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7178 {
7179 string_in = NULL;
7180 if (argvars[2].v_type == VAR_STRING)
7181 string_in = argvars[2].vval.v_string;
7182 if (type == VAR_NUMBER)
7183 string_result = NULL;
7184 else
7185 string_result = &rettv->vval.v_string;
7186 if (mch_libcall(argvars[0].vval.v_string,
7187 argvars[1].vval.v_string,
7188 string_in,
7189 argvars[2].vval.v_number,
7190 string_result,
7191 &nr_result) == OK
7192 && type == VAR_NUMBER)
7193 rettv->vval.v_number = nr_result;
7194 }
7195#endif
7196}
7197
7198/*
7199 * "libcall()" function
7200 */
7201 static void
7202f_libcall(typval_T *argvars, typval_T *rettv)
7203{
7204 libcall_common(argvars, rettv, VAR_STRING);
7205}
7206
7207/*
7208 * "libcallnr()" function
7209 */
7210 static void
7211f_libcallnr(typval_T *argvars, typval_T *rettv)
7212{
7213 libcall_common(argvars, rettv, VAR_NUMBER);
7214}
7215
7216/*
7217 * "line(string)" function
7218 */
7219 static void
7220f_line(typval_T *argvars, typval_T *rettv)
7221{
7222 linenr_T lnum = 0;
7223 pos_T *fp;
7224 int fnum;
7225
7226 fp = var2fpos(&argvars[0], TRUE, &fnum);
7227 if (fp != NULL)
7228 lnum = fp->lnum;
7229 rettv->vval.v_number = lnum;
7230}
7231
7232/*
7233 * "line2byte(lnum)" function
7234 */
7235 static void
7236f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7237{
7238#ifndef FEAT_BYTEOFF
7239 rettv->vval.v_number = -1;
7240#else
7241 linenr_T lnum;
7242
7243 lnum = get_tv_lnum(argvars);
7244 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7245 rettv->vval.v_number = -1;
7246 else
7247 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7248 if (rettv->vval.v_number >= 0)
7249 ++rettv->vval.v_number;
7250#endif
7251}
7252
7253/*
7254 * "lispindent(lnum)" function
7255 */
7256 static void
7257f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7258{
7259#ifdef FEAT_LISP
7260 pos_T pos;
7261 linenr_T lnum;
7262
7263 pos = curwin->w_cursor;
7264 lnum = get_tv_lnum(argvars);
7265 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7266 {
7267 curwin->w_cursor.lnum = lnum;
7268 rettv->vval.v_number = get_lisp_indent();
7269 curwin->w_cursor = pos;
7270 }
7271 else
7272#endif
7273 rettv->vval.v_number = -1;
7274}
7275
7276/*
7277 * "localtime()" function
7278 */
7279 static void
7280f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7281{
7282 rettv->vval.v_number = (varnumber_T)time(NULL);
7283}
7284
7285static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7286
7287 static void
7288get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7289{
7290 char_u *keys;
7291 char_u *which;
7292 char_u buf[NUMBUFLEN];
7293 char_u *keys_buf = NULL;
7294 char_u *rhs;
7295 int mode;
7296 int abbr = FALSE;
7297 int get_dict = FALSE;
7298 mapblock_T *mp;
7299 int buffer_local;
7300
7301 /* return empty string for failure */
7302 rettv->v_type = VAR_STRING;
7303 rettv->vval.v_string = NULL;
7304
7305 keys = get_tv_string(&argvars[0]);
7306 if (*keys == NUL)
7307 return;
7308
7309 if (argvars[1].v_type != VAR_UNKNOWN)
7310 {
7311 which = get_tv_string_buf_chk(&argvars[1], buf);
7312 if (argvars[2].v_type != VAR_UNKNOWN)
7313 {
7314 abbr = (int)get_tv_number(&argvars[2]);
7315 if (argvars[3].v_type != VAR_UNKNOWN)
7316 get_dict = (int)get_tv_number(&argvars[3]);
7317 }
7318 }
7319 else
7320 which = (char_u *)"";
7321 if (which == NULL)
7322 return;
7323
7324 mode = get_map_mode(&which, 0);
7325
7326 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7327 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7328 vim_free(keys_buf);
7329
7330 if (!get_dict)
7331 {
7332 /* Return a string. */
7333 if (rhs != NULL)
7334 rettv->vval.v_string = str2special_save(rhs, FALSE);
7335
7336 }
7337 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7338 {
7339 /* Return a dictionary. */
7340 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7341 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7342 dict_T *dict = rettv->vval.v_dict;
7343
7344 dict_add_nr_str(dict, "lhs", 0L, lhs);
7345 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7346 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7347 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7348 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7349 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7350 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7351 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7352 dict_add_nr_str(dict, "mode", 0L, mapmode);
7353
7354 vim_free(lhs);
7355 vim_free(mapmode);
7356 }
7357}
7358
7359#ifdef FEAT_FLOAT
7360/*
7361 * "log()" function
7362 */
7363 static void
7364f_log(typval_T *argvars, typval_T *rettv)
7365{
7366 float_T f = 0.0;
7367
7368 rettv->v_type = VAR_FLOAT;
7369 if (get_float_arg(argvars, &f) == OK)
7370 rettv->vval.v_float = log(f);
7371 else
7372 rettv->vval.v_float = 0.0;
7373}
7374
7375/*
7376 * "log10()" function
7377 */
7378 static void
7379f_log10(typval_T *argvars, typval_T *rettv)
7380{
7381 float_T f = 0.0;
7382
7383 rettv->v_type = VAR_FLOAT;
7384 if (get_float_arg(argvars, &f) == OK)
7385 rettv->vval.v_float = log10(f);
7386 else
7387 rettv->vval.v_float = 0.0;
7388}
7389#endif
7390
7391#ifdef FEAT_LUA
7392/*
7393 * "luaeval()" function
7394 */
7395 static void
7396f_luaeval(typval_T *argvars, typval_T *rettv)
7397{
7398 char_u *str;
7399 char_u buf[NUMBUFLEN];
7400
7401 str = get_tv_string_buf(&argvars[0], buf);
7402 do_luaeval(str, argvars + 1, rettv);
7403}
7404#endif
7405
7406/*
7407 * "map()" function
7408 */
7409 static void
7410f_map(typval_T *argvars, typval_T *rettv)
7411{
7412 filter_map(argvars, rettv, TRUE);
7413}
7414
7415/*
7416 * "maparg()" function
7417 */
7418 static void
7419f_maparg(typval_T *argvars, typval_T *rettv)
7420{
7421 get_maparg(argvars, rettv, TRUE);
7422}
7423
7424/*
7425 * "mapcheck()" function
7426 */
7427 static void
7428f_mapcheck(typval_T *argvars, typval_T *rettv)
7429{
7430 get_maparg(argvars, rettv, FALSE);
7431}
7432
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007433typedef enum
7434{
7435 MATCH_END, /* matchend() */
7436 MATCH_MATCH, /* match() */
7437 MATCH_STR, /* matchstr() */
7438 MATCH_LIST, /* matchlist() */
7439 MATCH_POS /* matchstrpos() */
7440} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007441
7442 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007443find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007444{
7445 char_u *str = NULL;
7446 long len = 0;
7447 char_u *expr = NULL;
7448 char_u *pat;
7449 regmatch_T regmatch;
7450 char_u patbuf[NUMBUFLEN];
7451 char_u strbuf[NUMBUFLEN];
7452 char_u *save_cpo;
7453 long start = 0;
7454 long nth = 1;
7455 colnr_T startcol = 0;
7456 int match = 0;
7457 list_T *l = NULL;
7458 listitem_T *li = NULL;
7459 long idx = 0;
7460 char_u *tofree = NULL;
7461
7462 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7463 save_cpo = p_cpo;
7464 p_cpo = (char_u *)"";
7465
7466 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007467 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007468 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007469 /* type MATCH_LIST: return empty list when there are no matches.
7470 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007471 if (rettv_list_alloc(rettv) == FAIL)
7472 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007473 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007474 && (list_append_string(rettv->vval.v_list,
7475 (char_u *)"", 0) == FAIL
7476 || list_append_number(rettv->vval.v_list,
7477 (varnumber_T)-1) == FAIL
7478 || list_append_number(rettv->vval.v_list,
7479 (varnumber_T)-1) == FAIL
7480 || list_append_number(rettv->vval.v_list,
7481 (varnumber_T)-1) == FAIL))
7482 {
7483 list_free(rettv->vval.v_list);
7484 rettv->vval.v_list = NULL;
7485 goto theend;
7486 }
7487 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007488 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007489 {
7490 rettv->v_type = VAR_STRING;
7491 rettv->vval.v_string = NULL;
7492 }
7493
7494 if (argvars[0].v_type == VAR_LIST)
7495 {
7496 if ((l = argvars[0].vval.v_list) == NULL)
7497 goto theend;
7498 li = l->lv_first;
7499 }
7500 else
7501 {
7502 expr = str = get_tv_string(&argvars[0]);
7503 len = (long)STRLEN(str);
7504 }
7505
7506 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7507 if (pat == NULL)
7508 goto theend;
7509
7510 if (argvars[2].v_type != VAR_UNKNOWN)
7511 {
7512 int error = FALSE;
7513
7514 start = (long)get_tv_number_chk(&argvars[2], &error);
7515 if (error)
7516 goto theend;
7517 if (l != NULL)
7518 {
7519 li = list_find(l, start);
7520 if (li == NULL)
7521 goto theend;
7522 idx = l->lv_idx; /* use the cached index */
7523 }
7524 else
7525 {
7526 if (start < 0)
7527 start = 0;
7528 if (start > len)
7529 goto theend;
7530 /* When "count" argument is there ignore matches before "start",
7531 * otherwise skip part of the string. Differs when pattern is "^"
7532 * or "\<". */
7533 if (argvars[3].v_type != VAR_UNKNOWN)
7534 startcol = start;
7535 else
7536 {
7537 str += start;
7538 len -= start;
7539 }
7540 }
7541
7542 if (argvars[3].v_type != VAR_UNKNOWN)
7543 nth = (long)get_tv_number_chk(&argvars[3], &error);
7544 if (error)
7545 goto theend;
7546 }
7547
7548 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7549 if (regmatch.regprog != NULL)
7550 {
7551 regmatch.rm_ic = p_ic;
7552
7553 for (;;)
7554 {
7555 if (l != NULL)
7556 {
7557 if (li == NULL)
7558 {
7559 match = FALSE;
7560 break;
7561 }
7562 vim_free(tofree);
7563 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7564 if (str == NULL)
7565 break;
7566 }
7567
7568 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7569
7570 if (match && --nth <= 0)
7571 break;
7572 if (l == NULL && !match)
7573 break;
7574
7575 /* Advance to just after the match. */
7576 if (l != NULL)
7577 {
7578 li = li->li_next;
7579 ++idx;
7580 }
7581 else
7582 {
7583#ifdef FEAT_MBYTE
7584 startcol = (colnr_T)(regmatch.startp[0]
7585 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7586#else
7587 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7588#endif
7589 if (startcol > (colnr_T)len
7590 || str + startcol <= regmatch.startp[0])
7591 {
7592 match = FALSE;
7593 break;
7594 }
7595 }
7596 }
7597
7598 if (match)
7599 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007600 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007601 {
7602 listitem_T *li1 = rettv->vval.v_list->lv_first;
7603 listitem_T *li2 = li1->li_next;
7604 listitem_T *li3 = li2->li_next;
7605 listitem_T *li4 = li3->li_next;
7606
7607 vim_free(li1->li_tv.vval.v_string);
7608 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7609 (int)(regmatch.endp[0] - regmatch.startp[0]));
7610 li3->li_tv.vval.v_number =
7611 (varnumber_T)(regmatch.startp[0] - expr);
7612 li4->li_tv.vval.v_number =
7613 (varnumber_T)(regmatch.endp[0] - expr);
7614 if (l != NULL)
7615 li2->li_tv.vval.v_number = (varnumber_T)idx;
7616 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007617 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007618 {
7619 int i;
7620
7621 /* return list with matched string and submatches */
7622 for (i = 0; i < NSUBEXP; ++i)
7623 {
7624 if (regmatch.endp[i] == NULL)
7625 {
7626 if (list_append_string(rettv->vval.v_list,
7627 (char_u *)"", 0) == FAIL)
7628 break;
7629 }
7630 else if (list_append_string(rettv->vval.v_list,
7631 regmatch.startp[i],
7632 (int)(regmatch.endp[i] - regmatch.startp[i]))
7633 == FAIL)
7634 break;
7635 }
7636 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007637 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007638 {
7639 /* return matched string */
7640 if (l != NULL)
7641 copy_tv(&li->li_tv, rettv);
7642 else
7643 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7644 (int)(regmatch.endp[0] - regmatch.startp[0]));
7645 }
7646 else if (l != NULL)
7647 rettv->vval.v_number = idx;
7648 else
7649 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007650 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007651 rettv->vval.v_number =
7652 (varnumber_T)(regmatch.startp[0] - str);
7653 else
7654 rettv->vval.v_number =
7655 (varnumber_T)(regmatch.endp[0] - str);
7656 rettv->vval.v_number += (varnumber_T)(str - expr);
7657 }
7658 }
7659 vim_regfree(regmatch.regprog);
7660 }
7661
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007662theend:
7663 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007664 /* matchstrpos() without a list: drop the second item. */
7665 listitem_remove(rettv->vval.v_list,
7666 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007667 vim_free(tofree);
7668 p_cpo = save_cpo;
7669}
7670
7671/*
7672 * "match()" function
7673 */
7674 static void
7675f_match(typval_T *argvars, typval_T *rettv)
7676{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007677 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007678}
7679
7680/*
7681 * "matchadd()" function
7682 */
7683 static void
7684f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7685{
7686#ifdef FEAT_SEARCH_EXTRA
7687 char_u buf[NUMBUFLEN];
7688 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7689 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7690 int prio = 10; /* default priority */
7691 int id = -1;
7692 int error = FALSE;
7693 char_u *conceal_char = NULL;
7694
7695 rettv->vval.v_number = -1;
7696
7697 if (grp == NULL || pat == NULL)
7698 return;
7699 if (argvars[2].v_type != VAR_UNKNOWN)
7700 {
7701 prio = (int)get_tv_number_chk(&argvars[2], &error);
7702 if (argvars[3].v_type != VAR_UNKNOWN)
7703 {
7704 id = (int)get_tv_number_chk(&argvars[3], &error);
7705 if (argvars[4].v_type != VAR_UNKNOWN)
7706 {
7707 if (argvars[4].v_type != VAR_DICT)
7708 {
7709 EMSG(_(e_dictreq));
7710 return;
7711 }
7712 if (dict_find(argvars[4].vval.v_dict,
7713 (char_u *)"conceal", -1) != NULL)
7714 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7715 (char_u *)"conceal", FALSE);
7716 }
7717 }
7718 }
7719 if (error == TRUE)
7720 return;
7721 if (id >= 1 && id <= 3)
7722 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007723 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007724 return;
7725 }
7726
7727 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7728 conceal_char);
7729#endif
7730}
7731
7732/*
7733 * "matchaddpos()" function
7734 */
7735 static void
7736f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7737{
7738#ifdef FEAT_SEARCH_EXTRA
7739 char_u buf[NUMBUFLEN];
7740 char_u *group;
7741 int prio = 10;
7742 int id = -1;
7743 int error = FALSE;
7744 list_T *l;
7745 char_u *conceal_char = NULL;
7746
7747 rettv->vval.v_number = -1;
7748
7749 group = get_tv_string_buf_chk(&argvars[0], buf);
7750 if (group == NULL)
7751 return;
7752
7753 if (argvars[1].v_type != VAR_LIST)
7754 {
7755 EMSG2(_(e_listarg), "matchaddpos()");
7756 return;
7757 }
7758 l = argvars[1].vval.v_list;
7759 if (l == NULL)
7760 return;
7761
7762 if (argvars[2].v_type != VAR_UNKNOWN)
7763 {
7764 prio = (int)get_tv_number_chk(&argvars[2], &error);
7765 if (argvars[3].v_type != VAR_UNKNOWN)
7766 {
7767 id = (int)get_tv_number_chk(&argvars[3], &error);
7768 if (argvars[4].v_type != VAR_UNKNOWN)
7769 {
7770 if (argvars[4].v_type != VAR_DICT)
7771 {
7772 EMSG(_(e_dictreq));
7773 return;
7774 }
7775 if (dict_find(argvars[4].vval.v_dict,
7776 (char_u *)"conceal", -1) != NULL)
7777 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7778 (char_u *)"conceal", FALSE);
7779 }
7780 }
7781 }
7782 if (error == TRUE)
7783 return;
7784
7785 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7786 if (id == 1 || id == 2)
7787 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007788 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007789 return;
7790 }
7791
7792 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7793 conceal_char);
7794#endif
7795}
7796
7797/*
7798 * "matcharg()" function
7799 */
7800 static void
7801f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7802{
7803 if (rettv_list_alloc(rettv) == OK)
7804 {
7805#ifdef FEAT_SEARCH_EXTRA
7806 int id = (int)get_tv_number(&argvars[0]);
7807 matchitem_T *m;
7808
7809 if (id >= 1 && id <= 3)
7810 {
7811 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7812 {
7813 list_append_string(rettv->vval.v_list,
7814 syn_id2name(m->hlg_id), -1);
7815 list_append_string(rettv->vval.v_list, m->pattern, -1);
7816 }
7817 else
7818 {
7819 list_append_string(rettv->vval.v_list, NULL, -1);
7820 list_append_string(rettv->vval.v_list, NULL, -1);
7821 }
7822 }
7823#endif
7824 }
7825}
7826
7827/*
7828 * "matchdelete()" function
7829 */
7830 static void
7831f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7832{
7833#ifdef FEAT_SEARCH_EXTRA
7834 rettv->vval.v_number = match_delete(curwin,
7835 (int)get_tv_number(&argvars[0]), TRUE);
7836#endif
7837}
7838
7839/*
7840 * "matchend()" function
7841 */
7842 static void
7843f_matchend(typval_T *argvars, typval_T *rettv)
7844{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007845 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007846}
7847
7848/*
7849 * "matchlist()" function
7850 */
7851 static void
7852f_matchlist(typval_T *argvars, typval_T *rettv)
7853{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007854 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007855}
7856
7857/*
7858 * "matchstr()" function
7859 */
7860 static void
7861f_matchstr(typval_T *argvars, typval_T *rettv)
7862{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007863 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007864}
7865
7866/*
7867 * "matchstrpos()" function
7868 */
7869 static void
7870f_matchstrpos(typval_T *argvars, typval_T *rettv)
7871{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007872 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007873}
7874
7875static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7876
7877 static void
7878max_min(typval_T *argvars, typval_T *rettv, int domax)
7879{
7880 varnumber_T n = 0;
7881 varnumber_T i;
7882 int error = FALSE;
7883
7884 if (argvars[0].v_type == VAR_LIST)
7885 {
7886 list_T *l;
7887 listitem_T *li;
7888
7889 l = argvars[0].vval.v_list;
7890 if (l != NULL)
7891 {
7892 li = l->lv_first;
7893 if (li != NULL)
7894 {
7895 n = get_tv_number_chk(&li->li_tv, &error);
7896 for (;;)
7897 {
7898 li = li->li_next;
7899 if (li == NULL)
7900 break;
7901 i = get_tv_number_chk(&li->li_tv, &error);
7902 if (domax ? i > n : i < n)
7903 n = i;
7904 }
7905 }
7906 }
7907 }
7908 else if (argvars[0].v_type == VAR_DICT)
7909 {
7910 dict_T *d;
7911 int first = TRUE;
7912 hashitem_T *hi;
7913 int todo;
7914
7915 d = argvars[0].vval.v_dict;
7916 if (d != NULL)
7917 {
7918 todo = (int)d->dv_hashtab.ht_used;
7919 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7920 {
7921 if (!HASHITEM_EMPTY(hi))
7922 {
7923 --todo;
7924 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7925 if (first)
7926 {
7927 n = i;
7928 first = FALSE;
7929 }
7930 else if (domax ? i > n : i < n)
7931 n = i;
7932 }
7933 }
7934 }
7935 }
7936 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007937 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007938 rettv->vval.v_number = error ? 0 : n;
7939}
7940
7941/*
7942 * "max()" function
7943 */
7944 static void
7945f_max(typval_T *argvars, typval_T *rettv)
7946{
7947 max_min(argvars, rettv, TRUE);
7948}
7949
7950/*
7951 * "min()" function
7952 */
7953 static void
7954f_min(typval_T *argvars, typval_T *rettv)
7955{
7956 max_min(argvars, rettv, FALSE);
7957}
7958
7959static int mkdir_recurse(char_u *dir, int prot);
7960
7961/*
7962 * Create the directory in which "dir" is located, and higher levels when
7963 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007964 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007965 */
7966 static int
7967mkdir_recurse(char_u *dir, int prot)
7968{
7969 char_u *p;
7970 char_u *updir;
7971 int r = FAIL;
7972
7973 /* Get end of directory name in "dir".
7974 * We're done when it's "/" or "c:/". */
7975 p = gettail_sep(dir);
7976 if (p <= get_past_head(dir))
7977 return OK;
7978
7979 /* If the directory exists we're done. Otherwise: create it.*/
7980 updir = vim_strnsave(dir, (int)(p - dir));
7981 if (updir == NULL)
7982 return FAIL;
7983 if (mch_isdir(updir))
7984 r = OK;
7985 else if (mkdir_recurse(updir, prot) == OK)
7986 r = vim_mkdir_emsg(updir, prot);
7987 vim_free(updir);
7988 return r;
7989}
7990
7991#ifdef vim_mkdir
7992/*
7993 * "mkdir()" function
7994 */
7995 static void
7996f_mkdir(typval_T *argvars, typval_T *rettv)
7997{
7998 char_u *dir;
7999 char_u buf[NUMBUFLEN];
8000 int prot = 0755;
8001
8002 rettv->vval.v_number = FAIL;
8003 if (check_restricted() || check_secure())
8004 return;
8005
8006 dir = get_tv_string_buf(&argvars[0], buf);
8007 if (*dir == NUL)
8008 rettv->vval.v_number = FAIL;
8009 else
8010 {
8011 if (*gettail(dir) == NUL)
8012 /* remove trailing slashes */
8013 *gettail_sep(dir) = NUL;
8014
8015 if (argvars[1].v_type != VAR_UNKNOWN)
8016 {
8017 if (argvars[2].v_type != VAR_UNKNOWN)
8018 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8019 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8020 mkdir_recurse(dir, prot);
8021 }
8022 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
8023 }
8024}
8025#endif
8026
8027/*
8028 * "mode()" function
8029 */
8030 static void
8031f_mode(typval_T *argvars, typval_T *rettv)
8032{
8033 char_u buf[3];
8034
8035 buf[1] = NUL;
8036 buf[2] = NUL;
8037
8038 if (time_for_testing == 93784)
8039 {
8040 /* Testing the two-character code. */
8041 buf[0] = 'x';
8042 buf[1] = '!';
8043 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008044#ifdef FEAT_TERMINAL
8045 else if (term_use_loop())
8046 buf[0] = 't';
8047#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008048 else if (VIsual_active)
8049 {
8050 if (VIsual_select)
8051 buf[0] = VIsual_mode + 's' - 'v';
8052 else
8053 buf[0] = VIsual_mode;
8054 }
8055 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8056 || State == CONFIRM)
8057 {
8058 buf[0] = 'r';
8059 if (State == ASKMORE)
8060 buf[1] = 'm';
8061 else if (State == CONFIRM)
8062 buf[1] = '?';
8063 }
8064 else if (State == EXTERNCMD)
8065 buf[0] = '!';
8066 else if (State & INSERT)
8067 {
8068#ifdef FEAT_VREPLACE
8069 if (State & VREPLACE_FLAG)
8070 {
8071 buf[0] = 'R';
8072 buf[1] = 'v';
8073 }
8074 else
8075#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008076 {
8077 if (State & REPLACE_FLAG)
8078 buf[0] = 'R';
8079 else
8080 buf[0] = 'i';
8081#ifdef FEAT_INS_EXPAND
8082 if (ins_compl_active())
8083 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008084 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008085 buf[1] = 'x';
8086#endif
8087 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008088 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008089 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008090 {
8091 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008092 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008093 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008094 else if (exmode_active == EXMODE_NORMAL)
8095 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008096 }
8097 else
8098 {
8099 buf[0] = 'n';
8100 if (finish_op)
8101 buf[1] = 'o';
8102 }
8103
8104 /* Clear out the minor mode when the argument is not a non-zero number or
8105 * non-empty string. */
8106 if (!non_zero_arg(&argvars[0]))
8107 buf[1] = NUL;
8108
8109 rettv->vval.v_string = vim_strsave(buf);
8110 rettv->v_type = VAR_STRING;
8111}
8112
8113#if defined(FEAT_MZSCHEME) || defined(PROTO)
8114/*
8115 * "mzeval()" function
8116 */
8117 static void
8118f_mzeval(typval_T *argvars, typval_T *rettv)
8119{
8120 char_u *str;
8121 char_u buf[NUMBUFLEN];
8122
8123 str = get_tv_string_buf(&argvars[0], buf);
8124 do_mzeval(str, rettv);
8125}
8126
8127 void
8128mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8129{
8130 typval_T argvars[3];
8131
8132 argvars[0].v_type = VAR_STRING;
8133 argvars[0].vval.v_string = name;
8134 copy_tv(args, &argvars[1]);
8135 argvars[2].v_type = VAR_UNKNOWN;
8136 f_call(argvars, rettv);
8137 clear_tv(&argvars[1]);
8138}
8139#endif
8140
8141/*
8142 * "nextnonblank()" function
8143 */
8144 static void
8145f_nextnonblank(typval_T *argvars, typval_T *rettv)
8146{
8147 linenr_T lnum;
8148
8149 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8150 {
8151 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8152 {
8153 lnum = 0;
8154 break;
8155 }
8156 if (*skipwhite(ml_get(lnum)) != NUL)
8157 break;
8158 }
8159 rettv->vval.v_number = lnum;
8160}
8161
8162/*
8163 * "nr2char()" function
8164 */
8165 static void
8166f_nr2char(typval_T *argvars, typval_T *rettv)
8167{
8168 char_u buf[NUMBUFLEN];
8169
8170#ifdef FEAT_MBYTE
8171 if (has_mbyte)
8172 {
8173 int utf8 = 0;
8174
8175 if (argvars[1].v_type != VAR_UNKNOWN)
8176 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8177 if (utf8)
8178 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8179 else
8180 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8181 }
8182 else
8183#endif
8184 {
8185 buf[0] = (char_u)get_tv_number(&argvars[0]);
8186 buf[1] = NUL;
8187 }
8188 rettv->v_type = VAR_STRING;
8189 rettv->vval.v_string = vim_strsave(buf);
8190}
8191
8192/*
8193 * "or(expr, expr)" function
8194 */
8195 static void
8196f_or(typval_T *argvars, typval_T *rettv)
8197{
8198 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8199 | get_tv_number_chk(&argvars[1], NULL);
8200}
8201
8202/*
8203 * "pathshorten()" function
8204 */
8205 static void
8206f_pathshorten(typval_T *argvars, typval_T *rettv)
8207{
8208 char_u *p;
8209
8210 rettv->v_type = VAR_STRING;
8211 p = get_tv_string_chk(&argvars[0]);
8212 if (p == NULL)
8213 rettv->vval.v_string = NULL;
8214 else
8215 {
8216 p = vim_strsave(p);
8217 rettv->vval.v_string = p;
8218 if (p != NULL)
8219 shorten_dir(p);
8220 }
8221}
8222
8223#ifdef FEAT_PERL
8224/*
8225 * "perleval()" function
8226 */
8227 static void
8228f_perleval(typval_T *argvars, typval_T *rettv)
8229{
8230 char_u *str;
8231 char_u buf[NUMBUFLEN];
8232
8233 str = get_tv_string_buf(&argvars[0], buf);
8234 do_perleval(str, rettv);
8235}
8236#endif
8237
8238#ifdef FEAT_FLOAT
8239/*
8240 * "pow()" function
8241 */
8242 static void
8243f_pow(typval_T *argvars, typval_T *rettv)
8244{
8245 float_T fx = 0.0, fy = 0.0;
8246
8247 rettv->v_type = VAR_FLOAT;
8248 if (get_float_arg(argvars, &fx) == OK
8249 && get_float_arg(&argvars[1], &fy) == OK)
8250 rettv->vval.v_float = pow(fx, fy);
8251 else
8252 rettv->vval.v_float = 0.0;
8253}
8254#endif
8255
8256/*
8257 * "prevnonblank()" function
8258 */
8259 static void
8260f_prevnonblank(typval_T *argvars, typval_T *rettv)
8261{
8262 linenr_T lnum;
8263
8264 lnum = get_tv_lnum(argvars);
8265 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8266 lnum = 0;
8267 else
8268 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8269 --lnum;
8270 rettv->vval.v_number = lnum;
8271}
8272
8273/* This dummy va_list is here because:
8274 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8275 * - locally in the function results in a "used before set" warning
8276 * - using va_start() to initialize it gives "function with fixed args" error */
8277static va_list ap;
8278
8279/*
8280 * "printf()" function
8281 */
8282 static void
8283f_printf(typval_T *argvars, typval_T *rettv)
8284{
8285 char_u buf[NUMBUFLEN];
8286 int len;
8287 char_u *s;
8288 int saved_did_emsg = did_emsg;
8289 char *fmt;
8290
8291 rettv->v_type = VAR_STRING;
8292 rettv->vval.v_string = NULL;
8293
8294 /* Get the required length, allocate the buffer and do it for real. */
8295 did_emsg = FALSE;
8296 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008297 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008298 if (!did_emsg)
8299 {
8300 s = alloc(len + 1);
8301 if (s != NULL)
8302 {
8303 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008304 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8305 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008306 }
8307 }
8308 did_emsg |= saved_did_emsg;
8309}
8310
8311/*
8312 * "pumvisible()" function
8313 */
8314 static void
8315f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8316{
8317#ifdef FEAT_INS_EXPAND
8318 if (pum_visible())
8319 rettv->vval.v_number = 1;
8320#endif
8321}
8322
8323#ifdef FEAT_PYTHON3
8324/*
8325 * "py3eval()" function
8326 */
8327 static void
8328f_py3eval(typval_T *argvars, typval_T *rettv)
8329{
8330 char_u *str;
8331 char_u buf[NUMBUFLEN];
8332
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008333 if (p_pyx == 0)
8334 p_pyx = 3;
8335
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008336 str = get_tv_string_buf(&argvars[0], buf);
8337 do_py3eval(str, rettv);
8338}
8339#endif
8340
8341#ifdef FEAT_PYTHON
8342/*
8343 * "pyeval()" function
8344 */
8345 static void
8346f_pyeval(typval_T *argvars, typval_T *rettv)
8347{
8348 char_u *str;
8349 char_u buf[NUMBUFLEN];
8350
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008351 if (p_pyx == 0)
8352 p_pyx = 2;
8353
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008354 str = get_tv_string_buf(&argvars[0], buf);
8355 do_pyeval(str, rettv);
8356}
8357#endif
8358
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008359#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8360/*
8361 * "pyxeval()" function
8362 */
8363 static void
8364f_pyxeval(typval_T *argvars, typval_T *rettv)
8365{
8366# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8367 init_pyxversion();
8368 if (p_pyx == 2)
8369 f_pyeval(argvars, rettv);
8370 else
8371 f_py3eval(argvars, rettv);
8372# elif defined(FEAT_PYTHON)
8373 f_pyeval(argvars, rettv);
8374# elif defined(FEAT_PYTHON3)
8375 f_py3eval(argvars, rettv);
8376# endif
8377}
8378#endif
8379
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008380/*
8381 * "range()" function
8382 */
8383 static void
8384f_range(typval_T *argvars, typval_T *rettv)
8385{
8386 varnumber_T start;
8387 varnumber_T end;
8388 varnumber_T stride = 1;
8389 varnumber_T i;
8390 int error = FALSE;
8391
8392 start = get_tv_number_chk(&argvars[0], &error);
8393 if (argvars[1].v_type == VAR_UNKNOWN)
8394 {
8395 end = start - 1;
8396 start = 0;
8397 }
8398 else
8399 {
8400 end = get_tv_number_chk(&argvars[1], &error);
8401 if (argvars[2].v_type != VAR_UNKNOWN)
8402 stride = get_tv_number_chk(&argvars[2], &error);
8403 }
8404
8405 if (error)
8406 return; /* type error; errmsg already given */
8407 if (stride == 0)
8408 EMSG(_("E726: Stride is zero"));
8409 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8410 EMSG(_("E727: Start past end"));
8411 else
8412 {
8413 if (rettv_list_alloc(rettv) == OK)
8414 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8415 if (list_append_number(rettv->vval.v_list,
8416 (varnumber_T)i) == FAIL)
8417 break;
8418 }
8419}
8420
8421/*
8422 * "readfile()" function
8423 */
8424 static void
8425f_readfile(typval_T *argvars, typval_T *rettv)
8426{
8427 int binary = FALSE;
8428 int failed = FALSE;
8429 char_u *fname;
8430 FILE *fd;
8431 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8432 int io_size = sizeof(buf);
8433 int readlen; /* size of last fread() */
8434 char_u *prev = NULL; /* previously read bytes, if any */
8435 long prevlen = 0; /* length of data in prev */
8436 long prevsize = 0; /* size of prev buffer */
8437 long maxline = MAXLNUM;
8438 long cnt = 0;
8439 char_u *p; /* position in buf */
8440 char_u *start; /* start of current line */
8441
8442 if (argvars[1].v_type != VAR_UNKNOWN)
8443 {
8444 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8445 binary = TRUE;
8446 if (argvars[2].v_type != VAR_UNKNOWN)
8447 maxline = (long)get_tv_number(&argvars[2]);
8448 }
8449
8450 if (rettv_list_alloc(rettv) == FAIL)
8451 return;
8452
8453 /* Always open the file in binary mode, library functions have a mind of
8454 * their own about CR-LF conversion. */
8455 fname = get_tv_string(&argvars[0]);
8456 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8457 {
8458 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8459 return;
8460 }
8461
8462 while (cnt < maxline || maxline < 0)
8463 {
8464 readlen = (int)fread(buf, 1, io_size, fd);
8465
8466 /* This for loop processes what was read, but is also entered at end
8467 * of file so that either:
8468 * - an incomplete line gets written
8469 * - a "binary" file gets an empty line at the end if it ends in a
8470 * newline. */
8471 for (p = buf, start = buf;
8472 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8473 ++p)
8474 {
8475 if (*p == '\n' || readlen <= 0)
8476 {
8477 listitem_T *li;
8478 char_u *s = NULL;
8479 long_u len = p - start;
8480
8481 /* Finished a line. Remove CRs before NL. */
8482 if (readlen > 0 && !binary)
8483 {
8484 while (len > 0 && start[len - 1] == '\r')
8485 --len;
8486 /* removal may cross back to the "prev" string */
8487 if (len == 0)
8488 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8489 --prevlen;
8490 }
8491 if (prevlen == 0)
8492 s = vim_strnsave(start, (int)len);
8493 else
8494 {
8495 /* Change "prev" buffer to be the right size. This way
8496 * the bytes are only copied once, and very long lines are
8497 * allocated only once. */
8498 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8499 {
8500 mch_memmove(s + prevlen, start, len);
8501 s[prevlen + len] = NUL;
8502 prev = NULL; /* the list will own the string */
8503 prevlen = prevsize = 0;
8504 }
8505 }
8506 if (s == NULL)
8507 {
8508 do_outofmem_msg((long_u) prevlen + len + 1);
8509 failed = TRUE;
8510 break;
8511 }
8512
8513 if ((li = listitem_alloc()) == NULL)
8514 {
8515 vim_free(s);
8516 failed = TRUE;
8517 break;
8518 }
8519 li->li_tv.v_type = VAR_STRING;
8520 li->li_tv.v_lock = 0;
8521 li->li_tv.vval.v_string = s;
8522 list_append(rettv->vval.v_list, li);
8523
8524 start = p + 1; /* step over newline */
8525 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8526 break;
8527 }
8528 else if (*p == NUL)
8529 *p = '\n';
8530#ifdef FEAT_MBYTE
8531 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8532 * when finding the BF and check the previous two bytes. */
8533 else if (*p == 0xbf && enc_utf8 && !binary)
8534 {
8535 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8536 * + 1, these may be in the "prev" string. */
8537 char_u back1 = p >= buf + 1 ? p[-1]
8538 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8539 char_u back2 = p >= buf + 2 ? p[-2]
8540 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8541 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8542
8543 if (back2 == 0xef && back1 == 0xbb)
8544 {
8545 char_u *dest = p - 2;
8546
8547 /* Usually a BOM is at the beginning of a file, and so at
8548 * the beginning of a line; then we can just step over it.
8549 */
8550 if (start == dest)
8551 start = p + 1;
8552 else
8553 {
8554 /* have to shuffle buf to close gap */
8555 int adjust_prevlen = 0;
8556
8557 if (dest < buf)
8558 {
8559 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8560 dest = buf;
8561 }
8562 if (readlen > p - buf + 1)
8563 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8564 readlen -= 3 - adjust_prevlen;
8565 prevlen -= adjust_prevlen;
8566 p = dest - 1;
8567 }
8568 }
8569 }
8570#endif
8571 } /* for */
8572
8573 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8574 break;
8575 if (start < p)
8576 {
8577 /* There's part of a line in buf, store it in "prev". */
8578 if (p - start + prevlen >= prevsize)
8579 {
8580 /* need bigger "prev" buffer */
8581 char_u *newprev;
8582
8583 /* A common use case is ordinary text files and "prev" gets a
8584 * fragment of a line, so the first allocation is made
8585 * small, to avoid repeatedly 'allocing' large and
8586 * 'reallocing' small. */
8587 if (prevsize == 0)
8588 prevsize = (long)(p - start);
8589 else
8590 {
8591 long grow50pc = (prevsize * 3) / 2;
8592 long growmin = (long)((p - start) * 2 + prevlen);
8593 prevsize = grow50pc > growmin ? grow50pc : growmin;
8594 }
8595 newprev = prev == NULL ? alloc(prevsize)
8596 : vim_realloc(prev, prevsize);
8597 if (newprev == NULL)
8598 {
8599 do_outofmem_msg((long_u)prevsize);
8600 failed = TRUE;
8601 break;
8602 }
8603 prev = newprev;
8604 }
8605 /* Add the line part to end of "prev". */
8606 mch_memmove(prev + prevlen, start, p - start);
8607 prevlen += (long)(p - start);
8608 }
8609 } /* while */
8610
8611 /*
8612 * For a negative line count use only the lines at the end of the file,
8613 * free the rest.
8614 */
8615 if (!failed && maxline < 0)
8616 while (cnt > -maxline)
8617 {
8618 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8619 --cnt;
8620 }
8621
8622 if (failed)
8623 {
8624 list_free(rettv->vval.v_list);
8625 /* readfile doc says an empty list is returned on error */
8626 rettv->vval.v_list = list_alloc();
8627 }
8628
8629 vim_free(prev);
8630 fclose(fd);
8631}
8632
8633#if defined(FEAT_RELTIME)
8634static int list2proftime(typval_T *arg, proftime_T *tm);
8635
8636/*
8637 * Convert a List to proftime_T.
8638 * Return FAIL when there is something wrong.
8639 */
8640 static int
8641list2proftime(typval_T *arg, proftime_T *tm)
8642{
8643 long n1, n2;
8644 int error = FALSE;
8645
8646 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8647 || arg->vval.v_list->lv_len != 2)
8648 return FAIL;
8649 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8650 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8651# ifdef WIN3264
8652 tm->HighPart = n1;
8653 tm->LowPart = n2;
8654# else
8655 tm->tv_sec = n1;
8656 tm->tv_usec = n2;
8657# endif
8658 return error ? FAIL : OK;
8659}
8660#endif /* FEAT_RELTIME */
8661
8662/*
8663 * "reltime()" function
8664 */
8665 static void
8666f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8667{
8668#ifdef FEAT_RELTIME
8669 proftime_T res;
8670 proftime_T start;
8671
8672 if (argvars[0].v_type == VAR_UNKNOWN)
8673 {
8674 /* No arguments: get current time. */
8675 profile_start(&res);
8676 }
8677 else if (argvars[1].v_type == VAR_UNKNOWN)
8678 {
8679 if (list2proftime(&argvars[0], &res) == FAIL)
8680 return;
8681 profile_end(&res);
8682 }
8683 else
8684 {
8685 /* Two arguments: compute the difference. */
8686 if (list2proftime(&argvars[0], &start) == FAIL
8687 || list2proftime(&argvars[1], &res) == FAIL)
8688 return;
8689 profile_sub(&res, &start);
8690 }
8691
8692 if (rettv_list_alloc(rettv) == OK)
8693 {
8694 long n1, n2;
8695
8696# ifdef WIN3264
8697 n1 = res.HighPart;
8698 n2 = res.LowPart;
8699# else
8700 n1 = res.tv_sec;
8701 n2 = res.tv_usec;
8702# endif
8703 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8704 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8705 }
8706#endif
8707}
8708
8709#ifdef FEAT_FLOAT
8710/*
8711 * "reltimefloat()" function
8712 */
8713 static void
8714f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8715{
8716# ifdef FEAT_RELTIME
8717 proftime_T tm;
8718# endif
8719
8720 rettv->v_type = VAR_FLOAT;
8721 rettv->vval.v_float = 0;
8722# ifdef FEAT_RELTIME
8723 if (list2proftime(&argvars[0], &tm) == OK)
8724 rettv->vval.v_float = profile_float(&tm);
8725# endif
8726}
8727#endif
8728
8729/*
8730 * "reltimestr()" function
8731 */
8732 static void
8733f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8734{
8735#ifdef FEAT_RELTIME
8736 proftime_T tm;
8737#endif
8738
8739 rettv->v_type = VAR_STRING;
8740 rettv->vval.v_string = NULL;
8741#ifdef FEAT_RELTIME
8742 if (list2proftime(&argvars[0], &tm) == OK)
8743 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8744#endif
8745}
8746
8747#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8748static void make_connection(void);
8749static int check_connection(void);
8750
8751 static void
8752make_connection(void)
8753{
8754 if (X_DISPLAY == NULL
8755# ifdef FEAT_GUI
8756 && !gui.in_use
8757# endif
8758 )
8759 {
8760 x_force_connect = TRUE;
8761 setup_term_clip();
8762 x_force_connect = FALSE;
8763 }
8764}
8765
8766 static int
8767check_connection(void)
8768{
8769 make_connection();
8770 if (X_DISPLAY == NULL)
8771 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008772 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008773 return FAIL;
8774 }
8775 return OK;
8776}
8777#endif
8778
8779#ifdef FEAT_CLIENTSERVER
8780 static void
8781remote_common(typval_T *argvars, typval_T *rettv, int expr)
8782{
8783 char_u *server_name;
8784 char_u *keys;
8785 char_u *r = NULL;
8786 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008787 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008788# ifdef WIN32
8789 HWND w;
8790# else
8791 Window w;
8792# endif
8793
8794 if (check_restricted() || check_secure())
8795 return;
8796
8797# ifdef FEAT_X11
8798 if (check_connection() == FAIL)
8799 return;
8800# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008801 if (argvars[2].v_type != VAR_UNKNOWN
8802 && argvars[3].v_type != VAR_UNKNOWN)
8803 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008804
8805 server_name = get_tv_string_chk(&argvars[0]);
8806 if (server_name == NULL)
8807 return; /* type error; errmsg already given */
8808 keys = get_tv_string_buf(&argvars[1], buf);
8809# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008810 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008811# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008812 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8813 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008814# endif
8815 {
8816 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008817 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008818 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008819 vim_free(r);
8820 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008821 else
8822 EMSG2(_("E241: Unable to send to %s"), server_name);
8823 return;
8824 }
8825
8826 rettv->vval.v_string = r;
8827
8828 if (argvars[2].v_type != VAR_UNKNOWN)
8829 {
8830 dictitem_T v;
8831 char_u str[30];
8832 char_u *idvar;
8833
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008834 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008835 if (idvar != NULL && *idvar != NUL)
8836 {
8837 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8838 v.di_tv.v_type = VAR_STRING;
8839 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008841 vim_free(v.di_tv.vval.v_string);
8842 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008843 }
8844}
8845#endif
8846
8847/*
8848 * "remote_expr()" function
8849 */
8850 static void
8851f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8852{
8853 rettv->v_type = VAR_STRING;
8854 rettv->vval.v_string = NULL;
8855#ifdef FEAT_CLIENTSERVER
8856 remote_common(argvars, rettv, TRUE);
8857#endif
8858}
8859
8860/*
8861 * "remote_foreground()" function
8862 */
8863 static void
8864f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8865{
8866#ifdef FEAT_CLIENTSERVER
8867# ifdef WIN32
8868 /* On Win32 it's done in this application. */
8869 {
8870 char_u *server_name = get_tv_string_chk(&argvars[0]);
8871
8872 if (server_name != NULL)
8873 serverForeground(server_name);
8874 }
8875# else
8876 /* Send a foreground() expression to the server. */
8877 argvars[1].v_type = VAR_STRING;
8878 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8879 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008880 rettv->v_type = VAR_STRING;
8881 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008882 remote_common(argvars, rettv, TRUE);
8883 vim_free(argvars[1].vval.v_string);
8884# endif
8885#endif
8886}
8887
8888 static void
8889f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8890{
8891#ifdef FEAT_CLIENTSERVER
8892 dictitem_T v;
8893 char_u *s = NULL;
8894# ifdef WIN32
8895 long_u n = 0;
8896# endif
8897 char_u *serverid;
8898
8899 if (check_restricted() || check_secure())
8900 {
8901 rettv->vval.v_number = -1;
8902 return;
8903 }
8904 serverid = get_tv_string_chk(&argvars[0]);
8905 if (serverid == NULL)
8906 {
8907 rettv->vval.v_number = -1;
8908 return; /* type error; errmsg already given */
8909 }
8910# ifdef WIN32
8911 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8912 if (n == 0)
8913 rettv->vval.v_number = -1;
8914 else
8915 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008916 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008917 rettv->vval.v_number = (s != NULL);
8918 }
8919# else
8920 if (check_connection() == FAIL)
8921 return;
8922
8923 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8924 serverStrToWin(serverid), &s);
8925# endif
8926
8927 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8928 {
8929 char_u *retvar;
8930
8931 v.di_tv.v_type = VAR_STRING;
8932 v.di_tv.vval.v_string = vim_strsave(s);
8933 retvar = get_tv_string_chk(&argvars[1]);
8934 if (retvar != NULL)
8935 set_var(retvar, &v.di_tv, FALSE);
8936 vim_free(v.di_tv.vval.v_string);
8937 }
8938#else
8939 rettv->vval.v_number = -1;
8940#endif
8941}
8942
8943 static void
8944f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8945{
8946 char_u *r = NULL;
8947
8948#ifdef FEAT_CLIENTSERVER
8949 char_u *serverid = get_tv_string_chk(&argvars[0]);
8950
8951 if (serverid != NULL && !check_restricted() && !check_secure())
8952 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008953 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008954# ifdef WIN32
8955 /* The server's HWND is encoded in the 'id' parameter */
8956 long_u n = 0;
8957# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008958
8959 if (argvars[1].v_type != VAR_UNKNOWN)
8960 timeout = get_tv_number(&argvars[1]);
8961
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008962# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008963 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8964 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008965 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008966 if (r == NULL)
8967# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008968 if (check_connection() == FAIL
8969 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8970 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008971# endif
8972 EMSG(_("E277: Unable to read a server reply"));
8973 }
8974#endif
8975 rettv->v_type = VAR_STRING;
8976 rettv->vval.v_string = r;
8977}
8978
8979/*
8980 * "remote_send()" function
8981 */
8982 static void
8983f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8984{
8985 rettv->v_type = VAR_STRING;
8986 rettv->vval.v_string = NULL;
8987#ifdef FEAT_CLIENTSERVER
8988 remote_common(argvars, rettv, FALSE);
8989#endif
8990}
8991
8992/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008993 * "remote_startserver()" function
8994 */
8995 static void
8996f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8997{
8998#ifdef FEAT_CLIENTSERVER
8999 char_u *server = get_tv_string_chk(&argvars[0]);
9000
9001 if (server == NULL)
9002 return; /* type error; errmsg already given */
9003 if (serverName != NULL)
9004 EMSG(_("E941: already started a server"));
9005 else
9006 {
9007# ifdef FEAT_X11
9008 if (check_connection() == OK)
9009 serverRegisterName(X_DISPLAY, server);
9010# else
9011 serverSetName(server);
9012# endif
9013 }
9014#else
9015 EMSG(_("E942: +clientserver feature not available"));
9016#endif
9017}
9018
9019/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009020 * "remove()" function
9021 */
9022 static void
9023f_remove(typval_T *argvars, typval_T *rettv)
9024{
9025 list_T *l;
9026 listitem_T *item, *item2;
9027 listitem_T *li;
9028 long idx;
9029 long end;
9030 char_u *key;
9031 dict_T *d;
9032 dictitem_T *di;
9033 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9034
9035 if (argvars[0].v_type == VAR_DICT)
9036 {
9037 if (argvars[2].v_type != VAR_UNKNOWN)
9038 EMSG2(_(e_toomanyarg), "remove()");
9039 else if ((d = argvars[0].vval.v_dict) != NULL
9040 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9041 {
9042 key = get_tv_string_chk(&argvars[1]);
9043 if (key != NULL)
9044 {
9045 di = dict_find(d, key, -1);
9046 if (di == NULL)
9047 EMSG2(_(e_dictkey), key);
9048 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9049 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9050 {
9051 *rettv = di->di_tv;
9052 init_tv(&di->di_tv);
9053 dictitem_remove(d, di);
9054 }
9055 }
9056 }
9057 }
9058 else if (argvars[0].v_type != VAR_LIST)
9059 EMSG2(_(e_listdictarg), "remove()");
9060 else if ((l = argvars[0].vval.v_list) != NULL
9061 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9062 {
9063 int error = FALSE;
9064
9065 idx = (long)get_tv_number_chk(&argvars[1], &error);
9066 if (error)
9067 ; /* type error: do nothing, errmsg already given */
9068 else if ((item = list_find(l, idx)) == NULL)
9069 EMSGN(_(e_listidx), idx);
9070 else
9071 {
9072 if (argvars[2].v_type == VAR_UNKNOWN)
9073 {
9074 /* Remove one item, return its value. */
9075 vimlist_remove(l, item, item);
9076 *rettv = item->li_tv;
9077 vim_free(item);
9078 }
9079 else
9080 {
9081 /* Remove range of items, return list with values. */
9082 end = (long)get_tv_number_chk(&argvars[2], &error);
9083 if (error)
9084 ; /* type error: do nothing */
9085 else if ((item2 = list_find(l, end)) == NULL)
9086 EMSGN(_(e_listidx), end);
9087 else
9088 {
9089 int cnt = 0;
9090
9091 for (li = item; li != NULL; li = li->li_next)
9092 {
9093 ++cnt;
9094 if (li == item2)
9095 break;
9096 }
9097 if (li == NULL) /* didn't find "item2" after "item" */
9098 EMSG(_(e_invrange));
9099 else
9100 {
9101 vimlist_remove(l, item, item2);
9102 if (rettv_list_alloc(rettv) == OK)
9103 {
9104 l = rettv->vval.v_list;
9105 l->lv_first = item;
9106 l->lv_last = item2;
9107 item->li_prev = NULL;
9108 item2->li_next = NULL;
9109 l->lv_len = cnt;
9110 }
9111 }
9112 }
9113 }
9114 }
9115 }
9116}
9117
9118/*
9119 * "rename({from}, {to})" function
9120 */
9121 static void
9122f_rename(typval_T *argvars, typval_T *rettv)
9123{
9124 char_u buf[NUMBUFLEN];
9125
9126 if (check_restricted() || check_secure())
9127 rettv->vval.v_number = -1;
9128 else
9129 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9130 get_tv_string_buf(&argvars[1], buf));
9131}
9132
9133/*
9134 * "repeat()" function
9135 */
9136 static void
9137f_repeat(typval_T *argvars, typval_T *rettv)
9138{
9139 char_u *p;
9140 int n;
9141 int slen;
9142 int len;
9143 char_u *r;
9144 int i;
9145
9146 n = (int)get_tv_number(&argvars[1]);
9147 if (argvars[0].v_type == VAR_LIST)
9148 {
9149 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9150 while (n-- > 0)
9151 if (list_extend(rettv->vval.v_list,
9152 argvars[0].vval.v_list, NULL) == FAIL)
9153 break;
9154 }
9155 else
9156 {
9157 p = get_tv_string(&argvars[0]);
9158 rettv->v_type = VAR_STRING;
9159 rettv->vval.v_string = NULL;
9160
9161 slen = (int)STRLEN(p);
9162 len = slen * n;
9163 if (len <= 0)
9164 return;
9165
9166 r = alloc(len + 1);
9167 if (r != NULL)
9168 {
9169 for (i = 0; i < n; i++)
9170 mch_memmove(r + i * slen, p, (size_t)slen);
9171 r[len] = NUL;
9172 }
9173
9174 rettv->vval.v_string = r;
9175 }
9176}
9177
9178/*
9179 * "resolve()" function
9180 */
9181 static void
9182f_resolve(typval_T *argvars, typval_T *rettv)
9183{
9184 char_u *p;
9185#ifdef HAVE_READLINK
9186 char_u *buf = NULL;
9187#endif
9188
9189 p = get_tv_string(&argvars[0]);
9190#ifdef FEAT_SHORTCUT
9191 {
9192 char_u *v = NULL;
9193
9194 v = mch_resolve_shortcut(p);
9195 if (v != NULL)
9196 rettv->vval.v_string = v;
9197 else
9198 rettv->vval.v_string = vim_strsave(p);
9199 }
9200#else
9201# ifdef HAVE_READLINK
9202 {
9203 char_u *cpy;
9204 int len;
9205 char_u *remain = NULL;
9206 char_u *q;
9207 int is_relative_to_current = FALSE;
9208 int has_trailing_pathsep = FALSE;
9209 int limit = 100;
9210
9211 p = vim_strsave(p);
9212
9213 if (p[0] == '.' && (vim_ispathsep(p[1])
9214 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9215 is_relative_to_current = TRUE;
9216
9217 len = STRLEN(p);
9218 if (len > 0 && after_pathsep(p, p + len))
9219 {
9220 has_trailing_pathsep = TRUE;
9221 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9222 }
9223
9224 q = getnextcomp(p);
9225 if (*q != NUL)
9226 {
9227 /* Separate the first path component in "p", and keep the
9228 * remainder (beginning with the path separator). */
9229 remain = vim_strsave(q - 1);
9230 q[-1] = NUL;
9231 }
9232
9233 buf = alloc(MAXPATHL + 1);
9234 if (buf == NULL)
9235 goto fail;
9236
9237 for (;;)
9238 {
9239 for (;;)
9240 {
9241 len = readlink((char *)p, (char *)buf, MAXPATHL);
9242 if (len <= 0)
9243 break;
9244 buf[len] = NUL;
9245
9246 if (limit-- == 0)
9247 {
9248 vim_free(p);
9249 vim_free(remain);
9250 EMSG(_("E655: Too many symbolic links (cycle?)"));
9251 rettv->vval.v_string = NULL;
9252 goto fail;
9253 }
9254
9255 /* Ensure that the result will have a trailing path separator
9256 * if the argument has one. */
9257 if (remain == NULL && has_trailing_pathsep)
9258 add_pathsep(buf);
9259
9260 /* Separate the first path component in the link value and
9261 * concatenate the remainders. */
9262 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9263 if (*q != NUL)
9264 {
9265 if (remain == NULL)
9266 remain = vim_strsave(q - 1);
9267 else
9268 {
9269 cpy = concat_str(q - 1, remain);
9270 if (cpy != NULL)
9271 {
9272 vim_free(remain);
9273 remain = cpy;
9274 }
9275 }
9276 q[-1] = NUL;
9277 }
9278
9279 q = gettail(p);
9280 if (q > p && *q == NUL)
9281 {
9282 /* Ignore trailing path separator. */
9283 q[-1] = NUL;
9284 q = gettail(p);
9285 }
9286 if (q > p && !mch_isFullName(buf))
9287 {
9288 /* symlink is relative to directory of argument */
9289 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9290 if (cpy != NULL)
9291 {
9292 STRCPY(cpy, p);
9293 STRCPY(gettail(cpy), buf);
9294 vim_free(p);
9295 p = cpy;
9296 }
9297 }
9298 else
9299 {
9300 vim_free(p);
9301 p = vim_strsave(buf);
9302 }
9303 }
9304
9305 if (remain == NULL)
9306 break;
9307
9308 /* Append the first path component of "remain" to "p". */
9309 q = getnextcomp(remain + 1);
9310 len = q - remain - (*q != NUL);
9311 cpy = vim_strnsave(p, STRLEN(p) + len);
9312 if (cpy != NULL)
9313 {
9314 STRNCAT(cpy, remain, len);
9315 vim_free(p);
9316 p = cpy;
9317 }
9318 /* Shorten "remain". */
9319 if (*q != NUL)
9320 STRMOVE(remain, q - 1);
9321 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009322 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009323 }
9324
9325 /* If the result is a relative path name, make it explicitly relative to
9326 * the current directory if and only if the argument had this form. */
9327 if (!vim_ispathsep(*p))
9328 {
9329 if (is_relative_to_current
9330 && *p != NUL
9331 && !(p[0] == '.'
9332 && (p[1] == NUL
9333 || vim_ispathsep(p[1])
9334 || (p[1] == '.'
9335 && (p[2] == NUL
9336 || vim_ispathsep(p[2]))))))
9337 {
9338 /* Prepend "./". */
9339 cpy = concat_str((char_u *)"./", p);
9340 if (cpy != NULL)
9341 {
9342 vim_free(p);
9343 p = cpy;
9344 }
9345 }
9346 else if (!is_relative_to_current)
9347 {
9348 /* Strip leading "./". */
9349 q = p;
9350 while (q[0] == '.' && vim_ispathsep(q[1]))
9351 q += 2;
9352 if (q > p)
9353 STRMOVE(p, p + 2);
9354 }
9355 }
9356
9357 /* Ensure that the result will have no trailing path separator
9358 * if the argument had none. But keep "/" or "//". */
9359 if (!has_trailing_pathsep)
9360 {
9361 q = p + STRLEN(p);
9362 if (after_pathsep(p, q))
9363 *gettail_sep(p) = NUL;
9364 }
9365
9366 rettv->vval.v_string = p;
9367 }
9368# else
9369 rettv->vval.v_string = vim_strsave(p);
9370# endif
9371#endif
9372
9373 simplify_filename(rettv->vval.v_string);
9374
9375#ifdef HAVE_READLINK
9376fail:
9377 vim_free(buf);
9378#endif
9379 rettv->v_type = VAR_STRING;
9380}
9381
9382/*
9383 * "reverse({list})" function
9384 */
9385 static void
9386f_reverse(typval_T *argvars, typval_T *rettv)
9387{
9388 list_T *l;
9389 listitem_T *li, *ni;
9390
9391 if (argvars[0].v_type != VAR_LIST)
9392 EMSG2(_(e_listarg), "reverse()");
9393 else if ((l = argvars[0].vval.v_list) != NULL
9394 && !tv_check_lock(l->lv_lock,
9395 (char_u *)N_("reverse() argument"), TRUE))
9396 {
9397 li = l->lv_last;
9398 l->lv_first = l->lv_last = NULL;
9399 l->lv_len = 0;
9400 while (li != NULL)
9401 {
9402 ni = li->li_prev;
9403 list_append(l, li);
9404 li = ni;
9405 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009406 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009407 l->lv_idx = l->lv_len - l->lv_idx - 1;
9408 }
9409}
9410
9411#define SP_NOMOVE 0x01 /* don't move cursor */
9412#define SP_REPEAT 0x02 /* repeat to find outer pair */
9413#define SP_RETCOUNT 0x04 /* return matchcount */
9414#define SP_SETPCMARK 0x08 /* set previous context mark */
9415#define SP_START 0x10 /* accept match at start position */
9416#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9417#define SP_END 0x40 /* leave cursor at end of match */
9418#define SP_COLUMN 0x80 /* start at cursor column */
9419
9420static int get_search_arg(typval_T *varp, int *flagsp);
9421
9422/*
9423 * Get flags for a search function.
9424 * Possibly sets "p_ws".
9425 * Returns BACKWARD, FORWARD or zero (for an error).
9426 */
9427 static int
9428get_search_arg(typval_T *varp, int *flagsp)
9429{
9430 int dir = FORWARD;
9431 char_u *flags;
9432 char_u nbuf[NUMBUFLEN];
9433 int mask;
9434
9435 if (varp->v_type != VAR_UNKNOWN)
9436 {
9437 flags = get_tv_string_buf_chk(varp, nbuf);
9438 if (flags == NULL)
9439 return 0; /* type error; errmsg already given */
9440 while (*flags != NUL)
9441 {
9442 switch (*flags)
9443 {
9444 case 'b': dir = BACKWARD; break;
9445 case 'w': p_ws = TRUE; break;
9446 case 'W': p_ws = FALSE; break;
9447 default: mask = 0;
9448 if (flagsp != NULL)
9449 switch (*flags)
9450 {
9451 case 'c': mask = SP_START; break;
9452 case 'e': mask = SP_END; break;
9453 case 'm': mask = SP_RETCOUNT; break;
9454 case 'n': mask = SP_NOMOVE; break;
9455 case 'p': mask = SP_SUBPAT; break;
9456 case 'r': mask = SP_REPEAT; break;
9457 case 's': mask = SP_SETPCMARK; break;
9458 case 'z': mask = SP_COLUMN; break;
9459 }
9460 if (mask == 0)
9461 {
9462 EMSG2(_(e_invarg2), flags);
9463 dir = 0;
9464 }
9465 else
9466 *flagsp |= mask;
9467 }
9468 if (dir == 0)
9469 break;
9470 ++flags;
9471 }
9472 }
9473 return dir;
9474}
9475
9476/*
9477 * Shared by search() and searchpos() functions.
9478 */
9479 static int
9480search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9481{
9482 int flags;
9483 char_u *pat;
9484 pos_T pos;
9485 pos_T save_cursor;
9486 int save_p_ws = p_ws;
9487 int dir;
9488 int retval = 0; /* default: FAIL */
9489 long lnum_stop = 0;
9490 proftime_T tm;
9491#ifdef FEAT_RELTIME
9492 long time_limit = 0;
9493#endif
9494 int options = SEARCH_KEEP;
9495 int subpatnum;
9496
9497 pat = get_tv_string(&argvars[0]);
9498 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9499 if (dir == 0)
9500 goto theend;
9501 flags = *flagsp;
9502 if (flags & SP_START)
9503 options |= SEARCH_START;
9504 if (flags & SP_END)
9505 options |= SEARCH_END;
9506 if (flags & SP_COLUMN)
9507 options |= SEARCH_COL;
9508
9509 /* Optional arguments: line number to stop searching and timeout. */
9510 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9511 {
9512 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9513 if (lnum_stop < 0)
9514 goto theend;
9515#ifdef FEAT_RELTIME
9516 if (argvars[3].v_type != VAR_UNKNOWN)
9517 {
9518 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9519 if (time_limit < 0)
9520 goto theend;
9521 }
9522#endif
9523 }
9524
9525#ifdef FEAT_RELTIME
9526 /* Set the time limit, if there is one. */
9527 profile_setlimit(time_limit, &tm);
9528#endif
9529
9530 /*
9531 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9532 * Check to make sure only those flags are set.
9533 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9534 * flags cannot be set. Check for that condition also.
9535 */
9536 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9537 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9538 {
9539 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9540 goto theend;
9541 }
9542
9543 pos = save_cursor = curwin->w_cursor;
9544 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009545 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009546 if (subpatnum != FAIL)
9547 {
9548 if (flags & SP_SUBPAT)
9549 retval = subpatnum;
9550 else
9551 retval = pos.lnum;
9552 if (flags & SP_SETPCMARK)
9553 setpcmark();
9554 curwin->w_cursor = pos;
9555 if (match_pos != NULL)
9556 {
9557 /* Store the match cursor position */
9558 match_pos->lnum = pos.lnum;
9559 match_pos->col = pos.col + 1;
9560 }
9561 /* "/$" will put the cursor after the end of the line, may need to
9562 * correct that here */
9563 check_cursor();
9564 }
9565
9566 /* If 'n' flag is used: restore cursor position. */
9567 if (flags & SP_NOMOVE)
9568 curwin->w_cursor = save_cursor;
9569 else
9570 curwin->w_set_curswant = TRUE;
9571theend:
9572 p_ws = save_p_ws;
9573
9574 return retval;
9575}
9576
9577#ifdef FEAT_FLOAT
9578
9579/*
9580 * round() is not in C90, use ceil() or floor() instead.
9581 */
9582 float_T
9583vim_round(float_T f)
9584{
9585 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9586}
9587
9588/*
9589 * "round({float})" function
9590 */
9591 static void
9592f_round(typval_T *argvars, typval_T *rettv)
9593{
9594 float_T f = 0.0;
9595
9596 rettv->v_type = VAR_FLOAT;
9597 if (get_float_arg(argvars, &f) == OK)
9598 rettv->vval.v_float = vim_round(f);
9599 else
9600 rettv->vval.v_float = 0.0;
9601}
9602#endif
9603
9604/*
9605 * "screenattr()" function
9606 */
9607 static void
9608f_screenattr(typval_T *argvars, typval_T *rettv)
9609{
9610 int row;
9611 int col;
9612 int c;
9613
9614 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9615 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9616 if (row < 0 || row >= screen_Rows
9617 || col < 0 || col >= screen_Columns)
9618 c = -1;
9619 else
9620 c = ScreenAttrs[LineOffset[row] + col];
9621 rettv->vval.v_number = c;
9622}
9623
9624/*
9625 * "screenchar()" function
9626 */
9627 static void
9628f_screenchar(typval_T *argvars, typval_T *rettv)
9629{
9630 int row;
9631 int col;
9632 int off;
9633 int c;
9634
9635 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9636 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9637 if (row < 0 || row >= screen_Rows
9638 || col < 0 || col >= screen_Columns)
9639 c = -1;
9640 else
9641 {
9642 off = LineOffset[row] + col;
9643#ifdef FEAT_MBYTE
9644 if (enc_utf8 && ScreenLinesUC[off] != 0)
9645 c = ScreenLinesUC[off];
9646 else
9647#endif
9648 c = ScreenLines[off];
9649 }
9650 rettv->vval.v_number = c;
9651}
9652
9653/*
9654 * "screencol()" function
9655 *
9656 * First column is 1 to be consistent with virtcol().
9657 */
9658 static void
9659f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9660{
9661 rettv->vval.v_number = screen_screencol() + 1;
9662}
9663
9664/*
9665 * "screenrow()" function
9666 */
9667 static void
9668f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9669{
9670 rettv->vval.v_number = screen_screenrow() + 1;
9671}
9672
9673/*
9674 * "search()" function
9675 */
9676 static void
9677f_search(typval_T *argvars, typval_T *rettv)
9678{
9679 int flags = 0;
9680
9681 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9682}
9683
9684/*
9685 * "searchdecl()" function
9686 */
9687 static void
9688f_searchdecl(typval_T *argvars, typval_T *rettv)
9689{
9690 int locally = 1;
9691 int thisblock = 0;
9692 int error = FALSE;
9693 char_u *name;
9694
9695 rettv->vval.v_number = 1; /* default: FAIL */
9696
9697 name = get_tv_string_chk(&argvars[0]);
9698 if (argvars[1].v_type != VAR_UNKNOWN)
9699 {
9700 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9701 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9702 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9703 }
9704 if (!error && name != NULL)
9705 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9706 locally, thisblock, SEARCH_KEEP) == FAIL;
9707}
9708
9709/*
9710 * Used by searchpair() and searchpairpos()
9711 */
9712 static int
9713searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9714{
9715 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009716 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009717 int save_p_ws = p_ws;
9718 int dir;
9719 int flags = 0;
9720 char_u nbuf1[NUMBUFLEN];
9721 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009722 int retval = 0; /* default: FAIL */
9723 long lnum_stop = 0;
9724 long time_limit = 0;
9725
9726 /* Get the three pattern arguments: start, middle, end. */
9727 spat = get_tv_string_chk(&argvars[0]);
9728 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9729 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9730 if (spat == NULL || mpat == NULL || epat == NULL)
9731 goto theend; /* type error */
9732
9733 /* Handle the optional fourth argument: flags */
9734 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9735 if (dir == 0)
9736 goto theend;
9737
9738 /* Don't accept SP_END or SP_SUBPAT.
9739 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9740 */
9741 if ((flags & (SP_END | SP_SUBPAT)) != 0
9742 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9743 {
9744 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9745 goto theend;
9746 }
9747
9748 /* Using 'r' implies 'W', otherwise it doesn't work. */
9749 if (flags & SP_REPEAT)
9750 p_ws = FALSE;
9751
9752 /* Optional fifth argument: skip expression */
9753 if (argvars[3].v_type == VAR_UNKNOWN
9754 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009755 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009756 else
9757 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009758 skip = &argvars[4];
9759 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9760 && skip->v_type != VAR_STRING)
9761 {
9762 /* Type error */
9763 goto theend;
9764 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009765 if (argvars[5].v_type != VAR_UNKNOWN)
9766 {
9767 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9768 if (lnum_stop < 0)
9769 goto theend;
9770#ifdef FEAT_RELTIME
9771 if (argvars[6].v_type != VAR_UNKNOWN)
9772 {
9773 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9774 if (time_limit < 0)
9775 goto theend;
9776 }
9777#endif
9778 }
9779 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009780
9781 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9782 match_pos, lnum_stop, time_limit);
9783
9784theend:
9785 p_ws = save_p_ws;
9786
9787 return retval;
9788}
9789
9790/*
9791 * "searchpair()" function
9792 */
9793 static void
9794f_searchpair(typval_T *argvars, typval_T *rettv)
9795{
9796 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9797}
9798
9799/*
9800 * "searchpairpos()" function
9801 */
9802 static void
9803f_searchpairpos(typval_T *argvars, typval_T *rettv)
9804{
9805 pos_T match_pos;
9806 int lnum = 0;
9807 int col = 0;
9808
9809 if (rettv_list_alloc(rettv) == FAIL)
9810 return;
9811
9812 if (searchpair_cmn(argvars, &match_pos) > 0)
9813 {
9814 lnum = match_pos.lnum;
9815 col = match_pos.col;
9816 }
9817
9818 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9819 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9820}
9821
9822/*
9823 * Search for a start/middle/end thing.
9824 * Used by searchpair(), see its documentation for the details.
9825 * Returns 0 or -1 for no match,
9826 */
9827 long
9828do_searchpair(
9829 char_u *spat, /* start pattern */
9830 char_u *mpat, /* middle pattern */
9831 char_u *epat, /* end pattern */
9832 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009833 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009834 int flags, /* SP_SETPCMARK and other SP_ values */
9835 pos_T *match_pos,
9836 linenr_T lnum_stop, /* stop at this line if not zero */
9837 long time_limit UNUSED) /* stop after this many msec */
9838{
9839 char_u *save_cpo;
9840 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9841 long retval = 0;
9842 pos_T pos;
9843 pos_T firstpos;
9844 pos_T foundpos;
9845 pos_T save_cursor;
9846 pos_T save_pos;
9847 int n;
9848 int r;
9849 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009850 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009851 int err;
9852 int options = SEARCH_KEEP;
9853 proftime_T tm;
9854
9855 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9856 save_cpo = p_cpo;
9857 p_cpo = empty_option;
9858
9859#ifdef FEAT_RELTIME
9860 /* Set the time limit, if there is one. */
9861 profile_setlimit(time_limit, &tm);
9862#endif
9863
9864 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9865 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009866 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9867 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009868 if (pat2 == NULL || pat3 == NULL)
9869 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009870 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009871 if (*mpat == NUL)
9872 STRCPY(pat3, pat2);
9873 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009874 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009875 spat, epat, mpat);
9876 if (flags & SP_START)
9877 options |= SEARCH_START;
9878
Bram Moolenaar48570482017-10-30 21:48:41 +01009879 if (skip != NULL)
9880 {
9881 /* Empty string means to not use the skip expression. */
9882 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9883 use_skip = skip->vval.v_string != NULL
9884 && *skip->vval.v_string != NUL;
9885 }
9886
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009887 save_cursor = curwin->w_cursor;
9888 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009889 CLEAR_POS(&firstpos);
9890 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009891 pat = pat3;
9892 for (;;)
9893 {
9894 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009895 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009896 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009897 /* didn't find it or found the first match again: FAIL */
9898 break;
9899
9900 if (firstpos.lnum == 0)
9901 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009902 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009903 {
9904 /* Found the same position again. Can happen with a pattern that
9905 * has "\zs" at the end and searching backwards. Advance one
9906 * character and try again. */
9907 if (dir == BACKWARD)
9908 decl(&pos);
9909 else
9910 incl(&pos);
9911 }
9912 foundpos = pos;
9913
9914 /* clear the start flag to avoid getting stuck here */
9915 options &= ~SEARCH_START;
9916
9917 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009918 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009919 {
9920 save_pos = curwin->w_cursor;
9921 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009922 err = FALSE;
9923 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009924 curwin->w_cursor = save_pos;
9925 if (err)
9926 {
9927 /* Evaluating {skip} caused an error, break here. */
9928 curwin->w_cursor = save_cursor;
9929 retval = -1;
9930 break;
9931 }
9932 if (r)
9933 continue;
9934 }
9935
9936 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9937 {
9938 /* Found end when searching backwards or start when searching
9939 * forward: nested pair. */
9940 ++nest;
9941 pat = pat2; /* nested, don't search for middle */
9942 }
9943 else
9944 {
9945 /* Found end when searching forward or start when searching
9946 * backward: end of (nested) pair; or found middle in outer pair. */
9947 if (--nest == 1)
9948 pat = pat3; /* outer level, search for middle */
9949 }
9950
9951 if (nest == 0)
9952 {
9953 /* Found the match: return matchcount or line number. */
9954 if (flags & SP_RETCOUNT)
9955 ++retval;
9956 else
9957 retval = pos.lnum;
9958 if (flags & SP_SETPCMARK)
9959 setpcmark();
9960 curwin->w_cursor = pos;
9961 if (!(flags & SP_REPEAT))
9962 break;
9963 nest = 1; /* search for next unmatched */
9964 }
9965 }
9966
9967 if (match_pos != NULL)
9968 {
9969 /* Store the match cursor position */
9970 match_pos->lnum = curwin->w_cursor.lnum;
9971 match_pos->col = curwin->w_cursor.col + 1;
9972 }
9973
9974 /* If 'n' flag is used or search failed: restore cursor position. */
9975 if ((flags & SP_NOMOVE) || retval == 0)
9976 curwin->w_cursor = save_cursor;
9977
9978theend:
9979 vim_free(pat2);
9980 vim_free(pat3);
9981 if (p_cpo == empty_option)
9982 p_cpo = save_cpo;
9983 else
9984 /* Darn, evaluating the {skip} expression changed the value. */
9985 free_string_option(save_cpo);
9986
9987 return retval;
9988}
9989
9990/*
9991 * "searchpos()" function
9992 */
9993 static void
9994f_searchpos(typval_T *argvars, typval_T *rettv)
9995{
9996 pos_T match_pos;
9997 int lnum = 0;
9998 int col = 0;
9999 int n;
10000 int flags = 0;
10001
10002 if (rettv_list_alloc(rettv) == FAIL)
10003 return;
10004
10005 n = search_cmn(argvars, &match_pos, &flags);
10006 if (n > 0)
10007 {
10008 lnum = match_pos.lnum;
10009 col = match_pos.col;
10010 }
10011
10012 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10013 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10014 if (flags & SP_SUBPAT)
10015 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10016}
10017
10018 static void
10019f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10020{
10021#ifdef FEAT_CLIENTSERVER
10022 char_u buf[NUMBUFLEN];
10023 char_u *server = get_tv_string_chk(&argvars[0]);
10024 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10025
10026 rettv->vval.v_number = -1;
10027 if (server == NULL || reply == NULL)
10028 return;
10029 if (check_restricted() || check_secure())
10030 return;
10031# ifdef FEAT_X11
10032 if (check_connection() == FAIL)
10033 return;
10034# endif
10035
10036 if (serverSendReply(server, reply) < 0)
10037 {
10038 EMSG(_("E258: Unable to send to client"));
10039 return;
10040 }
10041 rettv->vval.v_number = 0;
10042#else
10043 rettv->vval.v_number = -1;
10044#endif
10045}
10046
10047 static void
10048f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10049{
10050 char_u *r = NULL;
10051
10052#ifdef FEAT_CLIENTSERVER
10053# ifdef WIN32
10054 r = serverGetVimNames();
10055# else
10056 make_connection();
10057 if (X_DISPLAY != NULL)
10058 r = serverGetVimNames(X_DISPLAY);
10059# endif
10060#endif
10061 rettv->v_type = VAR_STRING;
10062 rettv->vval.v_string = r;
10063}
10064
10065/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010066 * Set line or list of lines in buffer "buf".
10067 */
10068 static void
10069set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
10070{
10071 char_u *line = NULL;
10072 list_T *l = NULL;
10073 listitem_T *li = NULL;
10074 long added = 0;
10075 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010076 buf_T *curbuf_save = NULL;
10077 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010078 int is_curbuf = buf == curbuf;
10079
Bram Moolenaar9d954202017-09-04 20:34:19 +020010080 /* When using the current buffer ml_mfp will be set if needed. Useful when
10081 * setline() is used on startup. For other buffers the buffer must be
10082 * loaded. */
10083 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010084 {
10085 rettv->vval.v_number = 1; /* FAIL */
10086 return;
10087 }
10088
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010089 if (!is_curbuf)
10090 {
10091 wininfo_T *wip;
10092
10093 curbuf_save = curbuf;
10094 curwin_save = curwin;
10095 curbuf = buf;
10096 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10097 {
10098 if (wip->wi_win != NULL)
10099 {
10100 curwin = wip->wi_win;
10101 break;
10102 }
10103 }
10104 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010105
10106 lcount = curbuf->b_ml.ml_line_count;
10107
10108 if (lines->v_type == VAR_LIST)
10109 {
10110 l = lines->vval.v_list;
10111 li = l->lv_first;
10112 }
10113 else
10114 line = get_tv_string_chk(lines);
10115
10116 /* default result is zero == OK */
10117 for (;;)
10118 {
10119 if (l != NULL)
10120 {
10121 /* list argument, get next string */
10122 if (li == NULL)
10123 break;
10124 line = get_tv_string_chk(&li->li_tv);
10125 li = li->li_next;
10126 }
10127
10128 rettv->vval.v_number = 1; /* FAIL */
10129 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10130 break;
10131
10132 /* When coming here from Insert mode, sync undo, so that this can be
10133 * undone separately from what was previously inserted. */
10134 if (u_sync_once == 2)
10135 {
10136 u_sync_once = 1; /* notify that u_sync() was called */
10137 u_sync(TRUE);
10138 }
10139
10140 if (lnum <= curbuf->b_ml.ml_line_count)
10141 {
10142 /* existing line, replace it */
10143 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10144 {
10145 changed_bytes(lnum, 0);
10146 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10147 check_cursor_col();
10148 rettv->vval.v_number = 0; /* OK */
10149 }
10150 }
10151 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10152 {
10153 /* lnum is one past the last line, append the line */
10154 ++added;
10155 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10156 rettv->vval.v_number = 0; /* OK */
10157 }
10158
10159 if (l == NULL) /* only one string argument */
10160 break;
10161 ++lnum;
10162 }
10163
10164 if (added > 0)
10165 appended_lines_mark(lcount, added);
10166
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010167 if (!is_curbuf)
10168 {
10169 curbuf = curbuf_save;
10170 curwin = curwin_save;
10171 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010172}
10173
10174/*
10175 * "setbufline()" function
10176 */
10177 static void
10178f_setbufline(argvars, rettv)
10179 typval_T *argvars;
10180 typval_T *rettv;
10181{
10182 linenr_T lnum;
10183 buf_T *buf;
10184
10185 buf = get_buf_tv(&argvars[0], FALSE);
10186 if (buf == NULL)
10187 rettv->vval.v_number = 1; /* FAIL */
10188 else
10189 {
10190 lnum = get_tv_lnum_buf(&argvars[1], buf);
10191
10192 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10193 }
10194}
10195
10196/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010197 * "setbufvar()" function
10198 */
10199 static void
10200f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10201{
10202 buf_T *buf;
10203 char_u *varname, *bufvarname;
10204 typval_T *varp;
10205 char_u nbuf[NUMBUFLEN];
10206
10207 if (check_restricted() || check_secure())
10208 return;
10209 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10210 varname = get_tv_string_chk(&argvars[1]);
10211 buf = get_buf_tv(&argvars[0], FALSE);
10212 varp = &argvars[2];
10213
10214 if (buf != NULL && varname != NULL && varp != NULL)
10215 {
10216 if (*varname == '&')
10217 {
10218 long numval;
10219 char_u *strval;
10220 int error = FALSE;
10221 aco_save_T aco;
10222
10223 /* set curbuf to be our buf, temporarily */
10224 aucmd_prepbuf(&aco, buf);
10225
10226 ++varname;
10227 numval = (long)get_tv_number_chk(varp, &error);
10228 strval = get_tv_string_buf_chk(varp, nbuf);
10229 if (!error && strval != NULL)
10230 set_option_value(varname, numval, strval, OPT_LOCAL);
10231
10232 /* reset notion of buffer */
10233 aucmd_restbuf(&aco);
10234 }
10235 else
10236 {
10237 buf_T *save_curbuf = curbuf;
10238
10239 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10240 if (bufvarname != NULL)
10241 {
10242 curbuf = buf;
10243 STRCPY(bufvarname, "b:");
10244 STRCPY(bufvarname + 2, varname);
10245 set_var(bufvarname, varp, TRUE);
10246 vim_free(bufvarname);
10247 curbuf = save_curbuf;
10248 }
10249 }
10250 }
10251}
10252
10253 static void
10254f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10255{
10256 dict_T *d;
10257 dictitem_T *di;
10258 char_u *csearch;
10259
10260 if (argvars[0].v_type != VAR_DICT)
10261 {
10262 EMSG(_(e_dictreq));
10263 return;
10264 }
10265
10266 if ((d = argvars[0].vval.v_dict) != NULL)
10267 {
10268 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10269 if (csearch != NULL)
10270 {
10271#ifdef FEAT_MBYTE
10272 if (enc_utf8)
10273 {
10274 int pcc[MAX_MCO];
10275 int c = utfc_ptr2char(csearch, pcc);
10276
10277 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10278 }
10279 else
10280#endif
10281 set_last_csearch(PTR2CHAR(csearch),
10282 csearch, MB_PTR2LEN(csearch));
10283 }
10284
10285 di = dict_find(d, (char_u *)"forward", -1);
10286 if (di != NULL)
10287 set_csearch_direction((int)get_tv_number(&di->di_tv)
10288 ? FORWARD : BACKWARD);
10289
10290 di = dict_find(d, (char_u *)"until", -1);
10291 if (di != NULL)
10292 set_csearch_until(!!get_tv_number(&di->di_tv));
10293 }
10294}
10295
10296/*
10297 * "setcmdpos()" function
10298 */
10299 static void
10300f_setcmdpos(typval_T *argvars, typval_T *rettv)
10301{
10302 int pos = (int)get_tv_number(&argvars[0]) - 1;
10303
10304 if (pos >= 0)
10305 rettv->vval.v_number = set_cmdline_pos(pos);
10306}
10307
10308/*
10309 * "setfperm({fname}, {mode})" function
10310 */
10311 static void
10312f_setfperm(typval_T *argvars, typval_T *rettv)
10313{
10314 char_u *fname;
10315 char_u modebuf[NUMBUFLEN];
10316 char_u *mode_str;
10317 int i;
10318 int mask;
10319 int mode = 0;
10320
10321 rettv->vval.v_number = 0;
10322 fname = get_tv_string_chk(&argvars[0]);
10323 if (fname == NULL)
10324 return;
10325 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10326 if (mode_str == NULL)
10327 return;
10328 if (STRLEN(mode_str) != 9)
10329 {
10330 EMSG2(_(e_invarg2), mode_str);
10331 return;
10332 }
10333
10334 mask = 1;
10335 for (i = 8; i >= 0; --i)
10336 {
10337 if (mode_str[i] != '-')
10338 mode |= mask;
10339 mask = mask << 1;
10340 }
10341 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10342}
10343
10344/*
10345 * "setline()" function
10346 */
10347 static void
10348f_setline(typval_T *argvars, typval_T *rettv)
10349{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010350 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010351
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010352 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010353}
10354
Bram Moolenaard823fa92016-08-12 16:29:27 +020010355static 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 +020010356
10357/*
10358 * Used by "setqflist()" and "setloclist()" functions
10359 */
10360 static void
10361set_qf_ll_list(
10362 win_T *wp UNUSED,
10363 typval_T *list_arg UNUSED,
10364 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010365 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010366 typval_T *rettv)
10367{
10368#ifdef FEAT_QUICKFIX
10369 static char *e_invact = N_("E927: Invalid action: '%s'");
10370 char_u *act;
10371 int action = 0;
10372#endif
10373
10374 rettv->vval.v_number = -1;
10375
10376#ifdef FEAT_QUICKFIX
10377 if (list_arg->v_type != VAR_LIST)
10378 EMSG(_(e_listreq));
10379 else
10380 {
10381 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010382 dict_T *d = NULL;
10383 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010384
10385 if (action_arg->v_type == VAR_STRING)
10386 {
10387 act = get_tv_string_chk(action_arg);
10388 if (act == NULL)
10389 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010390 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10391 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010392 action = *act;
10393 else
10394 EMSG2(_(e_invact), act);
10395 }
10396 else if (action_arg->v_type == VAR_UNKNOWN)
10397 action = ' ';
10398 else
10399 EMSG(_(e_stringreq));
10400
Bram Moolenaard823fa92016-08-12 16:29:27 +020010401 if (action_arg->v_type != VAR_UNKNOWN
10402 && what_arg->v_type != VAR_UNKNOWN)
10403 {
10404 if (what_arg->v_type == VAR_DICT)
10405 d = what_arg->vval.v_dict;
10406 else
10407 {
10408 EMSG(_(e_dictreq));
10409 valid_dict = FALSE;
10410 }
10411 }
10412
10413 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10414 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010415 rettv->vval.v_number = 0;
10416 }
10417#endif
10418}
10419
10420/*
10421 * "setloclist()" function
10422 */
10423 static void
10424f_setloclist(typval_T *argvars, typval_T *rettv)
10425{
10426 win_T *win;
10427
10428 rettv->vval.v_number = -1;
10429
10430 win = find_win_by_nr(&argvars[0], NULL);
10431 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010432 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010433}
10434
10435/*
10436 * "setmatches()" function
10437 */
10438 static void
10439f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10440{
10441#ifdef FEAT_SEARCH_EXTRA
10442 list_T *l;
10443 listitem_T *li;
10444 dict_T *d;
10445 list_T *s = NULL;
10446
10447 rettv->vval.v_number = -1;
10448 if (argvars[0].v_type != VAR_LIST)
10449 {
10450 EMSG(_(e_listreq));
10451 return;
10452 }
10453 if ((l = argvars[0].vval.v_list) != NULL)
10454 {
10455
10456 /* To some extent make sure that we are dealing with a list from
10457 * "getmatches()". */
10458 li = l->lv_first;
10459 while (li != NULL)
10460 {
10461 if (li->li_tv.v_type != VAR_DICT
10462 || (d = li->li_tv.vval.v_dict) == NULL)
10463 {
10464 EMSG(_(e_invarg));
10465 return;
10466 }
10467 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10468 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10469 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10470 && dict_find(d, (char_u *)"priority", -1) != NULL
10471 && dict_find(d, (char_u *)"id", -1) != NULL))
10472 {
10473 EMSG(_(e_invarg));
10474 return;
10475 }
10476 li = li->li_next;
10477 }
10478
10479 clear_matches(curwin);
10480 li = l->lv_first;
10481 while (li != NULL)
10482 {
10483 int i = 0;
10484 char_u buf[5];
10485 dictitem_T *di;
10486 char_u *group;
10487 int priority;
10488 int id;
10489 char_u *conceal;
10490
10491 d = li->li_tv.vval.v_dict;
10492 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10493 {
10494 if (s == NULL)
10495 {
10496 s = list_alloc();
10497 if (s == NULL)
10498 return;
10499 }
10500
10501 /* match from matchaddpos() */
10502 for (i = 1; i < 9; i++)
10503 {
10504 sprintf((char *)buf, (char *)"pos%d", i);
10505 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10506 {
10507 if (di->di_tv.v_type != VAR_LIST)
10508 return;
10509
10510 list_append_tv(s, &di->di_tv);
10511 s->lv_refcount++;
10512 }
10513 else
10514 break;
10515 }
10516 }
10517
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010518 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010519 priority = (int)get_dict_number(d, (char_u *)"priority");
10520 id = (int)get_dict_number(d, (char_u *)"id");
10521 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010522 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010523 : NULL;
10524 if (i == 0)
10525 {
10526 match_add(curwin, group,
10527 get_dict_string(d, (char_u *)"pattern", FALSE),
10528 priority, id, NULL, conceal);
10529 }
10530 else
10531 {
10532 match_add(curwin, group, NULL, priority, id, s, conceal);
10533 list_unref(s);
10534 s = NULL;
10535 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010536 vim_free(group);
10537 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010538
10539 li = li->li_next;
10540 }
10541 rettv->vval.v_number = 0;
10542 }
10543#endif
10544}
10545
10546/*
10547 * "setpos()" function
10548 */
10549 static void
10550f_setpos(typval_T *argvars, typval_T *rettv)
10551{
10552 pos_T pos;
10553 int fnum;
10554 char_u *name;
10555 colnr_T curswant = -1;
10556
10557 rettv->vval.v_number = -1;
10558 name = get_tv_string_chk(argvars);
10559 if (name != NULL)
10560 {
10561 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10562 {
10563 if (--pos.col < 0)
10564 pos.col = 0;
10565 if (name[0] == '.' && name[1] == NUL)
10566 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010567 /* set cursor; "fnum" is ignored */
10568 curwin->w_cursor = pos;
10569 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010570 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010571 curwin->w_curswant = curswant - 1;
10572 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010573 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010574 check_cursor();
10575 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010576 }
10577 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10578 {
10579 /* set mark */
10580 if (setmark_pos(name[1], &pos, fnum) == OK)
10581 rettv->vval.v_number = 0;
10582 }
10583 else
10584 EMSG(_(e_invarg));
10585 }
10586 }
10587}
10588
10589/*
10590 * "setqflist()" function
10591 */
10592 static void
10593f_setqflist(typval_T *argvars, typval_T *rettv)
10594{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010595 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010596}
10597
10598/*
10599 * "setreg()" function
10600 */
10601 static void
10602f_setreg(typval_T *argvars, typval_T *rettv)
10603{
10604 int regname;
10605 char_u *strregname;
10606 char_u *stropt;
10607 char_u *strval;
10608 int append;
10609 char_u yank_type;
10610 long block_len;
10611
10612 block_len = -1;
10613 yank_type = MAUTO;
10614 append = FALSE;
10615
10616 strregname = get_tv_string_chk(argvars);
10617 rettv->vval.v_number = 1; /* FAIL is default */
10618
10619 if (strregname == NULL)
10620 return; /* type error; errmsg already given */
10621 regname = *strregname;
10622 if (regname == 0 || regname == '@')
10623 regname = '"';
10624
10625 if (argvars[2].v_type != VAR_UNKNOWN)
10626 {
10627 stropt = get_tv_string_chk(&argvars[2]);
10628 if (stropt == NULL)
10629 return; /* type error */
10630 for (; *stropt != NUL; ++stropt)
10631 switch (*stropt)
10632 {
10633 case 'a': case 'A': /* append */
10634 append = TRUE;
10635 break;
10636 case 'v': case 'c': /* character-wise selection */
10637 yank_type = MCHAR;
10638 break;
10639 case 'V': case 'l': /* line-wise selection */
10640 yank_type = MLINE;
10641 break;
10642 case 'b': case Ctrl_V: /* block-wise selection */
10643 yank_type = MBLOCK;
10644 if (VIM_ISDIGIT(stropt[1]))
10645 {
10646 ++stropt;
10647 block_len = getdigits(&stropt) - 1;
10648 --stropt;
10649 }
10650 break;
10651 }
10652 }
10653
10654 if (argvars[1].v_type == VAR_LIST)
10655 {
10656 char_u **lstval;
10657 char_u **allocval;
10658 char_u buf[NUMBUFLEN];
10659 char_u **curval;
10660 char_u **curallocval;
10661 list_T *ll = argvars[1].vval.v_list;
10662 listitem_T *li;
10663 int len;
10664
10665 /* If the list is NULL handle like an empty list. */
10666 len = ll == NULL ? 0 : ll->lv_len;
10667
10668 /* First half: use for pointers to result lines; second half: use for
10669 * pointers to allocated copies. */
10670 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10671 if (lstval == NULL)
10672 return;
10673 curval = lstval;
10674 allocval = lstval + len + 2;
10675 curallocval = allocval;
10676
10677 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10678 li = li->li_next)
10679 {
10680 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10681 if (strval == NULL)
10682 goto free_lstval;
10683 if (strval == buf)
10684 {
10685 /* Need to make a copy, next get_tv_string_buf_chk() will
10686 * overwrite the string. */
10687 strval = vim_strsave(buf);
10688 if (strval == NULL)
10689 goto free_lstval;
10690 *curallocval++ = strval;
10691 }
10692 *curval++ = strval;
10693 }
10694 *curval++ = NULL;
10695
10696 write_reg_contents_lst(regname, lstval, -1,
10697 append, yank_type, block_len);
10698free_lstval:
10699 while (curallocval > allocval)
10700 vim_free(*--curallocval);
10701 vim_free(lstval);
10702 }
10703 else
10704 {
10705 strval = get_tv_string_chk(&argvars[1]);
10706 if (strval == NULL)
10707 return;
10708 write_reg_contents_ex(regname, strval, -1,
10709 append, yank_type, block_len);
10710 }
10711 rettv->vval.v_number = 0;
10712}
10713
10714/*
10715 * "settabvar()" function
10716 */
10717 static void
10718f_settabvar(typval_T *argvars, typval_T *rettv)
10719{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010720 tabpage_T *save_curtab;
10721 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010722 char_u *varname, *tabvarname;
10723 typval_T *varp;
10724
10725 rettv->vval.v_number = 0;
10726
10727 if (check_restricted() || check_secure())
10728 return;
10729
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010730 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010731 varname = get_tv_string_chk(&argvars[1]);
10732 varp = &argvars[2];
10733
Bram Moolenaar4033c552017-09-16 20:54:51 +020010734 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010735 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010736 save_curtab = curtab;
10737 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010738
10739 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10740 if (tabvarname != NULL)
10741 {
10742 STRCPY(tabvarname, "t:");
10743 STRCPY(tabvarname + 2, varname);
10744 set_var(tabvarname, varp, TRUE);
10745 vim_free(tabvarname);
10746 }
10747
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010748 /* Restore current tabpage */
10749 if (valid_tabpage(save_curtab))
10750 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010751 }
10752}
10753
10754/*
10755 * "settabwinvar()" function
10756 */
10757 static void
10758f_settabwinvar(typval_T *argvars, typval_T *rettv)
10759{
10760 setwinvar(argvars, rettv, 1);
10761}
10762
10763/*
10764 * "setwinvar()" function
10765 */
10766 static void
10767f_setwinvar(typval_T *argvars, typval_T *rettv)
10768{
10769 setwinvar(argvars, rettv, 0);
10770}
10771
10772#ifdef FEAT_CRYPT
10773/*
10774 * "sha256({string})" function
10775 */
10776 static void
10777f_sha256(typval_T *argvars, typval_T *rettv)
10778{
10779 char_u *p;
10780
10781 p = get_tv_string(&argvars[0]);
10782 rettv->vval.v_string = vim_strsave(
10783 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10784 rettv->v_type = VAR_STRING;
10785}
10786#endif /* FEAT_CRYPT */
10787
10788/*
10789 * "shellescape({string})" function
10790 */
10791 static void
10792f_shellescape(typval_T *argvars, typval_T *rettv)
10793{
Bram Moolenaar20615522017-06-05 18:46:26 +020010794 int do_special = non_zero_arg(&argvars[1]);
10795
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010796 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010797 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010798 rettv->v_type = VAR_STRING;
10799}
10800
10801/*
10802 * shiftwidth() function
10803 */
10804 static void
10805f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10806{
10807 rettv->vval.v_number = get_sw_value(curbuf);
10808}
10809
10810/*
10811 * "simplify()" function
10812 */
10813 static void
10814f_simplify(typval_T *argvars, typval_T *rettv)
10815{
10816 char_u *p;
10817
10818 p = get_tv_string(&argvars[0]);
10819 rettv->vval.v_string = vim_strsave(p);
10820 simplify_filename(rettv->vval.v_string); /* simplify in place */
10821 rettv->v_type = VAR_STRING;
10822}
10823
10824#ifdef FEAT_FLOAT
10825/*
10826 * "sin()" function
10827 */
10828 static void
10829f_sin(typval_T *argvars, typval_T *rettv)
10830{
10831 float_T f = 0.0;
10832
10833 rettv->v_type = VAR_FLOAT;
10834 if (get_float_arg(argvars, &f) == OK)
10835 rettv->vval.v_float = sin(f);
10836 else
10837 rettv->vval.v_float = 0.0;
10838}
10839
10840/*
10841 * "sinh()" function
10842 */
10843 static void
10844f_sinh(typval_T *argvars, typval_T *rettv)
10845{
10846 float_T f = 0.0;
10847
10848 rettv->v_type = VAR_FLOAT;
10849 if (get_float_arg(argvars, &f) == OK)
10850 rettv->vval.v_float = sinh(f);
10851 else
10852 rettv->vval.v_float = 0.0;
10853}
10854#endif
10855
10856static int
10857#ifdef __BORLANDC__
10858 _RTLENTRYF
10859#endif
10860 item_compare(const void *s1, const void *s2);
10861static int
10862#ifdef __BORLANDC__
10863 _RTLENTRYF
10864#endif
10865 item_compare2(const void *s1, const void *s2);
10866
10867/* struct used in the array that's given to qsort() */
10868typedef struct
10869{
10870 listitem_T *item;
10871 int idx;
10872} sortItem_T;
10873
10874/* struct storing information about current sort */
10875typedef struct
10876{
10877 int item_compare_ic;
10878 int item_compare_numeric;
10879 int item_compare_numbers;
10880#ifdef FEAT_FLOAT
10881 int item_compare_float;
10882#endif
10883 char_u *item_compare_func;
10884 partial_T *item_compare_partial;
10885 dict_T *item_compare_selfdict;
10886 int item_compare_func_err;
10887 int item_compare_keep_zero;
10888} sortinfo_T;
10889static sortinfo_T *sortinfo = NULL;
10890static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10891#define ITEM_COMPARE_FAIL 999
10892
10893/*
10894 * Compare functions for f_sort() and f_uniq() below.
10895 */
10896 static int
10897#ifdef __BORLANDC__
10898_RTLENTRYF
10899#endif
10900item_compare(const void *s1, const void *s2)
10901{
10902 sortItem_T *si1, *si2;
10903 typval_T *tv1, *tv2;
10904 char_u *p1, *p2;
10905 char_u *tofree1 = NULL, *tofree2 = NULL;
10906 int res;
10907 char_u numbuf1[NUMBUFLEN];
10908 char_u numbuf2[NUMBUFLEN];
10909
10910 si1 = (sortItem_T *)s1;
10911 si2 = (sortItem_T *)s2;
10912 tv1 = &si1->item->li_tv;
10913 tv2 = &si2->item->li_tv;
10914
10915 if (sortinfo->item_compare_numbers)
10916 {
10917 varnumber_T v1 = get_tv_number(tv1);
10918 varnumber_T v2 = get_tv_number(tv2);
10919
10920 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10921 }
10922
10923#ifdef FEAT_FLOAT
10924 if (sortinfo->item_compare_float)
10925 {
10926 float_T v1 = get_tv_float(tv1);
10927 float_T v2 = get_tv_float(tv2);
10928
10929 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10930 }
10931#endif
10932
10933 /* tv2string() puts quotes around a string and allocates memory. Don't do
10934 * that for string variables. Use a single quote when comparing with a
10935 * non-string to do what the docs promise. */
10936 if (tv1->v_type == VAR_STRING)
10937 {
10938 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10939 p1 = (char_u *)"'";
10940 else
10941 p1 = tv1->vval.v_string;
10942 }
10943 else
10944 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10945 if (tv2->v_type == VAR_STRING)
10946 {
10947 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10948 p2 = (char_u *)"'";
10949 else
10950 p2 = tv2->vval.v_string;
10951 }
10952 else
10953 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10954 if (p1 == NULL)
10955 p1 = (char_u *)"";
10956 if (p2 == NULL)
10957 p2 = (char_u *)"";
10958 if (!sortinfo->item_compare_numeric)
10959 {
10960 if (sortinfo->item_compare_ic)
10961 res = STRICMP(p1, p2);
10962 else
10963 res = STRCMP(p1, p2);
10964 }
10965 else
10966 {
10967 double n1, n2;
10968 n1 = strtod((char *)p1, (char **)&p1);
10969 n2 = strtod((char *)p2, (char **)&p2);
10970 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10971 }
10972
10973 /* When the result would be zero, compare the item indexes. Makes the
10974 * sort stable. */
10975 if (res == 0 && !sortinfo->item_compare_keep_zero)
10976 res = si1->idx > si2->idx ? 1 : -1;
10977
10978 vim_free(tofree1);
10979 vim_free(tofree2);
10980 return res;
10981}
10982
10983 static int
10984#ifdef __BORLANDC__
10985_RTLENTRYF
10986#endif
10987item_compare2(const void *s1, const void *s2)
10988{
10989 sortItem_T *si1, *si2;
10990 int res;
10991 typval_T rettv;
10992 typval_T argv[3];
10993 int dummy;
10994 char_u *func_name;
10995 partial_T *partial = sortinfo->item_compare_partial;
10996
10997 /* shortcut after failure in previous call; compare all items equal */
10998 if (sortinfo->item_compare_func_err)
10999 return 0;
11000
11001 si1 = (sortItem_T *)s1;
11002 si2 = (sortItem_T *)s2;
11003
11004 if (partial == NULL)
11005 func_name = sortinfo->item_compare_func;
11006 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011007 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011008
11009 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11010 * in the copy without changing the original list items. */
11011 copy_tv(&si1->item->li_tv, &argv[0]);
11012 copy_tv(&si2->item->li_tv, &argv[1]);
11013
11014 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11015 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011016 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011017 partial, sortinfo->item_compare_selfdict);
11018 clear_tv(&argv[0]);
11019 clear_tv(&argv[1]);
11020
11021 if (res == FAIL)
11022 res = ITEM_COMPARE_FAIL;
11023 else
11024 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11025 if (sortinfo->item_compare_func_err)
11026 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11027 clear_tv(&rettv);
11028
11029 /* When the result would be zero, compare the pointers themselves. Makes
11030 * the sort stable. */
11031 if (res == 0 && !sortinfo->item_compare_keep_zero)
11032 res = si1->idx > si2->idx ? 1 : -1;
11033
11034 return res;
11035}
11036
11037/*
11038 * "sort({list})" function
11039 */
11040 static void
11041do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11042{
11043 list_T *l;
11044 listitem_T *li;
11045 sortItem_T *ptrs;
11046 sortinfo_T *old_sortinfo;
11047 sortinfo_T info;
11048 long len;
11049 long i;
11050
11051 /* Pointer to current info struct used in compare function. Save and
11052 * restore the current one for nested calls. */
11053 old_sortinfo = sortinfo;
11054 sortinfo = &info;
11055
11056 if (argvars[0].v_type != VAR_LIST)
11057 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11058 else
11059 {
11060 l = argvars[0].vval.v_list;
11061 if (l == NULL || tv_check_lock(l->lv_lock,
11062 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11063 TRUE))
11064 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011065 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011066
11067 len = list_len(l);
11068 if (len <= 1)
11069 goto theend; /* short list sorts pretty quickly */
11070
11071 info.item_compare_ic = FALSE;
11072 info.item_compare_numeric = FALSE;
11073 info.item_compare_numbers = FALSE;
11074#ifdef FEAT_FLOAT
11075 info.item_compare_float = FALSE;
11076#endif
11077 info.item_compare_func = NULL;
11078 info.item_compare_partial = NULL;
11079 info.item_compare_selfdict = NULL;
11080 if (argvars[1].v_type != VAR_UNKNOWN)
11081 {
11082 /* optional second argument: {func} */
11083 if (argvars[1].v_type == VAR_FUNC)
11084 info.item_compare_func = argvars[1].vval.v_string;
11085 else if (argvars[1].v_type == VAR_PARTIAL)
11086 info.item_compare_partial = argvars[1].vval.v_partial;
11087 else
11088 {
11089 int error = FALSE;
11090
11091 i = (long)get_tv_number_chk(&argvars[1], &error);
11092 if (error)
11093 goto theend; /* type error; errmsg already given */
11094 if (i == 1)
11095 info.item_compare_ic = TRUE;
11096 else if (argvars[1].v_type != VAR_NUMBER)
11097 info.item_compare_func = get_tv_string(&argvars[1]);
11098 else if (i != 0)
11099 {
11100 EMSG(_(e_invarg));
11101 goto theend;
11102 }
11103 if (info.item_compare_func != NULL)
11104 {
11105 if (*info.item_compare_func == NUL)
11106 {
11107 /* empty string means default sort */
11108 info.item_compare_func = NULL;
11109 }
11110 else if (STRCMP(info.item_compare_func, "n") == 0)
11111 {
11112 info.item_compare_func = NULL;
11113 info.item_compare_numeric = TRUE;
11114 }
11115 else if (STRCMP(info.item_compare_func, "N") == 0)
11116 {
11117 info.item_compare_func = NULL;
11118 info.item_compare_numbers = TRUE;
11119 }
11120#ifdef FEAT_FLOAT
11121 else if (STRCMP(info.item_compare_func, "f") == 0)
11122 {
11123 info.item_compare_func = NULL;
11124 info.item_compare_float = TRUE;
11125 }
11126#endif
11127 else if (STRCMP(info.item_compare_func, "i") == 0)
11128 {
11129 info.item_compare_func = NULL;
11130 info.item_compare_ic = TRUE;
11131 }
11132 }
11133 }
11134
11135 if (argvars[2].v_type != VAR_UNKNOWN)
11136 {
11137 /* optional third argument: {dict} */
11138 if (argvars[2].v_type != VAR_DICT)
11139 {
11140 EMSG(_(e_dictreq));
11141 goto theend;
11142 }
11143 info.item_compare_selfdict = argvars[2].vval.v_dict;
11144 }
11145 }
11146
11147 /* Make an array with each entry pointing to an item in the List. */
11148 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11149 if (ptrs == NULL)
11150 goto theend;
11151
11152 i = 0;
11153 if (sort)
11154 {
11155 /* sort(): ptrs will be the list to sort */
11156 for (li = l->lv_first; li != NULL; li = li->li_next)
11157 {
11158 ptrs[i].item = li;
11159 ptrs[i].idx = i;
11160 ++i;
11161 }
11162
11163 info.item_compare_func_err = FALSE;
11164 info.item_compare_keep_zero = FALSE;
11165 /* test the compare function */
11166 if ((info.item_compare_func != NULL
11167 || info.item_compare_partial != NULL)
11168 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11169 == ITEM_COMPARE_FAIL)
11170 EMSG(_("E702: Sort compare function failed"));
11171 else
11172 {
11173 /* Sort the array with item pointers. */
11174 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11175 info.item_compare_func == NULL
11176 && info.item_compare_partial == NULL
11177 ? item_compare : item_compare2);
11178
11179 if (!info.item_compare_func_err)
11180 {
11181 /* Clear the List and append the items in sorted order. */
11182 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11183 l->lv_len = 0;
11184 for (i = 0; i < len; ++i)
11185 list_append(l, ptrs[i].item);
11186 }
11187 }
11188 }
11189 else
11190 {
11191 int (*item_compare_func_ptr)(const void *, const void *);
11192
11193 /* f_uniq(): ptrs will be a stack of items to remove */
11194 info.item_compare_func_err = FALSE;
11195 info.item_compare_keep_zero = TRUE;
11196 item_compare_func_ptr = info.item_compare_func != NULL
11197 || info.item_compare_partial != NULL
11198 ? item_compare2 : item_compare;
11199
11200 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11201 li = li->li_next)
11202 {
11203 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11204 == 0)
11205 ptrs[i++].item = li;
11206 if (info.item_compare_func_err)
11207 {
11208 EMSG(_("E882: Uniq compare function failed"));
11209 break;
11210 }
11211 }
11212
11213 if (!info.item_compare_func_err)
11214 {
11215 while (--i >= 0)
11216 {
11217 li = ptrs[i].item->li_next;
11218 ptrs[i].item->li_next = li->li_next;
11219 if (li->li_next != NULL)
11220 li->li_next->li_prev = ptrs[i].item;
11221 else
11222 l->lv_last = ptrs[i].item;
11223 list_fix_watch(l, li);
11224 listitem_free(li);
11225 l->lv_len--;
11226 }
11227 }
11228 }
11229
11230 vim_free(ptrs);
11231 }
11232theend:
11233 sortinfo = old_sortinfo;
11234}
11235
11236/*
11237 * "sort({list})" function
11238 */
11239 static void
11240f_sort(typval_T *argvars, typval_T *rettv)
11241{
11242 do_sort_uniq(argvars, rettv, TRUE);
11243}
11244
11245/*
11246 * "uniq({list})" function
11247 */
11248 static void
11249f_uniq(typval_T *argvars, typval_T *rettv)
11250{
11251 do_sort_uniq(argvars, rettv, FALSE);
11252}
11253
11254/*
11255 * "soundfold({word})" function
11256 */
11257 static void
11258f_soundfold(typval_T *argvars, typval_T *rettv)
11259{
11260 char_u *s;
11261
11262 rettv->v_type = VAR_STRING;
11263 s = get_tv_string(&argvars[0]);
11264#ifdef FEAT_SPELL
11265 rettv->vval.v_string = eval_soundfold(s);
11266#else
11267 rettv->vval.v_string = vim_strsave(s);
11268#endif
11269}
11270
11271/*
11272 * "spellbadword()" function
11273 */
11274 static void
11275f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11276{
11277 char_u *word = (char_u *)"";
11278 hlf_T attr = HLF_COUNT;
11279 int len = 0;
11280
11281 if (rettv_list_alloc(rettv) == FAIL)
11282 return;
11283
11284#ifdef FEAT_SPELL
11285 if (argvars[0].v_type == VAR_UNKNOWN)
11286 {
11287 /* Find the start and length of the badly spelled word. */
11288 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11289 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011290 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011291 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011292 curwin->w_set_curswant = TRUE;
11293 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011294 }
11295 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11296 {
11297 char_u *str = get_tv_string_chk(&argvars[0]);
11298 int capcol = -1;
11299
11300 if (str != NULL)
11301 {
11302 /* Check the argument for spelling. */
11303 while (*str != NUL)
11304 {
11305 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11306 if (attr != HLF_COUNT)
11307 {
11308 word = str;
11309 break;
11310 }
11311 str += len;
11312 }
11313 }
11314 }
11315#endif
11316
11317 list_append_string(rettv->vval.v_list, word, len);
11318 list_append_string(rettv->vval.v_list, (char_u *)(
11319 attr == HLF_SPB ? "bad" :
11320 attr == HLF_SPR ? "rare" :
11321 attr == HLF_SPL ? "local" :
11322 attr == HLF_SPC ? "caps" :
11323 ""), -1);
11324}
11325
11326/*
11327 * "spellsuggest()" function
11328 */
11329 static void
11330f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11331{
11332#ifdef FEAT_SPELL
11333 char_u *str;
11334 int typeerr = FALSE;
11335 int maxcount;
11336 garray_T ga;
11337 int i;
11338 listitem_T *li;
11339 int need_capital = FALSE;
11340#endif
11341
11342 if (rettv_list_alloc(rettv) == FAIL)
11343 return;
11344
11345#ifdef FEAT_SPELL
11346 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11347 {
11348 str = get_tv_string(&argvars[0]);
11349 if (argvars[1].v_type != VAR_UNKNOWN)
11350 {
11351 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11352 if (maxcount <= 0)
11353 return;
11354 if (argvars[2].v_type != VAR_UNKNOWN)
11355 {
11356 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11357 if (typeerr)
11358 return;
11359 }
11360 }
11361 else
11362 maxcount = 25;
11363
11364 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11365
11366 for (i = 0; i < ga.ga_len; ++i)
11367 {
11368 str = ((char_u **)ga.ga_data)[i];
11369
11370 li = listitem_alloc();
11371 if (li == NULL)
11372 vim_free(str);
11373 else
11374 {
11375 li->li_tv.v_type = VAR_STRING;
11376 li->li_tv.v_lock = 0;
11377 li->li_tv.vval.v_string = str;
11378 list_append(rettv->vval.v_list, li);
11379 }
11380 }
11381 ga_clear(&ga);
11382 }
11383#endif
11384}
11385
11386 static void
11387f_split(typval_T *argvars, typval_T *rettv)
11388{
11389 char_u *str;
11390 char_u *end;
11391 char_u *pat = NULL;
11392 regmatch_T regmatch;
11393 char_u patbuf[NUMBUFLEN];
11394 char_u *save_cpo;
11395 int match;
11396 colnr_T col = 0;
11397 int keepempty = FALSE;
11398 int typeerr = FALSE;
11399
11400 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11401 save_cpo = p_cpo;
11402 p_cpo = (char_u *)"";
11403
11404 str = get_tv_string(&argvars[0]);
11405 if (argvars[1].v_type != VAR_UNKNOWN)
11406 {
11407 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11408 if (pat == NULL)
11409 typeerr = TRUE;
11410 if (argvars[2].v_type != VAR_UNKNOWN)
11411 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11412 }
11413 if (pat == NULL || *pat == NUL)
11414 pat = (char_u *)"[\\x01- ]\\+";
11415
11416 if (rettv_list_alloc(rettv) == FAIL)
11417 return;
11418 if (typeerr)
11419 return;
11420
11421 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11422 if (regmatch.regprog != NULL)
11423 {
11424 regmatch.rm_ic = FALSE;
11425 while (*str != NUL || keepempty)
11426 {
11427 if (*str == NUL)
11428 match = FALSE; /* empty item at the end */
11429 else
11430 match = vim_regexec_nl(&regmatch, str, col);
11431 if (match)
11432 end = regmatch.startp[0];
11433 else
11434 end = str + STRLEN(str);
11435 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11436 && *str != NUL && match && end < regmatch.endp[0]))
11437 {
11438 if (list_append_string(rettv->vval.v_list, str,
11439 (int)(end - str)) == FAIL)
11440 break;
11441 }
11442 if (!match)
11443 break;
11444 /* Advance to just after the match. */
11445 if (regmatch.endp[0] > str)
11446 col = 0;
11447 else
11448 {
11449 /* Don't get stuck at the same match. */
11450#ifdef FEAT_MBYTE
11451 col = (*mb_ptr2len)(regmatch.endp[0]);
11452#else
11453 col = 1;
11454#endif
11455 }
11456 str = regmatch.endp[0];
11457 }
11458
11459 vim_regfree(regmatch.regprog);
11460 }
11461
11462 p_cpo = save_cpo;
11463}
11464
11465#ifdef FEAT_FLOAT
11466/*
11467 * "sqrt()" function
11468 */
11469 static void
11470f_sqrt(typval_T *argvars, typval_T *rettv)
11471{
11472 float_T f = 0.0;
11473
11474 rettv->v_type = VAR_FLOAT;
11475 if (get_float_arg(argvars, &f) == OK)
11476 rettv->vval.v_float = sqrt(f);
11477 else
11478 rettv->vval.v_float = 0.0;
11479}
11480
11481/*
11482 * "str2float()" function
11483 */
11484 static void
11485f_str2float(typval_T *argvars, typval_T *rettv)
11486{
11487 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011488 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011489
Bram Moolenaar08243d22017-01-10 16:12:29 +010011490 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011491 p = skipwhite(p + 1);
11492 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011493 if (isneg)
11494 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011495 rettv->v_type = VAR_FLOAT;
11496}
11497#endif
11498
11499/*
11500 * "str2nr()" function
11501 */
11502 static void
11503f_str2nr(typval_T *argvars, typval_T *rettv)
11504{
11505 int base = 10;
11506 char_u *p;
11507 varnumber_T n;
11508 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011509 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011510
11511 if (argvars[1].v_type != VAR_UNKNOWN)
11512 {
11513 base = (int)get_tv_number(&argvars[1]);
11514 if (base != 2 && base != 8 && base != 10 && base != 16)
11515 {
11516 EMSG(_(e_invarg));
11517 return;
11518 }
11519 }
11520
11521 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011522 isneg = (*p == '-');
11523 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011524 p = skipwhite(p + 1);
11525 switch (base)
11526 {
11527 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11528 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11529 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11530 default: what = 0;
11531 }
11532 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011533 if (isneg)
11534 rettv->vval.v_number = -n;
11535 else
11536 rettv->vval.v_number = n;
11537
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011538}
11539
11540#ifdef HAVE_STRFTIME
11541/*
11542 * "strftime({format}[, {time}])" function
11543 */
11544 static void
11545f_strftime(typval_T *argvars, typval_T *rettv)
11546{
11547 char_u result_buf[256];
11548 struct tm *curtime;
11549 time_t seconds;
11550 char_u *p;
11551
11552 rettv->v_type = VAR_STRING;
11553
11554 p = get_tv_string(&argvars[0]);
11555 if (argvars[1].v_type == VAR_UNKNOWN)
11556 seconds = time(NULL);
11557 else
11558 seconds = (time_t)get_tv_number(&argvars[1]);
11559 curtime = localtime(&seconds);
11560 /* MSVC returns NULL for an invalid value of seconds. */
11561 if (curtime == NULL)
11562 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11563 else
11564 {
11565# ifdef FEAT_MBYTE
11566 vimconv_T conv;
11567 char_u *enc;
11568
11569 conv.vc_type = CONV_NONE;
11570 enc = enc_locale();
11571 convert_setup(&conv, p_enc, enc);
11572 if (conv.vc_type != CONV_NONE)
11573 p = string_convert(&conv, p, NULL);
11574# endif
11575 if (p != NULL)
11576 (void)strftime((char *)result_buf, sizeof(result_buf),
11577 (char *)p, curtime);
11578 else
11579 result_buf[0] = NUL;
11580
11581# ifdef FEAT_MBYTE
11582 if (conv.vc_type != CONV_NONE)
11583 vim_free(p);
11584 convert_setup(&conv, enc, p_enc);
11585 if (conv.vc_type != CONV_NONE)
11586 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11587 else
11588# endif
11589 rettv->vval.v_string = vim_strsave(result_buf);
11590
11591# ifdef FEAT_MBYTE
11592 /* Release conversion descriptors */
11593 convert_setup(&conv, NULL, NULL);
11594 vim_free(enc);
11595# endif
11596 }
11597}
11598#endif
11599
11600/*
11601 * "strgetchar()" function
11602 */
11603 static void
11604f_strgetchar(typval_T *argvars, typval_T *rettv)
11605{
11606 char_u *str;
11607 int len;
11608 int error = FALSE;
11609 int charidx;
11610
11611 rettv->vval.v_number = -1;
11612 str = get_tv_string_chk(&argvars[0]);
11613 if (str == NULL)
11614 return;
11615 len = (int)STRLEN(str);
11616 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11617 if (error)
11618 return;
11619#ifdef FEAT_MBYTE
11620 {
11621 int byteidx = 0;
11622
11623 while (charidx >= 0 && byteidx < len)
11624 {
11625 if (charidx == 0)
11626 {
11627 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11628 break;
11629 }
11630 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011631 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011632 }
11633 }
11634#else
11635 if (charidx < len)
11636 rettv->vval.v_number = str[charidx];
11637#endif
11638}
11639
11640/*
11641 * "stridx()" function
11642 */
11643 static void
11644f_stridx(typval_T *argvars, typval_T *rettv)
11645{
11646 char_u buf[NUMBUFLEN];
11647 char_u *needle;
11648 char_u *haystack;
11649 char_u *save_haystack;
11650 char_u *pos;
11651 int start_idx;
11652
11653 needle = get_tv_string_chk(&argvars[1]);
11654 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11655 rettv->vval.v_number = -1;
11656 if (needle == NULL || haystack == NULL)
11657 return; /* type error; errmsg already given */
11658
11659 if (argvars[2].v_type != VAR_UNKNOWN)
11660 {
11661 int error = FALSE;
11662
11663 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11664 if (error || start_idx >= (int)STRLEN(haystack))
11665 return;
11666 if (start_idx >= 0)
11667 haystack += start_idx;
11668 }
11669
11670 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11671 if (pos != NULL)
11672 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11673}
11674
11675/*
11676 * "string()" function
11677 */
11678 static void
11679f_string(typval_T *argvars, typval_T *rettv)
11680{
11681 char_u *tofree;
11682 char_u numbuf[NUMBUFLEN];
11683
11684 rettv->v_type = VAR_STRING;
11685 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11686 get_copyID());
11687 /* Make a copy if we have a value but it's not in allocated memory. */
11688 if (rettv->vval.v_string != NULL && tofree == NULL)
11689 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11690}
11691
11692/*
11693 * "strlen()" function
11694 */
11695 static void
11696f_strlen(typval_T *argvars, typval_T *rettv)
11697{
11698 rettv->vval.v_number = (varnumber_T)(STRLEN(
11699 get_tv_string(&argvars[0])));
11700}
11701
11702/*
11703 * "strchars()" function
11704 */
11705 static void
11706f_strchars(typval_T *argvars, typval_T *rettv)
11707{
11708 char_u *s = get_tv_string(&argvars[0]);
11709 int skipcc = 0;
11710#ifdef FEAT_MBYTE
11711 varnumber_T len = 0;
11712 int (*func_mb_ptr2char_adv)(char_u **pp);
11713#endif
11714
11715 if (argvars[1].v_type != VAR_UNKNOWN)
11716 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11717 if (skipcc < 0 || skipcc > 1)
11718 EMSG(_(e_invarg));
11719 else
11720 {
11721#ifdef FEAT_MBYTE
11722 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11723 while (*s != NUL)
11724 {
11725 func_mb_ptr2char_adv(&s);
11726 ++len;
11727 }
11728 rettv->vval.v_number = len;
11729#else
11730 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11731#endif
11732 }
11733}
11734
11735/*
11736 * "strdisplaywidth()" function
11737 */
11738 static void
11739f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11740{
11741 char_u *s = get_tv_string(&argvars[0]);
11742 int col = 0;
11743
11744 if (argvars[1].v_type != VAR_UNKNOWN)
11745 col = (int)get_tv_number(&argvars[1]);
11746
11747 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11748}
11749
11750/*
11751 * "strwidth()" function
11752 */
11753 static void
11754f_strwidth(typval_T *argvars, typval_T *rettv)
11755{
11756 char_u *s = get_tv_string(&argvars[0]);
11757
11758 rettv->vval.v_number = (varnumber_T)(
11759#ifdef FEAT_MBYTE
11760 mb_string2cells(s, -1)
11761#else
11762 STRLEN(s)
11763#endif
11764 );
11765}
11766
11767/*
11768 * "strcharpart()" function
11769 */
11770 static void
11771f_strcharpart(typval_T *argvars, typval_T *rettv)
11772{
11773#ifdef FEAT_MBYTE
11774 char_u *p;
11775 int nchar;
11776 int nbyte = 0;
11777 int charlen;
11778 int len = 0;
11779 int slen;
11780 int error = FALSE;
11781
11782 p = get_tv_string(&argvars[0]);
11783 slen = (int)STRLEN(p);
11784
11785 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11786 if (!error)
11787 {
11788 if (nchar > 0)
11789 while (nchar > 0 && nbyte < slen)
11790 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011791 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011792 --nchar;
11793 }
11794 else
11795 nbyte = nchar;
11796 if (argvars[2].v_type != VAR_UNKNOWN)
11797 {
11798 charlen = (int)get_tv_number(&argvars[2]);
11799 while (charlen > 0 && nbyte + len < slen)
11800 {
11801 int off = nbyte + len;
11802
11803 if (off < 0)
11804 len += 1;
11805 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011806 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011807 --charlen;
11808 }
11809 }
11810 else
11811 len = slen - nbyte; /* default: all bytes that are available. */
11812 }
11813
11814 /*
11815 * Only return the overlap between the specified part and the actual
11816 * string.
11817 */
11818 if (nbyte < 0)
11819 {
11820 len += nbyte;
11821 nbyte = 0;
11822 }
11823 else if (nbyte > slen)
11824 nbyte = slen;
11825 if (len < 0)
11826 len = 0;
11827 else if (nbyte + len > slen)
11828 len = slen - nbyte;
11829
11830 rettv->v_type = VAR_STRING;
11831 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11832#else
11833 f_strpart(argvars, rettv);
11834#endif
11835}
11836
11837/*
11838 * "strpart()" function
11839 */
11840 static void
11841f_strpart(typval_T *argvars, typval_T *rettv)
11842{
11843 char_u *p;
11844 int n;
11845 int len;
11846 int slen;
11847 int error = FALSE;
11848
11849 p = get_tv_string(&argvars[0]);
11850 slen = (int)STRLEN(p);
11851
11852 n = (int)get_tv_number_chk(&argvars[1], &error);
11853 if (error)
11854 len = 0;
11855 else if (argvars[2].v_type != VAR_UNKNOWN)
11856 len = (int)get_tv_number(&argvars[2]);
11857 else
11858 len = slen - n; /* default len: all bytes that are available. */
11859
11860 /*
11861 * Only return the overlap between the specified part and the actual
11862 * string.
11863 */
11864 if (n < 0)
11865 {
11866 len += n;
11867 n = 0;
11868 }
11869 else if (n > slen)
11870 n = slen;
11871 if (len < 0)
11872 len = 0;
11873 else if (n + len > slen)
11874 len = slen - n;
11875
11876 rettv->v_type = VAR_STRING;
11877 rettv->vval.v_string = vim_strnsave(p + n, len);
11878}
11879
11880/*
11881 * "strridx()" function
11882 */
11883 static void
11884f_strridx(typval_T *argvars, typval_T *rettv)
11885{
11886 char_u buf[NUMBUFLEN];
11887 char_u *needle;
11888 char_u *haystack;
11889 char_u *rest;
11890 char_u *lastmatch = NULL;
11891 int haystack_len, end_idx;
11892
11893 needle = get_tv_string_chk(&argvars[1]);
11894 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11895
11896 rettv->vval.v_number = -1;
11897 if (needle == NULL || haystack == NULL)
11898 return; /* type error; errmsg already given */
11899
11900 haystack_len = (int)STRLEN(haystack);
11901 if (argvars[2].v_type != VAR_UNKNOWN)
11902 {
11903 /* Third argument: upper limit for index */
11904 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11905 if (end_idx < 0)
11906 return; /* can never find a match */
11907 }
11908 else
11909 end_idx = haystack_len;
11910
11911 if (*needle == NUL)
11912 {
11913 /* Empty string matches past the end. */
11914 lastmatch = haystack + end_idx;
11915 }
11916 else
11917 {
11918 for (rest = haystack; *rest != '\0'; ++rest)
11919 {
11920 rest = (char_u *)strstr((char *)rest, (char *)needle);
11921 if (rest == NULL || rest > haystack + end_idx)
11922 break;
11923 lastmatch = rest;
11924 }
11925 }
11926
11927 if (lastmatch == NULL)
11928 rettv->vval.v_number = -1;
11929 else
11930 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11931}
11932
11933/*
11934 * "strtrans()" function
11935 */
11936 static void
11937f_strtrans(typval_T *argvars, typval_T *rettv)
11938{
11939 rettv->v_type = VAR_STRING;
11940 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11941}
11942
11943/*
11944 * "submatch()" function
11945 */
11946 static void
11947f_submatch(typval_T *argvars, typval_T *rettv)
11948{
11949 int error = FALSE;
11950 int no;
11951 int retList = 0;
11952
11953 no = (int)get_tv_number_chk(&argvars[0], &error);
11954 if (error)
11955 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011956 if (no < 0 || no >= NSUBEXP)
11957 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010011958 EMSGN(_("E935: invalid submatch number: %d"), no);
11959 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011960 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011961 if (argvars[1].v_type != VAR_UNKNOWN)
11962 retList = (int)get_tv_number_chk(&argvars[1], &error);
11963 if (error)
11964 return;
11965
11966 if (retList == 0)
11967 {
11968 rettv->v_type = VAR_STRING;
11969 rettv->vval.v_string = reg_submatch(no);
11970 }
11971 else
11972 {
11973 rettv->v_type = VAR_LIST;
11974 rettv->vval.v_list = reg_submatch_list(no);
11975 }
11976}
11977
11978/*
11979 * "substitute()" function
11980 */
11981 static void
11982f_substitute(typval_T *argvars, typval_T *rettv)
11983{
11984 char_u patbuf[NUMBUFLEN];
11985 char_u subbuf[NUMBUFLEN];
11986 char_u flagsbuf[NUMBUFLEN];
11987
11988 char_u *str = get_tv_string_chk(&argvars[0]);
11989 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011990 char_u *sub = NULL;
11991 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011992 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11993
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011994 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11995 expr = &argvars[2];
11996 else
11997 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11998
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011999 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012000 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12001 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012002 rettv->vval.v_string = NULL;
12003 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012004 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012005}
12006
12007/*
12008 * "synID(lnum, col, trans)" function
12009 */
12010 static void
12011f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12012{
12013 int id = 0;
12014#ifdef FEAT_SYN_HL
12015 linenr_T lnum;
12016 colnr_T col;
12017 int trans;
12018 int transerr = FALSE;
12019
12020 lnum = get_tv_lnum(argvars); /* -1 on type error */
12021 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12022 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12023
12024 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12025 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12026 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12027#endif
12028
12029 rettv->vval.v_number = id;
12030}
12031
12032/*
12033 * "synIDattr(id, what [, mode])" function
12034 */
12035 static void
12036f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12037{
12038 char_u *p = NULL;
12039#ifdef FEAT_SYN_HL
12040 int id;
12041 char_u *what;
12042 char_u *mode;
12043 char_u modebuf[NUMBUFLEN];
12044 int modec;
12045
12046 id = (int)get_tv_number(&argvars[0]);
12047 what = get_tv_string(&argvars[1]);
12048 if (argvars[2].v_type != VAR_UNKNOWN)
12049 {
12050 mode = get_tv_string_buf(&argvars[2], modebuf);
12051 modec = TOLOWER_ASC(mode[0]);
12052 if (modec != 't' && modec != 'c' && modec != 'g')
12053 modec = 0; /* replace invalid with current */
12054 }
12055 else
12056 {
12057#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12058 if (USE_24BIT)
12059 modec = 'g';
12060 else
12061#endif
12062 if (t_colors > 1)
12063 modec = 'c';
12064 else
12065 modec = 't';
12066 }
12067
12068
12069 switch (TOLOWER_ASC(what[0]))
12070 {
12071 case 'b':
12072 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12073 p = highlight_color(id, what, modec);
12074 else /* bold */
12075 p = highlight_has_attr(id, HL_BOLD, modec);
12076 break;
12077
12078 case 'f': /* fg[#] or font */
12079 p = highlight_color(id, what, modec);
12080 break;
12081
12082 case 'i':
12083 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12084 p = highlight_has_attr(id, HL_INVERSE, modec);
12085 else /* italic */
12086 p = highlight_has_attr(id, HL_ITALIC, modec);
12087 break;
12088
12089 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012090 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012091 break;
12092
12093 case 'r': /* reverse */
12094 p = highlight_has_attr(id, HL_INVERSE, modec);
12095 break;
12096
12097 case 's':
12098 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12099 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012100 /* strikeout */
12101 else if (TOLOWER_ASC(what[1]) == 't' &&
12102 TOLOWER_ASC(what[2]) == 'r')
12103 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012104 else /* standout */
12105 p = highlight_has_attr(id, HL_STANDOUT, modec);
12106 break;
12107
12108 case 'u':
12109 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12110 /* underline */
12111 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12112 else
12113 /* undercurl */
12114 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12115 break;
12116 }
12117
12118 if (p != NULL)
12119 p = vim_strsave(p);
12120#endif
12121 rettv->v_type = VAR_STRING;
12122 rettv->vval.v_string = p;
12123}
12124
12125/*
12126 * "synIDtrans(id)" function
12127 */
12128 static void
12129f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12130{
12131 int id;
12132
12133#ifdef FEAT_SYN_HL
12134 id = (int)get_tv_number(&argvars[0]);
12135
12136 if (id > 0)
12137 id = syn_get_final_id(id);
12138 else
12139#endif
12140 id = 0;
12141
12142 rettv->vval.v_number = id;
12143}
12144
12145/*
12146 * "synconcealed(lnum, col)" function
12147 */
12148 static void
12149f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12150{
12151#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12152 linenr_T lnum;
12153 colnr_T col;
12154 int syntax_flags = 0;
12155 int cchar;
12156 int matchid = 0;
12157 char_u str[NUMBUFLEN];
12158#endif
12159
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012160 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012161
12162#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12163 lnum = get_tv_lnum(argvars); /* -1 on type error */
12164 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12165
12166 vim_memset(str, NUL, sizeof(str));
12167
12168 if (rettv_list_alloc(rettv) != FAIL)
12169 {
12170 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12171 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12172 && curwin->w_p_cole > 0)
12173 {
12174 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12175 syntax_flags = get_syntax_info(&matchid);
12176
12177 /* get the conceal character */
12178 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12179 {
12180 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012181 if (cchar == NUL && curwin->w_p_cole == 1)
12182 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012183 if (cchar != NUL)
12184 {
12185# ifdef FEAT_MBYTE
12186 if (has_mbyte)
12187 (*mb_char2bytes)(cchar, str);
12188 else
12189# endif
12190 str[0] = cchar;
12191 }
12192 }
12193 }
12194
12195 list_append_number(rettv->vval.v_list,
12196 (syntax_flags & HL_CONCEAL) != 0);
12197 /* -1 to auto-determine strlen */
12198 list_append_string(rettv->vval.v_list, str, -1);
12199 list_append_number(rettv->vval.v_list, matchid);
12200 }
12201#endif
12202}
12203
12204/*
12205 * "synstack(lnum, col)" function
12206 */
12207 static void
12208f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12209{
12210#ifdef FEAT_SYN_HL
12211 linenr_T lnum;
12212 colnr_T col;
12213 int i;
12214 int id;
12215#endif
12216
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012217 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012218
12219#ifdef FEAT_SYN_HL
12220 lnum = get_tv_lnum(argvars); /* -1 on type error */
12221 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12222
12223 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12224 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12225 && rettv_list_alloc(rettv) != FAIL)
12226 {
12227 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12228 for (i = 0; ; ++i)
12229 {
12230 id = syn_get_stack_item(i);
12231 if (id < 0)
12232 break;
12233 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12234 break;
12235 }
12236 }
12237#endif
12238}
12239
12240 static void
12241get_cmd_output_as_rettv(
12242 typval_T *argvars,
12243 typval_T *rettv,
12244 int retlist)
12245{
12246 char_u *res = NULL;
12247 char_u *p;
12248 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012249 int err = FALSE;
12250 FILE *fd;
12251 list_T *list = NULL;
12252 int flags = SHELL_SILENT;
12253
12254 rettv->v_type = VAR_STRING;
12255 rettv->vval.v_string = NULL;
12256 if (check_restricted() || check_secure())
12257 goto errret;
12258
12259 if (argvars[1].v_type != VAR_UNKNOWN)
12260 {
12261 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012262 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012263 * command.
12264 */
12265 if ((infile = vim_tempname('i', TRUE)) == NULL)
12266 {
12267 EMSG(_(e_notmp));
12268 goto errret;
12269 }
12270
12271 fd = mch_fopen((char *)infile, WRITEBIN);
12272 if (fd == NULL)
12273 {
12274 EMSG2(_(e_notopen), infile);
12275 goto errret;
12276 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012277 if (argvars[1].v_type == VAR_NUMBER)
12278 {
12279 linenr_T lnum;
12280 buf_T *buf;
12281
12282 buf = buflist_findnr(argvars[1].vval.v_number);
12283 if (buf == NULL)
12284 {
12285 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012286 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012287 goto errret;
12288 }
12289
12290 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12291 {
12292 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12293 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12294 {
12295 err = TRUE;
12296 break;
12297 }
12298 if (putc(NL, fd) == EOF)
12299 {
12300 err = TRUE;
12301 break;
12302 }
12303 }
12304 }
12305 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012306 {
12307 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12308 err = TRUE;
12309 }
12310 else
12311 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012312 size_t len;
12313 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012314
12315 p = get_tv_string_buf_chk(&argvars[1], buf);
12316 if (p == NULL)
12317 {
12318 fclose(fd);
12319 goto errret; /* type error; errmsg already given */
12320 }
12321 len = STRLEN(p);
12322 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12323 err = TRUE;
12324 }
12325 if (fclose(fd) != 0)
12326 err = TRUE;
12327 if (err)
12328 {
12329 EMSG(_("E677: Error writing temp file"));
12330 goto errret;
12331 }
12332 }
12333
12334 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12335 * echoes typeahead, that messes up the display. */
12336 if (!msg_silent)
12337 flags += SHELL_COOKED;
12338
12339 if (retlist)
12340 {
12341 int len;
12342 listitem_T *li;
12343 char_u *s = NULL;
12344 char_u *start;
12345 char_u *end;
12346 int i;
12347
12348 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12349 if (res == NULL)
12350 goto errret;
12351
12352 list = list_alloc();
12353 if (list == NULL)
12354 goto errret;
12355
12356 for (i = 0; i < len; ++i)
12357 {
12358 start = res + i;
12359 while (i < len && res[i] != NL)
12360 ++i;
12361 end = res + i;
12362
12363 s = alloc((unsigned)(end - start + 1));
12364 if (s == NULL)
12365 goto errret;
12366
12367 for (p = s; start < end; ++p, ++start)
12368 *p = *start == NUL ? NL : *start;
12369 *p = NUL;
12370
12371 li = listitem_alloc();
12372 if (li == NULL)
12373 {
12374 vim_free(s);
12375 goto errret;
12376 }
12377 li->li_tv.v_type = VAR_STRING;
12378 li->li_tv.v_lock = 0;
12379 li->li_tv.vval.v_string = s;
12380 list_append(list, li);
12381 }
12382
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012383 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012384 list = NULL;
12385 }
12386 else
12387 {
12388 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12389#ifdef USE_CR
12390 /* translate <CR> into <NL> */
12391 if (res != NULL)
12392 {
12393 char_u *s;
12394
12395 for (s = res; *s; ++s)
12396 {
12397 if (*s == CAR)
12398 *s = NL;
12399 }
12400 }
12401#else
12402# ifdef USE_CRNL
12403 /* translate <CR><NL> into <NL> */
12404 if (res != NULL)
12405 {
12406 char_u *s, *d;
12407
12408 d = res;
12409 for (s = res; *s; ++s)
12410 {
12411 if (s[0] == CAR && s[1] == NL)
12412 ++s;
12413 *d++ = *s;
12414 }
12415 *d = NUL;
12416 }
12417# endif
12418#endif
12419 rettv->vval.v_string = res;
12420 res = NULL;
12421 }
12422
12423errret:
12424 if (infile != NULL)
12425 {
12426 mch_remove(infile);
12427 vim_free(infile);
12428 }
12429 if (res != NULL)
12430 vim_free(res);
12431 if (list != NULL)
12432 list_free(list);
12433}
12434
12435/*
12436 * "system()" function
12437 */
12438 static void
12439f_system(typval_T *argvars, typval_T *rettv)
12440{
12441 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12442}
12443
12444/*
12445 * "systemlist()" function
12446 */
12447 static void
12448f_systemlist(typval_T *argvars, typval_T *rettv)
12449{
12450 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12451}
12452
12453/*
12454 * "tabpagebuflist()" function
12455 */
12456 static void
12457f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12458{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012459 tabpage_T *tp;
12460 win_T *wp = NULL;
12461
12462 if (argvars[0].v_type == VAR_UNKNOWN)
12463 wp = firstwin;
12464 else
12465 {
12466 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12467 if (tp != NULL)
12468 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12469 }
12470 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12471 {
12472 for (; wp != NULL; wp = wp->w_next)
12473 if (list_append_number(rettv->vval.v_list,
12474 wp->w_buffer->b_fnum) == FAIL)
12475 break;
12476 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012477}
12478
12479
12480/*
12481 * "tabpagenr()" function
12482 */
12483 static void
12484f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12485{
12486 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012487 char_u *arg;
12488
12489 if (argvars[0].v_type != VAR_UNKNOWN)
12490 {
12491 arg = get_tv_string_chk(&argvars[0]);
12492 nr = 0;
12493 if (arg != NULL)
12494 {
12495 if (STRCMP(arg, "$") == 0)
12496 nr = tabpage_index(NULL) - 1;
12497 else
12498 EMSG2(_(e_invexpr2), arg);
12499 }
12500 }
12501 else
12502 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012503 rettv->vval.v_number = nr;
12504}
12505
12506
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012507static int get_winnr(tabpage_T *tp, typval_T *argvar);
12508
12509/*
12510 * Common code for tabpagewinnr() and winnr().
12511 */
12512 static int
12513get_winnr(tabpage_T *tp, typval_T *argvar)
12514{
12515 win_T *twin;
12516 int nr = 1;
12517 win_T *wp;
12518 char_u *arg;
12519
12520 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12521 if (argvar->v_type != VAR_UNKNOWN)
12522 {
12523 arg = get_tv_string_chk(argvar);
12524 if (arg == NULL)
12525 nr = 0; /* type error; errmsg already given */
12526 else if (STRCMP(arg, "$") == 0)
12527 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12528 else if (STRCMP(arg, "#") == 0)
12529 {
12530 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12531 if (twin == NULL)
12532 nr = 0;
12533 }
12534 else
12535 {
12536 EMSG2(_(e_invexpr2), arg);
12537 nr = 0;
12538 }
12539 }
12540
12541 if (nr > 0)
12542 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12543 wp != twin; wp = wp->w_next)
12544 {
12545 if (wp == NULL)
12546 {
12547 /* didn't find it in this tabpage */
12548 nr = 0;
12549 break;
12550 }
12551 ++nr;
12552 }
12553 return nr;
12554}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012555
12556/*
12557 * "tabpagewinnr()" function
12558 */
12559 static void
12560f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12561{
12562 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012563 tabpage_T *tp;
12564
12565 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12566 if (tp == NULL)
12567 nr = 0;
12568 else
12569 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012570 rettv->vval.v_number = nr;
12571}
12572
12573
12574/*
12575 * "tagfiles()" function
12576 */
12577 static void
12578f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12579{
12580 char_u *fname;
12581 tagname_T tn;
12582 int first;
12583
12584 if (rettv_list_alloc(rettv) == FAIL)
12585 return;
12586 fname = alloc(MAXPATHL);
12587 if (fname == NULL)
12588 return;
12589
12590 for (first = TRUE; ; first = FALSE)
12591 if (get_tagfname(&tn, first, fname) == FAIL
12592 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12593 break;
12594 tagname_free(&tn);
12595 vim_free(fname);
12596}
12597
12598/*
12599 * "taglist()" function
12600 */
12601 static void
12602f_taglist(typval_T *argvars, typval_T *rettv)
12603{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012604 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012605 char_u *tag_pattern;
12606
12607 tag_pattern = get_tv_string(&argvars[0]);
12608
12609 rettv->vval.v_number = FALSE;
12610 if (*tag_pattern == NUL)
12611 return;
12612
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012613 if (argvars[1].v_type != VAR_UNKNOWN)
12614 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012615 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012616 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012617}
12618
12619/*
12620 * "tempname()" function
12621 */
12622 static void
12623f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12624{
12625 static int x = 'A';
12626
12627 rettv->v_type = VAR_STRING;
12628 rettv->vval.v_string = vim_tempname(x, FALSE);
12629
12630 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12631 * names. Skip 'I' and 'O', they are used for shell redirection. */
12632 do
12633 {
12634 if (x == 'Z')
12635 x = '0';
12636 else if (x == '9')
12637 x = 'A';
12638 else
12639 {
12640#ifdef EBCDIC
12641 if (x == 'I')
12642 x = 'J';
12643 else if (x == 'R')
12644 x = 'S';
12645 else
12646#endif
12647 ++x;
12648 }
12649 } while (x == 'I' || x == 'O');
12650}
12651
12652#ifdef FEAT_FLOAT
12653/*
12654 * "tan()" function
12655 */
12656 static void
12657f_tan(typval_T *argvars, typval_T *rettv)
12658{
12659 float_T f = 0.0;
12660
12661 rettv->v_type = VAR_FLOAT;
12662 if (get_float_arg(argvars, &f) == OK)
12663 rettv->vval.v_float = tan(f);
12664 else
12665 rettv->vval.v_float = 0.0;
12666}
12667
12668/*
12669 * "tanh()" function
12670 */
12671 static void
12672f_tanh(typval_T *argvars, typval_T *rettv)
12673{
12674 float_T f = 0.0;
12675
12676 rettv->v_type = VAR_FLOAT;
12677 if (get_float_arg(argvars, &f) == OK)
12678 rettv->vval.v_float = tanh(f);
12679 else
12680 rettv->vval.v_float = 0.0;
12681}
12682#endif
12683
12684/*
12685 * "test_alloc_fail(id, countdown, repeat)" function
12686 */
12687 static void
12688f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12689{
12690 if (argvars[0].v_type != VAR_NUMBER
12691 || argvars[0].vval.v_number <= 0
12692 || argvars[1].v_type != VAR_NUMBER
12693 || argvars[1].vval.v_number < 0
12694 || argvars[2].v_type != VAR_NUMBER)
12695 EMSG(_(e_invarg));
12696 else
12697 {
12698 alloc_fail_id = argvars[0].vval.v_number;
12699 if (alloc_fail_id >= aid_last)
12700 EMSG(_(e_invarg));
12701 alloc_fail_countdown = argvars[1].vval.v_number;
12702 alloc_fail_repeat = argvars[2].vval.v_number;
12703 did_outofmem_msg = FALSE;
12704 }
12705}
12706
12707/*
12708 * "test_autochdir()"
12709 */
12710 static void
12711f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12712{
12713#if defined(FEAT_AUTOCHDIR)
12714 test_autochdir = TRUE;
12715#endif
12716}
12717
12718/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012719 * "test_feedinput()"
12720 */
12721 static void
12722f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12723{
12724#ifdef USE_INPUT_BUF
12725 char_u *val = get_tv_string_chk(&argvars[0]);
12726
12727 if (val != NULL)
12728 {
12729 trash_input_buf();
12730 add_to_input_buf_csi(val, (int)STRLEN(val));
12731 }
12732#endif
12733}
12734
12735/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012736 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012737 */
12738 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012739f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012740{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012741 char_u *name = (char_u *)"";
12742 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012743 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012744
12745 if (argvars[0].v_type != VAR_STRING
12746 || (argvars[1].v_type) != VAR_NUMBER)
12747 EMSG(_(e_invarg));
12748 else
12749 {
12750 name = get_tv_string_chk(&argvars[0]);
12751 val = (int)get_tv_number(&argvars[1]);
12752
12753 if (STRCMP(name, (char_u *)"redraw") == 0)
12754 disable_redraw_for_testing = val;
12755 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12756 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012757 else if (STRCMP(name, (char_u *)"starting") == 0)
12758 {
12759 if (val)
12760 {
12761 if (save_starting < 0)
12762 save_starting = starting;
12763 starting = 0;
12764 }
12765 else
12766 {
12767 starting = save_starting;
12768 save_starting = -1;
12769 }
12770 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012771 else if (STRCMP(name, (char_u *)"ALL") == 0)
12772 {
12773 disable_char_avail_for_testing = FALSE;
12774 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012775 if (save_starting >= 0)
12776 {
12777 starting = save_starting;
12778 save_starting = -1;
12779 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012780 }
12781 else
12782 EMSG2(_(e_invarg2), name);
12783 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012784}
12785
12786/*
12787 * "test_garbagecollect_now()" function
12788 */
12789 static void
12790f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12791{
12792 /* This is dangerous, any Lists and Dicts used internally may be freed
12793 * while still in use. */
12794 garbage_collect(TRUE);
12795}
12796
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012797/*
12798 * "test_ignore_error()" function
12799 */
12800 static void
12801f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12802{
12803 ignore_error_for_testing(get_tv_string(&argvars[0]));
12804}
12805
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012806#ifdef FEAT_JOB_CHANNEL
12807 static void
12808f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12809{
12810 rettv->v_type = VAR_CHANNEL;
12811 rettv->vval.v_channel = NULL;
12812}
12813#endif
12814
12815 static void
12816f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12817{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012818 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012819}
12820
12821#ifdef FEAT_JOB_CHANNEL
12822 static void
12823f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12824{
12825 rettv->v_type = VAR_JOB;
12826 rettv->vval.v_job = NULL;
12827}
12828#endif
12829
12830 static void
12831f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12832{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012833 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012834}
12835
12836 static void
12837f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12838{
12839 rettv->v_type = VAR_PARTIAL;
12840 rettv->vval.v_partial = NULL;
12841}
12842
12843 static void
12844f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12845{
12846 rettv->v_type = VAR_STRING;
12847 rettv->vval.v_string = NULL;
12848}
12849
12850 static void
12851f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12852{
12853 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12854}
12855
12856#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12857/*
12858 * Get a callback from "arg". It can be a Funcref or a function name.
12859 * When "arg" is zero return an empty string.
12860 * Return NULL for an invalid argument.
12861 */
12862 char_u *
12863get_callback(typval_T *arg, partial_T **pp)
12864{
12865 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12866 {
12867 *pp = arg->vval.v_partial;
12868 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012869 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012870 }
12871 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012872 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012873 {
12874 func_ref(arg->vval.v_string);
12875 return arg->vval.v_string;
12876 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012877 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12878 return (char_u *)"";
12879 EMSG(_("E921: Invalid callback argument"));
12880 return NULL;
12881}
12882
12883/*
12884 * Unref/free "callback" and "partial" retured by get_callback().
12885 */
12886 void
12887free_callback(char_u *callback, partial_T *partial)
12888{
12889 if (partial != NULL)
12890 partial_unref(partial);
12891 else if (callback != NULL)
12892 {
12893 func_unref(callback);
12894 vim_free(callback);
12895 }
12896}
12897#endif
12898
12899#ifdef FEAT_TIMERS
12900/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012901 * "timer_info([timer])" function
12902 */
12903 static void
12904f_timer_info(typval_T *argvars, typval_T *rettv)
12905{
12906 timer_T *timer = NULL;
12907
12908 if (rettv_list_alloc(rettv) != OK)
12909 return;
12910 if (argvars[0].v_type != VAR_UNKNOWN)
12911 {
12912 if (argvars[0].v_type != VAR_NUMBER)
12913 EMSG(_(e_number_exp));
12914 else
12915 {
12916 timer = find_timer((int)get_tv_number(&argvars[0]));
12917 if (timer != NULL)
12918 add_timer_info(rettv, timer);
12919 }
12920 }
12921 else
12922 add_timer_info_all(rettv);
12923}
12924
12925/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012926 * "timer_pause(timer, paused)" function
12927 */
12928 static void
12929f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12930{
12931 timer_T *timer = NULL;
12932 int paused = (int)get_tv_number(&argvars[1]);
12933
12934 if (argvars[0].v_type != VAR_NUMBER)
12935 EMSG(_(e_number_exp));
12936 else
12937 {
12938 timer = find_timer((int)get_tv_number(&argvars[0]));
12939 if (timer != NULL)
12940 timer->tr_paused = paused;
12941 }
12942}
12943
12944/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012945 * "timer_start(time, callback [, options])" function
12946 */
12947 static void
12948f_timer_start(typval_T *argvars, typval_T *rettv)
12949{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012950 long msec = (long)get_tv_number(&argvars[0]);
12951 timer_T *timer;
12952 int repeat = 0;
12953 char_u *callback;
12954 dict_T *dict;
12955 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012956
Bram Moolenaar75537a92016-09-05 22:45:28 +020012957 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012958 if (check_secure())
12959 return;
12960 if (argvars[2].v_type != VAR_UNKNOWN)
12961 {
12962 if (argvars[2].v_type != VAR_DICT
12963 || (dict = argvars[2].vval.v_dict) == NULL)
12964 {
12965 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12966 return;
12967 }
12968 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12969 repeat = get_dict_number(dict, (char_u *)"repeat");
12970 }
12971
Bram Moolenaar75537a92016-09-05 22:45:28 +020012972 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012973 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012974 return;
12975
12976 timer = create_timer(msec, repeat);
12977 if (timer == NULL)
12978 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012979 else
12980 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012981 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012982 timer->tr_callback = vim_strsave(callback);
12983 else
12984 /* pointer into the partial */
12985 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012986 timer->tr_partial = partial;
12987 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012988 }
12989}
12990
12991/*
12992 * "timer_stop(timer)" function
12993 */
12994 static void
12995f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12996{
12997 timer_T *timer;
12998
12999 if (argvars[0].v_type != VAR_NUMBER)
13000 {
13001 EMSG(_(e_number_exp));
13002 return;
13003 }
13004 timer = find_timer((int)get_tv_number(&argvars[0]));
13005 if (timer != NULL)
13006 stop_timer(timer);
13007}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013008
13009/*
13010 * "timer_stopall()" function
13011 */
13012 static void
13013f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13014{
13015 stop_all_timers();
13016}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013017#endif
13018
13019/*
13020 * "tolower(string)" function
13021 */
13022 static void
13023f_tolower(typval_T *argvars, typval_T *rettv)
13024{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013025 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013026 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013027}
13028
13029/*
13030 * "toupper(string)" function
13031 */
13032 static void
13033f_toupper(typval_T *argvars, typval_T *rettv)
13034{
13035 rettv->v_type = VAR_STRING;
13036 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13037}
13038
13039/*
13040 * "tr(string, fromstr, tostr)" function
13041 */
13042 static void
13043f_tr(typval_T *argvars, typval_T *rettv)
13044{
13045 char_u *in_str;
13046 char_u *fromstr;
13047 char_u *tostr;
13048 char_u *p;
13049#ifdef FEAT_MBYTE
13050 int inlen;
13051 int fromlen;
13052 int tolen;
13053 int idx;
13054 char_u *cpstr;
13055 int cplen;
13056 int first = TRUE;
13057#endif
13058 char_u buf[NUMBUFLEN];
13059 char_u buf2[NUMBUFLEN];
13060 garray_T ga;
13061
13062 in_str = get_tv_string(&argvars[0]);
13063 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13064 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13065
13066 /* Default return value: empty string. */
13067 rettv->v_type = VAR_STRING;
13068 rettv->vval.v_string = NULL;
13069 if (fromstr == NULL || tostr == NULL)
13070 return; /* type error; errmsg already given */
13071 ga_init2(&ga, (int)sizeof(char), 80);
13072
13073#ifdef FEAT_MBYTE
13074 if (!has_mbyte)
13075#endif
13076 /* not multi-byte: fromstr and tostr must be the same length */
13077 if (STRLEN(fromstr) != STRLEN(tostr))
13078 {
13079#ifdef FEAT_MBYTE
13080error:
13081#endif
13082 EMSG2(_(e_invarg2), fromstr);
13083 ga_clear(&ga);
13084 return;
13085 }
13086
13087 /* fromstr and tostr have to contain the same number of chars */
13088 while (*in_str != NUL)
13089 {
13090#ifdef FEAT_MBYTE
13091 if (has_mbyte)
13092 {
13093 inlen = (*mb_ptr2len)(in_str);
13094 cpstr = in_str;
13095 cplen = inlen;
13096 idx = 0;
13097 for (p = fromstr; *p != NUL; p += fromlen)
13098 {
13099 fromlen = (*mb_ptr2len)(p);
13100 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13101 {
13102 for (p = tostr; *p != NUL; p += tolen)
13103 {
13104 tolen = (*mb_ptr2len)(p);
13105 if (idx-- == 0)
13106 {
13107 cplen = tolen;
13108 cpstr = p;
13109 break;
13110 }
13111 }
13112 if (*p == NUL) /* tostr is shorter than fromstr */
13113 goto error;
13114 break;
13115 }
13116 ++idx;
13117 }
13118
13119 if (first && cpstr == in_str)
13120 {
13121 /* Check that fromstr and tostr have the same number of
13122 * (multi-byte) characters. Done only once when a character
13123 * of in_str doesn't appear in fromstr. */
13124 first = FALSE;
13125 for (p = tostr; *p != NUL; p += tolen)
13126 {
13127 tolen = (*mb_ptr2len)(p);
13128 --idx;
13129 }
13130 if (idx != 0)
13131 goto error;
13132 }
13133
13134 (void)ga_grow(&ga, cplen);
13135 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13136 ga.ga_len += cplen;
13137
13138 in_str += inlen;
13139 }
13140 else
13141#endif
13142 {
13143 /* When not using multi-byte chars we can do it faster. */
13144 p = vim_strchr(fromstr, *in_str);
13145 if (p != NULL)
13146 ga_append(&ga, tostr[p - fromstr]);
13147 else
13148 ga_append(&ga, *in_str);
13149 ++in_str;
13150 }
13151 }
13152
13153 /* add a terminating NUL */
13154 (void)ga_grow(&ga, 1);
13155 ga_append(&ga, NUL);
13156
13157 rettv->vval.v_string = ga.ga_data;
13158}
13159
13160#ifdef FEAT_FLOAT
13161/*
13162 * "trunc({float})" function
13163 */
13164 static void
13165f_trunc(typval_T *argvars, typval_T *rettv)
13166{
13167 float_T f = 0.0;
13168
13169 rettv->v_type = VAR_FLOAT;
13170 if (get_float_arg(argvars, &f) == OK)
13171 /* trunc() is not in C90, use floor() or ceil() instead. */
13172 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13173 else
13174 rettv->vval.v_float = 0.0;
13175}
13176#endif
13177
13178/*
13179 * "type(expr)" function
13180 */
13181 static void
13182f_type(typval_T *argvars, typval_T *rettv)
13183{
13184 int n = -1;
13185
13186 switch (argvars[0].v_type)
13187 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013188 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13189 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013190 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013191 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13192 case VAR_LIST: n = VAR_TYPE_LIST; break;
13193 case VAR_DICT: n = VAR_TYPE_DICT; break;
13194 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013195 case VAR_SPECIAL:
13196 if (argvars[0].vval.v_number == VVAL_FALSE
13197 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013198 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013199 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013200 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013201 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013202 case VAR_JOB: n = VAR_TYPE_JOB; break;
13203 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013204 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013205 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013206 n = -1;
13207 break;
13208 }
13209 rettv->vval.v_number = n;
13210}
13211
13212/*
13213 * "undofile(name)" function
13214 */
13215 static void
13216f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13217{
13218 rettv->v_type = VAR_STRING;
13219#ifdef FEAT_PERSISTENT_UNDO
13220 {
13221 char_u *fname = get_tv_string(&argvars[0]);
13222
13223 if (*fname == NUL)
13224 {
13225 /* If there is no file name there will be no undo file. */
13226 rettv->vval.v_string = NULL;
13227 }
13228 else
13229 {
13230 char_u *ffname = FullName_save(fname, FALSE);
13231
13232 if (ffname != NULL)
13233 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13234 vim_free(ffname);
13235 }
13236 }
13237#else
13238 rettv->vval.v_string = NULL;
13239#endif
13240}
13241
13242/*
13243 * "undotree()" function
13244 */
13245 static void
13246f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13247{
13248 if (rettv_dict_alloc(rettv) == OK)
13249 {
13250 dict_T *dict = rettv->vval.v_dict;
13251 list_T *list;
13252
13253 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13254 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13255 dict_add_nr_str(dict, "save_last",
13256 (long)curbuf->b_u_save_nr_last, NULL);
13257 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13258 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13259 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13260
13261 list = list_alloc();
13262 if (list != NULL)
13263 {
13264 u_eval_tree(curbuf->b_u_oldhead, list);
13265 dict_add_list(dict, "entries", list);
13266 }
13267 }
13268}
13269
13270/*
13271 * "values(dict)" function
13272 */
13273 static void
13274f_values(typval_T *argvars, typval_T *rettv)
13275{
13276 dict_list(argvars, rettv, 1);
13277}
13278
13279/*
13280 * "virtcol(string)" function
13281 */
13282 static void
13283f_virtcol(typval_T *argvars, typval_T *rettv)
13284{
13285 colnr_T vcol = 0;
13286 pos_T *fp;
13287 int fnum = curbuf->b_fnum;
13288
13289 fp = var2fpos(&argvars[0], FALSE, &fnum);
13290 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13291 && fnum == curbuf->b_fnum)
13292 {
13293 getvvcol(curwin, fp, NULL, NULL, &vcol);
13294 ++vcol;
13295 }
13296
13297 rettv->vval.v_number = vcol;
13298}
13299
13300/*
13301 * "visualmode()" function
13302 */
13303 static void
13304f_visualmode(typval_T *argvars, typval_T *rettv)
13305{
13306 char_u str[2];
13307
13308 rettv->v_type = VAR_STRING;
13309 str[0] = curbuf->b_visual_mode_eval;
13310 str[1] = NUL;
13311 rettv->vval.v_string = vim_strsave(str);
13312
13313 /* A non-zero number or non-empty string argument: reset mode. */
13314 if (non_zero_arg(&argvars[0]))
13315 curbuf->b_visual_mode_eval = NUL;
13316}
13317
13318/*
13319 * "wildmenumode()" function
13320 */
13321 static void
13322f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13323{
13324#ifdef FEAT_WILDMENU
13325 if (wild_menu_showing)
13326 rettv->vval.v_number = 1;
13327#endif
13328}
13329
13330/*
13331 * "winbufnr(nr)" function
13332 */
13333 static void
13334f_winbufnr(typval_T *argvars, typval_T *rettv)
13335{
13336 win_T *wp;
13337
13338 wp = find_win_by_nr(&argvars[0], NULL);
13339 if (wp == NULL)
13340 rettv->vval.v_number = -1;
13341 else
13342 rettv->vval.v_number = wp->w_buffer->b_fnum;
13343}
13344
13345/*
13346 * "wincol()" function
13347 */
13348 static void
13349f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13350{
13351 validate_cursor();
13352 rettv->vval.v_number = curwin->w_wcol + 1;
13353}
13354
13355/*
13356 * "winheight(nr)" function
13357 */
13358 static void
13359f_winheight(typval_T *argvars, typval_T *rettv)
13360{
13361 win_T *wp;
13362
13363 wp = find_win_by_nr(&argvars[0], NULL);
13364 if (wp == NULL)
13365 rettv->vval.v_number = -1;
13366 else
13367 rettv->vval.v_number = wp->w_height;
13368}
13369
13370/*
13371 * "winline()" function
13372 */
13373 static void
13374f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13375{
13376 validate_cursor();
13377 rettv->vval.v_number = curwin->w_wrow + 1;
13378}
13379
13380/*
13381 * "winnr()" function
13382 */
13383 static void
13384f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13385{
13386 int nr = 1;
13387
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013388 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013389 rettv->vval.v_number = nr;
13390}
13391
13392/*
13393 * "winrestcmd()" function
13394 */
13395 static void
13396f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13397{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013398 win_T *wp;
13399 int winnr = 1;
13400 garray_T ga;
13401 char_u buf[50];
13402
13403 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013404 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013405 {
13406 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13407 ga_concat(&ga, buf);
13408 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13409 ga_concat(&ga, buf);
13410 ++winnr;
13411 }
13412 ga_append(&ga, NUL);
13413
13414 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013415 rettv->v_type = VAR_STRING;
13416}
13417
13418/*
13419 * "winrestview()" function
13420 */
13421 static void
13422f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13423{
13424 dict_T *dict;
13425
13426 if (argvars[0].v_type != VAR_DICT
13427 || (dict = argvars[0].vval.v_dict) == NULL)
13428 EMSG(_(e_invarg));
13429 else
13430 {
13431 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13432 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13433 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13434 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13435#ifdef FEAT_VIRTUALEDIT
13436 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13437 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13438#endif
13439 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13440 {
13441 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13442 curwin->w_set_curswant = FALSE;
13443 }
13444
13445 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13446 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13447#ifdef FEAT_DIFF
13448 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13449 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13450#endif
13451 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13452 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13453 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13454 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13455
13456 check_cursor();
13457 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013458 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013459 changed_window_setting();
13460
13461 if (curwin->w_topline <= 0)
13462 curwin->w_topline = 1;
13463 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13464 curwin->w_topline = curbuf->b_ml.ml_line_count;
13465#ifdef FEAT_DIFF
13466 check_topfill(curwin, TRUE);
13467#endif
13468 }
13469}
13470
13471/*
13472 * "winsaveview()" function
13473 */
13474 static void
13475f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13476{
13477 dict_T *dict;
13478
13479 if (rettv_dict_alloc(rettv) == FAIL)
13480 return;
13481 dict = rettv->vval.v_dict;
13482
13483 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13484 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13485#ifdef FEAT_VIRTUALEDIT
13486 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13487#endif
13488 update_curswant();
13489 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13490
13491 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13492#ifdef FEAT_DIFF
13493 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13494#endif
13495 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13496 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13497}
13498
13499/*
13500 * "winwidth(nr)" function
13501 */
13502 static void
13503f_winwidth(typval_T *argvars, typval_T *rettv)
13504{
13505 win_T *wp;
13506
13507 wp = find_win_by_nr(&argvars[0], NULL);
13508 if (wp == NULL)
13509 rettv->vval.v_number = -1;
13510 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013511 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013512}
13513
13514/*
13515 * "wordcount()" function
13516 */
13517 static void
13518f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13519{
13520 if (rettv_dict_alloc(rettv) == FAIL)
13521 return;
13522 cursor_pos_info(rettv->vval.v_dict);
13523}
13524
13525/*
13526 * "writefile()" function
13527 */
13528 static void
13529f_writefile(typval_T *argvars, typval_T *rettv)
13530{
13531 int binary = FALSE;
13532 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013533#ifdef HAVE_FSYNC
13534 int do_fsync = p_fs;
13535#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013536 char_u *fname;
13537 FILE *fd;
13538 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013539 listitem_T *li;
13540 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013541
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013542 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013543 if (check_restricted() || check_secure())
13544 return;
13545
13546 if (argvars[0].v_type != VAR_LIST)
13547 {
13548 EMSG2(_(e_listarg), "writefile()");
13549 return;
13550 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013551 list = argvars[0].vval.v_list;
13552 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013553 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013554 for (li = list->lv_first; li != NULL; li = li->li_next)
13555 if (get_tv_string_chk(&li->li_tv) == NULL)
13556 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013557
13558 if (argvars[2].v_type != VAR_UNKNOWN)
13559 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013560 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13561
13562 if (arg2 == NULL)
13563 return;
13564 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013565 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013566 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013567 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013568#ifdef HAVE_FSYNC
13569 if (vim_strchr(arg2, 's') != NULL)
13570 do_fsync = TRUE;
13571 else if (vim_strchr(arg2, 'S') != NULL)
13572 do_fsync = FALSE;
13573#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013574 }
13575
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013576 fname = get_tv_string_chk(&argvars[1]);
13577 if (fname == NULL)
13578 return;
13579
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013580 /* Always open the file in binary mode, library functions have a mind of
13581 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013582 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13583 append ? APPENDBIN : WRITEBIN)) == NULL)
13584 {
13585 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13586 ret = -1;
13587 }
13588 else
13589 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013590 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013591 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013592#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013593 else if (do_fsync)
13594 /* Ignore the error, the user wouldn't know what to do about it.
13595 * May happen for a device. */
13596 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013597#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013598 fclose(fd);
13599 }
13600
13601 rettv->vval.v_number = ret;
13602}
13603
13604/*
13605 * "xor(expr, expr)" function
13606 */
13607 static void
13608f_xor(typval_T *argvars, typval_T *rettv)
13609{
13610 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13611 ^ get_tv_number_chk(&argvars[1], NULL);
13612}
13613
13614
13615#endif /* FEAT_EVAL */