blob: 1ffbb21fa1669e4115ceeb8332277d100984aae8 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
32#ifdef FEAT_QUICKFIX
33static char *e_stringreq = N_("E928: String required");
34#endif
35
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
43static void f_argc(typval_T *argvars, typval_T *rettv);
44static void f_argidx(typval_T *argvars, typval_T *rettv);
45static void f_arglistid(typval_T *argvars, typval_T *rettv);
46static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010047static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010049static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_assert_exception(typval_T *argvars, typval_T *rettv);
51static void f_assert_fails(typval_T *argvars, typval_T *rettv);
52static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020053static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020054static void f_assert_match(typval_T *argvars, typval_T *rettv);
55static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
56static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010057static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020058static void f_assert_true(typval_T *argvars, typval_T *rettv);
59#ifdef FEAT_FLOAT
60static void f_asin(typval_T *argvars, typval_T *rettv);
61static void f_atan(typval_T *argvars, typval_T *rettv);
62static void f_atan2(typval_T *argvars, typval_T *rettv);
63#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010064#ifdef FEAT_BEVAL
65static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010066# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010067static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010069#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_browse(typval_T *argvars, typval_T *rettv);
71static void f_browsedir(typval_T *argvars, typval_T *rettv);
72static void f_bufexists(typval_T *argvars, typval_T *rettv);
73static void f_buflisted(typval_T *argvars, typval_T *rettv);
74static void f_bufloaded(typval_T *argvars, typval_T *rettv);
75static void f_bufname(typval_T *argvars, typval_T *rettv);
76static void f_bufnr(typval_T *argvars, typval_T *rettv);
77static void f_bufwinid(typval_T *argvars, typval_T *rettv);
78static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
79static void f_byte2line(typval_T *argvars, typval_T *rettv);
80static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
81static void f_byteidx(typval_T *argvars, typval_T *rettv);
82static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
83static void f_call(typval_T *argvars, typval_T *rettv);
84#ifdef FEAT_FLOAT
85static void f_ceil(typval_T *argvars, typval_T *rettv);
86#endif
87#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010088static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020090static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
92static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
93static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
94static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
95static void f_ch_info(typval_T *argvars, typval_T *rettv);
96static void f_ch_log(typval_T *argvars, typval_T *rettv);
97static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
98static void f_ch_open(typval_T *argvars, typval_T *rettv);
99static void f_ch_read(typval_T *argvars, typval_T *rettv);
100static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
101static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
102static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
103static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
104static void f_ch_status(typval_T *argvars, typval_T *rettv);
105#endif
106static void f_changenr(typval_T *argvars, typval_T *rettv);
107static void f_char2nr(typval_T *argvars, typval_T *rettv);
108static void f_cindent(typval_T *argvars, typval_T *rettv);
109static void f_clearmatches(typval_T *argvars, typval_T *rettv);
110static void f_col(typval_T *argvars, typval_T *rettv);
111#if defined(FEAT_INS_EXPAND)
112static void f_complete(typval_T *argvars, typval_T *rettv);
113static void f_complete_add(typval_T *argvars, typval_T *rettv);
114static void f_complete_check(typval_T *argvars, typval_T *rettv);
115#endif
116static void f_confirm(typval_T *argvars, typval_T *rettv);
117static void f_copy(typval_T *argvars, typval_T *rettv);
118#ifdef FEAT_FLOAT
119static void f_cos(typval_T *argvars, typval_T *rettv);
120static void f_cosh(typval_T *argvars, typval_T *rettv);
121#endif
122static void f_count(typval_T *argvars, typval_T *rettv);
123static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
124static void f_cursor(typval_T *argsvars, typval_T *rettv);
125static void f_deepcopy(typval_T *argvars, typval_T *rettv);
126static void f_delete(typval_T *argvars, typval_T *rettv);
127static void f_did_filetype(typval_T *argvars, typval_T *rettv);
128static void f_diff_filler(typval_T *argvars, typval_T *rettv);
129static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
130static void f_empty(typval_T *argvars, typval_T *rettv);
131static void f_escape(typval_T *argvars, typval_T *rettv);
132static void f_eval(typval_T *argvars, typval_T *rettv);
133static void f_eventhandler(typval_T *argvars, typval_T *rettv);
134static void f_executable(typval_T *argvars, typval_T *rettv);
135static void f_execute(typval_T *argvars, typval_T *rettv);
136static void f_exepath(typval_T *argvars, typval_T *rettv);
137static void f_exists(typval_T *argvars, typval_T *rettv);
138#ifdef FEAT_FLOAT
139static void f_exp(typval_T *argvars, typval_T *rettv);
140#endif
141static void f_expand(typval_T *argvars, typval_T *rettv);
142static void f_extend(typval_T *argvars, typval_T *rettv);
143static void f_feedkeys(typval_T *argvars, typval_T *rettv);
144static void f_filereadable(typval_T *argvars, typval_T *rettv);
145static void f_filewritable(typval_T *argvars, typval_T *rettv);
146static void f_filter(typval_T *argvars, typval_T *rettv);
147static void f_finddir(typval_T *argvars, typval_T *rettv);
148static void f_findfile(typval_T *argvars, typval_T *rettv);
149#ifdef FEAT_FLOAT
150static void f_float2nr(typval_T *argvars, typval_T *rettv);
151static void f_floor(typval_T *argvars, typval_T *rettv);
152static void f_fmod(typval_T *argvars, typval_T *rettv);
153#endif
154static void f_fnameescape(typval_T *argvars, typval_T *rettv);
155static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
156static void f_foldclosed(typval_T *argvars, typval_T *rettv);
157static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
158static void f_foldlevel(typval_T *argvars, typval_T *rettv);
159static void f_foldtext(typval_T *argvars, typval_T *rettv);
160static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
161static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200162static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200163static void f_function(typval_T *argvars, typval_T *rettv);
164static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
165static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200166static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200167static void f_getbufline(typval_T *argvars, typval_T *rettv);
168static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100169static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200170static void f_getchar(typval_T *argvars, typval_T *rettv);
171static void f_getcharmod(typval_T *argvars, typval_T *rettv);
172static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
173static void f_getcmdline(typval_T *argvars, typval_T *rettv);
174#if defined(FEAT_CMDL_COMPL)
175static void f_getcompletion(typval_T *argvars, typval_T *rettv);
176#endif
177static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
178static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
179static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
180static void f_getcwd(typval_T *argvars, typval_T *rettv);
181static void f_getfontname(typval_T *argvars, typval_T *rettv);
182static void f_getfperm(typval_T *argvars, typval_T *rettv);
183static void f_getfsize(typval_T *argvars, typval_T *rettv);
184static void f_getftime(typval_T *argvars, typval_T *rettv);
185static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100186static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200187static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200188static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200189static void f_getmatches(typval_T *argvars, typval_T *rettv);
190static void f_getpid(typval_T *argvars, typval_T *rettv);
191static void f_getcurpos(typval_T *argvars, typval_T *rettv);
192static void f_getpos(typval_T *argvars, typval_T *rettv);
193static void f_getqflist(typval_T *argvars, typval_T *rettv);
194static void f_getreg(typval_T *argvars, typval_T *rettv);
195static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200196static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197static void f_gettabvar(typval_T *argvars, typval_T *rettv);
198static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200199static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100200static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_getwinposx(typval_T *argvars, typval_T *rettv);
202static void f_getwinposy(typval_T *argvars, typval_T *rettv);
203static void f_getwinvar(typval_T *argvars, typval_T *rettv);
204static void f_glob(typval_T *argvars, typval_T *rettv);
205static void f_globpath(typval_T *argvars, typval_T *rettv);
206static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
207static void f_has(typval_T *argvars, typval_T *rettv);
208static void f_has_key(typval_T *argvars, typval_T *rettv);
209static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
210static void f_hasmapto(typval_T *argvars, typval_T *rettv);
211static void f_histadd(typval_T *argvars, typval_T *rettv);
212static void f_histdel(typval_T *argvars, typval_T *rettv);
213static void f_histget(typval_T *argvars, typval_T *rettv);
214static void f_histnr(typval_T *argvars, typval_T *rettv);
215static void f_hlID(typval_T *argvars, typval_T *rettv);
216static void f_hlexists(typval_T *argvars, typval_T *rettv);
217static void f_hostname(typval_T *argvars, typval_T *rettv);
218static void f_iconv(typval_T *argvars, typval_T *rettv);
219static void f_indent(typval_T *argvars, typval_T *rettv);
220static void f_index(typval_T *argvars, typval_T *rettv);
221static void f_input(typval_T *argvars, typval_T *rettv);
222static void f_inputdialog(typval_T *argvars, typval_T *rettv);
223static void f_inputlist(typval_T *argvars, typval_T *rettv);
224static void f_inputrestore(typval_T *argvars, typval_T *rettv);
225static void f_inputsave(typval_T *argvars, typval_T *rettv);
226static void f_inputsecret(typval_T *argvars, typval_T *rettv);
227static void f_insert(typval_T *argvars, typval_T *rettv);
228static void f_invert(typval_T *argvars, typval_T *rettv);
229static void f_isdirectory(typval_T *argvars, typval_T *rettv);
230static void f_islocked(typval_T *argvars, typval_T *rettv);
231#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
232static void f_isnan(typval_T *argvars, typval_T *rettv);
233#endif
234static void f_items(typval_T *argvars, typval_T *rettv);
235#ifdef FEAT_JOB_CHANNEL
236static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
237static void f_job_info(typval_T *argvars, typval_T *rettv);
238static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
239static void f_job_start(typval_T *argvars, typval_T *rettv);
240static void f_job_stop(typval_T *argvars, typval_T *rettv);
241static void f_job_status(typval_T *argvars, typval_T *rettv);
242#endif
243static void f_join(typval_T *argvars, typval_T *rettv);
244static void f_js_decode(typval_T *argvars, typval_T *rettv);
245static void f_js_encode(typval_T *argvars, typval_T *rettv);
246static void f_json_decode(typval_T *argvars, typval_T *rettv);
247static void f_json_encode(typval_T *argvars, typval_T *rettv);
248static void f_keys(typval_T *argvars, typval_T *rettv);
249static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
250static void f_len(typval_T *argvars, typval_T *rettv);
251static void f_libcall(typval_T *argvars, typval_T *rettv);
252static void f_libcallnr(typval_T *argvars, typval_T *rettv);
253static void f_line(typval_T *argvars, typval_T *rettv);
254static void f_line2byte(typval_T *argvars, typval_T *rettv);
255static void f_lispindent(typval_T *argvars, typval_T *rettv);
256static void f_localtime(typval_T *argvars, typval_T *rettv);
257#ifdef FEAT_FLOAT
258static void f_log(typval_T *argvars, typval_T *rettv);
259static void f_log10(typval_T *argvars, typval_T *rettv);
260#endif
261#ifdef FEAT_LUA
262static void f_luaeval(typval_T *argvars, typval_T *rettv);
263#endif
264static void f_map(typval_T *argvars, typval_T *rettv);
265static void f_maparg(typval_T *argvars, typval_T *rettv);
266static void f_mapcheck(typval_T *argvars, typval_T *rettv);
267static void f_match(typval_T *argvars, typval_T *rettv);
268static void f_matchadd(typval_T *argvars, typval_T *rettv);
269static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
270static void f_matcharg(typval_T *argvars, typval_T *rettv);
271static void f_matchdelete(typval_T *argvars, typval_T *rettv);
272static void f_matchend(typval_T *argvars, typval_T *rettv);
273static void f_matchlist(typval_T *argvars, typval_T *rettv);
274static void f_matchstr(typval_T *argvars, typval_T *rettv);
275static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
276static void f_max(typval_T *argvars, typval_T *rettv);
277static void f_min(typval_T *argvars, typval_T *rettv);
278#ifdef vim_mkdir
279static void f_mkdir(typval_T *argvars, typval_T *rettv);
280#endif
281static void f_mode(typval_T *argvars, typval_T *rettv);
282#ifdef FEAT_MZSCHEME
283static void f_mzeval(typval_T *argvars, typval_T *rettv);
284#endif
285static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
286static void f_nr2char(typval_T *argvars, typval_T *rettv);
287static void f_or(typval_T *argvars, typval_T *rettv);
288static void f_pathshorten(typval_T *argvars, typval_T *rettv);
289#ifdef FEAT_PERL
290static void f_perleval(typval_T *argvars, typval_T *rettv);
291#endif
292#ifdef FEAT_FLOAT
293static void f_pow(typval_T *argvars, typval_T *rettv);
294#endif
295static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
296static void f_printf(typval_T *argvars, typval_T *rettv);
297static void f_pumvisible(typval_T *argvars, typval_T *rettv);
298#ifdef FEAT_PYTHON3
299static void f_py3eval(typval_T *argvars, typval_T *rettv);
300#endif
301#ifdef FEAT_PYTHON
302static void f_pyeval(typval_T *argvars, typval_T *rettv);
303#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100304#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
305static void f_pyxeval(typval_T *argvars, typval_T *rettv);
306#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200307static void f_range(typval_T *argvars, typval_T *rettv);
308static void f_readfile(typval_T *argvars, typval_T *rettv);
309static void f_reltime(typval_T *argvars, typval_T *rettv);
310#ifdef FEAT_FLOAT
311static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
312#endif
313static void f_reltimestr(typval_T *argvars, typval_T *rettv);
314static void f_remote_expr(typval_T *argvars, typval_T *rettv);
315static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
316static void f_remote_peek(typval_T *argvars, typval_T *rettv);
317static void f_remote_read(typval_T *argvars, typval_T *rettv);
318static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100319static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200320static void f_remove(typval_T *argvars, typval_T *rettv);
321static void f_rename(typval_T *argvars, typval_T *rettv);
322static void f_repeat(typval_T *argvars, typval_T *rettv);
323static void f_resolve(typval_T *argvars, typval_T *rettv);
324static void f_reverse(typval_T *argvars, typval_T *rettv);
325#ifdef FEAT_FLOAT
326static void f_round(typval_T *argvars, typval_T *rettv);
327#endif
328static void f_screenattr(typval_T *argvars, typval_T *rettv);
329static void f_screenchar(typval_T *argvars, typval_T *rettv);
330static void f_screencol(typval_T *argvars, typval_T *rettv);
331static void f_screenrow(typval_T *argvars, typval_T *rettv);
332static void f_search(typval_T *argvars, typval_T *rettv);
333static void f_searchdecl(typval_T *argvars, typval_T *rettv);
334static void f_searchpair(typval_T *argvars, typval_T *rettv);
335static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
336static void f_searchpos(typval_T *argvars, typval_T *rettv);
337static void f_server2client(typval_T *argvars, typval_T *rettv);
338static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200339static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200340static void f_setbufvar(typval_T *argvars, typval_T *rettv);
341static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
342static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
343static void f_setfperm(typval_T *argvars, typval_T *rettv);
344static void f_setline(typval_T *argvars, typval_T *rettv);
345static void f_setloclist(typval_T *argvars, typval_T *rettv);
346static void f_setmatches(typval_T *argvars, typval_T *rettv);
347static void f_setpos(typval_T *argvars, typval_T *rettv);
348static void f_setqflist(typval_T *argvars, typval_T *rettv);
349static void f_setreg(typval_T *argvars, typval_T *rettv);
350static void f_settabvar(typval_T *argvars, typval_T *rettv);
351static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
352static void f_setwinvar(typval_T *argvars, typval_T *rettv);
353#ifdef FEAT_CRYPT
354static void f_sha256(typval_T *argvars, typval_T *rettv);
355#endif /* FEAT_CRYPT */
356static void f_shellescape(typval_T *argvars, typval_T *rettv);
357static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
358static void f_simplify(typval_T *argvars, typval_T *rettv);
359#ifdef FEAT_FLOAT
360static void f_sin(typval_T *argvars, typval_T *rettv);
361static void f_sinh(typval_T *argvars, typval_T *rettv);
362#endif
363static void f_sort(typval_T *argvars, typval_T *rettv);
364static void f_soundfold(typval_T *argvars, typval_T *rettv);
365static void f_spellbadword(typval_T *argvars, typval_T *rettv);
366static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
367static void f_split(typval_T *argvars, typval_T *rettv);
368#ifdef FEAT_FLOAT
369static void f_sqrt(typval_T *argvars, typval_T *rettv);
370static void f_str2float(typval_T *argvars, typval_T *rettv);
371#endif
372static void f_str2nr(typval_T *argvars, typval_T *rettv);
373static void f_strchars(typval_T *argvars, typval_T *rettv);
374#ifdef HAVE_STRFTIME
375static void f_strftime(typval_T *argvars, typval_T *rettv);
376#endif
377static void f_strgetchar(typval_T *argvars, typval_T *rettv);
378static void f_stridx(typval_T *argvars, typval_T *rettv);
379static void f_string(typval_T *argvars, typval_T *rettv);
380static void f_strlen(typval_T *argvars, typval_T *rettv);
381static void f_strcharpart(typval_T *argvars, typval_T *rettv);
382static void f_strpart(typval_T *argvars, typval_T *rettv);
383static void f_strridx(typval_T *argvars, typval_T *rettv);
384static void f_strtrans(typval_T *argvars, typval_T *rettv);
385static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
386static void f_strwidth(typval_T *argvars, typval_T *rettv);
387static void f_submatch(typval_T *argvars, typval_T *rettv);
388static void f_substitute(typval_T *argvars, typval_T *rettv);
389static void f_synID(typval_T *argvars, typval_T *rettv);
390static void f_synIDattr(typval_T *argvars, typval_T *rettv);
391static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
392static void f_synstack(typval_T *argvars, typval_T *rettv);
393static void f_synconcealed(typval_T *argvars, typval_T *rettv);
394static void f_system(typval_T *argvars, typval_T *rettv);
395static void f_systemlist(typval_T *argvars, typval_T *rettv);
396static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
397static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
398static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
399static void f_taglist(typval_T *argvars, typval_T *rettv);
400static void f_tagfiles(typval_T *argvars, typval_T *rettv);
401static void f_tempname(typval_T *argvars, typval_T *rettv);
402static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
403static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200404static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100405static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200406static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100407static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200408#ifdef FEAT_JOB_CHANNEL
409static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
410#endif
411static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
412#ifdef FEAT_JOB_CHANNEL
413static void f_test_null_job(typval_T *argvars, typval_T *rettv);
414#endif
415static void f_test_null_list(typval_T *argvars, typval_T *rettv);
416static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
417static void f_test_null_string(typval_T *argvars, typval_T *rettv);
418static void f_test_settime(typval_T *argvars, typval_T *rettv);
419#ifdef FEAT_FLOAT
420static void f_tan(typval_T *argvars, typval_T *rettv);
421static void f_tanh(typval_T *argvars, typval_T *rettv);
422#endif
423#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200424static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200425static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200426static void f_timer_start(typval_T *argvars, typval_T *rettv);
427static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200428static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200429#endif
430static void f_tolower(typval_T *argvars, typval_T *rettv);
431static void f_toupper(typval_T *argvars, typval_T *rettv);
432static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100433static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200434#ifdef FEAT_FLOAT
435static void f_trunc(typval_T *argvars, typval_T *rettv);
436#endif
437static void f_type(typval_T *argvars, typval_T *rettv);
438static void f_undofile(typval_T *argvars, typval_T *rettv);
439static void f_undotree(typval_T *argvars, typval_T *rettv);
440static void f_uniq(typval_T *argvars, typval_T *rettv);
441static void f_values(typval_T *argvars, typval_T *rettv);
442static void f_virtcol(typval_T *argvars, typval_T *rettv);
443static void f_visualmode(typval_T *argvars, typval_T *rettv);
444static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
445static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
446static void f_win_getid(typval_T *argvars, typval_T *rettv);
447static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
448static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
449static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100450static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200451static void f_winbufnr(typval_T *argvars, typval_T *rettv);
452static void f_wincol(typval_T *argvars, typval_T *rettv);
453static void f_winheight(typval_T *argvars, typval_T *rettv);
454static void f_winline(typval_T *argvars, typval_T *rettv);
455static void f_winnr(typval_T *argvars, typval_T *rettv);
456static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
457static void f_winrestview(typval_T *argvars, typval_T *rettv);
458static void f_winsaveview(typval_T *argvars, typval_T *rettv);
459static void f_winwidth(typval_T *argvars, typval_T *rettv);
460static void f_writefile(typval_T *argvars, typval_T *rettv);
461static void f_wordcount(typval_T *argvars, typval_T *rettv);
462static void f_xor(typval_T *argvars, typval_T *rettv);
463
464/*
465 * Array with names and number of arguments of all internal functions
466 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
467 */
468static struct fst
469{
470 char *f_name; /* function name */
471 char f_min_argc; /* minimal number of arguments */
472 char f_max_argc; /* maximal number of arguments */
473 void (*f_func)(typval_T *args, typval_T *rvar);
474 /* implementation of function */
475} functions[] =
476{
477#ifdef FEAT_FLOAT
478 {"abs", 1, 1, f_abs},
479 {"acos", 1, 1, f_acos}, /* WJMc */
480#endif
481 {"add", 2, 2, f_add},
482 {"and", 2, 2, f_and},
483 {"append", 2, 2, f_append},
484 {"argc", 0, 0, f_argc},
485 {"argidx", 0, 0, f_argidx},
486 {"arglistid", 0, 2, f_arglistid},
487 {"argv", 0, 1, f_argv},
488#ifdef FEAT_FLOAT
489 {"asin", 1, 1, f_asin}, /* WJMc */
490#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100491 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200492 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100493 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200494 {"assert_exception", 1, 2, f_assert_exception},
495 {"assert_fails", 1, 2, f_assert_fails},
496 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100497 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200498 {"assert_match", 2, 3, f_assert_match},
499 {"assert_notequal", 2, 3, f_assert_notequal},
500 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100501 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200502 {"assert_true", 1, 2, f_assert_true},
503#ifdef FEAT_FLOAT
504 {"atan", 1, 1, f_atan},
505 {"atan2", 2, 2, f_atan2},
506#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100507#ifdef FEAT_BEVAL
508 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100509# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100510 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100511# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100512#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200513 {"browse", 4, 4, f_browse},
514 {"browsedir", 2, 2, f_browsedir},
515 {"bufexists", 1, 1, f_bufexists},
516 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
517 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
518 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
519 {"buflisted", 1, 1, f_buflisted},
520 {"bufloaded", 1, 1, f_bufloaded},
521 {"bufname", 1, 1, f_bufname},
522 {"bufnr", 1, 2, f_bufnr},
523 {"bufwinid", 1, 1, f_bufwinid},
524 {"bufwinnr", 1, 1, f_bufwinnr},
525 {"byte2line", 1, 1, f_byte2line},
526 {"byteidx", 2, 2, f_byteidx},
527 {"byteidxcomp", 2, 2, f_byteidxcomp},
528 {"call", 2, 3, f_call},
529#ifdef FEAT_FLOAT
530 {"ceil", 1, 1, f_ceil},
531#endif
532#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100533 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200534 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200535 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200536 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
537 {"ch_evalraw", 2, 3, f_ch_evalraw},
538 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
539 {"ch_getjob", 1, 1, f_ch_getjob},
540 {"ch_info", 1, 1, f_ch_info},
541 {"ch_log", 1, 2, f_ch_log},
542 {"ch_logfile", 1, 2, f_ch_logfile},
543 {"ch_open", 1, 2, f_ch_open},
544 {"ch_read", 1, 2, f_ch_read},
545 {"ch_readraw", 1, 2, f_ch_readraw},
546 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
547 {"ch_sendraw", 2, 3, f_ch_sendraw},
548 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200549 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550#endif
551 {"changenr", 0, 0, f_changenr},
552 {"char2nr", 1, 2, f_char2nr},
553 {"cindent", 1, 1, f_cindent},
554 {"clearmatches", 0, 0, f_clearmatches},
555 {"col", 1, 1, f_col},
556#if defined(FEAT_INS_EXPAND)
557 {"complete", 2, 2, f_complete},
558 {"complete_add", 1, 1, f_complete_add},
559 {"complete_check", 0, 0, f_complete_check},
560#endif
561 {"confirm", 1, 4, f_confirm},
562 {"copy", 1, 1, f_copy},
563#ifdef FEAT_FLOAT
564 {"cos", 1, 1, f_cos},
565 {"cosh", 1, 1, f_cosh},
566#endif
567 {"count", 2, 4, f_count},
568 {"cscope_connection",0,3, f_cscope_connection},
569 {"cursor", 1, 3, f_cursor},
570 {"deepcopy", 1, 2, f_deepcopy},
571 {"delete", 1, 2, f_delete},
572 {"did_filetype", 0, 0, f_did_filetype},
573 {"diff_filler", 1, 1, f_diff_filler},
574 {"diff_hlID", 2, 2, f_diff_hlID},
575 {"empty", 1, 1, f_empty},
576 {"escape", 2, 2, f_escape},
577 {"eval", 1, 1, f_eval},
578 {"eventhandler", 0, 0, f_eventhandler},
579 {"executable", 1, 1, f_executable},
580 {"execute", 1, 2, f_execute},
581 {"exepath", 1, 1, f_exepath},
582 {"exists", 1, 1, f_exists},
583#ifdef FEAT_FLOAT
584 {"exp", 1, 1, f_exp},
585#endif
586 {"expand", 1, 3, f_expand},
587 {"extend", 2, 3, f_extend},
588 {"feedkeys", 1, 2, f_feedkeys},
589 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
590 {"filereadable", 1, 1, f_filereadable},
591 {"filewritable", 1, 1, f_filewritable},
592 {"filter", 2, 2, f_filter},
593 {"finddir", 1, 3, f_finddir},
594 {"findfile", 1, 3, f_findfile},
595#ifdef FEAT_FLOAT
596 {"float2nr", 1, 1, f_float2nr},
597 {"floor", 1, 1, f_floor},
598 {"fmod", 2, 2, f_fmod},
599#endif
600 {"fnameescape", 1, 1, f_fnameescape},
601 {"fnamemodify", 2, 2, f_fnamemodify},
602 {"foldclosed", 1, 1, f_foldclosed},
603 {"foldclosedend", 1, 1, f_foldclosedend},
604 {"foldlevel", 1, 1, f_foldlevel},
605 {"foldtext", 0, 0, f_foldtext},
606 {"foldtextresult", 1, 1, f_foldtextresult},
607 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200608 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200609 {"function", 1, 3, f_function},
610 {"garbagecollect", 0, 1, f_garbagecollect},
611 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200612 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200613 {"getbufline", 2, 3, f_getbufline},
614 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100615 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200616 {"getchar", 0, 1, f_getchar},
617 {"getcharmod", 0, 0, f_getcharmod},
618 {"getcharsearch", 0, 0, f_getcharsearch},
619 {"getcmdline", 0, 0, f_getcmdline},
620 {"getcmdpos", 0, 0, f_getcmdpos},
621 {"getcmdtype", 0, 0, f_getcmdtype},
622 {"getcmdwintype", 0, 0, f_getcmdwintype},
623#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200624 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200625#endif
626 {"getcurpos", 0, 0, f_getcurpos},
627 {"getcwd", 0, 2, f_getcwd},
628 {"getfontname", 0, 1, f_getfontname},
629 {"getfperm", 1, 1, f_getfperm},
630 {"getfsize", 1, 1, f_getfsize},
631 {"getftime", 1, 1, f_getftime},
632 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100633 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200634 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200635 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636 {"getmatches", 0, 0, f_getmatches},
637 {"getpid", 0, 0, f_getpid},
638 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200639 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200640 {"getreg", 0, 3, f_getreg},
641 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200642 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200643 {"gettabvar", 2, 3, f_gettabvar},
644 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200645 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100646 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200647 {"getwinposx", 0, 0, f_getwinposx},
648 {"getwinposy", 0, 0, f_getwinposy},
649 {"getwinvar", 2, 3, f_getwinvar},
650 {"glob", 1, 4, f_glob},
651 {"glob2regpat", 1, 1, f_glob2regpat},
652 {"globpath", 2, 5, f_globpath},
653 {"has", 1, 1, f_has},
654 {"has_key", 2, 2, f_has_key},
655 {"haslocaldir", 0, 2, f_haslocaldir},
656 {"hasmapto", 1, 3, f_hasmapto},
657 {"highlightID", 1, 1, f_hlID}, /* obsolete */
658 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
659 {"histadd", 2, 2, f_histadd},
660 {"histdel", 1, 2, f_histdel},
661 {"histget", 1, 2, f_histget},
662 {"histnr", 1, 1, f_histnr},
663 {"hlID", 1, 1, f_hlID},
664 {"hlexists", 1, 1, f_hlexists},
665 {"hostname", 0, 0, f_hostname},
666 {"iconv", 3, 3, f_iconv},
667 {"indent", 1, 1, f_indent},
668 {"index", 2, 4, f_index},
669 {"input", 1, 3, f_input},
670 {"inputdialog", 1, 3, f_inputdialog},
671 {"inputlist", 1, 1, f_inputlist},
672 {"inputrestore", 0, 0, f_inputrestore},
673 {"inputsave", 0, 0, f_inputsave},
674 {"inputsecret", 1, 2, f_inputsecret},
675 {"insert", 2, 3, f_insert},
676 {"invert", 1, 1, f_invert},
677 {"isdirectory", 1, 1, f_isdirectory},
678 {"islocked", 1, 1, f_islocked},
679#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
680 {"isnan", 1, 1, f_isnan},
681#endif
682 {"items", 1, 1, f_items},
683#ifdef FEAT_JOB_CHANNEL
684 {"job_getchannel", 1, 1, f_job_getchannel},
685 {"job_info", 1, 1, f_job_info},
686 {"job_setoptions", 2, 2, f_job_setoptions},
687 {"job_start", 1, 2, f_job_start},
688 {"job_status", 1, 1, f_job_status},
689 {"job_stop", 1, 2, f_job_stop},
690#endif
691 {"join", 1, 2, f_join},
692 {"js_decode", 1, 1, f_js_decode},
693 {"js_encode", 1, 1, f_js_encode},
694 {"json_decode", 1, 1, f_json_decode},
695 {"json_encode", 1, 1, f_json_encode},
696 {"keys", 1, 1, f_keys},
697 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
698 {"len", 1, 1, f_len},
699 {"libcall", 3, 3, f_libcall},
700 {"libcallnr", 3, 3, f_libcallnr},
701 {"line", 1, 1, f_line},
702 {"line2byte", 1, 1, f_line2byte},
703 {"lispindent", 1, 1, f_lispindent},
704 {"localtime", 0, 0, f_localtime},
705#ifdef FEAT_FLOAT
706 {"log", 1, 1, f_log},
707 {"log10", 1, 1, f_log10},
708#endif
709#ifdef FEAT_LUA
710 {"luaeval", 1, 2, f_luaeval},
711#endif
712 {"map", 2, 2, f_map},
713 {"maparg", 1, 4, f_maparg},
714 {"mapcheck", 1, 3, f_mapcheck},
715 {"match", 2, 4, f_match},
716 {"matchadd", 2, 5, f_matchadd},
717 {"matchaddpos", 2, 5, f_matchaddpos},
718 {"matcharg", 1, 1, f_matcharg},
719 {"matchdelete", 1, 1, f_matchdelete},
720 {"matchend", 2, 4, f_matchend},
721 {"matchlist", 2, 4, f_matchlist},
722 {"matchstr", 2, 4, f_matchstr},
723 {"matchstrpos", 2, 4, f_matchstrpos},
724 {"max", 1, 1, f_max},
725 {"min", 1, 1, f_min},
726#ifdef vim_mkdir
727 {"mkdir", 1, 3, f_mkdir},
728#endif
729 {"mode", 0, 1, f_mode},
730#ifdef FEAT_MZSCHEME
731 {"mzeval", 1, 1, f_mzeval},
732#endif
733 {"nextnonblank", 1, 1, f_nextnonblank},
734 {"nr2char", 1, 2, f_nr2char},
735 {"or", 2, 2, f_or},
736 {"pathshorten", 1, 1, f_pathshorten},
737#ifdef FEAT_PERL
738 {"perleval", 1, 1, f_perleval},
739#endif
740#ifdef FEAT_FLOAT
741 {"pow", 2, 2, f_pow},
742#endif
743 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100744 {"printf", 1, 19, f_printf},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200745 {"pumvisible", 0, 0, f_pumvisible},
746#ifdef FEAT_PYTHON3
747 {"py3eval", 1, 1, f_py3eval},
748#endif
749#ifdef FEAT_PYTHON
750 {"pyeval", 1, 1, f_pyeval},
751#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100752#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
753 {"pyxeval", 1, 1, f_pyxeval},
754#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200755 {"range", 1, 3, f_range},
756 {"readfile", 1, 3, f_readfile},
757 {"reltime", 0, 2, f_reltime},
758#ifdef FEAT_FLOAT
759 {"reltimefloat", 1, 1, f_reltimefloat},
760#endif
761 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100762 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200763 {"remote_foreground", 1, 1, f_remote_foreground},
764 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100765 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200766 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100767 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200768 {"remove", 2, 3, f_remove},
769 {"rename", 2, 2, f_rename},
770 {"repeat", 2, 2, f_repeat},
771 {"resolve", 1, 1, f_resolve},
772 {"reverse", 1, 1, f_reverse},
773#ifdef FEAT_FLOAT
774 {"round", 1, 1, f_round},
775#endif
776 {"screenattr", 2, 2, f_screenattr},
777 {"screenchar", 2, 2, f_screenchar},
778 {"screencol", 0, 0, f_screencol},
779 {"screenrow", 0, 0, f_screenrow},
780 {"search", 1, 4, f_search},
781 {"searchdecl", 1, 3, f_searchdecl},
782 {"searchpair", 3, 7, f_searchpair},
783 {"searchpairpos", 3, 7, f_searchpairpos},
784 {"searchpos", 1, 4, f_searchpos},
785 {"server2client", 2, 2, f_server2client},
786 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200787 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200788 {"setbufvar", 3, 3, f_setbufvar},
789 {"setcharsearch", 1, 1, f_setcharsearch},
790 {"setcmdpos", 1, 1, f_setcmdpos},
791 {"setfperm", 2, 2, f_setfperm},
792 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200793 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200794 {"setmatches", 1, 1, f_setmatches},
795 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200796 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200797 {"setreg", 2, 3, f_setreg},
798 {"settabvar", 3, 3, f_settabvar},
799 {"settabwinvar", 4, 4, f_settabwinvar},
800 {"setwinvar", 3, 3, f_setwinvar},
801#ifdef FEAT_CRYPT
802 {"sha256", 1, 1, f_sha256},
803#endif
804 {"shellescape", 1, 2, f_shellescape},
805 {"shiftwidth", 0, 0, f_shiftwidth},
806 {"simplify", 1, 1, f_simplify},
807#ifdef FEAT_FLOAT
808 {"sin", 1, 1, f_sin},
809 {"sinh", 1, 1, f_sinh},
810#endif
811 {"sort", 1, 3, f_sort},
812 {"soundfold", 1, 1, f_soundfold},
813 {"spellbadword", 0, 1, f_spellbadword},
814 {"spellsuggest", 1, 3, f_spellsuggest},
815 {"split", 1, 3, f_split},
816#ifdef FEAT_FLOAT
817 {"sqrt", 1, 1, f_sqrt},
818 {"str2float", 1, 1, f_str2float},
819#endif
820 {"str2nr", 1, 2, f_str2nr},
821 {"strcharpart", 2, 3, f_strcharpart},
822 {"strchars", 1, 2, f_strchars},
823 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
824#ifdef HAVE_STRFTIME
825 {"strftime", 1, 2, f_strftime},
826#endif
827 {"strgetchar", 2, 2, f_strgetchar},
828 {"stridx", 2, 3, f_stridx},
829 {"string", 1, 1, f_string},
830 {"strlen", 1, 1, f_strlen},
831 {"strpart", 2, 3, f_strpart},
832 {"strridx", 2, 3, f_strridx},
833 {"strtrans", 1, 1, f_strtrans},
834 {"strwidth", 1, 1, f_strwidth},
835 {"submatch", 1, 2, f_submatch},
836 {"substitute", 4, 4, f_substitute},
837 {"synID", 3, 3, f_synID},
838 {"synIDattr", 2, 3, f_synIDattr},
839 {"synIDtrans", 1, 1, f_synIDtrans},
840 {"synconcealed", 2, 2, f_synconcealed},
841 {"synstack", 2, 2, f_synstack},
842 {"system", 1, 2, f_system},
843 {"systemlist", 1, 2, f_systemlist},
844 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
845 {"tabpagenr", 0, 1, f_tabpagenr},
846 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
847 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100848 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200849#ifdef FEAT_FLOAT
850 {"tan", 1, 1, f_tan},
851 {"tanh", 1, 1, f_tanh},
852#endif
853 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200854#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100855 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
856 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100857 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200858 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200859 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200860 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200861 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200862 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200863 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200864 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200865 {"term_getstatus", 1, 1, f_term_getstatus},
866 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200867 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200868 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200869 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200870 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100871 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100872 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200873 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200874 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200875#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200876 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
877 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200878 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200879 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100880 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200881#ifdef FEAT_JOB_CHANNEL
882 {"test_null_channel", 0, 0, f_test_null_channel},
883#endif
884 {"test_null_dict", 0, 0, f_test_null_dict},
885#ifdef FEAT_JOB_CHANNEL
886 {"test_null_job", 0, 0, f_test_null_job},
887#endif
888 {"test_null_list", 0, 0, f_test_null_list},
889 {"test_null_partial", 0, 0, f_test_null_partial},
890 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100891 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200892 {"test_settime", 1, 1, f_test_settime},
893#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200894 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200895 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200896 {"timer_start", 2, 3, f_timer_start},
897 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200898 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200899#endif
900 {"tolower", 1, 1, f_tolower},
901 {"toupper", 1, 1, f_toupper},
902 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100903 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200904#ifdef FEAT_FLOAT
905 {"trunc", 1, 1, f_trunc},
906#endif
907 {"type", 1, 1, f_type},
908 {"undofile", 1, 1, f_undofile},
909 {"undotree", 0, 0, f_undotree},
910 {"uniq", 1, 3, f_uniq},
911 {"values", 1, 1, f_values},
912 {"virtcol", 1, 1, f_virtcol},
913 {"visualmode", 0, 1, f_visualmode},
914 {"wildmenumode", 0, 0, f_wildmenumode},
915 {"win_findbuf", 1, 1, f_win_findbuf},
916 {"win_getid", 0, 2, f_win_getid},
917 {"win_gotoid", 1, 1, f_win_gotoid},
918 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
919 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100920 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200921 {"winbufnr", 1, 1, f_winbufnr},
922 {"wincol", 0, 0, f_wincol},
923 {"winheight", 1, 1, f_winheight},
924 {"winline", 0, 0, f_winline},
925 {"winnr", 0, 1, f_winnr},
926 {"winrestcmd", 0, 0, f_winrestcmd},
927 {"winrestview", 1, 1, f_winrestview},
928 {"winsaveview", 0, 0, f_winsaveview},
929 {"winwidth", 1, 1, f_winwidth},
930 {"wordcount", 0, 0, f_wordcount},
931 {"writefile", 2, 3, f_writefile},
932 {"xor", 2, 2, f_xor},
933};
934
935#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
936
937/*
938 * Function given to ExpandGeneric() to obtain the list of internal
939 * or user defined function names.
940 */
941 char_u *
942get_function_name(expand_T *xp, int idx)
943{
944 static int intidx = -1;
945 char_u *name;
946
947 if (idx == 0)
948 intidx = -1;
949 if (intidx < 0)
950 {
951 name = get_user_func_name(xp, idx);
952 if (name != NULL)
953 return name;
954 }
955 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
956 {
957 STRCPY(IObuff, functions[intidx].f_name);
958 STRCAT(IObuff, "(");
959 if (functions[intidx].f_max_argc == 0)
960 STRCAT(IObuff, ")");
961 return IObuff;
962 }
963
964 return NULL;
965}
966
967/*
968 * Function given to ExpandGeneric() to obtain the list of internal or
969 * user defined variable or function names.
970 */
971 char_u *
972get_expr_name(expand_T *xp, int idx)
973{
974 static int intidx = -1;
975 char_u *name;
976
977 if (idx == 0)
978 intidx = -1;
979 if (intidx < 0)
980 {
981 name = get_function_name(xp, idx);
982 if (name != NULL)
983 return name;
984 }
985 return get_user_var_name(xp, ++intidx);
986}
987
988#endif /* FEAT_CMDL_COMPL */
989
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200990/*
991 * Find internal function in table above.
992 * Return index, or -1 if not found
993 */
994 int
995find_internal_func(
996 char_u *name) /* name of the function */
997{
998 int first = 0;
999 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1000 int cmp;
1001 int x;
1002
1003 /*
1004 * Find the function name in the table. Binary search.
1005 */
1006 while (first <= last)
1007 {
1008 x = first + ((unsigned)(last - first) >> 1);
1009 cmp = STRCMP(name, functions[x].f_name);
1010 if (cmp < 0)
1011 last = x - 1;
1012 else if (cmp > 0)
1013 first = x + 1;
1014 else
1015 return x;
1016 }
1017 return -1;
1018}
1019
1020 int
1021call_internal_func(
1022 char_u *name,
1023 int argcount,
1024 typval_T *argvars,
1025 typval_T *rettv)
1026{
1027 int i;
1028
1029 i = find_internal_func(name);
1030 if (i < 0)
1031 return ERROR_UNKNOWN;
1032 if (argcount < functions[i].f_min_argc)
1033 return ERROR_TOOFEW;
1034 if (argcount > functions[i].f_max_argc)
1035 return ERROR_TOOMANY;
1036 argvars[argcount].v_type = VAR_UNKNOWN;
1037 functions[i].f_func(argvars, rettv);
1038 return ERROR_NONE;
1039}
1040
1041/*
1042 * Return TRUE for a non-zero Number and a non-empty String.
1043 */
1044 static int
1045non_zero_arg(typval_T *argvars)
1046{
1047 return ((argvars[0].v_type == VAR_NUMBER
1048 && argvars[0].vval.v_number != 0)
1049 || (argvars[0].v_type == VAR_SPECIAL
1050 && argvars[0].vval.v_number == VVAL_TRUE)
1051 || (argvars[0].v_type == VAR_STRING
1052 && argvars[0].vval.v_string != NULL
1053 && *argvars[0].vval.v_string != NUL));
1054}
1055
1056/*
1057 * Get the lnum from the first argument.
1058 * Also accepts ".", "$", etc., but that only works for the current buffer.
1059 * Returns -1 on error.
1060 */
1061 static linenr_T
1062get_tv_lnum(typval_T *argvars)
1063{
1064 typval_T rettv;
1065 linenr_T lnum;
1066
1067 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1068 if (lnum == 0) /* no valid number, try using line() */
1069 {
1070 rettv.v_type = VAR_NUMBER;
1071 f_line(argvars, &rettv);
1072 lnum = (linenr_T)rettv.vval.v_number;
1073 clear_tv(&rettv);
1074 }
1075 return lnum;
1076}
1077
1078#ifdef FEAT_FLOAT
1079static int get_float_arg(typval_T *argvars, float_T *f);
1080
1081/*
1082 * Get the float value of "argvars[0]" into "f".
1083 * Returns FAIL when the argument is not a Number or Float.
1084 */
1085 static int
1086get_float_arg(typval_T *argvars, float_T *f)
1087{
1088 if (argvars[0].v_type == VAR_FLOAT)
1089 {
1090 *f = argvars[0].vval.v_float;
1091 return OK;
1092 }
1093 if (argvars[0].v_type == VAR_NUMBER)
1094 {
1095 *f = (float_T)argvars[0].vval.v_number;
1096 return OK;
1097 }
1098 EMSG(_("E808: Number or Float required"));
1099 return FAIL;
1100}
1101
1102/*
1103 * "abs(expr)" function
1104 */
1105 static void
1106f_abs(typval_T *argvars, typval_T *rettv)
1107{
1108 if (argvars[0].v_type == VAR_FLOAT)
1109 {
1110 rettv->v_type = VAR_FLOAT;
1111 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1112 }
1113 else
1114 {
1115 varnumber_T n;
1116 int error = FALSE;
1117
1118 n = get_tv_number_chk(&argvars[0], &error);
1119 if (error)
1120 rettv->vval.v_number = -1;
1121 else if (n > 0)
1122 rettv->vval.v_number = n;
1123 else
1124 rettv->vval.v_number = -n;
1125 }
1126}
1127
1128/*
1129 * "acos()" function
1130 */
1131 static void
1132f_acos(typval_T *argvars, typval_T *rettv)
1133{
1134 float_T f = 0.0;
1135
1136 rettv->v_type = VAR_FLOAT;
1137 if (get_float_arg(argvars, &f) == OK)
1138 rettv->vval.v_float = acos(f);
1139 else
1140 rettv->vval.v_float = 0.0;
1141}
1142#endif
1143
1144/*
1145 * "add(list, item)" function
1146 */
1147 static void
1148f_add(typval_T *argvars, typval_T *rettv)
1149{
1150 list_T *l;
1151
1152 rettv->vval.v_number = 1; /* Default: Failed */
1153 if (argvars[0].v_type == VAR_LIST)
1154 {
1155 if ((l = argvars[0].vval.v_list) != NULL
1156 && !tv_check_lock(l->lv_lock,
1157 (char_u *)N_("add() argument"), TRUE)
1158 && list_append_tv(l, &argvars[1]) == OK)
1159 copy_tv(&argvars[0], rettv);
1160 }
1161 else
1162 EMSG(_(e_listreq));
1163}
1164
1165/*
1166 * "and(expr, expr)" function
1167 */
1168 static void
1169f_and(typval_T *argvars, typval_T *rettv)
1170{
1171 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1172 & get_tv_number_chk(&argvars[1], NULL);
1173}
1174
1175/*
1176 * "append(lnum, string/list)" function
1177 */
1178 static void
1179f_append(typval_T *argvars, typval_T *rettv)
1180{
1181 long lnum;
1182 char_u *line;
1183 list_T *l = NULL;
1184 listitem_T *li = NULL;
1185 typval_T *tv;
1186 long added = 0;
1187
1188 /* When coming here from Insert mode, sync undo, so that this can be
1189 * undone separately from what was previously inserted. */
1190 if (u_sync_once == 2)
1191 {
1192 u_sync_once = 1; /* notify that u_sync() was called */
1193 u_sync(TRUE);
1194 }
1195
1196 lnum = get_tv_lnum(argvars);
1197 if (lnum >= 0
1198 && lnum <= curbuf->b_ml.ml_line_count
1199 && u_save(lnum, lnum + 1) == OK)
1200 {
1201 if (argvars[1].v_type == VAR_LIST)
1202 {
1203 l = argvars[1].vval.v_list;
1204 if (l == NULL)
1205 return;
1206 li = l->lv_first;
1207 }
1208 for (;;)
1209 {
1210 if (l == NULL)
1211 tv = &argvars[1]; /* append a string */
1212 else if (li == NULL)
1213 break; /* end of list */
1214 else
1215 tv = &li->li_tv; /* append item from list */
1216 line = get_tv_string_chk(tv);
1217 if (line == NULL) /* type error */
1218 {
1219 rettv->vval.v_number = 1; /* Failed */
1220 break;
1221 }
1222 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1223 ++added;
1224 if (l == NULL)
1225 break;
1226 li = li->li_next;
1227 }
1228
1229 appended_lines_mark(lnum, added);
1230 if (curwin->w_cursor.lnum > lnum)
1231 curwin->w_cursor.lnum += added;
1232 }
1233 else
1234 rettv->vval.v_number = 1; /* Failed */
1235}
1236
1237/*
1238 * "argc()" function
1239 */
1240 static void
1241f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1242{
1243 rettv->vval.v_number = ARGCOUNT;
1244}
1245
1246/*
1247 * "argidx()" function
1248 */
1249 static void
1250f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1251{
1252 rettv->vval.v_number = curwin->w_arg_idx;
1253}
1254
1255/*
1256 * "arglistid()" function
1257 */
1258 static void
1259f_arglistid(typval_T *argvars, typval_T *rettv)
1260{
1261 win_T *wp;
1262
1263 rettv->vval.v_number = -1;
1264 wp = find_tabwin(&argvars[0], &argvars[1]);
1265 if (wp != NULL)
1266 rettv->vval.v_number = wp->w_alist->id;
1267}
1268
1269/*
1270 * "argv(nr)" function
1271 */
1272 static void
1273f_argv(typval_T *argvars, typval_T *rettv)
1274{
1275 int idx;
1276
1277 if (argvars[0].v_type != VAR_UNKNOWN)
1278 {
1279 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1280 if (idx >= 0 && idx < ARGCOUNT)
1281 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1282 else
1283 rettv->vval.v_string = NULL;
1284 rettv->v_type = VAR_STRING;
1285 }
1286 else if (rettv_list_alloc(rettv) == OK)
1287 for (idx = 0; idx < ARGCOUNT; ++idx)
1288 list_append_string(rettv->vval.v_list,
1289 alist_name(&ARGLIST[idx]), -1);
1290}
1291
1292/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001293 * "assert_beeps(cmd [, error])" function
1294 */
1295 static void
1296f_assert_beeps(typval_T *argvars, typval_T *rettv UNUSED)
1297{
1298 assert_beeps(argvars);
1299}
1300
1301/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001302 * "assert_equal(expected, actual[, msg])" function
1303 */
1304 static void
1305f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1306{
1307 assert_equal_common(argvars, ASSERT_EQUAL);
1308}
1309
1310/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001311 * "assert_equalfile(fname-one, fname-two)" function
1312 */
1313 static void
1314f_assert_equalfile(typval_T *argvars, typval_T *rettv UNUSED)
1315{
1316 assert_equalfile(argvars);
1317}
1318
1319/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001320 * "assert_notequal(expected, actual[, msg])" function
1321 */
1322 static void
1323f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1324{
1325 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1326}
1327
1328/*
1329 * "assert_exception(string[, msg])" function
1330 */
1331 static void
1332f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1333{
1334 assert_exception(argvars);
1335}
1336
1337/*
1338 * "assert_fails(cmd [, error])" function
1339 */
1340 static void
1341f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1342{
1343 assert_fails(argvars);
1344}
1345
1346/*
1347 * "assert_false(actual[, msg])" function
1348 */
1349 static void
1350f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1351{
1352 assert_bool(argvars, FALSE);
1353}
1354
1355/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001356 * "assert_inrange(lower, upper[, msg])" function
1357 */
1358 static void
1359f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1360{
1361 assert_inrange(argvars);
1362}
1363
1364/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001365 * "assert_match(pattern, actual[, msg])" function
1366 */
1367 static void
1368f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1369{
1370 assert_match_common(argvars, ASSERT_MATCH);
1371}
1372
1373/*
1374 * "assert_notmatch(pattern, actual[, msg])" function
1375 */
1376 static void
1377f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1378{
1379 assert_match_common(argvars, ASSERT_NOTMATCH);
1380}
1381
1382/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001383 * "assert_report(msg)" function
1384 */
1385 static void
1386f_assert_report(typval_T *argvars, typval_T *rettv UNUSED)
1387{
1388 assert_report(argvars);
1389}
1390
1391/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001392 * "assert_true(actual[, msg])" function
1393 */
1394 static void
1395f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1396{
1397 assert_bool(argvars, TRUE);
1398}
1399
1400#ifdef FEAT_FLOAT
1401/*
1402 * "asin()" function
1403 */
1404 static void
1405f_asin(typval_T *argvars, typval_T *rettv)
1406{
1407 float_T f = 0.0;
1408
1409 rettv->v_type = VAR_FLOAT;
1410 if (get_float_arg(argvars, &f) == OK)
1411 rettv->vval.v_float = asin(f);
1412 else
1413 rettv->vval.v_float = 0.0;
1414}
1415
1416/*
1417 * "atan()" function
1418 */
1419 static void
1420f_atan(typval_T *argvars, typval_T *rettv)
1421{
1422 float_T f = 0.0;
1423
1424 rettv->v_type = VAR_FLOAT;
1425 if (get_float_arg(argvars, &f) == OK)
1426 rettv->vval.v_float = atan(f);
1427 else
1428 rettv->vval.v_float = 0.0;
1429}
1430
1431/*
1432 * "atan2()" function
1433 */
1434 static void
1435f_atan2(typval_T *argvars, typval_T *rettv)
1436{
1437 float_T fx = 0.0, fy = 0.0;
1438
1439 rettv->v_type = VAR_FLOAT;
1440 if (get_float_arg(argvars, &fx) == OK
1441 && get_float_arg(&argvars[1], &fy) == OK)
1442 rettv->vval.v_float = atan2(fx, fy);
1443 else
1444 rettv->vval.v_float = 0.0;
1445}
1446#endif
1447
1448/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001449 * "balloon_show()" function
1450 */
1451#ifdef FEAT_BEVAL
1452 static void
1453f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1454{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001455 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001456 {
1457 if (argvars[0].v_type == VAR_LIST
1458# ifdef FEAT_GUI
1459 && !gui.in_use
1460# endif
1461 )
1462 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1463 else
1464 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1465 }
1466}
1467
Bram Moolenaar669a8282017-11-19 20:13:05 +01001468# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001469 static void
1470f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1471{
1472 if (rettv_list_alloc(rettv) == OK)
1473 {
1474 char_u *msg = get_tv_string_chk(&argvars[0]);
1475
1476 if (msg != NULL)
1477 {
1478 pumitem_T *array;
1479 int size = split_message(msg, &array);
1480 int i;
1481
1482 /* Skip the first and last item, they are always empty. */
1483 for (i = 1; i < size - 1; ++i)
1484 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001485 while (size > 0)
1486 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001487 vim_free(array);
1488 }
1489 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001490}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001491# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001492#endif
1493
1494/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001495 * "browse(save, title, initdir, default)" function
1496 */
1497 static void
1498f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1499{
1500#ifdef FEAT_BROWSE
1501 int save;
1502 char_u *title;
1503 char_u *initdir;
1504 char_u *defname;
1505 char_u buf[NUMBUFLEN];
1506 char_u buf2[NUMBUFLEN];
1507 int error = FALSE;
1508
1509 save = (int)get_tv_number_chk(&argvars[0], &error);
1510 title = get_tv_string_chk(&argvars[1]);
1511 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1512 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1513
1514 if (error || title == NULL || initdir == NULL || defname == NULL)
1515 rettv->vval.v_string = NULL;
1516 else
1517 rettv->vval.v_string =
1518 do_browse(save ? BROWSE_SAVE : 0,
1519 title, defname, NULL, initdir, NULL, curbuf);
1520#else
1521 rettv->vval.v_string = NULL;
1522#endif
1523 rettv->v_type = VAR_STRING;
1524}
1525
1526/*
1527 * "browsedir(title, initdir)" function
1528 */
1529 static void
1530f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1531{
1532#ifdef FEAT_BROWSE
1533 char_u *title;
1534 char_u *initdir;
1535 char_u buf[NUMBUFLEN];
1536
1537 title = get_tv_string_chk(&argvars[0]);
1538 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1539
1540 if (title == NULL || initdir == NULL)
1541 rettv->vval.v_string = NULL;
1542 else
1543 rettv->vval.v_string = do_browse(BROWSE_DIR,
1544 title, NULL, NULL, initdir, NULL, curbuf);
1545#else
1546 rettv->vval.v_string = NULL;
1547#endif
1548 rettv->v_type = VAR_STRING;
1549}
1550
1551static buf_T *find_buffer(typval_T *avar);
1552
1553/*
1554 * Find a buffer by number or exact name.
1555 */
1556 static buf_T *
1557find_buffer(typval_T *avar)
1558{
1559 buf_T *buf = NULL;
1560
1561 if (avar->v_type == VAR_NUMBER)
1562 buf = buflist_findnr((int)avar->vval.v_number);
1563 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1564 {
1565 buf = buflist_findname_exp(avar->vval.v_string);
1566 if (buf == NULL)
1567 {
1568 /* No full path name match, try a match with a URL or a "nofile"
1569 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001570 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001571 if (buf->b_fname != NULL
1572 && (path_with_url(buf->b_fname)
1573#ifdef FEAT_QUICKFIX
1574 || bt_nofile(buf)
1575#endif
1576 )
1577 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1578 break;
1579 }
1580 }
1581 return buf;
1582}
1583
1584/*
1585 * "bufexists(expr)" function
1586 */
1587 static void
1588f_bufexists(typval_T *argvars, typval_T *rettv)
1589{
1590 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1591}
1592
1593/*
1594 * "buflisted(expr)" function
1595 */
1596 static void
1597f_buflisted(typval_T *argvars, typval_T *rettv)
1598{
1599 buf_T *buf;
1600
1601 buf = find_buffer(&argvars[0]);
1602 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1603}
1604
1605/*
1606 * "bufloaded(expr)" function
1607 */
1608 static void
1609f_bufloaded(typval_T *argvars, typval_T *rettv)
1610{
1611 buf_T *buf;
1612
1613 buf = find_buffer(&argvars[0]);
1614 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1615}
1616
1617 buf_T *
1618buflist_find_by_name(char_u *name, int curtab_only)
1619{
1620 int save_magic;
1621 char_u *save_cpo;
1622 buf_T *buf;
1623
1624 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1625 save_magic = p_magic;
1626 p_magic = TRUE;
1627 save_cpo = p_cpo;
1628 p_cpo = (char_u *)"";
1629
1630 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1631 TRUE, FALSE, curtab_only));
1632
1633 p_magic = save_magic;
1634 p_cpo = save_cpo;
1635 return buf;
1636}
1637
1638/*
1639 * Get buffer by number or pattern.
1640 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001641 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001642get_buf_tv(typval_T *tv, int curtab_only)
1643{
1644 char_u *name = tv->vval.v_string;
1645 buf_T *buf;
1646
1647 if (tv->v_type == VAR_NUMBER)
1648 return buflist_findnr((int)tv->vval.v_number);
1649 if (tv->v_type != VAR_STRING)
1650 return NULL;
1651 if (name == NULL || *name == NUL)
1652 return curbuf;
1653 if (name[0] == '$' && name[1] == NUL)
1654 return lastbuf;
1655
1656 buf = buflist_find_by_name(name, curtab_only);
1657
1658 /* If not found, try expanding the name, like done for bufexists(). */
1659 if (buf == NULL)
1660 buf = find_buffer(tv);
1661
1662 return buf;
1663}
1664
1665/*
1666 * "bufname(expr)" function
1667 */
1668 static void
1669f_bufname(typval_T *argvars, typval_T *rettv)
1670{
1671 buf_T *buf;
1672
1673 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1674 ++emsg_off;
1675 buf = get_buf_tv(&argvars[0], FALSE);
1676 rettv->v_type = VAR_STRING;
1677 if (buf != NULL && buf->b_fname != NULL)
1678 rettv->vval.v_string = vim_strsave(buf->b_fname);
1679 else
1680 rettv->vval.v_string = NULL;
1681 --emsg_off;
1682}
1683
1684/*
1685 * "bufnr(expr)" function
1686 */
1687 static void
1688f_bufnr(typval_T *argvars, typval_T *rettv)
1689{
1690 buf_T *buf;
1691 int error = FALSE;
1692 char_u *name;
1693
1694 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1695 ++emsg_off;
1696 buf = get_buf_tv(&argvars[0], FALSE);
1697 --emsg_off;
1698
1699 /* If the buffer isn't found and the second argument is not zero create a
1700 * new buffer. */
1701 if (buf == NULL
1702 && argvars[1].v_type != VAR_UNKNOWN
1703 && get_tv_number_chk(&argvars[1], &error) != 0
1704 && !error
1705 && (name = get_tv_string_chk(&argvars[0])) != NULL
1706 && !error)
1707 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1708
1709 if (buf != NULL)
1710 rettv->vval.v_number = buf->b_fnum;
1711 else
1712 rettv->vval.v_number = -1;
1713}
1714
1715 static void
1716buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1717{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001718 win_T *wp;
1719 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001720 buf_T *buf;
1721
1722 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1723 ++emsg_off;
1724 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001725 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001726 {
1727 ++winnr;
1728 if (wp->w_buffer == buf)
1729 break;
1730 }
1731 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001732 --emsg_off;
1733}
1734
1735/*
1736 * "bufwinid(nr)" function
1737 */
1738 static void
1739f_bufwinid(typval_T *argvars, typval_T *rettv)
1740{
1741 buf_win_common(argvars, rettv, FALSE);
1742}
1743
1744/*
1745 * "bufwinnr(nr)" function
1746 */
1747 static void
1748f_bufwinnr(typval_T *argvars, typval_T *rettv)
1749{
1750 buf_win_common(argvars, rettv, TRUE);
1751}
1752
1753/*
1754 * "byte2line(byte)" function
1755 */
1756 static void
1757f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1758{
1759#ifndef FEAT_BYTEOFF
1760 rettv->vval.v_number = -1;
1761#else
1762 long boff = 0;
1763
1764 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1765 if (boff < 0)
1766 rettv->vval.v_number = -1;
1767 else
1768 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1769 (linenr_T)0, &boff);
1770#endif
1771}
1772
1773 static void
1774byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1775{
1776#ifdef FEAT_MBYTE
1777 char_u *t;
1778#endif
1779 char_u *str;
1780 varnumber_T idx;
1781
1782 str = get_tv_string_chk(&argvars[0]);
1783 idx = get_tv_number_chk(&argvars[1], NULL);
1784 rettv->vval.v_number = -1;
1785 if (str == NULL || idx < 0)
1786 return;
1787
1788#ifdef FEAT_MBYTE
1789 t = str;
1790 for ( ; idx > 0; idx--)
1791 {
1792 if (*t == NUL) /* EOL reached */
1793 return;
1794 if (enc_utf8 && comp)
1795 t += utf_ptr2len(t);
1796 else
1797 t += (*mb_ptr2len)(t);
1798 }
1799 rettv->vval.v_number = (varnumber_T)(t - str);
1800#else
1801 if ((size_t)idx <= STRLEN(str))
1802 rettv->vval.v_number = idx;
1803#endif
1804}
1805
1806/*
1807 * "byteidx()" function
1808 */
1809 static void
1810f_byteidx(typval_T *argvars, typval_T *rettv)
1811{
1812 byteidx(argvars, rettv, FALSE);
1813}
1814
1815/*
1816 * "byteidxcomp()" function
1817 */
1818 static void
1819f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1820{
1821 byteidx(argvars, rettv, TRUE);
1822}
1823
1824/*
1825 * "call(func, arglist [, dict])" function
1826 */
1827 static void
1828f_call(typval_T *argvars, typval_T *rettv)
1829{
1830 char_u *func;
1831 partial_T *partial = NULL;
1832 dict_T *selfdict = NULL;
1833
1834 if (argvars[1].v_type != VAR_LIST)
1835 {
1836 EMSG(_(e_listreq));
1837 return;
1838 }
1839 if (argvars[1].vval.v_list == NULL)
1840 return;
1841
1842 if (argvars[0].v_type == VAR_FUNC)
1843 func = argvars[0].vval.v_string;
1844 else if (argvars[0].v_type == VAR_PARTIAL)
1845 {
1846 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001847 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001848 }
1849 else
1850 func = get_tv_string(&argvars[0]);
1851 if (*func == NUL)
1852 return; /* type error or empty name */
1853
1854 if (argvars[2].v_type != VAR_UNKNOWN)
1855 {
1856 if (argvars[2].v_type != VAR_DICT)
1857 {
1858 EMSG(_(e_dictreq));
1859 return;
1860 }
1861 selfdict = argvars[2].vval.v_dict;
1862 }
1863
1864 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1865}
1866
1867#ifdef FEAT_FLOAT
1868/*
1869 * "ceil({float})" function
1870 */
1871 static void
1872f_ceil(typval_T *argvars, typval_T *rettv)
1873{
1874 float_T f = 0.0;
1875
1876 rettv->v_type = VAR_FLOAT;
1877 if (get_float_arg(argvars, &f) == OK)
1878 rettv->vval.v_float = ceil(f);
1879 else
1880 rettv->vval.v_float = 0.0;
1881}
1882#endif
1883
1884#ifdef FEAT_JOB_CHANNEL
1885/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001886 * "ch_canread()" function
1887 */
1888 static void
1889f_ch_canread(typval_T *argvars, typval_T *rettv)
1890{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001891 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001892
1893 rettv->vval.v_number = 0;
1894 if (channel != NULL)
1895 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1896 || channel_has_readahead(channel, PART_OUT)
1897 || channel_has_readahead(channel, PART_ERR);
1898}
1899
1900/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001901 * "ch_close()" function
1902 */
1903 static void
1904f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1905{
1906 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1907
1908 if (channel != NULL)
1909 {
1910 channel_close(channel, FALSE);
1911 channel_clear(channel);
1912 }
1913}
1914
1915/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001916 * "ch_close()" function
1917 */
1918 static void
1919f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1920{
1921 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1922
1923 if (channel != NULL)
1924 channel_close_in(channel);
1925}
1926
1927/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001928 * "ch_getbufnr()" function
1929 */
1930 static void
1931f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1932{
1933 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1934
1935 rettv->vval.v_number = -1;
1936 if (channel != NULL)
1937 {
1938 char_u *what = get_tv_string(&argvars[1]);
1939 int part;
1940
1941 if (STRCMP(what, "err") == 0)
1942 part = PART_ERR;
1943 else if (STRCMP(what, "out") == 0)
1944 part = PART_OUT;
1945 else if (STRCMP(what, "in") == 0)
1946 part = PART_IN;
1947 else
1948 part = PART_SOCK;
1949 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1950 rettv->vval.v_number =
1951 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1952 }
1953}
1954
1955/*
1956 * "ch_getjob()" function
1957 */
1958 static void
1959f_ch_getjob(typval_T *argvars, typval_T *rettv)
1960{
1961 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1962
1963 if (channel != NULL)
1964 {
1965 rettv->v_type = VAR_JOB;
1966 rettv->vval.v_job = channel->ch_job;
1967 if (channel->ch_job != NULL)
1968 ++channel->ch_job->jv_refcount;
1969 }
1970}
1971
1972/*
1973 * "ch_info()" function
1974 */
1975 static void
1976f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1977{
1978 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1979
1980 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1981 channel_info(channel, rettv->vval.v_dict);
1982}
1983
1984/*
1985 * "ch_log()" function
1986 */
1987 static void
1988f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1989{
1990 char_u *msg = get_tv_string(&argvars[0]);
1991 channel_T *channel = NULL;
1992
1993 if (argvars[1].v_type != VAR_UNKNOWN)
1994 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1995
Bram Moolenaard5359b22018-04-05 22:44:39 +02001996 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001997}
1998
1999/*
2000 * "ch_logfile()" function
2001 */
2002 static void
2003f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2004{
2005 char_u *fname;
2006 char_u *opt = (char_u *)"";
2007 char_u buf[NUMBUFLEN];
2008
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002009 /* Don't open a file in restricted mode. */
2010 if (check_restricted() || check_secure())
2011 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002012 fname = get_tv_string(&argvars[0]);
2013 if (argvars[1].v_type == VAR_STRING)
2014 opt = get_tv_string_buf(&argvars[1], buf);
2015 ch_logfile(fname, opt);
2016}
2017
2018/*
2019 * "ch_open()" function
2020 */
2021 static void
2022f_ch_open(typval_T *argvars, typval_T *rettv)
2023{
2024 rettv->v_type = VAR_CHANNEL;
2025 if (check_restricted() || check_secure())
2026 return;
2027 rettv->vval.v_channel = channel_open_func(argvars);
2028}
2029
2030/*
2031 * "ch_read()" function
2032 */
2033 static void
2034f_ch_read(typval_T *argvars, typval_T *rettv)
2035{
2036 common_channel_read(argvars, rettv, FALSE);
2037}
2038
2039/*
2040 * "ch_readraw()" function
2041 */
2042 static void
2043f_ch_readraw(typval_T *argvars, typval_T *rettv)
2044{
2045 common_channel_read(argvars, rettv, TRUE);
2046}
2047
2048/*
2049 * "ch_evalexpr()" function
2050 */
2051 static void
2052f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2053{
2054 ch_expr_common(argvars, rettv, TRUE);
2055}
2056
2057/*
2058 * "ch_sendexpr()" function
2059 */
2060 static void
2061f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2062{
2063 ch_expr_common(argvars, rettv, FALSE);
2064}
2065
2066/*
2067 * "ch_evalraw()" function
2068 */
2069 static void
2070f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2071{
2072 ch_raw_common(argvars, rettv, TRUE);
2073}
2074
2075/*
2076 * "ch_sendraw()" function
2077 */
2078 static void
2079f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2080{
2081 ch_raw_common(argvars, rettv, FALSE);
2082}
2083
2084/*
2085 * "ch_setoptions()" function
2086 */
2087 static void
2088f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2089{
2090 channel_T *channel;
2091 jobopt_T opt;
2092
2093 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2094 if (channel == NULL)
2095 return;
2096 clear_job_options(&opt);
2097 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002098 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002099 channel_set_options(channel, &opt);
2100 free_job_options(&opt);
2101}
2102
2103/*
2104 * "ch_status()" function
2105 */
2106 static void
2107f_ch_status(typval_T *argvars, typval_T *rettv)
2108{
2109 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002110 jobopt_T opt;
2111 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002112
2113 /* return an empty string by default */
2114 rettv->v_type = VAR_STRING;
2115 rettv->vval.v_string = NULL;
2116
2117 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002118
2119 if (argvars[1].v_type != VAR_UNKNOWN)
2120 {
2121 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002122 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002123 && (opt.jo_set & JO_PART))
2124 part = opt.jo_part;
2125 }
2126
2127 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002128}
2129#endif
2130
2131/*
2132 * "changenr()" function
2133 */
2134 static void
2135f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2136{
2137 rettv->vval.v_number = curbuf->b_u_seq_cur;
2138}
2139
2140/*
2141 * "char2nr(string)" function
2142 */
2143 static void
2144f_char2nr(typval_T *argvars, typval_T *rettv)
2145{
2146#ifdef FEAT_MBYTE
2147 if (has_mbyte)
2148 {
2149 int utf8 = 0;
2150
2151 if (argvars[1].v_type != VAR_UNKNOWN)
2152 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2153
2154 if (utf8)
2155 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2156 else
2157 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2158 }
2159 else
2160#endif
2161 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2162}
2163
2164/*
2165 * "cindent(lnum)" function
2166 */
2167 static void
2168f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2169{
2170#ifdef FEAT_CINDENT
2171 pos_T pos;
2172 linenr_T lnum;
2173
2174 pos = curwin->w_cursor;
2175 lnum = get_tv_lnum(argvars);
2176 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2177 {
2178 curwin->w_cursor.lnum = lnum;
2179 rettv->vval.v_number = get_c_indent();
2180 curwin->w_cursor = pos;
2181 }
2182 else
2183#endif
2184 rettv->vval.v_number = -1;
2185}
2186
2187/*
2188 * "clearmatches()" function
2189 */
2190 static void
2191f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2192{
2193#ifdef FEAT_SEARCH_EXTRA
2194 clear_matches(curwin);
2195#endif
2196}
2197
2198/*
2199 * "col(string)" function
2200 */
2201 static void
2202f_col(typval_T *argvars, typval_T *rettv)
2203{
2204 colnr_T col = 0;
2205 pos_T *fp;
2206 int fnum = curbuf->b_fnum;
2207
2208 fp = var2fpos(&argvars[0], FALSE, &fnum);
2209 if (fp != NULL && fnum == curbuf->b_fnum)
2210 {
2211 if (fp->col == MAXCOL)
2212 {
2213 /* '> can be MAXCOL, get the length of the line then */
2214 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2215 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2216 else
2217 col = MAXCOL;
2218 }
2219 else
2220 {
2221 col = fp->col + 1;
2222#ifdef FEAT_VIRTUALEDIT
2223 /* col(".") when the cursor is on the NUL at the end of the line
2224 * because of "coladd" can be seen as an extra column. */
2225 if (virtual_active() && fp == &curwin->w_cursor)
2226 {
2227 char_u *p = ml_get_cursor();
2228
2229 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2230 curwin->w_virtcol - curwin->w_cursor.coladd))
2231 {
2232# ifdef FEAT_MBYTE
2233 int l;
2234
2235 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2236 col += l;
2237# else
2238 if (*p != NUL && p[1] == NUL)
2239 ++col;
2240# endif
2241 }
2242 }
2243#endif
2244 }
2245 }
2246 rettv->vval.v_number = col;
2247}
2248
2249#if defined(FEAT_INS_EXPAND)
2250/*
2251 * "complete()" function
2252 */
2253 static void
2254f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2255{
2256 int startcol;
2257
2258 if ((State & INSERT) == 0)
2259 {
2260 EMSG(_("E785: complete() can only be used in Insert mode"));
2261 return;
2262 }
2263
2264 /* Check for undo allowed here, because if something was already inserted
2265 * the line was already saved for undo and this check isn't done. */
2266 if (!undo_allowed())
2267 return;
2268
2269 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2270 {
2271 EMSG(_(e_invarg));
2272 return;
2273 }
2274
2275 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2276 if (startcol <= 0)
2277 return;
2278
2279 set_completion(startcol - 1, argvars[1].vval.v_list);
2280}
2281
2282/*
2283 * "complete_add()" function
2284 */
2285 static void
2286f_complete_add(typval_T *argvars, typval_T *rettv)
2287{
2288 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2289}
2290
2291/*
2292 * "complete_check()" function
2293 */
2294 static void
2295f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2296{
2297 int saved = RedrawingDisabled;
2298
2299 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002300 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002301 rettv->vval.v_number = compl_interrupted;
2302 RedrawingDisabled = saved;
2303}
2304#endif
2305
2306/*
2307 * "confirm(message, buttons[, default [, type]])" function
2308 */
2309 static void
2310f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2311{
2312#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2313 char_u *message;
2314 char_u *buttons = NULL;
2315 char_u buf[NUMBUFLEN];
2316 char_u buf2[NUMBUFLEN];
2317 int def = 1;
2318 int type = VIM_GENERIC;
2319 char_u *typestr;
2320 int error = FALSE;
2321
2322 message = get_tv_string_chk(&argvars[0]);
2323 if (message == NULL)
2324 error = TRUE;
2325 if (argvars[1].v_type != VAR_UNKNOWN)
2326 {
2327 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2328 if (buttons == NULL)
2329 error = TRUE;
2330 if (argvars[2].v_type != VAR_UNKNOWN)
2331 {
2332 def = (int)get_tv_number_chk(&argvars[2], &error);
2333 if (argvars[3].v_type != VAR_UNKNOWN)
2334 {
2335 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2336 if (typestr == NULL)
2337 error = TRUE;
2338 else
2339 {
2340 switch (TOUPPER_ASC(*typestr))
2341 {
2342 case 'E': type = VIM_ERROR; break;
2343 case 'Q': type = VIM_QUESTION; break;
2344 case 'I': type = VIM_INFO; break;
2345 case 'W': type = VIM_WARNING; break;
2346 case 'G': type = VIM_GENERIC; break;
2347 }
2348 }
2349 }
2350 }
2351 }
2352
2353 if (buttons == NULL || *buttons == NUL)
2354 buttons = (char_u *)_("&Ok");
2355
2356 if (!error)
2357 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2358 def, NULL, FALSE);
2359#endif
2360}
2361
2362/*
2363 * "copy()" function
2364 */
2365 static void
2366f_copy(typval_T *argvars, typval_T *rettv)
2367{
2368 item_copy(&argvars[0], rettv, FALSE, 0);
2369}
2370
2371#ifdef FEAT_FLOAT
2372/*
2373 * "cos()" function
2374 */
2375 static void
2376f_cos(typval_T *argvars, typval_T *rettv)
2377{
2378 float_T f = 0.0;
2379
2380 rettv->v_type = VAR_FLOAT;
2381 if (get_float_arg(argvars, &f) == OK)
2382 rettv->vval.v_float = cos(f);
2383 else
2384 rettv->vval.v_float = 0.0;
2385}
2386
2387/*
2388 * "cosh()" function
2389 */
2390 static void
2391f_cosh(typval_T *argvars, typval_T *rettv)
2392{
2393 float_T f = 0.0;
2394
2395 rettv->v_type = VAR_FLOAT;
2396 if (get_float_arg(argvars, &f) == OK)
2397 rettv->vval.v_float = cosh(f);
2398 else
2399 rettv->vval.v_float = 0.0;
2400}
2401#endif
2402
2403/*
2404 * "count()" function
2405 */
2406 static void
2407f_count(typval_T *argvars, typval_T *rettv)
2408{
2409 long n = 0;
2410 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002411 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002412
Bram Moolenaar9966b212017-07-28 16:46:57 +02002413 if (argvars[2].v_type != VAR_UNKNOWN)
2414 ic = (int)get_tv_number_chk(&argvars[2], &error);
2415
2416 if (argvars[0].v_type == VAR_STRING)
2417 {
2418 char_u *expr = get_tv_string_chk(&argvars[1]);
2419 char_u *p = argvars[0].vval.v_string;
2420 char_u *next;
2421
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002422 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002423 {
2424 if (ic)
2425 {
2426 size_t len = STRLEN(expr);
2427
2428 while (*p != NUL)
2429 {
2430 if (MB_STRNICMP(p, expr, len) == 0)
2431 {
2432 ++n;
2433 p += len;
2434 }
2435 else
2436 MB_PTR_ADV(p);
2437 }
2438 }
2439 else
2440 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2441 != NULL)
2442 {
2443 ++n;
2444 p = next + STRLEN(expr);
2445 }
2446 }
2447
2448 }
2449 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002450 {
2451 listitem_T *li;
2452 list_T *l;
2453 long idx;
2454
2455 if ((l = argvars[0].vval.v_list) != NULL)
2456 {
2457 li = l->lv_first;
2458 if (argvars[2].v_type != VAR_UNKNOWN)
2459 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002460 if (argvars[3].v_type != VAR_UNKNOWN)
2461 {
2462 idx = (long)get_tv_number_chk(&argvars[3], &error);
2463 if (!error)
2464 {
2465 li = list_find(l, idx);
2466 if (li == NULL)
2467 EMSGN(_(e_listidx), idx);
2468 }
2469 }
2470 if (error)
2471 li = NULL;
2472 }
2473
2474 for ( ; li != NULL; li = li->li_next)
2475 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2476 ++n;
2477 }
2478 }
2479 else if (argvars[0].v_type == VAR_DICT)
2480 {
2481 int todo;
2482 dict_T *d;
2483 hashitem_T *hi;
2484
2485 if ((d = argvars[0].vval.v_dict) != NULL)
2486 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002487 if (argvars[2].v_type != VAR_UNKNOWN)
2488 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002489 if (argvars[3].v_type != VAR_UNKNOWN)
2490 EMSG(_(e_invarg));
2491 }
2492
2493 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2494 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2495 {
2496 if (!HASHITEM_EMPTY(hi))
2497 {
2498 --todo;
2499 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2500 ++n;
2501 }
2502 }
2503 }
2504 }
2505 else
2506 EMSG2(_(e_listdictarg), "count()");
2507 rettv->vval.v_number = n;
2508}
2509
2510/*
2511 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2512 *
2513 * Checks the existence of a cscope connection.
2514 */
2515 static void
2516f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2517{
2518#ifdef FEAT_CSCOPE
2519 int num = 0;
2520 char_u *dbpath = NULL;
2521 char_u *prepend = NULL;
2522 char_u buf[NUMBUFLEN];
2523
2524 if (argvars[0].v_type != VAR_UNKNOWN
2525 && argvars[1].v_type != VAR_UNKNOWN)
2526 {
2527 num = (int)get_tv_number(&argvars[0]);
2528 dbpath = get_tv_string(&argvars[1]);
2529 if (argvars[2].v_type != VAR_UNKNOWN)
2530 prepend = get_tv_string_buf(&argvars[2], buf);
2531 }
2532
2533 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2534#endif
2535}
2536
2537/*
2538 * "cursor(lnum, col)" function, or
2539 * "cursor(list)"
2540 *
2541 * Moves the cursor to the specified line and column.
2542 * Returns 0 when the position could be set, -1 otherwise.
2543 */
2544 static void
2545f_cursor(typval_T *argvars, typval_T *rettv)
2546{
2547 long line, col;
2548#ifdef FEAT_VIRTUALEDIT
2549 long coladd = 0;
2550#endif
2551 int set_curswant = TRUE;
2552
2553 rettv->vval.v_number = -1;
2554 if (argvars[1].v_type == VAR_UNKNOWN)
2555 {
2556 pos_T pos;
2557 colnr_T curswant = -1;
2558
2559 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2560 {
2561 EMSG(_(e_invarg));
2562 return;
2563 }
2564 line = pos.lnum;
2565 col = pos.col;
2566#ifdef FEAT_VIRTUALEDIT
2567 coladd = pos.coladd;
2568#endif
2569 if (curswant >= 0)
2570 {
2571 curwin->w_curswant = curswant - 1;
2572 set_curswant = FALSE;
2573 }
2574 }
2575 else
2576 {
2577 line = get_tv_lnum(argvars);
2578 col = (long)get_tv_number_chk(&argvars[1], NULL);
2579#ifdef FEAT_VIRTUALEDIT
2580 if (argvars[2].v_type != VAR_UNKNOWN)
2581 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2582#endif
2583 }
2584 if (line < 0 || col < 0
2585#ifdef FEAT_VIRTUALEDIT
2586 || coladd < 0
2587#endif
2588 )
2589 return; /* type error; errmsg already given */
2590 if (line > 0)
2591 curwin->w_cursor.lnum = line;
2592 if (col > 0)
2593 curwin->w_cursor.col = col - 1;
2594#ifdef FEAT_VIRTUALEDIT
2595 curwin->w_cursor.coladd = coladd;
2596#endif
2597
2598 /* Make sure the cursor is in a valid position. */
2599 check_cursor();
2600#ifdef FEAT_MBYTE
2601 /* Correct cursor for multi-byte character. */
2602 if (has_mbyte)
2603 mb_adjust_cursor();
2604#endif
2605
2606 curwin->w_set_curswant = set_curswant;
2607 rettv->vval.v_number = 0;
2608}
2609
2610/*
2611 * "deepcopy()" function
2612 */
2613 static void
2614f_deepcopy(typval_T *argvars, typval_T *rettv)
2615{
2616 int noref = 0;
2617 int copyID;
2618
2619 if (argvars[1].v_type != VAR_UNKNOWN)
2620 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2621 if (noref < 0 || noref > 1)
2622 EMSG(_(e_invarg));
2623 else
2624 {
2625 copyID = get_copyID();
2626 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2627 }
2628}
2629
2630/*
2631 * "delete()" function
2632 */
2633 static void
2634f_delete(typval_T *argvars, typval_T *rettv)
2635{
2636 char_u nbuf[NUMBUFLEN];
2637 char_u *name;
2638 char_u *flags;
2639
2640 rettv->vval.v_number = -1;
2641 if (check_restricted() || check_secure())
2642 return;
2643
2644 name = get_tv_string(&argvars[0]);
2645 if (name == NULL || *name == NUL)
2646 {
2647 EMSG(_(e_invarg));
2648 return;
2649 }
2650
2651 if (argvars[1].v_type != VAR_UNKNOWN)
2652 flags = get_tv_string_buf(&argvars[1], nbuf);
2653 else
2654 flags = (char_u *)"";
2655
2656 if (*flags == NUL)
2657 /* delete a file */
2658 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2659 else if (STRCMP(flags, "d") == 0)
2660 /* delete an empty directory */
2661 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2662 else if (STRCMP(flags, "rf") == 0)
2663 /* delete a directory recursively */
2664 rettv->vval.v_number = delete_recursive(name);
2665 else
2666 EMSG2(_(e_invexpr2), flags);
2667}
2668
2669/*
2670 * "did_filetype()" function
2671 */
2672 static void
2673f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2674{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002675 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002676}
2677
2678/*
2679 * "diff_filler()" function
2680 */
2681 static void
2682f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2683{
2684#ifdef FEAT_DIFF
2685 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2686#endif
2687}
2688
2689/*
2690 * "diff_hlID()" function
2691 */
2692 static void
2693f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2694{
2695#ifdef FEAT_DIFF
2696 linenr_T lnum = get_tv_lnum(argvars);
2697 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002698 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002699 static int fnum = 0;
2700 static int change_start = 0;
2701 static int change_end = 0;
2702 static hlf_T hlID = (hlf_T)0;
2703 int filler_lines;
2704 int col;
2705
2706 if (lnum < 0) /* ignore type error in {lnum} arg */
2707 lnum = 0;
2708 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002709 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002710 || fnum != curbuf->b_fnum)
2711 {
2712 /* New line, buffer, change: need to get the values. */
2713 filler_lines = diff_check(curwin, lnum);
2714 if (filler_lines < 0)
2715 {
2716 if (filler_lines == -1)
2717 {
2718 change_start = MAXCOL;
2719 change_end = -1;
2720 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2721 hlID = HLF_ADD; /* added line */
2722 else
2723 hlID = HLF_CHD; /* changed line */
2724 }
2725 else
2726 hlID = HLF_ADD; /* added line */
2727 }
2728 else
2729 hlID = (hlf_T)0;
2730 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002731 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002732 fnum = curbuf->b_fnum;
2733 }
2734
2735 if (hlID == HLF_CHD || hlID == HLF_TXD)
2736 {
2737 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2738 if (col >= change_start && col <= change_end)
2739 hlID = HLF_TXD; /* changed text */
2740 else
2741 hlID = HLF_CHD; /* changed line */
2742 }
2743 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2744#endif
2745}
2746
2747/*
2748 * "empty({expr})" function
2749 */
2750 static void
2751f_empty(typval_T *argvars, typval_T *rettv)
2752{
2753 int n = FALSE;
2754
2755 switch (argvars[0].v_type)
2756 {
2757 case VAR_STRING:
2758 case VAR_FUNC:
2759 n = argvars[0].vval.v_string == NULL
2760 || *argvars[0].vval.v_string == NUL;
2761 break;
2762 case VAR_PARTIAL:
2763 n = FALSE;
2764 break;
2765 case VAR_NUMBER:
2766 n = argvars[0].vval.v_number == 0;
2767 break;
2768 case VAR_FLOAT:
2769#ifdef FEAT_FLOAT
2770 n = argvars[0].vval.v_float == 0.0;
2771 break;
2772#endif
2773 case VAR_LIST:
2774 n = argvars[0].vval.v_list == NULL
2775 || argvars[0].vval.v_list->lv_first == NULL;
2776 break;
2777 case VAR_DICT:
2778 n = argvars[0].vval.v_dict == NULL
2779 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2780 break;
2781 case VAR_SPECIAL:
2782 n = argvars[0].vval.v_number != VVAL_TRUE;
2783 break;
2784
2785 case VAR_JOB:
2786#ifdef FEAT_JOB_CHANNEL
2787 n = argvars[0].vval.v_job == NULL
2788 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2789 break;
2790#endif
2791 case VAR_CHANNEL:
2792#ifdef FEAT_JOB_CHANNEL
2793 n = argvars[0].vval.v_channel == NULL
2794 || !channel_is_open(argvars[0].vval.v_channel);
2795 break;
2796#endif
2797 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002798 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002799 n = TRUE;
2800 break;
2801 }
2802
2803 rettv->vval.v_number = n;
2804}
2805
2806/*
2807 * "escape({string}, {chars})" function
2808 */
2809 static void
2810f_escape(typval_T *argvars, typval_T *rettv)
2811{
2812 char_u buf[NUMBUFLEN];
2813
2814 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2815 get_tv_string_buf(&argvars[1], buf));
2816 rettv->v_type = VAR_STRING;
2817}
2818
2819/*
2820 * "eval()" function
2821 */
2822 static void
2823f_eval(typval_T *argvars, typval_T *rettv)
2824{
2825 char_u *s, *p;
2826
2827 s = get_tv_string_chk(&argvars[0]);
2828 if (s != NULL)
2829 s = skipwhite(s);
2830
2831 p = s;
2832 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2833 {
2834 if (p != NULL && !aborting())
2835 EMSG2(_(e_invexpr2), p);
2836 need_clr_eos = FALSE;
2837 rettv->v_type = VAR_NUMBER;
2838 rettv->vval.v_number = 0;
2839 }
2840 else if (*s != NUL)
2841 EMSG(_(e_trailing));
2842}
2843
2844/*
2845 * "eventhandler()" function
2846 */
2847 static void
2848f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2849{
2850 rettv->vval.v_number = vgetc_busy;
2851}
2852
2853/*
2854 * "executable()" function
2855 */
2856 static void
2857f_executable(typval_T *argvars, typval_T *rettv)
2858{
2859 char_u *name = get_tv_string(&argvars[0]);
2860
2861 /* Check in $PATH and also check directly if there is a directory name. */
2862 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2863 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2864}
2865
2866static garray_T redir_execute_ga;
2867
2868/*
2869 * Append "value[value_len]" to the execute() output.
2870 */
2871 void
2872execute_redir_str(char_u *value, int value_len)
2873{
2874 int len;
2875
2876 if (value_len == -1)
2877 len = (int)STRLEN(value); /* Append the entire string */
2878 else
2879 len = value_len; /* Append only "value_len" characters */
2880 if (ga_grow(&redir_execute_ga, len) == OK)
2881 {
2882 mch_memmove((char *)redir_execute_ga.ga_data
2883 + redir_execute_ga.ga_len, value, len);
2884 redir_execute_ga.ga_len += len;
2885 }
2886}
2887
2888/*
2889 * Get next line from a list.
2890 * Called by do_cmdline() to get the next line.
2891 * Returns allocated string, or NULL for end of function.
2892 */
2893
2894 static char_u *
2895get_list_line(
2896 int c UNUSED,
2897 void *cookie,
2898 int indent UNUSED)
2899{
2900 listitem_T **p = (listitem_T **)cookie;
2901 listitem_T *item = *p;
2902 char_u buf[NUMBUFLEN];
2903 char_u *s;
2904
2905 if (item == NULL)
2906 return NULL;
2907 s = get_tv_string_buf_chk(&item->li_tv, buf);
2908 *p = item->li_next;
2909 return s == NULL ? NULL : vim_strsave(s);
2910}
2911
2912/*
2913 * "execute()" function
2914 */
2915 static void
2916f_execute(typval_T *argvars, typval_T *rettv)
2917{
2918 char_u *cmd = NULL;
2919 list_T *list = NULL;
2920 int save_msg_silent = msg_silent;
2921 int save_emsg_silent = emsg_silent;
2922 int save_emsg_noredir = emsg_noredir;
2923 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002924 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002925 garray_T save_ga;
2926
2927 rettv->vval.v_string = NULL;
2928 rettv->v_type = VAR_STRING;
2929
2930 if (argvars[0].v_type == VAR_LIST)
2931 {
2932 list = argvars[0].vval.v_list;
2933 if (list == NULL || list->lv_first == NULL)
2934 /* empty list, no commands, empty output */
2935 return;
2936 ++list->lv_refcount;
2937 }
2938 else
2939 {
2940 cmd = get_tv_string_chk(&argvars[0]);
2941 if (cmd == NULL)
2942 return;
2943 }
2944
2945 if (argvars[1].v_type != VAR_UNKNOWN)
2946 {
2947 char_u buf[NUMBUFLEN];
2948 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2949
2950 if (s == NULL)
2951 return;
2952 if (STRNCMP(s, "silent", 6) == 0)
2953 ++msg_silent;
2954 if (STRCMP(s, "silent!") == 0)
2955 {
2956 emsg_silent = TRUE;
2957 emsg_noredir = TRUE;
2958 }
2959 }
2960 else
2961 ++msg_silent;
2962
2963 if (redir_execute)
2964 save_ga = redir_execute_ga;
2965 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2966 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002967 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002968
2969 if (cmd != NULL)
2970 do_cmdline_cmd(cmd);
2971 else
2972 {
2973 listitem_T *item = list->lv_first;
2974
2975 do_cmdline(NULL, get_list_line, (void *)&item,
2976 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2977 --list->lv_refcount;
2978 }
2979
Bram Moolenaard297f352017-01-29 20:31:21 +01002980 /* Need to append a NUL to the result. */
2981 if (ga_grow(&redir_execute_ga, 1) == OK)
2982 {
2983 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2984 rettv->vval.v_string = redir_execute_ga.ga_data;
2985 }
2986 else
2987 {
2988 ga_clear(&redir_execute_ga);
2989 rettv->vval.v_string = NULL;
2990 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002991 msg_silent = save_msg_silent;
2992 emsg_silent = save_emsg_silent;
2993 emsg_noredir = save_emsg_noredir;
2994
2995 redir_execute = save_redir_execute;
2996 if (redir_execute)
2997 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002998 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002999
3000 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
3001 * line. Put it back in the first column. */
3002 msg_col = 0;
3003}
3004
3005/*
3006 * "exepath()" function
3007 */
3008 static void
3009f_exepath(typval_T *argvars, typval_T *rettv)
3010{
3011 char_u *p = NULL;
3012
3013 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3014 rettv->v_type = VAR_STRING;
3015 rettv->vval.v_string = p;
3016}
3017
3018/*
3019 * "exists()" function
3020 */
3021 static void
3022f_exists(typval_T *argvars, typval_T *rettv)
3023{
3024 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003025 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003026
3027 p = get_tv_string(&argvars[0]);
3028 if (*p == '$') /* environment variable */
3029 {
3030 /* first try "normal" environment variables (fast) */
3031 if (mch_getenv(p + 1) != NULL)
3032 n = TRUE;
3033 else
3034 {
3035 /* try expanding things like $VIM and ${HOME} */
3036 p = expand_env_save(p);
3037 if (p != NULL && *p != '$')
3038 n = TRUE;
3039 vim_free(p);
3040 }
3041 }
3042 else if (*p == '&' || *p == '+') /* option */
3043 {
3044 n = (get_option_tv(&p, NULL, TRUE) == OK);
3045 if (*skipwhite(p) != NUL)
3046 n = FALSE; /* trailing garbage */
3047 }
3048 else if (*p == '*') /* internal or user defined function */
3049 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003050 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003051 }
3052 else if (*p == ':')
3053 {
3054 n = cmd_exists(p + 1);
3055 }
3056 else if (*p == '#')
3057 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003058 if (p[1] == '#')
3059 n = autocmd_supported(p + 2);
3060 else
3061 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003062 }
3063 else /* internal variable */
3064 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003065 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003066 }
3067
3068 rettv->vval.v_number = n;
3069}
3070
3071#ifdef FEAT_FLOAT
3072/*
3073 * "exp()" function
3074 */
3075 static void
3076f_exp(typval_T *argvars, typval_T *rettv)
3077{
3078 float_T f = 0.0;
3079
3080 rettv->v_type = VAR_FLOAT;
3081 if (get_float_arg(argvars, &f) == OK)
3082 rettv->vval.v_float = exp(f);
3083 else
3084 rettv->vval.v_float = 0.0;
3085}
3086#endif
3087
3088/*
3089 * "expand()" function
3090 */
3091 static void
3092f_expand(typval_T *argvars, typval_T *rettv)
3093{
3094 char_u *s;
3095 int len;
3096 char_u *errormsg;
3097 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3098 expand_T xpc;
3099 int error = FALSE;
3100 char_u *result;
3101
3102 rettv->v_type = VAR_STRING;
3103 if (argvars[1].v_type != VAR_UNKNOWN
3104 && argvars[2].v_type != VAR_UNKNOWN
3105 && get_tv_number_chk(&argvars[2], &error)
3106 && !error)
3107 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003108 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003109 }
3110
3111 s = get_tv_string(&argvars[0]);
3112 if (*s == '%' || *s == '#' || *s == '<')
3113 {
3114 ++emsg_off;
3115 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3116 --emsg_off;
3117 if (rettv->v_type == VAR_LIST)
3118 {
3119 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3120 list_append_string(rettv->vval.v_list, result, -1);
3121 else
3122 vim_free(result);
3123 }
3124 else
3125 rettv->vval.v_string = result;
3126 }
3127 else
3128 {
3129 /* When the optional second argument is non-zero, don't remove matches
3130 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3131 if (argvars[1].v_type != VAR_UNKNOWN
3132 && get_tv_number_chk(&argvars[1], &error))
3133 options |= WILD_KEEP_ALL;
3134 if (!error)
3135 {
3136 ExpandInit(&xpc);
3137 xpc.xp_context = EXPAND_FILES;
3138 if (p_wic)
3139 options += WILD_ICASE;
3140 if (rettv->v_type == VAR_STRING)
3141 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3142 options, WILD_ALL);
3143 else if (rettv_list_alloc(rettv) != FAIL)
3144 {
3145 int i;
3146
3147 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3148 for (i = 0; i < xpc.xp_numfiles; i++)
3149 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3150 ExpandCleanup(&xpc);
3151 }
3152 }
3153 else
3154 rettv->vval.v_string = NULL;
3155 }
3156}
3157
3158/*
3159 * "extend(list, list [, idx])" function
3160 * "extend(dict, dict [, action])" function
3161 */
3162 static void
3163f_extend(typval_T *argvars, typval_T *rettv)
3164{
3165 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3166
3167 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3168 {
3169 list_T *l1, *l2;
3170 listitem_T *item;
3171 long before;
3172 int error = FALSE;
3173
3174 l1 = argvars[0].vval.v_list;
3175 l2 = argvars[1].vval.v_list;
3176 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3177 && l2 != NULL)
3178 {
3179 if (argvars[2].v_type != VAR_UNKNOWN)
3180 {
3181 before = (long)get_tv_number_chk(&argvars[2], &error);
3182 if (error)
3183 return; /* type error; errmsg already given */
3184
3185 if (before == l1->lv_len)
3186 item = NULL;
3187 else
3188 {
3189 item = list_find(l1, before);
3190 if (item == NULL)
3191 {
3192 EMSGN(_(e_listidx), before);
3193 return;
3194 }
3195 }
3196 }
3197 else
3198 item = NULL;
3199 list_extend(l1, l2, item);
3200
3201 copy_tv(&argvars[0], rettv);
3202 }
3203 }
3204 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3205 {
3206 dict_T *d1, *d2;
3207 char_u *action;
3208 int i;
3209
3210 d1 = argvars[0].vval.v_dict;
3211 d2 = argvars[1].vval.v_dict;
3212 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3213 && d2 != NULL)
3214 {
3215 /* Check the third argument. */
3216 if (argvars[2].v_type != VAR_UNKNOWN)
3217 {
3218 static char *(av[]) = {"keep", "force", "error"};
3219
3220 action = get_tv_string_chk(&argvars[2]);
3221 if (action == NULL)
3222 return; /* type error; errmsg already given */
3223 for (i = 0; i < 3; ++i)
3224 if (STRCMP(action, av[i]) == 0)
3225 break;
3226 if (i == 3)
3227 {
3228 EMSG2(_(e_invarg2), action);
3229 return;
3230 }
3231 }
3232 else
3233 action = (char_u *)"force";
3234
3235 dict_extend(d1, d2, action);
3236
3237 copy_tv(&argvars[0], rettv);
3238 }
3239 }
3240 else
3241 EMSG2(_(e_listdictarg), "extend()");
3242}
3243
3244/*
3245 * "feedkeys()" function
3246 */
3247 static void
3248f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3249{
3250 int remap = TRUE;
3251 int insert = FALSE;
3252 char_u *keys, *flags;
3253 char_u nbuf[NUMBUFLEN];
3254 int typed = FALSE;
3255 int execute = FALSE;
3256 int dangerous = FALSE;
3257 char_u *keys_esc;
3258
3259 /* This is not allowed in the sandbox. If the commands would still be
3260 * executed in the sandbox it would be OK, but it probably happens later,
3261 * when "sandbox" is no longer set. */
3262 if (check_secure())
3263 return;
3264
3265 keys = get_tv_string(&argvars[0]);
3266
3267 if (argvars[1].v_type != VAR_UNKNOWN)
3268 {
3269 flags = get_tv_string_buf(&argvars[1], nbuf);
3270 for ( ; *flags != NUL; ++flags)
3271 {
3272 switch (*flags)
3273 {
3274 case 'n': remap = FALSE; break;
3275 case 'm': remap = TRUE; break;
3276 case 't': typed = TRUE; break;
3277 case 'i': insert = TRUE; break;
3278 case 'x': execute = TRUE; break;
3279 case '!': dangerous = TRUE; break;
3280 }
3281 }
3282 }
3283
3284 if (*keys != NUL || execute)
3285 {
3286 /* Need to escape K_SPECIAL and CSI before putting the string in the
3287 * typeahead buffer. */
3288 keys_esc = vim_strsave_escape_csi(keys);
3289 if (keys_esc != NULL)
3290 {
3291 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3292 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3293 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003294 if (vgetc_busy
3295#ifdef FEAT_TIMERS
3296 || timer_busy
3297#endif
3298 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299 typebuf_was_filled = TRUE;
3300 if (execute)
3301 {
3302 int save_msg_scroll = msg_scroll;
3303
3304 /* Avoid a 1 second delay when the keys start Insert mode. */
3305 msg_scroll = FALSE;
3306
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02003307#ifdef FEAT_TERMINAL
3308 if (term_use_loop())
3309 terminal_loop(FALSE);
3310 else
3311#endif
3312 {
3313 if (!dangerous)
3314 ++ex_normal_busy;
3315 exec_normal(TRUE);
3316 if (!dangerous)
3317 --ex_normal_busy;
3318 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003319 msg_scroll |= save_msg_scroll;
3320 }
3321 }
3322 }
3323}
3324
3325/*
3326 * "filereadable()" function
3327 */
3328 static void
3329f_filereadable(typval_T *argvars, typval_T *rettv)
3330{
3331 int fd;
3332 char_u *p;
3333 int n;
3334
3335#ifndef O_NONBLOCK
3336# define O_NONBLOCK 0
3337#endif
3338 p = get_tv_string(&argvars[0]);
3339 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3340 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3341 {
3342 n = TRUE;
3343 close(fd);
3344 }
3345 else
3346 n = FALSE;
3347
3348 rettv->vval.v_number = n;
3349}
3350
3351/*
3352 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3353 * rights to write into.
3354 */
3355 static void
3356f_filewritable(typval_T *argvars, typval_T *rettv)
3357{
3358 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3359}
3360
3361 static void
3362findfilendir(
3363 typval_T *argvars UNUSED,
3364 typval_T *rettv,
3365 int find_what UNUSED)
3366{
3367#ifdef FEAT_SEARCHPATH
3368 char_u *fname;
3369 char_u *fresult = NULL;
3370 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3371 char_u *p;
3372 char_u pathbuf[NUMBUFLEN];
3373 int count = 1;
3374 int first = TRUE;
3375 int error = FALSE;
3376#endif
3377
3378 rettv->vval.v_string = NULL;
3379 rettv->v_type = VAR_STRING;
3380
3381#ifdef FEAT_SEARCHPATH
3382 fname = get_tv_string(&argvars[0]);
3383
3384 if (argvars[1].v_type != VAR_UNKNOWN)
3385 {
3386 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3387 if (p == NULL)
3388 error = TRUE;
3389 else
3390 {
3391 if (*p != NUL)
3392 path = p;
3393
3394 if (argvars[2].v_type != VAR_UNKNOWN)
3395 count = (int)get_tv_number_chk(&argvars[2], &error);
3396 }
3397 }
3398
3399 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3400 error = TRUE;
3401
3402 if (*fname != NUL && !error)
3403 {
3404 do
3405 {
3406 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3407 vim_free(fresult);
3408 fresult = find_file_in_path_option(first ? fname : NULL,
3409 first ? (int)STRLEN(fname) : 0,
3410 0, first, path,
3411 find_what,
3412 curbuf->b_ffname,
3413 find_what == FINDFILE_DIR
3414 ? (char_u *)"" : curbuf->b_p_sua);
3415 first = FALSE;
3416
3417 if (fresult != NULL && rettv->v_type == VAR_LIST)
3418 list_append_string(rettv->vval.v_list, fresult, -1);
3419
3420 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3421 }
3422
3423 if (rettv->v_type == VAR_STRING)
3424 rettv->vval.v_string = fresult;
3425#endif
3426}
3427
3428/*
3429 * "filter()" function
3430 */
3431 static void
3432f_filter(typval_T *argvars, typval_T *rettv)
3433{
3434 filter_map(argvars, rettv, FALSE);
3435}
3436
3437/*
3438 * "finddir({fname}[, {path}[, {count}]])" function
3439 */
3440 static void
3441f_finddir(typval_T *argvars, typval_T *rettv)
3442{
3443 findfilendir(argvars, rettv, FINDFILE_DIR);
3444}
3445
3446/*
3447 * "findfile({fname}[, {path}[, {count}]])" function
3448 */
3449 static void
3450f_findfile(typval_T *argvars, typval_T *rettv)
3451{
3452 findfilendir(argvars, rettv, FINDFILE_FILE);
3453}
3454
3455#ifdef FEAT_FLOAT
3456/*
3457 * "float2nr({float})" function
3458 */
3459 static void
3460f_float2nr(typval_T *argvars, typval_T *rettv)
3461{
3462 float_T f = 0.0;
3463
3464 if (get_float_arg(argvars, &f) == OK)
3465 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003466 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003467 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003468 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003469 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003470 else
3471 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003472 }
3473}
3474
3475/*
3476 * "floor({float})" function
3477 */
3478 static void
3479f_floor(typval_T *argvars, typval_T *rettv)
3480{
3481 float_T f = 0.0;
3482
3483 rettv->v_type = VAR_FLOAT;
3484 if (get_float_arg(argvars, &f) == OK)
3485 rettv->vval.v_float = floor(f);
3486 else
3487 rettv->vval.v_float = 0.0;
3488}
3489
3490/*
3491 * "fmod()" function
3492 */
3493 static void
3494f_fmod(typval_T *argvars, typval_T *rettv)
3495{
3496 float_T fx = 0.0, fy = 0.0;
3497
3498 rettv->v_type = VAR_FLOAT;
3499 if (get_float_arg(argvars, &fx) == OK
3500 && get_float_arg(&argvars[1], &fy) == OK)
3501 rettv->vval.v_float = fmod(fx, fy);
3502 else
3503 rettv->vval.v_float = 0.0;
3504}
3505#endif
3506
3507/*
3508 * "fnameescape({string})" function
3509 */
3510 static void
3511f_fnameescape(typval_T *argvars, typval_T *rettv)
3512{
3513 rettv->vval.v_string = vim_strsave_fnameescape(
3514 get_tv_string(&argvars[0]), FALSE);
3515 rettv->v_type = VAR_STRING;
3516}
3517
3518/*
3519 * "fnamemodify({fname}, {mods})" function
3520 */
3521 static void
3522f_fnamemodify(typval_T *argvars, typval_T *rettv)
3523{
3524 char_u *fname;
3525 char_u *mods;
3526 int usedlen = 0;
3527 int len;
3528 char_u *fbuf = NULL;
3529 char_u buf[NUMBUFLEN];
3530
3531 fname = get_tv_string_chk(&argvars[0]);
3532 mods = get_tv_string_buf_chk(&argvars[1], buf);
3533 if (fname == NULL || mods == NULL)
3534 fname = NULL;
3535 else
3536 {
3537 len = (int)STRLEN(fname);
3538 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3539 }
3540
3541 rettv->v_type = VAR_STRING;
3542 if (fname == NULL)
3543 rettv->vval.v_string = NULL;
3544 else
3545 rettv->vval.v_string = vim_strnsave(fname, len);
3546 vim_free(fbuf);
3547}
3548
3549static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3550
3551/*
3552 * "foldclosed()" function
3553 */
3554 static void
3555foldclosed_both(
3556 typval_T *argvars UNUSED,
3557 typval_T *rettv,
3558 int end UNUSED)
3559{
3560#ifdef FEAT_FOLDING
3561 linenr_T lnum;
3562 linenr_T first, last;
3563
3564 lnum = get_tv_lnum(argvars);
3565 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3566 {
3567 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3568 {
3569 if (end)
3570 rettv->vval.v_number = (varnumber_T)last;
3571 else
3572 rettv->vval.v_number = (varnumber_T)first;
3573 return;
3574 }
3575 }
3576#endif
3577 rettv->vval.v_number = -1;
3578}
3579
3580/*
3581 * "foldclosed()" function
3582 */
3583 static void
3584f_foldclosed(typval_T *argvars, typval_T *rettv)
3585{
3586 foldclosed_both(argvars, rettv, FALSE);
3587}
3588
3589/*
3590 * "foldclosedend()" function
3591 */
3592 static void
3593f_foldclosedend(typval_T *argvars, typval_T *rettv)
3594{
3595 foldclosed_both(argvars, rettv, TRUE);
3596}
3597
3598/*
3599 * "foldlevel()" function
3600 */
3601 static void
3602f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3603{
3604#ifdef FEAT_FOLDING
3605 linenr_T lnum;
3606
3607 lnum = get_tv_lnum(argvars);
3608 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3609 rettv->vval.v_number = foldLevel(lnum);
3610#endif
3611}
3612
3613/*
3614 * "foldtext()" function
3615 */
3616 static void
3617f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3618{
3619#ifdef FEAT_FOLDING
3620 linenr_T foldstart;
3621 linenr_T foldend;
3622 char_u *dashes;
3623 linenr_T lnum;
3624 char_u *s;
3625 char_u *r;
3626 int len;
3627 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003628 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003629#endif
3630
3631 rettv->v_type = VAR_STRING;
3632 rettv->vval.v_string = NULL;
3633#ifdef FEAT_FOLDING
3634 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3635 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3636 dashes = get_vim_var_str(VV_FOLDDASHES);
3637 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3638 && dashes != NULL)
3639 {
3640 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003641 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003642 if (!linewhite(lnum))
3643 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003644
3645 /* Find interesting text in this line. */
3646 s = skipwhite(ml_get(lnum));
3647 /* skip C comment-start */
3648 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3649 {
3650 s = skipwhite(s + 2);
3651 if (*skipwhite(s) == NUL
3652 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3653 {
3654 s = skipwhite(ml_get(lnum + 1));
3655 if (*s == '*')
3656 s = skipwhite(s + 1);
3657 }
3658 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003659 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003660 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003661 r = alloc((unsigned)(STRLEN(txt)
3662 + STRLEN(dashes) /* for %s */
3663 + 20 /* for %3ld */
3664 + STRLEN(s))); /* concatenated */
3665 if (r != NULL)
3666 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003667 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003668 len = (int)STRLEN(r);
3669 STRCAT(r, s);
3670 /* remove 'foldmarker' and 'commentstring' */
3671 foldtext_cleanup(r + len);
3672 rettv->vval.v_string = r;
3673 }
3674 }
3675#endif
3676}
3677
3678/*
3679 * "foldtextresult(lnum)" function
3680 */
3681 static void
3682f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3683{
3684#ifdef FEAT_FOLDING
3685 linenr_T lnum;
3686 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003687 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003688 foldinfo_T foldinfo;
3689 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003690 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003691#endif
3692
3693 rettv->v_type = VAR_STRING;
3694 rettv->vval.v_string = NULL;
3695#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003696 if (entered)
3697 return; /* reject recursive use */
3698 entered = TRUE;
3699
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003700 lnum = get_tv_lnum(argvars);
3701 /* treat illegal types and illegal string values for {lnum} the same */
3702 if (lnum < 0)
3703 lnum = 0;
3704 fold_count = foldedCount(curwin, lnum, &foldinfo);
3705 if (fold_count > 0)
3706 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003707 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3708 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003709 if (text == buf)
3710 text = vim_strsave(text);
3711 rettv->vval.v_string = text;
3712 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003713
3714 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003715#endif
3716}
3717
3718/*
3719 * "foreground()" function
3720 */
3721 static void
3722f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3723{
3724#ifdef FEAT_GUI
3725 if (gui.in_use)
3726 gui_mch_set_foreground();
3727#else
3728# ifdef WIN32
3729 win32_set_foreground();
3730# endif
3731#endif
3732}
3733
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003734 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003735common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003736{
3737 char_u *s;
3738 char_u *name;
3739 int use_string = FALSE;
3740 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003741 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003742
3743 if (argvars[0].v_type == VAR_FUNC)
3744 {
3745 /* function(MyFunc, [arg], dict) */
3746 s = argvars[0].vval.v_string;
3747 }
3748 else if (argvars[0].v_type == VAR_PARTIAL
3749 && argvars[0].vval.v_partial != NULL)
3750 {
3751 /* function(dict.MyFunc, [arg]) */
3752 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003753 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003754 }
3755 else
3756 {
3757 /* function('MyFunc', [arg], dict) */
3758 s = get_tv_string(&argvars[0]);
3759 use_string = TRUE;
3760 }
3761
Bram Moolenaar843b8842016-08-21 14:36:15 +02003762 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003763 {
3764 name = s;
3765 trans_name = trans_function_name(&name, FALSE,
3766 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3767 if (*name != NUL)
3768 s = NULL;
3769 }
3770
Bram Moolenaar843b8842016-08-21 14:36:15 +02003771 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3772 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003773 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003774 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003775 else if (trans_name != NULL && (is_funcref
3776 ? find_func(trans_name) == NULL
3777 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003778 EMSG2(_("E700: Unknown function: %s"), s);
3779 else
3780 {
3781 int dict_idx = 0;
3782 int arg_idx = 0;
3783 list_T *list = NULL;
3784
3785 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3786 {
3787 char sid_buf[25];
3788 int off = *s == 's' ? 2 : 5;
3789
3790 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3791 * also be called from another script. Using trans_function_name()
3792 * would also work, but some plugins depend on the name being
3793 * printable text. */
3794 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3795 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3796 if (name != NULL)
3797 {
3798 STRCPY(name, sid_buf);
3799 STRCAT(name, s + off);
3800 }
3801 }
3802 else
3803 name = vim_strsave(s);
3804
3805 if (argvars[1].v_type != VAR_UNKNOWN)
3806 {
3807 if (argvars[2].v_type != VAR_UNKNOWN)
3808 {
3809 /* function(name, [args], dict) */
3810 arg_idx = 1;
3811 dict_idx = 2;
3812 }
3813 else if (argvars[1].v_type == VAR_DICT)
3814 /* function(name, dict) */
3815 dict_idx = 1;
3816 else
3817 /* function(name, [args]) */
3818 arg_idx = 1;
3819 if (dict_idx > 0)
3820 {
3821 if (argvars[dict_idx].v_type != VAR_DICT)
3822 {
3823 EMSG(_("E922: expected a dict"));
3824 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003825 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003826 }
3827 if (argvars[dict_idx].vval.v_dict == NULL)
3828 dict_idx = 0;
3829 }
3830 if (arg_idx > 0)
3831 {
3832 if (argvars[arg_idx].v_type != VAR_LIST)
3833 {
3834 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3835 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003836 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003837 }
3838 list = argvars[arg_idx].vval.v_list;
3839 if (list == NULL || list->lv_len == 0)
3840 arg_idx = 0;
3841 }
3842 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003843 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003844 {
3845 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3846
3847 /* result is a VAR_PARTIAL */
3848 if (pt == NULL)
3849 vim_free(name);
3850 else
3851 {
3852 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3853 {
3854 listitem_T *li;
3855 int i = 0;
3856 int arg_len = 0;
3857 int lv_len = 0;
3858
3859 if (arg_pt != NULL)
3860 arg_len = arg_pt->pt_argc;
3861 if (list != NULL)
3862 lv_len = list->lv_len;
3863 pt->pt_argc = arg_len + lv_len;
3864 pt->pt_argv = (typval_T *)alloc(
3865 sizeof(typval_T) * pt->pt_argc);
3866 if (pt->pt_argv == NULL)
3867 {
3868 vim_free(pt);
3869 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003870 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003871 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003872 for (i = 0; i < arg_len; i++)
3873 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3874 if (lv_len > 0)
3875 for (li = list->lv_first; li != NULL;
3876 li = li->li_next)
3877 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003878 }
3879
3880 /* For "function(dict.func, [], dict)" and "func" is a partial
3881 * use "dict". That is backwards compatible. */
3882 if (dict_idx > 0)
3883 {
3884 /* The dict is bound explicitly, pt_auto is FALSE. */
3885 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3886 ++pt->pt_dict->dv_refcount;
3887 }
3888 else if (arg_pt != NULL)
3889 {
3890 /* If the dict was bound automatically the result is also
3891 * bound automatically. */
3892 pt->pt_dict = arg_pt->pt_dict;
3893 pt->pt_auto = arg_pt->pt_auto;
3894 if (pt->pt_dict != NULL)
3895 ++pt->pt_dict->dv_refcount;
3896 }
3897
3898 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003899 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3900 {
3901 pt->pt_func = arg_pt->pt_func;
3902 func_ptr_ref(pt->pt_func);
3903 vim_free(name);
3904 }
3905 else if (is_funcref)
3906 {
3907 pt->pt_func = find_func(trans_name);
3908 func_ptr_ref(pt->pt_func);
3909 vim_free(name);
3910 }
3911 else
3912 {
3913 pt->pt_name = name;
3914 func_ref(name);
3915 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003916 }
3917 rettv->v_type = VAR_PARTIAL;
3918 rettv->vval.v_partial = pt;
3919 }
3920 else
3921 {
3922 /* result is a VAR_FUNC */
3923 rettv->v_type = VAR_FUNC;
3924 rettv->vval.v_string = name;
3925 func_ref(name);
3926 }
3927 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003928theend:
3929 vim_free(trans_name);
3930}
3931
3932/*
3933 * "funcref()" function
3934 */
3935 static void
3936f_funcref(typval_T *argvars, typval_T *rettv)
3937{
3938 common_function(argvars, rettv, TRUE);
3939}
3940
3941/*
3942 * "function()" function
3943 */
3944 static void
3945f_function(typval_T *argvars, typval_T *rettv)
3946{
3947 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003948}
3949
3950/*
3951 * "garbagecollect()" function
3952 */
3953 static void
3954f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3955{
3956 /* This is postponed until we are back at the toplevel, because we may be
3957 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3958 want_garbage_collect = TRUE;
3959
3960 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3961 garbage_collect_at_exit = TRUE;
3962}
3963
3964/*
3965 * "get()" function
3966 */
3967 static void
3968f_get(typval_T *argvars, typval_T *rettv)
3969{
3970 listitem_T *li;
3971 list_T *l;
3972 dictitem_T *di;
3973 dict_T *d;
3974 typval_T *tv = NULL;
3975
3976 if (argvars[0].v_type == VAR_LIST)
3977 {
3978 if ((l = argvars[0].vval.v_list) != NULL)
3979 {
3980 int error = FALSE;
3981
3982 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3983 if (!error && li != NULL)
3984 tv = &li->li_tv;
3985 }
3986 }
3987 else if (argvars[0].v_type == VAR_DICT)
3988 {
3989 if ((d = argvars[0].vval.v_dict) != NULL)
3990 {
3991 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3992 if (di != NULL)
3993 tv = &di->di_tv;
3994 }
3995 }
3996 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3997 {
3998 partial_T *pt;
3999 partial_T fref_pt;
4000
4001 if (argvars[0].v_type == VAR_PARTIAL)
4002 pt = argvars[0].vval.v_partial;
4003 else
4004 {
4005 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4006 fref_pt.pt_name = argvars[0].vval.v_string;
4007 pt = &fref_pt;
4008 }
4009
4010 if (pt != NULL)
4011 {
4012 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004013 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004014
4015 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4016 {
4017 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004018 n = partial_name(pt);
4019 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004020 rettv->vval.v_string = NULL;
4021 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004022 {
4023 rettv->vval.v_string = vim_strsave(n);
4024 if (rettv->v_type == VAR_FUNC)
4025 func_ref(rettv->vval.v_string);
4026 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004027 }
4028 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004029 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004030 else if (STRCMP(what, "args") == 0)
4031 {
4032 rettv->v_type = VAR_LIST;
4033 if (rettv_list_alloc(rettv) == OK)
4034 {
4035 int i;
4036
4037 for (i = 0; i < pt->pt_argc; ++i)
4038 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4039 }
4040 }
4041 else
4042 EMSG2(_(e_invarg2), what);
4043 return;
4044 }
4045 }
4046 else
4047 EMSG2(_(e_listdictarg), "get()");
4048
4049 if (tv == NULL)
4050 {
4051 if (argvars[2].v_type != VAR_UNKNOWN)
4052 copy_tv(&argvars[2], rettv);
4053 }
4054 else
4055 copy_tv(tv, rettv);
4056}
4057
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004058#ifdef FEAT_SIGNS
4059/*
4060 * Returns information about signs placed in a buffer as list of dicts.
4061 */
4062 static void
4063get_buffer_signs(buf_T *buf, list_T *l)
4064{
4065 signlist_T *sign;
4066
4067 for (sign = buf->b_signlist; sign; sign = sign->next)
4068 {
4069 dict_T *d = dict_alloc();
4070
4071 if (d != NULL)
4072 {
4073 dict_add_nr_str(d, "id", sign->id, NULL);
4074 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004075 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004076
4077 list_append_dict(l, d);
4078 }
4079 }
4080}
4081#endif
4082
4083/*
4084 * Returns buffer options, variables and other attributes in a dictionary.
4085 */
4086 static dict_T *
4087get_buffer_info(buf_T *buf)
4088{
4089 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004090 tabpage_T *tp;
4091 win_T *wp;
4092 list_T *windows;
4093
4094 dict = dict_alloc();
4095 if (dict == NULL)
4096 return NULL;
4097
Bram Moolenaar33928832016-08-18 21:22:04 +02004098 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004099 dict_add_nr_str(dict, "name", 0L,
4100 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004101 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4102 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004103 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4104 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4105 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004106 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004107 dict_add_nr_str(dict, "hidden",
4108 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4109 NULL);
4110
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004111 /* Get a reference to buffer variables */
4112 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004113
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004114 /* List of windows displaying this buffer */
4115 windows = list_alloc();
4116 if (windows != NULL)
4117 {
4118 FOR_ALL_TAB_WINDOWS(tp, wp)
4119 if (wp->w_buffer == buf)
4120 list_append_number(windows, (varnumber_T)wp->w_id);
4121 dict_add_list(dict, "windows", windows);
4122 }
4123
4124#ifdef FEAT_SIGNS
4125 if (buf->b_signlist != NULL)
4126 {
4127 /* List of signs placed in this buffer */
4128 list_T *signs = list_alloc();
4129 if (signs != NULL)
4130 {
4131 get_buffer_signs(buf, signs);
4132 dict_add_list(dict, "signs", signs);
4133 }
4134 }
4135#endif
4136
4137 return dict;
4138}
4139
4140/*
4141 * "getbufinfo()" function
4142 */
4143 static void
4144f_getbufinfo(typval_T *argvars, typval_T *rettv)
4145{
4146 buf_T *buf = NULL;
4147 buf_T *argbuf = NULL;
4148 dict_T *d;
4149 int filtered = FALSE;
4150 int sel_buflisted = FALSE;
4151 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004152 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004153
4154 if (rettv_list_alloc(rettv) != OK)
4155 return;
4156
4157 /* List of all the buffers or selected buffers */
4158 if (argvars[0].v_type == VAR_DICT)
4159 {
4160 dict_T *sel_d = argvars[0].vval.v_dict;
4161
4162 if (sel_d != NULL)
4163 {
4164 dictitem_T *di;
4165
4166 filtered = TRUE;
4167
4168 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4169 if (di != NULL && get_tv_number(&di->di_tv))
4170 sel_buflisted = TRUE;
4171
4172 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4173 if (di != NULL && get_tv_number(&di->di_tv))
4174 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004175
4176 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4177 if (di != NULL && get_tv_number(&di->di_tv))
4178 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004179 }
4180 }
4181 else if (argvars[0].v_type != VAR_UNKNOWN)
4182 {
4183 /* Information about one buffer. Argument specifies the buffer */
4184 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4185 ++emsg_off;
4186 argbuf = get_buf_tv(&argvars[0], FALSE);
4187 --emsg_off;
4188 if (argbuf == NULL)
4189 return;
4190 }
4191
4192 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004193 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004194 {
4195 if (argbuf != NULL && argbuf != buf)
4196 continue;
4197 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004198 || (sel_buflisted && !buf->b_p_bl)
4199 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004200 continue;
4201
4202 d = get_buffer_info(buf);
4203 if (d != NULL)
4204 list_append_dict(rettv->vval.v_list, d);
4205 if (argbuf != NULL)
4206 return;
4207 }
4208}
4209
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004210static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4211
4212/*
4213 * Get line or list of lines from buffer "buf" into "rettv".
4214 * Return a range (from start to end) of lines in rettv from the specified
4215 * buffer.
4216 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4217 */
4218 static void
4219get_buffer_lines(
4220 buf_T *buf,
4221 linenr_T start,
4222 linenr_T end,
4223 int retlist,
4224 typval_T *rettv)
4225{
4226 char_u *p;
4227
4228 rettv->v_type = VAR_STRING;
4229 rettv->vval.v_string = NULL;
4230 if (retlist && rettv_list_alloc(rettv) == FAIL)
4231 return;
4232
4233 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4234 return;
4235
4236 if (!retlist)
4237 {
4238 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4239 p = ml_get_buf(buf, start, FALSE);
4240 else
4241 p = (char_u *)"";
4242 rettv->vval.v_string = vim_strsave(p);
4243 }
4244 else
4245 {
4246 if (end < start)
4247 return;
4248
4249 if (start < 1)
4250 start = 1;
4251 if (end > buf->b_ml.ml_line_count)
4252 end = buf->b_ml.ml_line_count;
4253 while (start <= end)
4254 if (list_append_string(rettv->vval.v_list,
4255 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4256 break;
4257 }
4258}
4259
4260/*
4261 * Get the lnum from the first argument.
4262 * Also accepts "$", then "buf" is used.
4263 * Returns 0 on error.
4264 */
4265 static linenr_T
4266get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4267{
4268 if (argvars[0].v_type == VAR_STRING
4269 && argvars[0].vval.v_string != NULL
4270 && argvars[0].vval.v_string[0] == '$'
4271 && buf != NULL)
4272 return buf->b_ml.ml_line_count;
4273 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4274}
4275
4276/*
4277 * "getbufline()" function
4278 */
4279 static void
4280f_getbufline(typval_T *argvars, typval_T *rettv)
4281{
4282 linenr_T lnum;
4283 linenr_T end;
4284 buf_T *buf;
4285
4286 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4287 ++emsg_off;
4288 buf = get_buf_tv(&argvars[0], FALSE);
4289 --emsg_off;
4290
4291 lnum = get_tv_lnum_buf(&argvars[1], buf);
4292 if (argvars[2].v_type == VAR_UNKNOWN)
4293 end = lnum;
4294 else
4295 end = get_tv_lnum_buf(&argvars[2], buf);
4296
4297 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4298}
4299
4300/*
4301 * "getbufvar()" function
4302 */
4303 static void
4304f_getbufvar(typval_T *argvars, typval_T *rettv)
4305{
4306 buf_T *buf;
4307 buf_T *save_curbuf;
4308 char_u *varname;
4309 dictitem_T *v;
4310 int done = FALSE;
4311
4312 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4313 varname = get_tv_string_chk(&argvars[1]);
4314 ++emsg_off;
4315 buf = get_buf_tv(&argvars[0], FALSE);
4316
4317 rettv->v_type = VAR_STRING;
4318 rettv->vval.v_string = NULL;
4319
4320 if (buf != NULL && varname != NULL)
4321 {
4322 /* set curbuf to be our buf, temporarily */
4323 save_curbuf = curbuf;
4324 curbuf = buf;
4325
Bram Moolenaar30567352016-08-27 21:25:44 +02004326 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004327 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004328 if (varname[1] == NUL)
4329 {
4330 /* get all buffer-local options in a dict */
4331 dict_T *opts = get_winbuf_options(TRUE);
4332
4333 if (opts != NULL)
4334 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004335 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004336 done = TRUE;
4337 }
4338 }
4339 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4340 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004341 done = TRUE;
4342 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004343 else
4344 {
4345 /* Look up the variable. */
4346 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4347 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4348 'b', varname, FALSE);
4349 if (v != NULL)
4350 {
4351 copy_tv(&v->di_tv, rettv);
4352 done = TRUE;
4353 }
4354 }
4355
4356 /* restore previous notion of curbuf */
4357 curbuf = save_curbuf;
4358 }
4359
4360 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4361 /* use the default value */
4362 copy_tv(&argvars[2], rettv);
4363
4364 --emsg_off;
4365}
4366
4367/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004368 * "getchangelist()" function
4369 */
4370 static void
4371f_getchangelist(typval_T *argvars, typval_T *rettv)
4372{
4373#ifdef FEAT_JUMPLIST
4374 buf_T *buf;
4375 int i;
4376 list_T *l;
4377 dict_T *d;
4378#endif
4379
4380 if (rettv_list_alloc(rettv) != OK)
4381 return;
4382
4383#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004384 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4385 ++emsg_off;
4386 buf = get_buf_tv(&argvars[0], FALSE);
4387 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004388 if (buf == NULL)
4389 return;
4390
4391 l = list_alloc();
4392 if (l == NULL)
4393 return;
4394
4395 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4396 return;
4397 /*
4398 * The current window change list index tracks only the position in the
4399 * current buffer change list. For other buffers, use the change list
4400 * length as the current index.
4401 */
4402 list_append_number(rettv->vval.v_list,
4403 (varnumber_T)((buf == curwin->w_buffer)
4404 ? curwin->w_changelistidx : buf->b_changelistlen));
4405
4406 for (i = 0; i < buf->b_changelistlen; ++i)
4407 {
4408 if (buf->b_changelist[i].lnum == 0)
4409 continue;
4410 if ((d = dict_alloc()) == NULL)
4411 return;
4412 if (list_append_dict(l, d) == FAIL)
4413 return;
4414 dict_add_nr_str(d, "lnum", (long)buf->b_changelist[i].lnum, NULL);
4415 dict_add_nr_str(d, "col", (long)buf->b_changelist[i].col, NULL);
4416# ifdef FEAT_VIRTUALEDIT
4417 dict_add_nr_str(d, "coladd", (long)buf->b_changelist[i].coladd, NULL);
4418# endif
4419 }
4420#endif
4421}
4422/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004423 * "getchar()" function
4424 */
4425 static void
4426f_getchar(typval_T *argvars, typval_T *rettv)
4427{
4428 varnumber_T n;
4429 int error = FALSE;
4430
4431 /* Position the cursor. Needed after a message that ends in a space. */
4432 windgoto(msg_row, msg_col);
4433
4434 ++no_mapping;
4435 ++allow_keys;
4436 for (;;)
4437 {
4438 if (argvars[0].v_type == VAR_UNKNOWN)
4439 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004440 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004441 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4442 /* getchar(1): only check if char avail */
4443 n = vpeekc_any();
4444 else if (error || vpeekc_any() == NUL)
4445 /* illegal argument or getchar(0) and no char avail: return zero */
4446 n = 0;
4447 else
4448 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004449 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004450
4451 if (n == K_IGNORE)
4452 continue;
4453 break;
4454 }
4455 --no_mapping;
4456 --allow_keys;
4457
4458 set_vim_var_nr(VV_MOUSE_WIN, 0);
4459 set_vim_var_nr(VV_MOUSE_WINID, 0);
4460 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4461 set_vim_var_nr(VV_MOUSE_COL, 0);
4462
4463 rettv->vval.v_number = n;
4464 if (IS_SPECIAL(n) || mod_mask != 0)
4465 {
4466 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4467 int i = 0;
4468
4469 /* Turn a special key into three bytes, plus modifier. */
4470 if (mod_mask != 0)
4471 {
4472 temp[i++] = K_SPECIAL;
4473 temp[i++] = KS_MODIFIER;
4474 temp[i++] = mod_mask;
4475 }
4476 if (IS_SPECIAL(n))
4477 {
4478 temp[i++] = K_SPECIAL;
4479 temp[i++] = K_SECOND(n);
4480 temp[i++] = K_THIRD(n);
4481 }
4482#ifdef FEAT_MBYTE
4483 else if (has_mbyte)
4484 i += (*mb_char2bytes)(n, temp + i);
4485#endif
4486 else
4487 temp[i++] = n;
4488 temp[i++] = NUL;
4489 rettv->v_type = VAR_STRING;
4490 rettv->vval.v_string = vim_strsave(temp);
4491
4492#ifdef FEAT_MOUSE
4493 if (is_mouse_key(n))
4494 {
4495 int row = mouse_row;
4496 int col = mouse_col;
4497 win_T *win;
4498 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004499 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004500 int winnr = 1;
4501
4502 if (row >= 0 && col >= 0)
4503 {
4504 /* Find the window at the mouse coordinates and compute the
4505 * text position. */
4506 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004507 if (win == NULL)
4508 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004509 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004510 for (wp = firstwin; wp != win; wp = wp->w_next)
4511 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004512 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4513 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4514 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4515 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4516 }
4517 }
4518#endif
4519 }
4520}
4521
4522/*
4523 * "getcharmod()" function
4524 */
4525 static void
4526f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4527{
4528 rettv->vval.v_number = mod_mask;
4529}
4530
4531/*
4532 * "getcharsearch()" function
4533 */
4534 static void
4535f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4536{
4537 if (rettv_dict_alloc(rettv) != FAIL)
4538 {
4539 dict_T *dict = rettv->vval.v_dict;
4540
4541 dict_add_nr_str(dict, "char", 0L, last_csearch());
4542 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4543 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4544 }
4545}
4546
4547/*
4548 * "getcmdline()" function
4549 */
4550 static void
4551f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4552{
4553 rettv->v_type = VAR_STRING;
4554 rettv->vval.v_string = get_cmdline_str();
4555}
4556
4557/*
4558 * "getcmdpos()" function
4559 */
4560 static void
4561f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4562{
4563 rettv->vval.v_number = get_cmdline_pos() + 1;
4564}
4565
4566/*
4567 * "getcmdtype()" function
4568 */
4569 static void
4570f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4571{
4572 rettv->v_type = VAR_STRING;
4573 rettv->vval.v_string = alloc(2);
4574 if (rettv->vval.v_string != NULL)
4575 {
4576 rettv->vval.v_string[0] = get_cmdline_type();
4577 rettv->vval.v_string[1] = NUL;
4578 }
4579}
4580
4581/*
4582 * "getcmdwintype()" function
4583 */
4584 static void
4585f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4586{
4587 rettv->v_type = VAR_STRING;
4588 rettv->vval.v_string = NULL;
4589#ifdef FEAT_CMDWIN
4590 rettv->vval.v_string = alloc(2);
4591 if (rettv->vval.v_string != NULL)
4592 {
4593 rettv->vval.v_string[0] = cmdwin_type;
4594 rettv->vval.v_string[1] = NUL;
4595 }
4596#endif
4597}
4598
4599#if defined(FEAT_CMDL_COMPL)
4600/*
4601 * "getcompletion()" function
4602 */
4603 static void
4604f_getcompletion(typval_T *argvars, typval_T *rettv)
4605{
4606 char_u *pat;
4607 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004608 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004609 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4610 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004611
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004612 if (argvars[2].v_type != VAR_UNKNOWN)
4613 filtered = get_tv_number_chk(&argvars[2], NULL);
4614
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004615 if (p_wic)
4616 options |= WILD_ICASE;
4617
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004618 /* For filtered results, 'wildignore' is used */
4619 if (!filtered)
4620 options |= WILD_KEEP_ALL;
4621
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004622 ExpandInit(&xpc);
4623 xpc.xp_pattern = get_tv_string(&argvars[0]);
4624 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4625 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4626 if (xpc.xp_context == EXPAND_NOTHING)
4627 {
4628 if (argvars[1].v_type == VAR_STRING)
4629 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4630 else
4631 EMSG(_(e_invarg));
4632 return;
4633 }
4634
4635# if defined(FEAT_MENU)
4636 if (xpc.xp_context == EXPAND_MENUS)
4637 {
4638 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4639 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4640 }
4641# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004642#ifdef FEAT_CSCOPE
4643 if (xpc.xp_context == EXPAND_CSCOPE)
4644 {
4645 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4646 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4647 }
4648#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004649#ifdef FEAT_SIGNS
4650 if (xpc.xp_context == EXPAND_SIGN)
4651 {
4652 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4653 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4654 }
4655#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004656
4657 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4658 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4659 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004660 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004661
4662 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4663
4664 for (i = 0; i < xpc.xp_numfiles; i++)
4665 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4666 }
4667 vim_free(pat);
4668 ExpandCleanup(&xpc);
4669}
4670#endif
4671
4672/*
4673 * "getcwd()" function
4674 */
4675 static void
4676f_getcwd(typval_T *argvars, typval_T *rettv)
4677{
4678 win_T *wp = NULL;
4679 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004680 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004681
4682 rettv->v_type = VAR_STRING;
4683 rettv->vval.v_string = NULL;
4684
Bram Moolenaar54591292018-02-09 20:53:59 +01004685 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4686 global = TRUE;
4687 else
4688 wp = find_tabwin(&argvars[0], &argvars[1]);
4689
4690 if (wp != NULL && wp->w_localdir != NULL)
4691 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4692 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004693 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004694 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004695 rettv->vval.v_string = vim_strsave(globaldir);
4696 else
4697 {
4698 cwd = alloc(MAXPATHL);
4699 if (cwd != NULL)
4700 {
4701 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4702 rettv->vval.v_string = vim_strsave(cwd);
4703 vim_free(cwd);
4704 }
4705 }
4706#ifdef BACKSLASH_IN_FILENAME
4707 if (rettv->vval.v_string != NULL)
4708 slash_adjust(rettv->vval.v_string);
4709#endif
4710 }
4711}
4712
4713/*
4714 * "getfontname()" function
4715 */
4716 static void
4717f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4718{
4719 rettv->v_type = VAR_STRING;
4720 rettv->vval.v_string = NULL;
4721#ifdef FEAT_GUI
4722 if (gui.in_use)
4723 {
4724 GuiFont font;
4725 char_u *name = NULL;
4726
4727 if (argvars[0].v_type == VAR_UNKNOWN)
4728 {
4729 /* Get the "Normal" font. Either the name saved by
4730 * hl_set_font_name() or from the font ID. */
4731 font = gui.norm_font;
4732 name = hl_get_font_name();
4733 }
4734 else
4735 {
4736 name = get_tv_string(&argvars[0]);
4737 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4738 return;
4739 font = gui_mch_get_font(name, FALSE);
4740 if (font == NOFONT)
4741 return; /* Invalid font name, return empty string. */
4742 }
4743 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4744 if (argvars[0].v_type != VAR_UNKNOWN)
4745 gui_mch_free_font(font);
4746 }
4747#endif
4748}
4749
4750/*
4751 * "getfperm({fname})" function
4752 */
4753 static void
4754f_getfperm(typval_T *argvars, typval_T *rettv)
4755{
4756 char_u *fname;
4757 stat_T st;
4758 char_u *perm = NULL;
4759 char_u flags[] = "rwx";
4760 int i;
4761
4762 fname = get_tv_string(&argvars[0]);
4763
4764 rettv->v_type = VAR_STRING;
4765 if (mch_stat((char *)fname, &st) >= 0)
4766 {
4767 perm = vim_strsave((char_u *)"---------");
4768 if (perm != NULL)
4769 {
4770 for (i = 0; i < 9; i++)
4771 {
4772 if (st.st_mode & (1 << (8 - i)))
4773 perm[i] = flags[i % 3];
4774 }
4775 }
4776 }
4777 rettv->vval.v_string = perm;
4778}
4779
4780/*
4781 * "getfsize({fname})" function
4782 */
4783 static void
4784f_getfsize(typval_T *argvars, typval_T *rettv)
4785{
4786 char_u *fname;
4787 stat_T st;
4788
4789 fname = get_tv_string(&argvars[0]);
4790
4791 rettv->v_type = VAR_NUMBER;
4792
4793 if (mch_stat((char *)fname, &st) >= 0)
4794 {
4795 if (mch_isdir(fname))
4796 rettv->vval.v_number = 0;
4797 else
4798 {
4799 rettv->vval.v_number = (varnumber_T)st.st_size;
4800
4801 /* non-perfect check for overflow */
4802 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4803 rettv->vval.v_number = -2;
4804 }
4805 }
4806 else
4807 rettv->vval.v_number = -1;
4808}
4809
4810/*
4811 * "getftime({fname})" function
4812 */
4813 static void
4814f_getftime(typval_T *argvars, typval_T *rettv)
4815{
4816 char_u *fname;
4817 stat_T st;
4818
4819 fname = get_tv_string(&argvars[0]);
4820
4821 if (mch_stat((char *)fname, &st) >= 0)
4822 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4823 else
4824 rettv->vval.v_number = -1;
4825}
4826
4827/*
4828 * "getftype({fname})" function
4829 */
4830 static void
4831f_getftype(typval_T *argvars, typval_T *rettv)
4832{
4833 char_u *fname;
4834 stat_T st;
4835 char_u *type = NULL;
4836 char *t;
4837
4838 fname = get_tv_string(&argvars[0]);
4839
4840 rettv->v_type = VAR_STRING;
4841 if (mch_lstat((char *)fname, &st) >= 0)
4842 {
4843#ifdef S_ISREG
4844 if (S_ISREG(st.st_mode))
4845 t = "file";
4846 else if (S_ISDIR(st.st_mode))
4847 t = "dir";
4848# ifdef S_ISLNK
4849 else if (S_ISLNK(st.st_mode))
4850 t = "link";
4851# endif
4852# ifdef S_ISBLK
4853 else if (S_ISBLK(st.st_mode))
4854 t = "bdev";
4855# endif
4856# ifdef S_ISCHR
4857 else if (S_ISCHR(st.st_mode))
4858 t = "cdev";
4859# endif
4860# ifdef S_ISFIFO
4861 else if (S_ISFIFO(st.st_mode))
4862 t = "fifo";
4863# endif
4864# ifdef S_ISSOCK
4865 else if (S_ISSOCK(st.st_mode))
4866 t = "fifo";
4867# endif
4868 else
4869 t = "other";
4870#else
4871# ifdef S_IFMT
4872 switch (st.st_mode & S_IFMT)
4873 {
4874 case S_IFREG: t = "file"; break;
4875 case S_IFDIR: t = "dir"; break;
4876# ifdef S_IFLNK
4877 case S_IFLNK: t = "link"; break;
4878# endif
4879# ifdef S_IFBLK
4880 case S_IFBLK: t = "bdev"; break;
4881# endif
4882# ifdef S_IFCHR
4883 case S_IFCHR: t = "cdev"; break;
4884# endif
4885# ifdef S_IFIFO
4886 case S_IFIFO: t = "fifo"; break;
4887# endif
4888# ifdef S_IFSOCK
4889 case S_IFSOCK: t = "socket"; break;
4890# endif
4891 default: t = "other";
4892 }
4893# else
4894 if (mch_isdir(fname))
4895 t = "dir";
4896 else
4897 t = "file";
4898# endif
4899#endif
4900 type = vim_strsave((char_u *)t);
4901 }
4902 rettv->vval.v_string = type;
4903}
4904
4905/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004906 * "getjumplist()" function
4907 */
4908 static void
4909f_getjumplist(typval_T *argvars, typval_T *rettv)
4910{
4911#ifdef FEAT_JUMPLIST
4912 win_T *wp;
4913 int i;
4914 list_T *l;
4915 dict_T *d;
4916#endif
4917
4918 if (rettv_list_alloc(rettv) != OK)
4919 return;
4920
4921#ifdef FEAT_JUMPLIST
4922 wp = find_tabwin(&argvars[0], &argvars[1]);
4923 if (wp == NULL)
4924 return;
4925
4926 l = list_alloc();
4927 if (l == NULL)
4928 return;
4929
4930 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4931 return;
4932 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4933
Bram Moolenaar48679742018-02-13 13:33:29 +01004934 cleanup_jumplist(wp, TRUE);
4935
Bram Moolenaar4f505882018-02-10 21:06:32 +01004936 for (i = 0; i < wp->w_jumplistlen; ++i)
4937 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004938 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4939 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004940 if ((d = dict_alloc()) == NULL)
4941 return;
4942 if (list_append_dict(l, d) == FAIL)
4943 return;
4944 dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
4945 NULL);
4946 dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
4947 NULL);
4948# ifdef FEAT_VIRTUALEDIT
4949 dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
4950 NULL);
4951# endif
4952 dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004953 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaar4f505882018-02-10 21:06:32 +01004954 dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
4955 }
4956#endif
4957}
4958
4959/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004960 * "getline(lnum, [end])" function
4961 */
4962 static void
4963f_getline(typval_T *argvars, typval_T *rettv)
4964{
4965 linenr_T lnum;
4966 linenr_T end;
4967 int retlist;
4968
4969 lnum = get_tv_lnum(argvars);
4970 if (argvars[1].v_type == VAR_UNKNOWN)
4971 {
4972 end = 0;
4973 retlist = FALSE;
4974 }
4975 else
4976 {
4977 end = get_tv_lnum(&argvars[1]);
4978 retlist = TRUE;
4979 }
4980
4981 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4982}
4983
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004984#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004985 static void
4986get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4987{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004988 if (what_arg->v_type == VAR_UNKNOWN)
4989 {
4990 if (rettv_list_alloc(rettv) == OK)
4991 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004992 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004993 }
4994 else
4995 {
4996 if (rettv_dict_alloc(rettv) == OK)
4997 if (is_qf || (wp != NULL))
4998 {
4999 if (what_arg->v_type == VAR_DICT)
5000 {
5001 dict_T *d = what_arg->vval.v_dict;
5002
5003 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005004 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005005 }
5006 else
5007 EMSG(_(e_dictreq));
5008 }
5009 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005010}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005011#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005012
5013/*
5014 * "getloclist()" function
5015 */
5016 static void
5017f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5018{
5019#ifdef FEAT_QUICKFIX
5020 win_T *wp;
5021
5022 wp = find_win_by_nr(&argvars[0], NULL);
5023 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5024#endif
5025}
5026
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005027/*
5028 * "getmatches()" function
5029 */
5030 static void
5031f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5032{
5033#ifdef FEAT_SEARCH_EXTRA
5034 dict_T *dict;
5035 matchitem_T *cur = curwin->w_match_head;
5036 int i;
5037
5038 if (rettv_list_alloc(rettv) == OK)
5039 {
5040 while (cur != NULL)
5041 {
5042 dict = dict_alloc();
5043 if (dict == NULL)
5044 return;
5045 if (cur->match.regprog == NULL)
5046 {
5047 /* match added with matchaddpos() */
5048 for (i = 0; i < MAXPOSMATCH; ++i)
5049 {
5050 llpos_T *llpos;
5051 char buf[6];
5052 list_T *l;
5053
5054 llpos = &cur->pos.pos[i];
5055 if (llpos->lnum == 0)
5056 break;
5057 l = list_alloc();
5058 if (l == NULL)
5059 break;
5060 list_append_number(l, (varnumber_T)llpos->lnum);
5061 if (llpos->col > 0)
5062 {
5063 list_append_number(l, (varnumber_T)llpos->col);
5064 list_append_number(l, (varnumber_T)llpos->len);
5065 }
5066 sprintf(buf, "pos%d", i + 1);
5067 dict_add_list(dict, buf, l);
5068 }
5069 }
5070 else
5071 {
5072 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
5073 }
5074 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
5075 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
5076 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
5077# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5078 if (cur->conceal_char)
5079 {
5080 char_u buf[MB_MAXBYTES + 1];
5081
5082 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5083 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5084 }
5085# endif
5086 list_append_dict(rettv->vval.v_list, dict);
5087 cur = cur->next;
5088 }
5089 }
5090#endif
5091}
5092
5093/*
5094 * "getpid()" function
5095 */
5096 static void
5097f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5098{
5099 rettv->vval.v_number = mch_get_pid();
5100}
5101
5102 static void
5103getpos_both(
5104 typval_T *argvars,
5105 typval_T *rettv,
5106 int getcurpos)
5107{
5108 pos_T *fp;
5109 list_T *l;
5110 int fnum = -1;
5111
5112 if (rettv_list_alloc(rettv) == OK)
5113 {
5114 l = rettv->vval.v_list;
5115 if (getcurpos)
5116 fp = &curwin->w_cursor;
5117 else
5118 fp = var2fpos(&argvars[0], TRUE, &fnum);
5119 if (fnum != -1)
5120 list_append_number(l, (varnumber_T)fnum);
5121 else
5122 list_append_number(l, (varnumber_T)0);
5123 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5124 : (varnumber_T)0);
5125 list_append_number(l, (fp != NULL)
5126 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5127 : (varnumber_T)0);
5128 list_append_number(l,
5129#ifdef FEAT_VIRTUALEDIT
5130 (fp != NULL) ? (varnumber_T)fp->coladd :
5131#endif
5132 (varnumber_T)0);
5133 if (getcurpos)
5134 {
5135 update_curswant();
5136 list_append_number(l, curwin->w_curswant == MAXCOL ?
5137 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5138 }
5139 }
5140 else
5141 rettv->vval.v_number = FALSE;
5142}
5143
5144
5145/*
5146 * "getcurpos()" function
5147 */
5148 static void
5149f_getcurpos(typval_T *argvars, typval_T *rettv)
5150{
5151 getpos_both(argvars, rettv, TRUE);
5152}
5153
5154/*
5155 * "getpos(string)" function
5156 */
5157 static void
5158f_getpos(typval_T *argvars, typval_T *rettv)
5159{
5160 getpos_both(argvars, rettv, FALSE);
5161}
5162
5163/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005164 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005165 */
5166 static void
5167f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5168{
5169#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005170 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005171#endif
5172}
5173
5174/*
5175 * "getreg()" function
5176 */
5177 static void
5178f_getreg(typval_T *argvars, typval_T *rettv)
5179{
5180 char_u *strregname;
5181 int regname;
5182 int arg2 = FALSE;
5183 int return_list = FALSE;
5184 int error = FALSE;
5185
5186 if (argvars[0].v_type != VAR_UNKNOWN)
5187 {
5188 strregname = get_tv_string_chk(&argvars[0]);
5189 error = strregname == NULL;
5190 if (argvars[1].v_type != VAR_UNKNOWN)
5191 {
5192 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5193 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5194 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5195 }
5196 }
5197 else
5198 strregname = get_vim_var_str(VV_REG);
5199
5200 if (error)
5201 return;
5202
5203 regname = (strregname == NULL ? '"' : *strregname);
5204 if (regname == 0)
5205 regname = '"';
5206
5207 if (return_list)
5208 {
5209 rettv->v_type = VAR_LIST;
5210 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5211 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5212 if (rettv->vval.v_list == NULL)
5213 (void)rettv_list_alloc(rettv);
5214 else
5215 ++rettv->vval.v_list->lv_refcount;
5216 }
5217 else
5218 {
5219 rettv->v_type = VAR_STRING;
5220 rettv->vval.v_string = get_reg_contents(regname,
5221 arg2 ? GREG_EXPR_SRC : 0);
5222 }
5223}
5224
5225/*
5226 * "getregtype()" function
5227 */
5228 static void
5229f_getregtype(typval_T *argvars, typval_T *rettv)
5230{
5231 char_u *strregname;
5232 int regname;
5233 char_u buf[NUMBUFLEN + 2];
5234 long reglen = 0;
5235
5236 if (argvars[0].v_type != VAR_UNKNOWN)
5237 {
5238 strregname = get_tv_string_chk(&argvars[0]);
5239 if (strregname == NULL) /* type error; errmsg already given */
5240 {
5241 rettv->v_type = VAR_STRING;
5242 rettv->vval.v_string = NULL;
5243 return;
5244 }
5245 }
5246 else
5247 /* Default to v:register */
5248 strregname = get_vim_var_str(VV_REG);
5249
5250 regname = (strregname == NULL ? '"' : *strregname);
5251 if (regname == 0)
5252 regname = '"';
5253
5254 buf[0] = NUL;
5255 buf[1] = NUL;
5256 switch (get_reg_type(regname, &reglen))
5257 {
5258 case MLINE: buf[0] = 'V'; break;
5259 case MCHAR: buf[0] = 'v'; break;
5260 case MBLOCK:
5261 buf[0] = Ctrl_V;
5262 sprintf((char *)buf + 1, "%ld", reglen + 1);
5263 break;
5264 }
5265 rettv->v_type = VAR_STRING;
5266 rettv->vval.v_string = vim_strsave(buf);
5267}
5268
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005269/*
5270 * Returns information (variables, options, etc.) about a tab page
5271 * as a dictionary.
5272 */
5273 static dict_T *
5274get_tabpage_info(tabpage_T *tp, int tp_idx)
5275{
5276 win_T *wp;
5277 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005278 list_T *l;
5279
5280 dict = dict_alloc();
5281 if (dict == NULL)
5282 return NULL;
5283
Bram Moolenaar33928832016-08-18 21:22:04 +02005284 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005285
5286 l = list_alloc();
5287 if (l != NULL)
5288 {
5289 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5290 wp; wp = wp->w_next)
5291 list_append_number(l, (varnumber_T)wp->w_id);
5292 dict_add_list(dict, "windows", l);
5293 }
5294
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005295 /* Make a reference to tabpage variables */
5296 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005297
5298 return dict;
5299}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005300
5301/*
5302 * "gettabinfo()" function
5303 */
5304 static void
5305f_gettabinfo(typval_T *argvars, typval_T *rettv)
5306{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005307 tabpage_T *tp, *tparg = NULL;
5308 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005309 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005310
5311 if (rettv_list_alloc(rettv) != OK)
5312 return;
5313
5314 if (argvars[0].v_type != VAR_UNKNOWN)
5315 {
5316 /* Information about one tab page */
5317 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5318 if (tparg == NULL)
5319 return;
5320 }
5321
5322 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005323 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005324 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005325 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005326 if (tparg != NULL && tp != tparg)
5327 continue;
5328 d = get_tabpage_info(tp, tpnr);
5329 if (d != NULL)
5330 list_append_dict(rettv->vval.v_list, d);
5331 if (tparg != NULL)
5332 return;
5333 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005334}
5335
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005336/*
5337 * "gettabvar()" function
5338 */
5339 static void
5340f_gettabvar(typval_T *argvars, typval_T *rettv)
5341{
5342 win_T *oldcurwin;
5343 tabpage_T *tp, *oldtabpage;
5344 dictitem_T *v;
5345 char_u *varname;
5346 int done = FALSE;
5347
5348 rettv->v_type = VAR_STRING;
5349 rettv->vval.v_string = NULL;
5350
5351 varname = get_tv_string_chk(&argvars[1]);
5352 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5353 if (tp != NULL && varname != NULL)
5354 {
5355 /* Set tp to be our tabpage, temporarily. Also set the window to the
5356 * first window in the tabpage, otherwise the window is not valid. */
5357 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005358 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5359 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005360 {
5361 /* look up the variable */
5362 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5363 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5364 if (v != NULL)
5365 {
5366 copy_tv(&v->di_tv, rettv);
5367 done = TRUE;
5368 }
5369 }
5370
5371 /* restore previous notion of curwin */
5372 restore_win(oldcurwin, oldtabpage, TRUE);
5373 }
5374
5375 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5376 copy_tv(&argvars[2], rettv);
5377}
5378
5379/*
5380 * "gettabwinvar()" function
5381 */
5382 static void
5383f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5384{
5385 getwinvar(argvars, rettv, 1);
5386}
5387
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005388/*
5389 * Returns information about a window as a dictionary.
5390 */
5391 static dict_T *
5392get_win_info(win_T *wp, short tpnr, short winnr)
5393{
5394 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005395
5396 dict = dict_alloc();
5397 if (dict == NULL)
5398 return NULL;
5399
Bram Moolenaar33928832016-08-18 21:22:04 +02005400 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5401 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005402 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5403 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005404#ifdef FEAT_MENU
5405 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5406#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005407 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005408 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005409
Bram Moolenaar69905d12017-08-13 18:14:47 +02005410#ifdef FEAT_TERMINAL
5411 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5412#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005413#ifdef FEAT_QUICKFIX
5414 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5415 dict_add_nr_str(dict, "loclist",
5416 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5417#endif
5418
Bram Moolenaar30567352016-08-27 21:25:44 +02005419 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005420 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005421
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005422 return dict;
5423}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005424
5425/*
5426 * "getwininfo()" function
5427 */
5428 static void
5429f_getwininfo(typval_T *argvars, typval_T *rettv)
5430{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005431 tabpage_T *tp;
5432 win_T *wp = NULL, *wparg = NULL;
5433 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005434 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005435
5436 if (rettv_list_alloc(rettv) != OK)
5437 return;
5438
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005439 if (argvars[0].v_type != VAR_UNKNOWN)
5440 {
5441 wparg = win_id2wp(argvars);
5442 if (wparg == NULL)
5443 return;
5444 }
5445
5446 /* Collect information about either all the windows across all the tab
5447 * pages or one particular window.
5448 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005449 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005450 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005451 tabnr++;
5452 winnr = 0;
5453 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005454 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005455 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005456 if (wparg != NULL && wp != wparg)
5457 continue;
5458 d = get_win_info(wp, tabnr, winnr);
5459 if (d != NULL)
5460 list_append_dict(rettv->vval.v_list, d);
5461 if (wparg != NULL)
5462 /* found information about a specific window */
5463 return;
5464 }
5465 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005466}
5467
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005468/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005469 * "win_findbuf()" function
5470 */
5471 static void
5472f_win_findbuf(typval_T *argvars, typval_T *rettv)
5473{
5474 if (rettv_list_alloc(rettv) != FAIL)
5475 win_findbuf(argvars, rettv->vval.v_list);
5476}
5477
5478/*
5479 * "win_getid()" function
5480 */
5481 static void
5482f_win_getid(typval_T *argvars, typval_T *rettv)
5483{
5484 rettv->vval.v_number = win_getid(argvars);
5485}
5486
5487/*
5488 * "win_gotoid()" function
5489 */
5490 static void
5491f_win_gotoid(typval_T *argvars, typval_T *rettv)
5492{
5493 rettv->vval.v_number = win_gotoid(argvars);
5494}
5495
5496/*
5497 * "win_id2tabwin()" function
5498 */
5499 static void
5500f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5501{
5502 if (rettv_list_alloc(rettv) != FAIL)
5503 win_id2tabwin(argvars, rettv->vval.v_list);
5504}
5505
5506/*
5507 * "win_id2win()" function
5508 */
5509 static void
5510f_win_id2win(typval_T *argvars, typval_T *rettv)
5511{
5512 rettv->vval.v_number = win_id2win(argvars);
5513}
5514
5515/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005516 * "win_screenpos()" function
5517 */
5518 static void
5519f_win_screenpos(typval_T *argvars, typval_T *rettv)
5520{
5521 win_T *wp;
5522
5523 if (rettv_list_alloc(rettv) == FAIL)
5524 return;
5525
5526 wp = find_win_by_nr(&argvars[0], NULL);
5527 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5528 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5529}
5530
5531/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005532 * "getwinpos({timeout})" function
5533 */
5534 static void
5535f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5536{
5537 int x = -1;
5538 int y = -1;
5539
5540 if (rettv_list_alloc(rettv) == FAIL)
5541 return;
5542#ifdef FEAT_GUI
5543 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005544 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005545# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5546 else
5547# endif
5548#endif
5549#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5550 {
5551 varnumber_T timeout = 100;
5552
5553 if (argvars[0].v_type != VAR_UNKNOWN)
5554 timeout = get_tv_number(&argvars[0]);
5555 term_get_winpos(&x, &y, timeout);
5556 }
5557#endif
5558 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5559 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5560}
5561
5562
5563/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005564 * "getwinposx()" function
5565 */
5566 static void
5567f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5568{
5569 rettv->vval.v_number = -1;
5570#ifdef FEAT_GUI
5571 if (gui.in_use)
5572 {
5573 int x, y;
5574
5575 if (gui_mch_get_winpos(&x, &y) == OK)
5576 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005577 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005578 }
5579#endif
5580#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5581 {
5582 int x, y;
5583
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005584 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005585 rettv->vval.v_number = x;
5586 }
5587#endif
5588}
5589
5590/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005591 * "getwinposy()" function
5592 */
5593 static void
5594f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5595{
5596 rettv->vval.v_number = -1;
5597#ifdef FEAT_GUI
5598 if (gui.in_use)
5599 {
5600 int x, y;
5601
5602 if (gui_mch_get_winpos(&x, &y) == OK)
5603 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005604 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005605 }
5606#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005607#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5608 {
5609 int x, y;
5610
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005611 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005612 rettv->vval.v_number = y;
5613 }
5614#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005615}
5616
5617/*
5618 * "getwinvar()" function
5619 */
5620 static void
5621f_getwinvar(typval_T *argvars, typval_T *rettv)
5622{
5623 getwinvar(argvars, rettv, 0);
5624}
5625
5626/*
5627 * "glob()" function
5628 */
5629 static void
5630f_glob(typval_T *argvars, typval_T *rettv)
5631{
5632 int options = WILD_SILENT|WILD_USE_NL;
5633 expand_T xpc;
5634 int error = FALSE;
5635
5636 /* When the optional second argument is non-zero, don't remove matches
5637 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5638 rettv->v_type = VAR_STRING;
5639 if (argvars[1].v_type != VAR_UNKNOWN)
5640 {
5641 if (get_tv_number_chk(&argvars[1], &error))
5642 options |= WILD_KEEP_ALL;
5643 if (argvars[2].v_type != VAR_UNKNOWN)
5644 {
5645 if (get_tv_number_chk(&argvars[2], &error))
5646 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005647 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005648 }
5649 if (argvars[3].v_type != VAR_UNKNOWN
5650 && get_tv_number_chk(&argvars[3], &error))
5651 options |= WILD_ALLLINKS;
5652 }
5653 }
5654 if (!error)
5655 {
5656 ExpandInit(&xpc);
5657 xpc.xp_context = EXPAND_FILES;
5658 if (p_wic)
5659 options += WILD_ICASE;
5660 if (rettv->v_type == VAR_STRING)
5661 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5662 NULL, options, WILD_ALL);
5663 else if (rettv_list_alloc(rettv) != FAIL)
5664 {
5665 int i;
5666
5667 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5668 NULL, options, WILD_ALL_KEEP);
5669 for (i = 0; i < xpc.xp_numfiles; i++)
5670 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5671
5672 ExpandCleanup(&xpc);
5673 }
5674 }
5675 else
5676 rettv->vval.v_string = NULL;
5677}
5678
5679/*
5680 * "globpath()" function
5681 */
5682 static void
5683f_globpath(typval_T *argvars, typval_T *rettv)
5684{
5685 int flags = 0;
5686 char_u buf1[NUMBUFLEN];
5687 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5688 int error = FALSE;
5689 garray_T ga;
5690 int i;
5691
5692 /* When the optional second argument is non-zero, don't remove matches
5693 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5694 rettv->v_type = VAR_STRING;
5695 if (argvars[2].v_type != VAR_UNKNOWN)
5696 {
5697 if (get_tv_number_chk(&argvars[2], &error))
5698 flags |= WILD_KEEP_ALL;
5699 if (argvars[3].v_type != VAR_UNKNOWN)
5700 {
5701 if (get_tv_number_chk(&argvars[3], &error))
5702 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005703 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005704 }
5705 if (argvars[4].v_type != VAR_UNKNOWN
5706 && get_tv_number_chk(&argvars[4], &error))
5707 flags |= WILD_ALLLINKS;
5708 }
5709 }
5710 if (file != NULL && !error)
5711 {
5712 ga_init2(&ga, (int)sizeof(char_u *), 10);
5713 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5714 if (rettv->v_type == VAR_STRING)
5715 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5716 else if (rettv_list_alloc(rettv) != FAIL)
5717 for (i = 0; i < ga.ga_len; ++i)
5718 list_append_string(rettv->vval.v_list,
5719 ((char_u **)(ga.ga_data))[i], -1);
5720 ga_clear_strings(&ga);
5721 }
5722 else
5723 rettv->vval.v_string = NULL;
5724}
5725
5726/*
5727 * "glob2regpat()" function
5728 */
5729 static void
5730f_glob2regpat(typval_T *argvars, typval_T *rettv)
5731{
5732 char_u *pat = get_tv_string_chk(&argvars[0]);
5733
5734 rettv->v_type = VAR_STRING;
5735 rettv->vval.v_string = (pat == NULL)
5736 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5737}
5738
5739/* for VIM_VERSION_ defines */
5740#include "version.h"
5741
5742/*
5743 * "has()" function
5744 */
5745 static void
5746f_has(typval_T *argvars, typval_T *rettv)
5747{
5748 int i;
5749 char_u *name;
5750 int n = FALSE;
5751 static char *(has_list[]) =
5752 {
5753#ifdef AMIGA
5754 "amiga",
5755# ifdef FEAT_ARP
5756 "arp",
5757# endif
5758#endif
5759#ifdef __BEOS__
5760 "beos",
5761#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005762#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005763 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5764 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005765# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005766 "macunix", /* Mac OS X, with the darwin feature */
5767 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005768# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005769#endif
5770#ifdef __QNX__
5771 "qnx",
5772#endif
5773#ifdef UNIX
5774 "unix",
5775#endif
5776#ifdef VMS
5777 "vms",
5778#endif
5779#ifdef WIN32
5780 "win32",
5781#endif
5782#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5783 "win32unix",
5784#endif
5785#if defined(WIN64) || defined(_WIN64)
5786 "win64",
5787#endif
5788#ifdef EBCDIC
5789 "ebcdic",
5790#endif
5791#ifndef CASE_INSENSITIVE_FILENAME
5792 "fname_case",
5793#endif
5794#ifdef HAVE_ACL
5795 "acl",
5796#endif
5797#ifdef FEAT_ARABIC
5798 "arabic",
5799#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005800 "autocmd",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005801#ifdef FEAT_AUTOSERVERNAME
5802 "autoservername",
5803#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005804#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005805 "balloon_eval",
5806# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5807 "balloon_multiline",
5808# endif
5809#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005810#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005811 "balloon_eval_term",
5812#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005813#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5814 "builtin_terms",
5815# ifdef ALL_BUILTIN_TCAPS
5816 "all_builtin_terms",
5817# endif
5818#endif
5819#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5820 || defined(FEAT_GUI_W32) \
5821 || defined(FEAT_GUI_MOTIF))
5822 "browsefilter",
5823#endif
5824#ifdef FEAT_BYTEOFF
5825 "byte_offset",
5826#endif
5827#ifdef FEAT_JOB_CHANNEL
5828 "channel",
5829#endif
5830#ifdef FEAT_CINDENT
5831 "cindent",
5832#endif
5833#ifdef FEAT_CLIENTSERVER
5834 "clientserver",
5835#endif
5836#ifdef FEAT_CLIPBOARD
5837 "clipboard",
5838#endif
5839#ifdef FEAT_CMDL_COMPL
5840 "cmdline_compl",
5841#endif
5842#ifdef FEAT_CMDHIST
5843 "cmdline_hist",
5844#endif
5845#ifdef FEAT_COMMENTS
5846 "comments",
5847#endif
5848#ifdef FEAT_CONCEAL
5849 "conceal",
5850#endif
5851#ifdef FEAT_CRYPT
5852 "cryptv",
5853 "crypt-blowfish",
5854 "crypt-blowfish2",
5855#endif
5856#ifdef FEAT_CSCOPE
5857 "cscope",
5858#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005859 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005860#ifdef CURSOR_SHAPE
5861 "cursorshape",
5862#endif
5863#ifdef DEBUG
5864 "debug",
5865#endif
5866#ifdef FEAT_CON_DIALOG
5867 "dialog_con",
5868#endif
5869#ifdef FEAT_GUI_DIALOG
5870 "dialog_gui",
5871#endif
5872#ifdef FEAT_DIFF
5873 "diff",
5874#endif
5875#ifdef FEAT_DIGRAPHS
5876 "digraphs",
5877#endif
5878#ifdef FEAT_DIRECTX
5879 "directx",
5880#endif
5881#ifdef FEAT_DND
5882 "dnd",
5883#endif
5884#ifdef FEAT_EMACS_TAGS
5885 "emacs_tags",
5886#endif
5887 "eval", /* always present, of course! */
5888 "ex_extra", /* graduated feature */
5889#ifdef FEAT_SEARCH_EXTRA
5890 "extra_search",
5891#endif
5892#ifdef FEAT_FKMAP
5893 "farsi",
5894#endif
5895#ifdef FEAT_SEARCHPATH
5896 "file_in_path",
5897#endif
5898#ifdef FEAT_FILTERPIPE
5899 "filterpipe",
5900#endif
5901#ifdef FEAT_FIND_ID
5902 "find_in_path",
5903#endif
5904#ifdef FEAT_FLOAT
5905 "float",
5906#endif
5907#ifdef FEAT_FOLDING
5908 "folding",
5909#endif
5910#ifdef FEAT_FOOTER
5911 "footer",
5912#endif
5913#if !defined(USE_SYSTEM) && defined(UNIX)
5914 "fork",
5915#endif
5916#ifdef FEAT_GETTEXT
5917 "gettext",
5918#endif
5919#ifdef FEAT_GUI
5920 "gui",
5921#endif
5922#ifdef FEAT_GUI_ATHENA
5923# ifdef FEAT_GUI_NEXTAW
5924 "gui_neXtaw",
5925# else
5926 "gui_athena",
5927# endif
5928#endif
5929#ifdef FEAT_GUI_GTK
5930 "gui_gtk",
5931# ifdef USE_GTK3
5932 "gui_gtk3",
5933# else
5934 "gui_gtk2",
5935# endif
5936#endif
5937#ifdef FEAT_GUI_GNOME
5938 "gui_gnome",
5939#endif
5940#ifdef FEAT_GUI_MAC
5941 "gui_mac",
5942#endif
5943#ifdef FEAT_GUI_MOTIF
5944 "gui_motif",
5945#endif
5946#ifdef FEAT_GUI_PHOTON
5947 "gui_photon",
5948#endif
5949#ifdef FEAT_GUI_W32
5950 "gui_win32",
5951#endif
5952#ifdef FEAT_HANGULIN
5953 "hangul_input",
5954#endif
5955#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5956 "iconv",
5957#endif
5958#ifdef FEAT_INS_EXPAND
5959 "insert_expand",
5960#endif
5961#ifdef FEAT_JOB_CHANNEL
5962 "job",
5963#endif
5964#ifdef FEAT_JUMPLIST
5965 "jumplist",
5966#endif
5967#ifdef FEAT_KEYMAP
5968 "keymap",
5969#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005970 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005971#ifdef FEAT_LANGMAP
5972 "langmap",
5973#endif
5974#ifdef FEAT_LIBCALL
5975 "libcall",
5976#endif
5977#ifdef FEAT_LINEBREAK
5978 "linebreak",
5979#endif
5980#ifdef FEAT_LISP
5981 "lispindent",
5982#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005983 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005984#ifdef FEAT_LOCALMAP
5985 "localmap",
5986#endif
5987#ifdef FEAT_LUA
5988# ifndef DYNAMIC_LUA
5989 "lua",
5990# endif
5991#endif
5992#ifdef FEAT_MENU
5993 "menu",
5994#endif
5995#ifdef FEAT_SESSION
5996 "mksession",
5997#endif
5998#ifdef FEAT_MODIFY_FNAME
5999 "modify_fname",
6000#endif
6001#ifdef FEAT_MOUSE
6002 "mouse",
6003#endif
6004#ifdef FEAT_MOUSESHAPE
6005 "mouseshape",
6006#endif
6007#if defined(UNIX) || defined(VMS)
6008# ifdef FEAT_MOUSE_DEC
6009 "mouse_dec",
6010# endif
6011# ifdef FEAT_MOUSE_GPM
6012 "mouse_gpm",
6013# endif
6014# ifdef FEAT_MOUSE_JSB
6015 "mouse_jsbterm",
6016# endif
6017# ifdef FEAT_MOUSE_NET
6018 "mouse_netterm",
6019# endif
6020# ifdef FEAT_MOUSE_PTERM
6021 "mouse_pterm",
6022# endif
6023# ifdef FEAT_MOUSE_SGR
6024 "mouse_sgr",
6025# endif
6026# ifdef FEAT_SYSMOUSE
6027 "mouse_sysmouse",
6028# endif
6029# ifdef FEAT_MOUSE_URXVT
6030 "mouse_urxvt",
6031# endif
6032# ifdef FEAT_MOUSE_XTERM
6033 "mouse_xterm",
6034# endif
6035#endif
6036#ifdef FEAT_MBYTE
6037 "multi_byte",
6038#endif
6039#ifdef FEAT_MBYTE_IME
6040 "multi_byte_ime",
6041#endif
6042#ifdef FEAT_MULTI_LANG
6043 "multi_lang",
6044#endif
6045#ifdef FEAT_MZSCHEME
6046#ifndef DYNAMIC_MZSCHEME
6047 "mzscheme",
6048#endif
6049#endif
6050#ifdef FEAT_NUM64
6051 "num64",
6052#endif
6053#ifdef FEAT_OLE
6054 "ole",
6055#endif
6056 "packages",
6057#ifdef FEAT_PATH_EXTRA
6058 "path_extra",
6059#endif
6060#ifdef FEAT_PERL
6061#ifndef DYNAMIC_PERL
6062 "perl",
6063#endif
6064#endif
6065#ifdef FEAT_PERSISTENT_UNDO
6066 "persistent_undo",
6067#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006068#if defined(FEAT_PYTHON)
6069 "python_compiled",
6070# if defined(DYNAMIC_PYTHON)
6071 "python_dynamic",
6072# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006073 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006074 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006075# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006076#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006077#if defined(FEAT_PYTHON3)
6078 "python3_compiled",
6079# if defined(DYNAMIC_PYTHON3)
6080 "python3_dynamic",
6081# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006082 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006083 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006084# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006085#endif
6086#ifdef FEAT_POSTSCRIPT
6087 "postscript",
6088#endif
6089#ifdef FEAT_PRINTER
6090 "printer",
6091#endif
6092#ifdef FEAT_PROFILE
6093 "profile",
6094#endif
6095#ifdef FEAT_RELTIME
6096 "reltime",
6097#endif
6098#ifdef FEAT_QUICKFIX
6099 "quickfix",
6100#endif
6101#ifdef FEAT_RIGHTLEFT
6102 "rightleft",
6103#endif
6104#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6105 "ruby",
6106#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006107 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006108#ifdef FEAT_CMDL_INFO
6109 "showcmd",
6110 "cmdline_info",
6111#endif
6112#ifdef FEAT_SIGNS
6113 "signs",
6114#endif
6115#ifdef FEAT_SMARTINDENT
6116 "smartindent",
6117#endif
6118#ifdef STARTUPTIME
6119 "startuptime",
6120#endif
6121#ifdef FEAT_STL_OPT
6122 "statusline",
6123#endif
6124#ifdef FEAT_SUN_WORKSHOP
6125 "sun_workshop",
6126#endif
6127#ifdef FEAT_NETBEANS_INTG
6128 "netbeans_intg",
6129#endif
6130#ifdef FEAT_SPELL
6131 "spell",
6132#endif
6133#ifdef FEAT_SYN_HL
6134 "syntax",
6135#endif
6136#if defined(USE_SYSTEM) || !defined(UNIX)
6137 "system",
6138#endif
6139#ifdef FEAT_TAG_BINS
6140 "tag_binary",
6141#endif
6142#ifdef FEAT_TAG_OLDSTATIC
6143 "tag_old_static",
6144#endif
6145#ifdef FEAT_TAG_ANYWHITE
6146 "tag_any_white",
6147#endif
6148#ifdef FEAT_TCL
6149# ifndef DYNAMIC_TCL
6150 "tcl",
6151# endif
6152#endif
6153#ifdef FEAT_TERMGUICOLORS
6154 "termguicolors",
6155#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006156#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006157 "terminal",
6158#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006159#ifdef TERMINFO
6160 "terminfo",
6161#endif
6162#ifdef FEAT_TERMRESPONSE
6163 "termresponse",
6164#endif
6165#ifdef FEAT_TEXTOBJ
6166 "textobjects",
6167#endif
6168#ifdef HAVE_TGETENT
6169 "tgetent",
6170#endif
6171#ifdef FEAT_TIMERS
6172 "timers",
6173#endif
6174#ifdef FEAT_TITLE
6175 "title",
6176#endif
6177#ifdef FEAT_TOOLBAR
6178 "toolbar",
6179#endif
6180#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6181 "unnamedplus",
6182#endif
6183#ifdef FEAT_USR_CMDS
6184 "user-commands", /* was accidentally included in 5.4 */
6185 "user_commands",
6186#endif
6187#ifdef FEAT_VIMINFO
6188 "viminfo",
6189#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006190 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006191#ifdef FEAT_VIRTUALEDIT
6192 "virtualedit",
6193#endif
6194 "visual",
6195#ifdef FEAT_VISUALEXTRA
6196 "visualextra",
6197#endif
6198#ifdef FEAT_VREPLACE
6199 "vreplace",
6200#endif
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006201#ifdef FEAT_VTP
6202 "vtp",
6203#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006204#ifdef FEAT_WILDIGN
6205 "wildignore",
6206#endif
6207#ifdef FEAT_WILDMENU
6208 "wildmenu",
6209#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006210 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006211#ifdef FEAT_WAK
6212 "winaltkeys",
6213#endif
6214#ifdef FEAT_WRITEBACKUP
6215 "writebackup",
6216#endif
6217#ifdef FEAT_XIM
6218 "xim",
6219#endif
6220#ifdef FEAT_XFONTSET
6221 "xfontset",
6222#endif
6223#ifdef FEAT_XPM_W32
6224 "xpm",
6225 "xpm_w32", /* for backward compatibility */
6226#else
6227# if defined(HAVE_XPM)
6228 "xpm",
6229# endif
6230#endif
6231#ifdef USE_XSMP
6232 "xsmp",
6233#endif
6234#ifdef USE_XSMP_INTERACT
6235 "xsmp_interact",
6236#endif
6237#ifdef FEAT_XCLIPBOARD
6238 "xterm_clipboard",
6239#endif
6240#ifdef FEAT_XTERM_SAVE
6241 "xterm_save",
6242#endif
6243#if defined(UNIX) && defined(FEAT_X11)
6244 "X11",
6245#endif
6246 NULL
6247 };
6248
6249 name = get_tv_string(&argvars[0]);
6250 for (i = 0; has_list[i] != NULL; ++i)
6251 if (STRICMP(name, has_list[i]) == 0)
6252 {
6253 n = TRUE;
6254 break;
6255 }
6256
6257 if (n == FALSE)
6258 {
6259 if (STRNICMP(name, "patch", 5) == 0)
6260 {
6261 if (name[5] == '-'
6262 && STRLEN(name) >= 11
6263 && vim_isdigit(name[6])
6264 && vim_isdigit(name[8])
6265 && vim_isdigit(name[10]))
6266 {
6267 int major = atoi((char *)name + 6);
6268 int minor = atoi((char *)name + 8);
6269
6270 /* Expect "patch-9.9.01234". */
6271 n = (major < VIM_VERSION_MAJOR
6272 || (major == VIM_VERSION_MAJOR
6273 && (minor < VIM_VERSION_MINOR
6274 || (minor == VIM_VERSION_MINOR
6275 && has_patch(atoi((char *)name + 10))))));
6276 }
6277 else
6278 n = has_patch(atoi((char *)name + 5));
6279 }
6280 else if (STRICMP(name, "vim_starting") == 0)
6281 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006282 else if (STRICMP(name, "ttyin") == 0)
6283 n = mch_input_isatty();
6284 else if (STRICMP(name, "ttyout") == 0)
6285 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006286#ifdef FEAT_MBYTE
6287 else if (STRICMP(name, "multi_byte_encoding") == 0)
6288 n = has_mbyte;
6289#endif
6290#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6291 else if (STRICMP(name, "balloon_multiline") == 0)
6292 n = multiline_balloon_available();
6293#endif
6294#ifdef DYNAMIC_TCL
6295 else if (STRICMP(name, "tcl") == 0)
6296 n = tcl_enabled(FALSE);
6297#endif
6298#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6299 else if (STRICMP(name, "iconv") == 0)
6300 n = iconv_enabled(FALSE);
6301#endif
6302#ifdef DYNAMIC_LUA
6303 else if (STRICMP(name, "lua") == 0)
6304 n = lua_enabled(FALSE);
6305#endif
6306#ifdef DYNAMIC_MZSCHEME
6307 else if (STRICMP(name, "mzscheme") == 0)
6308 n = mzscheme_enabled(FALSE);
6309#endif
6310#ifdef DYNAMIC_RUBY
6311 else if (STRICMP(name, "ruby") == 0)
6312 n = ruby_enabled(FALSE);
6313#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006314#ifdef DYNAMIC_PYTHON
6315 else if (STRICMP(name, "python") == 0)
6316 n = python_enabled(FALSE);
6317#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006318#ifdef DYNAMIC_PYTHON3
6319 else if (STRICMP(name, "python3") == 0)
6320 n = python3_enabled(FALSE);
6321#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006322#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6323 else if (STRICMP(name, "pythonx") == 0)
6324 {
6325# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6326 if (p_pyx == 0)
6327 n = python3_enabled(FALSE) || python_enabled(FALSE);
6328 else if (p_pyx == 3)
6329 n = python3_enabled(FALSE);
6330 else if (p_pyx == 2)
6331 n = python_enabled(FALSE);
6332# elif defined(DYNAMIC_PYTHON)
6333 n = python_enabled(FALSE);
6334# elif defined(DYNAMIC_PYTHON3)
6335 n = python3_enabled(FALSE);
6336# endif
6337 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006338#endif
6339#ifdef DYNAMIC_PERL
6340 else if (STRICMP(name, "perl") == 0)
6341 n = perl_enabled(FALSE);
6342#endif
6343#ifdef FEAT_GUI
6344 else if (STRICMP(name, "gui_running") == 0)
6345 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006346# ifdef FEAT_BROWSE
6347 else if (STRICMP(name, "browse") == 0)
6348 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6349# endif
6350#endif
6351#ifdef FEAT_SYN_HL
6352 else if (STRICMP(name, "syntax_items") == 0)
6353 n = syntax_present(curwin);
6354#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006355#ifdef FEAT_VTP
6356 else if (STRICMP(name, "vcon") == 0)
6357 n = has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006358#endif
6359#ifdef FEAT_NETBEANS_INTG
6360 else if (STRICMP(name, "netbeans_enabled") == 0)
6361 n = netbeans_active();
6362#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006363#if defined(FEAT_TERMINAL) && defined(WIN3264)
6364 else if (STRICMP(name, "terminal") == 0)
6365 n = terminal_enabled();
6366#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006367 }
6368
6369 rettv->vval.v_number = n;
6370}
6371
6372/*
6373 * "has_key()" function
6374 */
6375 static void
6376f_has_key(typval_T *argvars, typval_T *rettv)
6377{
6378 if (argvars[0].v_type != VAR_DICT)
6379 {
6380 EMSG(_(e_dictreq));
6381 return;
6382 }
6383 if (argvars[0].vval.v_dict == NULL)
6384 return;
6385
6386 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6387 get_tv_string(&argvars[1]), -1) != NULL;
6388}
6389
6390/*
6391 * "haslocaldir()" function
6392 */
6393 static void
6394f_haslocaldir(typval_T *argvars, typval_T *rettv)
6395{
6396 win_T *wp = NULL;
6397
6398 wp = find_tabwin(&argvars[0], &argvars[1]);
6399 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6400}
6401
6402/*
6403 * "hasmapto()" function
6404 */
6405 static void
6406f_hasmapto(typval_T *argvars, typval_T *rettv)
6407{
6408 char_u *name;
6409 char_u *mode;
6410 char_u buf[NUMBUFLEN];
6411 int abbr = FALSE;
6412
6413 name = get_tv_string(&argvars[0]);
6414 if (argvars[1].v_type == VAR_UNKNOWN)
6415 mode = (char_u *)"nvo";
6416 else
6417 {
6418 mode = get_tv_string_buf(&argvars[1], buf);
6419 if (argvars[2].v_type != VAR_UNKNOWN)
6420 abbr = (int)get_tv_number(&argvars[2]);
6421 }
6422
6423 if (map_to_exists(name, mode, abbr))
6424 rettv->vval.v_number = TRUE;
6425 else
6426 rettv->vval.v_number = FALSE;
6427}
6428
6429/*
6430 * "histadd()" function
6431 */
6432 static void
6433f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6434{
6435#ifdef FEAT_CMDHIST
6436 int histype;
6437 char_u *str;
6438 char_u buf[NUMBUFLEN];
6439#endif
6440
6441 rettv->vval.v_number = FALSE;
6442 if (check_restricted() || check_secure())
6443 return;
6444#ifdef FEAT_CMDHIST
6445 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6446 histype = str != NULL ? get_histtype(str) : -1;
6447 if (histype >= 0)
6448 {
6449 str = get_tv_string_buf(&argvars[1], buf);
6450 if (*str != NUL)
6451 {
6452 init_history();
6453 add_to_history(histype, str, FALSE, NUL);
6454 rettv->vval.v_number = TRUE;
6455 return;
6456 }
6457 }
6458#endif
6459}
6460
6461/*
6462 * "histdel()" function
6463 */
6464 static void
6465f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6466{
6467#ifdef FEAT_CMDHIST
6468 int n;
6469 char_u buf[NUMBUFLEN];
6470 char_u *str;
6471
6472 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6473 if (str == NULL)
6474 n = 0;
6475 else if (argvars[1].v_type == VAR_UNKNOWN)
6476 /* only one argument: clear entire history */
6477 n = clr_history(get_histtype(str));
6478 else if (argvars[1].v_type == VAR_NUMBER)
6479 /* index given: remove that entry */
6480 n = del_history_idx(get_histtype(str),
6481 (int)get_tv_number(&argvars[1]));
6482 else
6483 /* string given: remove all matching entries */
6484 n = del_history_entry(get_histtype(str),
6485 get_tv_string_buf(&argvars[1], buf));
6486 rettv->vval.v_number = n;
6487#endif
6488}
6489
6490/*
6491 * "histget()" function
6492 */
6493 static void
6494f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6495{
6496#ifdef FEAT_CMDHIST
6497 int type;
6498 int idx;
6499 char_u *str;
6500
6501 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6502 if (str == NULL)
6503 rettv->vval.v_string = NULL;
6504 else
6505 {
6506 type = get_histtype(str);
6507 if (argvars[1].v_type == VAR_UNKNOWN)
6508 idx = get_history_idx(type);
6509 else
6510 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6511 /* -1 on type error */
6512 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6513 }
6514#else
6515 rettv->vval.v_string = NULL;
6516#endif
6517 rettv->v_type = VAR_STRING;
6518}
6519
6520/*
6521 * "histnr()" function
6522 */
6523 static void
6524f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6525{
6526 int i;
6527
6528#ifdef FEAT_CMDHIST
6529 char_u *history = get_tv_string_chk(&argvars[0]);
6530
6531 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6532 if (i >= HIST_CMD && i < HIST_COUNT)
6533 i = get_history_idx(i);
6534 else
6535#endif
6536 i = -1;
6537 rettv->vval.v_number = i;
6538}
6539
6540/*
6541 * "highlightID(name)" function
6542 */
6543 static void
6544f_hlID(typval_T *argvars, typval_T *rettv)
6545{
6546 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6547}
6548
6549/*
6550 * "highlight_exists()" function
6551 */
6552 static void
6553f_hlexists(typval_T *argvars, typval_T *rettv)
6554{
6555 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6556}
6557
6558/*
6559 * "hostname()" function
6560 */
6561 static void
6562f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6563{
6564 char_u hostname[256];
6565
6566 mch_get_host_name(hostname, 256);
6567 rettv->v_type = VAR_STRING;
6568 rettv->vval.v_string = vim_strsave(hostname);
6569}
6570
6571/*
6572 * iconv() function
6573 */
6574 static void
6575f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6576{
6577#ifdef FEAT_MBYTE
6578 char_u buf1[NUMBUFLEN];
6579 char_u buf2[NUMBUFLEN];
6580 char_u *from, *to, *str;
6581 vimconv_T vimconv;
6582#endif
6583
6584 rettv->v_type = VAR_STRING;
6585 rettv->vval.v_string = NULL;
6586
6587#ifdef FEAT_MBYTE
6588 str = get_tv_string(&argvars[0]);
6589 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6590 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6591 vimconv.vc_type = CONV_NONE;
6592 convert_setup(&vimconv, from, to);
6593
6594 /* If the encodings are equal, no conversion needed. */
6595 if (vimconv.vc_type == CONV_NONE)
6596 rettv->vval.v_string = vim_strsave(str);
6597 else
6598 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6599
6600 convert_setup(&vimconv, NULL, NULL);
6601 vim_free(from);
6602 vim_free(to);
6603#endif
6604}
6605
6606/*
6607 * "indent()" function
6608 */
6609 static void
6610f_indent(typval_T *argvars, typval_T *rettv)
6611{
6612 linenr_T lnum;
6613
6614 lnum = get_tv_lnum(argvars);
6615 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6616 rettv->vval.v_number = get_indent_lnum(lnum);
6617 else
6618 rettv->vval.v_number = -1;
6619}
6620
6621/*
6622 * "index()" function
6623 */
6624 static void
6625f_index(typval_T *argvars, typval_T *rettv)
6626{
6627 list_T *l;
6628 listitem_T *item;
6629 long idx = 0;
6630 int ic = FALSE;
6631
6632 rettv->vval.v_number = -1;
6633 if (argvars[0].v_type != VAR_LIST)
6634 {
6635 EMSG(_(e_listreq));
6636 return;
6637 }
6638 l = argvars[0].vval.v_list;
6639 if (l != NULL)
6640 {
6641 item = l->lv_first;
6642 if (argvars[2].v_type != VAR_UNKNOWN)
6643 {
6644 int error = FALSE;
6645
6646 /* Start at specified item. Use the cached index that list_find()
6647 * sets, so that a negative number also works. */
6648 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6649 idx = l->lv_idx;
6650 if (argvars[3].v_type != VAR_UNKNOWN)
6651 ic = (int)get_tv_number_chk(&argvars[3], &error);
6652 if (error)
6653 item = NULL;
6654 }
6655
6656 for ( ; item != NULL; item = item->li_next, ++idx)
6657 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6658 {
6659 rettv->vval.v_number = idx;
6660 break;
6661 }
6662 }
6663}
6664
6665static int inputsecret_flag = 0;
6666
6667/*
6668 * "input()" function
6669 * Also handles inputsecret() when inputsecret is set.
6670 */
6671 static void
6672f_input(typval_T *argvars, typval_T *rettv)
6673{
6674 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6675}
6676
6677/*
6678 * "inputdialog()" function
6679 */
6680 static void
6681f_inputdialog(typval_T *argvars, typval_T *rettv)
6682{
6683#if defined(FEAT_GUI_TEXTDIALOG)
6684 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6685 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6686 {
6687 char_u *message;
6688 char_u buf[NUMBUFLEN];
6689 char_u *defstr = (char_u *)"";
6690
6691 message = get_tv_string_chk(&argvars[0]);
6692 if (argvars[1].v_type != VAR_UNKNOWN
6693 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6694 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6695 else
6696 IObuff[0] = NUL;
6697 if (message != NULL && defstr != NULL
6698 && do_dialog(VIM_QUESTION, NULL, message,
6699 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6700 rettv->vval.v_string = vim_strsave(IObuff);
6701 else
6702 {
6703 if (message != NULL && defstr != NULL
6704 && argvars[1].v_type != VAR_UNKNOWN
6705 && argvars[2].v_type != VAR_UNKNOWN)
6706 rettv->vval.v_string = vim_strsave(
6707 get_tv_string_buf(&argvars[2], buf));
6708 else
6709 rettv->vval.v_string = NULL;
6710 }
6711 rettv->v_type = VAR_STRING;
6712 }
6713 else
6714#endif
6715 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6716}
6717
6718/*
6719 * "inputlist()" function
6720 */
6721 static void
6722f_inputlist(typval_T *argvars, typval_T *rettv)
6723{
6724 listitem_T *li;
6725 int selected;
6726 int mouse_used;
6727
6728#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006729 /* While starting up, there is no place to enter text. When running tests
6730 * with --not-a-term we assume feedkeys() will be used. */
6731 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006732 return;
6733#endif
6734 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6735 {
6736 EMSG2(_(e_listarg), "inputlist()");
6737 return;
6738 }
6739
6740 msg_start();
6741 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6742 lines_left = Rows; /* avoid more prompt */
6743 msg_scroll = TRUE;
6744 msg_clr_eos();
6745
6746 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6747 {
6748 msg_puts(get_tv_string(&li->li_tv));
6749 msg_putchar('\n');
6750 }
6751
6752 /* Ask for choice. */
6753 selected = prompt_for_number(&mouse_used);
6754 if (mouse_used)
6755 selected -= lines_left;
6756
6757 rettv->vval.v_number = selected;
6758}
6759
6760
6761static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6762
6763/*
6764 * "inputrestore()" function
6765 */
6766 static void
6767f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6768{
6769 if (ga_userinput.ga_len > 0)
6770 {
6771 --ga_userinput.ga_len;
6772 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6773 + ga_userinput.ga_len);
6774 /* default return is zero == OK */
6775 }
6776 else if (p_verbose > 1)
6777 {
6778 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6779 rettv->vval.v_number = 1; /* Failed */
6780 }
6781}
6782
6783/*
6784 * "inputsave()" function
6785 */
6786 static void
6787f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6788{
6789 /* Add an entry to the stack of typeahead storage. */
6790 if (ga_grow(&ga_userinput, 1) == OK)
6791 {
6792 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6793 + ga_userinput.ga_len);
6794 ++ga_userinput.ga_len;
6795 /* default return is zero == OK */
6796 }
6797 else
6798 rettv->vval.v_number = 1; /* Failed */
6799}
6800
6801/*
6802 * "inputsecret()" function
6803 */
6804 static void
6805f_inputsecret(typval_T *argvars, typval_T *rettv)
6806{
6807 ++cmdline_star;
6808 ++inputsecret_flag;
6809 f_input(argvars, rettv);
6810 --cmdline_star;
6811 --inputsecret_flag;
6812}
6813
6814/*
6815 * "insert()" function
6816 */
6817 static void
6818f_insert(typval_T *argvars, typval_T *rettv)
6819{
6820 long before = 0;
6821 listitem_T *item;
6822 list_T *l;
6823 int error = FALSE;
6824
6825 if (argvars[0].v_type != VAR_LIST)
6826 EMSG2(_(e_listarg), "insert()");
6827 else if ((l = argvars[0].vval.v_list) != NULL
6828 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6829 {
6830 if (argvars[2].v_type != VAR_UNKNOWN)
6831 before = (long)get_tv_number_chk(&argvars[2], &error);
6832 if (error)
6833 return; /* type error; errmsg already given */
6834
6835 if (before == l->lv_len)
6836 item = NULL;
6837 else
6838 {
6839 item = list_find(l, before);
6840 if (item == NULL)
6841 {
6842 EMSGN(_(e_listidx), before);
6843 l = NULL;
6844 }
6845 }
6846 if (l != NULL)
6847 {
6848 list_insert_tv(l, &argvars[1], item);
6849 copy_tv(&argvars[0], rettv);
6850 }
6851 }
6852}
6853
6854/*
6855 * "invert(expr)" function
6856 */
6857 static void
6858f_invert(typval_T *argvars, typval_T *rettv)
6859{
6860 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6861}
6862
6863/*
6864 * "isdirectory()" function
6865 */
6866 static void
6867f_isdirectory(typval_T *argvars, typval_T *rettv)
6868{
6869 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6870}
6871
6872/*
6873 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6874 * or it refers to a List or Dictionary that is locked.
6875 */
6876 static int
6877tv_islocked(typval_T *tv)
6878{
6879 return (tv->v_lock & VAR_LOCKED)
6880 || (tv->v_type == VAR_LIST
6881 && tv->vval.v_list != NULL
6882 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6883 || (tv->v_type == VAR_DICT
6884 && tv->vval.v_dict != NULL
6885 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6886}
6887
6888/*
6889 * "islocked()" function
6890 */
6891 static void
6892f_islocked(typval_T *argvars, typval_T *rettv)
6893{
6894 lval_T lv;
6895 char_u *end;
6896 dictitem_T *di;
6897
6898 rettv->vval.v_number = -1;
6899 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006900 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901 if (end != NULL && lv.ll_name != NULL)
6902 {
6903 if (*end != NUL)
6904 EMSG(_(e_trailing));
6905 else
6906 {
6907 if (lv.ll_tv == NULL)
6908 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006909 di = find_var(lv.ll_name, NULL, TRUE);
6910 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006911 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006912 /* Consider a variable locked when:
6913 * 1. the variable itself is locked
6914 * 2. the value of the variable is locked.
6915 * 3. the List or Dict value is locked.
6916 */
6917 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6918 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006919 }
6920 }
6921 else if (lv.ll_range)
6922 EMSG(_("E786: Range not allowed"));
6923 else if (lv.ll_newkey != NULL)
6924 EMSG2(_(e_dictkey), lv.ll_newkey);
6925 else if (lv.ll_list != NULL)
6926 /* List item. */
6927 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6928 else
6929 /* Dictionary item. */
6930 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6931 }
6932 }
6933
6934 clear_lval(&lv);
6935}
6936
6937#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6938/*
6939 * "isnan()" function
6940 */
6941 static void
6942f_isnan(typval_T *argvars, typval_T *rettv)
6943{
6944 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6945 && isnan(argvars[0].vval.v_float);
6946}
6947#endif
6948
6949/*
6950 * "items(dict)" function
6951 */
6952 static void
6953f_items(typval_T *argvars, typval_T *rettv)
6954{
6955 dict_list(argvars, rettv, 2);
6956}
6957
6958#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6959/*
6960 * Get the job from the argument.
6961 * Returns NULL if the job is invalid.
6962 */
6963 static job_T *
6964get_job_arg(typval_T *tv)
6965{
6966 job_T *job;
6967
6968 if (tv->v_type != VAR_JOB)
6969 {
6970 EMSG2(_(e_invarg2), get_tv_string(tv));
6971 return NULL;
6972 }
6973 job = tv->vval.v_job;
6974
6975 if (job == NULL)
6976 EMSG(_("E916: not a valid job"));
6977 return job;
6978}
6979
6980/*
6981 * "job_getchannel()" function
6982 */
6983 static void
6984f_job_getchannel(typval_T *argvars, typval_T *rettv)
6985{
6986 job_T *job = get_job_arg(&argvars[0]);
6987
6988 if (job != NULL)
6989 {
6990 rettv->v_type = VAR_CHANNEL;
6991 rettv->vval.v_channel = job->jv_channel;
6992 if (job->jv_channel != NULL)
6993 ++job->jv_channel->ch_refcount;
6994 }
6995}
6996
6997/*
6998 * "job_info()" function
6999 */
7000 static void
7001f_job_info(typval_T *argvars, typval_T *rettv)
7002{
7003 job_T *job = get_job_arg(&argvars[0]);
7004
7005 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7006 job_info(job, rettv->vval.v_dict);
7007}
7008
7009/*
7010 * "job_setoptions()" function
7011 */
7012 static void
7013f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7014{
7015 job_T *job = get_job_arg(&argvars[0]);
7016 jobopt_T opt;
7017
7018 if (job == NULL)
7019 return;
7020 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007021 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007022 job_set_options(job, &opt);
7023 free_job_options(&opt);
7024}
7025
7026/*
7027 * "job_start()" function
7028 */
7029 static void
7030f_job_start(typval_T *argvars, typval_T *rettv)
7031{
7032 rettv->v_type = VAR_JOB;
7033 if (check_restricted() || check_secure())
7034 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01007035 rettv->vval.v_job = job_start(argvars, NULL, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007036}
7037
7038/*
7039 * "job_status()" function
7040 */
7041 static void
7042f_job_status(typval_T *argvars, typval_T *rettv)
7043{
7044 job_T *job = get_job_arg(&argvars[0]);
7045
7046 if (job != NULL)
7047 {
7048 rettv->v_type = VAR_STRING;
7049 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7050 }
7051}
7052
7053/*
7054 * "job_stop()" function
7055 */
7056 static void
7057f_job_stop(typval_T *argvars, typval_T *rettv)
7058{
7059 job_T *job = get_job_arg(&argvars[0]);
7060
7061 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007062 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007063}
7064#endif
7065
7066/*
7067 * "join()" function
7068 */
7069 static void
7070f_join(typval_T *argvars, typval_T *rettv)
7071{
7072 garray_T ga;
7073 char_u *sep;
7074
7075 if (argvars[0].v_type != VAR_LIST)
7076 {
7077 EMSG(_(e_listreq));
7078 return;
7079 }
7080 if (argvars[0].vval.v_list == NULL)
7081 return;
7082 if (argvars[1].v_type == VAR_UNKNOWN)
7083 sep = (char_u *)" ";
7084 else
7085 sep = get_tv_string_chk(&argvars[1]);
7086
7087 rettv->v_type = VAR_STRING;
7088
7089 if (sep != NULL)
7090 {
7091 ga_init2(&ga, (int)sizeof(char), 80);
7092 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7093 ga_append(&ga, NUL);
7094 rettv->vval.v_string = (char_u *)ga.ga_data;
7095 }
7096 else
7097 rettv->vval.v_string = NULL;
7098}
7099
7100/*
7101 * "js_decode()" function
7102 */
7103 static void
7104f_js_decode(typval_T *argvars, typval_T *rettv)
7105{
7106 js_read_T reader;
7107
7108 reader.js_buf = get_tv_string(&argvars[0]);
7109 reader.js_fill = NULL;
7110 reader.js_used = 0;
7111 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7112 EMSG(_(e_invarg));
7113}
7114
7115/*
7116 * "js_encode()" function
7117 */
7118 static void
7119f_js_encode(typval_T *argvars, typval_T *rettv)
7120{
7121 rettv->v_type = VAR_STRING;
7122 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7123}
7124
7125/*
7126 * "json_decode()" function
7127 */
7128 static void
7129f_json_decode(typval_T *argvars, typval_T *rettv)
7130{
7131 js_read_T reader;
7132
7133 reader.js_buf = get_tv_string(&argvars[0]);
7134 reader.js_fill = NULL;
7135 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007136 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007137}
7138
7139/*
7140 * "json_encode()" function
7141 */
7142 static void
7143f_json_encode(typval_T *argvars, typval_T *rettv)
7144{
7145 rettv->v_type = VAR_STRING;
7146 rettv->vval.v_string = json_encode(&argvars[0], 0);
7147}
7148
7149/*
7150 * "keys()" function
7151 */
7152 static void
7153f_keys(typval_T *argvars, typval_T *rettv)
7154{
7155 dict_list(argvars, rettv, 0);
7156}
7157
7158/*
7159 * "last_buffer_nr()" function.
7160 */
7161 static void
7162f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7163{
7164 int n = 0;
7165 buf_T *buf;
7166
Bram Moolenaar29323592016-07-24 22:04:11 +02007167 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007168 if (n < buf->b_fnum)
7169 n = buf->b_fnum;
7170
7171 rettv->vval.v_number = n;
7172}
7173
7174/*
7175 * "len()" function
7176 */
7177 static void
7178f_len(typval_T *argvars, typval_T *rettv)
7179{
7180 switch (argvars[0].v_type)
7181 {
7182 case VAR_STRING:
7183 case VAR_NUMBER:
7184 rettv->vval.v_number = (varnumber_T)STRLEN(
7185 get_tv_string(&argvars[0]));
7186 break;
7187 case VAR_LIST:
7188 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7189 break;
7190 case VAR_DICT:
7191 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7192 break;
7193 case VAR_UNKNOWN:
7194 case VAR_SPECIAL:
7195 case VAR_FLOAT:
7196 case VAR_FUNC:
7197 case VAR_PARTIAL:
7198 case VAR_JOB:
7199 case VAR_CHANNEL:
7200 EMSG(_("E701: Invalid type for len()"));
7201 break;
7202 }
7203}
7204
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007205 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007206libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007207{
7208#ifdef FEAT_LIBCALL
7209 char_u *string_in;
7210 char_u **string_result;
7211 int nr_result;
7212#endif
7213
7214 rettv->v_type = type;
7215 if (type != VAR_NUMBER)
7216 rettv->vval.v_string = NULL;
7217
7218 if (check_restricted() || check_secure())
7219 return;
7220
7221#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007222 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007223 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7224 {
7225 string_in = NULL;
7226 if (argvars[2].v_type == VAR_STRING)
7227 string_in = argvars[2].vval.v_string;
7228 if (type == VAR_NUMBER)
7229 string_result = NULL;
7230 else
7231 string_result = &rettv->vval.v_string;
7232 if (mch_libcall(argvars[0].vval.v_string,
7233 argvars[1].vval.v_string,
7234 string_in,
7235 argvars[2].vval.v_number,
7236 string_result,
7237 &nr_result) == OK
7238 && type == VAR_NUMBER)
7239 rettv->vval.v_number = nr_result;
7240 }
7241#endif
7242}
7243
7244/*
7245 * "libcall()" function
7246 */
7247 static void
7248f_libcall(typval_T *argvars, typval_T *rettv)
7249{
7250 libcall_common(argvars, rettv, VAR_STRING);
7251}
7252
7253/*
7254 * "libcallnr()" function
7255 */
7256 static void
7257f_libcallnr(typval_T *argvars, typval_T *rettv)
7258{
7259 libcall_common(argvars, rettv, VAR_NUMBER);
7260}
7261
7262/*
7263 * "line(string)" function
7264 */
7265 static void
7266f_line(typval_T *argvars, typval_T *rettv)
7267{
7268 linenr_T lnum = 0;
7269 pos_T *fp;
7270 int fnum;
7271
7272 fp = var2fpos(&argvars[0], TRUE, &fnum);
7273 if (fp != NULL)
7274 lnum = fp->lnum;
7275 rettv->vval.v_number = lnum;
7276}
7277
7278/*
7279 * "line2byte(lnum)" function
7280 */
7281 static void
7282f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7283{
7284#ifndef FEAT_BYTEOFF
7285 rettv->vval.v_number = -1;
7286#else
7287 linenr_T lnum;
7288
7289 lnum = get_tv_lnum(argvars);
7290 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7291 rettv->vval.v_number = -1;
7292 else
7293 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7294 if (rettv->vval.v_number >= 0)
7295 ++rettv->vval.v_number;
7296#endif
7297}
7298
7299/*
7300 * "lispindent(lnum)" function
7301 */
7302 static void
7303f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7304{
7305#ifdef FEAT_LISP
7306 pos_T pos;
7307 linenr_T lnum;
7308
7309 pos = curwin->w_cursor;
7310 lnum = get_tv_lnum(argvars);
7311 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7312 {
7313 curwin->w_cursor.lnum = lnum;
7314 rettv->vval.v_number = get_lisp_indent();
7315 curwin->w_cursor = pos;
7316 }
7317 else
7318#endif
7319 rettv->vval.v_number = -1;
7320}
7321
7322/*
7323 * "localtime()" function
7324 */
7325 static void
7326f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7327{
7328 rettv->vval.v_number = (varnumber_T)time(NULL);
7329}
7330
7331static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7332
7333 static void
7334get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7335{
7336 char_u *keys;
7337 char_u *which;
7338 char_u buf[NUMBUFLEN];
7339 char_u *keys_buf = NULL;
7340 char_u *rhs;
7341 int mode;
7342 int abbr = FALSE;
7343 int get_dict = FALSE;
7344 mapblock_T *mp;
7345 int buffer_local;
7346
7347 /* return empty string for failure */
7348 rettv->v_type = VAR_STRING;
7349 rettv->vval.v_string = NULL;
7350
7351 keys = get_tv_string(&argvars[0]);
7352 if (*keys == NUL)
7353 return;
7354
7355 if (argvars[1].v_type != VAR_UNKNOWN)
7356 {
7357 which = get_tv_string_buf_chk(&argvars[1], buf);
7358 if (argvars[2].v_type != VAR_UNKNOWN)
7359 {
7360 abbr = (int)get_tv_number(&argvars[2]);
7361 if (argvars[3].v_type != VAR_UNKNOWN)
7362 get_dict = (int)get_tv_number(&argvars[3]);
7363 }
7364 }
7365 else
7366 which = (char_u *)"";
7367 if (which == NULL)
7368 return;
7369
7370 mode = get_map_mode(&which, 0);
7371
7372 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7373 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7374 vim_free(keys_buf);
7375
7376 if (!get_dict)
7377 {
7378 /* Return a string. */
7379 if (rhs != NULL)
7380 rettv->vval.v_string = str2special_save(rhs, FALSE);
7381
7382 }
7383 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7384 {
7385 /* Return a dictionary. */
7386 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7387 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7388 dict_T *dict = rettv->vval.v_dict;
7389
7390 dict_add_nr_str(dict, "lhs", 0L, lhs);
7391 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7392 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7393 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7394 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7395 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7396 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7397 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7398 dict_add_nr_str(dict, "mode", 0L, mapmode);
7399
7400 vim_free(lhs);
7401 vim_free(mapmode);
7402 }
7403}
7404
7405#ifdef FEAT_FLOAT
7406/*
7407 * "log()" function
7408 */
7409 static void
7410f_log(typval_T *argvars, typval_T *rettv)
7411{
7412 float_T f = 0.0;
7413
7414 rettv->v_type = VAR_FLOAT;
7415 if (get_float_arg(argvars, &f) == OK)
7416 rettv->vval.v_float = log(f);
7417 else
7418 rettv->vval.v_float = 0.0;
7419}
7420
7421/*
7422 * "log10()" function
7423 */
7424 static void
7425f_log10(typval_T *argvars, typval_T *rettv)
7426{
7427 float_T f = 0.0;
7428
7429 rettv->v_type = VAR_FLOAT;
7430 if (get_float_arg(argvars, &f) == OK)
7431 rettv->vval.v_float = log10(f);
7432 else
7433 rettv->vval.v_float = 0.0;
7434}
7435#endif
7436
7437#ifdef FEAT_LUA
7438/*
7439 * "luaeval()" function
7440 */
7441 static void
7442f_luaeval(typval_T *argvars, typval_T *rettv)
7443{
7444 char_u *str;
7445 char_u buf[NUMBUFLEN];
7446
7447 str = get_tv_string_buf(&argvars[0], buf);
7448 do_luaeval(str, argvars + 1, rettv);
7449}
7450#endif
7451
7452/*
7453 * "map()" function
7454 */
7455 static void
7456f_map(typval_T *argvars, typval_T *rettv)
7457{
7458 filter_map(argvars, rettv, TRUE);
7459}
7460
7461/*
7462 * "maparg()" function
7463 */
7464 static void
7465f_maparg(typval_T *argvars, typval_T *rettv)
7466{
7467 get_maparg(argvars, rettv, TRUE);
7468}
7469
7470/*
7471 * "mapcheck()" function
7472 */
7473 static void
7474f_mapcheck(typval_T *argvars, typval_T *rettv)
7475{
7476 get_maparg(argvars, rettv, FALSE);
7477}
7478
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007479typedef enum
7480{
7481 MATCH_END, /* matchend() */
7482 MATCH_MATCH, /* match() */
7483 MATCH_STR, /* matchstr() */
7484 MATCH_LIST, /* matchlist() */
7485 MATCH_POS /* matchstrpos() */
7486} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007487
7488 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007489find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007490{
7491 char_u *str = NULL;
7492 long len = 0;
7493 char_u *expr = NULL;
7494 char_u *pat;
7495 regmatch_T regmatch;
7496 char_u patbuf[NUMBUFLEN];
7497 char_u strbuf[NUMBUFLEN];
7498 char_u *save_cpo;
7499 long start = 0;
7500 long nth = 1;
7501 colnr_T startcol = 0;
7502 int match = 0;
7503 list_T *l = NULL;
7504 listitem_T *li = NULL;
7505 long idx = 0;
7506 char_u *tofree = NULL;
7507
7508 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7509 save_cpo = p_cpo;
7510 p_cpo = (char_u *)"";
7511
7512 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007513 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007514 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007515 /* type MATCH_LIST: return empty list when there are no matches.
7516 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007517 if (rettv_list_alloc(rettv) == FAIL)
7518 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007519 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007520 && (list_append_string(rettv->vval.v_list,
7521 (char_u *)"", 0) == FAIL
7522 || list_append_number(rettv->vval.v_list,
7523 (varnumber_T)-1) == FAIL
7524 || list_append_number(rettv->vval.v_list,
7525 (varnumber_T)-1) == FAIL
7526 || list_append_number(rettv->vval.v_list,
7527 (varnumber_T)-1) == FAIL))
7528 {
7529 list_free(rettv->vval.v_list);
7530 rettv->vval.v_list = NULL;
7531 goto theend;
7532 }
7533 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007534 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007535 {
7536 rettv->v_type = VAR_STRING;
7537 rettv->vval.v_string = NULL;
7538 }
7539
7540 if (argvars[0].v_type == VAR_LIST)
7541 {
7542 if ((l = argvars[0].vval.v_list) == NULL)
7543 goto theend;
7544 li = l->lv_first;
7545 }
7546 else
7547 {
7548 expr = str = get_tv_string(&argvars[0]);
7549 len = (long)STRLEN(str);
7550 }
7551
7552 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7553 if (pat == NULL)
7554 goto theend;
7555
7556 if (argvars[2].v_type != VAR_UNKNOWN)
7557 {
7558 int error = FALSE;
7559
7560 start = (long)get_tv_number_chk(&argvars[2], &error);
7561 if (error)
7562 goto theend;
7563 if (l != NULL)
7564 {
7565 li = list_find(l, start);
7566 if (li == NULL)
7567 goto theend;
7568 idx = l->lv_idx; /* use the cached index */
7569 }
7570 else
7571 {
7572 if (start < 0)
7573 start = 0;
7574 if (start > len)
7575 goto theend;
7576 /* When "count" argument is there ignore matches before "start",
7577 * otherwise skip part of the string. Differs when pattern is "^"
7578 * or "\<". */
7579 if (argvars[3].v_type != VAR_UNKNOWN)
7580 startcol = start;
7581 else
7582 {
7583 str += start;
7584 len -= start;
7585 }
7586 }
7587
7588 if (argvars[3].v_type != VAR_UNKNOWN)
7589 nth = (long)get_tv_number_chk(&argvars[3], &error);
7590 if (error)
7591 goto theend;
7592 }
7593
7594 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7595 if (regmatch.regprog != NULL)
7596 {
7597 regmatch.rm_ic = p_ic;
7598
7599 for (;;)
7600 {
7601 if (l != NULL)
7602 {
7603 if (li == NULL)
7604 {
7605 match = FALSE;
7606 break;
7607 }
7608 vim_free(tofree);
7609 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7610 if (str == NULL)
7611 break;
7612 }
7613
7614 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7615
7616 if (match && --nth <= 0)
7617 break;
7618 if (l == NULL && !match)
7619 break;
7620
7621 /* Advance to just after the match. */
7622 if (l != NULL)
7623 {
7624 li = li->li_next;
7625 ++idx;
7626 }
7627 else
7628 {
7629#ifdef FEAT_MBYTE
7630 startcol = (colnr_T)(regmatch.startp[0]
7631 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7632#else
7633 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7634#endif
7635 if (startcol > (colnr_T)len
7636 || str + startcol <= regmatch.startp[0])
7637 {
7638 match = FALSE;
7639 break;
7640 }
7641 }
7642 }
7643
7644 if (match)
7645 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007646 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007647 {
7648 listitem_T *li1 = rettv->vval.v_list->lv_first;
7649 listitem_T *li2 = li1->li_next;
7650 listitem_T *li3 = li2->li_next;
7651 listitem_T *li4 = li3->li_next;
7652
7653 vim_free(li1->li_tv.vval.v_string);
7654 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7655 (int)(regmatch.endp[0] - regmatch.startp[0]));
7656 li3->li_tv.vval.v_number =
7657 (varnumber_T)(regmatch.startp[0] - expr);
7658 li4->li_tv.vval.v_number =
7659 (varnumber_T)(regmatch.endp[0] - expr);
7660 if (l != NULL)
7661 li2->li_tv.vval.v_number = (varnumber_T)idx;
7662 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007663 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007664 {
7665 int i;
7666
7667 /* return list with matched string and submatches */
7668 for (i = 0; i < NSUBEXP; ++i)
7669 {
7670 if (regmatch.endp[i] == NULL)
7671 {
7672 if (list_append_string(rettv->vval.v_list,
7673 (char_u *)"", 0) == FAIL)
7674 break;
7675 }
7676 else if (list_append_string(rettv->vval.v_list,
7677 regmatch.startp[i],
7678 (int)(regmatch.endp[i] - regmatch.startp[i]))
7679 == FAIL)
7680 break;
7681 }
7682 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007683 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007684 {
7685 /* return matched string */
7686 if (l != NULL)
7687 copy_tv(&li->li_tv, rettv);
7688 else
7689 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7690 (int)(regmatch.endp[0] - regmatch.startp[0]));
7691 }
7692 else if (l != NULL)
7693 rettv->vval.v_number = idx;
7694 else
7695 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007696 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007697 rettv->vval.v_number =
7698 (varnumber_T)(regmatch.startp[0] - str);
7699 else
7700 rettv->vval.v_number =
7701 (varnumber_T)(regmatch.endp[0] - str);
7702 rettv->vval.v_number += (varnumber_T)(str - expr);
7703 }
7704 }
7705 vim_regfree(regmatch.regprog);
7706 }
7707
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007708theend:
7709 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007710 /* matchstrpos() without a list: drop the second item. */
7711 listitem_remove(rettv->vval.v_list,
7712 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007713 vim_free(tofree);
7714 p_cpo = save_cpo;
7715}
7716
7717/*
7718 * "match()" function
7719 */
7720 static void
7721f_match(typval_T *argvars, typval_T *rettv)
7722{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007723 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007724}
7725
7726/*
7727 * "matchadd()" function
7728 */
7729 static void
7730f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7731{
7732#ifdef FEAT_SEARCH_EXTRA
7733 char_u buf[NUMBUFLEN];
7734 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7735 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7736 int prio = 10; /* default priority */
7737 int id = -1;
7738 int error = FALSE;
7739 char_u *conceal_char = NULL;
7740
7741 rettv->vval.v_number = -1;
7742
7743 if (grp == NULL || pat == NULL)
7744 return;
7745 if (argvars[2].v_type != VAR_UNKNOWN)
7746 {
7747 prio = (int)get_tv_number_chk(&argvars[2], &error);
7748 if (argvars[3].v_type != VAR_UNKNOWN)
7749 {
7750 id = (int)get_tv_number_chk(&argvars[3], &error);
7751 if (argvars[4].v_type != VAR_UNKNOWN)
7752 {
7753 if (argvars[4].v_type != VAR_DICT)
7754 {
7755 EMSG(_(e_dictreq));
7756 return;
7757 }
7758 if (dict_find(argvars[4].vval.v_dict,
7759 (char_u *)"conceal", -1) != NULL)
7760 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7761 (char_u *)"conceal", FALSE);
7762 }
7763 }
7764 }
7765 if (error == TRUE)
7766 return;
7767 if (id >= 1 && id <= 3)
7768 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007769 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007770 return;
7771 }
7772
7773 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7774 conceal_char);
7775#endif
7776}
7777
7778/*
7779 * "matchaddpos()" function
7780 */
7781 static void
7782f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7783{
7784#ifdef FEAT_SEARCH_EXTRA
7785 char_u buf[NUMBUFLEN];
7786 char_u *group;
7787 int prio = 10;
7788 int id = -1;
7789 int error = FALSE;
7790 list_T *l;
7791 char_u *conceal_char = NULL;
7792
7793 rettv->vval.v_number = -1;
7794
7795 group = get_tv_string_buf_chk(&argvars[0], buf);
7796 if (group == NULL)
7797 return;
7798
7799 if (argvars[1].v_type != VAR_LIST)
7800 {
7801 EMSG2(_(e_listarg), "matchaddpos()");
7802 return;
7803 }
7804 l = argvars[1].vval.v_list;
7805 if (l == NULL)
7806 return;
7807
7808 if (argvars[2].v_type != VAR_UNKNOWN)
7809 {
7810 prio = (int)get_tv_number_chk(&argvars[2], &error);
7811 if (argvars[3].v_type != VAR_UNKNOWN)
7812 {
7813 id = (int)get_tv_number_chk(&argvars[3], &error);
7814 if (argvars[4].v_type != VAR_UNKNOWN)
7815 {
7816 if (argvars[4].v_type != VAR_DICT)
7817 {
7818 EMSG(_(e_dictreq));
7819 return;
7820 }
7821 if (dict_find(argvars[4].vval.v_dict,
7822 (char_u *)"conceal", -1) != NULL)
7823 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7824 (char_u *)"conceal", FALSE);
7825 }
7826 }
7827 }
7828 if (error == TRUE)
7829 return;
7830
7831 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7832 if (id == 1 || id == 2)
7833 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007834 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007835 return;
7836 }
7837
7838 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7839 conceal_char);
7840#endif
7841}
7842
7843/*
7844 * "matcharg()" function
7845 */
7846 static void
7847f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7848{
7849 if (rettv_list_alloc(rettv) == OK)
7850 {
7851#ifdef FEAT_SEARCH_EXTRA
7852 int id = (int)get_tv_number(&argvars[0]);
7853 matchitem_T *m;
7854
7855 if (id >= 1 && id <= 3)
7856 {
7857 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7858 {
7859 list_append_string(rettv->vval.v_list,
7860 syn_id2name(m->hlg_id), -1);
7861 list_append_string(rettv->vval.v_list, m->pattern, -1);
7862 }
7863 else
7864 {
7865 list_append_string(rettv->vval.v_list, NULL, -1);
7866 list_append_string(rettv->vval.v_list, NULL, -1);
7867 }
7868 }
7869#endif
7870 }
7871}
7872
7873/*
7874 * "matchdelete()" function
7875 */
7876 static void
7877f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7878{
7879#ifdef FEAT_SEARCH_EXTRA
7880 rettv->vval.v_number = match_delete(curwin,
7881 (int)get_tv_number(&argvars[0]), TRUE);
7882#endif
7883}
7884
7885/*
7886 * "matchend()" function
7887 */
7888 static void
7889f_matchend(typval_T *argvars, typval_T *rettv)
7890{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007891 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007892}
7893
7894/*
7895 * "matchlist()" function
7896 */
7897 static void
7898f_matchlist(typval_T *argvars, typval_T *rettv)
7899{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007900 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007901}
7902
7903/*
7904 * "matchstr()" function
7905 */
7906 static void
7907f_matchstr(typval_T *argvars, typval_T *rettv)
7908{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007909 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007910}
7911
7912/*
7913 * "matchstrpos()" function
7914 */
7915 static void
7916f_matchstrpos(typval_T *argvars, typval_T *rettv)
7917{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007918 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007919}
7920
7921static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7922
7923 static void
7924max_min(typval_T *argvars, typval_T *rettv, int domax)
7925{
7926 varnumber_T n = 0;
7927 varnumber_T i;
7928 int error = FALSE;
7929
7930 if (argvars[0].v_type == VAR_LIST)
7931 {
7932 list_T *l;
7933 listitem_T *li;
7934
7935 l = argvars[0].vval.v_list;
7936 if (l != NULL)
7937 {
7938 li = l->lv_first;
7939 if (li != NULL)
7940 {
7941 n = get_tv_number_chk(&li->li_tv, &error);
7942 for (;;)
7943 {
7944 li = li->li_next;
7945 if (li == NULL)
7946 break;
7947 i = get_tv_number_chk(&li->li_tv, &error);
7948 if (domax ? i > n : i < n)
7949 n = i;
7950 }
7951 }
7952 }
7953 }
7954 else if (argvars[0].v_type == VAR_DICT)
7955 {
7956 dict_T *d;
7957 int first = TRUE;
7958 hashitem_T *hi;
7959 int todo;
7960
7961 d = argvars[0].vval.v_dict;
7962 if (d != NULL)
7963 {
7964 todo = (int)d->dv_hashtab.ht_used;
7965 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7966 {
7967 if (!HASHITEM_EMPTY(hi))
7968 {
7969 --todo;
7970 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7971 if (first)
7972 {
7973 n = i;
7974 first = FALSE;
7975 }
7976 else if (domax ? i > n : i < n)
7977 n = i;
7978 }
7979 }
7980 }
7981 }
7982 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007983 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007984 rettv->vval.v_number = error ? 0 : n;
7985}
7986
7987/*
7988 * "max()" function
7989 */
7990 static void
7991f_max(typval_T *argvars, typval_T *rettv)
7992{
7993 max_min(argvars, rettv, TRUE);
7994}
7995
7996/*
7997 * "min()" function
7998 */
7999 static void
8000f_min(typval_T *argvars, typval_T *rettv)
8001{
8002 max_min(argvars, rettv, FALSE);
8003}
8004
8005static int mkdir_recurse(char_u *dir, int prot);
8006
8007/*
8008 * Create the directory in which "dir" is located, and higher levels when
8009 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008010 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008011 */
8012 static int
8013mkdir_recurse(char_u *dir, int prot)
8014{
8015 char_u *p;
8016 char_u *updir;
8017 int r = FAIL;
8018
8019 /* Get end of directory name in "dir".
8020 * We're done when it's "/" or "c:/". */
8021 p = gettail_sep(dir);
8022 if (p <= get_past_head(dir))
8023 return OK;
8024
8025 /* If the directory exists we're done. Otherwise: create it.*/
8026 updir = vim_strnsave(dir, (int)(p - dir));
8027 if (updir == NULL)
8028 return FAIL;
8029 if (mch_isdir(updir))
8030 r = OK;
8031 else if (mkdir_recurse(updir, prot) == OK)
8032 r = vim_mkdir_emsg(updir, prot);
8033 vim_free(updir);
8034 return r;
8035}
8036
8037#ifdef vim_mkdir
8038/*
8039 * "mkdir()" function
8040 */
8041 static void
8042f_mkdir(typval_T *argvars, typval_T *rettv)
8043{
8044 char_u *dir;
8045 char_u buf[NUMBUFLEN];
8046 int prot = 0755;
8047
8048 rettv->vval.v_number = FAIL;
8049 if (check_restricted() || check_secure())
8050 return;
8051
8052 dir = get_tv_string_buf(&argvars[0], buf);
8053 if (*dir == NUL)
8054 rettv->vval.v_number = FAIL;
8055 else
8056 {
8057 if (*gettail(dir) == NUL)
8058 /* remove trailing slashes */
8059 *gettail_sep(dir) = NUL;
8060
8061 if (argvars[1].v_type != VAR_UNKNOWN)
8062 {
8063 if (argvars[2].v_type != VAR_UNKNOWN)
8064 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8065 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8066 mkdir_recurse(dir, prot);
8067 }
8068 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
8069 }
8070}
8071#endif
8072
8073/*
8074 * "mode()" function
8075 */
8076 static void
8077f_mode(typval_T *argvars, typval_T *rettv)
8078{
8079 char_u buf[3];
8080
8081 buf[1] = NUL;
8082 buf[2] = NUL;
8083
8084 if (time_for_testing == 93784)
8085 {
8086 /* Testing the two-character code. */
8087 buf[0] = 'x';
8088 buf[1] = '!';
8089 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008090#ifdef FEAT_TERMINAL
8091 else if (term_use_loop())
8092 buf[0] = 't';
8093#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008094 else if (VIsual_active)
8095 {
8096 if (VIsual_select)
8097 buf[0] = VIsual_mode + 's' - 'v';
8098 else
8099 buf[0] = VIsual_mode;
8100 }
8101 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8102 || State == CONFIRM)
8103 {
8104 buf[0] = 'r';
8105 if (State == ASKMORE)
8106 buf[1] = 'm';
8107 else if (State == CONFIRM)
8108 buf[1] = '?';
8109 }
8110 else if (State == EXTERNCMD)
8111 buf[0] = '!';
8112 else if (State & INSERT)
8113 {
8114#ifdef FEAT_VREPLACE
8115 if (State & VREPLACE_FLAG)
8116 {
8117 buf[0] = 'R';
8118 buf[1] = 'v';
8119 }
8120 else
8121#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008122 {
8123 if (State & REPLACE_FLAG)
8124 buf[0] = 'R';
8125 else
8126 buf[0] = 'i';
8127#ifdef FEAT_INS_EXPAND
8128 if (ins_compl_active())
8129 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008130 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008131 buf[1] = 'x';
8132#endif
8133 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008134 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008135 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008136 {
8137 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008138 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008139 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008140 else if (exmode_active == EXMODE_NORMAL)
8141 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008142 }
8143 else
8144 {
8145 buf[0] = 'n';
8146 if (finish_op)
8147 buf[1] = 'o';
8148 }
8149
8150 /* Clear out the minor mode when the argument is not a non-zero number or
8151 * non-empty string. */
8152 if (!non_zero_arg(&argvars[0]))
8153 buf[1] = NUL;
8154
8155 rettv->vval.v_string = vim_strsave(buf);
8156 rettv->v_type = VAR_STRING;
8157}
8158
8159#if defined(FEAT_MZSCHEME) || defined(PROTO)
8160/*
8161 * "mzeval()" function
8162 */
8163 static void
8164f_mzeval(typval_T *argvars, typval_T *rettv)
8165{
8166 char_u *str;
8167 char_u buf[NUMBUFLEN];
8168
8169 str = get_tv_string_buf(&argvars[0], buf);
8170 do_mzeval(str, rettv);
8171}
8172
8173 void
8174mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8175{
8176 typval_T argvars[3];
8177
8178 argvars[0].v_type = VAR_STRING;
8179 argvars[0].vval.v_string = name;
8180 copy_tv(args, &argvars[1]);
8181 argvars[2].v_type = VAR_UNKNOWN;
8182 f_call(argvars, rettv);
8183 clear_tv(&argvars[1]);
8184}
8185#endif
8186
8187/*
8188 * "nextnonblank()" function
8189 */
8190 static void
8191f_nextnonblank(typval_T *argvars, typval_T *rettv)
8192{
8193 linenr_T lnum;
8194
8195 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8196 {
8197 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8198 {
8199 lnum = 0;
8200 break;
8201 }
8202 if (*skipwhite(ml_get(lnum)) != NUL)
8203 break;
8204 }
8205 rettv->vval.v_number = lnum;
8206}
8207
8208/*
8209 * "nr2char()" function
8210 */
8211 static void
8212f_nr2char(typval_T *argvars, typval_T *rettv)
8213{
8214 char_u buf[NUMBUFLEN];
8215
8216#ifdef FEAT_MBYTE
8217 if (has_mbyte)
8218 {
8219 int utf8 = 0;
8220
8221 if (argvars[1].v_type != VAR_UNKNOWN)
8222 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8223 if (utf8)
8224 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8225 else
8226 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8227 }
8228 else
8229#endif
8230 {
8231 buf[0] = (char_u)get_tv_number(&argvars[0]);
8232 buf[1] = NUL;
8233 }
8234 rettv->v_type = VAR_STRING;
8235 rettv->vval.v_string = vim_strsave(buf);
8236}
8237
8238/*
8239 * "or(expr, expr)" function
8240 */
8241 static void
8242f_or(typval_T *argvars, typval_T *rettv)
8243{
8244 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8245 | get_tv_number_chk(&argvars[1], NULL);
8246}
8247
8248/*
8249 * "pathshorten()" function
8250 */
8251 static void
8252f_pathshorten(typval_T *argvars, typval_T *rettv)
8253{
8254 char_u *p;
8255
8256 rettv->v_type = VAR_STRING;
8257 p = get_tv_string_chk(&argvars[0]);
8258 if (p == NULL)
8259 rettv->vval.v_string = NULL;
8260 else
8261 {
8262 p = vim_strsave(p);
8263 rettv->vval.v_string = p;
8264 if (p != NULL)
8265 shorten_dir(p);
8266 }
8267}
8268
8269#ifdef FEAT_PERL
8270/*
8271 * "perleval()" function
8272 */
8273 static void
8274f_perleval(typval_T *argvars, typval_T *rettv)
8275{
8276 char_u *str;
8277 char_u buf[NUMBUFLEN];
8278
8279 str = get_tv_string_buf(&argvars[0], buf);
8280 do_perleval(str, rettv);
8281}
8282#endif
8283
8284#ifdef FEAT_FLOAT
8285/*
8286 * "pow()" function
8287 */
8288 static void
8289f_pow(typval_T *argvars, typval_T *rettv)
8290{
8291 float_T fx = 0.0, fy = 0.0;
8292
8293 rettv->v_type = VAR_FLOAT;
8294 if (get_float_arg(argvars, &fx) == OK
8295 && get_float_arg(&argvars[1], &fy) == OK)
8296 rettv->vval.v_float = pow(fx, fy);
8297 else
8298 rettv->vval.v_float = 0.0;
8299}
8300#endif
8301
8302/*
8303 * "prevnonblank()" function
8304 */
8305 static void
8306f_prevnonblank(typval_T *argvars, typval_T *rettv)
8307{
8308 linenr_T lnum;
8309
8310 lnum = get_tv_lnum(argvars);
8311 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8312 lnum = 0;
8313 else
8314 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8315 --lnum;
8316 rettv->vval.v_number = lnum;
8317}
8318
8319/* This dummy va_list is here because:
8320 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8321 * - locally in the function results in a "used before set" warning
8322 * - using va_start() to initialize it gives "function with fixed args" error */
8323static va_list ap;
8324
8325/*
8326 * "printf()" function
8327 */
8328 static void
8329f_printf(typval_T *argvars, typval_T *rettv)
8330{
8331 char_u buf[NUMBUFLEN];
8332 int len;
8333 char_u *s;
8334 int saved_did_emsg = did_emsg;
8335 char *fmt;
8336
8337 rettv->v_type = VAR_STRING;
8338 rettv->vval.v_string = NULL;
8339
8340 /* Get the required length, allocate the buffer and do it for real. */
8341 did_emsg = FALSE;
8342 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008343 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008344 if (!did_emsg)
8345 {
8346 s = alloc(len + 1);
8347 if (s != NULL)
8348 {
8349 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008350 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8351 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008352 }
8353 }
8354 did_emsg |= saved_did_emsg;
8355}
8356
8357/*
8358 * "pumvisible()" function
8359 */
8360 static void
8361f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8362{
8363#ifdef FEAT_INS_EXPAND
8364 if (pum_visible())
8365 rettv->vval.v_number = 1;
8366#endif
8367}
8368
8369#ifdef FEAT_PYTHON3
8370/*
8371 * "py3eval()" function
8372 */
8373 static void
8374f_py3eval(typval_T *argvars, typval_T *rettv)
8375{
8376 char_u *str;
8377 char_u buf[NUMBUFLEN];
8378
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008379 if (p_pyx == 0)
8380 p_pyx = 3;
8381
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008382 str = get_tv_string_buf(&argvars[0], buf);
8383 do_py3eval(str, rettv);
8384}
8385#endif
8386
8387#ifdef FEAT_PYTHON
8388/*
8389 * "pyeval()" function
8390 */
8391 static void
8392f_pyeval(typval_T *argvars, typval_T *rettv)
8393{
8394 char_u *str;
8395 char_u buf[NUMBUFLEN];
8396
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008397 if (p_pyx == 0)
8398 p_pyx = 2;
8399
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008400 str = get_tv_string_buf(&argvars[0], buf);
8401 do_pyeval(str, rettv);
8402}
8403#endif
8404
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008405#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8406/*
8407 * "pyxeval()" function
8408 */
8409 static void
8410f_pyxeval(typval_T *argvars, typval_T *rettv)
8411{
8412# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8413 init_pyxversion();
8414 if (p_pyx == 2)
8415 f_pyeval(argvars, rettv);
8416 else
8417 f_py3eval(argvars, rettv);
8418# elif defined(FEAT_PYTHON)
8419 f_pyeval(argvars, rettv);
8420# elif defined(FEAT_PYTHON3)
8421 f_py3eval(argvars, rettv);
8422# endif
8423}
8424#endif
8425
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426/*
8427 * "range()" function
8428 */
8429 static void
8430f_range(typval_T *argvars, typval_T *rettv)
8431{
8432 varnumber_T start;
8433 varnumber_T end;
8434 varnumber_T stride = 1;
8435 varnumber_T i;
8436 int error = FALSE;
8437
8438 start = get_tv_number_chk(&argvars[0], &error);
8439 if (argvars[1].v_type == VAR_UNKNOWN)
8440 {
8441 end = start - 1;
8442 start = 0;
8443 }
8444 else
8445 {
8446 end = get_tv_number_chk(&argvars[1], &error);
8447 if (argvars[2].v_type != VAR_UNKNOWN)
8448 stride = get_tv_number_chk(&argvars[2], &error);
8449 }
8450
8451 if (error)
8452 return; /* type error; errmsg already given */
8453 if (stride == 0)
8454 EMSG(_("E726: Stride is zero"));
8455 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8456 EMSG(_("E727: Start past end"));
8457 else
8458 {
8459 if (rettv_list_alloc(rettv) == OK)
8460 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8461 if (list_append_number(rettv->vval.v_list,
8462 (varnumber_T)i) == FAIL)
8463 break;
8464 }
8465}
8466
8467/*
8468 * "readfile()" function
8469 */
8470 static void
8471f_readfile(typval_T *argvars, typval_T *rettv)
8472{
8473 int binary = FALSE;
8474 int failed = FALSE;
8475 char_u *fname;
8476 FILE *fd;
8477 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8478 int io_size = sizeof(buf);
8479 int readlen; /* size of last fread() */
8480 char_u *prev = NULL; /* previously read bytes, if any */
8481 long prevlen = 0; /* length of data in prev */
8482 long prevsize = 0; /* size of prev buffer */
8483 long maxline = MAXLNUM;
8484 long cnt = 0;
8485 char_u *p; /* position in buf */
8486 char_u *start; /* start of current line */
8487
8488 if (argvars[1].v_type != VAR_UNKNOWN)
8489 {
8490 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8491 binary = TRUE;
8492 if (argvars[2].v_type != VAR_UNKNOWN)
8493 maxline = (long)get_tv_number(&argvars[2]);
8494 }
8495
8496 if (rettv_list_alloc(rettv) == FAIL)
8497 return;
8498
8499 /* Always open the file in binary mode, library functions have a mind of
8500 * their own about CR-LF conversion. */
8501 fname = get_tv_string(&argvars[0]);
8502 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8503 {
8504 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8505 return;
8506 }
8507
8508 while (cnt < maxline || maxline < 0)
8509 {
8510 readlen = (int)fread(buf, 1, io_size, fd);
8511
8512 /* This for loop processes what was read, but is also entered at end
8513 * of file so that either:
8514 * - an incomplete line gets written
8515 * - a "binary" file gets an empty line at the end if it ends in a
8516 * newline. */
8517 for (p = buf, start = buf;
8518 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8519 ++p)
8520 {
8521 if (*p == '\n' || readlen <= 0)
8522 {
8523 listitem_T *li;
8524 char_u *s = NULL;
8525 long_u len = p - start;
8526
8527 /* Finished a line. Remove CRs before NL. */
8528 if (readlen > 0 && !binary)
8529 {
8530 while (len > 0 && start[len - 1] == '\r')
8531 --len;
8532 /* removal may cross back to the "prev" string */
8533 if (len == 0)
8534 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8535 --prevlen;
8536 }
8537 if (prevlen == 0)
8538 s = vim_strnsave(start, (int)len);
8539 else
8540 {
8541 /* Change "prev" buffer to be the right size. This way
8542 * the bytes are only copied once, and very long lines are
8543 * allocated only once. */
8544 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8545 {
8546 mch_memmove(s + prevlen, start, len);
8547 s[prevlen + len] = NUL;
8548 prev = NULL; /* the list will own the string */
8549 prevlen = prevsize = 0;
8550 }
8551 }
8552 if (s == NULL)
8553 {
8554 do_outofmem_msg((long_u) prevlen + len + 1);
8555 failed = TRUE;
8556 break;
8557 }
8558
8559 if ((li = listitem_alloc()) == NULL)
8560 {
8561 vim_free(s);
8562 failed = TRUE;
8563 break;
8564 }
8565 li->li_tv.v_type = VAR_STRING;
8566 li->li_tv.v_lock = 0;
8567 li->li_tv.vval.v_string = s;
8568 list_append(rettv->vval.v_list, li);
8569
8570 start = p + 1; /* step over newline */
8571 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8572 break;
8573 }
8574 else if (*p == NUL)
8575 *p = '\n';
8576#ifdef FEAT_MBYTE
8577 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8578 * when finding the BF and check the previous two bytes. */
8579 else if (*p == 0xbf && enc_utf8 && !binary)
8580 {
8581 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8582 * + 1, these may be in the "prev" string. */
8583 char_u back1 = p >= buf + 1 ? p[-1]
8584 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8585 char_u back2 = p >= buf + 2 ? p[-2]
8586 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8587 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8588
8589 if (back2 == 0xef && back1 == 0xbb)
8590 {
8591 char_u *dest = p - 2;
8592
8593 /* Usually a BOM is at the beginning of a file, and so at
8594 * the beginning of a line; then we can just step over it.
8595 */
8596 if (start == dest)
8597 start = p + 1;
8598 else
8599 {
8600 /* have to shuffle buf to close gap */
8601 int adjust_prevlen = 0;
8602
8603 if (dest < buf)
8604 {
8605 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8606 dest = buf;
8607 }
8608 if (readlen > p - buf + 1)
8609 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8610 readlen -= 3 - adjust_prevlen;
8611 prevlen -= adjust_prevlen;
8612 p = dest - 1;
8613 }
8614 }
8615 }
8616#endif
8617 } /* for */
8618
8619 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8620 break;
8621 if (start < p)
8622 {
8623 /* There's part of a line in buf, store it in "prev". */
8624 if (p - start + prevlen >= prevsize)
8625 {
8626 /* need bigger "prev" buffer */
8627 char_u *newprev;
8628
8629 /* A common use case is ordinary text files and "prev" gets a
8630 * fragment of a line, so the first allocation is made
8631 * small, to avoid repeatedly 'allocing' large and
8632 * 'reallocing' small. */
8633 if (prevsize == 0)
8634 prevsize = (long)(p - start);
8635 else
8636 {
8637 long grow50pc = (prevsize * 3) / 2;
8638 long growmin = (long)((p - start) * 2 + prevlen);
8639 prevsize = grow50pc > growmin ? grow50pc : growmin;
8640 }
8641 newprev = prev == NULL ? alloc(prevsize)
8642 : vim_realloc(prev, prevsize);
8643 if (newprev == NULL)
8644 {
8645 do_outofmem_msg((long_u)prevsize);
8646 failed = TRUE;
8647 break;
8648 }
8649 prev = newprev;
8650 }
8651 /* Add the line part to end of "prev". */
8652 mch_memmove(prev + prevlen, start, p - start);
8653 prevlen += (long)(p - start);
8654 }
8655 } /* while */
8656
8657 /*
8658 * For a negative line count use only the lines at the end of the file,
8659 * free the rest.
8660 */
8661 if (!failed && maxline < 0)
8662 while (cnt > -maxline)
8663 {
8664 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8665 --cnt;
8666 }
8667
8668 if (failed)
8669 {
8670 list_free(rettv->vval.v_list);
8671 /* readfile doc says an empty list is returned on error */
8672 rettv->vval.v_list = list_alloc();
8673 }
8674
8675 vim_free(prev);
8676 fclose(fd);
8677}
8678
8679#if defined(FEAT_RELTIME)
8680static int list2proftime(typval_T *arg, proftime_T *tm);
8681
8682/*
8683 * Convert a List to proftime_T.
8684 * Return FAIL when there is something wrong.
8685 */
8686 static int
8687list2proftime(typval_T *arg, proftime_T *tm)
8688{
8689 long n1, n2;
8690 int error = FALSE;
8691
8692 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8693 || arg->vval.v_list->lv_len != 2)
8694 return FAIL;
8695 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8696 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8697# ifdef WIN3264
8698 tm->HighPart = n1;
8699 tm->LowPart = n2;
8700# else
8701 tm->tv_sec = n1;
8702 tm->tv_usec = n2;
8703# endif
8704 return error ? FAIL : OK;
8705}
8706#endif /* FEAT_RELTIME */
8707
8708/*
8709 * "reltime()" function
8710 */
8711 static void
8712f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8713{
8714#ifdef FEAT_RELTIME
8715 proftime_T res;
8716 proftime_T start;
8717
8718 if (argvars[0].v_type == VAR_UNKNOWN)
8719 {
8720 /* No arguments: get current time. */
8721 profile_start(&res);
8722 }
8723 else if (argvars[1].v_type == VAR_UNKNOWN)
8724 {
8725 if (list2proftime(&argvars[0], &res) == FAIL)
8726 return;
8727 profile_end(&res);
8728 }
8729 else
8730 {
8731 /* Two arguments: compute the difference. */
8732 if (list2proftime(&argvars[0], &start) == FAIL
8733 || list2proftime(&argvars[1], &res) == FAIL)
8734 return;
8735 profile_sub(&res, &start);
8736 }
8737
8738 if (rettv_list_alloc(rettv) == OK)
8739 {
8740 long n1, n2;
8741
8742# ifdef WIN3264
8743 n1 = res.HighPart;
8744 n2 = res.LowPart;
8745# else
8746 n1 = res.tv_sec;
8747 n2 = res.tv_usec;
8748# endif
8749 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8750 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8751 }
8752#endif
8753}
8754
8755#ifdef FEAT_FLOAT
8756/*
8757 * "reltimefloat()" function
8758 */
8759 static void
8760f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8761{
8762# ifdef FEAT_RELTIME
8763 proftime_T tm;
8764# endif
8765
8766 rettv->v_type = VAR_FLOAT;
8767 rettv->vval.v_float = 0;
8768# ifdef FEAT_RELTIME
8769 if (list2proftime(&argvars[0], &tm) == OK)
8770 rettv->vval.v_float = profile_float(&tm);
8771# endif
8772}
8773#endif
8774
8775/*
8776 * "reltimestr()" function
8777 */
8778 static void
8779f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8780{
8781#ifdef FEAT_RELTIME
8782 proftime_T tm;
8783#endif
8784
8785 rettv->v_type = VAR_STRING;
8786 rettv->vval.v_string = NULL;
8787#ifdef FEAT_RELTIME
8788 if (list2proftime(&argvars[0], &tm) == OK)
8789 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8790#endif
8791}
8792
8793#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8794static void make_connection(void);
8795static int check_connection(void);
8796
8797 static void
8798make_connection(void)
8799{
8800 if (X_DISPLAY == NULL
8801# ifdef FEAT_GUI
8802 && !gui.in_use
8803# endif
8804 )
8805 {
8806 x_force_connect = TRUE;
8807 setup_term_clip();
8808 x_force_connect = FALSE;
8809 }
8810}
8811
8812 static int
8813check_connection(void)
8814{
8815 make_connection();
8816 if (X_DISPLAY == NULL)
8817 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008818 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008819 return FAIL;
8820 }
8821 return OK;
8822}
8823#endif
8824
8825#ifdef FEAT_CLIENTSERVER
8826 static void
8827remote_common(typval_T *argvars, typval_T *rettv, int expr)
8828{
8829 char_u *server_name;
8830 char_u *keys;
8831 char_u *r = NULL;
8832 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008833 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008834# ifdef WIN32
8835 HWND w;
8836# else
8837 Window w;
8838# endif
8839
8840 if (check_restricted() || check_secure())
8841 return;
8842
8843# ifdef FEAT_X11
8844 if (check_connection() == FAIL)
8845 return;
8846# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008847 if (argvars[2].v_type != VAR_UNKNOWN
8848 && argvars[3].v_type != VAR_UNKNOWN)
8849 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008850
8851 server_name = get_tv_string_chk(&argvars[0]);
8852 if (server_name == NULL)
8853 return; /* type error; errmsg already given */
8854 keys = get_tv_string_buf(&argvars[1], buf);
8855# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008856 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008857# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008858 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8859 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008860# endif
8861 {
8862 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008863 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008864 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008865 vim_free(r);
8866 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008867 else
8868 EMSG2(_("E241: Unable to send to %s"), server_name);
8869 return;
8870 }
8871
8872 rettv->vval.v_string = r;
8873
8874 if (argvars[2].v_type != VAR_UNKNOWN)
8875 {
8876 dictitem_T v;
8877 char_u str[30];
8878 char_u *idvar;
8879
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008880 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008881 if (idvar != NULL && *idvar != NUL)
8882 {
8883 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8884 v.di_tv.v_type = VAR_STRING;
8885 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008886 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008887 vim_free(v.di_tv.vval.v_string);
8888 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008889 }
8890}
8891#endif
8892
8893/*
8894 * "remote_expr()" function
8895 */
8896 static void
8897f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8898{
8899 rettv->v_type = VAR_STRING;
8900 rettv->vval.v_string = NULL;
8901#ifdef FEAT_CLIENTSERVER
8902 remote_common(argvars, rettv, TRUE);
8903#endif
8904}
8905
8906/*
8907 * "remote_foreground()" function
8908 */
8909 static void
8910f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8911{
8912#ifdef FEAT_CLIENTSERVER
8913# ifdef WIN32
8914 /* On Win32 it's done in this application. */
8915 {
8916 char_u *server_name = get_tv_string_chk(&argvars[0]);
8917
8918 if (server_name != NULL)
8919 serverForeground(server_name);
8920 }
8921# else
8922 /* Send a foreground() expression to the server. */
8923 argvars[1].v_type = VAR_STRING;
8924 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8925 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008926 rettv->v_type = VAR_STRING;
8927 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008928 remote_common(argvars, rettv, TRUE);
8929 vim_free(argvars[1].vval.v_string);
8930# endif
8931#endif
8932}
8933
8934 static void
8935f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8936{
8937#ifdef FEAT_CLIENTSERVER
8938 dictitem_T v;
8939 char_u *s = NULL;
8940# ifdef WIN32
8941 long_u n = 0;
8942# endif
8943 char_u *serverid;
8944
8945 if (check_restricted() || check_secure())
8946 {
8947 rettv->vval.v_number = -1;
8948 return;
8949 }
8950 serverid = get_tv_string_chk(&argvars[0]);
8951 if (serverid == NULL)
8952 {
8953 rettv->vval.v_number = -1;
8954 return; /* type error; errmsg already given */
8955 }
8956# ifdef WIN32
8957 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8958 if (n == 0)
8959 rettv->vval.v_number = -1;
8960 else
8961 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008962 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008963 rettv->vval.v_number = (s != NULL);
8964 }
8965# else
8966 if (check_connection() == FAIL)
8967 return;
8968
8969 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8970 serverStrToWin(serverid), &s);
8971# endif
8972
8973 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8974 {
8975 char_u *retvar;
8976
8977 v.di_tv.v_type = VAR_STRING;
8978 v.di_tv.vval.v_string = vim_strsave(s);
8979 retvar = get_tv_string_chk(&argvars[1]);
8980 if (retvar != NULL)
8981 set_var(retvar, &v.di_tv, FALSE);
8982 vim_free(v.di_tv.vval.v_string);
8983 }
8984#else
8985 rettv->vval.v_number = -1;
8986#endif
8987}
8988
8989 static void
8990f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8991{
8992 char_u *r = NULL;
8993
8994#ifdef FEAT_CLIENTSERVER
8995 char_u *serverid = get_tv_string_chk(&argvars[0]);
8996
8997 if (serverid != NULL && !check_restricted() && !check_secure())
8998 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008999 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009000# ifdef WIN32
9001 /* The server's HWND is encoded in the 'id' parameter */
9002 long_u n = 0;
9003# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009004
9005 if (argvars[1].v_type != VAR_UNKNOWN)
9006 timeout = get_tv_number(&argvars[1]);
9007
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009008# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009009 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9010 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009011 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009012 if (r == NULL)
9013# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009014 if (check_connection() == FAIL
9015 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9016 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009017# endif
9018 EMSG(_("E277: Unable to read a server reply"));
9019 }
9020#endif
9021 rettv->v_type = VAR_STRING;
9022 rettv->vval.v_string = r;
9023}
9024
9025/*
9026 * "remote_send()" function
9027 */
9028 static void
9029f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9030{
9031 rettv->v_type = VAR_STRING;
9032 rettv->vval.v_string = NULL;
9033#ifdef FEAT_CLIENTSERVER
9034 remote_common(argvars, rettv, FALSE);
9035#endif
9036}
9037
9038/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009039 * "remote_startserver()" function
9040 */
9041 static void
9042f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9043{
9044#ifdef FEAT_CLIENTSERVER
9045 char_u *server = get_tv_string_chk(&argvars[0]);
9046
9047 if (server == NULL)
9048 return; /* type error; errmsg already given */
9049 if (serverName != NULL)
9050 EMSG(_("E941: already started a server"));
9051 else
9052 {
9053# ifdef FEAT_X11
9054 if (check_connection() == OK)
9055 serverRegisterName(X_DISPLAY, server);
9056# else
9057 serverSetName(server);
9058# endif
9059 }
9060#else
9061 EMSG(_("E942: +clientserver feature not available"));
9062#endif
9063}
9064
9065/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009066 * "remove()" function
9067 */
9068 static void
9069f_remove(typval_T *argvars, typval_T *rettv)
9070{
9071 list_T *l;
9072 listitem_T *item, *item2;
9073 listitem_T *li;
9074 long idx;
9075 long end;
9076 char_u *key;
9077 dict_T *d;
9078 dictitem_T *di;
9079 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9080
9081 if (argvars[0].v_type == VAR_DICT)
9082 {
9083 if (argvars[2].v_type != VAR_UNKNOWN)
9084 EMSG2(_(e_toomanyarg), "remove()");
9085 else if ((d = argvars[0].vval.v_dict) != NULL
9086 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9087 {
9088 key = get_tv_string_chk(&argvars[1]);
9089 if (key != NULL)
9090 {
9091 di = dict_find(d, key, -1);
9092 if (di == NULL)
9093 EMSG2(_(e_dictkey), key);
9094 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9095 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9096 {
9097 *rettv = di->di_tv;
9098 init_tv(&di->di_tv);
9099 dictitem_remove(d, di);
9100 }
9101 }
9102 }
9103 }
9104 else if (argvars[0].v_type != VAR_LIST)
9105 EMSG2(_(e_listdictarg), "remove()");
9106 else if ((l = argvars[0].vval.v_list) != NULL
9107 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9108 {
9109 int error = FALSE;
9110
9111 idx = (long)get_tv_number_chk(&argvars[1], &error);
9112 if (error)
9113 ; /* type error: do nothing, errmsg already given */
9114 else if ((item = list_find(l, idx)) == NULL)
9115 EMSGN(_(e_listidx), idx);
9116 else
9117 {
9118 if (argvars[2].v_type == VAR_UNKNOWN)
9119 {
9120 /* Remove one item, return its value. */
9121 vimlist_remove(l, item, item);
9122 *rettv = item->li_tv;
9123 vim_free(item);
9124 }
9125 else
9126 {
9127 /* Remove range of items, return list with values. */
9128 end = (long)get_tv_number_chk(&argvars[2], &error);
9129 if (error)
9130 ; /* type error: do nothing */
9131 else if ((item2 = list_find(l, end)) == NULL)
9132 EMSGN(_(e_listidx), end);
9133 else
9134 {
9135 int cnt = 0;
9136
9137 for (li = item; li != NULL; li = li->li_next)
9138 {
9139 ++cnt;
9140 if (li == item2)
9141 break;
9142 }
9143 if (li == NULL) /* didn't find "item2" after "item" */
9144 EMSG(_(e_invrange));
9145 else
9146 {
9147 vimlist_remove(l, item, item2);
9148 if (rettv_list_alloc(rettv) == OK)
9149 {
9150 l = rettv->vval.v_list;
9151 l->lv_first = item;
9152 l->lv_last = item2;
9153 item->li_prev = NULL;
9154 item2->li_next = NULL;
9155 l->lv_len = cnt;
9156 }
9157 }
9158 }
9159 }
9160 }
9161 }
9162}
9163
9164/*
9165 * "rename({from}, {to})" function
9166 */
9167 static void
9168f_rename(typval_T *argvars, typval_T *rettv)
9169{
9170 char_u buf[NUMBUFLEN];
9171
9172 if (check_restricted() || check_secure())
9173 rettv->vval.v_number = -1;
9174 else
9175 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9176 get_tv_string_buf(&argvars[1], buf));
9177}
9178
9179/*
9180 * "repeat()" function
9181 */
9182 static void
9183f_repeat(typval_T *argvars, typval_T *rettv)
9184{
9185 char_u *p;
9186 int n;
9187 int slen;
9188 int len;
9189 char_u *r;
9190 int i;
9191
9192 n = (int)get_tv_number(&argvars[1]);
9193 if (argvars[0].v_type == VAR_LIST)
9194 {
9195 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9196 while (n-- > 0)
9197 if (list_extend(rettv->vval.v_list,
9198 argvars[0].vval.v_list, NULL) == FAIL)
9199 break;
9200 }
9201 else
9202 {
9203 p = get_tv_string(&argvars[0]);
9204 rettv->v_type = VAR_STRING;
9205 rettv->vval.v_string = NULL;
9206
9207 slen = (int)STRLEN(p);
9208 len = slen * n;
9209 if (len <= 0)
9210 return;
9211
9212 r = alloc(len + 1);
9213 if (r != NULL)
9214 {
9215 for (i = 0; i < n; i++)
9216 mch_memmove(r + i * slen, p, (size_t)slen);
9217 r[len] = NUL;
9218 }
9219
9220 rettv->vval.v_string = r;
9221 }
9222}
9223
9224/*
9225 * "resolve()" function
9226 */
9227 static void
9228f_resolve(typval_T *argvars, typval_T *rettv)
9229{
9230 char_u *p;
9231#ifdef HAVE_READLINK
9232 char_u *buf = NULL;
9233#endif
9234
9235 p = get_tv_string(&argvars[0]);
9236#ifdef FEAT_SHORTCUT
9237 {
9238 char_u *v = NULL;
9239
9240 v = mch_resolve_shortcut(p);
9241 if (v != NULL)
9242 rettv->vval.v_string = v;
9243 else
9244 rettv->vval.v_string = vim_strsave(p);
9245 }
9246#else
9247# ifdef HAVE_READLINK
9248 {
9249 char_u *cpy;
9250 int len;
9251 char_u *remain = NULL;
9252 char_u *q;
9253 int is_relative_to_current = FALSE;
9254 int has_trailing_pathsep = FALSE;
9255 int limit = 100;
9256
9257 p = vim_strsave(p);
9258
9259 if (p[0] == '.' && (vim_ispathsep(p[1])
9260 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9261 is_relative_to_current = TRUE;
9262
9263 len = STRLEN(p);
9264 if (len > 0 && after_pathsep(p, p + len))
9265 {
9266 has_trailing_pathsep = TRUE;
9267 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9268 }
9269
9270 q = getnextcomp(p);
9271 if (*q != NUL)
9272 {
9273 /* Separate the first path component in "p", and keep the
9274 * remainder (beginning with the path separator). */
9275 remain = vim_strsave(q - 1);
9276 q[-1] = NUL;
9277 }
9278
9279 buf = alloc(MAXPATHL + 1);
9280 if (buf == NULL)
9281 goto fail;
9282
9283 for (;;)
9284 {
9285 for (;;)
9286 {
9287 len = readlink((char *)p, (char *)buf, MAXPATHL);
9288 if (len <= 0)
9289 break;
9290 buf[len] = NUL;
9291
9292 if (limit-- == 0)
9293 {
9294 vim_free(p);
9295 vim_free(remain);
9296 EMSG(_("E655: Too many symbolic links (cycle?)"));
9297 rettv->vval.v_string = NULL;
9298 goto fail;
9299 }
9300
9301 /* Ensure that the result will have a trailing path separator
9302 * if the argument has one. */
9303 if (remain == NULL && has_trailing_pathsep)
9304 add_pathsep(buf);
9305
9306 /* Separate the first path component in the link value and
9307 * concatenate the remainders. */
9308 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9309 if (*q != NUL)
9310 {
9311 if (remain == NULL)
9312 remain = vim_strsave(q - 1);
9313 else
9314 {
9315 cpy = concat_str(q - 1, remain);
9316 if (cpy != NULL)
9317 {
9318 vim_free(remain);
9319 remain = cpy;
9320 }
9321 }
9322 q[-1] = NUL;
9323 }
9324
9325 q = gettail(p);
9326 if (q > p && *q == NUL)
9327 {
9328 /* Ignore trailing path separator. */
9329 q[-1] = NUL;
9330 q = gettail(p);
9331 }
9332 if (q > p && !mch_isFullName(buf))
9333 {
9334 /* symlink is relative to directory of argument */
9335 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9336 if (cpy != NULL)
9337 {
9338 STRCPY(cpy, p);
9339 STRCPY(gettail(cpy), buf);
9340 vim_free(p);
9341 p = cpy;
9342 }
9343 }
9344 else
9345 {
9346 vim_free(p);
9347 p = vim_strsave(buf);
9348 }
9349 }
9350
9351 if (remain == NULL)
9352 break;
9353
9354 /* Append the first path component of "remain" to "p". */
9355 q = getnextcomp(remain + 1);
9356 len = q - remain - (*q != NUL);
9357 cpy = vim_strnsave(p, STRLEN(p) + len);
9358 if (cpy != NULL)
9359 {
9360 STRNCAT(cpy, remain, len);
9361 vim_free(p);
9362 p = cpy;
9363 }
9364 /* Shorten "remain". */
9365 if (*q != NUL)
9366 STRMOVE(remain, q - 1);
9367 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009368 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009369 }
9370
9371 /* If the result is a relative path name, make it explicitly relative to
9372 * the current directory if and only if the argument had this form. */
9373 if (!vim_ispathsep(*p))
9374 {
9375 if (is_relative_to_current
9376 && *p != NUL
9377 && !(p[0] == '.'
9378 && (p[1] == NUL
9379 || vim_ispathsep(p[1])
9380 || (p[1] == '.'
9381 && (p[2] == NUL
9382 || vim_ispathsep(p[2]))))))
9383 {
9384 /* Prepend "./". */
9385 cpy = concat_str((char_u *)"./", p);
9386 if (cpy != NULL)
9387 {
9388 vim_free(p);
9389 p = cpy;
9390 }
9391 }
9392 else if (!is_relative_to_current)
9393 {
9394 /* Strip leading "./". */
9395 q = p;
9396 while (q[0] == '.' && vim_ispathsep(q[1]))
9397 q += 2;
9398 if (q > p)
9399 STRMOVE(p, p + 2);
9400 }
9401 }
9402
9403 /* Ensure that the result will have no trailing path separator
9404 * if the argument had none. But keep "/" or "//". */
9405 if (!has_trailing_pathsep)
9406 {
9407 q = p + STRLEN(p);
9408 if (after_pathsep(p, q))
9409 *gettail_sep(p) = NUL;
9410 }
9411
9412 rettv->vval.v_string = p;
9413 }
9414# else
9415 rettv->vval.v_string = vim_strsave(p);
9416# endif
9417#endif
9418
9419 simplify_filename(rettv->vval.v_string);
9420
9421#ifdef HAVE_READLINK
9422fail:
9423 vim_free(buf);
9424#endif
9425 rettv->v_type = VAR_STRING;
9426}
9427
9428/*
9429 * "reverse({list})" function
9430 */
9431 static void
9432f_reverse(typval_T *argvars, typval_T *rettv)
9433{
9434 list_T *l;
9435 listitem_T *li, *ni;
9436
9437 if (argvars[0].v_type != VAR_LIST)
9438 EMSG2(_(e_listarg), "reverse()");
9439 else if ((l = argvars[0].vval.v_list) != NULL
9440 && !tv_check_lock(l->lv_lock,
9441 (char_u *)N_("reverse() argument"), TRUE))
9442 {
9443 li = l->lv_last;
9444 l->lv_first = l->lv_last = NULL;
9445 l->lv_len = 0;
9446 while (li != NULL)
9447 {
9448 ni = li->li_prev;
9449 list_append(l, li);
9450 li = ni;
9451 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009452 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009453 l->lv_idx = l->lv_len - l->lv_idx - 1;
9454 }
9455}
9456
9457#define SP_NOMOVE 0x01 /* don't move cursor */
9458#define SP_REPEAT 0x02 /* repeat to find outer pair */
9459#define SP_RETCOUNT 0x04 /* return matchcount */
9460#define SP_SETPCMARK 0x08 /* set previous context mark */
9461#define SP_START 0x10 /* accept match at start position */
9462#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9463#define SP_END 0x40 /* leave cursor at end of match */
9464#define SP_COLUMN 0x80 /* start at cursor column */
9465
9466static int get_search_arg(typval_T *varp, int *flagsp);
9467
9468/*
9469 * Get flags for a search function.
9470 * Possibly sets "p_ws".
9471 * Returns BACKWARD, FORWARD or zero (for an error).
9472 */
9473 static int
9474get_search_arg(typval_T *varp, int *flagsp)
9475{
9476 int dir = FORWARD;
9477 char_u *flags;
9478 char_u nbuf[NUMBUFLEN];
9479 int mask;
9480
9481 if (varp->v_type != VAR_UNKNOWN)
9482 {
9483 flags = get_tv_string_buf_chk(varp, nbuf);
9484 if (flags == NULL)
9485 return 0; /* type error; errmsg already given */
9486 while (*flags != NUL)
9487 {
9488 switch (*flags)
9489 {
9490 case 'b': dir = BACKWARD; break;
9491 case 'w': p_ws = TRUE; break;
9492 case 'W': p_ws = FALSE; break;
9493 default: mask = 0;
9494 if (flagsp != NULL)
9495 switch (*flags)
9496 {
9497 case 'c': mask = SP_START; break;
9498 case 'e': mask = SP_END; break;
9499 case 'm': mask = SP_RETCOUNT; break;
9500 case 'n': mask = SP_NOMOVE; break;
9501 case 'p': mask = SP_SUBPAT; break;
9502 case 'r': mask = SP_REPEAT; break;
9503 case 's': mask = SP_SETPCMARK; break;
9504 case 'z': mask = SP_COLUMN; break;
9505 }
9506 if (mask == 0)
9507 {
9508 EMSG2(_(e_invarg2), flags);
9509 dir = 0;
9510 }
9511 else
9512 *flagsp |= mask;
9513 }
9514 if (dir == 0)
9515 break;
9516 ++flags;
9517 }
9518 }
9519 return dir;
9520}
9521
9522/*
9523 * Shared by search() and searchpos() functions.
9524 */
9525 static int
9526search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9527{
9528 int flags;
9529 char_u *pat;
9530 pos_T pos;
9531 pos_T save_cursor;
9532 int save_p_ws = p_ws;
9533 int dir;
9534 int retval = 0; /* default: FAIL */
9535 long lnum_stop = 0;
9536 proftime_T tm;
9537#ifdef FEAT_RELTIME
9538 long time_limit = 0;
9539#endif
9540 int options = SEARCH_KEEP;
9541 int subpatnum;
9542
9543 pat = get_tv_string(&argvars[0]);
9544 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9545 if (dir == 0)
9546 goto theend;
9547 flags = *flagsp;
9548 if (flags & SP_START)
9549 options |= SEARCH_START;
9550 if (flags & SP_END)
9551 options |= SEARCH_END;
9552 if (flags & SP_COLUMN)
9553 options |= SEARCH_COL;
9554
9555 /* Optional arguments: line number to stop searching and timeout. */
9556 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9557 {
9558 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9559 if (lnum_stop < 0)
9560 goto theend;
9561#ifdef FEAT_RELTIME
9562 if (argvars[3].v_type != VAR_UNKNOWN)
9563 {
9564 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9565 if (time_limit < 0)
9566 goto theend;
9567 }
9568#endif
9569 }
9570
9571#ifdef FEAT_RELTIME
9572 /* Set the time limit, if there is one. */
9573 profile_setlimit(time_limit, &tm);
9574#endif
9575
9576 /*
9577 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9578 * Check to make sure only those flags are set.
9579 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9580 * flags cannot be set. Check for that condition also.
9581 */
9582 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9583 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9584 {
9585 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9586 goto theend;
9587 }
9588
9589 pos = save_cursor = curwin->w_cursor;
9590 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009591 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009592 if (subpatnum != FAIL)
9593 {
9594 if (flags & SP_SUBPAT)
9595 retval = subpatnum;
9596 else
9597 retval = pos.lnum;
9598 if (flags & SP_SETPCMARK)
9599 setpcmark();
9600 curwin->w_cursor = pos;
9601 if (match_pos != NULL)
9602 {
9603 /* Store the match cursor position */
9604 match_pos->lnum = pos.lnum;
9605 match_pos->col = pos.col + 1;
9606 }
9607 /* "/$" will put the cursor after the end of the line, may need to
9608 * correct that here */
9609 check_cursor();
9610 }
9611
9612 /* If 'n' flag is used: restore cursor position. */
9613 if (flags & SP_NOMOVE)
9614 curwin->w_cursor = save_cursor;
9615 else
9616 curwin->w_set_curswant = TRUE;
9617theend:
9618 p_ws = save_p_ws;
9619
9620 return retval;
9621}
9622
9623#ifdef FEAT_FLOAT
9624
9625/*
9626 * round() is not in C90, use ceil() or floor() instead.
9627 */
9628 float_T
9629vim_round(float_T f)
9630{
9631 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9632}
9633
9634/*
9635 * "round({float})" function
9636 */
9637 static void
9638f_round(typval_T *argvars, typval_T *rettv)
9639{
9640 float_T f = 0.0;
9641
9642 rettv->v_type = VAR_FLOAT;
9643 if (get_float_arg(argvars, &f) == OK)
9644 rettv->vval.v_float = vim_round(f);
9645 else
9646 rettv->vval.v_float = 0.0;
9647}
9648#endif
9649
9650/*
9651 * "screenattr()" function
9652 */
9653 static void
9654f_screenattr(typval_T *argvars, typval_T *rettv)
9655{
9656 int row;
9657 int col;
9658 int c;
9659
9660 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9661 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9662 if (row < 0 || row >= screen_Rows
9663 || col < 0 || col >= screen_Columns)
9664 c = -1;
9665 else
9666 c = ScreenAttrs[LineOffset[row] + col];
9667 rettv->vval.v_number = c;
9668}
9669
9670/*
9671 * "screenchar()" function
9672 */
9673 static void
9674f_screenchar(typval_T *argvars, typval_T *rettv)
9675{
9676 int row;
9677 int col;
9678 int off;
9679 int c;
9680
9681 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9682 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9683 if (row < 0 || row >= screen_Rows
9684 || col < 0 || col >= screen_Columns)
9685 c = -1;
9686 else
9687 {
9688 off = LineOffset[row] + col;
9689#ifdef FEAT_MBYTE
9690 if (enc_utf8 && ScreenLinesUC[off] != 0)
9691 c = ScreenLinesUC[off];
9692 else
9693#endif
9694 c = ScreenLines[off];
9695 }
9696 rettv->vval.v_number = c;
9697}
9698
9699/*
9700 * "screencol()" function
9701 *
9702 * First column is 1 to be consistent with virtcol().
9703 */
9704 static void
9705f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9706{
9707 rettv->vval.v_number = screen_screencol() + 1;
9708}
9709
9710/*
9711 * "screenrow()" function
9712 */
9713 static void
9714f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9715{
9716 rettv->vval.v_number = screen_screenrow() + 1;
9717}
9718
9719/*
9720 * "search()" function
9721 */
9722 static void
9723f_search(typval_T *argvars, typval_T *rettv)
9724{
9725 int flags = 0;
9726
9727 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9728}
9729
9730/*
9731 * "searchdecl()" function
9732 */
9733 static void
9734f_searchdecl(typval_T *argvars, typval_T *rettv)
9735{
9736 int locally = 1;
9737 int thisblock = 0;
9738 int error = FALSE;
9739 char_u *name;
9740
9741 rettv->vval.v_number = 1; /* default: FAIL */
9742
9743 name = get_tv_string_chk(&argvars[0]);
9744 if (argvars[1].v_type != VAR_UNKNOWN)
9745 {
9746 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9747 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9748 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9749 }
9750 if (!error && name != NULL)
9751 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9752 locally, thisblock, SEARCH_KEEP) == FAIL;
9753}
9754
9755/*
9756 * Used by searchpair() and searchpairpos()
9757 */
9758 static int
9759searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9760{
9761 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009762 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009763 int save_p_ws = p_ws;
9764 int dir;
9765 int flags = 0;
9766 char_u nbuf1[NUMBUFLEN];
9767 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009768 int retval = 0; /* default: FAIL */
9769 long lnum_stop = 0;
9770 long time_limit = 0;
9771
9772 /* Get the three pattern arguments: start, middle, end. */
9773 spat = get_tv_string_chk(&argvars[0]);
9774 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9775 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9776 if (spat == NULL || mpat == NULL || epat == NULL)
9777 goto theend; /* type error */
9778
9779 /* Handle the optional fourth argument: flags */
9780 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9781 if (dir == 0)
9782 goto theend;
9783
9784 /* Don't accept SP_END or SP_SUBPAT.
9785 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9786 */
9787 if ((flags & (SP_END | SP_SUBPAT)) != 0
9788 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9789 {
9790 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9791 goto theend;
9792 }
9793
9794 /* Using 'r' implies 'W', otherwise it doesn't work. */
9795 if (flags & SP_REPEAT)
9796 p_ws = FALSE;
9797
9798 /* Optional fifth argument: skip expression */
9799 if (argvars[3].v_type == VAR_UNKNOWN
9800 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009801 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009802 else
9803 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009804 skip = &argvars[4];
9805 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9806 && skip->v_type != VAR_STRING)
9807 {
9808 /* Type error */
9809 goto theend;
9810 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009811 if (argvars[5].v_type != VAR_UNKNOWN)
9812 {
9813 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9814 if (lnum_stop < 0)
9815 goto theend;
9816#ifdef FEAT_RELTIME
9817 if (argvars[6].v_type != VAR_UNKNOWN)
9818 {
9819 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9820 if (time_limit < 0)
9821 goto theend;
9822 }
9823#endif
9824 }
9825 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009826
9827 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9828 match_pos, lnum_stop, time_limit);
9829
9830theend:
9831 p_ws = save_p_ws;
9832
9833 return retval;
9834}
9835
9836/*
9837 * "searchpair()" function
9838 */
9839 static void
9840f_searchpair(typval_T *argvars, typval_T *rettv)
9841{
9842 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9843}
9844
9845/*
9846 * "searchpairpos()" function
9847 */
9848 static void
9849f_searchpairpos(typval_T *argvars, typval_T *rettv)
9850{
9851 pos_T match_pos;
9852 int lnum = 0;
9853 int col = 0;
9854
9855 if (rettv_list_alloc(rettv) == FAIL)
9856 return;
9857
9858 if (searchpair_cmn(argvars, &match_pos) > 0)
9859 {
9860 lnum = match_pos.lnum;
9861 col = match_pos.col;
9862 }
9863
9864 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9865 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9866}
9867
9868/*
9869 * Search for a start/middle/end thing.
9870 * Used by searchpair(), see its documentation for the details.
9871 * Returns 0 or -1 for no match,
9872 */
9873 long
9874do_searchpair(
9875 char_u *spat, /* start pattern */
9876 char_u *mpat, /* middle pattern */
9877 char_u *epat, /* end pattern */
9878 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009879 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009880 int flags, /* SP_SETPCMARK and other SP_ values */
9881 pos_T *match_pos,
9882 linenr_T lnum_stop, /* stop at this line if not zero */
9883 long time_limit UNUSED) /* stop after this many msec */
9884{
9885 char_u *save_cpo;
9886 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9887 long retval = 0;
9888 pos_T pos;
9889 pos_T firstpos;
9890 pos_T foundpos;
9891 pos_T save_cursor;
9892 pos_T save_pos;
9893 int n;
9894 int r;
9895 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009896 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009897 int err;
9898 int options = SEARCH_KEEP;
9899 proftime_T tm;
9900
9901 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9902 save_cpo = p_cpo;
9903 p_cpo = empty_option;
9904
9905#ifdef FEAT_RELTIME
9906 /* Set the time limit, if there is one. */
9907 profile_setlimit(time_limit, &tm);
9908#endif
9909
9910 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9911 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009912 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9913 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009914 if (pat2 == NULL || pat3 == NULL)
9915 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009916 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009917 if (*mpat == NUL)
9918 STRCPY(pat3, pat2);
9919 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009920 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009921 spat, epat, mpat);
9922 if (flags & SP_START)
9923 options |= SEARCH_START;
9924
Bram Moolenaar48570482017-10-30 21:48:41 +01009925 if (skip != NULL)
9926 {
9927 /* Empty string means to not use the skip expression. */
9928 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9929 use_skip = skip->vval.v_string != NULL
9930 && *skip->vval.v_string != NUL;
9931 }
9932
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009933 save_cursor = curwin->w_cursor;
9934 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009935 CLEAR_POS(&firstpos);
9936 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009937 pat = pat3;
9938 for (;;)
9939 {
9940 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009941 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009942 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009943 /* didn't find it or found the first match again: FAIL */
9944 break;
9945
9946 if (firstpos.lnum == 0)
9947 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009948 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009949 {
9950 /* Found the same position again. Can happen with a pattern that
9951 * has "\zs" at the end and searching backwards. Advance one
9952 * character and try again. */
9953 if (dir == BACKWARD)
9954 decl(&pos);
9955 else
9956 incl(&pos);
9957 }
9958 foundpos = pos;
9959
9960 /* clear the start flag to avoid getting stuck here */
9961 options &= ~SEARCH_START;
9962
9963 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009964 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009965 {
9966 save_pos = curwin->w_cursor;
9967 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009968 err = FALSE;
9969 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009970 curwin->w_cursor = save_pos;
9971 if (err)
9972 {
9973 /* Evaluating {skip} caused an error, break here. */
9974 curwin->w_cursor = save_cursor;
9975 retval = -1;
9976 break;
9977 }
9978 if (r)
9979 continue;
9980 }
9981
9982 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9983 {
9984 /* Found end when searching backwards or start when searching
9985 * forward: nested pair. */
9986 ++nest;
9987 pat = pat2; /* nested, don't search for middle */
9988 }
9989 else
9990 {
9991 /* Found end when searching forward or start when searching
9992 * backward: end of (nested) pair; or found middle in outer pair. */
9993 if (--nest == 1)
9994 pat = pat3; /* outer level, search for middle */
9995 }
9996
9997 if (nest == 0)
9998 {
9999 /* Found the match: return matchcount or line number. */
10000 if (flags & SP_RETCOUNT)
10001 ++retval;
10002 else
10003 retval = pos.lnum;
10004 if (flags & SP_SETPCMARK)
10005 setpcmark();
10006 curwin->w_cursor = pos;
10007 if (!(flags & SP_REPEAT))
10008 break;
10009 nest = 1; /* search for next unmatched */
10010 }
10011 }
10012
10013 if (match_pos != NULL)
10014 {
10015 /* Store the match cursor position */
10016 match_pos->lnum = curwin->w_cursor.lnum;
10017 match_pos->col = curwin->w_cursor.col + 1;
10018 }
10019
10020 /* If 'n' flag is used or search failed: restore cursor position. */
10021 if ((flags & SP_NOMOVE) || retval == 0)
10022 curwin->w_cursor = save_cursor;
10023
10024theend:
10025 vim_free(pat2);
10026 vim_free(pat3);
10027 if (p_cpo == empty_option)
10028 p_cpo = save_cpo;
10029 else
10030 /* Darn, evaluating the {skip} expression changed the value. */
10031 free_string_option(save_cpo);
10032
10033 return retval;
10034}
10035
10036/*
10037 * "searchpos()" function
10038 */
10039 static void
10040f_searchpos(typval_T *argvars, typval_T *rettv)
10041{
10042 pos_T match_pos;
10043 int lnum = 0;
10044 int col = 0;
10045 int n;
10046 int flags = 0;
10047
10048 if (rettv_list_alloc(rettv) == FAIL)
10049 return;
10050
10051 n = search_cmn(argvars, &match_pos, &flags);
10052 if (n > 0)
10053 {
10054 lnum = match_pos.lnum;
10055 col = match_pos.col;
10056 }
10057
10058 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10059 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10060 if (flags & SP_SUBPAT)
10061 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10062}
10063
10064 static void
10065f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10066{
10067#ifdef FEAT_CLIENTSERVER
10068 char_u buf[NUMBUFLEN];
10069 char_u *server = get_tv_string_chk(&argvars[0]);
10070 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10071
10072 rettv->vval.v_number = -1;
10073 if (server == NULL || reply == NULL)
10074 return;
10075 if (check_restricted() || check_secure())
10076 return;
10077# ifdef FEAT_X11
10078 if (check_connection() == FAIL)
10079 return;
10080# endif
10081
10082 if (serverSendReply(server, reply) < 0)
10083 {
10084 EMSG(_("E258: Unable to send to client"));
10085 return;
10086 }
10087 rettv->vval.v_number = 0;
10088#else
10089 rettv->vval.v_number = -1;
10090#endif
10091}
10092
10093 static void
10094f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10095{
10096 char_u *r = NULL;
10097
10098#ifdef FEAT_CLIENTSERVER
10099# ifdef WIN32
10100 r = serverGetVimNames();
10101# else
10102 make_connection();
10103 if (X_DISPLAY != NULL)
10104 r = serverGetVimNames(X_DISPLAY);
10105# endif
10106#endif
10107 rettv->v_type = VAR_STRING;
10108 rettv->vval.v_string = r;
10109}
10110
10111/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010112 * Set line or list of lines in buffer "buf".
10113 */
10114 static void
10115set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
10116{
10117 char_u *line = NULL;
10118 list_T *l = NULL;
10119 listitem_T *li = NULL;
10120 long added = 0;
10121 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010122 buf_T *curbuf_save = NULL;
10123 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010124 int is_curbuf = buf == curbuf;
10125
Bram Moolenaar9d954202017-09-04 20:34:19 +020010126 /* When using the current buffer ml_mfp will be set if needed. Useful when
10127 * setline() is used on startup. For other buffers the buffer must be
10128 * loaded. */
10129 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010130 {
10131 rettv->vval.v_number = 1; /* FAIL */
10132 return;
10133 }
10134
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010135 if (!is_curbuf)
10136 {
10137 wininfo_T *wip;
10138
10139 curbuf_save = curbuf;
10140 curwin_save = curwin;
10141 curbuf = buf;
10142 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10143 {
10144 if (wip->wi_win != NULL)
10145 {
10146 curwin = wip->wi_win;
10147 break;
10148 }
10149 }
10150 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010151
10152 lcount = curbuf->b_ml.ml_line_count;
10153
10154 if (lines->v_type == VAR_LIST)
10155 {
10156 l = lines->vval.v_list;
10157 li = l->lv_first;
10158 }
10159 else
10160 line = get_tv_string_chk(lines);
10161
10162 /* default result is zero == OK */
10163 for (;;)
10164 {
10165 if (l != NULL)
10166 {
10167 /* list argument, get next string */
10168 if (li == NULL)
10169 break;
10170 line = get_tv_string_chk(&li->li_tv);
10171 li = li->li_next;
10172 }
10173
10174 rettv->vval.v_number = 1; /* FAIL */
10175 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10176 break;
10177
10178 /* When coming here from Insert mode, sync undo, so that this can be
10179 * undone separately from what was previously inserted. */
10180 if (u_sync_once == 2)
10181 {
10182 u_sync_once = 1; /* notify that u_sync() was called */
10183 u_sync(TRUE);
10184 }
10185
10186 if (lnum <= curbuf->b_ml.ml_line_count)
10187 {
10188 /* existing line, replace it */
10189 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10190 {
10191 changed_bytes(lnum, 0);
10192 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10193 check_cursor_col();
10194 rettv->vval.v_number = 0; /* OK */
10195 }
10196 }
10197 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10198 {
10199 /* lnum is one past the last line, append the line */
10200 ++added;
10201 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10202 rettv->vval.v_number = 0; /* OK */
10203 }
10204
10205 if (l == NULL) /* only one string argument */
10206 break;
10207 ++lnum;
10208 }
10209
10210 if (added > 0)
10211 appended_lines_mark(lcount, added);
10212
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010213 if (!is_curbuf)
10214 {
10215 curbuf = curbuf_save;
10216 curwin = curwin_save;
10217 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010218}
10219
10220/*
10221 * "setbufline()" function
10222 */
10223 static void
10224f_setbufline(argvars, rettv)
10225 typval_T *argvars;
10226 typval_T *rettv;
10227{
10228 linenr_T lnum;
10229 buf_T *buf;
10230
10231 buf = get_buf_tv(&argvars[0], FALSE);
10232 if (buf == NULL)
10233 rettv->vval.v_number = 1; /* FAIL */
10234 else
10235 {
10236 lnum = get_tv_lnum_buf(&argvars[1], buf);
10237
10238 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10239 }
10240}
10241
10242/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010243 * "setbufvar()" function
10244 */
10245 static void
10246f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10247{
10248 buf_T *buf;
10249 char_u *varname, *bufvarname;
10250 typval_T *varp;
10251 char_u nbuf[NUMBUFLEN];
10252
10253 if (check_restricted() || check_secure())
10254 return;
10255 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10256 varname = get_tv_string_chk(&argvars[1]);
10257 buf = get_buf_tv(&argvars[0], FALSE);
10258 varp = &argvars[2];
10259
10260 if (buf != NULL && varname != NULL && varp != NULL)
10261 {
10262 if (*varname == '&')
10263 {
10264 long numval;
10265 char_u *strval;
10266 int error = FALSE;
10267 aco_save_T aco;
10268
10269 /* set curbuf to be our buf, temporarily */
10270 aucmd_prepbuf(&aco, buf);
10271
10272 ++varname;
10273 numval = (long)get_tv_number_chk(varp, &error);
10274 strval = get_tv_string_buf_chk(varp, nbuf);
10275 if (!error && strval != NULL)
10276 set_option_value(varname, numval, strval, OPT_LOCAL);
10277
10278 /* reset notion of buffer */
10279 aucmd_restbuf(&aco);
10280 }
10281 else
10282 {
10283 buf_T *save_curbuf = curbuf;
10284
10285 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10286 if (bufvarname != NULL)
10287 {
10288 curbuf = buf;
10289 STRCPY(bufvarname, "b:");
10290 STRCPY(bufvarname + 2, varname);
10291 set_var(bufvarname, varp, TRUE);
10292 vim_free(bufvarname);
10293 curbuf = save_curbuf;
10294 }
10295 }
10296 }
10297}
10298
10299 static void
10300f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10301{
10302 dict_T *d;
10303 dictitem_T *di;
10304 char_u *csearch;
10305
10306 if (argvars[0].v_type != VAR_DICT)
10307 {
10308 EMSG(_(e_dictreq));
10309 return;
10310 }
10311
10312 if ((d = argvars[0].vval.v_dict) != NULL)
10313 {
10314 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10315 if (csearch != NULL)
10316 {
10317#ifdef FEAT_MBYTE
10318 if (enc_utf8)
10319 {
10320 int pcc[MAX_MCO];
10321 int c = utfc_ptr2char(csearch, pcc);
10322
10323 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10324 }
10325 else
10326#endif
10327 set_last_csearch(PTR2CHAR(csearch),
10328 csearch, MB_PTR2LEN(csearch));
10329 }
10330
10331 di = dict_find(d, (char_u *)"forward", -1);
10332 if (di != NULL)
10333 set_csearch_direction((int)get_tv_number(&di->di_tv)
10334 ? FORWARD : BACKWARD);
10335
10336 di = dict_find(d, (char_u *)"until", -1);
10337 if (di != NULL)
10338 set_csearch_until(!!get_tv_number(&di->di_tv));
10339 }
10340}
10341
10342/*
10343 * "setcmdpos()" function
10344 */
10345 static void
10346f_setcmdpos(typval_T *argvars, typval_T *rettv)
10347{
10348 int pos = (int)get_tv_number(&argvars[0]) - 1;
10349
10350 if (pos >= 0)
10351 rettv->vval.v_number = set_cmdline_pos(pos);
10352}
10353
10354/*
10355 * "setfperm({fname}, {mode})" function
10356 */
10357 static void
10358f_setfperm(typval_T *argvars, typval_T *rettv)
10359{
10360 char_u *fname;
10361 char_u modebuf[NUMBUFLEN];
10362 char_u *mode_str;
10363 int i;
10364 int mask;
10365 int mode = 0;
10366
10367 rettv->vval.v_number = 0;
10368 fname = get_tv_string_chk(&argvars[0]);
10369 if (fname == NULL)
10370 return;
10371 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10372 if (mode_str == NULL)
10373 return;
10374 if (STRLEN(mode_str) != 9)
10375 {
10376 EMSG2(_(e_invarg2), mode_str);
10377 return;
10378 }
10379
10380 mask = 1;
10381 for (i = 8; i >= 0; --i)
10382 {
10383 if (mode_str[i] != '-')
10384 mode |= mask;
10385 mask = mask << 1;
10386 }
10387 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10388}
10389
10390/*
10391 * "setline()" function
10392 */
10393 static void
10394f_setline(typval_T *argvars, typval_T *rettv)
10395{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010396 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010397
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010398 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010399}
10400
Bram Moolenaard823fa92016-08-12 16:29:27 +020010401static 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 +020010402
10403/*
10404 * Used by "setqflist()" and "setloclist()" functions
10405 */
10406 static void
10407set_qf_ll_list(
10408 win_T *wp UNUSED,
10409 typval_T *list_arg UNUSED,
10410 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010411 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010412 typval_T *rettv)
10413{
10414#ifdef FEAT_QUICKFIX
10415 static char *e_invact = N_("E927: Invalid action: '%s'");
10416 char_u *act;
10417 int action = 0;
10418#endif
10419
10420 rettv->vval.v_number = -1;
10421
10422#ifdef FEAT_QUICKFIX
10423 if (list_arg->v_type != VAR_LIST)
10424 EMSG(_(e_listreq));
10425 else
10426 {
10427 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010428 dict_T *d = NULL;
10429 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010430
10431 if (action_arg->v_type == VAR_STRING)
10432 {
10433 act = get_tv_string_chk(action_arg);
10434 if (act == NULL)
10435 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010436 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10437 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010438 action = *act;
10439 else
10440 EMSG2(_(e_invact), act);
10441 }
10442 else if (action_arg->v_type == VAR_UNKNOWN)
10443 action = ' ';
10444 else
10445 EMSG(_(e_stringreq));
10446
Bram Moolenaard823fa92016-08-12 16:29:27 +020010447 if (action_arg->v_type != VAR_UNKNOWN
10448 && what_arg->v_type != VAR_UNKNOWN)
10449 {
10450 if (what_arg->v_type == VAR_DICT)
10451 d = what_arg->vval.v_dict;
10452 else
10453 {
10454 EMSG(_(e_dictreq));
10455 valid_dict = FALSE;
10456 }
10457 }
10458
10459 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10460 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010461 rettv->vval.v_number = 0;
10462 }
10463#endif
10464}
10465
10466/*
10467 * "setloclist()" function
10468 */
10469 static void
10470f_setloclist(typval_T *argvars, typval_T *rettv)
10471{
10472 win_T *win;
10473
10474 rettv->vval.v_number = -1;
10475
10476 win = find_win_by_nr(&argvars[0], NULL);
10477 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010478 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010479}
10480
10481/*
10482 * "setmatches()" function
10483 */
10484 static void
10485f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10486{
10487#ifdef FEAT_SEARCH_EXTRA
10488 list_T *l;
10489 listitem_T *li;
10490 dict_T *d;
10491 list_T *s = NULL;
10492
10493 rettv->vval.v_number = -1;
10494 if (argvars[0].v_type != VAR_LIST)
10495 {
10496 EMSG(_(e_listreq));
10497 return;
10498 }
10499 if ((l = argvars[0].vval.v_list) != NULL)
10500 {
10501
10502 /* To some extent make sure that we are dealing with a list from
10503 * "getmatches()". */
10504 li = l->lv_first;
10505 while (li != NULL)
10506 {
10507 if (li->li_tv.v_type != VAR_DICT
10508 || (d = li->li_tv.vval.v_dict) == NULL)
10509 {
10510 EMSG(_(e_invarg));
10511 return;
10512 }
10513 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10514 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10515 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10516 && dict_find(d, (char_u *)"priority", -1) != NULL
10517 && dict_find(d, (char_u *)"id", -1) != NULL))
10518 {
10519 EMSG(_(e_invarg));
10520 return;
10521 }
10522 li = li->li_next;
10523 }
10524
10525 clear_matches(curwin);
10526 li = l->lv_first;
10527 while (li != NULL)
10528 {
10529 int i = 0;
10530 char_u buf[5];
10531 dictitem_T *di;
10532 char_u *group;
10533 int priority;
10534 int id;
10535 char_u *conceal;
10536
10537 d = li->li_tv.vval.v_dict;
10538 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10539 {
10540 if (s == NULL)
10541 {
10542 s = list_alloc();
10543 if (s == NULL)
10544 return;
10545 }
10546
10547 /* match from matchaddpos() */
10548 for (i = 1; i < 9; i++)
10549 {
10550 sprintf((char *)buf, (char *)"pos%d", i);
10551 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10552 {
10553 if (di->di_tv.v_type != VAR_LIST)
10554 return;
10555
10556 list_append_tv(s, &di->di_tv);
10557 s->lv_refcount++;
10558 }
10559 else
10560 break;
10561 }
10562 }
10563
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010564 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010565 priority = (int)get_dict_number(d, (char_u *)"priority");
10566 id = (int)get_dict_number(d, (char_u *)"id");
10567 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010568 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010569 : NULL;
10570 if (i == 0)
10571 {
10572 match_add(curwin, group,
10573 get_dict_string(d, (char_u *)"pattern", FALSE),
10574 priority, id, NULL, conceal);
10575 }
10576 else
10577 {
10578 match_add(curwin, group, NULL, priority, id, s, conceal);
10579 list_unref(s);
10580 s = NULL;
10581 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010582 vim_free(group);
10583 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010584
10585 li = li->li_next;
10586 }
10587 rettv->vval.v_number = 0;
10588 }
10589#endif
10590}
10591
10592/*
10593 * "setpos()" function
10594 */
10595 static void
10596f_setpos(typval_T *argvars, typval_T *rettv)
10597{
10598 pos_T pos;
10599 int fnum;
10600 char_u *name;
10601 colnr_T curswant = -1;
10602
10603 rettv->vval.v_number = -1;
10604 name = get_tv_string_chk(argvars);
10605 if (name != NULL)
10606 {
10607 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10608 {
10609 if (--pos.col < 0)
10610 pos.col = 0;
10611 if (name[0] == '.' && name[1] == NUL)
10612 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010613 /* set cursor; "fnum" is ignored */
10614 curwin->w_cursor = pos;
10615 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010616 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010617 curwin->w_curswant = curswant - 1;
10618 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010619 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010620 check_cursor();
10621 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010622 }
10623 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10624 {
10625 /* set mark */
10626 if (setmark_pos(name[1], &pos, fnum) == OK)
10627 rettv->vval.v_number = 0;
10628 }
10629 else
10630 EMSG(_(e_invarg));
10631 }
10632 }
10633}
10634
10635/*
10636 * "setqflist()" function
10637 */
10638 static void
10639f_setqflist(typval_T *argvars, typval_T *rettv)
10640{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010641 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010642}
10643
10644/*
10645 * "setreg()" function
10646 */
10647 static void
10648f_setreg(typval_T *argvars, typval_T *rettv)
10649{
10650 int regname;
10651 char_u *strregname;
10652 char_u *stropt;
10653 char_u *strval;
10654 int append;
10655 char_u yank_type;
10656 long block_len;
10657
10658 block_len = -1;
10659 yank_type = MAUTO;
10660 append = FALSE;
10661
10662 strregname = get_tv_string_chk(argvars);
10663 rettv->vval.v_number = 1; /* FAIL is default */
10664
10665 if (strregname == NULL)
10666 return; /* type error; errmsg already given */
10667 regname = *strregname;
10668 if (regname == 0 || regname == '@')
10669 regname = '"';
10670
10671 if (argvars[2].v_type != VAR_UNKNOWN)
10672 {
10673 stropt = get_tv_string_chk(&argvars[2]);
10674 if (stropt == NULL)
10675 return; /* type error */
10676 for (; *stropt != NUL; ++stropt)
10677 switch (*stropt)
10678 {
10679 case 'a': case 'A': /* append */
10680 append = TRUE;
10681 break;
10682 case 'v': case 'c': /* character-wise selection */
10683 yank_type = MCHAR;
10684 break;
10685 case 'V': case 'l': /* line-wise selection */
10686 yank_type = MLINE;
10687 break;
10688 case 'b': case Ctrl_V: /* block-wise selection */
10689 yank_type = MBLOCK;
10690 if (VIM_ISDIGIT(stropt[1]))
10691 {
10692 ++stropt;
10693 block_len = getdigits(&stropt) - 1;
10694 --stropt;
10695 }
10696 break;
10697 }
10698 }
10699
10700 if (argvars[1].v_type == VAR_LIST)
10701 {
10702 char_u **lstval;
10703 char_u **allocval;
10704 char_u buf[NUMBUFLEN];
10705 char_u **curval;
10706 char_u **curallocval;
10707 list_T *ll = argvars[1].vval.v_list;
10708 listitem_T *li;
10709 int len;
10710
10711 /* If the list is NULL handle like an empty list. */
10712 len = ll == NULL ? 0 : ll->lv_len;
10713
10714 /* First half: use for pointers to result lines; second half: use for
10715 * pointers to allocated copies. */
10716 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10717 if (lstval == NULL)
10718 return;
10719 curval = lstval;
10720 allocval = lstval + len + 2;
10721 curallocval = allocval;
10722
10723 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10724 li = li->li_next)
10725 {
10726 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10727 if (strval == NULL)
10728 goto free_lstval;
10729 if (strval == buf)
10730 {
10731 /* Need to make a copy, next get_tv_string_buf_chk() will
10732 * overwrite the string. */
10733 strval = vim_strsave(buf);
10734 if (strval == NULL)
10735 goto free_lstval;
10736 *curallocval++ = strval;
10737 }
10738 *curval++ = strval;
10739 }
10740 *curval++ = NULL;
10741
10742 write_reg_contents_lst(regname, lstval, -1,
10743 append, yank_type, block_len);
10744free_lstval:
10745 while (curallocval > allocval)
10746 vim_free(*--curallocval);
10747 vim_free(lstval);
10748 }
10749 else
10750 {
10751 strval = get_tv_string_chk(&argvars[1]);
10752 if (strval == NULL)
10753 return;
10754 write_reg_contents_ex(regname, strval, -1,
10755 append, yank_type, block_len);
10756 }
10757 rettv->vval.v_number = 0;
10758}
10759
10760/*
10761 * "settabvar()" function
10762 */
10763 static void
10764f_settabvar(typval_T *argvars, typval_T *rettv)
10765{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010766 tabpage_T *save_curtab;
10767 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010768 char_u *varname, *tabvarname;
10769 typval_T *varp;
10770
10771 rettv->vval.v_number = 0;
10772
10773 if (check_restricted() || check_secure())
10774 return;
10775
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010776 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010777 varname = get_tv_string_chk(&argvars[1]);
10778 varp = &argvars[2];
10779
Bram Moolenaar4033c552017-09-16 20:54:51 +020010780 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010781 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010782 save_curtab = curtab;
10783 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010784
10785 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10786 if (tabvarname != NULL)
10787 {
10788 STRCPY(tabvarname, "t:");
10789 STRCPY(tabvarname + 2, varname);
10790 set_var(tabvarname, varp, TRUE);
10791 vim_free(tabvarname);
10792 }
10793
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010794 /* Restore current tabpage */
10795 if (valid_tabpage(save_curtab))
10796 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010797 }
10798}
10799
10800/*
10801 * "settabwinvar()" function
10802 */
10803 static void
10804f_settabwinvar(typval_T *argvars, typval_T *rettv)
10805{
10806 setwinvar(argvars, rettv, 1);
10807}
10808
10809/*
10810 * "setwinvar()" function
10811 */
10812 static void
10813f_setwinvar(typval_T *argvars, typval_T *rettv)
10814{
10815 setwinvar(argvars, rettv, 0);
10816}
10817
10818#ifdef FEAT_CRYPT
10819/*
10820 * "sha256({string})" function
10821 */
10822 static void
10823f_sha256(typval_T *argvars, typval_T *rettv)
10824{
10825 char_u *p;
10826
10827 p = get_tv_string(&argvars[0]);
10828 rettv->vval.v_string = vim_strsave(
10829 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10830 rettv->v_type = VAR_STRING;
10831}
10832#endif /* FEAT_CRYPT */
10833
10834/*
10835 * "shellescape({string})" function
10836 */
10837 static void
10838f_shellescape(typval_T *argvars, typval_T *rettv)
10839{
Bram Moolenaar20615522017-06-05 18:46:26 +020010840 int do_special = non_zero_arg(&argvars[1]);
10841
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010842 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010843 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010844 rettv->v_type = VAR_STRING;
10845}
10846
10847/*
10848 * shiftwidth() function
10849 */
10850 static void
10851f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10852{
10853 rettv->vval.v_number = get_sw_value(curbuf);
10854}
10855
10856/*
10857 * "simplify()" function
10858 */
10859 static void
10860f_simplify(typval_T *argvars, typval_T *rettv)
10861{
10862 char_u *p;
10863
10864 p = get_tv_string(&argvars[0]);
10865 rettv->vval.v_string = vim_strsave(p);
10866 simplify_filename(rettv->vval.v_string); /* simplify in place */
10867 rettv->v_type = VAR_STRING;
10868}
10869
10870#ifdef FEAT_FLOAT
10871/*
10872 * "sin()" function
10873 */
10874 static void
10875f_sin(typval_T *argvars, typval_T *rettv)
10876{
10877 float_T f = 0.0;
10878
10879 rettv->v_type = VAR_FLOAT;
10880 if (get_float_arg(argvars, &f) == OK)
10881 rettv->vval.v_float = sin(f);
10882 else
10883 rettv->vval.v_float = 0.0;
10884}
10885
10886/*
10887 * "sinh()" function
10888 */
10889 static void
10890f_sinh(typval_T *argvars, typval_T *rettv)
10891{
10892 float_T f = 0.0;
10893
10894 rettv->v_type = VAR_FLOAT;
10895 if (get_float_arg(argvars, &f) == OK)
10896 rettv->vval.v_float = sinh(f);
10897 else
10898 rettv->vval.v_float = 0.0;
10899}
10900#endif
10901
10902static int
10903#ifdef __BORLANDC__
10904 _RTLENTRYF
10905#endif
10906 item_compare(const void *s1, const void *s2);
10907static int
10908#ifdef __BORLANDC__
10909 _RTLENTRYF
10910#endif
10911 item_compare2(const void *s1, const void *s2);
10912
10913/* struct used in the array that's given to qsort() */
10914typedef struct
10915{
10916 listitem_T *item;
10917 int idx;
10918} sortItem_T;
10919
10920/* struct storing information about current sort */
10921typedef struct
10922{
10923 int item_compare_ic;
10924 int item_compare_numeric;
10925 int item_compare_numbers;
10926#ifdef FEAT_FLOAT
10927 int item_compare_float;
10928#endif
10929 char_u *item_compare_func;
10930 partial_T *item_compare_partial;
10931 dict_T *item_compare_selfdict;
10932 int item_compare_func_err;
10933 int item_compare_keep_zero;
10934} sortinfo_T;
10935static sortinfo_T *sortinfo = NULL;
10936static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10937#define ITEM_COMPARE_FAIL 999
10938
10939/*
10940 * Compare functions for f_sort() and f_uniq() below.
10941 */
10942 static int
10943#ifdef __BORLANDC__
10944_RTLENTRYF
10945#endif
10946item_compare(const void *s1, const void *s2)
10947{
10948 sortItem_T *si1, *si2;
10949 typval_T *tv1, *tv2;
10950 char_u *p1, *p2;
10951 char_u *tofree1 = NULL, *tofree2 = NULL;
10952 int res;
10953 char_u numbuf1[NUMBUFLEN];
10954 char_u numbuf2[NUMBUFLEN];
10955
10956 si1 = (sortItem_T *)s1;
10957 si2 = (sortItem_T *)s2;
10958 tv1 = &si1->item->li_tv;
10959 tv2 = &si2->item->li_tv;
10960
10961 if (sortinfo->item_compare_numbers)
10962 {
10963 varnumber_T v1 = get_tv_number(tv1);
10964 varnumber_T v2 = get_tv_number(tv2);
10965
10966 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10967 }
10968
10969#ifdef FEAT_FLOAT
10970 if (sortinfo->item_compare_float)
10971 {
10972 float_T v1 = get_tv_float(tv1);
10973 float_T v2 = get_tv_float(tv2);
10974
10975 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10976 }
10977#endif
10978
10979 /* tv2string() puts quotes around a string and allocates memory. Don't do
10980 * that for string variables. Use a single quote when comparing with a
10981 * non-string to do what the docs promise. */
10982 if (tv1->v_type == VAR_STRING)
10983 {
10984 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10985 p1 = (char_u *)"'";
10986 else
10987 p1 = tv1->vval.v_string;
10988 }
10989 else
10990 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10991 if (tv2->v_type == VAR_STRING)
10992 {
10993 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10994 p2 = (char_u *)"'";
10995 else
10996 p2 = tv2->vval.v_string;
10997 }
10998 else
10999 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11000 if (p1 == NULL)
11001 p1 = (char_u *)"";
11002 if (p2 == NULL)
11003 p2 = (char_u *)"";
11004 if (!sortinfo->item_compare_numeric)
11005 {
11006 if (sortinfo->item_compare_ic)
11007 res = STRICMP(p1, p2);
11008 else
11009 res = STRCMP(p1, p2);
11010 }
11011 else
11012 {
11013 double n1, n2;
11014 n1 = strtod((char *)p1, (char **)&p1);
11015 n2 = strtod((char *)p2, (char **)&p2);
11016 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11017 }
11018
11019 /* When the result would be zero, compare the item indexes. Makes the
11020 * sort stable. */
11021 if (res == 0 && !sortinfo->item_compare_keep_zero)
11022 res = si1->idx > si2->idx ? 1 : -1;
11023
11024 vim_free(tofree1);
11025 vim_free(tofree2);
11026 return res;
11027}
11028
11029 static int
11030#ifdef __BORLANDC__
11031_RTLENTRYF
11032#endif
11033item_compare2(const void *s1, const void *s2)
11034{
11035 sortItem_T *si1, *si2;
11036 int res;
11037 typval_T rettv;
11038 typval_T argv[3];
11039 int dummy;
11040 char_u *func_name;
11041 partial_T *partial = sortinfo->item_compare_partial;
11042
11043 /* shortcut after failure in previous call; compare all items equal */
11044 if (sortinfo->item_compare_func_err)
11045 return 0;
11046
11047 si1 = (sortItem_T *)s1;
11048 si2 = (sortItem_T *)s2;
11049
11050 if (partial == NULL)
11051 func_name = sortinfo->item_compare_func;
11052 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011053 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011054
11055 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11056 * in the copy without changing the original list items. */
11057 copy_tv(&si1->item->li_tv, &argv[0]);
11058 copy_tv(&si2->item->li_tv, &argv[1]);
11059
11060 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11061 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011062 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011063 partial, sortinfo->item_compare_selfdict);
11064 clear_tv(&argv[0]);
11065 clear_tv(&argv[1]);
11066
11067 if (res == FAIL)
11068 res = ITEM_COMPARE_FAIL;
11069 else
11070 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11071 if (sortinfo->item_compare_func_err)
11072 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11073 clear_tv(&rettv);
11074
11075 /* When the result would be zero, compare the pointers themselves. Makes
11076 * the sort stable. */
11077 if (res == 0 && !sortinfo->item_compare_keep_zero)
11078 res = si1->idx > si2->idx ? 1 : -1;
11079
11080 return res;
11081}
11082
11083/*
11084 * "sort({list})" function
11085 */
11086 static void
11087do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11088{
11089 list_T *l;
11090 listitem_T *li;
11091 sortItem_T *ptrs;
11092 sortinfo_T *old_sortinfo;
11093 sortinfo_T info;
11094 long len;
11095 long i;
11096
11097 /* Pointer to current info struct used in compare function. Save and
11098 * restore the current one for nested calls. */
11099 old_sortinfo = sortinfo;
11100 sortinfo = &info;
11101
11102 if (argvars[0].v_type != VAR_LIST)
11103 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11104 else
11105 {
11106 l = argvars[0].vval.v_list;
11107 if (l == NULL || tv_check_lock(l->lv_lock,
11108 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11109 TRUE))
11110 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011111 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011112
11113 len = list_len(l);
11114 if (len <= 1)
11115 goto theend; /* short list sorts pretty quickly */
11116
11117 info.item_compare_ic = FALSE;
11118 info.item_compare_numeric = FALSE;
11119 info.item_compare_numbers = FALSE;
11120#ifdef FEAT_FLOAT
11121 info.item_compare_float = FALSE;
11122#endif
11123 info.item_compare_func = NULL;
11124 info.item_compare_partial = NULL;
11125 info.item_compare_selfdict = NULL;
11126 if (argvars[1].v_type != VAR_UNKNOWN)
11127 {
11128 /* optional second argument: {func} */
11129 if (argvars[1].v_type == VAR_FUNC)
11130 info.item_compare_func = argvars[1].vval.v_string;
11131 else if (argvars[1].v_type == VAR_PARTIAL)
11132 info.item_compare_partial = argvars[1].vval.v_partial;
11133 else
11134 {
11135 int error = FALSE;
11136
11137 i = (long)get_tv_number_chk(&argvars[1], &error);
11138 if (error)
11139 goto theend; /* type error; errmsg already given */
11140 if (i == 1)
11141 info.item_compare_ic = TRUE;
11142 else if (argvars[1].v_type != VAR_NUMBER)
11143 info.item_compare_func = get_tv_string(&argvars[1]);
11144 else if (i != 0)
11145 {
11146 EMSG(_(e_invarg));
11147 goto theend;
11148 }
11149 if (info.item_compare_func != NULL)
11150 {
11151 if (*info.item_compare_func == NUL)
11152 {
11153 /* empty string means default sort */
11154 info.item_compare_func = NULL;
11155 }
11156 else if (STRCMP(info.item_compare_func, "n") == 0)
11157 {
11158 info.item_compare_func = NULL;
11159 info.item_compare_numeric = TRUE;
11160 }
11161 else if (STRCMP(info.item_compare_func, "N") == 0)
11162 {
11163 info.item_compare_func = NULL;
11164 info.item_compare_numbers = TRUE;
11165 }
11166#ifdef FEAT_FLOAT
11167 else if (STRCMP(info.item_compare_func, "f") == 0)
11168 {
11169 info.item_compare_func = NULL;
11170 info.item_compare_float = TRUE;
11171 }
11172#endif
11173 else if (STRCMP(info.item_compare_func, "i") == 0)
11174 {
11175 info.item_compare_func = NULL;
11176 info.item_compare_ic = TRUE;
11177 }
11178 }
11179 }
11180
11181 if (argvars[2].v_type != VAR_UNKNOWN)
11182 {
11183 /* optional third argument: {dict} */
11184 if (argvars[2].v_type != VAR_DICT)
11185 {
11186 EMSG(_(e_dictreq));
11187 goto theend;
11188 }
11189 info.item_compare_selfdict = argvars[2].vval.v_dict;
11190 }
11191 }
11192
11193 /* Make an array with each entry pointing to an item in the List. */
11194 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11195 if (ptrs == NULL)
11196 goto theend;
11197
11198 i = 0;
11199 if (sort)
11200 {
11201 /* sort(): ptrs will be the list to sort */
11202 for (li = l->lv_first; li != NULL; li = li->li_next)
11203 {
11204 ptrs[i].item = li;
11205 ptrs[i].idx = i;
11206 ++i;
11207 }
11208
11209 info.item_compare_func_err = FALSE;
11210 info.item_compare_keep_zero = FALSE;
11211 /* test the compare function */
11212 if ((info.item_compare_func != NULL
11213 || info.item_compare_partial != NULL)
11214 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11215 == ITEM_COMPARE_FAIL)
11216 EMSG(_("E702: Sort compare function failed"));
11217 else
11218 {
11219 /* Sort the array with item pointers. */
11220 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11221 info.item_compare_func == NULL
11222 && info.item_compare_partial == NULL
11223 ? item_compare : item_compare2);
11224
11225 if (!info.item_compare_func_err)
11226 {
11227 /* Clear the List and append the items in sorted order. */
11228 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11229 l->lv_len = 0;
11230 for (i = 0; i < len; ++i)
11231 list_append(l, ptrs[i].item);
11232 }
11233 }
11234 }
11235 else
11236 {
11237 int (*item_compare_func_ptr)(const void *, const void *);
11238
11239 /* f_uniq(): ptrs will be a stack of items to remove */
11240 info.item_compare_func_err = FALSE;
11241 info.item_compare_keep_zero = TRUE;
11242 item_compare_func_ptr = info.item_compare_func != NULL
11243 || info.item_compare_partial != NULL
11244 ? item_compare2 : item_compare;
11245
11246 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11247 li = li->li_next)
11248 {
11249 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11250 == 0)
11251 ptrs[i++].item = li;
11252 if (info.item_compare_func_err)
11253 {
11254 EMSG(_("E882: Uniq compare function failed"));
11255 break;
11256 }
11257 }
11258
11259 if (!info.item_compare_func_err)
11260 {
11261 while (--i >= 0)
11262 {
11263 li = ptrs[i].item->li_next;
11264 ptrs[i].item->li_next = li->li_next;
11265 if (li->li_next != NULL)
11266 li->li_next->li_prev = ptrs[i].item;
11267 else
11268 l->lv_last = ptrs[i].item;
11269 list_fix_watch(l, li);
11270 listitem_free(li);
11271 l->lv_len--;
11272 }
11273 }
11274 }
11275
11276 vim_free(ptrs);
11277 }
11278theend:
11279 sortinfo = old_sortinfo;
11280}
11281
11282/*
11283 * "sort({list})" function
11284 */
11285 static void
11286f_sort(typval_T *argvars, typval_T *rettv)
11287{
11288 do_sort_uniq(argvars, rettv, TRUE);
11289}
11290
11291/*
11292 * "uniq({list})" function
11293 */
11294 static void
11295f_uniq(typval_T *argvars, typval_T *rettv)
11296{
11297 do_sort_uniq(argvars, rettv, FALSE);
11298}
11299
11300/*
11301 * "soundfold({word})" function
11302 */
11303 static void
11304f_soundfold(typval_T *argvars, typval_T *rettv)
11305{
11306 char_u *s;
11307
11308 rettv->v_type = VAR_STRING;
11309 s = get_tv_string(&argvars[0]);
11310#ifdef FEAT_SPELL
11311 rettv->vval.v_string = eval_soundfold(s);
11312#else
11313 rettv->vval.v_string = vim_strsave(s);
11314#endif
11315}
11316
11317/*
11318 * "spellbadword()" function
11319 */
11320 static void
11321f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11322{
11323 char_u *word = (char_u *)"";
11324 hlf_T attr = HLF_COUNT;
11325 int len = 0;
11326
11327 if (rettv_list_alloc(rettv) == FAIL)
11328 return;
11329
11330#ifdef FEAT_SPELL
11331 if (argvars[0].v_type == VAR_UNKNOWN)
11332 {
11333 /* Find the start and length of the badly spelled word. */
11334 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11335 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011336 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011337 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011338 curwin->w_set_curswant = TRUE;
11339 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011340 }
11341 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11342 {
11343 char_u *str = get_tv_string_chk(&argvars[0]);
11344 int capcol = -1;
11345
11346 if (str != NULL)
11347 {
11348 /* Check the argument for spelling. */
11349 while (*str != NUL)
11350 {
11351 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11352 if (attr != HLF_COUNT)
11353 {
11354 word = str;
11355 break;
11356 }
11357 str += len;
11358 }
11359 }
11360 }
11361#endif
11362
11363 list_append_string(rettv->vval.v_list, word, len);
11364 list_append_string(rettv->vval.v_list, (char_u *)(
11365 attr == HLF_SPB ? "bad" :
11366 attr == HLF_SPR ? "rare" :
11367 attr == HLF_SPL ? "local" :
11368 attr == HLF_SPC ? "caps" :
11369 ""), -1);
11370}
11371
11372/*
11373 * "spellsuggest()" function
11374 */
11375 static void
11376f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11377{
11378#ifdef FEAT_SPELL
11379 char_u *str;
11380 int typeerr = FALSE;
11381 int maxcount;
11382 garray_T ga;
11383 int i;
11384 listitem_T *li;
11385 int need_capital = FALSE;
11386#endif
11387
11388 if (rettv_list_alloc(rettv) == FAIL)
11389 return;
11390
11391#ifdef FEAT_SPELL
11392 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11393 {
11394 str = get_tv_string(&argvars[0]);
11395 if (argvars[1].v_type != VAR_UNKNOWN)
11396 {
11397 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11398 if (maxcount <= 0)
11399 return;
11400 if (argvars[2].v_type != VAR_UNKNOWN)
11401 {
11402 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11403 if (typeerr)
11404 return;
11405 }
11406 }
11407 else
11408 maxcount = 25;
11409
11410 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11411
11412 for (i = 0; i < ga.ga_len; ++i)
11413 {
11414 str = ((char_u **)ga.ga_data)[i];
11415
11416 li = listitem_alloc();
11417 if (li == NULL)
11418 vim_free(str);
11419 else
11420 {
11421 li->li_tv.v_type = VAR_STRING;
11422 li->li_tv.v_lock = 0;
11423 li->li_tv.vval.v_string = str;
11424 list_append(rettv->vval.v_list, li);
11425 }
11426 }
11427 ga_clear(&ga);
11428 }
11429#endif
11430}
11431
11432 static void
11433f_split(typval_T *argvars, typval_T *rettv)
11434{
11435 char_u *str;
11436 char_u *end;
11437 char_u *pat = NULL;
11438 regmatch_T regmatch;
11439 char_u patbuf[NUMBUFLEN];
11440 char_u *save_cpo;
11441 int match;
11442 colnr_T col = 0;
11443 int keepempty = FALSE;
11444 int typeerr = FALSE;
11445
11446 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11447 save_cpo = p_cpo;
11448 p_cpo = (char_u *)"";
11449
11450 str = get_tv_string(&argvars[0]);
11451 if (argvars[1].v_type != VAR_UNKNOWN)
11452 {
11453 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11454 if (pat == NULL)
11455 typeerr = TRUE;
11456 if (argvars[2].v_type != VAR_UNKNOWN)
11457 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11458 }
11459 if (pat == NULL || *pat == NUL)
11460 pat = (char_u *)"[\\x01- ]\\+";
11461
11462 if (rettv_list_alloc(rettv) == FAIL)
11463 return;
11464 if (typeerr)
11465 return;
11466
11467 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11468 if (regmatch.regprog != NULL)
11469 {
11470 regmatch.rm_ic = FALSE;
11471 while (*str != NUL || keepempty)
11472 {
11473 if (*str == NUL)
11474 match = FALSE; /* empty item at the end */
11475 else
11476 match = vim_regexec_nl(&regmatch, str, col);
11477 if (match)
11478 end = regmatch.startp[0];
11479 else
11480 end = str + STRLEN(str);
11481 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11482 && *str != NUL && match && end < regmatch.endp[0]))
11483 {
11484 if (list_append_string(rettv->vval.v_list, str,
11485 (int)(end - str)) == FAIL)
11486 break;
11487 }
11488 if (!match)
11489 break;
11490 /* Advance to just after the match. */
11491 if (regmatch.endp[0] > str)
11492 col = 0;
11493 else
11494 {
11495 /* Don't get stuck at the same match. */
11496#ifdef FEAT_MBYTE
11497 col = (*mb_ptr2len)(regmatch.endp[0]);
11498#else
11499 col = 1;
11500#endif
11501 }
11502 str = regmatch.endp[0];
11503 }
11504
11505 vim_regfree(regmatch.regprog);
11506 }
11507
11508 p_cpo = save_cpo;
11509}
11510
11511#ifdef FEAT_FLOAT
11512/*
11513 * "sqrt()" function
11514 */
11515 static void
11516f_sqrt(typval_T *argvars, typval_T *rettv)
11517{
11518 float_T f = 0.0;
11519
11520 rettv->v_type = VAR_FLOAT;
11521 if (get_float_arg(argvars, &f) == OK)
11522 rettv->vval.v_float = sqrt(f);
11523 else
11524 rettv->vval.v_float = 0.0;
11525}
11526
11527/*
11528 * "str2float()" function
11529 */
11530 static void
11531f_str2float(typval_T *argvars, typval_T *rettv)
11532{
11533 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011534 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011535
Bram Moolenaar08243d22017-01-10 16:12:29 +010011536 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011537 p = skipwhite(p + 1);
11538 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011539 if (isneg)
11540 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011541 rettv->v_type = VAR_FLOAT;
11542}
11543#endif
11544
11545/*
11546 * "str2nr()" function
11547 */
11548 static void
11549f_str2nr(typval_T *argvars, typval_T *rettv)
11550{
11551 int base = 10;
11552 char_u *p;
11553 varnumber_T n;
11554 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011555 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011556
11557 if (argvars[1].v_type != VAR_UNKNOWN)
11558 {
11559 base = (int)get_tv_number(&argvars[1]);
11560 if (base != 2 && base != 8 && base != 10 && base != 16)
11561 {
11562 EMSG(_(e_invarg));
11563 return;
11564 }
11565 }
11566
11567 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011568 isneg = (*p == '-');
11569 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011570 p = skipwhite(p + 1);
11571 switch (base)
11572 {
11573 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11574 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11575 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11576 default: what = 0;
11577 }
11578 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011579 if (isneg)
11580 rettv->vval.v_number = -n;
11581 else
11582 rettv->vval.v_number = n;
11583
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011584}
11585
11586#ifdef HAVE_STRFTIME
11587/*
11588 * "strftime({format}[, {time}])" function
11589 */
11590 static void
11591f_strftime(typval_T *argvars, typval_T *rettv)
11592{
11593 char_u result_buf[256];
11594 struct tm *curtime;
11595 time_t seconds;
11596 char_u *p;
11597
11598 rettv->v_type = VAR_STRING;
11599
11600 p = get_tv_string(&argvars[0]);
11601 if (argvars[1].v_type == VAR_UNKNOWN)
11602 seconds = time(NULL);
11603 else
11604 seconds = (time_t)get_tv_number(&argvars[1]);
11605 curtime = localtime(&seconds);
11606 /* MSVC returns NULL for an invalid value of seconds. */
11607 if (curtime == NULL)
11608 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11609 else
11610 {
11611# ifdef FEAT_MBYTE
11612 vimconv_T conv;
11613 char_u *enc;
11614
11615 conv.vc_type = CONV_NONE;
11616 enc = enc_locale();
11617 convert_setup(&conv, p_enc, enc);
11618 if (conv.vc_type != CONV_NONE)
11619 p = string_convert(&conv, p, NULL);
11620# endif
11621 if (p != NULL)
11622 (void)strftime((char *)result_buf, sizeof(result_buf),
11623 (char *)p, curtime);
11624 else
11625 result_buf[0] = NUL;
11626
11627# ifdef FEAT_MBYTE
11628 if (conv.vc_type != CONV_NONE)
11629 vim_free(p);
11630 convert_setup(&conv, enc, p_enc);
11631 if (conv.vc_type != CONV_NONE)
11632 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11633 else
11634# endif
11635 rettv->vval.v_string = vim_strsave(result_buf);
11636
11637# ifdef FEAT_MBYTE
11638 /* Release conversion descriptors */
11639 convert_setup(&conv, NULL, NULL);
11640 vim_free(enc);
11641# endif
11642 }
11643}
11644#endif
11645
11646/*
11647 * "strgetchar()" function
11648 */
11649 static void
11650f_strgetchar(typval_T *argvars, typval_T *rettv)
11651{
11652 char_u *str;
11653 int len;
11654 int error = FALSE;
11655 int charidx;
11656
11657 rettv->vval.v_number = -1;
11658 str = get_tv_string_chk(&argvars[0]);
11659 if (str == NULL)
11660 return;
11661 len = (int)STRLEN(str);
11662 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11663 if (error)
11664 return;
11665#ifdef FEAT_MBYTE
11666 {
11667 int byteidx = 0;
11668
11669 while (charidx >= 0 && byteidx < len)
11670 {
11671 if (charidx == 0)
11672 {
11673 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11674 break;
11675 }
11676 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011677 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011678 }
11679 }
11680#else
11681 if (charidx < len)
11682 rettv->vval.v_number = str[charidx];
11683#endif
11684}
11685
11686/*
11687 * "stridx()" function
11688 */
11689 static void
11690f_stridx(typval_T *argvars, typval_T *rettv)
11691{
11692 char_u buf[NUMBUFLEN];
11693 char_u *needle;
11694 char_u *haystack;
11695 char_u *save_haystack;
11696 char_u *pos;
11697 int start_idx;
11698
11699 needle = get_tv_string_chk(&argvars[1]);
11700 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11701 rettv->vval.v_number = -1;
11702 if (needle == NULL || haystack == NULL)
11703 return; /* type error; errmsg already given */
11704
11705 if (argvars[2].v_type != VAR_UNKNOWN)
11706 {
11707 int error = FALSE;
11708
11709 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11710 if (error || start_idx >= (int)STRLEN(haystack))
11711 return;
11712 if (start_idx >= 0)
11713 haystack += start_idx;
11714 }
11715
11716 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11717 if (pos != NULL)
11718 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11719}
11720
11721/*
11722 * "string()" function
11723 */
11724 static void
11725f_string(typval_T *argvars, typval_T *rettv)
11726{
11727 char_u *tofree;
11728 char_u numbuf[NUMBUFLEN];
11729
11730 rettv->v_type = VAR_STRING;
11731 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11732 get_copyID());
11733 /* Make a copy if we have a value but it's not in allocated memory. */
11734 if (rettv->vval.v_string != NULL && tofree == NULL)
11735 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11736}
11737
11738/*
11739 * "strlen()" function
11740 */
11741 static void
11742f_strlen(typval_T *argvars, typval_T *rettv)
11743{
11744 rettv->vval.v_number = (varnumber_T)(STRLEN(
11745 get_tv_string(&argvars[0])));
11746}
11747
11748/*
11749 * "strchars()" function
11750 */
11751 static void
11752f_strchars(typval_T *argvars, typval_T *rettv)
11753{
11754 char_u *s = get_tv_string(&argvars[0]);
11755 int skipcc = 0;
11756#ifdef FEAT_MBYTE
11757 varnumber_T len = 0;
11758 int (*func_mb_ptr2char_adv)(char_u **pp);
11759#endif
11760
11761 if (argvars[1].v_type != VAR_UNKNOWN)
11762 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11763 if (skipcc < 0 || skipcc > 1)
11764 EMSG(_(e_invarg));
11765 else
11766 {
11767#ifdef FEAT_MBYTE
11768 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11769 while (*s != NUL)
11770 {
11771 func_mb_ptr2char_adv(&s);
11772 ++len;
11773 }
11774 rettv->vval.v_number = len;
11775#else
11776 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11777#endif
11778 }
11779}
11780
11781/*
11782 * "strdisplaywidth()" function
11783 */
11784 static void
11785f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11786{
11787 char_u *s = get_tv_string(&argvars[0]);
11788 int col = 0;
11789
11790 if (argvars[1].v_type != VAR_UNKNOWN)
11791 col = (int)get_tv_number(&argvars[1]);
11792
11793 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11794}
11795
11796/*
11797 * "strwidth()" function
11798 */
11799 static void
11800f_strwidth(typval_T *argvars, typval_T *rettv)
11801{
11802 char_u *s = get_tv_string(&argvars[0]);
11803
11804 rettv->vval.v_number = (varnumber_T)(
11805#ifdef FEAT_MBYTE
11806 mb_string2cells(s, -1)
11807#else
11808 STRLEN(s)
11809#endif
11810 );
11811}
11812
11813/*
11814 * "strcharpart()" function
11815 */
11816 static void
11817f_strcharpart(typval_T *argvars, typval_T *rettv)
11818{
11819#ifdef FEAT_MBYTE
11820 char_u *p;
11821 int nchar;
11822 int nbyte = 0;
11823 int charlen;
11824 int len = 0;
11825 int slen;
11826 int error = FALSE;
11827
11828 p = get_tv_string(&argvars[0]);
11829 slen = (int)STRLEN(p);
11830
11831 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11832 if (!error)
11833 {
11834 if (nchar > 0)
11835 while (nchar > 0 && nbyte < slen)
11836 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011837 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011838 --nchar;
11839 }
11840 else
11841 nbyte = nchar;
11842 if (argvars[2].v_type != VAR_UNKNOWN)
11843 {
11844 charlen = (int)get_tv_number(&argvars[2]);
11845 while (charlen > 0 && nbyte + len < slen)
11846 {
11847 int off = nbyte + len;
11848
11849 if (off < 0)
11850 len += 1;
11851 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011852 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011853 --charlen;
11854 }
11855 }
11856 else
11857 len = slen - nbyte; /* default: all bytes that are available. */
11858 }
11859
11860 /*
11861 * Only return the overlap between the specified part and the actual
11862 * string.
11863 */
11864 if (nbyte < 0)
11865 {
11866 len += nbyte;
11867 nbyte = 0;
11868 }
11869 else if (nbyte > slen)
11870 nbyte = slen;
11871 if (len < 0)
11872 len = 0;
11873 else if (nbyte + len > slen)
11874 len = slen - nbyte;
11875
11876 rettv->v_type = VAR_STRING;
11877 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11878#else
11879 f_strpart(argvars, rettv);
11880#endif
11881}
11882
11883/*
11884 * "strpart()" function
11885 */
11886 static void
11887f_strpart(typval_T *argvars, typval_T *rettv)
11888{
11889 char_u *p;
11890 int n;
11891 int len;
11892 int slen;
11893 int error = FALSE;
11894
11895 p = get_tv_string(&argvars[0]);
11896 slen = (int)STRLEN(p);
11897
11898 n = (int)get_tv_number_chk(&argvars[1], &error);
11899 if (error)
11900 len = 0;
11901 else if (argvars[2].v_type != VAR_UNKNOWN)
11902 len = (int)get_tv_number(&argvars[2]);
11903 else
11904 len = slen - n; /* default len: all bytes that are available. */
11905
11906 /*
11907 * Only return the overlap between the specified part and the actual
11908 * string.
11909 */
11910 if (n < 0)
11911 {
11912 len += n;
11913 n = 0;
11914 }
11915 else if (n > slen)
11916 n = slen;
11917 if (len < 0)
11918 len = 0;
11919 else if (n + len > slen)
11920 len = slen - n;
11921
11922 rettv->v_type = VAR_STRING;
11923 rettv->vval.v_string = vim_strnsave(p + n, len);
11924}
11925
11926/*
11927 * "strridx()" function
11928 */
11929 static void
11930f_strridx(typval_T *argvars, typval_T *rettv)
11931{
11932 char_u buf[NUMBUFLEN];
11933 char_u *needle;
11934 char_u *haystack;
11935 char_u *rest;
11936 char_u *lastmatch = NULL;
11937 int haystack_len, end_idx;
11938
11939 needle = get_tv_string_chk(&argvars[1]);
11940 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11941
11942 rettv->vval.v_number = -1;
11943 if (needle == NULL || haystack == NULL)
11944 return; /* type error; errmsg already given */
11945
11946 haystack_len = (int)STRLEN(haystack);
11947 if (argvars[2].v_type != VAR_UNKNOWN)
11948 {
11949 /* Third argument: upper limit for index */
11950 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11951 if (end_idx < 0)
11952 return; /* can never find a match */
11953 }
11954 else
11955 end_idx = haystack_len;
11956
11957 if (*needle == NUL)
11958 {
11959 /* Empty string matches past the end. */
11960 lastmatch = haystack + end_idx;
11961 }
11962 else
11963 {
11964 for (rest = haystack; *rest != '\0'; ++rest)
11965 {
11966 rest = (char_u *)strstr((char *)rest, (char *)needle);
11967 if (rest == NULL || rest > haystack + end_idx)
11968 break;
11969 lastmatch = rest;
11970 }
11971 }
11972
11973 if (lastmatch == NULL)
11974 rettv->vval.v_number = -1;
11975 else
11976 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11977}
11978
11979/*
11980 * "strtrans()" function
11981 */
11982 static void
11983f_strtrans(typval_T *argvars, typval_T *rettv)
11984{
11985 rettv->v_type = VAR_STRING;
11986 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11987}
11988
11989/*
11990 * "submatch()" function
11991 */
11992 static void
11993f_submatch(typval_T *argvars, typval_T *rettv)
11994{
11995 int error = FALSE;
11996 int no;
11997 int retList = 0;
11998
11999 no = (int)get_tv_number_chk(&argvars[0], &error);
12000 if (error)
12001 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012002 if (no < 0 || no >= NSUBEXP)
12003 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012004 EMSGN(_("E935: invalid submatch number: %d"), no);
12005 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012006 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012007 if (argvars[1].v_type != VAR_UNKNOWN)
12008 retList = (int)get_tv_number_chk(&argvars[1], &error);
12009 if (error)
12010 return;
12011
12012 if (retList == 0)
12013 {
12014 rettv->v_type = VAR_STRING;
12015 rettv->vval.v_string = reg_submatch(no);
12016 }
12017 else
12018 {
12019 rettv->v_type = VAR_LIST;
12020 rettv->vval.v_list = reg_submatch_list(no);
12021 }
12022}
12023
12024/*
12025 * "substitute()" function
12026 */
12027 static void
12028f_substitute(typval_T *argvars, typval_T *rettv)
12029{
12030 char_u patbuf[NUMBUFLEN];
12031 char_u subbuf[NUMBUFLEN];
12032 char_u flagsbuf[NUMBUFLEN];
12033
12034 char_u *str = get_tv_string_chk(&argvars[0]);
12035 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012036 char_u *sub = NULL;
12037 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012038 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12039
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012040 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12041 expr = &argvars[2];
12042 else
12043 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12044
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012045 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012046 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12047 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012048 rettv->vval.v_string = NULL;
12049 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012050 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012051}
12052
12053/*
12054 * "synID(lnum, col, trans)" function
12055 */
12056 static void
12057f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12058{
12059 int id = 0;
12060#ifdef FEAT_SYN_HL
12061 linenr_T lnum;
12062 colnr_T col;
12063 int trans;
12064 int transerr = FALSE;
12065
12066 lnum = get_tv_lnum(argvars); /* -1 on type error */
12067 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12068 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12069
12070 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12071 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12072 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12073#endif
12074
12075 rettv->vval.v_number = id;
12076}
12077
12078/*
12079 * "synIDattr(id, what [, mode])" function
12080 */
12081 static void
12082f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12083{
12084 char_u *p = NULL;
12085#ifdef FEAT_SYN_HL
12086 int id;
12087 char_u *what;
12088 char_u *mode;
12089 char_u modebuf[NUMBUFLEN];
12090 int modec;
12091
12092 id = (int)get_tv_number(&argvars[0]);
12093 what = get_tv_string(&argvars[1]);
12094 if (argvars[2].v_type != VAR_UNKNOWN)
12095 {
12096 mode = get_tv_string_buf(&argvars[2], modebuf);
12097 modec = TOLOWER_ASC(mode[0]);
12098 if (modec != 't' && modec != 'c' && modec != 'g')
12099 modec = 0; /* replace invalid with current */
12100 }
12101 else
12102 {
12103#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12104 if (USE_24BIT)
12105 modec = 'g';
12106 else
12107#endif
12108 if (t_colors > 1)
12109 modec = 'c';
12110 else
12111 modec = 't';
12112 }
12113
12114
12115 switch (TOLOWER_ASC(what[0]))
12116 {
12117 case 'b':
12118 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12119 p = highlight_color(id, what, modec);
12120 else /* bold */
12121 p = highlight_has_attr(id, HL_BOLD, modec);
12122 break;
12123
12124 case 'f': /* fg[#] or font */
12125 p = highlight_color(id, what, modec);
12126 break;
12127
12128 case 'i':
12129 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12130 p = highlight_has_attr(id, HL_INVERSE, modec);
12131 else /* italic */
12132 p = highlight_has_attr(id, HL_ITALIC, modec);
12133 break;
12134
12135 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012136 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012137 break;
12138
12139 case 'r': /* reverse */
12140 p = highlight_has_attr(id, HL_INVERSE, modec);
12141 break;
12142
12143 case 's':
12144 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12145 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012146 /* strikeout */
12147 else if (TOLOWER_ASC(what[1]) == 't' &&
12148 TOLOWER_ASC(what[2]) == 'r')
12149 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012150 else /* standout */
12151 p = highlight_has_attr(id, HL_STANDOUT, modec);
12152 break;
12153
12154 case 'u':
12155 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12156 /* underline */
12157 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12158 else
12159 /* undercurl */
12160 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12161 break;
12162 }
12163
12164 if (p != NULL)
12165 p = vim_strsave(p);
12166#endif
12167 rettv->v_type = VAR_STRING;
12168 rettv->vval.v_string = p;
12169}
12170
12171/*
12172 * "synIDtrans(id)" function
12173 */
12174 static void
12175f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12176{
12177 int id;
12178
12179#ifdef FEAT_SYN_HL
12180 id = (int)get_tv_number(&argvars[0]);
12181
12182 if (id > 0)
12183 id = syn_get_final_id(id);
12184 else
12185#endif
12186 id = 0;
12187
12188 rettv->vval.v_number = id;
12189}
12190
12191/*
12192 * "synconcealed(lnum, col)" function
12193 */
12194 static void
12195f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12196{
12197#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12198 linenr_T lnum;
12199 colnr_T col;
12200 int syntax_flags = 0;
12201 int cchar;
12202 int matchid = 0;
12203 char_u str[NUMBUFLEN];
12204#endif
12205
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012206 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012207
12208#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12209 lnum = get_tv_lnum(argvars); /* -1 on type error */
12210 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12211
12212 vim_memset(str, NUL, sizeof(str));
12213
12214 if (rettv_list_alloc(rettv) != FAIL)
12215 {
12216 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12217 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12218 && curwin->w_p_cole > 0)
12219 {
12220 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12221 syntax_flags = get_syntax_info(&matchid);
12222
12223 /* get the conceal character */
12224 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12225 {
12226 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012227 if (cchar == NUL && curwin->w_p_cole == 1)
12228 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012229 if (cchar != NUL)
12230 {
12231# ifdef FEAT_MBYTE
12232 if (has_mbyte)
12233 (*mb_char2bytes)(cchar, str);
12234 else
12235# endif
12236 str[0] = cchar;
12237 }
12238 }
12239 }
12240
12241 list_append_number(rettv->vval.v_list,
12242 (syntax_flags & HL_CONCEAL) != 0);
12243 /* -1 to auto-determine strlen */
12244 list_append_string(rettv->vval.v_list, str, -1);
12245 list_append_number(rettv->vval.v_list, matchid);
12246 }
12247#endif
12248}
12249
12250/*
12251 * "synstack(lnum, col)" function
12252 */
12253 static void
12254f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12255{
12256#ifdef FEAT_SYN_HL
12257 linenr_T lnum;
12258 colnr_T col;
12259 int i;
12260 int id;
12261#endif
12262
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012263 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012264
12265#ifdef FEAT_SYN_HL
12266 lnum = get_tv_lnum(argvars); /* -1 on type error */
12267 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12268
12269 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12270 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12271 && rettv_list_alloc(rettv) != FAIL)
12272 {
12273 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12274 for (i = 0; ; ++i)
12275 {
12276 id = syn_get_stack_item(i);
12277 if (id < 0)
12278 break;
12279 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12280 break;
12281 }
12282 }
12283#endif
12284}
12285
12286 static void
12287get_cmd_output_as_rettv(
12288 typval_T *argvars,
12289 typval_T *rettv,
12290 int retlist)
12291{
12292 char_u *res = NULL;
12293 char_u *p;
12294 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012295 int err = FALSE;
12296 FILE *fd;
12297 list_T *list = NULL;
12298 int flags = SHELL_SILENT;
12299
12300 rettv->v_type = VAR_STRING;
12301 rettv->vval.v_string = NULL;
12302 if (check_restricted() || check_secure())
12303 goto errret;
12304
12305 if (argvars[1].v_type != VAR_UNKNOWN)
12306 {
12307 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012308 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012309 * command.
12310 */
12311 if ((infile = vim_tempname('i', TRUE)) == NULL)
12312 {
12313 EMSG(_(e_notmp));
12314 goto errret;
12315 }
12316
12317 fd = mch_fopen((char *)infile, WRITEBIN);
12318 if (fd == NULL)
12319 {
12320 EMSG2(_(e_notopen), infile);
12321 goto errret;
12322 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012323 if (argvars[1].v_type == VAR_NUMBER)
12324 {
12325 linenr_T lnum;
12326 buf_T *buf;
12327
12328 buf = buflist_findnr(argvars[1].vval.v_number);
12329 if (buf == NULL)
12330 {
12331 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012332 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012333 goto errret;
12334 }
12335
12336 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12337 {
12338 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12339 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12340 {
12341 err = TRUE;
12342 break;
12343 }
12344 if (putc(NL, fd) == EOF)
12345 {
12346 err = TRUE;
12347 break;
12348 }
12349 }
12350 }
12351 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012352 {
12353 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12354 err = TRUE;
12355 }
12356 else
12357 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012358 size_t len;
12359 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012360
12361 p = get_tv_string_buf_chk(&argvars[1], buf);
12362 if (p == NULL)
12363 {
12364 fclose(fd);
12365 goto errret; /* type error; errmsg already given */
12366 }
12367 len = STRLEN(p);
12368 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12369 err = TRUE;
12370 }
12371 if (fclose(fd) != 0)
12372 err = TRUE;
12373 if (err)
12374 {
12375 EMSG(_("E677: Error writing temp file"));
12376 goto errret;
12377 }
12378 }
12379
12380 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12381 * echoes typeahead, that messes up the display. */
12382 if (!msg_silent)
12383 flags += SHELL_COOKED;
12384
12385 if (retlist)
12386 {
12387 int len;
12388 listitem_T *li;
12389 char_u *s = NULL;
12390 char_u *start;
12391 char_u *end;
12392 int i;
12393
12394 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12395 if (res == NULL)
12396 goto errret;
12397
12398 list = list_alloc();
12399 if (list == NULL)
12400 goto errret;
12401
12402 for (i = 0; i < len; ++i)
12403 {
12404 start = res + i;
12405 while (i < len && res[i] != NL)
12406 ++i;
12407 end = res + i;
12408
12409 s = alloc((unsigned)(end - start + 1));
12410 if (s == NULL)
12411 goto errret;
12412
12413 for (p = s; start < end; ++p, ++start)
12414 *p = *start == NUL ? NL : *start;
12415 *p = NUL;
12416
12417 li = listitem_alloc();
12418 if (li == NULL)
12419 {
12420 vim_free(s);
12421 goto errret;
12422 }
12423 li->li_tv.v_type = VAR_STRING;
12424 li->li_tv.v_lock = 0;
12425 li->li_tv.vval.v_string = s;
12426 list_append(list, li);
12427 }
12428
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012429 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012430 list = NULL;
12431 }
12432 else
12433 {
12434 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12435#ifdef USE_CR
12436 /* translate <CR> into <NL> */
12437 if (res != NULL)
12438 {
12439 char_u *s;
12440
12441 for (s = res; *s; ++s)
12442 {
12443 if (*s == CAR)
12444 *s = NL;
12445 }
12446 }
12447#else
12448# ifdef USE_CRNL
12449 /* translate <CR><NL> into <NL> */
12450 if (res != NULL)
12451 {
12452 char_u *s, *d;
12453
12454 d = res;
12455 for (s = res; *s; ++s)
12456 {
12457 if (s[0] == CAR && s[1] == NL)
12458 ++s;
12459 *d++ = *s;
12460 }
12461 *d = NUL;
12462 }
12463# endif
12464#endif
12465 rettv->vval.v_string = res;
12466 res = NULL;
12467 }
12468
12469errret:
12470 if (infile != NULL)
12471 {
12472 mch_remove(infile);
12473 vim_free(infile);
12474 }
12475 if (res != NULL)
12476 vim_free(res);
12477 if (list != NULL)
12478 list_free(list);
12479}
12480
12481/*
12482 * "system()" function
12483 */
12484 static void
12485f_system(typval_T *argvars, typval_T *rettv)
12486{
12487 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12488}
12489
12490/*
12491 * "systemlist()" function
12492 */
12493 static void
12494f_systemlist(typval_T *argvars, typval_T *rettv)
12495{
12496 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12497}
12498
12499/*
12500 * "tabpagebuflist()" function
12501 */
12502 static void
12503f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12504{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012505 tabpage_T *tp;
12506 win_T *wp = NULL;
12507
12508 if (argvars[0].v_type == VAR_UNKNOWN)
12509 wp = firstwin;
12510 else
12511 {
12512 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12513 if (tp != NULL)
12514 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12515 }
12516 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12517 {
12518 for (; wp != NULL; wp = wp->w_next)
12519 if (list_append_number(rettv->vval.v_list,
12520 wp->w_buffer->b_fnum) == FAIL)
12521 break;
12522 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012523}
12524
12525
12526/*
12527 * "tabpagenr()" function
12528 */
12529 static void
12530f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12531{
12532 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012533 char_u *arg;
12534
12535 if (argvars[0].v_type != VAR_UNKNOWN)
12536 {
12537 arg = get_tv_string_chk(&argvars[0]);
12538 nr = 0;
12539 if (arg != NULL)
12540 {
12541 if (STRCMP(arg, "$") == 0)
12542 nr = tabpage_index(NULL) - 1;
12543 else
12544 EMSG2(_(e_invexpr2), arg);
12545 }
12546 }
12547 else
12548 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012549 rettv->vval.v_number = nr;
12550}
12551
12552
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012553static int get_winnr(tabpage_T *tp, typval_T *argvar);
12554
12555/*
12556 * Common code for tabpagewinnr() and winnr().
12557 */
12558 static int
12559get_winnr(tabpage_T *tp, typval_T *argvar)
12560{
12561 win_T *twin;
12562 int nr = 1;
12563 win_T *wp;
12564 char_u *arg;
12565
12566 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12567 if (argvar->v_type != VAR_UNKNOWN)
12568 {
12569 arg = get_tv_string_chk(argvar);
12570 if (arg == NULL)
12571 nr = 0; /* type error; errmsg already given */
12572 else if (STRCMP(arg, "$") == 0)
12573 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12574 else if (STRCMP(arg, "#") == 0)
12575 {
12576 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12577 if (twin == NULL)
12578 nr = 0;
12579 }
12580 else
12581 {
12582 EMSG2(_(e_invexpr2), arg);
12583 nr = 0;
12584 }
12585 }
12586
12587 if (nr > 0)
12588 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12589 wp != twin; wp = wp->w_next)
12590 {
12591 if (wp == NULL)
12592 {
12593 /* didn't find it in this tabpage */
12594 nr = 0;
12595 break;
12596 }
12597 ++nr;
12598 }
12599 return nr;
12600}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012601
12602/*
12603 * "tabpagewinnr()" function
12604 */
12605 static void
12606f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12607{
12608 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012609 tabpage_T *tp;
12610
12611 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12612 if (tp == NULL)
12613 nr = 0;
12614 else
12615 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012616 rettv->vval.v_number = nr;
12617}
12618
12619
12620/*
12621 * "tagfiles()" function
12622 */
12623 static void
12624f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12625{
12626 char_u *fname;
12627 tagname_T tn;
12628 int first;
12629
12630 if (rettv_list_alloc(rettv) == FAIL)
12631 return;
12632 fname = alloc(MAXPATHL);
12633 if (fname == NULL)
12634 return;
12635
12636 for (first = TRUE; ; first = FALSE)
12637 if (get_tagfname(&tn, first, fname) == FAIL
12638 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12639 break;
12640 tagname_free(&tn);
12641 vim_free(fname);
12642}
12643
12644/*
12645 * "taglist()" function
12646 */
12647 static void
12648f_taglist(typval_T *argvars, typval_T *rettv)
12649{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012650 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012651 char_u *tag_pattern;
12652
12653 tag_pattern = get_tv_string(&argvars[0]);
12654
12655 rettv->vval.v_number = FALSE;
12656 if (*tag_pattern == NUL)
12657 return;
12658
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012659 if (argvars[1].v_type != VAR_UNKNOWN)
12660 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012661 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012662 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012663}
12664
12665/*
12666 * "tempname()" function
12667 */
12668 static void
12669f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12670{
12671 static int x = 'A';
12672
12673 rettv->v_type = VAR_STRING;
12674 rettv->vval.v_string = vim_tempname(x, FALSE);
12675
12676 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12677 * names. Skip 'I' and 'O', they are used for shell redirection. */
12678 do
12679 {
12680 if (x == 'Z')
12681 x = '0';
12682 else if (x == '9')
12683 x = 'A';
12684 else
12685 {
12686#ifdef EBCDIC
12687 if (x == 'I')
12688 x = 'J';
12689 else if (x == 'R')
12690 x = 'S';
12691 else
12692#endif
12693 ++x;
12694 }
12695 } while (x == 'I' || x == 'O');
12696}
12697
12698#ifdef FEAT_FLOAT
12699/*
12700 * "tan()" function
12701 */
12702 static void
12703f_tan(typval_T *argvars, typval_T *rettv)
12704{
12705 float_T f = 0.0;
12706
12707 rettv->v_type = VAR_FLOAT;
12708 if (get_float_arg(argvars, &f) == OK)
12709 rettv->vval.v_float = tan(f);
12710 else
12711 rettv->vval.v_float = 0.0;
12712}
12713
12714/*
12715 * "tanh()" function
12716 */
12717 static void
12718f_tanh(typval_T *argvars, typval_T *rettv)
12719{
12720 float_T f = 0.0;
12721
12722 rettv->v_type = VAR_FLOAT;
12723 if (get_float_arg(argvars, &f) == OK)
12724 rettv->vval.v_float = tanh(f);
12725 else
12726 rettv->vval.v_float = 0.0;
12727}
12728#endif
12729
12730/*
12731 * "test_alloc_fail(id, countdown, repeat)" function
12732 */
12733 static void
12734f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12735{
12736 if (argvars[0].v_type != VAR_NUMBER
12737 || argvars[0].vval.v_number <= 0
12738 || argvars[1].v_type != VAR_NUMBER
12739 || argvars[1].vval.v_number < 0
12740 || argvars[2].v_type != VAR_NUMBER)
12741 EMSG(_(e_invarg));
12742 else
12743 {
12744 alloc_fail_id = argvars[0].vval.v_number;
12745 if (alloc_fail_id >= aid_last)
12746 EMSG(_(e_invarg));
12747 alloc_fail_countdown = argvars[1].vval.v_number;
12748 alloc_fail_repeat = argvars[2].vval.v_number;
12749 did_outofmem_msg = FALSE;
12750 }
12751}
12752
12753/*
12754 * "test_autochdir()"
12755 */
12756 static void
12757f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12758{
12759#if defined(FEAT_AUTOCHDIR)
12760 test_autochdir = TRUE;
12761#endif
12762}
12763
12764/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012765 * "test_feedinput()"
12766 */
12767 static void
12768f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12769{
12770#ifdef USE_INPUT_BUF
12771 char_u *val = get_tv_string_chk(&argvars[0]);
12772
12773 if (val != NULL)
12774 {
12775 trash_input_buf();
12776 add_to_input_buf_csi(val, (int)STRLEN(val));
12777 }
12778#endif
12779}
12780
12781/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012782 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012783 */
12784 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012785f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012786{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012787 char_u *name = (char_u *)"";
12788 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012789 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012790
12791 if (argvars[0].v_type != VAR_STRING
12792 || (argvars[1].v_type) != VAR_NUMBER)
12793 EMSG(_(e_invarg));
12794 else
12795 {
12796 name = get_tv_string_chk(&argvars[0]);
12797 val = (int)get_tv_number(&argvars[1]);
12798
12799 if (STRCMP(name, (char_u *)"redraw") == 0)
12800 disable_redraw_for_testing = val;
12801 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12802 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012803 else if (STRCMP(name, (char_u *)"starting") == 0)
12804 {
12805 if (val)
12806 {
12807 if (save_starting < 0)
12808 save_starting = starting;
12809 starting = 0;
12810 }
12811 else
12812 {
12813 starting = save_starting;
12814 save_starting = -1;
12815 }
12816 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012817 else if (STRCMP(name, (char_u *)"ALL") == 0)
12818 {
12819 disable_char_avail_for_testing = FALSE;
12820 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012821 if (save_starting >= 0)
12822 {
12823 starting = save_starting;
12824 save_starting = -1;
12825 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012826 }
12827 else
12828 EMSG2(_(e_invarg2), name);
12829 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012830}
12831
12832/*
12833 * "test_garbagecollect_now()" function
12834 */
12835 static void
12836f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12837{
12838 /* This is dangerous, any Lists and Dicts used internally may be freed
12839 * while still in use. */
12840 garbage_collect(TRUE);
12841}
12842
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012843/*
12844 * "test_ignore_error()" function
12845 */
12846 static void
12847f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12848{
12849 ignore_error_for_testing(get_tv_string(&argvars[0]));
12850}
12851
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012852#ifdef FEAT_JOB_CHANNEL
12853 static void
12854f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12855{
12856 rettv->v_type = VAR_CHANNEL;
12857 rettv->vval.v_channel = NULL;
12858}
12859#endif
12860
12861 static void
12862f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12863{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012864 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012865}
12866
12867#ifdef FEAT_JOB_CHANNEL
12868 static void
12869f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12870{
12871 rettv->v_type = VAR_JOB;
12872 rettv->vval.v_job = NULL;
12873}
12874#endif
12875
12876 static void
12877f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12878{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012879 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012880}
12881
12882 static void
12883f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12884{
12885 rettv->v_type = VAR_PARTIAL;
12886 rettv->vval.v_partial = NULL;
12887}
12888
12889 static void
12890f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12891{
12892 rettv->v_type = VAR_STRING;
12893 rettv->vval.v_string = NULL;
12894}
12895
12896 static void
12897f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12898{
12899 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12900}
12901
12902#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12903/*
12904 * Get a callback from "arg". It can be a Funcref or a function name.
12905 * When "arg" is zero return an empty string.
12906 * Return NULL for an invalid argument.
12907 */
12908 char_u *
12909get_callback(typval_T *arg, partial_T **pp)
12910{
12911 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12912 {
12913 *pp = arg->vval.v_partial;
12914 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012915 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012916 }
12917 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012918 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012919 {
12920 func_ref(arg->vval.v_string);
12921 return arg->vval.v_string;
12922 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012923 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12924 return (char_u *)"";
12925 EMSG(_("E921: Invalid callback argument"));
12926 return NULL;
12927}
12928
12929/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020012930 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012931 */
12932 void
12933free_callback(char_u *callback, partial_T *partial)
12934{
12935 if (partial != NULL)
12936 partial_unref(partial);
12937 else if (callback != NULL)
12938 {
12939 func_unref(callback);
12940 vim_free(callback);
12941 }
12942}
12943#endif
12944
12945#ifdef FEAT_TIMERS
12946/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012947 * "timer_info([timer])" function
12948 */
12949 static void
12950f_timer_info(typval_T *argvars, typval_T *rettv)
12951{
12952 timer_T *timer = NULL;
12953
12954 if (rettv_list_alloc(rettv) != OK)
12955 return;
12956 if (argvars[0].v_type != VAR_UNKNOWN)
12957 {
12958 if (argvars[0].v_type != VAR_NUMBER)
12959 EMSG(_(e_number_exp));
12960 else
12961 {
12962 timer = find_timer((int)get_tv_number(&argvars[0]));
12963 if (timer != NULL)
12964 add_timer_info(rettv, timer);
12965 }
12966 }
12967 else
12968 add_timer_info_all(rettv);
12969}
12970
12971/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012972 * "timer_pause(timer, paused)" function
12973 */
12974 static void
12975f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12976{
12977 timer_T *timer = NULL;
12978 int paused = (int)get_tv_number(&argvars[1]);
12979
12980 if (argvars[0].v_type != VAR_NUMBER)
12981 EMSG(_(e_number_exp));
12982 else
12983 {
12984 timer = find_timer((int)get_tv_number(&argvars[0]));
12985 if (timer != NULL)
12986 timer->tr_paused = paused;
12987 }
12988}
12989
12990/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012991 * "timer_start(time, callback [, options])" function
12992 */
12993 static void
12994f_timer_start(typval_T *argvars, typval_T *rettv)
12995{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012996 long msec = (long)get_tv_number(&argvars[0]);
12997 timer_T *timer;
12998 int repeat = 0;
12999 char_u *callback;
13000 dict_T *dict;
13001 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013002
Bram Moolenaar75537a92016-09-05 22:45:28 +020013003 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013004 if (check_secure())
13005 return;
13006 if (argvars[2].v_type != VAR_UNKNOWN)
13007 {
13008 if (argvars[2].v_type != VAR_DICT
13009 || (dict = argvars[2].vval.v_dict) == NULL)
13010 {
13011 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13012 return;
13013 }
13014 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13015 repeat = get_dict_number(dict, (char_u *)"repeat");
13016 }
13017
Bram Moolenaar75537a92016-09-05 22:45:28 +020013018 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013019 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013020 return;
13021
13022 timer = create_timer(msec, repeat);
13023 if (timer == NULL)
13024 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013025 else
13026 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013027 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013028 timer->tr_callback = vim_strsave(callback);
13029 else
13030 /* pointer into the partial */
13031 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013032 timer->tr_partial = partial;
13033 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013034 }
13035}
13036
13037/*
13038 * "timer_stop(timer)" function
13039 */
13040 static void
13041f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13042{
13043 timer_T *timer;
13044
13045 if (argvars[0].v_type != VAR_NUMBER)
13046 {
13047 EMSG(_(e_number_exp));
13048 return;
13049 }
13050 timer = find_timer((int)get_tv_number(&argvars[0]));
13051 if (timer != NULL)
13052 stop_timer(timer);
13053}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013054
13055/*
13056 * "timer_stopall()" function
13057 */
13058 static void
13059f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13060{
13061 stop_all_timers();
13062}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013063#endif
13064
13065/*
13066 * "tolower(string)" function
13067 */
13068 static void
13069f_tolower(typval_T *argvars, typval_T *rettv)
13070{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013071 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013072 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013073}
13074
13075/*
13076 * "toupper(string)" function
13077 */
13078 static void
13079f_toupper(typval_T *argvars, typval_T *rettv)
13080{
13081 rettv->v_type = VAR_STRING;
13082 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13083}
13084
13085/*
13086 * "tr(string, fromstr, tostr)" function
13087 */
13088 static void
13089f_tr(typval_T *argvars, typval_T *rettv)
13090{
13091 char_u *in_str;
13092 char_u *fromstr;
13093 char_u *tostr;
13094 char_u *p;
13095#ifdef FEAT_MBYTE
13096 int inlen;
13097 int fromlen;
13098 int tolen;
13099 int idx;
13100 char_u *cpstr;
13101 int cplen;
13102 int first = TRUE;
13103#endif
13104 char_u buf[NUMBUFLEN];
13105 char_u buf2[NUMBUFLEN];
13106 garray_T ga;
13107
13108 in_str = get_tv_string(&argvars[0]);
13109 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13110 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13111
13112 /* Default return value: empty string. */
13113 rettv->v_type = VAR_STRING;
13114 rettv->vval.v_string = NULL;
13115 if (fromstr == NULL || tostr == NULL)
13116 return; /* type error; errmsg already given */
13117 ga_init2(&ga, (int)sizeof(char), 80);
13118
13119#ifdef FEAT_MBYTE
13120 if (!has_mbyte)
13121#endif
13122 /* not multi-byte: fromstr and tostr must be the same length */
13123 if (STRLEN(fromstr) != STRLEN(tostr))
13124 {
13125#ifdef FEAT_MBYTE
13126error:
13127#endif
13128 EMSG2(_(e_invarg2), fromstr);
13129 ga_clear(&ga);
13130 return;
13131 }
13132
13133 /* fromstr and tostr have to contain the same number of chars */
13134 while (*in_str != NUL)
13135 {
13136#ifdef FEAT_MBYTE
13137 if (has_mbyte)
13138 {
13139 inlen = (*mb_ptr2len)(in_str);
13140 cpstr = in_str;
13141 cplen = inlen;
13142 idx = 0;
13143 for (p = fromstr; *p != NUL; p += fromlen)
13144 {
13145 fromlen = (*mb_ptr2len)(p);
13146 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13147 {
13148 for (p = tostr; *p != NUL; p += tolen)
13149 {
13150 tolen = (*mb_ptr2len)(p);
13151 if (idx-- == 0)
13152 {
13153 cplen = tolen;
13154 cpstr = p;
13155 break;
13156 }
13157 }
13158 if (*p == NUL) /* tostr is shorter than fromstr */
13159 goto error;
13160 break;
13161 }
13162 ++idx;
13163 }
13164
13165 if (first && cpstr == in_str)
13166 {
13167 /* Check that fromstr and tostr have the same number of
13168 * (multi-byte) characters. Done only once when a character
13169 * of in_str doesn't appear in fromstr. */
13170 first = FALSE;
13171 for (p = tostr; *p != NUL; p += tolen)
13172 {
13173 tolen = (*mb_ptr2len)(p);
13174 --idx;
13175 }
13176 if (idx != 0)
13177 goto error;
13178 }
13179
13180 (void)ga_grow(&ga, cplen);
13181 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13182 ga.ga_len += cplen;
13183
13184 in_str += inlen;
13185 }
13186 else
13187#endif
13188 {
13189 /* When not using multi-byte chars we can do it faster. */
13190 p = vim_strchr(fromstr, *in_str);
13191 if (p != NULL)
13192 ga_append(&ga, tostr[p - fromstr]);
13193 else
13194 ga_append(&ga, *in_str);
13195 ++in_str;
13196 }
13197 }
13198
13199 /* add a terminating NUL */
13200 (void)ga_grow(&ga, 1);
13201 ga_append(&ga, NUL);
13202
13203 rettv->vval.v_string = ga.ga_data;
13204}
13205
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013206/*
13207 * "trim({expr})" function
13208 */
13209 static void
13210f_trim(typval_T *argvars, typval_T *rettv)
13211{
13212 char_u buf1[NUMBUFLEN];
13213 char_u buf2[NUMBUFLEN];
13214 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13215 char_u *mask = NULL;
13216 char_u *tail;
13217 char_u *prev;
13218 char_u *p;
13219 int c1;
13220
13221 rettv->v_type = VAR_STRING;
13222 if (head == NULL)
13223 {
13224 rettv->vval.v_string = NULL;
13225 return;
13226 }
13227
13228 if (argvars[1].v_type == VAR_STRING)
13229 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13230
13231 while (*head != NUL)
13232 {
13233 c1 = PTR2CHAR(head);
13234 if (mask == NULL)
13235 {
13236 if (c1 > ' ' && c1 != 0xa0)
13237 break;
13238 }
13239 else
13240 {
13241 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13242 if (c1 == PTR2CHAR(p))
13243 break;
13244 if (*p == NUL)
13245 break;
13246 }
13247 MB_PTR_ADV(head);
13248 }
13249
13250 for (tail = head + STRLEN(head); tail > head; tail = prev)
13251 {
13252 prev = tail;
13253 MB_PTR_BACK(head, prev);
13254 c1 = PTR2CHAR(prev);
13255 if (mask == NULL)
13256 {
13257 if (c1 > ' ' && c1 != 0xa0)
13258 break;
13259 }
13260 else
13261 {
13262 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13263 if (c1 == PTR2CHAR(p))
13264 break;
13265 if (*p == NUL)
13266 break;
13267 }
13268 }
13269 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13270}
13271
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013272#ifdef FEAT_FLOAT
13273/*
13274 * "trunc({float})" function
13275 */
13276 static void
13277f_trunc(typval_T *argvars, typval_T *rettv)
13278{
13279 float_T f = 0.0;
13280
13281 rettv->v_type = VAR_FLOAT;
13282 if (get_float_arg(argvars, &f) == OK)
13283 /* trunc() is not in C90, use floor() or ceil() instead. */
13284 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13285 else
13286 rettv->vval.v_float = 0.0;
13287}
13288#endif
13289
13290/*
13291 * "type(expr)" function
13292 */
13293 static void
13294f_type(typval_T *argvars, typval_T *rettv)
13295{
13296 int n = -1;
13297
13298 switch (argvars[0].v_type)
13299 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013300 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13301 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013302 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013303 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13304 case VAR_LIST: n = VAR_TYPE_LIST; break;
13305 case VAR_DICT: n = VAR_TYPE_DICT; break;
13306 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013307 case VAR_SPECIAL:
13308 if (argvars[0].vval.v_number == VVAL_FALSE
13309 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013310 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013311 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013312 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013313 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013314 case VAR_JOB: n = VAR_TYPE_JOB; break;
13315 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013316 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013317 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013318 n = -1;
13319 break;
13320 }
13321 rettv->vval.v_number = n;
13322}
13323
13324/*
13325 * "undofile(name)" function
13326 */
13327 static void
13328f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13329{
13330 rettv->v_type = VAR_STRING;
13331#ifdef FEAT_PERSISTENT_UNDO
13332 {
13333 char_u *fname = get_tv_string(&argvars[0]);
13334
13335 if (*fname == NUL)
13336 {
13337 /* If there is no file name there will be no undo file. */
13338 rettv->vval.v_string = NULL;
13339 }
13340 else
13341 {
13342 char_u *ffname = FullName_save(fname, FALSE);
13343
13344 if (ffname != NULL)
13345 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13346 vim_free(ffname);
13347 }
13348 }
13349#else
13350 rettv->vval.v_string = NULL;
13351#endif
13352}
13353
13354/*
13355 * "undotree()" function
13356 */
13357 static void
13358f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13359{
13360 if (rettv_dict_alloc(rettv) == OK)
13361 {
13362 dict_T *dict = rettv->vval.v_dict;
13363 list_T *list;
13364
13365 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13366 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13367 dict_add_nr_str(dict, "save_last",
13368 (long)curbuf->b_u_save_nr_last, NULL);
13369 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13370 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13371 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13372
13373 list = list_alloc();
13374 if (list != NULL)
13375 {
13376 u_eval_tree(curbuf->b_u_oldhead, list);
13377 dict_add_list(dict, "entries", list);
13378 }
13379 }
13380}
13381
13382/*
13383 * "values(dict)" function
13384 */
13385 static void
13386f_values(typval_T *argvars, typval_T *rettv)
13387{
13388 dict_list(argvars, rettv, 1);
13389}
13390
13391/*
13392 * "virtcol(string)" function
13393 */
13394 static void
13395f_virtcol(typval_T *argvars, typval_T *rettv)
13396{
13397 colnr_T vcol = 0;
13398 pos_T *fp;
13399 int fnum = curbuf->b_fnum;
13400
13401 fp = var2fpos(&argvars[0], FALSE, &fnum);
13402 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13403 && fnum == curbuf->b_fnum)
13404 {
13405 getvvcol(curwin, fp, NULL, NULL, &vcol);
13406 ++vcol;
13407 }
13408
13409 rettv->vval.v_number = vcol;
13410}
13411
13412/*
13413 * "visualmode()" function
13414 */
13415 static void
13416f_visualmode(typval_T *argvars, typval_T *rettv)
13417{
13418 char_u str[2];
13419
13420 rettv->v_type = VAR_STRING;
13421 str[0] = curbuf->b_visual_mode_eval;
13422 str[1] = NUL;
13423 rettv->vval.v_string = vim_strsave(str);
13424
13425 /* A non-zero number or non-empty string argument: reset mode. */
13426 if (non_zero_arg(&argvars[0]))
13427 curbuf->b_visual_mode_eval = NUL;
13428}
13429
13430/*
13431 * "wildmenumode()" function
13432 */
13433 static void
13434f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13435{
13436#ifdef FEAT_WILDMENU
13437 if (wild_menu_showing)
13438 rettv->vval.v_number = 1;
13439#endif
13440}
13441
13442/*
13443 * "winbufnr(nr)" function
13444 */
13445 static void
13446f_winbufnr(typval_T *argvars, typval_T *rettv)
13447{
13448 win_T *wp;
13449
13450 wp = find_win_by_nr(&argvars[0], NULL);
13451 if (wp == NULL)
13452 rettv->vval.v_number = -1;
13453 else
13454 rettv->vval.v_number = wp->w_buffer->b_fnum;
13455}
13456
13457/*
13458 * "wincol()" function
13459 */
13460 static void
13461f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13462{
13463 validate_cursor();
13464 rettv->vval.v_number = curwin->w_wcol + 1;
13465}
13466
13467/*
13468 * "winheight(nr)" function
13469 */
13470 static void
13471f_winheight(typval_T *argvars, typval_T *rettv)
13472{
13473 win_T *wp;
13474
13475 wp = find_win_by_nr(&argvars[0], NULL);
13476 if (wp == NULL)
13477 rettv->vval.v_number = -1;
13478 else
13479 rettv->vval.v_number = wp->w_height;
13480}
13481
13482/*
13483 * "winline()" function
13484 */
13485 static void
13486f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13487{
13488 validate_cursor();
13489 rettv->vval.v_number = curwin->w_wrow + 1;
13490}
13491
13492/*
13493 * "winnr()" function
13494 */
13495 static void
13496f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13497{
13498 int nr = 1;
13499
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013500 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013501 rettv->vval.v_number = nr;
13502}
13503
13504/*
13505 * "winrestcmd()" function
13506 */
13507 static void
13508f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13509{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013510 win_T *wp;
13511 int winnr = 1;
13512 garray_T ga;
13513 char_u buf[50];
13514
13515 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013516 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013517 {
13518 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13519 ga_concat(&ga, buf);
13520 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13521 ga_concat(&ga, buf);
13522 ++winnr;
13523 }
13524 ga_append(&ga, NUL);
13525
13526 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013527 rettv->v_type = VAR_STRING;
13528}
13529
13530/*
13531 * "winrestview()" function
13532 */
13533 static void
13534f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13535{
13536 dict_T *dict;
13537
13538 if (argvars[0].v_type != VAR_DICT
13539 || (dict = argvars[0].vval.v_dict) == NULL)
13540 EMSG(_(e_invarg));
13541 else
13542 {
13543 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13544 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13545 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13546 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13547#ifdef FEAT_VIRTUALEDIT
13548 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13549 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13550#endif
13551 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13552 {
13553 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13554 curwin->w_set_curswant = FALSE;
13555 }
13556
13557 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13558 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13559#ifdef FEAT_DIFF
13560 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13561 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13562#endif
13563 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13564 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13565 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13566 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13567
13568 check_cursor();
13569 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013570 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013571 changed_window_setting();
13572
13573 if (curwin->w_topline <= 0)
13574 curwin->w_topline = 1;
13575 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13576 curwin->w_topline = curbuf->b_ml.ml_line_count;
13577#ifdef FEAT_DIFF
13578 check_topfill(curwin, TRUE);
13579#endif
13580 }
13581}
13582
13583/*
13584 * "winsaveview()" function
13585 */
13586 static void
13587f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13588{
13589 dict_T *dict;
13590
13591 if (rettv_dict_alloc(rettv) == FAIL)
13592 return;
13593 dict = rettv->vval.v_dict;
13594
13595 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13596 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13597#ifdef FEAT_VIRTUALEDIT
13598 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13599#endif
13600 update_curswant();
13601 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13602
13603 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13604#ifdef FEAT_DIFF
13605 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13606#endif
13607 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13608 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13609}
13610
13611/*
13612 * "winwidth(nr)" function
13613 */
13614 static void
13615f_winwidth(typval_T *argvars, typval_T *rettv)
13616{
13617 win_T *wp;
13618
13619 wp = find_win_by_nr(&argvars[0], NULL);
13620 if (wp == NULL)
13621 rettv->vval.v_number = -1;
13622 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013623 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013624}
13625
13626/*
13627 * "wordcount()" function
13628 */
13629 static void
13630f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13631{
13632 if (rettv_dict_alloc(rettv) == FAIL)
13633 return;
13634 cursor_pos_info(rettv->vval.v_dict);
13635}
13636
13637/*
13638 * "writefile()" function
13639 */
13640 static void
13641f_writefile(typval_T *argvars, typval_T *rettv)
13642{
13643 int binary = FALSE;
13644 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013645#ifdef HAVE_FSYNC
13646 int do_fsync = p_fs;
13647#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013648 char_u *fname;
13649 FILE *fd;
13650 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013651 listitem_T *li;
13652 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013653
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013654 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013655 if (check_restricted() || check_secure())
13656 return;
13657
13658 if (argvars[0].v_type != VAR_LIST)
13659 {
13660 EMSG2(_(e_listarg), "writefile()");
13661 return;
13662 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013663 list = argvars[0].vval.v_list;
13664 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013665 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013666 for (li = list->lv_first; li != NULL; li = li->li_next)
13667 if (get_tv_string_chk(&li->li_tv) == NULL)
13668 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013669
13670 if (argvars[2].v_type != VAR_UNKNOWN)
13671 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013672 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13673
13674 if (arg2 == NULL)
13675 return;
13676 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013677 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013678 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013679 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013680#ifdef HAVE_FSYNC
13681 if (vim_strchr(arg2, 's') != NULL)
13682 do_fsync = TRUE;
13683 else if (vim_strchr(arg2, 'S') != NULL)
13684 do_fsync = FALSE;
13685#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013686 }
13687
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013688 fname = get_tv_string_chk(&argvars[1]);
13689 if (fname == NULL)
13690 return;
13691
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013692 /* Always open the file in binary mode, library functions have a mind of
13693 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013694 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13695 append ? APPENDBIN : WRITEBIN)) == NULL)
13696 {
13697 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13698 ret = -1;
13699 }
13700 else
13701 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013702 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013703 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013704#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013705 else if (do_fsync)
13706 /* Ignore the error, the user wouldn't know what to do about it.
13707 * May happen for a device. */
13708 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013709#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013710 fclose(fd);
13711 }
13712
13713 rettv->vval.v_number = ret;
13714}
13715
13716/*
13717 * "xor(expr, expr)" function
13718 */
13719 static void
13720f_xor(typval_T *argvars, typval_T *rettv)
13721{
13722 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13723 ^ get_tv_number_chk(&argvars[1], NULL);
13724}
13725
13726
13727#endif /* FEAT_EVAL */