blob: 46e55b3fd202eb08eca16fc46ec1a0f15f8d2559 [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},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200685 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200686 {"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 Moolenaarf59c6e82018-04-10 15:59:11 +0200859# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
860 {"term_getansicolors", 1, 1, f_term_getansicolors},
861# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200862 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200863 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200864 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200865 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200866 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200867 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200868 {"term_getstatus", 1, 1, f_term_getstatus},
869 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200870 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200871 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200872 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200873 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200874# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
875 {"term_setansicolors", 2, 2, f_term_setansicolors},
876# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100877 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100878 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200879 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200880 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200881 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200882#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200883 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
884 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200885 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200886 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100887 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200888#ifdef FEAT_JOB_CHANNEL
889 {"test_null_channel", 0, 0, f_test_null_channel},
890#endif
891 {"test_null_dict", 0, 0, f_test_null_dict},
892#ifdef FEAT_JOB_CHANNEL
893 {"test_null_job", 0, 0, f_test_null_job},
894#endif
895 {"test_null_list", 0, 0, f_test_null_list},
896 {"test_null_partial", 0, 0, f_test_null_partial},
897 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100898 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200899 {"test_settime", 1, 1, f_test_settime},
900#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200901 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200902 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200903 {"timer_start", 2, 3, f_timer_start},
904 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200905 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200906#endif
907 {"tolower", 1, 1, f_tolower},
908 {"toupper", 1, 1, f_toupper},
909 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100910 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200911#ifdef FEAT_FLOAT
912 {"trunc", 1, 1, f_trunc},
913#endif
914 {"type", 1, 1, f_type},
915 {"undofile", 1, 1, f_undofile},
916 {"undotree", 0, 0, f_undotree},
917 {"uniq", 1, 3, f_uniq},
918 {"values", 1, 1, f_values},
919 {"virtcol", 1, 1, f_virtcol},
920 {"visualmode", 0, 1, f_visualmode},
921 {"wildmenumode", 0, 0, f_wildmenumode},
922 {"win_findbuf", 1, 1, f_win_findbuf},
923 {"win_getid", 0, 2, f_win_getid},
924 {"win_gotoid", 1, 1, f_win_gotoid},
925 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
926 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100927 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200928 {"winbufnr", 1, 1, f_winbufnr},
929 {"wincol", 0, 0, f_wincol},
930 {"winheight", 1, 1, f_winheight},
931 {"winline", 0, 0, f_winline},
932 {"winnr", 0, 1, f_winnr},
933 {"winrestcmd", 0, 0, f_winrestcmd},
934 {"winrestview", 1, 1, f_winrestview},
935 {"winsaveview", 0, 0, f_winsaveview},
936 {"winwidth", 1, 1, f_winwidth},
937 {"wordcount", 0, 0, f_wordcount},
938 {"writefile", 2, 3, f_writefile},
939 {"xor", 2, 2, f_xor},
940};
941
942#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
943
944/*
945 * Function given to ExpandGeneric() to obtain the list of internal
946 * or user defined function names.
947 */
948 char_u *
949get_function_name(expand_T *xp, int idx)
950{
951 static int intidx = -1;
952 char_u *name;
953
954 if (idx == 0)
955 intidx = -1;
956 if (intidx < 0)
957 {
958 name = get_user_func_name(xp, idx);
959 if (name != NULL)
960 return name;
961 }
962 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
963 {
964 STRCPY(IObuff, functions[intidx].f_name);
965 STRCAT(IObuff, "(");
966 if (functions[intidx].f_max_argc == 0)
967 STRCAT(IObuff, ")");
968 return IObuff;
969 }
970
971 return NULL;
972}
973
974/*
975 * Function given to ExpandGeneric() to obtain the list of internal or
976 * user defined variable or function names.
977 */
978 char_u *
979get_expr_name(expand_T *xp, int idx)
980{
981 static int intidx = -1;
982 char_u *name;
983
984 if (idx == 0)
985 intidx = -1;
986 if (intidx < 0)
987 {
988 name = get_function_name(xp, idx);
989 if (name != NULL)
990 return name;
991 }
992 return get_user_var_name(xp, ++intidx);
993}
994
995#endif /* FEAT_CMDL_COMPL */
996
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200997/*
998 * Find internal function in table above.
999 * Return index, or -1 if not found
1000 */
1001 int
1002find_internal_func(
1003 char_u *name) /* name of the function */
1004{
1005 int first = 0;
1006 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1007 int cmp;
1008 int x;
1009
1010 /*
1011 * Find the function name in the table. Binary search.
1012 */
1013 while (first <= last)
1014 {
1015 x = first + ((unsigned)(last - first) >> 1);
1016 cmp = STRCMP(name, functions[x].f_name);
1017 if (cmp < 0)
1018 last = x - 1;
1019 else if (cmp > 0)
1020 first = x + 1;
1021 else
1022 return x;
1023 }
1024 return -1;
1025}
1026
1027 int
1028call_internal_func(
1029 char_u *name,
1030 int argcount,
1031 typval_T *argvars,
1032 typval_T *rettv)
1033{
1034 int i;
1035
1036 i = find_internal_func(name);
1037 if (i < 0)
1038 return ERROR_UNKNOWN;
1039 if (argcount < functions[i].f_min_argc)
1040 return ERROR_TOOFEW;
1041 if (argcount > functions[i].f_max_argc)
1042 return ERROR_TOOMANY;
1043 argvars[argcount].v_type = VAR_UNKNOWN;
1044 functions[i].f_func(argvars, rettv);
1045 return ERROR_NONE;
1046}
1047
1048/*
1049 * Return TRUE for a non-zero Number and a non-empty String.
1050 */
1051 static int
1052non_zero_arg(typval_T *argvars)
1053{
1054 return ((argvars[0].v_type == VAR_NUMBER
1055 && argvars[0].vval.v_number != 0)
1056 || (argvars[0].v_type == VAR_SPECIAL
1057 && argvars[0].vval.v_number == VVAL_TRUE)
1058 || (argvars[0].v_type == VAR_STRING
1059 && argvars[0].vval.v_string != NULL
1060 && *argvars[0].vval.v_string != NUL));
1061}
1062
1063/*
1064 * Get the lnum from the first argument.
1065 * Also accepts ".", "$", etc., but that only works for the current buffer.
1066 * Returns -1 on error.
1067 */
1068 static linenr_T
1069get_tv_lnum(typval_T *argvars)
1070{
1071 typval_T rettv;
1072 linenr_T lnum;
1073
1074 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1075 if (lnum == 0) /* no valid number, try using line() */
1076 {
1077 rettv.v_type = VAR_NUMBER;
1078 f_line(argvars, &rettv);
1079 lnum = (linenr_T)rettv.vval.v_number;
1080 clear_tv(&rettv);
1081 }
1082 return lnum;
1083}
1084
1085#ifdef FEAT_FLOAT
1086static int get_float_arg(typval_T *argvars, float_T *f);
1087
1088/*
1089 * Get the float value of "argvars[0]" into "f".
1090 * Returns FAIL when the argument is not a Number or Float.
1091 */
1092 static int
1093get_float_arg(typval_T *argvars, float_T *f)
1094{
1095 if (argvars[0].v_type == VAR_FLOAT)
1096 {
1097 *f = argvars[0].vval.v_float;
1098 return OK;
1099 }
1100 if (argvars[0].v_type == VAR_NUMBER)
1101 {
1102 *f = (float_T)argvars[0].vval.v_number;
1103 return OK;
1104 }
1105 EMSG(_("E808: Number or Float required"));
1106 return FAIL;
1107}
1108
1109/*
1110 * "abs(expr)" function
1111 */
1112 static void
1113f_abs(typval_T *argvars, typval_T *rettv)
1114{
1115 if (argvars[0].v_type == VAR_FLOAT)
1116 {
1117 rettv->v_type = VAR_FLOAT;
1118 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1119 }
1120 else
1121 {
1122 varnumber_T n;
1123 int error = FALSE;
1124
1125 n = get_tv_number_chk(&argvars[0], &error);
1126 if (error)
1127 rettv->vval.v_number = -1;
1128 else if (n > 0)
1129 rettv->vval.v_number = n;
1130 else
1131 rettv->vval.v_number = -n;
1132 }
1133}
1134
1135/*
1136 * "acos()" function
1137 */
1138 static void
1139f_acos(typval_T *argvars, typval_T *rettv)
1140{
1141 float_T f = 0.0;
1142
1143 rettv->v_type = VAR_FLOAT;
1144 if (get_float_arg(argvars, &f) == OK)
1145 rettv->vval.v_float = acos(f);
1146 else
1147 rettv->vval.v_float = 0.0;
1148}
1149#endif
1150
1151/*
1152 * "add(list, item)" function
1153 */
1154 static void
1155f_add(typval_T *argvars, typval_T *rettv)
1156{
1157 list_T *l;
1158
1159 rettv->vval.v_number = 1; /* Default: Failed */
1160 if (argvars[0].v_type == VAR_LIST)
1161 {
1162 if ((l = argvars[0].vval.v_list) != NULL
1163 && !tv_check_lock(l->lv_lock,
1164 (char_u *)N_("add() argument"), TRUE)
1165 && list_append_tv(l, &argvars[1]) == OK)
1166 copy_tv(&argvars[0], rettv);
1167 }
1168 else
1169 EMSG(_(e_listreq));
1170}
1171
1172/*
1173 * "and(expr, expr)" function
1174 */
1175 static void
1176f_and(typval_T *argvars, typval_T *rettv)
1177{
1178 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1179 & get_tv_number_chk(&argvars[1], NULL);
1180}
1181
1182/*
1183 * "append(lnum, string/list)" function
1184 */
1185 static void
1186f_append(typval_T *argvars, typval_T *rettv)
1187{
1188 long lnum;
1189 char_u *line;
1190 list_T *l = NULL;
1191 listitem_T *li = NULL;
1192 typval_T *tv;
1193 long added = 0;
1194
1195 /* When coming here from Insert mode, sync undo, so that this can be
1196 * undone separately from what was previously inserted. */
1197 if (u_sync_once == 2)
1198 {
1199 u_sync_once = 1; /* notify that u_sync() was called */
1200 u_sync(TRUE);
1201 }
1202
1203 lnum = get_tv_lnum(argvars);
1204 if (lnum >= 0
1205 && lnum <= curbuf->b_ml.ml_line_count
1206 && u_save(lnum, lnum + 1) == OK)
1207 {
1208 if (argvars[1].v_type == VAR_LIST)
1209 {
1210 l = argvars[1].vval.v_list;
1211 if (l == NULL)
1212 return;
1213 li = l->lv_first;
1214 }
1215 for (;;)
1216 {
1217 if (l == NULL)
1218 tv = &argvars[1]; /* append a string */
1219 else if (li == NULL)
1220 break; /* end of list */
1221 else
1222 tv = &li->li_tv; /* append item from list */
1223 line = get_tv_string_chk(tv);
1224 if (line == NULL) /* type error */
1225 {
1226 rettv->vval.v_number = 1; /* Failed */
1227 break;
1228 }
1229 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1230 ++added;
1231 if (l == NULL)
1232 break;
1233 li = li->li_next;
1234 }
1235
1236 appended_lines_mark(lnum, added);
1237 if (curwin->w_cursor.lnum > lnum)
1238 curwin->w_cursor.lnum += added;
1239 }
1240 else
1241 rettv->vval.v_number = 1; /* Failed */
1242}
1243
1244/*
1245 * "argc()" function
1246 */
1247 static void
1248f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1249{
1250 rettv->vval.v_number = ARGCOUNT;
1251}
1252
1253/*
1254 * "argidx()" function
1255 */
1256 static void
1257f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1258{
1259 rettv->vval.v_number = curwin->w_arg_idx;
1260}
1261
1262/*
1263 * "arglistid()" function
1264 */
1265 static void
1266f_arglistid(typval_T *argvars, typval_T *rettv)
1267{
1268 win_T *wp;
1269
1270 rettv->vval.v_number = -1;
1271 wp = find_tabwin(&argvars[0], &argvars[1]);
1272 if (wp != NULL)
1273 rettv->vval.v_number = wp->w_alist->id;
1274}
1275
1276/*
1277 * "argv(nr)" function
1278 */
1279 static void
1280f_argv(typval_T *argvars, typval_T *rettv)
1281{
1282 int idx;
1283
1284 if (argvars[0].v_type != VAR_UNKNOWN)
1285 {
1286 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1287 if (idx >= 0 && idx < ARGCOUNT)
1288 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1289 else
1290 rettv->vval.v_string = NULL;
1291 rettv->v_type = VAR_STRING;
1292 }
1293 else if (rettv_list_alloc(rettv) == OK)
1294 for (idx = 0; idx < ARGCOUNT; ++idx)
1295 list_append_string(rettv->vval.v_list,
1296 alist_name(&ARGLIST[idx]), -1);
1297}
1298
1299/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001300 * "assert_beeps(cmd [, error])" function
1301 */
1302 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001303f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001304{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001305 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001306}
1307
1308/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001309 * "assert_equal(expected, actual[, msg])" function
1310 */
1311 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001312f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001313{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001314 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001315}
1316
1317/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001318 * "assert_equalfile(fname-one, fname-two)" function
1319 */
1320 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001321f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001322{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001323 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001324}
1325
1326/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001327 * "assert_notequal(expected, actual[, msg])" function
1328 */
1329 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001330f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001331{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001332 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001333}
1334
1335/*
1336 * "assert_exception(string[, msg])" function
1337 */
1338 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001339f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001340{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001341 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001342}
1343
1344/*
1345 * "assert_fails(cmd [, error])" function
1346 */
1347 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001348f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001349{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001350 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001351}
1352
1353/*
1354 * "assert_false(actual[, msg])" function
1355 */
1356 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001357f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001358{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001359 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001360}
1361
1362/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001363 * "assert_inrange(lower, upper[, msg])" function
1364 */
1365 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001366f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001367{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001368 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001369}
1370
1371/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001372 * "assert_match(pattern, actual[, msg])" function
1373 */
1374 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001375f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001376{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001377 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001378}
1379
1380/*
1381 * "assert_notmatch(pattern, actual[, msg])" function
1382 */
1383 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001384f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001385{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001386 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001387}
1388
1389/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001390 * "assert_report(msg)" function
1391 */
1392 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001393f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001394{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001395 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001396}
1397
1398/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001399 * "assert_true(actual[, msg])" function
1400 */
1401 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001402f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001403{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001404 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001405}
1406
1407#ifdef FEAT_FLOAT
1408/*
1409 * "asin()" function
1410 */
1411 static void
1412f_asin(typval_T *argvars, typval_T *rettv)
1413{
1414 float_T f = 0.0;
1415
1416 rettv->v_type = VAR_FLOAT;
1417 if (get_float_arg(argvars, &f) == OK)
1418 rettv->vval.v_float = asin(f);
1419 else
1420 rettv->vval.v_float = 0.0;
1421}
1422
1423/*
1424 * "atan()" function
1425 */
1426 static void
1427f_atan(typval_T *argvars, typval_T *rettv)
1428{
1429 float_T f = 0.0;
1430
1431 rettv->v_type = VAR_FLOAT;
1432 if (get_float_arg(argvars, &f) == OK)
1433 rettv->vval.v_float = atan(f);
1434 else
1435 rettv->vval.v_float = 0.0;
1436}
1437
1438/*
1439 * "atan2()" function
1440 */
1441 static void
1442f_atan2(typval_T *argvars, typval_T *rettv)
1443{
1444 float_T fx = 0.0, fy = 0.0;
1445
1446 rettv->v_type = VAR_FLOAT;
1447 if (get_float_arg(argvars, &fx) == OK
1448 && get_float_arg(&argvars[1], &fy) == OK)
1449 rettv->vval.v_float = atan2(fx, fy);
1450 else
1451 rettv->vval.v_float = 0.0;
1452}
1453#endif
1454
1455/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001456 * "balloon_show()" function
1457 */
1458#ifdef FEAT_BEVAL
1459 static void
1460f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1461{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001462 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001463 {
1464 if (argvars[0].v_type == VAR_LIST
1465# ifdef FEAT_GUI
1466 && !gui.in_use
1467# endif
1468 )
1469 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1470 else
1471 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1472 }
1473}
1474
Bram Moolenaar669a8282017-11-19 20:13:05 +01001475# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001476 static void
1477f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1478{
1479 if (rettv_list_alloc(rettv) == OK)
1480 {
1481 char_u *msg = get_tv_string_chk(&argvars[0]);
1482
1483 if (msg != NULL)
1484 {
1485 pumitem_T *array;
1486 int size = split_message(msg, &array);
1487 int i;
1488
1489 /* Skip the first and last item, they are always empty. */
1490 for (i = 1; i < size - 1; ++i)
1491 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001492 while (size > 0)
1493 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001494 vim_free(array);
1495 }
1496 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001497}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001498# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001499#endif
1500
1501/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001502 * "browse(save, title, initdir, default)" function
1503 */
1504 static void
1505f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1506{
1507#ifdef FEAT_BROWSE
1508 int save;
1509 char_u *title;
1510 char_u *initdir;
1511 char_u *defname;
1512 char_u buf[NUMBUFLEN];
1513 char_u buf2[NUMBUFLEN];
1514 int error = FALSE;
1515
1516 save = (int)get_tv_number_chk(&argvars[0], &error);
1517 title = get_tv_string_chk(&argvars[1]);
1518 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1519 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1520
1521 if (error || title == NULL || initdir == NULL || defname == NULL)
1522 rettv->vval.v_string = NULL;
1523 else
1524 rettv->vval.v_string =
1525 do_browse(save ? BROWSE_SAVE : 0,
1526 title, defname, NULL, initdir, NULL, curbuf);
1527#else
1528 rettv->vval.v_string = NULL;
1529#endif
1530 rettv->v_type = VAR_STRING;
1531}
1532
1533/*
1534 * "browsedir(title, initdir)" function
1535 */
1536 static void
1537f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1538{
1539#ifdef FEAT_BROWSE
1540 char_u *title;
1541 char_u *initdir;
1542 char_u buf[NUMBUFLEN];
1543
1544 title = get_tv_string_chk(&argvars[0]);
1545 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1546
1547 if (title == NULL || initdir == NULL)
1548 rettv->vval.v_string = NULL;
1549 else
1550 rettv->vval.v_string = do_browse(BROWSE_DIR,
1551 title, NULL, NULL, initdir, NULL, curbuf);
1552#else
1553 rettv->vval.v_string = NULL;
1554#endif
1555 rettv->v_type = VAR_STRING;
1556}
1557
1558static buf_T *find_buffer(typval_T *avar);
1559
1560/*
1561 * Find a buffer by number or exact name.
1562 */
1563 static buf_T *
1564find_buffer(typval_T *avar)
1565{
1566 buf_T *buf = NULL;
1567
1568 if (avar->v_type == VAR_NUMBER)
1569 buf = buflist_findnr((int)avar->vval.v_number);
1570 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1571 {
1572 buf = buflist_findname_exp(avar->vval.v_string);
1573 if (buf == NULL)
1574 {
1575 /* No full path name match, try a match with a URL or a "nofile"
1576 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001577 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001578 if (buf->b_fname != NULL
1579 && (path_with_url(buf->b_fname)
1580#ifdef FEAT_QUICKFIX
1581 || bt_nofile(buf)
1582#endif
1583 )
1584 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1585 break;
1586 }
1587 }
1588 return buf;
1589}
1590
1591/*
1592 * "bufexists(expr)" function
1593 */
1594 static void
1595f_bufexists(typval_T *argvars, typval_T *rettv)
1596{
1597 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1598}
1599
1600/*
1601 * "buflisted(expr)" function
1602 */
1603 static void
1604f_buflisted(typval_T *argvars, typval_T *rettv)
1605{
1606 buf_T *buf;
1607
1608 buf = find_buffer(&argvars[0]);
1609 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1610}
1611
1612/*
1613 * "bufloaded(expr)" function
1614 */
1615 static void
1616f_bufloaded(typval_T *argvars, typval_T *rettv)
1617{
1618 buf_T *buf;
1619
1620 buf = find_buffer(&argvars[0]);
1621 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1622}
1623
1624 buf_T *
1625buflist_find_by_name(char_u *name, int curtab_only)
1626{
1627 int save_magic;
1628 char_u *save_cpo;
1629 buf_T *buf;
1630
1631 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1632 save_magic = p_magic;
1633 p_magic = TRUE;
1634 save_cpo = p_cpo;
1635 p_cpo = (char_u *)"";
1636
1637 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1638 TRUE, FALSE, curtab_only));
1639
1640 p_magic = save_magic;
1641 p_cpo = save_cpo;
1642 return buf;
1643}
1644
1645/*
1646 * Get buffer by number or pattern.
1647 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001648 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001649get_buf_tv(typval_T *tv, int curtab_only)
1650{
1651 char_u *name = tv->vval.v_string;
1652 buf_T *buf;
1653
1654 if (tv->v_type == VAR_NUMBER)
1655 return buflist_findnr((int)tv->vval.v_number);
1656 if (tv->v_type != VAR_STRING)
1657 return NULL;
1658 if (name == NULL || *name == NUL)
1659 return curbuf;
1660 if (name[0] == '$' && name[1] == NUL)
1661 return lastbuf;
1662
1663 buf = buflist_find_by_name(name, curtab_only);
1664
1665 /* If not found, try expanding the name, like done for bufexists(). */
1666 if (buf == NULL)
1667 buf = find_buffer(tv);
1668
1669 return buf;
1670}
1671
1672/*
1673 * "bufname(expr)" function
1674 */
1675 static void
1676f_bufname(typval_T *argvars, typval_T *rettv)
1677{
1678 buf_T *buf;
1679
1680 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1681 ++emsg_off;
1682 buf = get_buf_tv(&argvars[0], FALSE);
1683 rettv->v_type = VAR_STRING;
1684 if (buf != NULL && buf->b_fname != NULL)
1685 rettv->vval.v_string = vim_strsave(buf->b_fname);
1686 else
1687 rettv->vval.v_string = NULL;
1688 --emsg_off;
1689}
1690
1691/*
1692 * "bufnr(expr)" function
1693 */
1694 static void
1695f_bufnr(typval_T *argvars, typval_T *rettv)
1696{
1697 buf_T *buf;
1698 int error = FALSE;
1699 char_u *name;
1700
1701 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1702 ++emsg_off;
1703 buf = get_buf_tv(&argvars[0], FALSE);
1704 --emsg_off;
1705
1706 /* If the buffer isn't found and the second argument is not zero create a
1707 * new buffer. */
1708 if (buf == NULL
1709 && argvars[1].v_type != VAR_UNKNOWN
1710 && get_tv_number_chk(&argvars[1], &error) != 0
1711 && !error
1712 && (name = get_tv_string_chk(&argvars[0])) != NULL
1713 && !error)
1714 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1715
1716 if (buf != NULL)
1717 rettv->vval.v_number = buf->b_fnum;
1718 else
1719 rettv->vval.v_number = -1;
1720}
1721
1722 static void
1723buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1724{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001725 win_T *wp;
1726 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001727 buf_T *buf;
1728
1729 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1730 ++emsg_off;
1731 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001732 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001733 {
1734 ++winnr;
1735 if (wp->w_buffer == buf)
1736 break;
1737 }
1738 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001739 --emsg_off;
1740}
1741
1742/*
1743 * "bufwinid(nr)" function
1744 */
1745 static void
1746f_bufwinid(typval_T *argvars, typval_T *rettv)
1747{
1748 buf_win_common(argvars, rettv, FALSE);
1749}
1750
1751/*
1752 * "bufwinnr(nr)" function
1753 */
1754 static void
1755f_bufwinnr(typval_T *argvars, typval_T *rettv)
1756{
1757 buf_win_common(argvars, rettv, TRUE);
1758}
1759
1760/*
1761 * "byte2line(byte)" function
1762 */
1763 static void
1764f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1765{
1766#ifndef FEAT_BYTEOFF
1767 rettv->vval.v_number = -1;
1768#else
1769 long boff = 0;
1770
1771 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1772 if (boff < 0)
1773 rettv->vval.v_number = -1;
1774 else
1775 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1776 (linenr_T)0, &boff);
1777#endif
1778}
1779
1780 static void
1781byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1782{
1783#ifdef FEAT_MBYTE
1784 char_u *t;
1785#endif
1786 char_u *str;
1787 varnumber_T idx;
1788
1789 str = get_tv_string_chk(&argvars[0]);
1790 idx = get_tv_number_chk(&argvars[1], NULL);
1791 rettv->vval.v_number = -1;
1792 if (str == NULL || idx < 0)
1793 return;
1794
1795#ifdef FEAT_MBYTE
1796 t = str;
1797 for ( ; idx > 0; idx--)
1798 {
1799 if (*t == NUL) /* EOL reached */
1800 return;
1801 if (enc_utf8 && comp)
1802 t += utf_ptr2len(t);
1803 else
1804 t += (*mb_ptr2len)(t);
1805 }
1806 rettv->vval.v_number = (varnumber_T)(t - str);
1807#else
1808 if ((size_t)idx <= STRLEN(str))
1809 rettv->vval.v_number = idx;
1810#endif
1811}
1812
1813/*
1814 * "byteidx()" function
1815 */
1816 static void
1817f_byteidx(typval_T *argvars, typval_T *rettv)
1818{
1819 byteidx(argvars, rettv, FALSE);
1820}
1821
1822/*
1823 * "byteidxcomp()" function
1824 */
1825 static void
1826f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1827{
1828 byteidx(argvars, rettv, TRUE);
1829}
1830
1831/*
1832 * "call(func, arglist [, dict])" function
1833 */
1834 static void
1835f_call(typval_T *argvars, typval_T *rettv)
1836{
1837 char_u *func;
1838 partial_T *partial = NULL;
1839 dict_T *selfdict = NULL;
1840
1841 if (argvars[1].v_type != VAR_LIST)
1842 {
1843 EMSG(_(e_listreq));
1844 return;
1845 }
1846 if (argvars[1].vval.v_list == NULL)
1847 return;
1848
1849 if (argvars[0].v_type == VAR_FUNC)
1850 func = argvars[0].vval.v_string;
1851 else if (argvars[0].v_type == VAR_PARTIAL)
1852 {
1853 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001854 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001855 }
1856 else
1857 func = get_tv_string(&argvars[0]);
1858 if (*func == NUL)
1859 return; /* type error or empty name */
1860
1861 if (argvars[2].v_type != VAR_UNKNOWN)
1862 {
1863 if (argvars[2].v_type != VAR_DICT)
1864 {
1865 EMSG(_(e_dictreq));
1866 return;
1867 }
1868 selfdict = argvars[2].vval.v_dict;
1869 }
1870
1871 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1872}
1873
1874#ifdef FEAT_FLOAT
1875/*
1876 * "ceil({float})" function
1877 */
1878 static void
1879f_ceil(typval_T *argvars, typval_T *rettv)
1880{
1881 float_T f = 0.0;
1882
1883 rettv->v_type = VAR_FLOAT;
1884 if (get_float_arg(argvars, &f) == OK)
1885 rettv->vval.v_float = ceil(f);
1886 else
1887 rettv->vval.v_float = 0.0;
1888}
1889#endif
1890
1891#ifdef FEAT_JOB_CHANNEL
1892/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001893 * "ch_canread()" function
1894 */
1895 static void
1896f_ch_canread(typval_T *argvars, typval_T *rettv)
1897{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001898 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001899
1900 rettv->vval.v_number = 0;
1901 if (channel != NULL)
1902 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1903 || channel_has_readahead(channel, PART_OUT)
1904 || channel_has_readahead(channel, PART_ERR);
1905}
1906
1907/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001908 * "ch_close()" function
1909 */
1910 static void
1911f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1912{
1913 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1914
1915 if (channel != NULL)
1916 {
1917 channel_close(channel, FALSE);
1918 channel_clear(channel);
1919 }
1920}
1921
1922/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001923 * "ch_close()" function
1924 */
1925 static void
1926f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1927{
1928 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1929
1930 if (channel != NULL)
1931 channel_close_in(channel);
1932}
1933
1934/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001935 * "ch_getbufnr()" function
1936 */
1937 static void
1938f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1939{
1940 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1941
1942 rettv->vval.v_number = -1;
1943 if (channel != NULL)
1944 {
1945 char_u *what = get_tv_string(&argvars[1]);
1946 int part;
1947
1948 if (STRCMP(what, "err") == 0)
1949 part = PART_ERR;
1950 else if (STRCMP(what, "out") == 0)
1951 part = PART_OUT;
1952 else if (STRCMP(what, "in") == 0)
1953 part = PART_IN;
1954 else
1955 part = PART_SOCK;
1956 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1957 rettv->vval.v_number =
1958 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1959 }
1960}
1961
1962/*
1963 * "ch_getjob()" function
1964 */
1965 static void
1966f_ch_getjob(typval_T *argvars, typval_T *rettv)
1967{
1968 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1969
1970 if (channel != NULL)
1971 {
1972 rettv->v_type = VAR_JOB;
1973 rettv->vval.v_job = channel->ch_job;
1974 if (channel->ch_job != NULL)
1975 ++channel->ch_job->jv_refcount;
1976 }
1977}
1978
1979/*
1980 * "ch_info()" function
1981 */
1982 static void
1983f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1984{
1985 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1986
1987 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1988 channel_info(channel, rettv->vval.v_dict);
1989}
1990
1991/*
1992 * "ch_log()" function
1993 */
1994 static void
1995f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1996{
1997 char_u *msg = get_tv_string(&argvars[0]);
1998 channel_T *channel = NULL;
1999
2000 if (argvars[1].v_type != VAR_UNKNOWN)
2001 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2002
Bram Moolenaard5359b22018-04-05 22:44:39 +02002003 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002004}
2005
2006/*
2007 * "ch_logfile()" function
2008 */
2009 static void
2010f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2011{
2012 char_u *fname;
2013 char_u *opt = (char_u *)"";
2014 char_u buf[NUMBUFLEN];
2015
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002016 /* Don't open a file in restricted mode. */
2017 if (check_restricted() || check_secure())
2018 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002019 fname = get_tv_string(&argvars[0]);
2020 if (argvars[1].v_type == VAR_STRING)
2021 opt = get_tv_string_buf(&argvars[1], buf);
2022 ch_logfile(fname, opt);
2023}
2024
2025/*
2026 * "ch_open()" function
2027 */
2028 static void
2029f_ch_open(typval_T *argvars, typval_T *rettv)
2030{
2031 rettv->v_type = VAR_CHANNEL;
2032 if (check_restricted() || check_secure())
2033 return;
2034 rettv->vval.v_channel = channel_open_func(argvars);
2035}
2036
2037/*
2038 * "ch_read()" function
2039 */
2040 static void
2041f_ch_read(typval_T *argvars, typval_T *rettv)
2042{
2043 common_channel_read(argvars, rettv, FALSE);
2044}
2045
2046/*
2047 * "ch_readraw()" function
2048 */
2049 static void
2050f_ch_readraw(typval_T *argvars, typval_T *rettv)
2051{
2052 common_channel_read(argvars, rettv, TRUE);
2053}
2054
2055/*
2056 * "ch_evalexpr()" function
2057 */
2058 static void
2059f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2060{
2061 ch_expr_common(argvars, rettv, TRUE);
2062}
2063
2064/*
2065 * "ch_sendexpr()" function
2066 */
2067 static void
2068f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2069{
2070 ch_expr_common(argvars, rettv, FALSE);
2071}
2072
2073/*
2074 * "ch_evalraw()" function
2075 */
2076 static void
2077f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2078{
2079 ch_raw_common(argvars, rettv, TRUE);
2080}
2081
2082/*
2083 * "ch_sendraw()" function
2084 */
2085 static void
2086f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2087{
2088 ch_raw_common(argvars, rettv, FALSE);
2089}
2090
2091/*
2092 * "ch_setoptions()" function
2093 */
2094 static void
2095f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2096{
2097 channel_T *channel;
2098 jobopt_T opt;
2099
2100 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2101 if (channel == NULL)
2102 return;
2103 clear_job_options(&opt);
2104 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002105 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002106 channel_set_options(channel, &opt);
2107 free_job_options(&opt);
2108}
2109
2110/*
2111 * "ch_status()" function
2112 */
2113 static void
2114f_ch_status(typval_T *argvars, typval_T *rettv)
2115{
2116 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002117 jobopt_T opt;
2118 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002119
2120 /* return an empty string by default */
2121 rettv->v_type = VAR_STRING;
2122 rettv->vval.v_string = NULL;
2123
2124 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002125
2126 if (argvars[1].v_type != VAR_UNKNOWN)
2127 {
2128 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002129 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002130 && (opt.jo_set & JO_PART))
2131 part = opt.jo_part;
2132 }
2133
2134 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002135}
2136#endif
2137
2138/*
2139 * "changenr()" function
2140 */
2141 static void
2142f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2143{
2144 rettv->vval.v_number = curbuf->b_u_seq_cur;
2145}
2146
2147/*
2148 * "char2nr(string)" function
2149 */
2150 static void
2151f_char2nr(typval_T *argvars, typval_T *rettv)
2152{
2153#ifdef FEAT_MBYTE
2154 if (has_mbyte)
2155 {
2156 int utf8 = 0;
2157
2158 if (argvars[1].v_type != VAR_UNKNOWN)
2159 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2160
2161 if (utf8)
2162 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2163 else
2164 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2165 }
2166 else
2167#endif
2168 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2169}
2170
2171/*
2172 * "cindent(lnum)" function
2173 */
2174 static void
2175f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2176{
2177#ifdef FEAT_CINDENT
2178 pos_T pos;
2179 linenr_T lnum;
2180
2181 pos = curwin->w_cursor;
2182 lnum = get_tv_lnum(argvars);
2183 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2184 {
2185 curwin->w_cursor.lnum = lnum;
2186 rettv->vval.v_number = get_c_indent();
2187 curwin->w_cursor = pos;
2188 }
2189 else
2190#endif
2191 rettv->vval.v_number = -1;
2192}
2193
2194/*
2195 * "clearmatches()" function
2196 */
2197 static void
2198f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2199{
2200#ifdef FEAT_SEARCH_EXTRA
2201 clear_matches(curwin);
2202#endif
2203}
2204
2205/*
2206 * "col(string)" function
2207 */
2208 static void
2209f_col(typval_T *argvars, typval_T *rettv)
2210{
2211 colnr_T col = 0;
2212 pos_T *fp;
2213 int fnum = curbuf->b_fnum;
2214
2215 fp = var2fpos(&argvars[0], FALSE, &fnum);
2216 if (fp != NULL && fnum == curbuf->b_fnum)
2217 {
2218 if (fp->col == MAXCOL)
2219 {
2220 /* '> can be MAXCOL, get the length of the line then */
2221 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2222 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2223 else
2224 col = MAXCOL;
2225 }
2226 else
2227 {
2228 col = fp->col + 1;
2229#ifdef FEAT_VIRTUALEDIT
2230 /* col(".") when the cursor is on the NUL at the end of the line
2231 * because of "coladd" can be seen as an extra column. */
2232 if (virtual_active() && fp == &curwin->w_cursor)
2233 {
2234 char_u *p = ml_get_cursor();
2235
2236 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2237 curwin->w_virtcol - curwin->w_cursor.coladd))
2238 {
2239# ifdef FEAT_MBYTE
2240 int l;
2241
2242 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2243 col += l;
2244# else
2245 if (*p != NUL && p[1] == NUL)
2246 ++col;
2247# endif
2248 }
2249 }
2250#endif
2251 }
2252 }
2253 rettv->vval.v_number = col;
2254}
2255
2256#if defined(FEAT_INS_EXPAND)
2257/*
2258 * "complete()" function
2259 */
2260 static void
2261f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2262{
2263 int startcol;
2264
2265 if ((State & INSERT) == 0)
2266 {
2267 EMSG(_("E785: complete() can only be used in Insert mode"));
2268 return;
2269 }
2270
2271 /* Check for undo allowed here, because if something was already inserted
2272 * the line was already saved for undo and this check isn't done. */
2273 if (!undo_allowed())
2274 return;
2275
2276 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2277 {
2278 EMSG(_(e_invarg));
2279 return;
2280 }
2281
2282 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2283 if (startcol <= 0)
2284 return;
2285
2286 set_completion(startcol - 1, argvars[1].vval.v_list);
2287}
2288
2289/*
2290 * "complete_add()" function
2291 */
2292 static void
2293f_complete_add(typval_T *argvars, typval_T *rettv)
2294{
2295 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2296}
2297
2298/*
2299 * "complete_check()" function
2300 */
2301 static void
2302f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2303{
2304 int saved = RedrawingDisabled;
2305
2306 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002307 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002308 rettv->vval.v_number = compl_interrupted;
2309 RedrawingDisabled = saved;
2310}
2311#endif
2312
2313/*
2314 * "confirm(message, buttons[, default [, type]])" function
2315 */
2316 static void
2317f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2318{
2319#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2320 char_u *message;
2321 char_u *buttons = NULL;
2322 char_u buf[NUMBUFLEN];
2323 char_u buf2[NUMBUFLEN];
2324 int def = 1;
2325 int type = VIM_GENERIC;
2326 char_u *typestr;
2327 int error = FALSE;
2328
2329 message = get_tv_string_chk(&argvars[0]);
2330 if (message == NULL)
2331 error = TRUE;
2332 if (argvars[1].v_type != VAR_UNKNOWN)
2333 {
2334 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2335 if (buttons == NULL)
2336 error = TRUE;
2337 if (argvars[2].v_type != VAR_UNKNOWN)
2338 {
2339 def = (int)get_tv_number_chk(&argvars[2], &error);
2340 if (argvars[3].v_type != VAR_UNKNOWN)
2341 {
2342 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2343 if (typestr == NULL)
2344 error = TRUE;
2345 else
2346 {
2347 switch (TOUPPER_ASC(*typestr))
2348 {
2349 case 'E': type = VIM_ERROR; break;
2350 case 'Q': type = VIM_QUESTION; break;
2351 case 'I': type = VIM_INFO; break;
2352 case 'W': type = VIM_WARNING; break;
2353 case 'G': type = VIM_GENERIC; break;
2354 }
2355 }
2356 }
2357 }
2358 }
2359
2360 if (buttons == NULL || *buttons == NUL)
2361 buttons = (char_u *)_("&Ok");
2362
2363 if (!error)
2364 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2365 def, NULL, FALSE);
2366#endif
2367}
2368
2369/*
2370 * "copy()" function
2371 */
2372 static void
2373f_copy(typval_T *argvars, typval_T *rettv)
2374{
2375 item_copy(&argvars[0], rettv, FALSE, 0);
2376}
2377
2378#ifdef FEAT_FLOAT
2379/*
2380 * "cos()" function
2381 */
2382 static void
2383f_cos(typval_T *argvars, typval_T *rettv)
2384{
2385 float_T f = 0.0;
2386
2387 rettv->v_type = VAR_FLOAT;
2388 if (get_float_arg(argvars, &f) == OK)
2389 rettv->vval.v_float = cos(f);
2390 else
2391 rettv->vval.v_float = 0.0;
2392}
2393
2394/*
2395 * "cosh()" function
2396 */
2397 static void
2398f_cosh(typval_T *argvars, typval_T *rettv)
2399{
2400 float_T f = 0.0;
2401
2402 rettv->v_type = VAR_FLOAT;
2403 if (get_float_arg(argvars, &f) == OK)
2404 rettv->vval.v_float = cosh(f);
2405 else
2406 rettv->vval.v_float = 0.0;
2407}
2408#endif
2409
2410/*
2411 * "count()" function
2412 */
2413 static void
2414f_count(typval_T *argvars, typval_T *rettv)
2415{
2416 long n = 0;
2417 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002418 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002419
Bram Moolenaar9966b212017-07-28 16:46:57 +02002420 if (argvars[2].v_type != VAR_UNKNOWN)
2421 ic = (int)get_tv_number_chk(&argvars[2], &error);
2422
2423 if (argvars[0].v_type == VAR_STRING)
2424 {
2425 char_u *expr = get_tv_string_chk(&argvars[1]);
2426 char_u *p = argvars[0].vval.v_string;
2427 char_u *next;
2428
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002429 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002430 {
2431 if (ic)
2432 {
2433 size_t len = STRLEN(expr);
2434
2435 while (*p != NUL)
2436 {
2437 if (MB_STRNICMP(p, expr, len) == 0)
2438 {
2439 ++n;
2440 p += len;
2441 }
2442 else
2443 MB_PTR_ADV(p);
2444 }
2445 }
2446 else
2447 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2448 != NULL)
2449 {
2450 ++n;
2451 p = next + STRLEN(expr);
2452 }
2453 }
2454
2455 }
2456 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002457 {
2458 listitem_T *li;
2459 list_T *l;
2460 long idx;
2461
2462 if ((l = argvars[0].vval.v_list) != NULL)
2463 {
2464 li = l->lv_first;
2465 if (argvars[2].v_type != VAR_UNKNOWN)
2466 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002467 if (argvars[3].v_type != VAR_UNKNOWN)
2468 {
2469 idx = (long)get_tv_number_chk(&argvars[3], &error);
2470 if (!error)
2471 {
2472 li = list_find(l, idx);
2473 if (li == NULL)
2474 EMSGN(_(e_listidx), idx);
2475 }
2476 }
2477 if (error)
2478 li = NULL;
2479 }
2480
2481 for ( ; li != NULL; li = li->li_next)
2482 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2483 ++n;
2484 }
2485 }
2486 else if (argvars[0].v_type == VAR_DICT)
2487 {
2488 int todo;
2489 dict_T *d;
2490 hashitem_T *hi;
2491
2492 if ((d = argvars[0].vval.v_dict) != NULL)
2493 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002494 if (argvars[2].v_type != VAR_UNKNOWN)
2495 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002496 if (argvars[3].v_type != VAR_UNKNOWN)
2497 EMSG(_(e_invarg));
2498 }
2499
2500 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2501 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2502 {
2503 if (!HASHITEM_EMPTY(hi))
2504 {
2505 --todo;
2506 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2507 ++n;
2508 }
2509 }
2510 }
2511 }
2512 else
2513 EMSG2(_(e_listdictarg), "count()");
2514 rettv->vval.v_number = n;
2515}
2516
2517/*
2518 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2519 *
2520 * Checks the existence of a cscope connection.
2521 */
2522 static void
2523f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2524{
2525#ifdef FEAT_CSCOPE
2526 int num = 0;
2527 char_u *dbpath = NULL;
2528 char_u *prepend = NULL;
2529 char_u buf[NUMBUFLEN];
2530
2531 if (argvars[0].v_type != VAR_UNKNOWN
2532 && argvars[1].v_type != VAR_UNKNOWN)
2533 {
2534 num = (int)get_tv_number(&argvars[0]);
2535 dbpath = get_tv_string(&argvars[1]);
2536 if (argvars[2].v_type != VAR_UNKNOWN)
2537 prepend = get_tv_string_buf(&argvars[2], buf);
2538 }
2539
2540 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2541#endif
2542}
2543
2544/*
2545 * "cursor(lnum, col)" function, or
2546 * "cursor(list)"
2547 *
2548 * Moves the cursor to the specified line and column.
2549 * Returns 0 when the position could be set, -1 otherwise.
2550 */
2551 static void
2552f_cursor(typval_T *argvars, typval_T *rettv)
2553{
2554 long line, col;
2555#ifdef FEAT_VIRTUALEDIT
2556 long coladd = 0;
2557#endif
2558 int set_curswant = TRUE;
2559
2560 rettv->vval.v_number = -1;
2561 if (argvars[1].v_type == VAR_UNKNOWN)
2562 {
2563 pos_T pos;
2564 colnr_T curswant = -1;
2565
2566 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2567 {
2568 EMSG(_(e_invarg));
2569 return;
2570 }
2571 line = pos.lnum;
2572 col = pos.col;
2573#ifdef FEAT_VIRTUALEDIT
2574 coladd = pos.coladd;
2575#endif
2576 if (curswant >= 0)
2577 {
2578 curwin->w_curswant = curswant - 1;
2579 set_curswant = FALSE;
2580 }
2581 }
2582 else
2583 {
2584 line = get_tv_lnum(argvars);
2585 col = (long)get_tv_number_chk(&argvars[1], NULL);
2586#ifdef FEAT_VIRTUALEDIT
2587 if (argvars[2].v_type != VAR_UNKNOWN)
2588 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2589#endif
2590 }
2591 if (line < 0 || col < 0
2592#ifdef FEAT_VIRTUALEDIT
2593 || coladd < 0
2594#endif
2595 )
2596 return; /* type error; errmsg already given */
2597 if (line > 0)
2598 curwin->w_cursor.lnum = line;
2599 if (col > 0)
2600 curwin->w_cursor.col = col - 1;
2601#ifdef FEAT_VIRTUALEDIT
2602 curwin->w_cursor.coladd = coladd;
2603#endif
2604
2605 /* Make sure the cursor is in a valid position. */
2606 check_cursor();
2607#ifdef FEAT_MBYTE
2608 /* Correct cursor for multi-byte character. */
2609 if (has_mbyte)
2610 mb_adjust_cursor();
2611#endif
2612
2613 curwin->w_set_curswant = set_curswant;
2614 rettv->vval.v_number = 0;
2615}
2616
2617/*
2618 * "deepcopy()" function
2619 */
2620 static void
2621f_deepcopy(typval_T *argvars, typval_T *rettv)
2622{
2623 int noref = 0;
2624 int copyID;
2625
2626 if (argvars[1].v_type != VAR_UNKNOWN)
2627 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2628 if (noref < 0 || noref > 1)
2629 EMSG(_(e_invarg));
2630 else
2631 {
2632 copyID = get_copyID();
2633 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2634 }
2635}
2636
2637/*
2638 * "delete()" function
2639 */
2640 static void
2641f_delete(typval_T *argvars, typval_T *rettv)
2642{
2643 char_u nbuf[NUMBUFLEN];
2644 char_u *name;
2645 char_u *flags;
2646
2647 rettv->vval.v_number = -1;
2648 if (check_restricted() || check_secure())
2649 return;
2650
2651 name = get_tv_string(&argvars[0]);
2652 if (name == NULL || *name == NUL)
2653 {
2654 EMSG(_(e_invarg));
2655 return;
2656 }
2657
2658 if (argvars[1].v_type != VAR_UNKNOWN)
2659 flags = get_tv_string_buf(&argvars[1], nbuf);
2660 else
2661 flags = (char_u *)"";
2662
2663 if (*flags == NUL)
2664 /* delete a file */
2665 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2666 else if (STRCMP(flags, "d") == 0)
2667 /* delete an empty directory */
2668 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2669 else if (STRCMP(flags, "rf") == 0)
2670 /* delete a directory recursively */
2671 rettv->vval.v_number = delete_recursive(name);
2672 else
2673 EMSG2(_(e_invexpr2), flags);
2674}
2675
2676/*
2677 * "did_filetype()" function
2678 */
2679 static void
2680f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2681{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002682 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002683}
2684
2685/*
2686 * "diff_filler()" function
2687 */
2688 static void
2689f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2690{
2691#ifdef FEAT_DIFF
2692 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2693#endif
2694}
2695
2696/*
2697 * "diff_hlID()" function
2698 */
2699 static void
2700f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2701{
2702#ifdef FEAT_DIFF
2703 linenr_T lnum = get_tv_lnum(argvars);
2704 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002705 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002706 static int fnum = 0;
2707 static int change_start = 0;
2708 static int change_end = 0;
2709 static hlf_T hlID = (hlf_T)0;
2710 int filler_lines;
2711 int col;
2712
2713 if (lnum < 0) /* ignore type error in {lnum} arg */
2714 lnum = 0;
2715 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002716 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002717 || fnum != curbuf->b_fnum)
2718 {
2719 /* New line, buffer, change: need to get the values. */
2720 filler_lines = diff_check(curwin, lnum);
2721 if (filler_lines < 0)
2722 {
2723 if (filler_lines == -1)
2724 {
2725 change_start = MAXCOL;
2726 change_end = -1;
2727 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2728 hlID = HLF_ADD; /* added line */
2729 else
2730 hlID = HLF_CHD; /* changed line */
2731 }
2732 else
2733 hlID = HLF_ADD; /* added line */
2734 }
2735 else
2736 hlID = (hlf_T)0;
2737 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002738 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002739 fnum = curbuf->b_fnum;
2740 }
2741
2742 if (hlID == HLF_CHD || hlID == HLF_TXD)
2743 {
2744 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2745 if (col >= change_start && col <= change_end)
2746 hlID = HLF_TXD; /* changed text */
2747 else
2748 hlID = HLF_CHD; /* changed line */
2749 }
2750 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2751#endif
2752}
2753
2754/*
2755 * "empty({expr})" function
2756 */
2757 static void
2758f_empty(typval_T *argvars, typval_T *rettv)
2759{
2760 int n = FALSE;
2761
2762 switch (argvars[0].v_type)
2763 {
2764 case VAR_STRING:
2765 case VAR_FUNC:
2766 n = argvars[0].vval.v_string == NULL
2767 || *argvars[0].vval.v_string == NUL;
2768 break;
2769 case VAR_PARTIAL:
2770 n = FALSE;
2771 break;
2772 case VAR_NUMBER:
2773 n = argvars[0].vval.v_number == 0;
2774 break;
2775 case VAR_FLOAT:
2776#ifdef FEAT_FLOAT
2777 n = argvars[0].vval.v_float == 0.0;
2778 break;
2779#endif
2780 case VAR_LIST:
2781 n = argvars[0].vval.v_list == NULL
2782 || argvars[0].vval.v_list->lv_first == NULL;
2783 break;
2784 case VAR_DICT:
2785 n = argvars[0].vval.v_dict == NULL
2786 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2787 break;
2788 case VAR_SPECIAL:
2789 n = argvars[0].vval.v_number != VVAL_TRUE;
2790 break;
2791
2792 case VAR_JOB:
2793#ifdef FEAT_JOB_CHANNEL
2794 n = argvars[0].vval.v_job == NULL
2795 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2796 break;
2797#endif
2798 case VAR_CHANNEL:
2799#ifdef FEAT_JOB_CHANNEL
2800 n = argvars[0].vval.v_channel == NULL
2801 || !channel_is_open(argvars[0].vval.v_channel);
2802 break;
2803#endif
2804 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002805 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002806 n = TRUE;
2807 break;
2808 }
2809
2810 rettv->vval.v_number = n;
2811}
2812
2813/*
2814 * "escape({string}, {chars})" function
2815 */
2816 static void
2817f_escape(typval_T *argvars, typval_T *rettv)
2818{
2819 char_u buf[NUMBUFLEN];
2820
2821 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2822 get_tv_string_buf(&argvars[1], buf));
2823 rettv->v_type = VAR_STRING;
2824}
2825
2826/*
2827 * "eval()" function
2828 */
2829 static void
2830f_eval(typval_T *argvars, typval_T *rettv)
2831{
2832 char_u *s, *p;
2833
2834 s = get_tv_string_chk(&argvars[0]);
2835 if (s != NULL)
2836 s = skipwhite(s);
2837
2838 p = s;
2839 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2840 {
2841 if (p != NULL && !aborting())
2842 EMSG2(_(e_invexpr2), p);
2843 need_clr_eos = FALSE;
2844 rettv->v_type = VAR_NUMBER;
2845 rettv->vval.v_number = 0;
2846 }
2847 else if (*s != NUL)
2848 EMSG(_(e_trailing));
2849}
2850
2851/*
2852 * "eventhandler()" function
2853 */
2854 static void
2855f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2856{
2857 rettv->vval.v_number = vgetc_busy;
2858}
2859
2860/*
2861 * "executable()" function
2862 */
2863 static void
2864f_executable(typval_T *argvars, typval_T *rettv)
2865{
2866 char_u *name = get_tv_string(&argvars[0]);
2867
2868 /* Check in $PATH and also check directly if there is a directory name. */
2869 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2870 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2871}
2872
2873static garray_T redir_execute_ga;
2874
2875/*
2876 * Append "value[value_len]" to the execute() output.
2877 */
2878 void
2879execute_redir_str(char_u *value, int value_len)
2880{
2881 int len;
2882
2883 if (value_len == -1)
2884 len = (int)STRLEN(value); /* Append the entire string */
2885 else
2886 len = value_len; /* Append only "value_len" characters */
2887 if (ga_grow(&redir_execute_ga, len) == OK)
2888 {
2889 mch_memmove((char *)redir_execute_ga.ga_data
2890 + redir_execute_ga.ga_len, value, len);
2891 redir_execute_ga.ga_len += len;
2892 }
2893}
2894
2895/*
2896 * Get next line from a list.
2897 * Called by do_cmdline() to get the next line.
2898 * Returns allocated string, or NULL for end of function.
2899 */
2900
2901 static char_u *
2902get_list_line(
2903 int c UNUSED,
2904 void *cookie,
2905 int indent UNUSED)
2906{
2907 listitem_T **p = (listitem_T **)cookie;
2908 listitem_T *item = *p;
2909 char_u buf[NUMBUFLEN];
2910 char_u *s;
2911
2912 if (item == NULL)
2913 return NULL;
2914 s = get_tv_string_buf_chk(&item->li_tv, buf);
2915 *p = item->li_next;
2916 return s == NULL ? NULL : vim_strsave(s);
2917}
2918
2919/*
2920 * "execute()" function
2921 */
2922 static void
2923f_execute(typval_T *argvars, typval_T *rettv)
2924{
2925 char_u *cmd = NULL;
2926 list_T *list = NULL;
2927 int save_msg_silent = msg_silent;
2928 int save_emsg_silent = emsg_silent;
2929 int save_emsg_noredir = emsg_noredir;
2930 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002931 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002932 garray_T save_ga;
2933
2934 rettv->vval.v_string = NULL;
2935 rettv->v_type = VAR_STRING;
2936
2937 if (argvars[0].v_type == VAR_LIST)
2938 {
2939 list = argvars[0].vval.v_list;
2940 if (list == NULL || list->lv_first == NULL)
2941 /* empty list, no commands, empty output */
2942 return;
2943 ++list->lv_refcount;
2944 }
2945 else
2946 {
2947 cmd = get_tv_string_chk(&argvars[0]);
2948 if (cmd == NULL)
2949 return;
2950 }
2951
2952 if (argvars[1].v_type != VAR_UNKNOWN)
2953 {
2954 char_u buf[NUMBUFLEN];
2955 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2956
2957 if (s == NULL)
2958 return;
2959 if (STRNCMP(s, "silent", 6) == 0)
2960 ++msg_silent;
2961 if (STRCMP(s, "silent!") == 0)
2962 {
2963 emsg_silent = TRUE;
2964 emsg_noredir = TRUE;
2965 }
2966 }
2967 else
2968 ++msg_silent;
2969
2970 if (redir_execute)
2971 save_ga = redir_execute_ga;
2972 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2973 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002974 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002975
2976 if (cmd != NULL)
2977 do_cmdline_cmd(cmd);
2978 else
2979 {
2980 listitem_T *item = list->lv_first;
2981
2982 do_cmdline(NULL, get_list_line, (void *)&item,
2983 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2984 --list->lv_refcount;
2985 }
2986
Bram Moolenaard297f352017-01-29 20:31:21 +01002987 /* Need to append a NUL to the result. */
2988 if (ga_grow(&redir_execute_ga, 1) == OK)
2989 {
2990 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2991 rettv->vval.v_string = redir_execute_ga.ga_data;
2992 }
2993 else
2994 {
2995 ga_clear(&redir_execute_ga);
2996 rettv->vval.v_string = NULL;
2997 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002998 msg_silent = save_msg_silent;
2999 emsg_silent = save_emsg_silent;
3000 emsg_noredir = save_emsg_noredir;
3001
3002 redir_execute = save_redir_execute;
3003 if (redir_execute)
3004 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003005 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003006
3007 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
3008 * line. Put it back in the first column. */
3009 msg_col = 0;
3010}
3011
3012/*
3013 * "exepath()" function
3014 */
3015 static void
3016f_exepath(typval_T *argvars, typval_T *rettv)
3017{
3018 char_u *p = NULL;
3019
3020 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3021 rettv->v_type = VAR_STRING;
3022 rettv->vval.v_string = p;
3023}
3024
3025/*
3026 * "exists()" function
3027 */
3028 static void
3029f_exists(typval_T *argvars, typval_T *rettv)
3030{
3031 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003032 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033
3034 p = get_tv_string(&argvars[0]);
3035 if (*p == '$') /* environment variable */
3036 {
3037 /* first try "normal" environment variables (fast) */
3038 if (mch_getenv(p + 1) != NULL)
3039 n = TRUE;
3040 else
3041 {
3042 /* try expanding things like $VIM and ${HOME} */
3043 p = expand_env_save(p);
3044 if (p != NULL && *p != '$')
3045 n = TRUE;
3046 vim_free(p);
3047 }
3048 }
3049 else if (*p == '&' || *p == '+') /* option */
3050 {
3051 n = (get_option_tv(&p, NULL, TRUE) == OK);
3052 if (*skipwhite(p) != NUL)
3053 n = FALSE; /* trailing garbage */
3054 }
3055 else if (*p == '*') /* internal or user defined function */
3056 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003057 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003058 }
3059 else if (*p == ':')
3060 {
3061 n = cmd_exists(p + 1);
3062 }
3063 else if (*p == '#')
3064 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003065 if (p[1] == '#')
3066 n = autocmd_supported(p + 2);
3067 else
3068 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003069 }
3070 else /* internal variable */
3071 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003072 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003073 }
3074
3075 rettv->vval.v_number = n;
3076}
3077
3078#ifdef FEAT_FLOAT
3079/*
3080 * "exp()" function
3081 */
3082 static void
3083f_exp(typval_T *argvars, typval_T *rettv)
3084{
3085 float_T f = 0.0;
3086
3087 rettv->v_type = VAR_FLOAT;
3088 if (get_float_arg(argvars, &f) == OK)
3089 rettv->vval.v_float = exp(f);
3090 else
3091 rettv->vval.v_float = 0.0;
3092}
3093#endif
3094
3095/*
3096 * "expand()" function
3097 */
3098 static void
3099f_expand(typval_T *argvars, typval_T *rettv)
3100{
3101 char_u *s;
3102 int len;
3103 char_u *errormsg;
3104 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3105 expand_T xpc;
3106 int error = FALSE;
3107 char_u *result;
3108
3109 rettv->v_type = VAR_STRING;
3110 if (argvars[1].v_type != VAR_UNKNOWN
3111 && argvars[2].v_type != VAR_UNKNOWN
3112 && get_tv_number_chk(&argvars[2], &error)
3113 && !error)
3114 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003115 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003116 }
3117
3118 s = get_tv_string(&argvars[0]);
3119 if (*s == '%' || *s == '#' || *s == '<')
3120 {
3121 ++emsg_off;
3122 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3123 --emsg_off;
3124 if (rettv->v_type == VAR_LIST)
3125 {
3126 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3127 list_append_string(rettv->vval.v_list, result, -1);
3128 else
3129 vim_free(result);
3130 }
3131 else
3132 rettv->vval.v_string = result;
3133 }
3134 else
3135 {
3136 /* When the optional second argument is non-zero, don't remove matches
3137 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3138 if (argvars[1].v_type != VAR_UNKNOWN
3139 && get_tv_number_chk(&argvars[1], &error))
3140 options |= WILD_KEEP_ALL;
3141 if (!error)
3142 {
3143 ExpandInit(&xpc);
3144 xpc.xp_context = EXPAND_FILES;
3145 if (p_wic)
3146 options += WILD_ICASE;
3147 if (rettv->v_type == VAR_STRING)
3148 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3149 options, WILD_ALL);
3150 else if (rettv_list_alloc(rettv) != FAIL)
3151 {
3152 int i;
3153
3154 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3155 for (i = 0; i < xpc.xp_numfiles; i++)
3156 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3157 ExpandCleanup(&xpc);
3158 }
3159 }
3160 else
3161 rettv->vval.v_string = NULL;
3162 }
3163}
3164
3165/*
3166 * "extend(list, list [, idx])" function
3167 * "extend(dict, dict [, action])" function
3168 */
3169 static void
3170f_extend(typval_T *argvars, typval_T *rettv)
3171{
3172 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3173
3174 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3175 {
3176 list_T *l1, *l2;
3177 listitem_T *item;
3178 long before;
3179 int error = FALSE;
3180
3181 l1 = argvars[0].vval.v_list;
3182 l2 = argvars[1].vval.v_list;
3183 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3184 && l2 != NULL)
3185 {
3186 if (argvars[2].v_type != VAR_UNKNOWN)
3187 {
3188 before = (long)get_tv_number_chk(&argvars[2], &error);
3189 if (error)
3190 return; /* type error; errmsg already given */
3191
3192 if (before == l1->lv_len)
3193 item = NULL;
3194 else
3195 {
3196 item = list_find(l1, before);
3197 if (item == NULL)
3198 {
3199 EMSGN(_(e_listidx), before);
3200 return;
3201 }
3202 }
3203 }
3204 else
3205 item = NULL;
3206 list_extend(l1, l2, item);
3207
3208 copy_tv(&argvars[0], rettv);
3209 }
3210 }
3211 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3212 {
3213 dict_T *d1, *d2;
3214 char_u *action;
3215 int i;
3216
3217 d1 = argvars[0].vval.v_dict;
3218 d2 = argvars[1].vval.v_dict;
3219 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3220 && d2 != NULL)
3221 {
3222 /* Check the third argument. */
3223 if (argvars[2].v_type != VAR_UNKNOWN)
3224 {
3225 static char *(av[]) = {"keep", "force", "error"};
3226
3227 action = get_tv_string_chk(&argvars[2]);
3228 if (action == NULL)
3229 return; /* type error; errmsg already given */
3230 for (i = 0; i < 3; ++i)
3231 if (STRCMP(action, av[i]) == 0)
3232 break;
3233 if (i == 3)
3234 {
3235 EMSG2(_(e_invarg2), action);
3236 return;
3237 }
3238 }
3239 else
3240 action = (char_u *)"force";
3241
3242 dict_extend(d1, d2, action);
3243
3244 copy_tv(&argvars[0], rettv);
3245 }
3246 }
3247 else
3248 EMSG2(_(e_listdictarg), "extend()");
3249}
3250
3251/*
3252 * "feedkeys()" function
3253 */
3254 static void
3255f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3256{
3257 int remap = TRUE;
3258 int insert = FALSE;
3259 char_u *keys, *flags;
3260 char_u nbuf[NUMBUFLEN];
3261 int typed = FALSE;
3262 int execute = FALSE;
3263 int dangerous = FALSE;
3264 char_u *keys_esc;
3265
3266 /* This is not allowed in the sandbox. If the commands would still be
3267 * executed in the sandbox it would be OK, but it probably happens later,
3268 * when "sandbox" is no longer set. */
3269 if (check_secure())
3270 return;
3271
3272 keys = get_tv_string(&argvars[0]);
3273
3274 if (argvars[1].v_type != VAR_UNKNOWN)
3275 {
3276 flags = get_tv_string_buf(&argvars[1], nbuf);
3277 for ( ; *flags != NUL; ++flags)
3278 {
3279 switch (*flags)
3280 {
3281 case 'n': remap = FALSE; break;
3282 case 'm': remap = TRUE; break;
3283 case 't': typed = TRUE; break;
3284 case 'i': insert = TRUE; break;
3285 case 'x': execute = TRUE; break;
3286 case '!': dangerous = TRUE; break;
3287 }
3288 }
3289 }
3290
3291 if (*keys != NUL || execute)
3292 {
3293 /* Need to escape K_SPECIAL and CSI before putting the string in the
3294 * typeahead buffer. */
3295 keys_esc = vim_strsave_escape_csi(keys);
3296 if (keys_esc != NULL)
3297 {
3298 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3299 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3300 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003301 if (vgetc_busy
3302#ifdef FEAT_TIMERS
3303 || timer_busy
3304#endif
3305 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003306 typebuf_was_filled = TRUE;
3307 if (execute)
3308 {
3309 int save_msg_scroll = msg_scroll;
3310
3311 /* Avoid a 1 second delay when the keys start Insert mode. */
3312 msg_scroll = FALSE;
3313
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003314 if (!dangerous)
3315 ++ex_normal_busy;
Bram Moolenaar655a82a2018-05-08 22:01:07 +02003316 exec_normal(TRUE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003317 if (!dangerous)
3318 --ex_normal_busy;
3319
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003320 msg_scroll |= save_msg_scroll;
3321 }
3322 }
3323 }
3324}
3325
3326/*
3327 * "filereadable()" function
3328 */
3329 static void
3330f_filereadable(typval_T *argvars, typval_T *rettv)
3331{
3332 int fd;
3333 char_u *p;
3334 int n;
3335
3336#ifndef O_NONBLOCK
3337# define O_NONBLOCK 0
3338#endif
3339 p = get_tv_string(&argvars[0]);
3340 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3341 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3342 {
3343 n = TRUE;
3344 close(fd);
3345 }
3346 else
3347 n = FALSE;
3348
3349 rettv->vval.v_number = n;
3350}
3351
3352/*
3353 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3354 * rights to write into.
3355 */
3356 static void
3357f_filewritable(typval_T *argvars, typval_T *rettv)
3358{
3359 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3360}
3361
3362 static void
3363findfilendir(
3364 typval_T *argvars UNUSED,
3365 typval_T *rettv,
3366 int find_what UNUSED)
3367{
3368#ifdef FEAT_SEARCHPATH
3369 char_u *fname;
3370 char_u *fresult = NULL;
3371 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3372 char_u *p;
3373 char_u pathbuf[NUMBUFLEN];
3374 int count = 1;
3375 int first = TRUE;
3376 int error = FALSE;
3377#endif
3378
3379 rettv->vval.v_string = NULL;
3380 rettv->v_type = VAR_STRING;
3381
3382#ifdef FEAT_SEARCHPATH
3383 fname = get_tv_string(&argvars[0]);
3384
3385 if (argvars[1].v_type != VAR_UNKNOWN)
3386 {
3387 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3388 if (p == NULL)
3389 error = TRUE;
3390 else
3391 {
3392 if (*p != NUL)
3393 path = p;
3394
3395 if (argvars[2].v_type != VAR_UNKNOWN)
3396 count = (int)get_tv_number_chk(&argvars[2], &error);
3397 }
3398 }
3399
3400 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3401 error = TRUE;
3402
3403 if (*fname != NUL && !error)
3404 {
3405 do
3406 {
3407 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3408 vim_free(fresult);
3409 fresult = find_file_in_path_option(first ? fname : NULL,
3410 first ? (int)STRLEN(fname) : 0,
3411 0, first, path,
3412 find_what,
3413 curbuf->b_ffname,
3414 find_what == FINDFILE_DIR
3415 ? (char_u *)"" : curbuf->b_p_sua);
3416 first = FALSE;
3417
3418 if (fresult != NULL && rettv->v_type == VAR_LIST)
3419 list_append_string(rettv->vval.v_list, fresult, -1);
3420
3421 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3422 }
3423
3424 if (rettv->v_type == VAR_STRING)
3425 rettv->vval.v_string = fresult;
3426#endif
3427}
3428
3429/*
3430 * "filter()" function
3431 */
3432 static void
3433f_filter(typval_T *argvars, typval_T *rettv)
3434{
3435 filter_map(argvars, rettv, FALSE);
3436}
3437
3438/*
3439 * "finddir({fname}[, {path}[, {count}]])" function
3440 */
3441 static void
3442f_finddir(typval_T *argvars, typval_T *rettv)
3443{
3444 findfilendir(argvars, rettv, FINDFILE_DIR);
3445}
3446
3447/*
3448 * "findfile({fname}[, {path}[, {count}]])" function
3449 */
3450 static void
3451f_findfile(typval_T *argvars, typval_T *rettv)
3452{
3453 findfilendir(argvars, rettv, FINDFILE_FILE);
3454}
3455
3456#ifdef FEAT_FLOAT
3457/*
3458 * "float2nr({float})" function
3459 */
3460 static void
3461f_float2nr(typval_T *argvars, typval_T *rettv)
3462{
3463 float_T f = 0.0;
3464
3465 if (get_float_arg(argvars, &f) == OK)
3466 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003467 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003468 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003469 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003470 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003471 else
3472 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003473 }
3474}
3475
3476/*
3477 * "floor({float})" function
3478 */
3479 static void
3480f_floor(typval_T *argvars, typval_T *rettv)
3481{
3482 float_T f = 0.0;
3483
3484 rettv->v_type = VAR_FLOAT;
3485 if (get_float_arg(argvars, &f) == OK)
3486 rettv->vval.v_float = floor(f);
3487 else
3488 rettv->vval.v_float = 0.0;
3489}
3490
3491/*
3492 * "fmod()" function
3493 */
3494 static void
3495f_fmod(typval_T *argvars, typval_T *rettv)
3496{
3497 float_T fx = 0.0, fy = 0.0;
3498
3499 rettv->v_type = VAR_FLOAT;
3500 if (get_float_arg(argvars, &fx) == OK
3501 && get_float_arg(&argvars[1], &fy) == OK)
3502 rettv->vval.v_float = fmod(fx, fy);
3503 else
3504 rettv->vval.v_float = 0.0;
3505}
3506#endif
3507
3508/*
3509 * "fnameescape({string})" function
3510 */
3511 static void
3512f_fnameescape(typval_T *argvars, typval_T *rettv)
3513{
3514 rettv->vval.v_string = vim_strsave_fnameescape(
3515 get_tv_string(&argvars[0]), FALSE);
3516 rettv->v_type = VAR_STRING;
3517}
3518
3519/*
3520 * "fnamemodify({fname}, {mods})" function
3521 */
3522 static void
3523f_fnamemodify(typval_T *argvars, typval_T *rettv)
3524{
3525 char_u *fname;
3526 char_u *mods;
3527 int usedlen = 0;
3528 int len;
3529 char_u *fbuf = NULL;
3530 char_u buf[NUMBUFLEN];
3531
3532 fname = get_tv_string_chk(&argvars[0]);
3533 mods = get_tv_string_buf_chk(&argvars[1], buf);
3534 if (fname == NULL || mods == NULL)
3535 fname = NULL;
3536 else
3537 {
3538 len = (int)STRLEN(fname);
3539 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3540 }
3541
3542 rettv->v_type = VAR_STRING;
3543 if (fname == NULL)
3544 rettv->vval.v_string = NULL;
3545 else
3546 rettv->vval.v_string = vim_strnsave(fname, len);
3547 vim_free(fbuf);
3548}
3549
3550static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3551
3552/*
3553 * "foldclosed()" function
3554 */
3555 static void
3556foldclosed_both(
3557 typval_T *argvars UNUSED,
3558 typval_T *rettv,
3559 int end UNUSED)
3560{
3561#ifdef FEAT_FOLDING
3562 linenr_T lnum;
3563 linenr_T first, last;
3564
3565 lnum = get_tv_lnum(argvars);
3566 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3567 {
3568 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3569 {
3570 if (end)
3571 rettv->vval.v_number = (varnumber_T)last;
3572 else
3573 rettv->vval.v_number = (varnumber_T)first;
3574 return;
3575 }
3576 }
3577#endif
3578 rettv->vval.v_number = -1;
3579}
3580
3581/*
3582 * "foldclosed()" function
3583 */
3584 static void
3585f_foldclosed(typval_T *argvars, typval_T *rettv)
3586{
3587 foldclosed_both(argvars, rettv, FALSE);
3588}
3589
3590/*
3591 * "foldclosedend()" function
3592 */
3593 static void
3594f_foldclosedend(typval_T *argvars, typval_T *rettv)
3595{
3596 foldclosed_both(argvars, rettv, TRUE);
3597}
3598
3599/*
3600 * "foldlevel()" function
3601 */
3602 static void
3603f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3604{
3605#ifdef FEAT_FOLDING
3606 linenr_T lnum;
3607
3608 lnum = get_tv_lnum(argvars);
3609 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3610 rettv->vval.v_number = foldLevel(lnum);
3611#endif
3612}
3613
3614/*
3615 * "foldtext()" function
3616 */
3617 static void
3618f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3619{
3620#ifdef FEAT_FOLDING
3621 linenr_T foldstart;
3622 linenr_T foldend;
3623 char_u *dashes;
3624 linenr_T lnum;
3625 char_u *s;
3626 char_u *r;
3627 int len;
3628 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003629 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003630#endif
3631
3632 rettv->v_type = VAR_STRING;
3633 rettv->vval.v_string = NULL;
3634#ifdef FEAT_FOLDING
3635 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3636 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3637 dashes = get_vim_var_str(VV_FOLDDASHES);
3638 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3639 && dashes != NULL)
3640 {
3641 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003642 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003643 if (!linewhite(lnum))
3644 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003645
3646 /* Find interesting text in this line. */
3647 s = skipwhite(ml_get(lnum));
3648 /* skip C comment-start */
3649 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3650 {
3651 s = skipwhite(s + 2);
3652 if (*skipwhite(s) == NUL
3653 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3654 {
3655 s = skipwhite(ml_get(lnum + 1));
3656 if (*s == '*')
3657 s = skipwhite(s + 1);
3658 }
3659 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003660 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003661 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003662 r = alloc((unsigned)(STRLEN(txt)
3663 + STRLEN(dashes) /* for %s */
3664 + 20 /* for %3ld */
3665 + STRLEN(s))); /* concatenated */
3666 if (r != NULL)
3667 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003668 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003669 len = (int)STRLEN(r);
3670 STRCAT(r, s);
3671 /* remove 'foldmarker' and 'commentstring' */
3672 foldtext_cleanup(r + len);
3673 rettv->vval.v_string = r;
3674 }
3675 }
3676#endif
3677}
3678
3679/*
3680 * "foldtextresult(lnum)" function
3681 */
3682 static void
3683f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3684{
3685#ifdef FEAT_FOLDING
3686 linenr_T lnum;
3687 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003688 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003689 foldinfo_T foldinfo;
3690 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003691 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003692#endif
3693
3694 rettv->v_type = VAR_STRING;
3695 rettv->vval.v_string = NULL;
3696#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003697 if (entered)
3698 return; /* reject recursive use */
3699 entered = TRUE;
3700
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003701 lnum = get_tv_lnum(argvars);
3702 /* treat illegal types and illegal string values for {lnum} the same */
3703 if (lnum < 0)
3704 lnum = 0;
3705 fold_count = foldedCount(curwin, lnum, &foldinfo);
3706 if (fold_count > 0)
3707 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003708 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3709 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003710 if (text == buf)
3711 text = vim_strsave(text);
3712 rettv->vval.v_string = text;
3713 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003714
3715 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003716#endif
3717}
3718
3719/*
3720 * "foreground()" function
3721 */
3722 static void
3723f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3724{
3725#ifdef FEAT_GUI
3726 if (gui.in_use)
3727 gui_mch_set_foreground();
3728#else
3729# ifdef WIN32
3730 win32_set_foreground();
3731# endif
3732#endif
3733}
3734
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003735 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003736common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003737{
3738 char_u *s;
3739 char_u *name;
3740 int use_string = FALSE;
3741 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003742 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003743
3744 if (argvars[0].v_type == VAR_FUNC)
3745 {
3746 /* function(MyFunc, [arg], dict) */
3747 s = argvars[0].vval.v_string;
3748 }
3749 else if (argvars[0].v_type == VAR_PARTIAL
3750 && argvars[0].vval.v_partial != NULL)
3751 {
3752 /* function(dict.MyFunc, [arg]) */
3753 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003754 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003755 }
3756 else
3757 {
3758 /* function('MyFunc', [arg], dict) */
3759 s = get_tv_string(&argvars[0]);
3760 use_string = TRUE;
3761 }
3762
Bram Moolenaar843b8842016-08-21 14:36:15 +02003763 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003764 {
3765 name = s;
3766 trans_name = trans_function_name(&name, FALSE,
3767 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3768 if (*name != NUL)
3769 s = NULL;
3770 }
3771
Bram Moolenaar843b8842016-08-21 14:36:15 +02003772 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3773 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003774 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003775 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003776 else if (trans_name != NULL && (is_funcref
3777 ? find_func(trans_name) == NULL
3778 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003779 EMSG2(_("E700: Unknown function: %s"), s);
3780 else
3781 {
3782 int dict_idx = 0;
3783 int arg_idx = 0;
3784 list_T *list = NULL;
3785
3786 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3787 {
3788 char sid_buf[25];
3789 int off = *s == 's' ? 2 : 5;
3790
3791 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3792 * also be called from another script. Using trans_function_name()
3793 * would also work, but some plugins depend on the name being
3794 * printable text. */
3795 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3796 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3797 if (name != NULL)
3798 {
3799 STRCPY(name, sid_buf);
3800 STRCAT(name, s + off);
3801 }
3802 }
3803 else
3804 name = vim_strsave(s);
3805
3806 if (argvars[1].v_type != VAR_UNKNOWN)
3807 {
3808 if (argvars[2].v_type != VAR_UNKNOWN)
3809 {
3810 /* function(name, [args], dict) */
3811 arg_idx = 1;
3812 dict_idx = 2;
3813 }
3814 else if (argvars[1].v_type == VAR_DICT)
3815 /* function(name, dict) */
3816 dict_idx = 1;
3817 else
3818 /* function(name, [args]) */
3819 arg_idx = 1;
3820 if (dict_idx > 0)
3821 {
3822 if (argvars[dict_idx].v_type != VAR_DICT)
3823 {
3824 EMSG(_("E922: expected a dict"));
3825 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003826 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003827 }
3828 if (argvars[dict_idx].vval.v_dict == NULL)
3829 dict_idx = 0;
3830 }
3831 if (arg_idx > 0)
3832 {
3833 if (argvars[arg_idx].v_type != VAR_LIST)
3834 {
3835 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3836 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003837 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003838 }
3839 list = argvars[arg_idx].vval.v_list;
3840 if (list == NULL || list->lv_len == 0)
3841 arg_idx = 0;
3842 }
3843 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003844 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003845 {
3846 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3847
3848 /* result is a VAR_PARTIAL */
3849 if (pt == NULL)
3850 vim_free(name);
3851 else
3852 {
3853 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3854 {
3855 listitem_T *li;
3856 int i = 0;
3857 int arg_len = 0;
3858 int lv_len = 0;
3859
3860 if (arg_pt != NULL)
3861 arg_len = arg_pt->pt_argc;
3862 if (list != NULL)
3863 lv_len = list->lv_len;
3864 pt->pt_argc = arg_len + lv_len;
3865 pt->pt_argv = (typval_T *)alloc(
3866 sizeof(typval_T) * pt->pt_argc);
3867 if (pt->pt_argv == NULL)
3868 {
3869 vim_free(pt);
3870 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003871 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003872 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003873 for (i = 0; i < arg_len; i++)
3874 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3875 if (lv_len > 0)
3876 for (li = list->lv_first; li != NULL;
3877 li = li->li_next)
3878 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003879 }
3880
3881 /* For "function(dict.func, [], dict)" and "func" is a partial
3882 * use "dict". That is backwards compatible. */
3883 if (dict_idx > 0)
3884 {
3885 /* The dict is bound explicitly, pt_auto is FALSE. */
3886 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3887 ++pt->pt_dict->dv_refcount;
3888 }
3889 else if (arg_pt != NULL)
3890 {
3891 /* If the dict was bound automatically the result is also
3892 * bound automatically. */
3893 pt->pt_dict = arg_pt->pt_dict;
3894 pt->pt_auto = arg_pt->pt_auto;
3895 if (pt->pt_dict != NULL)
3896 ++pt->pt_dict->dv_refcount;
3897 }
3898
3899 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003900 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3901 {
3902 pt->pt_func = arg_pt->pt_func;
3903 func_ptr_ref(pt->pt_func);
3904 vim_free(name);
3905 }
3906 else if (is_funcref)
3907 {
3908 pt->pt_func = find_func(trans_name);
3909 func_ptr_ref(pt->pt_func);
3910 vim_free(name);
3911 }
3912 else
3913 {
3914 pt->pt_name = name;
3915 func_ref(name);
3916 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003917 }
3918 rettv->v_type = VAR_PARTIAL;
3919 rettv->vval.v_partial = pt;
3920 }
3921 else
3922 {
3923 /* result is a VAR_FUNC */
3924 rettv->v_type = VAR_FUNC;
3925 rettv->vval.v_string = name;
3926 func_ref(name);
3927 }
3928 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003929theend:
3930 vim_free(trans_name);
3931}
3932
3933/*
3934 * "funcref()" function
3935 */
3936 static void
3937f_funcref(typval_T *argvars, typval_T *rettv)
3938{
3939 common_function(argvars, rettv, TRUE);
3940}
3941
3942/*
3943 * "function()" function
3944 */
3945 static void
3946f_function(typval_T *argvars, typval_T *rettv)
3947{
3948 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003949}
3950
3951/*
3952 * "garbagecollect()" function
3953 */
3954 static void
3955f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3956{
3957 /* This is postponed until we are back at the toplevel, because we may be
3958 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3959 want_garbage_collect = TRUE;
3960
3961 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3962 garbage_collect_at_exit = TRUE;
3963}
3964
3965/*
3966 * "get()" function
3967 */
3968 static void
3969f_get(typval_T *argvars, typval_T *rettv)
3970{
3971 listitem_T *li;
3972 list_T *l;
3973 dictitem_T *di;
3974 dict_T *d;
3975 typval_T *tv = NULL;
3976
3977 if (argvars[0].v_type == VAR_LIST)
3978 {
3979 if ((l = argvars[0].vval.v_list) != NULL)
3980 {
3981 int error = FALSE;
3982
3983 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3984 if (!error && li != NULL)
3985 tv = &li->li_tv;
3986 }
3987 }
3988 else if (argvars[0].v_type == VAR_DICT)
3989 {
3990 if ((d = argvars[0].vval.v_dict) != NULL)
3991 {
3992 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3993 if (di != NULL)
3994 tv = &di->di_tv;
3995 }
3996 }
3997 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3998 {
3999 partial_T *pt;
4000 partial_T fref_pt;
4001
4002 if (argvars[0].v_type == VAR_PARTIAL)
4003 pt = argvars[0].vval.v_partial;
4004 else
4005 {
4006 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4007 fref_pt.pt_name = argvars[0].vval.v_string;
4008 pt = &fref_pt;
4009 }
4010
4011 if (pt != NULL)
4012 {
4013 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004014 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004015
4016 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4017 {
4018 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004019 n = partial_name(pt);
4020 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004021 rettv->vval.v_string = NULL;
4022 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004023 {
4024 rettv->vval.v_string = vim_strsave(n);
4025 if (rettv->v_type == VAR_FUNC)
4026 func_ref(rettv->vval.v_string);
4027 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004028 }
4029 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004030 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004031 else if (STRCMP(what, "args") == 0)
4032 {
4033 rettv->v_type = VAR_LIST;
4034 if (rettv_list_alloc(rettv) == OK)
4035 {
4036 int i;
4037
4038 for (i = 0; i < pt->pt_argc; ++i)
4039 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4040 }
4041 }
4042 else
4043 EMSG2(_(e_invarg2), what);
4044 return;
4045 }
4046 }
4047 else
4048 EMSG2(_(e_listdictarg), "get()");
4049
4050 if (tv == NULL)
4051 {
4052 if (argvars[2].v_type != VAR_UNKNOWN)
4053 copy_tv(&argvars[2], rettv);
4054 }
4055 else
4056 copy_tv(tv, rettv);
4057}
4058
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004059#ifdef FEAT_SIGNS
4060/*
4061 * Returns information about signs placed in a buffer as list of dicts.
4062 */
4063 static void
4064get_buffer_signs(buf_T *buf, list_T *l)
4065{
4066 signlist_T *sign;
4067
4068 for (sign = buf->b_signlist; sign; sign = sign->next)
4069 {
4070 dict_T *d = dict_alloc();
4071
4072 if (d != NULL)
4073 {
4074 dict_add_nr_str(d, "id", sign->id, NULL);
4075 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004076 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004077
4078 list_append_dict(l, d);
4079 }
4080 }
4081}
4082#endif
4083
4084/*
4085 * Returns buffer options, variables and other attributes in a dictionary.
4086 */
4087 static dict_T *
4088get_buffer_info(buf_T *buf)
4089{
4090 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004091 tabpage_T *tp;
4092 win_T *wp;
4093 list_T *windows;
4094
4095 dict = dict_alloc();
4096 if (dict == NULL)
4097 return NULL;
4098
Bram Moolenaar33928832016-08-18 21:22:04 +02004099 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004100 dict_add_nr_str(dict, "name", 0L,
4101 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004102 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4103 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004104 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4105 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4106 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004107 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004108 dict_add_nr_str(dict, "hidden",
4109 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4110 NULL);
4111
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004112 /* Get a reference to buffer variables */
4113 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004114
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004115 /* List of windows displaying this buffer */
4116 windows = list_alloc();
4117 if (windows != NULL)
4118 {
4119 FOR_ALL_TAB_WINDOWS(tp, wp)
4120 if (wp->w_buffer == buf)
4121 list_append_number(windows, (varnumber_T)wp->w_id);
4122 dict_add_list(dict, "windows", windows);
4123 }
4124
4125#ifdef FEAT_SIGNS
4126 if (buf->b_signlist != NULL)
4127 {
4128 /* List of signs placed in this buffer */
4129 list_T *signs = list_alloc();
4130 if (signs != NULL)
4131 {
4132 get_buffer_signs(buf, signs);
4133 dict_add_list(dict, "signs", signs);
4134 }
4135 }
4136#endif
4137
4138 return dict;
4139}
4140
4141/*
4142 * "getbufinfo()" function
4143 */
4144 static void
4145f_getbufinfo(typval_T *argvars, typval_T *rettv)
4146{
4147 buf_T *buf = NULL;
4148 buf_T *argbuf = NULL;
4149 dict_T *d;
4150 int filtered = FALSE;
4151 int sel_buflisted = FALSE;
4152 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004153 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004154
4155 if (rettv_list_alloc(rettv) != OK)
4156 return;
4157
4158 /* List of all the buffers or selected buffers */
4159 if (argvars[0].v_type == VAR_DICT)
4160 {
4161 dict_T *sel_d = argvars[0].vval.v_dict;
4162
4163 if (sel_d != NULL)
4164 {
4165 dictitem_T *di;
4166
4167 filtered = TRUE;
4168
4169 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4170 if (di != NULL && get_tv_number(&di->di_tv))
4171 sel_buflisted = TRUE;
4172
4173 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4174 if (di != NULL && get_tv_number(&di->di_tv))
4175 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004176
4177 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4178 if (di != NULL && get_tv_number(&di->di_tv))
4179 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004180 }
4181 }
4182 else if (argvars[0].v_type != VAR_UNKNOWN)
4183 {
4184 /* Information about one buffer. Argument specifies the buffer */
4185 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4186 ++emsg_off;
4187 argbuf = get_buf_tv(&argvars[0], FALSE);
4188 --emsg_off;
4189 if (argbuf == NULL)
4190 return;
4191 }
4192
4193 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004194 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004195 {
4196 if (argbuf != NULL && argbuf != buf)
4197 continue;
4198 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004199 || (sel_buflisted && !buf->b_p_bl)
4200 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004201 continue;
4202
4203 d = get_buffer_info(buf);
4204 if (d != NULL)
4205 list_append_dict(rettv->vval.v_list, d);
4206 if (argbuf != NULL)
4207 return;
4208 }
4209}
4210
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004211static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4212
4213/*
4214 * Get line or list of lines from buffer "buf" into "rettv".
4215 * Return a range (from start to end) of lines in rettv from the specified
4216 * buffer.
4217 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4218 */
4219 static void
4220get_buffer_lines(
4221 buf_T *buf,
4222 linenr_T start,
4223 linenr_T end,
4224 int retlist,
4225 typval_T *rettv)
4226{
4227 char_u *p;
4228
4229 rettv->v_type = VAR_STRING;
4230 rettv->vval.v_string = NULL;
4231 if (retlist && rettv_list_alloc(rettv) == FAIL)
4232 return;
4233
4234 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4235 return;
4236
4237 if (!retlist)
4238 {
4239 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4240 p = ml_get_buf(buf, start, FALSE);
4241 else
4242 p = (char_u *)"";
4243 rettv->vval.v_string = vim_strsave(p);
4244 }
4245 else
4246 {
4247 if (end < start)
4248 return;
4249
4250 if (start < 1)
4251 start = 1;
4252 if (end > buf->b_ml.ml_line_count)
4253 end = buf->b_ml.ml_line_count;
4254 while (start <= end)
4255 if (list_append_string(rettv->vval.v_list,
4256 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4257 break;
4258 }
4259}
4260
4261/*
4262 * Get the lnum from the first argument.
4263 * Also accepts "$", then "buf" is used.
4264 * Returns 0 on error.
4265 */
4266 static linenr_T
4267get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4268{
4269 if (argvars[0].v_type == VAR_STRING
4270 && argvars[0].vval.v_string != NULL
4271 && argvars[0].vval.v_string[0] == '$'
4272 && buf != NULL)
4273 return buf->b_ml.ml_line_count;
4274 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4275}
4276
4277/*
4278 * "getbufline()" function
4279 */
4280 static void
4281f_getbufline(typval_T *argvars, typval_T *rettv)
4282{
4283 linenr_T lnum;
4284 linenr_T end;
4285 buf_T *buf;
4286
4287 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4288 ++emsg_off;
4289 buf = get_buf_tv(&argvars[0], FALSE);
4290 --emsg_off;
4291
4292 lnum = get_tv_lnum_buf(&argvars[1], buf);
4293 if (argvars[2].v_type == VAR_UNKNOWN)
4294 end = lnum;
4295 else
4296 end = get_tv_lnum_buf(&argvars[2], buf);
4297
4298 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4299}
4300
4301/*
4302 * "getbufvar()" function
4303 */
4304 static void
4305f_getbufvar(typval_T *argvars, typval_T *rettv)
4306{
4307 buf_T *buf;
4308 buf_T *save_curbuf;
4309 char_u *varname;
4310 dictitem_T *v;
4311 int done = FALSE;
4312
4313 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4314 varname = get_tv_string_chk(&argvars[1]);
4315 ++emsg_off;
4316 buf = get_buf_tv(&argvars[0], FALSE);
4317
4318 rettv->v_type = VAR_STRING;
4319 rettv->vval.v_string = NULL;
4320
4321 if (buf != NULL && varname != NULL)
4322 {
4323 /* set curbuf to be our buf, temporarily */
4324 save_curbuf = curbuf;
4325 curbuf = buf;
4326
Bram Moolenaar30567352016-08-27 21:25:44 +02004327 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004328 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004329 if (varname[1] == NUL)
4330 {
4331 /* get all buffer-local options in a dict */
4332 dict_T *opts = get_winbuf_options(TRUE);
4333
4334 if (opts != NULL)
4335 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004336 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004337 done = TRUE;
4338 }
4339 }
4340 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4341 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004342 done = TRUE;
4343 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004344 else
4345 {
4346 /* Look up the variable. */
4347 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4348 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4349 'b', varname, FALSE);
4350 if (v != NULL)
4351 {
4352 copy_tv(&v->di_tv, rettv);
4353 done = TRUE;
4354 }
4355 }
4356
4357 /* restore previous notion of curbuf */
4358 curbuf = save_curbuf;
4359 }
4360
4361 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4362 /* use the default value */
4363 copy_tv(&argvars[2], rettv);
4364
4365 --emsg_off;
4366}
4367
4368/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004369 * "getchangelist()" function
4370 */
4371 static void
4372f_getchangelist(typval_T *argvars, typval_T *rettv)
4373{
4374#ifdef FEAT_JUMPLIST
4375 buf_T *buf;
4376 int i;
4377 list_T *l;
4378 dict_T *d;
4379#endif
4380
4381 if (rettv_list_alloc(rettv) != OK)
4382 return;
4383
4384#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004385 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4386 ++emsg_off;
4387 buf = get_buf_tv(&argvars[0], FALSE);
4388 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004389 if (buf == NULL)
4390 return;
4391
4392 l = list_alloc();
4393 if (l == NULL)
4394 return;
4395
4396 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4397 return;
4398 /*
4399 * The current window change list index tracks only the position in the
4400 * current buffer change list. For other buffers, use the change list
4401 * length as the current index.
4402 */
4403 list_append_number(rettv->vval.v_list,
4404 (varnumber_T)((buf == curwin->w_buffer)
4405 ? curwin->w_changelistidx : buf->b_changelistlen));
4406
4407 for (i = 0; i < buf->b_changelistlen; ++i)
4408 {
4409 if (buf->b_changelist[i].lnum == 0)
4410 continue;
4411 if ((d = dict_alloc()) == NULL)
4412 return;
4413 if (list_append_dict(l, d) == FAIL)
4414 return;
4415 dict_add_nr_str(d, "lnum", (long)buf->b_changelist[i].lnum, NULL);
4416 dict_add_nr_str(d, "col", (long)buf->b_changelist[i].col, NULL);
4417# ifdef FEAT_VIRTUALEDIT
4418 dict_add_nr_str(d, "coladd", (long)buf->b_changelist[i].coladd, NULL);
4419# endif
4420 }
4421#endif
4422}
4423/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004424 * "getchar()" function
4425 */
4426 static void
4427f_getchar(typval_T *argvars, typval_T *rettv)
4428{
4429 varnumber_T n;
4430 int error = FALSE;
4431
4432 /* Position the cursor. Needed after a message that ends in a space. */
4433 windgoto(msg_row, msg_col);
4434
4435 ++no_mapping;
4436 ++allow_keys;
4437 for (;;)
4438 {
4439 if (argvars[0].v_type == VAR_UNKNOWN)
4440 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004441 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004442 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4443 /* getchar(1): only check if char avail */
4444 n = vpeekc_any();
4445 else if (error || vpeekc_any() == NUL)
4446 /* illegal argument or getchar(0) and no char avail: return zero */
4447 n = 0;
4448 else
4449 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004450 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004451
4452 if (n == K_IGNORE)
4453 continue;
4454 break;
4455 }
4456 --no_mapping;
4457 --allow_keys;
4458
4459 set_vim_var_nr(VV_MOUSE_WIN, 0);
4460 set_vim_var_nr(VV_MOUSE_WINID, 0);
4461 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4462 set_vim_var_nr(VV_MOUSE_COL, 0);
4463
4464 rettv->vval.v_number = n;
4465 if (IS_SPECIAL(n) || mod_mask != 0)
4466 {
4467 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4468 int i = 0;
4469
4470 /* Turn a special key into three bytes, plus modifier. */
4471 if (mod_mask != 0)
4472 {
4473 temp[i++] = K_SPECIAL;
4474 temp[i++] = KS_MODIFIER;
4475 temp[i++] = mod_mask;
4476 }
4477 if (IS_SPECIAL(n))
4478 {
4479 temp[i++] = K_SPECIAL;
4480 temp[i++] = K_SECOND(n);
4481 temp[i++] = K_THIRD(n);
4482 }
4483#ifdef FEAT_MBYTE
4484 else if (has_mbyte)
4485 i += (*mb_char2bytes)(n, temp + i);
4486#endif
4487 else
4488 temp[i++] = n;
4489 temp[i++] = NUL;
4490 rettv->v_type = VAR_STRING;
4491 rettv->vval.v_string = vim_strsave(temp);
4492
4493#ifdef FEAT_MOUSE
4494 if (is_mouse_key(n))
4495 {
4496 int row = mouse_row;
4497 int col = mouse_col;
4498 win_T *win;
4499 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004500 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004501 int winnr = 1;
4502
4503 if (row >= 0 && col >= 0)
4504 {
4505 /* Find the window at the mouse coordinates and compute the
4506 * text position. */
4507 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004508 if (win == NULL)
4509 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004510 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004511 for (wp = firstwin; wp != win; wp = wp->w_next)
4512 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004513 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4514 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4515 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4516 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4517 }
4518 }
4519#endif
4520 }
4521}
4522
4523/*
4524 * "getcharmod()" function
4525 */
4526 static void
4527f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4528{
4529 rettv->vval.v_number = mod_mask;
4530}
4531
4532/*
4533 * "getcharsearch()" function
4534 */
4535 static void
4536f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4537{
4538 if (rettv_dict_alloc(rettv) != FAIL)
4539 {
4540 dict_T *dict = rettv->vval.v_dict;
4541
4542 dict_add_nr_str(dict, "char", 0L, last_csearch());
4543 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4544 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4545 }
4546}
4547
4548/*
4549 * "getcmdline()" function
4550 */
4551 static void
4552f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4553{
4554 rettv->v_type = VAR_STRING;
4555 rettv->vval.v_string = get_cmdline_str();
4556}
4557
4558/*
4559 * "getcmdpos()" function
4560 */
4561 static void
4562f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4563{
4564 rettv->vval.v_number = get_cmdline_pos() + 1;
4565}
4566
4567/*
4568 * "getcmdtype()" function
4569 */
4570 static void
4571f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4572{
4573 rettv->v_type = VAR_STRING;
4574 rettv->vval.v_string = alloc(2);
4575 if (rettv->vval.v_string != NULL)
4576 {
4577 rettv->vval.v_string[0] = get_cmdline_type();
4578 rettv->vval.v_string[1] = NUL;
4579 }
4580}
4581
4582/*
4583 * "getcmdwintype()" function
4584 */
4585 static void
4586f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4587{
4588 rettv->v_type = VAR_STRING;
4589 rettv->vval.v_string = NULL;
4590#ifdef FEAT_CMDWIN
4591 rettv->vval.v_string = alloc(2);
4592 if (rettv->vval.v_string != NULL)
4593 {
4594 rettv->vval.v_string[0] = cmdwin_type;
4595 rettv->vval.v_string[1] = NUL;
4596 }
4597#endif
4598}
4599
4600#if defined(FEAT_CMDL_COMPL)
4601/*
4602 * "getcompletion()" function
4603 */
4604 static void
4605f_getcompletion(typval_T *argvars, typval_T *rettv)
4606{
4607 char_u *pat;
4608 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004609 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004610 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4611 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004612
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004613 if (argvars[2].v_type != VAR_UNKNOWN)
4614 filtered = get_tv_number_chk(&argvars[2], NULL);
4615
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004616 if (p_wic)
4617 options |= WILD_ICASE;
4618
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004619 /* For filtered results, 'wildignore' is used */
4620 if (!filtered)
4621 options |= WILD_KEEP_ALL;
4622
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004623 ExpandInit(&xpc);
4624 xpc.xp_pattern = get_tv_string(&argvars[0]);
4625 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4626 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4627 if (xpc.xp_context == EXPAND_NOTHING)
4628 {
4629 if (argvars[1].v_type == VAR_STRING)
4630 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4631 else
4632 EMSG(_(e_invarg));
4633 return;
4634 }
4635
4636# if defined(FEAT_MENU)
4637 if (xpc.xp_context == EXPAND_MENUS)
4638 {
4639 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4640 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4641 }
4642# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004643#ifdef FEAT_CSCOPE
4644 if (xpc.xp_context == EXPAND_CSCOPE)
4645 {
4646 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4647 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4648 }
4649#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004650#ifdef FEAT_SIGNS
4651 if (xpc.xp_context == EXPAND_SIGN)
4652 {
4653 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4654 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4655 }
4656#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004657
4658 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4659 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4660 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004661 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004662
4663 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4664
4665 for (i = 0; i < xpc.xp_numfiles; i++)
4666 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4667 }
4668 vim_free(pat);
4669 ExpandCleanup(&xpc);
4670}
4671#endif
4672
4673/*
4674 * "getcwd()" function
4675 */
4676 static void
4677f_getcwd(typval_T *argvars, typval_T *rettv)
4678{
4679 win_T *wp = NULL;
4680 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004681 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004682
4683 rettv->v_type = VAR_STRING;
4684 rettv->vval.v_string = NULL;
4685
Bram Moolenaar54591292018-02-09 20:53:59 +01004686 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4687 global = TRUE;
4688 else
4689 wp = find_tabwin(&argvars[0], &argvars[1]);
4690
4691 if (wp != NULL && wp->w_localdir != NULL)
4692 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4693 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004694 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004695 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004696 rettv->vval.v_string = vim_strsave(globaldir);
4697 else
4698 {
4699 cwd = alloc(MAXPATHL);
4700 if (cwd != NULL)
4701 {
4702 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4703 rettv->vval.v_string = vim_strsave(cwd);
4704 vim_free(cwd);
4705 }
4706 }
4707#ifdef BACKSLASH_IN_FILENAME
4708 if (rettv->vval.v_string != NULL)
4709 slash_adjust(rettv->vval.v_string);
4710#endif
4711 }
4712}
4713
4714/*
4715 * "getfontname()" function
4716 */
4717 static void
4718f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4719{
4720 rettv->v_type = VAR_STRING;
4721 rettv->vval.v_string = NULL;
4722#ifdef FEAT_GUI
4723 if (gui.in_use)
4724 {
4725 GuiFont font;
4726 char_u *name = NULL;
4727
4728 if (argvars[0].v_type == VAR_UNKNOWN)
4729 {
4730 /* Get the "Normal" font. Either the name saved by
4731 * hl_set_font_name() or from the font ID. */
4732 font = gui.norm_font;
4733 name = hl_get_font_name();
4734 }
4735 else
4736 {
4737 name = get_tv_string(&argvars[0]);
4738 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4739 return;
4740 font = gui_mch_get_font(name, FALSE);
4741 if (font == NOFONT)
4742 return; /* Invalid font name, return empty string. */
4743 }
4744 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4745 if (argvars[0].v_type != VAR_UNKNOWN)
4746 gui_mch_free_font(font);
4747 }
4748#endif
4749}
4750
4751/*
4752 * "getfperm({fname})" function
4753 */
4754 static void
4755f_getfperm(typval_T *argvars, typval_T *rettv)
4756{
4757 char_u *fname;
4758 stat_T st;
4759 char_u *perm = NULL;
4760 char_u flags[] = "rwx";
4761 int i;
4762
4763 fname = get_tv_string(&argvars[0]);
4764
4765 rettv->v_type = VAR_STRING;
4766 if (mch_stat((char *)fname, &st) >= 0)
4767 {
4768 perm = vim_strsave((char_u *)"---------");
4769 if (perm != NULL)
4770 {
4771 for (i = 0; i < 9; i++)
4772 {
4773 if (st.st_mode & (1 << (8 - i)))
4774 perm[i] = flags[i % 3];
4775 }
4776 }
4777 }
4778 rettv->vval.v_string = perm;
4779}
4780
4781/*
4782 * "getfsize({fname})" function
4783 */
4784 static void
4785f_getfsize(typval_T *argvars, typval_T *rettv)
4786{
4787 char_u *fname;
4788 stat_T st;
4789
4790 fname = get_tv_string(&argvars[0]);
4791
4792 rettv->v_type = VAR_NUMBER;
4793
4794 if (mch_stat((char *)fname, &st) >= 0)
4795 {
4796 if (mch_isdir(fname))
4797 rettv->vval.v_number = 0;
4798 else
4799 {
4800 rettv->vval.v_number = (varnumber_T)st.st_size;
4801
4802 /* non-perfect check for overflow */
4803 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4804 rettv->vval.v_number = -2;
4805 }
4806 }
4807 else
4808 rettv->vval.v_number = -1;
4809}
4810
4811/*
4812 * "getftime({fname})" function
4813 */
4814 static void
4815f_getftime(typval_T *argvars, typval_T *rettv)
4816{
4817 char_u *fname;
4818 stat_T st;
4819
4820 fname = get_tv_string(&argvars[0]);
4821
4822 if (mch_stat((char *)fname, &st) >= 0)
4823 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4824 else
4825 rettv->vval.v_number = -1;
4826}
4827
4828/*
4829 * "getftype({fname})" function
4830 */
4831 static void
4832f_getftype(typval_T *argvars, typval_T *rettv)
4833{
4834 char_u *fname;
4835 stat_T st;
4836 char_u *type = NULL;
4837 char *t;
4838
4839 fname = get_tv_string(&argvars[0]);
4840
4841 rettv->v_type = VAR_STRING;
4842 if (mch_lstat((char *)fname, &st) >= 0)
4843 {
4844#ifdef S_ISREG
4845 if (S_ISREG(st.st_mode))
4846 t = "file";
4847 else if (S_ISDIR(st.st_mode))
4848 t = "dir";
4849# ifdef S_ISLNK
4850 else if (S_ISLNK(st.st_mode))
4851 t = "link";
4852# endif
4853# ifdef S_ISBLK
4854 else if (S_ISBLK(st.st_mode))
4855 t = "bdev";
4856# endif
4857# ifdef S_ISCHR
4858 else if (S_ISCHR(st.st_mode))
4859 t = "cdev";
4860# endif
4861# ifdef S_ISFIFO
4862 else if (S_ISFIFO(st.st_mode))
4863 t = "fifo";
4864# endif
4865# ifdef S_ISSOCK
4866 else if (S_ISSOCK(st.st_mode))
4867 t = "fifo";
4868# endif
4869 else
4870 t = "other";
4871#else
4872# ifdef S_IFMT
4873 switch (st.st_mode & S_IFMT)
4874 {
4875 case S_IFREG: t = "file"; break;
4876 case S_IFDIR: t = "dir"; break;
4877# ifdef S_IFLNK
4878 case S_IFLNK: t = "link"; break;
4879# endif
4880# ifdef S_IFBLK
4881 case S_IFBLK: t = "bdev"; break;
4882# endif
4883# ifdef S_IFCHR
4884 case S_IFCHR: t = "cdev"; break;
4885# endif
4886# ifdef S_IFIFO
4887 case S_IFIFO: t = "fifo"; break;
4888# endif
4889# ifdef S_IFSOCK
4890 case S_IFSOCK: t = "socket"; break;
4891# endif
4892 default: t = "other";
4893 }
4894# else
4895 if (mch_isdir(fname))
4896 t = "dir";
4897 else
4898 t = "file";
4899# endif
4900#endif
4901 type = vim_strsave((char_u *)t);
4902 }
4903 rettv->vval.v_string = type;
4904}
4905
4906/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004907 * "getjumplist()" function
4908 */
4909 static void
4910f_getjumplist(typval_T *argvars, typval_T *rettv)
4911{
4912#ifdef FEAT_JUMPLIST
4913 win_T *wp;
4914 int i;
4915 list_T *l;
4916 dict_T *d;
4917#endif
4918
4919 if (rettv_list_alloc(rettv) != OK)
4920 return;
4921
4922#ifdef FEAT_JUMPLIST
4923 wp = find_tabwin(&argvars[0], &argvars[1]);
4924 if (wp == NULL)
4925 return;
4926
4927 l = list_alloc();
4928 if (l == NULL)
4929 return;
4930
4931 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4932 return;
4933 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4934
Bram Moolenaar48679742018-02-13 13:33:29 +01004935 cleanup_jumplist(wp, TRUE);
4936
Bram Moolenaar4f505882018-02-10 21:06:32 +01004937 for (i = 0; i < wp->w_jumplistlen; ++i)
4938 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004939 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4940 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004941 if ((d = dict_alloc()) == NULL)
4942 return;
4943 if (list_append_dict(l, d) == FAIL)
4944 return;
4945 dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
4946 NULL);
4947 dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
4948 NULL);
4949# ifdef FEAT_VIRTUALEDIT
4950 dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
4951 NULL);
4952# endif
4953 dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004954 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaar4f505882018-02-10 21:06:32 +01004955 dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
4956 }
4957#endif
4958}
4959
4960/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004961 * "getline(lnum, [end])" function
4962 */
4963 static void
4964f_getline(typval_T *argvars, typval_T *rettv)
4965{
4966 linenr_T lnum;
4967 linenr_T end;
4968 int retlist;
4969
4970 lnum = get_tv_lnum(argvars);
4971 if (argvars[1].v_type == VAR_UNKNOWN)
4972 {
4973 end = 0;
4974 retlist = FALSE;
4975 }
4976 else
4977 {
4978 end = get_tv_lnum(&argvars[1]);
4979 retlist = TRUE;
4980 }
4981
4982 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4983}
4984
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004985#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004986 static void
4987get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4988{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004989 if (what_arg->v_type == VAR_UNKNOWN)
4990 {
4991 if (rettv_list_alloc(rettv) == OK)
4992 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004993 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004994 }
4995 else
4996 {
4997 if (rettv_dict_alloc(rettv) == OK)
4998 if (is_qf || (wp != NULL))
4999 {
5000 if (what_arg->v_type == VAR_DICT)
5001 {
5002 dict_T *d = what_arg->vval.v_dict;
5003
5004 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005005 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005006 }
5007 else
5008 EMSG(_(e_dictreq));
5009 }
5010 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005011}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005012#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005013
5014/*
5015 * "getloclist()" function
5016 */
5017 static void
5018f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5019{
5020#ifdef FEAT_QUICKFIX
5021 win_T *wp;
5022
5023 wp = find_win_by_nr(&argvars[0], NULL);
5024 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5025#endif
5026}
5027
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005028/*
5029 * "getmatches()" function
5030 */
5031 static void
5032f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5033{
5034#ifdef FEAT_SEARCH_EXTRA
5035 dict_T *dict;
5036 matchitem_T *cur = curwin->w_match_head;
5037 int i;
5038
5039 if (rettv_list_alloc(rettv) == OK)
5040 {
5041 while (cur != NULL)
5042 {
5043 dict = dict_alloc();
5044 if (dict == NULL)
5045 return;
5046 if (cur->match.regprog == NULL)
5047 {
5048 /* match added with matchaddpos() */
5049 for (i = 0; i < MAXPOSMATCH; ++i)
5050 {
5051 llpos_T *llpos;
5052 char buf[6];
5053 list_T *l;
5054
5055 llpos = &cur->pos.pos[i];
5056 if (llpos->lnum == 0)
5057 break;
5058 l = list_alloc();
5059 if (l == NULL)
5060 break;
5061 list_append_number(l, (varnumber_T)llpos->lnum);
5062 if (llpos->col > 0)
5063 {
5064 list_append_number(l, (varnumber_T)llpos->col);
5065 list_append_number(l, (varnumber_T)llpos->len);
5066 }
5067 sprintf(buf, "pos%d", i + 1);
5068 dict_add_list(dict, buf, l);
5069 }
5070 }
5071 else
5072 {
5073 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
5074 }
5075 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
5076 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
5077 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
5078# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5079 if (cur->conceal_char)
5080 {
5081 char_u buf[MB_MAXBYTES + 1];
5082
5083 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5084 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5085 }
5086# endif
5087 list_append_dict(rettv->vval.v_list, dict);
5088 cur = cur->next;
5089 }
5090 }
5091#endif
5092}
5093
5094/*
5095 * "getpid()" function
5096 */
5097 static void
5098f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5099{
5100 rettv->vval.v_number = mch_get_pid();
5101}
5102
5103 static void
5104getpos_both(
5105 typval_T *argvars,
5106 typval_T *rettv,
5107 int getcurpos)
5108{
5109 pos_T *fp;
5110 list_T *l;
5111 int fnum = -1;
5112
5113 if (rettv_list_alloc(rettv) == OK)
5114 {
5115 l = rettv->vval.v_list;
5116 if (getcurpos)
5117 fp = &curwin->w_cursor;
5118 else
5119 fp = var2fpos(&argvars[0], TRUE, &fnum);
5120 if (fnum != -1)
5121 list_append_number(l, (varnumber_T)fnum);
5122 else
5123 list_append_number(l, (varnumber_T)0);
5124 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5125 : (varnumber_T)0);
5126 list_append_number(l, (fp != NULL)
5127 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5128 : (varnumber_T)0);
5129 list_append_number(l,
5130#ifdef FEAT_VIRTUALEDIT
5131 (fp != NULL) ? (varnumber_T)fp->coladd :
5132#endif
5133 (varnumber_T)0);
5134 if (getcurpos)
5135 {
5136 update_curswant();
5137 list_append_number(l, curwin->w_curswant == MAXCOL ?
5138 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5139 }
5140 }
5141 else
5142 rettv->vval.v_number = FALSE;
5143}
5144
5145
5146/*
5147 * "getcurpos()" function
5148 */
5149 static void
5150f_getcurpos(typval_T *argvars, typval_T *rettv)
5151{
5152 getpos_both(argvars, rettv, TRUE);
5153}
5154
5155/*
5156 * "getpos(string)" function
5157 */
5158 static void
5159f_getpos(typval_T *argvars, typval_T *rettv)
5160{
5161 getpos_both(argvars, rettv, FALSE);
5162}
5163
5164/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005165 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005166 */
5167 static void
5168f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5169{
5170#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005171 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005172#endif
5173}
5174
5175/*
5176 * "getreg()" function
5177 */
5178 static void
5179f_getreg(typval_T *argvars, typval_T *rettv)
5180{
5181 char_u *strregname;
5182 int regname;
5183 int arg2 = FALSE;
5184 int return_list = FALSE;
5185 int error = FALSE;
5186
5187 if (argvars[0].v_type != VAR_UNKNOWN)
5188 {
5189 strregname = get_tv_string_chk(&argvars[0]);
5190 error = strregname == NULL;
5191 if (argvars[1].v_type != VAR_UNKNOWN)
5192 {
5193 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5194 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5195 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5196 }
5197 }
5198 else
5199 strregname = get_vim_var_str(VV_REG);
5200
5201 if (error)
5202 return;
5203
5204 regname = (strregname == NULL ? '"' : *strregname);
5205 if (regname == 0)
5206 regname = '"';
5207
5208 if (return_list)
5209 {
5210 rettv->v_type = VAR_LIST;
5211 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5212 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5213 if (rettv->vval.v_list == NULL)
5214 (void)rettv_list_alloc(rettv);
5215 else
5216 ++rettv->vval.v_list->lv_refcount;
5217 }
5218 else
5219 {
5220 rettv->v_type = VAR_STRING;
5221 rettv->vval.v_string = get_reg_contents(regname,
5222 arg2 ? GREG_EXPR_SRC : 0);
5223 }
5224}
5225
5226/*
5227 * "getregtype()" function
5228 */
5229 static void
5230f_getregtype(typval_T *argvars, typval_T *rettv)
5231{
5232 char_u *strregname;
5233 int regname;
5234 char_u buf[NUMBUFLEN + 2];
5235 long reglen = 0;
5236
5237 if (argvars[0].v_type != VAR_UNKNOWN)
5238 {
5239 strregname = get_tv_string_chk(&argvars[0]);
5240 if (strregname == NULL) /* type error; errmsg already given */
5241 {
5242 rettv->v_type = VAR_STRING;
5243 rettv->vval.v_string = NULL;
5244 return;
5245 }
5246 }
5247 else
5248 /* Default to v:register */
5249 strregname = get_vim_var_str(VV_REG);
5250
5251 regname = (strregname == NULL ? '"' : *strregname);
5252 if (regname == 0)
5253 regname = '"';
5254
5255 buf[0] = NUL;
5256 buf[1] = NUL;
5257 switch (get_reg_type(regname, &reglen))
5258 {
5259 case MLINE: buf[0] = 'V'; break;
5260 case MCHAR: buf[0] = 'v'; break;
5261 case MBLOCK:
5262 buf[0] = Ctrl_V;
5263 sprintf((char *)buf + 1, "%ld", reglen + 1);
5264 break;
5265 }
5266 rettv->v_type = VAR_STRING;
5267 rettv->vval.v_string = vim_strsave(buf);
5268}
5269
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005270/*
5271 * Returns information (variables, options, etc.) about a tab page
5272 * as a dictionary.
5273 */
5274 static dict_T *
5275get_tabpage_info(tabpage_T *tp, int tp_idx)
5276{
5277 win_T *wp;
5278 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005279 list_T *l;
5280
5281 dict = dict_alloc();
5282 if (dict == NULL)
5283 return NULL;
5284
Bram Moolenaar33928832016-08-18 21:22:04 +02005285 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005286
5287 l = list_alloc();
5288 if (l != NULL)
5289 {
5290 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5291 wp; wp = wp->w_next)
5292 list_append_number(l, (varnumber_T)wp->w_id);
5293 dict_add_list(dict, "windows", l);
5294 }
5295
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005296 /* Make a reference to tabpage variables */
5297 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005298
5299 return dict;
5300}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005301
5302/*
5303 * "gettabinfo()" function
5304 */
5305 static void
5306f_gettabinfo(typval_T *argvars, typval_T *rettv)
5307{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005308 tabpage_T *tp, *tparg = NULL;
5309 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005310 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005311
5312 if (rettv_list_alloc(rettv) != OK)
5313 return;
5314
5315 if (argvars[0].v_type != VAR_UNKNOWN)
5316 {
5317 /* Information about one tab page */
5318 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5319 if (tparg == NULL)
5320 return;
5321 }
5322
5323 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005324 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005325 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005326 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005327 if (tparg != NULL && tp != tparg)
5328 continue;
5329 d = get_tabpage_info(tp, tpnr);
5330 if (d != NULL)
5331 list_append_dict(rettv->vval.v_list, d);
5332 if (tparg != NULL)
5333 return;
5334 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005335}
5336
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005337/*
5338 * "gettabvar()" function
5339 */
5340 static void
5341f_gettabvar(typval_T *argvars, typval_T *rettv)
5342{
5343 win_T *oldcurwin;
5344 tabpage_T *tp, *oldtabpage;
5345 dictitem_T *v;
5346 char_u *varname;
5347 int done = FALSE;
5348
5349 rettv->v_type = VAR_STRING;
5350 rettv->vval.v_string = NULL;
5351
5352 varname = get_tv_string_chk(&argvars[1]);
5353 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5354 if (tp != NULL && varname != NULL)
5355 {
5356 /* Set tp to be our tabpage, temporarily. Also set the window to the
5357 * first window in the tabpage, otherwise the window is not valid. */
5358 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005359 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5360 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005361 {
5362 /* look up the variable */
5363 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5364 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5365 if (v != NULL)
5366 {
5367 copy_tv(&v->di_tv, rettv);
5368 done = TRUE;
5369 }
5370 }
5371
5372 /* restore previous notion of curwin */
5373 restore_win(oldcurwin, oldtabpage, TRUE);
5374 }
5375
5376 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5377 copy_tv(&argvars[2], rettv);
5378}
5379
5380/*
5381 * "gettabwinvar()" function
5382 */
5383 static void
5384f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5385{
5386 getwinvar(argvars, rettv, 1);
5387}
5388
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005389/*
5390 * Returns information about a window as a dictionary.
5391 */
5392 static dict_T *
5393get_win_info(win_T *wp, short tpnr, short winnr)
5394{
5395 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005396
5397 dict = dict_alloc();
5398 if (dict == NULL)
5399 return NULL;
5400
Bram Moolenaar33928832016-08-18 21:22:04 +02005401 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5402 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005403 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5404 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005405#ifdef FEAT_MENU
5406 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5407#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005408 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005409 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005410
Bram Moolenaar69905d12017-08-13 18:14:47 +02005411#ifdef FEAT_TERMINAL
5412 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5413#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005414#ifdef FEAT_QUICKFIX
5415 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5416 dict_add_nr_str(dict, "loclist",
5417 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5418#endif
5419
Bram Moolenaar30567352016-08-27 21:25:44 +02005420 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005421 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005422
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005423 return dict;
5424}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005425
5426/*
5427 * "getwininfo()" function
5428 */
5429 static void
5430f_getwininfo(typval_T *argvars, typval_T *rettv)
5431{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005432 tabpage_T *tp;
5433 win_T *wp = NULL, *wparg = NULL;
5434 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005435 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005436
5437 if (rettv_list_alloc(rettv) != OK)
5438 return;
5439
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005440 if (argvars[0].v_type != VAR_UNKNOWN)
5441 {
5442 wparg = win_id2wp(argvars);
5443 if (wparg == NULL)
5444 return;
5445 }
5446
5447 /* Collect information about either all the windows across all the tab
5448 * pages or one particular window.
5449 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005450 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005451 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005452 tabnr++;
5453 winnr = 0;
5454 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005455 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005456 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005457 if (wparg != NULL && wp != wparg)
5458 continue;
5459 d = get_win_info(wp, tabnr, winnr);
5460 if (d != NULL)
5461 list_append_dict(rettv->vval.v_list, d);
5462 if (wparg != NULL)
5463 /* found information about a specific window */
5464 return;
5465 }
5466 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005467}
5468
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005469/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005470 * "win_findbuf()" function
5471 */
5472 static void
5473f_win_findbuf(typval_T *argvars, typval_T *rettv)
5474{
5475 if (rettv_list_alloc(rettv) != FAIL)
5476 win_findbuf(argvars, rettv->vval.v_list);
5477}
5478
5479/*
5480 * "win_getid()" function
5481 */
5482 static void
5483f_win_getid(typval_T *argvars, typval_T *rettv)
5484{
5485 rettv->vval.v_number = win_getid(argvars);
5486}
5487
5488/*
5489 * "win_gotoid()" function
5490 */
5491 static void
5492f_win_gotoid(typval_T *argvars, typval_T *rettv)
5493{
5494 rettv->vval.v_number = win_gotoid(argvars);
5495}
5496
5497/*
5498 * "win_id2tabwin()" function
5499 */
5500 static void
5501f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5502{
5503 if (rettv_list_alloc(rettv) != FAIL)
5504 win_id2tabwin(argvars, rettv->vval.v_list);
5505}
5506
5507/*
5508 * "win_id2win()" function
5509 */
5510 static void
5511f_win_id2win(typval_T *argvars, typval_T *rettv)
5512{
5513 rettv->vval.v_number = win_id2win(argvars);
5514}
5515
5516/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005517 * "win_screenpos()" function
5518 */
5519 static void
5520f_win_screenpos(typval_T *argvars, typval_T *rettv)
5521{
5522 win_T *wp;
5523
5524 if (rettv_list_alloc(rettv) == FAIL)
5525 return;
5526
5527 wp = find_win_by_nr(&argvars[0], NULL);
5528 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5529 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5530}
5531
5532/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005533 * "getwinpos({timeout})" function
5534 */
5535 static void
5536f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5537{
5538 int x = -1;
5539 int y = -1;
5540
5541 if (rettv_list_alloc(rettv) == FAIL)
5542 return;
5543#ifdef FEAT_GUI
5544 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005545 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005546# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5547 else
5548# endif
5549#endif
5550#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5551 {
5552 varnumber_T timeout = 100;
5553
5554 if (argvars[0].v_type != VAR_UNKNOWN)
5555 timeout = get_tv_number(&argvars[0]);
5556 term_get_winpos(&x, &y, timeout);
5557 }
5558#endif
5559 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5560 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5561}
5562
5563
5564/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005565 * "getwinposx()" function
5566 */
5567 static void
5568f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5569{
5570 rettv->vval.v_number = -1;
5571#ifdef FEAT_GUI
5572 if (gui.in_use)
5573 {
5574 int x, y;
5575
5576 if (gui_mch_get_winpos(&x, &y) == OK)
5577 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005578 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005579 }
5580#endif
5581#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5582 {
5583 int x, y;
5584
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005585 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005586 rettv->vval.v_number = x;
5587 }
5588#endif
5589}
5590
5591/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005592 * "getwinposy()" function
5593 */
5594 static void
5595f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5596{
5597 rettv->vval.v_number = -1;
5598#ifdef FEAT_GUI
5599 if (gui.in_use)
5600 {
5601 int x, y;
5602
5603 if (gui_mch_get_winpos(&x, &y) == OK)
5604 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005605 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005606 }
5607#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005608#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5609 {
5610 int x, y;
5611
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005612 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005613 rettv->vval.v_number = y;
5614 }
5615#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005616}
5617
5618/*
5619 * "getwinvar()" function
5620 */
5621 static void
5622f_getwinvar(typval_T *argvars, typval_T *rettv)
5623{
5624 getwinvar(argvars, rettv, 0);
5625}
5626
5627/*
5628 * "glob()" function
5629 */
5630 static void
5631f_glob(typval_T *argvars, typval_T *rettv)
5632{
5633 int options = WILD_SILENT|WILD_USE_NL;
5634 expand_T xpc;
5635 int error = FALSE;
5636
5637 /* When the optional second argument is non-zero, don't remove matches
5638 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5639 rettv->v_type = VAR_STRING;
5640 if (argvars[1].v_type != VAR_UNKNOWN)
5641 {
5642 if (get_tv_number_chk(&argvars[1], &error))
5643 options |= WILD_KEEP_ALL;
5644 if (argvars[2].v_type != VAR_UNKNOWN)
5645 {
5646 if (get_tv_number_chk(&argvars[2], &error))
5647 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005648 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005649 }
5650 if (argvars[3].v_type != VAR_UNKNOWN
5651 && get_tv_number_chk(&argvars[3], &error))
5652 options |= WILD_ALLLINKS;
5653 }
5654 }
5655 if (!error)
5656 {
5657 ExpandInit(&xpc);
5658 xpc.xp_context = EXPAND_FILES;
5659 if (p_wic)
5660 options += WILD_ICASE;
5661 if (rettv->v_type == VAR_STRING)
5662 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5663 NULL, options, WILD_ALL);
5664 else if (rettv_list_alloc(rettv) != FAIL)
5665 {
5666 int i;
5667
5668 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5669 NULL, options, WILD_ALL_KEEP);
5670 for (i = 0; i < xpc.xp_numfiles; i++)
5671 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5672
5673 ExpandCleanup(&xpc);
5674 }
5675 }
5676 else
5677 rettv->vval.v_string = NULL;
5678}
5679
5680/*
5681 * "globpath()" function
5682 */
5683 static void
5684f_globpath(typval_T *argvars, typval_T *rettv)
5685{
5686 int flags = 0;
5687 char_u buf1[NUMBUFLEN];
5688 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5689 int error = FALSE;
5690 garray_T ga;
5691 int i;
5692
5693 /* When the optional second argument is non-zero, don't remove matches
5694 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5695 rettv->v_type = VAR_STRING;
5696 if (argvars[2].v_type != VAR_UNKNOWN)
5697 {
5698 if (get_tv_number_chk(&argvars[2], &error))
5699 flags |= WILD_KEEP_ALL;
5700 if (argvars[3].v_type != VAR_UNKNOWN)
5701 {
5702 if (get_tv_number_chk(&argvars[3], &error))
5703 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005704 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005705 }
5706 if (argvars[4].v_type != VAR_UNKNOWN
5707 && get_tv_number_chk(&argvars[4], &error))
5708 flags |= WILD_ALLLINKS;
5709 }
5710 }
5711 if (file != NULL && !error)
5712 {
5713 ga_init2(&ga, (int)sizeof(char_u *), 10);
5714 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5715 if (rettv->v_type == VAR_STRING)
5716 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5717 else if (rettv_list_alloc(rettv) != FAIL)
5718 for (i = 0; i < ga.ga_len; ++i)
5719 list_append_string(rettv->vval.v_list,
5720 ((char_u **)(ga.ga_data))[i], -1);
5721 ga_clear_strings(&ga);
5722 }
5723 else
5724 rettv->vval.v_string = NULL;
5725}
5726
5727/*
5728 * "glob2regpat()" function
5729 */
5730 static void
5731f_glob2regpat(typval_T *argvars, typval_T *rettv)
5732{
5733 char_u *pat = get_tv_string_chk(&argvars[0]);
5734
5735 rettv->v_type = VAR_STRING;
5736 rettv->vval.v_string = (pat == NULL)
5737 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5738}
5739
5740/* for VIM_VERSION_ defines */
5741#include "version.h"
5742
5743/*
5744 * "has()" function
5745 */
5746 static void
5747f_has(typval_T *argvars, typval_T *rettv)
5748{
5749 int i;
5750 char_u *name;
5751 int n = FALSE;
5752 static char *(has_list[]) =
5753 {
5754#ifdef AMIGA
5755 "amiga",
5756# ifdef FEAT_ARP
5757 "arp",
5758# endif
5759#endif
5760#ifdef __BEOS__
5761 "beos",
5762#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005763#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005764 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5765 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005766# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005767 "macunix", /* Mac OS X, with the darwin feature */
5768 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005769# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005770#endif
5771#ifdef __QNX__
5772 "qnx",
5773#endif
5774#ifdef UNIX
5775 "unix",
5776#endif
5777#ifdef VMS
5778 "vms",
5779#endif
5780#ifdef WIN32
5781 "win32",
5782#endif
5783#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5784 "win32unix",
5785#endif
5786#if defined(WIN64) || defined(_WIN64)
5787 "win64",
5788#endif
5789#ifdef EBCDIC
5790 "ebcdic",
5791#endif
5792#ifndef CASE_INSENSITIVE_FILENAME
5793 "fname_case",
5794#endif
5795#ifdef HAVE_ACL
5796 "acl",
5797#endif
5798#ifdef FEAT_ARABIC
5799 "arabic",
5800#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005801 "autocmd",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005802#ifdef FEAT_AUTOSERVERNAME
5803 "autoservername",
5804#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005805#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005806 "balloon_eval",
5807# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5808 "balloon_multiline",
5809# endif
5810#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005811#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005812 "balloon_eval_term",
5813#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005814#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5815 "builtin_terms",
5816# ifdef ALL_BUILTIN_TCAPS
5817 "all_builtin_terms",
5818# endif
5819#endif
5820#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5821 || defined(FEAT_GUI_W32) \
5822 || defined(FEAT_GUI_MOTIF))
5823 "browsefilter",
5824#endif
5825#ifdef FEAT_BYTEOFF
5826 "byte_offset",
5827#endif
5828#ifdef FEAT_JOB_CHANNEL
5829 "channel",
5830#endif
5831#ifdef FEAT_CINDENT
5832 "cindent",
5833#endif
5834#ifdef FEAT_CLIENTSERVER
5835 "clientserver",
5836#endif
5837#ifdef FEAT_CLIPBOARD
5838 "clipboard",
5839#endif
5840#ifdef FEAT_CMDL_COMPL
5841 "cmdline_compl",
5842#endif
5843#ifdef FEAT_CMDHIST
5844 "cmdline_hist",
5845#endif
5846#ifdef FEAT_COMMENTS
5847 "comments",
5848#endif
5849#ifdef FEAT_CONCEAL
5850 "conceal",
5851#endif
5852#ifdef FEAT_CRYPT
5853 "cryptv",
5854 "crypt-blowfish",
5855 "crypt-blowfish2",
5856#endif
5857#ifdef FEAT_CSCOPE
5858 "cscope",
5859#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005860 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005861#ifdef CURSOR_SHAPE
5862 "cursorshape",
5863#endif
5864#ifdef DEBUG
5865 "debug",
5866#endif
5867#ifdef FEAT_CON_DIALOG
5868 "dialog_con",
5869#endif
5870#ifdef FEAT_GUI_DIALOG
5871 "dialog_gui",
5872#endif
5873#ifdef FEAT_DIFF
5874 "diff",
5875#endif
5876#ifdef FEAT_DIGRAPHS
5877 "digraphs",
5878#endif
5879#ifdef FEAT_DIRECTX
5880 "directx",
5881#endif
5882#ifdef FEAT_DND
5883 "dnd",
5884#endif
5885#ifdef FEAT_EMACS_TAGS
5886 "emacs_tags",
5887#endif
5888 "eval", /* always present, of course! */
5889 "ex_extra", /* graduated feature */
5890#ifdef FEAT_SEARCH_EXTRA
5891 "extra_search",
5892#endif
5893#ifdef FEAT_FKMAP
5894 "farsi",
5895#endif
5896#ifdef FEAT_SEARCHPATH
5897 "file_in_path",
5898#endif
5899#ifdef FEAT_FILTERPIPE
5900 "filterpipe",
5901#endif
5902#ifdef FEAT_FIND_ID
5903 "find_in_path",
5904#endif
5905#ifdef FEAT_FLOAT
5906 "float",
5907#endif
5908#ifdef FEAT_FOLDING
5909 "folding",
5910#endif
5911#ifdef FEAT_FOOTER
5912 "footer",
5913#endif
5914#if !defined(USE_SYSTEM) && defined(UNIX)
5915 "fork",
5916#endif
5917#ifdef FEAT_GETTEXT
5918 "gettext",
5919#endif
5920#ifdef FEAT_GUI
5921 "gui",
5922#endif
5923#ifdef FEAT_GUI_ATHENA
5924# ifdef FEAT_GUI_NEXTAW
5925 "gui_neXtaw",
5926# else
5927 "gui_athena",
5928# endif
5929#endif
5930#ifdef FEAT_GUI_GTK
5931 "gui_gtk",
5932# ifdef USE_GTK3
5933 "gui_gtk3",
5934# else
5935 "gui_gtk2",
5936# endif
5937#endif
5938#ifdef FEAT_GUI_GNOME
5939 "gui_gnome",
5940#endif
5941#ifdef FEAT_GUI_MAC
5942 "gui_mac",
5943#endif
5944#ifdef FEAT_GUI_MOTIF
5945 "gui_motif",
5946#endif
5947#ifdef FEAT_GUI_PHOTON
5948 "gui_photon",
5949#endif
5950#ifdef FEAT_GUI_W32
5951 "gui_win32",
5952#endif
5953#ifdef FEAT_HANGULIN
5954 "hangul_input",
5955#endif
5956#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5957 "iconv",
5958#endif
5959#ifdef FEAT_INS_EXPAND
5960 "insert_expand",
5961#endif
5962#ifdef FEAT_JOB_CHANNEL
5963 "job",
5964#endif
5965#ifdef FEAT_JUMPLIST
5966 "jumplist",
5967#endif
5968#ifdef FEAT_KEYMAP
5969 "keymap",
5970#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005971 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005972#ifdef FEAT_LANGMAP
5973 "langmap",
5974#endif
5975#ifdef FEAT_LIBCALL
5976 "libcall",
5977#endif
5978#ifdef FEAT_LINEBREAK
5979 "linebreak",
5980#endif
5981#ifdef FEAT_LISP
5982 "lispindent",
5983#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005984 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005985#ifdef FEAT_LOCALMAP
5986 "localmap",
5987#endif
5988#ifdef FEAT_LUA
5989# ifndef DYNAMIC_LUA
5990 "lua",
5991# endif
5992#endif
5993#ifdef FEAT_MENU
5994 "menu",
5995#endif
5996#ifdef FEAT_SESSION
5997 "mksession",
5998#endif
5999#ifdef FEAT_MODIFY_FNAME
6000 "modify_fname",
6001#endif
6002#ifdef FEAT_MOUSE
6003 "mouse",
6004#endif
6005#ifdef FEAT_MOUSESHAPE
6006 "mouseshape",
6007#endif
6008#if defined(UNIX) || defined(VMS)
6009# ifdef FEAT_MOUSE_DEC
6010 "mouse_dec",
6011# endif
6012# ifdef FEAT_MOUSE_GPM
6013 "mouse_gpm",
6014# endif
6015# ifdef FEAT_MOUSE_JSB
6016 "mouse_jsbterm",
6017# endif
6018# ifdef FEAT_MOUSE_NET
6019 "mouse_netterm",
6020# endif
6021# ifdef FEAT_MOUSE_PTERM
6022 "mouse_pterm",
6023# endif
6024# ifdef FEAT_MOUSE_SGR
6025 "mouse_sgr",
6026# endif
6027# ifdef FEAT_SYSMOUSE
6028 "mouse_sysmouse",
6029# endif
6030# ifdef FEAT_MOUSE_URXVT
6031 "mouse_urxvt",
6032# endif
6033# ifdef FEAT_MOUSE_XTERM
6034 "mouse_xterm",
6035# endif
6036#endif
6037#ifdef FEAT_MBYTE
6038 "multi_byte",
6039#endif
6040#ifdef FEAT_MBYTE_IME
6041 "multi_byte_ime",
6042#endif
6043#ifdef FEAT_MULTI_LANG
6044 "multi_lang",
6045#endif
6046#ifdef FEAT_MZSCHEME
6047#ifndef DYNAMIC_MZSCHEME
6048 "mzscheme",
6049#endif
6050#endif
6051#ifdef FEAT_NUM64
6052 "num64",
6053#endif
6054#ifdef FEAT_OLE
6055 "ole",
6056#endif
6057 "packages",
6058#ifdef FEAT_PATH_EXTRA
6059 "path_extra",
6060#endif
6061#ifdef FEAT_PERL
6062#ifndef DYNAMIC_PERL
6063 "perl",
6064#endif
6065#endif
6066#ifdef FEAT_PERSISTENT_UNDO
6067 "persistent_undo",
6068#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006069#if defined(FEAT_PYTHON)
6070 "python_compiled",
6071# if defined(DYNAMIC_PYTHON)
6072 "python_dynamic",
6073# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006074 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006075 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006076# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006077#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006078#if defined(FEAT_PYTHON3)
6079 "python3_compiled",
6080# if defined(DYNAMIC_PYTHON3)
6081 "python3_dynamic",
6082# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006083 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006084 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006085# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006086#endif
6087#ifdef FEAT_POSTSCRIPT
6088 "postscript",
6089#endif
6090#ifdef FEAT_PRINTER
6091 "printer",
6092#endif
6093#ifdef FEAT_PROFILE
6094 "profile",
6095#endif
6096#ifdef FEAT_RELTIME
6097 "reltime",
6098#endif
6099#ifdef FEAT_QUICKFIX
6100 "quickfix",
6101#endif
6102#ifdef FEAT_RIGHTLEFT
6103 "rightleft",
6104#endif
6105#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6106 "ruby",
6107#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006108 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006109#ifdef FEAT_CMDL_INFO
6110 "showcmd",
6111 "cmdline_info",
6112#endif
6113#ifdef FEAT_SIGNS
6114 "signs",
6115#endif
6116#ifdef FEAT_SMARTINDENT
6117 "smartindent",
6118#endif
6119#ifdef STARTUPTIME
6120 "startuptime",
6121#endif
6122#ifdef FEAT_STL_OPT
6123 "statusline",
6124#endif
6125#ifdef FEAT_SUN_WORKSHOP
6126 "sun_workshop",
6127#endif
6128#ifdef FEAT_NETBEANS_INTG
6129 "netbeans_intg",
6130#endif
6131#ifdef FEAT_SPELL
6132 "spell",
6133#endif
6134#ifdef FEAT_SYN_HL
6135 "syntax",
6136#endif
6137#if defined(USE_SYSTEM) || !defined(UNIX)
6138 "system",
6139#endif
6140#ifdef FEAT_TAG_BINS
6141 "tag_binary",
6142#endif
6143#ifdef FEAT_TAG_OLDSTATIC
6144 "tag_old_static",
6145#endif
6146#ifdef FEAT_TAG_ANYWHITE
6147 "tag_any_white",
6148#endif
6149#ifdef FEAT_TCL
6150# ifndef DYNAMIC_TCL
6151 "tcl",
6152# endif
6153#endif
6154#ifdef FEAT_TERMGUICOLORS
6155 "termguicolors",
6156#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006157#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006158 "terminal",
6159#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006160#ifdef TERMINFO
6161 "terminfo",
6162#endif
6163#ifdef FEAT_TERMRESPONSE
6164 "termresponse",
6165#endif
6166#ifdef FEAT_TEXTOBJ
6167 "textobjects",
6168#endif
6169#ifdef HAVE_TGETENT
6170 "tgetent",
6171#endif
6172#ifdef FEAT_TIMERS
6173 "timers",
6174#endif
6175#ifdef FEAT_TITLE
6176 "title",
6177#endif
6178#ifdef FEAT_TOOLBAR
6179 "toolbar",
6180#endif
6181#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6182 "unnamedplus",
6183#endif
6184#ifdef FEAT_USR_CMDS
6185 "user-commands", /* was accidentally included in 5.4 */
6186 "user_commands",
6187#endif
6188#ifdef FEAT_VIMINFO
6189 "viminfo",
6190#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006191 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006192#ifdef FEAT_VIRTUALEDIT
6193 "virtualedit",
6194#endif
6195 "visual",
6196#ifdef FEAT_VISUALEXTRA
6197 "visualextra",
6198#endif
6199#ifdef FEAT_VREPLACE
6200 "vreplace",
6201#endif
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006202#ifdef FEAT_VTP
6203 "vtp",
6204#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006205#ifdef FEAT_WILDIGN
6206 "wildignore",
6207#endif
6208#ifdef FEAT_WILDMENU
6209 "wildmenu",
6210#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006211 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006212#ifdef FEAT_WAK
6213 "winaltkeys",
6214#endif
6215#ifdef FEAT_WRITEBACKUP
6216 "writebackup",
6217#endif
6218#ifdef FEAT_XIM
6219 "xim",
6220#endif
6221#ifdef FEAT_XFONTSET
6222 "xfontset",
6223#endif
6224#ifdef FEAT_XPM_W32
6225 "xpm",
6226 "xpm_w32", /* for backward compatibility */
6227#else
6228# if defined(HAVE_XPM)
6229 "xpm",
6230# endif
6231#endif
6232#ifdef USE_XSMP
6233 "xsmp",
6234#endif
6235#ifdef USE_XSMP_INTERACT
6236 "xsmp_interact",
6237#endif
6238#ifdef FEAT_XCLIPBOARD
6239 "xterm_clipboard",
6240#endif
6241#ifdef FEAT_XTERM_SAVE
6242 "xterm_save",
6243#endif
6244#if defined(UNIX) && defined(FEAT_X11)
6245 "X11",
6246#endif
6247 NULL
6248 };
6249
6250 name = get_tv_string(&argvars[0]);
6251 for (i = 0; has_list[i] != NULL; ++i)
6252 if (STRICMP(name, has_list[i]) == 0)
6253 {
6254 n = TRUE;
6255 break;
6256 }
6257
6258 if (n == FALSE)
6259 {
6260 if (STRNICMP(name, "patch", 5) == 0)
6261 {
6262 if (name[5] == '-'
6263 && STRLEN(name) >= 11
6264 && vim_isdigit(name[6])
6265 && vim_isdigit(name[8])
6266 && vim_isdigit(name[10]))
6267 {
6268 int major = atoi((char *)name + 6);
6269 int minor = atoi((char *)name + 8);
6270
6271 /* Expect "patch-9.9.01234". */
6272 n = (major < VIM_VERSION_MAJOR
6273 || (major == VIM_VERSION_MAJOR
6274 && (minor < VIM_VERSION_MINOR
6275 || (minor == VIM_VERSION_MINOR
6276 && has_patch(atoi((char *)name + 10))))));
6277 }
6278 else
6279 n = has_patch(atoi((char *)name + 5));
6280 }
6281 else if (STRICMP(name, "vim_starting") == 0)
6282 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006283 else if (STRICMP(name, "ttyin") == 0)
6284 n = mch_input_isatty();
6285 else if (STRICMP(name, "ttyout") == 0)
6286 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006287#ifdef FEAT_MBYTE
6288 else if (STRICMP(name, "multi_byte_encoding") == 0)
6289 n = has_mbyte;
6290#endif
6291#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6292 else if (STRICMP(name, "balloon_multiline") == 0)
6293 n = multiline_balloon_available();
6294#endif
6295#ifdef DYNAMIC_TCL
6296 else if (STRICMP(name, "tcl") == 0)
6297 n = tcl_enabled(FALSE);
6298#endif
6299#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6300 else if (STRICMP(name, "iconv") == 0)
6301 n = iconv_enabled(FALSE);
6302#endif
6303#ifdef DYNAMIC_LUA
6304 else if (STRICMP(name, "lua") == 0)
6305 n = lua_enabled(FALSE);
6306#endif
6307#ifdef DYNAMIC_MZSCHEME
6308 else if (STRICMP(name, "mzscheme") == 0)
6309 n = mzscheme_enabled(FALSE);
6310#endif
6311#ifdef DYNAMIC_RUBY
6312 else if (STRICMP(name, "ruby") == 0)
6313 n = ruby_enabled(FALSE);
6314#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006315#ifdef DYNAMIC_PYTHON
6316 else if (STRICMP(name, "python") == 0)
6317 n = python_enabled(FALSE);
6318#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006319#ifdef DYNAMIC_PYTHON3
6320 else if (STRICMP(name, "python3") == 0)
6321 n = python3_enabled(FALSE);
6322#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006323#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6324 else if (STRICMP(name, "pythonx") == 0)
6325 {
6326# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6327 if (p_pyx == 0)
6328 n = python3_enabled(FALSE) || python_enabled(FALSE);
6329 else if (p_pyx == 3)
6330 n = python3_enabled(FALSE);
6331 else if (p_pyx == 2)
6332 n = python_enabled(FALSE);
6333# elif defined(DYNAMIC_PYTHON)
6334 n = python_enabled(FALSE);
6335# elif defined(DYNAMIC_PYTHON3)
6336 n = python3_enabled(FALSE);
6337# endif
6338 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006339#endif
6340#ifdef DYNAMIC_PERL
6341 else if (STRICMP(name, "perl") == 0)
6342 n = perl_enabled(FALSE);
6343#endif
6344#ifdef FEAT_GUI
6345 else if (STRICMP(name, "gui_running") == 0)
6346 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006347# ifdef FEAT_BROWSE
6348 else if (STRICMP(name, "browse") == 0)
6349 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6350# endif
6351#endif
6352#ifdef FEAT_SYN_HL
6353 else if (STRICMP(name, "syntax_items") == 0)
6354 n = syntax_present(curwin);
6355#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006356#ifdef FEAT_VTP
6357 else if (STRICMP(name, "vcon") == 0)
6358 n = has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006359#endif
6360#ifdef FEAT_NETBEANS_INTG
6361 else if (STRICMP(name, "netbeans_enabled") == 0)
6362 n = netbeans_active();
6363#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006364#if defined(FEAT_TERMINAL) && defined(WIN3264)
6365 else if (STRICMP(name, "terminal") == 0)
6366 n = terminal_enabled();
6367#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006368 }
6369
6370 rettv->vval.v_number = n;
6371}
6372
6373/*
6374 * "has_key()" function
6375 */
6376 static void
6377f_has_key(typval_T *argvars, typval_T *rettv)
6378{
6379 if (argvars[0].v_type != VAR_DICT)
6380 {
6381 EMSG(_(e_dictreq));
6382 return;
6383 }
6384 if (argvars[0].vval.v_dict == NULL)
6385 return;
6386
6387 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6388 get_tv_string(&argvars[1]), -1) != NULL;
6389}
6390
6391/*
6392 * "haslocaldir()" function
6393 */
6394 static void
6395f_haslocaldir(typval_T *argvars, typval_T *rettv)
6396{
6397 win_T *wp = NULL;
6398
6399 wp = find_tabwin(&argvars[0], &argvars[1]);
6400 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6401}
6402
6403/*
6404 * "hasmapto()" function
6405 */
6406 static void
6407f_hasmapto(typval_T *argvars, typval_T *rettv)
6408{
6409 char_u *name;
6410 char_u *mode;
6411 char_u buf[NUMBUFLEN];
6412 int abbr = FALSE;
6413
6414 name = get_tv_string(&argvars[0]);
6415 if (argvars[1].v_type == VAR_UNKNOWN)
6416 mode = (char_u *)"nvo";
6417 else
6418 {
6419 mode = get_tv_string_buf(&argvars[1], buf);
6420 if (argvars[2].v_type != VAR_UNKNOWN)
6421 abbr = (int)get_tv_number(&argvars[2]);
6422 }
6423
6424 if (map_to_exists(name, mode, abbr))
6425 rettv->vval.v_number = TRUE;
6426 else
6427 rettv->vval.v_number = FALSE;
6428}
6429
6430/*
6431 * "histadd()" function
6432 */
6433 static void
6434f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6435{
6436#ifdef FEAT_CMDHIST
6437 int histype;
6438 char_u *str;
6439 char_u buf[NUMBUFLEN];
6440#endif
6441
6442 rettv->vval.v_number = FALSE;
6443 if (check_restricted() || check_secure())
6444 return;
6445#ifdef FEAT_CMDHIST
6446 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6447 histype = str != NULL ? get_histtype(str) : -1;
6448 if (histype >= 0)
6449 {
6450 str = get_tv_string_buf(&argvars[1], buf);
6451 if (*str != NUL)
6452 {
6453 init_history();
6454 add_to_history(histype, str, FALSE, NUL);
6455 rettv->vval.v_number = TRUE;
6456 return;
6457 }
6458 }
6459#endif
6460}
6461
6462/*
6463 * "histdel()" function
6464 */
6465 static void
6466f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6467{
6468#ifdef FEAT_CMDHIST
6469 int n;
6470 char_u buf[NUMBUFLEN];
6471 char_u *str;
6472
6473 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6474 if (str == NULL)
6475 n = 0;
6476 else if (argvars[1].v_type == VAR_UNKNOWN)
6477 /* only one argument: clear entire history */
6478 n = clr_history(get_histtype(str));
6479 else if (argvars[1].v_type == VAR_NUMBER)
6480 /* index given: remove that entry */
6481 n = del_history_idx(get_histtype(str),
6482 (int)get_tv_number(&argvars[1]));
6483 else
6484 /* string given: remove all matching entries */
6485 n = del_history_entry(get_histtype(str),
6486 get_tv_string_buf(&argvars[1], buf));
6487 rettv->vval.v_number = n;
6488#endif
6489}
6490
6491/*
6492 * "histget()" function
6493 */
6494 static void
6495f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6496{
6497#ifdef FEAT_CMDHIST
6498 int type;
6499 int idx;
6500 char_u *str;
6501
6502 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6503 if (str == NULL)
6504 rettv->vval.v_string = NULL;
6505 else
6506 {
6507 type = get_histtype(str);
6508 if (argvars[1].v_type == VAR_UNKNOWN)
6509 idx = get_history_idx(type);
6510 else
6511 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6512 /* -1 on type error */
6513 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6514 }
6515#else
6516 rettv->vval.v_string = NULL;
6517#endif
6518 rettv->v_type = VAR_STRING;
6519}
6520
6521/*
6522 * "histnr()" function
6523 */
6524 static void
6525f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6526{
6527 int i;
6528
6529#ifdef FEAT_CMDHIST
6530 char_u *history = get_tv_string_chk(&argvars[0]);
6531
6532 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6533 if (i >= HIST_CMD && i < HIST_COUNT)
6534 i = get_history_idx(i);
6535 else
6536#endif
6537 i = -1;
6538 rettv->vval.v_number = i;
6539}
6540
6541/*
6542 * "highlightID(name)" function
6543 */
6544 static void
6545f_hlID(typval_T *argvars, typval_T *rettv)
6546{
6547 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6548}
6549
6550/*
6551 * "highlight_exists()" function
6552 */
6553 static void
6554f_hlexists(typval_T *argvars, typval_T *rettv)
6555{
6556 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6557}
6558
6559/*
6560 * "hostname()" function
6561 */
6562 static void
6563f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6564{
6565 char_u hostname[256];
6566
6567 mch_get_host_name(hostname, 256);
6568 rettv->v_type = VAR_STRING;
6569 rettv->vval.v_string = vim_strsave(hostname);
6570}
6571
6572/*
6573 * iconv() function
6574 */
6575 static void
6576f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6577{
6578#ifdef FEAT_MBYTE
6579 char_u buf1[NUMBUFLEN];
6580 char_u buf2[NUMBUFLEN];
6581 char_u *from, *to, *str;
6582 vimconv_T vimconv;
6583#endif
6584
6585 rettv->v_type = VAR_STRING;
6586 rettv->vval.v_string = NULL;
6587
6588#ifdef FEAT_MBYTE
6589 str = get_tv_string(&argvars[0]);
6590 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6591 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6592 vimconv.vc_type = CONV_NONE;
6593 convert_setup(&vimconv, from, to);
6594
6595 /* If the encodings are equal, no conversion needed. */
6596 if (vimconv.vc_type == CONV_NONE)
6597 rettv->vval.v_string = vim_strsave(str);
6598 else
6599 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6600
6601 convert_setup(&vimconv, NULL, NULL);
6602 vim_free(from);
6603 vim_free(to);
6604#endif
6605}
6606
6607/*
6608 * "indent()" function
6609 */
6610 static void
6611f_indent(typval_T *argvars, typval_T *rettv)
6612{
6613 linenr_T lnum;
6614
6615 lnum = get_tv_lnum(argvars);
6616 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6617 rettv->vval.v_number = get_indent_lnum(lnum);
6618 else
6619 rettv->vval.v_number = -1;
6620}
6621
6622/*
6623 * "index()" function
6624 */
6625 static void
6626f_index(typval_T *argvars, typval_T *rettv)
6627{
6628 list_T *l;
6629 listitem_T *item;
6630 long idx = 0;
6631 int ic = FALSE;
6632
6633 rettv->vval.v_number = -1;
6634 if (argvars[0].v_type != VAR_LIST)
6635 {
6636 EMSG(_(e_listreq));
6637 return;
6638 }
6639 l = argvars[0].vval.v_list;
6640 if (l != NULL)
6641 {
6642 item = l->lv_first;
6643 if (argvars[2].v_type != VAR_UNKNOWN)
6644 {
6645 int error = FALSE;
6646
6647 /* Start at specified item. Use the cached index that list_find()
6648 * sets, so that a negative number also works. */
6649 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6650 idx = l->lv_idx;
6651 if (argvars[3].v_type != VAR_UNKNOWN)
6652 ic = (int)get_tv_number_chk(&argvars[3], &error);
6653 if (error)
6654 item = NULL;
6655 }
6656
6657 for ( ; item != NULL; item = item->li_next, ++idx)
6658 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6659 {
6660 rettv->vval.v_number = idx;
6661 break;
6662 }
6663 }
6664}
6665
6666static int inputsecret_flag = 0;
6667
6668/*
6669 * "input()" function
6670 * Also handles inputsecret() when inputsecret is set.
6671 */
6672 static void
6673f_input(typval_T *argvars, typval_T *rettv)
6674{
6675 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6676}
6677
6678/*
6679 * "inputdialog()" function
6680 */
6681 static void
6682f_inputdialog(typval_T *argvars, typval_T *rettv)
6683{
6684#if defined(FEAT_GUI_TEXTDIALOG)
6685 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6686 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6687 {
6688 char_u *message;
6689 char_u buf[NUMBUFLEN];
6690 char_u *defstr = (char_u *)"";
6691
6692 message = get_tv_string_chk(&argvars[0]);
6693 if (argvars[1].v_type != VAR_UNKNOWN
6694 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6695 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6696 else
6697 IObuff[0] = NUL;
6698 if (message != NULL && defstr != NULL
6699 && do_dialog(VIM_QUESTION, NULL, message,
6700 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6701 rettv->vval.v_string = vim_strsave(IObuff);
6702 else
6703 {
6704 if (message != NULL && defstr != NULL
6705 && argvars[1].v_type != VAR_UNKNOWN
6706 && argvars[2].v_type != VAR_UNKNOWN)
6707 rettv->vval.v_string = vim_strsave(
6708 get_tv_string_buf(&argvars[2], buf));
6709 else
6710 rettv->vval.v_string = NULL;
6711 }
6712 rettv->v_type = VAR_STRING;
6713 }
6714 else
6715#endif
6716 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6717}
6718
6719/*
6720 * "inputlist()" function
6721 */
6722 static void
6723f_inputlist(typval_T *argvars, typval_T *rettv)
6724{
6725 listitem_T *li;
6726 int selected;
6727 int mouse_used;
6728
6729#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006730 /* While starting up, there is no place to enter text. When running tests
6731 * with --not-a-term we assume feedkeys() will be used. */
6732 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006733 return;
6734#endif
6735 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6736 {
6737 EMSG2(_(e_listarg), "inputlist()");
6738 return;
6739 }
6740
6741 msg_start();
6742 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6743 lines_left = Rows; /* avoid more prompt */
6744 msg_scroll = TRUE;
6745 msg_clr_eos();
6746
6747 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6748 {
6749 msg_puts(get_tv_string(&li->li_tv));
6750 msg_putchar('\n');
6751 }
6752
6753 /* Ask for choice. */
6754 selected = prompt_for_number(&mouse_used);
6755 if (mouse_used)
6756 selected -= lines_left;
6757
6758 rettv->vval.v_number = selected;
6759}
6760
6761
6762static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6763
6764/*
6765 * "inputrestore()" function
6766 */
6767 static void
6768f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6769{
6770 if (ga_userinput.ga_len > 0)
6771 {
6772 --ga_userinput.ga_len;
6773 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6774 + ga_userinput.ga_len);
6775 /* default return is zero == OK */
6776 }
6777 else if (p_verbose > 1)
6778 {
6779 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6780 rettv->vval.v_number = 1; /* Failed */
6781 }
6782}
6783
6784/*
6785 * "inputsave()" function
6786 */
6787 static void
6788f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6789{
6790 /* Add an entry to the stack of typeahead storage. */
6791 if (ga_grow(&ga_userinput, 1) == OK)
6792 {
6793 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6794 + ga_userinput.ga_len);
6795 ++ga_userinput.ga_len;
6796 /* default return is zero == OK */
6797 }
6798 else
6799 rettv->vval.v_number = 1; /* Failed */
6800}
6801
6802/*
6803 * "inputsecret()" function
6804 */
6805 static void
6806f_inputsecret(typval_T *argvars, typval_T *rettv)
6807{
6808 ++cmdline_star;
6809 ++inputsecret_flag;
6810 f_input(argvars, rettv);
6811 --cmdline_star;
6812 --inputsecret_flag;
6813}
6814
6815/*
6816 * "insert()" function
6817 */
6818 static void
6819f_insert(typval_T *argvars, typval_T *rettv)
6820{
6821 long before = 0;
6822 listitem_T *item;
6823 list_T *l;
6824 int error = FALSE;
6825
6826 if (argvars[0].v_type != VAR_LIST)
6827 EMSG2(_(e_listarg), "insert()");
6828 else if ((l = argvars[0].vval.v_list) != NULL
6829 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6830 {
6831 if (argvars[2].v_type != VAR_UNKNOWN)
6832 before = (long)get_tv_number_chk(&argvars[2], &error);
6833 if (error)
6834 return; /* type error; errmsg already given */
6835
6836 if (before == l->lv_len)
6837 item = NULL;
6838 else
6839 {
6840 item = list_find(l, before);
6841 if (item == NULL)
6842 {
6843 EMSGN(_(e_listidx), before);
6844 l = NULL;
6845 }
6846 }
6847 if (l != NULL)
6848 {
6849 list_insert_tv(l, &argvars[1], item);
6850 copy_tv(&argvars[0], rettv);
6851 }
6852 }
6853}
6854
6855/*
6856 * "invert(expr)" function
6857 */
6858 static void
6859f_invert(typval_T *argvars, typval_T *rettv)
6860{
6861 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6862}
6863
6864/*
6865 * "isdirectory()" function
6866 */
6867 static void
6868f_isdirectory(typval_T *argvars, typval_T *rettv)
6869{
6870 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6871}
6872
6873/*
6874 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6875 * or it refers to a List or Dictionary that is locked.
6876 */
6877 static int
6878tv_islocked(typval_T *tv)
6879{
6880 return (tv->v_lock & VAR_LOCKED)
6881 || (tv->v_type == VAR_LIST
6882 && tv->vval.v_list != NULL
6883 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6884 || (tv->v_type == VAR_DICT
6885 && tv->vval.v_dict != NULL
6886 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6887}
6888
6889/*
6890 * "islocked()" function
6891 */
6892 static void
6893f_islocked(typval_T *argvars, typval_T *rettv)
6894{
6895 lval_T lv;
6896 char_u *end;
6897 dictitem_T *di;
6898
6899 rettv->vval.v_number = -1;
6900 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006901 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006902 if (end != NULL && lv.ll_name != NULL)
6903 {
6904 if (*end != NUL)
6905 EMSG(_(e_trailing));
6906 else
6907 {
6908 if (lv.ll_tv == NULL)
6909 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006910 di = find_var(lv.ll_name, NULL, TRUE);
6911 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006912 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006913 /* Consider a variable locked when:
6914 * 1. the variable itself is locked
6915 * 2. the value of the variable is locked.
6916 * 3. the List or Dict value is locked.
6917 */
6918 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6919 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006920 }
6921 }
6922 else if (lv.ll_range)
6923 EMSG(_("E786: Range not allowed"));
6924 else if (lv.ll_newkey != NULL)
6925 EMSG2(_(e_dictkey), lv.ll_newkey);
6926 else if (lv.ll_list != NULL)
6927 /* List item. */
6928 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6929 else
6930 /* Dictionary item. */
6931 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6932 }
6933 }
6934
6935 clear_lval(&lv);
6936}
6937
6938#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6939/*
6940 * "isnan()" function
6941 */
6942 static void
6943f_isnan(typval_T *argvars, typval_T *rettv)
6944{
6945 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6946 && isnan(argvars[0].vval.v_float);
6947}
6948#endif
6949
6950/*
6951 * "items(dict)" function
6952 */
6953 static void
6954f_items(typval_T *argvars, typval_T *rettv)
6955{
6956 dict_list(argvars, rettv, 2);
6957}
6958
6959#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6960/*
6961 * Get the job from the argument.
6962 * Returns NULL if the job is invalid.
6963 */
6964 static job_T *
6965get_job_arg(typval_T *tv)
6966{
6967 job_T *job;
6968
6969 if (tv->v_type != VAR_JOB)
6970 {
6971 EMSG2(_(e_invarg2), get_tv_string(tv));
6972 return NULL;
6973 }
6974 job = tv->vval.v_job;
6975
6976 if (job == NULL)
6977 EMSG(_("E916: not a valid job"));
6978 return job;
6979}
6980
6981/*
6982 * "job_getchannel()" function
6983 */
6984 static void
6985f_job_getchannel(typval_T *argvars, typval_T *rettv)
6986{
6987 job_T *job = get_job_arg(&argvars[0]);
6988
6989 if (job != NULL)
6990 {
6991 rettv->v_type = VAR_CHANNEL;
6992 rettv->vval.v_channel = job->jv_channel;
6993 if (job->jv_channel != NULL)
6994 ++job->jv_channel->ch_refcount;
6995 }
6996}
6997
6998/*
6999 * "job_info()" function
7000 */
7001 static void
7002f_job_info(typval_T *argvars, typval_T *rettv)
7003{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007004 if (argvars[0].v_type != VAR_UNKNOWN)
7005 {
7006 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007007
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007008 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7009 job_info(job, rettv->vval.v_dict);
7010 }
7011 else if (rettv_list_alloc(rettv) == OK)
7012 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007013}
7014
7015/*
7016 * "job_setoptions()" function
7017 */
7018 static void
7019f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7020{
7021 job_T *job = get_job_arg(&argvars[0]);
7022 jobopt_T opt;
7023
7024 if (job == NULL)
7025 return;
7026 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007027 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028 job_set_options(job, &opt);
7029 free_job_options(&opt);
7030}
7031
7032/*
7033 * "job_start()" function
7034 */
7035 static void
7036f_job_start(typval_T *argvars, typval_T *rettv)
7037{
7038 rettv->v_type = VAR_JOB;
7039 if (check_restricted() || check_secure())
7040 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01007041 rettv->vval.v_job = job_start(argvars, NULL, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007042}
7043
7044/*
7045 * "job_status()" function
7046 */
7047 static void
7048f_job_status(typval_T *argvars, typval_T *rettv)
7049{
7050 job_T *job = get_job_arg(&argvars[0]);
7051
7052 if (job != NULL)
7053 {
7054 rettv->v_type = VAR_STRING;
7055 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7056 }
7057}
7058
7059/*
7060 * "job_stop()" function
7061 */
7062 static void
7063f_job_stop(typval_T *argvars, typval_T *rettv)
7064{
7065 job_T *job = get_job_arg(&argvars[0]);
7066
7067 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007068 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007069}
7070#endif
7071
7072/*
7073 * "join()" function
7074 */
7075 static void
7076f_join(typval_T *argvars, typval_T *rettv)
7077{
7078 garray_T ga;
7079 char_u *sep;
7080
7081 if (argvars[0].v_type != VAR_LIST)
7082 {
7083 EMSG(_(e_listreq));
7084 return;
7085 }
7086 if (argvars[0].vval.v_list == NULL)
7087 return;
7088 if (argvars[1].v_type == VAR_UNKNOWN)
7089 sep = (char_u *)" ";
7090 else
7091 sep = get_tv_string_chk(&argvars[1]);
7092
7093 rettv->v_type = VAR_STRING;
7094
7095 if (sep != NULL)
7096 {
7097 ga_init2(&ga, (int)sizeof(char), 80);
7098 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7099 ga_append(&ga, NUL);
7100 rettv->vval.v_string = (char_u *)ga.ga_data;
7101 }
7102 else
7103 rettv->vval.v_string = NULL;
7104}
7105
7106/*
7107 * "js_decode()" function
7108 */
7109 static void
7110f_js_decode(typval_T *argvars, typval_T *rettv)
7111{
7112 js_read_T reader;
7113
7114 reader.js_buf = get_tv_string(&argvars[0]);
7115 reader.js_fill = NULL;
7116 reader.js_used = 0;
7117 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7118 EMSG(_(e_invarg));
7119}
7120
7121/*
7122 * "js_encode()" function
7123 */
7124 static void
7125f_js_encode(typval_T *argvars, typval_T *rettv)
7126{
7127 rettv->v_type = VAR_STRING;
7128 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7129}
7130
7131/*
7132 * "json_decode()" function
7133 */
7134 static void
7135f_json_decode(typval_T *argvars, typval_T *rettv)
7136{
7137 js_read_T reader;
7138
7139 reader.js_buf = get_tv_string(&argvars[0]);
7140 reader.js_fill = NULL;
7141 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007142 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007143}
7144
7145/*
7146 * "json_encode()" function
7147 */
7148 static void
7149f_json_encode(typval_T *argvars, typval_T *rettv)
7150{
7151 rettv->v_type = VAR_STRING;
7152 rettv->vval.v_string = json_encode(&argvars[0], 0);
7153}
7154
7155/*
7156 * "keys()" function
7157 */
7158 static void
7159f_keys(typval_T *argvars, typval_T *rettv)
7160{
7161 dict_list(argvars, rettv, 0);
7162}
7163
7164/*
7165 * "last_buffer_nr()" function.
7166 */
7167 static void
7168f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7169{
7170 int n = 0;
7171 buf_T *buf;
7172
Bram Moolenaar29323592016-07-24 22:04:11 +02007173 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007174 if (n < buf->b_fnum)
7175 n = buf->b_fnum;
7176
7177 rettv->vval.v_number = n;
7178}
7179
7180/*
7181 * "len()" function
7182 */
7183 static void
7184f_len(typval_T *argvars, typval_T *rettv)
7185{
7186 switch (argvars[0].v_type)
7187 {
7188 case VAR_STRING:
7189 case VAR_NUMBER:
7190 rettv->vval.v_number = (varnumber_T)STRLEN(
7191 get_tv_string(&argvars[0]));
7192 break;
7193 case VAR_LIST:
7194 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7195 break;
7196 case VAR_DICT:
7197 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7198 break;
7199 case VAR_UNKNOWN:
7200 case VAR_SPECIAL:
7201 case VAR_FLOAT:
7202 case VAR_FUNC:
7203 case VAR_PARTIAL:
7204 case VAR_JOB:
7205 case VAR_CHANNEL:
7206 EMSG(_("E701: Invalid type for len()"));
7207 break;
7208 }
7209}
7210
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007211 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007212libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007213{
7214#ifdef FEAT_LIBCALL
7215 char_u *string_in;
7216 char_u **string_result;
7217 int nr_result;
7218#endif
7219
7220 rettv->v_type = type;
7221 if (type != VAR_NUMBER)
7222 rettv->vval.v_string = NULL;
7223
7224 if (check_restricted() || check_secure())
7225 return;
7226
7227#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007228 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007229 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7230 {
7231 string_in = NULL;
7232 if (argvars[2].v_type == VAR_STRING)
7233 string_in = argvars[2].vval.v_string;
7234 if (type == VAR_NUMBER)
7235 string_result = NULL;
7236 else
7237 string_result = &rettv->vval.v_string;
7238 if (mch_libcall(argvars[0].vval.v_string,
7239 argvars[1].vval.v_string,
7240 string_in,
7241 argvars[2].vval.v_number,
7242 string_result,
7243 &nr_result) == OK
7244 && type == VAR_NUMBER)
7245 rettv->vval.v_number = nr_result;
7246 }
7247#endif
7248}
7249
7250/*
7251 * "libcall()" function
7252 */
7253 static void
7254f_libcall(typval_T *argvars, typval_T *rettv)
7255{
7256 libcall_common(argvars, rettv, VAR_STRING);
7257}
7258
7259/*
7260 * "libcallnr()" function
7261 */
7262 static void
7263f_libcallnr(typval_T *argvars, typval_T *rettv)
7264{
7265 libcall_common(argvars, rettv, VAR_NUMBER);
7266}
7267
7268/*
7269 * "line(string)" function
7270 */
7271 static void
7272f_line(typval_T *argvars, typval_T *rettv)
7273{
7274 linenr_T lnum = 0;
7275 pos_T *fp;
7276 int fnum;
7277
7278 fp = var2fpos(&argvars[0], TRUE, &fnum);
7279 if (fp != NULL)
7280 lnum = fp->lnum;
7281 rettv->vval.v_number = lnum;
7282}
7283
7284/*
7285 * "line2byte(lnum)" function
7286 */
7287 static void
7288f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7289{
7290#ifndef FEAT_BYTEOFF
7291 rettv->vval.v_number = -1;
7292#else
7293 linenr_T lnum;
7294
7295 lnum = get_tv_lnum(argvars);
7296 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7297 rettv->vval.v_number = -1;
7298 else
7299 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7300 if (rettv->vval.v_number >= 0)
7301 ++rettv->vval.v_number;
7302#endif
7303}
7304
7305/*
7306 * "lispindent(lnum)" function
7307 */
7308 static void
7309f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7310{
7311#ifdef FEAT_LISP
7312 pos_T pos;
7313 linenr_T lnum;
7314
7315 pos = curwin->w_cursor;
7316 lnum = get_tv_lnum(argvars);
7317 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7318 {
7319 curwin->w_cursor.lnum = lnum;
7320 rettv->vval.v_number = get_lisp_indent();
7321 curwin->w_cursor = pos;
7322 }
7323 else
7324#endif
7325 rettv->vval.v_number = -1;
7326}
7327
7328/*
7329 * "localtime()" function
7330 */
7331 static void
7332f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7333{
7334 rettv->vval.v_number = (varnumber_T)time(NULL);
7335}
7336
7337static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7338
7339 static void
7340get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7341{
7342 char_u *keys;
7343 char_u *which;
7344 char_u buf[NUMBUFLEN];
7345 char_u *keys_buf = NULL;
7346 char_u *rhs;
7347 int mode;
7348 int abbr = FALSE;
7349 int get_dict = FALSE;
7350 mapblock_T *mp;
7351 int buffer_local;
7352
7353 /* return empty string for failure */
7354 rettv->v_type = VAR_STRING;
7355 rettv->vval.v_string = NULL;
7356
7357 keys = get_tv_string(&argvars[0]);
7358 if (*keys == NUL)
7359 return;
7360
7361 if (argvars[1].v_type != VAR_UNKNOWN)
7362 {
7363 which = get_tv_string_buf_chk(&argvars[1], buf);
7364 if (argvars[2].v_type != VAR_UNKNOWN)
7365 {
7366 abbr = (int)get_tv_number(&argvars[2]);
7367 if (argvars[3].v_type != VAR_UNKNOWN)
7368 get_dict = (int)get_tv_number(&argvars[3]);
7369 }
7370 }
7371 else
7372 which = (char_u *)"";
7373 if (which == NULL)
7374 return;
7375
7376 mode = get_map_mode(&which, 0);
7377
7378 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7379 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7380 vim_free(keys_buf);
7381
7382 if (!get_dict)
7383 {
7384 /* Return a string. */
7385 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007386 {
7387 if (*rhs == NUL)
7388 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7389 else
7390 rettv->vval.v_string = str2special_save(rhs, FALSE);
7391 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007392
7393 }
7394 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7395 {
7396 /* Return a dictionary. */
7397 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7398 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7399 dict_T *dict = rettv->vval.v_dict;
7400
7401 dict_add_nr_str(dict, "lhs", 0L, lhs);
7402 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7403 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7404 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7405 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7406 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7407 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7408 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7409 dict_add_nr_str(dict, "mode", 0L, mapmode);
7410
7411 vim_free(lhs);
7412 vim_free(mapmode);
7413 }
7414}
7415
7416#ifdef FEAT_FLOAT
7417/*
7418 * "log()" function
7419 */
7420 static void
7421f_log(typval_T *argvars, typval_T *rettv)
7422{
7423 float_T f = 0.0;
7424
7425 rettv->v_type = VAR_FLOAT;
7426 if (get_float_arg(argvars, &f) == OK)
7427 rettv->vval.v_float = log(f);
7428 else
7429 rettv->vval.v_float = 0.0;
7430}
7431
7432/*
7433 * "log10()" function
7434 */
7435 static void
7436f_log10(typval_T *argvars, typval_T *rettv)
7437{
7438 float_T f = 0.0;
7439
7440 rettv->v_type = VAR_FLOAT;
7441 if (get_float_arg(argvars, &f) == OK)
7442 rettv->vval.v_float = log10(f);
7443 else
7444 rettv->vval.v_float = 0.0;
7445}
7446#endif
7447
7448#ifdef FEAT_LUA
7449/*
7450 * "luaeval()" function
7451 */
7452 static void
7453f_luaeval(typval_T *argvars, typval_T *rettv)
7454{
7455 char_u *str;
7456 char_u buf[NUMBUFLEN];
7457
7458 str = get_tv_string_buf(&argvars[0], buf);
7459 do_luaeval(str, argvars + 1, rettv);
7460}
7461#endif
7462
7463/*
7464 * "map()" function
7465 */
7466 static void
7467f_map(typval_T *argvars, typval_T *rettv)
7468{
7469 filter_map(argvars, rettv, TRUE);
7470}
7471
7472/*
7473 * "maparg()" function
7474 */
7475 static void
7476f_maparg(typval_T *argvars, typval_T *rettv)
7477{
7478 get_maparg(argvars, rettv, TRUE);
7479}
7480
7481/*
7482 * "mapcheck()" function
7483 */
7484 static void
7485f_mapcheck(typval_T *argvars, typval_T *rettv)
7486{
7487 get_maparg(argvars, rettv, FALSE);
7488}
7489
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007490typedef enum
7491{
7492 MATCH_END, /* matchend() */
7493 MATCH_MATCH, /* match() */
7494 MATCH_STR, /* matchstr() */
7495 MATCH_LIST, /* matchlist() */
7496 MATCH_POS /* matchstrpos() */
7497} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007498
7499 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007500find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007501{
7502 char_u *str = NULL;
7503 long len = 0;
7504 char_u *expr = NULL;
7505 char_u *pat;
7506 regmatch_T regmatch;
7507 char_u patbuf[NUMBUFLEN];
7508 char_u strbuf[NUMBUFLEN];
7509 char_u *save_cpo;
7510 long start = 0;
7511 long nth = 1;
7512 colnr_T startcol = 0;
7513 int match = 0;
7514 list_T *l = NULL;
7515 listitem_T *li = NULL;
7516 long idx = 0;
7517 char_u *tofree = NULL;
7518
7519 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7520 save_cpo = p_cpo;
7521 p_cpo = (char_u *)"";
7522
7523 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007524 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007525 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007526 /* type MATCH_LIST: return empty list when there are no matches.
7527 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007528 if (rettv_list_alloc(rettv) == FAIL)
7529 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007530 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007531 && (list_append_string(rettv->vval.v_list,
7532 (char_u *)"", 0) == FAIL
7533 || list_append_number(rettv->vval.v_list,
7534 (varnumber_T)-1) == FAIL
7535 || list_append_number(rettv->vval.v_list,
7536 (varnumber_T)-1) == FAIL
7537 || list_append_number(rettv->vval.v_list,
7538 (varnumber_T)-1) == FAIL))
7539 {
7540 list_free(rettv->vval.v_list);
7541 rettv->vval.v_list = NULL;
7542 goto theend;
7543 }
7544 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007545 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007546 {
7547 rettv->v_type = VAR_STRING;
7548 rettv->vval.v_string = NULL;
7549 }
7550
7551 if (argvars[0].v_type == VAR_LIST)
7552 {
7553 if ((l = argvars[0].vval.v_list) == NULL)
7554 goto theend;
7555 li = l->lv_first;
7556 }
7557 else
7558 {
7559 expr = str = get_tv_string(&argvars[0]);
7560 len = (long)STRLEN(str);
7561 }
7562
7563 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7564 if (pat == NULL)
7565 goto theend;
7566
7567 if (argvars[2].v_type != VAR_UNKNOWN)
7568 {
7569 int error = FALSE;
7570
7571 start = (long)get_tv_number_chk(&argvars[2], &error);
7572 if (error)
7573 goto theend;
7574 if (l != NULL)
7575 {
7576 li = list_find(l, start);
7577 if (li == NULL)
7578 goto theend;
7579 idx = l->lv_idx; /* use the cached index */
7580 }
7581 else
7582 {
7583 if (start < 0)
7584 start = 0;
7585 if (start > len)
7586 goto theend;
7587 /* When "count" argument is there ignore matches before "start",
7588 * otherwise skip part of the string. Differs when pattern is "^"
7589 * or "\<". */
7590 if (argvars[3].v_type != VAR_UNKNOWN)
7591 startcol = start;
7592 else
7593 {
7594 str += start;
7595 len -= start;
7596 }
7597 }
7598
7599 if (argvars[3].v_type != VAR_UNKNOWN)
7600 nth = (long)get_tv_number_chk(&argvars[3], &error);
7601 if (error)
7602 goto theend;
7603 }
7604
7605 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7606 if (regmatch.regprog != NULL)
7607 {
7608 regmatch.rm_ic = p_ic;
7609
7610 for (;;)
7611 {
7612 if (l != NULL)
7613 {
7614 if (li == NULL)
7615 {
7616 match = FALSE;
7617 break;
7618 }
7619 vim_free(tofree);
7620 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7621 if (str == NULL)
7622 break;
7623 }
7624
7625 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7626
7627 if (match && --nth <= 0)
7628 break;
7629 if (l == NULL && !match)
7630 break;
7631
7632 /* Advance to just after the match. */
7633 if (l != NULL)
7634 {
7635 li = li->li_next;
7636 ++idx;
7637 }
7638 else
7639 {
7640#ifdef FEAT_MBYTE
7641 startcol = (colnr_T)(regmatch.startp[0]
7642 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7643#else
7644 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7645#endif
7646 if (startcol > (colnr_T)len
7647 || str + startcol <= regmatch.startp[0])
7648 {
7649 match = FALSE;
7650 break;
7651 }
7652 }
7653 }
7654
7655 if (match)
7656 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007657 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007658 {
7659 listitem_T *li1 = rettv->vval.v_list->lv_first;
7660 listitem_T *li2 = li1->li_next;
7661 listitem_T *li3 = li2->li_next;
7662 listitem_T *li4 = li3->li_next;
7663
7664 vim_free(li1->li_tv.vval.v_string);
7665 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7666 (int)(regmatch.endp[0] - regmatch.startp[0]));
7667 li3->li_tv.vval.v_number =
7668 (varnumber_T)(regmatch.startp[0] - expr);
7669 li4->li_tv.vval.v_number =
7670 (varnumber_T)(regmatch.endp[0] - expr);
7671 if (l != NULL)
7672 li2->li_tv.vval.v_number = (varnumber_T)idx;
7673 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007674 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007675 {
7676 int i;
7677
7678 /* return list with matched string and submatches */
7679 for (i = 0; i < NSUBEXP; ++i)
7680 {
7681 if (regmatch.endp[i] == NULL)
7682 {
7683 if (list_append_string(rettv->vval.v_list,
7684 (char_u *)"", 0) == FAIL)
7685 break;
7686 }
7687 else if (list_append_string(rettv->vval.v_list,
7688 regmatch.startp[i],
7689 (int)(regmatch.endp[i] - regmatch.startp[i]))
7690 == FAIL)
7691 break;
7692 }
7693 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007694 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007695 {
7696 /* return matched string */
7697 if (l != NULL)
7698 copy_tv(&li->li_tv, rettv);
7699 else
7700 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7701 (int)(regmatch.endp[0] - regmatch.startp[0]));
7702 }
7703 else if (l != NULL)
7704 rettv->vval.v_number = idx;
7705 else
7706 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007707 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007708 rettv->vval.v_number =
7709 (varnumber_T)(regmatch.startp[0] - str);
7710 else
7711 rettv->vval.v_number =
7712 (varnumber_T)(regmatch.endp[0] - str);
7713 rettv->vval.v_number += (varnumber_T)(str - expr);
7714 }
7715 }
7716 vim_regfree(regmatch.regprog);
7717 }
7718
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007719theend:
7720 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007721 /* matchstrpos() without a list: drop the second item. */
7722 listitem_remove(rettv->vval.v_list,
7723 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007724 vim_free(tofree);
7725 p_cpo = save_cpo;
7726}
7727
7728/*
7729 * "match()" function
7730 */
7731 static void
7732f_match(typval_T *argvars, typval_T *rettv)
7733{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007734 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007735}
7736
7737/*
7738 * "matchadd()" function
7739 */
7740 static void
7741f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7742{
7743#ifdef FEAT_SEARCH_EXTRA
7744 char_u buf[NUMBUFLEN];
7745 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7746 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7747 int prio = 10; /* default priority */
7748 int id = -1;
7749 int error = FALSE;
7750 char_u *conceal_char = NULL;
7751
7752 rettv->vval.v_number = -1;
7753
7754 if (grp == NULL || pat == NULL)
7755 return;
7756 if (argvars[2].v_type != VAR_UNKNOWN)
7757 {
7758 prio = (int)get_tv_number_chk(&argvars[2], &error);
7759 if (argvars[3].v_type != VAR_UNKNOWN)
7760 {
7761 id = (int)get_tv_number_chk(&argvars[3], &error);
7762 if (argvars[4].v_type != VAR_UNKNOWN)
7763 {
7764 if (argvars[4].v_type != VAR_DICT)
7765 {
7766 EMSG(_(e_dictreq));
7767 return;
7768 }
7769 if (dict_find(argvars[4].vval.v_dict,
7770 (char_u *)"conceal", -1) != NULL)
7771 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7772 (char_u *)"conceal", FALSE);
7773 }
7774 }
7775 }
7776 if (error == TRUE)
7777 return;
7778 if (id >= 1 && id <= 3)
7779 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007780 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007781 return;
7782 }
7783
7784 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7785 conceal_char);
7786#endif
7787}
7788
7789/*
7790 * "matchaddpos()" function
7791 */
7792 static void
7793f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7794{
7795#ifdef FEAT_SEARCH_EXTRA
7796 char_u buf[NUMBUFLEN];
7797 char_u *group;
7798 int prio = 10;
7799 int id = -1;
7800 int error = FALSE;
7801 list_T *l;
7802 char_u *conceal_char = NULL;
7803
7804 rettv->vval.v_number = -1;
7805
7806 group = get_tv_string_buf_chk(&argvars[0], buf);
7807 if (group == NULL)
7808 return;
7809
7810 if (argvars[1].v_type != VAR_LIST)
7811 {
7812 EMSG2(_(e_listarg), "matchaddpos()");
7813 return;
7814 }
7815 l = argvars[1].vval.v_list;
7816 if (l == NULL)
7817 return;
7818
7819 if (argvars[2].v_type != VAR_UNKNOWN)
7820 {
7821 prio = (int)get_tv_number_chk(&argvars[2], &error);
7822 if (argvars[3].v_type != VAR_UNKNOWN)
7823 {
7824 id = (int)get_tv_number_chk(&argvars[3], &error);
7825 if (argvars[4].v_type != VAR_UNKNOWN)
7826 {
7827 if (argvars[4].v_type != VAR_DICT)
7828 {
7829 EMSG(_(e_dictreq));
7830 return;
7831 }
7832 if (dict_find(argvars[4].vval.v_dict,
7833 (char_u *)"conceal", -1) != NULL)
7834 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7835 (char_u *)"conceal", FALSE);
7836 }
7837 }
7838 }
7839 if (error == TRUE)
7840 return;
7841
7842 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7843 if (id == 1 || id == 2)
7844 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007845 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007846 return;
7847 }
7848
7849 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7850 conceal_char);
7851#endif
7852}
7853
7854/*
7855 * "matcharg()" function
7856 */
7857 static void
7858f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7859{
7860 if (rettv_list_alloc(rettv) == OK)
7861 {
7862#ifdef FEAT_SEARCH_EXTRA
7863 int id = (int)get_tv_number(&argvars[0]);
7864 matchitem_T *m;
7865
7866 if (id >= 1 && id <= 3)
7867 {
7868 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7869 {
7870 list_append_string(rettv->vval.v_list,
7871 syn_id2name(m->hlg_id), -1);
7872 list_append_string(rettv->vval.v_list, m->pattern, -1);
7873 }
7874 else
7875 {
7876 list_append_string(rettv->vval.v_list, NULL, -1);
7877 list_append_string(rettv->vval.v_list, NULL, -1);
7878 }
7879 }
7880#endif
7881 }
7882}
7883
7884/*
7885 * "matchdelete()" function
7886 */
7887 static void
7888f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7889{
7890#ifdef FEAT_SEARCH_EXTRA
7891 rettv->vval.v_number = match_delete(curwin,
7892 (int)get_tv_number(&argvars[0]), TRUE);
7893#endif
7894}
7895
7896/*
7897 * "matchend()" function
7898 */
7899 static void
7900f_matchend(typval_T *argvars, typval_T *rettv)
7901{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007902 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007903}
7904
7905/*
7906 * "matchlist()" function
7907 */
7908 static void
7909f_matchlist(typval_T *argvars, typval_T *rettv)
7910{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007911 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007912}
7913
7914/*
7915 * "matchstr()" function
7916 */
7917 static void
7918f_matchstr(typval_T *argvars, typval_T *rettv)
7919{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007920 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007921}
7922
7923/*
7924 * "matchstrpos()" function
7925 */
7926 static void
7927f_matchstrpos(typval_T *argvars, typval_T *rettv)
7928{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007929 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007930}
7931
7932static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7933
7934 static void
7935max_min(typval_T *argvars, typval_T *rettv, int domax)
7936{
7937 varnumber_T n = 0;
7938 varnumber_T i;
7939 int error = FALSE;
7940
7941 if (argvars[0].v_type == VAR_LIST)
7942 {
7943 list_T *l;
7944 listitem_T *li;
7945
7946 l = argvars[0].vval.v_list;
7947 if (l != NULL)
7948 {
7949 li = l->lv_first;
7950 if (li != NULL)
7951 {
7952 n = get_tv_number_chk(&li->li_tv, &error);
7953 for (;;)
7954 {
7955 li = li->li_next;
7956 if (li == NULL)
7957 break;
7958 i = get_tv_number_chk(&li->li_tv, &error);
7959 if (domax ? i > n : i < n)
7960 n = i;
7961 }
7962 }
7963 }
7964 }
7965 else if (argvars[0].v_type == VAR_DICT)
7966 {
7967 dict_T *d;
7968 int first = TRUE;
7969 hashitem_T *hi;
7970 int todo;
7971
7972 d = argvars[0].vval.v_dict;
7973 if (d != NULL)
7974 {
7975 todo = (int)d->dv_hashtab.ht_used;
7976 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7977 {
7978 if (!HASHITEM_EMPTY(hi))
7979 {
7980 --todo;
7981 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7982 if (first)
7983 {
7984 n = i;
7985 first = FALSE;
7986 }
7987 else if (domax ? i > n : i < n)
7988 n = i;
7989 }
7990 }
7991 }
7992 }
7993 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007994 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007995 rettv->vval.v_number = error ? 0 : n;
7996}
7997
7998/*
7999 * "max()" function
8000 */
8001 static void
8002f_max(typval_T *argvars, typval_T *rettv)
8003{
8004 max_min(argvars, rettv, TRUE);
8005}
8006
8007/*
8008 * "min()" function
8009 */
8010 static void
8011f_min(typval_T *argvars, typval_T *rettv)
8012{
8013 max_min(argvars, rettv, FALSE);
8014}
8015
8016static int mkdir_recurse(char_u *dir, int prot);
8017
8018/*
8019 * Create the directory in which "dir" is located, and higher levels when
8020 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008021 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008022 */
8023 static int
8024mkdir_recurse(char_u *dir, int prot)
8025{
8026 char_u *p;
8027 char_u *updir;
8028 int r = FAIL;
8029
8030 /* Get end of directory name in "dir".
8031 * We're done when it's "/" or "c:/". */
8032 p = gettail_sep(dir);
8033 if (p <= get_past_head(dir))
8034 return OK;
8035
8036 /* If the directory exists we're done. Otherwise: create it.*/
8037 updir = vim_strnsave(dir, (int)(p - dir));
8038 if (updir == NULL)
8039 return FAIL;
8040 if (mch_isdir(updir))
8041 r = OK;
8042 else if (mkdir_recurse(updir, prot) == OK)
8043 r = vim_mkdir_emsg(updir, prot);
8044 vim_free(updir);
8045 return r;
8046}
8047
8048#ifdef vim_mkdir
8049/*
8050 * "mkdir()" function
8051 */
8052 static void
8053f_mkdir(typval_T *argvars, typval_T *rettv)
8054{
8055 char_u *dir;
8056 char_u buf[NUMBUFLEN];
8057 int prot = 0755;
8058
8059 rettv->vval.v_number = FAIL;
8060 if (check_restricted() || check_secure())
8061 return;
8062
8063 dir = get_tv_string_buf(&argvars[0], buf);
8064 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008065 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008066
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008067 if (*gettail(dir) == NUL)
8068 /* remove trailing slashes */
8069 *gettail_sep(dir) = NUL;
8070
8071 if (argvars[1].v_type != VAR_UNKNOWN)
8072 {
8073 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008074 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008075 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8076 if (prot == -1)
8077 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008078 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008079 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8080 {
8081 if (mch_isdir(dir))
8082 {
8083 /* With the "p" flag it's OK if the dir already exists. */
8084 rettv->vval.v_number = OK;
8085 return;
8086 }
8087 mkdir_recurse(dir, prot);
8088 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008089 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008090 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008091}
8092#endif
8093
8094/*
8095 * "mode()" function
8096 */
8097 static void
8098f_mode(typval_T *argvars, typval_T *rettv)
8099{
8100 char_u buf[3];
8101
8102 buf[1] = NUL;
8103 buf[2] = NUL;
8104
8105 if (time_for_testing == 93784)
8106 {
8107 /* Testing the two-character code. */
8108 buf[0] = 'x';
8109 buf[1] = '!';
8110 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008111#ifdef FEAT_TERMINAL
8112 else if (term_use_loop())
8113 buf[0] = 't';
8114#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008115 else if (VIsual_active)
8116 {
8117 if (VIsual_select)
8118 buf[0] = VIsual_mode + 's' - 'v';
8119 else
8120 buf[0] = VIsual_mode;
8121 }
8122 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8123 || State == CONFIRM)
8124 {
8125 buf[0] = 'r';
8126 if (State == ASKMORE)
8127 buf[1] = 'm';
8128 else if (State == CONFIRM)
8129 buf[1] = '?';
8130 }
8131 else if (State == EXTERNCMD)
8132 buf[0] = '!';
8133 else if (State & INSERT)
8134 {
8135#ifdef FEAT_VREPLACE
8136 if (State & VREPLACE_FLAG)
8137 {
8138 buf[0] = 'R';
8139 buf[1] = 'v';
8140 }
8141 else
8142#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008143 {
8144 if (State & REPLACE_FLAG)
8145 buf[0] = 'R';
8146 else
8147 buf[0] = 'i';
8148#ifdef FEAT_INS_EXPAND
8149 if (ins_compl_active())
8150 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008151 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008152 buf[1] = 'x';
8153#endif
8154 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008155 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008156 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008157 {
8158 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008159 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008160 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008161 else if (exmode_active == EXMODE_NORMAL)
8162 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008163 }
8164 else
8165 {
8166 buf[0] = 'n';
8167 if (finish_op)
8168 buf[1] = 'o';
8169 }
8170
8171 /* Clear out the minor mode when the argument is not a non-zero number or
8172 * non-empty string. */
8173 if (!non_zero_arg(&argvars[0]))
8174 buf[1] = NUL;
8175
8176 rettv->vval.v_string = vim_strsave(buf);
8177 rettv->v_type = VAR_STRING;
8178}
8179
8180#if defined(FEAT_MZSCHEME) || defined(PROTO)
8181/*
8182 * "mzeval()" function
8183 */
8184 static void
8185f_mzeval(typval_T *argvars, typval_T *rettv)
8186{
8187 char_u *str;
8188 char_u buf[NUMBUFLEN];
8189
8190 str = get_tv_string_buf(&argvars[0], buf);
8191 do_mzeval(str, rettv);
8192}
8193
8194 void
8195mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8196{
8197 typval_T argvars[3];
8198
8199 argvars[0].v_type = VAR_STRING;
8200 argvars[0].vval.v_string = name;
8201 copy_tv(args, &argvars[1]);
8202 argvars[2].v_type = VAR_UNKNOWN;
8203 f_call(argvars, rettv);
8204 clear_tv(&argvars[1]);
8205}
8206#endif
8207
8208/*
8209 * "nextnonblank()" function
8210 */
8211 static void
8212f_nextnonblank(typval_T *argvars, typval_T *rettv)
8213{
8214 linenr_T lnum;
8215
8216 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8217 {
8218 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8219 {
8220 lnum = 0;
8221 break;
8222 }
8223 if (*skipwhite(ml_get(lnum)) != NUL)
8224 break;
8225 }
8226 rettv->vval.v_number = lnum;
8227}
8228
8229/*
8230 * "nr2char()" function
8231 */
8232 static void
8233f_nr2char(typval_T *argvars, typval_T *rettv)
8234{
8235 char_u buf[NUMBUFLEN];
8236
8237#ifdef FEAT_MBYTE
8238 if (has_mbyte)
8239 {
8240 int utf8 = 0;
8241
8242 if (argvars[1].v_type != VAR_UNKNOWN)
8243 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8244 if (utf8)
8245 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8246 else
8247 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8248 }
8249 else
8250#endif
8251 {
8252 buf[0] = (char_u)get_tv_number(&argvars[0]);
8253 buf[1] = NUL;
8254 }
8255 rettv->v_type = VAR_STRING;
8256 rettv->vval.v_string = vim_strsave(buf);
8257}
8258
8259/*
8260 * "or(expr, expr)" function
8261 */
8262 static void
8263f_or(typval_T *argvars, typval_T *rettv)
8264{
8265 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8266 | get_tv_number_chk(&argvars[1], NULL);
8267}
8268
8269/*
8270 * "pathshorten()" function
8271 */
8272 static void
8273f_pathshorten(typval_T *argvars, typval_T *rettv)
8274{
8275 char_u *p;
8276
8277 rettv->v_type = VAR_STRING;
8278 p = get_tv_string_chk(&argvars[0]);
8279 if (p == NULL)
8280 rettv->vval.v_string = NULL;
8281 else
8282 {
8283 p = vim_strsave(p);
8284 rettv->vval.v_string = p;
8285 if (p != NULL)
8286 shorten_dir(p);
8287 }
8288}
8289
8290#ifdef FEAT_PERL
8291/*
8292 * "perleval()" function
8293 */
8294 static void
8295f_perleval(typval_T *argvars, typval_T *rettv)
8296{
8297 char_u *str;
8298 char_u buf[NUMBUFLEN];
8299
8300 str = get_tv_string_buf(&argvars[0], buf);
8301 do_perleval(str, rettv);
8302}
8303#endif
8304
8305#ifdef FEAT_FLOAT
8306/*
8307 * "pow()" function
8308 */
8309 static void
8310f_pow(typval_T *argvars, typval_T *rettv)
8311{
8312 float_T fx = 0.0, fy = 0.0;
8313
8314 rettv->v_type = VAR_FLOAT;
8315 if (get_float_arg(argvars, &fx) == OK
8316 && get_float_arg(&argvars[1], &fy) == OK)
8317 rettv->vval.v_float = pow(fx, fy);
8318 else
8319 rettv->vval.v_float = 0.0;
8320}
8321#endif
8322
8323/*
8324 * "prevnonblank()" function
8325 */
8326 static void
8327f_prevnonblank(typval_T *argvars, typval_T *rettv)
8328{
8329 linenr_T lnum;
8330
8331 lnum = get_tv_lnum(argvars);
8332 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8333 lnum = 0;
8334 else
8335 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8336 --lnum;
8337 rettv->vval.v_number = lnum;
8338}
8339
8340/* This dummy va_list is here because:
8341 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8342 * - locally in the function results in a "used before set" warning
8343 * - using va_start() to initialize it gives "function with fixed args" error */
8344static va_list ap;
8345
8346/*
8347 * "printf()" function
8348 */
8349 static void
8350f_printf(typval_T *argvars, typval_T *rettv)
8351{
8352 char_u buf[NUMBUFLEN];
8353 int len;
8354 char_u *s;
8355 int saved_did_emsg = did_emsg;
8356 char *fmt;
8357
8358 rettv->v_type = VAR_STRING;
8359 rettv->vval.v_string = NULL;
8360
8361 /* Get the required length, allocate the buffer and do it for real. */
8362 did_emsg = FALSE;
8363 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008364 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008365 if (!did_emsg)
8366 {
8367 s = alloc(len + 1);
8368 if (s != NULL)
8369 {
8370 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008371 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8372 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008373 }
8374 }
8375 did_emsg |= saved_did_emsg;
8376}
8377
8378/*
8379 * "pumvisible()" function
8380 */
8381 static void
8382f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8383{
8384#ifdef FEAT_INS_EXPAND
8385 if (pum_visible())
8386 rettv->vval.v_number = 1;
8387#endif
8388}
8389
8390#ifdef FEAT_PYTHON3
8391/*
8392 * "py3eval()" function
8393 */
8394 static void
8395f_py3eval(typval_T *argvars, typval_T *rettv)
8396{
8397 char_u *str;
8398 char_u buf[NUMBUFLEN];
8399
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008400 if (p_pyx == 0)
8401 p_pyx = 3;
8402
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008403 str = get_tv_string_buf(&argvars[0], buf);
8404 do_py3eval(str, rettv);
8405}
8406#endif
8407
8408#ifdef FEAT_PYTHON
8409/*
8410 * "pyeval()" function
8411 */
8412 static void
8413f_pyeval(typval_T *argvars, typval_T *rettv)
8414{
8415 char_u *str;
8416 char_u buf[NUMBUFLEN];
8417
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008418 if (p_pyx == 0)
8419 p_pyx = 2;
8420
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008421 str = get_tv_string_buf(&argvars[0], buf);
8422 do_pyeval(str, rettv);
8423}
8424#endif
8425
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008426#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8427/*
8428 * "pyxeval()" function
8429 */
8430 static void
8431f_pyxeval(typval_T *argvars, typval_T *rettv)
8432{
8433# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8434 init_pyxversion();
8435 if (p_pyx == 2)
8436 f_pyeval(argvars, rettv);
8437 else
8438 f_py3eval(argvars, rettv);
8439# elif defined(FEAT_PYTHON)
8440 f_pyeval(argvars, rettv);
8441# elif defined(FEAT_PYTHON3)
8442 f_py3eval(argvars, rettv);
8443# endif
8444}
8445#endif
8446
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008447/*
8448 * "range()" function
8449 */
8450 static void
8451f_range(typval_T *argvars, typval_T *rettv)
8452{
8453 varnumber_T start;
8454 varnumber_T end;
8455 varnumber_T stride = 1;
8456 varnumber_T i;
8457 int error = FALSE;
8458
8459 start = get_tv_number_chk(&argvars[0], &error);
8460 if (argvars[1].v_type == VAR_UNKNOWN)
8461 {
8462 end = start - 1;
8463 start = 0;
8464 }
8465 else
8466 {
8467 end = get_tv_number_chk(&argvars[1], &error);
8468 if (argvars[2].v_type != VAR_UNKNOWN)
8469 stride = get_tv_number_chk(&argvars[2], &error);
8470 }
8471
8472 if (error)
8473 return; /* type error; errmsg already given */
8474 if (stride == 0)
8475 EMSG(_("E726: Stride is zero"));
8476 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8477 EMSG(_("E727: Start past end"));
8478 else
8479 {
8480 if (rettv_list_alloc(rettv) == OK)
8481 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8482 if (list_append_number(rettv->vval.v_list,
8483 (varnumber_T)i) == FAIL)
8484 break;
8485 }
8486}
8487
8488/*
8489 * "readfile()" function
8490 */
8491 static void
8492f_readfile(typval_T *argvars, typval_T *rettv)
8493{
8494 int binary = FALSE;
8495 int failed = FALSE;
8496 char_u *fname;
8497 FILE *fd;
8498 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8499 int io_size = sizeof(buf);
8500 int readlen; /* size of last fread() */
8501 char_u *prev = NULL; /* previously read bytes, if any */
8502 long prevlen = 0; /* length of data in prev */
8503 long prevsize = 0; /* size of prev buffer */
8504 long maxline = MAXLNUM;
8505 long cnt = 0;
8506 char_u *p; /* position in buf */
8507 char_u *start; /* start of current line */
8508
8509 if (argvars[1].v_type != VAR_UNKNOWN)
8510 {
8511 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8512 binary = TRUE;
8513 if (argvars[2].v_type != VAR_UNKNOWN)
8514 maxline = (long)get_tv_number(&argvars[2]);
8515 }
8516
8517 if (rettv_list_alloc(rettv) == FAIL)
8518 return;
8519
8520 /* Always open the file in binary mode, library functions have a mind of
8521 * their own about CR-LF conversion. */
8522 fname = get_tv_string(&argvars[0]);
8523 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8524 {
8525 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8526 return;
8527 }
8528
8529 while (cnt < maxline || maxline < 0)
8530 {
8531 readlen = (int)fread(buf, 1, io_size, fd);
8532
8533 /* This for loop processes what was read, but is also entered at end
8534 * of file so that either:
8535 * - an incomplete line gets written
8536 * - a "binary" file gets an empty line at the end if it ends in a
8537 * newline. */
8538 for (p = buf, start = buf;
8539 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8540 ++p)
8541 {
8542 if (*p == '\n' || readlen <= 0)
8543 {
8544 listitem_T *li;
8545 char_u *s = NULL;
8546 long_u len = p - start;
8547
8548 /* Finished a line. Remove CRs before NL. */
8549 if (readlen > 0 && !binary)
8550 {
8551 while (len > 0 && start[len - 1] == '\r')
8552 --len;
8553 /* removal may cross back to the "prev" string */
8554 if (len == 0)
8555 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8556 --prevlen;
8557 }
8558 if (prevlen == 0)
8559 s = vim_strnsave(start, (int)len);
8560 else
8561 {
8562 /* Change "prev" buffer to be the right size. This way
8563 * the bytes are only copied once, and very long lines are
8564 * allocated only once. */
8565 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8566 {
8567 mch_memmove(s + prevlen, start, len);
8568 s[prevlen + len] = NUL;
8569 prev = NULL; /* the list will own the string */
8570 prevlen = prevsize = 0;
8571 }
8572 }
8573 if (s == NULL)
8574 {
8575 do_outofmem_msg((long_u) prevlen + len + 1);
8576 failed = TRUE;
8577 break;
8578 }
8579
8580 if ((li = listitem_alloc()) == NULL)
8581 {
8582 vim_free(s);
8583 failed = TRUE;
8584 break;
8585 }
8586 li->li_tv.v_type = VAR_STRING;
8587 li->li_tv.v_lock = 0;
8588 li->li_tv.vval.v_string = s;
8589 list_append(rettv->vval.v_list, li);
8590
8591 start = p + 1; /* step over newline */
8592 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8593 break;
8594 }
8595 else if (*p == NUL)
8596 *p = '\n';
8597#ifdef FEAT_MBYTE
8598 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8599 * when finding the BF and check the previous two bytes. */
8600 else if (*p == 0xbf && enc_utf8 && !binary)
8601 {
8602 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8603 * + 1, these may be in the "prev" string. */
8604 char_u back1 = p >= buf + 1 ? p[-1]
8605 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8606 char_u back2 = p >= buf + 2 ? p[-2]
8607 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8608 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8609
8610 if (back2 == 0xef && back1 == 0xbb)
8611 {
8612 char_u *dest = p - 2;
8613
8614 /* Usually a BOM is at the beginning of a file, and so at
8615 * the beginning of a line; then we can just step over it.
8616 */
8617 if (start == dest)
8618 start = p + 1;
8619 else
8620 {
8621 /* have to shuffle buf to close gap */
8622 int adjust_prevlen = 0;
8623
8624 if (dest < buf)
8625 {
8626 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8627 dest = buf;
8628 }
8629 if (readlen > p - buf + 1)
8630 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8631 readlen -= 3 - adjust_prevlen;
8632 prevlen -= adjust_prevlen;
8633 p = dest - 1;
8634 }
8635 }
8636 }
8637#endif
8638 } /* for */
8639
8640 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8641 break;
8642 if (start < p)
8643 {
8644 /* There's part of a line in buf, store it in "prev". */
8645 if (p - start + prevlen >= prevsize)
8646 {
8647 /* need bigger "prev" buffer */
8648 char_u *newprev;
8649
8650 /* A common use case is ordinary text files and "prev" gets a
8651 * fragment of a line, so the first allocation is made
8652 * small, to avoid repeatedly 'allocing' large and
8653 * 'reallocing' small. */
8654 if (prevsize == 0)
8655 prevsize = (long)(p - start);
8656 else
8657 {
8658 long grow50pc = (prevsize * 3) / 2;
8659 long growmin = (long)((p - start) * 2 + prevlen);
8660 prevsize = grow50pc > growmin ? grow50pc : growmin;
8661 }
8662 newprev = prev == NULL ? alloc(prevsize)
8663 : vim_realloc(prev, prevsize);
8664 if (newprev == NULL)
8665 {
8666 do_outofmem_msg((long_u)prevsize);
8667 failed = TRUE;
8668 break;
8669 }
8670 prev = newprev;
8671 }
8672 /* Add the line part to end of "prev". */
8673 mch_memmove(prev + prevlen, start, p - start);
8674 prevlen += (long)(p - start);
8675 }
8676 } /* while */
8677
8678 /*
8679 * For a negative line count use only the lines at the end of the file,
8680 * free the rest.
8681 */
8682 if (!failed && maxline < 0)
8683 while (cnt > -maxline)
8684 {
8685 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8686 --cnt;
8687 }
8688
8689 if (failed)
8690 {
8691 list_free(rettv->vval.v_list);
8692 /* readfile doc says an empty list is returned on error */
8693 rettv->vval.v_list = list_alloc();
8694 }
8695
8696 vim_free(prev);
8697 fclose(fd);
8698}
8699
8700#if defined(FEAT_RELTIME)
8701static int list2proftime(typval_T *arg, proftime_T *tm);
8702
8703/*
8704 * Convert a List to proftime_T.
8705 * Return FAIL when there is something wrong.
8706 */
8707 static int
8708list2proftime(typval_T *arg, proftime_T *tm)
8709{
8710 long n1, n2;
8711 int error = FALSE;
8712
8713 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8714 || arg->vval.v_list->lv_len != 2)
8715 return FAIL;
8716 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8717 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8718# ifdef WIN3264
8719 tm->HighPart = n1;
8720 tm->LowPart = n2;
8721# else
8722 tm->tv_sec = n1;
8723 tm->tv_usec = n2;
8724# endif
8725 return error ? FAIL : OK;
8726}
8727#endif /* FEAT_RELTIME */
8728
8729/*
8730 * "reltime()" function
8731 */
8732 static void
8733f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8734{
8735#ifdef FEAT_RELTIME
8736 proftime_T res;
8737 proftime_T start;
8738
8739 if (argvars[0].v_type == VAR_UNKNOWN)
8740 {
8741 /* No arguments: get current time. */
8742 profile_start(&res);
8743 }
8744 else if (argvars[1].v_type == VAR_UNKNOWN)
8745 {
8746 if (list2proftime(&argvars[0], &res) == FAIL)
8747 return;
8748 profile_end(&res);
8749 }
8750 else
8751 {
8752 /* Two arguments: compute the difference. */
8753 if (list2proftime(&argvars[0], &start) == FAIL
8754 || list2proftime(&argvars[1], &res) == FAIL)
8755 return;
8756 profile_sub(&res, &start);
8757 }
8758
8759 if (rettv_list_alloc(rettv) == OK)
8760 {
8761 long n1, n2;
8762
8763# ifdef WIN3264
8764 n1 = res.HighPart;
8765 n2 = res.LowPart;
8766# else
8767 n1 = res.tv_sec;
8768 n2 = res.tv_usec;
8769# endif
8770 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8771 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8772 }
8773#endif
8774}
8775
8776#ifdef FEAT_FLOAT
8777/*
8778 * "reltimefloat()" function
8779 */
8780 static void
8781f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8782{
8783# ifdef FEAT_RELTIME
8784 proftime_T tm;
8785# endif
8786
8787 rettv->v_type = VAR_FLOAT;
8788 rettv->vval.v_float = 0;
8789# ifdef FEAT_RELTIME
8790 if (list2proftime(&argvars[0], &tm) == OK)
8791 rettv->vval.v_float = profile_float(&tm);
8792# endif
8793}
8794#endif
8795
8796/*
8797 * "reltimestr()" function
8798 */
8799 static void
8800f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8801{
8802#ifdef FEAT_RELTIME
8803 proftime_T tm;
8804#endif
8805
8806 rettv->v_type = VAR_STRING;
8807 rettv->vval.v_string = NULL;
8808#ifdef FEAT_RELTIME
8809 if (list2proftime(&argvars[0], &tm) == OK)
8810 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8811#endif
8812}
8813
8814#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8815static void make_connection(void);
8816static int check_connection(void);
8817
8818 static void
8819make_connection(void)
8820{
8821 if (X_DISPLAY == NULL
8822# ifdef FEAT_GUI
8823 && !gui.in_use
8824# endif
8825 )
8826 {
8827 x_force_connect = TRUE;
8828 setup_term_clip();
8829 x_force_connect = FALSE;
8830 }
8831}
8832
8833 static int
8834check_connection(void)
8835{
8836 make_connection();
8837 if (X_DISPLAY == NULL)
8838 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008839 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840 return FAIL;
8841 }
8842 return OK;
8843}
8844#endif
8845
8846#ifdef FEAT_CLIENTSERVER
8847 static void
8848remote_common(typval_T *argvars, typval_T *rettv, int expr)
8849{
8850 char_u *server_name;
8851 char_u *keys;
8852 char_u *r = NULL;
8853 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008854 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008855# ifdef WIN32
8856 HWND w;
8857# else
8858 Window w;
8859# endif
8860
8861 if (check_restricted() || check_secure())
8862 return;
8863
8864# ifdef FEAT_X11
8865 if (check_connection() == FAIL)
8866 return;
8867# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008868 if (argvars[2].v_type != VAR_UNKNOWN
8869 && argvars[3].v_type != VAR_UNKNOWN)
8870 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008871
8872 server_name = get_tv_string_chk(&argvars[0]);
8873 if (server_name == NULL)
8874 return; /* type error; errmsg already given */
8875 keys = get_tv_string_buf(&argvars[1], buf);
8876# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008877 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008878# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008879 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8880 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008881# endif
8882 {
8883 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008884 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008885 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008886 vim_free(r);
8887 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008888 else
8889 EMSG2(_("E241: Unable to send to %s"), server_name);
8890 return;
8891 }
8892
8893 rettv->vval.v_string = r;
8894
8895 if (argvars[2].v_type != VAR_UNKNOWN)
8896 {
8897 dictitem_T v;
8898 char_u str[30];
8899 char_u *idvar;
8900
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008901 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008902 if (idvar != NULL && *idvar != NUL)
8903 {
8904 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8905 v.di_tv.v_type = VAR_STRING;
8906 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008907 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008908 vim_free(v.di_tv.vval.v_string);
8909 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008910 }
8911}
8912#endif
8913
8914/*
8915 * "remote_expr()" function
8916 */
8917 static void
8918f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8919{
8920 rettv->v_type = VAR_STRING;
8921 rettv->vval.v_string = NULL;
8922#ifdef FEAT_CLIENTSERVER
8923 remote_common(argvars, rettv, TRUE);
8924#endif
8925}
8926
8927/*
8928 * "remote_foreground()" function
8929 */
8930 static void
8931f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8932{
8933#ifdef FEAT_CLIENTSERVER
8934# ifdef WIN32
8935 /* On Win32 it's done in this application. */
8936 {
8937 char_u *server_name = get_tv_string_chk(&argvars[0]);
8938
8939 if (server_name != NULL)
8940 serverForeground(server_name);
8941 }
8942# else
8943 /* Send a foreground() expression to the server. */
8944 argvars[1].v_type = VAR_STRING;
8945 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8946 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008947 rettv->v_type = VAR_STRING;
8948 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008949 remote_common(argvars, rettv, TRUE);
8950 vim_free(argvars[1].vval.v_string);
8951# endif
8952#endif
8953}
8954
8955 static void
8956f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8957{
8958#ifdef FEAT_CLIENTSERVER
8959 dictitem_T v;
8960 char_u *s = NULL;
8961# ifdef WIN32
8962 long_u n = 0;
8963# endif
8964 char_u *serverid;
8965
8966 if (check_restricted() || check_secure())
8967 {
8968 rettv->vval.v_number = -1;
8969 return;
8970 }
8971 serverid = get_tv_string_chk(&argvars[0]);
8972 if (serverid == NULL)
8973 {
8974 rettv->vval.v_number = -1;
8975 return; /* type error; errmsg already given */
8976 }
8977# ifdef WIN32
8978 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8979 if (n == 0)
8980 rettv->vval.v_number = -1;
8981 else
8982 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008983 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008984 rettv->vval.v_number = (s != NULL);
8985 }
8986# else
8987 if (check_connection() == FAIL)
8988 return;
8989
8990 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8991 serverStrToWin(serverid), &s);
8992# endif
8993
8994 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8995 {
8996 char_u *retvar;
8997
8998 v.di_tv.v_type = VAR_STRING;
8999 v.di_tv.vval.v_string = vim_strsave(s);
9000 retvar = get_tv_string_chk(&argvars[1]);
9001 if (retvar != NULL)
9002 set_var(retvar, &v.di_tv, FALSE);
9003 vim_free(v.di_tv.vval.v_string);
9004 }
9005#else
9006 rettv->vval.v_number = -1;
9007#endif
9008}
9009
9010 static void
9011f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9012{
9013 char_u *r = NULL;
9014
9015#ifdef FEAT_CLIENTSERVER
9016 char_u *serverid = get_tv_string_chk(&argvars[0]);
9017
9018 if (serverid != NULL && !check_restricted() && !check_secure())
9019 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009020 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009021# ifdef WIN32
9022 /* The server's HWND is encoded in the 'id' parameter */
9023 long_u n = 0;
9024# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009025
9026 if (argvars[1].v_type != VAR_UNKNOWN)
9027 timeout = get_tv_number(&argvars[1]);
9028
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009029# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009030 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9031 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009032 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033 if (r == NULL)
9034# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009035 if (check_connection() == FAIL
9036 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9037 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009038# endif
9039 EMSG(_("E277: Unable to read a server reply"));
9040 }
9041#endif
9042 rettv->v_type = VAR_STRING;
9043 rettv->vval.v_string = r;
9044}
9045
9046/*
9047 * "remote_send()" function
9048 */
9049 static void
9050f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9051{
9052 rettv->v_type = VAR_STRING;
9053 rettv->vval.v_string = NULL;
9054#ifdef FEAT_CLIENTSERVER
9055 remote_common(argvars, rettv, FALSE);
9056#endif
9057}
9058
9059/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009060 * "remote_startserver()" function
9061 */
9062 static void
9063f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9064{
9065#ifdef FEAT_CLIENTSERVER
9066 char_u *server = get_tv_string_chk(&argvars[0]);
9067
9068 if (server == NULL)
9069 return; /* type error; errmsg already given */
9070 if (serverName != NULL)
9071 EMSG(_("E941: already started a server"));
9072 else
9073 {
9074# ifdef FEAT_X11
9075 if (check_connection() == OK)
9076 serverRegisterName(X_DISPLAY, server);
9077# else
9078 serverSetName(server);
9079# endif
9080 }
9081#else
9082 EMSG(_("E942: +clientserver feature not available"));
9083#endif
9084}
9085
9086/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009087 * "remove()" function
9088 */
9089 static void
9090f_remove(typval_T *argvars, typval_T *rettv)
9091{
9092 list_T *l;
9093 listitem_T *item, *item2;
9094 listitem_T *li;
9095 long idx;
9096 long end;
9097 char_u *key;
9098 dict_T *d;
9099 dictitem_T *di;
9100 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9101
9102 if (argvars[0].v_type == VAR_DICT)
9103 {
9104 if (argvars[2].v_type != VAR_UNKNOWN)
9105 EMSG2(_(e_toomanyarg), "remove()");
9106 else if ((d = argvars[0].vval.v_dict) != NULL
9107 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9108 {
9109 key = get_tv_string_chk(&argvars[1]);
9110 if (key != NULL)
9111 {
9112 di = dict_find(d, key, -1);
9113 if (di == NULL)
9114 EMSG2(_(e_dictkey), key);
9115 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9116 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9117 {
9118 *rettv = di->di_tv;
9119 init_tv(&di->di_tv);
9120 dictitem_remove(d, di);
9121 }
9122 }
9123 }
9124 }
9125 else if (argvars[0].v_type != VAR_LIST)
9126 EMSG2(_(e_listdictarg), "remove()");
9127 else if ((l = argvars[0].vval.v_list) != NULL
9128 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9129 {
9130 int error = FALSE;
9131
9132 idx = (long)get_tv_number_chk(&argvars[1], &error);
9133 if (error)
9134 ; /* type error: do nothing, errmsg already given */
9135 else if ((item = list_find(l, idx)) == NULL)
9136 EMSGN(_(e_listidx), idx);
9137 else
9138 {
9139 if (argvars[2].v_type == VAR_UNKNOWN)
9140 {
9141 /* Remove one item, return its value. */
9142 vimlist_remove(l, item, item);
9143 *rettv = item->li_tv;
9144 vim_free(item);
9145 }
9146 else
9147 {
9148 /* Remove range of items, return list with values. */
9149 end = (long)get_tv_number_chk(&argvars[2], &error);
9150 if (error)
9151 ; /* type error: do nothing */
9152 else if ((item2 = list_find(l, end)) == NULL)
9153 EMSGN(_(e_listidx), end);
9154 else
9155 {
9156 int cnt = 0;
9157
9158 for (li = item; li != NULL; li = li->li_next)
9159 {
9160 ++cnt;
9161 if (li == item2)
9162 break;
9163 }
9164 if (li == NULL) /* didn't find "item2" after "item" */
9165 EMSG(_(e_invrange));
9166 else
9167 {
9168 vimlist_remove(l, item, item2);
9169 if (rettv_list_alloc(rettv) == OK)
9170 {
9171 l = rettv->vval.v_list;
9172 l->lv_first = item;
9173 l->lv_last = item2;
9174 item->li_prev = NULL;
9175 item2->li_next = NULL;
9176 l->lv_len = cnt;
9177 }
9178 }
9179 }
9180 }
9181 }
9182 }
9183}
9184
9185/*
9186 * "rename({from}, {to})" function
9187 */
9188 static void
9189f_rename(typval_T *argvars, typval_T *rettv)
9190{
9191 char_u buf[NUMBUFLEN];
9192
9193 if (check_restricted() || check_secure())
9194 rettv->vval.v_number = -1;
9195 else
9196 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9197 get_tv_string_buf(&argvars[1], buf));
9198}
9199
9200/*
9201 * "repeat()" function
9202 */
9203 static void
9204f_repeat(typval_T *argvars, typval_T *rettv)
9205{
9206 char_u *p;
9207 int n;
9208 int slen;
9209 int len;
9210 char_u *r;
9211 int i;
9212
9213 n = (int)get_tv_number(&argvars[1]);
9214 if (argvars[0].v_type == VAR_LIST)
9215 {
9216 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9217 while (n-- > 0)
9218 if (list_extend(rettv->vval.v_list,
9219 argvars[0].vval.v_list, NULL) == FAIL)
9220 break;
9221 }
9222 else
9223 {
9224 p = get_tv_string(&argvars[0]);
9225 rettv->v_type = VAR_STRING;
9226 rettv->vval.v_string = NULL;
9227
9228 slen = (int)STRLEN(p);
9229 len = slen * n;
9230 if (len <= 0)
9231 return;
9232
9233 r = alloc(len + 1);
9234 if (r != NULL)
9235 {
9236 for (i = 0; i < n; i++)
9237 mch_memmove(r + i * slen, p, (size_t)slen);
9238 r[len] = NUL;
9239 }
9240
9241 rettv->vval.v_string = r;
9242 }
9243}
9244
9245/*
9246 * "resolve()" function
9247 */
9248 static void
9249f_resolve(typval_T *argvars, typval_T *rettv)
9250{
9251 char_u *p;
9252#ifdef HAVE_READLINK
9253 char_u *buf = NULL;
9254#endif
9255
9256 p = get_tv_string(&argvars[0]);
9257#ifdef FEAT_SHORTCUT
9258 {
9259 char_u *v = NULL;
9260
9261 v = mch_resolve_shortcut(p);
9262 if (v != NULL)
9263 rettv->vval.v_string = v;
9264 else
9265 rettv->vval.v_string = vim_strsave(p);
9266 }
9267#else
9268# ifdef HAVE_READLINK
9269 {
9270 char_u *cpy;
9271 int len;
9272 char_u *remain = NULL;
9273 char_u *q;
9274 int is_relative_to_current = FALSE;
9275 int has_trailing_pathsep = FALSE;
9276 int limit = 100;
9277
9278 p = vim_strsave(p);
9279
9280 if (p[0] == '.' && (vim_ispathsep(p[1])
9281 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9282 is_relative_to_current = TRUE;
9283
9284 len = STRLEN(p);
9285 if (len > 0 && after_pathsep(p, p + len))
9286 {
9287 has_trailing_pathsep = TRUE;
9288 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9289 }
9290
9291 q = getnextcomp(p);
9292 if (*q != NUL)
9293 {
9294 /* Separate the first path component in "p", and keep the
9295 * remainder (beginning with the path separator). */
9296 remain = vim_strsave(q - 1);
9297 q[-1] = NUL;
9298 }
9299
9300 buf = alloc(MAXPATHL + 1);
9301 if (buf == NULL)
9302 goto fail;
9303
9304 for (;;)
9305 {
9306 for (;;)
9307 {
9308 len = readlink((char *)p, (char *)buf, MAXPATHL);
9309 if (len <= 0)
9310 break;
9311 buf[len] = NUL;
9312
9313 if (limit-- == 0)
9314 {
9315 vim_free(p);
9316 vim_free(remain);
9317 EMSG(_("E655: Too many symbolic links (cycle?)"));
9318 rettv->vval.v_string = NULL;
9319 goto fail;
9320 }
9321
9322 /* Ensure that the result will have a trailing path separator
9323 * if the argument has one. */
9324 if (remain == NULL && has_trailing_pathsep)
9325 add_pathsep(buf);
9326
9327 /* Separate the first path component in the link value and
9328 * concatenate the remainders. */
9329 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9330 if (*q != NUL)
9331 {
9332 if (remain == NULL)
9333 remain = vim_strsave(q - 1);
9334 else
9335 {
9336 cpy = concat_str(q - 1, remain);
9337 if (cpy != NULL)
9338 {
9339 vim_free(remain);
9340 remain = cpy;
9341 }
9342 }
9343 q[-1] = NUL;
9344 }
9345
9346 q = gettail(p);
9347 if (q > p && *q == NUL)
9348 {
9349 /* Ignore trailing path separator. */
9350 q[-1] = NUL;
9351 q = gettail(p);
9352 }
9353 if (q > p && !mch_isFullName(buf))
9354 {
9355 /* symlink is relative to directory of argument */
9356 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9357 if (cpy != NULL)
9358 {
9359 STRCPY(cpy, p);
9360 STRCPY(gettail(cpy), buf);
9361 vim_free(p);
9362 p = cpy;
9363 }
9364 }
9365 else
9366 {
9367 vim_free(p);
9368 p = vim_strsave(buf);
9369 }
9370 }
9371
9372 if (remain == NULL)
9373 break;
9374
9375 /* Append the first path component of "remain" to "p". */
9376 q = getnextcomp(remain + 1);
9377 len = q - remain - (*q != NUL);
9378 cpy = vim_strnsave(p, STRLEN(p) + len);
9379 if (cpy != NULL)
9380 {
9381 STRNCAT(cpy, remain, len);
9382 vim_free(p);
9383 p = cpy;
9384 }
9385 /* Shorten "remain". */
9386 if (*q != NUL)
9387 STRMOVE(remain, q - 1);
9388 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009389 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009390 }
9391
9392 /* If the result is a relative path name, make it explicitly relative to
9393 * the current directory if and only if the argument had this form. */
9394 if (!vim_ispathsep(*p))
9395 {
9396 if (is_relative_to_current
9397 && *p != NUL
9398 && !(p[0] == '.'
9399 && (p[1] == NUL
9400 || vim_ispathsep(p[1])
9401 || (p[1] == '.'
9402 && (p[2] == NUL
9403 || vim_ispathsep(p[2]))))))
9404 {
9405 /* Prepend "./". */
9406 cpy = concat_str((char_u *)"./", p);
9407 if (cpy != NULL)
9408 {
9409 vim_free(p);
9410 p = cpy;
9411 }
9412 }
9413 else if (!is_relative_to_current)
9414 {
9415 /* Strip leading "./". */
9416 q = p;
9417 while (q[0] == '.' && vim_ispathsep(q[1]))
9418 q += 2;
9419 if (q > p)
9420 STRMOVE(p, p + 2);
9421 }
9422 }
9423
9424 /* Ensure that the result will have no trailing path separator
9425 * if the argument had none. But keep "/" or "//". */
9426 if (!has_trailing_pathsep)
9427 {
9428 q = p + STRLEN(p);
9429 if (after_pathsep(p, q))
9430 *gettail_sep(p) = NUL;
9431 }
9432
9433 rettv->vval.v_string = p;
9434 }
9435# else
9436 rettv->vval.v_string = vim_strsave(p);
9437# endif
9438#endif
9439
9440 simplify_filename(rettv->vval.v_string);
9441
9442#ifdef HAVE_READLINK
9443fail:
9444 vim_free(buf);
9445#endif
9446 rettv->v_type = VAR_STRING;
9447}
9448
9449/*
9450 * "reverse({list})" function
9451 */
9452 static void
9453f_reverse(typval_T *argvars, typval_T *rettv)
9454{
9455 list_T *l;
9456 listitem_T *li, *ni;
9457
9458 if (argvars[0].v_type != VAR_LIST)
9459 EMSG2(_(e_listarg), "reverse()");
9460 else if ((l = argvars[0].vval.v_list) != NULL
9461 && !tv_check_lock(l->lv_lock,
9462 (char_u *)N_("reverse() argument"), TRUE))
9463 {
9464 li = l->lv_last;
9465 l->lv_first = l->lv_last = NULL;
9466 l->lv_len = 0;
9467 while (li != NULL)
9468 {
9469 ni = li->li_prev;
9470 list_append(l, li);
9471 li = ni;
9472 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009473 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009474 l->lv_idx = l->lv_len - l->lv_idx - 1;
9475 }
9476}
9477
9478#define SP_NOMOVE 0x01 /* don't move cursor */
9479#define SP_REPEAT 0x02 /* repeat to find outer pair */
9480#define SP_RETCOUNT 0x04 /* return matchcount */
9481#define SP_SETPCMARK 0x08 /* set previous context mark */
9482#define SP_START 0x10 /* accept match at start position */
9483#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9484#define SP_END 0x40 /* leave cursor at end of match */
9485#define SP_COLUMN 0x80 /* start at cursor column */
9486
9487static int get_search_arg(typval_T *varp, int *flagsp);
9488
9489/*
9490 * Get flags for a search function.
9491 * Possibly sets "p_ws".
9492 * Returns BACKWARD, FORWARD or zero (for an error).
9493 */
9494 static int
9495get_search_arg(typval_T *varp, int *flagsp)
9496{
9497 int dir = FORWARD;
9498 char_u *flags;
9499 char_u nbuf[NUMBUFLEN];
9500 int mask;
9501
9502 if (varp->v_type != VAR_UNKNOWN)
9503 {
9504 flags = get_tv_string_buf_chk(varp, nbuf);
9505 if (flags == NULL)
9506 return 0; /* type error; errmsg already given */
9507 while (*flags != NUL)
9508 {
9509 switch (*flags)
9510 {
9511 case 'b': dir = BACKWARD; break;
9512 case 'w': p_ws = TRUE; break;
9513 case 'W': p_ws = FALSE; break;
9514 default: mask = 0;
9515 if (flagsp != NULL)
9516 switch (*flags)
9517 {
9518 case 'c': mask = SP_START; break;
9519 case 'e': mask = SP_END; break;
9520 case 'm': mask = SP_RETCOUNT; break;
9521 case 'n': mask = SP_NOMOVE; break;
9522 case 'p': mask = SP_SUBPAT; break;
9523 case 'r': mask = SP_REPEAT; break;
9524 case 's': mask = SP_SETPCMARK; break;
9525 case 'z': mask = SP_COLUMN; break;
9526 }
9527 if (mask == 0)
9528 {
9529 EMSG2(_(e_invarg2), flags);
9530 dir = 0;
9531 }
9532 else
9533 *flagsp |= mask;
9534 }
9535 if (dir == 0)
9536 break;
9537 ++flags;
9538 }
9539 }
9540 return dir;
9541}
9542
9543/*
9544 * Shared by search() and searchpos() functions.
9545 */
9546 static int
9547search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9548{
9549 int flags;
9550 char_u *pat;
9551 pos_T pos;
9552 pos_T save_cursor;
9553 int save_p_ws = p_ws;
9554 int dir;
9555 int retval = 0; /* default: FAIL */
9556 long lnum_stop = 0;
9557 proftime_T tm;
9558#ifdef FEAT_RELTIME
9559 long time_limit = 0;
9560#endif
9561 int options = SEARCH_KEEP;
9562 int subpatnum;
9563
9564 pat = get_tv_string(&argvars[0]);
9565 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9566 if (dir == 0)
9567 goto theend;
9568 flags = *flagsp;
9569 if (flags & SP_START)
9570 options |= SEARCH_START;
9571 if (flags & SP_END)
9572 options |= SEARCH_END;
9573 if (flags & SP_COLUMN)
9574 options |= SEARCH_COL;
9575
9576 /* Optional arguments: line number to stop searching and timeout. */
9577 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9578 {
9579 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9580 if (lnum_stop < 0)
9581 goto theend;
9582#ifdef FEAT_RELTIME
9583 if (argvars[3].v_type != VAR_UNKNOWN)
9584 {
9585 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9586 if (time_limit < 0)
9587 goto theend;
9588 }
9589#endif
9590 }
9591
9592#ifdef FEAT_RELTIME
9593 /* Set the time limit, if there is one. */
9594 profile_setlimit(time_limit, &tm);
9595#endif
9596
9597 /*
9598 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9599 * Check to make sure only those flags are set.
9600 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9601 * flags cannot be set. Check for that condition also.
9602 */
9603 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9604 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9605 {
9606 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9607 goto theend;
9608 }
9609
9610 pos = save_cursor = curwin->w_cursor;
9611 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009612 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009613 if (subpatnum != FAIL)
9614 {
9615 if (flags & SP_SUBPAT)
9616 retval = subpatnum;
9617 else
9618 retval = pos.lnum;
9619 if (flags & SP_SETPCMARK)
9620 setpcmark();
9621 curwin->w_cursor = pos;
9622 if (match_pos != NULL)
9623 {
9624 /* Store the match cursor position */
9625 match_pos->lnum = pos.lnum;
9626 match_pos->col = pos.col + 1;
9627 }
9628 /* "/$" will put the cursor after the end of the line, may need to
9629 * correct that here */
9630 check_cursor();
9631 }
9632
9633 /* If 'n' flag is used: restore cursor position. */
9634 if (flags & SP_NOMOVE)
9635 curwin->w_cursor = save_cursor;
9636 else
9637 curwin->w_set_curswant = TRUE;
9638theend:
9639 p_ws = save_p_ws;
9640
9641 return retval;
9642}
9643
9644#ifdef FEAT_FLOAT
9645
9646/*
9647 * round() is not in C90, use ceil() or floor() instead.
9648 */
9649 float_T
9650vim_round(float_T f)
9651{
9652 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9653}
9654
9655/*
9656 * "round({float})" function
9657 */
9658 static void
9659f_round(typval_T *argvars, typval_T *rettv)
9660{
9661 float_T f = 0.0;
9662
9663 rettv->v_type = VAR_FLOAT;
9664 if (get_float_arg(argvars, &f) == OK)
9665 rettv->vval.v_float = vim_round(f);
9666 else
9667 rettv->vval.v_float = 0.0;
9668}
9669#endif
9670
9671/*
9672 * "screenattr()" function
9673 */
9674 static void
9675f_screenattr(typval_T *argvars, typval_T *rettv)
9676{
9677 int row;
9678 int col;
9679 int c;
9680
9681 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9682 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9683 if (row < 0 || row >= screen_Rows
9684 || col < 0 || col >= screen_Columns)
9685 c = -1;
9686 else
9687 c = ScreenAttrs[LineOffset[row] + col];
9688 rettv->vval.v_number = c;
9689}
9690
9691/*
9692 * "screenchar()" function
9693 */
9694 static void
9695f_screenchar(typval_T *argvars, typval_T *rettv)
9696{
9697 int row;
9698 int col;
9699 int off;
9700 int c;
9701
9702 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9703 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9704 if (row < 0 || row >= screen_Rows
9705 || col < 0 || col >= screen_Columns)
9706 c = -1;
9707 else
9708 {
9709 off = LineOffset[row] + col;
9710#ifdef FEAT_MBYTE
9711 if (enc_utf8 && ScreenLinesUC[off] != 0)
9712 c = ScreenLinesUC[off];
9713 else
9714#endif
9715 c = ScreenLines[off];
9716 }
9717 rettv->vval.v_number = c;
9718}
9719
9720/*
9721 * "screencol()" function
9722 *
9723 * First column is 1 to be consistent with virtcol().
9724 */
9725 static void
9726f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9727{
9728 rettv->vval.v_number = screen_screencol() + 1;
9729}
9730
9731/*
9732 * "screenrow()" function
9733 */
9734 static void
9735f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9736{
9737 rettv->vval.v_number = screen_screenrow() + 1;
9738}
9739
9740/*
9741 * "search()" function
9742 */
9743 static void
9744f_search(typval_T *argvars, typval_T *rettv)
9745{
9746 int flags = 0;
9747
9748 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9749}
9750
9751/*
9752 * "searchdecl()" function
9753 */
9754 static void
9755f_searchdecl(typval_T *argvars, typval_T *rettv)
9756{
9757 int locally = 1;
9758 int thisblock = 0;
9759 int error = FALSE;
9760 char_u *name;
9761
9762 rettv->vval.v_number = 1; /* default: FAIL */
9763
9764 name = get_tv_string_chk(&argvars[0]);
9765 if (argvars[1].v_type != VAR_UNKNOWN)
9766 {
9767 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9768 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9769 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9770 }
9771 if (!error && name != NULL)
9772 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9773 locally, thisblock, SEARCH_KEEP) == FAIL;
9774}
9775
9776/*
9777 * Used by searchpair() and searchpairpos()
9778 */
9779 static int
9780searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9781{
9782 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009783 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009784 int save_p_ws = p_ws;
9785 int dir;
9786 int flags = 0;
9787 char_u nbuf1[NUMBUFLEN];
9788 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009789 int retval = 0; /* default: FAIL */
9790 long lnum_stop = 0;
9791 long time_limit = 0;
9792
9793 /* Get the three pattern arguments: start, middle, end. */
9794 spat = get_tv_string_chk(&argvars[0]);
9795 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9796 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9797 if (spat == NULL || mpat == NULL || epat == NULL)
9798 goto theend; /* type error */
9799
9800 /* Handle the optional fourth argument: flags */
9801 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9802 if (dir == 0)
9803 goto theend;
9804
9805 /* Don't accept SP_END or SP_SUBPAT.
9806 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9807 */
9808 if ((flags & (SP_END | SP_SUBPAT)) != 0
9809 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9810 {
9811 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9812 goto theend;
9813 }
9814
9815 /* Using 'r' implies 'W', otherwise it doesn't work. */
9816 if (flags & SP_REPEAT)
9817 p_ws = FALSE;
9818
9819 /* Optional fifth argument: skip expression */
9820 if (argvars[3].v_type == VAR_UNKNOWN
9821 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009822 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009823 else
9824 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009825 skip = &argvars[4];
9826 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9827 && skip->v_type != VAR_STRING)
9828 {
9829 /* Type error */
9830 goto theend;
9831 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009832 if (argvars[5].v_type != VAR_UNKNOWN)
9833 {
9834 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9835 if (lnum_stop < 0)
9836 goto theend;
9837#ifdef FEAT_RELTIME
9838 if (argvars[6].v_type != VAR_UNKNOWN)
9839 {
9840 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9841 if (time_limit < 0)
9842 goto theend;
9843 }
9844#endif
9845 }
9846 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009847
9848 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9849 match_pos, lnum_stop, time_limit);
9850
9851theend:
9852 p_ws = save_p_ws;
9853
9854 return retval;
9855}
9856
9857/*
9858 * "searchpair()" function
9859 */
9860 static void
9861f_searchpair(typval_T *argvars, typval_T *rettv)
9862{
9863 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9864}
9865
9866/*
9867 * "searchpairpos()" function
9868 */
9869 static void
9870f_searchpairpos(typval_T *argvars, typval_T *rettv)
9871{
9872 pos_T match_pos;
9873 int lnum = 0;
9874 int col = 0;
9875
9876 if (rettv_list_alloc(rettv) == FAIL)
9877 return;
9878
9879 if (searchpair_cmn(argvars, &match_pos) > 0)
9880 {
9881 lnum = match_pos.lnum;
9882 col = match_pos.col;
9883 }
9884
9885 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9886 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9887}
9888
9889/*
9890 * Search for a start/middle/end thing.
9891 * Used by searchpair(), see its documentation for the details.
9892 * Returns 0 or -1 for no match,
9893 */
9894 long
9895do_searchpair(
9896 char_u *spat, /* start pattern */
9897 char_u *mpat, /* middle pattern */
9898 char_u *epat, /* end pattern */
9899 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009900 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009901 int flags, /* SP_SETPCMARK and other SP_ values */
9902 pos_T *match_pos,
9903 linenr_T lnum_stop, /* stop at this line if not zero */
9904 long time_limit UNUSED) /* stop after this many msec */
9905{
9906 char_u *save_cpo;
9907 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9908 long retval = 0;
9909 pos_T pos;
9910 pos_T firstpos;
9911 pos_T foundpos;
9912 pos_T save_cursor;
9913 pos_T save_pos;
9914 int n;
9915 int r;
9916 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009917 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009918 int err;
9919 int options = SEARCH_KEEP;
9920 proftime_T tm;
9921
9922 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9923 save_cpo = p_cpo;
9924 p_cpo = empty_option;
9925
9926#ifdef FEAT_RELTIME
9927 /* Set the time limit, if there is one. */
9928 profile_setlimit(time_limit, &tm);
9929#endif
9930
9931 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9932 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009933 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9934 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009935 if (pat2 == NULL || pat3 == NULL)
9936 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009937 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009938 if (*mpat == NUL)
9939 STRCPY(pat3, pat2);
9940 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009941 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009942 spat, epat, mpat);
9943 if (flags & SP_START)
9944 options |= SEARCH_START;
9945
Bram Moolenaar48570482017-10-30 21:48:41 +01009946 if (skip != NULL)
9947 {
9948 /* Empty string means to not use the skip expression. */
9949 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9950 use_skip = skip->vval.v_string != NULL
9951 && *skip->vval.v_string != NUL;
9952 }
9953
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009954 save_cursor = curwin->w_cursor;
9955 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009956 CLEAR_POS(&firstpos);
9957 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009958 pat = pat3;
9959 for (;;)
9960 {
9961 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009962 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009963 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009964 /* didn't find it or found the first match again: FAIL */
9965 break;
9966
9967 if (firstpos.lnum == 0)
9968 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009969 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009970 {
9971 /* Found the same position again. Can happen with a pattern that
9972 * has "\zs" at the end and searching backwards. Advance one
9973 * character and try again. */
9974 if (dir == BACKWARD)
9975 decl(&pos);
9976 else
9977 incl(&pos);
9978 }
9979 foundpos = pos;
9980
9981 /* clear the start flag to avoid getting stuck here */
9982 options &= ~SEARCH_START;
9983
9984 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009985 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009986 {
9987 save_pos = curwin->w_cursor;
9988 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009989 err = FALSE;
9990 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009991 curwin->w_cursor = save_pos;
9992 if (err)
9993 {
9994 /* Evaluating {skip} caused an error, break here. */
9995 curwin->w_cursor = save_cursor;
9996 retval = -1;
9997 break;
9998 }
9999 if (r)
10000 continue;
10001 }
10002
10003 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10004 {
10005 /* Found end when searching backwards or start when searching
10006 * forward: nested pair. */
10007 ++nest;
10008 pat = pat2; /* nested, don't search for middle */
10009 }
10010 else
10011 {
10012 /* Found end when searching forward or start when searching
10013 * backward: end of (nested) pair; or found middle in outer pair. */
10014 if (--nest == 1)
10015 pat = pat3; /* outer level, search for middle */
10016 }
10017
10018 if (nest == 0)
10019 {
10020 /* Found the match: return matchcount or line number. */
10021 if (flags & SP_RETCOUNT)
10022 ++retval;
10023 else
10024 retval = pos.lnum;
10025 if (flags & SP_SETPCMARK)
10026 setpcmark();
10027 curwin->w_cursor = pos;
10028 if (!(flags & SP_REPEAT))
10029 break;
10030 nest = 1; /* search for next unmatched */
10031 }
10032 }
10033
10034 if (match_pos != NULL)
10035 {
10036 /* Store the match cursor position */
10037 match_pos->lnum = curwin->w_cursor.lnum;
10038 match_pos->col = curwin->w_cursor.col + 1;
10039 }
10040
10041 /* If 'n' flag is used or search failed: restore cursor position. */
10042 if ((flags & SP_NOMOVE) || retval == 0)
10043 curwin->w_cursor = save_cursor;
10044
10045theend:
10046 vim_free(pat2);
10047 vim_free(pat3);
10048 if (p_cpo == empty_option)
10049 p_cpo = save_cpo;
10050 else
10051 /* Darn, evaluating the {skip} expression changed the value. */
10052 free_string_option(save_cpo);
10053
10054 return retval;
10055}
10056
10057/*
10058 * "searchpos()" function
10059 */
10060 static void
10061f_searchpos(typval_T *argvars, typval_T *rettv)
10062{
10063 pos_T match_pos;
10064 int lnum = 0;
10065 int col = 0;
10066 int n;
10067 int flags = 0;
10068
10069 if (rettv_list_alloc(rettv) == FAIL)
10070 return;
10071
10072 n = search_cmn(argvars, &match_pos, &flags);
10073 if (n > 0)
10074 {
10075 lnum = match_pos.lnum;
10076 col = match_pos.col;
10077 }
10078
10079 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10080 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10081 if (flags & SP_SUBPAT)
10082 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10083}
10084
10085 static void
10086f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10087{
10088#ifdef FEAT_CLIENTSERVER
10089 char_u buf[NUMBUFLEN];
10090 char_u *server = get_tv_string_chk(&argvars[0]);
10091 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10092
10093 rettv->vval.v_number = -1;
10094 if (server == NULL || reply == NULL)
10095 return;
10096 if (check_restricted() || check_secure())
10097 return;
10098# ifdef FEAT_X11
10099 if (check_connection() == FAIL)
10100 return;
10101# endif
10102
10103 if (serverSendReply(server, reply) < 0)
10104 {
10105 EMSG(_("E258: Unable to send to client"));
10106 return;
10107 }
10108 rettv->vval.v_number = 0;
10109#else
10110 rettv->vval.v_number = -1;
10111#endif
10112}
10113
10114 static void
10115f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10116{
10117 char_u *r = NULL;
10118
10119#ifdef FEAT_CLIENTSERVER
10120# ifdef WIN32
10121 r = serverGetVimNames();
10122# else
10123 make_connection();
10124 if (X_DISPLAY != NULL)
10125 r = serverGetVimNames(X_DISPLAY);
10126# endif
10127#endif
10128 rettv->v_type = VAR_STRING;
10129 rettv->vval.v_string = r;
10130}
10131
10132/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010133 * Set line or list of lines in buffer "buf".
10134 */
10135 static void
10136set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
10137{
10138 char_u *line = NULL;
10139 list_T *l = NULL;
10140 listitem_T *li = NULL;
10141 long added = 0;
10142 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010143 buf_T *curbuf_save = NULL;
10144 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010145 int is_curbuf = buf == curbuf;
10146
Bram Moolenaar9d954202017-09-04 20:34:19 +020010147 /* When using the current buffer ml_mfp will be set if needed. Useful when
10148 * setline() is used on startup. For other buffers the buffer must be
10149 * loaded. */
10150 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010151 {
10152 rettv->vval.v_number = 1; /* FAIL */
10153 return;
10154 }
10155
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010156 if (!is_curbuf)
10157 {
10158 wininfo_T *wip;
10159
10160 curbuf_save = curbuf;
10161 curwin_save = curwin;
10162 curbuf = buf;
10163 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10164 {
10165 if (wip->wi_win != NULL)
10166 {
10167 curwin = wip->wi_win;
10168 break;
10169 }
10170 }
10171 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010172
10173 lcount = curbuf->b_ml.ml_line_count;
10174
10175 if (lines->v_type == VAR_LIST)
10176 {
10177 l = lines->vval.v_list;
10178 li = l->lv_first;
10179 }
10180 else
10181 line = get_tv_string_chk(lines);
10182
10183 /* default result is zero == OK */
10184 for (;;)
10185 {
10186 if (l != NULL)
10187 {
10188 /* list argument, get next string */
10189 if (li == NULL)
10190 break;
10191 line = get_tv_string_chk(&li->li_tv);
10192 li = li->li_next;
10193 }
10194
10195 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +020010196 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010197 break;
10198
10199 /* When coming here from Insert mode, sync undo, so that this can be
10200 * undone separately from what was previously inserted. */
10201 if (u_sync_once == 2)
10202 {
10203 u_sync_once = 1; /* notify that u_sync() was called */
10204 u_sync(TRUE);
10205 }
10206
10207 if (lnum <= curbuf->b_ml.ml_line_count)
10208 {
10209 /* existing line, replace it */
10210 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10211 {
10212 changed_bytes(lnum, 0);
10213 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10214 check_cursor_col();
10215 rettv->vval.v_number = 0; /* OK */
10216 }
10217 }
10218 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10219 {
10220 /* lnum is one past the last line, append the line */
10221 ++added;
10222 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10223 rettv->vval.v_number = 0; /* OK */
10224 }
10225
10226 if (l == NULL) /* only one string argument */
10227 break;
10228 ++lnum;
10229 }
10230
10231 if (added > 0)
10232 appended_lines_mark(lcount, added);
10233
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010234 if (!is_curbuf)
10235 {
10236 curbuf = curbuf_save;
10237 curwin = curwin_save;
10238 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010239}
10240
10241/*
10242 * "setbufline()" function
10243 */
10244 static void
10245f_setbufline(argvars, rettv)
10246 typval_T *argvars;
10247 typval_T *rettv;
10248{
10249 linenr_T lnum;
10250 buf_T *buf;
10251
10252 buf = get_buf_tv(&argvars[0], FALSE);
10253 if (buf == NULL)
10254 rettv->vval.v_number = 1; /* FAIL */
10255 else
10256 {
10257 lnum = get_tv_lnum_buf(&argvars[1], buf);
10258
10259 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10260 }
10261}
10262
10263/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010264 * "setbufvar()" function
10265 */
10266 static void
10267f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10268{
10269 buf_T *buf;
10270 char_u *varname, *bufvarname;
10271 typval_T *varp;
10272 char_u nbuf[NUMBUFLEN];
10273
10274 if (check_restricted() || check_secure())
10275 return;
10276 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10277 varname = get_tv_string_chk(&argvars[1]);
10278 buf = get_buf_tv(&argvars[0], FALSE);
10279 varp = &argvars[2];
10280
10281 if (buf != NULL && varname != NULL && varp != NULL)
10282 {
10283 if (*varname == '&')
10284 {
10285 long numval;
10286 char_u *strval;
10287 int error = FALSE;
10288 aco_save_T aco;
10289
10290 /* set curbuf to be our buf, temporarily */
10291 aucmd_prepbuf(&aco, buf);
10292
10293 ++varname;
10294 numval = (long)get_tv_number_chk(varp, &error);
10295 strval = get_tv_string_buf_chk(varp, nbuf);
10296 if (!error && strval != NULL)
10297 set_option_value(varname, numval, strval, OPT_LOCAL);
10298
10299 /* reset notion of buffer */
10300 aucmd_restbuf(&aco);
10301 }
10302 else
10303 {
10304 buf_T *save_curbuf = curbuf;
10305
10306 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10307 if (bufvarname != NULL)
10308 {
10309 curbuf = buf;
10310 STRCPY(bufvarname, "b:");
10311 STRCPY(bufvarname + 2, varname);
10312 set_var(bufvarname, varp, TRUE);
10313 vim_free(bufvarname);
10314 curbuf = save_curbuf;
10315 }
10316 }
10317 }
10318}
10319
10320 static void
10321f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10322{
10323 dict_T *d;
10324 dictitem_T *di;
10325 char_u *csearch;
10326
10327 if (argvars[0].v_type != VAR_DICT)
10328 {
10329 EMSG(_(e_dictreq));
10330 return;
10331 }
10332
10333 if ((d = argvars[0].vval.v_dict) != NULL)
10334 {
10335 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10336 if (csearch != NULL)
10337 {
10338#ifdef FEAT_MBYTE
10339 if (enc_utf8)
10340 {
10341 int pcc[MAX_MCO];
10342 int c = utfc_ptr2char(csearch, pcc);
10343
10344 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10345 }
10346 else
10347#endif
10348 set_last_csearch(PTR2CHAR(csearch),
10349 csearch, MB_PTR2LEN(csearch));
10350 }
10351
10352 di = dict_find(d, (char_u *)"forward", -1);
10353 if (di != NULL)
10354 set_csearch_direction((int)get_tv_number(&di->di_tv)
10355 ? FORWARD : BACKWARD);
10356
10357 di = dict_find(d, (char_u *)"until", -1);
10358 if (di != NULL)
10359 set_csearch_until(!!get_tv_number(&di->di_tv));
10360 }
10361}
10362
10363/*
10364 * "setcmdpos()" function
10365 */
10366 static void
10367f_setcmdpos(typval_T *argvars, typval_T *rettv)
10368{
10369 int pos = (int)get_tv_number(&argvars[0]) - 1;
10370
10371 if (pos >= 0)
10372 rettv->vval.v_number = set_cmdline_pos(pos);
10373}
10374
10375/*
10376 * "setfperm({fname}, {mode})" function
10377 */
10378 static void
10379f_setfperm(typval_T *argvars, typval_T *rettv)
10380{
10381 char_u *fname;
10382 char_u modebuf[NUMBUFLEN];
10383 char_u *mode_str;
10384 int i;
10385 int mask;
10386 int mode = 0;
10387
10388 rettv->vval.v_number = 0;
10389 fname = get_tv_string_chk(&argvars[0]);
10390 if (fname == NULL)
10391 return;
10392 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10393 if (mode_str == NULL)
10394 return;
10395 if (STRLEN(mode_str) != 9)
10396 {
10397 EMSG2(_(e_invarg2), mode_str);
10398 return;
10399 }
10400
10401 mask = 1;
10402 for (i = 8; i >= 0; --i)
10403 {
10404 if (mode_str[i] != '-')
10405 mode |= mask;
10406 mask = mask << 1;
10407 }
10408 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10409}
10410
10411/*
10412 * "setline()" function
10413 */
10414 static void
10415f_setline(typval_T *argvars, typval_T *rettv)
10416{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010417 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010418
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010419 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010420}
10421
Bram Moolenaard823fa92016-08-12 16:29:27 +020010422static 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 +020010423
10424/*
10425 * Used by "setqflist()" and "setloclist()" functions
10426 */
10427 static void
10428set_qf_ll_list(
10429 win_T *wp UNUSED,
10430 typval_T *list_arg UNUSED,
10431 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010432 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010433 typval_T *rettv)
10434{
10435#ifdef FEAT_QUICKFIX
10436 static char *e_invact = N_("E927: Invalid action: '%s'");
10437 char_u *act;
10438 int action = 0;
10439#endif
10440
10441 rettv->vval.v_number = -1;
10442
10443#ifdef FEAT_QUICKFIX
10444 if (list_arg->v_type != VAR_LIST)
10445 EMSG(_(e_listreq));
10446 else
10447 {
10448 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010449 dict_T *d = NULL;
10450 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010451
10452 if (action_arg->v_type == VAR_STRING)
10453 {
10454 act = get_tv_string_chk(action_arg);
10455 if (act == NULL)
10456 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010457 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10458 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010459 action = *act;
10460 else
10461 EMSG2(_(e_invact), act);
10462 }
10463 else if (action_arg->v_type == VAR_UNKNOWN)
10464 action = ' ';
10465 else
10466 EMSG(_(e_stringreq));
10467
Bram Moolenaard823fa92016-08-12 16:29:27 +020010468 if (action_arg->v_type != VAR_UNKNOWN
10469 && what_arg->v_type != VAR_UNKNOWN)
10470 {
10471 if (what_arg->v_type == VAR_DICT)
10472 d = what_arg->vval.v_dict;
10473 else
10474 {
10475 EMSG(_(e_dictreq));
10476 valid_dict = FALSE;
10477 }
10478 }
10479
10480 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar8b62e312018-05-13 15:29:04 +020010481 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010482 rettv->vval.v_number = 0;
10483 }
10484#endif
10485}
10486
10487/*
10488 * "setloclist()" function
10489 */
10490 static void
10491f_setloclist(typval_T *argvars, typval_T *rettv)
10492{
10493 win_T *win;
10494
10495 rettv->vval.v_number = -1;
10496
10497 win = find_win_by_nr(&argvars[0], NULL);
10498 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010499 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010500}
10501
10502/*
10503 * "setmatches()" function
10504 */
10505 static void
10506f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10507{
10508#ifdef FEAT_SEARCH_EXTRA
10509 list_T *l;
10510 listitem_T *li;
10511 dict_T *d;
10512 list_T *s = NULL;
10513
10514 rettv->vval.v_number = -1;
10515 if (argvars[0].v_type != VAR_LIST)
10516 {
10517 EMSG(_(e_listreq));
10518 return;
10519 }
10520 if ((l = argvars[0].vval.v_list) != NULL)
10521 {
10522
10523 /* To some extent make sure that we are dealing with a list from
10524 * "getmatches()". */
10525 li = l->lv_first;
10526 while (li != NULL)
10527 {
10528 if (li->li_tv.v_type != VAR_DICT
10529 || (d = li->li_tv.vval.v_dict) == NULL)
10530 {
10531 EMSG(_(e_invarg));
10532 return;
10533 }
10534 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10535 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10536 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10537 && dict_find(d, (char_u *)"priority", -1) != NULL
10538 && dict_find(d, (char_u *)"id", -1) != NULL))
10539 {
10540 EMSG(_(e_invarg));
10541 return;
10542 }
10543 li = li->li_next;
10544 }
10545
10546 clear_matches(curwin);
10547 li = l->lv_first;
10548 while (li != NULL)
10549 {
10550 int i = 0;
10551 char_u buf[5];
10552 dictitem_T *di;
10553 char_u *group;
10554 int priority;
10555 int id;
10556 char_u *conceal;
10557
10558 d = li->li_tv.vval.v_dict;
10559 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10560 {
10561 if (s == NULL)
10562 {
10563 s = list_alloc();
10564 if (s == NULL)
10565 return;
10566 }
10567
10568 /* match from matchaddpos() */
10569 for (i = 1; i < 9; i++)
10570 {
10571 sprintf((char *)buf, (char *)"pos%d", i);
10572 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10573 {
10574 if (di->di_tv.v_type != VAR_LIST)
10575 return;
10576
10577 list_append_tv(s, &di->di_tv);
10578 s->lv_refcount++;
10579 }
10580 else
10581 break;
10582 }
10583 }
10584
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010585 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010586 priority = (int)get_dict_number(d, (char_u *)"priority");
10587 id = (int)get_dict_number(d, (char_u *)"id");
10588 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010589 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010590 : NULL;
10591 if (i == 0)
10592 {
10593 match_add(curwin, group,
10594 get_dict_string(d, (char_u *)"pattern", FALSE),
10595 priority, id, NULL, conceal);
10596 }
10597 else
10598 {
10599 match_add(curwin, group, NULL, priority, id, s, conceal);
10600 list_unref(s);
10601 s = NULL;
10602 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010603 vim_free(group);
10604 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010605
10606 li = li->li_next;
10607 }
10608 rettv->vval.v_number = 0;
10609 }
10610#endif
10611}
10612
10613/*
10614 * "setpos()" function
10615 */
10616 static void
10617f_setpos(typval_T *argvars, typval_T *rettv)
10618{
10619 pos_T pos;
10620 int fnum;
10621 char_u *name;
10622 colnr_T curswant = -1;
10623
10624 rettv->vval.v_number = -1;
10625 name = get_tv_string_chk(argvars);
10626 if (name != NULL)
10627 {
10628 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10629 {
10630 if (--pos.col < 0)
10631 pos.col = 0;
10632 if (name[0] == '.' && name[1] == NUL)
10633 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010634 /* set cursor; "fnum" is ignored */
10635 curwin->w_cursor = pos;
10636 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010637 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010638 curwin->w_curswant = curswant - 1;
10639 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010640 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010641 check_cursor();
10642 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010643 }
10644 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10645 {
10646 /* set mark */
10647 if (setmark_pos(name[1], &pos, fnum) == OK)
10648 rettv->vval.v_number = 0;
10649 }
10650 else
10651 EMSG(_(e_invarg));
10652 }
10653 }
10654}
10655
10656/*
10657 * "setqflist()" function
10658 */
10659 static void
10660f_setqflist(typval_T *argvars, typval_T *rettv)
10661{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010662 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010663}
10664
10665/*
10666 * "setreg()" function
10667 */
10668 static void
10669f_setreg(typval_T *argvars, typval_T *rettv)
10670{
10671 int regname;
10672 char_u *strregname;
10673 char_u *stropt;
10674 char_u *strval;
10675 int append;
10676 char_u yank_type;
10677 long block_len;
10678
10679 block_len = -1;
10680 yank_type = MAUTO;
10681 append = FALSE;
10682
10683 strregname = get_tv_string_chk(argvars);
10684 rettv->vval.v_number = 1; /* FAIL is default */
10685
10686 if (strregname == NULL)
10687 return; /* type error; errmsg already given */
10688 regname = *strregname;
10689 if (regname == 0 || regname == '@')
10690 regname = '"';
10691
10692 if (argvars[2].v_type != VAR_UNKNOWN)
10693 {
10694 stropt = get_tv_string_chk(&argvars[2]);
10695 if (stropt == NULL)
10696 return; /* type error */
10697 for (; *stropt != NUL; ++stropt)
10698 switch (*stropt)
10699 {
10700 case 'a': case 'A': /* append */
10701 append = TRUE;
10702 break;
10703 case 'v': case 'c': /* character-wise selection */
10704 yank_type = MCHAR;
10705 break;
10706 case 'V': case 'l': /* line-wise selection */
10707 yank_type = MLINE;
10708 break;
10709 case 'b': case Ctrl_V: /* block-wise selection */
10710 yank_type = MBLOCK;
10711 if (VIM_ISDIGIT(stropt[1]))
10712 {
10713 ++stropt;
10714 block_len = getdigits(&stropt) - 1;
10715 --stropt;
10716 }
10717 break;
10718 }
10719 }
10720
10721 if (argvars[1].v_type == VAR_LIST)
10722 {
10723 char_u **lstval;
10724 char_u **allocval;
10725 char_u buf[NUMBUFLEN];
10726 char_u **curval;
10727 char_u **curallocval;
10728 list_T *ll = argvars[1].vval.v_list;
10729 listitem_T *li;
10730 int len;
10731
10732 /* If the list is NULL handle like an empty list. */
10733 len = ll == NULL ? 0 : ll->lv_len;
10734
10735 /* First half: use for pointers to result lines; second half: use for
10736 * pointers to allocated copies. */
10737 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10738 if (lstval == NULL)
10739 return;
10740 curval = lstval;
10741 allocval = lstval + len + 2;
10742 curallocval = allocval;
10743
10744 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10745 li = li->li_next)
10746 {
10747 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10748 if (strval == NULL)
10749 goto free_lstval;
10750 if (strval == buf)
10751 {
10752 /* Need to make a copy, next get_tv_string_buf_chk() will
10753 * overwrite the string. */
10754 strval = vim_strsave(buf);
10755 if (strval == NULL)
10756 goto free_lstval;
10757 *curallocval++ = strval;
10758 }
10759 *curval++ = strval;
10760 }
10761 *curval++ = NULL;
10762
10763 write_reg_contents_lst(regname, lstval, -1,
10764 append, yank_type, block_len);
10765free_lstval:
10766 while (curallocval > allocval)
10767 vim_free(*--curallocval);
10768 vim_free(lstval);
10769 }
10770 else
10771 {
10772 strval = get_tv_string_chk(&argvars[1]);
10773 if (strval == NULL)
10774 return;
10775 write_reg_contents_ex(regname, strval, -1,
10776 append, yank_type, block_len);
10777 }
10778 rettv->vval.v_number = 0;
10779}
10780
10781/*
10782 * "settabvar()" function
10783 */
10784 static void
10785f_settabvar(typval_T *argvars, typval_T *rettv)
10786{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010787 tabpage_T *save_curtab;
10788 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010789 char_u *varname, *tabvarname;
10790 typval_T *varp;
10791
10792 rettv->vval.v_number = 0;
10793
10794 if (check_restricted() || check_secure())
10795 return;
10796
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010797 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010798 varname = get_tv_string_chk(&argvars[1]);
10799 varp = &argvars[2];
10800
Bram Moolenaar4033c552017-09-16 20:54:51 +020010801 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010802 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010803 save_curtab = curtab;
10804 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010805
10806 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10807 if (tabvarname != NULL)
10808 {
10809 STRCPY(tabvarname, "t:");
10810 STRCPY(tabvarname + 2, varname);
10811 set_var(tabvarname, varp, TRUE);
10812 vim_free(tabvarname);
10813 }
10814
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010815 /* Restore current tabpage */
10816 if (valid_tabpage(save_curtab))
10817 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010818 }
10819}
10820
10821/*
10822 * "settabwinvar()" function
10823 */
10824 static void
10825f_settabwinvar(typval_T *argvars, typval_T *rettv)
10826{
10827 setwinvar(argvars, rettv, 1);
10828}
10829
10830/*
10831 * "setwinvar()" function
10832 */
10833 static void
10834f_setwinvar(typval_T *argvars, typval_T *rettv)
10835{
10836 setwinvar(argvars, rettv, 0);
10837}
10838
10839#ifdef FEAT_CRYPT
10840/*
10841 * "sha256({string})" function
10842 */
10843 static void
10844f_sha256(typval_T *argvars, typval_T *rettv)
10845{
10846 char_u *p;
10847
10848 p = get_tv_string(&argvars[0]);
10849 rettv->vval.v_string = vim_strsave(
10850 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10851 rettv->v_type = VAR_STRING;
10852}
10853#endif /* FEAT_CRYPT */
10854
10855/*
10856 * "shellescape({string})" function
10857 */
10858 static void
10859f_shellescape(typval_T *argvars, typval_T *rettv)
10860{
Bram Moolenaar20615522017-06-05 18:46:26 +020010861 int do_special = non_zero_arg(&argvars[1]);
10862
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010863 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010864 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010865 rettv->v_type = VAR_STRING;
10866}
10867
10868/*
10869 * shiftwidth() function
10870 */
10871 static void
10872f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10873{
10874 rettv->vval.v_number = get_sw_value(curbuf);
10875}
10876
10877/*
10878 * "simplify()" function
10879 */
10880 static void
10881f_simplify(typval_T *argvars, typval_T *rettv)
10882{
10883 char_u *p;
10884
10885 p = get_tv_string(&argvars[0]);
10886 rettv->vval.v_string = vim_strsave(p);
10887 simplify_filename(rettv->vval.v_string); /* simplify in place */
10888 rettv->v_type = VAR_STRING;
10889}
10890
10891#ifdef FEAT_FLOAT
10892/*
10893 * "sin()" function
10894 */
10895 static void
10896f_sin(typval_T *argvars, typval_T *rettv)
10897{
10898 float_T f = 0.0;
10899
10900 rettv->v_type = VAR_FLOAT;
10901 if (get_float_arg(argvars, &f) == OK)
10902 rettv->vval.v_float = sin(f);
10903 else
10904 rettv->vval.v_float = 0.0;
10905}
10906
10907/*
10908 * "sinh()" function
10909 */
10910 static void
10911f_sinh(typval_T *argvars, typval_T *rettv)
10912{
10913 float_T f = 0.0;
10914
10915 rettv->v_type = VAR_FLOAT;
10916 if (get_float_arg(argvars, &f) == OK)
10917 rettv->vval.v_float = sinh(f);
10918 else
10919 rettv->vval.v_float = 0.0;
10920}
10921#endif
10922
10923static int
10924#ifdef __BORLANDC__
10925 _RTLENTRYF
10926#endif
10927 item_compare(const void *s1, const void *s2);
10928static int
10929#ifdef __BORLANDC__
10930 _RTLENTRYF
10931#endif
10932 item_compare2(const void *s1, const void *s2);
10933
10934/* struct used in the array that's given to qsort() */
10935typedef struct
10936{
10937 listitem_T *item;
10938 int idx;
10939} sortItem_T;
10940
10941/* struct storing information about current sort */
10942typedef struct
10943{
10944 int item_compare_ic;
10945 int item_compare_numeric;
10946 int item_compare_numbers;
10947#ifdef FEAT_FLOAT
10948 int item_compare_float;
10949#endif
10950 char_u *item_compare_func;
10951 partial_T *item_compare_partial;
10952 dict_T *item_compare_selfdict;
10953 int item_compare_func_err;
10954 int item_compare_keep_zero;
10955} sortinfo_T;
10956static sortinfo_T *sortinfo = NULL;
10957static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10958#define ITEM_COMPARE_FAIL 999
10959
10960/*
10961 * Compare functions for f_sort() and f_uniq() below.
10962 */
10963 static int
10964#ifdef __BORLANDC__
10965_RTLENTRYF
10966#endif
10967item_compare(const void *s1, const void *s2)
10968{
10969 sortItem_T *si1, *si2;
10970 typval_T *tv1, *tv2;
10971 char_u *p1, *p2;
10972 char_u *tofree1 = NULL, *tofree2 = NULL;
10973 int res;
10974 char_u numbuf1[NUMBUFLEN];
10975 char_u numbuf2[NUMBUFLEN];
10976
10977 si1 = (sortItem_T *)s1;
10978 si2 = (sortItem_T *)s2;
10979 tv1 = &si1->item->li_tv;
10980 tv2 = &si2->item->li_tv;
10981
10982 if (sortinfo->item_compare_numbers)
10983 {
10984 varnumber_T v1 = get_tv_number(tv1);
10985 varnumber_T v2 = get_tv_number(tv2);
10986
10987 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10988 }
10989
10990#ifdef FEAT_FLOAT
10991 if (sortinfo->item_compare_float)
10992 {
10993 float_T v1 = get_tv_float(tv1);
10994 float_T v2 = get_tv_float(tv2);
10995
10996 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10997 }
10998#endif
10999
11000 /* tv2string() puts quotes around a string and allocates memory. Don't do
11001 * that for string variables. Use a single quote when comparing with a
11002 * non-string to do what the docs promise. */
11003 if (tv1->v_type == VAR_STRING)
11004 {
11005 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11006 p1 = (char_u *)"'";
11007 else
11008 p1 = tv1->vval.v_string;
11009 }
11010 else
11011 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11012 if (tv2->v_type == VAR_STRING)
11013 {
11014 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11015 p2 = (char_u *)"'";
11016 else
11017 p2 = tv2->vval.v_string;
11018 }
11019 else
11020 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11021 if (p1 == NULL)
11022 p1 = (char_u *)"";
11023 if (p2 == NULL)
11024 p2 = (char_u *)"";
11025 if (!sortinfo->item_compare_numeric)
11026 {
11027 if (sortinfo->item_compare_ic)
11028 res = STRICMP(p1, p2);
11029 else
11030 res = STRCMP(p1, p2);
11031 }
11032 else
11033 {
11034 double n1, n2;
11035 n1 = strtod((char *)p1, (char **)&p1);
11036 n2 = strtod((char *)p2, (char **)&p2);
11037 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11038 }
11039
11040 /* When the result would be zero, compare the item indexes. Makes the
11041 * sort stable. */
11042 if (res == 0 && !sortinfo->item_compare_keep_zero)
11043 res = si1->idx > si2->idx ? 1 : -1;
11044
11045 vim_free(tofree1);
11046 vim_free(tofree2);
11047 return res;
11048}
11049
11050 static int
11051#ifdef __BORLANDC__
11052_RTLENTRYF
11053#endif
11054item_compare2(const void *s1, const void *s2)
11055{
11056 sortItem_T *si1, *si2;
11057 int res;
11058 typval_T rettv;
11059 typval_T argv[3];
11060 int dummy;
11061 char_u *func_name;
11062 partial_T *partial = sortinfo->item_compare_partial;
11063
11064 /* shortcut after failure in previous call; compare all items equal */
11065 if (sortinfo->item_compare_func_err)
11066 return 0;
11067
11068 si1 = (sortItem_T *)s1;
11069 si2 = (sortItem_T *)s2;
11070
11071 if (partial == NULL)
11072 func_name = sortinfo->item_compare_func;
11073 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011074 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011075
11076 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11077 * in the copy without changing the original list items. */
11078 copy_tv(&si1->item->li_tv, &argv[0]);
11079 copy_tv(&si2->item->li_tv, &argv[1]);
11080
11081 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11082 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011083 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011084 partial, sortinfo->item_compare_selfdict);
11085 clear_tv(&argv[0]);
11086 clear_tv(&argv[1]);
11087
11088 if (res == FAIL)
11089 res = ITEM_COMPARE_FAIL;
11090 else
11091 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11092 if (sortinfo->item_compare_func_err)
11093 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11094 clear_tv(&rettv);
11095
11096 /* When the result would be zero, compare the pointers themselves. Makes
11097 * the sort stable. */
11098 if (res == 0 && !sortinfo->item_compare_keep_zero)
11099 res = si1->idx > si2->idx ? 1 : -1;
11100
11101 return res;
11102}
11103
11104/*
11105 * "sort({list})" function
11106 */
11107 static void
11108do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11109{
11110 list_T *l;
11111 listitem_T *li;
11112 sortItem_T *ptrs;
11113 sortinfo_T *old_sortinfo;
11114 sortinfo_T info;
11115 long len;
11116 long i;
11117
11118 /* Pointer to current info struct used in compare function. Save and
11119 * restore the current one for nested calls. */
11120 old_sortinfo = sortinfo;
11121 sortinfo = &info;
11122
11123 if (argvars[0].v_type != VAR_LIST)
11124 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11125 else
11126 {
11127 l = argvars[0].vval.v_list;
11128 if (l == NULL || tv_check_lock(l->lv_lock,
11129 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11130 TRUE))
11131 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011132 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011133
11134 len = list_len(l);
11135 if (len <= 1)
11136 goto theend; /* short list sorts pretty quickly */
11137
11138 info.item_compare_ic = FALSE;
11139 info.item_compare_numeric = FALSE;
11140 info.item_compare_numbers = FALSE;
11141#ifdef FEAT_FLOAT
11142 info.item_compare_float = FALSE;
11143#endif
11144 info.item_compare_func = NULL;
11145 info.item_compare_partial = NULL;
11146 info.item_compare_selfdict = NULL;
11147 if (argvars[1].v_type != VAR_UNKNOWN)
11148 {
11149 /* optional second argument: {func} */
11150 if (argvars[1].v_type == VAR_FUNC)
11151 info.item_compare_func = argvars[1].vval.v_string;
11152 else if (argvars[1].v_type == VAR_PARTIAL)
11153 info.item_compare_partial = argvars[1].vval.v_partial;
11154 else
11155 {
11156 int error = FALSE;
11157
11158 i = (long)get_tv_number_chk(&argvars[1], &error);
11159 if (error)
11160 goto theend; /* type error; errmsg already given */
11161 if (i == 1)
11162 info.item_compare_ic = TRUE;
11163 else if (argvars[1].v_type != VAR_NUMBER)
11164 info.item_compare_func = get_tv_string(&argvars[1]);
11165 else if (i != 0)
11166 {
11167 EMSG(_(e_invarg));
11168 goto theend;
11169 }
11170 if (info.item_compare_func != NULL)
11171 {
11172 if (*info.item_compare_func == NUL)
11173 {
11174 /* empty string means default sort */
11175 info.item_compare_func = NULL;
11176 }
11177 else if (STRCMP(info.item_compare_func, "n") == 0)
11178 {
11179 info.item_compare_func = NULL;
11180 info.item_compare_numeric = TRUE;
11181 }
11182 else if (STRCMP(info.item_compare_func, "N") == 0)
11183 {
11184 info.item_compare_func = NULL;
11185 info.item_compare_numbers = TRUE;
11186 }
11187#ifdef FEAT_FLOAT
11188 else if (STRCMP(info.item_compare_func, "f") == 0)
11189 {
11190 info.item_compare_func = NULL;
11191 info.item_compare_float = TRUE;
11192 }
11193#endif
11194 else if (STRCMP(info.item_compare_func, "i") == 0)
11195 {
11196 info.item_compare_func = NULL;
11197 info.item_compare_ic = TRUE;
11198 }
11199 }
11200 }
11201
11202 if (argvars[2].v_type != VAR_UNKNOWN)
11203 {
11204 /* optional third argument: {dict} */
11205 if (argvars[2].v_type != VAR_DICT)
11206 {
11207 EMSG(_(e_dictreq));
11208 goto theend;
11209 }
11210 info.item_compare_selfdict = argvars[2].vval.v_dict;
11211 }
11212 }
11213
11214 /* Make an array with each entry pointing to an item in the List. */
11215 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11216 if (ptrs == NULL)
11217 goto theend;
11218
11219 i = 0;
11220 if (sort)
11221 {
11222 /* sort(): ptrs will be the list to sort */
11223 for (li = l->lv_first; li != NULL; li = li->li_next)
11224 {
11225 ptrs[i].item = li;
11226 ptrs[i].idx = i;
11227 ++i;
11228 }
11229
11230 info.item_compare_func_err = FALSE;
11231 info.item_compare_keep_zero = FALSE;
11232 /* test the compare function */
11233 if ((info.item_compare_func != NULL
11234 || info.item_compare_partial != NULL)
11235 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11236 == ITEM_COMPARE_FAIL)
11237 EMSG(_("E702: Sort compare function failed"));
11238 else
11239 {
11240 /* Sort the array with item pointers. */
11241 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11242 info.item_compare_func == NULL
11243 && info.item_compare_partial == NULL
11244 ? item_compare : item_compare2);
11245
11246 if (!info.item_compare_func_err)
11247 {
11248 /* Clear the List and append the items in sorted order. */
11249 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11250 l->lv_len = 0;
11251 for (i = 0; i < len; ++i)
11252 list_append(l, ptrs[i].item);
11253 }
11254 }
11255 }
11256 else
11257 {
11258 int (*item_compare_func_ptr)(const void *, const void *);
11259
11260 /* f_uniq(): ptrs will be a stack of items to remove */
11261 info.item_compare_func_err = FALSE;
11262 info.item_compare_keep_zero = TRUE;
11263 item_compare_func_ptr = info.item_compare_func != NULL
11264 || info.item_compare_partial != NULL
11265 ? item_compare2 : item_compare;
11266
11267 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11268 li = li->li_next)
11269 {
11270 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11271 == 0)
11272 ptrs[i++].item = li;
11273 if (info.item_compare_func_err)
11274 {
11275 EMSG(_("E882: Uniq compare function failed"));
11276 break;
11277 }
11278 }
11279
11280 if (!info.item_compare_func_err)
11281 {
11282 while (--i >= 0)
11283 {
11284 li = ptrs[i].item->li_next;
11285 ptrs[i].item->li_next = li->li_next;
11286 if (li->li_next != NULL)
11287 li->li_next->li_prev = ptrs[i].item;
11288 else
11289 l->lv_last = ptrs[i].item;
11290 list_fix_watch(l, li);
11291 listitem_free(li);
11292 l->lv_len--;
11293 }
11294 }
11295 }
11296
11297 vim_free(ptrs);
11298 }
11299theend:
11300 sortinfo = old_sortinfo;
11301}
11302
11303/*
11304 * "sort({list})" function
11305 */
11306 static void
11307f_sort(typval_T *argvars, typval_T *rettv)
11308{
11309 do_sort_uniq(argvars, rettv, TRUE);
11310}
11311
11312/*
11313 * "uniq({list})" function
11314 */
11315 static void
11316f_uniq(typval_T *argvars, typval_T *rettv)
11317{
11318 do_sort_uniq(argvars, rettv, FALSE);
11319}
11320
11321/*
11322 * "soundfold({word})" function
11323 */
11324 static void
11325f_soundfold(typval_T *argvars, typval_T *rettv)
11326{
11327 char_u *s;
11328
11329 rettv->v_type = VAR_STRING;
11330 s = get_tv_string(&argvars[0]);
11331#ifdef FEAT_SPELL
11332 rettv->vval.v_string = eval_soundfold(s);
11333#else
11334 rettv->vval.v_string = vim_strsave(s);
11335#endif
11336}
11337
11338/*
11339 * "spellbadword()" function
11340 */
11341 static void
11342f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11343{
11344 char_u *word = (char_u *)"";
11345 hlf_T attr = HLF_COUNT;
11346 int len = 0;
11347
11348 if (rettv_list_alloc(rettv) == FAIL)
11349 return;
11350
11351#ifdef FEAT_SPELL
11352 if (argvars[0].v_type == VAR_UNKNOWN)
11353 {
11354 /* Find the start and length of the badly spelled word. */
11355 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11356 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011357 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011358 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011359 curwin->w_set_curswant = TRUE;
11360 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011361 }
11362 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11363 {
11364 char_u *str = get_tv_string_chk(&argvars[0]);
11365 int capcol = -1;
11366
11367 if (str != NULL)
11368 {
11369 /* Check the argument for spelling. */
11370 while (*str != NUL)
11371 {
11372 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11373 if (attr != HLF_COUNT)
11374 {
11375 word = str;
11376 break;
11377 }
11378 str += len;
11379 }
11380 }
11381 }
11382#endif
11383
11384 list_append_string(rettv->vval.v_list, word, len);
11385 list_append_string(rettv->vval.v_list, (char_u *)(
11386 attr == HLF_SPB ? "bad" :
11387 attr == HLF_SPR ? "rare" :
11388 attr == HLF_SPL ? "local" :
11389 attr == HLF_SPC ? "caps" :
11390 ""), -1);
11391}
11392
11393/*
11394 * "spellsuggest()" function
11395 */
11396 static void
11397f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11398{
11399#ifdef FEAT_SPELL
11400 char_u *str;
11401 int typeerr = FALSE;
11402 int maxcount;
11403 garray_T ga;
11404 int i;
11405 listitem_T *li;
11406 int need_capital = FALSE;
11407#endif
11408
11409 if (rettv_list_alloc(rettv) == FAIL)
11410 return;
11411
11412#ifdef FEAT_SPELL
11413 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11414 {
11415 str = get_tv_string(&argvars[0]);
11416 if (argvars[1].v_type != VAR_UNKNOWN)
11417 {
11418 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11419 if (maxcount <= 0)
11420 return;
11421 if (argvars[2].v_type != VAR_UNKNOWN)
11422 {
11423 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11424 if (typeerr)
11425 return;
11426 }
11427 }
11428 else
11429 maxcount = 25;
11430
11431 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11432
11433 for (i = 0; i < ga.ga_len; ++i)
11434 {
11435 str = ((char_u **)ga.ga_data)[i];
11436
11437 li = listitem_alloc();
11438 if (li == NULL)
11439 vim_free(str);
11440 else
11441 {
11442 li->li_tv.v_type = VAR_STRING;
11443 li->li_tv.v_lock = 0;
11444 li->li_tv.vval.v_string = str;
11445 list_append(rettv->vval.v_list, li);
11446 }
11447 }
11448 ga_clear(&ga);
11449 }
11450#endif
11451}
11452
11453 static void
11454f_split(typval_T *argvars, typval_T *rettv)
11455{
11456 char_u *str;
11457 char_u *end;
11458 char_u *pat = NULL;
11459 regmatch_T regmatch;
11460 char_u patbuf[NUMBUFLEN];
11461 char_u *save_cpo;
11462 int match;
11463 colnr_T col = 0;
11464 int keepempty = FALSE;
11465 int typeerr = FALSE;
11466
11467 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11468 save_cpo = p_cpo;
11469 p_cpo = (char_u *)"";
11470
11471 str = get_tv_string(&argvars[0]);
11472 if (argvars[1].v_type != VAR_UNKNOWN)
11473 {
11474 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11475 if (pat == NULL)
11476 typeerr = TRUE;
11477 if (argvars[2].v_type != VAR_UNKNOWN)
11478 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11479 }
11480 if (pat == NULL || *pat == NUL)
11481 pat = (char_u *)"[\\x01- ]\\+";
11482
11483 if (rettv_list_alloc(rettv) == FAIL)
11484 return;
11485 if (typeerr)
11486 return;
11487
11488 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11489 if (regmatch.regprog != NULL)
11490 {
11491 regmatch.rm_ic = FALSE;
11492 while (*str != NUL || keepempty)
11493 {
11494 if (*str == NUL)
11495 match = FALSE; /* empty item at the end */
11496 else
11497 match = vim_regexec_nl(&regmatch, str, col);
11498 if (match)
11499 end = regmatch.startp[0];
11500 else
11501 end = str + STRLEN(str);
11502 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11503 && *str != NUL && match && end < regmatch.endp[0]))
11504 {
11505 if (list_append_string(rettv->vval.v_list, str,
11506 (int)(end - str)) == FAIL)
11507 break;
11508 }
11509 if (!match)
11510 break;
11511 /* Advance to just after the match. */
11512 if (regmatch.endp[0] > str)
11513 col = 0;
11514 else
11515 {
11516 /* Don't get stuck at the same match. */
11517#ifdef FEAT_MBYTE
11518 col = (*mb_ptr2len)(regmatch.endp[0]);
11519#else
11520 col = 1;
11521#endif
11522 }
11523 str = regmatch.endp[0];
11524 }
11525
11526 vim_regfree(regmatch.regprog);
11527 }
11528
11529 p_cpo = save_cpo;
11530}
11531
11532#ifdef FEAT_FLOAT
11533/*
11534 * "sqrt()" function
11535 */
11536 static void
11537f_sqrt(typval_T *argvars, typval_T *rettv)
11538{
11539 float_T f = 0.0;
11540
11541 rettv->v_type = VAR_FLOAT;
11542 if (get_float_arg(argvars, &f) == OK)
11543 rettv->vval.v_float = sqrt(f);
11544 else
11545 rettv->vval.v_float = 0.0;
11546}
11547
11548/*
11549 * "str2float()" function
11550 */
11551 static void
11552f_str2float(typval_T *argvars, typval_T *rettv)
11553{
11554 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011555 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011556
Bram Moolenaar08243d22017-01-10 16:12:29 +010011557 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011558 p = skipwhite(p + 1);
11559 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011560 if (isneg)
11561 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011562 rettv->v_type = VAR_FLOAT;
11563}
11564#endif
11565
11566/*
11567 * "str2nr()" function
11568 */
11569 static void
11570f_str2nr(typval_T *argvars, typval_T *rettv)
11571{
11572 int base = 10;
11573 char_u *p;
11574 varnumber_T n;
11575 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011576 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011577
11578 if (argvars[1].v_type != VAR_UNKNOWN)
11579 {
11580 base = (int)get_tv_number(&argvars[1]);
11581 if (base != 2 && base != 8 && base != 10 && base != 16)
11582 {
11583 EMSG(_(e_invarg));
11584 return;
11585 }
11586 }
11587
11588 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011589 isneg = (*p == '-');
11590 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011591 p = skipwhite(p + 1);
11592 switch (base)
11593 {
11594 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11595 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11596 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11597 default: what = 0;
11598 }
11599 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011600 if (isneg)
11601 rettv->vval.v_number = -n;
11602 else
11603 rettv->vval.v_number = n;
11604
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011605}
11606
11607#ifdef HAVE_STRFTIME
11608/*
11609 * "strftime({format}[, {time}])" function
11610 */
11611 static void
11612f_strftime(typval_T *argvars, typval_T *rettv)
11613{
11614 char_u result_buf[256];
11615 struct tm *curtime;
11616 time_t seconds;
11617 char_u *p;
11618
11619 rettv->v_type = VAR_STRING;
11620
11621 p = get_tv_string(&argvars[0]);
11622 if (argvars[1].v_type == VAR_UNKNOWN)
11623 seconds = time(NULL);
11624 else
11625 seconds = (time_t)get_tv_number(&argvars[1]);
11626 curtime = localtime(&seconds);
11627 /* MSVC returns NULL for an invalid value of seconds. */
11628 if (curtime == NULL)
11629 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11630 else
11631 {
11632# ifdef FEAT_MBYTE
11633 vimconv_T conv;
11634 char_u *enc;
11635
11636 conv.vc_type = CONV_NONE;
11637 enc = enc_locale();
11638 convert_setup(&conv, p_enc, enc);
11639 if (conv.vc_type != CONV_NONE)
11640 p = string_convert(&conv, p, NULL);
11641# endif
11642 if (p != NULL)
11643 (void)strftime((char *)result_buf, sizeof(result_buf),
11644 (char *)p, curtime);
11645 else
11646 result_buf[0] = NUL;
11647
11648# ifdef FEAT_MBYTE
11649 if (conv.vc_type != CONV_NONE)
11650 vim_free(p);
11651 convert_setup(&conv, enc, p_enc);
11652 if (conv.vc_type != CONV_NONE)
11653 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11654 else
11655# endif
11656 rettv->vval.v_string = vim_strsave(result_buf);
11657
11658# ifdef FEAT_MBYTE
11659 /* Release conversion descriptors */
11660 convert_setup(&conv, NULL, NULL);
11661 vim_free(enc);
11662# endif
11663 }
11664}
11665#endif
11666
11667/*
11668 * "strgetchar()" function
11669 */
11670 static void
11671f_strgetchar(typval_T *argvars, typval_T *rettv)
11672{
11673 char_u *str;
11674 int len;
11675 int error = FALSE;
11676 int charidx;
11677
11678 rettv->vval.v_number = -1;
11679 str = get_tv_string_chk(&argvars[0]);
11680 if (str == NULL)
11681 return;
11682 len = (int)STRLEN(str);
11683 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11684 if (error)
11685 return;
11686#ifdef FEAT_MBYTE
11687 {
11688 int byteidx = 0;
11689
11690 while (charidx >= 0 && byteidx < len)
11691 {
11692 if (charidx == 0)
11693 {
11694 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11695 break;
11696 }
11697 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011698 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011699 }
11700 }
11701#else
11702 if (charidx < len)
11703 rettv->vval.v_number = str[charidx];
11704#endif
11705}
11706
11707/*
11708 * "stridx()" function
11709 */
11710 static void
11711f_stridx(typval_T *argvars, typval_T *rettv)
11712{
11713 char_u buf[NUMBUFLEN];
11714 char_u *needle;
11715 char_u *haystack;
11716 char_u *save_haystack;
11717 char_u *pos;
11718 int start_idx;
11719
11720 needle = get_tv_string_chk(&argvars[1]);
11721 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11722 rettv->vval.v_number = -1;
11723 if (needle == NULL || haystack == NULL)
11724 return; /* type error; errmsg already given */
11725
11726 if (argvars[2].v_type != VAR_UNKNOWN)
11727 {
11728 int error = FALSE;
11729
11730 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11731 if (error || start_idx >= (int)STRLEN(haystack))
11732 return;
11733 if (start_idx >= 0)
11734 haystack += start_idx;
11735 }
11736
11737 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11738 if (pos != NULL)
11739 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11740}
11741
11742/*
11743 * "string()" function
11744 */
11745 static void
11746f_string(typval_T *argvars, typval_T *rettv)
11747{
11748 char_u *tofree;
11749 char_u numbuf[NUMBUFLEN];
11750
11751 rettv->v_type = VAR_STRING;
11752 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11753 get_copyID());
11754 /* Make a copy if we have a value but it's not in allocated memory. */
11755 if (rettv->vval.v_string != NULL && tofree == NULL)
11756 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11757}
11758
11759/*
11760 * "strlen()" function
11761 */
11762 static void
11763f_strlen(typval_T *argvars, typval_T *rettv)
11764{
11765 rettv->vval.v_number = (varnumber_T)(STRLEN(
11766 get_tv_string(&argvars[0])));
11767}
11768
11769/*
11770 * "strchars()" function
11771 */
11772 static void
11773f_strchars(typval_T *argvars, typval_T *rettv)
11774{
11775 char_u *s = get_tv_string(&argvars[0]);
11776 int skipcc = 0;
11777#ifdef FEAT_MBYTE
11778 varnumber_T len = 0;
11779 int (*func_mb_ptr2char_adv)(char_u **pp);
11780#endif
11781
11782 if (argvars[1].v_type != VAR_UNKNOWN)
11783 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11784 if (skipcc < 0 || skipcc > 1)
11785 EMSG(_(e_invarg));
11786 else
11787 {
11788#ifdef FEAT_MBYTE
11789 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11790 while (*s != NUL)
11791 {
11792 func_mb_ptr2char_adv(&s);
11793 ++len;
11794 }
11795 rettv->vval.v_number = len;
11796#else
11797 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11798#endif
11799 }
11800}
11801
11802/*
11803 * "strdisplaywidth()" function
11804 */
11805 static void
11806f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11807{
11808 char_u *s = get_tv_string(&argvars[0]);
11809 int col = 0;
11810
11811 if (argvars[1].v_type != VAR_UNKNOWN)
11812 col = (int)get_tv_number(&argvars[1]);
11813
11814 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11815}
11816
11817/*
11818 * "strwidth()" function
11819 */
11820 static void
11821f_strwidth(typval_T *argvars, typval_T *rettv)
11822{
11823 char_u *s = get_tv_string(&argvars[0]);
11824
11825 rettv->vval.v_number = (varnumber_T)(
11826#ifdef FEAT_MBYTE
11827 mb_string2cells(s, -1)
11828#else
11829 STRLEN(s)
11830#endif
11831 );
11832}
11833
11834/*
11835 * "strcharpart()" function
11836 */
11837 static void
11838f_strcharpart(typval_T *argvars, typval_T *rettv)
11839{
11840#ifdef FEAT_MBYTE
11841 char_u *p;
11842 int nchar;
11843 int nbyte = 0;
11844 int charlen;
11845 int len = 0;
11846 int slen;
11847 int error = FALSE;
11848
11849 p = get_tv_string(&argvars[0]);
11850 slen = (int)STRLEN(p);
11851
11852 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11853 if (!error)
11854 {
11855 if (nchar > 0)
11856 while (nchar > 0 && nbyte < slen)
11857 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011858 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011859 --nchar;
11860 }
11861 else
11862 nbyte = nchar;
11863 if (argvars[2].v_type != VAR_UNKNOWN)
11864 {
11865 charlen = (int)get_tv_number(&argvars[2]);
11866 while (charlen > 0 && nbyte + len < slen)
11867 {
11868 int off = nbyte + len;
11869
11870 if (off < 0)
11871 len += 1;
11872 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011873 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011874 --charlen;
11875 }
11876 }
11877 else
11878 len = slen - nbyte; /* default: all bytes that are available. */
11879 }
11880
11881 /*
11882 * Only return the overlap between the specified part and the actual
11883 * string.
11884 */
11885 if (nbyte < 0)
11886 {
11887 len += nbyte;
11888 nbyte = 0;
11889 }
11890 else if (nbyte > slen)
11891 nbyte = slen;
11892 if (len < 0)
11893 len = 0;
11894 else if (nbyte + len > slen)
11895 len = slen - nbyte;
11896
11897 rettv->v_type = VAR_STRING;
11898 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11899#else
11900 f_strpart(argvars, rettv);
11901#endif
11902}
11903
11904/*
11905 * "strpart()" function
11906 */
11907 static void
11908f_strpart(typval_T *argvars, typval_T *rettv)
11909{
11910 char_u *p;
11911 int n;
11912 int len;
11913 int slen;
11914 int error = FALSE;
11915
11916 p = get_tv_string(&argvars[0]);
11917 slen = (int)STRLEN(p);
11918
11919 n = (int)get_tv_number_chk(&argvars[1], &error);
11920 if (error)
11921 len = 0;
11922 else if (argvars[2].v_type != VAR_UNKNOWN)
11923 len = (int)get_tv_number(&argvars[2]);
11924 else
11925 len = slen - n; /* default len: all bytes that are available. */
11926
11927 /*
11928 * Only return the overlap between the specified part and the actual
11929 * string.
11930 */
11931 if (n < 0)
11932 {
11933 len += n;
11934 n = 0;
11935 }
11936 else if (n > slen)
11937 n = slen;
11938 if (len < 0)
11939 len = 0;
11940 else if (n + len > slen)
11941 len = slen - n;
11942
11943 rettv->v_type = VAR_STRING;
11944 rettv->vval.v_string = vim_strnsave(p + n, len);
11945}
11946
11947/*
11948 * "strridx()" function
11949 */
11950 static void
11951f_strridx(typval_T *argvars, typval_T *rettv)
11952{
11953 char_u buf[NUMBUFLEN];
11954 char_u *needle;
11955 char_u *haystack;
11956 char_u *rest;
11957 char_u *lastmatch = NULL;
11958 int haystack_len, end_idx;
11959
11960 needle = get_tv_string_chk(&argvars[1]);
11961 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11962
11963 rettv->vval.v_number = -1;
11964 if (needle == NULL || haystack == NULL)
11965 return; /* type error; errmsg already given */
11966
11967 haystack_len = (int)STRLEN(haystack);
11968 if (argvars[2].v_type != VAR_UNKNOWN)
11969 {
11970 /* Third argument: upper limit for index */
11971 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11972 if (end_idx < 0)
11973 return; /* can never find a match */
11974 }
11975 else
11976 end_idx = haystack_len;
11977
11978 if (*needle == NUL)
11979 {
11980 /* Empty string matches past the end. */
11981 lastmatch = haystack + end_idx;
11982 }
11983 else
11984 {
11985 for (rest = haystack; *rest != '\0'; ++rest)
11986 {
11987 rest = (char_u *)strstr((char *)rest, (char *)needle);
11988 if (rest == NULL || rest > haystack + end_idx)
11989 break;
11990 lastmatch = rest;
11991 }
11992 }
11993
11994 if (lastmatch == NULL)
11995 rettv->vval.v_number = -1;
11996 else
11997 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11998}
11999
12000/*
12001 * "strtrans()" function
12002 */
12003 static void
12004f_strtrans(typval_T *argvars, typval_T *rettv)
12005{
12006 rettv->v_type = VAR_STRING;
12007 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12008}
12009
12010/*
12011 * "submatch()" function
12012 */
12013 static void
12014f_submatch(typval_T *argvars, typval_T *rettv)
12015{
12016 int error = FALSE;
12017 int no;
12018 int retList = 0;
12019
12020 no = (int)get_tv_number_chk(&argvars[0], &error);
12021 if (error)
12022 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012023 if (no < 0 || no >= NSUBEXP)
12024 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012025 EMSGN(_("E935: invalid submatch number: %d"), no);
12026 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012027 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012028 if (argvars[1].v_type != VAR_UNKNOWN)
12029 retList = (int)get_tv_number_chk(&argvars[1], &error);
12030 if (error)
12031 return;
12032
12033 if (retList == 0)
12034 {
12035 rettv->v_type = VAR_STRING;
12036 rettv->vval.v_string = reg_submatch(no);
12037 }
12038 else
12039 {
12040 rettv->v_type = VAR_LIST;
12041 rettv->vval.v_list = reg_submatch_list(no);
12042 }
12043}
12044
12045/*
12046 * "substitute()" function
12047 */
12048 static void
12049f_substitute(typval_T *argvars, typval_T *rettv)
12050{
12051 char_u patbuf[NUMBUFLEN];
12052 char_u subbuf[NUMBUFLEN];
12053 char_u flagsbuf[NUMBUFLEN];
12054
12055 char_u *str = get_tv_string_chk(&argvars[0]);
12056 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012057 char_u *sub = NULL;
12058 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012059 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12060
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012061 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12062 expr = &argvars[2];
12063 else
12064 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12065
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012066 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012067 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12068 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012069 rettv->vval.v_string = NULL;
12070 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012071 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012072}
12073
12074/*
12075 * "synID(lnum, col, trans)" function
12076 */
12077 static void
12078f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12079{
12080 int id = 0;
12081#ifdef FEAT_SYN_HL
12082 linenr_T lnum;
12083 colnr_T col;
12084 int trans;
12085 int transerr = FALSE;
12086
12087 lnum = get_tv_lnum(argvars); /* -1 on type error */
12088 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12089 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12090
12091 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12092 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12093 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12094#endif
12095
12096 rettv->vval.v_number = id;
12097}
12098
12099/*
12100 * "synIDattr(id, what [, mode])" function
12101 */
12102 static void
12103f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12104{
12105 char_u *p = NULL;
12106#ifdef FEAT_SYN_HL
12107 int id;
12108 char_u *what;
12109 char_u *mode;
12110 char_u modebuf[NUMBUFLEN];
12111 int modec;
12112
12113 id = (int)get_tv_number(&argvars[0]);
12114 what = get_tv_string(&argvars[1]);
12115 if (argvars[2].v_type != VAR_UNKNOWN)
12116 {
12117 mode = get_tv_string_buf(&argvars[2], modebuf);
12118 modec = TOLOWER_ASC(mode[0]);
12119 if (modec != 't' && modec != 'c' && modec != 'g')
12120 modec = 0; /* replace invalid with current */
12121 }
12122 else
12123 {
12124#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12125 if (USE_24BIT)
12126 modec = 'g';
12127 else
12128#endif
12129 if (t_colors > 1)
12130 modec = 'c';
12131 else
12132 modec = 't';
12133 }
12134
12135
12136 switch (TOLOWER_ASC(what[0]))
12137 {
12138 case 'b':
12139 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12140 p = highlight_color(id, what, modec);
12141 else /* bold */
12142 p = highlight_has_attr(id, HL_BOLD, modec);
12143 break;
12144
12145 case 'f': /* fg[#] or font */
12146 p = highlight_color(id, what, modec);
12147 break;
12148
12149 case 'i':
12150 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12151 p = highlight_has_attr(id, HL_INVERSE, modec);
12152 else /* italic */
12153 p = highlight_has_attr(id, HL_ITALIC, modec);
12154 break;
12155
12156 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012157 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012158 break;
12159
12160 case 'r': /* reverse */
12161 p = highlight_has_attr(id, HL_INVERSE, modec);
12162 break;
12163
12164 case 's':
12165 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12166 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012167 /* strikeout */
12168 else if (TOLOWER_ASC(what[1]) == 't' &&
12169 TOLOWER_ASC(what[2]) == 'r')
12170 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012171 else /* standout */
12172 p = highlight_has_attr(id, HL_STANDOUT, modec);
12173 break;
12174
12175 case 'u':
12176 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12177 /* underline */
12178 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12179 else
12180 /* undercurl */
12181 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12182 break;
12183 }
12184
12185 if (p != NULL)
12186 p = vim_strsave(p);
12187#endif
12188 rettv->v_type = VAR_STRING;
12189 rettv->vval.v_string = p;
12190}
12191
12192/*
12193 * "synIDtrans(id)" function
12194 */
12195 static void
12196f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12197{
12198 int id;
12199
12200#ifdef FEAT_SYN_HL
12201 id = (int)get_tv_number(&argvars[0]);
12202
12203 if (id > 0)
12204 id = syn_get_final_id(id);
12205 else
12206#endif
12207 id = 0;
12208
12209 rettv->vval.v_number = id;
12210}
12211
12212/*
12213 * "synconcealed(lnum, col)" function
12214 */
12215 static void
12216f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12217{
12218#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12219 linenr_T lnum;
12220 colnr_T col;
12221 int syntax_flags = 0;
12222 int cchar;
12223 int matchid = 0;
12224 char_u str[NUMBUFLEN];
12225#endif
12226
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012227 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012228
12229#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12230 lnum = get_tv_lnum(argvars); /* -1 on type error */
12231 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12232
12233 vim_memset(str, NUL, sizeof(str));
12234
12235 if (rettv_list_alloc(rettv) != FAIL)
12236 {
12237 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12238 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12239 && curwin->w_p_cole > 0)
12240 {
12241 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12242 syntax_flags = get_syntax_info(&matchid);
12243
12244 /* get the conceal character */
12245 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12246 {
12247 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012248 if (cchar == NUL && curwin->w_p_cole == 1)
12249 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012250 if (cchar != NUL)
12251 {
12252# ifdef FEAT_MBYTE
12253 if (has_mbyte)
12254 (*mb_char2bytes)(cchar, str);
12255 else
12256# endif
12257 str[0] = cchar;
12258 }
12259 }
12260 }
12261
12262 list_append_number(rettv->vval.v_list,
12263 (syntax_flags & HL_CONCEAL) != 0);
12264 /* -1 to auto-determine strlen */
12265 list_append_string(rettv->vval.v_list, str, -1);
12266 list_append_number(rettv->vval.v_list, matchid);
12267 }
12268#endif
12269}
12270
12271/*
12272 * "synstack(lnum, col)" function
12273 */
12274 static void
12275f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12276{
12277#ifdef FEAT_SYN_HL
12278 linenr_T lnum;
12279 colnr_T col;
12280 int i;
12281 int id;
12282#endif
12283
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012284 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012285
12286#ifdef FEAT_SYN_HL
12287 lnum = get_tv_lnum(argvars); /* -1 on type error */
12288 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12289
12290 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12291 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12292 && rettv_list_alloc(rettv) != FAIL)
12293 {
12294 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12295 for (i = 0; ; ++i)
12296 {
12297 id = syn_get_stack_item(i);
12298 if (id < 0)
12299 break;
12300 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12301 break;
12302 }
12303 }
12304#endif
12305}
12306
12307 static void
12308get_cmd_output_as_rettv(
12309 typval_T *argvars,
12310 typval_T *rettv,
12311 int retlist)
12312{
12313 char_u *res = NULL;
12314 char_u *p;
12315 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012316 int err = FALSE;
12317 FILE *fd;
12318 list_T *list = NULL;
12319 int flags = SHELL_SILENT;
12320
12321 rettv->v_type = VAR_STRING;
12322 rettv->vval.v_string = NULL;
12323 if (check_restricted() || check_secure())
12324 goto errret;
12325
12326 if (argvars[1].v_type != VAR_UNKNOWN)
12327 {
12328 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012329 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012330 * command.
12331 */
12332 if ((infile = vim_tempname('i', TRUE)) == NULL)
12333 {
12334 EMSG(_(e_notmp));
12335 goto errret;
12336 }
12337
12338 fd = mch_fopen((char *)infile, WRITEBIN);
12339 if (fd == NULL)
12340 {
12341 EMSG2(_(e_notopen), infile);
12342 goto errret;
12343 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012344 if (argvars[1].v_type == VAR_NUMBER)
12345 {
12346 linenr_T lnum;
12347 buf_T *buf;
12348
12349 buf = buflist_findnr(argvars[1].vval.v_number);
12350 if (buf == NULL)
12351 {
12352 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012353 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012354 goto errret;
12355 }
12356
12357 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12358 {
12359 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12360 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12361 {
12362 err = TRUE;
12363 break;
12364 }
12365 if (putc(NL, fd) == EOF)
12366 {
12367 err = TRUE;
12368 break;
12369 }
12370 }
12371 }
12372 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012373 {
12374 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12375 err = TRUE;
12376 }
12377 else
12378 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012379 size_t len;
12380 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012381
12382 p = get_tv_string_buf_chk(&argvars[1], buf);
12383 if (p == NULL)
12384 {
12385 fclose(fd);
12386 goto errret; /* type error; errmsg already given */
12387 }
12388 len = STRLEN(p);
12389 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12390 err = TRUE;
12391 }
12392 if (fclose(fd) != 0)
12393 err = TRUE;
12394 if (err)
12395 {
12396 EMSG(_("E677: Error writing temp file"));
12397 goto errret;
12398 }
12399 }
12400
12401 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12402 * echoes typeahead, that messes up the display. */
12403 if (!msg_silent)
12404 flags += SHELL_COOKED;
12405
12406 if (retlist)
12407 {
12408 int len;
12409 listitem_T *li;
12410 char_u *s = NULL;
12411 char_u *start;
12412 char_u *end;
12413 int i;
12414
12415 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12416 if (res == NULL)
12417 goto errret;
12418
12419 list = list_alloc();
12420 if (list == NULL)
12421 goto errret;
12422
12423 for (i = 0; i < len; ++i)
12424 {
12425 start = res + i;
12426 while (i < len && res[i] != NL)
12427 ++i;
12428 end = res + i;
12429
12430 s = alloc((unsigned)(end - start + 1));
12431 if (s == NULL)
12432 goto errret;
12433
12434 for (p = s; start < end; ++p, ++start)
12435 *p = *start == NUL ? NL : *start;
12436 *p = NUL;
12437
12438 li = listitem_alloc();
12439 if (li == NULL)
12440 {
12441 vim_free(s);
12442 goto errret;
12443 }
12444 li->li_tv.v_type = VAR_STRING;
12445 li->li_tv.v_lock = 0;
12446 li->li_tv.vval.v_string = s;
12447 list_append(list, li);
12448 }
12449
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012450 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012451 list = NULL;
12452 }
12453 else
12454 {
12455 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12456#ifdef USE_CR
12457 /* translate <CR> into <NL> */
12458 if (res != NULL)
12459 {
12460 char_u *s;
12461
12462 for (s = res; *s; ++s)
12463 {
12464 if (*s == CAR)
12465 *s = NL;
12466 }
12467 }
12468#else
12469# ifdef USE_CRNL
12470 /* translate <CR><NL> into <NL> */
12471 if (res != NULL)
12472 {
12473 char_u *s, *d;
12474
12475 d = res;
12476 for (s = res; *s; ++s)
12477 {
12478 if (s[0] == CAR && s[1] == NL)
12479 ++s;
12480 *d++ = *s;
12481 }
12482 *d = NUL;
12483 }
12484# endif
12485#endif
12486 rettv->vval.v_string = res;
12487 res = NULL;
12488 }
12489
12490errret:
12491 if (infile != NULL)
12492 {
12493 mch_remove(infile);
12494 vim_free(infile);
12495 }
12496 if (res != NULL)
12497 vim_free(res);
12498 if (list != NULL)
12499 list_free(list);
12500}
12501
12502/*
12503 * "system()" function
12504 */
12505 static void
12506f_system(typval_T *argvars, typval_T *rettv)
12507{
12508 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12509}
12510
12511/*
12512 * "systemlist()" function
12513 */
12514 static void
12515f_systemlist(typval_T *argvars, typval_T *rettv)
12516{
12517 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12518}
12519
12520/*
12521 * "tabpagebuflist()" function
12522 */
12523 static void
12524f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12525{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012526 tabpage_T *tp;
12527 win_T *wp = NULL;
12528
12529 if (argvars[0].v_type == VAR_UNKNOWN)
12530 wp = firstwin;
12531 else
12532 {
12533 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12534 if (tp != NULL)
12535 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12536 }
12537 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12538 {
12539 for (; wp != NULL; wp = wp->w_next)
12540 if (list_append_number(rettv->vval.v_list,
12541 wp->w_buffer->b_fnum) == FAIL)
12542 break;
12543 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012544}
12545
12546
12547/*
12548 * "tabpagenr()" function
12549 */
12550 static void
12551f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12552{
12553 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012554 char_u *arg;
12555
12556 if (argvars[0].v_type != VAR_UNKNOWN)
12557 {
12558 arg = get_tv_string_chk(&argvars[0]);
12559 nr = 0;
12560 if (arg != NULL)
12561 {
12562 if (STRCMP(arg, "$") == 0)
12563 nr = tabpage_index(NULL) - 1;
12564 else
12565 EMSG2(_(e_invexpr2), arg);
12566 }
12567 }
12568 else
12569 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012570 rettv->vval.v_number = nr;
12571}
12572
12573
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012574static int get_winnr(tabpage_T *tp, typval_T *argvar);
12575
12576/*
12577 * Common code for tabpagewinnr() and winnr().
12578 */
12579 static int
12580get_winnr(tabpage_T *tp, typval_T *argvar)
12581{
12582 win_T *twin;
12583 int nr = 1;
12584 win_T *wp;
12585 char_u *arg;
12586
12587 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12588 if (argvar->v_type != VAR_UNKNOWN)
12589 {
12590 arg = get_tv_string_chk(argvar);
12591 if (arg == NULL)
12592 nr = 0; /* type error; errmsg already given */
12593 else if (STRCMP(arg, "$") == 0)
12594 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12595 else if (STRCMP(arg, "#") == 0)
12596 {
12597 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12598 if (twin == NULL)
12599 nr = 0;
12600 }
12601 else
12602 {
12603 EMSG2(_(e_invexpr2), arg);
12604 nr = 0;
12605 }
12606 }
12607
12608 if (nr > 0)
12609 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12610 wp != twin; wp = wp->w_next)
12611 {
12612 if (wp == NULL)
12613 {
12614 /* didn't find it in this tabpage */
12615 nr = 0;
12616 break;
12617 }
12618 ++nr;
12619 }
12620 return nr;
12621}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012622
12623/*
12624 * "tabpagewinnr()" function
12625 */
12626 static void
12627f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12628{
12629 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012630 tabpage_T *tp;
12631
12632 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12633 if (tp == NULL)
12634 nr = 0;
12635 else
12636 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012637 rettv->vval.v_number = nr;
12638}
12639
12640
12641/*
12642 * "tagfiles()" function
12643 */
12644 static void
12645f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12646{
12647 char_u *fname;
12648 tagname_T tn;
12649 int first;
12650
12651 if (rettv_list_alloc(rettv) == FAIL)
12652 return;
12653 fname = alloc(MAXPATHL);
12654 if (fname == NULL)
12655 return;
12656
12657 for (first = TRUE; ; first = FALSE)
12658 if (get_tagfname(&tn, first, fname) == FAIL
12659 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12660 break;
12661 tagname_free(&tn);
12662 vim_free(fname);
12663}
12664
12665/*
12666 * "taglist()" function
12667 */
12668 static void
12669f_taglist(typval_T *argvars, typval_T *rettv)
12670{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012671 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012672 char_u *tag_pattern;
12673
12674 tag_pattern = get_tv_string(&argvars[0]);
12675
12676 rettv->vval.v_number = FALSE;
12677 if (*tag_pattern == NUL)
12678 return;
12679
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012680 if (argvars[1].v_type != VAR_UNKNOWN)
12681 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012682 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012683 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012684}
12685
12686/*
12687 * "tempname()" function
12688 */
12689 static void
12690f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12691{
12692 static int x = 'A';
12693
12694 rettv->v_type = VAR_STRING;
12695 rettv->vval.v_string = vim_tempname(x, FALSE);
12696
12697 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12698 * names. Skip 'I' and 'O', they are used for shell redirection. */
12699 do
12700 {
12701 if (x == 'Z')
12702 x = '0';
12703 else if (x == '9')
12704 x = 'A';
12705 else
12706 {
12707#ifdef EBCDIC
12708 if (x == 'I')
12709 x = 'J';
12710 else if (x == 'R')
12711 x = 'S';
12712 else
12713#endif
12714 ++x;
12715 }
12716 } while (x == 'I' || x == 'O');
12717}
12718
12719#ifdef FEAT_FLOAT
12720/*
12721 * "tan()" function
12722 */
12723 static void
12724f_tan(typval_T *argvars, typval_T *rettv)
12725{
12726 float_T f = 0.0;
12727
12728 rettv->v_type = VAR_FLOAT;
12729 if (get_float_arg(argvars, &f) == OK)
12730 rettv->vval.v_float = tan(f);
12731 else
12732 rettv->vval.v_float = 0.0;
12733}
12734
12735/*
12736 * "tanh()" function
12737 */
12738 static void
12739f_tanh(typval_T *argvars, typval_T *rettv)
12740{
12741 float_T f = 0.0;
12742
12743 rettv->v_type = VAR_FLOAT;
12744 if (get_float_arg(argvars, &f) == OK)
12745 rettv->vval.v_float = tanh(f);
12746 else
12747 rettv->vval.v_float = 0.0;
12748}
12749#endif
12750
12751/*
12752 * "test_alloc_fail(id, countdown, repeat)" function
12753 */
12754 static void
12755f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12756{
12757 if (argvars[0].v_type != VAR_NUMBER
12758 || argvars[0].vval.v_number <= 0
12759 || argvars[1].v_type != VAR_NUMBER
12760 || argvars[1].vval.v_number < 0
12761 || argvars[2].v_type != VAR_NUMBER)
12762 EMSG(_(e_invarg));
12763 else
12764 {
12765 alloc_fail_id = argvars[0].vval.v_number;
12766 if (alloc_fail_id >= aid_last)
12767 EMSG(_(e_invarg));
12768 alloc_fail_countdown = argvars[1].vval.v_number;
12769 alloc_fail_repeat = argvars[2].vval.v_number;
12770 did_outofmem_msg = FALSE;
12771 }
12772}
12773
12774/*
12775 * "test_autochdir()"
12776 */
12777 static void
12778f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12779{
12780#if defined(FEAT_AUTOCHDIR)
12781 test_autochdir = TRUE;
12782#endif
12783}
12784
12785/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012786 * "test_feedinput()"
12787 */
12788 static void
12789f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12790{
12791#ifdef USE_INPUT_BUF
12792 char_u *val = get_tv_string_chk(&argvars[0]);
12793
12794 if (val != NULL)
12795 {
12796 trash_input_buf();
12797 add_to_input_buf_csi(val, (int)STRLEN(val));
12798 }
12799#endif
12800}
12801
12802/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012803 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012804 */
12805 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012806f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012807{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012808 char_u *name = (char_u *)"";
12809 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012810 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012811
12812 if (argvars[0].v_type != VAR_STRING
12813 || (argvars[1].v_type) != VAR_NUMBER)
12814 EMSG(_(e_invarg));
12815 else
12816 {
12817 name = get_tv_string_chk(&argvars[0]);
12818 val = (int)get_tv_number(&argvars[1]);
12819
12820 if (STRCMP(name, (char_u *)"redraw") == 0)
12821 disable_redraw_for_testing = val;
12822 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12823 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012824 else if (STRCMP(name, (char_u *)"starting") == 0)
12825 {
12826 if (val)
12827 {
12828 if (save_starting < 0)
12829 save_starting = starting;
12830 starting = 0;
12831 }
12832 else
12833 {
12834 starting = save_starting;
12835 save_starting = -1;
12836 }
12837 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012838 else if (STRCMP(name, (char_u *)"ALL") == 0)
12839 {
12840 disable_char_avail_for_testing = FALSE;
12841 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012842 if (save_starting >= 0)
12843 {
12844 starting = save_starting;
12845 save_starting = -1;
12846 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012847 }
12848 else
12849 EMSG2(_(e_invarg2), name);
12850 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012851}
12852
12853/*
12854 * "test_garbagecollect_now()" function
12855 */
12856 static void
12857f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12858{
12859 /* This is dangerous, any Lists and Dicts used internally may be freed
12860 * while still in use. */
12861 garbage_collect(TRUE);
12862}
12863
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012864/*
12865 * "test_ignore_error()" function
12866 */
12867 static void
12868f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12869{
12870 ignore_error_for_testing(get_tv_string(&argvars[0]));
12871}
12872
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012873#ifdef FEAT_JOB_CHANNEL
12874 static void
12875f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12876{
12877 rettv->v_type = VAR_CHANNEL;
12878 rettv->vval.v_channel = NULL;
12879}
12880#endif
12881
12882 static void
12883f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12884{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012885 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012886}
12887
12888#ifdef FEAT_JOB_CHANNEL
12889 static void
12890f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12891{
12892 rettv->v_type = VAR_JOB;
12893 rettv->vval.v_job = NULL;
12894}
12895#endif
12896
12897 static void
12898f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12899{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012900 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012901}
12902
12903 static void
12904f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12905{
12906 rettv->v_type = VAR_PARTIAL;
12907 rettv->vval.v_partial = NULL;
12908}
12909
12910 static void
12911f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12912{
12913 rettv->v_type = VAR_STRING;
12914 rettv->vval.v_string = NULL;
12915}
12916
12917 static void
12918f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12919{
12920 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12921}
12922
12923#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12924/*
12925 * Get a callback from "arg". It can be a Funcref or a function name.
12926 * When "arg" is zero return an empty string.
12927 * Return NULL for an invalid argument.
12928 */
12929 char_u *
12930get_callback(typval_T *arg, partial_T **pp)
12931{
12932 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12933 {
12934 *pp = arg->vval.v_partial;
12935 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012936 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012937 }
12938 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012939 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012940 {
12941 func_ref(arg->vval.v_string);
12942 return arg->vval.v_string;
12943 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012944 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12945 return (char_u *)"";
12946 EMSG(_("E921: Invalid callback argument"));
12947 return NULL;
12948}
12949
12950/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020012951 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012952 */
12953 void
12954free_callback(char_u *callback, partial_T *partial)
12955{
12956 if (partial != NULL)
12957 partial_unref(partial);
12958 else if (callback != NULL)
12959 {
12960 func_unref(callback);
12961 vim_free(callback);
12962 }
12963}
12964#endif
12965
12966#ifdef FEAT_TIMERS
12967/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012968 * "timer_info([timer])" function
12969 */
12970 static void
12971f_timer_info(typval_T *argvars, typval_T *rettv)
12972{
12973 timer_T *timer = NULL;
12974
12975 if (rettv_list_alloc(rettv) != OK)
12976 return;
12977 if (argvars[0].v_type != VAR_UNKNOWN)
12978 {
12979 if (argvars[0].v_type != VAR_NUMBER)
12980 EMSG(_(e_number_exp));
12981 else
12982 {
12983 timer = find_timer((int)get_tv_number(&argvars[0]));
12984 if (timer != NULL)
12985 add_timer_info(rettv, timer);
12986 }
12987 }
12988 else
12989 add_timer_info_all(rettv);
12990}
12991
12992/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012993 * "timer_pause(timer, paused)" function
12994 */
12995 static void
12996f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12997{
12998 timer_T *timer = NULL;
12999 int paused = (int)get_tv_number(&argvars[1]);
13000
13001 if (argvars[0].v_type != VAR_NUMBER)
13002 EMSG(_(e_number_exp));
13003 else
13004 {
13005 timer = find_timer((int)get_tv_number(&argvars[0]));
13006 if (timer != NULL)
13007 timer->tr_paused = paused;
13008 }
13009}
13010
13011/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013012 * "timer_start(time, callback [, options])" function
13013 */
13014 static void
13015f_timer_start(typval_T *argvars, typval_T *rettv)
13016{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013017 long msec = (long)get_tv_number(&argvars[0]);
13018 timer_T *timer;
13019 int repeat = 0;
13020 char_u *callback;
13021 dict_T *dict;
13022 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013023
Bram Moolenaar75537a92016-09-05 22:45:28 +020013024 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013025 if (check_secure())
13026 return;
13027 if (argvars[2].v_type != VAR_UNKNOWN)
13028 {
13029 if (argvars[2].v_type != VAR_DICT
13030 || (dict = argvars[2].vval.v_dict) == NULL)
13031 {
13032 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13033 return;
13034 }
13035 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13036 repeat = get_dict_number(dict, (char_u *)"repeat");
13037 }
13038
Bram Moolenaar75537a92016-09-05 22:45:28 +020013039 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013040 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013041 return;
13042
13043 timer = create_timer(msec, repeat);
13044 if (timer == NULL)
13045 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013046 else
13047 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013048 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013049 timer->tr_callback = vim_strsave(callback);
13050 else
13051 /* pointer into the partial */
13052 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013053 timer->tr_partial = partial;
13054 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013055 }
13056}
13057
13058/*
13059 * "timer_stop(timer)" function
13060 */
13061 static void
13062f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13063{
13064 timer_T *timer;
13065
13066 if (argvars[0].v_type != VAR_NUMBER)
13067 {
13068 EMSG(_(e_number_exp));
13069 return;
13070 }
13071 timer = find_timer((int)get_tv_number(&argvars[0]));
13072 if (timer != NULL)
13073 stop_timer(timer);
13074}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013075
13076/*
13077 * "timer_stopall()" function
13078 */
13079 static void
13080f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13081{
13082 stop_all_timers();
13083}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013084#endif
13085
13086/*
13087 * "tolower(string)" function
13088 */
13089 static void
13090f_tolower(typval_T *argvars, typval_T *rettv)
13091{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013092 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013093 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013094}
13095
13096/*
13097 * "toupper(string)" function
13098 */
13099 static void
13100f_toupper(typval_T *argvars, typval_T *rettv)
13101{
13102 rettv->v_type = VAR_STRING;
13103 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13104}
13105
13106/*
13107 * "tr(string, fromstr, tostr)" function
13108 */
13109 static void
13110f_tr(typval_T *argvars, typval_T *rettv)
13111{
13112 char_u *in_str;
13113 char_u *fromstr;
13114 char_u *tostr;
13115 char_u *p;
13116#ifdef FEAT_MBYTE
13117 int inlen;
13118 int fromlen;
13119 int tolen;
13120 int idx;
13121 char_u *cpstr;
13122 int cplen;
13123 int first = TRUE;
13124#endif
13125 char_u buf[NUMBUFLEN];
13126 char_u buf2[NUMBUFLEN];
13127 garray_T ga;
13128
13129 in_str = get_tv_string(&argvars[0]);
13130 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13131 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13132
13133 /* Default return value: empty string. */
13134 rettv->v_type = VAR_STRING;
13135 rettv->vval.v_string = NULL;
13136 if (fromstr == NULL || tostr == NULL)
13137 return; /* type error; errmsg already given */
13138 ga_init2(&ga, (int)sizeof(char), 80);
13139
13140#ifdef FEAT_MBYTE
13141 if (!has_mbyte)
13142#endif
13143 /* not multi-byte: fromstr and tostr must be the same length */
13144 if (STRLEN(fromstr) != STRLEN(tostr))
13145 {
13146#ifdef FEAT_MBYTE
13147error:
13148#endif
13149 EMSG2(_(e_invarg2), fromstr);
13150 ga_clear(&ga);
13151 return;
13152 }
13153
13154 /* fromstr and tostr have to contain the same number of chars */
13155 while (*in_str != NUL)
13156 {
13157#ifdef FEAT_MBYTE
13158 if (has_mbyte)
13159 {
13160 inlen = (*mb_ptr2len)(in_str);
13161 cpstr = in_str;
13162 cplen = inlen;
13163 idx = 0;
13164 for (p = fromstr; *p != NUL; p += fromlen)
13165 {
13166 fromlen = (*mb_ptr2len)(p);
13167 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13168 {
13169 for (p = tostr; *p != NUL; p += tolen)
13170 {
13171 tolen = (*mb_ptr2len)(p);
13172 if (idx-- == 0)
13173 {
13174 cplen = tolen;
13175 cpstr = p;
13176 break;
13177 }
13178 }
13179 if (*p == NUL) /* tostr is shorter than fromstr */
13180 goto error;
13181 break;
13182 }
13183 ++idx;
13184 }
13185
13186 if (first && cpstr == in_str)
13187 {
13188 /* Check that fromstr and tostr have the same number of
13189 * (multi-byte) characters. Done only once when a character
13190 * of in_str doesn't appear in fromstr. */
13191 first = FALSE;
13192 for (p = tostr; *p != NUL; p += tolen)
13193 {
13194 tolen = (*mb_ptr2len)(p);
13195 --idx;
13196 }
13197 if (idx != 0)
13198 goto error;
13199 }
13200
13201 (void)ga_grow(&ga, cplen);
13202 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13203 ga.ga_len += cplen;
13204
13205 in_str += inlen;
13206 }
13207 else
13208#endif
13209 {
13210 /* When not using multi-byte chars we can do it faster. */
13211 p = vim_strchr(fromstr, *in_str);
13212 if (p != NULL)
13213 ga_append(&ga, tostr[p - fromstr]);
13214 else
13215 ga_append(&ga, *in_str);
13216 ++in_str;
13217 }
13218 }
13219
13220 /* add a terminating NUL */
13221 (void)ga_grow(&ga, 1);
13222 ga_append(&ga, NUL);
13223
13224 rettv->vval.v_string = ga.ga_data;
13225}
13226
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013227/*
13228 * "trim({expr})" function
13229 */
13230 static void
13231f_trim(typval_T *argvars, typval_T *rettv)
13232{
13233 char_u buf1[NUMBUFLEN];
13234 char_u buf2[NUMBUFLEN];
13235 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13236 char_u *mask = NULL;
13237 char_u *tail;
13238 char_u *prev;
13239 char_u *p;
13240 int c1;
13241
13242 rettv->v_type = VAR_STRING;
13243 if (head == NULL)
13244 {
13245 rettv->vval.v_string = NULL;
13246 return;
13247 }
13248
13249 if (argvars[1].v_type == VAR_STRING)
13250 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13251
13252 while (*head != NUL)
13253 {
13254 c1 = PTR2CHAR(head);
13255 if (mask == NULL)
13256 {
13257 if (c1 > ' ' && c1 != 0xa0)
13258 break;
13259 }
13260 else
13261 {
13262 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13263 if (c1 == PTR2CHAR(p))
13264 break;
13265 if (*p == NUL)
13266 break;
13267 }
13268 MB_PTR_ADV(head);
13269 }
13270
13271 for (tail = head + STRLEN(head); tail > head; tail = prev)
13272 {
13273 prev = tail;
13274 MB_PTR_BACK(head, prev);
13275 c1 = PTR2CHAR(prev);
13276 if (mask == NULL)
13277 {
13278 if (c1 > ' ' && c1 != 0xa0)
13279 break;
13280 }
13281 else
13282 {
13283 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13284 if (c1 == PTR2CHAR(p))
13285 break;
13286 if (*p == NUL)
13287 break;
13288 }
13289 }
13290 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13291}
13292
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013293#ifdef FEAT_FLOAT
13294/*
13295 * "trunc({float})" function
13296 */
13297 static void
13298f_trunc(typval_T *argvars, typval_T *rettv)
13299{
13300 float_T f = 0.0;
13301
13302 rettv->v_type = VAR_FLOAT;
13303 if (get_float_arg(argvars, &f) == OK)
13304 /* trunc() is not in C90, use floor() or ceil() instead. */
13305 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13306 else
13307 rettv->vval.v_float = 0.0;
13308}
13309#endif
13310
13311/*
13312 * "type(expr)" function
13313 */
13314 static void
13315f_type(typval_T *argvars, typval_T *rettv)
13316{
13317 int n = -1;
13318
13319 switch (argvars[0].v_type)
13320 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013321 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13322 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013323 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013324 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13325 case VAR_LIST: n = VAR_TYPE_LIST; break;
13326 case VAR_DICT: n = VAR_TYPE_DICT; break;
13327 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013328 case VAR_SPECIAL:
13329 if (argvars[0].vval.v_number == VVAL_FALSE
13330 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013331 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013332 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013333 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013334 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013335 case VAR_JOB: n = VAR_TYPE_JOB; break;
13336 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013337 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013338 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013339 n = -1;
13340 break;
13341 }
13342 rettv->vval.v_number = n;
13343}
13344
13345/*
13346 * "undofile(name)" function
13347 */
13348 static void
13349f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13350{
13351 rettv->v_type = VAR_STRING;
13352#ifdef FEAT_PERSISTENT_UNDO
13353 {
13354 char_u *fname = get_tv_string(&argvars[0]);
13355
13356 if (*fname == NUL)
13357 {
13358 /* If there is no file name there will be no undo file. */
13359 rettv->vval.v_string = NULL;
13360 }
13361 else
13362 {
13363 char_u *ffname = FullName_save(fname, FALSE);
13364
13365 if (ffname != NULL)
13366 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13367 vim_free(ffname);
13368 }
13369 }
13370#else
13371 rettv->vval.v_string = NULL;
13372#endif
13373}
13374
13375/*
13376 * "undotree()" function
13377 */
13378 static void
13379f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13380{
13381 if (rettv_dict_alloc(rettv) == OK)
13382 {
13383 dict_T *dict = rettv->vval.v_dict;
13384 list_T *list;
13385
13386 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13387 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13388 dict_add_nr_str(dict, "save_last",
13389 (long)curbuf->b_u_save_nr_last, NULL);
13390 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13391 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13392 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13393
13394 list = list_alloc();
13395 if (list != NULL)
13396 {
13397 u_eval_tree(curbuf->b_u_oldhead, list);
13398 dict_add_list(dict, "entries", list);
13399 }
13400 }
13401}
13402
13403/*
13404 * "values(dict)" function
13405 */
13406 static void
13407f_values(typval_T *argvars, typval_T *rettv)
13408{
13409 dict_list(argvars, rettv, 1);
13410}
13411
13412/*
13413 * "virtcol(string)" function
13414 */
13415 static void
13416f_virtcol(typval_T *argvars, typval_T *rettv)
13417{
13418 colnr_T vcol = 0;
13419 pos_T *fp;
13420 int fnum = curbuf->b_fnum;
13421
13422 fp = var2fpos(&argvars[0], FALSE, &fnum);
13423 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13424 && fnum == curbuf->b_fnum)
13425 {
13426 getvvcol(curwin, fp, NULL, NULL, &vcol);
13427 ++vcol;
13428 }
13429
13430 rettv->vval.v_number = vcol;
13431}
13432
13433/*
13434 * "visualmode()" function
13435 */
13436 static void
13437f_visualmode(typval_T *argvars, typval_T *rettv)
13438{
13439 char_u str[2];
13440
13441 rettv->v_type = VAR_STRING;
13442 str[0] = curbuf->b_visual_mode_eval;
13443 str[1] = NUL;
13444 rettv->vval.v_string = vim_strsave(str);
13445
13446 /* A non-zero number or non-empty string argument: reset mode. */
13447 if (non_zero_arg(&argvars[0]))
13448 curbuf->b_visual_mode_eval = NUL;
13449}
13450
13451/*
13452 * "wildmenumode()" function
13453 */
13454 static void
13455f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13456{
13457#ifdef FEAT_WILDMENU
13458 if (wild_menu_showing)
13459 rettv->vval.v_number = 1;
13460#endif
13461}
13462
13463/*
13464 * "winbufnr(nr)" function
13465 */
13466 static void
13467f_winbufnr(typval_T *argvars, typval_T *rettv)
13468{
13469 win_T *wp;
13470
13471 wp = find_win_by_nr(&argvars[0], NULL);
13472 if (wp == NULL)
13473 rettv->vval.v_number = -1;
13474 else
13475 rettv->vval.v_number = wp->w_buffer->b_fnum;
13476}
13477
13478/*
13479 * "wincol()" function
13480 */
13481 static void
13482f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13483{
13484 validate_cursor();
13485 rettv->vval.v_number = curwin->w_wcol + 1;
13486}
13487
13488/*
13489 * "winheight(nr)" function
13490 */
13491 static void
13492f_winheight(typval_T *argvars, typval_T *rettv)
13493{
13494 win_T *wp;
13495
13496 wp = find_win_by_nr(&argvars[0], NULL);
13497 if (wp == NULL)
13498 rettv->vval.v_number = -1;
13499 else
13500 rettv->vval.v_number = wp->w_height;
13501}
13502
13503/*
13504 * "winline()" function
13505 */
13506 static void
13507f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13508{
13509 validate_cursor();
13510 rettv->vval.v_number = curwin->w_wrow + 1;
13511}
13512
13513/*
13514 * "winnr()" function
13515 */
13516 static void
13517f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13518{
13519 int nr = 1;
13520
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013521 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013522 rettv->vval.v_number = nr;
13523}
13524
13525/*
13526 * "winrestcmd()" function
13527 */
13528 static void
13529f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13530{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013531 win_T *wp;
13532 int winnr = 1;
13533 garray_T ga;
13534 char_u buf[50];
13535
13536 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013537 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013538 {
13539 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13540 ga_concat(&ga, buf);
13541 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13542 ga_concat(&ga, buf);
13543 ++winnr;
13544 }
13545 ga_append(&ga, NUL);
13546
13547 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013548 rettv->v_type = VAR_STRING;
13549}
13550
13551/*
13552 * "winrestview()" function
13553 */
13554 static void
13555f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13556{
13557 dict_T *dict;
13558
13559 if (argvars[0].v_type != VAR_DICT
13560 || (dict = argvars[0].vval.v_dict) == NULL)
13561 EMSG(_(e_invarg));
13562 else
13563 {
13564 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13565 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13566 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13567 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13568#ifdef FEAT_VIRTUALEDIT
13569 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13570 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13571#endif
13572 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13573 {
13574 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13575 curwin->w_set_curswant = FALSE;
13576 }
13577
13578 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13579 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13580#ifdef FEAT_DIFF
13581 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13582 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13583#endif
13584 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13585 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13586 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13587 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13588
13589 check_cursor();
13590 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013591 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013592 changed_window_setting();
13593
13594 if (curwin->w_topline <= 0)
13595 curwin->w_topline = 1;
13596 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13597 curwin->w_topline = curbuf->b_ml.ml_line_count;
13598#ifdef FEAT_DIFF
13599 check_topfill(curwin, TRUE);
13600#endif
13601 }
13602}
13603
13604/*
13605 * "winsaveview()" function
13606 */
13607 static void
13608f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13609{
13610 dict_T *dict;
13611
13612 if (rettv_dict_alloc(rettv) == FAIL)
13613 return;
13614 dict = rettv->vval.v_dict;
13615
13616 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13617 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13618#ifdef FEAT_VIRTUALEDIT
13619 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13620#endif
13621 update_curswant();
13622 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13623
13624 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13625#ifdef FEAT_DIFF
13626 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13627#endif
13628 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13629 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13630}
13631
13632/*
13633 * "winwidth(nr)" function
13634 */
13635 static void
13636f_winwidth(typval_T *argvars, typval_T *rettv)
13637{
13638 win_T *wp;
13639
13640 wp = find_win_by_nr(&argvars[0], NULL);
13641 if (wp == NULL)
13642 rettv->vval.v_number = -1;
13643 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013644 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013645}
13646
13647/*
13648 * "wordcount()" function
13649 */
13650 static void
13651f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13652{
13653 if (rettv_dict_alloc(rettv) == FAIL)
13654 return;
13655 cursor_pos_info(rettv->vval.v_dict);
13656}
13657
13658/*
13659 * "writefile()" function
13660 */
13661 static void
13662f_writefile(typval_T *argvars, typval_T *rettv)
13663{
13664 int binary = FALSE;
13665 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013666#ifdef HAVE_FSYNC
13667 int do_fsync = p_fs;
13668#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013669 char_u *fname;
13670 FILE *fd;
13671 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013672 listitem_T *li;
13673 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013674
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013675 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013676 if (check_restricted() || check_secure())
13677 return;
13678
13679 if (argvars[0].v_type != VAR_LIST)
13680 {
13681 EMSG2(_(e_listarg), "writefile()");
13682 return;
13683 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013684 list = argvars[0].vval.v_list;
13685 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013686 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013687 for (li = list->lv_first; li != NULL; li = li->li_next)
13688 if (get_tv_string_chk(&li->li_tv) == NULL)
13689 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013690
13691 if (argvars[2].v_type != VAR_UNKNOWN)
13692 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013693 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13694
13695 if (arg2 == NULL)
13696 return;
13697 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013698 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013699 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013700 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013701#ifdef HAVE_FSYNC
13702 if (vim_strchr(arg2, 's') != NULL)
13703 do_fsync = TRUE;
13704 else if (vim_strchr(arg2, 'S') != NULL)
13705 do_fsync = FALSE;
13706#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013707 }
13708
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013709 fname = get_tv_string_chk(&argvars[1]);
13710 if (fname == NULL)
13711 return;
13712
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013713 /* Always open the file in binary mode, library functions have a mind of
13714 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013715 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13716 append ? APPENDBIN : WRITEBIN)) == NULL)
13717 {
13718 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13719 ret = -1;
13720 }
13721 else
13722 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013723 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013724 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013725#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013726 else if (do_fsync)
13727 /* Ignore the error, the user wouldn't know what to do about it.
13728 * May happen for a device. */
13729 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013730#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013731 fclose(fd);
13732 }
13733
13734 rettv->vval.v_number = ret;
13735}
13736
13737/*
13738 * "xor(expr, expr)" function
13739 */
13740 static void
13741f_xor(typval_T *argvars, typval_T *rettv)
13742{
13743 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13744 ^ get_tv_number_chk(&argvars[1], NULL);
13745}
13746
13747
13748#endif /* FEAT_EVAL */