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