blob: 780458af9559e824edd857bccd3acb0f2c4f84cd [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);
Bram Moolenaard96ff162018-02-18 22:13:29 +010049static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_assert_exception(typval_T *argvars, typval_T *rettv);
51static void f_assert_fails(typval_T *argvars, typval_T *rettv);
52static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020053static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020054static void f_assert_match(typval_T *argvars, typval_T *rettv);
55static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
56static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010057static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020058static void f_assert_true(typval_T *argvars, typval_T *rettv);
59#ifdef FEAT_FLOAT
60static void f_asin(typval_T *argvars, typval_T *rettv);
61static void f_atan(typval_T *argvars, typval_T *rettv);
62static void f_atan2(typval_T *argvars, typval_T *rettv);
63#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010064#ifdef FEAT_BEVAL
65static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010066# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010067static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010069#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_browse(typval_T *argvars, typval_T *rettv);
71static void f_browsedir(typval_T *argvars, typval_T *rettv);
72static void f_bufexists(typval_T *argvars, typval_T *rettv);
73static void f_buflisted(typval_T *argvars, typval_T *rettv);
74static void f_bufloaded(typval_T *argvars, typval_T *rettv);
75static void f_bufname(typval_T *argvars, typval_T *rettv);
76static void f_bufnr(typval_T *argvars, typval_T *rettv);
77static void f_bufwinid(typval_T *argvars, typval_T *rettv);
78static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
79static void f_byte2line(typval_T *argvars, typval_T *rettv);
80static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
81static void f_byteidx(typval_T *argvars, typval_T *rettv);
82static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
83static void f_call(typval_T *argvars, typval_T *rettv);
84#ifdef FEAT_FLOAT
85static void f_ceil(typval_T *argvars, typval_T *rettv);
86#endif
87#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010088static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020090static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
92static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
93static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
94static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
95static void f_ch_info(typval_T *argvars, typval_T *rettv);
96static void f_ch_log(typval_T *argvars, typval_T *rettv);
97static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
98static void f_ch_open(typval_T *argvars, typval_T *rettv);
99static void f_ch_read(typval_T *argvars, typval_T *rettv);
100static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
101static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
102static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
103static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
104static void f_ch_status(typval_T *argvars, typval_T *rettv);
105#endif
106static void f_changenr(typval_T *argvars, typval_T *rettv);
107static void f_char2nr(typval_T *argvars, typval_T *rettv);
108static void f_cindent(typval_T *argvars, typval_T *rettv);
109static void f_clearmatches(typval_T *argvars, typval_T *rettv);
110static void f_col(typval_T *argvars, typval_T *rettv);
111#if defined(FEAT_INS_EXPAND)
112static void f_complete(typval_T *argvars, typval_T *rettv);
113static void f_complete_add(typval_T *argvars, typval_T *rettv);
114static void f_complete_check(typval_T *argvars, typval_T *rettv);
115#endif
116static void f_confirm(typval_T *argvars, typval_T *rettv);
117static void f_copy(typval_T *argvars, typval_T *rettv);
118#ifdef FEAT_FLOAT
119static void f_cos(typval_T *argvars, typval_T *rettv);
120static void f_cosh(typval_T *argvars, typval_T *rettv);
121#endif
122static void f_count(typval_T *argvars, typval_T *rettv);
123static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
124static void f_cursor(typval_T *argsvars, typval_T *rettv);
125static void f_deepcopy(typval_T *argvars, typval_T *rettv);
126static void f_delete(typval_T *argvars, typval_T *rettv);
127static void f_did_filetype(typval_T *argvars, typval_T *rettv);
128static void f_diff_filler(typval_T *argvars, typval_T *rettv);
129static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
130static void f_empty(typval_T *argvars, typval_T *rettv);
131static void f_escape(typval_T *argvars, typval_T *rettv);
132static void f_eval(typval_T *argvars, typval_T *rettv);
133static void f_eventhandler(typval_T *argvars, typval_T *rettv);
134static void f_executable(typval_T *argvars, typval_T *rettv);
135static void f_execute(typval_T *argvars, typval_T *rettv);
136static void f_exepath(typval_T *argvars, typval_T *rettv);
137static void f_exists(typval_T *argvars, typval_T *rettv);
138#ifdef FEAT_FLOAT
139static void f_exp(typval_T *argvars, typval_T *rettv);
140#endif
141static void f_expand(typval_T *argvars, typval_T *rettv);
142static void f_extend(typval_T *argvars, typval_T *rettv);
143static void f_feedkeys(typval_T *argvars, typval_T *rettv);
144static void f_filereadable(typval_T *argvars, typval_T *rettv);
145static void f_filewritable(typval_T *argvars, typval_T *rettv);
146static void f_filter(typval_T *argvars, typval_T *rettv);
147static void f_finddir(typval_T *argvars, typval_T *rettv);
148static void f_findfile(typval_T *argvars, typval_T *rettv);
149#ifdef FEAT_FLOAT
150static void f_float2nr(typval_T *argvars, typval_T *rettv);
151static void f_floor(typval_T *argvars, typval_T *rettv);
152static void f_fmod(typval_T *argvars, typval_T *rettv);
153#endif
154static void f_fnameescape(typval_T *argvars, typval_T *rettv);
155static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
156static void f_foldclosed(typval_T *argvars, typval_T *rettv);
157static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
158static void f_foldlevel(typval_T *argvars, typval_T *rettv);
159static void f_foldtext(typval_T *argvars, typval_T *rettv);
160static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
161static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200162static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200163static void f_function(typval_T *argvars, typval_T *rettv);
164static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
165static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200166static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200167static void f_getbufline(typval_T *argvars, typval_T *rettv);
168static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100169static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200170static void f_getchar(typval_T *argvars, typval_T *rettv);
171static void f_getcharmod(typval_T *argvars, typval_T *rettv);
172static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
173static void f_getcmdline(typval_T *argvars, typval_T *rettv);
174#if defined(FEAT_CMDL_COMPL)
175static void f_getcompletion(typval_T *argvars, typval_T *rettv);
176#endif
177static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
178static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
179static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
180static void f_getcwd(typval_T *argvars, typval_T *rettv);
181static void f_getfontname(typval_T *argvars, typval_T *rettv);
182static void f_getfperm(typval_T *argvars, typval_T *rettv);
183static void f_getfsize(typval_T *argvars, typval_T *rettv);
184static void f_getftime(typval_T *argvars, typval_T *rettv);
185static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100186static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200187static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200188static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200189static void f_getmatches(typval_T *argvars, typval_T *rettv);
190static void f_getpid(typval_T *argvars, typval_T *rettv);
191static void f_getcurpos(typval_T *argvars, typval_T *rettv);
192static void f_getpos(typval_T *argvars, typval_T *rettv);
193static void f_getqflist(typval_T *argvars, typval_T *rettv);
194static void f_getreg(typval_T *argvars, typval_T *rettv);
195static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200196static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197static void f_gettabvar(typval_T *argvars, typval_T *rettv);
198static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200199static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100200static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_getwinposx(typval_T *argvars, typval_T *rettv);
202static void f_getwinposy(typval_T *argvars, typval_T *rettv);
203static void f_getwinvar(typval_T *argvars, typval_T *rettv);
204static void f_glob(typval_T *argvars, typval_T *rettv);
205static void f_globpath(typval_T *argvars, typval_T *rettv);
206static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
207static void f_has(typval_T *argvars, typval_T *rettv);
208static void f_has_key(typval_T *argvars, typval_T *rettv);
209static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
210static void f_hasmapto(typval_T *argvars, typval_T *rettv);
211static void f_histadd(typval_T *argvars, typval_T *rettv);
212static void f_histdel(typval_T *argvars, typval_T *rettv);
213static void f_histget(typval_T *argvars, typval_T *rettv);
214static void f_histnr(typval_T *argvars, typval_T *rettv);
215static void f_hlID(typval_T *argvars, typval_T *rettv);
216static void f_hlexists(typval_T *argvars, typval_T *rettv);
217static void f_hostname(typval_T *argvars, typval_T *rettv);
218static void f_iconv(typval_T *argvars, typval_T *rettv);
219static void f_indent(typval_T *argvars, typval_T *rettv);
220static void f_index(typval_T *argvars, typval_T *rettv);
221static void f_input(typval_T *argvars, typval_T *rettv);
222static void f_inputdialog(typval_T *argvars, typval_T *rettv);
223static void f_inputlist(typval_T *argvars, typval_T *rettv);
224static void f_inputrestore(typval_T *argvars, typval_T *rettv);
225static void f_inputsave(typval_T *argvars, typval_T *rettv);
226static void f_inputsecret(typval_T *argvars, typval_T *rettv);
227static void f_insert(typval_T *argvars, typval_T *rettv);
228static void f_invert(typval_T *argvars, typval_T *rettv);
229static void f_isdirectory(typval_T *argvars, typval_T *rettv);
230static void f_islocked(typval_T *argvars, typval_T *rettv);
231#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
232static void f_isnan(typval_T *argvars, typval_T *rettv);
233#endif
234static void f_items(typval_T *argvars, typval_T *rettv);
235#ifdef FEAT_JOB_CHANNEL
236static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
237static void f_job_info(typval_T *argvars, typval_T *rettv);
238static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
239static void f_job_start(typval_T *argvars, typval_T *rettv);
240static void f_job_stop(typval_T *argvars, typval_T *rettv);
241static void f_job_status(typval_T *argvars, typval_T *rettv);
242#endif
243static void f_join(typval_T *argvars, typval_T *rettv);
244static void f_js_decode(typval_T *argvars, typval_T *rettv);
245static void f_js_encode(typval_T *argvars, typval_T *rettv);
246static void f_json_decode(typval_T *argvars, typval_T *rettv);
247static void f_json_encode(typval_T *argvars, typval_T *rettv);
248static void f_keys(typval_T *argvars, typval_T *rettv);
249static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
250static void f_len(typval_T *argvars, typval_T *rettv);
251static void f_libcall(typval_T *argvars, typval_T *rettv);
252static void f_libcallnr(typval_T *argvars, typval_T *rettv);
253static void f_line(typval_T *argvars, typval_T *rettv);
254static void f_line2byte(typval_T *argvars, typval_T *rettv);
255static void f_lispindent(typval_T *argvars, typval_T *rettv);
256static void f_localtime(typval_T *argvars, typval_T *rettv);
257#ifdef FEAT_FLOAT
258static void f_log(typval_T *argvars, typval_T *rettv);
259static void f_log10(typval_T *argvars, typval_T *rettv);
260#endif
261#ifdef FEAT_LUA
262static void f_luaeval(typval_T *argvars, typval_T *rettv);
263#endif
264static void f_map(typval_T *argvars, typval_T *rettv);
265static void f_maparg(typval_T *argvars, typval_T *rettv);
266static void f_mapcheck(typval_T *argvars, typval_T *rettv);
267static void f_match(typval_T *argvars, typval_T *rettv);
268static void f_matchadd(typval_T *argvars, typval_T *rettv);
269static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
270static void f_matcharg(typval_T *argvars, typval_T *rettv);
271static void f_matchdelete(typval_T *argvars, typval_T *rettv);
272static void f_matchend(typval_T *argvars, typval_T *rettv);
273static void f_matchlist(typval_T *argvars, typval_T *rettv);
274static void f_matchstr(typval_T *argvars, typval_T *rettv);
275static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
276static void f_max(typval_T *argvars, typval_T *rettv);
277static void f_min(typval_T *argvars, typval_T *rettv);
278#ifdef vim_mkdir
279static void f_mkdir(typval_T *argvars, typval_T *rettv);
280#endif
281static void f_mode(typval_T *argvars, typval_T *rettv);
282#ifdef FEAT_MZSCHEME
283static void f_mzeval(typval_T *argvars, typval_T *rettv);
284#endif
285static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
286static void f_nr2char(typval_T *argvars, typval_T *rettv);
287static void f_or(typval_T *argvars, typval_T *rettv);
288static void f_pathshorten(typval_T *argvars, typval_T *rettv);
289#ifdef FEAT_PERL
290static void f_perleval(typval_T *argvars, typval_T *rettv);
291#endif
292#ifdef FEAT_FLOAT
293static void f_pow(typval_T *argvars, typval_T *rettv);
294#endif
295static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
296static void f_printf(typval_T *argvars, typval_T *rettv);
297static void f_pumvisible(typval_T *argvars, typval_T *rettv);
298#ifdef FEAT_PYTHON3
299static void f_py3eval(typval_T *argvars, typval_T *rettv);
300#endif
301#ifdef FEAT_PYTHON
302static void f_pyeval(typval_T *argvars, typval_T *rettv);
303#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100304#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
305static void f_pyxeval(typval_T *argvars, typval_T *rettv);
306#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200307static void f_range(typval_T *argvars, typval_T *rettv);
308static void f_readfile(typval_T *argvars, typval_T *rettv);
309static void f_reltime(typval_T *argvars, typval_T *rettv);
310#ifdef FEAT_FLOAT
311static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
312#endif
313static void f_reltimestr(typval_T *argvars, typval_T *rettv);
314static void f_remote_expr(typval_T *argvars, typval_T *rettv);
315static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
316static void f_remote_peek(typval_T *argvars, typval_T *rettv);
317static void f_remote_read(typval_T *argvars, typval_T *rettv);
318static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100319static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200320static void f_remove(typval_T *argvars, typval_T *rettv);
321static void f_rename(typval_T *argvars, typval_T *rettv);
322static void f_repeat(typval_T *argvars, typval_T *rettv);
323static void f_resolve(typval_T *argvars, typval_T *rettv);
324static void f_reverse(typval_T *argvars, typval_T *rettv);
325#ifdef FEAT_FLOAT
326static void f_round(typval_T *argvars, typval_T *rettv);
327#endif
328static void f_screenattr(typval_T *argvars, typval_T *rettv);
329static void f_screenchar(typval_T *argvars, typval_T *rettv);
330static void f_screencol(typval_T *argvars, typval_T *rettv);
331static void f_screenrow(typval_T *argvars, typval_T *rettv);
332static void f_search(typval_T *argvars, typval_T *rettv);
333static void f_searchdecl(typval_T *argvars, typval_T *rettv);
334static void f_searchpair(typval_T *argvars, typval_T *rettv);
335static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
336static void f_searchpos(typval_T *argvars, typval_T *rettv);
337static void f_server2client(typval_T *argvars, typval_T *rettv);
338static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200339static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200340static void f_setbufvar(typval_T *argvars, typval_T *rettv);
341static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
342static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
343static void f_setfperm(typval_T *argvars, typval_T *rettv);
344static void f_setline(typval_T *argvars, typval_T *rettv);
345static void f_setloclist(typval_T *argvars, typval_T *rettv);
346static void f_setmatches(typval_T *argvars, typval_T *rettv);
347static void f_setpos(typval_T *argvars, typval_T *rettv);
348static void f_setqflist(typval_T *argvars, typval_T *rettv);
349static void f_setreg(typval_T *argvars, typval_T *rettv);
350static void f_settabvar(typval_T *argvars, typval_T *rettv);
351static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
352static void f_setwinvar(typval_T *argvars, typval_T *rettv);
353#ifdef FEAT_CRYPT
354static void f_sha256(typval_T *argvars, typval_T *rettv);
355#endif /* FEAT_CRYPT */
356static void f_shellescape(typval_T *argvars, typval_T *rettv);
357static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
358static void f_simplify(typval_T *argvars, typval_T *rettv);
359#ifdef FEAT_FLOAT
360static void f_sin(typval_T *argvars, typval_T *rettv);
361static void f_sinh(typval_T *argvars, typval_T *rettv);
362#endif
363static void f_sort(typval_T *argvars, typval_T *rettv);
364static void f_soundfold(typval_T *argvars, typval_T *rettv);
365static void f_spellbadword(typval_T *argvars, typval_T *rettv);
366static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
367static void f_split(typval_T *argvars, typval_T *rettv);
368#ifdef FEAT_FLOAT
369static void f_sqrt(typval_T *argvars, typval_T *rettv);
370static void f_str2float(typval_T *argvars, typval_T *rettv);
371#endif
372static void f_str2nr(typval_T *argvars, typval_T *rettv);
373static void f_strchars(typval_T *argvars, typval_T *rettv);
374#ifdef HAVE_STRFTIME
375static void f_strftime(typval_T *argvars, typval_T *rettv);
376#endif
377static void f_strgetchar(typval_T *argvars, typval_T *rettv);
378static void f_stridx(typval_T *argvars, typval_T *rettv);
379static void f_string(typval_T *argvars, typval_T *rettv);
380static void f_strlen(typval_T *argvars, typval_T *rettv);
381static void f_strcharpart(typval_T *argvars, typval_T *rettv);
382static void f_strpart(typval_T *argvars, typval_T *rettv);
383static void f_strridx(typval_T *argvars, typval_T *rettv);
384static void f_strtrans(typval_T *argvars, typval_T *rettv);
385static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
386static void f_strwidth(typval_T *argvars, typval_T *rettv);
387static void f_submatch(typval_T *argvars, typval_T *rettv);
388static void f_substitute(typval_T *argvars, typval_T *rettv);
389static void f_synID(typval_T *argvars, typval_T *rettv);
390static void f_synIDattr(typval_T *argvars, typval_T *rettv);
391static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
392static void f_synstack(typval_T *argvars, typval_T *rettv);
393static void f_synconcealed(typval_T *argvars, typval_T *rettv);
394static void f_system(typval_T *argvars, typval_T *rettv);
395static void f_systemlist(typval_T *argvars, typval_T *rettv);
396static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
397static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
398static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
399static void f_taglist(typval_T *argvars, typval_T *rettv);
400static void f_tagfiles(typval_T *argvars, typval_T *rettv);
401static void f_tempname(typval_T *argvars, typval_T *rettv);
402static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
403static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200404static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100405static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200406static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100407static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200408#ifdef FEAT_JOB_CHANNEL
409static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
410#endif
411static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
412#ifdef FEAT_JOB_CHANNEL
413static void f_test_null_job(typval_T *argvars, typval_T *rettv);
414#endif
415static void f_test_null_list(typval_T *argvars, typval_T *rettv);
416static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
417static void f_test_null_string(typval_T *argvars, typval_T *rettv);
418static void f_test_settime(typval_T *argvars, typval_T *rettv);
419#ifdef FEAT_FLOAT
420static void f_tan(typval_T *argvars, typval_T *rettv);
421static void f_tanh(typval_T *argvars, typval_T *rettv);
422#endif
423#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200424static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200425static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200426static void f_timer_start(typval_T *argvars, typval_T *rettv);
427static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200428static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200429#endif
430static void f_tolower(typval_T *argvars, typval_T *rettv);
431static void f_toupper(typval_T *argvars, typval_T *rettv);
432static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100433static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200434#ifdef FEAT_FLOAT
435static void f_trunc(typval_T *argvars, typval_T *rettv);
436#endif
437static void f_type(typval_T *argvars, typval_T *rettv);
438static void f_undofile(typval_T *argvars, typval_T *rettv);
439static void f_undotree(typval_T *argvars, typval_T *rettv);
440static void f_uniq(typval_T *argvars, typval_T *rettv);
441static void f_values(typval_T *argvars, typval_T *rettv);
442static void f_virtcol(typval_T *argvars, typval_T *rettv);
443static void f_visualmode(typval_T *argvars, typval_T *rettv);
444static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
445static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
446static void f_win_getid(typval_T *argvars, typval_T *rettv);
447static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
448static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
449static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100450static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200451static void f_winbufnr(typval_T *argvars, typval_T *rettv);
452static void f_wincol(typval_T *argvars, typval_T *rettv);
453static void f_winheight(typval_T *argvars, typval_T *rettv);
454static void f_winline(typval_T *argvars, typval_T *rettv);
455static void f_winnr(typval_T *argvars, typval_T *rettv);
456static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
457static void f_winrestview(typval_T *argvars, typval_T *rettv);
458static void f_winsaveview(typval_T *argvars, typval_T *rettv);
459static void f_winwidth(typval_T *argvars, typval_T *rettv);
460static void f_writefile(typval_T *argvars, typval_T *rettv);
461static void f_wordcount(typval_T *argvars, typval_T *rettv);
462static void f_xor(typval_T *argvars, typval_T *rettv);
463
464/*
465 * Array with names and number of arguments of all internal functions
466 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
467 */
468static struct fst
469{
470 char *f_name; /* function name */
471 char f_min_argc; /* minimal number of arguments */
472 char f_max_argc; /* maximal number of arguments */
473 void (*f_func)(typval_T *args, typval_T *rvar);
474 /* implementation of function */
475} functions[] =
476{
477#ifdef FEAT_FLOAT
478 {"abs", 1, 1, f_abs},
479 {"acos", 1, 1, f_acos}, /* WJMc */
480#endif
481 {"add", 2, 2, f_add},
482 {"and", 2, 2, f_and},
483 {"append", 2, 2, f_append},
484 {"argc", 0, 0, f_argc},
485 {"argidx", 0, 0, f_argidx},
486 {"arglistid", 0, 2, f_arglistid},
487 {"argv", 0, 1, f_argv},
488#ifdef FEAT_FLOAT
489 {"asin", 1, 1, f_asin}, /* WJMc */
490#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100491 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200492 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100493 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200494 {"assert_exception", 1, 2, f_assert_exception},
495 {"assert_fails", 1, 2, f_assert_fails},
496 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100497 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200498 {"assert_match", 2, 3, f_assert_match},
499 {"assert_notequal", 2, 3, f_assert_notequal},
500 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100501 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200502 {"assert_true", 1, 2, f_assert_true},
503#ifdef FEAT_FLOAT
504 {"atan", 1, 1, f_atan},
505 {"atan2", 2, 2, f_atan2},
506#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100507#ifdef FEAT_BEVAL
508 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100509# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100510 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100511# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100512#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200513 {"browse", 4, 4, f_browse},
514 {"browsedir", 2, 2, f_browsedir},
515 {"bufexists", 1, 1, f_bufexists},
516 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
517 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
518 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
519 {"buflisted", 1, 1, f_buflisted},
520 {"bufloaded", 1, 1, f_bufloaded},
521 {"bufname", 1, 1, f_bufname},
522 {"bufnr", 1, 2, f_bufnr},
523 {"bufwinid", 1, 1, f_bufwinid},
524 {"bufwinnr", 1, 1, f_bufwinnr},
525 {"byte2line", 1, 1, f_byte2line},
526 {"byteidx", 2, 2, f_byteidx},
527 {"byteidxcomp", 2, 2, f_byteidxcomp},
528 {"call", 2, 3, f_call},
529#ifdef FEAT_FLOAT
530 {"ceil", 1, 1, f_ceil},
531#endif
532#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100533 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200534 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200535 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200536 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
537 {"ch_evalraw", 2, 3, f_ch_evalraw},
538 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
539 {"ch_getjob", 1, 1, f_ch_getjob},
540 {"ch_info", 1, 1, f_ch_info},
541 {"ch_log", 1, 2, f_ch_log},
542 {"ch_logfile", 1, 2, f_ch_logfile},
543 {"ch_open", 1, 2, f_ch_open},
544 {"ch_read", 1, 2, f_ch_read},
545 {"ch_readraw", 1, 2, f_ch_readraw},
546 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
547 {"ch_sendraw", 2, 3, f_ch_sendraw},
548 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200549 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550#endif
551 {"changenr", 0, 0, f_changenr},
552 {"char2nr", 1, 2, f_char2nr},
553 {"cindent", 1, 1, f_cindent},
554 {"clearmatches", 0, 0, f_clearmatches},
555 {"col", 1, 1, f_col},
556#if defined(FEAT_INS_EXPAND)
557 {"complete", 2, 2, f_complete},
558 {"complete_add", 1, 1, f_complete_add},
559 {"complete_check", 0, 0, f_complete_check},
560#endif
561 {"confirm", 1, 4, f_confirm},
562 {"copy", 1, 1, f_copy},
563#ifdef FEAT_FLOAT
564 {"cos", 1, 1, f_cos},
565 {"cosh", 1, 1, f_cosh},
566#endif
567 {"count", 2, 4, f_count},
568 {"cscope_connection",0,3, f_cscope_connection},
569 {"cursor", 1, 3, f_cursor},
570 {"deepcopy", 1, 2, f_deepcopy},
571 {"delete", 1, 2, f_delete},
572 {"did_filetype", 0, 0, f_did_filetype},
573 {"diff_filler", 1, 1, f_diff_filler},
574 {"diff_hlID", 2, 2, f_diff_hlID},
575 {"empty", 1, 1, f_empty},
576 {"escape", 2, 2, f_escape},
577 {"eval", 1, 1, f_eval},
578 {"eventhandler", 0, 0, f_eventhandler},
579 {"executable", 1, 1, f_executable},
580 {"execute", 1, 2, f_execute},
581 {"exepath", 1, 1, f_exepath},
582 {"exists", 1, 1, f_exists},
583#ifdef FEAT_FLOAT
584 {"exp", 1, 1, f_exp},
585#endif
586 {"expand", 1, 3, f_expand},
587 {"extend", 2, 3, f_extend},
588 {"feedkeys", 1, 2, f_feedkeys},
589 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
590 {"filereadable", 1, 1, f_filereadable},
591 {"filewritable", 1, 1, f_filewritable},
592 {"filter", 2, 2, f_filter},
593 {"finddir", 1, 3, f_finddir},
594 {"findfile", 1, 3, f_findfile},
595#ifdef FEAT_FLOAT
596 {"float2nr", 1, 1, f_float2nr},
597 {"floor", 1, 1, f_floor},
598 {"fmod", 2, 2, f_fmod},
599#endif
600 {"fnameescape", 1, 1, f_fnameescape},
601 {"fnamemodify", 2, 2, f_fnamemodify},
602 {"foldclosed", 1, 1, f_foldclosed},
603 {"foldclosedend", 1, 1, f_foldclosedend},
604 {"foldlevel", 1, 1, f_foldlevel},
605 {"foldtext", 0, 0, f_foldtext},
606 {"foldtextresult", 1, 1, f_foldtextresult},
607 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200608 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200609 {"function", 1, 3, f_function},
610 {"garbagecollect", 0, 1, f_garbagecollect},
611 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200612 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200613 {"getbufline", 2, 3, f_getbufline},
614 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100615 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200616 {"getchar", 0, 1, f_getchar},
617 {"getcharmod", 0, 0, f_getcharmod},
618 {"getcharsearch", 0, 0, f_getcharsearch},
619 {"getcmdline", 0, 0, f_getcmdline},
620 {"getcmdpos", 0, 0, f_getcmdpos},
621 {"getcmdtype", 0, 0, f_getcmdtype},
622 {"getcmdwintype", 0, 0, f_getcmdwintype},
623#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200624 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200625#endif
626 {"getcurpos", 0, 0, f_getcurpos},
627 {"getcwd", 0, 2, f_getcwd},
628 {"getfontname", 0, 1, f_getfontname},
629 {"getfperm", 1, 1, f_getfperm},
630 {"getfsize", 1, 1, f_getfsize},
631 {"getftime", 1, 1, f_getftime},
632 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100633 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200634 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200635 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636 {"getmatches", 0, 0, f_getmatches},
637 {"getpid", 0, 0, f_getpid},
638 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200639 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200640 {"getreg", 0, 3, f_getreg},
641 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200642 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200643 {"gettabvar", 2, 3, f_gettabvar},
644 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200645 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100646 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200647 {"getwinposx", 0, 0, f_getwinposx},
648 {"getwinposy", 0, 0, f_getwinposy},
649 {"getwinvar", 2, 3, f_getwinvar},
650 {"glob", 1, 4, f_glob},
651 {"glob2regpat", 1, 1, f_glob2regpat},
652 {"globpath", 2, 5, f_globpath},
653 {"has", 1, 1, f_has},
654 {"has_key", 2, 2, f_has_key},
655 {"haslocaldir", 0, 2, f_haslocaldir},
656 {"hasmapto", 1, 3, f_hasmapto},
657 {"highlightID", 1, 1, f_hlID}, /* obsolete */
658 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
659 {"histadd", 2, 2, f_histadd},
660 {"histdel", 1, 2, f_histdel},
661 {"histget", 1, 2, f_histget},
662 {"histnr", 1, 1, f_histnr},
663 {"hlID", 1, 1, f_hlID},
664 {"hlexists", 1, 1, f_hlexists},
665 {"hostname", 0, 0, f_hostname},
666 {"iconv", 3, 3, f_iconv},
667 {"indent", 1, 1, f_indent},
668 {"index", 2, 4, f_index},
669 {"input", 1, 3, f_input},
670 {"inputdialog", 1, 3, f_inputdialog},
671 {"inputlist", 1, 1, f_inputlist},
672 {"inputrestore", 0, 0, f_inputrestore},
673 {"inputsave", 0, 0, f_inputsave},
674 {"inputsecret", 1, 2, f_inputsecret},
675 {"insert", 2, 3, f_insert},
676 {"invert", 1, 1, f_invert},
677 {"isdirectory", 1, 1, f_isdirectory},
678 {"islocked", 1, 1, f_islocked},
679#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
680 {"isnan", 1, 1, f_isnan},
681#endif
682 {"items", 1, 1, f_items},
683#ifdef FEAT_JOB_CHANNEL
684 {"job_getchannel", 1, 1, f_job_getchannel},
685 {"job_info", 1, 1, f_job_info},
686 {"job_setoptions", 2, 2, f_job_setoptions},
687 {"job_start", 1, 2, f_job_start},
688 {"job_status", 1, 1, f_job_status},
689 {"job_stop", 1, 2, f_job_stop},
690#endif
691 {"join", 1, 2, f_join},
692 {"js_decode", 1, 1, f_js_decode},
693 {"js_encode", 1, 1, f_js_encode},
694 {"json_decode", 1, 1, f_json_decode},
695 {"json_encode", 1, 1, f_json_encode},
696 {"keys", 1, 1, f_keys},
697 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
698 {"len", 1, 1, f_len},
699 {"libcall", 3, 3, f_libcall},
700 {"libcallnr", 3, 3, f_libcallnr},
701 {"line", 1, 1, f_line},
702 {"line2byte", 1, 1, f_line2byte},
703 {"lispindent", 1, 1, f_lispindent},
704 {"localtime", 0, 0, f_localtime},
705#ifdef FEAT_FLOAT
706 {"log", 1, 1, f_log},
707 {"log10", 1, 1, f_log10},
708#endif
709#ifdef FEAT_LUA
710 {"luaeval", 1, 2, f_luaeval},
711#endif
712 {"map", 2, 2, f_map},
713 {"maparg", 1, 4, f_maparg},
714 {"mapcheck", 1, 3, f_mapcheck},
715 {"match", 2, 4, f_match},
716 {"matchadd", 2, 5, f_matchadd},
717 {"matchaddpos", 2, 5, f_matchaddpos},
718 {"matcharg", 1, 1, f_matcharg},
719 {"matchdelete", 1, 1, f_matchdelete},
720 {"matchend", 2, 4, f_matchend},
721 {"matchlist", 2, 4, f_matchlist},
722 {"matchstr", 2, 4, f_matchstr},
723 {"matchstrpos", 2, 4, f_matchstrpos},
724 {"max", 1, 1, f_max},
725 {"min", 1, 1, f_min},
726#ifdef vim_mkdir
727 {"mkdir", 1, 3, f_mkdir},
728#endif
729 {"mode", 0, 1, f_mode},
730#ifdef FEAT_MZSCHEME
731 {"mzeval", 1, 1, f_mzeval},
732#endif
733 {"nextnonblank", 1, 1, f_nextnonblank},
734 {"nr2char", 1, 2, f_nr2char},
735 {"or", 2, 2, f_or},
736 {"pathshorten", 1, 1, f_pathshorten},
737#ifdef FEAT_PERL
738 {"perleval", 1, 1, f_perleval},
739#endif
740#ifdef FEAT_FLOAT
741 {"pow", 2, 2, f_pow},
742#endif
743 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100744 {"printf", 1, 19, f_printf},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200745 {"pumvisible", 0, 0, f_pumvisible},
746#ifdef FEAT_PYTHON3
747 {"py3eval", 1, 1, f_py3eval},
748#endif
749#ifdef FEAT_PYTHON
750 {"pyeval", 1, 1, f_pyeval},
751#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100752#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
753 {"pyxeval", 1, 1, f_pyxeval},
754#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200755 {"range", 1, 3, f_range},
756 {"readfile", 1, 3, f_readfile},
757 {"reltime", 0, 2, f_reltime},
758#ifdef FEAT_FLOAT
759 {"reltimefloat", 1, 1, f_reltimefloat},
760#endif
761 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100762 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200763 {"remote_foreground", 1, 1, f_remote_foreground},
764 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100765 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200766 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100767 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200768 {"remove", 2, 3, f_remove},
769 {"rename", 2, 2, f_rename},
770 {"repeat", 2, 2, f_repeat},
771 {"resolve", 1, 1, f_resolve},
772 {"reverse", 1, 1, f_reverse},
773#ifdef FEAT_FLOAT
774 {"round", 1, 1, f_round},
775#endif
776 {"screenattr", 2, 2, f_screenattr},
777 {"screenchar", 2, 2, f_screenchar},
778 {"screencol", 0, 0, f_screencol},
779 {"screenrow", 0, 0, f_screenrow},
780 {"search", 1, 4, f_search},
781 {"searchdecl", 1, 3, f_searchdecl},
782 {"searchpair", 3, 7, f_searchpair},
783 {"searchpairpos", 3, 7, f_searchpairpos},
784 {"searchpos", 1, 4, f_searchpos},
785 {"server2client", 2, 2, f_server2client},
786 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200787 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200788 {"setbufvar", 3, 3, f_setbufvar},
789 {"setcharsearch", 1, 1, f_setcharsearch},
790 {"setcmdpos", 1, 1, f_setcmdpos},
791 {"setfperm", 2, 2, f_setfperm},
792 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200793 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200794 {"setmatches", 1, 1, f_setmatches},
795 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200796 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200797 {"setreg", 2, 3, f_setreg},
798 {"settabvar", 3, 3, f_settabvar},
799 {"settabwinvar", 4, 4, f_settabwinvar},
800 {"setwinvar", 3, 3, f_setwinvar},
801#ifdef FEAT_CRYPT
802 {"sha256", 1, 1, f_sha256},
803#endif
804 {"shellescape", 1, 2, f_shellescape},
805 {"shiftwidth", 0, 0, f_shiftwidth},
806 {"simplify", 1, 1, f_simplify},
807#ifdef FEAT_FLOAT
808 {"sin", 1, 1, f_sin},
809 {"sinh", 1, 1, f_sinh},
810#endif
811 {"sort", 1, 3, f_sort},
812 {"soundfold", 1, 1, f_soundfold},
813 {"spellbadword", 0, 1, f_spellbadword},
814 {"spellsuggest", 1, 3, f_spellsuggest},
815 {"split", 1, 3, f_split},
816#ifdef FEAT_FLOAT
817 {"sqrt", 1, 1, f_sqrt},
818 {"str2float", 1, 1, f_str2float},
819#endif
820 {"str2nr", 1, 2, f_str2nr},
821 {"strcharpart", 2, 3, f_strcharpart},
822 {"strchars", 1, 2, f_strchars},
823 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
824#ifdef HAVE_STRFTIME
825 {"strftime", 1, 2, f_strftime},
826#endif
827 {"strgetchar", 2, 2, f_strgetchar},
828 {"stridx", 2, 3, f_stridx},
829 {"string", 1, 1, f_string},
830 {"strlen", 1, 1, f_strlen},
831 {"strpart", 2, 3, f_strpart},
832 {"strridx", 2, 3, f_strridx},
833 {"strtrans", 1, 1, f_strtrans},
834 {"strwidth", 1, 1, f_strwidth},
835 {"submatch", 1, 2, f_submatch},
836 {"substitute", 4, 4, f_substitute},
837 {"synID", 3, 3, f_synID},
838 {"synIDattr", 2, 3, f_synIDattr},
839 {"synIDtrans", 1, 1, f_synIDtrans},
840 {"synconcealed", 2, 2, f_synconcealed},
841 {"synstack", 2, 2, f_synstack},
842 {"system", 1, 2, f_system},
843 {"systemlist", 1, 2, f_systemlist},
844 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
845 {"tabpagenr", 0, 1, f_tabpagenr},
846 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
847 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100848 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200849#ifdef FEAT_FLOAT
850 {"tan", 1, 1, f_tan},
851 {"tanh", 1, 1, f_tanh},
852#endif
853 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200854#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100855 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
856 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100857 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200858 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200859 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200860 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200861 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200862 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200863 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200864 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200865 {"term_getstatus", 1, 1, f_term_getstatus},
866 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200867 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200868 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200869 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200870 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100871 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100872 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200873 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200874 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200875#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200876 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
877 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200878 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200879 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100880 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200881#ifdef FEAT_JOB_CHANNEL
882 {"test_null_channel", 0, 0, f_test_null_channel},
883#endif
884 {"test_null_dict", 0, 0, f_test_null_dict},
885#ifdef FEAT_JOB_CHANNEL
886 {"test_null_job", 0, 0, f_test_null_job},
887#endif
888 {"test_null_list", 0, 0, f_test_null_list},
889 {"test_null_partial", 0, 0, f_test_null_partial},
890 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100891 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200892 {"test_settime", 1, 1, f_test_settime},
893#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200894 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200895 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200896 {"timer_start", 2, 3, f_timer_start},
897 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200898 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200899#endif
900 {"tolower", 1, 1, f_tolower},
901 {"toupper", 1, 1, f_toupper},
902 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100903 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200904#ifdef FEAT_FLOAT
905 {"trunc", 1, 1, f_trunc},
906#endif
907 {"type", 1, 1, f_type},
908 {"undofile", 1, 1, f_undofile},
909 {"undotree", 0, 0, f_undotree},
910 {"uniq", 1, 3, f_uniq},
911 {"values", 1, 1, f_values},
912 {"virtcol", 1, 1, f_virtcol},
913 {"visualmode", 0, 1, f_visualmode},
914 {"wildmenumode", 0, 0, f_wildmenumode},
915 {"win_findbuf", 1, 1, f_win_findbuf},
916 {"win_getid", 0, 2, f_win_getid},
917 {"win_gotoid", 1, 1, f_win_gotoid},
918 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
919 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100920 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200921 {"winbufnr", 1, 1, f_winbufnr},
922 {"wincol", 0, 0, f_wincol},
923 {"winheight", 1, 1, f_winheight},
924 {"winline", 0, 0, f_winline},
925 {"winnr", 0, 1, f_winnr},
926 {"winrestcmd", 0, 0, f_winrestcmd},
927 {"winrestview", 1, 1, f_winrestview},
928 {"winsaveview", 0, 0, f_winsaveview},
929 {"winwidth", 1, 1, f_winwidth},
930 {"wordcount", 0, 0, f_wordcount},
931 {"writefile", 2, 3, f_writefile},
932 {"xor", 2, 2, f_xor},
933};
934
935#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
936
937/*
938 * Function given to ExpandGeneric() to obtain the list of internal
939 * or user defined function names.
940 */
941 char_u *
942get_function_name(expand_T *xp, int idx)
943{
944 static int intidx = -1;
945 char_u *name;
946
947 if (idx == 0)
948 intidx = -1;
949 if (intidx < 0)
950 {
951 name = get_user_func_name(xp, idx);
952 if (name != NULL)
953 return name;
954 }
955 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
956 {
957 STRCPY(IObuff, functions[intidx].f_name);
958 STRCAT(IObuff, "(");
959 if (functions[intidx].f_max_argc == 0)
960 STRCAT(IObuff, ")");
961 return IObuff;
962 }
963
964 return NULL;
965}
966
967/*
968 * Function given to ExpandGeneric() to obtain the list of internal or
969 * user defined variable or function names.
970 */
971 char_u *
972get_expr_name(expand_T *xp, int idx)
973{
974 static int intidx = -1;
975 char_u *name;
976
977 if (idx == 0)
978 intidx = -1;
979 if (intidx < 0)
980 {
981 name = get_function_name(xp, idx);
982 if (name != NULL)
983 return name;
984 }
985 return get_user_var_name(xp, ++intidx);
986}
987
988#endif /* FEAT_CMDL_COMPL */
989
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200990/*
991 * Find internal function in table above.
992 * Return index, or -1 if not found
993 */
994 int
995find_internal_func(
996 char_u *name) /* name of the function */
997{
998 int first = 0;
999 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1000 int cmp;
1001 int x;
1002
1003 /*
1004 * Find the function name in the table. Binary search.
1005 */
1006 while (first <= last)
1007 {
1008 x = first + ((unsigned)(last - first) >> 1);
1009 cmp = STRCMP(name, functions[x].f_name);
1010 if (cmp < 0)
1011 last = x - 1;
1012 else if (cmp > 0)
1013 first = x + 1;
1014 else
1015 return x;
1016 }
1017 return -1;
1018}
1019
1020 int
1021call_internal_func(
1022 char_u *name,
1023 int argcount,
1024 typval_T *argvars,
1025 typval_T *rettv)
1026{
1027 int i;
1028
1029 i = find_internal_func(name);
1030 if (i < 0)
1031 return ERROR_UNKNOWN;
1032 if (argcount < functions[i].f_min_argc)
1033 return ERROR_TOOFEW;
1034 if (argcount > functions[i].f_max_argc)
1035 return ERROR_TOOMANY;
1036 argvars[argcount].v_type = VAR_UNKNOWN;
1037 functions[i].f_func(argvars, rettv);
1038 return ERROR_NONE;
1039}
1040
1041/*
1042 * Return TRUE for a non-zero Number and a non-empty String.
1043 */
1044 static int
1045non_zero_arg(typval_T *argvars)
1046{
1047 return ((argvars[0].v_type == VAR_NUMBER
1048 && argvars[0].vval.v_number != 0)
1049 || (argvars[0].v_type == VAR_SPECIAL
1050 && argvars[0].vval.v_number == VVAL_TRUE)
1051 || (argvars[0].v_type == VAR_STRING
1052 && argvars[0].vval.v_string != NULL
1053 && *argvars[0].vval.v_string != NUL));
1054}
1055
1056/*
1057 * Get the lnum from the first argument.
1058 * Also accepts ".", "$", etc., but that only works for the current buffer.
1059 * Returns -1 on error.
1060 */
1061 static linenr_T
1062get_tv_lnum(typval_T *argvars)
1063{
1064 typval_T rettv;
1065 linenr_T lnum;
1066
1067 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1068 if (lnum == 0) /* no valid number, try using line() */
1069 {
1070 rettv.v_type = VAR_NUMBER;
1071 f_line(argvars, &rettv);
1072 lnum = (linenr_T)rettv.vval.v_number;
1073 clear_tv(&rettv);
1074 }
1075 return lnum;
1076}
1077
1078#ifdef FEAT_FLOAT
1079static int get_float_arg(typval_T *argvars, float_T *f);
1080
1081/*
1082 * Get the float value of "argvars[0]" into "f".
1083 * Returns FAIL when the argument is not a Number or Float.
1084 */
1085 static int
1086get_float_arg(typval_T *argvars, float_T *f)
1087{
1088 if (argvars[0].v_type == VAR_FLOAT)
1089 {
1090 *f = argvars[0].vval.v_float;
1091 return OK;
1092 }
1093 if (argvars[0].v_type == VAR_NUMBER)
1094 {
1095 *f = (float_T)argvars[0].vval.v_number;
1096 return OK;
1097 }
1098 EMSG(_("E808: Number or Float required"));
1099 return FAIL;
1100}
1101
1102/*
1103 * "abs(expr)" function
1104 */
1105 static void
1106f_abs(typval_T *argvars, typval_T *rettv)
1107{
1108 if (argvars[0].v_type == VAR_FLOAT)
1109 {
1110 rettv->v_type = VAR_FLOAT;
1111 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1112 }
1113 else
1114 {
1115 varnumber_T n;
1116 int error = FALSE;
1117
1118 n = get_tv_number_chk(&argvars[0], &error);
1119 if (error)
1120 rettv->vval.v_number = -1;
1121 else if (n > 0)
1122 rettv->vval.v_number = n;
1123 else
1124 rettv->vval.v_number = -n;
1125 }
1126}
1127
1128/*
1129 * "acos()" function
1130 */
1131 static void
1132f_acos(typval_T *argvars, typval_T *rettv)
1133{
1134 float_T f = 0.0;
1135
1136 rettv->v_type = VAR_FLOAT;
1137 if (get_float_arg(argvars, &f) == OK)
1138 rettv->vval.v_float = acos(f);
1139 else
1140 rettv->vval.v_float = 0.0;
1141}
1142#endif
1143
1144/*
1145 * "add(list, item)" function
1146 */
1147 static void
1148f_add(typval_T *argvars, typval_T *rettv)
1149{
1150 list_T *l;
1151
1152 rettv->vval.v_number = 1; /* Default: Failed */
1153 if (argvars[0].v_type == VAR_LIST)
1154 {
1155 if ((l = argvars[0].vval.v_list) != NULL
1156 && !tv_check_lock(l->lv_lock,
1157 (char_u *)N_("add() argument"), TRUE)
1158 && list_append_tv(l, &argvars[1]) == OK)
1159 copy_tv(&argvars[0], rettv);
1160 }
1161 else
1162 EMSG(_(e_listreq));
1163}
1164
1165/*
1166 * "and(expr, expr)" function
1167 */
1168 static void
1169f_and(typval_T *argvars, typval_T *rettv)
1170{
1171 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1172 & get_tv_number_chk(&argvars[1], NULL);
1173}
1174
1175/*
1176 * "append(lnum, string/list)" function
1177 */
1178 static void
1179f_append(typval_T *argvars, typval_T *rettv)
1180{
1181 long lnum;
1182 char_u *line;
1183 list_T *l = NULL;
1184 listitem_T *li = NULL;
1185 typval_T *tv;
1186 long added = 0;
1187
1188 /* When coming here from Insert mode, sync undo, so that this can be
1189 * undone separately from what was previously inserted. */
1190 if (u_sync_once == 2)
1191 {
1192 u_sync_once = 1; /* notify that u_sync() was called */
1193 u_sync(TRUE);
1194 }
1195
1196 lnum = get_tv_lnum(argvars);
1197 if (lnum >= 0
1198 && lnum <= curbuf->b_ml.ml_line_count
1199 && u_save(lnum, lnum + 1) == OK)
1200 {
1201 if (argvars[1].v_type == VAR_LIST)
1202 {
1203 l = argvars[1].vval.v_list;
1204 if (l == NULL)
1205 return;
1206 li = l->lv_first;
1207 }
1208 for (;;)
1209 {
1210 if (l == NULL)
1211 tv = &argvars[1]; /* append a string */
1212 else if (li == NULL)
1213 break; /* end of list */
1214 else
1215 tv = &li->li_tv; /* append item from list */
1216 line = get_tv_string_chk(tv);
1217 if (line == NULL) /* type error */
1218 {
1219 rettv->vval.v_number = 1; /* Failed */
1220 break;
1221 }
1222 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1223 ++added;
1224 if (l == NULL)
1225 break;
1226 li = li->li_next;
1227 }
1228
1229 appended_lines_mark(lnum, added);
1230 if (curwin->w_cursor.lnum > lnum)
1231 curwin->w_cursor.lnum += added;
1232 }
1233 else
1234 rettv->vval.v_number = 1; /* Failed */
1235}
1236
1237/*
1238 * "argc()" function
1239 */
1240 static void
1241f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1242{
1243 rettv->vval.v_number = ARGCOUNT;
1244}
1245
1246/*
1247 * "argidx()" function
1248 */
1249 static void
1250f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1251{
1252 rettv->vval.v_number = curwin->w_arg_idx;
1253}
1254
1255/*
1256 * "arglistid()" function
1257 */
1258 static void
1259f_arglistid(typval_T *argvars, typval_T *rettv)
1260{
1261 win_T *wp;
1262
1263 rettv->vval.v_number = -1;
1264 wp = find_tabwin(&argvars[0], &argvars[1]);
1265 if (wp != NULL)
1266 rettv->vval.v_number = wp->w_alist->id;
1267}
1268
1269/*
1270 * "argv(nr)" function
1271 */
1272 static void
1273f_argv(typval_T *argvars, typval_T *rettv)
1274{
1275 int idx;
1276
1277 if (argvars[0].v_type != VAR_UNKNOWN)
1278 {
1279 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1280 if (idx >= 0 && idx < ARGCOUNT)
1281 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1282 else
1283 rettv->vval.v_string = NULL;
1284 rettv->v_type = VAR_STRING;
1285 }
1286 else if (rettv_list_alloc(rettv) == OK)
1287 for (idx = 0; idx < ARGCOUNT; ++idx)
1288 list_append_string(rettv->vval.v_list,
1289 alist_name(&ARGLIST[idx]), -1);
1290}
1291
1292/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001293 * "assert_beeps(cmd [, error])" function
1294 */
1295 static void
1296f_assert_beeps(typval_T *argvars, typval_T *rettv UNUSED)
1297{
1298 assert_beeps(argvars);
1299}
1300
1301/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001302 * "assert_equal(expected, actual[, msg])" function
1303 */
1304 static void
1305f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1306{
1307 assert_equal_common(argvars, ASSERT_EQUAL);
1308}
1309
1310/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001311 * "assert_equalfile(fname-one, fname-two)" function
1312 */
1313 static void
1314f_assert_equalfile(typval_T *argvars, typval_T *rettv UNUSED)
1315{
1316 assert_equalfile(argvars);
1317}
1318
1319/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001320 * "assert_notequal(expected, actual[, msg])" function
1321 */
1322 static void
1323f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1324{
1325 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1326}
1327
1328/*
1329 * "assert_exception(string[, msg])" function
1330 */
1331 static void
1332f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1333{
1334 assert_exception(argvars);
1335}
1336
1337/*
1338 * "assert_fails(cmd [, error])" function
1339 */
1340 static void
1341f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1342{
1343 assert_fails(argvars);
1344}
1345
1346/*
1347 * "assert_false(actual[, msg])" function
1348 */
1349 static void
1350f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1351{
1352 assert_bool(argvars, FALSE);
1353}
1354
1355/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001356 * "assert_inrange(lower, upper[, msg])" function
1357 */
1358 static void
1359f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1360{
1361 assert_inrange(argvars);
1362}
1363
1364/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001365 * "assert_match(pattern, actual[, msg])" function
1366 */
1367 static void
1368f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1369{
1370 assert_match_common(argvars, ASSERT_MATCH);
1371}
1372
1373/*
1374 * "assert_notmatch(pattern, actual[, msg])" function
1375 */
1376 static void
1377f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1378{
1379 assert_match_common(argvars, ASSERT_NOTMATCH);
1380}
1381
1382/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001383 * "assert_report(msg)" function
1384 */
1385 static void
1386f_assert_report(typval_T *argvars, typval_T *rettv UNUSED)
1387{
1388 assert_report(argvars);
1389}
1390
1391/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001392 * "assert_true(actual[, msg])" function
1393 */
1394 static void
1395f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1396{
1397 assert_bool(argvars, TRUE);
1398}
1399
1400#ifdef FEAT_FLOAT
1401/*
1402 * "asin()" function
1403 */
1404 static void
1405f_asin(typval_T *argvars, typval_T *rettv)
1406{
1407 float_T f = 0.0;
1408
1409 rettv->v_type = VAR_FLOAT;
1410 if (get_float_arg(argvars, &f) == OK)
1411 rettv->vval.v_float = asin(f);
1412 else
1413 rettv->vval.v_float = 0.0;
1414}
1415
1416/*
1417 * "atan()" function
1418 */
1419 static void
1420f_atan(typval_T *argvars, typval_T *rettv)
1421{
1422 float_T f = 0.0;
1423
1424 rettv->v_type = VAR_FLOAT;
1425 if (get_float_arg(argvars, &f) == OK)
1426 rettv->vval.v_float = atan(f);
1427 else
1428 rettv->vval.v_float = 0.0;
1429}
1430
1431/*
1432 * "atan2()" function
1433 */
1434 static void
1435f_atan2(typval_T *argvars, typval_T *rettv)
1436{
1437 float_T fx = 0.0, fy = 0.0;
1438
1439 rettv->v_type = VAR_FLOAT;
1440 if (get_float_arg(argvars, &fx) == OK
1441 && get_float_arg(&argvars[1], &fy) == OK)
1442 rettv->vval.v_float = atan2(fx, fy);
1443 else
1444 rettv->vval.v_float = 0.0;
1445}
1446#endif
1447
1448/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001449 * "balloon_show()" function
1450 */
1451#ifdef FEAT_BEVAL
1452 static void
1453f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1454{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001455 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001456 {
1457 if (argvars[0].v_type == VAR_LIST
1458# ifdef FEAT_GUI
1459 && !gui.in_use
1460# endif
1461 )
1462 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1463 else
1464 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1465 }
1466}
1467
Bram Moolenaar669a8282017-11-19 20:13:05 +01001468# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001469 static void
1470f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1471{
1472 if (rettv_list_alloc(rettv) == OK)
1473 {
1474 char_u *msg = get_tv_string_chk(&argvars[0]);
1475
1476 if (msg != NULL)
1477 {
1478 pumitem_T *array;
1479 int size = split_message(msg, &array);
1480 int i;
1481
1482 /* Skip the first and last item, they are always empty. */
1483 for (i = 1; i < size - 1; ++i)
1484 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001485 while (size > 0)
1486 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001487 vim_free(array);
1488 }
1489 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001490}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001491# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001492#endif
1493
1494/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001495 * "browse(save, title, initdir, default)" function
1496 */
1497 static void
1498f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1499{
1500#ifdef FEAT_BROWSE
1501 int save;
1502 char_u *title;
1503 char_u *initdir;
1504 char_u *defname;
1505 char_u buf[NUMBUFLEN];
1506 char_u buf2[NUMBUFLEN];
1507 int error = FALSE;
1508
1509 save = (int)get_tv_number_chk(&argvars[0], &error);
1510 title = get_tv_string_chk(&argvars[1]);
1511 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1512 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1513
1514 if (error || title == NULL || initdir == NULL || defname == NULL)
1515 rettv->vval.v_string = NULL;
1516 else
1517 rettv->vval.v_string =
1518 do_browse(save ? BROWSE_SAVE : 0,
1519 title, defname, NULL, initdir, NULL, curbuf);
1520#else
1521 rettv->vval.v_string = NULL;
1522#endif
1523 rettv->v_type = VAR_STRING;
1524}
1525
1526/*
1527 * "browsedir(title, initdir)" function
1528 */
1529 static void
1530f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1531{
1532#ifdef FEAT_BROWSE
1533 char_u *title;
1534 char_u *initdir;
1535 char_u buf[NUMBUFLEN];
1536
1537 title = get_tv_string_chk(&argvars[0]);
1538 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1539
1540 if (title == NULL || initdir == NULL)
1541 rettv->vval.v_string = NULL;
1542 else
1543 rettv->vval.v_string = do_browse(BROWSE_DIR,
1544 title, NULL, NULL, initdir, NULL, curbuf);
1545#else
1546 rettv->vval.v_string = NULL;
1547#endif
1548 rettv->v_type = VAR_STRING;
1549}
1550
1551static buf_T *find_buffer(typval_T *avar);
1552
1553/*
1554 * Find a buffer by number or exact name.
1555 */
1556 static buf_T *
1557find_buffer(typval_T *avar)
1558{
1559 buf_T *buf = NULL;
1560
1561 if (avar->v_type == VAR_NUMBER)
1562 buf = buflist_findnr((int)avar->vval.v_number);
1563 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1564 {
1565 buf = buflist_findname_exp(avar->vval.v_string);
1566 if (buf == NULL)
1567 {
1568 /* No full path name match, try a match with a URL or a "nofile"
1569 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001570 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001571 if (buf->b_fname != NULL
1572 && (path_with_url(buf->b_fname)
1573#ifdef FEAT_QUICKFIX
1574 || bt_nofile(buf)
1575#endif
1576 )
1577 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1578 break;
1579 }
1580 }
1581 return buf;
1582}
1583
1584/*
1585 * "bufexists(expr)" function
1586 */
1587 static void
1588f_bufexists(typval_T *argvars, typval_T *rettv)
1589{
1590 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1591}
1592
1593/*
1594 * "buflisted(expr)" function
1595 */
1596 static void
1597f_buflisted(typval_T *argvars, typval_T *rettv)
1598{
1599 buf_T *buf;
1600
1601 buf = find_buffer(&argvars[0]);
1602 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1603}
1604
1605/*
1606 * "bufloaded(expr)" function
1607 */
1608 static void
1609f_bufloaded(typval_T *argvars, typval_T *rettv)
1610{
1611 buf_T *buf;
1612
1613 buf = find_buffer(&argvars[0]);
1614 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1615}
1616
1617 buf_T *
1618buflist_find_by_name(char_u *name, int curtab_only)
1619{
1620 int save_magic;
1621 char_u *save_cpo;
1622 buf_T *buf;
1623
1624 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1625 save_magic = p_magic;
1626 p_magic = TRUE;
1627 save_cpo = p_cpo;
1628 p_cpo = (char_u *)"";
1629
1630 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1631 TRUE, FALSE, curtab_only));
1632
1633 p_magic = save_magic;
1634 p_cpo = save_cpo;
1635 return buf;
1636}
1637
1638/*
1639 * Get buffer by number or pattern.
1640 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001641 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001642get_buf_tv(typval_T *tv, int curtab_only)
1643{
1644 char_u *name = tv->vval.v_string;
1645 buf_T *buf;
1646
1647 if (tv->v_type == VAR_NUMBER)
1648 return buflist_findnr((int)tv->vval.v_number);
1649 if (tv->v_type != VAR_STRING)
1650 return NULL;
1651 if (name == NULL || *name == NUL)
1652 return curbuf;
1653 if (name[0] == '$' && name[1] == NUL)
1654 return lastbuf;
1655
1656 buf = buflist_find_by_name(name, curtab_only);
1657
1658 /* If not found, try expanding the name, like done for bufexists(). */
1659 if (buf == NULL)
1660 buf = find_buffer(tv);
1661
1662 return buf;
1663}
1664
1665/*
1666 * "bufname(expr)" function
1667 */
1668 static void
1669f_bufname(typval_T *argvars, typval_T *rettv)
1670{
1671 buf_T *buf;
1672
1673 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1674 ++emsg_off;
1675 buf = get_buf_tv(&argvars[0], FALSE);
1676 rettv->v_type = VAR_STRING;
1677 if (buf != NULL && buf->b_fname != NULL)
1678 rettv->vval.v_string = vim_strsave(buf->b_fname);
1679 else
1680 rettv->vval.v_string = NULL;
1681 --emsg_off;
1682}
1683
1684/*
1685 * "bufnr(expr)" function
1686 */
1687 static void
1688f_bufnr(typval_T *argvars, typval_T *rettv)
1689{
1690 buf_T *buf;
1691 int error = FALSE;
1692 char_u *name;
1693
1694 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1695 ++emsg_off;
1696 buf = get_buf_tv(&argvars[0], FALSE);
1697 --emsg_off;
1698
1699 /* If the buffer isn't found and the second argument is not zero create a
1700 * new buffer. */
1701 if (buf == NULL
1702 && argvars[1].v_type != VAR_UNKNOWN
1703 && get_tv_number_chk(&argvars[1], &error) != 0
1704 && !error
1705 && (name = get_tv_string_chk(&argvars[0])) != NULL
1706 && !error)
1707 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1708
1709 if (buf != NULL)
1710 rettv->vval.v_number = buf->b_fnum;
1711 else
1712 rettv->vval.v_number = -1;
1713}
1714
1715 static void
1716buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1717{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001718 win_T *wp;
1719 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001720 buf_T *buf;
1721
1722 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1723 ++emsg_off;
1724 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001725 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001726 {
1727 ++winnr;
1728 if (wp->w_buffer == buf)
1729 break;
1730 }
1731 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001732 --emsg_off;
1733}
1734
1735/*
1736 * "bufwinid(nr)" function
1737 */
1738 static void
1739f_bufwinid(typval_T *argvars, typval_T *rettv)
1740{
1741 buf_win_common(argvars, rettv, FALSE);
1742}
1743
1744/*
1745 * "bufwinnr(nr)" function
1746 */
1747 static void
1748f_bufwinnr(typval_T *argvars, typval_T *rettv)
1749{
1750 buf_win_common(argvars, rettv, TRUE);
1751}
1752
1753/*
1754 * "byte2line(byte)" function
1755 */
1756 static void
1757f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1758{
1759#ifndef FEAT_BYTEOFF
1760 rettv->vval.v_number = -1;
1761#else
1762 long boff = 0;
1763
1764 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1765 if (boff < 0)
1766 rettv->vval.v_number = -1;
1767 else
1768 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1769 (linenr_T)0, &boff);
1770#endif
1771}
1772
1773 static void
1774byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1775{
1776#ifdef FEAT_MBYTE
1777 char_u *t;
1778#endif
1779 char_u *str;
1780 varnumber_T idx;
1781
1782 str = get_tv_string_chk(&argvars[0]);
1783 idx = get_tv_number_chk(&argvars[1], NULL);
1784 rettv->vval.v_number = -1;
1785 if (str == NULL || idx < 0)
1786 return;
1787
1788#ifdef FEAT_MBYTE
1789 t = str;
1790 for ( ; idx > 0; idx--)
1791 {
1792 if (*t == NUL) /* EOL reached */
1793 return;
1794 if (enc_utf8 && comp)
1795 t += utf_ptr2len(t);
1796 else
1797 t += (*mb_ptr2len)(t);
1798 }
1799 rettv->vval.v_number = (varnumber_T)(t - str);
1800#else
1801 if ((size_t)idx <= STRLEN(str))
1802 rettv->vval.v_number = idx;
1803#endif
1804}
1805
1806/*
1807 * "byteidx()" function
1808 */
1809 static void
1810f_byteidx(typval_T *argvars, typval_T *rettv)
1811{
1812 byteidx(argvars, rettv, FALSE);
1813}
1814
1815/*
1816 * "byteidxcomp()" function
1817 */
1818 static void
1819f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1820{
1821 byteidx(argvars, rettv, TRUE);
1822}
1823
1824/*
1825 * "call(func, arglist [, dict])" function
1826 */
1827 static void
1828f_call(typval_T *argvars, typval_T *rettv)
1829{
1830 char_u *func;
1831 partial_T *partial = NULL;
1832 dict_T *selfdict = NULL;
1833
1834 if (argvars[1].v_type != VAR_LIST)
1835 {
1836 EMSG(_(e_listreq));
1837 return;
1838 }
1839 if (argvars[1].vval.v_list == NULL)
1840 return;
1841
1842 if (argvars[0].v_type == VAR_FUNC)
1843 func = argvars[0].vval.v_string;
1844 else if (argvars[0].v_type == VAR_PARTIAL)
1845 {
1846 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001847 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001848 }
1849 else
1850 func = get_tv_string(&argvars[0]);
1851 if (*func == NUL)
1852 return; /* type error or empty name */
1853
1854 if (argvars[2].v_type != VAR_UNKNOWN)
1855 {
1856 if (argvars[2].v_type != VAR_DICT)
1857 {
1858 EMSG(_(e_dictreq));
1859 return;
1860 }
1861 selfdict = argvars[2].vval.v_dict;
1862 }
1863
1864 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1865}
1866
1867#ifdef FEAT_FLOAT
1868/*
1869 * "ceil({float})" function
1870 */
1871 static void
1872f_ceil(typval_T *argvars, typval_T *rettv)
1873{
1874 float_T f = 0.0;
1875
1876 rettv->v_type = VAR_FLOAT;
1877 if (get_float_arg(argvars, &f) == OK)
1878 rettv->vval.v_float = ceil(f);
1879 else
1880 rettv->vval.v_float = 0.0;
1881}
1882#endif
1883
1884#ifdef FEAT_JOB_CHANNEL
1885/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001886 * "ch_canread()" function
1887 */
1888 static void
1889f_ch_canread(typval_T *argvars, typval_T *rettv)
1890{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001891 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001892
1893 rettv->vval.v_number = 0;
1894 if (channel != NULL)
1895 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1896 || channel_has_readahead(channel, PART_OUT)
1897 || channel_has_readahead(channel, PART_ERR);
1898}
1899
1900/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001901 * "ch_close()" function
1902 */
1903 static void
1904f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1905{
1906 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1907
1908 if (channel != NULL)
1909 {
1910 channel_close(channel, FALSE);
1911 channel_clear(channel);
1912 }
1913}
1914
1915/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001916 * "ch_close()" function
1917 */
1918 static void
1919f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1920{
1921 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1922
1923 if (channel != NULL)
1924 channel_close_in(channel);
1925}
1926
1927/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001928 * "ch_getbufnr()" function
1929 */
1930 static void
1931f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1932{
1933 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1934
1935 rettv->vval.v_number = -1;
1936 if (channel != NULL)
1937 {
1938 char_u *what = get_tv_string(&argvars[1]);
1939 int part;
1940
1941 if (STRCMP(what, "err") == 0)
1942 part = PART_ERR;
1943 else if (STRCMP(what, "out") == 0)
1944 part = PART_OUT;
1945 else if (STRCMP(what, "in") == 0)
1946 part = PART_IN;
1947 else
1948 part = PART_SOCK;
1949 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1950 rettv->vval.v_number =
1951 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1952 }
1953}
1954
1955/*
1956 * "ch_getjob()" function
1957 */
1958 static void
1959f_ch_getjob(typval_T *argvars, typval_T *rettv)
1960{
1961 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1962
1963 if (channel != NULL)
1964 {
1965 rettv->v_type = VAR_JOB;
1966 rettv->vval.v_job = channel->ch_job;
1967 if (channel->ch_job != NULL)
1968 ++channel->ch_job->jv_refcount;
1969 }
1970}
1971
1972/*
1973 * "ch_info()" function
1974 */
1975 static void
1976f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1977{
1978 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1979
1980 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1981 channel_info(channel, rettv->vval.v_dict);
1982}
1983
1984/*
1985 * "ch_log()" function
1986 */
1987 static void
1988f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1989{
1990 char_u *msg = get_tv_string(&argvars[0]);
1991 channel_T *channel = NULL;
1992
1993 if (argvars[1].v_type != VAR_UNKNOWN)
1994 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1995
1996 ch_log(channel, (char *)msg);
1997}
1998
1999/*
2000 * "ch_logfile()" function
2001 */
2002 static void
2003f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2004{
2005 char_u *fname;
2006 char_u *opt = (char_u *)"";
2007 char_u buf[NUMBUFLEN];
2008
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002009 /* Don't open a file in restricted mode. */
2010 if (check_restricted() || check_secure())
2011 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002012 fname = get_tv_string(&argvars[0]);
2013 if (argvars[1].v_type == VAR_STRING)
2014 opt = get_tv_string_buf(&argvars[1], buf);
2015 ch_logfile(fname, opt);
2016}
2017
2018/*
2019 * "ch_open()" function
2020 */
2021 static void
2022f_ch_open(typval_T *argvars, typval_T *rettv)
2023{
2024 rettv->v_type = VAR_CHANNEL;
2025 if (check_restricted() || check_secure())
2026 return;
2027 rettv->vval.v_channel = channel_open_func(argvars);
2028}
2029
2030/*
2031 * "ch_read()" function
2032 */
2033 static void
2034f_ch_read(typval_T *argvars, typval_T *rettv)
2035{
2036 common_channel_read(argvars, rettv, FALSE);
2037}
2038
2039/*
2040 * "ch_readraw()" function
2041 */
2042 static void
2043f_ch_readraw(typval_T *argvars, typval_T *rettv)
2044{
2045 common_channel_read(argvars, rettv, TRUE);
2046}
2047
2048/*
2049 * "ch_evalexpr()" function
2050 */
2051 static void
2052f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2053{
2054 ch_expr_common(argvars, rettv, TRUE);
2055}
2056
2057/*
2058 * "ch_sendexpr()" function
2059 */
2060 static void
2061f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2062{
2063 ch_expr_common(argvars, rettv, FALSE);
2064}
2065
2066/*
2067 * "ch_evalraw()" function
2068 */
2069 static void
2070f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2071{
2072 ch_raw_common(argvars, rettv, TRUE);
2073}
2074
2075/*
2076 * "ch_sendraw()" function
2077 */
2078 static void
2079f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2080{
2081 ch_raw_common(argvars, rettv, FALSE);
2082}
2083
2084/*
2085 * "ch_setoptions()" function
2086 */
2087 static void
2088f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2089{
2090 channel_T *channel;
2091 jobopt_T opt;
2092
2093 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2094 if (channel == NULL)
2095 return;
2096 clear_job_options(&opt);
2097 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002098 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002099 channel_set_options(channel, &opt);
2100 free_job_options(&opt);
2101}
2102
2103/*
2104 * "ch_status()" function
2105 */
2106 static void
2107f_ch_status(typval_T *argvars, typval_T *rettv)
2108{
2109 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002110 jobopt_T opt;
2111 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002112
2113 /* return an empty string by default */
2114 rettv->v_type = VAR_STRING;
2115 rettv->vval.v_string = NULL;
2116
2117 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002118
2119 if (argvars[1].v_type != VAR_UNKNOWN)
2120 {
2121 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002122 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002123 && (opt.jo_set & JO_PART))
2124 part = opt.jo_part;
2125 }
2126
2127 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002128}
2129#endif
2130
2131/*
2132 * "changenr()" function
2133 */
2134 static void
2135f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2136{
2137 rettv->vval.v_number = curbuf->b_u_seq_cur;
2138}
2139
2140/*
2141 * "char2nr(string)" function
2142 */
2143 static void
2144f_char2nr(typval_T *argvars, typval_T *rettv)
2145{
2146#ifdef FEAT_MBYTE
2147 if (has_mbyte)
2148 {
2149 int utf8 = 0;
2150
2151 if (argvars[1].v_type != VAR_UNKNOWN)
2152 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2153
2154 if (utf8)
2155 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2156 else
2157 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2158 }
2159 else
2160#endif
2161 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2162}
2163
2164/*
2165 * "cindent(lnum)" function
2166 */
2167 static void
2168f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2169{
2170#ifdef FEAT_CINDENT
2171 pos_T pos;
2172 linenr_T lnum;
2173
2174 pos = curwin->w_cursor;
2175 lnum = get_tv_lnum(argvars);
2176 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2177 {
2178 curwin->w_cursor.lnum = lnum;
2179 rettv->vval.v_number = get_c_indent();
2180 curwin->w_cursor = pos;
2181 }
2182 else
2183#endif
2184 rettv->vval.v_number = -1;
2185}
2186
2187/*
2188 * "clearmatches()" function
2189 */
2190 static void
2191f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2192{
2193#ifdef FEAT_SEARCH_EXTRA
2194 clear_matches(curwin);
2195#endif
2196}
2197
2198/*
2199 * "col(string)" function
2200 */
2201 static void
2202f_col(typval_T *argvars, typval_T *rettv)
2203{
2204 colnr_T col = 0;
2205 pos_T *fp;
2206 int fnum = curbuf->b_fnum;
2207
2208 fp = var2fpos(&argvars[0], FALSE, &fnum);
2209 if (fp != NULL && fnum == curbuf->b_fnum)
2210 {
2211 if (fp->col == MAXCOL)
2212 {
2213 /* '> can be MAXCOL, get the length of the line then */
2214 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2215 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2216 else
2217 col = MAXCOL;
2218 }
2219 else
2220 {
2221 col = fp->col + 1;
2222#ifdef FEAT_VIRTUALEDIT
2223 /* col(".") when the cursor is on the NUL at the end of the line
2224 * because of "coladd" can be seen as an extra column. */
2225 if (virtual_active() && fp == &curwin->w_cursor)
2226 {
2227 char_u *p = ml_get_cursor();
2228
2229 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2230 curwin->w_virtcol - curwin->w_cursor.coladd))
2231 {
2232# ifdef FEAT_MBYTE
2233 int l;
2234
2235 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2236 col += l;
2237# else
2238 if (*p != NUL && p[1] == NUL)
2239 ++col;
2240# endif
2241 }
2242 }
2243#endif
2244 }
2245 }
2246 rettv->vval.v_number = col;
2247}
2248
2249#if defined(FEAT_INS_EXPAND)
2250/*
2251 * "complete()" function
2252 */
2253 static void
2254f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2255{
2256 int startcol;
2257
2258 if ((State & INSERT) == 0)
2259 {
2260 EMSG(_("E785: complete() can only be used in Insert mode"));
2261 return;
2262 }
2263
2264 /* Check for undo allowed here, because if something was already inserted
2265 * the line was already saved for undo and this check isn't done. */
2266 if (!undo_allowed())
2267 return;
2268
2269 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2270 {
2271 EMSG(_(e_invarg));
2272 return;
2273 }
2274
2275 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2276 if (startcol <= 0)
2277 return;
2278
2279 set_completion(startcol - 1, argvars[1].vval.v_list);
2280}
2281
2282/*
2283 * "complete_add()" function
2284 */
2285 static void
2286f_complete_add(typval_T *argvars, typval_T *rettv)
2287{
2288 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2289}
2290
2291/*
2292 * "complete_check()" function
2293 */
2294 static void
2295f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2296{
2297 int saved = RedrawingDisabled;
2298
2299 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002300 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002301 rettv->vval.v_number = compl_interrupted;
2302 RedrawingDisabled = saved;
2303}
2304#endif
2305
2306/*
2307 * "confirm(message, buttons[, default [, type]])" function
2308 */
2309 static void
2310f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2311{
2312#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2313 char_u *message;
2314 char_u *buttons = NULL;
2315 char_u buf[NUMBUFLEN];
2316 char_u buf2[NUMBUFLEN];
2317 int def = 1;
2318 int type = VIM_GENERIC;
2319 char_u *typestr;
2320 int error = FALSE;
2321
2322 message = get_tv_string_chk(&argvars[0]);
2323 if (message == NULL)
2324 error = TRUE;
2325 if (argvars[1].v_type != VAR_UNKNOWN)
2326 {
2327 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2328 if (buttons == NULL)
2329 error = TRUE;
2330 if (argvars[2].v_type != VAR_UNKNOWN)
2331 {
2332 def = (int)get_tv_number_chk(&argvars[2], &error);
2333 if (argvars[3].v_type != VAR_UNKNOWN)
2334 {
2335 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2336 if (typestr == NULL)
2337 error = TRUE;
2338 else
2339 {
2340 switch (TOUPPER_ASC(*typestr))
2341 {
2342 case 'E': type = VIM_ERROR; break;
2343 case 'Q': type = VIM_QUESTION; break;
2344 case 'I': type = VIM_INFO; break;
2345 case 'W': type = VIM_WARNING; break;
2346 case 'G': type = VIM_GENERIC; break;
2347 }
2348 }
2349 }
2350 }
2351 }
2352
2353 if (buttons == NULL || *buttons == NUL)
2354 buttons = (char_u *)_("&Ok");
2355
2356 if (!error)
2357 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2358 def, NULL, FALSE);
2359#endif
2360}
2361
2362/*
2363 * "copy()" function
2364 */
2365 static void
2366f_copy(typval_T *argvars, typval_T *rettv)
2367{
2368 item_copy(&argvars[0], rettv, FALSE, 0);
2369}
2370
2371#ifdef FEAT_FLOAT
2372/*
2373 * "cos()" function
2374 */
2375 static void
2376f_cos(typval_T *argvars, typval_T *rettv)
2377{
2378 float_T f = 0.0;
2379
2380 rettv->v_type = VAR_FLOAT;
2381 if (get_float_arg(argvars, &f) == OK)
2382 rettv->vval.v_float = cos(f);
2383 else
2384 rettv->vval.v_float = 0.0;
2385}
2386
2387/*
2388 * "cosh()" function
2389 */
2390 static void
2391f_cosh(typval_T *argvars, typval_T *rettv)
2392{
2393 float_T f = 0.0;
2394
2395 rettv->v_type = VAR_FLOAT;
2396 if (get_float_arg(argvars, &f) == OK)
2397 rettv->vval.v_float = cosh(f);
2398 else
2399 rettv->vval.v_float = 0.0;
2400}
2401#endif
2402
2403/*
2404 * "count()" function
2405 */
2406 static void
2407f_count(typval_T *argvars, typval_T *rettv)
2408{
2409 long n = 0;
2410 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002411 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002412
Bram Moolenaar9966b212017-07-28 16:46:57 +02002413 if (argvars[2].v_type != VAR_UNKNOWN)
2414 ic = (int)get_tv_number_chk(&argvars[2], &error);
2415
2416 if (argvars[0].v_type == VAR_STRING)
2417 {
2418 char_u *expr = get_tv_string_chk(&argvars[1]);
2419 char_u *p = argvars[0].vval.v_string;
2420 char_u *next;
2421
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002422 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002423 {
2424 if (ic)
2425 {
2426 size_t len = STRLEN(expr);
2427
2428 while (*p != NUL)
2429 {
2430 if (MB_STRNICMP(p, expr, len) == 0)
2431 {
2432 ++n;
2433 p += len;
2434 }
2435 else
2436 MB_PTR_ADV(p);
2437 }
2438 }
2439 else
2440 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2441 != NULL)
2442 {
2443 ++n;
2444 p = next + STRLEN(expr);
2445 }
2446 }
2447
2448 }
2449 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002450 {
2451 listitem_T *li;
2452 list_T *l;
2453 long idx;
2454
2455 if ((l = argvars[0].vval.v_list) != NULL)
2456 {
2457 li = l->lv_first;
2458 if (argvars[2].v_type != VAR_UNKNOWN)
2459 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002460 if (argvars[3].v_type != VAR_UNKNOWN)
2461 {
2462 idx = (long)get_tv_number_chk(&argvars[3], &error);
2463 if (!error)
2464 {
2465 li = list_find(l, idx);
2466 if (li == NULL)
2467 EMSGN(_(e_listidx), idx);
2468 }
2469 }
2470 if (error)
2471 li = NULL;
2472 }
2473
2474 for ( ; li != NULL; li = li->li_next)
2475 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2476 ++n;
2477 }
2478 }
2479 else if (argvars[0].v_type == VAR_DICT)
2480 {
2481 int todo;
2482 dict_T *d;
2483 hashitem_T *hi;
2484
2485 if ((d = argvars[0].vval.v_dict) != NULL)
2486 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002487 if (argvars[2].v_type != VAR_UNKNOWN)
2488 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002489 if (argvars[3].v_type != VAR_UNKNOWN)
2490 EMSG(_(e_invarg));
2491 }
2492
2493 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2494 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2495 {
2496 if (!HASHITEM_EMPTY(hi))
2497 {
2498 --todo;
2499 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2500 ++n;
2501 }
2502 }
2503 }
2504 }
2505 else
2506 EMSG2(_(e_listdictarg), "count()");
2507 rettv->vval.v_number = n;
2508}
2509
2510/*
2511 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2512 *
2513 * Checks the existence of a cscope connection.
2514 */
2515 static void
2516f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2517{
2518#ifdef FEAT_CSCOPE
2519 int num = 0;
2520 char_u *dbpath = NULL;
2521 char_u *prepend = NULL;
2522 char_u buf[NUMBUFLEN];
2523
2524 if (argvars[0].v_type != VAR_UNKNOWN
2525 && argvars[1].v_type != VAR_UNKNOWN)
2526 {
2527 num = (int)get_tv_number(&argvars[0]);
2528 dbpath = get_tv_string(&argvars[1]);
2529 if (argvars[2].v_type != VAR_UNKNOWN)
2530 prepend = get_tv_string_buf(&argvars[2], buf);
2531 }
2532
2533 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2534#endif
2535}
2536
2537/*
2538 * "cursor(lnum, col)" function, or
2539 * "cursor(list)"
2540 *
2541 * Moves the cursor to the specified line and column.
2542 * Returns 0 when the position could be set, -1 otherwise.
2543 */
2544 static void
2545f_cursor(typval_T *argvars, typval_T *rettv)
2546{
2547 long line, col;
2548#ifdef FEAT_VIRTUALEDIT
2549 long coladd = 0;
2550#endif
2551 int set_curswant = TRUE;
2552
2553 rettv->vval.v_number = -1;
2554 if (argvars[1].v_type == VAR_UNKNOWN)
2555 {
2556 pos_T pos;
2557 colnr_T curswant = -1;
2558
2559 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2560 {
2561 EMSG(_(e_invarg));
2562 return;
2563 }
2564 line = pos.lnum;
2565 col = pos.col;
2566#ifdef FEAT_VIRTUALEDIT
2567 coladd = pos.coladd;
2568#endif
2569 if (curswant >= 0)
2570 {
2571 curwin->w_curswant = curswant - 1;
2572 set_curswant = FALSE;
2573 }
2574 }
2575 else
2576 {
2577 line = get_tv_lnum(argvars);
2578 col = (long)get_tv_number_chk(&argvars[1], NULL);
2579#ifdef FEAT_VIRTUALEDIT
2580 if (argvars[2].v_type != VAR_UNKNOWN)
2581 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2582#endif
2583 }
2584 if (line < 0 || col < 0
2585#ifdef FEAT_VIRTUALEDIT
2586 || coladd < 0
2587#endif
2588 )
2589 return; /* type error; errmsg already given */
2590 if (line > 0)
2591 curwin->w_cursor.lnum = line;
2592 if (col > 0)
2593 curwin->w_cursor.col = col - 1;
2594#ifdef FEAT_VIRTUALEDIT
2595 curwin->w_cursor.coladd = coladd;
2596#endif
2597
2598 /* Make sure the cursor is in a valid position. */
2599 check_cursor();
2600#ifdef FEAT_MBYTE
2601 /* Correct cursor for multi-byte character. */
2602 if (has_mbyte)
2603 mb_adjust_cursor();
2604#endif
2605
2606 curwin->w_set_curswant = set_curswant;
2607 rettv->vval.v_number = 0;
2608}
2609
2610/*
2611 * "deepcopy()" function
2612 */
2613 static void
2614f_deepcopy(typval_T *argvars, typval_T *rettv)
2615{
2616 int noref = 0;
2617 int copyID;
2618
2619 if (argvars[1].v_type != VAR_UNKNOWN)
2620 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2621 if (noref < 0 || noref > 1)
2622 EMSG(_(e_invarg));
2623 else
2624 {
2625 copyID = get_copyID();
2626 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2627 }
2628}
2629
2630/*
2631 * "delete()" function
2632 */
2633 static void
2634f_delete(typval_T *argvars, typval_T *rettv)
2635{
2636 char_u nbuf[NUMBUFLEN];
2637 char_u *name;
2638 char_u *flags;
2639
2640 rettv->vval.v_number = -1;
2641 if (check_restricted() || check_secure())
2642 return;
2643
2644 name = get_tv_string(&argvars[0]);
2645 if (name == NULL || *name == NUL)
2646 {
2647 EMSG(_(e_invarg));
2648 return;
2649 }
2650
2651 if (argvars[1].v_type != VAR_UNKNOWN)
2652 flags = get_tv_string_buf(&argvars[1], nbuf);
2653 else
2654 flags = (char_u *)"";
2655
2656 if (*flags == NUL)
2657 /* delete a file */
2658 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2659 else if (STRCMP(flags, "d") == 0)
2660 /* delete an empty directory */
2661 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2662 else if (STRCMP(flags, "rf") == 0)
2663 /* delete a directory recursively */
2664 rettv->vval.v_number = delete_recursive(name);
2665 else
2666 EMSG2(_(e_invexpr2), flags);
2667}
2668
2669/*
2670 * "did_filetype()" function
2671 */
2672 static void
2673f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2674{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002675 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002676}
2677
2678/*
2679 * "diff_filler()" function
2680 */
2681 static void
2682f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2683{
2684#ifdef FEAT_DIFF
2685 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2686#endif
2687}
2688
2689/*
2690 * "diff_hlID()" function
2691 */
2692 static void
2693f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2694{
2695#ifdef FEAT_DIFF
2696 linenr_T lnum = get_tv_lnum(argvars);
2697 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002698 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002699 static int fnum = 0;
2700 static int change_start = 0;
2701 static int change_end = 0;
2702 static hlf_T hlID = (hlf_T)0;
2703 int filler_lines;
2704 int col;
2705
2706 if (lnum < 0) /* ignore type error in {lnum} arg */
2707 lnum = 0;
2708 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002709 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002710 || fnum != curbuf->b_fnum)
2711 {
2712 /* New line, buffer, change: need to get the values. */
2713 filler_lines = diff_check(curwin, lnum);
2714 if (filler_lines < 0)
2715 {
2716 if (filler_lines == -1)
2717 {
2718 change_start = MAXCOL;
2719 change_end = -1;
2720 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2721 hlID = HLF_ADD; /* added line */
2722 else
2723 hlID = HLF_CHD; /* changed line */
2724 }
2725 else
2726 hlID = HLF_ADD; /* added line */
2727 }
2728 else
2729 hlID = (hlf_T)0;
2730 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002731 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002732 fnum = curbuf->b_fnum;
2733 }
2734
2735 if (hlID == HLF_CHD || hlID == HLF_TXD)
2736 {
2737 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2738 if (col >= change_start && col <= change_end)
2739 hlID = HLF_TXD; /* changed text */
2740 else
2741 hlID = HLF_CHD; /* changed line */
2742 }
2743 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2744#endif
2745}
2746
2747/*
2748 * "empty({expr})" function
2749 */
2750 static void
2751f_empty(typval_T *argvars, typval_T *rettv)
2752{
2753 int n = FALSE;
2754
2755 switch (argvars[0].v_type)
2756 {
2757 case VAR_STRING:
2758 case VAR_FUNC:
2759 n = argvars[0].vval.v_string == NULL
2760 || *argvars[0].vval.v_string == NUL;
2761 break;
2762 case VAR_PARTIAL:
2763 n = FALSE;
2764 break;
2765 case VAR_NUMBER:
2766 n = argvars[0].vval.v_number == 0;
2767 break;
2768 case VAR_FLOAT:
2769#ifdef FEAT_FLOAT
2770 n = argvars[0].vval.v_float == 0.0;
2771 break;
2772#endif
2773 case VAR_LIST:
2774 n = argvars[0].vval.v_list == NULL
2775 || argvars[0].vval.v_list->lv_first == NULL;
2776 break;
2777 case VAR_DICT:
2778 n = argvars[0].vval.v_dict == NULL
2779 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2780 break;
2781 case VAR_SPECIAL:
2782 n = argvars[0].vval.v_number != VVAL_TRUE;
2783 break;
2784
2785 case VAR_JOB:
2786#ifdef FEAT_JOB_CHANNEL
2787 n = argvars[0].vval.v_job == NULL
2788 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2789 break;
2790#endif
2791 case VAR_CHANNEL:
2792#ifdef FEAT_JOB_CHANNEL
2793 n = argvars[0].vval.v_channel == NULL
2794 || !channel_is_open(argvars[0].vval.v_channel);
2795 break;
2796#endif
2797 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002798 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002799 n = TRUE;
2800 break;
2801 }
2802
2803 rettv->vval.v_number = n;
2804}
2805
2806/*
2807 * "escape({string}, {chars})" function
2808 */
2809 static void
2810f_escape(typval_T *argvars, typval_T *rettv)
2811{
2812 char_u buf[NUMBUFLEN];
2813
2814 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2815 get_tv_string_buf(&argvars[1], buf));
2816 rettv->v_type = VAR_STRING;
2817}
2818
2819/*
2820 * "eval()" function
2821 */
2822 static void
2823f_eval(typval_T *argvars, typval_T *rettv)
2824{
2825 char_u *s, *p;
2826
2827 s = get_tv_string_chk(&argvars[0]);
2828 if (s != NULL)
2829 s = skipwhite(s);
2830
2831 p = s;
2832 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2833 {
2834 if (p != NULL && !aborting())
2835 EMSG2(_(e_invexpr2), p);
2836 need_clr_eos = FALSE;
2837 rettv->v_type = VAR_NUMBER;
2838 rettv->vval.v_number = 0;
2839 }
2840 else if (*s != NUL)
2841 EMSG(_(e_trailing));
2842}
2843
2844/*
2845 * "eventhandler()" function
2846 */
2847 static void
2848f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2849{
2850 rettv->vval.v_number = vgetc_busy;
2851}
2852
2853/*
2854 * "executable()" function
2855 */
2856 static void
2857f_executable(typval_T *argvars, typval_T *rettv)
2858{
2859 char_u *name = get_tv_string(&argvars[0]);
2860
2861 /* Check in $PATH and also check directly if there is a directory name. */
2862 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2863 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2864}
2865
2866static garray_T redir_execute_ga;
2867
2868/*
2869 * Append "value[value_len]" to the execute() output.
2870 */
2871 void
2872execute_redir_str(char_u *value, int value_len)
2873{
2874 int len;
2875
2876 if (value_len == -1)
2877 len = (int)STRLEN(value); /* Append the entire string */
2878 else
2879 len = value_len; /* Append only "value_len" characters */
2880 if (ga_grow(&redir_execute_ga, len) == OK)
2881 {
2882 mch_memmove((char *)redir_execute_ga.ga_data
2883 + redir_execute_ga.ga_len, value, len);
2884 redir_execute_ga.ga_len += len;
2885 }
2886}
2887
2888/*
2889 * Get next line from a list.
2890 * Called by do_cmdline() to get the next line.
2891 * Returns allocated string, or NULL for end of function.
2892 */
2893
2894 static char_u *
2895get_list_line(
2896 int c UNUSED,
2897 void *cookie,
2898 int indent UNUSED)
2899{
2900 listitem_T **p = (listitem_T **)cookie;
2901 listitem_T *item = *p;
2902 char_u buf[NUMBUFLEN];
2903 char_u *s;
2904
2905 if (item == NULL)
2906 return NULL;
2907 s = get_tv_string_buf_chk(&item->li_tv, buf);
2908 *p = item->li_next;
2909 return s == NULL ? NULL : vim_strsave(s);
2910}
2911
2912/*
2913 * "execute()" function
2914 */
2915 static void
2916f_execute(typval_T *argvars, typval_T *rettv)
2917{
2918 char_u *cmd = NULL;
2919 list_T *list = NULL;
2920 int save_msg_silent = msg_silent;
2921 int save_emsg_silent = emsg_silent;
2922 int save_emsg_noredir = emsg_noredir;
2923 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002924 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002925 garray_T save_ga;
2926
2927 rettv->vval.v_string = NULL;
2928 rettv->v_type = VAR_STRING;
2929
2930 if (argvars[0].v_type == VAR_LIST)
2931 {
2932 list = argvars[0].vval.v_list;
2933 if (list == NULL || list->lv_first == NULL)
2934 /* empty list, no commands, empty output */
2935 return;
2936 ++list->lv_refcount;
2937 }
2938 else
2939 {
2940 cmd = get_tv_string_chk(&argvars[0]);
2941 if (cmd == NULL)
2942 return;
2943 }
2944
2945 if (argvars[1].v_type != VAR_UNKNOWN)
2946 {
2947 char_u buf[NUMBUFLEN];
2948 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2949
2950 if (s == NULL)
2951 return;
2952 if (STRNCMP(s, "silent", 6) == 0)
2953 ++msg_silent;
2954 if (STRCMP(s, "silent!") == 0)
2955 {
2956 emsg_silent = TRUE;
2957 emsg_noredir = TRUE;
2958 }
2959 }
2960 else
2961 ++msg_silent;
2962
2963 if (redir_execute)
2964 save_ga = redir_execute_ga;
2965 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2966 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002967 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002968
2969 if (cmd != NULL)
2970 do_cmdline_cmd(cmd);
2971 else
2972 {
2973 listitem_T *item = list->lv_first;
2974
2975 do_cmdline(NULL, get_list_line, (void *)&item,
2976 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2977 --list->lv_refcount;
2978 }
2979
Bram Moolenaard297f352017-01-29 20:31:21 +01002980 /* Need to append a NUL to the result. */
2981 if (ga_grow(&redir_execute_ga, 1) == OK)
2982 {
2983 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2984 rettv->vval.v_string = redir_execute_ga.ga_data;
2985 }
2986 else
2987 {
2988 ga_clear(&redir_execute_ga);
2989 rettv->vval.v_string = NULL;
2990 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002991 msg_silent = save_msg_silent;
2992 emsg_silent = save_emsg_silent;
2993 emsg_noredir = save_emsg_noredir;
2994
2995 redir_execute = save_redir_execute;
2996 if (redir_execute)
2997 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002998 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002999
3000 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
3001 * line. Put it back in the first column. */
3002 msg_col = 0;
3003}
3004
3005/*
3006 * "exepath()" function
3007 */
3008 static void
3009f_exepath(typval_T *argvars, typval_T *rettv)
3010{
3011 char_u *p = NULL;
3012
3013 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3014 rettv->v_type = VAR_STRING;
3015 rettv->vval.v_string = p;
3016}
3017
3018/*
3019 * "exists()" function
3020 */
3021 static void
3022f_exists(typval_T *argvars, typval_T *rettv)
3023{
3024 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003025 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003026
3027 p = get_tv_string(&argvars[0]);
3028 if (*p == '$') /* environment variable */
3029 {
3030 /* first try "normal" environment variables (fast) */
3031 if (mch_getenv(p + 1) != NULL)
3032 n = TRUE;
3033 else
3034 {
3035 /* try expanding things like $VIM and ${HOME} */
3036 p = expand_env_save(p);
3037 if (p != NULL && *p != '$')
3038 n = TRUE;
3039 vim_free(p);
3040 }
3041 }
3042 else if (*p == '&' || *p == '+') /* option */
3043 {
3044 n = (get_option_tv(&p, NULL, TRUE) == OK);
3045 if (*skipwhite(p) != NUL)
3046 n = FALSE; /* trailing garbage */
3047 }
3048 else if (*p == '*') /* internal or user defined function */
3049 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003050 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003051 }
3052 else if (*p == ':')
3053 {
3054 n = cmd_exists(p + 1);
3055 }
3056 else if (*p == '#')
3057 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003058 if (p[1] == '#')
3059 n = autocmd_supported(p + 2);
3060 else
3061 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003062 }
3063 else /* internal variable */
3064 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003065 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003066 }
3067
3068 rettv->vval.v_number = n;
3069}
3070
3071#ifdef FEAT_FLOAT
3072/*
3073 * "exp()" function
3074 */
3075 static void
3076f_exp(typval_T *argvars, typval_T *rettv)
3077{
3078 float_T f = 0.0;
3079
3080 rettv->v_type = VAR_FLOAT;
3081 if (get_float_arg(argvars, &f) == OK)
3082 rettv->vval.v_float = exp(f);
3083 else
3084 rettv->vval.v_float = 0.0;
3085}
3086#endif
3087
3088/*
3089 * "expand()" function
3090 */
3091 static void
3092f_expand(typval_T *argvars, typval_T *rettv)
3093{
3094 char_u *s;
3095 int len;
3096 char_u *errormsg;
3097 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3098 expand_T xpc;
3099 int error = FALSE;
3100 char_u *result;
3101
3102 rettv->v_type = VAR_STRING;
3103 if (argvars[1].v_type != VAR_UNKNOWN
3104 && argvars[2].v_type != VAR_UNKNOWN
3105 && get_tv_number_chk(&argvars[2], &error)
3106 && !error)
3107 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003108 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003109 }
3110
3111 s = get_tv_string(&argvars[0]);
3112 if (*s == '%' || *s == '#' || *s == '<')
3113 {
3114 ++emsg_off;
3115 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3116 --emsg_off;
3117 if (rettv->v_type == VAR_LIST)
3118 {
3119 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3120 list_append_string(rettv->vval.v_list, result, -1);
3121 else
3122 vim_free(result);
3123 }
3124 else
3125 rettv->vval.v_string = result;
3126 }
3127 else
3128 {
3129 /* When the optional second argument is non-zero, don't remove matches
3130 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3131 if (argvars[1].v_type != VAR_UNKNOWN
3132 && get_tv_number_chk(&argvars[1], &error))
3133 options |= WILD_KEEP_ALL;
3134 if (!error)
3135 {
3136 ExpandInit(&xpc);
3137 xpc.xp_context = EXPAND_FILES;
3138 if (p_wic)
3139 options += WILD_ICASE;
3140 if (rettv->v_type == VAR_STRING)
3141 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3142 options, WILD_ALL);
3143 else if (rettv_list_alloc(rettv) != FAIL)
3144 {
3145 int i;
3146
3147 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3148 for (i = 0; i < xpc.xp_numfiles; i++)
3149 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3150 ExpandCleanup(&xpc);
3151 }
3152 }
3153 else
3154 rettv->vval.v_string = NULL;
3155 }
3156}
3157
3158/*
3159 * "extend(list, list [, idx])" function
3160 * "extend(dict, dict [, action])" function
3161 */
3162 static void
3163f_extend(typval_T *argvars, typval_T *rettv)
3164{
3165 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3166
3167 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3168 {
3169 list_T *l1, *l2;
3170 listitem_T *item;
3171 long before;
3172 int error = FALSE;
3173
3174 l1 = argvars[0].vval.v_list;
3175 l2 = argvars[1].vval.v_list;
3176 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3177 && l2 != NULL)
3178 {
3179 if (argvars[2].v_type != VAR_UNKNOWN)
3180 {
3181 before = (long)get_tv_number_chk(&argvars[2], &error);
3182 if (error)
3183 return; /* type error; errmsg already given */
3184
3185 if (before == l1->lv_len)
3186 item = NULL;
3187 else
3188 {
3189 item = list_find(l1, before);
3190 if (item == NULL)
3191 {
3192 EMSGN(_(e_listidx), before);
3193 return;
3194 }
3195 }
3196 }
3197 else
3198 item = NULL;
3199 list_extend(l1, l2, item);
3200
3201 copy_tv(&argvars[0], rettv);
3202 }
3203 }
3204 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3205 {
3206 dict_T *d1, *d2;
3207 char_u *action;
3208 int i;
3209
3210 d1 = argvars[0].vval.v_dict;
3211 d2 = argvars[1].vval.v_dict;
3212 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3213 && d2 != NULL)
3214 {
3215 /* Check the third argument. */
3216 if (argvars[2].v_type != VAR_UNKNOWN)
3217 {
3218 static char *(av[]) = {"keep", "force", "error"};
3219
3220 action = get_tv_string_chk(&argvars[2]);
3221 if (action == NULL)
3222 return; /* type error; errmsg already given */
3223 for (i = 0; i < 3; ++i)
3224 if (STRCMP(action, av[i]) == 0)
3225 break;
3226 if (i == 3)
3227 {
3228 EMSG2(_(e_invarg2), action);
3229 return;
3230 }
3231 }
3232 else
3233 action = (char_u *)"force";
3234
3235 dict_extend(d1, d2, action);
3236
3237 copy_tv(&argvars[0], rettv);
3238 }
3239 }
3240 else
3241 EMSG2(_(e_listdictarg), "extend()");
3242}
3243
3244/*
3245 * "feedkeys()" function
3246 */
3247 static void
3248f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3249{
3250 int remap = TRUE;
3251 int insert = FALSE;
3252 char_u *keys, *flags;
3253 char_u nbuf[NUMBUFLEN];
3254 int typed = FALSE;
3255 int execute = FALSE;
3256 int dangerous = FALSE;
3257 char_u *keys_esc;
3258
3259 /* This is not allowed in the sandbox. If the commands would still be
3260 * executed in the sandbox it would be OK, but it probably happens later,
3261 * when "sandbox" is no longer set. */
3262 if (check_secure())
3263 return;
3264
3265 keys = get_tv_string(&argvars[0]);
3266
3267 if (argvars[1].v_type != VAR_UNKNOWN)
3268 {
3269 flags = get_tv_string_buf(&argvars[1], nbuf);
3270 for ( ; *flags != NUL; ++flags)
3271 {
3272 switch (*flags)
3273 {
3274 case 'n': remap = FALSE; break;
3275 case 'm': remap = TRUE; break;
3276 case 't': typed = TRUE; break;
3277 case 'i': insert = TRUE; break;
3278 case 'x': execute = TRUE; break;
3279 case '!': dangerous = TRUE; break;
3280 }
3281 }
3282 }
3283
3284 if (*keys != NUL || execute)
3285 {
3286 /* Need to escape K_SPECIAL and CSI before putting the string in the
3287 * typeahead buffer. */
3288 keys_esc = vim_strsave_escape_csi(keys);
3289 if (keys_esc != NULL)
3290 {
3291 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3292 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3293 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003294 if (vgetc_busy
3295#ifdef FEAT_TIMERS
3296 || timer_busy
3297#endif
3298 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299 typebuf_was_filled = TRUE;
3300 if (execute)
3301 {
3302 int save_msg_scroll = msg_scroll;
3303
3304 /* Avoid a 1 second delay when the keys start Insert mode. */
3305 msg_scroll = FALSE;
3306
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02003307#ifdef FEAT_TERMINAL
3308 if (term_use_loop())
3309 terminal_loop(FALSE);
3310 else
3311#endif
3312 {
3313 if (!dangerous)
3314 ++ex_normal_busy;
3315 exec_normal(TRUE);
3316 if (!dangerous)
3317 --ex_normal_busy;
3318 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003319 msg_scroll |= save_msg_scroll;
3320 }
3321 }
3322 }
3323}
3324
3325/*
3326 * "filereadable()" function
3327 */
3328 static void
3329f_filereadable(typval_T *argvars, typval_T *rettv)
3330{
3331 int fd;
3332 char_u *p;
3333 int n;
3334
3335#ifndef O_NONBLOCK
3336# define O_NONBLOCK 0
3337#endif
3338 p = get_tv_string(&argvars[0]);
3339 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3340 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3341 {
3342 n = TRUE;
3343 close(fd);
3344 }
3345 else
3346 n = FALSE;
3347
3348 rettv->vval.v_number = n;
3349}
3350
3351/*
3352 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3353 * rights to write into.
3354 */
3355 static void
3356f_filewritable(typval_T *argvars, typval_T *rettv)
3357{
3358 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3359}
3360
3361 static void
3362findfilendir(
3363 typval_T *argvars UNUSED,
3364 typval_T *rettv,
3365 int find_what UNUSED)
3366{
3367#ifdef FEAT_SEARCHPATH
3368 char_u *fname;
3369 char_u *fresult = NULL;
3370 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3371 char_u *p;
3372 char_u pathbuf[NUMBUFLEN];
3373 int count = 1;
3374 int first = TRUE;
3375 int error = FALSE;
3376#endif
3377
3378 rettv->vval.v_string = NULL;
3379 rettv->v_type = VAR_STRING;
3380
3381#ifdef FEAT_SEARCHPATH
3382 fname = get_tv_string(&argvars[0]);
3383
3384 if (argvars[1].v_type != VAR_UNKNOWN)
3385 {
3386 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3387 if (p == NULL)
3388 error = TRUE;
3389 else
3390 {
3391 if (*p != NUL)
3392 path = p;
3393
3394 if (argvars[2].v_type != VAR_UNKNOWN)
3395 count = (int)get_tv_number_chk(&argvars[2], &error);
3396 }
3397 }
3398
3399 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3400 error = TRUE;
3401
3402 if (*fname != NUL && !error)
3403 {
3404 do
3405 {
3406 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3407 vim_free(fresult);
3408 fresult = find_file_in_path_option(first ? fname : NULL,
3409 first ? (int)STRLEN(fname) : 0,
3410 0, first, path,
3411 find_what,
3412 curbuf->b_ffname,
3413 find_what == FINDFILE_DIR
3414 ? (char_u *)"" : curbuf->b_p_sua);
3415 first = FALSE;
3416
3417 if (fresult != NULL && rettv->v_type == VAR_LIST)
3418 list_append_string(rettv->vval.v_list, fresult, -1);
3419
3420 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3421 }
3422
3423 if (rettv->v_type == VAR_STRING)
3424 rettv->vval.v_string = fresult;
3425#endif
3426}
3427
3428/*
3429 * "filter()" function
3430 */
3431 static void
3432f_filter(typval_T *argvars, typval_T *rettv)
3433{
3434 filter_map(argvars, rettv, FALSE);
3435}
3436
3437/*
3438 * "finddir({fname}[, {path}[, {count}]])" function
3439 */
3440 static void
3441f_finddir(typval_T *argvars, typval_T *rettv)
3442{
3443 findfilendir(argvars, rettv, FINDFILE_DIR);
3444}
3445
3446/*
3447 * "findfile({fname}[, {path}[, {count}]])" function
3448 */
3449 static void
3450f_findfile(typval_T *argvars, typval_T *rettv)
3451{
3452 findfilendir(argvars, rettv, FINDFILE_FILE);
3453}
3454
3455#ifdef FEAT_FLOAT
3456/*
3457 * "float2nr({float})" function
3458 */
3459 static void
3460f_float2nr(typval_T *argvars, typval_T *rettv)
3461{
3462 float_T f = 0.0;
3463
3464 if (get_float_arg(argvars, &f) == OK)
3465 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003466 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003467 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003468 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003469 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003470 else
3471 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003472 }
3473}
3474
3475/*
3476 * "floor({float})" function
3477 */
3478 static void
3479f_floor(typval_T *argvars, typval_T *rettv)
3480{
3481 float_T f = 0.0;
3482
3483 rettv->v_type = VAR_FLOAT;
3484 if (get_float_arg(argvars, &f) == OK)
3485 rettv->vval.v_float = floor(f);
3486 else
3487 rettv->vval.v_float = 0.0;
3488}
3489
3490/*
3491 * "fmod()" function
3492 */
3493 static void
3494f_fmod(typval_T *argvars, typval_T *rettv)
3495{
3496 float_T fx = 0.0, fy = 0.0;
3497
3498 rettv->v_type = VAR_FLOAT;
3499 if (get_float_arg(argvars, &fx) == OK
3500 && get_float_arg(&argvars[1], &fy) == OK)
3501 rettv->vval.v_float = fmod(fx, fy);
3502 else
3503 rettv->vval.v_float = 0.0;
3504}
3505#endif
3506
3507/*
3508 * "fnameescape({string})" function
3509 */
3510 static void
3511f_fnameescape(typval_T *argvars, typval_T *rettv)
3512{
3513 rettv->vval.v_string = vim_strsave_fnameescape(
3514 get_tv_string(&argvars[0]), FALSE);
3515 rettv->v_type = VAR_STRING;
3516}
3517
3518/*
3519 * "fnamemodify({fname}, {mods})" function
3520 */
3521 static void
3522f_fnamemodify(typval_T *argvars, typval_T *rettv)
3523{
3524 char_u *fname;
3525 char_u *mods;
3526 int usedlen = 0;
3527 int len;
3528 char_u *fbuf = NULL;
3529 char_u buf[NUMBUFLEN];
3530
3531 fname = get_tv_string_chk(&argvars[0]);
3532 mods = get_tv_string_buf_chk(&argvars[1], buf);
3533 if (fname == NULL || mods == NULL)
3534 fname = NULL;
3535 else
3536 {
3537 len = (int)STRLEN(fname);
3538 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3539 }
3540
3541 rettv->v_type = VAR_STRING;
3542 if (fname == NULL)
3543 rettv->vval.v_string = NULL;
3544 else
3545 rettv->vval.v_string = vim_strnsave(fname, len);
3546 vim_free(fbuf);
3547}
3548
3549static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3550
3551/*
3552 * "foldclosed()" function
3553 */
3554 static void
3555foldclosed_both(
3556 typval_T *argvars UNUSED,
3557 typval_T *rettv,
3558 int end UNUSED)
3559{
3560#ifdef FEAT_FOLDING
3561 linenr_T lnum;
3562 linenr_T first, last;
3563
3564 lnum = get_tv_lnum(argvars);
3565 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3566 {
3567 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3568 {
3569 if (end)
3570 rettv->vval.v_number = (varnumber_T)last;
3571 else
3572 rettv->vval.v_number = (varnumber_T)first;
3573 return;
3574 }
3575 }
3576#endif
3577 rettv->vval.v_number = -1;
3578}
3579
3580/*
3581 * "foldclosed()" function
3582 */
3583 static void
3584f_foldclosed(typval_T *argvars, typval_T *rettv)
3585{
3586 foldclosed_both(argvars, rettv, FALSE);
3587}
3588
3589/*
3590 * "foldclosedend()" function
3591 */
3592 static void
3593f_foldclosedend(typval_T *argvars, typval_T *rettv)
3594{
3595 foldclosed_both(argvars, rettv, TRUE);
3596}
3597
3598/*
3599 * "foldlevel()" function
3600 */
3601 static void
3602f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3603{
3604#ifdef FEAT_FOLDING
3605 linenr_T lnum;
3606
3607 lnum = get_tv_lnum(argvars);
3608 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3609 rettv->vval.v_number = foldLevel(lnum);
3610#endif
3611}
3612
3613/*
3614 * "foldtext()" function
3615 */
3616 static void
3617f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3618{
3619#ifdef FEAT_FOLDING
3620 linenr_T foldstart;
3621 linenr_T foldend;
3622 char_u *dashes;
3623 linenr_T lnum;
3624 char_u *s;
3625 char_u *r;
3626 int len;
3627 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003628 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003629#endif
3630
3631 rettv->v_type = VAR_STRING;
3632 rettv->vval.v_string = NULL;
3633#ifdef FEAT_FOLDING
3634 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3635 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3636 dashes = get_vim_var_str(VV_FOLDDASHES);
3637 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3638 && dashes != NULL)
3639 {
3640 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003641 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003642 if (!linewhite(lnum))
3643 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003644
3645 /* Find interesting text in this line. */
3646 s = skipwhite(ml_get(lnum));
3647 /* skip C comment-start */
3648 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3649 {
3650 s = skipwhite(s + 2);
3651 if (*skipwhite(s) == NUL
3652 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3653 {
3654 s = skipwhite(ml_get(lnum + 1));
3655 if (*s == '*')
3656 s = skipwhite(s + 1);
3657 }
3658 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003659 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003660 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003661 r = alloc((unsigned)(STRLEN(txt)
3662 + STRLEN(dashes) /* for %s */
3663 + 20 /* for %3ld */
3664 + STRLEN(s))); /* concatenated */
3665 if (r != NULL)
3666 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003667 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003668 len = (int)STRLEN(r);
3669 STRCAT(r, s);
3670 /* remove 'foldmarker' and 'commentstring' */
3671 foldtext_cleanup(r + len);
3672 rettv->vval.v_string = r;
3673 }
3674 }
3675#endif
3676}
3677
3678/*
3679 * "foldtextresult(lnum)" function
3680 */
3681 static void
3682f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3683{
3684#ifdef FEAT_FOLDING
3685 linenr_T lnum;
3686 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003687 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003688 foldinfo_T foldinfo;
3689 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003690 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003691#endif
3692
3693 rettv->v_type = VAR_STRING;
3694 rettv->vval.v_string = NULL;
3695#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003696 if (entered)
3697 return; /* reject recursive use */
3698 entered = TRUE;
3699
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003700 lnum = get_tv_lnum(argvars);
3701 /* treat illegal types and illegal string values for {lnum} the same */
3702 if (lnum < 0)
3703 lnum = 0;
3704 fold_count = foldedCount(curwin, lnum, &foldinfo);
3705 if (fold_count > 0)
3706 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003707 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3708 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003709 if (text == buf)
3710 text = vim_strsave(text);
3711 rettv->vval.v_string = text;
3712 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003713
3714 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003715#endif
3716}
3717
3718/*
3719 * "foreground()" function
3720 */
3721 static void
3722f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3723{
3724#ifdef FEAT_GUI
3725 if (gui.in_use)
3726 gui_mch_set_foreground();
3727#else
3728# ifdef WIN32
3729 win32_set_foreground();
3730# endif
3731#endif
3732}
3733
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003734 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003735common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003736{
3737 char_u *s;
3738 char_u *name;
3739 int use_string = FALSE;
3740 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003741 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003742
3743 if (argvars[0].v_type == VAR_FUNC)
3744 {
3745 /* function(MyFunc, [arg], dict) */
3746 s = argvars[0].vval.v_string;
3747 }
3748 else if (argvars[0].v_type == VAR_PARTIAL
3749 && argvars[0].vval.v_partial != NULL)
3750 {
3751 /* function(dict.MyFunc, [arg]) */
3752 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003753 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003754 }
3755 else
3756 {
3757 /* function('MyFunc', [arg], dict) */
3758 s = get_tv_string(&argvars[0]);
3759 use_string = TRUE;
3760 }
3761
Bram Moolenaar843b8842016-08-21 14:36:15 +02003762 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003763 {
3764 name = s;
3765 trans_name = trans_function_name(&name, FALSE,
3766 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3767 if (*name != NUL)
3768 s = NULL;
3769 }
3770
Bram Moolenaar843b8842016-08-21 14:36:15 +02003771 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3772 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003773 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003774 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003775 else if (trans_name != NULL && (is_funcref
3776 ? find_func(trans_name) == NULL
3777 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003778 EMSG2(_("E700: Unknown function: %s"), s);
3779 else
3780 {
3781 int dict_idx = 0;
3782 int arg_idx = 0;
3783 list_T *list = NULL;
3784
3785 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3786 {
3787 char sid_buf[25];
3788 int off = *s == 's' ? 2 : 5;
3789
3790 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3791 * also be called from another script. Using trans_function_name()
3792 * would also work, but some plugins depend on the name being
3793 * printable text. */
3794 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3795 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3796 if (name != NULL)
3797 {
3798 STRCPY(name, sid_buf);
3799 STRCAT(name, s + off);
3800 }
3801 }
3802 else
3803 name = vim_strsave(s);
3804
3805 if (argvars[1].v_type != VAR_UNKNOWN)
3806 {
3807 if (argvars[2].v_type != VAR_UNKNOWN)
3808 {
3809 /* function(name, [args], dict) */
3810 arg_idx = 1;
3811 dict_idx = 2;
3812 }
3813 else if (argvars[1].v_type == VAR_DICT)
3814 /* function(name, dict) */
3815 dict_idx = 1;
3816 else
3817 /* function(name, [args]) */
3818 arg_idx = 1;
3819 if (dict_idx > 0)
3820 {
3821 if (argvars[dict_idx].v_type != VAR_DICT)
3822 {
3823 EMSG(_("E922: expected a dict"));
3824 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003825 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003826 }
3827 if (argvars[dict_idx].vval.v_dict == NULL)
3828 dict_idx = 0;
3829 }
3830 if (arg_idx > 0)
3831 {
3832 if (argvars[arg_idx].v_type != VAR_LIST)
3833 {
3834 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3835 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003836 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003837 }
3838 list = argvars[arg_idx].vval.v_list;
3839 if (list == NULL || list->lv_len == 0)
3840 arg_idx = 0;
3841 }
3842 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003843 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003844 {
3845 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3846
3847 /* result is a VAR_PARTIAL */
3848 if (pt == NULL)
3849 vim_free(name);
3850 else
3851 {
3852 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3853 {
3854 listitem_T *li;
3855 int i = 0;
3856 int arg_len = 0;
3857 int lv_len = 0;
3858
3859 if (arg_pt != NULL)
3860 arg_len = arg_pt->pt_argc;
3861 if (list != NULL)
3862 lv_len = list->lv_len;
3863 pt->pt_argc = arg_len + lv_len;
3864 pt->pt_argv = (typval_T *)alloc(
3865 sizeof(typval_T) * pt->pt_argc);
3866 if (pt->pt_argv == NULL)
3867 {
3868 vim_free(pt);
3869 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003870 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003871 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003872 for (i = 0; i < arg_len; i++)
3873 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3874 if (lv_len > 0)
3875 for (li = list->lv_first; li != NULL;
3876 li = li->li_next)
3877 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003878 }
3879
3880 /* For "function(dict.func, [], dict)" and "func" is a partial
3881 * use "dict". That is backwards compatible. */
3882 if (dict_idx > 0)
3883 {
3884 /* The dict is bound explicitly, pt_auto is FALSE. */
3885 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3886 ++pt->pt_dict->dv_refcount;
3887 }
3888 else if (arg_pt != NULL)
3889 {
3890 /* If the dict was bound automatically the result is also
3891 * bound automatically. */
3892 pt->pt_dict = arg_pt->pt_dict;
3893 pt->pt_auto = arg_pt->pt_auto;
3894 if (pt->pt_dict != NULL)
3895 ++pt->pt_dict->dv_refcount;
3896 }
3897
3898 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003899 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3900 {
3901 pt->pt_func = arg_pt->pt_func;
3902 func_ptr_ref(pt->pt_func);
3903 vim_free(name);
3904 }
3905 else if (is_funcref)
3906 {
3907 pt->pt_func = find_func(trans_name);
3908 func_ptr_ref(pt->pt_func);
3909 vim_free(name);
3910 }
3911 else
3912 {
3913 pt->pt_name = name;
3914 func_ref(name);
3915 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003916 }
3917 rettv->v_type = VAR_PARTIAL;
3918 rettv->vval.v_partial = pt;
3919 }
3920 else
3921 {
3922 /* result is a VAR_FUNC */
3923 rettv->v_type = VAR_FUNC;
3924 rettv->vval.v_string = name;
3925 func_ref(name);
3926 }
3927 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003928theend:
3929 vim_free(trans_name);
3930}
3931
3932/*
3933 * "funcref()" function
3934 */
3935 static void
3936f_funcref(typval_T *argvars, typval_T *rettv)
3937{
3938 common_function(argvars, rettv, TRUE);
3939}
3940
3941/*
3942 * "function()" function
3943 */
3944 static void
3945f_function(typval_T *argvars, typval_T *rettv)
3946{
3947 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003948}
3949
3950/*
3951 * "garbagecollect()" function
3952 */
3953 static void
3954f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3955{
3956 /* This is postponed until we are back at the toplevel, because we may be
3957 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3958 want_garbage_collect = TRUE;
3959
3960 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3961 garbage_collect_at_exit = TRUE;
3962}
3963
3964/*
3965 * "get()" function
3966 */
3967 static void
3968f_get(typval_T *argvars, typval_T *rettv)
3969{
3970 listitem_T *li;
3971 list_T *l;
3972 dictitem_T *di;
3973 dict_T *d;
3974 typval_T *tv = NULL;
3975
3976 if (argvars[0].v_type == VAR_LIST)
3977 {
3978 if ((l = argvars[0].vval.v_list) != NULL)
3979 {
3980 int error = FALSE;
3981
3982 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3983 if (!error && li != NULL)
3984 tv = &li->li_tv;
3985 }
3986 }
3987 else if (argvars[0].v_type == VAR_DICT)
3988 {
3989 if ((d = argvars[0].vval.v_dict) != NULL)
3990 {
3991 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3992 if (di != NULL)
3993 tv = &di->di_tv;
3994 }
3995 }
3996 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3997 {
3998 partial_T *pt;
3999 partial_T fref_pt;
4000
4001 if (argvars[0].v_type == VAR_PARTIAL)
4002 pt = argvars[0].vval.v_partial;
4003 else
4004 {
4005 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4006 fref_pt.pt_name = argvars[0].vval.v_string;
4007 pt = &fref_pt;
4008 }
4009
4010 if (pt != NULL)
4011 {
4012 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004013 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004014
4015 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4016 {
4017 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004018 n = partial_name(pt);
4019 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004020 rettv->vval.v_string = NULL;
4021 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004022 {
4023 rettv->vval.v_string = vim_strsave(n);
4024 if (rettv->v_type == VAR_FUNC)
4025 func_ref(rettv->vval.v_string);
4026 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004027 }
4028 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004029 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004030 else if (STRCMP(what, "args") == 0)
4031 {
4032 rettv->v_type = VAR_LIST;
4033 if (rettv_list_alloc(rettv) == OK)
4034 {
4035 int i;
4036
4037 for (i = 0; i < pt->pt_argc; ++i)
4038 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4039 }
4040 }
4041 else
4042 EMSG2(_(e_invarg2), what);
4043 return;
4044 }
4045 }
4046 else
4047 EMSG2(_(e_listdictarg), "get()");
4048
4049 if (tv == NULL)
4050 {
4051 if (argvars[2].v_type != VAR_UNKNOWN)
4052 copy_tv(&argvars[2], rettv);
4053 }
4054 else
4055 copy_tv(tv, rettv);
4056}
4057
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004058#ifdef FEAT_SIGNS
4059/*
4060 * Returns information about signs placed in a buffer as list of dicts.
4061 */
4062 static void
4063get_buffer_signs(buf_T *buf, list_T *l)
4064{
4065 signlist_T *sign;
4066
4067 for (sign = buf->b_signlist; sign; sign = sign->next)
4068 {
4069 dict_T *d = dict_alloc();
4070
4071 if (d != NULL)
4072 {
4073 dict_add_nr_str(d, "id", sign->id, NULL);
4074 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004075 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004076
4077 list_append_dict(l, d);
4078 }
4079 }
4080}
4081#endif
4082
4083/*
4084 * Returns buffer options, variables and other attributes in a dictionary.
4085 */
4086 static dict_T *
4087get_buffer_info(buf_T *buf)
4088{
4089 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004090 tabpage_T *tp;
4091 win_T *wp;
4092 list_T *windows;
4093
4094 dict = dict_alloc();
4095 if (dict == NULL)
4096 return NULL;
4097
Bram Moolenaar33928832016-08-18 21:22:04 +02004098 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004099 dict_add_nr_str(dict, "name", 0L,
4100 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004101 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4102 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004103 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4104 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4105 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004106 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004107 dict_add_nr_str(dict, "hidden",
4108 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4109 NULL);
4110
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004111 /* Get a reference to buffer variables */
4112 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004113
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004114 /* List of windows displaying this buffer */
4115 windows = list_alloc();
4116 if (windows != NULL)
4117 {
4118 FOR_ALL_TAB_WINDOWS(tp, wp)
4119 if (wp->w_buffer == buf)
4120 list_append_number(windows, (varnumber_T)wp->w_id);
4121 dict_add_list(dict, "windows", windows);
4122 }
4123
4124#ifdef FEAT_SIGNS
4125 if (buf->b_signlist != NULL)
4126 {
4127 /* List of signs placed in this buffer */
4128 list_T *signs = list_alloc();
4129 if (signs != NULL)
4130 {
4131 get_buffer_signs(buf, signs);
4132 dict_add_list(dict, "signs", signs);
4133 }
4134 }
4135#endif
4136
4137 return dict;
4138}
4139
4140/*
4141 * "getbufinfo()" function
4142 */
4143 static void
4144f_getbufinfo(typval_T *argvars, typval_T *rettv)
4145{
4146 buf_T *buf = NULL;
4147 buf_T *argbuf = NULL;
4148 dict_T *d;
4149 int filtered = FALSE;
4150 int sel_buflisted = FALSE;
4151 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004152 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004153
4154 if (rettv_list_alloc(rettv) != OK)
4155 return;
4156
4157 /* List of all the buffers or selected buffers */
4158 if (argvars[0].v_type == VAR_DICT)
4159 {
4160 dict_T *sel_d = argvars[0].vval.v_dict;
4161
4162 if (sel_d != NULL)
4163 {
4164 dictitem_T *di;
4165
4166 filtered = TRUE;
4167
4168 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4169 if (di != NULL && get_tv_number(&di->di_tv))
4170 sel_buflisted = TRUE;
4171
4172 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4173 if (di != NULL && get_tv_number(&di->di_tv))
4174 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004175
4176 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4177 if (di != NULL && get_tv_number(&di->di_tv))
4178 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004179 }
4180 }
4181 else if (argvars[0].v_type != VAR_UNKNOWN)
4182 {
4183 /* Information about one buffer. Argument specifies the buffer */
4184 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4185 ++emsg_off;
4186 argbuf = get_buf_tv(&argvars[0], FALSE);
4187 --emsg_off;
4188 if (argbuf == NULL)
4189 return;
4190 }
4191
4192 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004193 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004194 {
4195 if (argbuf != NULL && argbuf != buf)
4196 continue;
4197 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004198 || (sel_buflisted && !buf->b_p_bl)
4199 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004200 continue;
4201
4202 d = get_buffer_info(buf);
4203 if (d != NULL)
4204 list_append_dict(rettv->vval.v_list, d);
4205 if (argbuf != NULL)
4206 return;
4207 }
4208}
4209
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004210static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4211
4212/*
4213 * Get line or list of lines from buffer "buf" into "rettv".
4214 * Return a range (from start to end) of lines in rettv from the specified
4215 * buffer.
4216 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4217 */
4218 static void
4219get_buffer_lines(
4220 buf_T *buf,
4221 linenr_T start,
4222 linenr_T end,
4223 int retlist,
4224 typval_T *rettv)
4225{
4226 char_u *p;
4227
4228 rettv->v_type = VAR_STRING;
4229 rettv->vval.v_string = NULL;
4230 if (retlist && rettv_list_alloc(rettv) == FAIL)
4231 return;
4232
4233 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4234 return;
4235
4236 if (!retlist)
4237 {
4238 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4239 p = ml_get_buf(buf, start, FALSE);
4240 else
4241 p = (char_u *)"";
4242 rettv->vval.v_string = vim_strsave(p);
4243 }
4244 else
4245 {
4246 if (end < start)
4247 return;
4248
4249 if (start < 1)
4250 start = 1;
4251 if (end > buf->b_ml.ml_line_count)
4252 end = buf->b_ml.ml_line_count;
4253 while (start <= end)
4254 if (list_append_string(rettv->vval.v_list,
4255 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4256 break;
4257 }
4258}
4259
4260/*
4261 * Get the lnum from the first argument.
4262 * Also accepts "$", then "buf" is used.
4263 * Returns 0 on error.
4264 */
4265 static linenr_T
4266get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4267{
4268 if (argvars[0].v_type == VAR_STRING
4269 && argvars[0].vval.v_string != NULL
4270 && argvars[0].vval.v_string[0] == '$'
4271 && buf != NULL)
4272 return buf->b_ml.ml_line_count;
4273 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4274}
4275
4276/*
4277 * "getbufline()" function
4278 */
4279 static void
4280f_getbufline(typval_T *argvars, typval_T *rettv)
4281{
4282 linenr_T lnum;
4283 linenr_T end;
4284 buf_T *buf;
4285
4286 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4287 ++emsg_off;
4288 buf = get_buf_tv(&argvars[0], FALSE);
4289 --emsg_off;
4290
4291 lnum = get_tv_lnum_buf(&argvars[1], buf);
4292 if (argvars[2].v_type == VAR_UNKNOWN)
4293 end = lnum;
4294 else
4295 end = get_tv_lnum_buf(&argvars[2], buf);
4296
4297 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4298}
4299
4300/*
4301 * "getbufvar()" function
4302 */
4303 static void
4304f_getbufvar(typval_T *argvars, typval_T *rettv)
4305{
4306 buf_T *buf;
4307 buf_T *save_curbuf;
4308 char_u *varname;
4309 dictitem_T *v;
4310 int done = FALSE;
4311
4312 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4313 varname = get_tv_string_chk(&argvars[1]);
4314 ++emsg_off;
4315 buf = get_buf_tv(&argvars[0], FALSE);
4316
4317 rettv->v_type = VAR_STRING;
4318 rettv->vval.v_string = NULL;
4319
4320 if (buf != NULL && varname != NULL)
4321 {
4322 /* set curbuf to be our buf, temporarily */
4323 save_curbuf = curbuf;
4324 curbuf = buf;
4325
Bram Moolenaar30567352016-08-27 21:25:44 +02004326 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004327 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004328 if (varname[1] == NUL)
4329 {
4330 /* get all buffer-local options in a dict */
4331 dict_T *opts = get_winbuf_options(TRUE);
4332
4333 if (opts != NULL)
4334 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004335 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004336 done = TRUE;
4337 }
4338 }
4339 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4340 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004341 done = TRUE;
4342 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004343 else
4344 {
4345 /* Look up the variable. */
4346 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4347 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4348 'b', varname, FALSE);
4349 if (v != NULL)
4350 {
4351 copy_tv(&v->di_tv, rettv);
4352 done = TRUE;
4353 }
4354 }
4355
4356 /* restore previous notion of curbuf */
4357 curbuf = save_curbuf;
4358 }
4359
4360 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4361 /* use the default value */
4362 copy_tv(&argvars[2], rettv);
4363
4364 --emsg_off;
4365}
4366
4367/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004368 * "getchangelist()" function
4369 */
4370 static void
4371f_getchangelist(typval_T *argvars, typval_T *rettv)
4372{
4373#ifdef FEAT_JUMPLIST
4374 buf_T *buf;
4375 int i;
4376 list_T *l;
4377 dict_T *d;
4378#endif
4379
4380 if (rettv_list_alloc(rettv) != OK)
4381 return;
4382
4383#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004384 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4385 ++emsg_off;
4386 buf = get_buf_tv(&argvars[0], FALSE);
4387 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004388 if (buf == NULL)
4389 return;
4390
4391 l = list_alloc();
4392 if (l == NULL)
4393 return;
4394
4395 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4396 return;
4397 /*
4398 * The current window change list index tracks only the position in the
4399 * current buffer change list. For other buffers, use the change list
4400 * length as the current index.
4401 */
4402 list_append_number(rettv->vval.v_list,
4403 (varnumber_T)((buf == curwin->w_buffer)
4404 ? curwin->w_changelistidx : buf->b_changelistlen));
4405
4406 for (i = 0; i < buf->b_changelistlen; ++i)
4407 {
4408 if (buf->b_changelist[i].lnum == 0)
4409 continue;
4410 if ((d = dict_alloc()) == NULL)
4411 return;
4412 if (list_append_dict(l, d) == FAIL)
4413 return;
4414 dict_add_nr_str(d, "lnum", (long)buf->b_changelist[i].lnum, NULL);
4415 dict_add_nr_str(d, "col", (long)buf->b_changelist[i].col, NULL);
4416# ifdef FEAT_VIRTUALEDIT
4417 dict_add_nr_str(d, "coladd", (long)buf->b_changelist[i].coladd, NULL);
4418# endif
4419 }
4420#endif
4421}
4422/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004423 * "getchar()" function
4424 */
4425 static void
4426f_getchar(typval_T *argvars, typval_T *rettv)
4427{
4428 varnumber_T n;
4429 int error = FALSE;
4430
4431 /* Position the cursor. Needed after a message that ends in a space. */
4432 windgoto(msg_row, msg_col);
4433
4434 ++no_mapping;
4435 ++allow_keys;
4436 for (;;)
4437 {
4438 if (argvars[0].v_type == VAR_UNKNOWN)
4439 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004440 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004441 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4442 /* getchar(1): only check if char avail */
4443 n = vpeekc_any();
4444 else if (error || vpeekc_any() == NUL)
4445 /* illegal argument or getchar(0) and no char avail: return zero */
4446 n = 0;
4447 else
4448 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004449 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004450
4451 if (n == K_IGNORE)
4452 continue;
4453 break;
4454 }
4455 --no_mapping;
4456 --allow_keys;
4457
4458 set_vim_var_nr(VV_MOUSE_WIN, 0);
4459 set_vim_var_nr(VV_MOUSE_WINID, 0);
4460 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4461 set_vim_var_nr(VV_MOUSE_COL, 0);
4462
4463 rettv->vval.v_number = n;
4464 if (IS_SPECIAL(n) || mod_mask != 0)
4465 {
4466 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4467 int i = 0;
4468
4469 /* Turn a special key into three bytes, plus modifier. */
4470 if (mod_mask != 0)
4471 {
4472 temp[i++] = K_SPECIAL;
4473 temp[i++] = KS_MODIFIER;
4474 temp[i++] = mod_mask;
4475 }
4476 if (IS_SPECIAL(n))
4477 {
4478 temp[i++] = K_SPECIAL;
4479 temp[i++] = K_SECOND(n);
4480 temp[i++] = K_THIRD(n);
4481 }
4482#ifdef FEAT_MBYTE
4483 else if (has_mbyte)
4484 i += (*mb_char2bytes)(n, temp + i);
4485#endif
4486 else
4487 temp[i++] = n;
4488 temp[i++] = NUL;
4489 rettv->v_type = VAR_STRING;
4490 rettv->vval.v_string = vim_strsave(temp);
4491
4492#ifdef FEAT_MOUSE
4493 if (is_mouse_key(n))
4494 {
4495 int row = mouse_row;
4496 int col = mouse_col;
4497 win_T *win;
4498 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004499 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004500 int winnr = 1;
4501
4502 if (row >= 0 && col >= 0)
4503 {
4504 /* Find the window at the mouse coordinates and compute the
4505 * text position. */
4506 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004507 if (win == NULL)
4508 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004509 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004510 for (wp = firstwin; wp != win; wp = wp->w_next)
4511 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004512 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4513 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4514 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4515 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4516 }
4517 }
4518#endif
4519 }
4520}
4521
4522/*
4523 * "getcharmod()" function
4524 */
4525 static void
4526f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4527{
4528 rettv->vval.v_number = mod_mask;
4529}
4530
4531/*
4532 * "getcharsearch()" function
4533 */
4534 static void
4535f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4536{
4537 if (rettv_dict_alloc(rettv) != FAIL)
4538 {
4539 dict_T *dict = rettv->vval.v_dict;
4540
4541 dict_add_nr_str(dict, "char", 0L, last_csearch());
4542 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4543 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4544 }
4545}
4546
4547/*
4548 * "getcmdline()" function
4549 */
4550 static void
4551f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4552{
4553 rettv->v_type = VAR_STRING;
4554 rettv->vval.v_string = get_cmdline_str();
4555}
4556
4557/*
4558 * "getcmdpos()" function
4559 */
4560 static void
4561f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4562{
4563 rettv->vval.v_number = get_cmdline_pos() + 1;
4564}
4565
4566/*
4567 * "getcmdtype()" function
4568 */
4569 static void
4570f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4571{
4572 rettv->v_type = VAR_STRING;
4573 rettv->vval.v_string = alloc(2);
4574 if (rettv->vval.v_string != NULL)
4575 {
4576 rettv->vval.v_string[0] = get_cmdline_type();
4577 rettv->vval.v_string[1] = NUL;
4578 }
4579}
4580
4581/*
4582 * "getcmdwintype()" function
4583 */
4584 static void
4585f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4586{
4587 rettv->v_type = VAR_STRING;
4588 rettv->vval.v_string = NULL;
4589#ifdef FEAT_CMDWIN
4590 rettv->vval.v_string = alloc(2);
4591 if (rettv->vval.v_string != NULL)
4592 {
4593 rettv->vval.v_string[0] = cmdwin_type;
4594 rettv->vval.v_string[1] = NUL;
4595 }
4596#endif
4597}
4598
4599#if defined(FEAT_CMDL_COMPL)
4600/*
4601 * "getcompletion()" function
4602 */
4603 static void
4604f_getcompletion(typval_T *argvars, typval_T *rettv)
4605{
4606 char_u *pat;
4607 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004608 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004609 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4610 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004611
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004612 if (argvars[2].v_type != VAR_UNKNOWN)
4613 filtered = get_tv_number_chk(&argvars[2], NULL);
4614
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004615 if (p_wic)
4616 options |= WILD_ICASE;
4617
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004618 /* For filtered results, 'wildignore' is used */
4619 if (!filtered)
4620 options |= WILD_KEEP_ALL;
4621
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004622 ExpandInit(&xpc);
4623 xpc.xp_pattern = get_tv_string(&argvars[0]);
4624 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4625 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4626 if (xpc.xp_context == EXPAND_NOTHING)
4627 {
4628 if (argvars[1].v_type == VAR_STRING)
4629 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4630 else
4631 EMSG(_(e_invarg));
4632 return;
4633 }
4634
4635# if defined(FEAT_MENU)
4636 if (xpc.xp_context == EXPAND_MENUS)
4637 {
4638 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4639 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4640 }
4641# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004642#ifdef FEAT_CSCOPE
4643 if (xpc.xp_context == EXPAND_CSCOPE)
4644 {
4645 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4646 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4647 }
4648#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004649#ifdef FEAT_SIGNS
4650 if (xpc.xp_context == EXPAND_SIGN)
4651 {
4652 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4653 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4654 }
4655#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004656
4657 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4658 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4659 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004660 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004661
4662 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4663
4664 for (i = 0; i < xpc.xp_numfiles; i++)
4665 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4666 }
4667 vim_free(pat);
4668 ExpandCleanup(&xpc);
4669}
4670#endif
4671
4672/*
4673 * "getcwd()" function
4674 */
4675 static void
4676f_getcwd(typval_T *argvars, typval_T *rettv)
4677{
4678 win_T *wp = NULL;
4679 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004680 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004681
4682 rettv->v_type = VAR_STRING;
4683 rettv->vval.v_string = NULL;
4684
Bram Moolenaar54591292018-02-09 20:53:59 +01004685 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4686 global = TRUE;
4687 else
4688 wp = find_tabwin(&argvars[0], &argvars[1]);
4689
4690 if (wp != NULL && wp->w_localdir != NULL)
4691 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4692 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004693 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004694 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004695 rettv->vval.v_string = vim_strsave(globaldir);
4696 else
4697 {
4698 cwd = alloc(MAXPATHL);
4699 if (cwd != NULL)
4700 {
4701 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4702 rettv->vval.v_string = vim_strsave(cwd);
4703 vim_free(cwd);
4704 }
4705 }
4706#ifdef BACKSLASH_IN_FILENAME
4707 if (rettv->vval.v_string != NULL)
4708 slash_adjust(rettv->vval.v_string);
4709#endif
4710 }
4711}
4712
4713/*
4714 * "getfontname()" function
4715 */
4716 static void
4717f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4718{
4719 rettv->v_type = VAR_STRING;
4720 rettv->vval.v_string = NULL;
4721#ifdef FEAT_GUI
4722 if (gui.in_use)
4723 {
4724 GuiFont font;
4725 char_u *name = NULL;
4726
4727 if (argvars[0].v_type == VAR_UNKNOWN)
4728 {
4729 /* Get the "Normal" font. Either the name saved by
4730 * hl_set_font_name() or from the font ID. */
4731 font = gui.norm_font;
4732 name = hl_get_font_name();
4733 }
4734 else
4735 {
4736 name = get_tv_string(&argvars[0]);
4737 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4738 return;
4739 font = gui_mch_get_font(name, FALSE);
4740 if (font == NOFONT)
4741 return; /* Invalid font name, return empty string. */
4742 }
4743 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4744 if (argvars[0].v_type != VAR_UNKNOWN)
4745 gui_mch_free_font(font);
4746 }
4747#endif
4748}
4749
4750/*
4751 * "getfperm({fname})" function
4752 */
4753 static void
4754f_getfperm(typval_T *argvars, typval_T *rettv)
4755{
4756 char_u *fname;
4757 stat_T st;
4758 char_u *perm = NULL;
4759 char_u flags[] = "rwx";
4760 int i;
4761
4762 fname = get_tv_string(&argvars[0]);
4763
4764 rettv->v_type = VAR_STRING;
4765 if (mch_stat((char *)fname, &st) >= 0)
4766 {
4767 perm = vim_strsave((char_u *)"---------");
4768 if (perm != NULL)
4769 {
4770 for (i = 0; i < 9; i++)
4771 {
4772 if (st.st_mode & (1 << (8 - i)))
4773 perm[i] = flags[i % 3];
4774 }
4775 }
4776 }
4777 rettv->vval.v_string = perm;
4778}
4779
4780/*
4781 * "getfsize({fname})" function
4782 */
4783 static void
4784f_getfsize(typval_T *argvars, typval_T *rettv)
4785{
4786 char_u *fname;
4787 stat_T st;
4788
4789 fname = get_tv_string(&argvars[0]);
4790
4791 rettv->v_type = VAR_NUMBER;
4792
4793 if (mch_stat((char *)fname, &st) >= 0)
4794 {
4795 if (mch_isdir(fname))
4796 rettv->vval.v_number = 0;
4797 else
4798 {
4799 rettv->vval.v_number = (varnumber_T)st.st_size;
4800
4801 /* non-perfect check for overflow */
4802 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4803 rettv->vval.v_number = -2;
4804 }
4805 }
4806 else
4807 rettv->vval.v_number = -1;
4808}
4809
4810/*
4811 * "getftime({fname})" function
4812 */
4813 static void
4814f_getftime(typval_T *argvars, typval_T *rettv)
4815{
4816 char_u *fname;
4817 stat_T st;
4818
4819 fname = get_tv_string(&argvars[0]);
4820
4821 if (mch_stat((char *)fname, &st) >= 0)
4822 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4823 else
4824 rettv->vval.v_number = -1;
4825}
4826
4827/*
4828 * "getftype({fname})" function
4829 */
4830 static void
4831f_getftype(typval_T *argvars, typval_T *rettv)
4832{
4833 char_u *fname;
4834 stat_T st;
4835 char_u *type = NULL;
4836 char *t;
4837
4838 fname = get_tv_string(&argvars[0]);
4839
4840 rettv->v_type = VAR_STRING;
4841 if (mch_lstat((char *)fname, &st) >= 0)
4842 {
4843#ifdef S_ISREG
4844 if (S_ISREG(st.st_mode))
4845 t = "file";
4846 else if (S_ISDIR(st.st_mode))
4847 t = "dir";
4848# ifdef S_ISLNK
4849 else if (S_ISLNK(st.st_mode))
4850 t = "link";
4851# endif
4852# ifdef S_ISBLK
4853 else if (S_ISBLK(st.st_mode))
4854 t = "bdev";
4855# endif
4856# ifdef S_ISCHR
4857 else if (S_ISCHR(st.st_mode))
4858 t = "cdev";
4859# endif
4860# ifdef S_ISFIFO
4861 else if (S_ISFIFO(st.st_mode))
4862 t = "fifo";
4863# endif
4864# ifdef S_ISSOCK
4865 else if (S_ISSOCK(st.st_mode))
4866 t = "fifo";
4867# endif
4868 else
4869 t = "other";
4870#else
4871# ifdef S_IFMT
4872 switch (st.st_mode & S_IFMT)
4873 {
4874 case S_IFREG: t = "file"; break;
4875 case S_IFDIR: t = "dir"; break;
4876# ifdef S_IFLNK
4877 case S_IFLNK: t = "link"; break;
4878# endif
4879# ifdef S_IFBLK
4880 case S_IFBLK: t = "bdev"; break;
4881# endif
4882# ifdef S_IFCHR
4883 case S_IFCHR: t = "cdev"; break;
4884# endif
4885# ifdef S_IFIFO
4886 case S_IFIFO: t = "fifo"; break;
4887# endif
4888# ifdef S_IFSOCK
4889 case S_IFSOCK: t = "socket"; break;
4890# endif
4891 default: t = "other";
4892 }
4893# else
4894 if (mch_isdir(fname))
4895 t = "dir";
4896 else
4897 t = "file";
4898# endif
4899#endif
4900 type = vim_strsave((char_u *)t);
4901 }
4902 rettv->vval.v_string = type;
4903}
4904
4905/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004906 * "getjumplist()" function
4907 */
4908 static void
4909f_getjumplist(typval_T *argvars, typval_T *rettv)
4910{
4911#ifdef FEAT_JUMPLIST
4912 win_T *wp;
4913 int i;
4914 list_T *l;
4915 dict_T *d;
4916#endif
4917
4918 if (rettv_list_alloc(rettv) != OK)
4919 return;
4920
4921#ifdef FEAT_JUMPLIST
4922 wp = find_tabwin(&argvars[0], &argvars[1]);
4923 if (wp == NULL)
4924 return;
4925
4926 l = list_alloc();
4927 if (l == NULL)
4928 return;
4929
4930 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4931 return;
4932 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4933
Bram Moolenaar48679742018-02-13 13:33:29 +01004934 cleanup_jumplist(wp, TRUE);
4935
Bram Moolenaar4f505882018-02-10 21:06:32 +01004936 for (i = 0; i < wp->w_jumplistlen; ++i)
4937 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004938 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4939 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004940 if ((d = dict_alloc()) == NULL)
4941 return;
4942 if (list_append_dict(l, d) == FAIL)
4943 return;
4944 dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
4945 NULL);
4946 dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
4947 NULL);
4948# ifdef FEAT_VIRTUALEDIT
4949 dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
4950 NULL);
4951# endif
4952 dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004953 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaar4f505882018-02-10 21:06:32 +01004954 dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
4955 }
4956#endif
4957}
4958
4959/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004960 * "getline(lnum, [end])" function
4961 */
4962 static void
4963f_getline(typval_T *argvars, typval_T *rettv)
4964{
4965 linenr_T lnum;
4966 linenr_T end;
4967 int retlist;
4968
4969 lnum = get_tv_lnum(argvars);
4970 if (argvars[1].v_type == VAR_UNKNOWN)
4971 {
4972 end = 0;
4973 retlist = FALSE;
4974 }
4975 else
4976 {
4977 end = get_tv_lnum(&argvars[1]);
4978 retlist = TRUE;
4979 }
4980
4981 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4982}
4983
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004984#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004985 static void
4986get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4987{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004988 if (what_arg->v_type == VAR_UNKNOWN)
4989 {
4990 if (rettv_list_alloc(rettv) == OK)
4991 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004992 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004993 }
4994 else
4995 {
4996 if (rettv_dict_alloc(rettv) == OK)
4997 if (is_qf || (wp != NULL))
4998 {
4999 if (what_arg->v_type == VAR_DICT)
5000 {
5001 dict_T *d = what_arg->vval.v_dict;
5002
5003 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005004 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005005 }
5006 else
5007 EMSG(_(e_dictreq));
5008 }
5009 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005010}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005011#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005012
5013/*
5014 * "getloclist()" function
5015 */
5016 static void
5017f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5018{
5019#ifdef FEAT_QUICKFIX
5020 win_T *wp;
5021
5022 wp = find_win_by_nr(&argvars[0], NULL);
5023 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5024#endif
5025}
5026
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005027/*
5028 * "getmatches()" function
5029 */
5030 static void
5031f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5032{
5033#ifdef FEAT_SEARCH_EXTRA
5034 dict_T *dict;
5035 matchitem_T *cur = curwin->w_match_head;
5036 int i;
5037
5038 if (rettv_list_alloc(rettv) == OK)
5039 {
5040 while (cur != NULL)
5041 {
5042 dict = dict_alloc();
5043 if (dict == NULL)
5044 return;
5045 if (cur->match.regprog == NULL)
5046 {
5047 /* match added with matchaddpos() */
5048 for (i = 0; i < MAXPOSMATCH; ++i)
5049 {
5050 llpos_T *llpos;
5051 char buf[6];
5052 list_T *l;
5053
5054 llpos = &cur->pos.pos[i];
5055 if (llpos->lnum == 0)
5056 break;
5057 l = list_alloc();
5058 if (l == NULL)
5059 break;
5060 list_append_number(l, (varnumber_T)llpos->lnum);
5061 if (llpos->col > 0)
5062 {
5063 list_append_number(l, (varnumber_T)llpos->col);
5064 list_append_number(l, (varnumber_T)llpos->len);
5065 }
5066 sprintf(buf, "pos%d", i + 1);
5067 dict_add_list(dict, buf, l);
5068 }
5069 }
5070 else
5071 {
5072 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
5073 }
5074 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
5075 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
5076 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
5077# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5078 if (cur->conceal_char)
5079 {
5080 char_u buf[MB_MAXBYTES + 1];
5081
5082 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5083 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5084 }
5085# endif
5086 list_append_dict(rettv->vval.v_list, dict);
5087 cur = cur->next;
5088 }
5089 }
5090#endif
5091}
5092
5093/*
5094 * "getpid()" function
5095 */
5096 static void
5097f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5098{
5099 rettv->vval.v_number = mch_get_pid();
5100}
5101
5102 static void
5103getpos_both(
5104 typval_T *argvars,
5105 typval_T *rettv,
5106 int getcurpos)
5107{
5108 pos_T *fp;
5109 list_T *l;
5110 int fnum = -1;
5111
5112 if (rettv_list_alloc(rettv) == OK)
5113 {
5114 l = rettv->vval.v_list;
5115 if (getcurpos)
5116 fp = &curwin->w_cursor;
5117 else
5118 fp = var2fpos(&argvars[0], TRUE, &fnum);
5119 if (fnum != -1)
5120 list_append_number(l, (varnumber_T)fnum);
5121 else
5122 list_append_number(l, (varnumber_T)0);
5123 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5124 : (varnumber_T)0);
5125 list_append_number(l, (fp != NULL)
5126 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5127 : (varnumber_T)0);
5128 list_append_number(l,
5129#ifdef FEAT_VIRTUALEDIT
5130 (fp != NULL) ? (varnumber_T)fp->coladd :
5131#endif
5132 (varnumber_T)0);
5133 if (getcurpos)
5134 {
5135 update_curswant();
5136 list_append_number(l, curwin->w_curswant == MAXCOL ?
5137 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5138 }
5139 }
5140 else
5141 rettv->vval.v_number = FALSE;
5142}
5143
5144
5145/*
5146 * "getcurpos()" function
5147 */
5148 static void
5149f_getcurpos(typval_T *argvars, typval_T *rettv)
5150{
5151 getpos_both(argvars, rettv, TRUE);
5152}
5153
5154/*
5155 * "getpos(string)" function
5156 */
5157 static void
5158f_getpos(typval_T *argvars, typval_T *rettv)
5159{
5160 getpos_both(argvars, rettv, FALSE);
5161}
5162
5163/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005164 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005165 */
5166 static void
5167f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5168{
5169#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005170 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005171#endif
5172}
5173
5174/*
5175 * "getreg()" function
5176 */
5177 static void
5178f_getreg(typval_T *argvars, typval_T *rettv)
5179{
5180 char_u *strregname;
5181 int regname;
5182 int arg2 = FALSE;
5183 int return_list = FALSE;
5184 int error = FALSE;
5185
5186 if (argvars[0].v_type != VAR_UNKNOWN)
5187 {
5188 strregname = get_tv_string_chk(&argvars[0]);
5189 error = strregname == NULL;
5190 if (argvars[1].v_type != VAR_UNKNOWN)
5191 {
5192 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5193 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5194 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5195 }
5196 }
5197 else
5198 strregname = get_vim_var_str(VV_REG);
5199
5200 if (error)
5201 return;
5202
5203 regname = (strregname == NULL ? '"' : *strregname);
5204 if (regname == 0)
5205 regname = '"';
5206
5207 if (return_list)
5208 {
5209 rettv->v_type = VAR_LIST;
5210 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5211 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5212 if (rettv->vval.v_list == NULL)
5213 (void)rettv_list_alloc(rettv);
5214 else
5215 ++rettv->vval.v_list->lv_refcount;
5216 }
5217 else
5218 {
5219 rettv->v_type = VAR_STRING;
5220 rettv->vval.v_string = get_reg_contents(regname,
5221 arg2 ? GREG_EXPR_SRC : 0);
5222 }
5223}
5224
5225/*
5226 * "getregtype()" function
5227 */
5228 static void
5229f_getregtype(typval_T *argvars, typval_T *rettv)
5230{
5231 char_u *strregname;
5232 int regname;
5233 char_u buf[NUMBUFLEN + 2];
5234 long reglen = 0;
5235
5236 if (argvars[0].v_type != VAR_UNKNOWN)
5237 {
5238 strregname = get_tv_string_chk(&argvars[0]);
5239 if (strregname == NULL) /* type error; errmsg already given */
5240 {
5241 rettv->v_type = VAR_STRING;
5242 rettv->vval.v_string = NULL;
5243 return;
5244 }
5245 }
5246 else
5247 /* Default to v:register */
5248 strregname = get_vim_var_str(VV_REG);
5249
5250 regname = (strregname == NULL ? '"' : *strregname);
5251 if (regname == 0)
5252 regname = '"';
5253
5254 buf[0] = NUL;
5255 buf[1] = NUL;
5256 switch (get_reg_type(regname, &reglen))
5257 {
5258 case MLINE: buf[0] = 'V'; break;
5259 case MCHAR: buf[0] = 'v'; break;
5260 case MBLOCK:
5261 buf[0] = Ctrl_V;
5262 sprintf((char *)buf + 1, "%ld", reglen + 1);
5263 break;
5264 }
5265 rettv->v_type = VAR_STRING;
5266 rettv->vval.v_string = vim_strsave(buf);
5267}
5268
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005269/*
5270 * Returns information (variables, options, etc.) about a tab page
5271 * as a dictionary.
5272 */
5273 static dict_T *
5274get_tabpage_info(tabpage_T *tp, int tp_idx)
5275{
5276 win_T *wp;
5277 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005278 list_T *l;
5279
5280 dict = dict_alloc();
5281 if (dict == NULL)
5282 return NULL;
5283
Bram Moolenaar33928832016-08-18 21:22:04 +02005284 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005285
5286 l = list_alloc();
5287 if (l != NULL)
5288 {
5289 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5290 wp; wp = wp->w_next)
5291 list_append_number(l, (varnumber_T)wp->w_id);
5292 dict_add_list(dict, "windows", l);
5293 }
5294
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005295 /* Make a reference to tabpage variables */
5296 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005297
5298 return dict;
5299}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005300
5301/*
5302 * "gettabinfo()" function
5303 */
5304 static void
5305f_gettabinfo(typval_T *argvars, typval_T *rettv)
5306{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005307 tabpage_T *tp, *tparg = NULL;
5308 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005309 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005310
5311 if (rettv_list_alloc(rettv) != OK)
5312 return;
5313
5314 if (argvars[0].v_type != VAR_UNKNOWN)
5315 {
5316 /* Information about one tab page */
5317 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5318 if (tparg == NULL)
5319 return;
5320 }
5321
5322 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005323 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005324 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005325 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005326 if (tparg != NULL && tp != tparg)
5327 continue;
5328 d = get_tabpage_info(tp, tpnr);
5329 if (d != NULL)
5330 list_append_dict(rettv->vval.v_list, d);
5331 if (tparg != NULL)
5332 return;
5333 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005334}
5335
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005336/*
5337 * "gettabvar()" function
5338 */
5339 static void
5340f_gettabvar(typval_T *argvars, typval_T *rettv)
5341{
5342 win_T *oldcurwin;
5343 tabpage_T *tp, *oldtabpage;
5344 dictitem_T *v;
5345 char_u *varname;
5346 int done = FALSE;
5347
5348 rettv->v_type = VAR_STRING;
5349 rettv->vval.v_string = NULL;
5350
5351 varname = get_tv_string_chk(&argvars[1]);
5352 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5353 if (tp != NULL && varname != NULL)
5354 {
5355 /* Set tp to be our tabpage, temporarily. Also set the window to the
5356 * first window in the tabpage, otherwise the window is not valid. */
5357 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005358 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5359 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005360 {
5361 /* look up the variable */
5362 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5363 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5364 if (v != NULL)
5365 {
5366 copy_tv(&v->di_tv, rettv);
5367 done = TRUE;
5368 }
5369 }
5370
5371 /* restore previous notion of curwin */
5372 restore_win(oldcurwin, oldtabpage, TRUE);
5373 }
5374
5375 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5376 copy_tv(&argvars[2], rettv);
5377}
5378
5379/*
5380 * "gettabwinvar()" function
5381 */
5382 static void
5383f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5384{
5385 getwinvar(argvars, rettv, 1);
5386}
5387
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005388/*
5389 * Returns information about a window as a dictionary.
5390 */
5391 static dict_T *
5392get_win_info(win_T *wp, short tpnr, short winnr)
5393{
5394 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005395
5396 dict = dict_alloc();
5397 if (dict == NULL)
5398 return NULL;
5399
Bram Moolenaar33928832016-08-18 21:22:04 +02005400 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5401 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005402 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5403 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005404#ifdef FEAT_MENU
5405 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5406#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005407 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005408 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005409
Bram Moolenaar69905d12017-08-13 18:14:47 +02005410#ifdef FEAT_TERMINAL
5411 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5412#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005413#ifdef FEAT_QUICKFIX
5414 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5415 dict_add_nr_str(dict, "loclist",
5416 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5417#endif
5418
Bram Moolenaar30567352016-08-27 21:25:44 +02005419 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005420 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005421
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005422 return dict;
5423}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005424
5425/*
5426 * "getwininfo()" function
5427 */
5428 static void
5429f_getwininfo(typval_T *argvars, typval_T *rettv)
5430{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005431 tabpage_T *tp;
5432 win_T *wp = NULL, *wparg = NULL;
5433 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005434 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005435
5436 if (rettv_list_alloc(rettv) != OK)
5437 return;
5438
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005439 if (argvars[0].v_type != VAR_UNKNOWN)
5440 {
5441 wparg = win_id2wp(argvars);
5442 if (wparg == NULL)
5443 return;
5444 }
5445
5446 /* Collect information about either all the windows across all the tab
5447 * pages or one particular window.
5448 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005449 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005450 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005451 tabnr++;
5452 winnr = 0;
5453 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005454 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005455 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005456 if (wparg != NULL && wp != wparg)
5457 continue;
5458 d = get_win_info(wp, tabnr, winnr);
5459 if (d != NULL)
5460 list_append_dict(rettv->vval.v_list, d);
5461 if (wparg != NULL)
5462 /* found information about a specific window */
5463 return;
5464 }
5465 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005466}
5467
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005468/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005469 * "win_findbuf()" function
5470 */
5471 static void
5472f_win_findbuf(typval_T *argvars, typval_T *rettv)
5473{
5474 if (rettv_list_alloc(rettv) != FAIL)
5475 win_findbuf(argvars, rettv->vval.v_list);
5476}
5477
5478/*
5479 * "win_getid()" function
5480 */
5481 static void
5482f_win_getid(typval_T *argvars, typval_T *rettv)
5483{
5484 rettv->vval.v_number = win_getid(argvars);
5485}
5486
5487/*
5488 * "win_gotoid()" function
5489 */
5490 static void
5491f_win_gotoid(typval_T *argvars, typval_T *rettv)
5492{
5493 rettv->vval.v_number = win_gotoid(argvars);
5494}
5495
5496/*
5497 * "win_id2tabwin()" function
5498 */
5499 static void
5500f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5501{
5502 if (rettv_list_alloc(rettv) != FAIL)
5503 win_id2tabwin(argvars, rettv->vval.v_list);
5504}
5505
5506/*
5507 * "win_id2win()" function
5508 */
5509 static void
5510f_win_id2win(typval_T *argvars, typval_T *rettv)
5511{
5512 rettv->vval.v_number = win_id2win(argvars);
5513}
5514
5515/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005516 * "win_screenpos()" function
5517 */
5518 static void
5519f_win_screenpos(typval_T *argvars, typval_T *rettv)
5520{
5521 win_T *wp;
5522
5523 if (rettv_list_alloc(rettv) == FAIL)
5524 return;
5525
5526 wp = find_win_by_nr(&argvars[0], NULL);
5527 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5528 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5529}
5530
5531/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005532 * "getwinpos({timeout})" function
5533 */
5534 static void
5535f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5536{
5537 int x = -1;
5538 int y = -1;
5539
5540 if (rettv_list_alloc(rettv) == FAIL)
5541 return;
5542#ifdef FEAT_GUI
5543 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005544 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005545# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5546 else
5547# endif
5548#endif
5549#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5550 {
5551 varnumber_T timeout = 100;
5552
5553 if (argvars[0].v_type != VAR_UNKNOWN)
5554 timeout = get_tv_number(&argvars[0]);
5555 term_get_winpos(&x, &y, timeout);
5556 }
5557#endif
5558 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5559 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5560}
5561
5562
5563/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005564 * "getwinposx()" function
5565 */
5566 static void
5567f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5568{
5569 rettv->vval.v_number = -1;
5570#ifdef FEAT_GUI
5571 if (gui.in_use)
5572 {
5573 int x, y;
5574
5575 if (gui_mch_get_winpos(&x, &y) == OK)
5576 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005577 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005578 }
5579#endif
5580#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5581 {
5582 int x, y;
5583
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005584 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005585 rettv->vval.v_number = x;
5586 }
5587#endif
5588}
5589
5590/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005591 * "getwinposy()" function
5592 */
5593 static void
5594f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5595{
5596 rettv->vval.v_number = -1;
5597#ifdef FEAT_GUI
5598 if (gui.in_use)
5599 {
5600 int x, y;
5601
5602 if (gui_mch_get_winpos(&x, &y) == OK)
5603 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005604 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005605 }
5606#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005607#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5608 {
5609 int x, y;
5610
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005611 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005612 rettv->vval.v_number = y;
5613 }
5614#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005615}
5616
5617/*
5618 * "getwinvar()" function
5619 */
5620 static void
5621f_getwinvar(typval_T *argvars, typval_T *rettv)
5622{
5623 getwinvar(argvars, rettv, 0);
5624}
5625
5626/*
5627 * "glob()" function
5628 */
5629 static void
5630f_glob(typval_T *argvars, typval_T *rettv)
5631{
5632 int options = WILD_SILENT|WILD_USE_NL;
5633 expand_T xpc;
5634 int error = FALSE;
5635
5636 /* When the optional second argument is non-zero, don't remove matches
5637 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5638 rettv->v_type = VAR_STRING;
5639 if (argvars[1].v_type != VAR_UNKNOWN)
5640 {
5641 if (get_tv_number_chk(&argvars[1], &error))
5642 options |= WILD_KEEP_ALL;
5643 if (argvars[2].v_type != VAR_UNKNOWN)
5644 {
5645 if (get_tv_number_chk(&argvars[2], &error))
5646 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005647 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005648 }
5649 if (argvars[3].v_type != VAR_UNKNOWN
5650 && get_tv_number_chk(&argvars[3], &error))
5651 options |= WILD_ALLLINKS;
5652 }
5653 }
5654 if (!error)
5655 {
5656 ExpandInit(&xpc);
5657 xpc.xp_context = EXPAND_FILES;
5658 if (p_wic)
5659 options += WILD_ICASE;
5660 if (rettv->v_type == VAR_STRING)
5661 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5662 NULL, options, WILD_ALL);
5663 else if (rettv_list_alloc(rettv) != FAIL)
5664 {
5665 int i;
5666
5667 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5668 NULL, options, WILD_ALL_KEEP);
5669 for (i = 0; i < xpc.xp_numfiles; i++)
5670 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5671
5672 ExpandCleanup(&xpc);
5673 }
5674 }
5675 else
5676 rettv->vval.v_string = NULL;
5677}
5678
5679/*
5680 * "globpath()" function
5681 */
5682 static void
5683f_globpath(typval_T *argvars, typval_T *rettv)
5684{
5685 int flags = 0;
5686 char_u buf1[NUMBUFLEN];
5687 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5688 int error = FALSE;
5689 garray_T ga;
5690 int i;
5691
5692 /* When the optional second argument is non-zero, don't remove matches
5693 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5694 rettv->v_type = VAR_STRING;
5695 if (argvars[2].v_type != VAR_UNKNOWN)
5696 {
5697 if (get_tv_number_chk(&argvars[2], &error))
5698 flags |= WILD_KEEP_ALL;
5699 if (argvars[3].v_type != VAR_UNKNOWN)
5700 {
5701 if (get_tv_number_chk(&argvars[3], &error))
5702 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005703 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005704 }
5705 if (argvars[4].v_type != VAR_UNKNOWN
5706 && get_tv_number_chk(&argvars[4], &error))
5707 flags |= WILD_ALLLINKS;
5708 }
5709 }
5710 if (file != NULL && !error)
5711 {
5712 ga_init2(&ga, (int)sizeof(char_u *), 10);
5713 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5714 if (rettv->v_type == VAR_STRING)
5715 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5716 else if (rettv_list_alloc(rettv) != FAIL)
5717 for (i = 0; i < ga.ga_len; ++i)
5718 list_append_string(rettv->vval.v_list,
5719 ((char_u **)(ga.ga_data))[i], -1);
5720 ga_clear_strings(&ga);
5721 }
5722 else
5723 rettv->vval.v_string = NULL;
5724}
5725
5726/*
5727 * "glob2regpat()" function
5728 */
5729 static void
5730f_glob2regpat(typval_T *argvars, typval_T *rettv)
5731{
5732 char_u *pat = get_tv_string_chk(&argvars[0]);
5733
5734 rettv->v_type = VAR_STRING;
5735 rettv->vval.v_string = (pat == NULL)
5736 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5737}
5738
5739/* for VIM_VERSION_ defines */
5740#include "version.h"
5741
5742/*
5743 * "has()" function
5744 */
5745 static void
5746f_has(typval_T *argvars, typval_T *rettv)
5747{
5748 int i;
5749 char_u *name;
5750 int n = FALSE;
5751 static char *(has_list[]) =
5752 {
5753#ifdef AMIGA
5754 "amiga",
5755# ifdef FEAT_ARP
5756 "arp",
5757# endif
5758#endif
5759#ifdef __BEOS__
5760 "beos",
5761#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005762#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005763 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5764 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005765# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005766 "macunix", /* Mac OS X, with the darwin feature */
5767 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005768# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005769#endif
5770#ifdef __QNX__
5771 "qnx",
5772#endif
5773#ifdef UNIX
5774 "unix",
5775#endif
5776#ifdef VMS
5777 "vms",
5778#endif
5779#ifdef WIN32
5780 "win32",
5781#endif
5782#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5783 "win32unix",
5784#endif
5785#if defined(WIN64) || defined(_WIN64)
5786 "win64",
5787#endif
5788#ifdef EBCDIC
5789 "ebcdic",
5790#endif
5791#ifndef CASE_INSENSITIVE_FILENAME
5792 "fname_case",
5793#endif
5794#ifdef HAVE_ACL
5795 "acl",
5796#endif
5797#ifdef FEAT_ARABIC
5798 "arabic",
5799#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005800 "autocmd",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005801#ifdef FEAT_AUTOSERVERNAME
5802 "autoservername",
5803#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005804#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005805 "balloon_eval",
5806# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5807 "balloon_multiline",
5808# endif
5809#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005810#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005811 "balloon_eval_term",
5812#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005813#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5814 "builtin_terms",
5815# ifdef ALL_BUILTIN_TCAPS
5816 "all_builtin_terms",
5817# endif
5818#endif
5819#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5820 || defined(FEAT_GUI_W32) \
5821 || defined(FEAT_GUI_MOTIF))
5822 "browsefilter",
5823#endif
5824#ifdef FEAT_BYTEOFF
5825 "byte_offset",
5826#endif
5827#ifdef FEAT_JOB_CHANNEL
5828 "channel",
5829#endif
5830#ifdef FEAT_CINDENT
5831 "cindent",
5832#endif
5833#ifdef FEAT_CLIENTSERVER
5834 "clientserver",
5835#endif
5836#ifdef FEAT_CLIPBOARD
5837 "clipboard",
5838#endif
5839#ifdef FEAT_CMDL_COMPL
5840 "cmdline_compl",
5841#endif
5842#ifdef FEAT_CMDHIST
5843 "cmdline_hist",
5844#endif
5845#ifdef FEAT_COMMENTS
5846 "comments",
5847#endif
5848#ifdef FEAT_CONCEAL
5849 "conceal",
5850#endif
5851#ifdef FEAT_CRYPT
5852 "cryptv",
5853 "crypt-blowfish",
5854 "crypt-blowfish2",
5855#endif
5856#ifdef FEAT_CSCOPE
5857 "cscope",
5858#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005859 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005860#ifdef CURSOR_SHAPE
5861 "cursorshape",
5862#endif
5863#ifdef DEBUG
5864 "debug",
5865#endif
5866#ifdef FEAT_CON_DIALOG
5867 "dialog_con",
5868#endif
5869#ifdef FEAT_GUI_DIALOG
5870 "dialog_gui",
5871#endif
5872#ifdef FEAT_DIFF
5873 "diff",
5874#endif
5875#ifdef FEAT_DIGRAPHS
5876 "digraphs",
5877#endif
5878#ifdef FEAT_DIRECTX
5879 "directx",
5880#endif
5881#ifdef FEAT_DND
5882 "dnd",
5883#endif
5884#ifdef FEAT_EMACS_TAGS
5885 "emacs_tags",
5886#endif
5887 "eval", /* always present, of course! */
5888 "ex_extra", /* graduated feature */
5889#ifdef FEAT_SEARCH_EXTRA
5890 "extra_search",
5891#endif
5892#ifdef FEAT_FKMAP
5893 "farsi",
5894#endif
5895#ifdef FEAT_SEARCHPATH
5896 "file_in_path",
5897#endif
5898#ifdef FEAT_FILTERPIPE
5899 "filterpipe",
5900#endif
5901#ifdef FEAT_FIND_ID
5902 "find_in_path",
5903#endif
5904#ifdef FEAT_FLOAT
5905 "float",
5906#endif
5907#ifdef FEAT_FOLDING
5908 "folding",
5909#endif
5910#ifdef FEAT_FOOTER
5911 "footer",
5912#endif
5913#if !defined(USE_SYSTEM) && defined(UNIX)
5914 "fork",
5915#endif
5916#ifdef FEAT_GETTEXT
5917 "gettext",
5918#endif
5919#ifdef FEAT_GUI
5920 "gui",
5921#endif
5922#ifdef FEAT_GUI_ATHENA
5923# ifdef FEAT_GUI_NEXTAW
5924 "gui_neXtaw",
5925# else
5926 "gui_athena",
5927# endif
5928#endif
5929#ifdef FEAT_GUI_GTK
5930 "gui_gtk",
5931# ifdef USE_GTK3
5932 "gui_gtk3",
5933# else
5934 "gui_gtk2",
5935# endif
5936#endif
5937#ifdef FEAT_GUI_GNOME
5938 "gui_gnome",
5939#endif
5940#ifdef FEAT_GUI_MAC
5941 "gui_mac",
5942#endif
5943#ifdef FEAT_GUI_MOTIF
5944 "gui_motif",
5945#endif
5946#ifdef FEAT_GUI_PHOTON
5947 "gui_photon",
5948#endif
5949#ifdef FEAT_GUI_W32
5950 "gui_win32",
5951#endif
5952#ifdef FEAT_HANGULIN
5953 "hangul_input",
5954#endif
5955#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5956 "iconv",
5957#endif
5958#ifdef FEAT_INS_EXPAND
5959 "insert_expand",
5960#endif
5961#ifdef FEAT_JOB_CHANNEL
5962 "job",
5963#endif
5964#ifdef FEAT_JUMPLIST
5965 "jumplist",
5966#endif
5967#ifdef FEAT_KEYMAP
5968 "keymap",
5969#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005970 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005971#ifdef FEAT_LANGMAP
5972 "langmap",
5973#endif
5974#ifdef FEAT_LIBCALL
5975 "libcall",
5976#endif
5977#ifdef FEAT_LINEBREAK
5978 "linebreak",
5979#endif
5980#ifdef FEAT_LISP
5981 "lispindent",
5982#endif
5983#ifdef FEAT_LISTCMDS
5984 "listcmds",
5985#endif
5986#ifdef FEAT_LOCALMAP
5987 "localmap",
5988#endif
5989#ifdef FEAT_LUA
5990# ifndef DYNAMIC_LUA
5991 "lua",
5992# endif
5993#endif
5994#ifdef FEAT_MENU
5995 "menu",
5996#endif
5997#ifdef FEAT_SESSION
5998 "mksession",
5999#endif
6000#ifdef FEAT_MODIFY_FNAME
6001 "modify_fname",
6002#endif
6003#ifdef FEAT_MOUSE
6004 "mouse",
6005#endif
6006#ifdef FEAT_MOUSESHAPE
6007 "mouseshape",
6008#endif
6009#if defined(UNIX) || defined(VMS)
6010# ifdef FEAT_MOUSE_DEC
6011 "mouse_dec",
6012# endif
6013# ifdef FEAT_MOUSE_GPM
6014 "mouse_gpm",
6015# endif
6016# ifdef FEAT_MOUSE_JSB
6017 "mouse_jsbterm",
6018# endif
6019# ifdef FEAT_MOUSE_NET
6020 "mouse_netterm",
6021# endif
6022# ifdef FEAT_MOUSE_PTERM
6023 "mouse_pterm",
6024# endif
6025# ifdef FEAT_MOUSE_SGR
6026 "mouse_sgr",
6027# endif
6028# ifdef FEAT_SYSMOUSE
6029 "mouse_sysmouse",
6030# endif
6031# ifdef FEAT_MOUSE_URXVT
6032 "mouse_urxvt",
6033# endif
6034# ifdef FEAT_MOUSE_XTERM
6035 "mouse_xterm",
6036# endif
6037#endif
6038#ifdef FEAT_MBYTE
6039 "multi_byte",
6040#endif
6041#ifdef FEAT_MBYTE_IME
6042 "multi_byte_ime",
6043#endif
6044#ifdef FEAT_MULTI_LANG
6045 "multi_lang",
6046#endif
6047#ifdef FEAT_MZSCHEME
6048#ifndef DYNAMIC_MZSCHEME
6049 "mzscheme",
6050#endif
6051#endif
6052#ifdef FEAT_NUM64
6053 "num64",
6054#endif
6055#ifdef FEAT_OLE
6056 "ole",
6057#endif
6058 "packages",
6059#ifdef FEAT_PATH_EXTRA
6060 "path_extra",
6061#endif
6062#ifdef FEAT_PERL
6063#ifndef DYNAMIC_PERL
6064 "perl",
6065#endif
6066#endif
6067#ifdef FEAT_PERSISTENT_UNDO
6068 "persistent_undo",
6069#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006070#if defined(FEAT_PYTHON)
6071 "python_compiled",
6072# if defined(DYNAMIC_PYTHON)
6073 "python_dynamic",
6074# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006075 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006076 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006077# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006078#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006079#if defined(FEAT_PYTHON3)
6080 "python3_compiled",
6081# if defined(DYNAMIC_PYTHON3)
6082 "python3_dynamic",
6083# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006084 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006085 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006086# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006087#endif
6088#ifdef FEAT_POSTSCRIPT
6089 "postscript",
6090#endif
6091#ifdef FEAT_PRINTER
6092 "printer",
6093#endif
6094#ifdef FEAT_PROFILE
6095 "profile",
6096#endif
6097#ifdef FEAT_RELTIME
6098 "reltime",
6099#endif
6100#ifdef FEAT_QUICKFIX
6101 "quickfix",
6102#endif
6103#ifdef FEAT_RIGHTLEFT
6104 "rightleft",
6105#endif
6106#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6107 "ruby",
6108#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006109 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006110#ifdef FEAT_CMDL_INFO
6111 "showcmd",
6112 "cmdline_info",
6113#endif
6114#ifdef FEAT_SIGNS
6115 "signs",
6116#endif
6117#ifdef FEAT_SMARTINDENT
6118 "smartindent",
6119#endif
6120#ifdef STARTUPTIME
6121 "startuptime",
6122#endif
6123#ifdef FEAT_STL_OPT
6124 "statusline",
6125#endif
6126#ifdef FEAT_SUN_WORKSHOP
6127 "sun_workshop",
6128#endif
6129#ifdef FEAT_NETBEANS_INTG
6130 "netbeans_intg",
6131#endif
6132#ifdef FEAT_SPELL
6133 "spell",
6134#endif
6135#ifdef FEAT_SYN_HL
6136 "syntax",
6137#endif
6138#if defined(USE_SYSTEM) || !defined(UNIX)
6139 "system",
6140#endif
6141#ifdef FEAT_TAG_BINS
6142 "tag_binary",
6143#endif
6144#ifdef FEAT_TAG_OLDSTATIC
6145 "tag_old_static",
6146#endif
6147#ifdef FEAT_TAG_ANYWHITE
6148 "tag_any_white",
6149#endif
6150#ifdef FEAT_TCL
6151# ifndef DYNAMIC_TCL
6152 "tcl",
6153# endif
6154#endif
6155#ifdef FEAT_TERMGUICOLORS
6156 "termguicolors",
6157#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006158#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006159 "terminal",
6160#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006161#ifdef TERMINFO
6162 "terminfo",
6163#endif
6164#ifdef FEAT_TERMRESPONSE
6165 "termresponse",
6166#endif
6167#ifdef FEAT_TEXTOBJ
6168 "textobjects",
6169#endif
6170#ifdef HAVE_TGETENT
6171 "tgetent",
6172#endif
6173#ifdef FEAT_TIMERS
6174 "timers",
6175#endif
6176#ifdef FEAT_TITLE
6177 "title",
6178#endif
6179#ifdef FEAT_TOOLBAR
6180 "toolbar",
6181#endif
6182#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6183 "unnamedplus",
6184#endif
6185#ifdef FEAT_USR_CMDS
6186 "user-commands", /* was accidentally included in 5.4 */
6187 "user_commands",
6188#endif
6189#ifdef FEAT_VIMINFO
6190 "viminfo",
6191#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006192 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006193#ifdef FEAT_VIRTUALEDIT
6194 "virtualedit",
6195#endif
6196 "visual",
6197#ifdef FEAT_VISUALEXTRA
6198 "visualextra",
6199#endif
6200#ifdef FEAT_VREPLACE
6201 "vreplace",
6202#endif
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006203#ifdef FEAT_VTP
6204 "vtp",
6205#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006206#ifdef FEAT_WILDIGN
6207 "wildignore",
6208#endif
6209#ifdef FEAT_WILDMENU
6210 "wildmenu",
6211#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006212 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006213#ifdef FEAT_WAK
6214 "winaltkeys",
6215#endif
6216#ifdef FEAT_WRITEBACKUP
6217 "writebackup",
6218#endif
6219#ifdef FEAT_XIM
6220 "xim",
6221#endif
6222#ifdef FEAT_XFONTSET
6223 "xfontset",
6224#endif
6225#ifdef FEAT_XPM_W32
6226 "xpm",
6227 "xpm_w32", /* for backward compatibility */
6228#else
6229# if defined(HAVE_XPM)
6230 "xpm",
6231# endif
6232#endif
6233#ifdef USE_XSMP
6234 "xsmp",
6235#endif
6236#ifdef USE_XSMP_INTERACT
6237 "xsmp_interact",
6238#endif
6239#ifdef FEAT_XCLIPBOARD
6240 "xterm_clipboard",
6241#endif
6242#ifdef FEAT_XTERM_SAVE
6243 "xterm_save",
6244#endif
6245#if defined(UNIX) && defined(FEAT_X11)
6246 "X11",
6247#endif
6248 NULL
6249 };
6250
6251 name = get_tv_string(&argvars[0]);
6252 for (i = 0; has_list[i] != NULL; ++i)
6253 if (STRICMP(name, has_list[i]) == 0)
6254 {
6255 n = TRUE;
6256 break;
6257 }
6258
6259 if (n == FALSE)
6260 {
6261 if (STRNICMP(name, "patch", 5) == 0)
6262 {
6263 if (name[5] == '-'
6264 && STRLEN(name) >= 11
6265 && vim_isdigit(name[6])
6266 && vim_isdigit(name[8])
6267 && vim_isdigit(name[10]))
6268 {
6269 int major = atoi((char *)name + 6);
6270 int minor = atoi((char *)name + 8);
6271
6272 /* Expect "patch-9.9.01234". */
6273 n = (major < VIM_VERSION_MAJOR
6274 || (major == VIM_VERSION_MAJOR
6275 && (minor < VIM_VERSION_MINOR
6276 || (minor == VIM_VERSION_MINOR
6277 && has_patch(atoi((char *)name + 10))))));
6278 }
6279 else
6280 n = has_patch(atoi((char *)name + 5));
6281 }
6282 else if (STRICMP(name, "vim_starting") == 0)
6283 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006284 else if (STRICMP(name, "ttyin") == 0)
6285 n = mch_input_isatty();
6286 else if (STRICMP(name, "ttyout") == 0)
6287 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006288#ifdef FEAT_MBYTE
6289 else if (STRICMP(name, "multi_byte_encoding") == 0)
6290 n = has_mbyte;
6291#endif
6292#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6293 else if (STRICMP(name, "balloon_multiline") == 0)
6294 n = multiline_balloon_available();
6295#endif
6296#ifdef DYNAMIC_TCL
6297 else if (STRICMP(name, "tcl") == 0)
6298 n = tcl_enabled(FALSE);
6299#endif
6300#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6301 else if (STRICMP(name, "iconv") == 0)
6302 n = iconv_enabled(FALSE);
6303#endif
6304#ifdef DYNAMIC_LUA
6305 else if (STRICMP(name, "lua") == 0)
6306 n = lua_enabled(FALSE);
6307#endif
6308#ifdef DYNAMIC_MZSCHEME
6309 else if (STRICMP(name, "mzscheme") == 0)
6310 n = mzscheme_enabled(FALSE);
6311#endif
6312#ifdef DYNAMIC_RUBY
6313 else if (STRICMP(name, "ruby") == 0)
6314 n = ruby_enabled(FALSE);
6315#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006316#ifdef DYNAMIC_PYTHON
6317 else if (STRICMP(name, "python") == 0)
6318 n = python_enabled(FALSE);
6319#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006320#ifdef DYNAMIC_PYTHON3
6321 else if (STRICMP(name, "python3") == 0)
6322 n = python3_enabled(FALSE);
6323#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006324#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6325 else if (STRICMP(name, "pythonx") == 0)
6326 {
6327# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6328 if (p_pyx == 0)
6329 n = python3_enabled(FALSE) || python_enabled(FALSE);
6330 else if (p_pyx == 3)
6331 n = python3_enabled(FALSE);
6332 else if (p_pyx == 2)
6333 n = python_enabled(FALSE);
6334# elif defined(DYNAMIC_PYTHON)
6335 n = python_enabled(FALSE);
6336# elif defined(DYNAMIC_PYTHON3)
6337 n = python3_enabled(FALSE);
6338# endif
6339 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006340#endif
6341#ifdef DYNAMIC_PERL
6342 else if (STRICMP(name, "perl") == 0)
6343 n = perl_enabled(FALSE);
6344#endif
6345#ifdef FEAT_GUI
6346 else if (STRICMP(name, "gui_running") == 0)
6347 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006348# ifdef FEAT_BROWSE
6349 else if (STRICMP(name, "browse") == 0)
6350 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6351# endif
6352#endif
6353#ifdef FEAT_SYN_HL
6354 else if (STRICMP(name, "syntax_items") == 0)
6355 n = syntax_present(curwin);
6356#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006357#ifdef FEAT_VTP
6358 else if (STRICMP(name, "vcon") == 0)
6359 n = has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006360#endif
6361#ifdef FEAT_NETBEANS_INTG
6362 else if (STRICMP(name, "netbeans_enabled") == 0)
6363 n = netbeans_active();
6364#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006365#if defined(FEAT_TERMINAL) && defined(WIN3264)
6366 else if (STRICMP(name, "terminal") == 0)
6367 n = terminal_enabled();
6368#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006369 }
6370
6371 rettv->vval.v_number = n;
6372}
6373
6374/*
6375 * "has_key()" function
6376 */
6377 static void
6378f_has_key(typval_T *argvars, typval_T *rettv)
6379{
6380 if (argvars[0].v_type != VAR_DICT)
6381 {
6382 EMSG(_(e_dictreq));
6383 return;
6384 }
6385 if (argvars[0].vval.v_dict == NULL)
6386 return;
6387
6388 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6389 get_tv_string(&argvars[1]), -1) != NULL;
6390}
6391
6392/*
6393 * "haslocaldir()" function
6394 */
6395 static void
6396f_haslocaldir(typval_T *argvars, typval_T *rettv)
6397{
6398 win_T *wp = NULL;
6399
6400 wp = find_tabwin(&argvars[0], &argvars[1]);
6401 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6402}
6403
6404/*
6405 * "hasmapto()" function
6406 */
6407 static void
6408f_hasmapto(typval_T *argvars, typval_T *rettv)
6409{
6410 char_u *name;
6411 char_u *mode;
6412 char_u buf[NUMBUFLEN];
6413 int abbr = FALSE;
6414
6415 name = get_tv_string(&argvars[0]);
6416 if (argvars[1].v_type == VAR_UNKNOWN)
6417 mode = (char_u *)"nvo";
6418 else
6419 {
6420 mode = get_tv_string_buf(&argvars[1], buf);
6421 if (argvars[2].v_type != VAR_UNKNOWN)
6422 abbr = (int)get_tv_number(&argvars[2]);
6423 }
6424
6425 if (map_to_exists(name, mode, abbr))
6426 rettv->vval.v_number = TRUE;
6427 else
6428 rettv->vval.v_number = FALSE;
6429}
6430
6431/*
6432 * "histadd()" function
6433 */
6434 static void
6435f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6436{
6437#ifdef FEAT_CMDHIST
6438 int histype;
6439 char_u *str;
6440 char_u buf[NUMBUFLEN];
6441#endif
6442
6443 rettv->vval.v_number = FALSE;
6444 if (check_restricted() || check_secure())
6445 return;
6446#ifdef FEAT_CMDHIST
6447 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6448 histype = str != NULL ? get_histtype(str) : -1;
6449 if (histype >= 0)
6450 {
6451 str = get_tv_string_buf(&argvars[1], buf);
6452 if (*str != NUL)
6453 {
6454 init_history();
6455 add_to_history(histype, str, FALSE, NUL);
6456 rettv->vval.v_number = TRUE;
6457 return;
6458 }
6459 }
6460#endif
6461}
6462
6463/*
6464 * "histdel()" function
6465 */
6466 static void
6467f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6468{
6469#ifdef FEAT_CMDHIST
6470 int n;
6471 char_u buf[NUMBUFLEN];
6472 char_u *str;
6473
6474 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6475 if (str == NULL)
6476 n = 0;
6477 else if (argvars[1].v_type == VAR_UNKNOWN)
6478 /* only one argument: clear entire history */
6479 n = clr_history(get_histtype(str));
6480 else if (argvars[1].v_type == VAR_NUMBER)
6481 /* index given: remove that entry */
6482 n = del_history_idx(get_histtype(str),
6483 (int)get_tv_number(&argvars[1]));
6484 else
6485 /* string given: remove all matching entries */
6486 n = del_history_entry(get_histtype(str),
6487 get_tv_string_buf(&argvars[1], buf));
6488 rettv->vval.v_number = n;
6489#endif
6490}
6491
6492/*
6493 * "histget()" function
6494 */
6495 static void
6496f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6497{
6498#ifdef FEAT_CMDHIST
6499 int type;
6500 int idx;
6501 char_u *str;
6502
6503 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6504 if (str == NULL)
6505 rettv->vval.v_string = NULL;
6506 else
6507 {
6508 type = get_histtype(str);
6509 if (argvars[1].v_type == VAR_UNKNOWN)
6510 idx = get_history_idx(type);
6511 else
6512 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6513 /* -1 on type error */
6514 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6515 }
6516#else
6517 rettv->vval.v_string = NULL;
6518#endif
6519 rettv->v_type = VAR_STRING;
6520}
6521
6522/*
6523 * "histnr()" function
6524 */
6525 static void
6526f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6527{
6528 int i;
6529
6530#ifdef FEAT_CMDHIST
6531 char_u *history = get_tv_string_chk(&argvars[0]);
6532
6533 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6534 if (i >= HIST_CMD && i < HIST_COUNT)
6535 i = get_history_idx(i);
6536 else
6537#endif
6538 i = -1;
6539 rettv->vval.v_number = i;
6540}
6541
6542/*
6543 * "highlightID(name)" function
6544 */
6545 static void
6546f_hlID(typval_T *argvars, typval_T *rettv)
6547{
6548 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6549}
6550
6551/*
6552 * "highlight_exists()" function
6553 */
6554 static void
6555f_hlexists(typval_T *argvars, typval_T *rettv)
6556{
6557 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6558}
6559
6560/*
6561 * "hostname()" function
6562 */
6563 static void
6564f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6565{
6566 char_u hostname[256];
6567
6568 mch_get_host_name(hostname, 256);
6569 rettv->v_type = VAR_STRING;
6570 rettv->vval.v_string = vim_strsave(hostname);
6571}
6572
6573/*
6574 * iconv() function
6575 */
6576 static void
6577f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6578{
6579#ifdef FEAT_MBYTE
6580 char_u buf1[NUMBUFLEN];
6581 char_u buf2[NUMBUFLEN];
6582 char_u *from, *to, *str;
6583 vimconv_T vimconv;
6584#endif
6585
6586 rettv->v_type = VAR_STRING;
6587 rettv->vval.v_string = NULL;
6588
6589#ifdef FEAT_MBYTE
6590 str = get_tv_string(&argvars[0]);
6591 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6592 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6593 vimconv.vc_type = CONV_NONE;
6594 convert_setup(&vimconv, from, to);
6595
6596 /* If the encodings are equal, no conversion needed. */
6597 if (vimconv.vc_type == CONV_NONE)
6598 rettv->vval.v_string = vim_strsave(str);
6599 else
6600 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6601
6602 convert_setup(&vimconv, NULL, NULL);
6603 vim_free(from);
6604 vim_free(to);
6605#endif
6606}
6607
6608/*
6609 * "indent()" function
6610 */
6611 static void
6612f_indent(typval_T *argvars, typval_T *rettv)
6613{
6614 linenr_T lnum;
6615
6616 lnum = get_tv_lnum(argvars);
6617 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6618 rettv->vval.v_number = get_indent_lnum(lnum);
6619 else
6620 rettv->vval.v_number = -1;
6621}
6622
6623/*
6624 * "index()" function
6625 */
6626 static void
6627f_index(typval_T *argvars, typval_T *rettv)
6628{
6629 list_T *l;
6630 listitem_T *item;
6631 long idx = 0;
6632 int ic = FALSE;
6633
6634 rettv->vval.v_number = -1;
6635 if (argvars[0].v_type != VAR_LIST)
6636 {
6637 EMSG(_(e_listreq));
6638 return;
6639 }
6640 l = argvars[0].vval.v_list;
6641 if (l != NULL)
6642 {
6643 item = l->lv_first;
6644 if (argvars[2].v_type != VAR_UNKNOWN)
6645 {
6646 int error = FALSE;
6647
6648 /* Start at specified item. Use the cached index that list_find()
6649 * sets, so that a negative number also works. */
6650 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6651 idx = l->lv_idx;
6652 if (argvars[3].v_type != VAR_UNKNOWN)
6653 ic = (int)get_tv_number_chk(&argvars[3], &error);
6654 if (error)
6655 item = NULL;
6656 }
6657
6658 for ( ; item != NULL; item = item->li_next, ++idx)
6659 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6660 {
6661 rettv->vval.v_number = idx;
6662 break;
6663 }
6664 }
6665}
6666
6667static int inputsecret_flag = 0;
6668
6669/*
6670 * "input()" function
6671 * Also handles inputsecret() when inputsecret is set.
6672 */
6673 static void
6674f_input(typval_T *argvars, typval_T *rettv)
6675{
6676 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6677}
6678
6679/*
6680 * "inputdialog()" function
6681 */
6682 static void
6683f_inputdialog(typval_T *argvars, typval_T *rettv)
6684{
6685#if defined(FEAT_GUI_TEXTDIALOG)
6686 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6687 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6688 {
6689 char_u *message;
6690 char_u buf[NUMBUFLEN];
6691 char_u *defstr = (char_u *)"";
6692
6693 message = get_tv_string_chk(&argvars[0]);
6694 if (argvars[1].v_type != VAR_UNKNOWN
6695 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6696 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6697 else
6698 IObuff[0] = NUL;
6699 if (message != NULL && defstr != NULL
6700 && do_dialog(VIM_QUESTION, NULL, message,
6701 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6702 rettv->vval.v_string = vim_strsave(IObuff);
6703 else
6704 {
6705 if (message != NULL && defstr != NULL
6706 && argvars[1].v_type != VAR_UNKNOWN
6707 && argvars[2].v_type != VAR_UNKNOWN)
6708 rettv->vval.v_string = vim_strsave(
6709 get_tv_string_buf(&argvars[2], buf));
6710 else
6711 rettv->vval.v_string = NULL;
6712 }
6713 rettv->v_type = VAR_STRING;
6714 }
6715 else
6716#endif
6717 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6718}
6719
6720/*
6721 * "inputlist()" function
6722 */
6723 static void
6724f_inputlist(typval_T *argvars, typval_T *rettv)
6725{
6726 listitem_T *li;
6727 int selected;
6728 int mouse_used;
6729
6730#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006731 /* While starting up, there is no place to enter text. When running tests
6732 * with --not-a-term we assume feedkeys() will be used. */
6733 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006734 return;
6735#endif
6736 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6737 {
6738 EMSG2(_(e_listarg), "inputlist()");
6739 return;
6740 }
6741
6742 msg_start();
6743 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6744 lines_left = Rows; /* avoid more prompt */
6745 msg_scroll = TRUE;
6746 msg_clr_eos();
6747
6748 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6749 {
6750 msg_puts(get_tv_string(&li->li_tv));
6751 msg_putchar('\n');
6752 }
6753
6754 /* Ask for choice. */
6755 selected = prompt_for_number(&mouse_used);
6756 if (mouse_used)
6757 selected -= lines_left;
6758
6759 rettv->vval.v_number = selected;
6760}
6761
6762
6763static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6764
6765/*
6766 * "inputrestore()" function
6767 */
6768 static void
6769f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6770{
6771 if (ga_userinput.ga_len > 0)
6772 {
6773 --ga_userinput.ga_len;
6774 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6775 + ga_userinput.ga_len);
6776 /* default return is zero == OK */
6777 }
6778 else if (p_verbose > 1)
6779 {
6780 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6781 rettv->vval.v_number = 1; /* Failed */
6782 }
6783}
6784
6785/*
6786 * "inputsave()" function
6787 */
6788 static void
6789f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6790{
6791 /* Add an entry to the stack of typeahead storage. */
6792 if (ga_grow(&ga_userinput, 1) == OK)
6793 {
6794 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6795 + ga_userinput.ga_len);
6796 ++ga_userinput.ga_len;
6797 /* default return is zero == OK */
6798 }
6799 else
6800 rettv->vval.v_number = 1; /* Failed */
6801}
6802
6803/*
6804 * "inputsecret()" function
6805 */
6806 static void
6807f_inputsecret(typval_T *argvars, typval_T *rettv)
6808{
6809 ++cmdline_star;
6810 ++inputsecret_flag;
6811 f_input(argvars, rettv);
6812 --cmdline_star;
6813 --inputsecret_flag;
6814}
6815
6816/*
6817 * "insert()" function
6818 */
6819 static void
6820f_insert(typval_T *argvars, typval_T *rettv)
6821{
6822 long before = 0;
6823 listitem_T *item;
6824 list_T *l;
6825 int error = FALSE;
6826
6827 if (argvars[0].v_type != VAR_LIST)
6828 EMSG2(_(e_listarg), "insert()");
6829 else if ((l = argvars[0].vval.v_list) != NULL
6830 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6831 {
6832 if (argvars[2].v_type != VAR_UNKNOWN)
6833 before = (long)get_tv_number_chk(&argvars[2], &error);
6834 if (error)
6835 return; /* type error; errmsg already given */
6836
6837 if (before == l->lv_len)
6838 item = NULL;
6839 else
6840 {
6841 item = list_find(l, before);
6842 if (item == NULL)
6843 {
6844 EMSGN(_(e_listidx), before);
6845 l = NULL;
6846 }
6847 }
6848 if (l != NULL)
6849 {
6850 list_insert_tv(l, &argvars[1], item);
6851 copy_tv(&argvars[0], rettv);
6852 }
6853 }
6854}
6855
6856/*
6857 * "invert(expr)" function
6858 */
6859 static void
6860f_invert(typval_T *argvars, typval_T *rettv)
6861{
6862 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6863}
6864
6865/*
6866 * "isdirectory()" function
6867 */
6868 static void
6869f_isdirectory(typval_T *argvars, typval_T *rettv)
6870{
6871 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6872}
6873
6874/*
6875 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6876 * or it refers to a List or Dictionary that is locked.
6877 */
6878 static int
6879tv_islocked(typval_T *tv)
6880{
6881 return (tv->v_lock & VAR_LOCKED)
6882 || (tv->v_type == VAR_LIST
6883 && tv->vval.v_list != NULL
6884 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6885 || (tv->v_type == VAR_DICT
6886 && tv->vval.v_dict != NULL
6887 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6888}
6889
6890/*
6891 * "islocked()" function
6892 */
6893 static void
6894f_islocked(typval_T *argvars, typval_T *rettv)
6895{
6896 lval_T lv;
6897 char_u *end;
6898 dictitem_T *di;
6899
6900 rettv->vval.v_number = -1;
6901 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006902 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006903 if (end != NULL && lv.ll_name != NULL)
6904 {
6905 if (*end != NUL)
6906 EMSG(_(e_trailing));
6907 else
6908 {
6909 if (lv.ll_tv == NULL)
6910 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006911 di = find_var(lv.ll_name, NULL, TRUE);
6912 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006913 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006914 /* Consider a variable locked when:
6915 * 1. the variable itself is locked
6916 * 2. the value of the variable is locked.
6917 * 3. the List or Dict value is locked.
6918 */
6919 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6920 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006921 }
6922 }
6923 else if (lv.ll_range)
6924 EMSG(_("E786: Range not allowed"));
6925 else if (lv.ll_newkey != NULL)
6926 EMSG2(_(e_dictkey), lv.ll_newkey);
6927 else if (lv.ll_list != NULL)
6928 /* List item. */
6929 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6930 else
6931 /* Dictionary item. */
6932 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6933 }
6934 }
6935
6936 clear_lval(&lv);
6937}
6938
6939#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6940/*
6941 * "isnan()" function
6942 */
6943 static void
6944f_isnan(typval_T *argvars, typval_T *rettv)
6945{
6946 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6947 && isnan(argvars[0].vval.v_float);
6948}
6949#endif
6950
6951/*
6952 * "items(dict)" function
6953 */
6954 static void
6955f_items(typval_T *argvars, typval_T *rettv)
6956{
6957 dict_list(argvars, rettv, 2);
6958}
6959
6960#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6961/*
6962 * Get the job from the argument.
6963 * Returns NULL if the job is invalid.
6964 */
6965 static job_T *
6966get_job_arg(typval_T *tv)
6967{
6968 job_T *job;
6969
6970 if (tv->v_type != VAR_JOB)
6971 {
6972 EMSG2(_(e_invarg2), get_tv_string(tv));
6973 return NULL;
6974 }
6975 job = tv->vval.v_job;
6976
6977 if (job == NULL)
6978 EMSG(_("E916: not a valid job"));
6979 return job;
6980}
6981
6982/*
6983 * "job_getchannel()" function
6984 */
6985 static void
6986f_job_getchannel(typval_T *argvars, typval_T *rettv)
6987{
6988 job_T *job = get_job_arg(&argvars[0]);
6989
6990 if (job != NULL)
6991 {
6992 rettv->v_type = VAR_CHANNEL;
6993 rettv->vval.v_channel = job->jv_channel;
6994 if (job->jv_channel != NULL)
6995 ++job->jv_channel->ch_refcount;
6996 }
6997}
6998
6999/*
7000 * "job_info()" function
7001 */
7002 static void
7003f_job_info(typval_T *argvars, typval_T *rettv)
7004{
7005 job_T *job = get_job_arg(&argvars[0]);
7006
7007 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7008 job_info(job, rettv->vval.v_dict);
7009}
7010
7011/*
7012 * "job_setoptions()" function
7013 */
7014 static void
7015f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7016{
7017 job_T *job = get_job_arg(&argvars[0]);
7018 jobopt_T opt;
7019
7020 if (job == NULL)
7021 return;
7022 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007023 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007024 job_set_options(job, &opt);
7025 free_job_options(&opt);
7026}
7027
7028/*
7029 * "job_start()" function
7030 */
7031 static void
7032f_job_start(typval_T *argvars, typval_T *rettv)
7033{
7034 rettv->v_type = VAR_JOB;
7035 if (check_restricted() || check_secure())
7036 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01007037 rettv->vval.v_job = job_start(argvars, NULL, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007038}
7039
7040/*
7041 * "job_status()" function
7042 */
7043 static void
7044f_job_status(typval_T *argvars, typval_T *rettv)
7045{
7046 job_T *job = get_job_arg(&argvars[0]);
7047
7048 if (job != NULL)
7049 {
7050 rettv->v_type = VAR_STRING;
7051 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7052 }
7053}
7054
7055/*
7056 * "job_stop()" function
7057 */
7058 static void
7059f_job_stop(typval_T *argvars, typval_T *rettv)
7060{
7061 job_T *job = get_job_arg(&argvars[0]);
7062
7063 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007064 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007065}
7066#endif
7067
7068/*
7069 * "join()" function
7070 */
7071 static void
7072f_join(typval_T *argvars, typval_T *rettv)
7073{
7074 garray_T ga;
7075 char_u *sep;
7076
7077 if (argvars[0].v_type != VAR_LIST)
7078 {
7079 EMSG(_(e_listreq));
7080 return;
7081 }
7082 if (argvars[0].vval.v_list == NULL)
7083 return;
7084 if (argvars[1].v_type == VAR_UNKNOWN)
7085 sep = (char_u *)" ";
7086 else
7087 sep = get_tv_string_chk(&argvars[1]);
7088
7089 rettv->v_type = VAR_STRING;
7090
7091 if (sep != NULL)
7092 {
7093 ga_init2(&ga, (int)sizeof(char), 80);
7094 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7095 ga_append(&ga, NUL);
7096 rettv->vval.v_string = (char_u *)ga.ga_data;
7097 }
7098 else
7099 rettv->vval.v_string = NULL;
7100}
7101
7102/*
7103 * "js_decode()" function
7104 */
7105 static void
7106f_js_decode(typval_T *argvars, typval_T *rettv)
7107{
7108 js_read_T reader;
7109
7110 reader.js_buf = get_tv_string(&argvars[0]);
7111 reader.js_fill = NULL;
7112 reader.js_used = 0;
7113 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7114 EMSG(_(e_invarg));
7115}
7116
7117/*
7118 * "js_encode()" function
7119 */
7120 static void
7121f_js_encode(typval_T *argvars, typval_T *rettv)
7122{
7123 rettv->v_type = VAR_STRING;
7124 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7125}
7126
7127/*
7128 * "json_decode()" function
7129 */
7130 static void
7131f_json_decode(typval_T *argvars, typval_T *rettv)
7132{
7133 js_read_T reader;
7134
7135 reader.js_buf = get_tv_string(&argvars[0]);
7136 reader.js_fill = NULL;
7137 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007138 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007139}
7140
7141/*
7142 * "json_encode()" function
7143 */
7144 static void
7145f_json_encode(typval_T *argvars, typval_T *rettv)
7146{
7147 rettv->v_type = VAR_STRING;
7148 rettv->vval.v_string = json_encode(&argvars[0], 0);
7149}
7150
7151/*
7152 * "keys()" function
7153 */
7154 static void
7155f_keys(typval_T *argvars, typval_T *rettv)
7156{
7157 dict_list(argvars, rettv, 0);
7158}
7159
7160/*
7161 * "last_buffer_nr()" function.
7162 */
7163 static void
7164f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7165{
7166 int n = 0;
7167 buf_T *buf;
7168
Bram Moolenaar29323592016-07-24 22:04:11 +02007169 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007170 if (n < buf->b_fnum)
7171 n = buf->b_fnum;
7172
7173 rettv->vval.v_number = n;
7174}
7175
7176/*
7177 * "len()" function
7178 */
7179 static void
7180f_len(typval_T *argvars, typval_T *rettv)
7181{
7182 switch (argvars[0].v_type)
7183 {
7184 case VAR_STRING:
7185 case VAR_NUMBER:
7186 rettv->vval.v_number = (varnumber_T)STRLEN(
7187 get_tv_string(&argvars[0]));
7188 break;
7189 case VAR_LIST:
7190 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7191 break;
7192 case VAR_DICT:
7193 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7194 break;
7195 case VAR_UNKNOWN:
7196 case VAR_SPECIAL:
7197 case VAR_FLOAT:
7198 case VAR_FUNC:
7199 case VAR_PARTIAL:
7200 case VAR_JOB:
7201 case VAR_CHANNEL:
7202 EMSG(_("E701: Invalid type for len()"));
7203 break;
7204 }
7205}
7206
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007207 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007208libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007209{
7210#ifdef FEAT_LIBCALL
7211 char_u *string_in;
7212 char_u **string_result;
7213 int nr_result;
7214#endif
7215
7216 rettv->v_type = type;
7217 if (type != VAR_NUMBER)
7218 rettv->vval.v_string = NULL;
7219
7220 if (check_restricted() || check_secure())
7221 return;
7222
7223#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007224 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007225 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7226 {
7227 string_in = NULL;
7228 if (argvars[2].v_type == VAR_STRING)
7229 string_in = argvars[2].vval.v_string;
7230 if (type == VAR_NUMBER)
7231 string_result = NULL;
7232 else
7233 string_result = &rettv->vval.v_string;
7234 if (mch_libcall(argvars[0].vval.v_string,
7235 argvars[1].vval.v_string,
7236 string_in,
7237 argvars[2].vval.v_number,
7238 string_result,
7239 &nr_result) == OK
7240 && type == VAR_NUMBER)
7241 rettv->vval.v_number = nr_result;
7242 }
7243#endif
7244}
7245
7246/*
7247 * "libcall()" function
7248 */
7249 static void
7250f_libcall(typval_T *argvars, typval_T *rettv)
7251{
7252 libcall_common(argvars, rettv, VAR_STRING);
7253}
7254
7255/*
7256 * "libcallnr()" function
7257 */
7258 static void
7259f_libcallnr(typval_T *argvars, typval_T *rettv)
7260{
7261 libcall_common(argvars, rettv, VAR_NUMBER);
7262}
7263
7264/*
7265 * "line(string)" function
7266 */
7267 static void
7268f_line(typval_T *argvars, typval_T *rettv)
7269{
7270 linenr_T lnum = 0;
7271 pos_T *fp;
7272 int fnum;
7273
7274 fp = var2fpos(&argvars[0], TRUE, &fnum);
7275 if (fp != NULL)
7276 lnum = fp->lnum;
7277 rettv->vval.v_number = lnum;
7278}
7279
7280/*
7281 * "line2byte(lnum)" function
7282 */
7283 static void
7284f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7285{
7286#ifndef FEAT_BYTEOFF
7287 rettv->vval.v_number = -1;
7288#else
7289 linenr_T lnum;
7290
7291 lnum = get_tv_lnum(argvars);
7292 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7293 rettv->vval.v_number = -1;
7294 else
7295 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7296 if (rettv->vval.v_number >= 0)
7297 ++rettv->vval.v_number;
7298#endif
7299}
7300
7301/*
7302 * "lispindent(lnum)" function
7303 */
7304 static void
7305f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7306{
7307#ifdef FEAT_LISP
7308 pos_T pos;
7309 linenr_T lnum;
7310
7311 pos = curwin->w_cursor;
7312 lnum = get_tv_lnum(argvars);
7313 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7314 {
7315 curwin->w_cursor.lnum = lnum;
7316 rettv->vval.v_number = get_lisp_indent();
7317 curwin->w_cursor = pos;
7318 }
7319 else
7320#endif
7321 rettv->vval.v_number = -1;
7322}
7323
7324/*
7325 * "localtime()" function
7326 */
7327 static void
7328f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7329{
7330 rettv->vval.v_number = (varnumber_T)time(NULL);
7331}
7332
7333static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7334
7335 static void
7336get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7337{
7338 char_u *keys;
7339 char_u *which;
7340 char_u buf[NUMBUFLEN];
7341 char_u *keys_buf = NULL;
7342 char_u *rhs;
7343 int mode;
7344 int abbr = FALSE;
7345 int get_dict = FALSE;
7346 mapblock_T *mp;
7347 int buffer_local;
7348
7349 /* return empty string for failure */
7350 rettv->v_type = VAR_STRING;
7351 rettv->vval.v_string = NULL;
7352
7353 keys = get_tv_string(&argvars[0]);
7354 if (*keys == NUL)
7355 return;
7356
7357 if (argvars[1].v_type != VAR_UNKNOWN)
7358 {
7359 which = get_tv_string_buf_chk(&argvars[1], buf);
7360 if (argvars[2].v_type != VAR_UNKNOWN)
7361 {
7362 abbr = (int)get_tv_number(&argvars[2]);
7363 if (argvars[3].v_type != VAR_UNKNOWN)
7364 get_dict = (int)get_tv_number(&argvars[3]);
7365 }
7366 }
7367 else
7368 which = (char_u *)"";
7369 if (which == NULL)
7370 return;
7371
7372 mode = get_map_mode(&which, 0);
7373
7374 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7375 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7376 vim_free(keys_buf);
7377
7378 if (!get_dict)
7379 {
7380 /* Return a string. */
7381 if (rhs != NULL)
7382 rettv->vval.v_string = str2special_save(rhs, FALSE);
7383
7384 }
7385 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7386 {
7387 /* Return a dictionary. */
7388 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7389 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7390 dict_T *dict = rettv->vval.v_dict;
7391
7392 dict_add_nr_str(dict, "lhs", 0L, lhs);
7393 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7394 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7395 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7396 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7397 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7398 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7399 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7400 dict_add_nr_str(dict, "mode", 0L, mapmode);
7401
7402 vim_free(lhs);
7403 vim_free(mapmode);
7404 }
7405}
7406
7407#ifdef FEAT_FLOAT
7408/*
7409 * "log()" function
7410 */
7411 static void
7412f_log(typval_T *argvars, typval_T *rettv)
7413{
7414 float_T f = 0.0;
7415
7416 rettv->v_type = VAR_FLOAT;
7417 if (get_float_arg(argvars, &f) == OK)
7418 rettv->vval.v_float = log(f);
7419 else
7420 rettv->vval.v_float = 0.0;
7421}
7422
7423/*
7424 * "log10()" function
7425 */
7426 static void
7427f_log10(typval_T *argvars, typval_T *rettv)
7428{
7429 float_T f = 0.0;
7430
7431 rettv->v_type = VAR_FLOAT;
7432 if (get_float_arg(argvars, &f) == OK)
7433 rettv->vval.v_float = log10(f);
7434 else
7435 rettv->vval.v_float = 0.0;
7436}
7437#endif
7438
7439#ifdef FEAT_LUA
7440/*
7441 * "luaeval()" function
7442 */
7443 static void
7444f_luaeval(typval_T *argvars, typval_T *rettv)
7445{
7446 char_u *str;
7447 char_u buf[NUMBUFLEN];
7448
7449 str = get_tv_string_buf(&argvars[0], buf);
7450 do_luaeval(str, argvars + 1, rettv);
7451}
7452#endif
7453
7454/*
7455 * "map()" function
7456 */
7457 static void
7458f_map(typval_T *argvars, typval_T *rettv)
7459{
7460 filter_map(argvars, rettv, TRUE);
7461}
7462
7463/*
7464 * "maparg()" function
7465 */
7466 static void
7467f_maparg(typval_T *argvars, typval_T *rettv)
7468{
7469 get_maparg(argvars, rettv, TRUE);
7470}
7471
7472/*
7473 * "mapcheck()" function
7474 */
7475 static void
7476f_mapcheck(typval_T *argvars, typval_T *rettv)
7477{
7478 get_maparg(argvars, rettv, FALSE);
7479}
7480
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007481typedef enum
7482{
7483 MATCH_END, /* matchend() */
7484 MATCH_MATCH, /* match() */
7485 MATCH_STR, /* matchstr() */
7486 MATCH_LIST, /* matchlist() */
7487 MATCH_POS /* matchstrpos() */
7488} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007489
7490 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007491find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007492{
7493 char_u *str = NULL;
7494 long len = 0;
7495 char_u *expr = NULL;
7496 char_u *pat;
7497 regmatch_T regmatch;
7498 char_u patbuf[NUMBUFLEN];
7499 char_u strbuf[NUMBUFLEN];
7500 char_u *save_cpo;
7501 long start = 0;
7502 long nth = 1;
7503 colnr_T startcol = 0;
7504 int match = 0;
7505 list_T *l = NULL;
7506 listitem_T *li = NULL;
7507 long idx = 0;
7508 char_u *tofree = NULL;
7509
7510 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7511 save_cpo = p_cpo;
7512 p_cpo = (char_u *)"";
7513
7514 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007515 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007516 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007517 /* type MATCH_LIST: return empty list when there are no matches.
7518 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007519 if (rettv_list_alloc(rettv) == FAIL)
7520 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007521 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007522 && (list_append_string(rettv->vval.v_list,
7523 (char_u *)"", 0) == FAIL
7524 || list_append_number(rettv->vval.v_list,
7525 (varnumber_T)-1) == FAIL
7526 || list_append_number(rettv->vval.v_list,
7527 (varnumber_T)-1) == FAIL
7528 || list_append_number(rettv->vval.v_list,
7529 (varnumber_T)-1) == FAIL))
7530 {
7531 list_free(rettv->vval.v_list);
7532 rettv->vval.v_list = NULL;
7533 goto theend;
7534 }
7535 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007536 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007537 {
7538 rettv->v_type = VAR_STRING;
7539 rettv->vval.v_string = NULL;
7540 }
7541
7542 if (argvars[0].v_type == VAR_LIST)
7543 {
7544 if ((l = argvars[0].vval.v_list) == NULL)
7545 goto theend;
7546 li = l->lv_first;
7547 }
7548 else
7549 {
7550 expr = str = get_tv_string(&argvars[0]);
7551 len = (long)STRLEN(str);
7552 }
7553
7554 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7555 if (pat == NULL)
7556 goto theend;
7557
7558 if (argvars[2].v_type != VAR_UNKNOWN)
7559 {
7560 int error = FALSE;
7561
7562 start = (long)get_tv_number_chk(&argvars[2], &error);
7563 if (error)
7564 goto theend;
7565 if (l != NULL)
7566 {
7567 li = list_find(l, start);
7568 if (li == NULL)
7569 goto theend;
7570 idx = l->lv_idx; /* use the cached index */
7571 }
7572 else
7573 {
7574 if (start < 0)
7575 start = 0;
7576 if (start > len)
7577 goto theend;
7578 /* When "count" argument is there ignore matches before "start",
7579 * otherwise skip part of the string. Differs when pattern is "^"
7580 * or "\<". */
7581 if (argvars[3].v_type != VAR_UNKNOWN)
7582 startcol = start;
7583 else
7584 {
7585 str += start;
7586 len -= start;
7587 }
7588 }
7589
7590 if (argvars[3].v_type != VAR_UNKNOWN)
7591 nth = (long)get_tv_number_chk(&argvars[3], &error);
7592 if (error)
7593 goto theend;
7594 }
7595
7596 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7597 if (regmatch.regprog != NULL)
7598 {
7599 regmatch.rm_ic = p_ic;
7600
7601 for (;;)
7602 {
7603 if (l != NULL)
7604 {
7605 if (li == NULL)
7606 {
7607 match = FALSE;
7608 break;
7609 }
7610 vim_free(tofree);
7611 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7612 if (str == NULL)
7613 break;
7614 }
7615
7616 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7617
7618 if (match && --nth <= 0)
7619 break;
7620 if (l == NULL && !match)
7621 break;
7622
7623 /* Advance to just after the match. */
7624 if (l != NULL)
7625 {
7626 li = li->li_next;
7627 ++idx;
7628 }
7629 else
7630 {
7631#ifdef FEAT_MBYTE
7632 startcol = (colnr_T)(regmatch.startp[0]
7633 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7634#else
7635 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7636#endif
7637 if (startcol > (colnr_T)len
7638 || str + startcol <= regmatch.startp[0])
7639 {
7640 match = FALSE;
7641 break;
7642 }
7643 }
7644 }
7645
7646 if (match)
7647 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007648 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007649 {
7650 listitem_T *li1 = rettv->vval.v_list->lv_first;
7651 listitem_T *li2 = li1->li_next;
7652 listitem_T *li3 = li2->li_next;
7653 listitem_T *li4 = li3->li_next;
7654
7655 vim_free(li1->li_tv.vval.v_string);
7656 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7657 (int)(regmatch.endp[0] - regmatch.startp[0]));
7658 li3->li_tv.vval.v_number =
7659 (varnumber_T)(regmatch.startp[0] - expr);
7660 li4->li_tv.vval.v_number =
7661 (varnumber_T)(regmatch.endp[0] - expr);
7662 if (l != NULL)
7663 li2->li_tv.vval.v_number = (varnumber_T)idx;
7664 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007665 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007666 {
7667 int i;
7668
7669 /* return list with matched string and submatches */
7670 for (i = 0; i < NSUBEXP; ++i)
7671 {
7672 if (regmatch.endp[i] == NULL)
7673 {
7674 if (list_append_string(rettv->vval.v_list,
7675 (char_u *)"", 0) == FAIL)
7676 break;
7677 }
7678 else if (list_append_string(rettv->vval.v_list,
7679 regmatch.startp[i],
7680 (int)(regmatch.endp[i] - regmatch.startp[i]))
7681 == FAIL)
7682 break;
7683 }
7684 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007685 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007686 {
7687 /* return matched string */
7688 if (l != NULL)
7689 copy_tv(&li->li_tv, rettv);
7690 else
7691 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7692 (int)(regmatch.endp[0] - regmatch.startp[0]));
7693 }
7694 else if (l != NULL)
7695 rettv->vval.v_number = idx;
7696 else
7697 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007698 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007699 rettv->vval.v_number =
7700 (varnumber_T)(regmatch.startp[0] - str);
7701 else
7702 rettv->vval.v_number =
7703 (varnumber_T)(regmatch.endp[0] - str);
7704 rettv->vval.v_number += (varnumber_T)(str - expr);
7705 }
7706 }
7707 vim_regfree(regmatch.regprog);
7708 }
7709
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007710theend:
7711 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007712 /* matchstrpos() without a list: drop the second item. */
7713 listitem_remove(rettv->vval.v_list,
7714 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007715 vim_free(tofree);
7716 p_cpo = save_cpo;
7717}
7718
7719/*
7720 * "match()" function
7721 */
7722 static void
7723f_match(typval_T *argvars, typval_T *rettv)
7724{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007725 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007726}
7727
7728/*
7729 * "matchadd()" function
7730 */
7731 static void
7732f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7733{
7734#ifdef FEAT_SEARCH_EXTRA
7735 char_u buf[NUMBUFLEN];
7736 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7737 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7738 int prio = 10; /* default priority */
7739 int id = -1;
7740 int error = FALSE;
7741 char_u *conceal_char = NULL;
7742
7743 rettv->vval.v_number = -1;
7744
7745 if (grp == NULL || pat == NULL)
7746 return;
7747 if (argvars[2].v_type != VAR_UNKNOWN)
7748 {
7749 prio = (int)get_tv_number_chk(&argvars[2], &error);
7750 if (argvars[3].v_type != VAR_UNKNOWN)
7751 {
7752 id = (int)get_tv_number_chk(&argvars[3], &error);
7753 if (argvars[4].v_type != VAR_UNKNOWN)
7754 {
7755 if (argvars[4].v_type != VAR_DICT)
7756 {
7757 EMSG(_(e_dictreq));
7758 return;
7759 }
7760 if (dict_find(argvars[4].vval.v_dict,
7761 (char_u *)"conceal", -1) != NULL)
7762 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7763 (char_u *)"conceal", FALSE);
7764 }
7765 }
7766 }
7767 if (error == TRUE)
7768 return;
7769 if (id >= 1 && id <= 3)
7770 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007771 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007772 return;
7773 }
7774
7775 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7776 conceal_char);
7777#endif
7778}
7779
7780/*
7781 * "matchaddpos()" function
7782 */
7783 static void
7784f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7785{
7786#ifdef FEAT_SEARCH_EXTRA
7787 char_u buf[NUMBUFLEN];
7788 char_u *group;
7789 int prio = 10;
7790 int id = -1;
7791 int error = FALSE;
7792 list_T *l;
7793 char_u *conceal_char = NULL;
7794
7795 rettv->vval.v_number = -1;
7796
7797 group = get_tv_string_buf_chk(&argvars[0], buf);
7798 if (group == NULL)
7799 return;
7800
7801 if (argvars[1].v_type != VAR_LIST)
7802 {
7803 EMSG2(_(e_listarg), "matchaddpos()");
7804 return;
7805 }
7806 l = argvars[1].vval.v_list;
7807 if (l == NULL)
7808 return;
7809
7810 if (argvars[2].v_type != VAR_UNKNOWN)
7811 {
7812 prio = (int)get_tv_number_chk(&argvars[2], &error);
7813 if (argvars[3].v_type != VAR_UNKNOWN)
7814 {
7815 id = (int)get_tv_number_chk(&argvars[3], &error);
7816 if (argvars[4].v_type != VAR_UNKNOWN)
7817 {
7818 if (argvars[4].v_type != VAR_DICT)
7819 {
7820 EMSG(_(e_dictreq));
7821 return;
7822 }
7823 if (dict_find(argvars[4].vval.v_dict,
7824 (char_u *)"conceal", -1) != NULL)
7825 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7826 (char_u *)"conceal", FALSE);
7827 }
7828 }
7829 }
7830 if (error == TRUE)
7831 return;
7832
7833 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7834 if (id == 1 || id == 2)
7835 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007836 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007837 return;
7838 }
7839
7840 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7841 conceal_char);
7842#endif
7843}
7844
7845/*
7846 * "matcharg()" function
7847 */
7848 static void
7849f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7850{
7851 if (rettv_list_alloc(rettv) == OK)
7852 {
7853#ifdef FEAT_SEARCH_EXTRA
7854 int id = (int)get_tv_number(&argvars[0]);
7855 matchitem_T *m;
7856
7857 if (id >= 1 && id <= 3)
7858 {
7859 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7860 {
7861 list_append_string(rettv->vval.v_list,
7862 syn_id2name(m->hlg_id), -1);
7863 list_append_string(rettv->vval.v_list, m->pattern, -1);
7864 }
7865 else
7866 {
7867 list_append_string(rettv->vval.v_list, NULL, -1);
7868 list_append_string(rettv->vval.v_list, NULL, -1);
7869 }
7870 }
7871#endif
7872 }
7873}
7874
7875/*
7876 * "matchdelete()" function
7877 */
7878 static void
7879f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7880{
7881#ifdef FEAT_SEARCH_EXTRA
7882 rettv->vval.v_number = match_delete(curwin,
7883 (int)get_tv_number(&argvars[0]), TRUE);
7884#endif
7885}
7886
7887/*
7888 * "matchend()" function
7889 */
7890 static void
7891f_matchend(typval_T *argvars, typval_T *rettv)
7892{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007893 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007894}
7895
7896/*
7897 * "matchlist()" function
7898 */
7899 static void
7900f_matchlist(typval_T *argvars, typval_T *rettv)
7901{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007902 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007903}
7904
7905/*
7906 * "matchstr()" function
7907 */
7908 static void
7909f_matchstr(typval_T *argvars, typval_T *rettv)
7910{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007911 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007912}
7913
7914/*
7915 * "matchstrpos()" function
7916 */
7917 static void
7918f_matchstrpos(typval_T *argvars, typval_T *rettv)
7919{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007920 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007921}
7922
7923static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7924
7925 static void
7926max_min(typval_T *argvars, typval_T *rettv, int domax)
7927{
7928 varnumber_T n = 0;
7929 varnumber_T i;
7930 int error = FALSE;
7931
7932 if (argvars[0].v_type == VAR_LIST)
7933 {
7934 list_T *l;
7935 listitem_T *li;
7936
7937 l = argvars[0].vval.v_list;
7938 if (l != NULL)
7939 {
7940 li = l->lv_first;
7941 if (li != NULL)
7942 {
7943 n = get_tv_number_chk(&li->li_tv, &error);
7944 for (;;)
7945 {
7946 li = li->li_next;
7947 if (li == NULL)
7948 break;
7949 i = get_tv_number_chk(&li->li_tv, &error);
7950 if (domax ? i > n : i < n)
7951 n = i;
7952 }
7953 }
7954 }
7955 }
7956 else if (argvars[0].v_type == VAR_DICT)
7957 {
7958 dict_T *d;
7959 int first = TRUE;
7960 hashitem_T *hi;
7961 int todo;
7962
7963 d = argvars[0].vval.v_dict;
7964 if (d != NULL)
7965 {
7966 todo = (int)d->dv_hashtab.ht_used;
7967 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7968 {
7969 if (!HASHITEM_EMPTY(hi))
7970 {
7971 --todo;
7972 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7973 if (first)
7974 {
7975 n = i;
7976 first = FALSE;
7977 }
7978 else if (domax ? i > n : i < n)
7979 n = i;
7980 }
7981 }
7982 }
7983 }
7984 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007985 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007986 rettv->vval.v_number = error ? 0 : n;
7987}
7988
7989/*
7990 * "max()" function
7991 */
7992 static void
7993f_max(typval_T *argvars, typval_T *rettv)
7994{
7995 max_min(argvars, rettv, TRUE);
7996}
7997
7998/*
7999 * "min()" function
8000 */
8001 static void
8002f_min(typval_T *argvars, typval_T *rettv)
8003{
8004 max_min(argvars, rettv, FALSE);
8005}
8006
8007static int mkdir_recurse(char_u *dir, int prot);
8008
8009/*
8010 * Create the directory in which "dir" is located, and higher levels when
8011 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008012 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008013 */
8014 static int
8015mkdir_recurse(char_u *dir, int prot)
8016{
8017 char_u *p;
8018 char_u *updir;
8019 int r = FAIL;
8020
8021 /* Get end of directory name in "dir".
8022 * We're done when it's "/" or "c:/". */
8023 p = gettail_sep(dir);
8024 if (p <= get_past_head(dir))
8025 return OK;
8026
8027 /* If the directory exists we're done. Otherwise: create it.*/
8028 updir = vim_strnsave(dir, (int)(p - dir));
8029 if (updir == NULL)
8030 return FAIL;
8031 if (mch_isdir(updir))
8032 r = OK;
8033 else if (mkdir_recurse(updir, prot) == OK)
8034 r = vim_mkdir_emsg(updir, prot);
8035 vim_free(updir);
8036 return r;
8037}
8038
8039#ifdef vim_mkdir
8040/*
8041 * "mkdir()" function
8042 */
8043 static void
8044f_mkdir(typval_T *argvars, typval_T *rettv)
8045{
8046 char_u *dir;
8047 char_u buf[NUMBUFLEN];
8048 int prot = 0755;
8049
8050 rettv->vval.v_number = FAIL;
8051 if (check_restricted() || check_secure())
8052 return;
8053
8054 dir = get_tv_string_buf(&argvars[0], buf);
8055 if (*dir == NUL)
8056 rettv->vval.v_number = FAIL;
8057 else
8058 {
8059 if (*gettail(dir) == NUL)
8060 /* remove trailing slashes */
8061 *gettail_sep(dir) = NUL;
8062
8063 if (argvars[1].v_type != VAR_UNKNOWN)
8064 {
8065 if (argvars[2].v_type != VAR_UNKNOWN)
8066 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8067 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8068 mkdir_recurse(dir, prot);
8069 }
8070 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
8071 }
8072}
8073#endif
8074
8075/*
8076 * "mode()" function
8077 */
8078 static void
8079f_mode(typval_T *argvars, typval_T *rettv)
8080{
8081 char_u buf[3];
8082
8083 buf[1] = NUL;
8084 buf[2] = NUL;
8085
8086 if (time_for_testing == 93784)
8087 {
8088 /* Testing the two-character code. */
8089 buf[0] = 'x';
8090 buf[1] = '!';
8091 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008092#ifdef FEAT_TERMINAL
8093 else if (term_use_loop())
8094 buf[0] = 't';
8095#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008096 else if (VIsual_active)
8097 {
8098 if (VIsual_select)
8099 buf[0] = VIsual_mode + 's' - 'v';
8100 else
8101 buf[0] = VIsual_mode;
8102 }
8103 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8104 || State == CONFIRM)
8105 {
8106 buf[0] = 'r';
8107 if (State == ASKMORE)
8108 buf[1] = 'm';
8109 else if (State == CONFIRM)
8110 buf[1] = '?';
8111 }
8112 else if (State == EXTERNCMD)
8113 buf[0] = '!';
8114 else if (State & INSERT)
8115 {
8116#ifdef FEAT_VREPLACE
8117 if (State & VREPLACE_FLAG)
8118 {
8119 buf[0] = 'R';
8120 buf[1] = 'v';
8121 }
8122 else
8123#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008124 {
8125 if (State & REPLACE_FLAG)
8126 buf[0] = 'R';
8127 else
8128 buf[0] = 'i';
8129#ifdef FEAT_INS_EXPAND
8130 if (ins_compl_active())
8131 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008132 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008133 buf[1] = 'x';
8134#endif
8135 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008136 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008137 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008138 {
8139 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008140 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008141 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008142 else if (exmode_active == EXMODE_NORMAL)
8143 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008144 }
8145 else
8146 {
8147 buf[0] = 'n';
8148 if (finish_op)
8149 buf[1] = 'o';
8150 }
8151
8152 /* Clear out the minor mode when the argument is not a non-zero number or
8153 * non-empty string. */
8154 if (!non_zero_arg(&argvars[0]))
8155 buf[1] = NUL;
8156
8157 rettv->vval.v_string = vim_strsave(buf);
8158 rettv->v_type = VAR_STRING;
8159}
8160
8161#if defined(FEAT_MZSCHEME) || defined(PROTO)
8162/*
8163 * "mzeval()" function
8164 */
8165 static void
8166f_mzeval(typval_T *argvars, typval_T *rettv)
8167{
8168 char_u *str;
8169 char_u buf[NUMBUFLEN];
8170
8171 str = get_tv_string_buf(&argvars[0], buf);
8172 do_mzeval(str, rettv);
8173}
8174
8175 void
8176mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8177{
8178 typval_T argvars[3];
8179
8180 argvars[0].v_type = VAR_STRING;
8181 argvars[0].vval.v_string = name;
8182 copy_tv(args, &argvars[1]);
8183 argvars[2].v_type = VAR_UNKNOWN;
8184 f_call(argvars, rettv);
8185 clear_tv(&argvars[1]);
8186}
8187#endif
8188
8189/*
8190 * "nextnonblank()" function
8191 */
8192 static void
8193f_nextnonblank(typval_T *argvars, typval_T *rettv)
8194{
8195 linenr_T lnum;
8196
8197 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8198 {
8199 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8200 {
8201 lnum = 0;
8202 break;
8203 }
8204 if (*skipwhite(ml_get(lnum)) != NUL)
8205 break;
8206 }
8207 rettv->vval.v_number = lnum;
8208}
8209
8210/*
8211 * "nr2char()" function
8212 */
8213 static void
8214f_nr2char(typval_T *argvars, typval_T *rettv)
8215{
8216 char_u buf[NUMBUFLEN];
8217
8218#ifdef FEAT_MBYTE
8219 if (has_mbyte)
8220 {
8221 int utf8 = 0;
8222
8223 if (argvars[1].v_type != VAR_UNKNOWN)
8224 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8225 if (utf8)
8226 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8227 else
8228 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8229 }
8230 else
8231#endif
8232 {
8233 buf[0] = (char_u)get_tv_number(&argvars[0]);
8234 buf[1] = NUL;
8235 }
8236 rettv->v_type = VAR_STRING;
8237 rettv->vval.v_string = vim_strsave(buf);
8238}
8239
8240/*
8241 * "or(expr, expr)" function
8242 */
8243 static void
8244f_or(typval_T *argvars, typval_T *rettv)
8245{
8246 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8247 | get_tv_number_chk(&argvars[1], NULL);
8248}
8249
8250/*
8251 * "pathshorten()" function
8252 */
8253 static void
8254f_pathshorten(typval_T *argvars, typval_T *rettv)
8255{
8256 char_u *p;
8257
8258 rettv->v_type = VAR_STRING;
8259 p = get_tv_string_chk(&argvars[0]);
8260 if (p == NULL)
8261 rettv->vval.v_string = NULL;
8262 else
8263 {
8264 p = vim_strsave(p);
8265 rettv->vval.v_string = p;
8266 if (p != NULL)
8267 shorten_dir(p);
8268 }
8269}
8270
8271#ifdef FEAT_PERL
8272/*
8273 * "perleval()" function
8274 */
8275 static void
8276f_perleval(typval_T *argvars, typval_T *rettv)
8277{
8278 char_u *str;
8279 char_u buf[NUMBUFLEN];
8280
8281 str = get_tv_string_buf(&argvars[0], buf);
8282 do_perleval(str, rettv);
8283}
8284#endif
8285
8286#ifdef FEAT_FLOAT
8287/*
8288 * "pow()" function
8289 */
8290 static void
8291f_pow(typval_T *argvars, typval_T *rettv)
8292{
8293 float_T fx = 0.0, fy = 0.0;
8294
8295 rettv->v_type = VAR_FLOAT;
8296 if (get_float_arg(argvars, &fx) == OK
8297 && get_float_arg(&argvars[1], &fy) == OK)
8298 rettv->vval.v_float = pow(fx, fy);
8299 else
8300 rettv->vval.v_float = 0.0;
8301}
8302#endif
8303
8304/*
8305 * "prevnonblank()" function
8306 */
8307 static void
8308f_prevnonblank(typval_T *argvars, typval_T *rettv)
8309{
8310 linenr_T lnum;
8311
8312 lnum = get_tv_lnum(argvars);
8313 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8314 lnum = 0;
8315 else
8316 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8317 --lnum;
8318 rettv->vval.v_number = lnum;
8319}
8320
8321/* This dummy va_list is here because:
8322 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8323 * - locally in the function results in a "used before set" warning
8324 * - using va_start() to initialize it gives "function with fixed args" error */
8325static va_list ap;
8326
8327/*
8328 * "printf()" function
8329 */
8330 static void
8331f_printf(typval_T *argvars, typval_T *rettv)
8332{
8333 char_u buf[NUMBUFLEN];
8334 int len;
8335 char_u *s;
8336 int saved_did_emsg = did_emsg;
8337 char *fmt;
8338
8339 rettv->v_type = VAR_STRING;
8340 rettv->vval.v_string = NULL;
8341
8342 /* Get the required length, allocate the buffer and do it for real. */
8343 did_emsg = FALSE;
8344 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008345 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008346 if (!did_emsg)
8347 {
8348 s = alloc(len + 1);
8349 if (s != NULL)
8350 {
8351 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008352 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8353 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008354 }
8355 }
8356 did_emsg |= saved_did_emsg;
8357}
8358
8359/*
8360 * "pumvisible()" function
8361 */
8362 static void
8363f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8364{
8365#ifdef FEAT_INS_EXPAND
8366 if (pum_visible())
8367 rettv->vval.v_number = 1;
8368#endif
8369}
8370
8371#ifdef FEAT_PYTHON3
8372/*
8373 * "py3eval()" function
8374 */
8375 static void
8376f_py3eval(typval_T *argvars, typval_T *rettv)
8377{
8378 char_u *str;
8379 char_u buf[NUMBUFLEN];
8380
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008381 if (p_pyx == 0)
8382 p_pyx = 3;
8383
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008384 str = get_tv_string_buf(&argvars[0], buf);
8385 do_py3eval(str, rettv);
8386}
8387#endif
8388
8389#ifdef FEAT_PYTHON
8390/*
8391 * "pyeval()" function
8392 */
8393 static void
8394f_pyeval(typval_T *argvars, typval_T *rettv)
8395{
8396 char_u *str;
8397 char_u buf[NUMBUFLEN];
8398
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008399 if (p_pyx == 0)
8400 p_pyx = 2;
8401
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008402 str = get_tv_string_buf(&argvars[0], buf);
8403 do_pyeval(str, rettv);
8404}
8405#endif
8406
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008407#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8408/*
8409 * "pyxeval()" function
8410 */
8411 static void
8412f_pyxeval(typval_T *argvars, typval_T *rettv)
8413{
8414# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8415 init_pyxversion();
8416 if (p_pyx == 2)
8417 f_pyeval(argvars, rettv);
8418 else
8419 f_py3eval(argvars, rettv);
8420# elif defined(FEAT_PYTHON)
8421 f_pyeval(argvars, rettv);
8422# elif defined(FEAT_PYTHON3)
8423 f_py3eval(argvars, rettv);
8424# endif
8425}
8426#endif
8427
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008428/*
8429 * "range()" function
8430 */
8431 static void
8432f_range(typval_T *argvars, typval_T *rettv)
8433{
8434 varnumber_T start;
8435 varnumber_T end;
8436 varnumber_T stride = 1;
8437 varnumber_T i;
8438 int error = FALSE;
8439
8440 start = get_tv_number_chk(&argvars[0], &error);
8441 if (argvars[1].v_type == VAR_UNKNOWN)
8442 {
8443 end = start - 1;
8444 start = 0;
8445 }
8446 else
8447 {
8448 end = get_tv_number_chk(&argvars[1], &error);
8449 if (argvars[2].v_type != VAR_UNKNOWN)
8450 stride = get_tv_number_chk(&argvars[2], &error);
8451 }
8452
8453 if (error)
8454 return; /* type error; errmsg already given */
8455 if (stride == 0)
8456 EMSG(_("E726: Stride is zero"));
8457 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8458 EMSG(_("E727: Start past end"));
8459 else
8460 {
8461 if (rettv_list_alloc(rettv) == OK)
8462 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8463 if (list_append_number(rettv->vval.v_list,
8464 (varnumber_T)i) == FAIL)
8465 break;
8466 }
8467}
8468
8469/*
8470 * "readfile()" function
8471 */
8472 static void
8473f_readfile(typval_T *argvars, typval_T *rettv)
8474{
8475 int binary = FALSE;
8476 int failed = FALSE;
8477 char_u *fname;
8478 FILE *fd;
8479 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8480 int io_size = sizeof(buf);
8481 int readlen; /* size of last fread() */
8482 char_u *prev = NULL; /* previously read bytes, if any */
8483 long prevlen = 0; /* length of data in prev */
8484 long prevsize = 0; /* size of prev buffer */
8485 long maxline = MAXLNUM;
8486 long cnt = 0;
8487 char_u *p; /* position in buf */
8488 char_u *start; /* start of current line */
8489
8490 if (argvars[1].v_type != VAR_UNKNOWN)
8491 {
8492 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8493 binary = TRUE;
8494 if (argvars[2].v_type != VAR_UNKNOWN)
8495 maxline = (long)get_tv_number(&argvars[2]);
8496 }
8497
8498 if (rettv_list_alloc(rettv) == FAIL)
8499 return;
8500
8501 /* Always open the file in binary mode, library functions have a mind of
8502 * their own about CR-LF conversion. */
8503 fname = get_tv_string(&argvars[0]);
8504 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8505 {
8506 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8507 return;
8508 }
8509
8510 while (cnt < maxline || maxline < 0)
8511 {
8512 readlen = (int)fread(buf, 1, io_size, fd);
8513
8514 /* This for loop processes what was read, but is also entered at end
8515 * of file so that either:
8516 * - an incomplete line gets written
8517 * - a "binary" file gets an empty line at the end if it ends in a
8518 * newline. */
8519 for (p = buf, start = buf;
8520 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8521 ++p)
8522 {
8523 if (*p == '\n' || readlen <= 0)
8524 {
8525 listitem_T *li;
8526 char_u *s = NULL;
8527 long_u len = p - start;
8528
8529 /* Finished a line. Remove CRs before NL. */
8530 if (readlen > 0 && !binary)
8531 {
8532 while (len > 0 && start[len - 1] == '\r')
8533 --len;
8534 /* removal may cross back to the "prev" string */
8535 if (len == 0)
8536 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8537 --prevlen;
8538 }
8539 if (prevlen == 0)
8540 s = vim_strnsave(start, (int)len);
8541 else
8542 {
8543 /* Change "prev" buffer to be the right size. This way
8544 * the bytes are only copied once, and very long lines are
8545 * allocated only once. */
8546 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8547 {
8548 mch_memmove(s + prevlen, start, len);
8549 s[prevlen + len] = NUL;
8550 prev = NULL; /* the list will own the string */
8551 prevlen = prevsize = 0;
8552 }
8553 }
8554 if (s == NULL)
8555 {
8556 do_outofmem_msg((long_u) prevlen + len + 1);
8557 failed = TRUE;
8558 break;
8559 }
8560
8561 if ((li = listitem_alloc()) == NULL)
8562 {
8563 vim_free(s);
8564 failed = TRUE;
8565 break;
8566 }
8567 li->li_tv.v_type = VAR_STRING;
8568 li->li_tv.v_lock = 0;
8569 li->li_tv.vval.v_string = s;
8570 list_append(rettv->vval.v_list, li);
8571
8572 start = p + 1; /* step over newline */
8573 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8574 break;
8575 }
8576 else if (*p == NUL)
8577 *p = '\n';
8578#ifdef FEAT_MBYTE
8579 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8580 * when finding the BF and check the previous two bytes. */
8581 else if (*p == 0xbf && enc_utf8 && !binary)
8582 {
8583 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8584 * + 1, these may be in the "prev" string. */
8585 char_u back1 = p >= buf + 1 ? p[-1]
8586 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8587 char_u back2 = p >= buf + 2 ? p[-2]
8588 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8589 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8590
8591 if (back2 == 0xef && back1 == 0xbb)
8592 {
8593 char_u *dest = p - 2;
8594
8595 /* Usually a BOM is at the beginning of a file, and so at
8596 * the beginning of a line; then we can just step over it.
8597 */
8598 if (start == dest)
8599 start = p + 1;
8600 else
8601 {
8602 /* have to shuffle buf to close gap */
8603 int adjust_prevlen = 0;
8604
8605 if (dest < buf)
8606 {
8607 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8608 dest = buf;
8609 }
8610 if (readlen > p - buf + 1)
8611 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8612 readlen -= 3 - adjust_prevlen;
8613 prevlen -= adjust_prevlen;
8614 p = dest - 1;
8615 }
8616 }
8617 }
8618#endif
8619 } /* for */
8620
8621 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8622 break;
8623 if (start < p)
8624 {
8625 /* There's part of a line in buf, store it in "prev". */
8626 if (p - start + prevlen >= prevsize)
8627 {
8628 /* need bigger "prev" buffer */
8629 char_u *newprev;
8630
8631 /* A common use case is ordinary text files and "prev" gets a
8632 * fragment of a line, so the first allocation is made
8633 * small, to avoid repeatedly 'allocing' large and
8634 * 'reallocing' small. */
8635 if (prevsize == 0)
8636 prevsize = (long)(p - start);
8637 else
8638 {
8639 long grow50pc = (prevsize * 3) / 2;
8640 long growmin = (long)((p - start) * 2 + prevlen);
8641 prevsize = grow50pc > growmin ? grow50pc : growmin;
8642 }
8643 newprev = prev == NULL ? alloc(prevsize)
8644 : vim_realloc(prev, prevsize);
8645 if (newprev == NULL)
8646 {
8647 do_outofmem_msg((long_u)prevsize);
8648 failed = TRUE;
8649 break;
8650 }
8651 prev = newprev;
8652 }
8653 /* Add the line part to end of "prev". */
8654 mch_memmove(prev + prevlen, start, p - start);
8655 prevlen += (long)(p - start);
8656 }
8657 } /* while */
8658
8659 /*
8660 * For a negative line count use only the lines at the end of the file,
8661 * free the rest.
8662 */
8663 if (!failed && maxline < 0)
8664 while (cnt > -maxline)
8665 {
8666 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8667 --cnt;
8668 }
8669
8670 if (failed)
8671 {
8672 list_free(rettv->vval.v_list);
8673 /* readfile doc says an empty list is returned on error */
8674 rettv->vval.v_list = list_alloc();
8675 }
8676
8677 vim_free(prev);
8678 fclose(fd);
8679}
8680
8681#if defined(FEAT_RELTIME)
8682static int list2proftime(typval_T *arg, proftime_T *tm);
8683
8684/*
8685 * Convert a List to proftime_T.
8686 * Return FAIL when there is something wrong.
8687 */
8688 static int
8689list2proftime(typval_T *arg, proftime_T *tm)
8690{
8691 long n1, n2;
8692 int error = FALSE;
8693
8694 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8695 || arg->vval.v_list->lv_len != 2)
8696 return FAIL;
8697 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8698 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8699# ifdef WIN3264
8700 tm->HighPart = n1;
8701 tm->LowPart = n2;
8702# else
8703 tm->tv_sec = n1;
8704 tm->tv_usec = n2;
8705# endif
8706 return error ? FAIL : OK;
8707}
8708#endif /* FEAT_RELTIME */
8709
8710/*
8711 * "reltime()" function
8712 */
8713 static void
8714f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8715{
8716#ifdef FEAT_RELTIME
8717 proftime_T res;
8718 proftime_T start;
8719
8720 if (argvars[0].v_type == VAR_UNKNOWN)
8721 {
8722 /* No arguments: get current time. */
8723 profile_start(&res);
8724 }
8725 else if (argvars[1].v_type == VAR_UNKNOWN)
8726 {
8727 if (list2proftime(&argvars[0], &res) == FAIL)
8728 return;
8729 profile_end(&res);
8730 }
8731 else
8732 {
8733 /* Two arguments: compute the difference. */
8734 if (list2proftime(&argvars[0], &start) == FAIL
8735 || list2proftime(&argvars[1], &res) == FAIL)
8736 return;
8737 profile_sub(&res, &start);
8738 }
8739
8740 if (rettv_list_alloc(rettv) == OK)
8741 {
8742 long n1, n2;
8743
8744# ifdef WIN3264
8745 n1 = res.HighPart;
8746 n2 = res.LowPart;
8747# else
8748 n1 = res.tv_sec;
8749 n2 = res.tv_usec;
8750# endif
8751 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8752 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8753 }
8754#endif
8755}
8756
8757#ifdef FEAT_FLOAT
8758/*
8759 * "reltimefloat()" function
8760 */
8761 static void
8762f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8763{
8764# ifdef FEAT_RELTIME
8765 proftime_T tm;
8766# endif
8767
8768 rettv->v_type = VAR_FLOAT;
8769 rettv->vval.v_float = 0;
8770# ifdef FEAT_RELTIME
8771 if (list2proftime(&argvars[0], &tm) == OK)
8772 rettv->vval.v_float = profile_float(&tm);
8773# endif
8774}
8775#endif
8776
8777/*
8778 * "reltimestr()" function
8779 */
8780 static void
8781f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8782{
8783#ifdef FEAT_RELTIME
8784 proftime_T tm;
8785#endif
8786
8787 rettv->v_type = VAR_STRING;
8788 rettv->vval.v_string = NULL;
8789#ifdef FEAT_RELTIME
8790 if (list2proftime(&argvars[0], &tm) == OK)
8791 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8792#endif
8793}
8794
8795#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8796static void make_connection(void);
8797static int check_connection(void);
8798
8799 static void
8800make_connection(void)
8801{
8802 if (X_DISPLAY == NULL
8803# ifdef FEAT_GUI
8804 && !gui.in_use
8805# endif
8806 )
8807 {
8808 x_force_connect = TRUE;
8809 setup_term_clip();
8810 x_force_connect = FALSE;
8811 }
8812}
8813
8814 static int
8815check_connection(void)
8816{
8817 make_connection();
8818 if (X_DISPLAY == NULL)
8819 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008820 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008821 return FAIL;
8822 }
8823 return OK;
8824}
8825#endif
8826
8827#ifdef FEAT_CLIENTSERVER
8828 static void
8829remote_common(typval_T *argvars, typval_T *rettv, int expr)
8830{
8831 char_u *server_name;
8832 char_u *keys;
8833 char_u *r = NULL;
8834 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008835 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008836# ifdef WIN32
8837 HWND w;
8838# else
8839 Window w;
8840# endif
8841
8842 if (check_restricted() || check_secure())
8843 return;
8844
8845# ifdef FEAT_X11
8846 if (check_connection() == FAIL)
8847 return;
8848# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008849 if (argvars[2].v_type != VAR_UNKNOWN
8850 && argvars[3].v_type != VAR_UNKNOWN)
8851 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008852
8853 server_name = get_tv_string_chk(&argvars[0]);
8854 if (server_name == NULL)
8855 return; /* type error; errmsg already given */
8856 keys = get_tv_string_buf(&argvars[1], buf);
8857# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008858 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008859# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008860 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8861 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008862# endif
8863 {
8864 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008865 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008866 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008867 vim_free(r);
8868 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008869 else
8870 EMSG2(_("E241: Unable to send to %s"), server_name);
8871 return;
8872 }
8873
8874 rettv->vval.v_string = r;
8875
8876 if (argvars[2].v_type != VAR_UNKNOWN)
8877 {
8878 dictitem_T v;
8879 char_u str[30];
8880 char_u *idvar;
8881
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008882 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008883 if (idvar != NULL && *idvar != NUL)
8884 {
8885 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8886 v.di_tv.v_type = VAR_STRING;
8887 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008888 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008889 vim_free(v.di_tv.vval.v_string);
8890 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008891 }
8892}
8893#endif
8894
8895/*
8896 * "remote_expr()" function
8897 */
8898 static void
8899f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8900{
8901 rettv->v_type = VAR_STRING;
8902 rettv->vval.v_string = NULL;
8903#ifdef FEAT_CLIENTSERVER
8904 remote_common(argvars, rettv, TRUE);
8905#endif
8906}
8907
8908/*
8909 * "remote_foreground()" function
8910 */
8911 static void
8912f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8913{
8914#ifdef FEAT_CLIENTSERVER
8915# ifdef WIN32
8916 /* On Win32 it's done in this application. */
8917 {
8918 char_u *server_name = get_tv_string_chk(&argvars[0]);
8919
8920 if (server_name != NULL)
8921 serverForeground(server_name);
8922 }
8923# else
8924 /* Send a foreground() expression to the server. */
8925 argvars[1].v_type = VAR_STRING;
8926 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8927 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008928 rettv->v_type = VAR_STRING;
8929 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008930 remote_common(argvars, rettv, TRUE);
8931 vim_free(argvars[1].vval.v_string);
8932# endif
8933#endif
8934}
8935
8936 static void
8937f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8938{
8939#ifdef FEAT_CLIENTSERVER
8940 dictitem_T v;
8941 char_u *s = NULL;
8942# ifdef WIN32
8943 long_u n = 0;
8944# endif
8945 char_u *serverid;
8946
8947 if (check_restricted() || check_secure())
8948 {
8949 rettv->vval.v_number = -1;
8950 return;
8951 }
8952 serverid = get_tv_string_chk(&argvars[0]);
8953 if (serverid == NULL)
8954 {
8955 rettv->vval.v_number = -1;
8956 return; /* type error; errmsg already given */
8957 }
8958# ifdef WIN32
8959 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8960 if (n == 0)
8961 rettv->vval.v_number = -1;
8962 else
8963 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008964 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008965 rettv->vval.v_number = (s != NULL);
8966 }
8967# else
8968 if (check_connection() == FAIL)
8969 return;
8970
8971 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8972 serverStrToWin(serverid), &s);
8973# endif
8974
8975 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8976 {
8977 char_u *retvar;
8978
8979 v.di_tv.v_type = VAR_STRING;
8980 v.di_tv.vval.v_string = vim_strsave(s);
8981 retvar = get_tv_string_chk(&argvars[1]);
8982 if (retvar != NULL)
8983 set_var(retvar, &v.di_tv, FALSE);
8984 vim_free(v.di_tv.vval.v_string);
8985 }
8986#else
8987 rettv->vval.v_number = -1;
8988#endif
8989}
8990
8991 static void
8992f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8993{
8994 char_u *r = NULL;
8995
8996#ifdef FEAT_CLIENTSERVER
8997 char_u *serverid = get_tv_string_chk(&argvars[0]);
8998
8999 if (serverid != NULL && !check_restricted() && !check_secure())
9000 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009001 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009002# ifdef WIN32
9003 /* The server's HWND is encoded in the 'id' parameter */
9004 long_u n = 0;
9005# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009006
9007 if (argvars[1].v_type != VAR_UNKNOWN)
9008 timeout = get_tv_number(&argvars[1]);
9009
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009010# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009011 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9012 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009013 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009014 if (r == NULL)
9015# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009016 if (check_connection() == FAIL
9017 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9018 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009019# endif
9020 EMSG(_("E277: Unable to read a server reply"));
9021 }
9022#endif
9023 rettv->v_type = VAR_STRING;
9024 rettv->vval.v_string = r;
9025}
9026
9027/*
9028 * "remote_send()" function
9029 */
9030 static void
9031f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9032{
9033 rettv->v_type = VAR_STRING;
9034 rettv->vval.v_string = NULL;
9035#ifdef FEAT_CLIENTSERVER
9036 remote_common(argvars, rettv, FALSE);
9037#endif
9038}
9039
9040/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009041 * "remote_startserver()" function
9042 */
9043 static void
9044f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9045{
9046#ifdef FEAT_CLIENTSERVER
9047 char_u *server = get_tv_string_chk(&argvars[0]);
9048
9049 if (server == NULL)
9050 return; /* type error; errmsg already given */
9051 if (serverName != NULL)
9052 EMSG(_("E941: already started a server"));
9053 else
9054 {
9055# ifdef FEAT_X11
9056 if (check_connection() == OK)
9057 serverRegisterName(X_DISPLAY, server);
9058# else
9059 serverSetName(server);
9060# endif
9061 }
9062#else
9063 EMSG(_("E942: +clientserver feature not available"));
9064#endif
9065}
9066
9067/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009068 * "remove()" function
9069 */
9070 static void
9071f_remove(typval_T *argvars, typval_T *rettv)
9072{
9073 list_T *l;
9074 listitem_T *item, *item2;
9075 listitem_T *li;
9076 long idx;
9077 long end;
9078 char_u *key;
9079 dict_T *d;
9080 dictitem_T *di;
9081 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9082
9083 if (argvars[0].v_type == VAR_DICT)
9084 {
9085 if (argvars[2].v_type != VAR_UNKNOWN)
9086 EMSG2(_(e_toomanyarg), "remove()");
9087 else if ((d = argvars[0].vval.v_dict) != NULL
9088 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9089 {
9090 key = get_tv_string_chk(&argvars[1]);
9091 if (key != NULL)
9092 {
9093 di = dict_find(d, key, -1);
9094 if (di == NULL)
9095 EMSG2(_(e_dictkey), key);
9096 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9097 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9098 {
9099 *rettv = di->di_tv;
9100 init_tv(&di->di_tv);
9101 dictitem_remove(d, di);
9102 }
9103 }
9104 }
9105 }
9106 else if (argvars[0].v_type != VAR_LIST)
9107 EMSG2(_(e_listdictarg), "remove()");
9108 else if ((l = argvars[0].vval.v_list) != NULL
9109 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9110 {
9111 int error = FALSE;
9112
9113 idx = (long)get_tv_number_chk(&argvars[1], &error);
9114 if (error)
9115 ; /* type error: do nothing, errmsg already given */
9116 else if ((item = list_find(l, idx)) == NULL)
9117 EMSGN(_(e_listidx), idx);
9118 else
9119 {
9120 if (argvars[2].v_type == VAR_UNKNOWN)
9121 {
9122 /* Remove one item, return its value. */
9123 vimlist_remove(l, item, item);
9124 *rettv = item->li_tv;
9125 vim_free(item);
9126 }
9127 else
9128 {
9129 /* Remove range of items, return list with values. */
9130 end = (long)get_tv_number_chk(&argvars[2], &error);
9131 if (error)
9132 ; /* type error: do nothing */
9133 else if ((item2 = list_find(l, end)) == NULL)
9134 EMSGN(_(e_listidx), end);
9135 else
9136 {
9137 int cnt = 0;
9138
9139 for (li = item; li != NULL; li = li->li_next)
9140 {
9141 ++cnt;
9142 if (li == item2)
9143 break;
9144 }
9145 if (li == NULL) /* didn't find "item2" after "item" */
9146 EMSG(_(e_invrange));
9147 else
9148 {
9149 vimlist_remove(l, item, item2);
9150 if (rettv_list_alloc(rettv) == OK)
9151 {
9152 l = rettv->vval.v_list;
9153 l->lv_first = item;
9154 l->lv_last = item2;
9155 item->li_prev = NULL;
9156 item2->li_next = NULL;
9157 l->lv_len = cnt;
9158 }
9159 }
9160 }
9161 }
9162 }
9163 }
9164}
9165
9166/*
9167 * "rename({from}, {to})" function
9168 */
9169 static void
9170f_rename(typval_T *argvars, typval_T *rettv)
9171{
9172 char_u buf[NUMBUFLEN];
9173
9174 if (check_restricted() || check_secure())
9175 rettv->vval.v_number = -1;
9176 else
9177 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9178 get_tv_string_buf(&argvars[1], buf));
9179}
9180
9181/*
9182 * "repeat()" function
9183 */
9184 static void
9185f_repeat(typval_T *argvars, typval_T *rettv)
9186{
9187 char_u *p;
9188 int n;
9189 int slen;
9190 int len;
9191 char_u *r;
9192 int i;
9193
9194 n = (int)get_tv_number(&argvars[1]);
9195 if (argvars[0].v_type == VAR_LIST)
9196 {
9197 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9198 while (n-- > 0)
9199 if (list_extend(rettv->vval.v_list,
9200 argvars[0].vval.v_list, NULL) == FAIL)
9201 break;
9202 }
9203 else
9204 {
9205 p = get_tv_string(&argvars[0]);
9206 rettv->v_type = VAR_STRING;
9207 rettv->vval.v_string = NULL;
9208
9209 slen = (int)STRLEN(p);
9210 len = slen * n;
9211 if (len <= 0)
9212 return;
9213
9214 r = alloc(len + 1);
9215 if (r != NULL)
9216 {
9217 for (i = 0; i < n; i++)
9218 mch_memmove(r + i * slen, p, (size_t)slen);
9219 r[len] = NUL;
9220 }
9221
9222 rettv->vval.v_string = r;
9223 }
9224}
9225
9226/*
9227 * "resolve()" function
9228 */
9229 static void
9230f_resolve(typval_T *argvars, typval_T *rettv)
9231{
9232 char_u *p;
9233#ifdef HAVE_READLINK
9234 char_u *buf = NULL;
9235#endif
9236
9237 p = get_tv_string(&argvars[0]);
9238#ifdef FEAT_SHORTCUT
9239 {
9240 char_u *v = NULL;
9241
9242 v = mch_resolve_shortcut(p);
9243 if (v != NULL)
9244 rettv->vval.v_string = v;
9245 else
9246 rettv->vval.v_string = vim_strsave(p);
9247 }
9248#else
9249# ifdef HAVE_READLINK
9250 {
9251 char_u *cpy;
9252 int len;
9253 char_u *remain = NULL;
9254 char_u *q;
9255 int is_relative_to_current = FALSE;
9256 int has_trailing_pathsep = FALSE;
9257 int limit = 100;
9258
9259 p = vim_strsave(p);
9260
9261 if (p[0] == '.' && (vim_ispathsep(p[1])
9262 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9263 is_relative_to_current = TRUE;
9264
9265 len = STRLEN(p);
9266 if (len > 0 && after_pathsep(p, p + len))
9267 {
9268 has_trailing_pathsep = TRUE;
9269 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9270 }
9271
9272 q = getnextcomp(p);
9273 if (*q != NUL)
9274 {
9275 /* Separate the first path component in "p", and keep the
9276 * remainder (beginning with the path separator). */
9277 remain = vim_strsave(q - 1);
9278 q[-1] = NUL;
9279 }
9280
9281 buf = alloc(MAXPATHL + 1);
9282 if (buf == NULL)
9283 goto fail;
9284
9285 for (;;)
9286 {
9287 for (;;)
9288 {
9289 len = readlink((char *)p, (char *)buf, MAXPATHL);
9290 if (len <= 0)
9291 break;
9292 buf[len] = NUL;
9293
9294 if (limit-- == 0)
9295 {
9296 vim_free(p);
9297 vim_free(remain);
9298 EMSG(_("E655: Too many symbolic links (cycle?)"));
9299 rettv->vval.v_string = NULL;
9300 goto fail;
9301 }
9302
9303 /* Ensure that the result will have a trailing path separator
9304 * if the argument has one. */
9305 if (remain == NULL && has_trailing_pathsep)
9306 add_pathsep(buf);
9307
9308 /* Separate the first path component in the link value and
9309 * concatenate the remainders. */
9310 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9311 if (*q != NUL)
9312 {
9313 if (remain == NULL)
9314 remain = vim_strsave(q - 1);
9315 else
9316 {
9317 cpy = concat_str(q - 1, remain);
9318 if (cpy != NULL)
9319 {
9320 vim_free(remain);
9321 remain = cpy;
9322 }
9323 }
9324 q[-1] = NUL;
9325 }
9326
9327 q = gettail(p);
9328 if (q > p && *q == NUL)
9329 {
9330 /* Ignore trailing path separator. */
9331 q[-1] = NUL;
9332 q = gettail(p);
9333 }
9334 if (q > p && !mch_isFullName(buf))
9335 {
9336 /* symlink is relative to directory of argument */
9337 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9338 if (cpy != NULL)
9339 {
9340 STRCPY(cpy, p);
9341 STRCPY(gettail(cpy), buf);
9342 vim_free(p);
9343 p = cpy;
9344 }
9345 }
9346 else
9347 {
9348 vim_free(p);
9349 p = vim_strsave(buf);
9350 }
9351 }
9352
9353 if (remain == NULL)
9354 break;
9355
9356 /* Append the first path component of "remain" to "p". */
9357 q = getnextcomp(remain + 1);
9358 len = q - remain - (*q != NUL);
9359 cpy = vim_strnsave(p, STRLEN(p) + len);
9360 if (cpy != NULL)
9361 {
9362 STRNCAT(cpy, remain, len);
9363 vim_free(p);
9364 p = cpy;
9365 }
9366 /* Shorten "remain". */
9367 if (*q != NUL)
9368 STRMOVE(remain, q - 1);
9369 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009370 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009371 }
9372
9373 /* If the result is a relative path name, make it explicitly relative to
9374 * the current directory if and only if the argument had this form. */
9375 if (!vim_ispathsep(*p))
9376 {
9377 if (is_relative_to_current
9378 && *p != NUL
9379 && !(p[0] == '.'
9380 && (p[1] == NUL
9381 || vim_ispathsep(p[1])
9382 || (p[1] == '.'
9383 && (p[2] == NUL
9384 || vim_ispathsep(p[2]))))))
9385 {
9386 /* Prepend "./". */
9387 cpy = concat_str((char_u *)"./", p);
9388 if (cpy != NULL)
9389 {
9390 vim_free(p);
9391 p = cpy;
9392 }
9393 }
9394 else if (!is_relative_to_current)
9395 {
9396 /* Strip leading "./". */
9397 q = p;
9398 while (q[0] == '.' && vim_ispathsep(q[1]))
9399 q += 2;
9400 if (q > p)
9401 STRMOVE(p, p + 2);
9402 }
9403 }
9404
9405 /* Ensure that the result will have no trailing path separator
9406 * if the argument had none. But keep "/" or "//". */
9407 if (!has_trailing_pathsep)
9408 {
9409 q = p + STRLEN(p);
9410 if (after_pathsep(p, q))
9411 *gettail_sep(p) = NUL;
9412 }
9413
9414 rettv->vval.v_string = p;
9415 }
9416# else
9417 rettv->vval.v_string = vim_strsave(p);
9418# endif
9419#endif
9420
9421 simplify_filename(rettv->vval.v_string);
9422
9423#ifdef HAVE_READLINK
9424fail:
9425 vim_free(buf);
9426#endif
9427 rettv->v_type = VAR_STRING;
9428}
9429
9430/*
9431 * "reverse({list})" function
9432 */
9433 static void
9434f_reverse(typval_T *argvars, typval_T *rettv)
9435{
9436 list_T *l;
9437 listitem_T *li, *ni;
9438
9439 if (argvars[0].v_type != VAR_LIST)
9440 EMSG2(_(e_listarg), "reverse()");
9441 else if ((l = argvars[0].vval.v_list) != NULL
9442 && !tv_check_lock(l->lv_lock,
9443 (char_u *)N_("reverse() argument"), TRUE))
9444 {
9445 li = l->lv_last;
9446 l->lv_first = l->lv_last = NULL;
9447 l->lv_len = 0;
9448 while (li != NULL)
9449 {
9450 ni = li->li_prev;
9451 list_append(l, li);
9452 li = ni;
9453 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009454 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009455 l->lv_idx = l->lv_len - l->lv_idx - 1;
9456 }
9457}
9458
9459#define SP_NOMOVE 0x01 /* don't move cursor */
9460#define SP_REPEAT 0x02 /* repeat to find outer pair */
9461#define SP_RETCOUNT 0x04 /* return matchcount */
9462#define SP_SETPCMARK 0x08 /* set previous context mark */
9463#define SP_START 0x10 /* accept match at start position */
9464#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9465#define SP_END 0x40 /* leave cursor at end of match */
9466#define SP_COLUMN 0x80 /* start at cursor column */
9467
9468static int get_search_arg(typval_T *varp, int *flagsp);
9469
9470/*
9471 * Get flags for a search function.
9472 * Possibly sets "p_ws".
9473 * Returns BACKWARD, FORWARD or zero (for an error).
9474 */
9475 static int
9476get_search_arg(typval_T *varp, int *flagsp)
9477{
9478 int dir = FORWARD;
9479 char_u *flags;
9480 char_u nbuf[NUMBUFLEN];
9481 int mask;
9482
9483 if (varp->v_type != VAR_UNKNOWN)
9484 {
9485 flags = get_tv_string_buf_chk(varp, nbuf);
9486 if (flags == NULL)
9487 return 0; /* type error; errmsg already given */
9488 while (*flags != NUL)
9489 {
9490 switch (*flags)
9491 {
9492 case 'b': dir = BACKWARD; break;
9493 case 'w': p_ws = TRUE; break;
9494 case 'W': p_ws = FALSE; break;
9495 default: mask = 0;
9496 if (flagsp != NULL)
9497 switch (*flags)
9498 {
9499 case 'c': mask = SP_START; break;
9500 case 'e': mask = SP_END; break;
9501 case 'm': mask = SP_RETCOUNT; break;
9502 case 'n': mask = SP_NOMOVE; break;
9503 case 'p': mask = SP_SUBPAT; break;
9504 case 'r': mask = SP_REPEAT; break;
9505 case 's': mask = SP_SETPCMARK; break;
9506 case 'z': mask = SP_COLUMN; break;
9507 }
9508 if (mask == 0)
9509 {
9510 EMSG2(_(e_invarg2), flags);
9511 dir = 0;
9512 }
9513 else
9514 *flagsp |= mask;
9515 }
9516 if (dir == 0)
9517 break;
9518 ++flags;
9519 }
9520 }
9521 return dir;
9522}
9523
9524/*
9525 * Shared by search() and searchpos() functions.
9526 */
9527 static int
9528search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9529{
9530 int flags;
9531 char_u *pat;
9532 pos_T pos;
9533 pos_T save_cursor;
9534 int save_p_ws = p_ws;
9535 int dir;
9536 int retval = 0; /* default: FAIL */
9537 long lnum_stop = 0;
9538 proftime_T tm;
9539#ifdef FEAT_RELTIME
9540 long time_limit = 0;
9541#endif
9542 int options = SEARCH_KEEP;
9543 int subpatnum;
9544
9545 pat = get_tv_string(&argvars[0]);
9546 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9547 if (dir == 0)
9548 goto theend;
9549 flags = *flagsp;
9550 if (flags & SP_START)
9551 options |= SEARCH_START;
9552 if (flags & SP_END)
9553 options |= SEARCH_END;
9554 if (flags & SP_COLUMN)
9555 options |= SEARCH_COL;
9556
9557 /* Optional arguments: line number to stop searching and timeout. */
9558 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9559 {
9560 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9561 if (lnum_stop < 0)
9562 goto theend;
9563#ifdef FEAT_RELTIME
9564 if (argvars[3].v_type != VAR_UNKNOWN)
9565 {
9566 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9567 if (time_limit < 0)
9568 goto theend;
9569 }
9570#endif
9571 }
9572
9573#ifdef FEAT_RELTIME
9574 /* Set the time limit, if there is one. */
9575 profile_setlimit(time_limit, &tm);
9576#endif
9577
9578 /*
9579 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9580 * Check to make sure only those flags are set.
9581 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9582 * flags cannot be set. Check for that condition also.
9583 */
9584 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9585 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9586 {
9587 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9588 goto theend;
9589 }
9590
9591 pos = save_cursor = curwin->w_cursor;
9592 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009593 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009594 if (subpatnum != FAIL)
9595 {
9596 if (flags & SP_SUBPAT)
9597 retval = subpatnum;
9598 else
9599 retval = pos.lnum;
9600 if (flags & SP_SETPCMARK)
9601 setpcmark();
9602 curwin->w_cursor = pos;
9603 if (match_pos != NULL)
9604 {
9605 /* Store the match cursor position */
9606 match_pos->lnum = pos.lnum;
9607 match_pos->col = pos.col + 1;
9608 }
9609 /* "/$" will put the cursor after the end of the line, may need to
9610 * correct that here */
9611 check_cursor();
9612 }
9613
9614 /* If 'n' flag is used: restore cursor position. */
9615 if (flags & SP_NOMOVE)
9616 curwin->w_cursor = save_cursor;
9617 else
9618 curwin->w_set_curswant = TRUE;
9619theend:
9620 p_ws = save_p_ws;
9621
9622 return retval;
9623}
9624
9625#ifdef FEAT_FLOAT
9626
9627/*
9628 * round() is not in C90, use ceil() or floor() instead.
9629 */
9630 float_T
9631vim_round(float_T f)
9632{
9633 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9634}
9635
9636/*
9637 * "round({float})" function
9638 */
9639 static void
9640f_round(typval_T *argvars, typval_T *rettv)
9641{
9642 float_T f = 0.0;
9643
9644 rettv->v_type = VAR_FLOAT;
9645 if (get_float_arg(argvars, &f) == OK)
9646 rettv->vval.v_float = vim_round(f);
9647 else
9648 rettv->vval.v_float = 0.0;
9649}
9650#endif
9651
9652/*
9653 * "screenattr()" function
9654 */
9655 static void
9656f_screenattr(typval_T *argvars, typval_T *rettv)
9657{
9658 int row;
9659 int col;
9660 int c;
9661
9662 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9663 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9664 if (row < 0 || row >= screen_Rows
9665 || col < 0 || col >= screen_Columns)
9666 c = -1;
9667 else
9668 c = ScreenAttrs[LineOffset[row] + col];
9669 rettv->vval.v_number = c;
9670}
9671
9672/*
9673 * "screenchar()" function
9674 */
9675 static void
9676f_screenchar(typval_T *argvars, typval_T *rettv)
9677{
9678 int row;
9679 int col;
9680 int off;
9681 int c;
9682
9683 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9684 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9685 if (row < 0 || row >= screen_Rows
9686 || col < 0 || col >= screen_Columns)
9687 c = -1;
9688 else
9689 {
9690 off = LineOffset[row] + col;
9691#ifdef FEAT_MBYTE
9692 if (enc_utf8 && ScreenLinesUC[off] != 0)
9693 c = ScreenLinesUC[off];
9694 else
9695#endif
9696 c = ScreenLines[off];
9697 }
9698 rettv->vval.v_number = c;
9699}
9700
9701/*
9702 * "screencol()" function
9703 *
9704 * First column is 1 to be consistent with virtcol().
9705 */
9706 static void
9707f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9708{
9709 rettv->vval.v_number = screen_screencol() + 1;
9710}
9711
9712/*
9713 * "screenrow()" function
9714 */
9715 static void
9716f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9717{
9718 rettv->vval.v_number = screen_screenrow() + 1;
9719}
9720
9721/*
9722 * "search()" function
9723 */
9724 static void
9725f_search(typval_T *argvars, typval_T *rettv)
9726{
9727 int flags = 0;
9728
9729 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9730}
9731
9732/*
9733 * "searchdecl()" function
9734 */
9735 static void
9736f_searchdecl(typval_T *argvars, typval_T *rettv)
9737{
9738 int locally = 1;
9739 int thisblock = 0;
9740 int error = FALSE;
9741 char_u *name;
9742
9743 rettv->vval.v_number = 1; /* default: FAIL */
9744
9745 name = get_tv_string_chk(&argvars[0]);
9746 if (argvars[1].v_type != VAR_UNKNOWN)
9747 {
9748 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9749 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9750 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9751 }
9752 if (!error && name != NULL)
9753 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9754 locally, thisblock, SEARCH_KEEP) == FAIL;
9755}
9756
9757/*
9758 * Used by searchpair() and searchpairpos()
9759 */
9760 static int
9761searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9762{
9763 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009764 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009765 int save_p_ws = p_ws;
9766 int dir;
9767 int flags = 0;
9768 char_u nbuf1[NUMBUFLEN];
9769 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009770 int retval = 0; /* default: FAIL */
9771 long lnum_stop = 0;
9772 long time_limit = 0;
9773
9774 /* Get the three pattern arguments: start, middle, end. */
9775 spat = get_tv_string_chk(&argvars[0]);
9776 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9777 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9778 if (spat == NULL || mpat == NULL || epat == NULL)
9779 goto theend; /* type error */
9780
9781 /* Handle the optional fourth argument: flags */
9782 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9783 if (dir == 0)
9784 goto theend;
9785
9786 /* Don't accept SP_END or SP_SUBPAT.
9787 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9788 */
9789 if ((flags & (SP_END | SP_SUBPAT)) != 0
9790 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9791 {
9792 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9793 goto theend;
9794 }
9795
9796 /* Using 'r' implies 'W', otherwise it doesn't work. */
9797 if (flags & SP_REPEAT)
9798 p_ws = FALSE;
9799
9800 /* Optional fifth argument: skip expression */
9801 if (argvars[3].v_type == VAR_UNKNOWN
9802 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009803 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009804 else
9805 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009806 skip = &argvars[4];
9807 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9808 && skip->v_type != VAR_STRING)
9809 {
9810 /* Type error */
9811 goto theend;
9812 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009813 if (argvars[5].v_type != VAR_UNKNOWN)
9814 {
9815 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9816 if (lnum_stop < 0)
9817 goto theend;
9818#ifdef FEAT_RELTIME
9819 if (argvars[6].v_type != VAR_UNKNOWN)
9820 {
9821 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9822 if (time_limit < 0)
9823 goto theend;
9824 }
9825#endif
9826 }
9827 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009828
9829 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9830 match_pos, lnum_stop, time_limit);
9831
9832theend:
9833 p_ws = save_p_ws;
9834
9835 return retval;
9836}
9837
9838/*
9839 * "searchpair()" function
9840 */
9841 static void
9842f_searchpair(typval_T *argvars, typval_T *rettv)
9843{
9844 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9845}
9846
9847/*
9848 * "searchpairpos()" function
9849 */
9850 static void
9851f_searchpairpos(typval_T *argvars, typval_T *rettv)
9852{
9853 pos_T match_pos;
9854 int lnum = 0;
9855 int col = 0;
9856
9857 if (rettv_list_alloc(rettv) == FAIL)
9858 return;
9859
9860 if (searchpair_cmn(argvars, &match_pos) > 0)
9861 {
9862 lnum = match_pos.lnum;
9863 col = match_pos.col;
9864 }
9865
9866 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9867 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9868}
9869
9870/*
9871 * Search for a start/middle/end thing.
9872 * Used by searchpair(), see its documentation for the details.
9873 * Returns 0 or -1 for no match,
9874 */
9875 long
9876do_searchpair(
9877 char_u *spat, /* start pattern */
9878 char_u *mpat, /* middle pattern */
9879 char_u *epat, /* end pattern */
9880 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009881 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009882 int flags, /* SP_SETPCMARK and other SP_ values */
9883 pos_T *match_pos,
9884 linenr_T lnum_stop, /* stop at this line if not zero */
9885 long time_limit UNUSED) /* stop after this many msec */
9886{
9887 char_u *save_cpo;
9888 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9889 long retval = 0;
9890 pos_T pos;
9891 pos_T firstpos;
9892 pos_T foundpos;
9893 pos_T save_cursor;
9894 pos_T save_pos;
9895 int n;
9896 int r;
9897 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009898 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009899 int err;
9900 int options = SEARCH_KEEP;
9901 proftime_T tm;
9902
9903 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9904 save_cpo = p_cpo;
9905 p_cpo = empty_option;
9906
9907#ifdef FEAT_RELTIME
9908 /* Set the time limit, if there is one. */
9909 profile_setlimit(time_limit, &tm);
9910#endif
9911
9912 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9913 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009914 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9915 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009916 if (pat2 == NULL || pat3 == NULL)
9917 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009918 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009919 if (*mpat == NUL)
9920 STRCPY(pat3, pat2);
9921 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009922 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009923 spat, epat, mpat);
9924 if (flags & SP_START)
9925 options |= SEARCH_START;
9926
Bram Moolenaar48570482017-10-30 21:48:41 +01009927 if (skip != NULL)
9928 {
9929 /* Empty string means to not use the skip expression. */
9930 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9931 use_skip = skip->vval.v_string != NULL
9932 && *skip->vval.v_string != NUL;
9933 }
9934
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009935 save_cursor = curwin->w_cursor;
9936 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009937 CLEAR_POS(&firstpos);
9938 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009939 pat = pat3;
9940 for (;;)
9941 {
9942 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009943 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009944 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009945 /* didn't find it or found the first match again: FAIL */
9946 break;
9947
9948 if (firstpos.lnum == 0)
9949 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009950 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009951 {
9952 /* Found the same position again. Can happen with a pattern that
9953 * has "\zs" at the end and searching backwards. Advance one
9954 * character and try again. */
9955 if (dir == BACKWARD)
9956 decl(&pos);
9957 else
9958 incl(&pos);
9959 }
9960 foundpos = pos;
9961
9962 /* clear the start flag to avoid getting stuck here */
9963 options &= ~SEARCH_START;
9964
9965 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009966 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009967 {
9968 save_pos = curwin->w_cursor;
9969 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009970 err = FALSE;
9971 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009972 curwin->w_cursor = save_pos;
9973 if (err)
9974 {
9975 /* Evaluating {skip} caused an error, break here. */
9976 curwin->w_cursor = save_cursor;
9977 retval = -1;
9978 break;
9979 }
9980 if (r)
9981 continue;
9982 }
9983
9984 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9985 {
9986 /* Found end when searching backwards or start when searching
9987 * forward: nested pair. */
9988 ++nest;
9989 pat = pat2; /* nested, don't search for middle */
9990 }
9991 else
9992 {
9993 /* Found end when searching forward or start when searching
9994 * backward: end of (nested) pair; or found middle in outer pair. */
9995 if (--nest == 1)
9996 pat = pat3; /* outer level, search for middle */
9997 }
9998
9999 if (nest == 0)
10000 {
10001 /* Found the match: return matchcount or line number. */
10002 if (flags & SP_RETCOUNT)
10003 ++retval;
10004 else
10005 retval = pos.lnum;
10006 if (flags & SP_SETPCMARK)
10007 setpcmark();
10008 curwin->w_cursor = pos;
10009 if (!(flags & SP_REPEAT))
10010 break;
10011 nest = 1; /* search for next unmatched */
10012 }
10013 }
10014
10015 if (match_pos != NULL)
10016 {
10017 /* Store the match cursor position */
10018 match_pos->lnum = curwin->w_cursor.lnum;
10019 match_pos->col = curwin->w_cursor.col + 1;
10020 }
10021
10022 /* If 'n' flag is used or search failed: restore cursor position. */
10023 if ((flags & SP_NOMOVE) || retval == 0)
10024 curwin->w_cursor = save_cursor;
10025
10026theend:
10027 vim_free(pat2);
10028 vim_free(pat3);
10029 if (p_cpo == empty_option)
10030 p_cpo = save_cpo;
10031 else
10032 /* Darn, evaluating the {skip} expression changed the value. */
10033 free_string_option(save_cpo);
10034
10035 return retval;
10036}
10037
10038/*
10039 * "searchpos()" function
10040 */
10041 static void
10042f_searchpos(typval_T *argvars, typval_T *rettv)
10043{
10044 pos_T match_pos;
10045 int lnum = 0;
10046 int col = 0;
10047 int n;
10048 int flags = 0;
10049
10050 if (rettv_list_alloc(rettv) == FAIL)
10051 return;
10052
10053 n = search_cmn(argvars, &match_pos, &flags);
10054 if (n > 0)
10055 {
10056 lnum = match_pos.lnum;
10057 col = match_pos.col;
10058 }
10059
10060 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10061 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10062 if (flags & SP_SUBPAT)
10063 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10064}
10065
10066 static void
10067f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10068{
10069#ifdef FEAT_CLIENTSERVER
10070 char_u buf[NUMBUFLEN];
10071 char_u *server = get_tv_string_chk(&argvars[0]);
10072 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10073
10074 rettv->vval.v_number = -1;
10075 if (server == NULL || reply == NULL)
10076 return;
10077 if (check_restricted() || check_secure())
10078 return;
10079# ifdef FEAT_X11
10080 if (check_connection() == FAIL)
10081 return;
10082# endif
10083
10084 if (serverSendReply(server, reply) < 0)
10085 {
10086 EMSG(_("E258: Unable to send to client"));
10087 return;
10088 }
10089 rettv->vval.v_number = 0;
10090#else
10091 rettv->vval.v_number = -1;
10092#endif
10093}
10094
10095 static void
10096f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10097{
10098 char_u *r = NULL;
10099
10100#ifdef FEAT_CLIENTSERVER
10101# ifdef WIN32
10102 r = serverGetVimNames();
10103# else
10104 make_connection();
10105 if (X_DISPLAY != NULL)
10106 r = serverGetVimNames(X_DISPLAY);
10107# endif
10108#endif
10109 rettv->v_type = VAR_STRING;
10110 rettv->vval.v_string = r;
10111}
10112
10113/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010114 * Set line or list of lines in buffer "buf".
10115 */
10116 static void
10117set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
10118{
10119 char_u *line = NULL;
10120 list_T *l = NULL;
10121 listitem_T *li = NULL;
10122 long added = 0;
10123 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010124 buf_T *curbuf_save = NULL;
10125 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010126 int is_curbuf = buf == curbuf;
10127
Bram Moolenaar9d954202017-09-04 20:34:19 +020010128 /* When using the current buffer ml_mfp will be set if needed. Useful when
10129 * setline() is used on startup. For other buffers the buffer must be
10130 * loaded. */
10131 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010132 {
10133 rettv->vval.v_number = 1; /* FAIL */
10134 return;
10135 }
10136
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010137 if (!is_curbuf)
10138 {
10139 wininfo_T *wip;
10140
10141 curbuf_save = curbuf;
10142 curwin_save = curwin;
10143 curbuf = buf;
10144 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10145 {
10146 if (wip->wi_win != NULL)
10147 {
10148 curwin = wip->wi_win;
10149 break;
10150 }
10151 }
10152 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010153
10154 lcount = curbuf->b_ml.ml_line_count;
10155
10156 if (lines->v_type == VAR_LIST)
10157 {
10158 l = lines->vval.v_list;
10159 li = l->lv_first;
10160 }
10161 else
10162 line = get_tv_string_chk(lines);
10163
10164 /* default result is zero == OK */
10165 for (;;)
10166 {
10167 if (l != NULL)
10168 {
10169 /* list argument, get next string */
10170 if (li == NULL)
10171 break;
10172 line = get_tv_string_chk(&li->li_tv);
10173 li = li->li_next;
10174 }
10175
10176 rettv->vval.v_number = 1; /* FAIL */
10177 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10178 break;
10179
10180 /* When coming here from Insert mode, sync undo, so that this can be
10181 * undone separately from what was previously inserted. */
10182 if (u_sync_once == 2)
10183 {
10184 u_sync_once = 1; /* notify that u_sync() was called */
10185 u_sync(TRUE);
10186 }
10187
10188 if (lnum <= curbuf->b_ml.ml_line_count)
10189 {
10190 /* existing line, replace it */
10191 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10192 {
10193 changed_bytes(lnum, 0);
10194 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10195 check_cursor_col();
10196 rettv->vval.v_number = 0; /* OK */
10197 }
10198 }
10199 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10200 {
10201 /* lnum is one past the last line, append the line */
10202 ++added;
10203 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10204 rettv->vval.v_number = 0; /* OK */
10205 }
10206
10207 if (l == NULL) /* only one string argument */
10208 break;
10209 ++lnum;
10210 }
10211
10212 if (added > 0)
10213 appended_lines_mark(lcount, added);
10214
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010215 if (!is_curbuf)
10216 {
10217 curbuf = curbuf_save;
10218 curwin = curwin_save;
10219 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010220}
10221
10222/*
10223 * "setbufline()" function
10224 */
10225 static void
10226f_setbufline(argvars, rettv)
10227 typval_T *argvars;
10228 typval_T *rettv;
10229{
10230 linenr_T lnum;
10231 buf_T *buf;
10232
10233 buf = get_buf_tv(&argvars[0], FALSE);
10234 if (buf == NULL)
10235 rettv->vval.v_number = 1; /* FAIL */
10236 else
10237 {
10238 lnum = get_tv_lnum_buf(&argvars[1], buf);
10239
10240 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10241 }
10242}
10243
10244/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010245 * "setbufvar()" function
10246 */
10247 static void
10248f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10249{
10250 buf_T *buf;
10251 char_u *varname, *bufvarname;
10252 typval_T *varp;
10253 char_u nbuf[NUMBUFLEN];
10254
10255 if (check_restricted() || check_secure())
10256 return;
10257 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10258 varname = get_tv_string_chk(&argvars[1]);
10259 buf = get_buf_tv(&argvars[0], FALSE);
10260 varp = &argvars[2];
10261
10262 if (buf != NULL && varname != NULL && varp != NULL)
10263 {
10264 if (*varname == '&')
10265 {
10266 long numval;
10267 char_u *strval;
10268 int error = FALSE;
10269 aco_save_T aco;
10270
10271 /* set curbuf to be our buf, temporarily */
10272 aucmd_prepbuf(&aco, buf);
10273
10274 ++varname;
10275 numval = (long)get_tv_number_chk(varp, &error);
10276 strval = get_tv_string_buf_chk(varp, nbuf);
10277 if (!error && strval != NULL)
10278 set_option_value(varname, numval, strval, OPT_LOCAL);
10279
10280 /* reset notion of buffer */
10281 aucmd_restbuf(&aco);
10282 }
10283 else
10284 {
10285 buf_T *save_curbuf = curbuf;
10286
10287 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10288 if (bufvarname != NULL)
10289 {
10290 curbuf = buf;
10291 STRCPY(bufvarname, "b:");
10292 STRCPY(bufvarname + 2, varname);
10293 set_var(bufvarname, varp, TRUE);
10294 vim_free(bufvarname);
10295 curbuf = save_curbuf;
10296 }
10297 }
10298 }
10299}
10300
10301 static void
10302f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10303{
10304 dict_T *d;
10305 dictitem_T *di;
10306 char_u *csearch;
10307
10308 if (argvars[0].v_type != VAR_DICT)
10309 {
10310 EMSG(_(e_dictreq));
10311 return;
10312 }
10313
10314 if ((d = argvars[0].vval.v_dict) != NULL)
10315 {
10316 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10317 if (csearch != NULL)
10318 {
10319#ifdef FEAT_MBYTE
10320 if (enc_utf8)
10321 {
10322 int pcc[MAX_MCO];
10323 int c = utfc_ptr2char(csearch, pcc);
10324
10325 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10326 }
10327 else
10328#endif
10329 set_last_csearch(PTR2CHAR(csearch),
10330 csearch, MB_PTR2LEN(csearch));
10331 }
10332
10333 di = dict_find(d, (char_u *)"forward", -1);
10334 if (di != NULL)
10335 set_csearch_direction((int)get_tv_number(&di->di_tv)
10336 ? FORWARD : BACKWARD);
10337
10338 di = dict_find(d, (char_u *)"until", -1);
10339 if (di != NULL)
10340 set_csearch_until(!!get_tv_number(&di->di_tv));
10341 }
10342}
10343
10344/*
10345 * "setcmdpos()" function
10346 */
10347 static void
10348f_setcmdpos(typval_T *argvars, typval_T *rettv)
10349{
10350 int pos = (int)get_tv_number(&argvars[0]) - 1;
10351
10352 if (pos >= 0)
10353 rettv->vval.v_number = set_cmdline_pos(pos);
10354}
10355
10356/*
10357 * "setfperm({fname}, {mode})" function
10358 */
10359 static void
10360f_setfperm(typval_T *argvars, typval_T *rettv)
10361{
10362 char_u *fname;
10363 char_u modebuf[NUMBUFLEN];
10364 char_u *mode_str;
10365 int i;
10366 int mask;
10367 int mode = 0;
10368
10369 rettv->vval.v_number = 0;
10370 fname = get_tv_string_chk(&argvars[0]);
10371 if (fname == NULL)
10372 return;
10373 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10374 if (mode_str == NULL)
10375 return;
10376 if (STRLEN(mode_str) != 9)
10377 {
10378 EMSG2(_(e_invarg2), mode_str);
10379 return;
10380 }
10381
10382 mask = 1;
10383 for (i = 8; i >= 0; --i)
10384 {
10385 if (mode_str[i] != '-')
10386 mode |= mask;
10387 mask = mask << 1;
10388 }
10389 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10390}
10391
10392/*
10393 * "setline()" function
10394 */
10395 static void
10396f_setline(typval_T *argvars, typval_T *rettv)
10397{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010398 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010399
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010400 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010401}
10402
Bram Moolenaard823fa92016-08-12 16:29:27 +020010403static 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 +020010404
10405/*
10406 * Used by "setqflist()" and "setloclist()" functions
10407 */
10408 static void
10409set_qf_ll_list(
10410 win_T *wp UNUSED,
10411 typval_T *list_arg UNUSED,
10412 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010413 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010414 typval_T *rettv)
10415{
10416#ifdef FEAT_QUICKFIX
10417 static char *e_invact = N_("E927: Invalid action: '%s'");
10418 char_u *act;
10419 int action = 0;
10420#endif
10421
10422 rettv->vval.v_number = -1;
10423
10424#ifdef FEAT_QUICKFIX
10425 if (list_arg->v_type != VAR_LIST)
10426 EMSG(_(e_listreq));
10427 else
10428 {
10429 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010430 dict_T *d = NULL;
10431 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010432
10433 if (action_arg->v_type == VAR_STRING)
10434 {
10435 act = get_tv_string_chk(action_arg);
10436 if (act == NULL)
10437 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010438 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10439 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010440 action = *act;
10441 else
10442 EMSG2(_(e_invact), act);
10443 }
10444 else if (action_arg->v_type == VAR_UNKNOWN)
10445 action = ' ';
10446 else
10447 EMSG(_(e_stringreq));
10448
Bram Moolenaard823fa92016-08-12 16:29:27 +020010449 if (action_arg->v_type != VAR_UNKNOWN
10450 && what_arg->v_type != VAR_UNKNOWN)
10451 {
10452 if (what_arg->v_type == VAR_DICT)
10453 d = what_arg->vval.v_dict;
10454 else
10455 {
10456 EMSG(_(e_dictreq));
10457 valid_dict = FALSE;
10458 }
10459 }
10460
10461 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10462 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010463 rettv->vval.v_number = 0;
10464 }
10465#endif
10466}
10467
10468/*
10469 * "setloclist()" function
10470 */
10471 static void
10472f_setloclist(typval_T *argvars, typval_T *rettv)
10473{
10474 win_T *win;
10475
10476 rettv->vval.v_number = -1;
10477
10478 win = find_win_by_nr(&argvars[0], NULL);
10479 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010480 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010481}
10482
10483/*
10484 * "setmatches()" function
10485 */
10486 static void
10487f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10488{
10489#ifdef FEAT_SEARCH_EXTRA
10490 list_T *l;
10491 listitem_T *li;
10492 dict_T *d;
10493 list_T *s = NULL;
10494
10495 rettv->vval.v_number = -1;
10496 if (argvars[0].v_type != VAR_LIST)
10497 {
10498 EMSG(_(e_listreq));
10499 return;
10500 }
10501 if ((l = argvars[0].vval.v_list) != NULL)
10502 {
10503
10504 /* To some extent make sure that we are dealing with a list from
10505 * "getmatches()". */
10506 li = l->lv_first;
10507 while (li != NULL)
10508 {
10509 if (li->li_tv.v_type != VAR_DICT
10510 || (d = li->li_tv.vval.v_dict) == NULL)
10511 {
10512 EMSG(_(e_invarg));
10513 return;
10514 }
10515 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10516 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10517 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10518 && dict_find(d, (char_u *)"priority", -1) != NULL
10519 && dict_find(d, (char_u *)"id", -1) != NULL))
10520 {
10521 EMSG(_(e_invarg));
10522 return;
10523 }
10524 li = li->li_next;
10525 }
10526
10527 clear_matches(curwin);
10528 li = l->lv_first;
10529 while (li != NULL)
10530 {
10531 int i = 0;
10532 char_u buf[5];
10533 dictitem_T *di;
10534 char_u *group;
10535 int priority;
10536 int id;
10537 char_u *conceal;
10538
10539 d = li->li_tv.vval.v_dict;
10540 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10541 {
10542 if (s == NULL)
10543 {
10544 s = list_alloc();
10545 if (s == NULL)
10546 return;
10547 }
10548
10549 /* match from matchaddpos() */
10550 for (i = 1; i < 9; i++)
10551 {
10552 sprintf((char *)buf, (char *)"pos%d", i);
10553 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10554 {
10555 if (di->di_tv.v_type != VAR_LIST)
10556 return;
10557
10558 list_append_tv(s, &di->di_tv);
10559 s->lv_refcount++;
10560 }
10561 else
10562 break;
10563 }
10564 }
10565
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010566 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010567 priority = (int)get_dict_number(d, (char_u *)"priority");
10568 id = (int)get_dict_number(d, (char_u *)"id");
10569 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010570 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010571 : NULL;
10572 if (i == 0)
10573 {
10574 match_add(curwin, group,
10575 get_dict_string(d, (char_u *)"pattern", FALSE),
10576 priority, id, NULL, conceal);
10577 }
10578 else
10579 {
10580 match_add(curwin, group, NULL, priority, id, s, conceal);
10581 list_unref(s);
10582 s = NULL;
10583 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010584 vim_free(group);
10585 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010586
10587 li = li->li_next;
10588 }
10589 rettv->vval.v_number = 0;
10590 }
10591#endif
10592}
10593
10594/*
10595 * "setpos()" function
10596 */
10597 static void
10598f_setpos(typval_T *argvars, typval_T *rettv)
10599{
10600 pos_T pos;
10601 int fnum;
10602 char_u *name;
10603 colnr_T curswant = -1;
10604
10605 rettv->vval.v_number = -1;
10606 name = get_tv_string_chk(argvars);
10607 if (name != NULL)
10608 {
10609 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10610 {
10611 if (--pos.col < 0)
10612 pos.col = 0;
10613 if (name[0] == '.' && name[1] == NUL)
10614 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010615 /* set cursor; "fnum" is ignored */
10616 curwin->w_cursor = pos;
10617 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010618 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010619 curwin->w_curswant = curswant - 1;
10620 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010621 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010622 check_cursor();
10623 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010624 }
10625 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10626 {
10627 /* set mark */
10628 if (setmark_pos(name[1], &pos, fnum) == OK)
10629 rettv->vval.v_number = 0;
10630 }
10631 else
10632 EMSG(_(e_invarg));
10633 }
10634 }
10635}
10636
10637/*
10638 * "setqflist()" function
10639 */
10640 static void
10641f_setqflist(typval_T *argvars, typval_T *rettv)
10642{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010643 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010644}
10645
10646/*
10647 * "setreg()" function
10648 */
10649 static void
10650f_setreg(typval_T *argvars, typval_T *rettv)
10651{
10652 int regname;
10653 char_u *strregname;
10654 char_u *stropt;
10655 char_u *strval;
10656 int append;
10657 char_u yank_type;
10658 long block_len;
10659
10660 block_len = -1;
10661 yank_type = MAUTO;
10662 append = FALSE;
10663
10664 strregname = get_tv_string_chk(argvars);
10665 rettv->vval.v_number = 1; /* FAIL is default */
10666
10667 if (strregname == NULL)
10668 return; /* type error; errmsg already given */
10669 regname = *strregname;
10670 if (regname == 0 || regname == '@')
10671 regname = '"';
10672
10673 if (argvars[2].v_type != VAR_UNKNOWN)
10674 {
10675 stropt = get_tv_string_chk(&argvars[2]);
10676 if (stropt == NULL)
10677 return; /* type error */
10678 for (; *stropt != NUL; ++stropt)
10679 switch (*stropt)
10680 {
10681 case 'a': case 'A': /* append */
10682 append = TRUE;
10683 break;
10684 case 'v': case 'c': /* character-wise selection */
10685 yank_type = MCHAR;
10686 break;
10687 case 'V': case 'l': /* line-wise selection */
10688 yank_type = MLINE;
10689 break;
10690 case 'b': case Ctrl_V: /* block-wise selection */
10691 yank_type = MBLOCK;
10692 if (VIM_ISDIGIT(stropt[1]))
10693 {
10694 ++stropt;
10695 block_len = getdigits(&stropt) - 1;
10696 --stropt;
10697 }
10698 break;
10699 }
10700 }
10701
10702 if (argvars[1].v_type == VAR_LIST)
10703 {
10704 char_u **lstval;
10705 char_u **allocval;
10706 char_u buf[NUMBUFLEN];
10707 char_u **curval;
10708 char_u **curallocval;
10709 list_T *ll = argvars[1].vval.v_list;
10710 listitem_T *li;
10711 int len;
10712
10713 /* If the list is NULL handle like an empty list. */
10714 len = ll == NULL ? 0 : ll->lv_len;
10715
10716 /* First half: use for pointers to result lines; second half: use for
10717 * pointers to allocated copies. */
10718 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10719 if (lstval == NULL)
10720 return;
10721 curval = lstval;
10722 allocval = lstval + len + 2;
10723 curallocval = allocval;
10724
10725 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10726 li = li->li_next)
10727 {
10728 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10729 if (strval == NULL)
10730 goto free_lstval;
10731 if (strval == buf)
10732 {
10733 /* Need to make a copy, next get_tv_string_buf_chk() will
10734 * overwrite the string. */
10735 strval = vim_strsave(buf);
10736 if (strval == NULL)
10737 goto free_lstval;
10738 *curallocval++ = strval;
10739 }
10740 *curval++ = strval;
10741 }
10742 *curval++ = NULL;
10743
10744 write_reg_contents_lst(regname, lstval, -1,
10745 append, yank_type, block_len);
10746free_lstval:
10747 while (curallocval > allocval)
10748 vim_free(*--curallocval);
10749 vim_free(lstval);
10750 }
10751 else
10752 {
10753 strval = get_tv_string_chk(&argvars[1]);
10754 if (strval == NULL)
10755 return;
10756 write_reg_contents_ex(regname, strval, -1,
10757 append, yank_type, block_len);
10758 }
10759 rettv->vval.v_number = 0;
10760}
10761
10762/*
10763 * "settabvar()" function
10764 */
10765 static void
10766f_settabvar(typval_T *argvars, typval_T *rettv)
10767{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010768 tabpage_T *save_curtab;
10769 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010770 char_u *varname, *tabvarname;
10771 typval_T *varp;
10772
10773 rettv->vval.v_number = 0;
10774
10775 if (check_restricted() || check_secure())
10776 return;
10777
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010778 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010779 varname = get_tv_string_chk(&argvars[1]);
10780 varp = &argvars[2];
10781
Bram Moolenaar4033c552017-09-16 20:54:51 +020010782 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010783 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010784 save_curtab = curtab;
10785 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010786
10787 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10788 if (tabvarname != NULL)
10789 {
10790 STRCPY(tabvarname, "t:");
10791 STRCPY(tabvarname + 2, varname);
10792 set_var(tabvarname, varp, TRUE);
10793 vim_free(tabvarname);
10794 }
10795
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010796 /* Restore current tabpage */
10797 if (valid_tabpage(save_curtab))
10798 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010799 }
10800}
10801
10802/*
10803 * "settabwinvar()" function
10804 */
10805 static void
10806f_settabwinvar(typval_T *argvars, typval_T *rettv)
10807{
10808 setwinvar(argvars, rettv, 1);
10809}
10810
10811/*
10812 * "setwinvar()" function
10813 */
10814 static void
10815f_setwinvar(typval_T *argvars, typval_T *rettv)
10816{
10817 setwinvar(argvars, rettv, 0);
10818}
10819
10820#ifdef FEAT_CRYPT
10821/*
10822 * "sha256({string})" function
10823 */
10824 static void
10825f_sha256(typval_T *argvars, typval_T *rettv)
10826{
10827 char_u *p;
10828
10829 p = get_tv_string(&argvars[0]);
10830 rettv->vval.v_string = vim_strsave(
10831 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10832 rettv->v_type = VAR_STRING;
10833}
10834#endif /* FEAT_CRYPT */
10835
10836/*
10837 * "shellescape({string})" function
10838 */
10839 static void
10840f_shellescape(typval_T *argvars, typval_T *rettv)
10841{
Bram Moolenaar20615522017-06-05 18:46:26 +020010842 int do_special = non_zero_arg(&argvars[1]);
10843
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010844 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010845 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010846 rettv->v_type = VAR_STRING;
10847}
10848
10849/*
10850 * shiftwidth() function
10851 */
10852 static void
10853f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10854{
10855 rettv->vval.v_number = get_sw_value(curbuf);
10856}
10857
10858/*
10859 * "simplify()" function
10860 */
10861 static void
10862f_simplify(typval_T *argvars, typval_T *rettv)
10863{
10864 char_u *p;
10865
10866 p = get_tv_string(&argvars[0]);
10867 rettv->vval.v_string = vim_strsave(p);
10868 simplify_filename(rettv->vval.v_string); /* simplify in place */
10869 rettv->v_type = VAR_STRING;
10870}
10871
10872#ifdef FEAT_FLOAT
10873/*
10874 * "sin()" function
10875 */
10876 static void
10877f_sin(typval_T *argvars, typval_T *rettv)
10878{
10879 float_T f = 0.0;
10880
10881 rettv->v_type = VAR_FLOAT;
10882 if (get_float_arg(argvars, &f) == OK)
10883 rettv->vval.v_float = sin(f);
10884 else
10885 rettv->vval.v_float = 0.0;
10886}
10887
10888/*
10889 * "sinh()" function
10890 */
10891 static void
10892f_sinh(typval_T *argvars, typval_T *rettv)
10893{
10894 float_T f = 0.0;
10895
10896 rettv->v_type = VAR_FLOAT;
10897 if (get_float_arg(argvars, &f) == OK)
10898 rettv->vval.v_float = sinh(f);
10899 else
10900 rettv->vval.v_float = 0.0;
10901}
10902#endif
10903
10904static int
10905#ifdef __BORLANDC__
10906 _RTLENTRYF
10907#endif
10908 item_compare(const void *s1, const void *s2);
10909static int
10910#ifdef __BORLANDC__
10911 _RTLENTRYF
10912#endif
10913 item_compare2(const void *s1, const void *s2);
10914
10915/* struct used in the array that's given to qsort() */
10916typedef struct
10917{
10918 listitem_T *item;
10919 int idx;
10920} sortItem_T;
10921
10922/* struct storing information about current sort */
10923typedef struct
10924{
10925 int item_compare_ic;
10926 int item_compare_numeric;
10927 int item_compare_numbers;
10928#ifdef FEAT_FLOAT
10929 int item_compare_float;
10930#endif
10931 char_u *item_compare_func;
10932 partial_T *item_compare_partial;
10933 dict_T *item_compare_selfdict;
10934 int item_compare_func_err;
10935 int item_compare_keep_zero;
10936} sortinfo_T;
10937static sortinfo_T *sortinfo = NULL;
10938static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10939#define ITEM_COMPARE_FAIL 999
10940
10941/*
10942 * Compare functions for f_sort() and f_uniq() below.
10943 */
10944 static int
10945#ifdef __BORLANDC__
10946_RTLENTRYF
10947#endif
10948item_compare(const void *s1, const void *s2)
10949{
10950 sortItem_T *si1, *si2;
10951 typval_T *tv1, *tv2;
10952 char_u *p1, *p2;
10953 char_u *tofree1 = NULL, *tofree2 = NULL;
10954 int res;
10955 char_u numbuf1[NUMBUFLEN];
10956 char_u numbuf2[NUMBUFLEN];
10957
10958 si1 = (sortItem_T *)s1;
10959 si2 = (sortItem_T *)s2;
10960 tv1 = &si1->item->li_tv;
10961 tv2 = &si2->item->li_tv;
10962
10963 if (sortinfo->item_compare_numbers)
10964 {
10965 varnumber_T v1 = get_tv_number(tv1);
10966 varnumber_T v2 = get_tv_number(tv2);
10967
10968 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10969 }
10970
10971#ifdef FEAT_FLOAT
10972 if (sortinfo->item_compare_float)
10973 {
10974 float_T v1 = get_tv_float(tv1);
10975 float_T v2 = get_tv_float(tv2);
10976
10977 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10978 }
10979#endif
10980
10981 /* tv2string() puts quotes around a string and allocates memory. Don't do
10982 * that for string variables. Use a single quote when comparing with a
10983 * non-string to do what the docs promise. */
10984 if (tv1->v_type == VAR_STRING)
10985 {
10986 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10987 p1 = (char_u *)"'";
10988 else
10989 p1 = tv1->vval.v_string;
10990 }
10991 else
10992 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10993 if (tv2->v_type == VAR_STRING)
10994 {
10995 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10996 p2 = (char_u *)"'";
10997 else
10998 p2 = tv2->vval.v_string;
10999 }
11000 else
11001 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11002 if (p1 == NULL)
11003 p1 = (char_u *)"";
11004 if (p2 == NULL)
11005 p2 = (char_u *)"";
11006 if (!sortinfo->item_compare_numeric)
11007 {
11008 if (sortinfo->item_compare_ic)
11009 res = STRICMP(p1, p2);
11010 else
11011 res = STRCMP(p1, p2);
11012 }
11013 else
11014 {
11015 double n1, n2;
11016 n1 = strtod((char *)p1, (char **)&p1);
11017 n2 = strtod((char *)p2, (char **)&p2);
11018 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11019 }
11020
11021 /* When the result would be zero, compare the item indexes. Makes the
11022 * sort stable. */
11023 if (res == 0 && !sortinfo->item_compare_keep_zero)
11024 res = si1->idx > si2->idx ? 1 : -1;
11025
11026 vim_free(tofree1);
11027 vim_free(tofree2);
11028 return res;
11029}
11030
11031 static int
11032#ifdef __BORLANDC__
11033_RTLENTRYF
11034#endif
11035item_compare2(const void *s1, const void *s2)
11036{
11037 sortItem_T *si1, *si2;
11038 int res;
11039 typval_T rettv;
11040 typval_T argv[3];
11041 int dummy;
11042 char_u *func_name;
11043 partial_T *partial = sortinfo->item_compare_partial;
11044
11045 /* shortcut after failure in previous call; compare all items equal */
11046 if (sortinfo->item_compare_func_err)
11047 return 0;
11048
11049 si1 = (sortItem_T *)s1;
11050 si2 = (sortItem_T *)s2;
11051
11052 if (partial == NULL)
11053 func_name = sortinfo->item_compare_func;
11054 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011055 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011056
11057 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11058 * in the copy without changing the original list items. */
11059 copy_tv(&si1->item->li_tv, &argv[0]);
11060 copy_tv(&si2->item->li_tv, &argv[1]);
11061
11062 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11063 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011064 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011065 partial, sortinfo->item_compare_selfdict);
11066 clear_tv(&argv[0]);
11067 clear_tv(&argv[1]);
11068
11069 if (res == FAIL)
11070 res = ITEM_COMPARE_FAIL;
11071 else
11072 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11073 if (sortinfo->item_compare_func_err)
11074 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11075 clear_tv(&rettv);
11076
11077 /* When the result would be zero, compare the pointers themselves. Makes
11078 * the sort stable. */
11079 if (res == 0 && !sortinfo->item_compare_keep_zero)
11080 res = si1->idx > si2->idx ? 1 : -1;
11081
11082 return res;
11083}
11084
11085/*
11086 * "sort({list})" function
11087 */
11088 static void
11089do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11090{
11091 list_T *l;
11092 listitem_T *li;
11093 sortItem_T *ptrs;
11094 sortinfo_T *old_sortinfo;
11095 sortinfo_T info;
11096 long len;
11097 long i;
11098
11099 /* Pointer to current info struct used in compare function. Save and
11100 * restore the current one for nested calls. */
11101 old_sortinfo = sortinfo;
11102 sortinfo = &info;
11103
11104 if (argvars[0].v_type != VAR_LIST)
11105 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11106 else
11107 {
11108 l = argvars[0].vval.v_list;
11109 if (l == NULL || tv_check_lock(l->lv_lock,
11110 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11111 TRUE))
11112 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011113 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011114
11115 len = list_len(l);
11116 if (len <= 1)
11117 goto theend; /* short list sorts pretty quickly */
11118
11119 info.item_compare_ic = FALSE;
11120 info.item_compare_numeric = FALSE;
11121 info.item_compare_numbers = FALSE;
11122#ifdef FEAT_FLOAT
11123 info.item_compare_float = FALSE;
11124#endif
11125 info.item_compare_func = NULL;
11126 info.item_compare_partial = NULL;
11127 info.item_compare_selfdict = NULL;
11128 if (argvars[1].v_type != VAR_UNKNOWN)
11129 {
11130 /* optional second argument: {func} */
11131 if (argvars[1].v_type == VAR_FUNC)
11132 info.item_compare_func = argvars[1].vval.v_string;
11133 else if (argvars[1].v_type == VAR_PARTIAL)
11134 info.item_compare_partial = argvars[1].vval.v_partial;
11135 else
11136 {
11137 int error = FALSE;
11138
11139 i = (long)get_tv_number_chk(&argvars[1], &error);
11140 if (error)
11141 goto theend; /* type error; errmsg already given */
11142 if (i == 1)
11143 info.item_compare_ic = TRUE;
11144 else if (argvars[1].v_type != VAR_NUMBER)
11145 info.item_compare_func = get_tv_string(&argvars[1]);
11146 else if (i != 0)
11147 {
11148 EMSG(_(e_invarg));
11149 goto theend;
11150 }
11151 if (info.item_compare_func != NULL)
11152 {
11153 if (*info.item_compare_func == NUL)
11154 {
11155 /* empty string means default sort */
11156 info.item_compare_func = NULL;
11157 }
11158 else if (STRCMP(info.item_compare_func, "n") == 0)
11159 {
11160 info.item_compare_func = NULL;
11161 info.item_compare_numeric = TRUE;
11162 }
11163 else if (STRCMP(info.item_compare_func, "N") == 0)
11164 {
11165 info.item_compare_func = NULL;
11166 info.item_compare_numbers = TRUE;
11167 }
11168#ifdef FEAT_FLOAT
11169 else if (STRCMP(info.item_compare_func, "f") == 0)
11170 {
11171 info.item_compare_func = NULL;
11172 info.item_compare_float = TRUE;
11173 }
11174#endif
11175 else if (STRCMP(info.item_compare_func, "i") == 0)
11176 {
11177 info.item_compare_func = NULL;
11178 info.item_compare_ic = TRUE;
11179 }
11180 }
11181 }
11182
11183 if (argvars[2].v_type != VAR_UNKNOWN)
11184 {
11185 /* optional third argument: {dict} */
11186 if (argvars[2].v_type != VAR_DICT)
11187 {
11188 EMSG(_(e_dictreq));
11189 goto theend;
11190 }
11191 info.item_compare_selfdict = argvars[2].vval.v_dict;
11192 }
11193 }
11194
11195 /* Make an array with each entry pointing to an item in the List. */
11196 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11197 if (ptrs == NULL)
11198 goto theend;
11199
11200 i = 0;
11201 if (sort)
11202 {
11203 /* sort(): ptrs will be the list to sort */
11204 for (li = l->lv_first; li != NULL; li = li->li_next)
11205 {
11206 ptrs[i].item = li;
11207 ptrs[i].idx = i;
11208 ++i;
11209 }
11210
11211 info.item_compare_func_err = FALSE;
11212 info.item_compare_keep_zero = FALSE;
11213 /* test the compare function */
11214 if ((info.item_compare_func != NULL
11215 || info.item_compare_partial != NULL)
11216 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11217 == ITEM_COMPARE_FAIL)
11218 EMSG(_("E702: Sort compare function failed"));
11219 else
11220 {
11221 /* Sort the array with item pointers. */
11222 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11223 info.item_compare_func == NULL
11224 && info.item_compare_partial == NULL
11225 ? item_compare : item_compare2);
11226
11227 if (!info.item_compare_func_err)
11228 {
11229 /* Clear the List and append the items in sorted order. */
11230 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11231 l->lv_len = 0;
11232 for (i = 0; i < len; ++i)
11233 list_append(l, ptrs[i].item);
11234 }
11235 }
11236 }
11237 else
11238 {
11239 int (*item_compare_func_ptr)(const void *, const void *);
11240
11241 /* f_uniq(): ptrs will be a stack of items to remove */
11242 info.item_compare_func_err = FALSE;
11243 info.item_compare_keep_zero = TRUE;
11244 item_compare_func_ptr = info.item_compare_func != NULL
11245 || info.item_compare_partial != NULL
11246 ? item_compare2 : item_compare;
11247
11248 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11249 li = li->li_next)
11250 {
11251 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11252 == 0)
11253 ptrs[i++].item = li;
11254 if (info.item_compare_func_err)
11255 {
11256 EMSG(_("E882: Uniq compare function failed"));
11257 break;
11258 }
11259 }
11260
11261 if (!info.item_compare_func_err)
11262 {
11263 while (--i >= 0)
11264 {
11265 li = ptrs[i].item->li_next;
11266 ptrs[i].item->li_next = li->li_next;
11267 if (li->li_next != NULL)
11268 li->li_next->li_prev = ptrs[i].item;
11269 else
11270 l->lv_last = ptrs[i].item;
11271 list_fix_watch(l, li);
11272 listitem_free(li);
11273 l->lv_len--;
11274 }
11275 }
11276 }
11277
11278 vim_free(ptrs);
11279 }
11280theend:
11281 sortinfo = old_sortinfo;
11282}
11283
11284/*
11285 * "sort({list})" function
11286 */
11287 static void
11288f_sort(typval_T *argvars, typval_T *rettv)
11289{
11290 do_sort_uniq(argvars, rettv, TRUE);
11291}
11292
11293/*
11294 * "uniq({list})" function
11295 */
11296 static void
11297f_uniq(typval_T *argvars, typval_T *rettv)
11298{
11299 do_sort_uniq(argvars, rettv, FALSE);
11300}
11301
11302/*
11303 * "soundfold({word})" function
11304 */
11305 static void
11306f_soundfold(typval_T *argvars, typval_T *rettv)
11307{
11308 char_u *s;
11309
11310 rettv->v_type = VAR_STRING;
11311 s = get_tv_string(&argvars[0]);
11312#ifdef FEAT_SPELL
11313 rettv->vval.v_string = eval_soundfold(s);
11314#else
11315 rettv->vval.v_string = vim_strsave(s);
11316#endif
11317}
11318
11319/*
11320 * "spellbadword()" function
11321 */
11322 static void
11323f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11324{
11325 char_u *word = (char_u *)"";
11326 hlf_T attr = HLF_COUNT;
11327 int len = 0;
11328
11329 if (rettv_list_alloc(rettv) == FAIL)
11330 return;
11331
11332#ifdef FEAT_SPELL
11333 if (argvars[0].v_type == VAR_UNKNOWN)
11334 {
11335 /* Find the start and length of the badly spelled word. */
11336 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11337 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011338 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011339 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011340 curwin->w_set_curswant = TRUE;
11341 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011342 }
11343 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11344 {
11345 char_u *str = get_tv_string_chk(&argvars[0]);
11346 int capcol = -1;
11347
11348 if (str != NULL)
11349 {
11350 /* Check the argument for spelling. */
11351 while (*str != NUL)
11352 {
11353 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11354 if (attr != HLF_COUNT)
11355 {
11356 word = str;
11357 break;
11358 }
11359 str += len;
11360 }
11361 }
11362 }
11363#endif
11364
11365 list_append_string(rettv->vval.v_list, word, len);
11366 list_append_string(rettv->vval.v_list, (char_u *)(
11367 attr == HLF_SPB ? "bad" :
11368 attr == HLF_SPR ? "rare" :
11369 attr == HLF_SPL ? "local" :
11370 attr == HLF_SPC ? "caps" :
11371 ""), -1);
11372}
11373
11374/*
11375 * "spellsuggest()" function
11376 */
11377 static void
11378f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11379{
11380#ifdef FEAT_SPELL
11381 char_u *str;
11382 int typeerr = FALSE;
11383 int maxcount;
11384 garray_T ga;
11385 int i;
11386 listitem_T *li;
11387 int need_capital = FALSE;
11388#endif
11389
11390 if (rettv_list_alloc(rettv) == FAIL)
11391 return;
11392
11393#ifdef FEAT_SPELL
11394 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11395 {
11396 str = get_tv_string(&argvars[0]);
11397 if (argvars[1].v_type != VAR_UNKNOWN)
11398 {
11399 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11400 if (maxcount <= 0)
11401 return;
11402 if (argvars[2].v_type != VAR_UNKNOWN)
11403 {
11404 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11405 if (typeerr)
11406 return;
11407 }
11408 }
11409 else
11410 maxcount = 25;
11411
11412 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11413
11414 for (i = 0; i < ga.ga_len; ++i)
11415 {
11416 str = ((char_u **)ga.ga_data)[i];
11417
11418 li = listitem_alloc();
11419 if (li == NULL)
11420 vim_free(str);
11421 else
11422 {
11423 li->li_tv.v_type = VAR_STRING;
11424 li->li_tv.v_lock = 0;
11425 li->li_tv.vval.v_string = str;
11426 list_append(rettv->vval.v_list, li);
11427 }
11428 }
11429 ga_clear(&ga);
11430 }
11431#endif
11432}
11433
11434 static void
11435f_split(typval_T *argvars, typval_T *rettv)
11436{
11437 char_u *str;
11438 char_u *end;
11439 char_u *pat = NULL;
11440 regmatch_T regmatch;
11441 char_u patbuf[NUMBUFLEN];
11442 char_u *save_cpo;
11443 int match;
11444 colnr_T col = 0;
11445 int keepempty = FALSE;
11446 int typeerr = FALSE;
11447
11448 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11449 save_cpo = p_cpo;
11450 p_cpo = (char_u *)"";
11451
11452 str = get_tv_string(&argvars[0]);
11453 if (argvars[1].v_type != VAR_UNKNOWN)
11454 {
11455 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11456 if (pat == NULL)
11457 typeerr = TRUE;
11458 if (argvars[2].v_type != VAR_UNKNOWN)
11459 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11460 }
11461 if (pat == NULL || *pat == NUL)
11462 pat = (char_u *)"[\\x01- ]\\+";
11463
11464 if (rettv_list_alloc(rettv) == FAIL)
11465 return;
11466 if (typeerr)
11467 return;
11468
11469 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11470 if (regmatch.regprog != NULL)
11471 {
11472 regmatch.rm_ic = FALSE;
11473 while (*str != NUL || keepempty)
11474 {
11475 if (*str == NUL)
11476 match = FALSE; /* empty item at the end */
11477 else
11478 match = vim_regexec_nl(&regmatch, str, col);
11479 if (match)
11480 end = regmatch.startp[0];
11481 else
11482 end = str + STRLEN(str);
11483 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11484 && *str != NUL && match && end < regmatch.endp[0]))
11485 {
11486 if (list_append_string(rettv->vval.v_list, str,
11487 (int)(end - str)) == FAIL)
11488 break;
11489 }
11490 if (!match)
11491 break;
11492 /* Advance to just after the match. */
11493 if (regmatch.endp[0] > str)
11494 col = 0;
11495 else
11496 {
11497 /* Don't get stuck at the same match. */
11498#ifdef FEAT_MBYTE
11499 col = (*mb_ptr2len)(regmatch.endp[0]);
11500#else
11501 col = 1;
11502#endif
11503 }
11504 str = regmatch.endp[0];
11505 }
11506
11507 vim_regfree(regmatch.regprog);
11508 }
11509
11510 p_cpo = save_cpo;
11511}
11512
11513#ifdef FEAT_FLOAT
11514/*
11515 * "sqrt()" function
11516 */
11517 static void
11518f_sqrt(typval_T *argvars, typval_T *rettv)
11519{
11520 float_T f = 0.0;
11521
11522 rettv->v_type = VAR_FLOAT;
11523 if (get_float_arg(argvars, &f) == OK)
11524 rettv->vval.v_float = sqrt(f);
11525 else
11526 rettv->vval.v_float = 0.0;
11527}
11528
11529/*
11530 * "str2float()" function
11531 */
11532 static void
11533f_str2float(typval_T *argvars, typval_T *rettv)
11534{
11535 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011536 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011537
Bram Moolenaar08243d22017-01-10 16:12:29 +010011538 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011539 p = skipwhite(p + 1);
11540 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011541 if (isneg)
11542 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011543 rettv->v_type = VAR_FLOAT;
11544}
11545#endif
11546
11547/*
11548 * "str2nr()" function
11549 */
11550 static void
11551f_str2nr(typval_T *argvars, typval_T *rettv)
11552{
11553 int base = 10;
11554 char_u *p;
11555 varnumber_T n;
11556 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011557 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011558
11559 if (argvars[1].v_type != VAR_UNKNOWN)
11560 {
11561 base = (int)get_tv_number(&argvars[1]);
11562 if (base != 2 && base != 8 && base != 10 && base != 16)
11563 {
11564 EMSG(_(e_invarg));
11565 return;
11566 }
11567 }
11568
11569 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011570 isneg = (*p == '-');
11571 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011572 p = skipwhite(p + 1);
11573 switch (base)
11574 {
11575 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11576 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11577 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11578 default: what = 0;
11579 }
11580 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011581 if (isneg)
11582 rettv->vval.v_number = -n;
11583 else
11584 rettv->vval.v_number = n;
11585
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011586}
11587
11588#ifdef HAVE_STRFTIME
11589/*
11590 * "strftime({format}[, {time}])" function
11591 */
11592 static void
11593f_strftime(typval_T *argvars, typval_T *rettv)
11594{
11595 char_u result_buf[256];
11596 struct tm *curtime;
11597 time_t seconds;
11598 char_u *p;
11599
11600 rettv->v_type = VAR_STRING;
11601
11602 p = get_tv_string(&argvars[0]);
11603 if (argvars[1].v_type == VAR_UNKNOWN)
11604 seconds = time(NULL);
11605 else
11606 seconds = (time_t)get_tv_number(&argvars[1]);
11607 curtime = localtime(&seconds);
11608 /* MSVC returns NULL for an invalid value of seconds. */
11609 if (curtime == NULL)
11610 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11611 else
11612 {
11613# ifdef FEAT_MBYTE
11614 vimconv_T conv;
11615 char_u *enc;
11616
11617 conv.vc_type = CONV_NONE;
11618 enc = enc_locale();
11619 convert_setup(&conv, p_enc, enc);
11620 if (conv.vc_type != CONV_NONE)
11621 p = string_convert(&conv, p, NULL);
11622# endif
11623 if (p != NULL)
11624 (void)strftime((char *)result_buf, sizeof(result_buf),
11625 (char *)p, curtime);
11626 else
11627 result_buf[0] = NUL;
11628
11629# ifdef FEAT_MBYTE
11630 if (conv.vc_type != CONV_NONE)
11631 vim_free(p);
11632 convert_setup(&conv, enc, p_enc);
11633 if (conv.vc_type != CONV_NONE)
11634 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11635 else
11636# endif
11637 rettv->vval.v_string = vim_strsave(result_buf);
11638
11639# ifdef FEAT_MBYTE
11640 /* Release conversion descriptors */
11641 convert_setup(&conv, NULL, NULL);
11642 vim_free(enc);
11643# endif
11644 }
11645}
11646#endif
11647
11648/*
11649 * "strgetchar()" function
11650 */
11651 static void
11652f_strgetchar(typval_T *argvars, typval_T *rettv)
11653{
11654 char_u *str;
11655 int len;
11656 int error = FALSE;
11657 int charidx;
11658
11659 rettv->vval.v_number = -1;
11660 str = get_tv_string_chk(&argvars[0]);
11661 if (str == NULL)
11662 return;
11663 len = (int)STRLEN(str);
11664 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11665 if (error)
11666 return;
11667#ifdef FEAT_MBYTE
11668 {
11669 int byteidx = 0;
11670
11671 while (charidx >= 0 && byteidx < len)
11672 {
11673 if (charidx == 0)
11674 {
11675 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11676 break;
11677 }
11678 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011679 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011680 }
11681 }
11682#else
11683 if (charidx < len)
11684 rettv->vval.v_number = str[charidx];
11685#endif
11686}
11687
11688/*
11689 * "stridx()" function
11690 */
11691 static void
11692f_stridx(typval_T *argvars, typval_T *rettv)
11693{
11694 char_u buf[NUMBUFLEN];
11695 char_u *needle;
11696 char_u *haystack;
11697 char_u *save_haystack;
11698 char_u *pos;
11699 int start_idx;
11700
11701 needle = get_tv_string_chk(&argvars[1]);
11702 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11703 rettv->vval.v_number = -1;
11704 if (needle == NULL || haystack == NULL)
11705 return; /* type error; errmsg already given */
11706
11707 if (argvars[2].v_type != VAR_UNKNOWN)
11708 {
11709 int error = FALSE;
11710
11711 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11712 if (error || start_idx >= (int)STRLEN(haystack))
11713 return;
11714 if (start_idx >= 0)
11715 haystack += start_idx;
11716 }
11717
11718 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11719 if (pos != NULL)
11720 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11721}
11722
11723/*
11724 * "string()" function
11725 */
11726 static void
11727f_string(typval_T *argvars, typval_T *rettv)
11728{
11729 char_u *tofree;
11730 char_u numbuf[NUMBUFLEN];
11731
11732 rettv->v_type = VAR_STRING;
11733 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11734 get_copyID());
11735 /* Make a copy if we have a value but it's not in allocated memory. */
11736 if (rettv->vval.v_string != NULL && tofree == NULL)
11737 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11738}
11739
11740/*
11741 * "strlen()" function
11742 */
11743 static void
11744f_strlen(typval_T *argvars, typval_T *rettv)
11745{
11746 rettv->vval.v_number = (varnumber_T)(STRLEN(
11747 get_tv_string(&argvars[0])));
11748}
11749
11750/*
11751 * "strchars()" function
11752 */
11753 static void
11754f_strchars(typval_T *argvars, typval_T *rettv)
11755{
11756 char_u *s = get_tv_string(&argvars[0]);
11757 int skipcc = 0;
11758#ifdef FEAT_MBYTE
11759 varnumber_T len = 0;
11760 int (*func_mb_ptr2char_adv)(char_u **pp);
11761#endif
11762
11763 if (argvars[1].v_type != VAR_UNKNOWN)
11764 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11765 if (skipcc < 0 || skipcc > 1)
11766 EMSG(_(e_invarg));
11767 else
11768 {
11769#ifdef FEAT_MBYTE
11770 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11771 while (*s != NUL)
11772 {
11773 func_mb_ptr2char_adv(&s);
11774 ++len;
11775 }
11776 rettv->vval.v_number = len;
11777#else
11778 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11779#endif
11780 }
11781}
11782
11783/*
11784 * "strdisplaywidth()" function
11785 */
11786 static void
11787f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11788{
11789 char_u *s = get_tv_string(&argvars[0]);
11790 int col = 0;
11791
11792 if (argvars[1].v_type != VAR_UNKNOWN)
11793 col = (int)get_tv_number(&argvars[1]);
11794
11795 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11796}
11797
11798/*
11799 * "strwidth()" function
11800 */
11801 static void
11802f_strwidth(typval_T *argvars, typval_T *rettv)
11803{
11804 char_u *s = get_tv_string(&argvars[0]);
11805
11806 rettv->vval.v_number = (varnumber_T)(
11807#ifdef FEAT_MBYTE
11808 mb_string2cells(s, -1)
11809#else
11810 STRLEN(s)
11811#endif
11812 );
11813}
11814
11815/*
11816 * "strcharpart()" function
11817 */
11818 static void
11819f_strcharpart(typval_T *argvars, typval_T *rettv)
11820{
11821#ifdef FEAT_MBYTE
11822 char_u *p;
11823 int nchar;
11824 int nbyte = 0;
11825 int charlen;
11826 int len = 0;
11827 int slen;
11828 int error = FALSE;
11829
11830 p = get_tv_string(&argvars[0]);
11831 slen = (int)STRLEN(p);
11832
11833 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11834 if (!error)
11835 {
11836 if (nchar > 0)
11837 while (nchar > 0 && nbyte < slen)
11838 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011839 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011840 --nchar;
11841 }
11842 else
11843 nbyte = nchar;
11844 if (argvars[2].v_type != VAR_UNKNOWN)
11845 {
11846 charlen = (int)get_tv_number(&argvars[2]);
11847 while (charlen > 0 && nbyte + len < slen)
11848 {
11849 int off = nbyte + len;
11850
11851 if (off < 0)
11852 len += 1;
11853 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011854 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011855 --charlen;
11856 }
11857 }
11858 else
11859 len = slen - nbyte; /* default: all bytes that are available. */
11860 }
11861
11862 /*
11863 * Only return the overlap between the specified part and the actual
11864 * string.
11865 */
11866 if (nbyte < 0)
11867 {
11868 len += nbyte;
11869 nbyte = 0;
11870 }
11871 else if (nbyte > slen)
11872 nbyte = slen;
11873 if (len < 0)
11874 len = 0;
11875 else if (nbyte + len > slen)
11876 len = slen - nbyte;
11877
11878 rettv->v_type = VAR_STRING;
11879 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11880#else
11881 f_strpart(argvars, rettv);
11882#endif
11883}
11884
11885/*
11886 * "strpart()" function
11887 */
11888 static void
11889f_strpart(typval_T *argvars, typval_T *rettv)
11890{
11891 char_u *p;
11892 int n;
11893 int len;
11894 int slen;
11895 int error = FALSE;
11896
11897 p = get_tv_string(&argvars[0]);
11898 slen = (int)STRLEN(p);
11899
11900 n = (int)get_tv_number_chk(&argvars[1], &error);
11901 if (error)
11902 len = 0;
11903 else if (argvars[2].v_type != VAR_UNKNOWN)
11904 len = (int)get_tv_number(&argvars[2]);
11905 else
11906 len = slen - n; /* default len: all bytes that are available. */
11907
11908 /*
11909 * Only return the overlap between the specified part and the actual
11910 * string.
11911 */
11912 if (n < 0)
11913 {
11914 len += n;
11915 n = 0;
11916 }
11917 else if (n > slen)
11918 n = slen;
11919 if (len < 0)
11920 len = 0;
11921 else if (n + len > slen)
11922 len = slen - n;
11923
11924 rettv->v_type = VAR_STRING;
11925 rettv->vval.v_string = vim_strnsave(p + n, len);
11926}
11927
11928/*
11929 * "strridx()" function
11930 */
11931 static void
11932f_strridx(typval_T *argvars, typval_T *rettv)
11933{
11934 char_u buf[NUMBUFLEN];
11935 char_u *needle;
11936 char_u *haystack;
11937 char_u *rest;
11938 char_u *lastmatch = NULL;
11939 int haystack_len, end_idx;
11940
11941 needle = get_tv_string_chk(&argvars[1]);
11942 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11943
11944 rettv->vval.v_number = -1;
11945 if (needle == NULL || haystack == NULL)
11946 return; /* type error; errmsg already given */
11947
11948 haystack_len = (int)STRLEN(haystack);
11949 if (argvars[2].v_type != VAR_UNKNOWN)
11950 {
11951 /* Third argument: upper limit for index */
11952 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11953 if (end_idx < 0)
11954 return; /* can never find a match */
11955 }
11956 else
11957 end_idx = haystack_len;
11958
11959 if (*needle == NUL)
11960 {
11961 /* Empty string matches past the end. */
11962 lastmatch = haystack + end_idx;
11963 }
11964 else
11965 {
11966 for (rest = haystack; *rest != '\0'; ++rest)
11967 {
11968 rest = (char_u *)strstr((char *)rest, (char *)needle);
11969 if (rest == NULL || rest > haystack + end_idx)
11970 break;
11971 lastmatch = rest;
11972 }
11973 }
11974
11975 if (lastmatch == NULL)
11976 rettv->vval.v_number = -1;
11977 else
11978 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11979}
11980
11981/*
11982 * "strtrans()" function
11983 */
11984 static void
11985f_strtrans(typval_T *argvars, typval_T *rettv)
11986{
11987 rettv->v_type = VAR_STRING;
11988 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11989}
11990
11991/*
11992 * "submatch()" function
11993 */
11994 static void
11995f_submatch(typval_T *argvars, typval_T *rettv)
11996{
11997 int error = FALSE;
11998 int no;
11999 int retList = 0;
12000
12001 no = (int)get_tv_number_chk(&argvars[0], &error);
12002 if (error)
12003 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012004 if (no < 0 || no >= NSUBEXP)
12005 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012006 EMSGN(_("E935: invalid submatch number: %d"), no);
12007 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012008 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012009 if (argvars[1].v_type != VAR_UNKNOWN)
12010 retList = (int)get_tv_number_chk(&argvars[1], &error);
12011 if (error)
12012 return;
12013
12014 if (retList == 0)
12015 {
12016 rettv->v_type = VAR_STRING;
12017 rettv->vval.v_string = reg_submatch(no);
12018 }
12019 else
12020 {
12021 rettv->v_type = VAR_LIST;
12022 rettv->vval.v_list = reg_submatch_list(no);
12023 }
12024}
12025
12026/*
12027 * "substitute()" function
12028 */
12029 static void
12030f_substitute(typval_T *argvars, typval_T *rettv)
12031{
12032 char_u patbuf[NUMBUFLEN];
12033 char_u subbuf[NUMBUFLEN];
12034 char_u flagsbuf[NUMBUFLEN];
12035
12036 char_u *str = get_tv_string_chk(&argvars[0]);
12037 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012038 char_u *sub = NULL;
12039 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012040 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12041
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012042 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12043 expr = &argvars[2];
12044 else
12045 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12046
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012047 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012048 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12049 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012050 rettv->vval.v_string = NULL;
12051 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012052 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012053}
12054
12055/*
12056 * "synID(lnum, col, trans)" function
12057 */
12058 static void
12059f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12060{
12061 int id = 0;
12062#ifdef FEAT_SYN_HL
12063 linenr_T lnum;
12064 colnr_T col;
12065 int trans;
12066 int transerr = FALSE;
12067
12068 lnum = get_tv_lnum(argvars); /* -1 on type error */
12069 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12070 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12071
12072 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12073 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12074 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12075#endif
12076
12077 rettv->vval.v_number = id;
12078}
12079
12080/*
12081 * "synIDattr(id, what [, mode])" function
12082 */
12083 static void
12084f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12085{
12086 char_u *p = NULL;
12087#ifdef FEAT_SYN_HL
12088 int id;
12089 char_u *what;
12090 char_u *mode;
12091 char_u modebuf[NUMBUFLEN];
12092 int modec;
12093
12094 id = (int)get_tv_number(&argvars[0]);
12095 what = get_tv_string(&argvars[1]);
12096 if (argvars[2].v_type != VAR_UNKNOWN)
12097 {
12098 mode = get_tv_string_buf(&argvars[2], modebuf);
12099 modec = TOLOWER_ASC(mode[0]);
12100 if (modec != 't' && modec != 'c' && modec != 'g')
12101 modec = 0; /* replace invalid with current */
12102 }
12103 else
12104 {
12105#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12106 if (USE_24BIT)
12107 modec = 'g';
12108 else
12109#endif
12110 if (t_colors > 1)
12111 modec = 'c';
12112 else
12113 modec = 't';
12114 }
12115
12116
12117 switch (TOLOWER_ASC(what[0]))
12118 {
12119 case 'b':
12120 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12121 p = highlight_color(id, what, modec);
12122 else /* bold */
12123 p = highlight_has_attr(id, HL_BOLD, modec);
12124 break;
12125
12126 case 'f': /* fg[#] or font */
12127 p = highlight_color(id, what, modec);
12128 break;
12129
12130 case 'i':
12131 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12132 p = highlight_has_attr(id, HL_INVERSE, modec);
12133 else /* italic */
12134 p = highlight_has_attr(id, HL_ITALIC, modec);
12135 break;
12136
12137 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012138 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012139 break;
12140
12141 case 'r': /* reverse */
12142 p = highlight_has_attr(id, HL_INVERSE, modec);
12143 break;
12144
12145 case 's':
12146 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12147 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012148 /* strikeout */
12149 else if (TOLOWER_ASC(what[1]) == 't' &&
12150 TOLOWER_ASC(what[2]) == 'r')
12151 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012152 else /* standout */
12153 p = highlight_has_attr(id, HL_STANDOUT, modec);
12154 break;
12155
12156 case 'u':
12157 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12158 /* underline */
12159 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12160 else
12161 /* undercurl */
12162 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12163 break;
12164 }
12165
12166 if (p != NULL)
12167 p = vim_strsave(p);
12168#endif
12169 rettv->v_type = VAR_STRING;
12170 rettv->vval.v_string = p;
12171}
12172
12173/*
12174 * "synIDtrans(id)" function
12175 */
12176 static void
12177f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12178{
12179 int id;
12180
12181#ifdef FEAT_SYN_HL
12182 id = (int)get_tv_number(&argvars[0]);
12183
12184 if (id > 0)
12185 id = syn_get_final_id(id);
12186 else
12187#endif
12188 id = 0;
12189
12190 rettv->vval.v_number = id;
12191}
12192
12193/*
12194 * "synconcealed(lnum, col)" function
12195 */
12196 static void
12197f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12198{
12199#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12200 linenr_T lnum;
12201 colnr_T col;
12202 int syntax_flags = 0;
12203 int cchar;
12204 int matchid = 0;
12205 char_u str[NUMBUFLEN];
12206#endif
12207
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012208 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012209
12210#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12211 lnum = get_tv_lnum(argvars); /* -1 on type error */
12212 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12213
12214 vim_memset(str, NUL, sizeof(str));
12215
12216 if (rettv_list_alloc(rettv) != FAIL)
12217 {
12218 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12219 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12220 && curwin->w_p_cole > 0)
12221 {
12222 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12223 syntax_flags = get_syntax_info(&matchid);
12224
12225 /* get the conceal character */
12226 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12227 {
12228 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012229 if (cchar == NUL && curwin->w_p_cole == 1)
12230 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012231 if (cchar != NUL)
12232 {
12233# ifdef FEAT_MBYTE
12234 if (has_mbyte)
12235 (*mb_char2bytes)(cchar, str);
12236 else
12237# endif
12238 str[0] = cchar;
12239 }
12240 }
12241 }
12242
12243 list_append_number(rettv->vval.v_list,
12244 (syntax_flags & HL_CONCEAL) != 0);
12245 /* -1 to auto-determine strlen */
12246 list_append_string(rettv->vval.v_list, str, -1);
12247 list_append_number(rettv->vval.v_list, matchid);
12248 }
12249#endif
12250}
12251
12252/*
12253 * "synstack(lnum, col)" function
12254 */
12255 static void
12256f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12257{
12258#ifdef FEAT_SYN_HL
12259 linenr_T lnum;
12260 colnr_T col;
12261 int i;
12262 int id;
12263#endif
12264
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012265 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012266
12267#ifdef FEAT_SYN_HL
12268 lnum = get_tv_lnum(argvars); /* -1 on type error */
12269 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12270
12271 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12272 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12273 && rettv_list_alloc(rettv) != FAIL)
12274 {
12275 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12276 for (i = 0; ; ++i)
12277 {
12278 id = syn_get_stack_item(i);
12279 if (id < 0)
12280 break;
12281 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12282 break;
12283 }
12284 }
12285#endif
12286}
12287
12288 static void
12289get_cmd_output_as_rettv(
12290 typval_T *argvars,
12291 typval_T *rettv,
12292 int retlist)
12293{
12294 char_u *res = NULL;
12295 char_u *p;
12296 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012297 int err = FALSE;
12298 FILE *fd;
12299 list_T *list = NULL;
12300 int flags = SHELL_SILENT;
12301
12302 rettv->v_type = VAR_STRING;
12303 rettv->vval.v_string = NULL;
12304 if (check_restricted() || check_secure())
12305 goto errret;
12306
12307 if (argvars[1].v_type != VAR_UNKNOWN)
12308 {
12309 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012310 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012311 * command.
12312 */
12313 if ((infile = vim_tempname('i', TRUE)) == NULL)
12314 {
12315 EMSG(_(e_notmp));
12316 goto errret;
12317 }
12318
12319 fd = mch_fopen((char *)infile, WRITEBIN);
12320 if (fd == NULL)
12321 {
12322 EMSG2(_(e_notopen), infile);
12323 goto errret;
12324 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012325 if (argvars[1].v_type == VAR_NUMBER)
12326 {
12327 linenr_T lnum;
12328 buf_T *buf;
12329
12330 buf = buflist_findnr(argvars[1].vval.v_number);
12331 if (buf == NULL)
12332 {
12333 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012334 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012335 goto errret;
12336 }
12337
12338 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12339 {
12340 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12341 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12342 {
12343 err = TRUE;
12344 break;
12345 }
12346 if (putc(NL, fd) == EOF)
12347 {
12348 err = TRUE;
12349 break;
12350 }
12351 }
12352 }
12353 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012354 {
12355 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12356 err = TRUE;
12357 }
12358 else
12359 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012360 size_t len;
12361 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012362
12363 p = get_tv_string_buf_chk(&argvars[1], buf);
12364 if (p == NULL)
12365 {
12366 fclose(fd);
12367 goto errret; /* type error; errmsg already given */
12368 }
12369 len = STRLEN(p);
12370 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12371 err = TRUE;
12372 }
12373 if (fclose(fd) != 0)
12374 err = TRUE;
12375 if (err)
12376 {
12377 EMSG(_("E677: Error writing temp file"));
12378 goto errret;
12379 }
12380 }
12381
12382 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12383 * echoes typeahead, that messes up the display. */
12384 if (!msg_silent)
12385 flags += SHELL_COOKED;
12386
12387 if (retlist)
12388 {
12389 int len;
12390 listitem_T *li;
12391 char_u *s = NULL;
12392 char_u *start;
12393 char_u *end;
12394 int i;
12395
12396 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12397 if (res == NULL)
12398 goto errret;
12399
12400 list = list_alloc();
12401 if (list == NULL)
12402 goto errret;
12403
12404 for (i = 0; i < len; ++i)
12405 {
12406 start = res + i;
12407 while (i < len && res[i] != NL)
12408 ++i;
12409 end = res + i;
12410
12411 s = alloc((unsigned)(end - start + 1));
12412 if (s == NULL)
12413 goto errret;
12414
12415 for (p = s; start < end; ++p, ++start)
12416 *p = *start == NUL ? NL : *start;
12417 *p = NUL;
12418
12419 li = listitem_alloc();
12420 if (li == NULL)
12421 {
12422 vim_free(s);
12423 goto errret;
12424 }
12425 li->li_tv.v_type = VAR_STRING;
12426 li->li_tv.v_lock = 0;
12427 li->li_tv.vval.v_string = s;
12428 list_append(list, li);
12429 }
12430
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012431 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012432 list = NULL;
12433 }
12434 else
12435 {
12436 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12437#ifdef USE_CR
12438 /* translate <CR> into <NL> */
12439 if (res != NULL)
12440 {
12441 char_u *s;
12442
12443 for (s = res; *s; ++s)
12444 {
12445 if (*s == CAR)
12446 *s = NL;
12447 }
12448 }
12449#else
12450# ifdef USE_CRNL
12451 /* translate <CR><NL> into <NL> */
12452 if (res != NULL)
12453 {
12454 char_u *s, *d;
12455
12456 d = res;
12457 for (s = res; *s; ++s)
12458 {
12459 if (s[0] == CAR && s[1] == NL)
12460 ++s;
12461 *d++ = *s;
12462 }
12463 *d = NUL;
12464 }
12465# endif
12466#endif
12467 rettv->vval.v_string = res;
12468 res = NULL;
12469 }
12470
12471errret:
12472 if (infile != NULL)
12473 {
12474 mch_remove(infile);
12475 vim_free(infile);
12476 }
12477 if (res != NULL)
12478 vim_free(res);
12479 if (list != NULL)
12480 list_free(list);
12481}
12482
12483/*
12484 * "system()" function
12485 */
12486 static void
12487f_system(typval_T *argvars, typval_T *rettv)
12488{
12489 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12490}
12491
12492/*
12493 * "systemlist()" function
12494 */
12495 static void
12496f_systemlist(typval_T *argvars, typval_T *rettv)
12497{
12498 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12499}
12500
12501/*
12502 * "tabpagebuflist()" function
12503 */
12504 static void
12505f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12506{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012507 tabpage_T *tp;
12508 win_T *wp = NULL;
12509
12510 if (argvars[0].v_type == VAR_UNKNOWN)
12511 wp = firstwin;
12512 else
12513 {
12514 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12515 if (tp != NULL)
12516 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12517 }
12518 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12519 {
12520 for (; wp != NULL; wp = wp->w_next)
12521 if (list_append_number(rettv->vval.v_list,
12522 wp->w_buffer->b_fnum) == FAIL)
12523 break;
12524 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012525}
12526
12527
12528/*
12529 * "tabpagenr()" function
12530 */
12531 static void
12532f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12533{
12534 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012535 char_u *arg;
12536
12537 if (argvars[0].v_type != VAR_UNKNOWN)
12538 {
12539 arg = get_tv_string_chk(&argvars[0]);
12540 nr = 0;
12541 if (arg != NULL)
12542 {
12543 if (STRCMP(arg, "$") == 0)
12544 nr = tabpage_index(NULL) - 1;
12545 else
12546 EMSG2(_(e_invexpr2), arg);
12547 }
12548 }
12549 else
12550 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012551 rettv->vval.v_number = nr;
12552}
12553
12554
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012555static int get_winnr(tabpage_T *tp, typval_T *argvar);
12556
12557/*
12558 * Common code for tabpagewinnr() and winnr().
12559 */
12560 static int
12561get_winnr(tabpage_T *tp, typval_T *argvar)
12562{
12563 win_T *twin;
12564 int nr = 1;
12565 win_T *wp;
12566 char_u *arg;
12567
12568 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12569 if (argvar->v_type != VAR_UNKNOWN)
12570 {
12571 arg = get_tv_string_chk(argvar);
12572 if (arg == NULL)
12573 nr = 0; /* type error; errmsg already given */
12574 else if (STRCMP(arg, "$") == 0)
12575 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12576 else if (STRCMP(arg, "#") == 0)
12577 {
12578 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12579 if (twin == NULL)
12580 nr = 0;
12581 }
12582 else
12583 {
12584 EMSG2(_(e_invexpr2), arg);
12585 nr = 0;
12586 }
12587 }
12588
12589 if (nr > 0)
12590 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12591 wp != twin; wp = wp->w_next)
12592 {
12593 if (wp == NULL)
12594 {
12595 /* didn't find it in this tabpage */
12596 nr = 0;
12597 break;
12598 }
12599 ++nr;
12600 }
12601 return nr;
12602}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012603
12604/*
12605 * "tabpagewinnr()" function
12606 */
12607 static void
12608f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12609{
12610 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012611 tabpage_T *tp;
12612
12613 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12614 if (tp == NULL)
12615 nr = 0;
12616 else
12617 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012618 rettv->vval.v_number = nr;
12619}
12620
12621
12622/*
12623 * "tagfiles()" function
12624 */
12625 static void
12626f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12627{
12628 char_u *fname;
12629 tagname_T tn;
12630 int first;
12631
12632 if (rettv_list_alloc(rettv) == FAIL)
12633 return;
12634 fname = alloc(MAXPATHL);
12635 if (fname == NULL)
12636 return;
12637
12638 for (first = TRUE; ; first = FALSE)
12639 if (get_tagfname(&tn, first, fname) == FAIL
12640 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12641 break;
12642 tagname_free(&tn);
12643 vim_free(fname);
12644}
12645
12646/*
12647 * "taglist()" function
12648 */
12649 static void
12650f_taglist(typval_T *argvars, typval_T *rettv)
12651{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012652 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012653 char_u *tag_pattern;
12654
12655 tag_pattern = get_tv_string(&argvars[0]);
12656
12657 rettv->vval.v_number = FALSE;
12658 if (*tag_pattern == NUL)
12659 return;
12660
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012661 if (argvars[1].v_type != VAR_UNKNOWN)
12662 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012663 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012664 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012665}
12666
12667/*
12668 * "tempname()" function
12669 */
12670 static void
12671f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12672{
12673 static int x = 'A';
12674
12675 rettv->v_type = VAR_STRING;
12676 rettv->vval.v_string = vim_tempname(x, FALSE);
12677
12678 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12679 * names. Skip 'I' and 'O', they are used for shell redirection. */
12680 do
12681 {
12682 if (x == 'Z')
12683 x = '0';
12684 else if (x == '9')
12685 x = 'A';
12686 else
12687 {
12688#ifdef EBCDIC
12689 if (x == 'I')
12690 x = 'J';
12691 else if (x == 'R')
12692 x = 'S';
12693 else
12694#endif
12695 ++x;
12696 }
12697 } while (x == 'I' || x == 'O');
12698}
12699
12700#ifdef FEAT_FLOAT
12701/*
12702 * "tan()" function
12703 */
12704 static void
12705f_tan(typval_T *argvars, typval_T *rettv)
12706{
12707 float_T f = 0.0;
12708
12709 rettv->v_type = VAR_FLOAT;
12710 if (get_float_arg(argvars, &f) == OK)
12711 rettv->vval.v_float = tan(f);
12712 else
12713 rettv->vval.v_float = 0.0;
12714}
12715
12716/*
12717 * "tanh()" function
12718 */
12719 static void
12720f_tanh(typval_T *argvars, typval_T *rettv)
12721{
12722 float_T f = 0.0;
12723
12724 rettv->v_type = VAR_FLOAT;
12725 if (get_float_arg(argvars, &f) == OK)
12726 rettv->vval.v_float = tanh(f);
12727 else
12728 rettv->vval.v_float = 0.0;
12729}
12730#endif
12731
12732/*
12733 * "test_alloc_fail(id, countdown, repeat)" function
12734 */
12735 static void
12736f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12737{
12738 if (argvars[0].v_type != VAR_NUMBER
12739 || argvars[0].vval.v_number <= 0
12740 || argvars[1].v_type != VAR_NUMBER
12741 || argvars[1].vval.v_number < 0
12742 || argvars[2].v_type != VAR_NUMBER)
12743 EMSG(_(e_invarg));
12744 else
12745 {
12746 alloc_fail_id = argvars[0].vval.v_number;
12747 if (alloc_fail_id >= aid_last)
12748 EMSG(_(e_invarg));
12749 alloc_fail_countdown = argvars[1].vval.v_number;
12750 alloc_fail_repeat = argvars[2].vval.v_number;
12751 did_outofmem_msg = FALSE;
12752 }
12753}
12754
12755/*
12756 * "test_autochdir()"
12757 */
12758 static void
12759f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12760{
12761#if defined(FEAT_AUTOCHDIR)
12762 test_autochdir = TRUE;
12763#endif
12764}
12765
12766/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012767 * "test_feedinput()"
12768 */
12769 static void
12770f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12771{
12772#ifdef USE_INPUT_BUF
12773 char_u *val = get_tv_string_chk(&argvars[0]);
12774
12775 if (val != NULL)
12776 {
12777 trash_input_buf();
12778 add_to_input_buf_csi(val, (int)STRLEN(val));
12779 }
12780#endif
12781}
12782
12783/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012784 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012785 */
12786 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012787f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012788{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012789 char_u *name = (char_u *)"";
12790 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012791 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012792
12793 if (argvars[0].v_type != VAR_STRING
12794 || (argvars[1].v_type) != VAR_NUMBER)
12795 EMSG(_(e_invarg));
12796 else
12797 {
12798 name = get_tv_string_chk(&argvars[0]);
12799 val = (int)get_tv_number(&argvars[1]);
12800
12801 if (STRCMP(name, (char_u *)"redraw") == 0)
12802 disable_redraw_for_testing = val;
12803 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12804 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012805 else if (STRCMP(name, (char_u *)"starting") == 0)
12806 {
12807 if (val)
12808 {
12809 if (save_starting < 0)
12810 save_starting = starting;
12811 starting = 0;
12812 }
12813 else
12814 {
12815 starting = save_starting;
12816 save_starting = -1;
12817 }
12818 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012819 else if (STRCMP(name, (char_u *)"ALL") == 0)
12820 {
12821 disable_char_avail_for_testing = FALSE;
12822 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012823 if (save_starting >= 0)
12824 {
12825 starting = save_starting;
12826 save_starting = -1;
12827 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012828 }
12829 else
12830 EMSG2(_(e_invarg2), name);
12831 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012832}
12833
12834/*
12835 * "test_garbagecollect_now()" function
12836 */
12837 static void
12838f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12839{
12840 /* This is dangerous, any Lists and Dicts used internally may be freed
12841 * while still in use. */
12842 garbage_collect(TRUE);
12843}
12844
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012845/*
12846 * "test_ignore_error()" function
12847 */
12848 static void
12849f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12850{
12851 ignore_error_for_testing(get_tv_string(&argvars[0]));
12852}
12853
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012854#ifdef FEAT_JOB_CHANNEL
12855 static void
12856f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12857{
12858 rettv->v_type = VAR_CHANNEL;
12859 rettv->vval.v_channel = NULL;
12860}
12861#endif
12862
12863 static void
12864f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12865{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012866 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012867}
12868
12869#ifdef FEAT_JOB_CHANNEL
12870 static void
12871f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12872{
12873 rettv->v_type = VAR_JOB;
12874 rettv->vval.v_job = NULL;
12875}
12876#endif
12877
12878 static void
12879f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12880{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012881 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012882}
12883
12884 static void
12885f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12886{
12887 rettv->v_type = VAR_PARTIAL;
12888 rettv->vval.v_partial = NULL;
12889}
12890
12891 static void
12892f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12893{
12894 rettv->v_type = VAR_STRING;
12895 rettv->vval.v_string = NULL;
12896}
12897
12898 static void
12899f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12900{
12901 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12902}
12903
12904#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12905/*
12906 * Get a callback from "arg". It can be a Funcref or a function name.
12907 * When "arg" is zero return an empty string.
12908 * Return NULL for an invalid argument.
12909 */
12910 char_u *
12911get_callback(typval_T *arg, partial_T **pp)
12912{
12913 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12914 {
12915 *pp = arg->vval.v_partial;
12916 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012917 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012918 }
12919 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012920 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012921 {
12922 func_ref(arg->vval.v_string);
12923 return arg->vval.v_string;
12924 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012925 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12926 return (char_u *)"";
12927 EMSG(_("E921: Invalid callback argument"));
12928 return NULL;
12929}
12930
12931/*
12932 * Unref/free "callback" and "partial" retured by get_callback().
12933 */
12934 void
12935free_callback(char_u *callback, partial_T *partial)
12936{
12937 if (partial != NULL)
12938 partial_unref(partial);
12939 else if (callback != NULL)
12940 {
12941 func_unref(callback);
12942 vim_free(callback);
12943 }
12944}
12945#endif
12946
12947#ifdef FEAT_TIMERS
12948/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012949 * "timer_info([timer])" function
12950 */
12951 static void
12952f_timer_info(typval_T *argvars, typval_T *rettv)
12953{
12954 timer_T *timer = NULL;
12955
12956 if (rettv_list_alloc(rettv) != OK)
12957 return;
12958 if (argvars[0].v_type != VAR_UNKNOWN)
12959 {
12960 if (argvars[0].v_type != VAR_NUMBER)
12961 EMSG(_(e_number_exp));
12962 else
12963 {
12964 timer = find_timer((int)get_tv_number(&argvars[0]));
12965 if (timer != NULL)
12966 add_timer_info(rettv, timer);
12967 }
12968 }
12969 else
12970 add_timer_info_all(rettv);
12971}
12972
12973/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012974 * "timer_pause(timer, paused)" function
12975 */
12976 static void
12977f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12978{
12979 timer_T *timer = NULL;
12980 int paused = (int)get_tv_number(&argvars[1]);
12981
12982 if (argvars[0].v_type != VAR_NUMBER)
12983 EMSG(_(e_number_exp));
12984 else
12985 {
12986 timer = find_timer((int)get_tv_number(&argvars[0]));
12987 if (timer != NULL)
12988 timer->tr_paused = paused;
12989 }
12990}
12991
12992/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012993 * "timer_start(time, callback [, options])" function
12994 */
12995 static void
12996f_timer_start(typval_T *argvars, typval_T *rettv)
12997{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012998 long msec = (long)get_tv_number(&argvars[0]);
12999 timer_T *timer;
13000 int repeat = 0;
13001 char_u *callback;
13002 dict_T *dict;
13003 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013004
Bram Moolenaar75537a92016-09-05 22:45:28 +020013005 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013006 if (check_secure())
13007 return;
13008 if (argvars[2].v_type != VAR_UNKNOWN)
13009 {
13010 if (argvars[2].v_type != VAR_DICT
13011 || (dict = argvars[2].vval.v_dict) == NULL)
13012 {
13013 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13014 return;
13015 }
13016 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13017 repeat = get_dict_number(dict, (char_u *)"repeat");
13018 }
13019
Bram Moolenaar75537a92016-09-05 22:45:28 +020013020 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013021 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013022 return;
13023
13024 timer = create_timer(msec, repeat);
13025 if (timer == NULL)
13026 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013027 else
13028 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013029 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013030 timer->tr_callback = vim_strsave(callback);
13031 else
13032 /* pointer into the partial */
13033 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013034 timer->tr_partial = partial;
13035 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013036 }
13037}
13038
13039/*
13040 * "timer_stop(timer)" function
13041 */
13042 static void
13043f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13044{
13045 timer_T *timer;
13046
13047 if (argvars[0].v_type != VAR_NUMBER)
13048 {
13049 EMSG(_(e_number_exp));
13050 return;
13051 }
13052 timer = find_timer((int)get_tv_number(&argvars[0]));
13053 if (timer != NULL)
13054 stop_timer(timer);
13055}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013056
13057/*
13058 * "timer_stopall()" function
13059 */
13060 static void
13061f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13062{
13063 stop_all_timers();
13064}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013065#endif
13066
13067/*
13068 * "tolower(string)" function
13069 */
13070 static void
13071f_tolower(typval_T *argvars, typval_T *rettv)
13072{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013073 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013074 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013075}
13076
13077/*
13078 * "toupper(string)" function
13079 */
13080 static void
13081f_toupper(typval_T *argvars, typval_T *rettv)
13082{
13083 rettv->v_type = VAR_STRING;
13084 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13085}
13086
13087/*
13088 * "tr(string, fromstr, tostr)" function
13089 */
13090 static void
13091f_tr(typval_T *argvars, typval_T *rettv)
13092{
13093 char_u *in_str;
13094 char_u *fromstr;
13095 char_u *tostr;
13096 char_u *p;
13097#ifdef FEAT_MBYTE
13098 int inlen;
13099 int fromlen;
13100 int tolen;
13101 int idx;
13102 char_u *cpstr;
13103 int cplen;
13104 int first = TRUE;
13105#endif
13106 char_u buf[NUMBUFLEN];
13107 char_u buf2[NUMBUFLEN];
13108 garray_T ga;
13109
13110 in_str = get_tv_string(&argvars[0]);
13111 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13112 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13113
13114 /* Default return value: empty string. */
13115 rettv->v_type = VAR_STRING;
13116 rettv->vval.v_string = NULL;
13117 if (fromstr == NULL || tostr == NULL)
13118 return; /* type error; errmsg already given */
13119 ga_init2(&ga, (int)sizeof(char), 80);
13120
13121#ifdef FEAT_MBYTE
13122 if (!has_mbyte)
13123#endif
13124 /* not multi-byte: fromstr and tostr must be the same length */
13125 if (STRLEN(fromstr) != STRLEN(tostr))
13126 {
13127#ifdef FEAT_MBYTE
13128error:
13129#endif
13130 EMSG2(_(e_invarg2), fromstr);
13131 ga_clear(&ga);
13132 return;
13133 }
13134
13135 /* fromstr and tostr have to contain the same number of chars */
13136 while (*in_str != NUL)
13137 {
13138#ifdef FEAT_MBYTE
13139 if (has_mbyte)
13140 {
13141 inlen = (*mb_ptr2len)(in_str);
13142 cpstr = in_str;
13143 cplen = inlen;
13144 idx = 0;
13145 for (p = fromstr; *p != NUL; p += fromlen)
13146 {
13147 fromlen = (*mb_ptr2len)(p);
13148 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13149 {
13150 for (p = tostr; *p != NUL; p += tolen)
13151 {
13152 tolen = (*mb_ptr2len)(p);
13153 if (idx-- == 0)
13154 {
13155 cplen = tolen;
13156 cpstr = p;
13157 break;
13158 }
13159 }
13160 if (*p == NUL) /* tostr is shorter than fromstr */
13161 goto error;
13162 break;
13163 }
13164 ++idx;
13165 }
13166
13167 if (first && cpstr == in_str)
13168 {
13169 /* Check that fromstr and tostr have the same number of
13170 * (multi-byte) characters. Done only once when a character
13171 * of in_str doesn't appear in fromstr. */
13172 first = FALSE;
13173 for (p = tostr; *p != NUL; p += tolen)
13174 {
13175 tolen = (*mb_ptr2len)(p);
13176 --idx;
13177 }
13178 if (idx != 0)
13179 goto error;
13180 }
13181
13182 (void)ga_grow(&ga, cplen);
13183 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13184 ga.ga_len += cplen;
13185
13186 in_str += inlen;
13187 }
13188 else
13189#endif
13190 {
13191 /* When not using multi-byte chars we can do it faster. */
13192 p = vim_strchr(fromstr, *in_str);
13193 if (p != NULL)
13194 ga_append(&ga, tostr[p - fromstr]);
13195 else
13196 ga_append(&ga, *in_str);
13197 ++in_str;
13198 }
13199 }
13200
13201 /* add a terminating NUL */
13202 (void)ga_grow(&ga, 1);
13203 ga_append(&ga, NUL);
13204
13205 rettv->vval.v_string = ga.ga_data;
13206}
13207
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013208/*
13209 * "trim({expr})" function
13210 */
13211 static void
13212f_trim(typval_T *argvars, typval_T *rettv)
13213{
13214 char_u buf1[NUMBUFLEN];
13215 char_u buf2[NUMBUFLEN];
13216 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13217 char_u *mask = NULL;
13218 char_u *tail;
13219 char_u *prev;
13220 char_u *p;
13221 int c1;
13222
13223 rettv->v_type = VAR_STRING;
13224 if (head == NULL)
13225 {
13226 rettv->vval.v_string = NULL;
13227 return;
13228 }
13229
13230 if (argvars[1].v_type == VAR_STRING)
13231 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13232
13233 while (*head != NUL)
13234 {
13235 c1 = PTR2CHAR(head);
13236 if (mask == NULL)
13237 {
13238 if (c1 > ' ' && c1 != 0xa0)
13239 break;
13240 }
13241 else
13242 {
13243 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13244 if (c1 == PTR2CHAR(p))
13245 break;
13246 if (*p == NUL)
13247 break;
13248 }
13249 MB_PTR_ADV(head);
13250 }
13251
13252 for (tail = head + STRLEN(head); tail > head; tail = prev)
13253 {
13254 prev = tail;
13255 MB_PTR_BACK(head, prev);
13256 c1 = PTR2CHAR(prev);
13257 if (mask == NULL)
13258 {
13259 if (c1 > ' ' && c1 != 0xa0)
13260 break;
13261 }
13262 else
13263 {
13264 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13265 if (c1 == PTR2CHAR(p))
13266 break;
13267 if (*p == NUL)
13268 break;
13269 }
13270 }
13271 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13272}
13273
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013274#ifdef FEAT_FLOAT
13275/*
13276 * "trunc({float})" function
13277 */
13278 static void
13279f_trunc(typval_T *argvars, typval_T *rettv)
13280{
13281 float_T f = 0.0;
13282
13283 rettv->v_type = VAR_FLOAT;
13284 if (get_float_arg(argvars, &f) == OK)
13285 /* trunc() is not in C90, use floor() or ceil() instead. */
13286 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13287 else
13288 rettv->vval.v_float = 0.0;
13289}
13290#endif
13291
13292/*
13293 * "type(expr)" function
13294 */
13295 static void
13296f_type(typval_T *argvars, typval_T *rettv)
13297{
13298 int n = -1;
13299
13300 switch (argvars[0].v_type)
13301 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013302 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13303 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013304 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013305 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13306 case VAR_LIST: n = VAR_TYPE_LIST; break;
13307 case VAR_DICT: n = VAR_TYPE_DICT; break;
13308 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013309 case VAR_SPECIAL:
13310 if (argvars[0].vval.v_number == VVAL_FALSE
13311 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013312 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013313 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013314 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013315 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013316 case VAR_JOB: n = VAR_TYPE_JOB; break;
13317 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013318 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013319 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013320 n = -1;
13321 break;
13322 }
13323 rettv->vval.v_number = n;
13324}
13325
13326/*
13327 * "undofile(name)" function
13328 */
13329 static void
13330f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13331{
13332 rettv->v_type = VAR_STRING;
13333#ifdef FEAT_PERSISTENT_UNDO
13334 {
13335 char_u *fname = get_tv_string(&argvars[0]);
13336
13337 if (*fname == NUL)
13338 {
13339 /* If there is no file name there will be no undo file. */
13340 rettv->vval.v_string = NULL;
13341 }
13342 else
13343 {
13344 char_u *ffname = FullName_save(fname, FALSE);
13345
13346 if (ffname != NULL)
13347 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13348 vim_free(ffname);
13349 }
13350 }
13351#else
13352 rettv->vval.v_string = NULL;
13353#endif
13354}
13355
13356/*
13357 * "undotree()" function
13358 */
13359 static void
13360f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13361{
13362 if (rettv_dict_alloc(rettv) == OK)
13363 {
13364 dict_T *dict = rettv->vval.v_dict;
13365 list_T *list;
13366
13367 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13368 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13369 dict_add_nr_str(dict, "save_last",
13370 (long)curbuf->b_u_save_nr_last, NULL);
13371 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13372 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13373 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13374
13375 list = list_alloc();
13376 if (list != NULL)
13377 {
13378 u_eval_tree(curbuf->b_u_oldhead, list);
13379 dict_add_list(dict, "entries", list);
13380 }
13381 }
13382}
13383
13384/*
13385 * "values(dict)" function
13386 */
13387 static void
13388f_values(typval_T *argvars, typval_T *rettv)
13389{
13390 dict_list(argvars, rettv, 1);
13391}
13392
13393/*
13394 * "virtcol(string)" function
13395 */
13396 static void
13397f_virtcol(typval_T *argvars, typval_T *rettv)
13398{
13399 colnr_T vcol = 0;
13400 pos_T *fp;
13401 int fnum = curbuf->b_fnum;
13402
13403 fp = var2fpos(&argvars[0], FALSE, &fnum);
13404 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13405 && fnum == curbuf->b_fnum)
13406 {
13407 getvvcol(curwin, fp, NULL, NULL, &vcol);
13408 ++vcol;
13409 }
13410
13411 rettv->vval.v_number = vcol;
13412}
13413
13414/*
13415 * "visualmode()" function
13416 */
13417 static void
13418f_visualmode(typval_T *argvars, typval_T *rettv)
13419{
13420 char_u str[2];
13421
13422 rettv->v_type = VAR_STRING;
13423 str[0] = curbuf->b_visual_mode_eval;
13424 str[1] = NUL;
13425 rettv->vval.v_string = vim_strsave(str);
13426
13427 /* A non-zero number or non-empty string argument: reset mode. */
13428 if (non_zero_arg(&argvars[0]))
13429 curbuf->b_visual_mode_eval = NUL;
13430}
13431
13432/*
13433 * "wildmenumode()" function
13434 */
13435 static void
13436f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13437{
13438#ifdef FEAT_WILDMENU
13439 if (wild_menu_showing)
13440 rettv->vval.v_number = 1;
13441#endif
13442}
13443
13444/*
13445 * "winbufnr(nr)" function
13446 */
13447 static void
13448f_winbufnr(typval_T *argvars, typval_T *rettv)
13449{
13450 win_T *wp;
13451
13452 wp = find_win_by_nr(&argvars[0], NULL);
13453 if (wp == NULL)
13454 rettv->vval.v_number = -1;
13455 else
13456 rettv->vval.v_number = wp->w_buffer->b_fnum;
13457}
13458
13459/*
13460 * "wincol()" function
13461 */
13462 static void
13463f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13464{
13465 validate_cursor();
13466 rettv->vval.v_number = curwin->w_wcol + 1;
13467}
13468
13469/*
13470 * "winheight(nr)" function
13471 */
13472 static void
13473f_winheight(typval_T *argvars, typval_T *rettv)
13474{
13475 win_T *wp;
13476
13477 wp = find_win_by_nr(&argvars[0], NULL);
13478 if (wp == NULL)
13479 rettv->vval.v_number = -1;
13480 else
13481 rettv->vval.v_number = wp->w_height;
13482}
13483
13484/*
13485 * "winline()" function
13486 */
13487 static void
13488f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13489{
13490 validate_cursor();
13491 rettv->vval.v_number = curwin->w_wrow + 1;
13492}
13493
13494/*
13495 * "winnr()" function
13496 */
13497 static void
13498f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13499{
13500 int nr = 1;
13501
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013502 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013503 rettv->vval.v_number = nr;
13504}
13505
13506/*
13507 * "winrestcmd()" function
13508 */
13509 static void
13510f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13511{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013512 win_T *wp;
13513 int winnr = 1;
13514 garray_T ga;
13515 char_u buf[50];
13516
13517 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013518 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013519 {
13520 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13521 ga_concat(&ga, buf);
13522 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13523 ga_concat(&ga, buf);
13524 ++winnr;
13525 }
13526 ga_append(&ga, NUL);
13527
13528 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013529 rettv->v_type = VAR_STRING;
13530}
13531
13532/*
13533 * "winrestview()" function
13534 */
13535 static void
13536f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13537{
13538 dict_T *dict;
13539
13540 if (argvars[0].v_type != VAR_DICT
13541 || (dict = argvars[0].vval.v_dict) == NULL)
13542 EMSG(_(e_invarg));
13543 else
13544 {
13545 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13546 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13547 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13548 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13549#ifdef FEAT_VIRTUALEDIT
13550 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13551 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13552#endif
13553 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13554 {
13555 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13556 curwin->w_set_curswant = FALSE;
13557 }
13558
13559 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13560 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13561#ifdef FEAT_DIFF
13562 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13563 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13564#endif
13565 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13566 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13567 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13568 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13569
13570 check_cursor();
13571 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013572 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013573 changed_window_setting();
13574
13575 if (curwin->w_topline <= 0)
13576 curwin->w_topline = 1;
13577 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13578 curwin->w_topline = curbuf->b_ml.ml_line_count;
13579#ifdef FEAT_DIFF
13580 check_topfill(curwin, TRUE);
13581#endif
13582 }
13583}
13584
13585/*
13586 * "winsaveview()" function
13587 */
13588 static void
13589f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13590{
13591 dict_T *dict;
13592
13593 if (rettv_dict_alloc(rettv) == FAIL)
13594 return;
13595 dict = rettv->vval.v_dict;
13596
13597 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13598 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13599#ifdef FEAT_VIRTUALEDIT
13600 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13601#endif
13602 update_curswant();
13603 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13604
13605 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13606#ifdef FEAT_DIFF
13607 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13608#endif
13609 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13610 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13611}
13612
13613/*
13614 * "winwidth(nr)" function
13615 */
13616 static void
13617f_winwidth(typval_T *argvars, typval_T *rettv)
13618{
13619 win_T *wp;
13620
13621 wp = find_win_by_nr(&argvars[0], NULL);
13622 if (wp == NULL)
13623 rettv->vval.v_number = -1;
13624 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013625 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013626}
13627
13628/*
13629 * "wordcount()" function
13630 */
13631 static void
13632f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13633{
13634 if (rettv_dict_alloc(rettv) == FAIL)
13635 return;
13636 cursor_pos_info(rettv->vval.v_dict);
13637}
13638
13639/*
13640 * "writefile()" function
13641 */
13642 static void
13643f_writefile(typval_T *argvars, typval_T *rettv)
13644{
13645 int binary = FALSE;
13646 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013647#ifdef HAVE_FSYNC
13648 int do_fsync = p_fs;
13649#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013650 char_u *fname;
13651 FILE *fd;
13652 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013653 listitem_T *li;
13654 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013655
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013656 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013657 if (check_restricted() || check_secure())
13658 return;
13659
13660 if (argvars[0].v_type != VAR_LIST)
13661 {
13662 EMSG2(_(e_listarg), "writefile()");
13663 return;
13664 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013665 list = argvars[0].vval.v_list;
13666 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013667 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013668 for (li = list->lv_first; li != NULL; li = li->li_next)
13669 if (get_tv_string_chk(&li->li_tv) == NULL)
13670 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013671
13672 if (argvars[2].v_type != VAR_UNKNOWN)
13673 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013674 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13675
13676 if (arg2 == NULL)
13677 return;
13678 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013679 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013680 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013681 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013682#ifdef HAVE_FSYNC
13683 if (vim_strchr(arg2, 's') != NULL)
13684 do_fsync = TRUE;
13685 else if (vim_strchr(arg2, 'S') != NULL)
13686 do_fsync = FALSE;
13687#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013688 }
13689
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013690 fname = get_tv_string_chk(&argvars[1]);
13691 if (fname == NULL)
13692 return;
13693
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013694 /* Always open the file in binary mode, library functions have a mind of
13695 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013696 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13697 append ? APPENDBIN : WRITEBIN)) == NULL)
13698 {
13699 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13700 ret = -1;
13701 }
13702 else
13703 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013704 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013705 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013706#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013707 else if (do_fsync)
13708 /* Ignore the error, the user wouldn't know what to do about it.
13709 * May happen for a device. */
13710 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013711#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013712 fclose(fd);
13713 }
13714
13715 rettv->vval.v_number = ret;
13716}
13717
13718/*
13719 * "xor(expr, expr)" function
13720 */
13721 static void
13722f_xor(typval_T *argvars, typval_T *rettv)
13723{
13724 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13725 ^ get_tv_number_chk(&argvars[1], NULL);
13726}
13727
13728
13729#endif /* FEAT_EVAL */