blob: 259edb8b1fed0e5fa36323e3e91542f6d9d560b4 [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;
3316 exec_normal(TRUE);
3317 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)
7386 rettv->vval.v_string = str2special_save(rhs, FALSE);
7387
7388 }
7389 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7390 {
7391 /* Return a dictionary. */
7392 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7393 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7394 dict_T *dict = rettv->vval.v_dict;
7395
7396 dict_add_nr_str(dict, "lhs", 0L, lhs);
7397 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7398 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7399 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7400 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7401 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7402 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7403 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7404 dict_add_nr_str(dict, "mode", 0L, mapmode);
7405
7406 vim_free(lhs);
7407 vim_free(mapmode);
7408 }
7409}
7410
7411#ifdef FEAT_FLOAT
7412/*
7413 * "log()" function
7414 */
7415 static void
7416f_log(typval_T *argvars, typval_T *rettv)
7417{
7418 float_T f = 0.0;
7419
7420 rettv->v_type = VAR_FLOAT;
7421 if (get_float_arg(argvars, &f) == OK)
7422 rettv->vval.v_float = log(f);
7423 else
7424 rettv->vval.v_float = 0.0;
7425}
7426
7427/*
7428 * "log10()" function
7429 */
7430 static void
7431f_log10(typval_T *argvars, typval_T *rettv)
7432{
7433 float_T f = 0.0;
7434
7435 rettv->v_type = VAR_FLOAT;
7436 if (get_float_arg(argvars, &f) == OK)
7437 rettv->vval.v_float = log10(f);
7438 else
7439 rettv->vval.v_float = 0.0;
7440}
7441#endif
7442
7443#ifdef FEAT_LUA
7444/*
7445 * "luaeval()" function
7446 */
7447 static void
7448f_luaeval(typval_T *argvars, typval_T *rettv)
7449{
7450 char_u *str;
7451 char_u buf[NUMBUFLEN];
7452
7453 str = get_tv_string_buf(&argvars[0], buf);
7454 do_luaeval(str, argvars + 1, rettv);
7455}
7456#endif
7457
7458/*
7459 * "map()" function
7460 */
7461 static void
7462f_map(typval_T *argvars, typval_T *rettv)
7463{
7464 filter_map(argvars, rettv, TRUE);
7465}
7466
7467/*
7468 * "maparg()" function
7469 */
7470 static void
7471f_maparg(typval_T *argvars, typval_T *rettv)
7472{
7473 get_maparg(argvars, rettv, TRUE);
7474}
7475
7476/*
7477 * "mapcheck()" function
7478 */
7479 static void
7480f_mapcheck(typval_T *argvars, typval_T *rettv)
7481{
7482 get_maparg(argvars, rettv, FALSE);
7483}
7484
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007485typedef enum
7486{
7487 MATCH_END, /* matchend() */
7488 MATCH_MATCH, /* match() */
7489 MATCH_STR, /* matchstr() */
7490 MATCH_LIST, /* matchlist() */
7491 MATCH_POS /* matchstrpos() */
7492} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007493
7494 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007495find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007496{
7497 char_u *str = NULL;
7498 long len = 0;
7499 char_u *expr = NULL;
7500 char_u *pat;
7501 regmatch_T regmatch;
7502 char_u patbuf[NUMBUFLEN];
7503 char_u strbuf[NUMBUFLEN];
7504 char_u *save_cpo;
7505 long start = 0;
7506 long nth = 1;
7507 colnr_T startcol = 0;
7508 int match = 0;
7509 list_T *l = NULL;
7510 listitem_T *li = NULL;
7511 long idx = 0;
7512 char_u *tofree = NULL;
7513
7514 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7515 save_cpo = p_cpo;
7516 p_cpo = (char_u *)"";
7517
7518 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007519 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007520 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007521 /* type MATCH_LIST: return empty list when there are no matches.
7522 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007523 if (rettv_list_alloc(rettv) == FAIL)
7524 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007525 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007526 && (list_append_string(rettv->vval.v_list,
7527 (char_u *)"", 0) == FAIL
7528 || list_append_number(rettv->vval.v_list,
7529 (varnumber_T)-1) == FAIL
7530 || list_append_number(rettv->vval.v_list,
7531 (varnumber_T)-1) == FAIL
7532 || list_append_number(rettv->vval.v_list,
7533 (varnumber_T)-1) == FAIL))
7534 {
7535 list_free(rettv->vval.v_list);
7536 rettv->vval.v_list = NULL;
7537 goto theend;
7538 }
7539 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007540 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007541 {
7542 rettv->v_type = VAR_STRING;
7543 rettv->vval.v_string = NULL;
7544 }
7545
7546 if (argvars[0].v_type == VAR_LIST)
7547 {
7548 if ((l = argvars[0].vval.v_list) == NULL)
7549 goto theend;
7550 li = l->lv_first;
7551 }
7552 else
7553 {
7554 expr = str = get_tv_string(&argvars[0]);
7555 len = (long)STRLEN(str);
7556 }
7557
7558 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7559 if (pat == NULL)
7560 goto theend;
7561
7562 if (argvars[2].v_type != VAR_UNKNOWN)
7563 {
7564 int error = FALSE;
7565
7566 start = (long)get_tv_number_chk(&argvars[2], &error);
7567 if (error)
7568 goto theend;
7569 if (l != NULL)
7570 {
7571 li = list_find(l, start);
7572 if (li == NULL)
7573 goto theend;
7574 idx = l->lv_idx; /* use the cached index */
7575 }
7576 else
7577 {
7578 if (start < 0)
7579 start = 0;
7580 if (start > len)
7581 goto theend;
7582 /* When "count" argument is there ignore matches before "start",
7583 * otherwise skip part of the string. Differs when pattern is "^"
7584 * or "\<". */
7585 if (argvars[3].v_type != VAR_UNKNOWN)
7586 startcol = start;
7587 else
7588 {
7589 str += start;
7590 len -= start;
7591 }
7592 }
7593
7594 if (argvars[3].v_type != VAR_UNKNOWN)
7595 nth = (long)get_tv_number_chk(&argvars[3], &error);
7596 if (error)
7597 goto theend;
7598 }
7599
7600 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7601 if (regmatch.regprog != NULL)
7602 {
7603 regmatch.rm_ic = p_ic;
7604
7605 for (;;)
7606 {
7607 if (l != NULL)
7608 {
7609 if (li == NULL)
7610 {
7611 match = FALSE;
7612 break;
7613 }
7614 vim_free(tofree);
7615 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7616 if (str == NULL)
7617 break;
7618 }
7619
7620 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7621
7622 if (match && --nth <= 0)
7623 break;
7624 if (l == NULL && !match)
7625 break;
7626
7627 /* Advance to just after the match. */
7628 if (l != NULL)
7629 {
7630 li = li->li_next;
7631 ++idx;
7632 }
7633 else
7634 {
7635#ifdef FEAT_MBYTE
7636 startcol = (colnr_T)(regmatch.startp[0]
7637 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7638#else
7639 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7640#endif
7641 if (startcol > (colnr_T)len
7642 || str + startcol <= regmatch.startp[0])
7643 {
7644 match = FALSE;
7645 break;
7646 }
7647 }
7648 }
7649
7650 if (match)
7651 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007652 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007653 {
7654 listitem_T *li1 = rettv->vval.v_list->lv_first;
7655 listitem_T *li2 = li1->li_next;
7656 listitem_T *li3 = li2->li_next;
7657 listitem_T *li4 = li3->li_next;
7658
7659 vim_free(li1->li_tv.vval.v_string);
7660 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7661 (int)(regmatch.endp[0] - regmatch.startp[0]));
7662 li3->li_tv.vval.v_number =
7663 (varnumber_T)(regmatch.startp[0] - expr);
7664 li4->li_tv.vval.v_number =
7665 (varnumber_T)(regmatch.endp[0] - expr);
7666 if (l != NULL)
7667 li2->li_tv.vval.v_number = (varnumber_T)idx;
7668 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007669 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007670 {
7671 int i;
7672
7673 /* return list with matched string and submatches */
7674 for (i = 0; i < NSUBEXP; ++i)
7675 {
7676 if (regmatch.endp[i] == NULL)
7677 {
7678 if (list_append_string(rettv->vval.v_list,
7679 (char_u *)"", 0) == FAIL)
7680 break;
7681 }
7682 else if (list_append_string(rettv->vval.v_list,
7683 regmatch.startp[i],
7684 (int)(regmatch.endp[i] - regmatch.startp[i]))
7685 == FAIL)
7686 break;
7687 }
7688 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007689 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007690 {
7691 /* return matched string */
7692 if (l != NULL)
7693 copy_tv(&li->li_tv, rettv);
7694 else
7695 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7696 (int)(regmatch.endp[0] - regmatch.startp[0]));
7697 }
7698 else if (l != NULL)
7699 rettv->vval.v_number = idx;
7700 else
7701 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007702 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007703 rettv->vval.v_number =
7704 (varnumber_T)(regmatch.startp[0] - str);
7705 else
7706 rettv->vval.v_number =
7707 (varnumber_T)(regmatch.endp[0] - str);
7708 rettv->vval.v_number += (varnumber_T)(str - expr);
7709 }
7710 }
7711 vim_regfree(regmatch.regprog);
7712 }
7713
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007714theend:
7715 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007716 /* matchstrpos() without a list: drop the second item. */
7717 listitem_remove(rettv->vval.v_list,
7718 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007719 vim_free(tofree);
7720 p_cpo = save_cpo;
7721}
7722
7723/*
7724 * "match()" function
7725 */
7726 static void
7727f_match(typval_T *argvars, typval_T *rettv)
7728{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007729 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007730}
7731
7732/*
7733 * "matchadd()" function
7734 */
7735 static void
7736f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7737{
7738#ifdef FEAT_SEARCH_EXTRA
7739 char_u buf[NUMBUFLEN];
7740 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7741 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7742 int prio = 10; /* default priority */
7743 int id = -1;
7744 int error = FALSE;
7745 char_u *conceal_char = NULL;
7746
7747 rettv->vval.v_number = -1;
7748
7749 if (grp == NULL || pat == NULL)
7750 return;
7751 if (argvars[2].v_type != VAR_UNKNOWN)
7752 {
7753 prio = (int)get_tv_number_chk(&argvars[2], &error);
7754 if (argvars[3].v_type != VAR_UNKNOWN)
7755 {
7756 id = (int)get_tv_number_chk(&argvars[3], &error);
7757 if (argvars[4].v_type != VAR_UNKNOWN)
7758 {
7759 if (argvars[4].v_type != VAR_DICT)
7760 {
7761 EMSG(_(e_dictreq));
7762 return;
7763 }
7764 if (dict_find(argvars[4].vval.v_dict,
7765 (char_u *)"conceal", -1) != NULL)
7766 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7767 (char_u *)"conceal", FALSE);
7768 }
7769 }
7770 }
7771 if (error == TRUE)
7772 return;
7773 if (id >= 1 && id <= 3)
7774 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007775 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007776 return;
7777 }
7778
7779 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7780 conceal_char);
7781#endif
7782}
7783
7784/*
7785 * "matchaddpos()" function
7786 */
7787 static void
7788f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7789{
7790#ifdef FEAT_SEARCH_EXTRA
7791 char_u buf[NUMBUFLEN];
7792 char_u *group;
7793 int prio = 10;
7794 int id = -1;
7795 int error = FALSE;
7796 list_T *l;
7797 char_u *conceal_char = NULL;
7798
7799 rettv->vval.v_number = -1;
7800
7801 group = get_tv_string_buf_chk(&argvars[0], buf);
7802 if (group == NULL)
7803 return;
7804
7805 if (argvars[1].v_type != VAR_LIST)
7806 {
7807 EMSG2(_(e_listarg), "matchaddpos()");
7808 return;
7809 }
7810 l = argvars[1].vval.v_list;
7811 if (l == NULL)
7812 return;
7813
7814 if (argvars[2].v_type != VAR_UNKNOWN)
7815 {
7816 prio = (int)get_tv_number_chk(&argvars[2], &error);
7817 if (argvars[3].v_type != VAR_UNKNOWN)
7818 {
7819 id = (int)get_tv_number_chk(&argvars[3], &error);
7820 if (argvars[4].v_type != VAR_UNKNOWN)
7821 {
7822 if (argvars[4].v_type != VAR_DICT)
7823 {
7824 EMSG(_(e_dictreq));
7825 return;
7826 }
7827 if (dict_find(argvars[4].vval.v_dict,
7828 (char_u *)"conceal", -1) != NULL)
7829 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7830 (char_u *)"conceal", FALSE);
7831 }
7832 }
7833 }
7834 if (error == TRUE)
7835 return;
7836
7837 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7838 if (id == 1 || id == 2)
7839 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007840 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007841 return;
7842 }
7843
7844 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7845 conceal_char);
7846#endif
7847}
7848
7849/*
7850 * "matcharg()" function
7851 */
7852 static void
7853f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7854{
7855 if (rettv_list_alloc(rettv) == OK)
7856 {
7857#ifdef FEAT_SEARCH_EXTRA
7858 int id = (int)get_tv_number(&argvars[0]);
7859 matchitem_T *m;
7860
7861 if (id >= 1 && id <= 3)
7862 {
7863 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7864 {
7865 list_append_string(rettv->vval.v_list,
7866 syn_id2name(m->hlg_id), -1);
7867 list_append_string(rettv->vval.v_list, m->pattern, -1);
7868 }
7869 else
7870 {
7871 list_append_string(rettv->vval.v_list, NULL, -1);
7872 list_append_string(rettv->vval.v_list, NULL, -1);
7873 }
7874 }
7875#endif
7876 }
7877}
7878
7879/*
7880 * "matchdelete()" function
7881 */
7882 static void
7883f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7884{
7885#ifdef FEAT_SEARCH_EXTRA
7886 rettv->vval.v_number = match_delete(curwin,
7887 (int)get_tv_number(&argvars[0]), TRUE);
7888#endif
7889}
7890
7891/*
7892 * "matchend()" function
7893 */
7894 static void
7895f_matchend(typval_T *argvars, typval_T *rettv)
7896{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007897 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007898}
7899
7900/*
7901 * "matchlist()" function
7902 */
7903 static void
7904f_matchlist(typval_T *argvars, typval_T *rettv)
7905{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007906 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007907}
7908
7909/*
7910 * "matchstr()" function
7911 */
7912 static void
7913f_matchstr(typval_T *argvars, typval_T *rettv)
7914{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007915 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007916}
7917
7918/*
7919 * "matchstrpos()" function
7920 */
7921 static void
7922f_matchstrpos(typval_T *argvars, typval_T *rettv)
7923{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007924 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007925}
7926
7927static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7928
7929 static void
7930max_min(typval_T *argvars, typval_T *rettv, int domax)
7931{
7932 varnumber_T n = 0;
7933 varnumber_T i;
7934 int error = FALSE;
7935
7936 if (argvars[0].v_type == VAR_LIST)
7937 {
7938 list_T *l;
7939 listitem_T *li;
7940
7941 l = argvars[0].vval.v_list;
7942 if (l != NULL)
7943 {
7944 li = l->lv_first;
7945 if (li != NULL)
7946 {
7947 n = get_tv_number_chk(&li->li_tv, &error);
7948 for (;;)
7949 {
7950 li = li->li_next;
7951 if (li == NULL)
7952 break;
7953 i = get_tv_number_chk(&li->li_tv, &error);
7954 if (domax ? i > n : i < n)
7955 n = i;
7956 }
7957 }
7958 }
7959 }
7960 else if (argvars[0].v_type == VAR_DICT)
7961 {
7962 dict_T *d;
7963 int first = TRUE;
7964 hashitem_T *hi;
7965 int todo;
7966
7967 d = argvars[0].vval.v_dict;
7968 if (d != NULL)
7969 {
7970 todo = (int)d->dv_hashtab.ht_used;
7971 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7972 {
7973 if (!HASHITEM_EMPTY(hi))
7974 {
7975 --todo;
7976 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7977 if (first)
7978 {
7979 n = i;
7980 first = FALSE;
7981 }
7982 else if (domax ? i > n : i < n)
7983 n = i;
7984 }
7985 }
7986 }
7987 }
7988 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007989 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007990 rettv->vval.v_number = error ? 0 : n;
7991}
7992
7993/*
7994 * "max()" function
7995 */
7996 static void
7997f_max(typval_T *argvars, typval_T *rettv)
7998{
7999 max_min(argvars, rettv, TRUE);
8000}
8001
8002/*
8003 * "min()" function
8004 */
8005 static void
8006f_min(typval_T *argvars, typval_T *rettv)
8007{
8008 max_min(argvars, rettv, FALSE);
8009}
8010
8011static int mkdir_recurse(char_u *dir, int prot);
8012
8013/*
8014 * Create the directory in which "dir" is located, and higher levels when
8015 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008016 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008017 */
8018 static int
8019mkdir_recurse(char_u *dir, int prot)
8020{
8021 char_u *p;
8022 char_u *updir;
8023 int r = FAIL;
8024
8025 /* Get end of directory name in "dir".
8026 * We're done when it's "/" or "c:/". */
8027 p = gettail_sep(dir);
8028 if (p <= get_past_head(dir))
8029 return OK;
8030
8031 /* If the directory exists we're done. Otherwise: create it.*/
8032 updir = vim_strnsave(dir, (int)(p - dir));
8033 if (updir == NULL)
8034 return FAIL;
8035 if (mch_isdir(updir))
8036 r = OK;
8037 else if (mkdir_recurse(updir, prot) == OK)
8038 r = vim_mkdir_emsg(updir, prot);
8039 vim_free(updir);
8040 return r;
8041}
8042
8043#ifdef vim_mkdir
8044/*
8045 * "mkdir()" function
8046 */
8047 static void
8048f_mkdir(typval_T *argvars, typval_T *rettv)
8049{
8050 char_u *dir;
8051 char_u buf[NUMBUFLEN];
8052 int prot = 0755;
8053
8054 rettv->vval.v_number = FAIL;
8055 if (check_restricted() || check_secure())
8056 return;
8057
8058 dir = get_tv_string_buf(&argvars[0], buf);
8059 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008060 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008061
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008062 if (*gettail(dir) == NUL)
8063 /* remove trailing slashes */
8064 *gettail_sep(dir) = NUL;
8065
8066 if (argvars[1].v_type != VAR_UNKNOWN)
8067 {
8068 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008069 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008070 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8071 if (prot == -1)
8072 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008073 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008074 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8075 {
8076 if (mch_isdir(dir))
8077 {
8078 /* With the "p" flag it's OK if the dir already exists. */
8079 rettv->vval.v_number = OK;
8080 return;
8081 }
8082 mkdir_recurse(dir, prot);
8083 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008084 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008085 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008086}
8087#endif
8088
8089/*
8090 * "mode()" function
8091 */
8092 static void
8093f_mode(typval_T *argvars, typval_T *rettv)
8094{
8095 char_u buf[3];
8096
8097 buf[1] = NUL;
8098 buf[2] = NUL;
8099
8100 if (time_for_testing == 93784)
8101 {
8102 /* Testing the two-character code. */
8103 buf[0] = 'x';
8104 buf[1] = '!';
8105 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008106#ifdef FEAT_TERMINAL
8107 else if (term_use_loop())
8108 buf[0] = 't';
8109#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008110 else if (VIsual_active)
8111 {
8112 if (VIsual_select)
8113 buf[0] = VIsual_mode + 's' - 'v';
8114 else
8115 buf[0] = VIsual_mode;
8116 }
8117 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8118 || State == CONFIRM)
8119 {
8120 buf[0] = 'r';
8121 if (State == ASKMORE)
8122 buf[1] = 'm';
8123 else if (State == CONFIRM)
8124 buf[1] = '?';
8125 }
8126 else if (State == EXTERNCMD)
8127 buf[0] = '!';
8128 else if (State & INSERT)
8129 {
8130#ifdef FEAT_VREPLACE
8131 if (State & VREPLACE_FLAG)
8132 {
8133 buf[0] = 'R';
8134 buf[1] = 'v';
8135 }
8136 else
8137#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008138 {
8139 if (State & REPLACE_FLAG)
8140 buf[0] = 'R';
8141 else
8142 buf[0] = 'i';
8143#ifdef FEAT_INS_EXPAND
8144 if (ins_compl_active())
8145 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008146 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008147 buf[1] = 'x';
8148#endif
8149 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008150 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008151 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008152 {
8153 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008154 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008155 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008156 else if (exmode_active == EXMODE_NORMAL)
8157 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008158 }
8159 else
8160 {
8161 buf[0] = 'n';
8162 if (finish_op)
8163 buf[1] = 'o';
8164 }
8165
8166 /* Clear out the minor mode when the argument is not a non-zero number or
8167 * non-empty string. */
8168 if (!non_zero_arg(&argvars[0]))
8169 buf[1] = NUL;
8170
8171 rettv->vval.v_string = vim_strsave(buf);
8172 rettv->v_type = VAR_STRING;
8173}
8174
8175#if defined(FEAT_MZSCHEME) || defined(PROTO)
8176/*
8177 * "mzeval()" function
8178 */
8179 static void
8180f_mzeval(typval_T *argvars, typval_T *rettv)
8181{
8182 char_u *str;
8183 char_u buf[NUMBUFLEN];
8184
8185 str = get_tv_string_buf(&argvars[0], buf);
8186 do_mzeval(str, rettv);
8187}
8188
8189 void
8190mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8191{
8192 typval_T argvars[3];
8193
8194 argvars[0].v_type = VAR_STRING;
8195 argvars[0].vval.v_string = name;
8196 copy_tv(args, &argvars[1]);
8197 argvars[2].v_type = VAR_UNKNOWN;
8198 f_call(argvars, rettv);
8199 clear_tv(&argvars[1]);
8200}
8201#endif
8202
8203/*
8204 * "nextnonblank()" function
8205 */
8206 static void
8207f_nextnonblank(typval_T *argvars, typval_T *rettv)
8208{
8209 linenr_T lnum;
8210
8211 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8212 {
8213 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8214 {
8215 lnum = 0;
8216 break;
8217 }
8218 if (*skipwhite(ml_get(lnum)) != NUL)
8219 break;
8220 }
8221 rettv->vval.v_number = lnum;
8222}
8223
8224/*
8225 * "nr2char()" function
8226 */
8227 static void
8228f_nr2char(typval_T *argvars, typval_T *rettv)
8229{
8230 char_u buf[NUMBUFLEN];
8231
8232#ifdef FEAT_MBYTE
8233 if (has_mbyte)
8234 {
8235 int utf8 = 0;
8236
8237 if (argvars[1].v_type != VAR_UNKNOWN)
8238 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8239 if (utf8)
8240 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8241 else
8242 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8243 }
8244 else
8245#endif
8246 {
8247 buf[0] = (char_u)get_tv_number(&argvars[0]);
8248 buf[1] = NUL;
8249 }
8250 rettv->v_type = VAR_STRING;
8251 rettv->vval.v_string = vim_strsave(buf);
8252}
8253
8254/*
8255 * "or(expr, expr)" function
8256 */
8257 static void
8258f_or(typval_T *argvars, typval_T *rettv)
8259{
8260 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8261 | get_tv_number_chk(&argvars[1], NULL);
8262}
8263
8264/*
8265 * "pathshorten()" function
8266 */
8267 static void
8268f_pathshorten(typval_T *argvars, typval_T *rettv)
8269{
8270 char_u *p;
8271
8272 rettv->v_type = VAR_STRING;
8273 p = get_tv_string_chk(&argvars[0]);
8274 if (p == NULL)
8275 rettv->vval.v_string = NULL;
8276 else
8277 {
8278 p = vim_strsave(p);
8279 rettv->vval.v_string = p;
8280 if (p != NULL)
8281 shorten_dir(p);
8282 }
8283}
8284
8285#ifdef FEAT_PERL
8286/*
8287 * "perleval()" function
8288 */
8289 static void
8290f_perleval(typval_T *argvars, typval_T *rettv)
8291{
8292 char_u *str;
8293 char_u buf[NUMBUFLEN];
8294
8295 str = get_tv_string_buf(&argvars[0], buf);
8296 do_perleval(str, rettv);
8297}
8298#endif
8299
8300#ifdef FEAT_FLOAT
8301/*
8302 * "pow()" function
8303 */
8304 static void
8305f_pow(typval_T *argvars, typval_T *rettv)
8306{
8307 float_T fx = 0.0, fy = 0.0;
8308
8309 rettv->v_type = VAR_FLOAT;
8310 if (get_float_arg(argvars, &fx) == OK
8311 && get_float_arg(&argvars[1], &fy) == OK)
8312 rettv->vval.v_float = pow(fx, fy);
8313 else
8314 rettv->vval.v_float = 0.0;
8315}
8316#endif
8317
8318/*
8319 * "prevnonblank()" function
8320 */
8321 static void
8322f_prevnonblank(typval_T *argvars, typval_T *rettv)
8323{
8324 linenr_T lnum;
8325
8326 lnum = get_tv_lnum(argvars);
8327 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8328 lnum = 0;
8329 else
8330 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8331 --lnum;
8332 rettv->vval.v_number = lnum;
8333}
8334
8335/* This dummy va_list is here because:
8336 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8337 * - locally in the function results in a "used before set" warning
8338 * - using va_start() to initialize it gives "function with fixed args" error */
8339static va_list ap;
8340
8341/*
8342 * "printf()" function
8343 */
8344 static void
8345f_printf(typval_T *argvars, typval_T *rettv)
8346{
8347 char_u buf[NUMBUFLEN];
8348 int len;
8349 char_u *s;
8350 int saved_did_emsg = did_emsg;
8351 char *fmt;
8352
8353 rettv->v_type = VAR_STRING;
8354 rettv->vval.v_string = NULL;
8355
8356 /* Get the required length, allocate the buffer and do it for real. */
8357 did_emsg = FALSE;
8358 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008359 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008360 if (!did_emsg)
8361 {
8362 s = alloc(len + 1);
8363 if (s != NULL)
8364 {
8365 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008366 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8367 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008368 }
8369 }
8370 did_emsg |= saved_did_emsg;
8371}
8372
8373/*
8374 * "pumvisible()" function
8375 */
8376 static void
8377f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8378{
8379#ifdef FEAT_INS_EXPAND
8380 if (pum_visible())
8381 rettv->vval.v_number = 1;
8382#endif
8383}
8384
8385#ifdef FEAT_PYTHON3
8386/*
8387 * "py3eval()" function
8388 */
8389 static void
8390f_py3eval(typval_T *argvars, typval_T *rettv)
8391{
8392 char_u *str;
8393 char_u buf[NUMBUFLEN];
8394
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008395 if (p_pyx == 0)
8396 p_pyx = 3;
8397
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008398 str = get_tv_string_buf(&argvars[0], buf);
8399 do_py3eval(str, rettv);
8400}
8401#endif
8402
8403#ifdef FEAT_PYTHON
8404/*
8405 * "pyeval()" function
8406 */
8407 static void
8408f_pyeval(typval_T *argvars, typval_T *rettv)
8409{
8410 char_u *str;
8411 char_u buf[NUMBUFLEN];
8412
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008413 if (p_pyx == 0)
8414 p_pyx = 2;
8415
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008416 str = get_tv_string_buf(&argvars[0], buf);
8417 do_pyeval(str, rettv);
8418}
8419#endif
8420
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008421#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8422/*
8423 * "pyxeval()" function
8424 */
8425 static void
8426f_pyxeval(typval_T *argvars, typval_T *rettv)
8427{
8428# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8429 init_pyxversion();
8430 if (p_pyx == 2)
8431 f_pyeval(argvars, rettv);
8432 else
8433 f_py3eval(argvars, rettv);
8434# elif defined(FEAT_PYTHON)
8435 f_pyeval(argvars, rettv);
8436# elif defined(FEAT_PYTHON3)
8437 f_py3eval(argvars, rettv);
8438# endif
8439}
8440#endif
8441
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008442/*
8443 * "range()" function
8444 */
8445 static void
8446f_range(typval_T *argvars, typval_T *rettv)
8447{
8448 varnumber_T start;
8449 varnumber_T end;
8450 varnumber_T stride = 1;
8451 varnumber_T i;
8452 int error = FALSE;
8453
8454 start = get_tv_number_chk(&argvars[0], &error);
8455 if (argvars[1].v_type == VAR_UNKNOWN)
8456 {
8457 end = start - 1;
8458 start = 0;
8459 }
8460 else
8461 {
8462 end = get_tv_number_chk(&argvars[1], &error);
8463 if (argvars[2].v_type != VAR_UNKNOWN)
8464 stride = get_tv_number_chk(&argvars[2], &error);
8465 }
8466
8467 if (error)
8468 return; /* type error; errmsg already given */
8469 if (stride == 0)
8470 EMSG(_("E726: Stride is zero"));
8471 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8472 EMSG(_("E727: Start past end"));
8473 else
8474 {
8475 if (rettv_list_alloc(rettv) == OK)
8476 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8477 if (list_append_number(rettv->vval.v_list,
8478 (varnumber_T)i) == FAIL)
8479 break;
8480 }
8481}
8482
8483/*
8484 * "readfile()" function
8485 */
8486 static void
8487f_readfile(typval_T *argvars, typval_T *rettv)
8488{
8489 int binary = FALSE;
8490 int failed = FALSE;
8491 char_u *fname;
8492 FILE *fd;
8493 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8494 int io_size = sizeof(buf);
8495 int readlen; /* size of last fread() */
8496 char_u *prev = NULL; /* previously read bytes, if any */
8497 long prevlen = 0; /* length of data in prev */
8498 long prevsize = 0; /* size of prev buffer */
8499 long maxline = MAXLNUM;
8500 long cnt = 0;
8501 char_u *p; /* position in buf */
8502 char_u *start; /* start of current line */
8503
8504 if (argvars[1].v_type != VAR_UNKNOWN)
8505 {
8506 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8507 binary = TRUE;
8508 if (argvars[2].v_type != VAR_UNKNOWN)
8509 maxline = (long)get_tv_number(&argvars[2]);
8510 }
8511
8512 if (rettv_list_alloc(rettv) == FAIL)
8513 return;
8514
8515 /* Always open the file in binary mode, library functions have a mind of
8516 * their own about CR-LF conversion. */
8517 fname = get_tv_string(&argvars[0]);
8518 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8519 {
8520 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8521 return;
8522 }
8523
8524 while (cnt < maxline || maxline < 0)
8525 {
8526 readlen = (int)fread(buf, 1, io_size, fd);
8527
8528 /* This for loop processes what was read, but is also entered at end
8529 * of file so that either:
8530 * - an incomplete line gets written
8531 * - a "binary" file gets an empty line at the end if it ends in a
8532 * newline. */
8533 for (p = buf, start = buf;
8534 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8535 ++p)
8536 {
8537 if (*p == '\n' || readlen <= 0)
8538 {
8539 listitem_T *li;
8540 char_u *s = NULL;
8541 long_u len = p - start;
8542
8543 /* Finished a line. Remove CRs before NL. */
8544 if (readlen > 0 && !binary)
8545 {
8546 while (len > 0 && start[len - 1] == '\r')
8547 --len;
8548 /* removal may cross back to the "prev" string */
8549 if (len == 0)
8550 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8551 --prevlen;
8552 }
8553 if (prevlen == 0)
8554 s = vim_strnsave(start, (int)len);
8555 else
8556 {
8557 /* Change "prev" buffer to be the right size. This way
8558 * the bytes are only copied once, and very long lines are
8559 * allocated only once. */
8560 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8561 {
8562 mch_memmove(s + prevlen, start, len);
8563 s[prevlen + len] = NUL;
8564 prev = NULL; /* the list will own the string */
8565 prevlen = prevsize = 0;
8566 }
8567 }
8568 if (s == NULL)
8569 {
8570 do_outofmem_msg((long_u) prevlen + len + 1);
8571 failed = TRUE;
8572 break;
8573 }
8574
8575 if ((li = listitem_alloc()) == NULL)
8576 {
8577 vim_free(s);
8578 failed = TRUE;
8579 break;
8580 }
8581 li->li_tv.v_type = VAR_STRING;
8582 li->li_tv.v_lock = 0;
8583 li->li_tv.vval.v_string = s;
8584 list_append(rettv->vval.v_list, li);
8585
8586 start = p + 1; /* step over newline */
8587 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8588 break;
8589 }
8590 else if (*p == NUL)
8591 *p = '\n';
8592#ifdef FEAT_MBYTE
8593 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8594 * when finding the BF and check the previous two bytes. */
8595 else if (*p == 0xbf && enc_utf8 && !binary)
8596 {
8597 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8598 * + 1, these may be in the "prev" string. */
8599 char_u back1 = p >= buf + 1 ? p[-1]
8600 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8601 char_u back2 = p >= buf + 2 ? p[-2]
8602 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8603 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8604
8605 if (back2 == 0xef && back1 == 0xbb)
8606 {
8607 char_u *dest = p - 2;
8608
8609 /* Usually a BOM is at the beginning of a file, and so at
8610 * the beginning of a line; then we can just step over it.
8611 */
8612 if (start == dest)
8613 start = p + 1;
8614 else
8615 {
8616 /* have to shuffle buf to close gap */
8617 int adjust_prevlen = 0;
8618
8619 if (dest < buf)
8620 {
8621 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8622 dest = buf;
8623 }
8624 if (readlen > p - buf + 1)
8625 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8626 readlen -= 3 - adjust_prevlen;
8627 prevlen -= adjust_prevlen;
8628 p = dest - 1;
8629 }
8630 }
8631 }
8632#endif
8633 } /* for */
8634
8635 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8636 break;
8637 if (start < p)
8638 {
8639 /* There's part of a line in buf, store it in "prev". */
8640 if (p - start + prevlen >= prevsize)
8641 {
8642 /* need bigger "prev" buffer */
8643 char_u *newprev;
8644
8645 /* A common use case is ordinary text files and "prev" gets a
8646 * fragment of a line, so the first allocation is made
8647 * small, to avoid repeatedly 'allocing' large and
8648 * 'reallocing' small. */
8649 if (prevsize == 0)
8650 prevsize = (long)(p - start);
8651 else
8652 {
8653 long grow50pc = (prevsize * 3) / 2;
8654 long growmin = (long)((p - start) * 2 + prevlen);
8655 prevsize = grow50pc > growmin ? grow50pc : growmin;
8656 }
8657 newprev = prev == NULL ? alloc(prevsize)
8658 : vim_realloc(prev, prevsize);
8659 if (newprev == NULL)
8660 {
8661 do_outofmem_msg((long_u)prevsize);
8662 failed = TRUE;
8663 break;
8664 }
8665 prev = newprev;
8666 }
8667 /* Add the line part to end of "prev". */
8668 mch_memmove(prev + prevlen, start, p - start);
8669 prevlen += (long)(p - start);
8670 }
8671 } /* while */
8672
8673 /*
8674 * For a negative line count use only the lines at the end of the file,
8675 * free the rest.
8676 */
8677 if (!failed && maxline < 0)
8678 while (cnt > -maxline)
8679 {
8680 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8681 --cnt;
8682 }
8683
8684 if (failed)
8685 {
8686 list_free(rettv->vval.v_list);
8687 /* readfile doc says an empty list is returned on error */
8688 rettv->vval.v_list = list_alloc();
8689 }
8690
8691 vim_free(prev);
8692 fclose(fd);
8693}
8694
8695#if defined(FEAT_RELTIME)
8696static int list2proftime(typval_T *arg, proftime_T *tm);
8697
8698/*
8699 * Convert a List to proftime_T.
8700 * Return FAIL when there is something wrong.
8701 */
8702 static int
8703list2proftime(typval_T *arg, proftime_T *tm)
8704{
8705 long n1, n2;
8706 int error = FALSE;
8707
8708 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8709 || arg->vval.v_list->lv_len != 2)
8710 return FAIL;
8711 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8712 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8713# ifdef WIN3264
8714 tm->HighPart = n1;
8715 tm->LowPart = n2;
8716# else
8717 tm->tv_sec = n1;
8718 tm->tv_usec = n2;
8719# endif
8720 return error ? FAIL : OK;
8721}
8722#endif /* FEAT_RELTIME */
8723
8724/*
8725 * "reltime()" function
8726 */
8727 static void
8728f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8729{
8730#ifdef FEAT_RELTIME
8731 proftime_T res;
8732 proftime_T start;
8733
8734 if (argvars[0].v_type == VAR_UNKNOWN)
8735 {
8736 /* No arguments: get current time. */
8737 profile_start(&res);
8738 }
8739 else if (argvars[1].v_type == VAR_UNKNOWN)
8740 {
8741 if (list2proftime(&argvars[0], &res) == FAIL)
8742 return;
8743 profile_end(&res);
8744 }
8745 else
8746 {
8747 /* Two arguments: compute the difference. */
8748 if (list2proftime(&argvars[0], &start) == FAIL
8749 || list2proftime(&argvars[1], &res) == FAIL)
8750 return;
8751 profile_sub(&res, &start);
8752 }
8753
8754 if (rettv_list_alloc(rettv) == OK)
8755 {
8756 long n1, n2;
8757
8758# ifdef WIN3264
8759 n1 = res.HighPart;
8760 n2 = res.LowPart;
8761# else
8762 n1 = res.tv_sec;
8763 n2 = res.tv_usec;
8764# endif
8765 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8766 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8767 }
8768#endif
8769}
8770
8771#ifdef FEAT_FLOAT
8772/*
8773 * "reltimefloat()" function
8774 */
8775 static void
8776f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8777{
8778# ifdef FEAT_RELTIME
8779 proftime_T tm;
8780# endif
8781
8782 rettv->v_type = VAR_FLOAT;
8783 rettv->vval.v_float = 0;
8784# ifdef FEAT_RELTIME
8785 if (list2proftime(&argvars[0], &tm) == OK)
8786 rettv->vval.v_float = profile_float(&tm);
8787# endif
8788}
8789#endif
8790
8791/*
8792 * "reltimestr()" function
8793 */
8794 static void
8795f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8796{
8797#ifdef FEAT_RELTIME
8798 proftime_T tm;
8799#endif
8800
8801 rettv->v_type = VAR_STRING;
8802 rettv->vval.v_string = NULL;
8803#ifdef FEAT_RELTIME
8804 if (list2proftime(&argvars[0], &tm) == OK)
8805 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8806#endif
8807}
8808
8809#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8810static void make_connection(void);
8811static int check_connection(void);
8812
8813 static void
8814make_connection(void)
8815{
8816 if (X_DISPLAY == NULL
8817# ifdef FEAT_GUI
8818 && !gui.in_use
8819# endif
8820 )
8821 {
8822 x_force_connect = TRUE;
8823 setup_term_clip();
8824 x_force_connect = FALSE;
8825 }
8826}
8827
8828 static int
8829check_connection(void)
8830{
8831 make_connection();
8832 if (X_DISPLAY == NULL)
8833 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008834 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835 return FAIL;
8836 }
8837 return OK;
8838}
8839#endif
8840
8841#ifdef FEAT_CLIENTSERVER
8842 static void
8843remote_common(typval_T *argvars, typval_T *rettv, int expr)
8844{
8845 char_u *server_name;
8846 char_u *keys;
8847 char_u *r = NULL;
8848 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008849 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008850# ifdef WIN32
8851 HWND w;
8852# else
8853 Window w;
8854# endif
8855
8856 if (check_restricted() || check_secure())
8857 return;
8858
8859# ifdef FEAT_X11
8860 if (check_connection() == FAIL)
8861 return;
8862# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008863 if (argvars[2].v_type != VAR_UNKNOWN
8864 && argvars[3].v_type != VAR_UNKNOWN)
8865 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008866
8867 server_name = get_tv_string_chk(&argvars[0]);
8868 if (server_name == NULL)
8869 return; /* type error; errmsg already given */
8870 keys = get_tv_string_buf(&argvars[1], buf);
8871# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008872 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008873# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008874 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8875 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876# endif
8877 {
8878 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008879 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008880 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008881 vim_free(r);
8882 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008883 else
8884 EMSG2(_("E241: Unable to send to %s"), server_name);
8885 return;
8886 }
8887
8888 rettv->vval.v_string = r;
8889
8890 if (argvars[2].v_type != VAR_UNKNOWN)
8891 {
8892 dictitem_T v;
8893 char_u str[30];
8894 char_u *idvar;
8895
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008896 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008897 if (idvar != NULL && *idvar != NUL)
8898 {
8899 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8900 v.di_tv.v_type = VAR_STRING;
8901 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008902 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008903 vim_free(v.di_tv.vval.v_string);
8904 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008905 }
8906}
8907#endif
8908
8909/*
8910 * "remote_expr()" function
8911 */
8912 static void
8913f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8914{
8915 rettv->v_type = VAR_STRING;
8916 rettv->vval.v_string = NULL;
8917#ifdef FEAT_CLIENTSERVER
8918 remote_common(argvars, rettv, TRUE);
8919#endif
8920}
8921
8922/*
8923 * "remote_foreground()" function
8924 */
8925 static void
8926f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8927{
8928#ifdef FEAT_CLIENTSERVER
8929# ifdef WIN32
8930 /* On Win32 it's done in this application. */
8931 {
8932 char_u *server_name = get_tv_string_chk(&argvars[0]);
8933
8934 if (server_name != NULL)
8935 serverForeground(server_name);
8936 }
8937# else
8938 /* Send a foreground() expression to the server. */
8939 argvars[1].v_type = VAR_STRING;
8940 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8941 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008942 rettv->v_type = VAR_STRING;
8943 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008944 remote_common(argvars, rettv, TRUE);
8945 vim_free(argvars[1].vval.v_string);
8946# endif
8947#endif
8948}
8949
8950 static void
8951f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8952{
8953#ifdef FEAT_CLIENTSERVER
8954 dictitem_T v;
8955 char_u *s = NULL;
8956# ifdef WIN32
8957 long_u n = 0;
8958# endif
8959 char_u *serverid;
8960
8961 if (check_restricted() || check_secure())
8962 {
8963 rettv->vval.v_number = -1;
8964 return;
8965 }
8966 serverid = get_tv_string_chk(&argvars[0]);
8967 if (serverid == NULL)
8968 {
8969 rettv->vval.v_number = -1;
8970 return; /* type error; errmsg already given */
8971 }
8972# ifdef WIN32
8973 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8974 if (n == 0)
8975 rettv->vval.v_number = -1;
8976 else
8977 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008978 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008979 rettv->vval.v_number = (s != NULL);
8980 }
8981# else
8982 if (check_connection() == FAIL)
8983 return;
8984
8985 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8986 serverStrToWin(serverid), &s);
8987# endif
8988
8989 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8990 {
8991 char_u *retvar;
8992
8993 v.di_tv.v_type = VAR_STRING;
8994 v.di_tv.vval.v_string = vim_strsave(s);
8995 retvar = get_tv_string_chk(&argvars[1]);
8996 if (retvar != NULL)
8997 set_var(retvar, &v.di_tv, FALSE);
8998 vim_free(v.di_tv.vval.v_string);
8999 }
9000#else
9001 rettv->vval.v_number = -1;
9002#endif
9003}
9004
9005 static void
9006f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9007{
9008 char_u *r = NULL;
9009
9010#ifdef FEAT_CLIENTSERVER
9011 char_u *serverid = get_tv_string_chk(&argvars[0]);
9012
9013 if (serverid != NULL && !check_restricted() && !check_secure())
9014 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009015 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009016# ifdef WIN32
9017 /* The server's HWND is encoded in the 'id' parameter */
9018 long_u n = 0;
9019# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009020
9021 if (argvars[1].v_type != VAR_UNKNOWN)
9022 timeout = get_tv_number(&argvars[1]);
9023
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009024# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009025 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9026 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009027 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009028 if (r == NULL)
9029# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009030 if (check_connection() == FAIL
9031 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9032 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033# endif
9034 EMSG(_("E277: Unable to read a server reply"));
9035 }
9036#endif
9037 rettv->v_type = VAR_STRING;
9038 rettv->vval.v_string = r;
9039}
9040
9041/*
9042 * "remote_send()" function
9043 */
9044 static void
9045f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9046{
9047 rettv->v_type = VAR_STRING;
9048 rettv->vval.v_string = NULL;
9049#ifdef FEAT_CLIENTSERVER
9050 remote_common(argvars, rettv, FALSE);
9051#endif
9052}
9053
9054/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009055 * "remote_startserver()" function
9056 */
9057 static void
9058f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9059{
9060#ifdef FEAT_CLIENTSERVER
9061 char_u *server = get_tv_string_chk(&argvars[0]);
9062
9063 if (server == NULL)
9064 return; /* type error; errmsg already given */
9065 if (serverName != NULL)
9066 EMSG(_("E941: already started a server"));
9067 else
9068 {
9069# ifdef FEAT_X11
9070 if (check_connection() == OK)
9071 serverRegisterName(X_DISPLAY, server);
9072# else
9073 serverSetName(server);
9074# endif
9075 }
9076#else
9077 EMSG(_("E942: +clientserver feature not available"));
9078#endif
9079}
9080
9081/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009082 * "remove()" function
9083 */
9084 static void
9085f_remove(typval_T *argvars, typval_T *rettv)
9086{
9087 list_T *l;
9088 listitem_T *item, *item2;
9089 listitem_T *li;
9090 long idx;
9091 long end;
9092 char_u *key;
9093 dict_T *d;
9094 dictitem_T *di;
9095 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9096
9097 if (argvars[0].v_type == VAR_DICT)
9098 {
9099 if (argvars[2].v_type != VAR_UNKNOWN)
9100 EMSG2(_(e_toomanyarg), "remove()");
9101 else if ((d = argvars[0].vval.v_dict) != NULL
9102 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9103 {
9104 key = get_tv_string_chk(&argvars[1]);
9105 if (key != NULL)
9106 {
9107 di = dict_find(d, key, -1);
9108 if (di == NULL)
9109 EMSG2(_(e_dictkey), key);
9110 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9111 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9112 {
9113 *rettv = di->di_tv;
9114 init_tv(&di->di_tv);
9115 dictitem_remove(d, di);
9116 }
9117 }
9118 }
9119 }
9120 else if (argvars[0].v_type != VAR_LIST)
9121 EMSG2(_(e_listdictarg), "remove()");
9122 else if ((l = argvars[0].vval.v_list) != NULL
9123 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9124 {
9125 int error = FALSE;
9126
9127 idx = (long)get_tv_number_chk(&argvars[1], &error);
9128 if (error)
9129 ; /* type error: do nothing, errmsg already given */
9130 else if ((item = list_find(l, idx)) == NULL)
9131 EMSGN(_(e_listidx), idx);
9132 else
9133 {
9134 if (argvars[2].v_type == VAR_UNKNOWN)
9135 {
9136 /* Remove one item, return its value. */
9137 vimlist_remove(l, item, item);
9138 *rettv = item->li_tv;
9139 vim_free(item);
9140 }
9141 else
9142 {
9143 /* Remove range of items, return list with values. */
9144 end = (long)get_tv_number_chk(&argvars[2], &error);
9145 if (error)
9146 ; /* type error: do nothing */
9147 else if ((item2 = list_find(l, end)) == NULL)
9148 EMSGN(_(e_listidx), end);
9149 else
9150 {
9151 int cnt = 0;
9152
9153 for (li = item; li != NULL; li = li->li_next)
9154 {
9155 ++cnt;
9156 if (li == item2)
9157 break;
9158 }
9159 if (li == NULL) /* didn't find "item2" after "item" */
9160 EMSG(_(e_invrange));
9161 else
9162 {
9163 vimlist_remove(l, item, item2);
9164 if (rettv_list_alloc(rettv) == OK)
9165 {
9166 l = rettv->vval.v_list;
9167 l->lv_first = item;
9168 l->lv_last = item2;
9169 item->li_prev = NULL;
9170 item2->li_next = NULL;
9171 l->lv_len = cnt;
9172 }
9173 }
9174 }
9175 }
9176 }
9177 }
9178}
9179
9180/*
9181 * "rename({from}, {to})" function
9182 */
9183 static void
9184f_rename(typval_T *argvars, typval_T *rettv)
9185{
9186 char_u buf[NUMBUFLEN];
9187
9188 if (check_restricted() || check_secure())
9189 rettv->vval.v_number = -1;
9190 else
9191 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9192 get_tv_string_buf(&argvars[1], buf));
9193}
9194
9195/*
9196 * "repeat()" function
9197 */
9198 static void
9199f_repeat(typval_T *argvars, typval_T *rettv)
9200{
9201 char_u *p;
9202 int n;
9203 int slen;
9204 int len;
9205 char_u *r;
9206 int i;
9207
9208 n = (int)get_tv_number(&argvars[1]);
9209 if (argvars[0].v_type == VAR_LIST)
9210 {
9211 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9212 while (n-- > 0)
9213 if (list_extend(rettv->vval.v_list,
9214 argvars[0].vval.v_list, NULL) == FAIL)
9215 break;
9216 }
9217 else
9218 {
9219 p = get_tv_string(&argvars[0]);
9220 rettv->v_type = VAR_STRING;
9221 rettv->vval.v_string = NULL;
9222
9223 slen = (int)STRLEN(p);
9224 len = slen * n;
9225 if (len <= 0)
9226 return;
9227
9228 r = alloc(len + 1);
9229 if (r != NULL)
9230 {
9231 for (i = 0; i < n; i++)
9232 mch_memmove(r + i * slen, p, (size_t)slen);
9233 r[len] = NUL;
9234 }
9235
9236 rettv->vval.v_string = r;
9237 }
9238}
9239
9240/*
9241 * "resolve()" function
9242 */
9243 static void
9244f_resolve(typval_T *argvars, typval_T *rettv)
9245{
9246 char_u *p;
9247#ifdef HAVE_READLINK
9248 char_u *buf = NULL;
9249#endif
9250
9251 p = get_tv_string(&argvars[0]);
9252#ifdef FEAT_SHORTCUT
9253 {
9254 char_u *v = NULL;
9255
9256 v = mch_resolve_shortcut(p);
9257 if (v != NULL)
9258 rettv->vval.v_string = v;
9259 else
9260 rettv->vval.v_string = vim_strsave(p);
9261 }
9262#else
9263# ifdef HAVE_READLINK
9264 {
9265 char_u *cpy;
9266 int len;
9267 char_u *remain = NULL;
9268 char_u *q;
9269 int is_relative_to_current = FALSE;
9270 int has_trailing_pathsep = FALSE;
9271 int limit = 100;
9272
9273 p = vim_strsave(p);
9274
9275 if (p[0] == '.' && (vim_ispathsep(p[1])
9276 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9277 is_relative_to_current = TRUE;
9278
9279 len = STRLEN(p);
9280 if (len > 0 && after_pathsep(p, p + len))
9281 {
9282 has_trailing_pathsep = TRUE;
9283 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9284 }
9285
9286 q = getnextcomp(p);
9287 if (*q != NUL)
9288 {
9289 /* Separate the first path component in "p", and keep the
9290 * remainder (beginning with the path separator). */
9291 remain = vim_strsave(q - 1);
9292 q[-1] = NUL;
9293 }
9294
9295 buf = alloc(MAXPATHL + 1);
9296 if (buf == NULL)
9297 goto fail;
9298
9299 for (;;)
9300 {
9301 for (;;)
9302 {
9303 len = readlink((char *)p, (char *)buf, MAXPATHL);
9304 if (len <= 0)
9305 break;
9306 buf[len] = NUL;
9307
9308 if (limit-- == 0)
9309 {
9310 vim_free(p);
9311 vim_free(remain);
9312 EMSG(_("E655: Too many symbolic links (cycle?)"));
9313 rettv->vval.v_string = NULL;
9314 goto fail;
9315 }
9316
9317 /* Ensure that the result will have a trailing path separator
9318 * if the argument has one. */
9319 if (remain == NULL && has_trailing_pathsep)
9320 add_pathsep(buf);
9321
9322 /* Separate the first path component in the link value and
9323 * concatenate the remainders. */
9324 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9325 if (*q != NUL)
9326 {
9327 if (remain == NULL)
9328 remain = vim_strsave(q - 1);
9329 else
9330 {
9331 cpy = concat_str(q - 1, remain);
9332 if (cpy != NULL)
9333 {
9334 vim_free(remain);
9335 remain = cpy;
9336 }
9337 }
9338 q[-1] = NUL;
9339 }
9340
9341 q = gettail(p);
9342 if (q > p && *q == NUL)
9343 {
9344 /* Ignore trailing path separator. */
9345 q[-1] = NUL;
9346 q = gettail(p);
9347 }
9348 if (q > p && !mch_isFullName(buf))
9349 {
9350 /* symlink is relative to directory of argument */
9351 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9352 if (cpy != NULL)
9353 {
9354 STRCPY(cpy, p);
9355 STRCPY(gettail(cpy), buf);
9356 vim_free(p);
9357 p = cpy;
9358 }
9359 }
9360 else
9361 {
9362 vim_free(p);
9363 p = vim_strsave(buf);
9364 }
9365 }
9366
9367 if (remain == NULL)
9368 break;
9369
9370 /* Append the first path component of "remain" to "p". */
9371 q = getnextcomp(remain + 1);
9372 len = q - remain - (*q != NUL);
9373 cpy = vim_strnsave(p, STRLEN(p) + len);
9374 if (cpy != NULL)
9375 {
9376 STRNCAT(cpy, remain, len);
9377 vim_free(p);
9378 p = cpy;
9379 }
9380 /* Shorten "remain". */
9381 if (*q != NUL)
9382 STRMOVE(remain, q - 1);
9383 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009384 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009385 }
9386
9387 /* If the result is a relative path name, make it explicitly relative to
9388 * the current directory if and only if the argument had this form. */
9389 if (!vim_ispathsep(*p))
9390 {
9391 if (is_relative_to_current
9392 && *p != NUL
9393 && !(p[0] == '.'
9394 && (p[1] == NUL
9395 || vim_ispathsep(p[1])
9396 || (p[1] == '.'
9397 && (p[2] == NUL
9398 || vim_ispathsep(p[2]))))))
9399 {
9400 /* Prepend "./". */
9401 cpy = concat_str((char_u *)"./", p);
9402 if (cpy != NULL)
9403 {
9404 vim_free(p);
9405 p = cpy;
9406 }
9407 }
9408 else if (!is_relative_to_current)
9409 {
9410 /* Strip leading "./". */
9411 q = p;
9412 while (q[0] == '.' && vim_ispathsep(q[1]))
9413 q += 2;
9414 if (q > p)
9415 STRMOVE(p, p + 2);
9416 }
9417 }
9418
9419 /* Ensure that the result will have no trailing path separator
9420 * if the argument had none. But keep "/" or "//". */
9421 if (!has_trailing_pathsep)
9422 {
9423 q = p + STRLEN(p);
9424 if (after_pathsep(p, q))
9425 *gettail_sep(p) = NUL;
9426 }
9427
9428 rettv->vval.v_string = p;
9429 }
9430# else
9431 rettv->vval.v_string = vim_strsave(p);
9432# endif
9433#endif
9434
9435 simplify_filename(rettv->vval.v_string);
9436
9437#ifdef HAVE_READLINK
9438fail:
9439 vim_free(buf);
9440#endif
9441 rettv->v_type = VAR_STRING;
9442}
9443
9444/*
9445 * "reverse({list})" function
9446 */
9447 static void
9448f_reverse(typval_T *argvars, typval_T *rettv)
9449{
9450 list_T *l;
9451 listitem_T *li, *ni;
9452
9453 if (argvars[0].v_type != VAR_LIST)
9454 EMSG2(_(e_listarg), "reverse()");
9455 else if ((l = argvars[0].vval.v_list) != NULL
9456 && !tv_check_lock(l->lv_lock,
9457 (char_u *)N_("reverse() argument"), TRUE))
9458 {
9459 li = l->lv_last;
9460 l->lv_first = l->lv_last = NULL;
9461 l->lv_len = 0;
9462 while (li != NULL)
9463 {
9464 ni = li->li_prev;
9465 list_append(l, li);
9466 li = ni;
9467 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009468 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009469 l->lv_idx = l->lv_len - l->lv_idx - 1;
9470 }
9471}
9472
9473#define SP_NOMOVE 0x01 /* don't move cursor */
9474#define SP_REPEAT 0x02 /* repeat to find outer pair */
9475#define SP_RETCOUNT 0x04 /* return matchcount */
9476#define SP_SETPCMARK 0x08 /* set previous context mark */
9477#define SP_START 0x10 /* accept match at start position */
9478#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9479#define SP_END 0x40 /* leave cursor at end of match */
9480#define SP_COLUMN 0x80 /* start at cursor column */
9481
9482static int get_search_arg(typval_T *varp, int *flagsp);
9483
9484/*
9485 * Get flags for a search function.
9486 * Possibly sets "p_ws".
9487 * Returns BACKWARD, FORWARD or zero (for an error).
9488 */
9489 static int
9490get_search_arg(typval_T *varp, int *flagsp)
9491{
9492 int dir = FORWARD;
9493 char_u *flags;
9494 char_u nbuf[NUMBUFLEN];
9495 int mask;
9496
9497 if (varp->v_type != VAR_UNKNOWN)
9498 {
9499 flags = get_tv_string_buf_chk(varp, nbuf);
9500 if (flags == NULL)
9501 return 0; /* type error; errmsg already given */
9502 while (*flags != NUL)
9503 {
9504 switch (*flags)
9505 {
9506 case 'b': dir = BACKWARD; break;
9507 case 'w': p_ws = TRUE; break;
9508 case 'W': p_ws = FALSE; break;
9509 default: mask = 0;
9510 if (flagsp != NULL)
9511 switch (*flags)
9512 {
9513 case 'c': mask = SP_START; break;
9514 case 'e': mask = SP_END; break;
9515 case 'm': mask = SP_RETCOUNT; break;
9516 case 'n': mask = SP_NOMOVE; break;
9517 case 'p': mask = SP_SUBPAT; break;
9518 case 'r': mask = SP_REPEAT; break;
9519 case 's': mask = SP_SETPCMARK; break;
9520 case 'z': mask = SP_COLUMN; break;
9521 }
9522 if (mask == 0)
9523 {
9524 EMSG2(_(e_invarg2), flags);
9525 dir = 0;
9526 }
9527 else
9528 *flagsp |= mask;
9529 }
9530 if (dir == 0)
9531 break;
9532 ++flags;
9533 }
9534 }
9535 return dir;
9536}
9537
9538/*
9539 * Shared by search() and searchpos() functions.
9540 */
9541 static int
9542search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9543{
9544 int flags;
9545 char_u *pat;
9546 pos_T pos;
9547 pos_T save_cursor;
9548 int save_p_ws = p_ws;
9549 int dir;
9550 int retval = 0; /* default: FAIL */
9551 long lnum_stop = 0;
9552 proftime_T tm;
9553#ifdef FEAT_RELTIME
9554 long time_limit = 0;
9555#endif
9556 int options = SEARCH_KEEP;
9557 int subpatnum;
9558
9559 pat = get_tv_string(&argvars[0]);
9560 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9561 if (dir == 0)
9562 goto theend;
9563 flags = *flagsp;
9564 if (flags & SP_START)
9565 options |= SEARCH_START;
9566 if (flags & SP_END)
9567 options |= SEARCH_END;
9568 if (flags & SP_COLUMN)
9569 options |= SEARCH_COL;
9570
9571 /* Optional arguments: line number to stop searching and timeout. */
9572 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9573 {
9574 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9575 if (lnum_stop < 0)
9576 goto theend;
9577#ifdef FEAT_RELTIME
9578 if (argvars[3].v_type != VAR_UNKNOWN)
9579 {
9580 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9581 if (time_limit < 0)
9582 goto theend;
9583 }
9584#endif
9585 }
9586
9587#ifdef FEAT_RELTIME
9588 /* Set the time limit, if there is one. */
9589 profile_setlimit(time_limit, &tm);
9590#endif
9591
9592 /*
9593 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9594 * Check to make sure only those flags are set.
9595 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9596 * flags cannot be set. Check for that condition also.
9597 */
9598 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9599 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9600 {
9601 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9602 goto theend;
9603 }
9604
9605 pos = save_cursor = curwin->w_cursor;
9606 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009607 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009608 if (subpatnum != FAIL)
9609 {
9610 if (flags & SP_SUBPAT)
9611 retval = subpatnum;
9612 else
9613 retval = pos.lnum;
9614 if (flags & SP_SETPCMARK)
9615 setpcmark();
9616 curwin->w_cursor = pos;
9617 if (match_pos != NULL)
9618 {
9619 /* Store the match cursor position */
9620 match_pos->lnum = pos.lnum;
9621 match_pos->col = pos.col + 1;
9622 }
9623 /* "/$" will put the cursor after the end of the line, may need to
9624 * correct that here */
9625 check_cursor();
9626 }
9627
9628 /* If 'n' flag is used: restore cursor position. */
9629 if (flags & SP_NOMOVE)
9630 curwin->w_cursor = save_cursor;
9631 else
9632 curwin->w_set_curswant = TRUE;
9633theend:
9634 p_ws = save_p_ws;
9635
9636 return retval;
9637}
9638
9639#ifdef FEAT_FLOAT
9640
9641/*
9642 * round() is not in C90, use ceil() or floor() instead.
9643 */
9644 float_T
9645vim_round(float_T f)
9646{
9647 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9648}
9649
9650/*
9651 * "round({float})" function
9652 */
9653 static void
9654f_round(typval_T *argvars, typval_T *rettv)
9655{
9656 float_T f = 0.0;
9657
9658 rettv->v_type = VAR_FLOAT;
9659 if (get_float_arg(argvars, &f) == OK)
9660 rettv->vval.v_float = vim_round(f);
9661 else
9662 rettv->vval.v_float = 0.0;
9663}
9664#endif
9665
9666/*
9667 * "screenattr()" function
9668 */
9669 static void
9670f_screenattr(typval_T *argvars, typval_T *rettv)
9671{
9672 int row;
9673 int col;
9674 int c;
9675
9676 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9677 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9678 if (row < 0 || row >= screen_Rows
9679 || col < 0 || col >= screen_Columns)
9680 c = -1;
9681 else
9682 c = ScreenAttrs[LineOffset[row] + col];
9683 rettv->vval.v_number = c;
9684}
9685
9686/*
9687 * "screenchar()" function
9688 */
9689 static void
9690f_screenchar(typval_T *argvars, typval_T *rettv)
9691{
9692 int row;
9693 int col;
9694 int off;
9695 int c;
9696
9697 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9698 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9699 if (row < 0 || row >= screen_Rows
9700 || col < 0 || col >= screen_Columns)
9701 c = -1;
9702 else
9703 {
9704 off = LineOffset[row] + col;
9705#ifdef FEAT_MBYTE
9706 if (enc_utf8 && ScreenLinesUC[off] != 0)
9707 c = ScreenLinesUC[off];
9708 else
9709#endif
9710 c = ScreenLines[off];
9711 }
9712 rettv->vval.v_number = c;
9713}
9714
9715/*
9716 * "screencol()" function
9717 *
9718 * First column is 1 to be consistent with virtcol().
9719 */
9720 static void
9721f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9722{
9723 rettv->vval.v_number = screen_screencol() + 1;
9724}
9725
9726/*
9727 * "screenrow()" function
9728 */
9729 static void
9730f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9731{
9732 rettv->vval.v_number = screen_screenrow() + 1;
9733}
9734
9735/*
9736 * "search()" function
9737 */
9738 static void
9739f_search(typval_T *argvars, typval_T *rettv)
9740{
9741 int flags = 0;
9742
9743 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9744}
9745
9746/*
9747 * "searchdecl()" function
9748 */
9749 static void
9750f_searchdecl(typval_T *argvars, typval_T *rettv)
9751{
9752 int locally = 1;
9753 int thisblock = 0;
9754 int error = FALSE;
9755 char_u *name;
9756
9757 rettv->vval.v_number = 1; /* default: FAIL */
9758
9759 name = get_tv_string_chk(&argvars[0]);
9760 if (argvars[1].v_type != VAR_UNKNOWN)
9761 {
9762 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9763 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9764 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9765 }
9766 if (!error && name != NULL)
9767 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9768 locally, thisblock, SEARCH_KEEP) == FAIL;
9769}
9770
9771/*
9772 * Used by searchpair() and searchpairpos()
9773 */
9774 static int
9775searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9776{
9777 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009778 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009779 int save_p_ws = p_ws;
9780 int dir;
9781 int flags = 0;
9782 char_u nbuf1[NUMBUFLEN];
9783 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009784 int retval = 0; /* default: FAIL */
9785 long lnum_stop = 0;
9786 long time_limit = 0;
9787
9788 /* Get the three pattern arguments: start, middle, end. */
9789 spat = get_tv_string_chk(&argvars[0]);
9790 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9791 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9792 if (spat == NULL || mpat == NULL || epat == NULL)
9793 goto theend; /* type error */
9794
9795 /* Handle the optional fourth argument: flags */
9796 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9797 if (dir == 0)
9798 goto theend;
9799
9800 /* Don't accept SP_END or SP_SUBPAT.
9801 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9802 */
9803 if ((flags & (SP_END | SP_SUBPAT)) != 0
9804 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9805 {
9806 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9807 goto theend;
9808 }
9809
9810 /* Using 'r' implies 'W', otherwise it doesn't work. */
9811 if (flags & SP_REPEAT)
9812 p_ws = FALSE;
9813
9814 /* Optional fifth argument: skip expression */
9815 if (argvars[3].v_type == VAR_UNKNOWN
9816 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009817 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009818 else
9819 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009820 skip = &argvars[4];
9821 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9822 && skip->v_type != VAR_STRING)
9823 {
9824 /* Type error */
9825 goto theend;
9826 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009827 if (argvars[5].v_type != VAR_UNKNOWN)
9828 {
9829 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9830 if (lnum_stop < 0)
9831 goto theend;
9832#ifdef FEAT_RELTIME
9833 if (argvars[6].v_type != VAR_UNKNOWN)
9834 {
9835 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9836 if (time_limit < 0)
9837 goto theend;
9838 }
9839#endif
9840 }
9841 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009842
9843 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9844 match_pos, lnum_stop, time_limit);
9845
9846theend:
9847 p_ws = save_p_ws;
9848
9849 return retval;
9850}
9851
9852/*
9853 * "searchpair()" function
9854 */
9855 static void
9856f_searchpair(typval_T *argvars, typval_T *rettv)
9857{
9858 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9859}
9860
9861/*
9862 * "searchpairpos()" function
9863 */
9864 static void
9865f_searchpairpos(typval_T *argvars, typval_T *rettv)
9866{
9867 pos_T match_pos;
9868 int lnum = 0;
9869 int col = 0;
9870
9871 if (rettv_list_alloc(rettv) == FAIL)
9872 return;
9873
9874 if (searchpair_cmn(argvars, &match_pos) > 0)
9875 {
9876 lnum = match_pos.lnum;
9877 col = match_pos.col;
9878 }
9879
9880 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9881 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9882}
9883
9884/*
9885 * Search for a start/middle/end thing.
9886 * Used by searchpair(), see its documentation for the details.
9887 * Returns 0 or -1 for no match,
9888 */
9889 long
9890do_searchpair(
9891 char_u *spat, /* start pattern */
9892 char_u *mpat, /* middle pattern */
9893 char_u *epat, /* end pattern */
9894 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009895 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009896 int flags, /* SP_SETPCMARK and other SP_ values */
9897 pos_T *match_pos,
9898 linenr_T lnum_stop, /* stop at this line if not zero */
9899 long time_limit UNUSED) /* stop after this many msec */
9900{
9901 char_u *save_cpo;
9902 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9903 long retval = 0;
9904 pos_T pos;
9905 pos_T firstpos;
9906 pos_T foundpos;
9907 pos_T save_cursor;
9908 pos_T save_pos;
9909 int n;
9910 int r;
9911 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009912 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009913 int err;
9914 int options = SEARCH_KEEP;
9915 proftime_T tm;
9916
9917 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9918 save_cpo = p_cpo;
9919 p_cpo = empty_option;
9920
9921#ifdef FEAT_RELTIME
9922 /* Set the time limit, if there is one. */
9923 profile_setlimit(time_limit, &tm);
9924#endif
9925
9926 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9927 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009928 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9929 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009930 if (pat2 == NULL || pat3 == NULL)
9931 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009932 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009933 if (*mpat == NUL)
9934 STRCPY(pat3, pat2);
9935 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009936 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009937 spat, epat, mpat);
9938 if (flags & SP_START)
9939 options |= SEARCH_START;
9940
Bram Moolenaar48570482017-10-30 21:48:41 +01009941 if (skip != NULL)
9942 {
9943 /* Empty string means to not use the skip expression. */
9944 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9945 use_skip = skip->vval.v_string != NULL
9946 && *skip->vval.v_string != NUL;
9947 }
9948
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009949 save_cursor = curwin->w_cursor;
9950 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009951 CLEAR_POS(&firstpos);
9952 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009953 pat = pat3;
9954 for (;;)
9955 {
9956 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009957 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009958 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009959 /* didn't find it or found the first match again: FAIL */
9960 break;
9961
9962 if (firstpos.lnum == 0)
9963 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009964 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009965 {
9966 /* Found the same position again. Can happen with a pattern that
9967 * has "\zs" at the end and searching backwards. Advance one
9968 * character and try again. */
9969 if (dir == BACKWARD)
9970 decl(&pos);
9971 else
9972 incl(&pos);
9973 }
9974 foundpos = pos;
9975
9976 /* clear the start flag to avoid getting stuck here */
9977 options &= ~SEARCH_START;
9978
9979 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009980 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009981 {
9982 save_pos = curwin->w_cursor;
9983 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009984 err = FALSE;
9985 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009986 curwin->w_cursor = save_pos;
9987 if (err)
9988 {
9989 /* Evaluating {skip} caused an error, break here. */
9990 curwin->w_cursor = save_cursor;
9991 retval = -1;
9992 break;
9993 }
9994 if (r)
9995 continue;
9996 }
9997
9998 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9999 {
10000 /* Found end when searching backwards or start when searching
10001 * forward: nested pair. */
10002 ++nest;
10003 pat = pat2; /* nested, don't search for middle */
10004 }
10005 else
10006 {
10007 /* Found end when searching forward or start when searching
10008 * backward: end of (nested) pair; or found middle in outer pair. */
10009 if (--nest == 1)
10010 pat = pat3; /* outer level, search for middle */
10011 }
10012
10013 if (nest == 0)
10014 {
10015 /* Found the match: return matchcount or line number. */
10016 if (flags & SP_RETCOUNT)
10017 ++retval;
10018 else
10019 retval = pos.lnum;
10020 if (flags & SP_SETPCMARK)
10021 setpcmark();
10022 curwin->w_cursor = pos;
10023 if (!(flags & SP_REPEAT))
10024 break;
10025 nest = 1; /* search for next unmatched */
10026 }
10027 }
10028
10029 if (match_pos != NULL)
10030 {
10031 /* Store the match cursor position */
10032 match_pos->lnum = curwin->w_cursor.lnum;
10033 match_pos->col = curwin->w_cursor.col + 1;
10034 }
10035
10036 /* If 'n' flag is used or search failed: restore cursor position. */
10037 if ((flags & SP_NOMOVE) || retval == 0)
10038 curwin->w_cursor = save_cursor;
10039
10040theend:
10041 vim_free(pat2);
10042 vim_free(pat3);
10043 if (p_cpo == empty_option)
10044 p_cpo = save_cpo;
10045 else
10046 /* Darn, evaluating the {skip} expression changed the value. */
10047 free_string_option(save_cpo);
10048
10049 return retval;
10050}
10051
10052/*
10053 * "searchpos()" function
10054 */
10055 static void
10056f_searchpos(typval_T *argvars, typval_T *rettv)
10057{
10058 pos_T match_pos;
10059 int lnum = 0;
10060 int col = 0;
10061 int n;
10062 int flags = 0;
10063
10064 if (rettv_list_alloc(rettv) == FAIL)
10065 return;
10066
10067 n = search_cmn(argvars, &match_pos, &flags);
10068 if (n > 0)
10069 {
10070 lnum = match_pos.lnum;
10071 col = match_pos.col;
10072 }
10073
10074 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10075 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10076 if (flags & SP_SUBPAT)
10077 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10078}
10079
10080 static void
10081f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10082{
10083#ifdef FEAT_CLIENTSERVER
10084 char_u buf[NUMBUFLEN];
10085 char_u *server = get_tv_string_chk(&argvars[0]);
10086 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10087
10088 rettv->vval.v_number = -1;
10089 if (server == NULL || reply == NULL)
10090 return;
10091 if (check_restricted() || check_secure())
10092 return;
10093# ifdef FEAT_X11
10094 if (check_connection() == FAIL)
10095 return;
10096# endif
10097
10098 if (serverSendReply(server, reply) < 0)
10099 {
10100 EMSG(_("E258: Unable to send to client"));
10101 return;
10102 }
10103 rettv->vval.v_number = 0;
10104#else
10105 rettv->vval.v_number = -1;
10106#endif
10107}
10108
10109 static void
10110f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10111{
10112 char_u *r = NULL;
10113
10114#ifdef FEAT_CLIENTSERVER
10115# ifdef WIN32
10116 r = serverGetVimNames();
10117# else
10118 make_connection();
10119 if (X_DISPLAY != NULL)
10120 r = serverGetVimNames(X_DISPLAY);
10121# endif
10122#endif
10123 rettv->v_type = VAR_STRING;
10124 rettv->vval.v_string = r;
10125}
10126
10127/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010128 * Set line or list of lines in buffer "buf".
10129 */
10130 static void
10131set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
10132{
10133 char_u *line = NULL;
10134 list_T *l = NULL;
10135 listitem_T *li = NULL;
10136 long added = 0;
10137 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010138 buf_T *curbuf_save = NULL;
10139 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010140 int is_curbuf = buf == curbuf;
10141
Bram Moolenaar9d954202017-09-04 20:34:19 +020010142 /* When using the current buffer ml_mfp will be set if needed. Useful when
10143 * setline() is used on startup. For other buffers the buffer must be
10144 * loaded. */
10145 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010146 {
10147 rettv->vval.v_number = 1; /* FAIL */
10148 return;
10149 }
10150
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010151 if (!is_curbuf)
10152 {
10153 wininfo_T *wip;
10154
10155 curbuf_save = curbuf;
10156 curwin_save = curwin;
10157 curbuf = buf;
10158 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10159 {
10160 if (wip->wi_win != NULL)
10161 {
10162 curwin = wip->wi_win;
10163 break;
10164 }
10165 }
10166 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010167
10168 lcount = curbuf->b_ml.ml_line_count;
10169
10170 if (lines->v_type == VAR_LIST)
10171 {
10172 l = lines->vval.v_list;
10173 li = l->lv_first;
10174 }
10175 else
10176 line = get_tv_string_chk(lines);
10177
10178 /* default result is zero == OK */
10179 for (;;)
10180 {
10181 if (l != NULL)
10182 {
10183 /* list argument, get next string */
10184 if (li == NULL)
10185 break;
10186 line = get_tv_string_chk(&li->li_tv);
10187 li = li->li_next;
10188 }
10189
10190 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +020010191 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010192 break;
10193
10194 /* When coming here from Insert mode, sync undo, so that this can be
10195 * undone separately from what was previously inserted. */
10196 if (u_sync_once == 2)
10197 {
10198 u_sync_once = 1; /* notify that u_sync() was called */
10199 u_sync(TRUE);
10200 }
10201
10202 if (lnum <= curbuf->b_ml.ml_line_count)
10203 {
10204 /* existing line, replace it */
10205 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10206 {
10207 changed_bytes(lnum, 0);
10208 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10209 check_cursor_col();
10210 rettv->vval.v_number = 0; /* OK */
10211 }
10212 }
10213 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10214 {
10215 /* lnum is one past the last line, append the line */
10216 ++added;
10217 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10218 rettv->vval.v_number = 0; /* OK */
10219 }
10220
10221 if (l == NULL) /* only one string argument */
10222 break;
10223 ++lnum;
10224 }
10225
10226 if (added > 0)
10227 appended_lines_mark(lcount, added);
10228
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010229 if (!is_curbuf)
10230 {
10231 curbuf = curbuf_save;
10232 curwin = curwin_save;
10233 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010234}
10235
10236/*
10237 * "setbufline()" function
10238 */
10239 static void
10240f_setbufline(argvars, rettv)
10241 typval_T *argvars;
10242 typval_T *rettv;
10243{
10244 linenr_T lnum;
10245 buf_T *buf;
10246
10247 buf = get_buf_tv(&argvars[0], FALSE);
10248 if (buf == NULL)
10249 rettv->vval.v_number = 1; /* FAIL */
10250 else
10251 {
10252 lnum = get_tv_lnum_buf(&argvars[1], buf);
10253
10254 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10255 }
10256}
10257
10258/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010259 * "setbufvar()" function
10260 */
10261 static void
10262f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10263{
10264 buf_T *buf;
10265 char_u *varname, *bufvarname;
10266 typval_T *varp;
10267 char_u nbuf[NUMBUFLEN];
10268
10269 if (check_restricted() || check_secure())
10270 return;
10271 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10272 varname = get_tv_string_chk(&argvars[1]);
10273 buf = get_buf_tv(&argvars[0], FALSE);
10274 varp = &argvars[2];
10275
10276 if (buf != NULL && varname != NULL && varp != NULL)
10277 {
10278 if (*varname == '&')
10279 {
10280 long numval;
10281 char_u *strval;
10282 int error = FALSE;
10283 aco_save_T aco;
10284
10285 /* set curbuf to be our buf, temporarily */
10286 aucmd_prepbuf(&aco, buf);
10287
10288 ++varname;
10289 numval = (long)get_tv_number_chk(varp, &error);
10290 strval = get_tv_string_buf_chk(varp, nbuf);
10291 if (!error && strval != NULL)
10292 set_option_value(varname, numval, strval, OPT_LOCAL);
10293
10294 /* reset notion of buffer */
10295 aucmd_restbuf(&aco);
10296 }
10297 else
10298 {
10299 buf_T *save_curbuf = curbuf;
10300
10301 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10302 if (bufvarname != NULL)
10303 {
10304 curbuf = buf;
10305 STRCPY(bufvarname, "b:");
10306 STRCPY(bufvarname + 2, varname);
10307 set_var(bufvarname, varp, TRUE);
10308 vim_free(bufvarname);
10309 curbuf = save_curbuf;
10310 }
10311 }
10312 }
10313}
10314
10315 static void
10316f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10317{
10318 dict_T *d;
10319 dictitem_T *di;
10320 char_u *csearch;
10321
10322 if (argvars[0].v_type != VAR_DICT)
10323 {
10324 EMSG(_(e_dictreq));
10325 return;
10326 }
10327
10328 if ((d = argvars[0].vval.v_dict) != NULL)
10329 {
10330 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10331 if (csearch != NULL)
10332 {
10333#ifdef FEAT_MBYTE
10334 if (enc_utf8)
10335 {
10336 int pcc[MAX_MCO];
10337 int c = utfc_ptr2char(csearch, pcc);
10338
10339 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10340 }
10341 else
10342#endif
10343 set_last_csearch(PTR2CHAR(csearch),
10344 csearch, MB_PTR2LEN(csearch));
10345 }
10346
10347 di = dict_find(d, (char_u *)"forward", -1);
10348 if (di != NULL)
10349 set_csearch_direction((int)get_tv_number(&di->di_tv)
10350 ? FORWARD : BACKWARD);
10351
10352 di = dict_find(d, (char_u *)"until", -1);
10353 if (di != NULL)
10354 set_csearch_until(!!get_tv_number(&di->di_tv));
10355 }
10356}
10357
10358/*
10359 * "setcmdpos()" function
10360 */
10361 static void
10362f_setcmdpos(typval_T *argvars, typval_T *rettv)
10363{
10364 int pos = (int)get_tv_number(&argvars[0]) - 1;
10365
10366 if (pos >= 0)
10367 rettv->vval.v_number = set_cmdline_pos(pos);
10368}
10369
10370/*
10371 * "setfperm({fname}, {mode})" function
10372 */
10373 static void
10374f_setfperm(typval_T *argvars, typval_T *rettv)
10375{
10376 char_u *fname;
10377 char_u modebuf[NUMBUFLEN];
10378 char_u *mode_str;
10379 int i;
10380 int mask;
10381 int mode = 0;
10382
10383 rettv->vval.v_number = 0;
10384 fname = get_tv_string_chk(&argvars[0]);
10385 if (fname == NULL)
10386 return;
10387 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10388 if (mode_str == NULL)
10389 return;
10390 if (STRLEN(mode_str) != 9)
10391 {
10392 EMSG2(_(e_invarg2), mode_str);
10393 return;
10394 }
10395
10396 mask = 1;
10397 for (i = 8; i >= 0; --i)
10398 {
10399 if (mode_str[i] != '-')
10400 mode |= mask;
10401 mask = mask << 1;
10402 }
10403 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10404}
10405
10406/*
10407 * "setline()" function
10408 */
10409 static void
10410f_setline(typval_T *argvars, typval_T *rettv)
10411{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010412 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010413
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010414 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010415}
10416
Bram Moolenaard823fa92016-08-12 16:29:27 +020010417static 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 +020010418
10419/*
10420 * Used by "setqflist()" and "setloclist()" functions
10421 */
10422 static void
10423set_qf_ll_list(
10424 win_T *wp UNUSED,
10425 typval_T *list_arg UNUSED,
10426 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010427 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010428 typval_T *rettv)
10429{
10430#ifdef FEAT_QUICKFIX
10431 static char *e_invact = N_("E927: Invalid action: '%s'");
10432 char_u *act;
10433 int action = 0;
10434#endif
10435
10436 rettv->vval.v_number = -1;
10437
10438#ifdef FEAT_QUICKFIX
10439 if (list_arg->v_type != VAR_LIST)
10440 EMSG(_(e_listreq));
10441 else
10442 {
10443 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010444 dict_T *d = NULL;
10445 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010446
10447 if (action_arg->v_type == VAR_STRING)
10448 {
10449 act = get_tv_string_chk(action_arg);
10450 if (act == NULL)
10451 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010452 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10453 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010454 action = *act;
10455 else
10456 EMSG2(_(e_invact), act);
10457 }
10458 else if (action_arg->v_type == VAR_UNKNOWN)
10459 action = ' ';
10460 else
10461 EMSG(_(e_stringreq));
10462
Bram Moolenaard823fa92016-08-12 16:29:27 +020010463 if (action_arg->v_type != VAR_UNKNOWN
10464 && what_arg->v_type != VAR_UNKNOWN)
10465 {
10466 if (what_arg->v_type == VAR_DICT)
10467 d = what_arg->vval.v_dict;
10468 else
10469 {
10470 EMSG(_(e_dictreq));
10471 valid_dict = FALSE;
10472 }
10473 }
10474
10475 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10476 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010477 rettv->vval.v_number = 0;
10478 }
10479#endif
10480}
10481
10482/*
10483 * "setloclist()" function
10484 */
10485 static void
10486f_setloclist(typval_T *argvars, typval_T *rettv)
10487{
10488 win_T *win;
10489
10490 rettv->vval.v_number = -1;
10491
10492 win = find_win_by_nr(&argvars[0], NULL);
10493 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010494 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010495}
10496
10497/*
10498 * "setmatches()" function
10499 */
10500 static void
10501f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10502{
10503#ifdef FEAT_SEARCH_EXTRA
10504 list_T *l;
10505 listitem_T *li;
10506 dict_T *d;
10507 list_T *s = NULL;
10508
10509 rettv->vval.v_number = -1;
10510 if (argvars[0].v_type != VAR_LIST)
10511 {
10512 EMSG(_(e_listreq));
10513 return;
10514 }
10515 if ((l = argvars[0].vval.v_list) != NULL)
10516 {
10517
10518 /* To some extent make sure that we are dealing with a list from
10519 * "getmatches()". */
10520 li = l->lv_first;
10521 while (li != NULL)
10522 {
10523 if (li->li_tv.v_type != VAR_DICT
10524 || (d = li->li_tv.vval.v_dict) == NULL)
10525 {
10526 EMSG(_(e_invarg));
10527 return;
10528 }
10529 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10530 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10531 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10532 && dict_find(d, (char_u *)"priority", -1) != NULL
10533 && dict_find(d, (char_u *)"id", -1) != NULL))
10534 {
10535 EMSG(_(e_invarg));
10536 return;
10537 }
10538 li = li->li_next;
10539 }
10540
10541 clear_matches(curwin);
10542 li = l->lv_first;
10543 while (li != NULL)
10544 {
10545 int i = 0;
10546 char_u buf[5];
10547 dictitem_T *di;
10548 char_u *group;
10549 int priority;
10550 int id;
10551 char_u *conceal;
10552
10553 d = li->li_tv.vval.v_dict;
10554 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10555 {
10556 if (s == NULL)
10557 {
10558 s = list_alloc();
10559 if (s == NULL)
10560 return;
10561 }
10562
10563 /* match from matchaddpos() */
10564 for (i = 1; i < 9; i++)
10565 {
10566 sprintf((char *)buf, (char *)"pos%d", i);
10567 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10568 {
10569 if (di->di_tv.v_type != VAR_LIST)
10570 return;
10571
10572 list_append_tv(s, &di->di_tv);
10573 s->lv_refcount++;
10574 }
10575 else
10576 break;
10577 }
10578 }
10579
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010580 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010581 priority = (int)get_dict_number(d, (char_u *)"priority");
10582 id = (int)get_dict_number(d, (char_u *)"id");
10583 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010584 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010585 : NULL;
10586 if (i == 0)
10587 {
10588 match_add(curwin, group,
10589 get_dict_string(d, (char_u *)"pattern", FALSE),
10590 priority, id, NULL, conceal);
10591 }
10592 else
10593 {
10594 match_add(curwin, group, NULL, priority, id, s, conceal);
10595 list_unref(s);
10596 s = NULL;
10597 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010598 vim_free(group);
10599 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010600
10601 li = li->li_next;
10602 }
10603 rettv->vval.v_number = 0;
10604 }
10605#endif
10606}
10607
10608/*
10609 * "setpos()" function
10610 */
10611 static void
10612f_setpos(typval_T *argvars, typval_T *rettv)
10613{
10614 pos_T pos;
10615 int fnum;
10616 char_u *name;
10617 colnr_T curswant = -1;
10618
10619 rettv->vval.v_number = -1;
10620 name = get_tv_string_chk(argvars);
10621 if (name != NULL)
10622 {
10623 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10624 {
10625 if (--pos.col < 0)
10626 pos.col = 0;
10627 if (name[0] == '.' && name[1] == NUL)
10628 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010629 /* set cursor; "fnum" is ignored */
10630 curwin->w_cursor = pos;
10631 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010632 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010633 curwin->w_curswant = curswant - 1;
10634 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010635 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010636 check_cursor();
10637 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010638 }
10639 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10640 {
10641 /* set mark */
10642 if (setmark_pos(name[1], &pos, fnum) == OK)
10643 rettv->vval.v_number = 0;
10644 }
10645 else
10646 EMSG(_(e_invarg));
10647 }
10648 }
10649}
10650
10651/*
10652 * "setqflist()" function
10653 */
10654 static void
10655f_setqflist(typval_T *argvars, typval_T *rettv)
10656{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010657 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010658}
10659
10660/*
10661 * "setreg()" function
10662 */
10663 static void
10664f_setreg(typval_T *argvars, typval_T *rettv)
10665{
10666 int regname;
10667 char_u *strregname;
10668 char_u *stropt;
10669 char_u *strval;
10670 int append;
10671 char_u yank_type;
10672 long block_len;
10673
10674 block_len = -1;
10675 yank_type = MAUTO;
10676 append = FALSE;
10677
10678 strregname = get_tv_string_chk(argvars);
10679 rettv->vval.v_number = 1; /* FAIL is default */
10680
10681 if (strregname == NULL)
10682 return; /* type error; errmsg already given */
10683 regname = *strregname;
10684 if (regname == 0 || regname == '@')
10685 regname = '"';
10686
10687 if (argvars[2].v_type != VAR_UNKNOWN)
10688 {
10689 stropt = get_tv_string_chk(&argvars[2]);
10690 if (stropt == NULL)
10691 return; /* type error */
10692 for (; *stropt != NUL; ++stropt)
10693 switch (*stropt)
10694 {
10695 case 'a': case 'A': /* append */
10696 append = TRUE;
10697 break;
10698 case 'v': case 'c': /* character-wise selection */
10699 yank_type = MCHAR;
10700 break;
10701 case 'V': case 'l': /* line-wise selection */
10702 yank_type = MLINE;
10703 break;
10704 case 'b': case Ctrl_V: /* block-wise selection */
10705 yank_type = MBLOCK;
10706 if (VIM_ISDIGIT(stropt[1]))
10707 {
10708 ++stropt;
10709 block_len = getdigits(&stropt) - 1;
10710 --stropt;
10711 }
10712 break;
10713 }
10714 }
10715
10716 if (argvars[1].v_type == VAR_LIST)
10717 {
10718 char_u **lstval;
10719 char_u **allocval;
10720 char_u buf[NUMBUFLEN];
10721 char_u **curval;
10722 char_u **curallocval;
10723 list_T *ll = argvars[1].vval.v_list;
10724 listitem_T *li;
10725 int len;
10726
10727 /* If the list is NULL handle like an empty list. */
10728 len = ll == NULL ? 0 : ll->lv_len;
10729
10730 /* First half: use for pointers to result lines; second half: use for
10731 * pointers to allocated copies. */
10732 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10733 if (lstval == NULL)
10734 return;
10735 curval = lstval;
10736 allocval = lstval + len + 2;
10737 curallocval = allocval;
10738
10739 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10740 li = li->li_next)
10741 {
10742 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10743 if (strval == NULL)
10744 goto free_lstval;
10745 if (strval == buf)
10746 {
10747 /* Need to make a copy, next get_tv_string_buf_chk() will
10748 * overwrite the string. */
10749 strval = vim_strsave(buf);
10750 if (strval == NULL)
10751 goto free_lstval;
10752 *curallocval++ = strval;
10753 }
10754 *curval++ = strval;
10755 }
10756 *curval++ = NULL;
10757
10758 write_reg_contents_lst(regname, lstval, -1,
10759 append, yank_type, block_len);
10760free_lstval:
10761 while (curallocval > allocval)
10762 vim_free(*--curallocval);
10763 vim_free(lstval);
10764 }
10765 else
10766 {
10767 strval = get_tv_string_chk(&argvars[1]);
10768 if (strval == NULL)
10769 return;
10770 write_reg_contents_ex(regname, strval, -1,
10771 append, yank_type, block_len);
10772 }
10773 rettv->vval.v_number = 0;
10774}
10775
10776/*
10777 * "settabvar()" function
10778 */
10779 static void
10780f_settabvar(typval_T *argvars, typval_T *rettv)
10781{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010782 tabpage_T *save_curtab;
10783 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010784 char_u *varname, *tabvarname;
10785 typval_T *varp;
10786
10787 rettv->vval.v_number = 0;
10788
10789 if (check_restricted() || check_secure())
10790 return;
10791
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010792 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010793 varname = get_tv_string_chk(&argvars[1]);
10794 varp = &argvars[2];
10795
Bram Moolenaar4033c552017-09-16 20:54:51 +020010796 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010797 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010798 save_curtab = curtab;
10799 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010800
10801 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10802 if (tabvarname != NULL)
10803 {
10804 STRCPY(tabvarname, "t:");
10805 STRCPY(tabvarname + 2, varname);
10806 set_var(tabvarname, varp, TRUE);
10807 vim_free(tabvarname);
10808 }
10809
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010810 /* Restore current tabpage */
10811 if (valid_tabpage(save_curtab))
10812 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010813 }
10814}
10815
10816/*
10817 * "settabwinvar()" function
10818 */
10819 static void
10820f_settabwinvar(typval_T *argvars, typval_T *rettv)
10821{
10822 setwinvar(argvars, rettv, 1);
10823}
10824
10825/*
10826 * "setwinvar()" function
10827 */
10828 static void
10829f_setwinvar(typval_T *argvars, typval_T *rettv)
10830{
10831 setwinvar(argvars, rettv, 0);
10832}
10833
10834#ifdef FEAT_CRYPT
10835/*
10836 * "sha256({string})" function
10837 */
10838 static void
10839f_sha256(typval_T *argvars, typval_T *rettv)
10840{
10841 char_u *p;
10842
10843 p = get_tv_string(&argvars[0]);
10844 rettv->vval.v_string = vim_strsave(
10845 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10846 rettv->v_type = VAR_STRING;
10847}
10848#endif /* FEAT_CRYPT */
10849
10850/*
10851 * "shellescape({string})" function
10852 */
10853 static void
10854f_shellescape(typval_T *argvars, typval_T *rettv)
10855{
Bram Moolenaar20615522017-06-05 18:46:26 +020010856 int do_special = non_zero_arg(&argvars[1]);
10857
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010858 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010859 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010860 rettv->v_type = VAR_STRING;
10861}
10862
10863/*
10864 * shiftwidth() function
10865 */
10866 static void
10867f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10868{
10869 rettv->vval.v_number = get_sw_value(curbuf);
10870}
10871
10872/*
10873 * "simplify()" function
10874 */
10875 static void
10876f_simplify(typval_T *argvars, typval_T *rettv)
10877{
10878 char_u *p;
10879
10880 p = get_tv_string(&argvars[0]);
10881 rettv->vval.v_string = vim_strsave(p);
10882 simplify_filename(rettv->vval.v_string); /* simplify in place */
10883 rettv->v_type = VAR_STRING;
10884}
10885
10886#ifdef FEAT_FLOAT
10887/*
10888 * "sin()" function
10889 */
10890 static void
10891f_sin(typval_T *argvars, typval_T *rettv)
10892{
10893 float_T f = 0.0;
10894
10895 rettv->v_type = VAR_FLOAT;
10896 if (get_float_arg(argvars, &f) == OK)
10897 rettv->vval.v_float = sin(f);
10898 else
10899 rettv->vval.v_float = 0.0;
10900}
10901
10902/*
10903 * "sinh()" function
10904 */
10905 static void
10906f_sinh(typval_T *argvars, typval_T *rettv)
10907{
10908 float_T f = 0.0;
10909
10910 rettv->v_type = VAR_FLOAT;
10911 if (get_float_arg(argvars, &f) == OK)
10912 rettv->vval.v_float = sinh(f);
10913 else
10914 rettv->vval.v_float = 0.0;
10915}
10916#endif
10917
10918static int
10919#ifdef __BORLANDC__
10920 _RTLENTRYF
10921#endif
10922 item_compare(const void *s1, const void *s2);
10923static int
10924#ifdef __BORLANDC__
10925 _RTLENTRYF
10926#endif
10927 item_compare2(const void *s1, const void *s2);
10928
10929/* struct used in the array that's given to qsort() */
10930typedef struct
10931{
10932 listitem_T *item;
10933 int idx;
10934} sortItem_T;
10935
10936/* struct storing information about current sort */
10937typedef struct
10938{
10939 int item_compare_ic;
10940 int item_compare_numeric;
10941 int item_compare_numbers;
10942#ifdef FEAT_FLOAT
10943 int item_compare_float;
10944#endif
10945 char_u *item_compare_func;
10946 partial_T *item_compare_partial;
10947 dict_T *item_compare_selfdict;
10948 int item_compare_func_err;
10949 int item_compare_keep_zero;
10950} sortinfo_T;
10951static sortinfo_T *sortinfo = NULL;
10952static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10953#define ITEM_COMPARE_FAIL 999
10954
10955/*
10956 * Compare functions for f_sort() and f_uniq() below.
10957 */
10958 static int
10959#ifdef __BORLANDC__
10960_RTLENTRYF
10961#endif
10962item_compare(const void *s1, const void *s2)
10963{
10964 sortItem_T *si1, *si2;
10965 typval_T *tv1, *tv2;
10966 char_u *p1, *p2;
10967 char_u *tofree1 = NULL, *tofree2 = NULL;
10968 int res;
10969 char_u numbuf1[NUMBUFLEN];
10970 char_u numbuf2[NUMBUFLEN];
10971
10972 si1 = (sortItem_T *)s1;
10973 si2 = (sortItem_T *)s2;
10974 tv1 = &si1->item->li_tv;
10975 tv2 = &si2->item->li_tv;
10976
10977 if (sortinfo->item_compare_numbers)
10978 {
10979 varnumber_T v1 = get_tv_number(tv1);
10980 varnumber_T v2 = get_tv_number(tv2);
10981
10982 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10983 }
10984
10985#ifdef FEAT_FLOAT
10986 if (sortinfo->item_compare_float)
10987 {
10988 float_T v1 = get_tv_float(tv1);
10989 float_T v2 = get_tv_float(tv2);
10990
10991 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10992 }
10993#endif
10994
10995 /* tv2string() puts quotes around a string and allocates memory. Don't do
10996 * that for string variables. Use a single quote when comparing with a
10997 * non-string to do what the docs promise. */
10998 if (tv1->v_type == VAR_STRING)
10999 {
11000 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11001 p1 = (char_u *)"'";
11002 else
11003 p1 = tv1->vval.v_string;
11004 }
11005 else
11006 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11007 if (tv2->v_type == VAR_STRING)
11008 {
11009 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11010 p2 = (char_u *)"'";
11011 else
11012 p2 = tv2->vval.v_string;
11013 }
11014 else
11015 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11016 if (p1 == NULL)
11017 p1 = (char_u *)"";
11018 if (p2 == NULL)
11019 p2 = (char_u *)"";
11020 if (!sortinfo->item_compare_numeric)
11021 {
11022 if (sortinfo->item_compare_ic)
11023 res = STRICMP(p1, p2);
11024 else
11025 res = STRCMP(p1, p2);
11026 }
11027 else
11028 {
11029 double n1, n2;
11030 n1 = strtod((char *)p1, (char **)&p1);
11031 n2 = strtod((char *)p2, (char **)&p2);
11032 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11033 }
11034
11035 /* When the result would be zero, compare the item indexes. Makes the
11036 * sort stable. */
11037 if (res == 0 && !sortinfo->item_compare_keep_zero)
11038 res = si1->idx > si2->idx ? 1 : -1;
11039
11040 vim_free(tofree1);
11041 vim_free(tofree2);
11042 return res;
11043}
11044
11045 static int
11046#ifdef __BORLANDC__
11047_RTLENTRYF
11048#endif
11049item_compare2(const void *s1, const void *s2)
11050{
11051 sortItem_T *si1, *si2;
11052 int res;
11053 typval_T rettv;
11054 typval_T argv[3];
11055 int dummy;
11056 char_u *func_name;
11057 partial_T *partial = sortinfo->item_compare_partial;
11058
11059 /* shortcut after failure in previous call; compare all items equal */
11060 if (sortinfo->item_compare_func_err)
11061 return 0;
11062
11063 si1 = (sortItem_T *)s1;
11064 si2 = (sortItem_T *)s2;
11065
11066 if (partial == NULL)
11067 func_name = sortinfo->item_compare_func;
11068 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011069 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011070
11071 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11072 * in the copy without changing the original list items. */
11073 copy_tv(&si1->item->li_tv, &argv[0]);
11074 copy_tv(&si2->item->li_tv, &argv[1]);
11075
11076 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11077 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011078 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011079 partial, sortinfo->item_compare_selfdict);
11080 clear_tv(&argv[0]);
11081 clear_tv(&argv[1]);
11082
11083 if (res == FAIL)
11084 res = ITEM_COMPARE_FAIL;
11085 else
11086 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11087 if (sortinfo->item_compare_func_err)
11088 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11089 clear_tv(&rettv);
11090
11091 /* When the result would be zero, compare the pointers themselves. Makes
11092 * the sort stable. */
11093 if (res == 0 && !sortinfo->item_compare_keep_zero)
11094 res = si1->idx > si2->idx ? 1 : -1;
11095
11096 return res;
11097}
11098
11099/*
11100 * "sort({list})" function
11101 */
11102 static void
11103do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11104{
11105 list_T *l;
11106 listitem_T *li;
11107 sortItem_T *ptrs;
11108 sortinfo_T *old_sortinfo;
11109 sortinfo_T info;
11110 long len;
11111 long i;
11112
11113 /* Pointer to current info struct used in compare function. Save and
11114 * restore the current one for nested calls. */
11115 old_sortinfo = sortinfo;
11116 sortinfo = &info;
11117
11118 if (argvars[0].v_type != VAR_LIST)
11119 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11120 else
11121 {
11122 l = argvars[0].vval.v_list;
11123 if (l == NULL || tv_check_lock(l->lv_lock,
11124 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11125 TRUE))
11126 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011127 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011128
11129 len = list_len(l);
11130 if (len <= 1)
11131 goto theend; /* short list sorts pretty quickly */
11132
11133 info.item_compare_ic = FALSE;
11134 info.item_compare_numeric = FALSE;
11135 info.item_compare_numbers = FALSE;
11136#ifdef FEAT_FLOAT
11137 info.item_compare_float = FALSE;
11138#endif
11139 info.item_compare_func = NULL;
11140 info.item_compare_partial = NULL;
11141 info.item_compare_selfdict = NULL;
11142 if (argvars[1].v_type != VAR_UNKNOWN)
11143 {
11144 /* optional second argument: {func} */
11145 if (argvars[1].v_type == VAR_FUNC)
11146 info.item_compare_func = argvars[1].vval.v_string;
11147 else if (argvars[1].v_type == VAR_PARTIAL)
11148 info.item_compare_partial = argvars[1].vval.v_partial;
11149 else
11150 {
11151 int error = FALSE;
11152
11153 i = (long)get_tv_number_chk(&argvars[1], &error);
11154 if (error)
11155 goto theend; /* type error; errmsg already given */
11156 if (i == 1)
11157 info.item_compare_ic = TRUE;
11158 else if (argvars[1].v_type != VAR_NUMBER)
11159 info.item_compare_func = get_tv_string(&argvars[1]);
11160 else if (i != 0)
11161 {
11162 EMSG(_(e_invarg));
11163 goto theend;
11164 }
11165 if (info.item_compare_func != NULL)
11166 {
11167 if (*info.item_compare_func == NUL)
11168 {
11169 /* empty string means default sort */
11170 info.item_compare_func = NULL;
11171 }
11172 else if (STRCMP(info.item_compare_func, "n") == 0)
11173 {
11174 info.item_compare_func = NULL;
11175 info.item_compare_numeric = TRUE;
11176 }
11177 else if (STRCMP(info.item_compare_func, "N") == 0)
11178 {
11179 info.item_compare_func = NULL;
11180 info.item_compare_numbers = TRUE;
11181 }
11182#ifdef FEAT_FLOAT
11183 else if (STRCMP(info.item_compare_func, "f") == 0)
11184 {
11185 info.item_compare_func = NULL;
11186 info.item_compare_float = TRUE;
11187 }
11188#endif
11189 else if (STRCMP(info.item_compare_func, "i") == 0)
11190 {
11191 info.item_compare_func = NULL;
11192 info.item_compare_ic = TRUE;
11193 }
11194 }
11195 }
11196
11197 if (argvars[2].v_type != VAR_UNKNOWN)
11198 {
11199 /* optional third argument: {dict} */
11200 if (argvars[2].v_type != VAR_DICT)
11201 {
11202 EMSG(_(e_dictreq));
11203 goto theend;
11204 }
11205 info.item_compare_selfdict = argvars[2].vval.v_dict;
11206 }
11207 }
11208
11209 /* Make an array with each entry pointing to an item in the List. */
11210 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11211 if (ptrs == NULL)
11212 goto theend;
11213
11214 i = 0;
11215 if (sort)
11216 {
11217 /* sort(): ptrs will be the list to sort */
11218 for (li = l->lv_first; li != NULL; li = li->li_next)
11219 {
11220 ptrs[i].item = li;
11221 ptrs[i].idx = i;
11222 ++i;
11223 }
11224
11225 info.item_compare_func_err = FALSE;
11226 info.item_compare_keep_zero = FALSE;
11227 /* test the compare function */
11228 if ((info.item_compare_func != NULL
11229 || info.item_compare_partial != NULL)
11230 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11231 == ITEM_COMPARE_FAIL)
11232 EMSG(_("E702: Sort compare function failed"));
11233 else
11234 {
11235 /* Sort the array with item pointers. */
11236 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11237 info.item_compare_func == NULL
11238 && info.item_compare_partial == NULL
11239 ? item_compare : item_compare2);
11240
11241 if (!info.item_compare_func_err)
11242 {
11243 /* Clear the List and append the items in sorted order. */
11244 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11245 l->lv_len = 0;
11246 for (i = 0; i < len; ++i)
11247 list_append(l, ptrs[i].item);
11248 }
11249 }
11250 }
11251 else
11252 {
11253 int (*item_compare_func_ptr)(const void *, const void *);
11254
11255 /* f_uniq(): ptrs will be a stack of items to remove */
11256 info.item_compare_func_err = FALSE;
11257 info.item_compare_keep_zero = TRUE;
11258 item_compare_func_ptr = info.item_compare_func != NULL
11259 || info.item_compare_partial != NULL
11260 ? item_compare2 : item_compare;
11261
11262 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11263 li = li->li_next)
11264 {
11265 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11266 == 0)
11267 ptrs[i++].item = li;
11268 if (info.item_compare_func_err)
11269 {
11270 EMSG(_("E882: Uniq compare function failed"));
11271 break;
11272 }
11273 }
11274
11275 if (!info.item_compare_func_err)
11276 {
11277 while (--i >= 0)
11278 {
11279 li = ptrs[i].item->li_next;
11280 ptrs[i].item->li_next = li->li_next;
11281 if (li->li_next != NULL)
11282 li->li_next->li_prev = ptrs[i].item;
11283 else
11284 l->lv_last = ptrs[i].item;
11285 list_fix_watch(l, li);
11286 listitem_free(li);
11287 l->lv_len--;
11288 }
11289 }
11290 }
11291
11292 vim_free(ptrs);
11293 }
11294theend:
11295 sortinfo = old_sortinfo;
11296}
11297
11298/*
11299 * "sort({list})" function
11300 */
11301 static void
11302f_sort(typval_T *argvars, typval_T *rettv)
11303{
11304 do_sort_uniq(argvars, rettv, TRUE);
11305}
11306
11307/*
11308 * "uniq({list})" function
11309 */
11310 static void
11311f_uniq(typval_T *argvars, typval_T *rettv)
11312{
11313 do_sort_uniq(argvars, rettv, FALSE);
11314}
11315
11316/*
11317 * "soundfold({word})" function
11318 */
11319 static void
11320f_soundfold(typval_T *argvars, typval_T *rettv)
11321{
11322 char_u *s;
11323
11324 rettv->v_type = VAR_STRING;
11325 s = get_tv_string(&argvars[0]);
11326#ifdef FEAT_SPELL
11327 rettv->vval.v_string = eval_soundfold(s);
11328#else
11329 rettv->vval.v_string = vim_strsave(s);
11330#endif
11331}
11332
11333/*
11334 * "spellbadword()" function
11335 */
11336 static void
11337f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11338{
11339 char_u *word = (char_u *)"";
11340 hlf_T attr = HLF_COUNT;
11341 int len = 0;
11342
11343 if (rettv_list_alloc(rettv) == FAIL)
11344 return;
11345
11346#ifdef FEAT_SPELL
11347 if (argvars[0].v_type == VAR_UNKNOWN)
11348 {
11349 /* Find the start and length of the badly spelled word. */
11350 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11351 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011352 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011353 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011354 curwin->w_set_curswant = TRUE;
11355 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011356 }
11357 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11358 {
11359 char_u *str = get_tv_string_chk(&argvars[0]);
11360 int capcol = -1;
11361
11362 if (str != NULL)
11363 {
11364 /* Check the argument for spelling. */
11365 while (*str != NUL)
11366 {
11367 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11368 if (attr != HLF_COUNT)
11369 {
11370 word = str;
11371 break;
11372 }
11373 str += len;
11374 }
11375 }
11376 }
11377#endif
11378
11379 list_append_string(rettv->vval.v_list, word, len);
11380 list_append_string(rettv->vval.v_list, (char_u *)(
11381 attr == HLF_SPB ? "bad" :
11382 attr == HLF_SPR ? "rare" :
11383 attr == HLF_SPL ? "local" :
11384 attr == HLF_SPC ? "caps" :
11385 ""), -1);
11386}
11387
11388/*
11389 * "spellsuggest()" function
11390 */
11391 static void
11392f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11393{
11394#ifdef FEAT_SPELL
11395 char_u *str;
11396 int typeerr = FALSE;
11397 int maxcount;
11398 garray_T ga;
11399 int i;
11400 listitem_T *li;
11401 int need_capital = FALSE;
11402#endif
11403
11404 if (rettv_list_alloc(rettv) == FAIL)
11405 return;
11406
11407#ifdef FEAT_SPELL
11408 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11409 {
11410 str = get_tv_string(&argvars[0]);
11411 if (argvars[1].v_type != VAR_UNKNOWN)
11412 {
11413 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11414 if (maxcount <= 0)
11415 return;
11416 if (argvars[2].v_type != VAR_UNKNOWN)
11417 {
11418 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11419 if (typeerr)
11420 return;
11421 }
11422 }
11423 else
11424 maxcount = 25;
11425
11426 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11427
11428 for (i = 0; i < ga.ga_len; ++i)
11429 {
11430 str = ((char_u **)ga.ga_data)[i];
11431
11432 li = listitem_alloc();
11433 if (li == NULL)
11434 vim_free(str);
11435 else
11436 {
11437 li->li_tv.v_type = VAR_STRING;
11438 li->li_tv.v_lock = 0;
11439 li->li_tv.vval.v_string = str;
11440 list_append(rettv->vval.v_list, li);
11441 }
11442 }
11443 ga_clear(&ga);
11444 }
11445#endif
11446}
11447
11448 static void
11449f_split(typval_T *argvars, typval_T *rettv)
11450{
11451 char_u *str;
11452 char_u *end;
11453 char_u *pat = NULL;
11454 regmatch_T regmatch;
11455 char_u patbuf[NUMBUFLEN];
11456 char_u *save_cpo;
11457 int match;
11458 colnr_T col = 0;
11459 int keepempty = FALSE;
11460 int typeerr = FALSE;
11461
11462 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11463 save_cpo = p_cpo;
11464 p_cpo = (char_u *)"";
11465
11466 str = get_tv_string(&argvars[0]);
11467 if (argvars[1].v_type != VAR_UNKNOWN)
11468 {
11469 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11470 if (pat == NULL)
11471 typeerr = TRUE;
11472 if (argvars[2].v_type != VAR_UNKNOWN)
11473 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11474 }
11475 if (pat == NULL || *pat == NUL)
11476 pat = (char_u *)"[\\x01- ]\\+";
11477
11478 if (rettv_list_alloc(rettv) == FAIL)
11479 return;
11480 if (typeerr)
11481 return;
11482
11483 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11484 if (regmatch.regprog != NULL)
11485 {
11486 regmatch.rm_ic = FALSE;
11487 while (*str != NUL || keepempty)
11488 {
11489 if (*str == NUL)
11490 match = FALSE; /* empty item at the end */
11491 else
11492 match = vim_regexec_nl(&regmatch, str, col);
11493 if (match)
11494 end = regmatch.startp[0];
11495 else
11496 end = str + STRLEN(str);
11497 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11498 && *str != NUL && match && end < regmatch.endp[0]))
11499 {
11500 if (list_append_string(rettv->vval.v_list, str,
11501 (int)(end - str)) == FAIL)
11502 break;
11503 }
11504 if (!match)
11505 break;
11506 /* Advance to just after the match. */
11507 if (regmatch.endp[0] > str)
11508 col = 0;
11509 else
11510 {
11511 /* Don't get stuck at the same match. */
11512#ifdef FEAT_MBYTE
11513 col = (*mb_ptr2len)(regmatch.endp[0]);
11514#else
11515 col = 1;
11516#endif
11517 }
11518 str = regmatch.endp[0];
11519 }
11520
11521 vim_regfree(regmatch.regprog);
11522 }
11523
11524 p_cpo = save_cpo;
11525}
11526
11527#ifdef FEAT_FLOAT
11528/*
11529 * "sqrt()" function
11530 */
11531 static void
11532f_sqrt(typval_T *argvars, typval_T *rettv)
11533{
11534 float_T f = 0.0;
11535
11536 rettv->v_type = VAR_FLOAT;
11537 if (get_float_arg(argvars, &f) == OK)
11538 rettv->vval.v_float = sqrt(f);
11539 else
11540 rettv->vval.v_float = 0.0;
11541}
11542
11543/*
11544 * "str2float()" function
11545 */
11546 static void
11547f_str2float(typval_T *argvars, typval_T *rettv)
11548{
11549 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011550 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011551
Bram Moolenaar08243d22017-01-10 16:12:29 +010011552 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011553 p = skipwhite(p + 1);
11554 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011555 if (isneg)
11556 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011557 rettv->v_type = VAR_FLOAT;
11558}
11559#endif
11560
11561/*
11562 * "str2nr()" function
11563 */
11564 static void
11565f_str2nr(typval_T *argvars, typval_T *rettv)
11566{
11567 int base = 10;
11568 char_u *p;
11569 varnumber_T n;
11570 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011571 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011572
11573 if (argvars[1].v_type != VAR_UNKNOWN)
11574 {
11575 base = (int)get_tv_number(&argvars[1]);
11576 if (base != 2 && base != 8 && base != 10 && base != 16)
11577 {
11578 EMSG(_(e_invarg));
11579 return;
11580 }
11581 }
11582
11583 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011584 isneg = (*p == '-');
11585 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011586 p = skipwhite(p + 1);
11587 switch (base)
11588 {
11589 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11590 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11591 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11592 default: what = 0;
11593 }
11594 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011595 if (isneg)
11596 rettv->vval.v_number = -n;
11597 else
11598 rettv->vval.v_number = n;
11599
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011600}
11601
11602#ifdef HAVE_STRFTIME
11603/*
11604 * "strftime({format}[, {time}])" function
11605 */
11606 static void
11607f_strftime(typval_T *argvars, typval_T *rettv)
11608{
11609 char_u result_buf[256];
11610 struct tm *curtime;
11611 time_t seconds;
11612 char_u *p;
11613
11614 rettv->v_type = VAR_STRING;
11615
11616 p = get_tv_string(&argvars[0]);
11617 if (argvars[1].v_type == VAR_UNKNOWN)
11618 seconds = time(NULL);
11619 else
11620 seconds = (time_t)get_tv_number(&argvars[1]);
11621 curtime = localtime(&seconds);
11622 /* MSVC returns NULL for an invalid value of seconds. */
11623 if (curtime == NULL)
11624 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11625 else
11626 {
11627# ifdef FEAT_MBYTE
11628 vimconv_T conv;
11629 char_u *enc;
11630
11631 conv.vc_type = CONV_NONE;
11632 enc = enc_locale();
11633 convert_setup(&conv, p_enc, enc);
11634 if (conv.vc_type != CONV_NONE)
11635 p = string_convert(&conv, p, NULL);
11636# endif
11637 if (p != NULL)
11638 (void)strftime((char *)result_buf, sizeof(result_buf),
11639 (char *)p, curtime);
11640 else
11641 result_buf[0] = NUL;
11642
11643# ifdef FEAT_MBYTE
11644 if (conv.vc_type != CONV_NONE)
11645 vim_free(p);
11646 convert_setup(&conv, enc, p_enc);
11647 if (conv.vc_type != CONV_NONE)
11648 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11649 else
11650# endif
11651 rettv->vval.v_string = vim_strsave(result_buf);
11652
11653# ifdef FEAT_MBYTE
11654 /* Release conversion descriptors */
11655 convert_setup(&conv, NULL, NULL);
11656 vim_free(enc);
11657# endif
11658 }
11659}
11660#endif
11661
11662/*
11663 * "strgetchar()" function
11664 */
11665 static void
11666f_strgetchar(typval_T *argvars, typval_T *rettv)
11667{
11668 char_u *str;
11669 int len;
11670 int error = FALSE;
11671 int charidx;
11672
11673 rettv->vval.v_number = -1;
11674 str = get_tv_string_chk(&argvars[0]);
11675 if (str == NULL)
11676 return;
11677 len = (int)STRLEN(str);
11678 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11679 if (error)
11680 return;
11681#ifdef FEAT_MBYTE
11682 {
11683 int byteidx = 0;
11684
11685 while (charidx >= 0 && byteidx < len)
11686 {
11687 if (charidx == 0)
11688 {
11689 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11690 break;
11691 }
11692 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011693 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011694 }
11695 }
11696#else
11697 if (charidx < len)
11698 rettv->vval.v_number = str[charidx];
11699#endif
11700}
11701
11702/*
11703 * "stridx()" function
11704 */
11705 static void
11706f_stridx(typval_T *argvars, typval_T *rettv)
11707{
11708 char_u buf[NUMBUFLEN];
11709 char_u *needle;
11710 char_u *haystack;
11711 char_u *save_haystack;
11712 char_u *pos;
11713 int start_idx;
11714
11715 needle = get_tv_string_chk(&argvars[1]);
11716 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11717 rettv->vval.v_number = -1;
11718 if (needle == NULL || haystack == NULL)
11719 return; /* type error; errmsg already given */
11720
11721 if (argvars[2].v_type != VAR_UNKNOWN)
11722 {
11723 int error = FALSE;
11724
11725 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11726 if (error || start_idx >= (int)STRLEN(haystack))
11727 return;
11728 if (start_idx >= 0)
11729 haystack += start_idx;
11730 }
11731
11732 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11733 if (pos != NULL)
11734 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11735}
11736
11737/*
11738 * "string()" function
11739 */
11740 static void
11741f_string(typval_T *argvars, typval_T *rettv)
11742{
11743 char_u *tofree;
11744 char_u numbuf[NUMBUFLEN];
11745
11746 rettv->v_type = VAR_STRING;
11747 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11748 get_copyID());
11749 /* Make a copy if we have a value but it's not in allocated memory. */
11750 if (rettv->vval.v_string != NULL && tofree == NULL)
11751 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11752}
11753
11754/*
11755 * "strlen()" function
11756 */
11757 static void
11758f_strlen(typval_T *argvars, typval_T *rettv)
11759{
11760 rettv->vval.v_number = (varnumber_T)(STRLEN(
11761 get_tv_string(&argvars[0])));
11762}
11763
11764/*
11765 * "strchars()" function
11766 */
11767 static void
11768f_strchars(typval_T *argvars, typval_T *rettv)
11769{
11770 char_u *s = get_tv_string(&argvars[0]);
11771 int skipcc = 0;
11772#ifdef FEAT_MBYTE
11773 varnumber_T len = 0;
11774 int (*func_mb_ptr2char_adv)(char_u **pp);
11775#endif
11776
11777 if (argvars[1].v_type != VAR_UNKNOWN)
11778 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11779 if (skipcc < 0 || skipcc > 1)
11780 EMSG(_(e_invarg));
11781 else
11782 {
11783#ifdef FEAT_MBYTE
11784 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11785 while (*s != NUL)
11786 {
11787 func_mb_ptr2char_adv(&s);
11788 ++len;
11789 }
11790 rettv->vval.v_number = len;
11791#else
11792 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11793#endif
11794 }
11795}
11796
11797/*
11798 * "strdisplaywidth()" function
11799 */
11800 static void
11801f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11802{
11803 char_u *s = get_tv_string(&argvars[0]);
11804 int col = 0;
11805
11806 if (argvars[1].v_type != VAR_UNKNOWN)
11807 col = (int)get_tv_number(&argvars[1]);
11808
11809 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11810}
11811
11812/*
11813 * "strwidth()" function
11814 */
11815 static void
11816f_strwidth(typval_T *argvars, typval_T *rettv)
11817{
11818 char_u *s = get_tv_string(&argvars[0]);
11819
11820 rettv->vval.v_number = (varnumber_T)(
11821#ifdef FEAT_MBYTE
11822 mb_string2cells(s, -1)
11823#else
11824 STRLEN(s)
11825#endif
11826 );
11827}
11828
11829/*
11830 * "strcharpart()" function
11831 */
11832 static void
11833f_strcharpart(typval_T *argvars, typval_T *rettv)
11834{
11835#ifdef FEAT_MBYTE
11836 char_u *p;
11837 int nchar;
11838 int nbyte = 0;
11839 int charlen;
11840 int len = 0;
11841 int slen;
11842 int error = FALSE;
11843
11844 p = get_tv_string(&argvars[0]);
11845 slen = (int)STRLEN(p);
11846
11847 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11848 if (!error)
11849 {
11850 if (nchar > 0)
11851 while (nchar > 0 && nbyte < slen)
11852 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011853 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011854 --nchar;
11855 }
11856 else
11857 nbyte = nchar;
11858 if (argvars[2].v_type != VAR_UNKNOWN)
11859 {
11860 charlen = (int)get_tv_number(&argvars[2]);
11861 while (charlen > 0 && nbyte + len < slen)
11862 {
11863 int off = nbyte + len;
11864
11865 if (off < 0)
11866 len += 1;
11867 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011868 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011869 --charlen;
11870 }
11871 }
11872 else
11873 len = slen - nbyte; /* default: all bytes that are available. */
11874 }
11875
11876 /*
11877 * Only return the overlap between the specified part and the actual
11878 * string.
11879 */
11880 if (nbyte < 0)
11881 {
11882 len += nbyte;
11883 nbyte = 0;
11884 }
11885 else if (nbyte > slen)
11886 nbyte = slen;
11887 if (len < 0)
11888 len = 0;
11889 else if (nbyte + len > slen)
11890 len = slen - nbyte;
11891
11892 rettv->v_type = VAR_STRING;
11893 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11894#else
11895 f_strpart(argvars, rettv);
11896#endif
11897}
11898
11899/*
11900 * "strpart()" function
11901 */
11902 static void
11903f_strpart(typval_T *argvars, typval_T *rettv)
11904{
11905 char_u *p;
11906 int n;
11907 int len;
11908 int slen;
11909 int error = FALSE;
11910
11911 p = get_tv_string(&argvars[0]);
11912 slen = (int)STRLEN(p);
11913
11914 n = (int)get_tv_number_chk(&argvars[1], &error);
11915 if (error)
11916 len = 0;
11917 else if (argvars[2].v_type != VAR_UNKNOWN)
11918 len = (int)get_tv_number(&argvars[2]);
11919 else
11920 len = slen - n; /* default len: all bytes that are available. */
11921
11922 /*
11923 * Only return the overlap between the specified part and the actual
11924 * string.
11925 */
11926 if (n < 0)
11927 {
11928 len += n;
11929 n = 0;
11930 }
11931 else if (n > slen)
11932 n = slen;
11933 if (len < 0)
11934 len = 0;
11935 else if (n + len > slen)
11936 len = slen - n;
11937
11938 rettv->v_type = VAR_STRING;
11939 rettv->vval.v_string = vim_strnsave(p + n, len);
11940}
11941
11942/*
11943 * "strridx()" function
11944 */
11945 static void
11946f_strridx(typval_T *argvars, typval_T *rettv)
11947{
11948 char_u buf[NUMBUFLEN];
11949 char_u *needle;
11950 char_u *haystack;
11951 char_u *rest;
11952 char_u *lastmatch = NULL;
11953 int haystack_len, end_idx;
11954
11955 needle = get_tv_string_chk(&argvars[1]);
11956 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11957
11958 rettv->vval.v_number = -1;
11959 if (needle == NULL || haystack == NULL)
11960 return; /* type error; errmsg already given */
11961
11962 haystack_len = (int)STRLEN(haystack);
11963 if (argvars[2].v_type != VAR_UNKNOWN)
11964 {
11965 /* Third argument: upper limit for index */
11966 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11967 if (end_idx < 0)
11968 return; /* can never find a match */
11969 }
11970 else
11971 end_idx = haystack_len;
11972
11973 if (*needle == NUL)
11974 {
11975 /* Empty string matches past the end. */
11976 lastmatch = haystack + end_idx;
11977 }
11978 else
11979 {
11980 for (rest = haystack; *rest != '\0'; ++rest)
11981 {
11982 rest = (char_u *)strstr((char *)rest, (char *)needle);
11983 if (rest == NULL || rest > haystack + end_idx)
11984 break;
11985 lastmatch = rest;
11986 }
11987 }
11988
11989 if (lastmatch == NULL)
11990 rettv->vval.v_number = -1;
11991 else
11992 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11993}
11994
11995/*
11996 * "strtrans()" function
11997 */
11998 static void
11999f_strtrans(typval_T *argvars, typval_T *rettv)
12000{
12001 rettv->v_type = VAR_STRING;
12002 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12003}
12004
12005/*
12006 * "submatch()" function
12007 */
12008 static void
12009f_submatch(typval_T *argvars, typval_T *rettv)
12010{
12011 int error = FALSE;
12012 int no;
12013 int retList = 0;
12014
12015 no = (int)get_tv_number_chk(&argvars[0], &error);
12016 if (error)
12017 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012018 if (no < 0 || no >= NSUBEXP)
12019 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012020 EMSGN(_("E935: invalid submatch number: %d"), no);
12021 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012022 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012023 if (argvars[1].v_type != VAR_UNKNOWN)
12024 retList = (int)get_tv_number_chk(&argvars[1], &error);
12025 if (error)
12026 return;
12027
12028 if (retList == 0)
12029 {
12030 rettv->v_type = VAR_STRING;
12031 rettv->vval.v_string = reg_submatch(no);
12032 }
12033 else
12034 {
12035 rettv->v_type = VAR_LIST;
12036 rettv->vval.v_list = reg_submatch_list(no);
12037 }
12038}
12039
12040/*
12041 * "substitute()" function
12042 */
12043 static void
12044f_substitute(typval_T *argvars, typval_T *rettv)
12045{
12046 char_u patbuf[NUMBUFLEN];
12047 char_u subbuf[NUMBUFLEN];
12048 char_u flagsbuf[NUMBUFLEN];
12049
12050 char_u *str = get_tv_string_chk(&argvars[0]);
12051 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012052 char_u *sub = NULL;
12053 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012054 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12055
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012056 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12057 expr = &argvars[2];
12058 else
12059 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12060
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012061 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012062 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12063 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012064 rettv->vval.v_string = NULL;
12065 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012066 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012067}
12068
12069/*
12070 * "synID(lnum, col, trans)" function
12071 */
12072 static void
12073f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12074{
12075 int id = 0;
12076#ifdef FEAT_SYN_HL
12077 linenr_T lnum;
12078 colnr_T col;
12079 int trans;
12080 int transerr = FALSE;
12081
12082 lnum = get_tv_lnum(argvars); /* -1 on type error */
12083 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12084 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12085
12086 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12087 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12088 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12089#endif
12090
12091 rettv->vval.v_number = id;
12092}
12093
12094/*
12095 * "synIDattr(id, what [, mode])" function
12096 */
12097 static void
12098f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12099{
12100 char_u *p = NULL;
12101#ifdef FEAT_SYN_HL
12102 int id;
12103 char_u *what;
12104 char_u *mode;
12105 char_u modebuf[NUMBUFLEN];
12106 int modec;
12107
12108 id = (int)get_tv_number(&argvars[0]);
12109 what = get_tv_string(&argvars[1]);
12110 if (argvars[2].v_type != VAR_UNKNOWN)
12111 {
12112 mode = get_tv_string_buf(&argvars[2], modebuf);
12113 modec = TOLOWER_ASC(mode[0]);
12114 if (modec != 't' && modec != 'c' && modec != 'g')
12115 modec = 0; /* replace invalid with current */
12116 }
12117 else
12118 {
12119#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12120 if (USE_24BIT)
12121 modec = 'g';
12122 else
12123#endif
12124 if (t_colors > 1)
12125 modec = 'c';
12126 else
12127 modec = 't';
12128 }
12129
12130
12131 switch (TOLOWER_ASC(what[0]))
12132 {
12133 case 'b':
12134 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12135 p = highlight_color(id, what, modec);
12136 else /* bold */
12137 p = highlight_has_attr(id, HL_BOLD, modec);
12138 break;
12139
12140 case 'f': /* fg[#] or font */
12141 p = highlight_color(id, what, modec);
12142 break;
12143
12144 case 'i':
12145 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12146 p = highlight_has_attr(id, HL_INVERSE, modec);
12147 else /* italic */
12148 p = highlight_has_attr(id, HL_ITALIC, modec);
12149 break;
12150
12151 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012152 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012153 break;
12154
12155 case 'r': /* reverse */
12156 p = highlight_has_attr(id, HL_INVERSE, modec);
12157 break;
12158
12159 case 's':
12160 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12161 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012162 /* strikeout */
12163 else if (TOLOWER_ASC(what[1]) == 't' &&
12164 TOLOWER_ASC(what[2]) == 'r')
12165 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012166 else /* standout */
12167 p = highlight_has_attr(id, HL_STANDOUT, modec);
12168 break;
12169
12170 case 'u':
12171 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12172 /* underline */
12173 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12174 else
12175 /* undercurl */
12176 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12177 break;
12178 }
12179
12180 if (p != NULL)
12181 p = vim_strsave(p);
12182#endif
12183 rettv->v_type = VAR_STRING;
12184 rettv->vval.v_string = p;
12185}
12186
12187/*
12188 * "synIDtrans(id)" function
12189 */
12190 static void
12191f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12192{
12193 int id;
12194
12195#ifdef FEAT_SYN_HL
12196 id = (int)get_tv_number(&argvars[0]);
12197
12198 if (id > 0)
12199 id = syn_get_final_id(id);
12200 else
12201#endif
12202 id = 0;
12203
12204 rettv->vval.v_number = id;
12205}
12206
12207/*
12208 * "synconcealed(lnum, col)" function
12209 */
12210 static void
12211f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12212{
12213#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12214 linenr_T lnum;
12215 colnr_T col;
12216 int syntax_flags = 0;
12217 int cchar;
12218 int matchid = 0;
12219 char_u str[NUMBUFLEN];
12220#endif
12221
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012222 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012223
12224#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12225 lnum = get_tv_lnum(argvars); /* -1 on type error */
12226 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12227
12228 vim_memset(str, NUL, sizeof(str));
12229
12230 if (rettv_list_alloc(rettv) != FAIL)
12231 {
12232 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12233 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12234 && curwin->w_p_cole > 0)
12235 {
12236 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12237 syntax_flags = get_syntax_info(&matchid);
12238
12239 /* get the conceal character */
12240 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12241 {
12242 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012243 if (cchar == NUL && curwin->w_p_cole == 1)
12244 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012245 if (cchar != NUL)
12246 {
12247# ifdef FEAT_MBYTE
12248 if (has_mbyte)
12249 (*mb_char2bytes)(cchar, str);
12250 else
12251# endif
12252 str[0] = cchar;
12253 }
12254 }
12255 }
12256
12257 list_append_number(rettv->vval.v_list,
12258 (syntax_flags & HL_CONCEAL) != 0);
12259 /* -1 to auto-determine strlen */
12260 list_append_string(rettv->vval.v_list, str, -1);
12261 list_append_number(rettv->vval.v_list, matchid);
12262 }
12263#endif
12264}
12265
12266/*
12267 * "synstack(lnum, col)" function
12268 */
12269 static void
12270f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12271{
12272#ifdef FEAT_SYN_HL
12273 linenr_T lnum;
12274 colnr_T col;
12275 int i;
12276 int id;
12277#endif
12278
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012279 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012280
12281#ifdef FEAT_SYN_HL
12282 lnum = get_tv_lnum(argvars); /* -1 on type error */
12283 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12284
12285 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12286 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12287 && rettv_list_alloc(rettv) != FAIL)
12288 {
12289 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12290 for (i = 0; ; ++i)
12291 {
12292 id = syn_get_stack_item(i);
12293 if (id < 0)
12294 break;
12295 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12296 break;
12297 }
12298 }
12299#endif
12300}
12301
12302 static void
12303get_cmd_output_as_rettv(
12304 typval_T *argvars,
12305 typval_T *rettv,
12306 int retlist)
12307{
12308 char_u *res = NULL;
12309 char_u *p;
12310 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012311 int err = FALSE;
12312 FILE *fd;
12313 list_T *list = NULL;
12314 int flags = SHELL_SILENT;
12315
12316 rettv->v_type = VAR_STRING;
12317 rettv->vval.v_string = NULL;
12318 if (check_restricted() || check_secure())
12319 goto errret;
12320
12321 if (argvars[1].v_type != VAR_UNKNOWN)
12322 {
12323 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012324 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012325 * command.
12326 */
12327 if ((infile = vim_tempname('i', TRUE)) == NULL)
12328 {
12329 EMSG(_(e_notmp));
12330 goto errret;
12331 }
12332
12333 fd = mch_fopen((char *)infile, WRITEBIN);
12334 if (fd == NULL)
12335 {
12336 EMSG2(_(e_notopen), infile);
12337 goto errret;
12338 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012339 if (argvars[1].v_type == VAR_NUMBER)
12340 {
12341 linenr_T lnum;
12342 buf_T *buf;
12343
12344 buf = buflist_findnr(argvars[1].vval.v_number);
12345 if (buf == NULL)
12346 {
12347 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012348 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012349 goto errret;
12350 }
12351
12352 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12353 {
12354 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12355 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12356 {
12357 err = TRUE;
12358 break;
12359 }
12360 if (putc(NL, fd) == EOF)
12361 {
12362 err = TRUE;
12363 break;
12364 }
12365 }
12366 }
12367 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012368 {
12369 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12370 err = TRUE;
12371 }
12372 else
12373 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012374 size_t len;
12375 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012376
12377 p = get_tv_string_buf_chk(&argvars[1], buf);
12378 if (p == NULL)
12379 {
12380 fclose(fd);
12381 goto errret; /* type error; errmsg already given */
12382 }
12383 len = STRLEN(p);
12384 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12385 err = TRUE;
12386 }
12387 if (fclose(fd) != 0)
12388 err = TRUE;
12389 if (err)
12390 {
12391 EMSG(_("E677: Error writing temp file"));
12392 goto errret;
12393 }
12394 }
12395
12396 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12397 * echoes typeahead, that messes up the display. */
12398 if (!msg_silent)
12399 flags += SHELL_COOKED;
12400
12401 if (retlist)
12402 {
12403 int len;
12404 listitem_T *li;
12405 char_u *s = NULL;
12406 char_u *start;
12407 char_u *end;
12408 int i;
12409
12410 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12411 if (res == NULL)
12412 goto errret;
12413
12414 list = list_alloc();
12415 if (list == NULL)
12416 goto errret;
12417
12418 for (i = 0; i < len; ++i)
12419 {
12420 start = res + i;
12421 while (i < len && res[i] != NL)
12422 ++i;
12423 end = res + i;
12424
12425 s = alloc((unsigned)(end - start + 1));
12426 if (s == NULL)
12427 goto errret;
12428
12429 for (p = s; start < end; ++p, ++start)
12430 *p = *start == NUL ? NL : *start;
12431 *p = NUL;
12432
12433 li = listitem_alloc();
12434 if (li == NULL)
12435 {
12436 vim_free(s);
12437 goto errret;
12438 }
12439 li->li_tv.v_type = VAR_STRING;
12440 li->li_tv.v_lock = 0;
12441 li->li_tv.vval.v_string = s;
12442 list_append(list, li);
12443 }
12444
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012445 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012446 list = NULL;
12447 }
12448 else
12449 {
12450 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12451#ifdef USE_CR
12452 /* translate <CR> into <NL> */
12453 if (res != NULL)
12454 {
12455 char_u *s;
12456
12457 for (s = res; *s; ++s)
12458 {
12459 if (*s == CAR)
12460 *s = NL;
12461 }
12462 }
12463#else
12464# ifdef USE_CRNL
12465 /* translate <CR><NL> into <NL> */
12466 if (res != NULL)
12467 {
12468 char_u *s, *d;
12469
12470 d = res;
12471 for (s = res; *s; ++s)
12472 {
12473 if (s[0] == CAR && s[1] == NL)
12474 ++s;
12475 *d++ = *s;
12476 }
12477 *d = NUL;
12478 }
12479# endif
12480#endif
12481 rettv->vval.v_string = res;
12482 res = NULL;
12483 }
12484
12485errret:
12486 if (infile != NULL)
12487 {
12488 mch_remove(infile);
12489 vim_free(infile);
12490 }
12491 if (res != NULL)
12492 vim_free(res);
12493 if (list != NULL)
12494 list_free(list);
12495}
12496
12497/*
12498 * "system()" function
12499 */
12500 static void
12501f_system(typval_T *argvars, typval_T *rettv)
12502{
12503 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12504}
12505
12506/*
12507 * "systemlist()" function
12508 */
12509 static void
12510f_systemlist(typval_T *argvars, typval_T *rettv)
12511{
12512 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12513}
12514
12515/*
12516 * "tabpagebuflist()" function
12517 */
12518 static void
12519f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12520{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012521 tabpage_T *tp;
12522 win_T *wp = NULL;
12523
12524 if (argvars[0].v_type == VAR_UNKNOWN)
12525 wp = firstwin;
12526 else
12527 {
12528 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12529 if (tp != NULL)
12530 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12531 }
12532 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12533 {
12534 for (; wp != NULL; wp = wp->w_next)
12535 if (list_append_number(rettv->vval.v_list,
12536 wp->w_buffer->b_fnum) == FAIL)
12537 break;
12538 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012539}
12540
12541
12542/*
12543 * "tabpagenr()" function
12544 */
12545 static void
12546f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12547{
12548 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012549 char_u *arg;
12550
12551 if (argvars[0].v_type != VAR_UNKNOWN)
12552 {
12553 arg = get_tv_string_chk(&argvars[0]);
12554 nr = 0;
12555 if (arg != NULL)
12556 {
12557 if (STRCMP(arg, "$") == 0)
12558 nr = tabpage_index(NULL) - 1;
12559 else
12560 EMSG2(_(e_invexpr2), arg);
12561 }
12562 }
12563 else
12564 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012565 rettv->vval.v_number = nr;
12566}
12567
12568
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012569static int get_winnr(tabpage_T *tp, typval_T *argvar);
12570
12571/*
12572 * Common code for tabpagewinnr() and winnr().
12573 */
12574 static int
12575get_winnr(tabpage_T *tp, typval_T *argvar)
12576{
12577 win_T *twin;
12578 int nr = 1;
12579 win_T *wp;
12580 char_u *arg;
12581
12582 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12583 if (argvar->v_type != VAR_UNKNOWN)
12584 {
12585 arg = get_tv_string_chk(argvar);
12586 if (arg == NULL)
12587 nr = 0; /* type error; errmsg already given */
12588 else if (STRCMP(arg, "$") == 0)
12589 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12590 else if (STRCMP(arg, "#") == 0)
12591 {
12592 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12593 if (twin == NULL)
12594 nr = 0;
12595 }
12596 else
12597 {
12598 EMSG2(_(e_invexpr2), arg);
12599 nr = 0;
12600 }
12601 }
12602
12603 if (nr > 0)
12604 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12605 wp != twin; wp = wp->w_next)
12606 {
12607 if (wp == NULL)
12608 {
12609 /* didn't find it in this tabpage */
12610 nr = 0;
12611 break;
12612 }
12613 ++nr;
12614 }
12615 return nr;
12616}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012617
12618/*
12619 * "tabpagewinnr()" function
12620 */
12621 static void
12622f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12623{
12624 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012625 tabpage_T *tp;
12626
12627 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12628 if (tp == NULL)
12629 nr = 0;
12630 else
12631 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012632 rettv->vval.v_number = nr;
12633}
12634
12635
12636/*
12637 * "tagfiles()" function
12638 */
12639 static void
12640f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12641{
12642 char_u *fname;
12643 tagname_T tn;
12644 int first;
12645
12646 if (rettv_list_alloc(rettv) == FAIL)
12647 return;
12648 fname = alloc(MAXPATHL);
12649 if (fname == NULL)
12650 return;
12651
12652 for (first = TRUE; ; first = FALSE)
12653 if (get_tagfname(&tn, first, fname) == FAIL
12654 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12655 break;
12656 tagname_free(&tn);
12657 vim_free(fname);
12658}
12659
12660/*
12661 * "taglist()" function
12662 */
12663 static void
12664f_taglist(typval_T *argvars, typval_T *rettv)
12665{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012666 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012667 char_u *tag_pattern;
12668
12669 tag_pattern = get_tv_string(&argvars[0]);
12670
12671 rettv->vval.v_number = FALSE;
12672 if (*tag_pattern == NUL)
12673 return;
12674
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012675 if (argvars[1].v_type != VAR_UNKNOWN)
12676 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012677 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012678 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012679}
12680
12681/*
12682 * "tempname()" function
12683 */
12684 static void
12685f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12686{
12687 static int x = 'A';
12688
12689 rettv->v_type = VAR_STRING;
12690 rettv->vval.v_string = vim_tempname(x, FALSE);
12691
12692 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12693 * names. Skip 'I' and 'O', they are used for shell redirection. */
12694 do
12695 {
12696 if (x == 'Z')
12697 x = '0';
12698 else if (x == '9')
12699 x = 'A';
12700 else
12701 {
12702#ifdef EBCDIC
12703 if (x == 'I')
12704 x = 'J';
12705 else if (x == 'R')
12706 x = 'S';
12707 else
12708#endif
12709 ++x;
12710 }
12711 } while (x == 'I' || x == 'O');
12712}
12713
12714#ifdef FEAT_FLOAT
12715/*
12716 * "tan()" function
12717 */
12718 static void
12719f_tan(typval_T *argvars, typval_T *rettv)
12720{
12721 float_T f = 0.0;
12722
12723 rettv->v_type = VAR_FLOAT;
12724 if (get_float_arg(argvars, &f) == OK)
12725 rettv->vval.v_float = tan(f);
12726 else
12727 rettv->vval.v_float = 0.0;
12728}
12729
12730/*
12731 * "tanh()" function
12732 */
12733 static void
12734f_tanh(typval_T *argvars, typval_T *rettv)
12735{
12736 float_T f = 0.0;
12737
12738 rettv->v_type = VAR_FLOAT;
12739 if (get_float_arg(argvars, &f) == OK)
12740 rettv->vval.v_float = tanh(f);
12741 else
12742 rettv->vval.v_float = 0.0;
12743}
12744#endif
12745
12746/*
12747 * "test_alloc_fail(id, countdown, repeat)" function
12748 */
12749 static void
12750f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12751{
12752 if (argvars[0].v_type != VAR_NUMBER
12753 || argvars[0].vval.v_number <= 0
12754 || argvars[1].v_type != VAR_NUMBER
12755 || argvars[1].vval.v_number < 0
12756 || argvars[2].v_type != VAR_NUMBER)
12757 EMSG(_(e_invarg));
12758 else
12759 {
12760 alloc_fail_id = argvars[0].vval.v_number;
12761 if (alloc_fail_id >= aid_last)
12762 EMSG(_(e_invarg));
12763 alloc_fail_countdown = argvars[1].vval.v_number;
12764 alloc_fail_repeat = argvars[2].vval.v_number;
12765 did_outofmem_msg = FALSE;
12766 }
12767}
12768
12769/*
12770 * "test_autochdir()"
12771 */
12772 static void
12773f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12774{
12775#if defined(FEAT_AUTOCHDIR)
12776 test_autochdir = TRUE;
12777#endif
12778}
12779
12780/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012781 * "test_feedinput()"
12782 */
12783 static void
12784f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12785{
12786#ifdef USE_INPUT_BUF
12787 char_u *val = get_tv_string_chk(&argvars[0]);
12788
12789 if (val != NULL)
12790 {
12791 trash_input_buf();
12792 add_to_input_buf_csi(val, (int)STRLEN(val));
12793 }
12794#endif
12795}
12796
12797/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012798 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012799 */
12800 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012801f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012802{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012803 char_u *name = (char_u *)"";
12804 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012805 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012806
12807 if (argvars[0].v_type != VAR_STRING
12808 || (argvars[1].v_type) != VAR_NUMBER)
12809 EMSG(_(e_invarg));
12810 else
12811 {
12812 name = get_tv_string_chk(&argvars[0]);
12813 val = (int)get_tv_number(&argvars[1]);
12814
12815 if (STRCMP(name, (char_u *)"redraw") == 0)
12816 disable_redraw_for_testing = val;
12817 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12818 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012819 else if (STRCMP(name, (char_u *)"starting") == 0)
12820 {
12821 if (val)
12822 {
12823 if (save_starting < 0)
12824 save_starting = starting;
12825 starting = 0;
12826 }
12827 else
12828 {
12829 starting = save_starting;
12830 save_starting = -1;
12831 }
12832 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012833 else if (STRCMP(name, (char_u *)"ALL") == 0)
12834 {
12835 disable_char_avail_for_testing = FALSE;
12836 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012837 if (save_starting >= 0)
12838 {
12839 starting = save_starting;
12840 save_starting = -1;
12841 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012842 }
12843 else
12844 EMSG2(_(e_invarg2), name);
12845 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012846}
12847
12848/*
12849 * "test_garbagecollect_now()" function
12850 */
12851 static void
12852f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12853{
12854 /* This is dangerous, any Lists and Dicts used internally may be freed
12855 * while still in use. */
12856 garbage_collect(TRUE);
12857}
12858
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012859/*
12860 * "test_ignore_error()" function
12861 */
12862 static void
12863f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12864{
12865 ignore_error_for_testing(get_tv_string(&argvars[0]));
12866}
12867
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012868#ifdef FEAT_JOB_CHANNEL
12869 static void
12870f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12871{
12872 rettv->v_type = VAR_CHANNEL;
12873 rettv->vval.v_channel = NULL;
12874}
12875#endif
12876
12877 static void
12878f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12879{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012880 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012881}
12882
12883#ifdef FEAT_JOB_CHANNEL
12884 static void
12885f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12886{
12887 rettv->v_type = VAR_JOB;
12888 rettv->vval.v_job = NULL;
12889}
12890#endif
12891
12892 static void
12893f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12894{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012895 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012896}
12897
12898 static void
12899f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12900{
12901 rettv->v_type = VAR_PARTIAL;
12902 rettv->vval.v_partial = NULL;
12903}
12904
12905 static void
12906f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12907{
12908 rettv->v_type = VAR_STRING;
12909 rettv->vval.v_string = NULL;
12910}
12911
12912 static void
12913f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12914{
12915 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12916}
12917
12918#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12919/*
12920 * Get a callback from "arg". It can be a Funcref or a function name.
12921 * When "arg" is zero return an empty string.
12922 * Return NULL for an invalid argument.
12923 */
12924 char_u *
12925get_callback(typval_T *arg, partial_T **pp)
12926{
12927 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12928 {
12929 *pp = arg->vval.v_partial;
12930 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012931 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012932 }
12933 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012934 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012935 {
12936 func_ref(arg->vval.v_string);
12937 return arg->vval.v_string;
12938 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012939 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12940 return (char_u *)"";
12941 EMSG(_("E921: Invalid callback argument"));
12942 return NULL;
12943}
12944
12945/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020012946 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012947 */
12948 void
12949free_callback(char_u *callback, partial_T *partial)
12950{
12951 if (partial != NULL)
12952 partial_unref(partial);
12953 else if (callback != NULL)
12954 {
12955 func_unref(callback);
12956 vim_free(callback);
12957 }
12958}
12959#endif
12960
12961#ifdef FEAT_TIMERS
12962/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012963 * "timer_info([timer])" function
12964 */
12965 static void
12966f_timer_info(typval_T *argvars, typval_T *rettv)
12967{
12968 timer_T *timer = NULL;
12969
12970 if (rettv_list_alloc(rettv) != OK)
12971 return;
12972 if (argvars[0].v_type != VAR_UNKNOWN)
12973 {
12974 if (argvars[0].v_type != VAR_NUMBER)
12975 EMSG(_(e_number_exp));
12976 else
12977 {
12978 timer = find_timer((int)get_tv_number(&argvars[0]));
12979 if (timer != NULL)
12980 add_timer_info(rettv, timer);
12981 }
12982 }
12983 else
12984 add_timer_info_all(rettv);
12985}
12986
12987/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012988 * "timer_pause(timer, paused)" function
12989 */
12990 static void
12991f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12992{
12993 timer_T *timer = NULL;
12994 int paused = (int)get_tv_number(&argvars[1]);
12995
12996 if (argvars[0].v_type != VAR_NUMBER)
12997 EMSG(_(e_number_exp));
12998 else
12999 {
13000 timer = find_timer((int)get_tv_number(&argvars[0]));
13001 if (timer != NULL)
13002 timer->tr_paused = paused;
13003 }
13004}
13005
13006/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013007 * "timer_start(time, callback [, options])" function
13008 */
13009 static void
13010f_timer_start(typval_T *argvars, typval_T *rettv)
13011{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013012 long msec = (long)get_tv_number(&argvars[0]);
13013 timer_T *timer;
13014 int repeat = 0;
13015 char_u *callback;
13016 dict_T *dict;
13017 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013018
Bram Moolenaar75537a92016-09-05 22:45:28 +020013019 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013020 if (check_secure())
13021 return;
13022 if (argvars[2].v_type != VAR_UNKNOWN)
13023 {
13024 if (argvars[2].v_type != VAR_DICT
13025 || (dict = argvars[2].vval.v_dict) == NULL)
13026 {
13027 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13028 return;
13029 }
13030 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13031 repeat = get_dict_number(dict, (char_u *)"repeat");
13032 }
13033
Bram Moolenaar75537a92016-09-05 22:45:28 +020013034 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013035 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013036 return;
13037
13038 timer = create_timer(msec, repeat);
13039 if (timer == NULL)
13040 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013041 else
13042 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013043 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013044 timer->tr_callback = vim_strsave(callback);
13045 else
13046 /* pointer into the partial */
13047 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013048 timer->tr_partial = partial;
13049 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013050 }
13051}
13052
13053/*
13054 * "timer_stop(timer)" function
13055 */
13056 static void
13057f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13058{
13059 timer_T *timer;
13060
13061 if (argvars[0].v_type != VAR_NUMBER)
13062 {
13063 EMSG(_(e_number_exp));
13064 return;
13065 }
13066 timer = find_timer((int)get_tv_number(&argvars[0]));
13067 if (timer != NULL)
13068 stop_timer(timer);
13069}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013070
13071/*
13072 * "timer_stopall()" function
13073 */
13074 static void
13075f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13076{
13077 stop_all_timers();
13078}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013079#endif
13080
13081/*
13082 * "tolower(string)" function
13083 */
13084 static void
13085f_tolower(typval_T *argvars, typval_T *rettv)
13086{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013087 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013088 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013089}
13090
13091/*
13092 * "toupper(string)" function
13093 */
13094 static void
13095f_toupper(typval_T *argvars, typval_T *rettv)
13096{
13097 rettv->v_type = VAR_STRING;
13098 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13099}
13100
13101/*
13102 * "tr(string, fromstr, tostr)" function
13103 */
13104 static void
13105f_tr(typval_T *argvars, typval_T *rettv)
13106{
13107 char_u *in_str;
13108 char_u *fromstr;
13109 char_u *tostr;
13110 char_u *p;
13111#ifdef FEAT_MBYTE
13112 int inlen;
13113 int fromlen;
13114 int tolen;
13115 int idx;
13116 char_u *cpstr;
13117 int cplen;
13118 int first = TRUE;
13119#endif
13120 char_u buf[NUMBUFLEN];
13121 char_u buf2[NUMBUFLEN];
13122 garray_T ga;
13123
13124 in_str = get_tv_string(&argvars[0]);
13125 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13126 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13127
13128 /* Default return value: empty string. */
13129 rettv->v_type = VAR_STRING;
13130 rettv->vval.v_string = NULL;
13131 if (fromstr == NULL || tostr == NULL)
13132 return; /* type error; errmsg already given */
13133 ga_init2(&ga, (int)sizeof(char), 80);
13134
13135#ifdef FEAT_MBYTE
13136 if (!has_mbyte)
13137#endif
13138 /* not multi-byte: fromstr and tostr must be the same length */
13139 if (STRLEN(fromstr) != STRLEN(tostr))
13140 {
13141#ifdef FEAT_MBYTE
13142error:
13143#endif
13144 EMSG2(_(e_invarg2), fromstr);
13145 ga_clear(&ga);
13146 return;
13147 }
13148
13149 /* fromstr and tostr have to contain the same number of chars */
13150 while (*in_str != NUL)
13151 {
13152#ifdef FEAT_MBYTE
13153 if (has_mbyte)
13154 {
13155 inlen = (*mb_ptr2len)(in_str);
13156 cpstr = in_str;
13157 cplen = inlen;
13158 idx = 0;
13159 for (p = fromstr; *p != NUL; p += fromlen)
13160 {
13161 fromlen = (*mb_ptr2len)(p);
13162 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13163 {
13164 for (p = tostr; *p != NUL; p += tolen)
13165 {
13166 tolen = (*mb_ptr2len)(p);
13167 if (idx-- == 0)
13168 {
13169 cplen = tolen;
13170 cpstr = p;
13171 break;
13172 }
13173 }
13174 if (*p == NUL) /* tostr is shorter than fromstr */
13175 goto error;
13176 break;
13177 }
13178 ++idx;
13179 }
13180
13181 if (first && cpstr == in_str)
13182 {
13183 /* Check that fromstr and tostr have the same number of
13184 * (multi-byte) characters. Done only once when a character
13185 * of in_str doesn't appear in fromstr. */
13186 first = FALSE;
13187 for (p = tostr; *p != NUL; p += tolen)
13188 {
13189 tolen = (*mb_ptr2len)(p);
13190 --idx;
13191 }
13192 if (idx != 0)
13193 goto error;
13194 }
13195
13196 (void)ga_grow(&ga, cplen);
13197 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13198 ga.ga_len += cplen;
13199
13200 in_str += inlen;
13201 }
13202 else
13203#endif
13204 {
13205 /* When not using multi-byte chars we can do it faster. */
13206 p = vim_strchr(fromstr, *in_str);
13207 if (p != NULL)
13208 ga_append(&ga, tostr[p - fromstr]);
13209 else
13210 ga_append(&ga, *in_str);
13211 ++in_str;
13212 }
13213 }
13214
13215 /* add a terminating NUL */
13216 (void)ga_grow(&ga, 1);
13217 ga_append(&ga, NUL);
13218
13219 rettv->vval.v_string = ga.ga_data;
13220}
13221
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013222/*
13223 * "trim({expr})" function
13224 */
13225 static void
13226f_trim(typval_T *argvars, typval_T *rettv)
13227{
13228 char_u buf1[NUMBUFLEN];
13229 char_u buf2[NUMBUFLEN];
13230 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13231 char_u *mask = NULL;
13232 char_u *tail;
13233 char_u *prev;
13234 char_u *p;
13235 int c1;
13236
13237 rettv->v_type = VAR_STRING;
13238 if (head == NULL)
13239 {
13240 rettv->vval.v_string = NULL;
13241 return;
13242 }
13243
13244 if (argvars[1].v_type == VAR_STRING)
13245 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13246
13247 while (*head != NUL)
13248 {
13249 c1 = PTR2CHAR(head);
13250 if (mask == NULL)
13251 {
13252 if (c1 > ' ' && c1 != 0xa0)
13253 break;
13254 }
13255 else
13256 {
13257 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13258 if (c1 == PTR2CHAR(p))
13259 break;
13260 if (*p == NUL)
13261 break;
13262 }
13263 MB_PTR_ADV(head);
13264 }
13265
13266 for (tail = head + STRLEN(head); tail > head; tail = prev)
13267 {
13268 prev = tail;
13269 MB_PTR_BACK(head, prev);
13270 c1 = PTR2CHAR(prev);
13271 if (mask == NULL)
13272 {
13273 if (c1 > ' ' && c1 != 0xa0)
13274 break;
13275 }
13276 else
13277 {
13278 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13279 if (c1 == PTR2CHAR(p))
13280 break;
13281 if (*p == NUL)
13282 break;
13283 }
13284 }
13285 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13286}
13287
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013288#ifdef FEAT_FLOAT
13289/*
13290 * "trunc({float})" function
13291 */
13292 static void
13293f_trunc(typval_T *argvars, typval_T *rettv)
13294{
13295 float_T f = 0.0;
13296
13297 rettv->v_type = VAR_FLOAT;
13298 if (get_float_arg(argvars, &f) == OK)
13299 /* trunc() is not in C90, use floor() or ceil() instead. */
13300 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13301 else
13302 rettv->vval.v_float = 0.0;
13303}
13304#endif
13305
13306/*
13307 * "type(expr)" function
13308 */
13309 static void
13310f_type(typval_T *argvars, typval_T *rettv)
13311{
13312 int n = -1;
13313
13314 switch (argvars[0].v_type)
13315 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013316 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13317 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013318 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013319 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13320 case VAR_LIST: n = VAR_TYPE_LIST; break;
13321 case VAR_DICT: n = VAR_TYPE_DICT; break;
13322 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013323 case VAR_SPECIAL:
13324 if (argvars[0].vval.v_number == VVAL_FALSE
13325 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013326 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013327 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013328 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013329 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013330 case VAR_JOB: n = VAR_TYPE_JOB; break;
13331 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013332 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013333 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013334 n = -1;
13335 break;
13336 }
13337 rettv->vval.v_number = n;
13338}
13339
13340/*
13341 * "undofile(name)" function
13342 */
13343 static void
13344f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13345{
13346 rettv->v_type = VAR_STRING;
13347#ifdef FEAT_PERSISTENT_UNDO
13348 {
13349 char_u *fname = get_tv_string(&argvars[0]);
13350
13351 if (*fname == NUL)
13352 {
13353 /* If there is no file name there will be no undo file. */
13354 rettv->vval.v_string = NULL;
13355 }
13356 else
13357 {
13358 char_u *ffname = FullName_save(fname, FALSE);
13359
13360 if (ffname != NULL)
13361 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13362 vim_free(ffname);
13363 }
13364 }
13365#else
13366 rettv->vval.v_string = NULL;
13367#endif
13368}
13369
13370/*
13371 * "undotree()" function
13372 */
13373 static void
13374f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13375{
13376 if (rettv_dict_alloc(rettv) == OK)
13377 {
13378 dict_T *dict = rettv->vval.v_dict;
13379 list_T *list;
13380
13381 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13382 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13383 dict_add_nr_str(dict, "save_last",
13384 (long)curbuf->b_u_save_nr_last, NULL);
13385 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13386 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13387 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13388
13389 list = list_alloc();
13390 if (list != NULL)
13391 {
13392 u_eval_tree(curbuf->b_u_oldhead, list);
13393 dict_add_list(dict, "entries", list);
13394 }
13395 }
13396}
13397
13398/*
13399 * "values(dict)" function
13400 */
13401 static void
13402f_values(typval_T *argvars, typval_T *rettv)
13403{
13404 dict_list(argvars, rettv, 1);
13405}
13406
13407/*
13408 * "virtcol(string)" function
13409 */
13410 static void
13411f_virtcol(typval_T *argvars, typval_T *rettv)
13412{
13413 colnr_T vcol = 0;
13414 pos_T *fp;
13415 int fnum = curbuf->b_fnum;
13416
13417 fp = var2fpos(&argvars[0], FALSE, &fnum);
13418 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13419 && fnum == curbuf->b_fnum)
13420 {
13421 getvvcol(curwin, fp, NULL, NULL, &vcol);
13422 ++vcol;
13423 }
13424
13425 rettv->vval.v_number = vcol;
13426}
13427
13428/*
13429 * "visualmode()" function
13430 */
13431 static void
13432f_visualmode(typval_T *argvars, typval_T *rettv)
13433{
13434 char_u str[2];
13435
13436 rettv->v_type = VAR_STRING;
13437 str[0] = curbuf->b_visual_mode_eval;
13438 str[1] = NUL;
13439 rettv->vval.v_string = vim_strsave(str);
13440
13441 /* A non-zero number or non-empty string argument: reset mode. */
13442 if (non_zero_arg(&argvars[0]))
13443 curbuf->b_visual_mode_eval = NUL;
13444}
13445
13446/*
13447 * "wildmenumode()" function
13448 */
13449 static void
13450f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13451{
13452#ifdef FEAT_WILDMENU
13453 if (wild_menu_showing)
13454 rettv->vval.v_number = 1;
13455#endif
13456}
13457
13458/*
13459 * "winbufnr(nr)" function
13460 */
13461 static void
13462f_winbufnr(typval_T *argvars, typval_T *rettv)
13463{
13464 win_T *wp;
13465
13466 wp = find_win_by_nr(&argvars[0], NULL);
13467 if (wp == NULL)
13468 rettv->vval.v_number = -1;
13469 else
13470 rettv->vval.v_number = wp->w_buffer->b_fnum;
13471}
13472
13473/*
13474 * "wincol()" function
13475 */
13476 static void
13477f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13478{
13479 validate_cursor();
13480 rettv->vval.v_number = curwin->w_wcol + 1;
13481}
13482
13483/*
13484 * "winheight(nr)" function
13485 */
13486 static void
13487f_winheight(typval_T *argvars, typval_T *rettv)
13488{
13489 win_T *wp;
13490
13491 wp = find_win_by_nr(&argvars[0], NULL);
13492 if (wp == NULL)
13493 rettv->vval.v_number = -1;
13494 else
13495 rettv->vval.v_number = wp->w_height;
13496}
13497
13498/*
13499 * "winline()" function
13500 */
13501 static void
13502f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13503{
13504 validate_cursor();
13505 rettv->vval.v_number = curwin->w_wrow + 1;
13506}
13507
13508/*
13509 * "winnr()" function
13510 */
13511 static void
13512f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13513{
13514 int nr = 1;
13515
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013516 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013517 rettv->vval.v_number = nr;
13518}
13519
13520/*
13521 * "winrestcmd()" function
13522 */
13523 static void
13524f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13525{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013526 win_T *wp;
13527 int winnr = 1;
13528 garray_T ga;
13529 char_u buf[50];
13530
13531 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013532 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013533 {
13534 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13535 ga_concat(&ga, buf);
13536 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13537 ga_concat(&ga, buf);
13538 ++winnr;
13539 }
13540 ga_append(&ga, NUL);
13541
13542 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013543 rettv->v_type = VAR_STRING;
13544}
13545
13546/*
13547 * "winrestview()" function
13548 */
13549 static void
13550f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13551{
13552 dict_T *dict;
13553
13554 if (argvars[0].v_type != VAR_DICT
13555 || (dict = argvars[0].vval.v_dict) == NULL)
13556 EMSG(_(e_invarg));
13557 else
13558 {
13559 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13560 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13561 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13562 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13563#ifdef FEAT_VIRTUALEDIT
13564 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13565 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13566#endif
13567 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13568 {
13569 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13570 curwin->w_set_curswant = FALSE;
13571 }
13572
13573 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13574 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13575#ifdef FEAT_DIFF
13576 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13577 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13578#endif
13579 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13580 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13581 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13582 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13583
13584 check_cursor();
13585 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013586 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013587 changed_window_setting();
13588
13589 if (curwin->w_topline <= 0)
13590 curwin->w_topline = 1;
13591 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13592 curwin->w_topline = curbuf->b_ml.ml_line_count;
13593#ifdef FEAT_DIFF
13594 check_topfill(curwin, TRUE);
13595#endif
13596 }
13597}
13598
13599/*
13600 * "winsaveview()" function
13601 */
13602 static void
13603f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13604{
13605 dict_T *dict;
13606
13607 if (rettv_dict_alloc(rettv) == FAIL)
13608 return;
13609 dict = rettv->vval.v_dict;
13610
13611 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13612 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13613#ifdef FEAT_VIRTUALEDIT
13614 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13615#endif
13616 update_curswant();
13617 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13618
13619 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13620#ifdef FEAT_DIFF
13621 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13622#endif
13623 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13624 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13625}
13626
13627/*
13628 * "winwidth(nr)" function
13629 */
13630 static void
13631f_winwidth(typval_T *argvars, typval_T *rettv)
13632{
13633 win_T *wp;
13634
13635 wp = find_win_by_nr(&argvars[0], NULL);
13636 if (wp == NULL)
13637 rettv->vval.v_number = -1;
13638 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013639 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013640}
13641
13642/*
13643 * "wordcount()" function
13644 */
13645 static void
13646f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13647{
13648 if (rettv_dict_alloc(rettv) == FAIL)
13649 return;
13650 cursor_pos_info(rettv->vval.v_dict);
13651}
13652
13653/*
13654 * "writefile()" function
13655 */
13656 static void
13657f_writefile(typval_T *argvars, typval_T *rettv)
13658{
13659 int binary = FALSE;
13660 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013661#ifdef HAVE_FSYNC
13662 int do_fsync = p_fs;
13663#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013664 char_u *fname;
13665 FILE *fd;
13666 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013667 listitem_T *li;
13668 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013669
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013670 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013671 if (check_restricted() || check_secure())
13672 return;
13673
13674 if (argvars[0].v_type != VAR_LIST)
13675 {
13676 EMSG2(_(e_listarg), "writefile()");
13677 return;
13678 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013679 list = argvars[0].vval.v_list;
13680 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013681 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013682 for (li = list->lv_first; li != NULL; li = li->li_next)
13683 if (get_tv_string_chk(&li->li_tv) == NULL)
13684 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013685
13686 if (argvars[2].v_type != VAR_UNKNOWN)
13687 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013688 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13689
13690 if (arg2 == NULL)
13691 return;
13692 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013693 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013694 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013695 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013696#ifdef HAVE_FSYNC
13697 if (vim_strchr(arg2, 's') != NULL)
13698 do_fsync = TRUE;
13699 else if (vim_strchr(arg2, 'S') != NULL)
13700 do_fsync = FALSE;
13701#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013702 }
13703
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013704 fname = get_tv_string_chk(&argvars[1]);
13705 if (fname == NULL)
13706 return;
13707
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013708 /* Always open the file in binary mode, library functions have a mind of
13709 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013710 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13711 append ? APPENDBIN : WRITEBIN)) == NULL)
13712 {
13713 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13714 ret = -1;
13715 }
13716 else
13717 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013718 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013719 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013720#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013721 else if (do_fsync)
13722 /* Ignore the error, the user wouldn't know what to do about it.
13723 * May happen for a device. */
13724 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013725#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013726 fclose(fd);
13727 }
13728
13729 rettv->vval.v_number = ret;
13730}
13731
13732/*
13733 * "xor(expr, expr)" function
13734 */
13735 static void
13736f_xor(typval_T *argvars, typval_T *rettv)
13737{
13738 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13739 ^ get_tv_number_chk(&argvars[1], NULL);
13740}
13741
13742
13743#endif /* FEAT_EVAL */