blob: 9441bd89f4f92c027c79c27cdae24eff7df21642 [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);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200297#ifdef FEAT_JOB_CHANNEL
298static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
299static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
300#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200301static void f_pumvisible(typval_T *argvars, typval_T *rettv);
302#ifdef FEAT_PYTHON3
303static void f_py3eval(typval_T *argvars, typval_T *rettv);
304#endif
305#ifdef FEAT_PYTHON
306static void f_pyeval(typval_T *argvars, typval_T *rettv);
307#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100308#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
309static void f_pyxeval(typval_T *argvars, typval_T *rettv);
310#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200311static void f_range(typval_T *argvars, typval_T *rettv);
312static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200313static void f_reg_executing(typval_T *argvars, typval_T *rettv);
314static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200315static void f_reltime(typval_T *argvars, typval_T *rettv);
316#ifdef FEAT_FLOAT
317static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
318#endif
319static void f_reltimestr(typval_T *argvars, typval_T *rettv);
320static void f_remote_expr(typval_T *argvars, typval_T *rettv);
321static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
322static void f_remote_peek(typval_T *argvars, typval_T *rettv);
323static void f_remote_read(typval_T *argvars, typval_T *rettv);
324static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100325static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200326static void f_remove(typval_T *argvars, typval_T *rettv);
327static void f_rename(typval_T *argvars, typval_T *rettv);
328static void f_repeat(typval_T *argvars, typval_T *rettv);
329static void f_resolve(typval_T *argvars, typval_T *rettv);
330static void f_reverse(typval_T *argvars, typval_T *rettv);
331#ifdef FEAT_FLOAT
332static void f_round(typval_T *argvars, typval_T *rettv);
333#endif
334static void f_screenattr(typval_T *argvars, typval_T *rettv);
335static void f_screenchar(typval_T *argvars, typval_T *rettv);
336static void f_screencol(typval_T *argvars, typval_T *rettv);
337static void f_screenrow(typval_T *argvars, typval_T *rettv);
338static void f_search(typval_T *argvars, typval_T *rettv);
339static void f_searchdecl(typval_T *argvars, typval_T *rettv);
340static void f_searchpair(typval_T *argvars, typval_T *rettv);
341static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
342static void f_searchpos(typval_T *argvars, typval_T *rettv);
343static void f_server2client(typval_T *argvars, typval_T *rettv);
344static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200345static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200346static void f_setbufvar(typval_T *argvars, typval_T *rettv);
347static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
348static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
349static void f_setfperm(typval_T *argvars, typval_T *rettv);
350static void f_setline(typval_T *argvars, typval_T *rettv);
351static void f_setloclist(typval_T *argvars, typval_T *rettv);
352static void f_setmatches(typval_T *argvars, typval_T *rettv);
353static void f_setpos(typval_T *argvars, typval_T *rettv);
354static void f_setqflist(typval_T *argvars, typval_T *rettv);
355static void f_setreg(typval_T *argvars, typval_T *rettv);
356static void f_settabvar(typval_T *argvars, typval_T *rettv);
357static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
358static void f_setwinvar(typval_T *argvars, typval_T *rettv);
359#ifdef FEAT_CRYPT
360static void f_sha256(typval_T *argvars, typval_T *rettv);
361#endif /* FEAT_CRYPT */
362static void f_shellescape(typval_T *argvars, typval_T *rettv);
363static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
364static void f_simplify(typval_T *argvars, typval_T *rettv);
365#ifdef FEAT_FLOAT
366static void f_sin(typval_T *argvars, typval_T *rettv);
367static void f_sinh(typval_T *argvars, typval_T *rettv);
368#endif
369static void f_sort(typval_T *argvars, typval_T *rettv);
370static void f_soundfold(typval_T *argvars, typval_T *rettv);
371static void f_spellbadword(typval_T *argvars, typval_T *rettv);
372static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
373static void f_split(typval_T *argvars, typval_T *rettv);
374#ifdef FEAT_FLOAT
375static void f_sqrt(typval_T *argvars, typval_T *rettv);
376static void f_str2float(typval_T *argvars, typval_T *rettv);
377#endif
378static void f_str2nr(typval_T *argvars, typval_T *rettv);
379static void f_strchars(typval_T *argvars, typval_T *rettv);
380#ifdef HAVE_STRFTIME
381static void f_strftime(typval_T *argvars, typval_T *rettv);
382#endif
383static void f_strgetchar(typval_T *argvars, typval_T *rettv);
384static void f_stridx(typval_T *argvars, typval_T *rettv);
385static void f_string(typval_T *argvars, typval_T *rettv);
386static void f_strlen(typval_T *argvars, typval_T *rettv);
387static void f_strcharpart(typval_T *argvars, typval_T *rettv);
388static void f_strpart(typval_T *argvars, typval_T *rettv);
389static void f_strridx(typval_T *argvars, typval_T *rettv);
390static void f_strtrans(typval_T *argvars, typval_T *rettv);
391static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
392static void f_strwidth(typval_T *argvars, typval_T *rettv);
393static void f_submatch(typval_T *argvars, typval_T *rettv);
394static void f_substitute(typval_T *argvars, typval_T *rettv);
395static void f_synID(typval_T *argvars, typval_T *rettv);
396static void f_synIDattr(typval_T *argvars, typval_T *rettv);
397static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
398static void f_synstack(typval_T *argvars, typval_T *rettv);
399static void f_synconcealed(typval_T *argvars, typval_T *rettv);
400static void f_system(typval_T *argvars, typval_T *rettv);
401static void f_systemlist(typval_T *argvars, typval_T *rettv);
402static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
403static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
404static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
405static void f_taglist(typval_T *argvars, typval_T *rettv);
406static void f_tagfiles(typval_T *argvars, typval_T *rettv);
407static void f_tempname(typval_T *argvars, typval_T *rettv);
408static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
409static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200410static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100411static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200412static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100413static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200414#ifdef FEAT_JOB_CHANNEL
415static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
416#endif
417static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
418#ifdef FEAT_JOB_CHANNEL
419static void f_test_null_job(typval_T *argvars, typval_T *rettv);
420#endif
421static void f_test_null_list(typval_T *argvars, typval_T *rettv);
422static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
423static void f_test_null_string(typval_T *argvars, typval_T *rettv);
424static void f_test_settime(typval_T *argvars, typval_T *rettv);
425#ifdef FEAT_FLOAT
426static void f_tan(typval_T *argvars, typval_T *rettv);
427static void f_tanh(typval_T *argvars, typval_T *rettv);
428#endif
429#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200430static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200431static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200432static void f_timer_start(typval_T *argvars, typval_T *rettv);
433static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200434static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200435#endif
436static void f_tolower(typval_T *argvars, typval_T *rettv);
437static void f_toupper(typval_T *argvars, typval_T *rettv);
438static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100439static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200440#ifdef FEAT_FLOAT
441static void f_trunc(typval_T *argvars, typval_T *rettv);
442#endif
443static void f_type(typval_T *argvars, typval_T *rettv);
444static void f_undofile(typval_T *argvars, typval_T *rettv);
445static void f_undotree(typval_T *argvars, typval_T *rettv);
446static void f_uniq(typval_T *argvars, typval_T *rettv);
447static void f_values(typval_T *argvars, typval_T *rettv);
448static void f_virtcol(typval_T *argvars, typval_T *rettv);
449static void f_visualmode(typval_T *argvars, typval_T *rettv);
450static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
451static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
452static void f_win_getid(typval_T *argvars, typval_T *rettv);
453static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
454static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
455static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100456static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200457static void f_winbufnr(typval_T *argvars, typval_T *rettv);
458static void f_wincol(typval_T *argvars, typval_T *rettv);
459static void f_winheight(typval_T *argvars, typval_T *rettv);
460static void f_winline(typval_T *argvars, typval_T *rettv);
461static void f_winnr(typval_T *argvars, typval_T *rettv);
462static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
463static void f_winrestview(typval_T *argvars, typval_T *rettv);
464static void f_winsaveview(typval_T *argvars, typval_T *rettv);
465static void f_winwidth(typval_T *argvars, typval_T *rettv);
466static void f_writefile(typval_T *argvars, typval_T *rettv);
467static void f_wordcount(typval_T *argvars, typval_T *rettv);
468static void f_xor(typval_T *argvars, typval_T *rettv);
469
470/*
471 * Array with names and number of arguments of all internal functions
472 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
473 */
474static struct fst
475{
476 char *f_name; /* function name */
477 char f_min_argc; /* minimal number of arguments */
478 char f_max_argc; /* maximal number of arguments */
479 void (*f_func)(typval_T *args, typval_T *rvar);
480 /* implementation of function */
481} functions[] =
482{
483#ifdef FEAT_FLOAT
484 {"abs", 1, 1, f_abs},
485 {"acos", 1, 1, f_acos}, /* WJMc */
486#endif
487 {"add", 2, 2, f_add},
488 {"and", 2, 2, f_and},
489 {"append", 2, 2, f_append},
490 {"argc", 0, 0, f_argc},
491 {"argidx", 0, 0, f_argidx},
492 {"arglistid", 0, 2, f_arglistid},
493 {"argv", 0, 1, f_argv},
494#ifdef FEAT_FLOAT
495 {"asin", 1, 1, f_asin}, /* WJMc */
496#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100497 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200498 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100499 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200500 {"assert_exception", 1, 2, f_assert_exception},
501 {"assert_fails", 1, 2, f_assert_fails},
502 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100503 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200504 {"assert_match", 2, 3, f_assert_match},
505 {"assert_notequal", 2, 3, f_assert_notequal},
506 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100507 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200508 {"assert_true", 1, 2, f_assert_true},
509#ifdef FEAT_FLOAT
510 {"atan", 1, 1, f_atan},
511 {"atan2", 2, 2, f_atan2},
512#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100513#ifdef FEAT_BEVAL
514 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100515# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100516 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100517# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100518#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200519 {"browse", 4, 4, f_browse},
520 {"browsedir", 2, 2, f_browsedir},
521 {"bufexists", 1, 1, f_bufexists},
522 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
523 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
524 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
525 {"buflisted", 1, 1, f_buflisted},
526 {"bufloaded", 1, 1, f_bufloaded},
527 {"bufname", 1, 1, f_bufname},
528 {"bufnr", 1, 2, f_bufnr},
529 {"bufwinid", 1, 1, f_bufwinid},
530 {"bufwinnr", 1, 1, f_bufwinnr},
531 {"byte2line", 1, 1, f_byte2line},
532 {"byteidx", 2, 2, f_byteidx},
533 {"byteidxcomp", 2, 2, f_byteidxcomp},
534 {"call", 2, 3, f_call},
535#ifdef FEAT_FLOAT
536 {"ceil", 1, 1, f_ceil},
537#endif
538#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100539 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200540 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200541 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200542 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
543 {"ch_evalraw", 2, 3, f_ch_evalraw},
544 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
545 {"ch_getjob", 1, 1, f_ch_getjob},
546 {"ch_info", 1, 1, f_ch_info},
547 {"ch_log", 1, 2, f_ch_log},
548 {"ch_logfile", 1, 2, f_ch_logfile},
549 {"ch_open", 1, 2, f_ch_open},
550 {"ch_read", 1, 2, f_ch_read},
551 {"ch_readraw", 1, 2, f_ch_readraw},
552 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
553 {"ch_sendraw", 2, 3, f_ch_sendraw},
554 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200555 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200556#endif
557 {"changenr", 0, 0, f_changenr},
558 {"char2nr", 1, 2, f_char2nr},
559 {"cindent", 1, 1, f_cindent},
560 {"clearmatches", 0, 0, f_clearmatches},
561 {"col", 1, 1, f_col},
562#if defined(FEAT_INS_EXPAND)
563 {"complete", 2, 2, f_complete},
564 {"complete_add", 1, 1, f_complete_add},
565 {"complete_check", 0, 0, f_complete_check},
566#endif
567 {"confirm", 1, 4, f_confirm},
568 {"copy", 1, 1, f_copy},
569#ifdef FEAT_FLOAT
570 {"cos", 1, 1, f_cos},
571 {"cosh", 1, 1, f_cosh},
572#endif
573 {"count", 2, 4, f_count},
574 {"cscope_connection",0,3, f_cscope_connection},
575 {"cursor", 1, 3, f_cursor},
576 {"deepcopy", 1, 2, f_deepcopy},
577 {"delete", 1, 2, f_delete},
578 {"did_filetype", 0, 0, f_did_filetype},
579 {"diff_filler", 1, 1, f_diff_filler},
580 {"diff_hlID", 2, 2, f_diff_hlID},
581 {"empty", 1, 1, f_empty},
582 {"escape", 2, 2, f_escape},
583 {"eval", 1, 1, f_eval},
584 {"eventhandler", 0, 0, f_eventhandler},
585 {"executable", 1, 1, f_executable},
586 {"execute", 1, 2, f_execute},
587 {"exepath", 1, 1, f_exepath},
588 {"exists", 1, 1, f_exists},
589#ifdef FEAT_FLOAT
590 {"exp", 1, 1, f_exp},
591#endif
592 {"expand", 1, 3, f_expand},
593 {"extend", 2, 3, f_extend},
594 {"feedkeys", 1, 2, f_feedkeys},
595 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
596 {"filereadable", 1, 1, f_filereadable},
597 {"filewritable", 1, 1, f_filewritable},
598 {"filter", 2, 2, f_filter},
599 {"finddir", 1, 3, f_finddir},
600 {"findfile", 1, 3, f_findfile},
601#ifdef FEAT_FLOAT
602 {"float2nr", 1, 1, f_float2nr},
603 {"floor", 1, 1, f_floor},
604 {"fmod", 2, 2, f_fmod},
605#endif
606 {"fnameescape", 1, 1, f_fnameescape},
607 {"fnamemodify", 2, 2, f_fnamemodify},
608 {"foldclosed", 1, 1, f_foldclosed},
609 {"foldclosedend", 1, 1, f_foldclosedend},
610 {"foldlevel", 1, 1, f_foldlevel},
611 {"foldtext", 0, 0, f_foldtext},
612 {"foldtextresult", 1, 1, f_foldtextresult},
613 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200614 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200615 {"function", 1, 3, f_function},
616 {"garbagecollect", 0, 1, f_garbagecollect},
617 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200618 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200619 {"getbufline", 2, 3, f_getbufline},
620 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100621 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200622 {"getchar", 0, 1, f_getchar},
623 {"getcharmod", 0, 0, f_getcharmod},
624 {"getcharsearch", 0, 0, f_getcharsearch},
625 {"getcmdline", 0, 0, f_getcmdline},
626 {"getcmdpos", 0, 0, f_getcmdpos},
627 {"getcmdtype", 0, 0, f_getcmdtype},
628 {"getcmdwintype", 0, 0, f_getcmdwintype},
629#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200630 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200631#endif
632 {"getcurpos", 0, 0, f_getcurpos},
633 {"getcwd", 0, 2, f_getcwd},
634 {"getfontname", 0, 1, f_getfontname},
635 {"getfperm", 1, 1, f_getfperm},
636 {"getfsize", 1, 1, f_getfsize},
637 {"getftime", 1, 1, f_getftime},
638 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100639 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200640 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200641 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200642 {"getmatches", 0, 0, f_getmatches},
643 {"getpid", 0, 0, f_getpid},
644 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200645 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200646 {"getreg", 0, 3, f_getreg},
647 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200648 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200649 {"gettabvar", 2, 3, f_gettabvar},
650 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200651 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100652 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200653 {"getwinposx", 0, 0, f_getwinposx},
654 {"getwinposy", 0, 0, f_getwinposy},
655 {"getwinvar", 2, 3, f_getwinvar},
656 {"glob", 1, 4, f_glob},
657 {"glob2regpat", 1, 1, f_glob2regpat},
658 {"globpath", 2, 5, f_globpath},
659 {"has", 1, 1, f_has},
660 {"has_key", 2, 2, f_has_key},
661 {"haslocaldir", 0, 2, f_haslocaldir},
662 {"hasmapto", 1, 3, f_hasmapto},
663 {"highlightID", 1, 1, f_hlID}, /* obsolete */
664 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
665 {"histadd", 2, 2, f_histadd},
666 {"histdel", 1, 2, f_histdel},
667 {"histget", 1, 2, f_histget},
668 {"histnr", 1, 1, f_histnr},
669 {"hlID", 1, 1, f_hlID},
670 {"hlexists", 1, 1, f_hlexists},
671 {"hostname", 0, 0, f_hostname},
672 {"iconv", 3, 3, f_iconv},
673 {"indent", 1, 1, f_indent},
674 {"index", 2, 4, f_index},
675 {"input", 1, 3, f_input},
676 {"inputdialog", 1, 3, f_inputdialog},
677 {"inputlist", 1, 1, f_inputlist},
678 {"inputrestore", 0, 0, f_inputrestore},
679 {"inputsave", 0, 0, f_inputsave},
680 {"inputsecret", 1, 2, f_inputsecret},
681 {"insert", 2, 3, f_insert},
682 {"invert", 1, 1, f_invert},
683 {"isdirectory", 1, 1, f_isdirectory},
684 {"islocked", 1, 1, f_islocked},
685#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
686 {"isnan", 1, 1, f_isnan},
687#endif
688 {"items", 1, 1, f_items},
689#ifdef FEAT_JOB_CHANNEL
690 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200691 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200692 {"job_setoptions", 2, 2, f_job_setoptions},
693 {"job_start", 1, 2, f_job_start},
694 {"job_status", 1, 1, f_job_status},
695 {"job_stop", 1, 2, f_job_stop},
696#endif
697 {"join", 1, 2, f_join},
698 {"js_decode", 1, 1, f_js_decode},
699 {"js_encode", 1, 1, f_js_encode},
700 {"json_decode", 1, 1, f_json_decode},
701 {"json_encode", 1, 1, f_json_encode},
702 {"keys", 1, 1, f_keys},
703 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
704 {"len", 1, 1, f_len},
705 {"libcall", 3, 3, f_libcall},
706 {"libcallnr", 3, 3, f_libcallnr},
707 {"line", 1, 1, f_line},
708 {"line2byte", 1, 1, f_line2byte},
709 {"lispindent", 1, 1, f_lispindent},
710 {"localtime", 0, 0, f_localtime},
711#ifdef FEAT_FLOAT
712 {"log", 1, 1, f_log},
713 {"log10", 1, 1, f_log10},
714#endif
715#ifdef FEAT_LUA
716 {"luaeval", 1, 2, f_luaeval},
717#endif
718 {"map", 2, 2, f_map},
719 {"maparg", 1, 4, f_maparg},
720 {"mapcheck", 1, 3, f_mapcheck},
721 {"match", 2, 4, f_match},
722 {"matchadd", 2, 5, f_matchadd},
723 {"matchaddpos", 2, 5, f_matchaddpos},
724 {"matcharg", 1, 1, f_matcharg},
725 {"matchdelete", 1, 1, f_matchdelete},
726 {"matchend", 2, 4, f_matchend},
727 {"matchlist", 2, 4, f_matchlist},
728 {"matchstr", 2, 4, f_matchstr},
729 {"matchstrpos", 2, 4, f_matchstrpos},
730 {"max", 1, 1, f_max},
731 {"min", 1, 1, f_min},
732#ifdef vim_mkdir
733 {"mkdir", 1, 3, f_mkdir},
734#endif
735 {"mode", 0, 1, f_mode},
736#ifdef FEAT_MZSCHEME
737 {"mzeval", 1, 1, f_mzeval},
738#endif
739 {"nextnonblank", 1, 1, f_nextnonblank},
740 {"nr2char", 1, 2, f_nr2char},
741 {"or", 2, 2, f_or},
742 {"pathshorten", 1, 1, f_pathshorten},
743#ifdef FEAT_PERL
744 {"perleval", 1, 1, f_perleval},
745#endif
746#ifdef FEAT_FLOAT
747 {"pow", 2, 2, f_pow},
748#endif
749 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100750 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200751#ifdef FEAT_JOB_CHANNEL
752 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
753 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
754#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200755 {"pumvisible", 0, 0, f_pumvisible},
756#ifdef FEAT_PYTHON3
757 {"py3eval", 1, 1, f_py3eval},
758#endif
759#ifdef FEAT_PYTHON
760 {"pyeval", 1, 1, f_pyeval},
761#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100762#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
763 {"pyxeval", 1, 1, f_pyxeval},
764#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200765 {"range", 1, 3, f_range},
766 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200767 {"reg_executing", 0, 0, f_reg_executing},
768 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200769 {"reltime", 0, 2, f_reltime},
770#ifdef FEAT_FLOAT
771 {"reltimefloat", 1, 1, f_reltimefloat},
772#endif
773 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100774 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200775 {"remote_foreground", 1, 1, f_remote_foreground},
776 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100777 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200778 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100779 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200780 {"remove", 2, 3, f_remove},
781 {"rename", 2, 2, f_rename},
782 {"repeat", 2, 2, f_repeat},
783 {"resolve", 1, 1, f_resolve},
784 {"reverse", 1, 1, f_reverse},
785#ifdef FEAT_FLOAT
786 {"round", 1, 1, f_round},
787#endif
788 {"screenattr", 2, 2, f_screenattr},
789 {"screenchar", 2, 2, f_screenchar},
790 {"screencol", 0, 0, f_screencol},
791 {"screenrow", 0, 0, f_screenrow},
792 {"search", 1, 4, f_search},
793 {"searchdecl", 1, 3, f_searchdecl},
794 {"searchpair", 3, 7, f_searchpair},
795 {"searchpairpos", 3, 7, f_searchpairpos},
796 {"searchpos", 1, 4, f_searchpos},
797 {"server2client", 2, 2, f_server2client},
798 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200799 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200800 {"setbufvar", 3, 3, f_setbufvar},
801 {"setcharsearch", 1, 1, f_setcharsearch},
802 {"setcmdpos", 1, 1, f_setcmdpos},
803 {"setfperm", 2, 2, f_setfperm},
804 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200805 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200806 {"setmatches", 1, 1, f_setmatches},
807 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200808 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200809 {"setreg", 2, 3, f_setreg},
810 {"settabvar", 3, 3, f_settabvar},
811 {"settabwinvar", 4, 4, f_settabwinvar},
812 {"setwinvar", 3, 3, f_setwinvar},
813#ifdef FEAT_CRYPT
814 {"sha256", 1, 1, f_sha256},
815#endif
816 {"shellescape", 1, 2, f_shellescape},
817 {"shiftwidth", 0, 0, f_shiftwidth},
818 {"simplify", 1, 1, f_simplify},
819#ifdef FEAT_FLOAT
820 {"sin", 1, 1, f_sin},
821 {"sinh", 1, 1, f_sinh},
822#endif
823 {"sort", 1, 3, f_sort},
824 {"soundfold", 1, 1, f_soundfold},
825 {"spellbadword", 0, 1, f_spellbadword},
826 {"spellsuggest", 1, 3, f_spellsuggest},
827 {"split", 1, 3, f_split},
828#ifdef FEAT_FLOAT
829 {"sqrt", 1, 1, f_sqrt},
830 {"str2float", 1, 1, f_str2float},
831#endif
832 {"str2nr", 1, 2, f_str2nr},
833 {"strcharpart", 2, 3, f_strcharpart},
834 {"strchars", 1, 2, f_strchars},
835 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
836#ifdef HAVE_STRFTIME
837 {"strftime", 1, 2, f_strftime},
838#endif
839 {"strgetchar", 2, 2, f_strgetchar},
840 {"stridx", 2, 3, f_stridx},
841 {"string", 1, 1, f_string},
842 {"strlen", 1, 1, f_strlen},
843 {"strpart", 2, 3, f_strpart},
844 {"strridx", 2, 3, f_strridx},
845 {"strtrans", 1, 1, f_strtrans},
846 {"strwidth", 1, 1, f_strwidth},
847 {"submatch", 1, 2, f_submatch},
848 {"substitute", 4, 4, f_substitute},
849 {"synID", 3, 3, f_synID},
850 {"synIDattr", 2, 3, f_synIDattr},
851 {"synIDtrans", 1, 1, f_synIDtrans},
852 {"synconcealed", 2, 2, f_synconcealed},
853 {"synstack", 2, 2, f_synstack},
854 {"system", 1, 2, f_system},
855 {"systemlist", 1, 2, f_systemlist},
856 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
857 {"tabpagenr", 0, 1, f_tabpagenr},
858 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
859 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100860 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200861#ifdef FEAT_FLOAT
862 {"tan", 1, 1, f_tan},
863 {"tanh", 1, 1, f_tanh},
864#endif
865 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200866#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100867 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
868 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100869 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200870 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200871# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
872 {"term_getansicolors", 1, 1, f_term_getansicolors},
873# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200874 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200875 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200876 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200877 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200878 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200879 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200880 {"term_getstatus", 1, 1, f_term_getstatus},
881 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200882 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200883 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200884 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200885 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200886# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
887 {"term_setansicolors", 2, 2, f_term_setansicolors},
888# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100889 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100890 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200891 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200892 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200893 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200894#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200895 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
896 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200897 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200898 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100899 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200900#ifdef FEAT_JOB_CHANNEL
901 {"test_null_channel", 0, 0, f_test_null_channel},
902#endif
903 {"test_null_dict", 0, 0, f_test_null_dict},
904#ifdef FEAT_JOB_CHANNEL
905 {"test_null_job", 0, 0, f_test_null_job},
906#endif
907 {"test_null_list", 0, 0, f_test_null_list},
908 {"test_null_partial", 0, 0, f_test_null_partial},
909 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100910 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200911 {"test_settime", 1, 1, f_test_settime},
912#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200913 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200914 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200915 {"timer_start", 2, 3, f_timer_start},
916 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200917 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200918#endif
919 {"tolower", 1, 1, f_tolower},
920 {"toupper", 1, 1, f_toupper},
921 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100922 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200923#ifdef FEAT_FLOAT
924 {"trunc", 1, 1, f_trunc},
925#endif
926 {"type", 1, 1, f_type},
927 {"undofile", 1, 1, f_undofile},
928 {"undotree", 0, 0, f_undotree},
929 {"uniq", 1, 3, f_uniq},
930 {"values", 1, 1, f_values},
931 {"virtcol", 1, 1, f_virtcol},
932 {"visualmode", 0, 1, f_visualmode},
933 {"wildmenumode", 0, 0, f_wildmenumode},
934 {"win_findbuf", 1, 1, f_win_findbuf},
935 {"win_getid", 0, 2, f_win_getid},
936 {"win_gotoid", 1, 1, f_win_gotoid},
937 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
938 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100939 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200940 {"winbufnr", 1, 1, f_winbufnr},
941 {"wincol", 0, 0, f_wincol},
942 {"winheight", 1, 1, f_winheight},
943 {"winline", 0, 0, f_winline},
944 {"winnr", 0, 1, f_winnr},
945 {"winrestcmd", 0, 0, f_winrestcmd},
946 {"winrestview", 1, 1, f_winrestview},
947 {"winsaveview", 0, 0, f_winsaveview},
948 {"winwidth", 1, 1, f_winwidth},
949 {"wordcount", 0, 0, f_wordcount},
950 {"writefile", 2, 3, f_writefile},
951 {"xor", 2, 2, f_xor},
952};
953
954#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
955
956/*
957 * Function given to ExpandGeneric() to obtain the list of internal
958 * or user defined function names.
959 */
960 char_u *
961get_function_name(expand_T *xp, int idx)
962{
963 static int intidx = -1;
964 char_u *name;
965
966 if (idx == 0)
967 intidx = -1;
968 if (intidx < 0)
969 {
970 name = get_user_func_name(xp, idx);
971 if (name != NULL)
972 return name;
973 }
974 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
975 {
976 STRCPY(IObuff, functions[intidx].f_name);
977 STRCAT(IObuff, "(");
978 if (functions[intidx].f_max_argc == 0)
979 STRCAT(IObuff, ")");
980 return IObuff;
981 }
982
983 return NULL;
984}
985
986/*
987 * Function given to ExpandGeneric() to obtain the list of internal or
988 * user defined variable or function names.
989 */
990 char_u *
991get_expr_name(expand_T *xp, int idx)
992{
993 static int intidx = -1;
994 char_u *name;
995
996 if (idx == 0)
997 intidx = -1;
998 if (intidx < 0)
999 {
1000 name = get_function_name(xp, idx);
1001 if (name != NULL)
1002 return name;
1003 }
1004 return get_user_var_name(xp, ++intidx);
1005}
1006
1007#endif /* FEAT_CMDL_COMPL */
1008
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001009/*
1010 * Find internal function in table above.
1011 * Return index, or -1 if not found
1012 */
1013 int
1014find_internal_func(
1015 char_u *name) /* name of the function */
1016{
1017 int first = 0;
1018 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1019 int cmp;
1020 int x;
1021
1022 /*
1023 * Find the function name in the table. Binary search.
1024 */
1025 while (first <= last)
1026 {
1027 x = first + ((unsigned)(last - first) >> 1);
1028 cmp = STRCMP(name, functions[x].f_name);
1029 if (cmp < 0)
1030 last = x - 1;
1031 else if (cmp > 0)
1032 first = x + 1;
1033 else
1034 return x;
1035 }
1036 return -1;
1037}
1038
1039 int
1040call_internal_func(
1041 char_u *name,
1042 int argcount,
1043 typval_T *argvars,
1044 typval_T *rettv)
1045{
1046 int i;
1047
1048 i = find_internal_func(name);
1049 if (i < 0)
1050 return ERROR_UNKNOWN;
1051 if (argcount < functions[i].f_min_argc)
1052 return ERROR_TOOFEW;
1053 if (argcount > functions[i].f_max_argc)
1054 return ERROR_TOOMANY;
1055 argvars[argcount].v_type = VAR_UNKNOWN;
1056 functions[i].f_func(argvars, rettv);
1057 return ERROR_NONE;
1058}
1059
1060/*
1061 * Return TRUE for a non-zero Number and a non-empty String.
1062 */
1063 static int
1064non_zero_arg(typval_T *argvars)
1065{
1066 return ((argvars[0].v_type == VAR_NUMBER
1067 && argvars[0].vval.v_number != 0)
1068 || (argvars[0].v_type == VAR_SPECIAL
1069 && argvars[0].vval.v_number == VVAL_TRUE)
1070 || (argvars[0].v_type == VAR_STRING
1071 && argvars[0].vval.v_string != NULL
1072 && *argvars[0].vval.v_string != NUL));
1073}
1074
1075/*
1076 * Get the lnum from the first argument.
1077 * Also accepts ".", "$", etc., but that only works for the current buffer.
1078 * Returns -1 on error.
1079 */
1080 static linenr_T
1081get_tv_lnum(typval_T *argvars)
1082{
1083 typval_T rettv;
1084 linenr_T lnum;
1085
1086 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1087 if (lnum == 0) /* no valid number, try using line() */
1088 {
1089 rettv.v_type = VAR_NUMBER;
1090 f_line(argvars, &rettv);
1091 lnum = (linenr_T)rettv.vval.v_number;
1092 clear_tv(&rettv);
1093 }
1094 return lnum;
1095}
1096
1097#ifdef FEAT_FLOAT
1098static int get_float_arg(typval_T *argvars, float_T *f);
1099
1100/*
1101 * Get the float value of "argvars[0]" into "f".
1102 * Returns FAIL when the argument is not a Number or Float.
1103 */
1104 static int
1105get_float_arg(typval_T *argvars, float_T *f)
1106{
1107 if (argvars[0].v_type == VAR_FLOAT)
1108 {
1109 *f = argvars[0].vval.v_float;
1110 return OK;
1111 }
1112 if (argvars[0].v_type == VAR_NUMBER)
1113 {
1114 *f = (float_T)argvars[0].vval.v_number;
1115 return OK;
1116 }
1117 EMSG(_("E808: Number or Float required"));
1118 return FAIL;
1119}
1120
1121/*
1122 * "abs(expr)" function
1123 */
1124 static void
1125f_abs(typval_T *argvars, typval_T *rettv)
1126{
1127 if (argvars[0].v_type == VAR_FLOAT)
1128 {
1129 rettv->v_type = VAR_FLOAT;
1130 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1131 }
1132 else
1133 {
1134 varnumber_T n;
1135 int error = FALSE;
1136
1137 n = get_tv_number_chk(&argvars[0], &error);
1138 if (error)
1139 rettv->vval.v_number = -1;
1140 else if (n > 0)
1141 rettv->vval.v_number = n;
1142 else
1143 rettv->vval.v_number = -n;
1144 }
1145}
1146
1147/*
1148 * "acos()" function
1149 */
1150 static void
1151f_acos(typval_T *argvars, typval_T *rettv)
1152{
1153 float_T f = 0.0;
1154
1155 rettv->v_type = VAR_FLOAT;
1156 if (get_float_arg(argvars, &f) == OK)
1157 rettv->vval.v_float = acos(f);
1158 else
1159 rettv->vval.v_float = 0.0;
1160}
1161#endif
1162
1163/*
1164 * "add(list, item)" function
1165 */
1166 static void
1167f_add(typval_T *argvars, typval_T *rettv)
1168{
1169 list_T *l;
1170
1171 rettv->vval.v_number = 1; /* Default: Failed */
1172 if (argvars[0].v_type == VAR_LIST)
1173 {
1174 if ((l = argvars[0].vval.v_list) != NULL
1175 && !tv_check_lock(l->lv_lock,
1176 (char_u *)N_("add() argument"), TRUE)
1177 && list_append_tv(l, &argvars[1]) == OK)
1178 copy_tv(&argvars[0], rettv);
1179 }
1180 else
1181 EMSG(_(e_listreq));
1182}
1183
1184/*
1185 * "and(expr, expr)" function
1186 */
1187 static void
1188f_and(typval_T *argvars, typval_T *rettv)
1189{
1190 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1191 & get_tv_number_chk(&argvars[1], NULL);
1192}
1193
1194/*
1195 * "append(lnum, string/list)" function
1196 */
1197 static void
1198f_append(typval_T *argvars, typval_T *rettv)
1199{
1200 long lnum;
1201 char_u *line;
1202 list_T *l = NULL;
1203 listitem_T *li = NULL;
1204 typval_T *tv;
1205 long added = 0;
1206
1207 /* When coming here from Insert mode, sync undo, so that this can be
1208 * undone separately from what was previously inserted. */
1209 if (u_sync_once == 2)
1210 {
1211 u_sync_once = 1; /* notify that u_sync() was called */
1212 u_sync(TRUE);
1213 }
1214
1215 lnum = get_tv_lnum(argvars);
1216 if (lnum >= 0
1217 && lnum <= curbuf->b_ml.ml_line_count
1218 && u_save(lnum, lnum + 1) == OK)
1219 {
1220 if (argvars[1].v_type == VAR_LIST)
1221 {
1222 l = argvars[1].vval.v_list;
1223 if (l == NULL)
1224 return;
1225 li = l->lv_first;
1226 }
1227 for (;;)
1228 {
1229 if (l == NULL)
1230 tv = &argvars[1]; /* append a string */
1231 else if (li == NULL)
1232 break; /* end of list */
1233 else
1234 tv = &li->li_tv; /* append item from list */
1235 line = get_tv_string_chk(tv);
1236 if (line == NULL) /* type error */
1237 {
1238 rettv->vval.v_number = 1; /* Failed */
1239 break;
1240 }
1241 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1242 ++added;
1243 if (l == NULL)
1244 break;
1245 li = li->li_next;
1246 }
1247
1248 appended_lines_mark(lnum, added);
1249 if (curwin->w_cursor.lnum > lnum)
1250 curwin->w_cursor.lnum += added;
Bram Moolenaarf2732452018-06-03 14:47:35 +02001251#ifdef FEAT_JOB_CHANNEL
1252 if (bt_prompt(curbuf) && (State & INSERT))
1253 // show the line with the prompt
1254 update_topline();
1255#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001256 }
1257 else
1258 rettv->vval.v_number = 1; /* Failed */
1259}
1260
1261/*
1262 * "argc()" function
1263 */
1264 static void
1265f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1266{
1267 rettv->vval.v_number = ARGCOUNT;
1268}
1269
1270/*
1271 * "argidx()" function
1272 */
1273 static void
1274f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1275{
1276 rettv->vval.v_number = curwin->w_arg_idx;
1277}
1278
1279/*
1280 * "arglistid()" function
1281 */
1282 static void
1283f_arglistid(typval_T *argvars, typval_T *rettv)
1284{
1285 win_T *wp;
1286
1287 rettv->vval.v_number = -1;
1288 wp = find_tabwin(&argvars[0], &argvars[1]);
1289 if (wp != NULL)
1290 rettv->vval.v_number = wp->w_alist->id;
1291}
1292
1293/*
1294 * "argv(nr)" function
1295 */
1296 static void
1297f_argv(typval_T *argvars, typval_T *rettv)
1298{
1299 int idx;
1300
1301 if (argvars[0].v_type != VAR_UNKNOWN)
1302 {
1303 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1304 if (idx >= 0 && idx < ARGCOUNT)
1305 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1306 else
1307 rettv->vval.v_string = NULL;
1308 rettv->v_type = VAR_STRING;
1309 }
1310 else if (rettv_list_alloc(rettv) == OK)
1311 for (idx = 0; idx < ARGCOUNT; ++idx)
1312 list_append_string(rettv->vval.v_list,
1313 alist_name(&ARGLIST[idx]), -1);
1314}
1315
1316/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001317 * "assert_beeps(cmd [, error])" function
1318 */
1319 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001320f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001321{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001322 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001323}
1324
1325/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001326 * "assert_equal(expected, actual[, msg])" function
1327 */
1328 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001329f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001330{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001331 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001332}
1333
1334/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001335 * "assert_equalfile(fname-one, fname-two)" function
1336 */
1337 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001338f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001339{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001340 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001341}
1342
1343/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001344 * "assert_notequal(expected, actual[, msg])" function
1345 */
1346 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001347f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001348{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001349 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001350}
1351
1352/*
1353 * "assert_exception(string[, msg])" function
1354 */
1355 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001356f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001357{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001358 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001359}
1360
1361/*
1362 * "assert_fails(cmd [, error])" function
1363 */
1364 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001365f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001366{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001367 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001368}
1369
1370/*
1371 * "assert_false(actual[, msg])" function
1372 */
1373 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001374f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001375{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001376 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001377}
1378
1379/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001380 * "assert_inrange(lower, upper[, msg])" function
1381 */
1382 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001383f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001384{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001385 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001386}
1387
1388/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001389 * "assert_match(pattern, actual[, msg])" function
1390 */
1391 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001392f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001393{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001394 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001395}
1396
1397/*
1398 * "assert_notmatch(pattern, actual[, msg])" function
1399 */
1400 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001401f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001402{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001403 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001404}
1405
1406/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001407 * "assert_report(msg)" function
1408 */
1409 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001410f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001411{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001412 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001413}
1414
1415/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001416 * "assert_true(actual[, msg])" function
1417 */
1418 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001419f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001420{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001421 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001422}
1423
1424#ifdef FEAT_FLOAT
1425/*
1426 * "asin()" function
1427 */
1428 static void
1429f_asin(typval_T *argvars, typval_T *rettv)
1430{
1431 float_T f = 0.0;
1432
1433 rettv->v_type = VAR_FLOAT;
1434 if (get_float_arg(argvars, &f) == OK)
1435 rettv->vval.v_float = asin(f);
1436 else
1437 rettv->vval.v_float = 0.0;
1438}
1439
1440/*
1441 * "atan()" function
1442 */
1443 static void
1444f_atan(typval_T *argvars, typval_T *rettv)
1445{
1446 float_T f = 0.0;
1447
1448 rettv->v_type = VAR_FLOAT;
1449 if (get_float_arg(argvars, &f) == OK)
1450 rettv->vval.v_float = atan(f);
1451 else
1452 rettv->vval.v_float = 0.0;
1453}
1454
1455/*
1456 * "atan2()" function
1457 */
1458 static void
1459f_atan2(typval_T *argvars, typval_T *rettv)
1460{
1461 float_T fx = 0.0, fy = 0.0;
1462
1463 rettv->v_type = VAR_FLOAT;
1464 if (get_float_arg(argvars, &fx) == OK
1465 && get_float_arg(&argvars[1], &fy) == OK)
1466 rettv->vval.v_float = atan2(fx, fy);
1467 else
1468 rettv->vval.v_float = 0.0;
1469}
1470#endif
1471
1472/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001473 * "balloon_show()" function
1474 */
1475#ifdef FEAT_BEVAL
1476 static void
1477f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1478{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001479 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001480 {
1481 if (argvars[0].v_type == VAR_LIST
1482# ifdef FEAT_GUI
1483 && !gui.in_use
1484# endif
1485 )
1486 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1487 else
1488 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1489 }
1490}
1491
Bram Moolenaar669a8282017-11-19 20:13:05 +01001492# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001493 static void
1494f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1495{
1496 if (rettv_list_alloc(rettv) == OK)
1497 {
1498 char_u *msg = get_tv_string_chk(&argvars[0]);
1499
1500 if (msg != NULL)
1501 {
1502 pumitem_T *array;
1503 int size = split_message(msg, &array);
1504 int i;
1505
1506 /* Skip the first and last item, they are always empty. */
1507 for (i = 1; i < size - 1; ++i)
1508 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001509 while (size > 0)
1510 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001511 vim_free(array);
1512 }
1513 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001514}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001515# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001516#endif
1517
1518/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001519 * "browse(save, title, initdir, default)" function
1520 */
1521 static void
1522f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1523{
1524#ifdef FEAT_BROWSE
1525 int save;
1526 char_u *title;
1527 char_u *initdir;
1528 char_u *defname;
1529 char_u buf[NUMBUFLEN];
1530 char_u buf2[NUMBUFLEN];
1531 int error = FALSE;
1532
1533 save = (int)get_tv_number_chk(&argvars[0], &error);
1534 title = get_tv_string_chk(&argvars[1]);
1535 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1536 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1537
1538 if (error || title == NULL || initdir == NULL || defname == NULL)
1539 rettv->vval.v_string = NULL;
1540 else
1541 rettv->vval.v_string =
1542 do_browse(save ? BROWSE_SAVE : 0,
1543 title, defname, NULL, initdir, NULL, curbuf);
1544#else
1545 rettv->vval.v_string = NULL;
1546#endif
1547 rettv->v_type = VAR_STRING;
1548}
1549
1550/*
1551 * "browsedir(title, initdir)" function
1552 */
1553 static void
1554f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1555{
1556#ifdef FEAT_BROWSE
1557 char_u *title;
1558 char_u *initdir;
1559 char_u buf[NUMBUFLEN];
1560
1561 title = get_tv_string_chk(&argvars[0]);
1562 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1563
1564 if (title == NULL || initdir == NULL)
1565 rettv->vval.v_string = NULL;
1566 else
1567 rettv->vval.v_string = do_browse(BROWSE_DIR,
1568 title, NULL, NULL, initdir, NULL, curbuf);
1569#else
1570 rettv->vval.v_string = NULL;
1571#endif
1572 rettv->v_type = VAR_STRING;
1573}
1574
1575static buf_T *find_buffer(typval_T *avar);
1576
1577/*
1578 * Find a buffer by number or exact name.
1579 */
1580 static buf_T *
1581find_buffer(typval_T *avar)
1582{
1583 buf_T *buf = NULL;
1584
1585 if (avar->v_type == VAR_NUMBER)
1586 buf = buflist_findnr((int)avar->vval.v_number);
1587 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1588 {
1589 buf = buflist_findname_exp(avar->vval.v_string);
1590 if (buf == NULL)
1591 {
1592 /* No full path name match, try a match with a URL or a "nofile"
1593 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001594 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001595 if (buf->b_fname != NULL
1596 && (path_with_url(buf->b_fname)
1597#ifdef FEAT_QUICKFIX
1598 || bt_nofile(buf)
1599#endif
1600 )
1601 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1602 break;
1603 }
1604 }
1605 return buf;
1606}
1607
1608/*
1609 * "bufexists(expr)" function
1610 */
1611 static void
1612f_bufexists(typval_T *argvars, typval_T *rettv)
1613{
1614 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1615}
1616
1617/*
1618 * "buflisted(expr)" function
1619 */
1620 static void
1621f_buflisted(typval_T *argvars, typval_T *rettv)
1622{
1623 buf_T *buf;
1624
1625 buf = find_buffer(&argvars[0]);
1626 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1627}
1628
1629/*
1630 * "bufloaded(expr)" function
1631 */
1632 static void
1633f_bufloaded(typval_T *argvars, typval_T *rettv)
1634{
1635 buf_T *buf;
1636
1637 buf = find_buffer(&argvars[0]);
1638 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1639}
1640
1641 buf_T *
1642buflist_find_by_name(char_u *name, int curtab_only)
1643{
1644 int save_magic;
1645 char_u *save_cpo;
1646 buf_T *buf;
1647
1648 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1649 save_magic = p_magic;
1650 p_magic = TRUE;
1651 save_cpo = p_cpo;
1652 p_cpo = (char_u *)"";
1653
1654 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1655 TRUE, FALSE, curtab_only));
1656
1657 p_magic = save_magic;
1658 p_cpo = save_cpo;
1659 return buf;
1660}
1661
1662/*
1663 * Get buffer by number or pattern.
1664 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001665 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001666get_buf_tv(typval_T *tv, int curtab_only)
1667{
1668 char_u *name = tv->vval.v_string;
1669 buf_T *buf;
1670
1671 if (tv->v_type == VAR_NUMBER)
1672 return buflist_findnr((int)tv->vval.v_number);
1673 if (tv->v_type != VAR_STRING)
1674 return NULL;
1675 if (name == NULL || *name == NUL)
1676 return curbuf;
1677 if (name[0] == '$' && name[1] == NUL)
1678 return lastbuf;
1679
1680 buf = buflist_find_by_name(name, curtab_only);
1681
1682 /* If not found, try expanding the name, like done for bufexists(). */
1683 if (buf == NULL)
1684 buf = find_buffer(tv);
1685
1686 return buf;
1687}
1688
1689/*
1690 * "bufname(expr)" function
1691 */
1692 static void
1693f_bufname(typval_T *argvars, typval_T *rettv)
1694{
1695 buf_T *buf;
1696
1697 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1698 ++emsg_off;
1699 buf = get_buf_tv(&argvars[0], FALSE);
1700 rettv->v_type = VAR_STRING;
1701 if (buf != NULL && buf->b_fname != NULL)
1702 rettv->vval.v_string = vim_strsave(buf->b_fname);
1703 else
1704 rettv->vval.v_string = NULL;
1705 --emsg_off;
1706}
1707
1708/*
1709 * "bufnr(expr)" function
1710 */
1711 static void
1712f_bufnr(typval_T *argvars, typval_T *rettv)
1713{
1714 buf_T *buf;
1715 int error = FALSE;
1716 char_u *name;
1717
1718 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1719 ++emsg_off;
1720 buf = get_buf_tv(&argvars[0], FALSE);
1721 --emsg_off;
1722
1723 /* If the buffer isn't found and the second argument is not zero create a
1724 * new buffer. */
1725 if (buf == NULL
1726 && argvars[1].v_type != VAR_UNKNOWN
1727 && get_tv_number_chk(&argvars[1], &error) != 0
1728 && !error
1729 && (name = get_tv_string_chk(&argvars[0])) != NULL
1730 && !error)
1731 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1732
1733 if (buf != NULL)
1734 rettv->vval.v_number = buf->b_fnum;
1735 else
1736 rettv->vval.v_number = -1;
1737}
1738
1739 static void
1740buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1741{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001742 win_T *wp;
1743 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001744 buf_T *buf;
1745
1746 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1747 ++emsg_off;
1748 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001749 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001750 {
1751 ++winnr;
1752 if (wp->w_buffer == buf)
1753 break;
1754 }
1755 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001756 --emsg_off;
1757}
1758
1759/*
1760 * "bufwinid(nr)" function
1761 */
1762 static void
1763f_bufwinid(typval_T *argvars, typval_T *rettv)
1764{
1765 buf_win_common(argvars, rettv, FALSE);
1766}
1767
1768/*
1769 * "bufwinnr(nr)" function
1770 */
1771 static void
1772f_bufwinnr(typval_T *argvars, typval_T *rettv)
1773{
1774 buf_win_common(argvars, rettv, TRUE);
1775}
1776
1777/*
1778 * "byte2line(byte)" function
1779 */
1780 static void
1781f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1782{
1783#ifndef FEAT_BYTEOFF
1784 rettv->vval.v_number = -1;
1785#else
1786 long boff = 0;
1787
1788 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1789 if (boff < 0)
1790 rettv->vval.v_number = -1;
1791 else
1792 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1793 (linenr_T)0, &boff);
1794#endif
1795}
1796
1797 static void
1798byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1799{
1800#ifdef FEAT_MBYTE
1801 char_u *t;
1802#endif
1803 char_u *str;
1804 varnumber_T idx;
1805
1806 str = get_tv_string_chk(&argvars[0]);
1807 idx = get_tv_number_chk(&argvars[1], NULL);
1808 rettv->vval.v_number = -1;
1809 if (str == NULL || idx < 0)
1810 return;
1811
1812#ifdef FEAT_MBYTE
1813 t = str;
1814 for ( ; idx > 0; idx--)
1815 {
1816 if (*t == NUL) /* EOL reached */
1817 return;
1818 if (enc_utf8 && comp)
1819 t += utf_ptr2len(t);
1820 else
1821 t += (*mb_ptr2len)(t);
1822 }
1823 rettv->vval.v_number = (varnumber_T)(t - str);
1824#else
1825 if ((size_t)idx <= STRLEN(str))
1826 rettv->vval.v_number = idx;
1827#endif
1828}
1829
1830/*
1831 * "byteidx()" function
1832 */
1833 static void
1834f_byteidx(typval_T *argvars, typval_T *rettv)
1835{
1836 byteidx(argvars, rettv, FALSE);
1837}
1838
1839/*
1840 * "byteidxcomp()" function
1841 */
1842 static void
1843f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1844{
1845 byteidx(argvars, rettv, TRUE);
1846}
1847
1848/*
1849 * "call(func, arglist [, dict])" function
1850 */
1851 static void
1852f_call(typval_T *argvars, typval_T *rettv)
1853{
1854 char_u *func;
1855 partial_T *partial = NULL;
1856 dict_T *selfdict = NULL;
1857
1858 if (argvars[1].v_type != VAR_LIST)
1859 {
1860 EMSG(_(e_listreq));
1861 return;
1862 }
1863 if (argvars[1].vval.v_list == NULL)
1864 return;
1865
1866 if (argvars[0].v_type == VAR_FUNC)
1867 func = argvars[0].vval.v_string;
1868 else if (argvars[0].v_type == VAR_PARTIAL)
1869 {
1870 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001871 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001872 }
1873 else
1874 func = get_tv_string(&argvars[0]);
1875 if (*func == NUL)
1876 return; /* type error or empty name */
1877
1878 if (argvars[2].v_type != VAR_UNKNOWN)
1879 {
1880 if (argvars[2].v_type != VAR_DICT)
1881 {
1882 EMSG(_(e_dictreq));
1883 return;
1884 }
1885 selfdict = argvars[2].vval.v_dict;
1886 }
1887
1888 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1889}
1890
1891#ifdef FEAT_FLOAT
1892/*
1893 * "ceil({float})" function
1894 */
1895 static void
1896f_ceil(typval_T *argvars, typval_T *rettv)
1897{
1898 float_T f = 0.0;
1899
1900 rettv->v_type = VAR_FLOAT;
1901 if (get_float_arg(argvars, &f) == OK)
1902 rettv->vval.v_float = ceil(f);
1903 else
1904 rettv->vval.v_float = 0.0;
1905}
1906#endif
1907
1908#ifdef FEAT_JOB_CHANNEL
1909/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001910 * "ch_canread()" function
1911 */
1912 static void
1913f_ch_canread(typval_T *argvars, typval_T *rettv)
1914{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001915 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001916
1917 rettv->vval.v_number = 0;
1918 if (channel != NULL)
1919 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1920 || channel_has_readahead(channel, PART_OUT)
1921 || channel_has_readahead(channel, PART_ERR);
1922}
1923
1924/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001925 * "ch_close()" function
1926 */
1927 static void
1928f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1929{
1930 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1931
1932 if (channel != NULL)
1933 {
1934 channel_close(channel, FALSE);
1935 channel_clear(channel);
1936 }
1937}
1938
1939/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001940 * "ch_close()" function
1941 */
1942 static void
1943f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1944{
1945 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1946
1947 if (channel != NULL)
1948 channel_close_in(channel);
1949}
1950
1951/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001952 * "ch_getbufnr()" function
1953 */
1954 static void
1955f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1956{
1957 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1958
1959 rettv->vval.v_number = -1;
1960 if (channel != NULL)
1961 {
1962 char_u *what = get_tv_string(&argvars[1]);
1963 int part;
1964
1965 if (STRCMP(what, "err") == 0)
1966 part = PART_ERR;
1967 else if (STRCMP(what, "out") == 0)
1968 part = PART_OUT;
1969 else if (STRCMP(what, "in") == 0)
1970 part = PART_IN;
1971 else
1972 part = PART_SOCK;
1973 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1974 rettv->vval.v_number =
1975 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1976 }
1977}
1978
1979/*
1980 * "ch_getjob()" function
1981 */
1982 static void
1983f_ch_getjob(typval_T *argvars, typval_T *rettv)
1984{
1985 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1986
1987 if (channel != NULL)
1988 {
1989 rettv->v_type = VAR_JOB;
1990 rettv->vval.v_job = channel->ch_job;
1991 if (channel->ch_job != NULL)
1992 ++channel->ch_job->jv_refcount;
1993 }
1994}
1995
1996/*
1997 * "ch_info()" function
1998 */
1999 static void
2000f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2001{
2002 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2003
2004 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2005 channel_info(channel, rettv->vval.v_dict);
2006}
2007
2008/*
2009 * "ch_log()" function
2010 */
2011 static void
2012f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2013{
2014 char_u *msg = get_tv_string(&argvars[0]);
2015 channel_T *channel = NULL;
2016
2017 if (argvars[1].v_type != VAR_UNKNOWN)
2018 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2019
Bram Moolenaard5359b22018-04-05 22:44:39 +02002020 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002021}
2022
2023/*
2024 * "ch_logfile()" function
2025 */
2026 static void
2027f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2028{
2029 char_u *fname;
2030 char_u *opt = (char_u *)"";
2031 char_u buf[NUMBUFLEN];
2032
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002033 /* Don't open a file in restricted mode. */
2034 if (check_restricted() || check_secure())
2035 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002036 fname = get_tv_string(&argvars[0]);
2037 if (argvars[1].v_type == VAR_STRING)
2038 opt = get_tv_string_buf(&argvars[1], buf);
2039 ch_logfile(fname, opt);
2040}
2041
2042/*
2043 * "ch_open()" function
2044 */
2045 static void
2046f_ch_open(typval_T *argvars, typval_T *rettv)
2047{
2048 rettv->v_type = VAR_CHANNEL;
2049 if (check_restricted() || check_secure())
2050 return;
2051 rettv->vval.v_channel = channel_open_func(argvars);
2052}
2053
2054/*
2055 * "ch_read()" function
2056 */
2057 static void
2058f_ch_read(typval_T *argvars, typval_T *rettv)
2059{
2060 common_channel_read(argvars, rettv, FALSE);
2061}
2062
2063/*
2064 * "ch_readraw()" function
2065 */
2066 static void
2067f_ch_readraw(typval_T *argvars, typval_T *rettv)
2068{
2069 common_channel_read(argvars, rettv, TRUE);
2070}
2071
2072/*
2073 * "ch_evalexpr()" function
2074 */
2075 static void
2076f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2077{
2078 ch_expr_common(argvars, rettv, TRUE);
2079}
2080
2081/*
2082 * "ch_sendexpr()" function
2083 */
2084 static void
2085f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2086{
2087 ch_expr_common(argvars, rettv, FALSE);
2088}
2089
2090/*
2091 * "ch_evalraw()" function
2092 */
2093 static void
2094f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2095{
2096 ch_raw_common(argvars, rettv, TRUE);
2097}
2098
2099/*
2100 * "ch_sendraw()" function
2101 */
2102 static void
2103f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2104{
2105 ch_raw_common(argvars, rettv, FALSE);
2106}
2107
2108/*
2109 * "ch_setoptions()" function
2110 */
2111 static void
2112f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2113{
2114 channel_T *channel;
2115 jobopt_T opt;
2116
2117 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2118 if (channel == NULL)
2119 return;
2120 clear_job_options(&opt);
2121 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002122 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002123 channel_set_options(channel, &opt);
2124 free_job_options(&opt);
2125}
2126
2127/*
2128 * "ch_status()" function
2129 */
2130 static void
2131f_ch_status(typval_T *argvars, typval_T *rettv)
2132{
2133 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002134 jobopt_T opt;
2135 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002136
2137 /* return an empty string by default */
2138 rettv->v_type = VAR_STRING;
2139 rettv->vval.v_string = NULL;
2140
2141 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002142
2143 if (argvars[1].v_type != VAR_UNKNOWN)
2144 {
2145 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002146 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002147 && (opt.jo_set & JO_PART))
2148 part = opt.jo_part;
2149 }
2150
2151 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002152}
2153#endif
2154
2155/*
2156 * "changenr()" function
2157 */
2158 static void
2159f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2160{
2161 rettv->vval.v_number = curbuf->b_u_seq_cur;
2162}
2163
2164/*
2165 * "char2nr(string)" function
2166 */
2167 static void
2168f_char2nr(typval_T *argvars, typval_T *rettv)
2169{
2170#ifdef FEAT_MBYTE
2171 if (has_mbyte)
2172 {
2173 int utf8 = 0;
2174
2175 if (argvars[1].v_type != VAR_UNKNOWN)
2176 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2177
2178 if (utf8)
2179 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2180 else
2181 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2182 }
2183 else
2184#endif
2185 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2186}
2187
2188/*
2189 * "cindent(lnum)" function
2190 */
2191 static void
2192f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2193{
2194#ifdef FEAT_CINDENT
2195 pos_T pos;
2196 linenr_T lnum;
2197
2198 pos = curwin->w_cursor;
2199 lnum = get_tv_lnum(argvars);
2200 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2201 {
2202 curwin->w_cursor.lnum = lnum;
2203 rettv->vval.v_number = get_c_indent();
2204 curwin->w_cursor = pos;
2205 }
2206 else
2207#endif
2208 rettv->vval.v_number = -1;
2209}
2210
2211/*
2212 * "clearmatches()" function
2213 */
2214 static void
2215f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2216{
2217#ifdef FEAT_SEARCH_EXTRA
2218 clear_matches(curwin);
2219#endif
2220}
2221
2222/*
2223 * "col(string)" function
2224 */
2225 static void
2226f_col(typval_T *argvars, typval_T *rettv)
2227{
2228 colnr_T col = 0;
2229 pos_T *fp;
2230 int fnum = curbuf->b_fnum;
2231
2232 fp = var2fpos(&argvars[0], FALSE, &fnum);
2233 if (fp != NULL && fnum == curbuf->b_fnum)
2234 {
2235 if (fp->col == MAXCOL)
2236 {
2237 /* '> can be MAXCOL, get the length of the line then */
2238 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2239 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2240 else
2241 col = MAXCOL;
2242 }
2243 else
2244 {
2245 col = fp->col + 1;
2246#ifdef FEAT_VIRTUALEDIT
2247 /* col(".") when the cursor is on the NUL at the end of the line
2248 * because of "coladd" can be seen as an extra column. */
2249 if (virtual_active() && fp == &curwin->w_cursor)
2250 {
2251 char_u *p = ml_get_cursor();
2252
2253 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2254 curwin->w_virtcol - curwin->w_cursor.coladd))
2255 {
2256# ifdef FEAT_MBYTE
2257 int l;
2258
2259 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2260 col += l;
2261# else
2262 if (*p != NUL && p[1] == NUL)
2263 ++col;
2264# endif
2265 }
2266 }
2267#endif
2268 }
2269 }
2270 rettv->vval.v_number = col;
2271}
2272
2273#if defined(FEAT_INS_EXPAND)
2274/*
2275 * "complete()" function
2276 */
2277 static void
2278f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2279{
2280 int startcol;
2281
2282 if ((State & INSERT) == 0)
2283 {
2284 EMSG(_("E785: complete() can only be used in Insert mode"));
2285 return;
2286 }
2287
2288 /* Check for undo allowed here, because if something was already inserted
2289 * the line was already saved for undo and this check isn't done. */
2290 if (!undo_allowed())
2291 return;
2292
2293 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2294 {
2295 EMSG(_(e_invarg));
2296 return;
2297 }
2298
2299 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2300 if (startcol <= 0)
2301 return;
2302
2303 set_completion(startcol - 1, argvars[1].vval.v_list);
2304}
2305
2306/*
2307 * "complete_add()" function
2308 */
2309 static void
2310f_complete_add(typval_T *argvars, typval_T *rettv)
2311{
2312 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2313}
2314
2315/*
2316 * "complete_check()" function
2317 */
2318 static void
2319f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2320{
2321 int saved = RedrawingDisabled;
2322
2323 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002324 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002325 rettv->vval.v_number = compl_interrupted;
2326 RedrawingDisabled = saved;
2327}
2328#endif
2329
2330/*
2331 * "confirm(message, buttons[, default [, type]])" function
2332 */
2333 static void
2334f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2335{
2336#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2337 char_u *message;
2338 char_u *buttons = NULL;
2339 char_u buf[NUMBUFLEN];
2340 char_u buf2[NUMBUFLEN];
2341 int def = 1;
2342 int type = VIM_GENERIC;
2343 char_u *typestr;
2344 int error = FALSE;
2345
2346 message = get_tv_string_chk(&argvars[0]);
2347 if (message == NULL)
2348 error = TRUE;
2349 if (argvars[1].v_type != VAR_UNKNOWN)
2350 {
2351 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2352 if (buttons == NULL)
2353 error = TRUE;
2354 if (argvars[2].v_type != VAR_UNKNOWN)
2355 {
2356 def = (int)get_tv_number_chk(&argvars[2], &error);
2357 if (argvars[3].v_type != VAR_UNKNOWN)
2358 {
2359 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2360 if (typestr == NULL)
2361 error = TRUE;
2362 else
2363 {
2364 switch (TOUPPER_ASC(*typestr))
2365 {
2366 case 'E': type = VIM_ERROR; break;
2367 case 'Q': type = VIM_QUESTION; break;
2368 case 'I': type = VIM_INFO; break;
2369 case 'W': type = VIM_WARNING; break;
2370 case 'G': type = VIM_GENERIC; break;
2371 }
2372 }
2373 }
2374 }
2375 }
2376
2377 if (buttons == NULL || *buttons == NUL)
2378 buttons = (char_u *)_("&Ok");
2379
2380 if (!error)
2381 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2382 def, NULL, FALSE);
2383#endif
2384}
2385
2386/*
2387 * "copy()" function
2388 */
2389 static void
2390f_copy(typval_T *argvars, typval_T *rettv)
2391{
2392 item_copy(&argvars[0], rettv, FALSE, 0);
2393}
2394
2395#ifdef FEAT_FLOAT
2396/*
2397 * "cos()" function
2398 */
2399 static void
2400f_cos(typval_T *argvars, typval_T *rettv)
2401{
2402 float_T f = 0.0;
2403
2404 rettv->v_type = VAR_FLOAT;
2405 if (get_float_arg(argvars, &f) == OK)
2406 rettv->vval.v_float = cos(f);
2407 else
2408 rettv->vval.v_float = 0.0;
2409}
2410
2411/*
2412 * "cosh()" function
2413 */
2414 static void
2415f_cosh(typval_T *argvars, typval_T *rettv)
2416{
2417 float_T f = 0.0;
2418
2419 rettv->v_type = VAR_FLOAT;
2420 if (get_float_arg(argvars, &f) == OK)
2421 rettv->vval.v_float = cosh(f);
2422 else
2423 rettv->vval.v_float = 0.0;
2424}
2425#endif
2426
2427/*
2428 * "count()" function
2429 */
2430 static void
2431f_count(typval_T *argvars, typval_T *rettv)
2432{
2433 long n = 0;
2434 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002435 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002436
Bram Moolenaar9966b212017-07-28 16:46:57 +02002437 if (argvars[2].v_type != VAR_UNKNOWN)
2438 ic = (int)get_tv_number_chk(&argvars[2], &error);
2439
2440 if (argvars[0].v_type == VAR_STRING)
2441 {
2442 char_u *expr = get_tv_string_chk(&argvars[1]);
2443 char_u *p = argvars[0].vval.v_string;
2444 char_u *next;
2445
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002446 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002447 {
2448 if (ic)
2449 {
2450 size_t len = STRLEN(expr);
2451
2452 while (*p != NUL)
2453 {
2454 if (MB_STRNICMP(p, expr, len) == 0)
2455 {
2456 ++n;
2457 p += len;
2458 }
2459 else
2460 MB_PTR_ADV(p);
2461 }
2462 }
2463 else
2464 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2465 != NULL)
2466 {
2467 ++n;
2468 p = next + STRLEN(expr);
2469 }
2470 }
2471
2472 }
2473 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002474 {
2475 listitem_T *li;
2476 list_T *l;
2477 long idx;
2478
2479 if ((l = argvars[0].vval.v_list) != NULL)
2480 {
2481 li = l->lv_first;
2482 if (argvars[2].v_type != VAR_UNKNOWN)
2483 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002484 if (argvars[3].v_type != VAR_UNKNOWN)
2485 {
2486 idx = (long)get_tv_number_chk(&argvars[3], &error);
2487 if (!error)
2488 {
2489 li = list_find(l, idx);
2490 if (li == NULL)
2491 EMSGN(_(e_listidx), idx);
2492 }
2493 }
2494 if (error)
2495 li = NULL;
2496 }
2497
2498 for ( ; li != NULL; li = li->li_next)
2499 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2500 ++n;
2501 }
2502 }
2503 else if (argvars[0].v_type == VAR_DICT)
2504 {
2505 int todo;
2506 dict_T *d;
2507 hashitem_T *hi;
2508
2509 if ((d = argvars[0].vval.v_dict) != NULL)
2510 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002511 if (argvars[2].v_type != VAR_UNKNOWN)
2512 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002513 if (argvars[3].v_type != VAR_UNKNOWN)
2514 EMSG(_(e_invarg));
2515 }
2516
2517 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2518 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2519 {
2520 if (!HASHITEM_EMPTY(hi))
2521 {
2522 --todo;
2523 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2524 ++n;
2525 }
2526 }
2527 }
2528 }
2529 else
2530 EMSG2(_(e_listdictarg), "count()");
2531 rettv->vval.v_number = n;
2532}
2533
2534/*
2535 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2536 *
2537 * Checks the existence of a cscope connection.
2538 */
2539 static void
2540f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2541{
2542#ifdef FEAT_CSCOPE
2543 int num = 0;
2544 char_u *dbpath = NULL;
2545 char_u *prepend = NULL;
2546 char_u buf[NUMBUFLEN];
2547
2548 if (argvars[0].v_type != VAR_UNKNOWN
2549 && argvars[1].v_type != VAR_UNKNOWN)
2550 {
2551 num = (int)get_tv_number(&argvars[0]);
2552 dbpath = get_tv_string(&argvars[1]);
2553 if (argvars[2].v_type != VAR_UNKNOWN)
2554 prepend = get_tv_string_buf(&argvars[2], buf);
2555 }
2556
2557 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2558#endif
2559}
2560
2561/*
2562 * "cursor(lnum, col)" function, or
2563 * "cursor(list)"
2564 *
2565 * Moves the cursor to the specified line and column.
2566 * Returns 0 when the position could be set, -1 otherwise.
2567 */
2568 static void
2569f_cursor(typval_T *argvars, typval_T *rettv)
2570{
2571 long line, col;
2572#ifdef FEAT_VIRTUALEDIT
2573 long coladd = 0;
2574#endif
2575 int set_curswant = TRUE;
2576
2577 rettv->vval.v_number = -1;
2578 if (argvars[1].v_type == VAR_UNKNOWN)
2579 {
2580 pos_T pos;
2581 colnr_T curswant = -1;
2582
2583 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2584 {
2585 EMSG(_(e_invarg));
2586 return;
2587 }
2588 line = pos.lnum;
2589 col = pos.col;
2590#ifdef FEAT_VIRTUALEDIT
2591 coladd = pos.coladd;
2592#endif
2593 if (curswant >= 0)
2594 {
2595 curwin->w_curswant = curswant - 1;
2596 set_curswant = FALSE;
2597 }
2598 }
2599 else
2600 {
2601 line = get_tv_lnum(argvars);
2602 col = (long)get_tv_number_chk(&argvars[1], NULL);
2603#ifdef FEAT_VIRTUALEDIT
2604 if (argvars[2].v_type != VAR_UNKNOWN)
2605 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2606#endif
2607 }
2608 if (line < 0 || col < 0
2609#ifdef FEAT_VIRTUALEDIT
2610 || coladd < 0
2611#endif
2612 )
2613 return; /* type error; errmsg already given */
2614 if (line > 0)
2615 curwin->w_cursor.lnum = line;
2616 if (col > 0)
2617 curwin->w_cursor.col = col - 1;
2618#ifdef FEAT_VIRTUALEDIT
2619 curwin->w_cursor.coladd = coladd;
2620#endif
2621
2622 /* Make sure the cursor is in a valid position. */
2623 check_cursor();
2624#ifdef FEAT_MBYTE
2625 /* Correct cursor for multi-byte character. */
2626 if (has_mbyte)
2627 mb_adjust_cursor();
2628#endif
2629
2630 curwin->w_set_curswant = set_curswant;
2631 rettv->vval.v_number = 0;
2632}
2633
2634/*
2635 * "deepcopy()" function
2636 */
2637 static void
2638f_deepcopy(typval_T *argvars, typval_T *rettv)
2639{
2640 int noref = 0;
2641 int copyID;
2642
2643 if (argvars[1].v_type != VAR_UNKNOWN)
2644 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2645 if (noref < 0 || noref > 1)
2646 EMSG(_(e_invarg));
2647 else
2648 {
2649 copyID = get_copyID();
2650 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2651 }
2652}
2653
2654/*
2655 * "delete()" function
2656 */
2657 static void
2658f_delete(typval_T *argvars, typval_T *rettv)
2659{
2660 char_u nbuf[NUMBUFLEN];
2661 char_u *name;
2662 char_u *flags;
2663
2664 rettv->vval.v_number = -1;
2665 if (check_restricted() || check_secure())
2666 return;
2667
2668 name = get_tv_string(&argvars[0]);
2669 if (name == NULL || *name == NUL)
2670 {
2671 EMSG(_(e_invarg));
2672 return;
2673 }
2674
2675 if (argvars[1].v_type != VAR_UNKNOWN)
2676 flags = get_tv_string_buf(&argvars[1], nbuf);
2677 else
2678 flags = (char_u *)"";
2679
2680 if (*flags == NUL)
2681 /* delete a file */
2682 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2683 else if (STRCMP(flags, "d") == 0)
2684 /* delete an empty directory */
2685 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2686 else if (STRCMP(flags, "rf") == 0)
2687 /* delete a directory recursively */
2688 rettv->vval.v_number = delete_recursive(name);
2689 else
2690 EMSG2(_(e_invexpr2), flags);
2691}
2692
2693/*
2694 * "did_filetype()" function
2695 */
2696 static void
2697f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2698{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002699 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002700}
2701
2702/*
2703 * "diff_filler()" function
2704 */
2705 static void
2706f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2707{
2708#ifdef FEAT_DIFF
2709 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2710#endif
2711}
2712
2713/*
2714 * "diff_hlID()" function
2715 */
2716 static void
2717f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2718{
2719#ifdef FEAT_DIFF
2720 linenr_T lnum = get_tv_lnum(argvars);
2721 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002722 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002723 static int fnum = 0;
2724 static int change_start = 0;
2725 static int change_end = 0;
2726 static hlf_T hlID = (hlf_T)0;
2727 int filler_lines;
2728 int col;
2729
2730 if (lnum < 0) /* ignore type error in {lnum} arg */
2731 lnum = 0;
2732 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002733 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002734 || fnum != curbuf->b_fnum)
2735 {
2736 /* New line, buffer, change: need to get the values. */
2737 filler_lines = diff_check(curwin, lnum);
2738 if (filler_lines < 0)
2739 {
2740 if (filler_lines == -1)
2741 {
2742 change_start = MAXCOL;
2743 change_end = -1;
2744 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2745 hlID = HLF_ADD; /* added line */
2746 else
2747 hlID = HLF_CHD; /* changed line */
2748 }
2749 else
2750 hlID = HLF_ADD; /* added line */
2751 }
2752 else
2753 hlID = (hlf_T)0;
2754 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002755 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002756 fnum = curbuf->b_fnum;
2757 }
2758
2759 if (hlID == HLF_CHD || hlID == HLF_TXD)
2760 {
2761 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2762 if (col >= change_start && col <= change_end)
2763 hlID = HLF_TXD; /* changed text */
2764 else
2765 hlID = HLF_CHD; /* changed line */
2766 }
2767 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2768#endif
2769}
2770
2771/*
2772 * "empty({expr})" function
2773 */
2774 static void
2775f_empty(typval_T *argvars, typval_T *rettv)
2776{
2777 int n = FALSE;
2778
2779 switch (argvars[0].v_type)
2780 {
2781 case VAR_STRING:
2782 case VAR_FUNC:
2783 n = argvars[0].vval.v_string == NULL
2784 || *argvars[0].vval.v_string == NUL;
2785 break;
2786 case VAR_PARTIAL:
2787 n = FALSE;
2788 break;
2789 case VAR_NUMBER:
2790 n = argvars[0].vval.v_number == 0;
2791 break;
2792 case VAR_FLOAT:
2793#ifdef FEAT_FLOAT
2794 n = argvars[0].vval.v_float == 0.0;
2795 break;
2796#endif
2797 case VAR_LIST:
2798 n = argvars[0].vval.v_list == NULL
2799 || argvars[0].vval.v_list->lv_first == NULL;
2800 break;
2801 case VAR_DICT:
2802 n = argvars[0].vval.v_dict == NULL
2803 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2804 break;
2805 case VAR_SPECIAL:
2806 n = argvars[0].vval.v_number != VVAL_TRUE;
2807 break;
2808
2809 case VAR_JOB:
2810#ifdef FEAT_JOB_CHANNEL
2811 n = argvars[0].vval.v_job == NULL
2812 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2813 break;
2814#endif
2815 case VAR_CHANNEL:
2816#ifdef FEAT_JOB_CHANNEL
2817 n = argvars[0].vval.v_channel == NULL
2818 || !channel_is_open(argvars[0].vval.v_channel);
2819 break;
2820#endif
2821 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002822 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002823 n = TRUE;
2824 break;
2825 }
2826
2827 rettv->vval.v_number = n;
2828}
2829
2830/*
2831 * "escape({string}, {chars})" function
2832 */
2833 static void
2834f_escape(typval_T *argvars, typval_T *rettv)
2835{
2836 char_u buf[NUMBUFLEN];
2837
2838 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2839 get_tv_string_buf(&argvars[1], buf));
2840 rettv->v_type = VAR_STRING;
2841}
2842
2843/*
2844 * "eval()" function
2845 */
2846 static void
2847f_eval(typval_T *argvars, typval_T *rettv)
2848{
2849 char_u *s, *p;
2850
2851 s = get_tv_string_chk(&argvars[0]);
2852 if (s != NULL)
2853 s = skipwhite(s);
2854
2855 p = s;
2856 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2857 {
2858 if (p != NULL && !aborting())
2859 EMSG2(_(e_invexpr2), p);
2860 need_clr_eos = FALSE;
2861 rettv->v_type = VAR_NUMBER;
2862 rettv->vval.v_number = 0;
2863 }
2864 else if (*s != NUL)
2865 EMSG(_(e_trailing));
2866}
2867
2868/*
2869 * "eventhandler()" function
2870 */
2871 static void
2872f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2873{
2874 rettv->vval.v_number = vgetc_busy;
2875}
2876
2877/*
2878 * "executable()" function
2879 */
2880 static void
2881f_executable(typval_T *argvars, typval_T *rettv)
2882{
2883 char_u *name = get_tv_string(&argvars[0]);
2884
2885 /* Check in $PATH and also check directly if there is a directory name. */
2886 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2887 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2888}
2889
2890static garray_T redir_execute_ga;
2891
2892/*
2893 * Append "value[value_len]" to the execute() output.
2894 */
2895 void
2896execute_redir_str(char_u *value, int value_len)
2897{
2898 int len;
2899
2900 if (value_len == -1)
2901 len = (int)STRLEN(value); /* Append the entire string */
2902 else
2903 len = value_len; /* Append only "value_len" characters */
2904 if (ga_grow(&redir_execute_ga, len) == OK)
2905 {
2906 mch_memmove((char *)redir_execute_ga.ga_data
2907 + redir_execute_ga.ga_len, value, len);
2908 redir_execute_ga.ga_len += len;
2909 }
2910}
2911
2912/*
2913 * Get next line from a list.
2914 * Called by do_cmdline() to get the next line.
2915 * Returns allocated string, or NULL for end of function.
2916 */
2917
2918 static char_u *
2919get_list_line(
2920 int c UNUSED,
2921 void *cookie,
2922 int indent UNUSED)
2923{
2924 listitem_T **p = (listitem_T **)cookie;
2925 listitem_T *item = *p;
2926 char_u buf[NUMBUFLEN];
2927 char_u *s;
2928
2929 if (item == NULL)
2930 return NULL;
2931 s = get_tv_string_buf_chk(&item->li_tv, buf);
2932 *p = item->li_next;
2933 return s == NULL ? NULL : vim_strsave(s);
2934}
2935
2936/*
2937 * "execute()" function
2938 */
2939 static void
2940f_execute(typval_T *argvars, typval_T *rettv)
2941{
2942 char_u *cmd = NULL;
2943 list_T *list = NULL;
2944 int save_msg_silent = msg_silent;
2945 int save_emsg_silent = emsg_silent;
2946 int save_emsg_noredir = emsg_noredir;
2947 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002948 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002949 garray_T save_ga;
2950
2951 rettv->vval.v_string = NULL;
2952 rettv->v_type = VAR_STRING;
2953
2954 if (argvars[0].v_type == VAR_LIST)
2955 {
2956 list = argvars[0].vval.v_list;
2957 if (list == NULL || list->lv_first == NULL)
2958 /* empty list, no commands, empty output */
2959 return;
2960 ++list->lv_refcount;
2961 }
2962 else
2963 {
2964 cmd = get_tv_string_chk(&argvars[0]);
2965 if (cmd == NULL)
2966 return;
2967 }
2968
2969 if (argvars[1].v_type != VAR_UNKNOWN)
2970 {
2971 char_u buf[NUMBUFLEN];
2972 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2973
2974 if (s == NULL)
2975 return;
2976 if (STRNCMP(s, "silent", 6) == 0)
2977 ++msg_silent;
2978 if (STRCMP(s, "silent!") == 0)
2979 {
2980 emsg_silent = TRUE;
2981 emsg_noredir = TRUE;
2982 }
2983 }
2984 else
2985 ++msg_silent;
2986
2987 if (redir_execute)
2988 save_ga = redir_execute_ga;
2989 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2990 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002991 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002992
2993 if (cmd != NULL)
2994 do_cmdline_cmd(cmd);
2995 else
2996 {
2997 listitem_T *item = list->lv_first;
2998
2999 do_cmdline(NULL, get_list_line, (void *)&item,
3000 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3001 --list->lv_refcount;
3002 }
3003
Bram Moolenaard297f352017-01-29 20:31:21 +01003004 /* Need to append a NUL to the result. */
3005 if (ga_grow(&redir_execute_ga, 1) == OK)
3006 {
3007 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3008 rettv->vval.v_string = redir_execute_ga.ga_data;
3009 }
3010 else
3011 {
3012 ga_clear(&redir_execute_ga);
3013 rettv->vval.v_string = NULL;
3014 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003015 msg_silent = save_msg_silent;
3016 emsg_silent = save_emsg_silent;
3017 emsg_noredir = save_emsg_noredir;
3018
3019 redir_execute = save_redir_execute;
3020 if (redir_execute)
3021 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003022 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003023
3024 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
3025 * line. Put it back in the first column. */
3026 msg_col = 0;
3027}
3028
3029/*
3030 * "exepath()" function
3031 */
3032 static void
3033f_exepath(typval_T *argvars, typval_T *rettv)
3034{
3035 char_u *p = NULL;
3036
3037 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3038 rettv->v_type = VAR_STRING;
3039 rettv->vval.v_string = p;
3040}
3041
3042/*
3043 * "exists()" function
3044 */
3045 static void
3046f_exists(typval_T *argvars, typval_T *rettv)
3047{
3048 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003049 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003050
3051 p = get_tv_string(&argvars[0]);
3052 if (*p == '$') /* environment variable */
3053 {
3054 /* first try "normal" environment variables (fast) */
3055 if (mch_getenv(p + 1) != NULL)
3056 n = TRUE;
3057 else
3058 {
3059 /* try expanding things like $VIM and ${HOME} */
3060 p = expand_env_save(p);
3061 if (p != NULL && *p != '$')
3062 n = TRUE;
3063 vim_free(p);
3064 }
3065 }
3066 else if (*p == '&' || *p == '+') /* option */
3067 {
3068 n = (get_option_tv(&p, NULL, TRUE) == OK);
3069 if (*skipwhite(p) != NUL)
3070 n = FALSE; /* trailing garbage */
3071 }
3072 else if (*p == '*') /* internal or user defined function */
3073 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003074 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003075 }
3076 else if (*p == ':')
3077 {
3078 n = cmd_exists(p + 1);
3079 }
3080 else if (*p == '#')
3081 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003082 if (p[1] == '#')
3083 n = autocmd_supported(p + 2);
3084 else
3085 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003086 }
3087 else /* internal variable */
3088 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003089 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003090 }
3091
3092 rettv->vval.v_number = n;
3093}
3094
3095#ifdef FEAT_FLOAT
3096/*
3097 * "exp()" function
3098 */
3099 static void
3100f_exp(typval_T *argvars, typval_T *rettv)
3101{
3102 float_T f = 0.0;
3103
3104 rettv->v_type = VAR_FLOAT;
3105 if (get_float_arg(argvars, &f) == OK)
3106 rettv->vval.v_float = exp(f);
3107 else
3108 rettv->vval.v_float = 0.0;
3109}
3110#endif
3111
3112/*
3113 * "expand()" function
3114 */
3115 static void
3116f_expand(typval_T *argvars, typval_T *rettv)
3117{
3118 char_u *s;
3119 int len;
3120 char_u *errormsg;
3121 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3122 expand_T xpc;
3123 int error = FALSE;
3124 char_u *result;
3125
3126 rettv->v_type = VAR_STRING;
3127 if (argvars[1].v_type != VAR_UNKNOWN
3128 && argvars[2].v_type != VAR_UNKNOWN
3129 && get_tv_number_chk(&argvars[2], &error)
3130 && !error)
3131 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003132 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003133 }
3134
3135 s = get_tv_string(&argvars[0]);
3136 if (*s == '%' || *s == '#' || *s == '<')
3137 {
3138 ++emsg_off;
3139 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3140 --emsg_off;
3141 if (rettv->v_type == VAR_LIST)
3142 {
3143 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3144 list_append_string(rettv->vval.v_list, result, -1);
3145 else
3146 vim_free(result);
3147 }
3148 else
3149 rettv->vval.v_string = result;
3150 }
3151 else
3152 {
3153 /* When the optional second argument is non-zero, don't remove matches
3154 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3155 if (argvars[1].v_type != VAR_UNKNOWN
3156 && get_tv_number_chk(&argvars[1], &error))
3157 options |= WILD_KEEP_ALL;
3158 if (!error)
3159 {
3160 ExpandInit(&xpc);
3161 xpc.xp_context = EXPAND_FILES;
3162 if (p_wic)
3163 options += WILD_ICASE;
3164 if (rettv->v_type == VAR_STRING)
3165 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3166 options, WILD_ALL);
3167 else if (rettv_list_alloc(rettv) != FAIL)
3168 {
3169 int i;
3170
3171 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3172 for (i = 0; i < xpc.xp_numfiles; i++)
3173 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3174 ExpandCleanup(&xpc);
3175 }
3176 }
3177 else
3178 rettv->vval.v_string = NULL;
3179 }
3180}
3181
3182/*
3183 * "extend(list, list [, idx])" function
3184 * "extend(dict, dict [, action])" function
3185 */
3186 static void
3187f_extend(typval_T *argvars, typval_T *rettv)
3188{
3189 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3190
3191 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3192 {
3193 list_T *l1, *l2;
3194 listitem_T *item;
3195 long before;
3196 int error = FALSE;
3197
3198 l1 = argvars[0].vval.v_list;
3199 l2 = argvars[1].vval.v_list;
3200 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3201 && l2 != NULL)
3202 {
3203 if (argvars[2].v_type != VAR_UNKNOWN)
3204 {
3205 before = (long)get_tv_number_chk(&argvars[2], &error);
3206 if (error)
3207 return; /* type error; errmsg already given */
3208
3209 if (before == l1->lv_len)
3210 item = NULL;
3211 else
3212 {
3213 item = list_find(l1, before);
3214 if (item == NULL)
3215 {
3216 EMSGN(_(e_listidx), before);
3217 return;
3218 }
3219 }
3220 }
3221 else
3222 item = NULL;
3223 list_extend(l1, l2, item);
3224
3225 copy_tv(&argvars[0], rettv);
3226 }
3227 }
3228 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3229 {
3230 dict_T *d1, *d2;
3231 char_u *action;
3232 int i;
3233
3234 d1 = argvars[0].vval.v_dict;
3235 d2 = argvars[1].vval.v_dict;
3236 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3237 && d2 != NULL)
3238 {
3239 /* Check the third argument. */
3240 if (argvars[2].v_type != VAR_UNKNOWN)
3241 {
3242 static char *(av[]) = {"keep", "force", "error"};
3243
3244 action = get_tv_string_chk(&argvars[2]);
3245 if (action == NULL)
3246 return; /* type error; errmsg already given */
3247 for (i = 0; i < 3; ++i)
3248 if (STRCMP(action, av[i]) == 0)
3249 break;
3250 if (i == 3)
3251 {
3252 EMSG2(_(e_invarg2), action);
3253 return;
3254 }
3255 }
3256 else
3257 action = (char_u *)"force";
3258
3259 dict_extend(d1, d2, action);
3260
3261 copy_tv(&argvars[0], rettv);
3262 }
3263 }
3264 else
3265 EMSG2(_(e_listdictarg), "extend()");
3266}
3267
3268/*
3269 * "feedkeys()" function
3270 */
3271 static void
3272f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3273{
3274 int remap = TRUE;
3275 int insert = FALSE;
3276 char_u *keys, *flags;
3277 char_u nbuf[NUMBUFLEN];
3278 int typed = FALSE;
3279 int execute = FALSE;
3280 int dangerous = FALSE;
3281 char_u *keys_esc;
3282
3283 /* This is not allowed in the sandbox. If the commands would still be
3284 * executed in the sandbox it would be OK, but it probably happens later,
3285 * when "sandbox" is no longer set. */
3286 if (check_secure())
3287 return;
3288
3289 keys = get_tv_string(&argvars[0]);
3290
3291 if (argvars[1].v_type != VAR_UNKNOWN)
3292 {
3293 flags = get_tv_string_buf(&argvars[1], nbuf);
3294 for ( ; *flags != NUL; ++flags)
3295 {
3296 switch (*flags)
3297 {
3298 case 'n': remap = FALSE; break;
3299 case 'm': remap = TRUE; break;
3300 case 't': typed = TRUE; break;
3301 case 'i': insert = TRUE; break;
3302 case 'x': execute = TRUE; break;
3303 case '!': dangerous = TRUE; break;
3304 }
3305 }
3306 }
3307
3308 if (*keys != NUL || execute)
3309 {
3310 /* Need to escape K_SPECIAL and CSI before putting the string in the
3311 * typeahead buffer. */
3312 keys_esc = vim_strsave_escape_csi(keys);
3313 if (keys_esc != NULL)
3314 {
3315 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3316 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3317 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003318 if (vgetc_busy
3319#ifdef FEAT_TIMERS
3320 || timer_busy
3321#endif
3322 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003323 typebuf_was_filled = TRUE;
3324 if (execute)
3325 {
3326 int save_msg_scroll = msg_scroll;
3327
3328 /* Avoid a 1 second delay when the keys start Insert mode. */
3329 msg_scroll = FALSE;
3330
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003331 if (!dangerous)
3332 ++ex_normal_busy;
Bram Moolenaar655a82a2018-05-08 22:01:07 +02003333 exec_normal(TRUE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003334 if (!dangerous)
3335 --ex_normal_busy;
3336
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003337 msg_scroll |= save_msg_scroll;
3338 }
3339 }
3340 }
3341}
3342
3343/*
3344 * "filereadable()" function
3345 */
3346 static void
3347f_filereadable(typval_T *argvars, typval_T *rettv)
3348{
3349 int fd;
3350 char_u *p;
3351 int n;
3352
3353#ifndef O_NONBLOCK
3354# define O_NONBLOCK 0
3355#endif
3356 p = get_tv_string(&argvars[0]);
3357 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3358 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3359 {
3360 n = TRUE;
3361 close(fd);
3362 }
3363 else
3364 n = FALSE;
3365
3366 rettv->vval.v_number = n;
3367}
3368
3369/*
3370 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3371 * rights to write into.
3372 */
3373 static void
3374f_filewritable(typval_T *argvars, typval_T *rettv)
3375{
3376 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3377}
3378
3379 static void
3380findfilendir(
3381 typval_T *argvars UNUSED,
3382 typval_T *rettv,
3383 int find_what UNUSED)
3384{
3385#ifdef FEAT_SEARCHPATH
3386 char_u *fname;
3387 char_u *fresult = NULL;
3388 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3389 char_u *p;
3390 char_u pathbuf[NUMBUFLEN];
3391 int count = 1;
3392 int first = TRUE;
3393 int error = FALSE;
3394#endif
3395
3396 rettv->vval.v_string = NULL;
3397 rettv->v_type = VAR_STRING;
3398
3399#ifdef FEAT_SEARCHPATH
3400 fname = get_tv_string(&argvars[0]);
3401
3402 if (argvars[1].v_type != VAR_UNKNOWN)
3403 {
3404 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3405 if (p == NULL)
3406 error = TRUE;
3407 else
3408 {
3409 if (*p != NUL)
3410 path = p;
3411
3412 if (argvars[2].v_type != VAR_UNKNOWN)
3413 count = (int)get_tv_number_chk(&argvars[2], &error);
3414 }
3415 }
3416
3417 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3418 error = TRUE;
3419
3420 if (*fname != NUL && !error)
3421 {
3422 do
3423 {
3424 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3425 vim_free(fresult);
3426 fresult = find_file_in_path_option(first ? fname : NULL,
3427 first ? (int)STRLEN(fname) : 0,
3428 0, first, path,
3429 find_what,
3430 curbuf->b_ffname,
3431 find_what == FINDFILE_DIR
3432 ? (char_u *)"" : curbuf->b_p_sua);
3433 first = FALSE;
3434
3435 if (fresult != NULL && rettv->v_type == VAR_LIST)
3436 list_append_string(rettv->vval.v_list, fresult, -1);
3437
3438 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3439 }
3440
3441 if (rettv->v_type == VAR_STRING)
3442 rettv->vval.v_string = fresult;
3443#endif
3444}
3445
3446/*
3447 * "filter()" function
3448 */
3449 static void
3450f_filter(typval_T *argvars, typval_T *rettv)
3451{
3452 filter_map(argvars, rettv, FALSE);
3453}
3454
3455/*
3456 * "finddir({fname}[, {path}[, {count}]])" function
3457 */
3458 static void
3459f_finddir(typval_T *argvars, typval_T *rettv)
3460{
3461 findfilendir(argvars, rettv, FINDFILE_DIR);
3462}
3463
3464/*
3465 * "findfile({fname}[, {path}[, {count}]])" function
3466 */
3467 static void
3468f_findfile(typval_T *argvars, typval_T *rettv)
3469{
3470 findfilendir(argvars, rettv, FINDFILE_FILE);
3471}
3472
3473#ifdef FEAT_FLOAT
3474/*
3475 * "float2nr({float})" function
3476 */
3477 static void
3478f_float2nr(typval_T *argvars, typval_T *rettv)
3479{
3480 float_T f = 0.0;
3481
3482 if (get_float_arg(argvars, &f) == OK)
3483 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003484 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003485 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003486 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003487 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003488 else
3489 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003490 }
3491}
3492
3493/*
3494 * "floor({float})" function
3495 */
3496 static void
3497f_floor(typval_T *argvars, typval_T *rettv)
3498{
3499 float_T f = 0.0;
3500
3501 rettv->v_type = VAR_FLOAT;
3502 if (get_float_arg(argvars, &f) == OK)
3503 rettv->vval.v_float = floor(f);
3504 else
3505 rettv->vval.v_float = 0.0;
3506}
3507
3508/*
3509 * "fmod()" function
3510 */
3511 static void
3512f_fmod(typval_T *argvars, typval_T *rettv)
3513{
3514 float_T fx = 0.0, fy = 0.0;
3515
3516 rettv->v_type = VAR_FLOAT;
3517 if (get_float_arg(argvars, &fx) == OK
3518 && get_float_arg(&argvars[1], &fy) == OK)
3519 rettv->vval.v_float = fmod(fx, fy);
3520 else
3521 rettv->vval.v_float = 0.0;
3522}
3523#endif
3524
3525/*
3526 * "fnameescape({string})" function
3527 */
3528 static void
3529f_fnameescape(typval_T *argvars, typval_T *rettv)
3530{
3531 rettv->vval.v_string = vim_strsave_fnameescape(
3532 get_tv_string(&argvars[0]), FALSE);
3533 rettv->v_type = VAR_STRING;
3534}
3535
3536/*
3537 * "fnamemodify({fname}, {mods})" function
3538 */
3539 static void
3540f_fnamemodify(typval_T *argvars, typval_T *rettv)
3541{
3542 char_u *fname;
3543 char_u *mods;
3544 int usedlen = 0;
3545 int len;
3546 char_u *fbuf = NULL;
3547 char_u buf[NUMBUFLEN];
3548
3549 fname = get_tv_string_chk(&argvars[0]);
3550 mods = get_tv_string_buf_chk(&argvars[1], buf);
3551 if (fname == NULL || mods == NULL)
3552 fname = NULL;
3553 else
3554 {
3555 len = (int)STRLEN(fname);
3556 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3557 }
3558
3559 rettv->v_type = VAR_STRING;
3560 if (fname == NULL)
3561 rettv->vval.v_string = NULL;
3562 else
3563 rettv->vval.v_string = vim_strnsave(fname, len);
3564 vim_free(fbuf);
3565}
3566
3567static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3568
3569/*
3570 * "foldclosed()" function
3571 */
3572 static void
3573foldclosed_both(
3574 typval_T *argvars UNUSED,
3575 typval_T *rettv,
3576 int end UNUSED)
3577{
3578#ifdef FEAT_FOLDING
3579 linenr_T lnum;
3580 linenr_T first, last;
3581
3582 lnum = get_tv_lnum(argvars);
3583 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3584 {
3585 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3586 {
3587 if (end)
3588 rettv->vval.v_number = (varnumber_T)last;
3589 else
3590 rettv->vval.v_number = (varnumber_T)first;
3591 return;
3592 }
3593 }
3594#endif
3595 rettv->vval.v_number = -1;
3596}
3597
3598/*
3599 * "foldclosed()" function
3600 */
3601 static void
3602f_foldclosed(typval_T *argvars, typval_T *rettv)
3603{
3604 foldclosed_both(argvars, rettv, FALSE);
3605}
3606
3607/*
3608 * "foldclosedend()" function
3609 */
3610 static void
3611f_foldclosedend(typval_T *argvars, typval_T *rettv)
3612{
3613 foldclosed_both(argvars, rettv, TRUE);
3614}
3615
3616/*
3617 * "foldlevel()" function
3618 */
3619 static void
3620f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3621{
3622#ifdef FEAT_FOLDING
3623 linenr_T lnum;
3624
3625 lnum = get_tv_lnum(argvars);
3626 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3627 rettv->vval.v_number = foldLevel(lnum);
3628#endif
3629}
3630
3631/*
3632 * "foldtext()" function
3633 */
3634 static void
3635f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3636{
3637#ifdef FEAT_FOLDING
3638 linenr_T foldstart;
3639 linenr_T foldend;
3640 char_u *dashes;
3641 linenr_T lnum;
3642 char_u *s;
3643 char_u *r;
3644 int len;
3645 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003646 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003647#endif
3648
3649 rettv->v_type = VAR_STRING;
3650 rettv->vval.v_string = NULL;
3651#ifdef FEAT_FOLDING
3652 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3653 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3654 dashes = get_vim_var_str(VV_FOLDDASHES);
3655 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3656 && dashes != NULL)
3657 {
3658 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003659 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003660 if (!linewhite(lnum))
3661 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003662
3663 /* Find interesting text in this line. */
3664 s = skipwhite(ml_get(lnum));
3665 /* skip C comment-start */
3666 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3667 {
3668 s = skipwhite(s + 2);
3669 if (*skipwhite(s) == NUL
3670 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3671 {
3672 s = skipwhite(ml_get(lnum + 1));
3673 if (*s == '*')
3674 s = skipwhite(s + 1);
3675 }
3676 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003677 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003678 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003679 r = alloc((unsigned)(STRLEN(txt)
3680 + STRLEN(dashes) /* for %s */
3681 + 20 /* for %3ld */
3682 + STRLEN(s))); /* concatenated */
3683 if (r != NULL)
3684 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003685 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003686 len = (int)STRLEN(r);
3687 STRCAT(r, s);
3688 /* remove 'foldmarker' and 'commentstring' */
3689 foldtext_cleanup(r + len);
3690 rettv->vval.v_string = r;
3691 }
3692 }
3693#endif
3694}
3695
3696/*
3697 * "foldtextresult(lnum)" function
3698 */
3699 static void
3700f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3701{
3702#ifdef FEAT_FOLDING
3703 linenr_T lnum;
3704 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003705 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003706 foldinfo_T foldinfo;
3707 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003708 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003709#endif
3710
3711 rettv->v_type = VAR_STRING;
3712 rettv->vval.v_string = NULL;
3713#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003714 if (entered)
3715 return; /* reject recursive use */
3716 entered = TRUE;
3717
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003718 lnum = get_tv_lnum(argvars);
3719 /* treat illegal types and illegal string values for {lnum} the same */
3720 if (lnum < 0)
3721 lnum = 0;
3722 fold_count = foldedCount(curwin, lnum, &foldinfo);
3723 if (fold_count > 0)
3724 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003725 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3726 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003727 if (text == buf)
3728 text = vim_strsave(text);
3729 rettv->vval.v_string = text;
3730 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003731
3732 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003733#endif
3734}
3735
3736/*
3737 * "foreground()" function
3738 */
3739 static void
3740f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3741{
3742#ifdef FEAT_GUI
3743 if (gui.in_use)
3744 gui_mch_set_foreground();
3745#else
3746# ifdef WIN32
3747 win32_set_foreground();
3748# endif
3749#endif
3750}
3751
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003752 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003753common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003754{
3755 char_u *s;
3756 char_u *name;
3757 int use_string = FALSE;
3758 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003759 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003760
3761 if (argvars[0].v_type == VAR_FUNC)
3762 {
3763 /* function(MyFunc, [arg], dict) */
3764 s = argvars[0].vval.v_string;
3765 }
3766 else if (argvars[0].v_type == VAR_PARTIAL
3767 && argvars[0].vval.v_partial != NULL)
3768 {
3769 /* function(dict.MyFunc, [arg]) */
3770 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003771 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003772 }
3773 else
3774 {
3775 /* function('MyFunc', [arg], dict) */
3776 s = get_tv_string(&argvars[0]);
3777 use_string = TRUE;
3778 }
3779
Bram Moolenaar843b8842016-08-21 14:36:15 +02003780 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003781 {
3782 name = s;
3783 trans_name = trans_function_name(&name, FALSE,
3784 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3785 if (*name != NUL)
3786 s = NULL;
3787 }
3788
Bram Moolenaar843b8842016-08-21 14:36:15 +02003789 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3790 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003791 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003792 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003793 else if (trans_name != NULL && (is_funcref
3794 ? find_func(trans_name) == NULL
3795 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003796 EMSG2(_("E700: Unknown function: %s"), s);
3797 else
3798 {
3799 int dict_idx = 0;
3800 int arg_idx = 0;
3801 list_T *list = NULL;
3802
3803 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3804 {
3805 char sid_buf[25];
3806 int off = *s == 's' ? 2 : 5;
3807
3808 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3809 * also be called from another script. Using trans_function_name()
3810 * would also work, but some plugins depend on the name being
3811 * printable text. */
3812 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3813 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3814 if (name != NULL)
3815 {
3816 STRCPY(name, sid_buf);
3817 STRCAT(name, s + off);
3818 }
3819 }
3820 else
3821 name = vim_strsave(s);
3822
3823 if (argvars[1].v_type != VAR_UNKNOWN)
3824 {
3825 if (argvars[2].v_type != VAR_UNKNOWN)
3826 {
3827 /* function(name, [args], dict) */
3828 arg_idx = 1;
3829 dict_idx = 2;
3830 }
3831 else if (argvars[1].v_type == VAR_DICT)
3832 /* function(name, dict) */
3833 dict_idx = 1;
3834 else
3835 /* function(name, [args]) */
3836 arg_idx = 1;
3837 if (dict_idx > 0)
3838 {
3839 if (argvars[dict_idx].v_type != VAR_DICT)
3840 {
3841 EMSG(_("E922: expected a dict"));
3842 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003843 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003844 }
3845 if (argvars[dict_idx].vval.v_dict == NULL)
3846 dict_idx = 0;
3847 }
3848 if (arg_idx > 0)
3849 {
3850 if (argvars[arg_idx].v_type != VAR_LIST)
3851 {
3852 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3853 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003854 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003855 }
3856 list = argvars[arg_idx].vval.v_list;
3857 if (list == NULL || list->lv_len == 0)
3858 arg_idx = 0;
3859 }
3860 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003861 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003862 {
3863 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3864
3865 /* result is a VAR_PARTIAL */
3866 if (pt == NULL)
3867 vim_free(name);
3868 else
3869 {
3870 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3871 {
3872 listitem_T *li;
3873 int i = 0;
3874 int arg_len = 0;
3875 int lv_len = 0;
3876
3877 if (arg_pt != NULL)
3878 arg_len = arg_pt->pt_argc;
3879 if (list != NULL)
3880 lv_len = list->lv_len;
3881 pt->pt_argc = arg_len + lv_len;
3882 pt->pt_argv = (typval_T *)alloc(
3883 sizeof(typval_T) * pt->pt_argc);
3884 if (pt->pt_argv == NULL)
3885 {
3886 vim_free(pt);
3887 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003888 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003889 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003890 for (i = 0; i < arg_len; i++)
3891 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3892 if (lv_len > 0)
3893 for (li = list->lv_first; li != NULL;
3894 li = li->li_next)
3895 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003896 }
3897
3898 /* For "function(dict.func, [], dict)" and "func" is a partial
3899 * use "dict". That is backwards compatible. */
3900 if (dict_idx > 0)
3901 {
3902 /* The dict is bound explicitly, pt_auto is FALSE. */
3903 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3904 ++pt->pt_dict->dv_refcount;
3905 }
3906 else if (arg_pt != NULL)
3907 {
3908 /* If the dict was bound automatically the result is also
3909 * bound automatically. */
3910 pt->pt_dict = arg_pt->pt_dict;
3911 pt->pt_auto = arg_pt->pt_auto;
3912 if (pt->pt_dict != NULL)
3913 ++pt->pt_dict->dv_refcount;
3914 }
3915
3916 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003917 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3918 {
3919 pt->pt_func = arg_pt->pt_func;
3920 func_ptr_ref(pt->pt_func);
3921 vim_free(name);
3922 }
3923 else if (is_funcref)
3924 {
3925 pt->pt_func = find_func(trans_name);
3926 func_ptr_ref(pt->pt_func);
3927 vim_free(name);
3928 }
3929 else
3930 {
3931 pt->pt_name = name;
3932 func_ref(name);
3933 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003934 }
3935 rettv->v_type = VAR_PARTIAL;
3936 rettv->vval.v_partial = pt;
3937 }
3938 else
3939 {
3940 /* result is a VAR_FUNC */
3941 rettv->v_type = VAR_FUNC;
3942 rettv->vval.v_string = name;
3943 func_ref(name);
3944 }
3945 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003946theend:
3947 vim_free(trans_name);
3948}
3949
3950/*
3951 * "funcref()" function
3952 */
3953 static void
3954f_funcref(typval_T *argvars, typval_T *rettv)
3955{
3956 common_function(argvars, rettv, TRUE);
3957}
3958
3959/*
3960 * "function()" function
3961 */
3962 static void
3963f_function(typval_T *argvars, typval_T *rettv)
3964{
3965 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003966}
3967
3968/*
3969 * "garbagecollect()" function
3970 */
3971 static void
3972f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3973{
3974 /* This is postponed until we are back at the toplevel, because we may be
3975 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3976 want_garbage_collect = TRUE;
3977
3978 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3979 garbage_collect_at_exit = TRUE;
3980}
3981
3982/*
3983 * "get()" function
3984 */
3985 static void
3986f_get(typval_T *argvars, typval_T *rettv)
3987{
3988 listitem_T *li;
3989 list_T *l;
3990 dictitem_T *di;
3991 dict_T *d;
3992 typval_T *tv = NULL;
3993
3994 if (argvars[0].v_type == VAR_LIST)
3995 {
3996 if ((l = argvars[0].vval.v_list) != NULL)
3997 {
3998 int error = FALSE;
3999
4000 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
4001 if (!error && li != NULL)
4002 tv = &li->li_tv;
4003 }
4004 }
4005 else if (argvars[0].v_type == VAR_DICT)
4006 {
4007 if ((d = argvars[0].vval.v_dict) != NULL)
4008 {
4009 di = dict_find(d, get_tv_string(&argvars[1]), -1);
4010 if (di != NULL)
4011 tv = &di->di_tv;
4012 }
4013 }
4014 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4015 {
4016 partial_T *pt;
4017 partial_T fref_pt;
4018
4019 if (argvars[0].v_type == VAR_PARTIAL)
4020 pt = argvars[0].vval.v_partial;
4021 else
4022 {
4023 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4024 fref_pt.pt_name = argvars[0].vval.v_string;
4025 pt = &fref_pt;
4026 }
4027
4028 if (pt != NULL)
4029 {
4030 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004031 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004032
4033 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4034 {
4035 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004036 n = partial_name(pt);
4037 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004038 rettv->vval.v_string = NULL;
4039 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004040 {
4041 rettv->vval.v_string = vim_strsave(n);
4042 if (rettv->v_type == VAR_FUNC)
4043 func_ref(rettv->vval.v_string);
4044 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004045 }
4046 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004047 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004048 else if (STRCMP(what, "args") == 0)
4049 {
4050 rettv->v_type = VAR_LIST;
4051 if (rettv_list_alloc(rettv) == OK)
4052 {
4053 int i;
4054
4055 for (i = 0; i < pt->pt_argc; ++i)
4056 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4057 }
4058 }
4059 else
4060 EMSG2(_(e_invarg2), what);
4061 return;
4062 }
4063 }
4064 else
4065 EMSG2(_(e_listdictarg), "get()");
4066
4067 if (tv == NULL)
4068 {
4069 if (argvars[2].v_type != VAR_UNKNOWN)
4070 copy_tv(&argvars[2], rettv);
4071 }
4072 else
4073 copy_tv(tv, rettv);
4074}
4075
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004076#ifdef FEAT_SIGNS
4077/*
4078 * Returns information about signs placed in a buffer as list of dicts.
4079 */
4080 static void
4081get_buffer_signs(buf_T *buf, list_T *l)
4082{
4083 signlist_T *sign;
4084
4085 for (sign = buf->b_signlist; sign; sign = sign->next)
4086 {
4087 dict_T *d = dict_alloc();
4088
4089 if (d != NULL)
4090 {
4091 dict_add_nr_str(d, "id", sign->id, NULL);
4092 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004093 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004094
4095 list_append_dict(l, d);
4096 }
4097 }
4098}
4099#endif
4100
4101/*
4102 * Returns buffer options, variables and other attributes in a dictionary.
4103 */
4104 static dict_T *
4105get_buffer_info(buf_T *buf)
4106{
4107 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004108 tabpage_T *tp;
4109 win_T *wp;
4110 list_T *windows;
4111
4112 dict = dict_alloc();
4113 if (dict == NULL)
4114 return NULL;
4115
Bram Moolenaar33928832016-08-18 21:22:04 +02004116 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004117 dict_add_nr_str(dict, "name", 0L,
4118 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004119 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4120 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004121 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4122 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4123 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004124 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004125 dict_add_nr_str(dict, "hidden",
4126 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4127 NULL);
4128
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004129 /* Get a reference to buffer variables */
4130 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004131
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004132 /* List of windows displaying this buffer */
4133 windows = list_alloc();
4134 if (windows != NULL)
4135 {
4136 FOR_ALL_TAB_WINDOWS(tp, wp)
4137 if (wp->w_buffer == buf)
4138 list_append_number(windows, (varnumber_T)wp->w_id);
4139 dict_add_list(dict, "windows", windows);
4140 }
4141
4142#ifdef FEAT_SIGNS
4143 if (buf->b_signlist != NULL)
4144 {
4145 /* List of signs placed in this buffer */
4146 list_T *signs = list_alloc();
4147 if (signs != NULL)
4148 {
4149 get_buffer_signs(buf, signs);
4150 dict_add_list(dict, "signs", signs);
4151 }
4152 }
4153#endif
4154
4155 return dict;
4156}
4157
4158/*
4159 * "getbufinfo()" function
4160 */
4161 static void
4162f_getbufinfo(typval_T *argvars, typval_T *rettv)
4163{
4164 buf_T *buf = NULL;
4165 buf_T *argbuf = NULL;
4166 dict_T *d;
4167 int filtered = FALSE;
4168 int sel_buflisted = FALSE;
4169 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004170 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004171
4172 if (rettv_list_alloc(rettv) != OK)
4173 return;
4174
4175 /* List of all the buffers or selected buffers */
4176 if (argvars[0].v_type == VAR_DICT)
4177 {
4178 dict_T *sel_d = argvars[0].vval.v_dict;
4179
4180 if (sel_d != NULL)
4181 {
4182 dictitem_T *di;
4183
4184 filtered = TRUE;
4185
4186 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4187 if (di != NULL && get_tv_number(&di->di_tv))
4188 sel_buflisted = TRUE;
4189
4190 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4191 if (di != NULL && get_tv_number(&di->di_tv))
4192 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004193
4194 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4195 if (di != NULL && get_tv_number(&di->di_tv))
4196 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004197 }
4198 }
4199 else if (argvars[0].v_type != VAR_UNKNOWN)
4200 {
4201 /* Information about one buffer. Argument specifies the buffer */
4202 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4203 ++emsg_off;
4204 argbuf = get_buf_tv(&argvars[0], FALSE);
4205 --emsg_off;
4206 if (argbuf == NULL)
4207 return;
4208 }
4209
4210 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004211 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004212 {
4213 if (argbuf != NULL && argbuf != buf)
4214 continue;
4215 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004216 || (sel_buflisted && !buf->b_p_bl)
4217 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004218 continue;
4219
4220 d = get_buffer_info(buf);
4221 if (d != NULL)
4222 list_append_dict(rettv->vval.v_list, d);
4223 if (argbuf != NULL)
4224 return;
4225 }
4226}
4227
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004228static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4229
4230/*
4231 * Get line or list of lines from buffer "buf" into "rettv".
4232 * Return a range (from start to end) of lines in rettv from the specified
4233 * buffer.
4234 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4235 */
4236 static void
4237get_buffer_lines(
4238 buf_T *buf,
4239 linenr_T start,
4240 linenr_T end,
4241 int retlist,
4242 typval_T *rettv)
4243{
4244 char_u *p;
4245
4246 rettv->v_type = VAR_STRING;
4247 rettv->vval.v_string = NULL;
4248 if (retlist && rettv_list_alloc(rettv) == FAIL)
4249 return;
4250
4251 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4252 return;
4253
4254 if (!retlist)
4255 {
4256 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4257 p = ml_get_buf(buf, start, FALSE);
4258 else
4259 p = (char_u *)"";
4260 rettv->vval.v_string = vim_strsave(p);
4261 }
4262 else
4263 {
4264 if (end < start)
4265 return;
4266
4267 if (start < 1)
4268 start = 1;
4269 if (end > buf->b_ml.ml_line_count)
4270 end = buf->b_ml.ml_line_count;
4271 while (start <= end)
4272 if (list_append_string(rettv->vval.v_list,
4273 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4274 break;
4275 }
4276}
4277
4278/*
4279 * Get the lnum from the first argument.
4280 * Also accepts "$", then "buf" is used.
4281 * Returns 0 on error.
4282 */
4283 static linenr_T
4284get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4285{
4286 if (argvars[0].v_type == VAR_STRING
4287 && argvars[0].vval.v_string != NULL
4288 && argvars[0].vval.v_string[0] == '$'
4289 && buf != NULL)
4290 return buf->b_ml.ml_line_count;
4291 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4292}
4293
4294/*
4295 * "getbufline()" function
4296 */
4297 static void
4298f_getbufline(typval_T *argvars, typval_T *rettv)
4299{
4300 linenr_T lnum;
4301 linenr_T end;
4302 buf_T *buf;
4303
4304 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4305 ++emsg_off;
4306 buf = get_buf_tv(&argvars[0], FALSE);
4307 --emsg_off;
4308
4309 lnum = get_tv_lnum_buf(&argvars[1], buf);
4310 if (argvars[2].v_type == VAR_UNKNOWN)
4311 end = lnum;
4312 else
4313 end = get_tv_lnum_buf(&argvars[2], buf);
4314
4315 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4316}
4317
4318/*
4319 * "getbufvar()" function
4320 */
4321 static void
4322f_getbufvar(typval_T *argvars, typval_T *rettv)
4323{
4324 buf_T *buf;
4325 buf_T *save_curbuf;
4326 char_u *varname;
4327 dictitem_T *v;
4328 int done = FALSE;
4329
4330 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4331 varname = get_tv_string_chk(&argvars[1]);
4332 ++emsg_off;
4333 buf = get_buf_tv(&argvars[0], FALSE);
4334
4335 rettv->v_type = VAR_STRING;
4336 rettv->vval.v_string = NULL;
4337
4338 if (buf != NULL && varname != NULL)
4339 {
4340 /* set curbuf to be our buf, temporarily */
4341 save_curbuf = curbuf;
4342 curbuf = buf;
4343
Bram Moolenaar30567352016-08-27 21:25:44 +02004344 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004345 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004346 if (varname[1] == NUL)
4347 {
4348 /* get all buffer-local options in a dict */
4349 dict_T *opts = get_winbuf_options(TRUE);
4350
4351 if (opts != NULL)
4352 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004353 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004354 done = TRUE;
4355 }
4356 }
4357 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4358 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004359 done = TRUE;
4360 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004361 else
4362 {
4363 /* Look up the variable. */
4364 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4365 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4366 'b', varname, FALSE);
4367 if (v != NULL)
4368 {
4369 copy_tv(&v->di_tv, rettv);
4370 done = TRUE;
4371 }
4372 }
4373
4374 /* restore previous notion of curbuf */
4375 curbuf = save_curbuf;
4376 }
4377
4378 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4379 /* use the default value */
4380 copy_tv(&argvars[2], rettv);
4381
4382 --emsg_off;
4383}
4384
4385/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004386 * "getchangelist()" function
4387 */
4388 static void
4389f_getchangelist(typval_T *argvars, typval_T *rettv)
4390{
4391#ifdef FEAT_JUMPLIST
4392 buf_T *buf;
4393 int i;
4394 list_T *l;
4395 dict_T *d;
4396#endif
4397
4398 if (rettv_list_alloc(rettv) != OK)
4399 return;
4400
4401#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004402 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4403 ++emsg_off;
4404 buf = get_buf_tv(&argvars[0], FALSE);
4405 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004406 if (buf == NULL)
4407 return;
4408
4409 l = list_alloc();
4410 if (l == NULL)
4411 return;
4412
4413 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4414 return;
4415 /*
4416 * The current window change list index tracks only the position in the
4417 * current buffer change list. For other buffers, use the change list
4418 * length as the current index.
4419 */
4420 list_append_number(rettv->vval.v_list,
4421 (varnumber_T)((buf == curwin->w_buffer)
4422 ? curwin->w_changelistidx : buf->b_changelistlen));
4423
4424 for (i = 0; i < buf->b_changelistlen; ++i)
4425 {
4426 if (buf->b_changelist[i].lnum == 0)
4427 continue;
4428 if ((d = dict_alloc()) == NULL)
4429 return;
4430 if (list_append_dict(l, d) == FAIL)
4431 return;
4432 dict_add_nr_str(d, "lnum", (long)buf->b_changelist[i].lnum, NULL);
4433 dict_add_nr_str(d, "col", (long)buf->b_changelist[i].col, NULL);
4434# ifdef FEAT_VIRTUALEDIT
4435 dict_add_nr_str(d, "coladd", (long)buf->b_changelist[i].coladd, NULL);
4436# endif
4437 }
4438#endif
4439}
4440/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004441 * "getchar()" function
4442 */
4443 static void
4444f_getchar(typval_T *argvars, typval_T *rettv)
4445{
4446 varnumber_T n;
4447 int error = FALSE;
4448
4449 /* Position the cursor. Needed after a message that ends in a space. */
4450 windgoto(msg_row, msg_col);
4451
4452 ++no_mapping;
4453 ++allow_keys;
4454 for (;;)
4455 {
4456 if (argvars[0].v_type == VAR_UNKNOWN)
4457 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004458 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004459 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4460 /* getchar(1): only check if char avail */
4461 n = vpeekc_any();
4462 else if (error || vpeekc_any() == NUL)
4463 /* illegal argument or getchar(0) and no char avail: return zero */
4464 n = 0;
4465 else
4466 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004467 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004468
4469 if (n == K_IGNORE)
4470 continue;
4471 break;
4472 }
4473 --no_mapping;
4474 --allow_keys;
4475
4476 set_vim_var_nr(VV_MOUSE_WIN, 0);
4477 set_vim_var_nr(VV_MOUSE_WINID, 0);
4478 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4479 set_vim_var_nr(VV_MOUSE_COL, 0);
4480
4481 rettv->vval.v_number = n;
4482 if (IS_SPECIAL(n) || mod_mask != 0)
4483 {
4484 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4485 int i = 0;
4486
4487 /* Turn a special key into three bytes, plus modifier. */
4488 if (mod_mask != 0)
4489 {
4490 temp[i++] = K_SPECIAL;
4491 temp[i++] = KS_MODIFIER;
4492 temp[i++] = mod_mask;
4493 }
4494 if (IS_SPECIAL(n))
4495 {
4496 temp[i++] = K_SPECIAL;
4497 temp[i++] = K_SECOND(n);
4498 temp[i++] = K_THIRD(n);
4499 }
4500#ifdef FEAT_MBYTE
4501 else if (has_mbyte)
4502 i += (*mb_char2bytes)(n, temp + i);
4503#endif
4504 else
4505 temp[i++] = n;
4506 temp[i++] = NUL;
4507 rettv->v_type = VAR_STRING;
4508 rettv->vval.v_string = vim_strsave(temp);
4509
4510#ifdef FEAT_MOUSE
4511 if (is_mouse_key(n))
4512 {
4513 int row = mouse_row;
4514 int col = mouse_col;
4515 win_T *win;
4516 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004517 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004518 int winnr = 1;
4519
4520 if (row >= 0 && col >= 0)
4521 {
4522 /* Find the window at the mouse coordinates and compute the
4523 * text position. */
4524 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004525 if (win == NULL)
4526 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004527 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004528 for (wp = firstwin; wp != win; wp = wp->w_next)
4529 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004530 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4531 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4532 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4533 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4534 }
4535 }
4536#endif
4537 }
4538}
4539
4540/*
4541 * "getcharmod()" function
4542 */
4543 static void
4544f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4545{
4546 rettv->vval.v_number = mod_mask;
4547}
4548
4549/*
4550 * "getcharsearch()" function
4551 */
4552 static void
4553f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4554{
4555 if (rettv_dict_alloc(rettv) != FAIL)
4556 {
4557 dict_T *dict = rettv->vval.v_dict;
4558
4559 dict_add_nr_str(dict, "char", 0L, last_csearch());
4560 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4561 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4562 }
4563}
4564
4565/*
4566 * "getcmdline()" function
4567 */
4568 static void
4569f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4570{
4571 rettv->v_type = VAR_STRING;
4572 rettv->vval.v_string = get_cmdline_str();
4573}
4574
4575/*
4576 * "getcmdpos()" function
4577 */
4578 static void
4579f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4580{
4581 rettv->vval.v_number = get_cmdline_pos() + 1;
4582}
4583
4584/*
4585 * "getcmdtype()" function
4586 */
4587 static void
4588f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4589{
4590 rettv->v_type = VAR_STRING;
4591 rettv->vval.v_string = alloc(2);
4592 if (rettv->vval.v_string != NULL)
4593 {
4594 rettv->vval.v_string[0] = get_cmdline_type();
4595 rettv->vval.v_string[1] = NUL;
4596 }
4597}
4598
4599/*
4600 * "getcmdwintype()" function
4601 */
4602 static void
4603f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4604{
4605 rettv->v_type = VAR_STRING;
4606 rettv->vval.v_string = NULL;
4607#ifdef FEAT_CMDWIN
4608 rettv->vval.v_string = alloc(2);
4609 if (rettv->vval.v_string != NULL)
4610 {
4611 rettv->vval.v_string[0] = cmdwin_type;
4612 rettv->vval.v_string[1] = NUL;
4613 }
4614#endif
4615}
4616
4617#if defined(FEAT_CMDL_COMPL)
4618/*
4619 * "getcompletion()" function
4620 */
4621 static void
4622f_getcompletion(typval_T *argvars, typval_T *rettv)
4623{
4624 char_u *pat;
4625 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004626 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004627 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4628 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004629
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004630 if (argvars[2].v_type != VAR_UNKNOWN)
4631 filtered = get_tv_number_chk(&argvars[2], NULL);
4632
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004633 if (p_wic)
4634 options |= WILD_ICASE;
4635
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004636 /* For filtered results, 'wildignore' is used */
4637 if (!filtered)
4638 options |= WILD_KEEP_ALL;
4639
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004640 ExpandInit(&xpc);
4641 xpc.xp_pattern = get_tv_string(&argvars[0]);
4642 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4643 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4644 if (xpc.xp_context == EXPAND_NOTHING)
4645 {
4646 if (argvars[1].v_type == VAR_STRING)
4647 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4648 else
4649 EMSG(_(e_invarg));
4650 return;
4651 }
4652
4653# if defined(FEAT_MENU)
4654 if (xpc.xp_context == EXPAND_MENUS)
4655 {
4656 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4657 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4658 }
4659# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004660#ifdef FEAT_CSCOPE
4661 if (xpc.xp_context == EXPAND_CSCOPE)
4662 {
4663 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4664 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4665 }
4666#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004667#ifdef FEAT_SIGNS
4668 if (xpc.xp_context == EXPAND_SIGN)
4669 {
4670 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4671 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4672 }
4673#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004674
4675 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4676 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4677 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004678 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004679
4680 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4681
4682 for (i = 0; i < xpc.xp_numfiles; i++)
4683 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4684 }
4685 vim_free(pat);
4686 ExpandCleanup(&xpc);
4687}
4688#endif
4689
4690/*
4691 * "getcwd()" function
4692 */
4693 static void
4694f_getcwd(typval_T *argvars, typval_T *rettv)
4695{
4696 win_T *wp = NULL;
4697 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004698 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004699
4700 rettv->v_type = VAR_STRING;
4701 rettv->vval.v_string = NULL;
4702
Bram Moolenaar54591292018-02-09 20:53:59 +01004703 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4704 global = TRUE;
4705 else
4706 wp = find_tabwin(&argvars[0], &argvars[1]);
4707
4708 if (wp != NULL && wp->w_localdir != NULL)
4709 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4710 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004711 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004712 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004713 rettv->vval.v_string = vim_strsave(globaldir);
4714 else
4715 {
4716 cwd = alloc(MAXPATHL);
4717 if (cwd != NULL)
4718 {
4719 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4720 rettv->vval.v_string = vim_strsave(cwd);
4721 vim_free(cwd);
4722 }
4723 }
4724#ifdef BACKSLASH_IN_FILENAME
4725 if (rettv->vval.v_string != NULL)
4726 slash_adjust(rettv->vval.v_string);
4727#endif
4728 }
4729}
4730
4731/*
4732 * "getfontname()" function
4733 */
4734 static void
4735f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4736{
4737 rettv->v_type = VAR_STRING;
4738 rettv->vval.v_string = NULL;
4739#ifdef FEAT_GUI
4740 if (gui.in_use)
4741 {
4742 GuiFont font;
4743 char_u *name = NULL;
4744
4745 if (argvars[0].v_type == VAR_UNKNOWN)
4746 {
4747 /* Get the "Normal" font. Either the name saved by
4748 * hl_set_font_name() or from the font ID. */
4749 font = gui.norm_font;
4750 name = hl_get_font_name();
4751 }
4752 else
4753 {
4754 name = get_tv_string(&argvars[0]);
4755 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4756 return;
4757 font = gui_mch_get_font(name, FALSE);
4758 if (font == NOFONT)
4759 return; /* Invalid font name, return empty string. */
4760 }
4761 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4762 if (argvars[0].v_type != VAR_UNKNOWN)
4763 gui_mch_free_font(font);
4764 }
4765#endif
4766}
4767
4768/*
4769 * "getfperm({fname})" function
4770 */
4771 static void
4772f_getfperm(typval_T *argvars, typval_T *rettv)
4773{
4774 char_u *fname;
4775 stat_T st;
4776 char_u *perm = NULL;
4777 char_u flags[] = "rwx";
4778 int i;
4779
4780 fname = get_tv_string(&argvars[0]);
4781
4782 rettv->v_type = VAR_STRING;
4783 if (mch_stat((char *)fname, &st) >= 0)
4784 {
4785 perm = vim_strsave((char_u *)"---------");
4786 if (perm != NULL)
4787 {
4788 for (i = 0; i < 9; i++)
4789 {
4790 if (st.st_mode & (1 << (8 - i)))
4791 perm[i] = flags[i % 3];
4792 }
4793 }
4794 }
4795 rettv->vval.v_string = perm;
4796}
4797
4798/*
4799 * "getfsize({fname})" function
4800 */
4801 static void
4802f_getfsize(typval_T *argvars, typval_T *rettv)
4803{
4804 char_u *fname;
4805 stat_T st;
4806
4807 fname = get_tv_string(&argvars[0]);
4808
4809 rettv->v_type = VAR_NUMBER;
4810
4811 if (mch_stat((char *)fname, &st) >= 0)
4812 {
4813 if (mch_isdir(fname))
4814 rettv->vval.v_number = 0;
4815 else
4816 {
4817 rettv->vval.v_number = (varnumber_T)st.st_size;
4818
4819 /* non-perfect check for overflow */
4820 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4821 rettv->vval.v_number = -2;
4822 }
4823 }
4824 else
4825 rettv->vval.v_number = -1;
4826}
4827
4828/*
4829 * "getftime({fname})" function
4830 */
4831 static void
4832f_getftime(typval_T *argvars, typval_T *rettv)
4833{
4834 char_u *fname;
4835 stat_T st;
4836
4837 fname = get_tv_string(&argvars[0]);
4838
4839 if (mch_stat((char *)fname, &st) >= 0)
4840 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4841 else
4842 rettv->vval.v_number = -1;
4843}
4844
4845/*
4846 * "getftype({fname})" function
4847 */
4848 static void
4849f_getftype(typval_T *argvars, typval_T *rettv)
4850{
4851 char_u *fname;
4852 stat_T st;
4853 char_u *type = NULL;
4854 char *t;
4855
4856 fname = get_tv_string(&argvars[0]);
4857
4858 rettv->v_type = VAR_STRING;
4859 if (mch_lstat((char *)fname, &st) >= 0)
4860 {
4861#ifdef S_ISREG
4862 if (S_ISREG(st.st_mode))
4863 t = "file";
4864 else if (S_ISDIR(st.st_mode))
4865 t = "dir";
4866# ifdef S_ISLNK
4867 else if (S_ISLNK(st.st_mode))
4868 t = "link";
4869# endif
4870# ifdef S_ISBLK
4871 else if (S_ISBLK(st.st_mode))
4872 t = "bdev";
4873# endif
4874# ifdef S_ISCHR
4875 else if (S_ISCHR(st.st_mode))
4876 t = "cdev";
4877# endif
4878# ifdef S_ISFIFO
4879 else if (S_ISFIFO(st.st_mode))
4880 t = "fifo";
4881# endif
4882# ifdef S_ISSOCK
4883 else if (S_ISSOCK(st.st_mode))
4884 t = "fifo";
4885# endif
4886 else
4887 t = "other";
4888#else
4889# ifdef S_IFMT
4890 switch (st.st_mode & S_IFMT)
4891 {
4892 case S_IFREG: t = "file"; break;
4893 case S_IFDIR: t = "dir"; break;
4894# ifdef S_IFLNK
4895 case S_IFLNK: t = "link"; break;
4896# endif
4897# ifdef S_IFBLK
4898 case S_IFBLK: t = "bdev"; break;
4899# endif
4900# ifdef S_IFCHR
4901 case S_IFCHR: t = "cdev"; break;
4902# endif
4903# ifdef S_IFIFO
4904 case S_IFIFO: t = "fifo"; break;
4905# endif
4906# ifdef S_IFSOCK
4907 case S_IFSOCK: t = "socket"; break;
4908# endif
4909 default: t = "other";
4910 }
4911# else
4912 if (mch_isdir(fname))
4913 t = "dir";
4914 else
4915 t = "file";
4916# endif
4917#endif
4918 type = vim_strsave((char_u *)t);
4919 }
4920 rettv->vval.v_string = type;
4921}
4922
4923/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004924 * "getjumplist()" function
4925 */
4926 static void
4927f_getjumplist(typval_T *argvars, typval_T *rettv)
4928{
4929#ifdef FEAT_JUMPLIST
4930 win_T *wp;
4931 int i;
4932 list_T *l;
4933 dict_T *d;
4934#endif
4935
4936 if (rettv_list_alloc(rettv) != OK)
4937 return;
4938
4939#ifdef FEAT_JUMPLIST
4940 wp = find_tabwin(&argvars[0], &argvars[1]);
4941 if (wp == NULL)
4942 return;
4943
4944 l = list_alloc();
4945 if (l == NULL)
4946 return;
4947
4948 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4949 return;
4950 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4951
Bram Moolenaar48679742018-02-13 13:33:29 +01004952 cleanup_jumplist(wp, TRUE);
4953
Bram Moolenaar4f505882018-02-10 21:06:32 +01004954 for (i = 0; i < wp->w_jumplistlen; ++i)
4955 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004956 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4957 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004958 if ((d = dict_alloc()) == NULL)
4959 return;
4960 if (list_append_dict(l, d) == FAIL)
4961 return;
4962 dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
4963 NULL);
4964 dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
4965 NULL);
4966# ifdef FEAT_VIRTUALEDIT
4967 dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
4968 NULL);
4969# endif
4970 dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004971 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaar4f505882018-02-10 21:06:32 +01004972 dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
4973 }
4974#endif
4975}
4976
4977/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004978 * "getline(lnum, [end])" function
4979 */
4980 static void
4981f_getline(typval_T *argvars, typval_T *rettv)
4982{
4983 linenr_T lnum;
4984 linenr_T end;
4985 int retlist;
4986
4987 lnum = get_tv_lnum(argvars);
4988 if (argvars[1].v_type == VAR_UNKNOWN)
4989 {
4990 end = 0;
4991 retlist = FALSE;
4992 }
4993 else
4994 {
4995 end = get_tv_lnum(&argvars[1]);
4996 retlist = TRUE;
4997 }
4998
4999 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5000}
5001
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005002#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005003 static void
5004get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5005{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005006 if (what_arg->v_type == VAR_UNKNOWN)
5007 {
5008 if (rettv_list_alloc(rettv) == OK)
5009 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005010 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005011 }
5012 else
5013 {
5014 if (rettv_dict_alloc(rettv) == OK)
5015 if (is_qf || (wp != NULL))
5016 {
5017 if (what_arg->v_type == VAR_DICT)
5018 {
5019 dict_T *d = what_arg->vval.v_dict;
5020
5021 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005022 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005023 }
5024 else
5025 EMSG(_(e_dictreq));
5026 }
5027 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005028}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005029#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005030
5031/*
5032 * "getloclist()" function
5033 */
5034 static void
5035f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5036{
5037#ifdef FEAT_QUICKFIX
5038 win_T *wp;
5039
5040 wp = find_win_by_nr(&argvars[0], NULL);
5041 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5042#endif
5043}
5044
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005045/*
5046 * "getmatches()" function
5047 */
5048 static void
5049f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5050{
5051#ifdef FEAT_SEARCH_EXTRA
5052 dict_T *dict;
5053 matchitem_T *cur = curwin->w_match_head;
5054 int i;
5055
5056 if (rettv_list_alloc(rettv) == OK)
5057 {
5058 while (cur != NULL)
5059 {
5060 dict = dict_alloc();
5061 if (dict == NULL)
5062 return;
5063 if (cur->match.regprog == NULL)
5064 {
5065 /* match added with matchaddpos() */
5066 for (i = 0; i < MAXPOSMATCH; ++i)
5067 {
5068 llpos_T *llpos;
5069 char buf[6];
5070 list_T *l;
5071
5072 llpos = &cur->pos.pos[i];
5073 if (llpos->lnum == 0)
5074 break;
5075 l = list_alloc();
5076 if (l == NULL)
5077 break;
5078 list_append_number(l, (varnumber_T)llpos->lnum);
5079 if (llpos->col > 0)
5080 {
5081 list_append_number(l, (varnumber_T)llpos->col);
5082 list_append_number(l, (varnumber_T)llpos->len);
5083 }
5084 sprintf(buf, "pos%d", i + 1);
5085 dict_add_list(dict, buf, l);
5086 }
5087 }
5088 else
5089 {
5090 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
5091 }
5092 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
5093 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
5094 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
5095# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5096 if (cur->conceal_char)
5097 {
5098 char_u buf[MB_MAXBYTES + 1];
5099
5100 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5101 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5102 }
5103# endif
5104 list_append_dict(rettv->vval.v_list, dict);
5105 cur = cur->next;
5106 }
5107 }
5108#endif
5109}
5110
5111/*
5112 * "getpid()" function
5113 */
5114 static void
5115f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5116{
5117 rettv->vval.v_number = mch_get_pid();
5118}
5119
5120 static void
5121getpos_both(
5122 typval_T *argvars,
5123 typval_T *rettv,
5124 int getcurpos)
5125{
5126 pos_T *fp;
5127 list_T *l;
5128 int fnum = -1;
5129
5130 if (rettv_list_alloc(rettv) == OK)
5131 {
5132 l = rettv->vval.v_list;
5133 if (getcurpos)
5134 fp = &curwin->w_cursor;
5135 else
5136 fp = var2fpos(&argvars[0], TRUE, &fnum);
5137 if (fnum != -1)
5138 list_append_number(l, (varnumber_T)fnum);
5139 else
5140 list_append_number(l, (varnumber_T)0);
5141 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5142 : (varnumber_T)0);
5143 list_append_number(l, (fp != NULL)
5144 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5145 : (varnumber_T)0);
5146 list_append_number(l,
5147#ifdef FEAT_VIRTUALEDIT
5148 (fp != NULL) ? (varnumber_T)fp->coladd :
5149#endif
5150 (varnumber_T)0);
5151 if (getcurpos)
5152 {
5153 update_curswant();
5154 list_append_number(l, curwin->w_curswant == MAXCOL ?
5155 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5156 }
5157 }
5158 else
5159 rettv->vval.v_number = FALSE;
5160}
5161
5162
5163/*
5164 * "getcurpos()" function
5165 */
5166 static void
5167f_getcurpos(typval_T *argvars, typval_T *rettv)
5168{
5169 getpos_both(argvars, rettv, TRUE);
5170}
5171
5172/*
5173 * "getpos(string)" function
5174 */
5175 static void
5176f_getpos(typval_T *argvars, typval_T *rettv)
5177{
5178 getpos_both(argvars, rettv, FALSE);
5179}
5180
5181/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005182 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005183 */
5184 static void
5185f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5186{
5187#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005188 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005189#endif
5190}
5191
5192/*
5193 * "getreg()" function
5194 */
5195 static void
5196f_getreg(typval_T *argvars, typval_T *rettv)
5197{
5198 char_u *strregname;
5199 int regname;
5200 int arg2 = FALSE;
5201 int return_list = FALSE;
5202 int error = FALSE;
5203
5204 if (argvars[0].v_type != VAR_UNKNOWN)
5205 {
5206 strregname = get_tv_string_chk(&argvars[0]);
5207 error = strregname == NULL;
5208 if (argvars[1].v_type != VAR_UNKNOWN)
5209 {
5210 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5211 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5212 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5213 }
5214 }
5215 else
5216 strregname = get_vim_var_str(VV_REG);
5217
5218 if (error)
5219 return;
5220
5221 regname = (strregname == NULL ? '"' : *strregname);
5222 if (regname == 0)
5223 regname = '"';
5224
5225 if (return_list)
5226 {
5227 rettv->v_type = VAR_LIST;
5228 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5229 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5230 if (rettv->vval.v_list == NULL)
5231 (void)rettv_list_alloc(rettv);
5232 else
5233 ++rettv->vval.v_list->lv_refcount;
5234 }
5235 else
5236 {
5237 rettv->v_type = VAR_STRING;
5238 rettv->vval.v_string = get_reg_contents(regname,
5239 arg2 ? GREG_EXPR_SRC : 0);
5240 }
5241}
5242
5243/*
5244 * "getregtype()" function
5245 */
5246 static void
5247f_getregtype(typval_T *argvars, typval_T *rettv)
5248{
5249 char_u *strregname;
5250 int regname;
5251 char_u buf[NUMBUFLEN + 2];
5252 long reglen = 0;
5253
5254 if (argvars[0].v_type != VAR_UNKNOWN)
5255 {
5256 strregname = get_tv_string_chk(&argvars[0]);
5257 if (strregname == NULL) /* type error; errmsg already given */
5258 {
5259 rettv->v_type = VAR_STRING;
5260 rettv->vval.v_string = NULL;
5261 return;
5262 }
5263 }
5264 else
5265 /* Default to v:register */
5266 strregname = get_vim_var_str(VV_REG);
5267
5268 regname = (strregname == NULL ? '"' : *strregname);
5269 if (regname == 0)
5270 regname = '"';
5271
5272 buf[0] = NUL;
5273 buf[1] = NUL;
5274 switch (get_reg_type(regname, &reglen))
5275 {
5276 case MLINE: buf[0] = 'V'; break;
5277 case MCHAR: buf[0] = 'v'; break;
5278 case MBLOCK:
5279 buf[0] = Ctrl_V;
5280 sprintf((char *)buf + 1, "%ld", reglen + 1);
5281 break;
5282 }
5283 rettv->v_type = VAR_STRING;
5284 rettv->vval.v_string = vim_strsave(buf);
5285}
5286
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005287/*
5288 * Returns information (variables, options, etc.) about a tab page
5289 * as a dictionary.
5290 */
5291 static dict_T *
5292get_tabpage_info(tabpage_T *tp, int tp_idx)
5293{
5294 win_T *wp;
5295 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005296 list_T *l;
5297
5298 dict = dict_alloc();
5299 if (dict == NULL)
5300 return NULL;
5301
Bram Moolenaar33928832016-08-18 21:22:04 +02005302 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005303
5304 l = list_alloc();
5305 if (l != NULL)
5306 {
5307 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5308 wp; wp = wp->w_next)
5309 list_append_number(l, (varnumber_T)wp->w_id);
5310 dict_add_list(dict, "windows", l);
5311 }
5312
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005313 /* Make a reference to tabpage variables */
5314 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005315
5316 return dict;
5317}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005318
5319/*
5320 * "gettabinfo()" function
5321 */
5322 static void
5323f_gettabinfo(typval_T *argvars, typval_T *rettv)
5324{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005325 tabpage_T *tp, *tparg = NULL;
5326 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005327 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005328
5329 if (rettv_list_alloc(rettv) != OK)
5330 return;
5331
5332 if (argvars[0].v_type != VAR_UNKNOWN)
5333 {
5334 /* Information about one tab page */
5335 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5336 if (tparg == NULL)
5337 return;
5338 }
5339
5340 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005341 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005342 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005343 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005344 if (tparg != NULL && tp != tparg)
5345 continue;
5346 d = get_tabpage_info(tp, tpnr);
5347 if (d != NULL)
5348 list_append_dict(rettv->vval.v_list, d);
5349 if (tparg != NULL)
5350 return;
5351 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005352}
5353
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005354/*
5355 * "gettabvar()" function
5356 */
5357 static void
5358f_gettabvar(typval_T *argvars, typval_T *rettv)
5359{
5360 win_T *oldcurwin;
5361 tabpage_T *tp, *oldtabpage;
5362 dictitem_T *v;
5363 char_u *varname;
5364 int done = FALSE;
5365
5366 rettv->v_type = VAR_STRING;
5367 rettv->vval.v_string = NULL;
5368
5369 varname = get_tv_string_chk(&argvars[1]);
5370 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5371 if (tp != NULL && varname != NULL)
5372 {
5373 /* Set tp to be our tabpage, temporarily. Also set the window to the
5374 * first window in the tabpage, otherwise the window is not valid. */
5375 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005376 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5377 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005378 {
5379 /* look up the variable */
5380 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5381 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5382 if (v != NULL)
5383 {
5384 copy_tv(&v->di_tv, rettv);
5385 done = TRUE;
5386 }
5387 }
5388
5389 /* restore previous notion of curwin */
5390 restore_win(oldcurwin, oldtabpage, TRUE);
5391 }
5392
5393 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5394 copy_tv(&argvars[2], rettv);
5395}
5396
5397/*
5398 * "gettabwinvar()" function
5399 */
5400 static void
5401f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5402{
5403 getwinvar(argvars, rettv, 1);
5404}
5405
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005406/*
5407 * Returns information about a window as a dictionary.
5408 */
5409 static dict_T *
5410get_win_info(win_T *wp, short tpnr, short winnr)
5411{
5412 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005413
5414 dict = dict_alloc();
5415 if (dict == NULL)
5416 return NULL;
5417
Bram Moolenaar33928832016-08-18 21:22:04 +02005418 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5419 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005420 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5421 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005422#ifdef FEAT_MENU
5423 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5424#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005425 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005426 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005427
Bram Moolenaar69905d12017-08-13 18:14:47 +02005428#ifdef FEAT_TERMINAL
5429 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5430#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005431#ifdef FEAT_QUICKFIX
5432 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5433 dict_add_nr_str(dict, "loclist",
5434 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5435#endif
5436
Bram Moolenaar30567352016-08-27 21:25:44 +02005437 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005438 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005439
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005440 return dict;
5441}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005442
5443/*
5444 * "getwininfo()" function
5445 */
5446 static void
5447f_getwininfo(typval_T *argvars, typval_T *rettv)
5448{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005449 tabpage_T *tp;
5450 win_T *wp = NULL, *wparg = NULL;
5451 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005452 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005453
5454 if (rettv_list_alloc(rettv) != OK)
5455 return;
5456
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005457 if (argvars[0].v_type != VAR_UNKNOWN)
5458 {
5459 wparg = win_id2wp(argvars);
5460 if (wparg == NULL)
5461 return;
5462 }
5463
5464 /* Collect information about either all the windows across all the tab
5465 * pages or one particular window.
5466 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005467 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005468 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005469 tabnr++;
5470 winnr = 0;
5471 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005472 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005473 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005474 if (wparg != NULL && wp != wparg)
5475 continue;
5476 d = get_win_info(wp, tabnr, winnr);
5477 if (d != NULL)
5478 list_append_dict(rettv->vval.v_list, d);
5479 if (wparg != NULL)
5480 /* found information about a specific window */
5481 return;
5482 }
5483 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005484}
5485
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005486/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005487 * "win_findbuf()" function
5488 */
5489 static void
5490f_win_findbuf(typval_T *argvars, typval_T *rettv)
5491{
5492 if (rettv_list_alloc(rettv) != FAIL)
5493 win_findbuf(argvars, rettv->vval.v_list);
5494}
5495
5496/*
5497 * "win_getid()" function
5498 */
5499 static void
5500f_win_getid(typval_T *argvars, typval_T *rettv)
5501{
5502 rettv->vval.v_number = win_getid(argvars);
5503}
5504
5505/*
5506 * "win_gotoid()" function
5507 */
5508 static void
5509f_win_gotoid(typval_T *argvars, typval_T *rettv)
5510{
5511 rettv->vval.v_number = win_gotoid(argvars);
5512}
5513
5514/*
5515 * "win_id2tabwin()" function
5516 */
5517 static void
5518f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5519{
5520 if (rettv_list_alloc(rettv) != FAIL)
5521 win_id2tabwin(argvars, rettv->vval.v_list);
5522}
5523
5524/*
5525 * "win_id2win()" function
5526 */
5527 static void
5528f_win_id2win(typval_T *argvars, typval_T *rettv)
5529{
5530 rettv->vval.v_number = win_id2win(argvars);
5531}
5532
5533/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005534 * "win_screenpos()" function
5535 */
5536 static void
5537f_win_screenpos(typval_T *argvars, typval_T *rettv)
5538{
5539 win_T *wp;
5540
5541 if (rettv_list_alloc(rettv) == FAIL)
5542 return;
5543
5544 wp = find_win_by_nr(&argvars[0], NULL);
5545 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5546 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5547}
5548
5549/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005550 * "getwinpos({timeout})" function
5551 */
5552 static void
5553f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5554{
5555 int x = -1;
5556 int y = -1;
5557
5558 if (rettv_list_alloc(rettv) == FAIL)
5559 return;
5560#ifdef FEAT_GUI
5561 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005562 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005563# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5564 else
5565# endif
5566#endif
5567#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5568 {
5569 varnumber_T timeout = 100;
5570
5571 if (argvars[0].v_type != VAR_UNKNOWN)
5572 timeout = get_tv_number(&argvars[0]);
5573 term_get_winpos(&x, &y, timeout);
5574 }
5575#endif
5576 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5577 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5578}
5579
5580
5581/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005582 * "getwinposx()" function
5583 */
5584 static void
5585f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5586{
5587 rettv->vval.v_number = -1;
5588#ifdef FEAT_GUI
5589 if (gui.in_use)
5590 {
5591 int x, y;
5592
5593 if (gui_mch_get_winpos(&x, &y) == OK)
5594 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005595 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005596 }
5597#endif
5598#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5599 {
5600 int x, y;
5601
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005602 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005603 rettv->vval.v_number = x;
5604 }
5605#endif
5606}
5607
5608/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005609 * "getwinposy()" function
5610 */
5611 static void
5612f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5613{
5614 rettv->vval.v_number = -1;
5615#ifdef FEAT_GUI
5616 if (gui.in_use)
5617 {
5618 int x, y;
5619
5620 if (gui_mch_get_winpos(&x, &y) == OK)
5621 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005622 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005623 }
5624#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005625#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5626 {
5627 int x, y;
5628
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005629 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005630 rettv->vval.v_number = y;
5631 }
5632#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005633}
5634
5635/*
5636 * "getwinvar()" function
5637 */
5638 static void
5639f_getwinvar(typval_T *argvars, typval_T *rettv)
5640{
5641 getwinvar(argvars, rettv, 0);
5642}
5643
5644/*
5645 * "glob()" function
5646 */
5647 static void
5648f_glob(typval_T *argvars, typval_T *rettv)
5649{
5650 int options = WILD_SILENT|WILD_USE_NL;
5651 expand_T xpc;
5652 int error = FALSE;
5653
5654 /* When the optional second argument is non-zero, don't remove matches
5655 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5656 rettv->v_type = VAR_STRING;
5657 if (argvars[1].v_type != VAR_UNKNOWN)
5658 {
5659 if (get_tv_number_chk(&argvars[1], &error))
5660 options |= WILD_KEEP_ALL;
5661 if (argvars[2].v_type != VAR_UNKNOWN)
5662 {
5663 if (get_tv_number_chk(&argvars[2], &error))
5664 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005665 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005666 }
5667 if (argvars[3].v_type != VAR_UNKNOWN
5668 && get_tv_number_chk(&argvars[3], &error))
5669 options |= WILD_ALLLINKS;
5670 }
5671 }
5672 if (!error)
5673 {
5674 ExpandInit(&xpc);
5675 xpc.xp_context = EXPAND_FILES;
5676 if (p_wic)
5677 options += WILD_ICASE;
5678 if (rettv->v_type == VAR_STRING)
5679 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5680 NULL, options, WILD_ALL);
5681 else if (rettv_list_alloc(rettv) != FAIL)
5682 {
5683 int i;
5684
5685 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5686 NULL, options, WILD_ALL_KEEP);
5687 for (i = 0; i < xpc.xp_numfiles; i++)
5688 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5689
5690 ExpandCleanup(&xpc);
5691 }
5692 }
5693 else
5694 rettv->vval.v_string = NULL;
5695}
5696
5697/*
5698 * "globpath()" function
5699 */
5700 static void
5701f_globpath(typval_T *argvars, typval_T *rettv)
5702{
5703 int flags = 0;
5704 char_u buf1[NUMBUFLEN];
5705 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5706 int error = FALSE;
5707 garray_T ga;
5708 int i;
5709
5710 /* When the optional second argument is non-zero, don't remove matches
5711 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5712 rettv->v_type = VAR_STRING;
5713 if (argvars[2].v_type != VAR_UNKNOWN)
5714 {
5715 if (get_tv_number_chk(&argvars[2], &error))
5716 flags |= WILD_KEEP_ALL;
5717 if (argvars[3].v_type != VAR_UNKNOWN)
5718 {
5719 if (get_tv_number_chk(&argvars[3], &error))
5720 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005721 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005722 }
5723 if (argvars[4].v_type != VAR_UNKNOWN
5724 && get_tv_number_chk(&argvars[4], &error))
5725 flags |= WILD_ALLLINKS;
5726 }
5727 }
5728 if (file != NULL && !error)
5729 {
5730 ga_init2(&ga, (int)sizeof(char_u *), 10);
5731 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5732 if (rettv->v_type == VAR_STRING)
5733 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5734 else if (rettv_list_alloc(rettv) != FAIL)
5735 for (i = 0; i < ga.ga_len; ++i)
5736 list_append_string(rettv->vval.v_list,
5737 ((char_u **)(ga.ga_data))[i], -1);
5738 ga_clear_strings(&ga);
5739 }
5740 else
5741 rettv->vval.v_string = NULL;
5742}
5743
5744/*
5745 * "glob2regpat()" function
5746 */
5747 static void
5748f_glob2regpat(typval_T *argvars, typval_T *rettv)
5749{
5750 char_u *pat = get_tv_string_chk(&argvars[0]);
5751
5752 rettv->v_type = VAR_STRING;
5753 rettv->vval.v_string = (pat == NULL)
5754 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5755}
5756
5757/* for VIM_VERSION_ defines */
5758#include "version.h"
5759
5760/*
5761 * "has()" function
5762 */
5763 static void
5764f_has(typval_T *argvars, typval_T *rettv)
5765{
5766 int i;
5767 char_u *name;
5768 int n = FALSE;
5769 static char *(has_list[]) =
5770 {
5771#ifdef AMIGA
5772 "amiga",
5773# ifdef FEAT_ARP
5774 "arp",
5775# endif
5776#endif
5777#ifdef __BEOS__
5778 "beos",
5779#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005780#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005781 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5782 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005783# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005784 "macunix", /* Mac OS X, with the darwin feature */
5785 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005786# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005787#endif
5788#ifdef __QNX__
5789 "qnx",
5790#endif
5791#ifdef UNIX
5792 "unix",
5793#endif
5794#ifdef VMS
5795 "vms",
5796#endif
5797#ifdef WIN32
5798 "win32",
5799#endif
5800#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5801 "win32unix",
5802#endif
5803#if defined(WIN64) || defined(_WIN64)
5804 "win64",
5805#endif
5806#ifdef EBCDIC
5807 "ebcdic",
5808#endif
5809#ifndef CASE_INSENSITIVE_FILENAME
5810 "fname_case",
5811#endif
5812#ifdef HAVE_ACL
5813 "acl",
5814#endif
5815#ifdef FEAT_ARABIC
5816 "arabic",
5817#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005818 "autocmd",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005819#ifdef FEAT_AUTOSERVERNAME
5820 "autoservername",
5821#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005822#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005823 "balloon_eval",
5824# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5825 "balloon_multiline",
5826# endif
5827#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005828#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005829 "balloon_eval_term",
5830#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005831#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5832 "builtin_terms",
5833# ifdef ALL_BUILTIN_TCAPS
5834 "all_builtin_terms",
5835# endif
5836#endif
5837#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5838 || defined(FEAT_GUI_W32) \
5839 || defined(FEAT_GUI_MOTIF))
5840 "browsefilter",
5841#endif
5842#ifdef FEAT_BYTEOFF
5843 "byte_offset",
5844#endif
5845#ifdef FEAT_JOB_CHANNEL
5846 "channel",
5847#endif
5848#ifdef FEAT_CINDENT
5849 "cindent",
5850#endif
5851#ifdef FEAT_CLIENTSERVER
5852 "clientserver",
5853#endif
5854#ifdef FEAT_CLIPBOARD
5855 "clipboard",
5856#endif
5857#ifdef FEAT_CMDL_COMPL
5858 "cmdline_compl",
5859#endif
5860#ifdef FEAT_CMDHIST
5861 "cmdline_hist",
5862#endif
5863#ifdef FEAT_COMMENTS
5864 "comments",
5865#endif
5866#ifdef FEAT_CONCEAL
5867 "conceal",
5868#endif
5869#ifdef FEAT_CRYPT
5870 "cryptv",
5871 "crypt-blowfish",
5872 "crypt-blowfish2",
5873#endif
5874#ifdef FEAT_CSCOPE
5875 "cscope",
5876#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005877 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005878#ifdef CURSOR_SHAPE
5879 "cursorshape",
5880#endif
5881#ifdef DEBUG
5882 "debug",
5883#endif
5884#ifdef FEAT_CON_DIALOG
5885 "dialog_con",
5886#endif
5887#ifdef FEAT_GUI_DIALOG
5888 "dialog_gui",
5889#endif
5890#ifdef FEAT_DIFF
5891 "diff",
5892#endif
5893#ifdef FEAT_DIGRAPHS
5894 "digraphs",
5895#endif
5896#ifdef FEAT_DIRECTX
5897 "directx",
5898#endif
5899#ifdef FEAT_DND
5900 "dnd",
5901#endif
5902#ifdef FEAT_EMACS_TAGS
5903 "emacs_tags",
5904#endif
5905 "eval", /* always present, of course! */
5906 "ex_extra", /* graduated feature */
5907#ifdef FEAT_SEARCH_EXTRA
5908 "extra_search",
5909#endif
5910#ifdef FEAT_FKMAP
5911 "farsi",
5912#endif
5913#ifdef FEAT_SEARCHPATH
5914 "file_in_path",
5915#endif
5916#ifdef FEAT_FILTERPIPE
5917 "filterpipe",
5918#endif
5919#ifdef FEAT_FIND_ID
5920 "find_in_path",
5921#endif
5922#ifdef FEAT_FLOAT
5923 "float",
5924#endif
5925#ifdef FEAT_FOLDING
5926 "folding",
5927#endif
5928#ifdef FEAT_FOOTER
5929 "footer",
5930#endif
5931#if !defined(USE_SYSTEM) && defined(UNIX)
5932 "fork",
5933#endif
5934#ifdef FEAT_GETTEXT
5935 "gettext",
5936#endif
5937#ifdef FEAT_GUI
5938 "gui",
5939#endif
5940#ifdef FEAT_GUI_ATHENA
5941# ifdef FEAT_GUI_NEXTAW
5942 "gui_neXtaw",
5943# else
5944 "gui_athena",
5945# endif
5946#endif
5947#ifdef FEAT_GUI_GTK
5948 "gui_gtk",
5949# ifdef USE_GTK3
5950 "gui_gtk3",
5951# else
5952 "gui_gtk2",
5953# endif
5954#endif
5955#ifdef FEAT_GUI_GNOME
5956 "gui_gnome",
5957#endif
5958#ifdef FEAT_GUI_MAC
5959 "gui_mac",
5960#endif
5961#ifdef FEAT_GUI_MOTIF
5962 "gui_motif",
5963#endif
5964#ifdef FEAT_GUI_PHOTON
5965 "gui_photon",
5966#endif
5967#ifdef FEAT_GUI_W32
5968 "gui_win32",
5969#endif
5970#ifdef FEAT_HANGULIN
5971 "hangul_input",
5972#endif
5973#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5974 "iconv",
5975#endif
5976#ifdef FEAT_INS_EXPAND
5977 "insert_expand",
5978#endif
5979#ifdef FEAT_JOB_CHANNEL
5980 "job",
5981#endif
5982#ifdef FEAT_JUMPLIST
5983 "jumplist",
5984#endif
5985#ifdef FEAT_KEYMAP
5986 "keymap",
5987#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005988 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005989#ifdef FEAT_LANGMAP
5990 "langmap",
5991#endif
5992#ifdef FEAT_LIBCALL
5993 "libcall",
5994#endif
5995#ifdef FEAT_LINEBREAK
5996 "linebreak",
5997#endif
5998#ifdef FEAT_LISP
5999 "lispindent",
6000#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006001 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006002#ifdef FEAT_LOCALMAP
6003 "localmap",
6004#endif
6005#ifdef FEAT_LUA
6006# ifndef DYNAMIC_LUA
6007 "lua",
6008# endif
6009#endif
6010#ifdef FEAT_MENU
6011 "menu",
6012#endif
6013#ifdef FEAT_SESSION
6014 "mksession",
6015#endif
6016#ifdef FEAT_MODIFY_FNAME
6017 "modify_fname",
6018#endif
6019#ifdef FEAT_MOUSE
6020 "mouse",
6021#endif
6022#ifdef FEAT_MOUSESHAPE
6023 "mouseshape",
6024#endif
6025#if defined(UNIX) || defined(VMS)
6026# ifdef FEAT_MOUSE_DEC
6027 "mouse_dec",
6028# endif
6029# ifdef FEAT_MOUSE_GPM
6030 "mouse_gpm",
6031# endif
6032# ifdef FEAT_MOUSE_JSB
6033 "mouse_jsbterm",
6034# endif
6035# ifdef FEAT_MOUSE_NET
6036 "mouse_netterm",
6037# endif
6038# ifdef FEAT_MOUSE_PTERM
6039 "mouse_pterm",
6040# endif
6041# ifdef FEAT_MOUSE_SGR
6042 "mouse_sgr",
6043# endif
6044# ifdef FEAT_SYSMOUSE
6045 "mouse_sysmouse",
6046# endif
6047# ifdef FEAT_MOUSE_URXVT
6048 "mouse_urxvt",
6049# endif
6050# ifdef FEAT_MOUSE_XTERM
6051 "mouse_xterm",
6052# endif
6053#endif
6054#ifdef FEAT_MBYTE
6055 "multi_byte",
6056#endif
6057#ifdef FEAT_MBYTE_IME
6058 "multi_byte_ime",
6059#endif
6060#ifdef FEAT_MULTI_LANG
6061 "multi_lang",
6062#endif
6063#ifdef FEAT_MZSCHEME
6064#ifndef DYNAMIC_MZSCHEME
6065 "mzscheme",
6066#endif
6067#endif
6068#ifdef FEAT_NUM64
6069 "num64",
6070#endif
6071#ifdef FEAT_OLE
6072 "ole",
6073#endif
6074 "packages",
6075#ifdef FEAT_PATH_EXTRA
6076 "path_extra",
6077#endif
6078#ifdef FEAT_PERL
6079#ifndef DYNAMIC_PERL
6080 "perl",
6081#endif
6082#endif
6083#ifdef FEAT_PERSISTENT_UNDO
6084 "persistent_undo",
6085#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006086#if defined(FEAT_PYTHON)
6087 "python_compiled",
6088# if defined(DYNAMIC_PYTHON)
6089 "python_dynamic",
6090# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006091 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006092 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006093# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006094#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006095#if defined(FEAT_PYTHON3)
6096 "python3_compiled",
6097# if defined(DYNAMIC_PYTHON3)
6098 "python3_dynamic",
6099# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006100 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006101 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006102# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006103#endif
6104#ifdef FEAT_POSTSCRIPT
6105 "postscript",
6106#endif
6107#ifdef FEAT_PRINTER
6108 "printer",
6109#endif
6110#ifdef FEAT_PROFILE
6111 "profile",
6112#endif
6113#ifdef FEAT_RELTIME
6114 "reltime",
6115#endif
6116#ifdef FEAT_QUICKFIX
6117 "quickfix",
6118#endif
6119#ifdef FEAT_RIGHTLEFT
6120 "rightleft",
6121#endif
6122#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6123 "ruby",
6124#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006125 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006126#ifdef FEAT_CMDL_INFO
6127 "showcmd",
6128 "cmdline_info",
6129#endif
6130#ifdef FEAT_SIGNS
6131 "signs",
6132#endif
6133#ifdef FEAT_SMARTINDENT
6134 "smartindent",
6135#endif
6136#ifdef STARTUPTIME
6137 "startuptime",
6138#endif
6139#ifdef FEAT_STL_OPT
6140 "statusline",
6141#endif
6142#ifdef FEAT_SUN_WORKSHOP
6143 "sun_workshop",
6144#endif
6145#ifdef FEAT_NETBEANS_INTG
6146 "netbeans_intg",
6147#endif
6148#ifdef FEAT_SPELL
6149 "spell",
6150#endif
6151#ifdef FEAT_SYN_HL
6152 "syntax",
6153#endif
6154#if defined(USE_SYSTEM) || !defined(UNIX)
6155 "system",
6156#endif
6157#ifdef FEAT_TAG_BINS
6158 "tag_binary",
6159#endif
6160#ifdef FEAT_TAG_OLDSTATIC
6161 "tag_old_static",
6162#endif
6163#ifdef FEAT_TAG_ANYWHITE
6164 "tag_any_white",
6165#endif
6166#ifdef FEAT_TCL
6167# ifndef DYNAMIC_TCL
6168 "tcl",
6169# endif
6170#endif
6171#ifdef FEAT_TERMGUICOLORS
6172 "termguicolors",
6173#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006174#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006175 "terminal",
6176#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006177#ifdef TERMINFO
6178 "terminfo",
6179#endif
6180#ifdef FEAT_TERMRESPONSE
6181 "termresponse",
6182#endif
6183#ifdef FEAT_TEXTOBJ
6184 "textobjects",
6185#endif
6186#ifdef HAVE_TGETENT
6187 "tgetent",
6188#endif
6189#ifdef FEAT_TIMERS
6190 "timers",
6191#endif
6192#ifdef FEAT_TITLE
6193 "title",
6194#endif
6195#ifdef FEAT_TOOLBAR
6196 "toolbar",
6197#endif
6198#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6199 "unnamedplus",
6200#endif
6201#ifdef FEAT_USR_CMDS
6202 "user-commands", /* was accidentally included in 5.4 */
6203 "user_commands",
6204#endif
6205#ifdef FEAT_VIMINFO
6206 "viminfo",
6207#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006208 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006209#ifdef FEAT_VIRTUALEDIT
6210 "virtualedit",
6211#endif
6212 "visual",
6213#ifdef FEAT_VISUALEXTRA
6214 "visualextra",
6215#endif
6216#ifdef FEAT_VREPLACE
6217 "vreplace",
6218#endif
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006219#ifdef FEAT_VTP
6220 "vtp",
6221#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006222#ifdef FEAT_WILDIGN
6223 "wildignore",
6224#endif
6225#ifdef FEAT_WILDMENU
6226 "wildmenu",
6227#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006228 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006229#ifdef FEAT_WAK
6230 "winaltkeys",
6231#endif
6232#ifdef FEAT_WRITEBACKUP
6233 "writebackup",
6234#endif
6235#ifdef FEAT_XIM
6236 "xim",
6237#endif
6238#ifdef FEAT_XFONTSET
6239 "xfontset",
6240#endif
6241#ifdef FEAT_XPM_W32
6242 "xpm",
6243 "xpm_w32", /* for backward compatibility */
6244#else
6245# if defined(HAVE_XPM)
6246 "xpm",
6247# endif
6248#endif
6249#ifdef USE_XSMP
6250 "xsmp",
6251#endif
6252#ifdef USE_XSMP_INTERACT
6253 "xsmp_interact",
6254#endif
6255#ifdef FEAT_XCLIPBOARD
6256 "xterm_clipboard",
6257#endif
6258#ifdef FEAT_XTERM_SAVE
6259 "xterm_save",
6260#endif
6261#if defined(UNIX) && defined(FEAT_X11)
6262 "X11",
6263#endif
6264 NULL
6265 };
6266
6267 name = get_tv_string(&argvars[0]);
6268 for (i = 0; has_list[i] != NULL; ++i)
6269 if (STRICMP(name, has_list[i]) == 0)
6270 {
6271 n = TRUE;
6272 break;
6273 }
6274
6275 if (n == FALSE)
6276 {
6277 if (STRNICMP(name, "patch", 5) == 0)
6278 {
6279 if (name[5] == '-'
6280 && STRLEN(name) >= 11
6281 && vim_isdigit(name[6])
6282 && vim_isdigit(name[8])
6283 && vim_isdigit(name[10]))
6284 {
6285 int major = atoi((char *)name + 6);
6286 int minor = atoi((char *)name + 8);
6287
6288 /* Expect "patch-9.9.01234". */
6289 n = (major < VIM_VERSION_MAJOR
6290 || (major == VIM_VERSION_MAJOR
6291 && (minor < VIM_VERSION_MINOR
6292 || (minor == VIM_VERSION_MINOR
6293 && has_patch(atoi((char *)name + 10))))));
6294 }
6295 else
6296 n = has_patch(atoi((char *)name + 5));
6297 }
6298 else if (STRICMP(name, "vim_starting") == 0)
6299 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006300 else if (STRICMP(name, "ttyin") == 0)
6301 n = mch_input_isatty();
6302 else if (STRICMP(name, "ttyout") == 0)
6303 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006304#ifdef FEAT_MBYTE
6305 else if (STRICMP(name, "multi_byte_encoding") == 0)
6306 n = has_mbyte;
6307#endif
6308#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6309 else if (STRICMP(name, "balloon_multiline") == 0)
6310 n = multiline_balloon_available();
6311#endif
6312#ifdef DYNAMIC_TCL
6313 else if (STRICMP(name, "tcl") == 0)
6314 n = tcl_enabled(FALSE);
6315#endif
6316#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6317 else if (STRICMP(name, "iconv") == 0)
6318 n = iconv_enabled(FALSE);
6319#endif
6320#ifdef DYNAMIC_LUA
6321 else if (STRICMP(name, "lua") == 0)
6322 n = lua_enabled(FALSE);
6323#endif
6324#ifdef DYNAMIC_MZSCHEME
6325 else if (STRICMP(name, "mzscheme") == 0)
6326 n = mzscheme_enabled(FALSE);
6327#endif
6328#ifdef DYNAMIC_RUBY
6329 else if (STRICMP(name, "ruby") == 0)
6330 n = ruby_enabled(FALSE);
6331#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006332#ifdef DYNAMIC_PYTHON
6333 else if (STRICMP(name, "python") == 0)
6334 n = python_enabled(FALSE);
6335#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006336#ifdef DYNAMIC_PYTHON3
6337 else if (STRICMP(name, "python3") == 0)
6338 n = python3_enabled(FALSE);
6339#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006340#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6341 else if (STRICMP(name, "pythonx") == 0)
6342 {
6343# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6344 if (p_pyx == 0)
6345 n = python3_enabled(FALSE) || python_enabled(FALSE);
6346 else if (p_pyx == 3)
6347 n = python3_enabled(FALSE);
6348 else if (p_pyx == 2)
6349 n = python_enabled(FALSE);
6350# elif defined(DYNAMIC_PYTHON)
6351 n = python_enabled(FALSE);
6352# elif defined(DYNAMIC_PYTHON3)
6353 n = python3_enabled(FALSE);
6354# endif
6355 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006356#endif
6357#ifdef DYNAMIC_PERL
6358 else if (STRICMP(name, "perl") == 0)
6359 n = perl_enabled(FALSE);
6360#endif
6361#ifdef FEAT_GUI
6362 else if (STRICMP(name, "gui_running") == 0)
6363 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006364# ifdef FEAT_BROWSE
6365 else if (STRICMP(name, "browse") == 0)
6366 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6367# endif
6368#endif
6369#ifdef FEAT_SYN_HL
6370 else if (STRICMP(name, "syntax_items") == 0)
6371 n = syntax_present(curwin);
6372#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006373#ifdef FEAT_VTP
6374 else if (STRICMP(name, "vcon") == 0)
6375 n = has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006376#endif
6377#ifdef FEAT_NETBEANS_INTG
6378 else if (STRICMP(name, "netbeans_enabled") == 0)
6379 n = netbeans_active();
6380#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006381#if defined(FEAT_TERMINAL) && defined(WIN3264)
6382 else if (STRICMP(name, "terminal") == 0)
6383 n = terminal_enabled();
6384#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006385 }
6386
6387 rettv->vval.v_number = n;
6388}
6389
6390/*
6391 * "has_key()" function
6392 */
6393 static void
6394f_has_key(typval_T *argvars, typval_T *rettv)
6395{
6396 if (argvars[0].v_type != VAR_DICT)
6397 {
6398 EMSG(_(e_dictreq));
6399 return;
6400 }
6401 if (argvars[0].vval.v_dict == NULL)
6402 return;
6403
6404 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6405 get_tv_string(&argvars[1]), -1) != NULL;
6406}
6407
6408/*
6409 * "haslocaldir()" function
6410 */
6411 static void
6412f_haslocaldir(typval_T *argvars, typval_T *rettv)
6413{
6414 win_T *wp = NULL;
6415
6416 wp = find_tabwin(&argvars[0], &argvars[1]);
6417 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6418}
6419
6420/*
6421 * "hasmapto()" function
6422 */
6423 static void
6424f_hasmapto(typval_T *argvars, typval_T *rettv)
6425{
6426 char_u *name;
6427 char_u *mode;
6428 char_u buf[NUMBUFLEN];
6429 int abbr = FALSE;
6430
6431 name = get_tv_string(&argvars[0]);
6432 if (argvars[1].v_type == VAR_UNKNOWN)
6433 mode = (char_u *)"nvo";
6434 else
6435 {
6436 mode = get_tv_string_buf(&argvars[1], buf);
6437 if (argvars[2].v_type != VAR_UNKNOWN)
6438 abbr = (int)get_tv_number(&argvars[2]);
6439 }
6440
6441 if (map_to_exists(name, mode, abbr))
6442 rettv->vval.v_number = TRUE;
6443 else
6444 rettv->vval.v_number = FALSE;
6445}
6446
6447/*
6448 * "histadd()" function
6449 */
6450 static void
6451f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6452{
6453#ifdef FEAT_CMDHIST
6454 int histype;
6455 char_u *str;
6456 char_u buf[NUMBUFLEN];
6457#endif
6458
6459 rettv->vval.v_number = FALSE;
6460 if (check_restricted() || check_secure())
6461 return;
6462#ifdef FEAT_CMDHIST
6463 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6464 histype = str != NULL ? get_histtype(str) : -1;
6465 if (histype >= 0)
6466 {
6467 str = get_tv_string_buf(&argvars[1], buf);
6468 if (*str != NUL)
6469 {
6470 init_history();
6471 add_to_history(histype, str, FALSE, NUL);
6472 rettv->vval.v_number = TRUE;
6473 return;
6474 }
6475 }
6476#endif
6477}
6478
6479/*
6480 * "histdel()" function
6481 */
6482 static void
6483f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6484{
6485#ifdef FEAT_CMDHIST
6486 int n;
6487 char_u buf[NUMBUFLEN];
6488 char_u *str;
6489
6490 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6491 if (str == NULL)
6492 n = 0;
6493 else if (argvars[1].v_type == VAR_UNKNOWN)
6494 /* only one argument: clear entire history */
6495 n = clr_history(get_histtype(str));
6496 else if (argvars[1].v_type == VAR_NUMBER)
6497 /* index given: remove that entry */
6498 n = del_history_idx(get_histtype(str),
6499 (int)get_tv_number(&argvars[1]));
6500 else
6501 /* string given: remove all matching entries */
6502 n = del_history_entry(get_histtype(str),
6503 get_tv_string_buf(&argvars[1], buf));
6504 rettv->vval.v_number = n;
6505#endif
6506}
6507
6508/*
6509 * "histget()" function
6510 */
6511 static void
6512f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6513{
6514#ifdef FEAT_CMDHIST
6515 int type;
6516 int idx;
6517 char_u *str;
6518
6519 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6520 if (str == NULL)
6521 rettv->vval.v_string = NULL;
6522 else
6523 {
6524 type = get_histtype(str);
6525 if (argvars[1].v_type == VAR_UNKNOWN)
6526 idx = get_history_idx(type);
6527 else
6528 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6529 /* -1 on type error */
6530 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6531 }
6532#else
6533 rettv->vval.v_string = NULL;
6534#endif
6535 rettv->v_type = VAR_STRING;
6536}
6537
6538/*
6539 * "histnr()" function
6540 */
6541 static void
6542f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6543{
6544 int i;
6545
6546#ifdef FEAT_CMDHIST
6547 char_u *history = get_tv_string_chk(&argvars[0]);
6548
6549 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6550 if (i >= HIST_CMD && i < HIST_COUNT)
6551 i = get_history_idx(i);
6552 else
6553#endif
6554 i = -1;
6555 rettv->vval.v_number = i;
6556}
6557
6558/*
6559 * "highlightID(name)" function
6560 */
6561 static void
6562f_hlID(typval_T *argvars, typval_T *rettv)
6563{
6564 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6565}
6566
6567/*
6568 * "highlight_exists()" function
6569 */
6570 static void
6571f_hlexists(typval_T *argvars, typval_T *rettv)
6572{
6573 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6574}
6575
6576/*
6577 * "hostname()" function
6578 */
6579 static void
6580f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6581{
6582 char_u hostname[256];
6583
6584 mch_get_host_name(hostname, 256);
6585 rettv->v_type = VAR_STRING;
6586 rettv->vval.v_string = vim_strsave(hostname);
6587}
6588
6589/*
6590 * iconv() function
6591 */
6592 static void
6593f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6594{
6595#ifdef FEAT_MBYTE
6596 char_u buf1[NUMBUFLEN];
6597 char_u buf2[NUMBUFLEN];
6598 char_u *from, *to, *str;
6599 vimconv_T vimconv;
6600#endif
6601
6602 rettv->v_type = VAR_STRING;
6603 rettv->vval.v_string = NULL;
6604
6605#ifdef FEAT_MBYTE
6606 str = get_tv_string(&argvars[0]);
6607 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6608 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6609 vimconv.vc_type = CONV_NONE;
6610 convert_setup(&vimconv, from, to);
6611
6612 /* If the encodings are equal, no conversion needed. */
6613 if (vimconv.vc_type == CONV_NONE)
6614 rettv->vval.v_string = vim_strsave(str);
6615 else
6616 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6617
6618 convert_setup(&vimconv, NULL, NULL);
6619 vim_free(from);
6620 vim_free(to);
6621#endif
6622}
6623
6624/*
6625 * "indent()" function
6626 */
6627 static void
6628f_indent(typval_T *argvars, typval_T *rettv)
6629{
6630 linenr_T lnum;
6631
6632 lnum = get_tv_lnum(argvars);
6633 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6634 rettv->vval.v_number = get_indent_lnum(lnum);
6635 else
6636 rettv->vval.v_number = -1;
6637}
6638
6639/*
6640 * "index()" function
6641 */
6642 static void
6643f_index(typval_T *argvars, typval_T *rettv)
6644{
6645 list_T *l;
6646 listitem_T *item;
6647 long idx = 0;
6648 int ic = FALSE;
6649
6650 rettv->vval.v_number = -1;
6651 if (argvars[0].v_type != VAR_LIST)
6652 {
6653 EMSG(_(e_listreq));
6654 return;
6655 }
6656 l = argvars[0].vval.v_list;
6657 if (l != NULL)
6658 {
6659 item = l->lv_first;
6660 if (argvars[2].v_type != VAR_UNKNOWN)
6661 {
6662 int error = FALSE;
6663
6664 /* Start at specified item. Use the cached index that list_find()
6665 * sets, so that a negative number also works. */
6666 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6667 idx = l->lv_idx;
6668 if (argvars[3].v_type != VAR_UNKNOWN)
6669 ic = (int)get_tv_number_chk(&argvars[3], &error);
6670 if (error)
6671 item = NULL;
6672 }
6673
6674 for ( ; item != NULL; item = item->li_next, ++idx)
6675 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6676 {
6677 rettv->vval.v_number = idx;
6678 break;
6679 }
6680 }
6681}
6682
6683static int inputsecret_flag = 0;
6684
6685/*
6686 * "input()" function
6687 * Also handles inputsecret() when inputsecret is set.
6688 */
6689 static void
6690f_input(typval_T *argvars, typval_T *rettv)
6691{
6692 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6693}
6694
6695/*
6696 * "inputdialog()" function
6697 */
6698 static void
6699f_inputdialog(typval_T *argvars, typval_T *rettv)
6700{
6701#if defined(FEAT_GUI_TEXTDIALOG)
6702 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6703 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6704 {
6705 char_u *message;
6706 char_u buf[NUMBUFLEN];
6707 char_u *defstr = (char_u *)"";
6708
6709 message = get_tv_string_chk(&argvars[0]);
6710 if (argvars[1].v_type != VAR_UNKNOWN
6711 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6712 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6713 else
6714 IObuff[0] = NUL;
6715 if (message != NULL && defstr != NULL
6716 && do_dialog(VIM_QUESTION, NULL, message,
6717 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6718 rettv->vval.v_string = vim_strsave(IObuff);
6719 else
6720 {
6721 if (message != NULL && defstr != NULL
6722 && argvars[1].v_type != VAR_UNKNOWN
6723 && argvars[2].v_type != VAR_UNKNOWN)
6724 rettv->vval.v_string = vim_strsave(
6725 get_tv_string_buf(&argvars[2], buf));
6726 else
6727 rettv->vval.v_string = NULL;
6728 }
6729 rettv->v_type = VAR_STRING;
6730 }
6731 else
6732#endif
6733 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6734}
6735
6736/*
6737 * "inputlist()" function
6738 */
6739 static void
6740f_inputlist(typval_T *argvars, typval_T *rettv)
6741{
6742 listitem_T *li;
6743 int selected;
6744 int mouse_used;
6745
6746#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006747 /* While starting up, there is no place to enter text. When running tests
6748 * with --not-a-term we assume feedkeys() will be used. */
6749 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006750 return;
6751#endif
6752 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6753 {
6754 EMSG2(_(e_listarg), "inputlist()");
6755 return;
6756 }
6757
6758 msg_start();
6759 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6760 lines_left = Rows; /* avoid more prompt */
6761 msg_scroll = TRUE;
6762 msg_clr_eos();
6763
6764 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6765 {
6766 msg_puts(get_tv_string(&li->li_tv));
6767 msg_putchar('\n');
6768 }
6769
6770 /* Ask for choice. */
6771 selected = prompt_for_number(&mouse_used);
6772 if (mouse_used)
6773 selected -= lines_left;
6774
6775 rettv->vval.v_number = selected;
6776}
6777
6778
6779static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6780
6781/*
6782 * "inputrestore()" function
6783 */
6784 static void
6785f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6786{
6787 if (ga_userinput.ga_len > 0)
6788 {
6789 --ga_userinput.ga_len;
6790 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6791 + ga_userinput.ga_len);
6792 /* default return is zero == OK */
6793 }
6794 else if (p_verbose > 1)
6795 {
6796 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6797 rettv->vval.v_number = 1; /* Failed */
6798 }
6799}
6800
6801/*
6802 * "inputsave()" function
6803 */
6804 static void
6805f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6806{
6807 /* Add an entry to the stack of typeahead storage. */
6808 if (ga_grow(&ga_userinput, 1) == OK)
6809 {
6810 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6811 + ga_userinput.ga_len);
6812 ++ga_userinput.ga_len;
6813 /* default return is zero == OK */
6814 }
6815 else
6816 rettv->vval.v_number = 1; /* Failed */
6817}
6818
6819/*
6820 * "inputsecret()" function
6821 */
6822 static void
6823f_inputsecret(typval_T *argvars, typval_T *rettv)
6824{
6825 ++cmdline_star;
6826 ++inputsecret_flag;
6827 f_input(argvars, rettv);
6828 --cmdline_star;
6829 --inputsecret_flag;
6830}
6831
6832/*
6833 * "insert()" function
6834 */
6835 static void
6836f_insert(typval_T *argvars, typval_T *rettv)
6837{
6838 long before = 0;
6839 listitem_T *item;
6840 list_T *l;
6841 int error = FALSE;
6842
6843 if (argvars[0].v_type != VAR_LIST)
6844 EMSG2(_(e_listarg), "insert()");
6845 else if ((l = argvars[0].vval.v_list) != NULL
6846 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6847 {
6848 if (argvars[2].v_type != VAR_UNKNOWN)
6849 before = (long)get_tv_number_chk(&argvars[2], &error);
6850 if (error)
6851 return; /* type error; errmsg already given */
6852
6853 if (before == l->lv_len)
6854 item = NULL;
6855 else
6856 {
6857 item = list_find(l, before);
6858 if (item == NULL)
6859 {
6860 EMSGN(_(e_listidx), before);
6861 l = NULL;
6862 }
6863 }
6864 if (l != NULL)
6865 {
6866 list_insert_tv(l, &argvars[1], item);
6867 copy_tv(&argvars[0], rettv);
6868 }
6869 }
6870}
6871
6872/*
6873 * "invert(expr)" function
6874 */
6875 static void
6876f_invert(typval_T *argvars, typval_T *rettv)
6877{
6878 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6879}
6880
6881/*
6882 * "isdirectory()" function
6883 */
6884 static void
6885f_isdirectory(typval_T *argvars, typval_T *rettv)
6886{
6887 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6888}
6889
6890/*
6891 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6892 * or it refers to a List or Dictionary that is locked.
6893 */
6894 static int
6895tv_islocked(typval_T *tv)
6896{
6897 return (tv->v_lock & VAR_LOCKED)
6898 || (tv->v_type == VAR_LIST
6899 && tv->vval.v_list != NULL
6900 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6901 || (tv->v_type == VAR_DICT
6902 && tv->vval.v_dict != NULL
6903 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6904}
6905
6906/*
6907 * "islocked()" function
6908 */
6909 static void
6910f_islocked(typval_T *argvars, typval_T *rettv)
6911{
6912 lval_T lv;
6913 char_u *end;
6914 dictitem_T *di;
6915
6916 rettv->vval.v_number = -1;
6917 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006918 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006919 if (end != NULL && lv.ll_name != NULL)
6920 {
6921 if (*end != NUL)
6922 EMSG(_(e_trailing));
6923 else
6924 {
6925 if (lv.ll_tv == NULL)
6926 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006927 di = find_var(lv.ll_name, NULL, TRUE);
6928 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006929 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006930 /* Consider a variable locked when:
6931 * 1. the variable itself is locked
6932 * 2. the value of the variable is locked.
6933 * 3. the List or Dict value is locked.
6934 */
6935 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6936 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006937 }
6938 }
6939 else if (lv.ll_range)
6940 EMSG(_("E786: Range not allowed"));
6941 else if (lv.ll_newkey != NULL)
6942 EMSG2(_(e_dictkey), lv.ll_newkey);
6943 else if (lv.ll_list != NULL)
6944 /* List item. */
6945 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6946 else
6947 /* Dictionary item. */
6948 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6949 }
6950 }
6951
6952 clear_lval(&lv);
6953}
6954
6955#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6956/*
6957 * "isnan()" function
6958 */
6959 static void
6960f_isnan(typval_T *argvars, typval_T *rettv)
6961{
6962 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6963 && isnan(argvars[0].vval.v_float);
6964}
6965#endif
6966
6967/*
6968 * "items(dict)" function
6969 */
6970 static void
6971f_items(typval_T *argvars, typval_T *rettv)
6972{
6973 dict_list(argvars, rettv, 2);
6974}
6975
6976#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6977/*
6978 * Get the job from the argument.
6979 * Returns NULL if the job is invalid.
6980 */
6981 static job_T *
6982get_job_arg(typval_T *tv)
6983{
6984 job_T *job;
6985
6986 if (tv->v_type != VAR_JOB)
6987 {
6988 EMSG2(_(e_invarg2), get_tv_string(tv));
6989 return NULL;
6990 }
6991 job = tv->vval.v_job;
6992
6993 if (job == NULL)
6994 EMSG(_("E916: not a valid job"));
6995 return job;
6996}
6997
6998/*
6999 * "job_getchannel()" function
7000 */
7001 static void
7002f_job_getchannel(typval_T *argvars, typval_T *rettv)
7003{
7004 job_T *job = get_job_arg(&argvars[0]);
7005
7006 if (job != NULL)
7007 {
7008 rettv->v_type = VAR_CHANNEL;
7009 rettv->vval.v_channel = job->jv_channel;
7010 if (job->jv_channel != NULL)
7011 ++job->jv_channel->ch_refcount;
7012 }
7013}
7014
7015/*
7016 * "job_info()" function
7017 */
7018 static void
7019f_job_info(typval_T *argvars, typval_T *rettv)
7020{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007021 if (argvars[0].v_type != VAR_UNKNOWN)
7022 {
7023 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007024
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007025 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7026 job_info(job, rettv->vval.v_dict);
7027 }
7028 else if (rettv_list_alloc(rettv) == OK)
7029 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007030}
7031
7032/*
7033 * "job_setoptions()" function
7034 */
7035 static void
7036f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7037{
7038 job_T *job = get_job_arg(&argvars[0]);
7039 jobopt_T opt;
7040
7041 if (job == NULL)
7042 return;
7043 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007044 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007045 job_set_options(job, &opt);
7046 free_job_options(&opt);
7047}
7048
7049/*
7050 * "job_start()" function
7051 */
7052 static void
7053f_job_start(typval_T *argvars, typval_T *rettv)
7054{
7055 rettv->v_type = VAR_JOB;
7056 if (check_restricted() || check_secure())
7057 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01007058 rettv->vval.v_job = job_start(argvars, NULL, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007059}
7060
7061/*
7062 * "job_status()" function
7063 */
7064 static void
7065f_job_status(typval_T *argvars, typval_T *rettv)
7066{
7067 job_T *job = get_job_arg(&argvars[0]);
7068
7069 if (job != NULL)
7070 {
7071 rettv->v_type = VAR_STRING;
7072 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7073 }
7074}
7075
7076/*
7077 * "job_stop()" function
7078 */
7079 static void
7080f_job_stop(typval_T *argvars, typval_T *rettv)
7081{
7082 job_T *job = get_job_arg(&argvars[0]);
7083
7084 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007085 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007086}
7087#endif
7088
7089/*
7090 * "join()" function
7091 */
7092 static void
7093f_join(typval_T *argvars, typval_T *rettv)
7094{
7095 garray_T ga;
7096 char_u *sep;
7097
7098 if (argvars[0].v_type != VAR_LIST)
7099 {
7100 EMSG(_(e_listreq));
7101 return;
7102 }
7103 if (argvars[0].vval.v_list == NULL)
7104 return;
7105 if (argvars[1].v_type == VAR_UNKNOWN)
7106 sep = (char_u *)" ";
7107 else
7108 sep = get_tv_string_chk(&argvars[1]);
7109
7110 rettv->v_type = VAR_STRING;
7111
7112 if (sep != NULL)
7113 {
7114 ga_init2(&ga, (int)sizeof(char), 80);
7115 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7116 ga_append(&ga, NUL);
7117 rettv->vval.v_string = (char_u *)ga.ga_data;
7118 }
7119 else
7120 rettv->vval.v_string = NULL;
7121}
7122
7123/*
7124 * "js_decode()" function
7125 */
7126 static void
7127f_js_decode(typval_T *argvars, typval_T *rettv)
7128{
7129 js_read_T reader;
7130
7131 reader.js_buf = get_tv_string(&argvars[0]);
7132 reader.js_fill = NULL;
7133 reader.js_used = 0;
7134 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7135 EMSG(_(e_invarg));
7136}
7137
7138/*
7139 * "js_encode()" function
7140 */
7141 static void
7142f_js_encode(typval_T *argvars, typval_T *rettv)
7143{
7144 rettv->v_type = VAR_STRING;
7145 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7146}
7147
7148/*
7149 * "json_decode()" function
7150 */
7151 static void
7152f_json_decode(typval_T *argvars, typval_T *rettv)
7153{
7154 js_read_T reader;
7155
7156 reader.js_buf = get_tv_string(&argvars[0]);
7157 reader.js_fill = NULL;
7158 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007159 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007160}
7161
7162/*
7163 * "json_encode()" function
7164 */
7165 static void
7166f_json_encode(typval_T *argvars, typval_T *rettv)
7167{
7168 rettv->v_type = VAR_STRING;
7169 rettv->vval.v_string = json_encode(&argvars[0], 0);
7170}
7171
7172/*
7173 * "keys()" function
7174 */
7175 static void
7176f_keys(typval_T *argvars, typval_T *rettv)
7177{
7178 dict_list(argvars, rettv, 0);
7179}
7180
7181/*
7182 * "last_buffer_nr()" function.
7183 */
7184 static void
7185f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7186{
7187 int n = 0;
7188 buf_T *buf;
7189
Bram Moolenaar29323592016-07-24 22:04:11 +02007190 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007191 if (n < buf->b_fnum)
7192 n = buf->b_fnum;
7193
7194 rettv->vval.v_number = n;
7195}
7196
7197/*
7198 * "len()" function
7199 */
7200 static void
7201f_len(typval_T *argvars, typval_T *rettv)
7202{
7203 switch (argvars[0].v_type)
7204 {
7205 case VAR_STRING:
7206 case VAR_NUMBER:
7207 rettv->vval.v_number = (varnumber_T)STRLEN(
7208 get_tv_string(&argvars[0]));
7209 break;
7210 case VAR_LIST:
7211 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7212 break;
7213 case VAR_DICT:
7214 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7215 break;
7216 case VAR_UNKNOWN:
7217 case VAR_SPECIAL:
7218 case VAR_FLOAT:
7219 case VAR_FUNC:
7220 case VAR_PARTIAL:
7221 case VAR_JOB:
7222 case VAR_CHANNEL:
7223 EMSG(_("E701: Invalid type for len()"));
7224 break;
7225 }
7226}
7227
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007228 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007229libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007230{
7231#ifdef FEAT_LIBCALL
7232 char_u *string_in;
7233 char_u **string_result;
7234 int nr_result;
7235#endif
7236
7237 rettv->v_type = type;
7238 if (type != VAR_NUMBER)
7239 rettv->vval.v_string = NULL;
7240
7241 if (check_restricted() || check_secure())
7242 return;
7243
7244#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007245 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007246 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7247 {
7248 string_in = NULL;
7249 if (argvars[2].v_type == VAR_STRING)
7250 string_in = argvars[2].vval.v_string;
7251 if (type == VAR_NUMBER)
7252 string_result = NULL;
7253 else
7254 string_result = &rettv->vval.v_string;
7255 if (mch_libcall(argvars[0].vval.v_string,
7256 argvars[1].vval.v_string,
7257 string_in,
7258 argvars[2].vval.v_number,
7259 string_result,
7260 &nr_result) == OK
7261 && type == VAR_NUMBER)
7262 rettv->vval.v_number = nr_result;
7263 }
7264#endif
7265}
7266
7267/*
7268 * "libcall()" function
7269 */
7270 static void
7271f_libcall(typval_T *argvars, typval_T *rettv)
7272{
7273 libcall_common(argvars, rettv, VAR_STRING);
7274}
7275
7276/*
7277 * "libcallnr()" function
7278 */
7279 static void
7280f_libcallnr(typval_T *argvars, typval_T *rettv)
7281{
7282 libcall_common(argvars, rettv, VAR_NUMBER);
7283}
7284
7285/*
7286 * "line(string)" function
7287 */
7288 static void
7289f_line(typval_T *argvars, typval_T *rettv)
7290{
7291 linenr_T lnum = 0;
7292 pos_T *fp;
7293 int fnum;
7294
7295 fp = var2fpos(&argvars[0], TRUE, &fnum);
7296 if (fp != NULL)
7297 lnum = fp->lnum;
7298 rettv->vval.v_number = lnum;
7299}
7300
7301/*
7302 * "line2byte(lnum)" function
7303 */
7304 static void
7305f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7306{
7307#ifndef FEAT_BYTEOFF
7308 rettv->vval.v_number = -1;
7309#else
7310 linenr_T lnum;
7311
7312 lnum = get_tv_lnum(argvars);
7313 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7314 rettv->vval.v_number = -1;
7315 else
7316 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7317 if (rettv->vval.v_number >= 0)
7318 ++rettv->vval.v_number;
7319#endif
7320}
7321
7322/*
7323 * "lispindent(lnum)" function
7324 */
7325 static void
7326f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7327{
7328#ifdef FEAT_LISP
7329 pos_T pos;
7330 linenr_T lnum;
7331
7332 pos = curwin->w_cursor;
7333 lnum = get_tv_lnum(argvars);
7334 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7335 {
7336 curwin->w_cursor.lnum = lnum;
7337 rettv->vval.v_number = get_lisp_indent();
7338 curwin->w_cursor = pos;
7339 }
7340 else
7341#endif
7342 rettv->vval.v_number = -1;
7343}
7344
7345/*
7346 * "localtime()" function
7347 */
7348 static void
7349f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7350{
7351 rettv->vval.v_number = (varnumber_T)time(NULL);
7352}
7353
7354static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7355
7356 static void
7357get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7358{
7359 char_u *keys;
7360 char_u *which;
7361 char_u buf[NUMBUFLEN];
7362 char_u *keys_buf = NULL;
7363 char_u *rhs;
7364 int mode;
7365 int abbr = FALSE;
7366 int get_dict = FALSE;
7367 mapblock_T *mp;
7368 int buffer_local;
7369
7370 /* return empty string for failure */
7371 rettv->v_type = VAR_STRING;
7372 rettv->vval.v_string = NULL;
7373
7374 keys = get_tv_string(&argvars[0]);
7375 if (*keys == NUL)
7376 return;
7377
7378 if (argvars[1].v_type != VAR_UNKNOWN)
7379 {
7380 which = get_tv_string_buf_chk(&argvars[1], buf);
7381 if (argvars[2].v_type != VAR_UNKNOWN)
7382 {
7383 abbr = (int)get_tv_number(&argvars[2]);
7384 if (argvars[3].v_type != VAR_UNKNOWN)
7385 get_dict = (int)get_tv_number(&argvars[3]);
7386 }
7387 }
7388 else
7389 which = (char_u *)"";
7390 if (which == NULL)
7391 return;
7392
7393 mode = get_map_mode(&which, 0);
7394
7395 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7396 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7397 vim_free(keys_buf);
7398
7399 if (!get_dict)
7400 {
7401 /* Return a string. */
7402 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007403 {
7404 if (*rhs == NUL)
7405 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7406 else
7407 rettv->vval.v_string = str2special_save(rhs, FALSE);
7408 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007409
7410 }
7411 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7412 {
7413 /* Return a dictionary. */
7414 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7415 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7416 dict_T *dict = rettv->vval.v_dict;
7417
7418 dict_add_nr_str(dict, "lhs", 0L, lhs);
7419 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7420 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7421 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7422 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7423 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7424 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7425 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7426 dict_add_nr_str(dict, "mode", 0L, mapmode);
7427
7428 vim_free(lhs);
7429 vim_free(mapmode);
7430 }
7431}
7432
7433#ifdef FEAT_FLOAT
7434/*
7435 * "log()" function
7436 */
7437 static void
7438f_log(typval_T *argvars, typval_T *rettv)
7439{
7440 float_T f = 0.0;
7441
7442 rettv->v_type = VAR_FLOAT;
7443 if (get_float_arg(argvars, &f) == OK)
7444 rettv->vval.v_float = log(f);
7445 else
7446 rettv->vval.v_float = 0.0;
7447}
7448
7449/*
7450 * "log10()" function
7451 */
7452 static void
7453f_log10(typval_T *argvars, typval_T *rettv)
7454{
7455 float_T f = 0.0;
7456
7457 rettv->v_type = VAR_FLOAT;
7458 if (get_float_arg(argvars, &f) == OK)
7459 rettv->vval.v_float = log10(f);
7460 else
7461 rettv->vval.v_float = 0.0;
7462}
7463#endif
7464
7465#ifdef FEAT_LUA
7466/*
7467 * "luaeval()" function
7468 */
7469 static void
7470f_luaeval(typval_T *argvars, typval_T *rettv)
7471{
7472 char_u *str;
7473 char_u buf[NUMBUFLEN];
7474
7475 str = get_tv_string_buf(&argvars[0], buf);
7476 do_luaeval(str, argvars + 1, rettv);
7477}
7478#endif
7479
7480/*
7481 * "map()" function
7482 */
7483 static void
7484f_map(typval_T *argvars, typval_T *rettv)
7485{
7486 filter_map(argvars, rettv, TRUE);
7487}
7488
7489/*
7490 * "maparg()" function
7491 */
7492 static void
7493f_maparg(typval_T *argvars, typval_T *rettv)
7494{
7495 get_maparg(argvars, rettv, TRUE);
7496}
7497
7498/*
7499 * "mapcheck()" function
7500 */
7501 static void
7502f_mapcheck(typval_T *argvars, typval_T *rettv)
7503{
7504 get_maparg(argvars, rettv, FALSE);
7505}
7506
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007507typedef enum
7508{
7509 MATCH_END, /* matchend() */
7510 MATCH_MATCH, /* match() */
7511 MATCH_STR, /* matchstr() */
7512 MATCH_LIST, /* matchlist() */
7513 MATCH_POS /* matchstrpos() */
7514} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007515
7516 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007517find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007518{
7519 char_u *str = NULL;
7520 long len = 0;
7521 char_u *expr = NULL;
7522 char_u *pat;
7523 regmatch_T regmatch;
7524 char_u patbuf[NUMBUFLEN];
7525 char_u strbuf[NUMBUFLEN];
7526 char_u *save_cpo;
7527 long start = 0;
7528 long nth = 1;
7529 colnr_T startcol = 0;
7530 int match = 0;
7531 list_T *l = NULL;
7532 listitem_T *li = NULL;
7533 long idx = 0;
7534 char_u *tofree = NULL;
7535
7536 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7537 save_cpo = p_cpo;
7538 p_cpo = (char_u *)"";
7539
7540 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007541 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007542 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007543 /* type MATCH_LIST: return empty list when there are no matches.
7544 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007545 if (rettv_list_alloc(rettv) == FAIL)
7546 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007547 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007548 && (list_append_string(rettv->vval.v_list,
7549 (char_u *)"", 0) == FAIL
7550 || list_append_number(rettv->vval.v_list,
7551 (varnumber_T)-1) == FAIL
7552 || list_append_number(rettv->vval.v_list,
7553 (varnumber_T)-1) == FAIL
7554 || list_append_number(rettv->vval.v_list,
7555 (varnumber_T)-1) == FAIL))
7556 {
7557 list_free(rettv->vval.v_list);
7558 rettv->vval.v_list = NULL;
7559 goto theend;
7560 }
7561 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007562 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007563 {
7564 rettv->v_type = VAR_STRING;
7565 rettv->vval.v_string = NULL;
7566 }
7567
7568 if (argvars[0].v_type == VAR_LIST)
7569 {
7570 if ((l = argvars[0].vval.v_list) == NULL)
7571 goto theend;
7572 li = l->lv_first;
7573 }
7574 else
7575 {
7576 expr = str = get_tv_string(&argvars[0]);
7577 len = (long)STRLEN(str);
7578 }
7579
7580 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7581 if (pat == NULL)
7582 goto theend;
7583
7584 if (argvars[2].v_type != VAR_UNKNOWN)
7585 {
7586 int error = FALSE;
7587
7588 start = (long)get_tv_number_chk(&argvars[2], &error);
7589 if (error)
7590 goto theend;
7591 if (l != NULL)
7592 {
7593 li = list_find(l, start);
7594 if (li == NULL)
7595 goto theend;
7596 idx = l->lv_idx; /* use the cached index */
7597 }
7598 else
7599 {
7600 if (start < 0)
7601 start = 0;
7602 if (start > len)
7603 goto theend;
7604 /* When "count" argument is there ignore matches before "start",
7605 * otherwise skip part of the string. Differs when pattern is "^"
7606 * or "\<". */
7607 if (argvars[3].v_type != VAR_UNKNOWN)
7608 startcol = start;
7609 else
7610 {
7611 str += start;
7612 len -= start;
7613 }
7614 }
7615
7616 if (argvars[3].v_type != VAR_UNKNOWN)
7617 nth = (long)get_tv_number_chk(&argvars[3], &error);
7618 if (error)
7619 goto theend;
7620 }
7621
7622 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7623 if (regmatch.regprog != NULL)
7624 {
7625 regmatch.rm_ic = p_ic;
7626
7627 for (;;)
7628 {
7629 if (l != NULL)
7630 {
7631 if (li == NULL)
7632 {
7633 match = FALSE;
7634 break;
7635 }
7636 vim_free(tofree);
7637 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7638 if (str == NULL)
7639 break;
7640 }
7641
7642 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7643
7644 if (match && --nth <= 0)
7645 break;
7646 if (l == NULL && !match)
7647 break;
7648
7649 /* Advance to just after the match. */
7650 if (l != NULL)
7651 {
7652 li = li->li_next;
7653 ++idx;
7654 }
7655 else
7656 {
7657#ifdef FEAT_MBYTE
7658 startcol = (colnr_T)(regmatch.startp[0]
7659 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7660#else
7661 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7662#endif
7663 if (startcol > (colnr_T)len
7664 || str + startcol <= regmatch.startp[0])
7665 {
7666 match = FALSE;
7667 break;
7668 }
7669 }
7670 }
7671
7672 if (match)
7673 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007674 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007675 {
7676 listitem_T *li1 = rettv->vval.v_list->lv_first;
7677 listitem_T *li2 = li1->li_next;
7678 listitem_T *li3 = li2->li_next;
7679 listitem_T *li4 = li3->li_next;
7680
7681 vim_free(li1->li_tv.vval.v_string);
7682 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7683 (int)(regmatch.endp[0] - regmatch.startp[0]));
7684 li3->li_tv.vval.v_number =
7685 (varnumber_T)(regmatch.startp[0] - expr);
7686 li4->li_tv.vval.v_number =
7687 (varnumber_T)(regmatch.endp[0] - expr);
7688 if (l != NULL)
7689 li2->li_tv.vval.v_number = (varnumber_T)idx;
7690 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007691 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007692 {
7693 int i;
7694
7695 /* return list with matched string and submatches */
7696 for (i = 0; i < NSUBEXP; ++i)
7697 {
7698 if (regmatch.endp[i] == NULL)
7699 {
7700 if (list_append_string(rettv->vval.v_list,
7701 (char_u *)"", 0) == FAIL)
7702 break;
7703 }
7704 else if (list_append_string(rettv->vval.v_list,
7705 regmatch.startp[i],
7706 (int)(regmatch.endp[i] - regmatch.startp[i]))
7707 == FAIL)
7708 break;
7709 }
7710 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007711 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007712 {
7713 /* return matched string */
7714 if (l != NULL)
7715 copy_tv(&li->li_tv, rettv);
7716 else
7717 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7718 (int)(regmatch.endp[0] - regmatch.startp[0]));
7719 }
7720 else if (l != NULL)
7721 rettv->vval.v_number = idx;
7722 else
7723 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007724 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007725 rettv->vval.v_number =
7726 (varnumber_T)(regmatch.startp[0] - str);
7727 else
7728 rettv->vval.v_number =
7729 (varnumber_T)(regmatch.endp[0] - str);
7730 rettv->vval.v_number += (varnumber_T)(str - expr);
7731 }
7732 }
7733 vim_regfree(regmatch.regprog);
7734 }
7735
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007736theend:
7737 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007738 /* matchstrpos() without a list: drop the second item. */
7739 listitem_remove(rettv->vval.v_list,
7740 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007741 vim_free(tofree);
7742 p_cpo = save_cpo;
7743}
7744
7745/*
7746 * "match()" function
7747 */
7748 static void
7749f_match(typval_T *argvars, typval_T *rettv)
7750{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007751 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007752}
7753
7754/*
7755 * "matchadd()" function
7756 */
7757 static void
7758f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7759{
7760#ifdef FEAT_SEARCH_EXTRA
7761 char_u buf[NUMBUFLEN];
7762 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7763 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7764 int prio = 10; /* default priority */
7765 int id = -1;
7766 int error = FALSE;
7767 char_u *conceal_char = NULL;
7768
7769 rettv->vval.v_number = -1;
7770
7771 if (grp == NULL || pat == NULL)
7772 return;
7773 if (argvars[2].v_type != VAR_UNKNOWN)
7774 {
7775 prio = (int)get_tv_number_chk(&argvars[2], &error);
7776 if (argvars[3].v_type != VAR_UNKNOWN)
7777 {
7778 id = (int)get_tv_number_chk(&argvars[3], &error);
7779 if (argvars[4].v_type != VAR_UNKNOWN)
7780 {
7781 if (argvars[4].v_type != VAR_DICT)
7782 {
7783 EMSG(_(e_dictreq));
7784 return;
7785 }
7786 if (dict_find(argvars[4].vval.v_dict,
7787 (char_u *)"conceal", -1) != NULL)
7788 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7789 (char_u *)"conceal", FALSE);
7790 }
7791 }
7792 }
7793 if (error == TRUE)
7794 return;
7795 if (id >= 1 && id <= 3)
7796 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007797 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007798 return;
7799 }
7800
7801 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7802 conceal_char);
7803#endif
7804}
7805
7806/*
7807 * "matchaddpos()" function
7808 */
7809 static void
7810f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7811{
7812#ifdef FEAT_SEARCH_EXTRA
7813 char_u buf[NUMBUFLEN];
7814 char_u *group;
7815 int prio = 10;
7816 int id = -1;
7817 int error = FALSE;
7818 list_T *l;
7819 char_u *conceal_char = NULL;
7820
7821 rettv->vval.v_number = -1;
7822
7823 group = get_tv_string_buf_chk(&argvars[0], buf);
7824 if (group == NULL)
7825 return;
7826
7827 if (argvars[1].v_type != VAR_LIST)
7828 {
7829 EMSG2(_(e_listarg), "matchaddpos()");
7830 return;
7831 }
7832 l = argvars[1].vval.v_list;
7833 if (l == NULL)
7834 return;
7835
7836 if (argvars[2].v_type != VAR_UNKNOWN)
7837 {
7838 prio = (int)get_tv_number_chk(&argvars[2], &error);
7839 if (argvars[3].v_type != VAR_UNKNOWN)
7840 {
7841 id = (int)get_tv_number_chk(&argvars[3], &error);
7842 if (argvars[4].v_type != VAR_UNKNOWN)
7843 {
7844 if (argvars[4].v_type != VAR_DICT)
7845 {
7846 EMSG(_(e_dictreq));
7847 return;
7848 }
7849 if (dict_find(argvars[4].vval.v_dict,
7850 (char_u *)"conceal", -1) != NULL)
7851 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7852 (char_u *)"conceal", FALSE);
7853 }
7854 }
7855 }
7856 if (error == TRUE)
7857 return;
7858
7859 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7860 if (id == 1 || id == 2)
7861 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007862 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007863 return;
7864 }
7865
7866 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7867 conceal_char);
7868#endif
7869}
7870
7871/*
7872 * "matcharg()" function
7873 */
7874 static void
7875f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7876{
7877 if (rettv_list_alloc(rettv) == OK)
7878 {
7879#ifdef FEAT_SEARCH_EXTRA
7880 int id = (int)get_tv_number(&argvars[0]);
7881 matchitem_T *m;
7882
7883 if (id >= 1 && id <= 3)
7884 {
7885 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7886 {
7887 list_append_string(rettv->vval.v_list,
7888 syn_id2name(m->hlg_id), -1);
7889 list_append_string(rettv->vval.v_list, m->pattern, -1);
7890 }
7891 else
7892 {
7893 list_append_string(rettv->vval.v_list, NULL, -1);
7894 list_append_string(rettv->vval.v_list, NULL, -1);
7895 }
7896 }
7897#endif
7898 }
7899}
7900
7901/*
7902 * "matchdelete()" function
7903 */
7904 static void
7905f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7906{
7907#ifdef FEAT_SEARCH_EXTRA
7908 rettv->vval.v_number = match_delete(curwin,
7909 (int)get_tv_number(&argvars[0]), TRUE);
7910#endif
7911}
7912
7913/*
7914 * "matchend()" function
7915 */
7916 static void
7917f_matchend(typval_T *argvars, typval_T *rettv)
7918{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007919 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007920}
7921
7922/*
7923 * "matchlist()" function
7924 */
7925 static void
7926f_matchlist(typval_T *argvars, typval_T *rettv)
7927{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007928 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007929}
7930
7931/*
7932 * "matchstr()" function
7933 */
7934 static void
7935f_matchstr(typval_T *argvars, typval_T *rettv)
7936{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007937 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007938}
7939
7940/*
7941 * "matchstrpos()" function
7942 */
7943 static void
7944f_matchstrpos(typval_T *argvars, typval_T *rettv)
7945{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007946 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007947}
7948
7949static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7950
7951 static void
7952max_min(typval_T *argvars, typval_T *rettv, int domax)
7953{
7954 varnumber_T n = 0;
7955 varnumber_T i;
7956 int error = FALSE;
7957
7958 if (argvars[0].v_type == VAR_LIST)
7959 {
7960 list_T *l;
7961 listitem_T *li;
7962
7963 l = argvars[0].vval.v_list;
7964 if (l != NULL)
7965 {
7966 li = l->lv_first;
7967 if (li != NULL)
7968 {
7969 n = get_tv_number_chk(&li->li_tv, &error);
7970 for (;;)
7971 {
7972 li = li->li_next;
7973 if (li == NULL)
7974 break;
7975 i = get_tv_number_chk(&li->li_tv, &error);
7976 if (domax ? i > n : i < n)
7977 n = i;
7978 }
7979 }
7980 }
7981 }
7982 else if (argvars[0].v_type == VAR_DICT)
7983 {
7984 dict_T *d;
7985 int first = TRUE;
7986 hashitem_T *hi;
7987 int todo;
7988
7989 d = argvars[0].vval.v_dict;
7990 if (d != NULL)
7991 {
7992 todo = (int)d->dv_hashtab.ht_used;
7993 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7994 {
7995 if (!HASHITEM_EMPTY(hi))
7996 {
7997 --todo;
7998 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7999 if (first)
8000 {
8001 n = i;
8002 first = FALSE;
8003 }
8004 else if (domax ? i > n : i < n)
8005 n = i;
8006 }
8007 }
8008 }
8009 }
8010 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008011 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008012 rettv->vval.v_number = error ? 0 : n;
8013}
8014
8015/*
8016 * "max()" function
8017 */
8018 static void
8019f_max(typval_T *argvars, typval_T *rettv)
8020{
8021 max_min(argvars, rettv, TRUE);
8022}
8023
8024/*
8025 * "min()" function
8026 */
8027 static void
8028f_min(typval_T *argvars, typval_T *rettv)
8029{
8030 max_min(argvars, rettv, FALSE);
8031}
8032
8033static int mkdir_recurse(char_u *dir, int prot);
8034
8035/*
8036 * Create the directory in which "dir" is located, and higher levels when
8037 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008038 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008039 */
8040 static int
8041mkdir_recurse(char_u *dir, int prot)
8042{
8043 char_u *p;
8044 char_u *updir;
8045 int r = FAIL;
8046
8047 /* Get end of directory name in "dir".
8048 * We're done when it's "/" or "c:/". */
8049 p = gettail_sep(dir);
8050 if (p <= get_past_head(dir))
8051 return OK;
8052
8053 /* If the directory exists we're done. Otherwise: create it.*/
8054 updir = vim_strnsave(dir, (int)(p - dir));
8055 if (updir == NULL)
8056 return FAIL;
8057 if (mch_isdir(updir))
8058 r = OK;
8059 else if (mkdir_recurse(updir, prot) == OK)
8060 r = vim_mkdir_emsg(updir, prot);
8061 vim_free(updir);
8062 return r;
8063}
8064
8065#ifdef vim_mkdir
8066/*
8067 * "mkdir()" function
8068 */
8069 static void
8070f_mkdir(typval_T *argvars, typval_T *rettv)
8071{
8072 char_u *dir;
8073 char_u buf[NUMBUFLEN];
8074 int prot = 0755;
8075
8076 rettv->vval.v_number = FAIL;
8077 if (check_restricted() || check_secure())
8078 return;
8079
8080 dir = get_tv_string_buf(&argvars[0], buf);
8081 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008082 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008083
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008084 if (*gettail(dir) == NUL)
8085 /* remove trailing slashes */
8086 *gettail_sep(dir) = NUL;
8087
8088 if (argvars[1].v_type != VAR_UNKNOWN)
8089 {
8090 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008091 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008092 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8093 if (prot == -1)
8094 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008095 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008096 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8097 {
8098 if (mch_isdir(dir))
8099 {
8100 /* With the "p" flag it's OK if the dir already exists. */
8101 rettv->vval.v_number = OK;
8102 return;
8103 }
8104 mkdir_recurse(dir, prot);
8105 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008106 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008107 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008108}
8109#endif
8110
8111/*
8112 * "mode()" function
8113 */
8114 static void
8115f_mode(typval_T *argvars, typval_T *rettv)
8116{
8117 char_u buf[3];
8118
8119 buf[1] = NUL;
8120 buf[2] = NUL;
8121
8122 if (time_for_testing == 93784)
8123 {
8124 /* Testing the two-character code. */
8125 buf[0] = 'x';
8126 buf[1] = '!';
8127 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008128#ifdef FEAT_TERMINAL
8129 else if (term_use_loop())
8130 buf[0] = 't';
8131#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008132 else if (VIsual_active)
8133 {
8134 if (VIsual_select)
8135 buf[0] = VIsual_mode + 's' - 'v';
8136 else
8137 buf[0] = VIsual_mode;
8138 }
8139 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8140 || State == CONFIRM)
8141 {
8142 buf[0] = 'r';
8143 if (State == ASKMORE)
8144 buf[1] = 'm';
8145 else if (State == CONFIRM)
8146 buf[1] = '?';
8147 }
8148 else if (State == EXTERNCMD)
8149 buf[0] = '!';
8150 else if (State & INSERT)
8151 {
8152#ifdef FEAT_VREPLACE
8153 if (State & VREPLACE_FLAG)
8154 {
8155 buf[0] = 'R';
8156 buf[1] = 'v';
8157 }
8158 else
8159#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008160 {
8161 if (State & REPLACE_FLAG)
8162 buf[0] = 'R';
8163 else
8164 buf[0] = 'i';
8165#ifdef FEAT_INS_EXPAND
8166 if (ins_compl_active())
8167 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008168 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008169 buf[1] = 'x';
8170#endif
8171 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008172 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008173 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008174 {
8175 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008176 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008177 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008178 else if (exmode_active == EXMODE_NORMAL)
8179 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008180 }
8181 else
8182 {
8183 buf[0] = 'n';
8184 if (finish_op)
8185 buf[1] = 'o';
8186 }
8187
8188 /* Clear out the minor mode when the argument is not a non-zero number or
8189 * non-empty string. */
8190 if (!non_zero_arg(&argvars[0]))
8191 buf[1] = NUL;
8192
8193 rettv->vval.v_string = vim_strsave(buf);
8194 rettv->v_type = VAR_STRING;
8195}
8196
8197#if defined(FEAT_MZSCHEME) || defined(PROTO)
8198/*
8199 * "mzeval()" function
8200 */
8201 static void
8202f_mzeval(typval_T *argvars, typval_T *rettv)
8203{
8204 char_u *str;
8205 char_u buf[NUMBUFLEN];
8206
8207 str = get_tv_string_buf(&argvars[0], buf);
8208 do_mzeval(str, rettv);
8209}
8210
8211 void
8212mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8213{
8214 typval_T argvars[3];
8215
8216 argvars[0].v_type = VAR_STRING;
8217 argvars[0].vval.v_string = name;
8218 copy_tv(args, &argvars[1]);
8219 argvars[2].v_type = VAR_UNKNOWN;
8220 f_call(argvars, rettv);
8221 clear_tv(&argvars[1]);
8222}
8223#endif
8224
8225/*
8226 * "nextnonblank()" function
8227 */
8228 static void
8229f_nextnonblank(typval_T *argvars, typval_T *rettv)
8230{
8231 linenr_T lnum;
8232
8233 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8234 {
8235 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8236 {
8237 lnum = 0;
8238 break;
8239 }
8240 if (*skipwhite(ml_get(lnum)) != NUL)
8241 break;
8242 }
8243 rettv->vval.v_number = lnum;
8244}
8245
8246/*
8247 * "nr2char()" function
8248 */
8249 static void
8250f_nr2char(typval_T *argvars, typval_T *rettv)
8251{
8252 char_u buf[NUMBUFLEN];
8253
8254#ifdef FEAT_MBYTE
8255 if (has_mbyte)
8256 {
8257 int utf8 = 0;
8258
8259 if (argvars[1].v_type != VAR_UNKNOWN)
8260 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8261 if (utf8)
8262 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8263 else
8264 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8265 }
8266 else
8267#endif
8268 {
8269 buf[0] = (char_u)get_tv_number(&argvars[0]);
8270 buf[1] = NUL;
8271 }
8272 rettv->v_type = VAR_STRING;
8273 rettv->vval.v_string = vim_strsave(buf);
8274}
8275
8276/*
8277 * "or(expr, expr)" function
8278 */
8279 static void
8280f_or(typval_T *argvars, typval_T *rettv)
8281{
8282 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8283 | get_tv_number_chk(&argvars[1], NULL);
8284}
8285
8286/*
8287 * "pathshorten()" function
8288 */
8289 static void
8290f_pathshorten(typval_T *argvars, typval_T *rettv)
8291{
8292 char_u *p;
8293
8294 rettv->v_type = VAR_STRING;
8295 p = get_tv_string_chk(&argvars[0]);
8296 if (p == NULL)
8297 rettv->vval.v_string = NULL;
8298 else
8299 {
8300 p = vim_strsave(p);
8301 rettv->vval.v_string = p;
8302 if (p != NULL)
8303 shorten_dir(p);
8304 }
8305}
8306
8307#ifdef FEAT_PERL
8308/*
8309 * "perleval()" function
8310 */
8311 static void
8312f_perleval(typval_T *argvars, typval_T *rettv)
8313{
8314 char_u *str;
8315 char_u buf[NUMBUFLEN];
8316
8317 str = get_tv_string_buf(&argvars[0], buf);
8318 do_perleval(str, rettv);
8319}
8320#endif
8321
8322#ifdef FEAT_FLOAT
8323/*
8324 * "pow()" function
8325 */
8326 static void
8327f_pow(typval_T *argvars, typval_T *rettv)
8328{
8329 float_T fx = 0.0, fy = 0.0;
8330
8331 rettv->v_type = VAR_FLOAT;
8332 if (get_float_arg(argvars, &fx) == OK
8333 && get_float_arg(&argvars[1], &fy) == OK)
8334 rettv->vval.v_float = pow(fx, fy);
8335 else
8336 rettv->vval.v_float = 0.0;
8337}
8338#endif
8339
8340/*
8341 * "prevnonblank()" function
8342 */
8343 static void
8344f_prevnonblank(typval_T *argvars, typval_T *rettv)
8345{
8346 linenr_T lnum;
8347
8348 lnum = get_tv_lnum(argvars);
8349 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8350 lnum = 0;
8351 else
8352 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8353 --lnum;
8354 rettv->vval.v_number = lnum;
8355}
8356
8357/* This dummy va_list is here because:
8358 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8359 * - locally in the function results in a "used before set" warning
8360 * - using va_start() to initialize it gives "function with fixed args" error */
8361static va_list ap;
8362
8363/*
8364 * "printf()" function
8365 */
8366 static void
8367f_printf(typval_T *argvars, typval_T *rettv)
8368{
8369 char_u buf[NUMBUFLEN];
8370 int len;
8371 char_u *s;
8372 int saved_did_emsg = did_emsg;
8373 char *fmt;
8374
8375 rettv->v_type = VAR_STRING;
8376 rettv->vval.v_string = NULL;
8377
8378 /* Get the required length, allocate the buffer and do it for real. */
8379 did_emsg = FALSE;
8380 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008381 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008382 if (!did_emsg)
8383 {
8384 s = alloc(len + 1);
8385 if (s != NULL)
8386 {
8387 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008388 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8389 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008390 }
8391 }
8392 did_emsg |= saved_did_emsg;
8393}
8394
Bram Moolenaarf2732452018-06-03 14:47:35 +02008395#ifdef FEAT_JOB_CHANNEL
8396/*
8397 * "prompt_setcallback({buffer}, {callback})" function
8398 */
8399 static void
8400f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8401{
8402 buf_T *buf;
8403 char_u *callback;
8404 partial_T *partial;
8405
8406 if (check_secure())
8407 return;
8408 buf = get_buf_tv(&argvars[0], FALSE);
8409 if (buf == NULL)
8410 return;
8411
8412 callback = get_callback(&argvars[1], &partial);
8413 if (callback == NULL)
8414 return;
8415
8416 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8417 if (partial == NULL)
8418 buf->b_prompt_callback = vim_strsave(callback);
8419 else
8420 /* pointer into the partial */
8421 buf->b_prompt_callback = callback;
8422 buf->b_prompt_partial = partial;
8423}
8424
8425/*
8426 * "prompt_setprompt({buffer}, {text})" function
8427 */
8428 static void
8429f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8430{
8431 buf_T *buf;
8432 char_u *text;
8433
8434 if (check_secure())
8435 return;
8436 buf = get_buf_tv(&argvars[0], FALSE);
8437 if (buf == NULL)
8438 return;
8439
8440 text = get_tv_string(&argvars[1]);
8441 vim_free(buf->b_prompt_text);
8442 buf->b_prompt_text = vim_strsave(text);
8443}
8444#endif
8445
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008446/*
8447 * "pumvisible()" function
8448 */
8449 static void
8450f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8451{
8452#ifdef FEAT_INS_EXPAND
8453 if (pum_visible())
8454 rettv->vval.v_number = 1;
8455#endif
8456}
8457
8458#ifdef FEAT_PYTHON3
8459/*
8460 * "py3eval()" function
8461 */
8462 static void
8463f_py3eval(typval_T *argvars, typval_T *rettv)
8464{
8465 char_u *str;
8466 char_u buf[NUMBUFLEN];
8467
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008468 if (p_pyx == 0)
8469 p_pyx = 3;
8470
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008471 str = get_tv_string_buf(&argvars[0], buf);
8472 do_py3eval(str, rettv);
8473}
8474#endif
8475
8476#ifdef FEAT_PYTHON
8477/*
8478 * "pyeval()" function
8479 */
8480 static void
8481f_pyeval(typval_T *argvars, typval_T *rettv)
8482{
8483 char_u *str;
8484 char_u buf[NUMBUFLEN];
8485
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008486 if (p_pyx == 0)
8487 p_pyx = 2;
8488
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008489 str = get_tv_string_buf(&argvars[0], buf);
8490 do_pyeval(str, rettv);
8491}
8492#endif
8493
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008494#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8495/*
8496 * "pyxeval()" function
8497 */
8498 static void
8499f_pyxeval(typval_T *argvars, typval_T *rettv)
8500{
8501# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8502 init_pyxversion();
8503 if (p_pyx == 2)
8504 f_pyeval(argvars, rettv);
8505 else
8506 f_py3eval(argvars, rettv);
8507# elif defined(FEAT_PYTHON)
8508 f_pyeval(argvars, rettv);
8509# elif defined(FEAT_PYTHON3)
8510 f_py3eval(argvars, rettv);
8511# endif
8512}
8513#endif
8514
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008515/*
8516 * "range()" function
8517 */
8518 static void
8519f_range(typval_T *argvars, typval_T *rettv)
8520{
8521 varnumber_T start;
8522 varnumber_T end;
8523 varnumber_T stride = 1;
8524 varnumber_T i;
8525 int error = FALSE;
8526
8527 start = get_tv_number_chk(&argvars[0], &error);
8528 if (argvars[1].v_type == VAR_UNKNOWN)
8529 {
8530 end = start - 1;
8531 start = 0;
8532 }
8533 else
8534 {
8535 end = get_tv_number_chk(&argvars[1], &error);
8536 if (argvars[2].v_type != VAR_UNKNOWN)
8537 stride = get_tv_number_chk(&argvars[2], &error);
8538 }
8539
8540 if (error)
8541 return; /* type error; errmsg already given */
8542 if (stride == 0)
8543 EMSG(_("E726: Stride is zero"));
8544 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8545 EMSG(_("E727: Start past end"));
8546 else
8547 {
8548 if (rettv_list_alloc(rettv) == OK)
8549 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8550 if (list_append_number(rettv->vval.v_list,
8551 (varnumber_T)i) == FAIL)
8552 break;
8553 }
8554}
8555
8556/*
8557 * "readfile()" function
8558 */
8559 static void
8560f_readfile(typval_T *argvars, typval_T *rettv)
8561{
8562 int binary = FALSE;
8563 int failed = FALSE;
8564 char_u *fname;
8565 FILE *fd;
8566 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8567 int io_size = sizeof(buf);
8568 int readlen; /* size of last fread() */
8569 char_u *prev = NULL; /* previously read bytes, if any */
8570 long prevlen = 0; /* length of data in prev */
8571 long prevsize = 0; /* size of prev buffer */
8572 long maxline = MAXLNUM;
8573 long cnt = 0;
8574 char_u *p; /* position in buf */
8575 char_u *start; /* start of current line */
8576
8577 if (argvars[1].v_type != VAR_UNKNOWN)
8578 {
8579 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8580 binary = TRUE;
8581 if (argvars[2].v_type != VAR_UNKNOWN)
8582 maxline = (long)get_tv_number(&argvars[2]);
8583 }
8584
8585 if (rettv_list_alloc(rettv) == FAIL)
8586 return;
8587
8588 /* Always open the file in binary mode, library functions have a mind of
8589 * their own about CR-LF conversion. */
8590 fname = get_tv_string(&argvars[0]);
8591 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8592 {
8593 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8594 return;
8595 }
8596
8597 while (cnt < maxline || maxline < 0)
8598 {
8599 readlen = (int)fread(buf, 1, io_size, fd);
8600
8601 /* This for loop processes what was read, but is also entered at end
8602 * of file so that either:
8603 * - an incomplete line gets written
8604 * - a "binary" file gets an empty line at the end if it ends in a
8605 * newline. */
8606 for (p = buf, start = buf;
8607 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8608 ++p)
8609 {
8610 if (*p == '\n' || readlen <= 0)
8611 {
8612 listitem_T *li;
8613 char_u *s = NULL;
8614 long_u len = p - start;
8615
8616 /* Finished a line. Remove CRs before NL. */
8617 if (readlen > 0 && !binary)
8618 {
8619 while (len > 0 && start[len - 1] == '\r')
8620 --len;
8621 /* removal may cross back to the "prev" string */
8622 if (len == 0)
8623 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8624 --prevlen;
8625 }
8626 if (prevlen == 0)
8627 s = vim_strnsave(start, (int)len);
8628 else
8629 {
8630 /* Change "prev" buffer to be the right size. This way
8631 * the bytes are only copied once, and very long lines are
8632 * allocated only once. */
8633 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8634 {
8635 mch_memmove(s + prevlen, start, len);
8636 s[prevlen + len] = NUL;
8637 prev = NULL; /* the list will own the string */
8638 prevlen = prevsize = 0;
8639 }
8640 }
8641 if (s == NULL)
8642 {
8643 do_outofmem_msg((long_u) prevlen + len + 1);
8644 failed = TRUE;
8645 break;
8646 }
8647
8648 if ((li = listitem_alloc()) == NULL)
8649 {
8650 vim_free(s);
8651 failed = TRUE;
8652 break;
8653 }
8654 li->li_tv.v_type = VAR_STRING;
8655 li->li_tv.v_lock = 0;
8656 li->li_tv.vval.v_string = s;
8657 list_append(rettv->vval.v_list, li);
8658
8659 start = p + 1; /* step over newline */
8660 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8661 break;
8662 }
8663 else if (*p == NUL)
8664 *p = '\n';
8665#ifdef FEAT_MBYTE
8666 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8667 * when finding the BF and check the previous two bytes. */
8668 else if (*p == 0xbf && enc_utf8 && !binary)
8669 {
8670 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8671 * + 1, these may be in the "prev" string. */
8672 char_u back1 = p >= buf + 1 ? p[-1]
8673 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8674 char_u back2 = p >= buf + 2 ? p[-2]
8675 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8676 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8677
8678 if (back2 == 0xef && back1 == 0xbb)
8679 {
8680 char_u *dest = p - 2;
8681
8682 /* Usually a BOM is at the beginning of a file, and so at
8683 * the beginning of a line; then we can just step over it.
8684 */
8685 if (start == dest)
8686 start = p + 1;
8687 else
8688 {
8689 /* have to shuffle buf to close gap */
8690 int adjust_prevlen = 0;
8691
8692 if (dest < buf)
8693 {
8694 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8695 dest = buf;
8696 }
8697 if (readlen > p - buf + 1)
8698 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8699 readlen -= 3 - adjust_prevlen;
8700 prevlen -= adjust_prevlen;
8701 p = dest - 1;
8702 }
8703 }
8704 }
8705#endif
8706 } /* for */
8707
8708 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8709 break;
8710 if (start < p)
8711 {
8712 /* There's part of a line in buf, store it in "prev". */
8713 if (p - start + prevlen >= prevsize)
8714 {
8715 /* need bigger "prev" buffer */
8716 char_u *newprev;
8717
8718 /* A common use case is ordinary text files and "prev" gets a
8719 * fragment of a line, so the first allocation is made
8720 * small, to avoid repeatedly 'allocing' large and
8721 * 'reallocing' small. */
8722 if (prevsize == 0)
8723 prevsize = (long)(p - start);
8724 else
8725 {
8726 long grow50pc = (prevsize * 3) / 2;
8727 long growmin = (long)((p - start) * 2 + prevlen);
8728 prevsize = grow50pc > growmin ? grow50pc : growmin;
8729 }
8730 newprev = prev == NULL ? alloc(prevsize)
8731 : vim_realloc(prev, prevsize);
8732 if (newprev == NULL)
8733 {
8734 do_outofmem_msg((long_u)prevsize);
8735 failed = TRUE;
8736 break;
8737 }
8738 prev = newprev;
8739 }
8740 /* Add the line part to end of "prev". */
8741 mch_memmove(prev + prevlen, start, p - start);
8742 prevlen += (long)(p - start);
8743 }
8744 } /* while */
8745
8746 /*
8747 * For a negative line count use only the lines at the end of the file,
8748 * free the rest.
8749 */
8750 if (!failed && maxline < 0)
8751 while (cnt > -maxline)
8752 {
8753 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8754 --cnt;
8755 }
8756
8757 if (failed)
8758 {
8759 list_free(rettv->vval.v_list);
8760 /* readfile doc says an empty list is returned on error */
8761 rettv->vval.v_list = list_alloc();
8762 }
8763
8764 vim_free(prev);
8765 fclose(fd);
8766}
8767
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008768 static void
8769return_register(int regname, typval_T *rettv)
8770{
8771 char_u buf[2] = {0, 0};
8772
8773 buf[0] = (char_u)regname;
8774 rettv->v_type = VAR_STRING;
8775 rettv->vval.v_string = vim_strsave(buf);
8776}
8777
8778/*
8779 * "reg_executing()" function
8780 */
8781 static void
8782f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
8783{
8784 return_register(reg_executing, rettv);
8785}
8786
8787/*
8788 * "reg_recording()" function
8789 */
8790 static void
8791f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
8792{
8793 return_register(reg_recording, rettv);
8794}
8795
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796#if defined(FEAT_RELTIME)
8797static int list2proftime(typval_T *arg, proftime_T *tm);
8798
8799/*
8800 * Convert a List to proftime_T.
8801 * Return FAIL when there is something wrong.
8802 */
8803 static int
8804list2proftime(typval_T *arg, proftime_T *tm)
8805{
8806 long n1, n2;
8807 int error = FALSE;
8808
8809 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8810 || arg->vval.v_list->lv_len != 2)
8811 return FAIL;
8812 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8813 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8814# ifdef WIN3264
8815 tm->HighPart = n1;
8816 tm->LowPart = n2;
8817# else
8818 tm->tv_sec = n1;
8819 tm->tv_usec = n2;
8820# endif
8821 return error ? FAIL : OK;
8822}
8823#endif /* FEAT_RELTIME */
8824
8825/*
8826 * "reltime()" function
8827 */
8828 static void
8829f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8830{
8831#ifdef FEAT_RELTIME
8832 proftime_T res;
8833 proftime_T start;
8834
8835 if (argvars[0].v_type == VAR_UNKNOWN)
8836 {
8837 /* No arguments: get current time. */
8838 profile_start(&res);
8839 }
8840 else if (argvars[1].v_type == VAR_UNKNOWN)
8841 {
8842 if (list2proftime(&argvars[0], &res) == FAIL)
8843 return;
8844 profile_end(&res);
8845 }
8846 else
8847 {
8848 /* Two arguments: compute the difference. */
8849 if (list2proftime(&argvars[0], &start) == FAIL
8850 || list2proftime(&argvars[1], &res) == FAIL)
8851 return;
8852 profile_sub(&res, &start);
8853 }
8854
8855 if (rettv_list_alloc(rettv) == OK)
8856 {
8857 long n1, n2;
8858
8859# ifdef WIN3264
8860 n1 = res.HighPart;
8861 n2 = res.LowPart;
8862# else
8863 n1 = res.tv_sec;
8864 n2 = res.tv_usec;
8865# endif
8866 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8867 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8868 }
8869#endif
8870}
8871
8872#ifdef FEAT_FLOAT
8873/*
8874 * "reltimefloat()" function
8875 */
8876 static void
8877f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8878{
8879# ifdef FEAT_RELTIME
8880 proftime_T tm;
8881# endif
8882
8883 rettv->v_type = VAR_FLOAT;
8884 rettv->vval.v_float = 0;
8885# ifdef FEAT_RELTIME
8886 if (list2proftime(&argvars[0], &tm) == OK)
8887 rettv->vval.v_float = profile_float(&tm);
8888# endif
8889}
8890#endif
8891
8892/*
8893 * "reltimestr()" function
8894 */
8895 static void
8896f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8897{
8898#ifdef FEAT_RELTIME
8899 proftime_T tm;
8900#endif
8901
8902 rettv->v_type = VAR_STRING;
8903 rettv->vval.v_string = NULL;
8904#ifdef FEAT_RELTIME
8905 if (list2proftime(&argvars[0], &tm) == OK)
8906 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8907#endif
8908}
8909
8910#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8911static void make_connection(void);
8912static int check_connection(void);
8913
8914 static void
8915make_connection(void)
8916{
8917 if (X_DISPLAY == NULL
8918# ifdef FEAT_GUI
8919 && !gui.in_use
8920# endif
8921 )
8922 {
8923 x_force_connect = TRUE;
8924 setup_term_clip();
8925 x_force_connect = FALSE;
8926 }
8927}
8928
8929 static int
8930check_connection(void)
8931{
8932 make_connection();
8933 if (X_DISPLAY == NULL)
8934 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008935 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008936 return FAIL;
8937 }
8938 return OK;
8939}
8940#endif
8941
8942#ifdef FEAT_CLIENTSERVER
8943 static void
8944remote_common(typval_T *argvars, typval_T *rettv, int expr)
8945{
8946 char_u *server_name;
8947 char_u *keys;
8948 char_u *r = NULL;
8949 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008950 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008951# ifdef WIN32
8952 HWND w;
8953# else
8954 Window w;
8955# endif
8956
8957 if (check_restricted() || check_secure())
8958 return;
8959
8960# ifdef FEAT_X11
8961 if (check_connection() == FAIL)
8962 return;
8963# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008964 if (argvars[2].v_type != VAR_UNKNOWN
8965 && argvars[3].v_type != VAR_UNKNOWN)
8966 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008967
8968 server_name = get_tv_string_chk(&argvars[0]);
8969 if (server_name == NULL)
8970 return; /* type error; errmsg already given */
8971 keys = get_tv_string_buf(&argvars[1], buf);
8972# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008973 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008974# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008975 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8976 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008977# endif
8978 {
8979 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008980 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008981 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008982 vim_free(r);
8983 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008984 else
8985 EMSG2(_("E241: Unable to send to %s"), server_name);
8986 return;
8987 }
8988
8989 rettv->vval.v_string = r;
8990
8991 if (argvars[2].v_type != VAR_UNKNOWN)
8992 {
8993 dictitem_T v;
8994 char_u str[30];
8995 char_u *idvar;
8996
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008997 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008998 if (idvar != NULL && *idvar != NUL)
8999 {
9000 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9001 v.di_tv.v_type = VAR_STRING;
9002 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009003 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009004 vim_free(v.di_tv.vval.v_string);
9005 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009006 }
9007}
9008#endif
9009
9010/*
9011 * "remote_expr()" function
9012 */
9013 static void
9014f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9015{
9016 rettv->v_type = VAR_STRING;
9017 rettv->vval.v_string = NULL;
9018#ifdef FEAT_CLIENTSERVER
9019 remote_common(argvars, rettv, TRUE);
9020#endif
9021}
9022
9023/*
9024 * "remote_foreground()" function
9025 */
9026 static void
9027f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9028{
9029#ifdef FEAT_CLIENTSERVER
9030# ifdef WIN32
9031 /* On Win32 it's done in this application. */
9032 {
9033 char_u *server_name = get_tv_string_chk(&argvars[0]);
9034
9035 if (server_name != NULL)
9036 serverForeground(server_name);
9037 }
9038# else
9039 /* Send a foreground() expression to the server. */
9040 argvars[1].v_type = VAR_STRING;
9041 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9042 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009043 rettv->v_type = VAR_STRING;
9044 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009045 remote_common(argvars, rettv, TRUE);
9046 vim_free(argvars[1].vval.v_string);
9047# endif
9048#endif
9049}
9050
9051 static void
9052f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9053{
9054#ifdef FEAT_CLIENTSERVER
9055 dictitem_T v;
9056 char_u *s = NULL;
9057# ifdef WIN32
9058 long_u n = 0;
9059# endif
9060 char_u *serverid;
9061
9062 if (check_restricted() || check_secure())
9063 {
9064 rettv->vval.v_number = -1;
9065 return;
9066 }
9067 serverid = get_tv_string_chk(&argvars[0]);
9068 if (serverid == NULL)
9069 {
9070 rettv->vval.v_number = -1;
9071 return; /* type error; errmsg already given */
9072 }
9073# ifdef WIN32
9074 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9075 if (n == 0)
9076 rettv->vval.v_number = -1;
9077 else
9078 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009079 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009080 rettv->vval.v_number = (s != NULL);
9081 }
9082# else
9083 if (check_connection() == FAIL)
9084 return;
9085
9086 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9087 serverStrToWin(serverid), &s);
9088# endif
9089
9090 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9091 {
9092 char_u *retvar;
9093
9094 v.di_tv.v_type = VAR_STRING;
9095 v.di_tv.vval.v_string = vim_strsave(s);
9096 retvar = get_tv_string_chk(&argvars[1]);
9097 if (retvar != NULL)
9098 set_var(retvar, &v.di_tv, FALSE);
9099 vim_free(v.di_tv.vval.v_string);
9100 }
9101#else
9102 rettv->vval.v_number = -1;
9103#endif
9104}
9105
9106 static void
9107f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9108{
9109 char_u *r = NULL;
9110
9111#ifdef FEAT_CLIENTSERVER
9112 char_u *serverid = get_tv_string_chk(&argvars[0]);
9113
9114 if (serverid != NULL && !check_restricted() && !check_secure())
9115 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009116 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009117# ifdef WIN32
9118 /* The server's HWND is encoded in the 'id' parameter */
9119 long_u n = 0;
9120# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009121
9122 if (argvars[1].v_type != VAR_UNKNOWN)
9123 timeout = get_tv_number(&argvars[1]);
9124
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009125# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009126 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9127 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009128 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009129 if (r == NULL)
9130# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009131 if (check_connection() == FAIL
9132 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9133 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009134# endif
9135 EMSG(_("E277: Unable to read a server reply"));
9136 }
9137#endif
9138 rettv->v_type = VAR_STRING;
9139 rettv->vval.v_string = r;
9140}
9141
9142/*
9143 * "remote_send()" function
9144 */
9145 static void
9146f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9147{
9148 rettv->v_type = VAR_STRING;
9149 rettv->vval.v_string = NULL;
9150#ifdef FEAT_CLIENTSERVER
9151 remote_common(argvars, rettv, FALSE);
9152#endif
9153}
9154
9155/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009156 * "remote_startserver()" function
9157 */
9158 static void
9159f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9160{
9161#ifdef FEAT_CLIENTSERVER
9162 char_u *server = get_tv_string_chk(&argvars[0]);
9163
9164 if (server == NULL)
9165 return; /* type error; errmsg already given */
9166 if (serverName != NULL)
9167 EMSG(_("E941: already started a server"));
9168 else
9169 {
9170# ifdef FEAT_X11
9171 if (check_connection() == OK)
9172 serverRegisterName(X_DISPLAY, server);
9173# else
9174 serverSetName(server);
9175# endif
9176 }
9177#else
9178 EMSG(_("E942: +clientserver feature not available"));
9179#endif
9180}
9181
9182/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009183 * "remove()" function
9184 */
9185 static void
9186f_remove(typval_T *argvars, typval_T *rettv)
9187{
9188 list_T *l;
9189 listitem_T *item, *item2;
9190 listitem_T *li;
9191 long idx;
9192 long end;
9193 char_u *key;
9194 dict_T *d;
9195 dictitem_T *di;
9196 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9197
9198 if (argvars[0].v_type == VAR_DICT)
9199 {
9200 if (argvars[2].v_type != VAR_UNKNOWN)
9201 EMSG2(_(e_toomanyarg), "remove()");
9202 else if ((d = argvars[0].vval.v_dict) != NULL
9203 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9204 {
9205 key = get_tv_string_chk(&argvars[1]);
9206 if (key != NULL)
9207 {
9208 di = dict_find(d, key, -1);
9209 if (di == NULL)
9210 EMSG2(_(e_dictkey), key);
9211 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9212 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9213 {
9214 *rettv = di->di_tv;
9215 init_tv(&di->di_tv);
9216 dictitem_remove(d, di);
9217 }
9218 }
9219 }
9220 }
9221 else if (argvars[0].v_type != VAR_LIST)
9222 EMSG2(_(e_listdictarg), "remove()");
9223 else if ((l = argvars[0].vval.v_list) != NULL
9224 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9225 {
9226 int error = FALSE;
9227
9228 idx = (long)get_tv_number_chk(&argvars[1], &error);
9229 if (error)
9230 ; /* type error: do nothing, errmsg already given */
9231 else if ((item = list_find(l, idx)) == NULL)
9232 EMSGN(_(e_listidx), idx);
9233 else
9234 {
9235 if (argvars[2].v_type == VAR_UNKNOWN)
9236 {
9237 /* Remove one item, return its value. */
9238 vimlist_remove(l, item, item);
9239 *rettv = item->li_tv;
9240 vim_free(item);
9241 }
9242 else
9243 {
9244 /* Remove range of items, return list with values. */
9245 end = (long)get_tv_number_chk(&argvars[2], &error);
9246 if (error)
9247 ; /* type error: do nothing */
9248 else if ((item2 = list_find(l, end)) == NULL)
9249 EMSGN(_(e_listidx), end);
9250 else
9251 {
9252 int cnt = 0;
9253
9254 for (li = item; li != NULL; li = li->li_next)
9255 {
9256 ++cnt;
9257 if (li == item2)
9258 break;
9259 }
9260 if (li == NULL) /* didn't find "item2" after "item" */
9261 EMSG(_(e_invrange));
9262 else
9263 {
9264 vimlist_remove(l, item, item2);
9265 if (rettv_list_alloc(rettv) == OK)
9266 {
9267 l = rettv->vval.v_list;
9268 l->lv_first = item;
9269 l->lv_last = item2;
9270 item->li_prev = NULL;
9271 item2->li_next = NULL;
9272 l->lv_len = cnt;
9273 }
9274 }
9275 }
9276 }
9277 }
9278 }
9279}
9280
9281/*
9282 * "rename({from}, {to})" function
9283 */
9284 static void
9285f_rename(typval_T *argvars, typval_T *rettv)
9286{
9287 char_u buf[NUMBUFLEN];
9288
9289 if (check_restricted() || check_secure())
9290 rettv->vval.v_number = -1;
9291 else
9292 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9293 get_tv_string_buf(&argvars[1], buf));
9294}
9295
9296/*
9297 * "repeat()" function
9298 */
9299 static void
9300f_repeat(typval_T *argvars, typval_T *rettv)
9301{
9302 char_u *p;
9303 int n;
9304 int slen;
9305 int len;
9306 char_u *r;
9307 int i;
9308
9309 n = (int)get_tv_number(&argvars[1]);
9310 if (argvars[0].v_type == VAR_LIST)
9311 {
9312 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9313 while (n-- > 0)
9314 if (list_extend(rettv->vval.v_list,
9315 argvars[0].vval.v_list, NULL) == FAIL)
9316 break;
9317 }
9318 else
9319 {
9320 p = get_tv_string(&argvars[0]);
9321 rettv->v_type = VAR_STRING;
9322 rettv->vval.v_string = NULL;
9323
9324 slen = (int)STRLEN(p);
9325 len = slen * n;
9326 if (len <= 0)
9327 return;
9328
9329 r = alloc(len + 1);
9330 if (r != NULL)
9331 {
9332 for (i = 0; i < n; i++)
9333 mch_memmove(r + i * slen, p, (size_t)slen);
9334 r[len] = NUL;
9335 }
9336
9337 rettv->vval.v_string = r;
9338 }
9339}
9340
9341/*
9342 * "resolve()" function
9343 */
9344 static void
9345f_resolve(typval_T *argvars, typval_T *rettv)
9346{
9347 char_u *p;
9348#ifdef HAVE_READLINK
9349 char_u *buf = NULL;
9350#endif
9351
9352 p = get_tv_string(&argvars[0]);
9353#ifdef FEAT_SHORTCUT
9354 {
9355 char_u *v = NULL;
9356
9357 v = mch_resolve_shortcut(p);
9358 if (v != NULL)
9359 rettv->vval.v_string = v;
9360 else
9361 rettv->vval.v_string = vim_strsave(p);
9362 }
9363#else
9364# ifdef HAVE_READLINK
9365 {
9366 char_u *cpy;
9367 int len;
9368 char_u *remain = NULL;
9369 char_u *q;
9370 int is_relative_to_current = FALSE;
9371 int has_trailing_pathsep = FALSE;
9372 int limit = 100;
9373
9374 p = vim_strsave(p);
9375
9376 if (p[0] == '.' && (vim_ispathsep(p[1])
9377 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9378 is_relative_to_current = TRUE;
9379
9380 len = STRLEN(p);
9381 if (len > 0 && after_pathsep(p, p + len))
9382 {
9383 has_trailing_pathsep = TRUE;
9384 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9385 }
9386
9387 q = getnextcomp(p);
9388 if (*q != NUL)
9389 {
9390 /* Separate the first path component in "p", and keep the
9391 * remainder (beginning with the path separator). */
9392 remain = vim_strsave(q - 1);
9393 q[-1] = NUL;
9394 }
9395
9396 buf = alloc(MAXPATHL + 1);
9397 if (buf == NULL)
9398 goto fail;
9399
9400 for (;;)
9401 {
9402 for (;;)
9403 {
9404 len = readlink((char *)p, (char *)buf, MAXPATHL);
9405 if (len <= 0)
9406 break;
9407 buf[len] = NUL;
9408
9409 if (limit-- == 0)
9410 {
9411 vim_free(p);
9412 vim_free(remain);
9413 EMSG(_("E655: Too many symbolic links (cycle?)"));
9414 rettv->vval.v_string = NULL;
9415 goto fail;
9416 }
9417
9418 /* Ensure that the result will have a trailing path separator
9419 * if the argument has one. */
9420 if (remain == NULL && has_trailing_pathsep)
9421 add_pathsep(buf);
9422
9423 /* Separate the first path component in the link value and
9424 * concatenate the remainders. */
9425 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9426 if (*q != NUL)
9427 {
9428 if (remain == NULL)
9429 remain = vim_strsave(q - 1);
9430 else
9431 {
9432 cpy = concat_str(q - 1, remain);
9433 if (cpy != NULL)
9434 {
9435 vim_free(remain);
9436 remain = cpy;
9437 }
9438 }
9439 q[-1] = NUL;
9440 }
9441
9442 q = gettail(p);
9443 if (q > p && *q == NUL)
9444 {
9445 /* Ignore trailing path separator. */
9446 q[-1] = NUL;
9447 q = gettail(p);
9448 }
9449 if (q > p && !mch_isFullName(buf))
9450 {
9451 /* symlink is relative to directory of argument */
9452 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9453 if (cpy != NULL)
9454 {
9455 STRCPY(cpy, p);
9456 STRCPY(gettail(cpy), buf);
9457 vim_free(p);
9458 p = cpy;
9459 }
9460 }
9461 else
9462 {
9463 vim_free(p);
9464 p = vim_strsave(buf);
9465 }
9466 }
9467
9468 if (remain == NULL)
9469 break;
9470
9471 /* Append the first path component of "remain" to "p". */
9472 q = getnextcomp(remain + 1);
9473 len = q - remain - (*q != NUL);
9474 cpy = vim_strnsave(p, STRLEN(p) + len);
9475 if (cpy != NULL)
9476 {
9477 STRNCAT(cpy, remain, len);
9478 vim_free(p);
9479 p = cpy;
9480 }
9481 /* Shorten "remain". */
9482 if (*q != NUL)
9483 STRMOVE(remain, q - 1);
9484 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009485 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009486 }
9487
9488 /* If the result is a relative path name, make it explicitly relative to
9489 * the current directory if and only if the argument had this form. */
9490 if (!vim_ispathsep(*p))
9491 {
9492 if (is_relative_to_current
9493 && *p != NUL
9494 && !(p[0] == '.'
9495 && (p[1] == NUL
9496 || vim_ispathsep(p[1])
9497 || (p[1] == '.'
9498 && (p[2] == NUL
9499 || vim_ispathsep(p[2]))))))
9500 {
9501 /* Prepend "./". */
9502 cpy = concat_str((char_u *)"./", p);
9503 if (cpy != NULL)
9504 {
9505 vim_free(p);
9506 p = cpy;
9507 }
9508 }
9509 else if (!is_relative_to_current)
9510 {
9511 /* Strip leading "./". */
9512 q = p;
9513 while (q[0] == '.' && vim_ispathsep(q[1]))
9514 q += 2;
9515 if (q > p)
9516 STRMOVE(p, p + 2);
9517 }
9518 }
9519
9520 /* Ensure that the result will have no trailing path separator
9521 * if the argument had none. But keep "/" or "//". */
9522 if (!has_trailing_pathsep)
9523 {
9524 q = p + STRLEN(p);
9525 if (after_pathsep(p, q))
9526 *gettail_sep(p) = NUL;
9527 }
9528
9529 rettv->vval.v_string = p;
9530 }
9531# else
9532 rettv->vval.v_string = vim_strsave(p);
9533# endif
9534#endif
9535
9536 simplify_filename(rettv->vval.v_string);
9537
9538#ifdef HAVE_READLINK
9539fail:
9540 vim_free(buf);
9541#endif
9542 rettv->v_type = VAR_STRING;
9543}
9544
9545/*
9546 * "reverse({list})" function
9547 */
9548 static void
9549f_reverse(typval_T *argvars, typval_T *rettv)
9550{
9551 list_T *l;
9552 listitem_T *li, *ni;
9553
9554 if (argvars[0].v_type != VAR_LIST)
9555 EMSG2(_(e_listarg), "reverse()");
9556 else if ((l = argvars[0].vval.v_list) != NULL
9557 && !tv_check_lock(l->lv_lock,
9558 (char_u *)N_("reverse() argument"), TRUE))
9559 {
9560 li = l->lv_last;
9561 l->lv_first = l->lv_last = NULL;
9562 l->lv_len = 0;
9563 while (li != NULL)
9564 {
9565 ni = li->li_prev;
9566 list_append(l, li);
9567 li = ni;
9568 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009569 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009570 l->lv_idx = l->lv_len - l->lv_idx - 1;
9571 }
9572}
9573
9574#define SP_NOMOVE 0x01 /* don't move cursor */
9575#define SP_REPEAT 0x02 /* repeat to find outer pair */
9576#define SP_RETCOUNT 0x04 /* return matchcount */
9577#define SP_SETPCMARK 0x08 /* set previous context mark */
9578#define SP_START 0x10 /* accept match at start position */
9579#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9580#define SP_END 0x40 /* leave cursor at end of match */
9581#define SP_COLUMN 0x80 /* start at cursor column */
9582
9583static int get_search_arg(typval_T *varp, int *flagsp);
9584
9585/*
9586 * Get flags for a search function.
9587 * Possibly sets "p_ws".
9588 * Returns BACKWARD, FORWARD or zero (for an error).
9589 */
9590 static int
9591get_search_arg(typval_T *varp, int *flagsp)
9592{
9593 int dir = FORWARD;
9594 char_u *flags;
9595 char_u nbuf[NUMBUFLEN];
9596 int mask;
9597
9598 if (varp->v_type != VAR_UNKNOWN)
9599 {
9600 flags = get_tv_string_buf_chk(varp, nbuf);
9601 if (flags == NULL)
9602 return 0; /* type error; errmsg already given */
9603 while (*flags != NUL)
9604 {
9605 switch (*flags)
9606 {
9607 case 'b': dir = BACKWARD; break;
9608 case 'w': p_ws = TRUE; break;
9609 case 'W': p_ws = FALSE; break;
9610 default: mask = 0;
9611 if (flagsp != NULL)
9612 switch (*flags)
9613 {
9614 case 'c': mask = SP_START; break;
9615 case 'e': mask = SP_END; break;
9616 case 'm': mask = SP_RETCOUNT; break;
9617 case 'n': mask = SP_NOMOVE; break;
9618 case 'p': mask = SP_SUBPAT; break;
9619 case 'r': mask = SP_REPEAT; break;
9620 case 's': mask = SP_SETPCMARK; break;
9621 case 'z': mask = SP_COLUMN; break;
9622 }
9623 if (mask == 0)
9624 {
9625 EMSG2(_(e_invarg2), flags);
9626 dir = 0;
9627 }
9628 else
9629 *flagsp |= mask;
9630 }
9631 if (dir == 0)
9632 break;
9633 ++flags;
9634 }
9635 }
9636 return dir;
9637}
9638
9639/*
9640 * Shared by search() and searchpos() functions.
9641 */
9642 static int
9643search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9644{
9645 int flags;
9646 char_u *pat;
9647 pos_T pos;
9648 pos_T save_cursor;
9649 int save_p_ws = p_ws;
9650 int dir;
9651 int retval = 0; /* default: FAIL */
9652 long lnum_stop = 0;
9653 proftime_T tm;
9654#ifdef FEAT_RELTIME
9655 long time_limit = 0;
9656#endif
9657 int options = SEARCH_KEEP;
9658 int subpatnum;
9659
9660 pat = get_tv_string(&argvars[0]);
9661 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9662 if (dir == 0)
9663 goto theend;
9664 flags = *flagsp;
9665 if (flags & SP_START)
9666 options |= SEARCH_START;
9667 if (flags & SP_END)
9668 options |= SEARCH_END;
9669 if (flags & SP_COLUMN)
9670 options |= SEARCH_COL;
9671
9672 /* Optional arguments: line number to stop searching and timeout. */
9673 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9674 {
9675 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9676 if (lnum_stop < 0)
9677 goto theend;
9678#ifdef FEAT_RELTIME
9679 if (argvars[3].v_type != VAR_UNKNOWN)
9680 {
9681 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9682 if (time_limit < 0)
9683 goto theend;
9684 }
9685#endif
9686 }
9687
9688#ifdef FEAT_RELTIME
9689 /* Set the time limit, if there is one. */
9690 profile_setlimit(time_limit, &tm);
9691#endif
9692
9693 /*
9694 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9695 * Check to make sure only those flags are set.
9696 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9697 * flags cannot be set. Check for that condition also.
9698 */
9699 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9700 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9701 {
9702 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9703 goto theend;
9704 }
9705
9706 pos = save_cursor = curwin->w_cursor;
9707 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009708 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009709 if (subpatnum != FAIL)
9710 {
9711 if (flags & SP_SUBPAT)
9712 retval = subpatnum;
9713 else
9714 retval = pos.lnum;
9715 if (flags & SP_SETPCMARK)
9716 setpcmark();
9717 curwin->w_cursor = pos;
9718 if (match_pos != NULL)
9719 {
9720 /* Store the match cursor position */
9721 match_pos->lnum = pos.lnum;
9722 match_pos->col = pos.col + 1;
9723 }
9724 /* "/$" will put the cursor after the end of the line, may need to
9725 * correct that here */
9726 check_cursor();
9727 }
9728
9729 /* If 'n' flag is used: restore cursor position. */
9730 if (flags & SP_NOMOVE)
9731 curwin->w_cursor = save_cursor;
9732 else
9733 curwin->w_set_curswant = TRUE;
9734theend:
9735 p_ws = save_p_ws;
9736
9737 return retval;
9738}
9739
9740#ifdef FEAT_FLOAT
9741
9742/*
9743 * round() is not in C90, use ceil() or floor() instead.
9744 */
9745 float_T
9746vim_round(float_T f)
9747{
9748 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9749}
9750
9751/*
9752 * "round({float})" function
9753 */
9754 static void
9755f_round(typval_T *argvars, typval_T *rettv)
9756{
9757 float_T f = 0.0;
9758
9759 rettv->v_type = VAR_FLOAT;
9760 if (get_float_arg(argvars, &f) == OK)
9761 rettv->vval.v_float = vim_round(f);
9762 else
9763 rettv->vval.v_float = 0.0;
9764}
9765#endif
9766
9767/*
9768 * "screenattr()" function
9769 */
9770 static void
9771f_screenattr(typval_T *argvars, typval_T *rettv)
9772{
9773 int row;
9774 int col;
9775 int c;
9776
9777 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9778 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9779 if (row < 0 || row >= screen_Rows
9780 || col < 0 || col >= screen_Columns)
9781 c = -1;
9782 else
9783 c = ScreenAttrs[LineOffset[row] + col];
9784 rettv->vval.v_number = c;
9785}
9786
9787/*
9788 * "screenchar()" function
9789 */
9790 static void
9791f_screenchar(typval_T *argvars, typval_T *rettv)
9792{
9793 int row;
9794 int col;
9795 int off;
9796 int c;
9797
9798 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9799 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9800 if (row < 0 || row >= screen_Rows
9801 || col < 0 || col >= screen_Columns)
9802 c = -1;
9803 else
9804 {
9805 off = LineOffset[row] + col;
9806#ifdef FEAT_MBYTE
9807 if (enc_utf8 && ScreenLinesUC[off] != 0)
9808 c = ScreenLinesUC[off];
9809 else
9810#endif
9811 c = ScreenLines[off];
9812 }
9813 rettv->vval.v_number = c;
9814}
9815
9816/*
9817 * "screencol()" function
9818 *
9819 * First column is 1 to be consistent with virtcol().
9820 */
9821 static void
9822f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9823{
9824 rettv->vval.v_number = screen_screencol() + 1;
9825}
9826
9827/*
9828 * "screenrow()" function
9829 */
9830 static void
9831f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9832{
9833 rettv->vval.v_number = screen_screenrow() + 1;
9834}
9835
9836/*
9837 * "search()" function
9838 */
9839 static void
9840f_search(typval_T *argvars, typval_T *rettv)
9841{
9842 int flags = 0;
9843
9844 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9845}
9846
9847/*
9848 * "searchdecl()" function
9849 */
9850 static void
9851f_searchdecl(typval_T *argvars, typval_T *rettv)
9852{
9853 int locally = 1;
9854 int thisblock = 0;
9855 int error = FALSE;
9856 char_u *name;
9857
9858 rettv->vval.v_number = 1; /* default: FAIL */
9859
9860 name = get_tv_string_chk(&argvars[0]);
9861 if (argvars[1].v_type != VAR_UNKNOWN)
9862 {
9863 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9864 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9865 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9866 }
9867 if (!error && name != NULL)
9868 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9869 locally, thisblock, SEARCH_KEEP) == FAIL;
9870}
9871
9872/*
9873 * Used by searchpair() and searchpairpos()
9874 */
9875 static int
9876searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9877{
9878 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009879 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009880 int save_p_ws = p_ws;
9881 int dir;
9882 int flags = 0;
9883 char_u nbuf1[NUMBUFLEN];
9884 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009885 int retval = 0; /* default: FAIL */
9886 long lnum_stop = 0;
9887 long time_limit = 0;
9888
9889 /* Get the three pattern arguments: start, middle, end. */
9890 spat = get_tv_string_chk(&argvars[0]);
9891 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9892 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9893 if (spat == NULL || mpat == NULL || epat == NULL)
9894 goto theend; /* type error */
9895
9896 /* Handle the optional fourth argument: flags */
9897 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9898 if (dir == 0)
9899 goto theend;
9900
9901 /* Don't accept SP_END or SP_SUBPAT.
9902 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9903 */
9904 if ((flags & (SP_END | SP_SUBPAT)) != 0
9905 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9906 {
9907 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9908 goto theend;
9909 }
9910
9911 /* Using 'r' implies 'W', otherwise it doesn't work. */
9912 if (flags & SP_REPEAT)
9913 p_ws = FALSE;
9914
9915 /* Optional fifth argument: skip expression */
9916 if (argvars[3].v_type == VAR_UNKNOWN
9917 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009918 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009919 else
9920 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009921 skip = &argvars[4];
9922 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9923 && skip->v_type != VAR_STRING)
9924 {
9925 /* Type error */
9926 goto theend;
9927 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009928 if (argvars[5].v_type != VAR_UNKNOWN)
9929 {
9930 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9931 if (lnum_stop < 0)
9932 goto theend;
9933#ifdef FEAT_RELTIME
9934 if (argvars[6].v_type != VAR_UNKNOWN)
9935 {
9936 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9937 if (time_limit < 0)
9938 goto theend;
9939 }
9940#endif
9941 }
9942 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009943
9944 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9945 match_pos, lnum_stop, time_limit);
9946
9947theend:
9948 p_ws = save_p_ws;
9949
9950 return retval;
9951}
9952
9953/*
9954 * "searchpair()" function
9955 */
9956 static void
9957f_searchpair(typval_T *argvars, typval_T *rettv)
9958{
9959 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9960}
9961
9962/*
9963 * "searchpairpos()" function
9964 */
9965 static void
9966f_searchpairpos(typval_T *argvars, typval_T *rettv)
9967{
9968 pos_T match_pos;
9969 int lnum = 0;
9970 int col = 0;
9971
9972 if (rettv_list_alloc(rettv) == FAIL)
9973 return;
9974
9975 if (searchpair_cmn(argvars, &match_pos) > 0)
9976 {
9977 lnum = match_pos.lnum;
9978 col = match_pos.col;
9979 }
9980
9981 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9982 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9983}
9984
9985/*
9986 * Search for a start/middle/end thing.
9987 * Used by searchpair(), see its documentation for the details.
9988 * Returns 0 or -1 for no match,
9989 */
9990 long
9991do_searchpair(
9992 char_u *spat, /* start pattern */
9993 char_u *mpat, /* middle pattern */
9994 char_u *epat, /* end pattern */
9995 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009996 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009997 int flags, /* SP_SETPCMARK and other SP_ values */
9998 pos_T *match_pos,
9999 linenr_T lnum_stop, /* stop at this line if not zero */
10000 long time_limit UNUSED) /* stop after this many msec */
10001{
10002 char_u *save_cpo;
10003 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10004 long retval = 0;
10005 pos_T pos;
10006 pos_T firstpos;
10007 pos_T foundpos;
10008 pos_T save_cursor;
10009 pos_T save_pos;
10010 int n;
10011 int r;
10012 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010013 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010014 int err;
10015 int options = SEARCH_KEEP;
10016 proftime_T tm;
10017
10018 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10019 save_cpo = p_cpo;
10020 p_cpo = empty_option;
10021
10022#ifdef FEAT_RELTIME
10023 /* Set the time limit, if there is one. */
10024 profile_setlimit(time_limit, &tm);
10025#endif
10026
10027 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10028 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010029 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10030 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010031 if (pat2 == NULL || pat3 == NULL)
10032 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010033 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010034 if (*mpat == NUL)
10035 STRCPY(pat3, pat2);
10036 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010037 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010038 spat, epat, mpat);
10039 if (flags & SP_START)
10040 options |= SEARCH_START;
10041
Bram Moolenaar48570482017-10-30 21:48:41 +010010042 if (skip != NULL)
10043 {
10044 /* Empty string means to not use the skip expression. */
10045 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10046 use_skip = skip->vval.v_string != NULL
10047 && *skip->vval.v_string != NUL;
10048 }
10049
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010050 save_cursor = curwin->w_cursor;
10051 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010052 CLEAR_POS(&firstpos);
10053 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010054 pat = pat3;
10055 for (;;)
10056 {
10057 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010058 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010059 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010060 /* didn't find it or found the first match again: FAIL */
10061 break;
10062
10063 if (firstpos.lnum == 0)
10064 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010065 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010066 {
10067 /* Found the same position again. Can happen with a pattern that
10068 * has "\zs" at the end and searching backwards. Advance one
10069 * character and try again. */
10070 if (dir == BACKWARD)
10071 decl(&pos);
10072 else
10073 incl(&pos);
10074 }
10075 foundpos = pos;
10076
10077 /* clear the start flag to avoid getting stuck here */
10078 options &= ~SEARCH_START;
10079
10080 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010081 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010082 {
10083 save_pos = curwin->w_cursor;
10084 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010085 err = FALSE;
10086 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010087 curwin->w_cursor = save_pos;
10088 if (err)
10089 {
10090 /* Evaluating {skip} caused an error, break here. */
10091 curwin->w_cursor = save_cursor;
10092 retval = -1;
10093 break;
10094 }
10095 if (r)
10096 continue;
10097 }
10098
10099 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10100 {
10101 /* Found end when searching backwards or start when searching
10102 * forward: nested pair. */
10103 ++nest;
10104 pat = pat2; /* nested, don't search for middle */
10105 }
10106 else
10107 {
10108 /* Found end when searching forward or start when searching
10109 * backward: end of (nested) pair; or found middle in outer pair. */
10110 if (--nest == 1)
10111 pat = pat3; /* outer level, search for middle */
10112 }
10113
10114 if (nest == 0)
10115 {
10116 /* Found the match: return matchcount or line number. */
10117 if (flags & SP_RETCOUNT)
10118 ++retval;
10119 else
10120 retval = pos.lnum;
10121 if (flags & SP_SETPCMARK)
10122 setpcmark();
10123 curwin->w_cursor = pos;
10124 if (!(flags & SP_REPEAT))
10125 break;
10126 nest = 1; /* search for next unmatched */
10127 }
10128 }
10129
10130 if (match_pos != NULL)
10131 {
10132 /* Store the match cursor position */
10133 match_pos->lnum = curwin->w_cursor.lnum;
10134 match_pos->col = curwin->w_cursor.col + 1;
10135 }
10136
10137 /* If 'n' flag is used or search failed: restore cursor position. */
10138 if ((flags & SP_NOMOVE) || retval == 0)
10139 curwin->w_cursor = save_cursor;
10140
10141theend:
10142 vim_free(pat2);
10143 vim_free(pat3);
10144 if (p_cpo == empty_option)
10145 p_cpo = save_cpo;
10146 else
10147 /* Darn, evaluating the {skip} expression changed the value. */
10148 free_string_option(save_cpo);
10149
10150 return retval;
10151}
10152
10153/*
10154 * "searchpos()" function
10155 */
10156 static void
10157f_searchpos(typval_T *argvars, typval_T *rettv)
10158{
10159 pos_T match_pos;
10160 int lnum = 0;
10161 int col = 0;
10162 int n;
10163 int flags = 0;
10164
10165 if (rettv_list_alloc(rettv) == FAIL)
10166 return;
10167
10168 n = search_cmn(argvars, &match_pos, &flags);
10169 if (n > 0)
10170 {
10171 lnum = match_pos.lnum;
10172 col = match_pos.col;
10173 }
10174
10175 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10176 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10177 if (flags & SP_SUBPAT)
10178 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10179}
10180
10181 static void
10182f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10183{
10184#ifdef FEAT_CLIENTSERVER
10185 char_u buf[NUMBUFLEN];
10186 char_u *server = get_tv_string_chk(&argvars[0]);
10187 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10188
10189 rettv->vval.v_number = -1;
10190 if (server == NULL || reply == NULL)
10191 return;
10192 if (check_restricted() || check_secure())
10193 return;
10194# ifdef FEAT_X11
10195 if (check_connection() == FAIL)
10196 return;
10197# endif
10198
10199 if (serverSendReply(server, reply) < 0)
10200 {
10201 EMSG(_("E258: Unable to send to client"));
10202 return;
10203 }
10204 rettv->vval.v_number = 0;
10205#else
10206 rettv->vval.v_number = -1;
10207#endif
10208}
10209
10210 static void
10211f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10212{
10213 char_u *r = NULL;
10214
10215#ifdef FEAT_CLIENTSERVER
10216# ifdef WIN32
10217 r = serverGetVimNames();
10218# else
10219 make_connection();
10220 if (X_DISPLAY != NULL)
10221 r = serverGetVimNames(X_DISPLAY);
10222# endif
10223#endif
10224 rettv->v_type = VAR_STRING;
10225 rettv->vval.v_string = r;
10226}
10227
10228/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010229 * Set line or list of lines in buffer "buf".
10230 */
10231 static void
10232set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
10233{
10234 char_u *line = NULL;
10235 list_T *l = NULL;
10236 listitem_T *li = NULL;
10237 long added = 0;
10238 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010239 buf_T *curbuf_save = NULL;
10240 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010241 int is_curbuf = buf == curbuf;
10242
Bram Moolenaar9d954202017-09-04 20:34:19 +020010243 /* When using the current buffer ml_mfp will be set if needed. Useful when
10244 * setline() is used on startup. For other buffers the buffer must be
10245 * loaded. */
10246 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010247 {
10248 rettv->vval.v_number = 1; /* FAIL */
10249 return;
10250 }
10251
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010252 if (!is_curbuf)
10253 {
10254 wininfo_T *wip;
10255
10256 curbuf_save = curbuf;
10257 curwin_save = curwin;
10258 curbuf = buf;
10259 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10260 {
10261 if (wip->wi_win != NULL)
10262 {
10263 curwin = wip->wi_win;
10264 break;
10265 }
10266 }
10267 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010268
10269 lcount = curbuf->b_ml.ml_line_count;
10270
10271 if (lines->v_type == VAR_LIST)
10272 {
10273 l = lines->vval.v_list;
10274 li = l->lv_first;
10275 }
10276 else
10277 line = get_tv_string_chk(lines);
10278
10279 /* default result is zero == OK */
10280 for (;;)
10281 {
10282 if (l != NULL)
10283 {
10284 /* list argument, get next string */
10285 if (li == NULL)
10286 break;
10287 line = get_tv_string_chk(&li->li_tv);
10288 li = li->li_next;
10289 }
10290
10291 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +020010292 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010293 break;
10294
10295 /* When coming here from Insert mode, sync undo, so that this can be
10296 * undone separately from what was previously inserted. */
10297 if (u_sync_once == 2)
10298 {
10299 u_sync_once = 1; /* notify that u_sync() was called */
10300 u_sync(TRUE);
10301 }
10302
10303 if (lnum <= curbuf->b_ml.ml_line_count)
10304 {
10305 /* existing line, replace it */
10306 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10307 {
10308 changed_bytes(lnum, 0);
10309 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10310 check_cursor_col();
10311 rettv->vval.v_number = 0; /* OK */
10312 }
10313 }
10314 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10315 {
10316 /* lnum is one past the last line, append the line */
10317 ++added;
10318 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10319 rettv->vval.v_number = 0; /* OK */
10320 }
10321
10322 if (l == NULL) /* only one string argument */
10323 break;
10324 ++lnum;
10325 }
10326
10327 if (added > 0)
10328 appended_lines_mark(lcount, added);
10329
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010330 if (!is_curbuf)
10331 {
10332 curbuf = curbuf_save;
10333 curwin = curwin_save;
10334 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010335}
10336
10337/*
10338 * "setbufline()" function
10339 */
10340 static void
10341f_setbufline(argvars, rettv)
10342 typval_T *argvars;
10343 typval_T *rettv;
10344{
10345 linenr_T lnum;
10346 buf_T *buf;
10347
10348 buf = get_buf_tv(&argvars[0], FALSE);
10349 if (buf == NULL)
10350 rettv->vval.v_number = 1; /* FAIL */
10351 else
10352 {
10353 lnum = get_tv_lnum_buf(&argvars[1], buf);
10354
10355 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10356 }
10357}
10358
10359/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010360 * "setbufvar()" function
10361 */
10362 static void
10363f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10364{
10365 buf_T *buf;
10366 char_u *varname, *bufvarname;
10367 typval_T *varp;
10368 char_u nbuf[NUMBUFLEN];
10369
10370 if (check_restricted() || check_secure())
10371 return;
10372 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10373 varname = get_tv_string_chk(&argvars[1]);
10374 buf = get_buf_tv(&argvars[0], FALSE);
10375 varp = &argvars[2];
10376
10377 if (buf != NULL && varname != NULL && varp != NULL)
10378 {
10379 if (*varname == '&')
10380 {
10381 long numval;
10382 char_u *strval;
10383 int error = FALSE;
10384 aco_save_T aco;
10385
10386 /* set curbuf to be our buf, temporarily */
10387 aucmd_prepbuf(&aco, buf);
10388
10389 ++varname;
10390 numval = (long)get_tv_number_chk(varp, &error);
10391 strval = get_tv_string_buf_chk(varp, nbuf);
10392 if (!error && strval != NULL)
10393 set_option_value(varname, numval, strval, OPT_LOCAL);
10394
10395 /* reset notion of buffer */
10396 aucmd_restbuf(&aco);
10397 }
10398 else
10399 {
10400 buf_T *save_curbuf = curbuf;
10401
10402 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10403 if (bufvarname != NULL)
10404 {
10405 curbuf = buf;
10406 STRCPY(bufvarname, "b:");
10407 STRCPY(bufvarname + 2, varname);
10408 set_var(bufvarname, varp, TRUE);
10409 vim_free(bufvarname);
10410 curbuf = save_curbuf;
10411 }
10412 }
10413 }
10414}
10415
10416 static void
10417f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10418{
10419 dict_T *d;
10420 dictitem_T *di;
10421 char_u *csearch;
10422
10423 if (argvars[0].v_type != VAR_DICT)
10424 {
10425 EMSG(_(e_dictreq));
10426 return;
10427 }
10428
10429 if ((d = argvars[0].vval.v_dict) != NULL)
10430 {
10431 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10432 if (csearch != NULL)
10433 {
10434#ifdef FEAT_MBYTE
10435 if (enc_utf8)
10436 {
10437 int pcc[MAX_MCO];
10438 int c = utfc_ptr2char(csearch, pcc);
10439
10440 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10441 }
10442 else
10443#endif
10444 set_last_csearch(PTR2CHAR(csearch),
10445 csearch, MB_PTR2LEN(csearch));
10446 }
10447
10448 di = dict_find(d, (char_u *)"forward", -1);
10449 if (di != NULL)
10450 set_csearch_direction((int)get_tv_number(&di->di_tv)
10451 ? FORWARD : BACKWARD);
10452
10453 di = dict_find(d, (char_u *)"until", -1);
10454 if (di != NULL)
10455 set_csearch_until(!!get_tv_number(&di->di_tv));
10456 }
10457}
10458
10459/*
10460 * "setcmdpos()" function
10461 */
10462 static void
10463f_setcmdpos(typval_T *argvars, typval_T *rettv)
10464{
10465 int pos = (int)get_tv_number(&argvars[0]) - 1;
10466
10467 if (pos >= 0)
10468 rettv->vval.v_number = set_cmdline_pos(pos);
10469}
10470
10471/*
10472 * "setfperm({fname}, {mode})" function
10473 */
10474 static void
10475f_setfperm(typval_T *argvars, typval_T *rettv)
10476{
10477 char_u *fname;
10478 char_u modebuf[NUMBUFLEN];
10479 char_u *mode_str;
10480 int i;
10481 int mask;
10482 int mode = 0;
10483
10484 rettv->vval.v_number = 0;
10485 fname = get_tv_string_chk(&argvars[0]);
10486 if (fname == NULL)
10487 return;
10488 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10489 if (mode_str == NULL)
10490 return;
10491 if (STRLEN(mode_str) != 9)
10492 {
10493 EMSG2(_(e_invarg2), mode_str);
10494 return;
10495 }
10496
10497 mask = 1;
10498 for (i = 8; i >= 0; --i)
10499 {
10500 if (mode_str[i] != '-')
10501 mode |= mask;
10502 mask = mask << 1;
10503 }
10504 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10505}
10506
10507/*
10508 * "setline()" function
10509 */
10510 static void
10511f_setline(typval_T *argvars, typval_T *rettv)
10512{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010513 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010514
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010515 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010516}
10517
Bram Moolenaard823fa92016-08-12 16:29:27 +020010518static 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 +020010519
10520/*
10521 * Used by "setqflist()" and "setloclist()" functions
10522 */
10523 static void
10524set_qf_ll_list(
10525 win_T *wp UNUSED,
10526 typval_T *list_arg UNUSED,
10527 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010528 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010529 typval_T *rettv)
10530{
10531#ifdef FEAT_QUICKFIX
10532 static char *e_invact = N_("E927: Invalid action: '%s'");
10533 char_u *act;
10534 int action = 0;
10535#endif
10536
10537 rettv->vval.v_number = -1;
10538
10539#ifdef FEAT_QUICKFIX
10540 if (list_arg->v_type != VAR_LIST)
10541 EMSG(_(e_listreq));
10542 else
10543 {
10544 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010545 dict_T *d = NULL;
10546 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010547
10548 if (action_arg->v_type == VAR_STRING)
10549 {
10550 act = get_tv_string_chk(action_arg);
10551 if (act == NULL)
10552 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010553 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10554 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010555 action = *act;
10556 else
10557 EMSG2(_(e_invact), act);
10558 }
10559 else if (action_arg->v_type == VAR_UNKNOWN)
10560 action = ' ';
10561 else
10562 EMSG(_(e_stringreq));
10563
Bram Moolenaard823fa92016-08-12 16:29:27 +020010564 if (action_arg->v_type != VAR_UNKNOWN
10565 && what_arg->v_type != VAR_UNKNOWN)
10566 {
10567 if (what_arg->v_type == VAR_DICT)
10568 d = what_arg->vval.v_dict;
10569 else
10570 {
10571 EMSG(_(e_dictreq));
10572 valid_dict = FALSE;
10573 }
10574 }
10575
10576 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar8b62e312018-05-13 15:29:04 +020010577 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010578 rettv->vval.v_number = 0;
10579 }
10580#endif
10581}
10582
10583/*
10584 * "setloclist()" function
10585 */
10586 static void
10587f_setloclist(typval_T *argvars, typval_T *rettv)
10588{
10589 win_T *win;
10590
10591 rettv->vval.v_number = -1;
10592
10593 win = find_win_by_nr(&argvars[0], NULL);
10594 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010595 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010596}
10597
10598/*
10599 * "setmatches()" function
10600 */
10601 static void
10602f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10603{
10604#ifdef FEAT_SEARCH_EXTRA
10605 list_T *l;
10606 listitem_T *li;
10607 dict_T *d;
10608 list_T *s = NULL;
10609
10610 rettv->vval.v_number = -1;
10611 if (argvars[0].v_type != VAR_LIST)
10612 {
10613 EMSG(_(e_listreq));
10614 return;
10615 }
10616 if ((l = argvars[0].vval.v_list) != NULL)
10617 {
10618
10619 /* To some extent make sure that we are dealing with a list from
10620 * "getmatches()". */
10621 li = l->lv_first;
10622 while (li != NULL)
10623 {
10624 if (li->li_tv.v_type != VAR_DICT
10625 || (d = li->li_tv.vval.v_dict) == NULL)
10626 {
10627 EMSG(_(e_invarg));
10628 return;
10629 }
10630 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10631 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10632 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10633 && dict_find(d, (char_u *)"priority", -1) != NULL
10634 && dict_find(d, (char_u *)"id", -1) != NULL))
10635 {
10636 EMSG(_(e_invarg));
10637 return;
10638 }
10639 li = li->li_next;
10640 }
10641
10642 clear_matches(curwin);
10643 li = l->lv_first;
10644 while (li != NULL)
10645 {
10646 int i = 0;
10647 char_u buf[5];
10648 dictitem_T *di;
10649 char_u *group;
10650 int priority;
10651 int id;
10652 char_u *conceal;
10653
10654 d = li->li_tv.vval.v_dict;
10655 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10656 {
10657 if (s == NULL)
10658 {
10659 s = list_alloc();
10660 if (s == NULL)
10661 return;
10662 }
10663
10664 /* match from matchaddpos() */
10665 for (i = 1; i < 9; i++)
10666 {
10667 sprintf((char *)buf, (char *)"pos%d", i);
10668 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10669 {
10670 if (di->di_tv.v_type != VAR_LIST)
10671 return;
10672
10673 list_append_tv(s, &di->di_tv);
10674 s->lv_refcount++;
10675 }
10676 else
10677 break;
10678 }
10679 }
10680
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010681 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010682 priority = (int)get_dict_number(d, (char_u *)"priority");
10683 id = (int)get_dict_number(d, (char_u *)"id");
10684 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010685 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010686 : NULL;
10687 if (i == 0)
10688 {
10689 match_add(curwin, group,
10690 get_dict_string(d, (char_u *)"pattern", FALSE),
10691 priority, id, NULL, conceal);
10692 }
10693 else
10694 {
10695 match_add(curwin, group, NULL, priority, id, s, conceal);
10696 list_unref(s);
10697 s = NULL;
10698 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010699 vim_free(group);
10700 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010701
10702 li = li->li_next;
10703 }
10704 rettv->vval.v_number = 0;
10705 }
10706#endif
10707}
10708
10709/*
10710 * "setpos()" function
10711 */
10712 static void
10713f_setpos(typval_T *argvars, typval_T *rettv)
10714{
10715 pos_T pos;
10716 int fnum;
10717 char_u *name;
10718 colnr_T curswant = -1;
10719
10720 rettv->vval.v_number = -1;
10721 name = get_tv_string_chk(argvars);
10722 if (name != NULL)
10723 {
10724 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10725 {
10726 if (--pos.col < 0)
10727 pos.col = 0;
10728 if (name[0] == '.' && name[1] == NUL)
10729 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010730 /* set cursor; "fnum" is ignored */
10731 curwin->w_cursor = pos;
10732 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010733 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010734 curwin->w_curswant = curswant - 1;
10735 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010736 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010737 check_cursor();
10738 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010739 }
10740 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10741 {
10742 /* set mark */
10743 if (setmark_pos(name[1], &pos, fnum) == OK)
10744 rettv->vval.v_number = 0;
10745 }
10746 else
10747 EMSG(_(e_invarg));
10748 }
10749 }
10750}
10751
10752/*
10753 * "setqflist()" function
10754 */
10755 static void
10756f_setqflist(typval_T *argvars, typval_T *rettv)
10757{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010758 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010759}
10760
10761/*
10762 * "setreg()" function
10763 */
10764 static void
10765f_setreg(typval_T *argvars, typval_T *rettv)
10766{
10767 int regname;
10768 char_u *strregname;
10769 char_u *stropt;
10770 char_u *strval;
10771 int append;
10772 char_u yank_type;
10773 long block_len;
10774
10775 block_len = -1;
10776 yank_type = MAUTO;
10777 append = FALSE;
10778
10779 strregname = get_tv_string_chk(argvars);
10780 rettv->vval.v_number = 1; /* FAIL is default */
10781
10782 if (strregname == NULL)
10783 return; /* type error; errmsg already given */
10784 regname = *strregname;
10785 if (regname == 0 || regname == '@')
10786 regname = '"';
10787
10788 if (argvars[2].v_type != VAR_UNKNOWN)
10789 {
10790 stropt = get_tv_string_chk(&argvars[2]);
10791 if (stropt == NULL)
10792 return; /* type error */
10793 for (; *stropt != NUL; ++stropt)
10794 switch (*stropt)
10795 {
10796 case 'a': case 'A': /* append */
10797 append = TRUE;
10798 break;
10799 case 'v': case 'c': /* character-wise selection */
10800 yank_type = MCHAR;
10801 break;
10802 case 'V': case 'l': /* line-wise selection */
10803 yank_type = MLINE;
10804 break;
10805 case 'b': case Ctrl_V: /* block-wise selection */
10806 yank_type = MBLOCK;
10807 if (VIM_ISDIGIT(stropt[1]))
10808 {
10809 ++stropt;
10810 block_len = getdigits(&stropt) - 1;
10811 --stropt;
10812 }
10813 break;
10814 }
10815 }
10816
10817 if (argvars[1].v_type == VAR_LIST)
10818 {
10819 char_u **lstval;
10820 char_u **allocval;
10821 char_u buf[NUMBUFLEN];
10822 char_u **curval;
10823 char_u **curallocval;
10824 list_T *ll = argvars[1].vval.v_list;
10825 listitem_T *li;
10826 int len;
10827
10828 /* If the list is NULL handle like an empty list. */
10829 len = ll == NULL ? 0 : ll->lv_len;
10830
10831 /* First half: use for pointers to result lines; second half: use for
10832 * pointers to allocated copies. */
10833 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10834 if (lstval == NULL)
10835 return;
10836 curval = lstval;
10837 allocval = lstval + len + 2;
10838 curallocval = allocval;
10839
10840 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10841 li = li->li_next)
10842 {
10843 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10844 if (strval == NULL)
10845 goto free_lstval;
10846 if (strval == buf)
10847 {
10848 /* Need to make a copy, next get_tv_string_buf_chk() will
10849 * overwrite the string. */
10850 strval = vim_strsave(buf);
10851 if (strval == NULL)
10852 goto free_lstval;
10853 *curallocval++ = strval;
10854 }
10855 *curval++ = strval;
10856 }
10857 *curval++ = NULL;
10858
10859 write_reg_contents_lst(regname, lstval, -1,
10860 append, yank_type, block_len);
10861free_lstval:
10862 while (curallocval > allocval)
10863 vim_free(*--curallocval);
10864 vim_free(lstval);
10865 }
10866 else
10867 {
10868 strval = get_tv_string_chk(&argvars[1]);
10869 if (strval == NULL)
10870 return;
10871 write_reg_contents_ex(regname, strval, -1,
10872 append, yank_type, block_len);
10873 }
10874 rettv->vval.v_number = 0;
10875}
10876
10877/*
10878 * "settabvar()" function
10879 */
10880 static void
10881f_settabvar(typval_T *argvars, typval_T *rettv)
10882{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010883 tabpage_T *save_curtab;
10884 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010885 char_u *varname, *tabvarname;
10886 typval_T *varp;
10887
10888 rettv->vval.v_number = 0;
10889
10890 if (check_restricted() || check_secure())
10891 return;
10892
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010893 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010894 varname = get_tv_string_chk(&argvars[1]);
10895 varp = &argvars[2];
10896
Bram Moolenaar4033c552017-09-16 20:54:51 +020010897 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010898 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010899 save_curtab = curtab;
10900 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010901
10902 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10903 if (tabvarname != NULL)
10904 {
10905 STRCPY(tabvarname, "t:");
10906 STRCPY(tabvarname + 2, varname);
10907 set_var(tabvarname, varp, TRUE);
10908 vim_free(tabvarname);
10909 }
10910
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010911 /* Restore current tabpage */
10912 if (valid_tabpage(save_curtab))
10913 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010914 }
10915}
10916
10917/*
10918 * "settabwinvar()" function
10919 */
10920 static void
10921f_settabwinvar(typval_T *argvars, typval_T *rettv)
10922{
10923 setwinvar(argvars, rettv, 1);
10924}
10925
10926/*
10927 * "setwinvar()" function
10928 */
10929 static void
10930f_setwinvar(typval_T *argvars, typval_T *rettv)
10931{
10932 setwinvar(argvars, rettv, 0);
10933}
10934
10935#ifdef FEAT_CRYPT
10936/*
10937 * "sha256({string})" function
10938 */
10939 static void
10940f_sha256(typval_T *argvars, typval_T *rettv)
10941{
10942 char_u *p;
10943
10944 p = get_tv_string(&argvars[0]);
10945 rettv->vval.v_string = vim_strsave(
10946 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10947 rettv->v_type = VAR_STRING;
10948}
10949#endif /* FEAT_CRYPT */
10950
10951/*
10952 * "shellescape({string})" function
10953 */
10954 static void
10955f_shellescape(typval_T *argvars, typval_T *rettv)
10956{
Bram Moolenaar20615522017-06-05 18:46:26 +020010957 int do_special = non_zero_arg(&argvars[1]);
10958
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010959 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010960 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010961 rettv->v_type = VAR_STRING;
10962}
10963
10964/*
10965 * shiftwidth() function
10966 */
10967 static void
10968f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10969{
10970 rettv->vval.v_number = get_sw_value(curbuf);
10971}
10972
10973/*
10974 * "simplify()" function
10975 */
10976 static void
10977f_simplify(typval_T *argvars, typval_T *rettv)
10978{
10979 char_u *p;
10980
10981 p = get_tv_string(&argvars[0]);
10982 rettv->vval.v_string = vim_strsave(p);
10983 simplify_filename(rettv->vval.v_string); /* simplify in place */
10984 rettv->v_type = VAR_STRING;
10985}
10986
10987#ifdef FEAT_FLOAT
10988/*
10989 * "sin()" function
10990 */
10991 static void
10992f_sin(typval_T *argvars, typval_T *rettv)
10993{
10994 float_T f = 0.0;
10995
10996 rettv->v_type = VAR_FLOAT;
10997 if (get_float_arg(argvars, &f) == OK)
10998 rettv->vval.v_float = sin(f);
10999 else
11000 rettv->vval.v_float = 0.0;
11001}
11002
11003/*
11004 * "sinh()" function
11005 */
11006 static void
11007f_sinh(typval_T *argvars, typval_T *rettv)
11008{
11009 float_T f = 0.0;
11010
11011 rettv->v_type = VAR_FLOAT;
11012 if (get_float_arg(argvars, &f) == OK)
11013 rettv->vval.v_float = sinh(f);
11014 else
11015 rettv->vval.v_float = 0.0;
11016}
11017#endif
11018
11019static int
11020#ifdef __BORLANDC__
11021 _RTLENTRYF
11022#endif
11023 item_compare(const void *s1, const void *s2);
11024static int
11025#ifdef __BORLANDC__
11026 _RTLENTRYF
11027#endif
11028 item_compare2(const void *s1, const void *s2);
11029
11030/* struct used in the array that's given to qsort() */
11031typedef struct
11032{
11033 listitem_T *item;
11034 int idx;
11035} sortItem_T;
11036
11037/* struct storing information about current sort */
11038typedef struct
11039{
11040 int item_compare_ic;
11041 int item_compare_numeric;
11042 int item_compare_numbers;
11043#ifdef FEAT_FLOAT
11044 int item_compare_float;
11045#endif
11046 char_u *item_compare_func;
11047 partial_T *item_compare_partial;
11048 dict_T *item_compare_selfdict;
11049 int item_compare_func_err;
11050 int item_compare_keep_zero;
11051} sortinfo_T;
11052static sortinfo_T *sortinfo = NULL;
11053static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
11054#define ITEM_COMPARE_FAIL 999
11055
11056/*
11057 * Compare functions for f_sort() and f_uniq() below.
11058 */
11059 static int
11060#ifdef __BORLANDC__
11061_RTLENTRYF
11062#endif
11063item_compare(const void *s1, const void *s2)
11064{
11065 sortItem_T *si1, *si2;
11066 typval_T *tv1, *tv2;
11067 char_u *p1, *p2;
11068 char_u *tofree1 = NULL, *tofree2 = NULL;
11069 int res;
11070 char_u numbuf1[NUMBUFLEN];
11071 char_u numbuf2[NUMBUFLEN];
11072
11073 si1 = (sortItem_T *)s1;
11074 si2 = (sortItem_T *)s2;
11075 tv1 = &si1->item->li_tv;
11076 tv2 = &si2->item->li_tv;
11077
11078 if (sortinfo->item_compare_numbers)
11079 {
11080 varnumber_T v1 = get_tv_number(tv1);
11081 varnumber_T v2 = get_tv_number(tv2);
11082
11083 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11084 }
11085
11086#ifdef FEAT_FLOAT
11087 if (sortinfo->item_compare_float)
11088 {
11089 float_T v1 = get_tv_float(tv1);
11090 float_T v2 = get_tv_float(tv2);
11091
11092 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11093 }
11094#endif
11095
11096 /* tv2string() puts quotes around a string and allocates memory. Don't do
11097 * that for string variables. Use a single quote when comparing with a
11098 * non-string to do what the docs promise. */
11099 if (tv1->v_type == VAR_STRING)
11100 {
11101 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11102 p1 = (char_u *)"'";
11103 else
11104 p1 = tv1->vval.v_string;
11105 }
11106 else
11107 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11108 if (tv2->v_type == VAR_STRING)
11109 {
11110 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11111 p2 = (char_u *)"'";
11112 else
11113 p2 = tv2->vval.v_string;
11114 }
11115 else
11116 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11117 if (p1 == NULL)
11118 p1 = (char_u *)"";
11119 if (p2 == NULL)
11120 p2 = (char_u *)"";
11121 if (!sortinfo->item_compare_numeric)
11122 {
11123 if (sortinfo->item_compare_ic)
11124 res = STRICMP(p1, p2);
11125 else
11126 res = STRCMP(p1, p2);
11127 }
11128 else
11129 {
11130 double n1, n2;
11131 n1 = strtod((char *)p1, (char **)&p1);
11132 n2 = strtod((char *)p2, (char **)&p2);
11133 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11134 }
11135
11136 /* When the result would be zero, compare the item indexes. Makes the
11137 * sort stable. */
11138 if (res == 0 && !sortinfo->item_compare_keep_zero)
11139 res = si1->idx > si2->idx ? 1 : -1;
11140
11141 vim_free(tofree1);
11142 vim_free(tofree2);
11143 return res;
11144}
11145
11146 static int
11147#ifdef __BORLANDC__
11148_RTLENTRYF
11149#endif
11150item_compare2(const void *s1, const void *s2)
11151{
11152 sortItem_T *si1, *si2;
11153 int res;
11154 typval_T rettv;
11155 typval_T argv[3];
11156 int dummy;
11157 char_u *func_name;
11158 partial_T *partial = sortinfo->item_compare_partial;
11159
11160 /* shortcut after failure in previous call; compare all items equal */
11161 if (sortinfo->item_compare_func_err)
11162 return 0;
11163
11164 si1 = (sortItem_T *)s1;
11165 si2 = (sortItem_T *)s2;
11166
11167 if (partial == NULL)
11168 func_name = sortinfo->item_compare_func;
11169 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011170 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011171
11172 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11173 * in the copy without changing the original list items. */
11174 copy_tv(&si1->item->li_tv, &argv[0]);
11175 copy_tv(&si2->item->li_tv, &argv[1]);
11176
11177 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11178 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011179 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011180 partial, sortinfo->item_compare_selfdict);
11181 clear_tv(&argv[0]);
11182 clear_tv(&argv[1]);
11183
11184 if (res == FAIL)
11185 res = ITEM_COMPARE_FAIL;
11186 else
11187 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11188 if (sortinfo->item_compare_func_err)
11189 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11190 clear_tv(&rettv);
11191
11192 /* When the result would be zero, compare the pointers themselves. Makes
11193 * the sort stable. */
11194 if (res == 0 && !sortinfo->item_compare_keep_zero)
11195 res = si1->idx > si2->idx ? 1 : -1;
11196
11197 return res;
11198}
11199
11200/*
11201 * "sort({list})" function
11202 */
11203 static void
11204do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11205{
11206 list_T *l;
11207 listitem_T *li;
11208 sortItem_T *ptrs;
11209 sortinfo_T *old_sortinfo;
11210 sortinfo_T info;
11211 long len;
11212 long i;
11213
11214 /* Pointer to current info struct used in compare function. Save and
11215 * restore the current one for nested calls. */
11216 old_sortinfo = sortinfo;
11217 sortinfo = &info;
11218
11219 if (argvars[0].v_type != VAR_LIST)
11220 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11221 else
11222 {
11223 l = argvars[0].vval.v_list;
11224 if (l == NULL || tv_check_lock(l->lv_lock,
11225 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11226 TRUE))
11227 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011228 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011229
11230 len = list_len(l);
11231 if (len <= 1)
11232 goto theend; /* short list sorts pretty quickly */
11233
11234 info.item_compare_ic = FALSE;
11235 info.item_compare_numeric = FALSE;
11236 info.item_compare_numbers = FALSE;
11237#ifdef FEAT_FLOAT
11238 info.item_compare_float = FALSE;
11239#endif
11240 info.item_compare_func = NULL;
11241 info.item_compare_partial = NULL;
11242 info.item_compare_selfdict = NULL;
11243 if (argvars[1].v_type != VAR_UNKNOWN)
11244 {
11245 /* optional second argument: {func} */
11246 if (argvars[1].v_type == VAR_FUNC)
11247 info.item_compare_func = argvars[1].vval.v_string;
11248 else if (argvars[1].v_type == VAR_PARTIAL)
11249 info.item_compare_partial = argvars[1].vval.v_partial;
11250 else
11251 {
11252 int error = FALSE;
11253
11254 i = (long)get_tv_number_chk(&argvars[1], &error);
11255 if (error)
11256 goto theend; /* type error; errmsg already given */
11257 if (i == 1)
11258 info.item_compare_ic = TRUE;
11259 else if (argvars[1].v_type != VAR_NUMBER)
11260 info.item_compare_func = get_tv_string(&argvars[1]);
11261 else if (i != 0)
11262 {
11263 EMSG(_(e_invarg));
11264 goto theend;
11265 }
11266 if (info.item_compare_func != NULL)
11267 {
11268 if (*info.item_compare_func == NUL)
11269 {
11270 /* empty string means default sort */
11271 info.item_compare_func = NULL;
11272 }
11273 else if (STRCMP(info.item_compare_func, "n") == 0)
11274 {
11275 info.item_compare_func = NULL;
11276 info.item_compare_numeric = TRUE;
11277 }
11278 else if (STRCMP(info.item_compare_func, "N") == 0)
11279 {
11280 info.item_compare_func = NULL;
11281 info.item_compare_numbers = TRUE;
11282 }
11283#ifdef FEAT_FLOAT
11284 else if (STRCMP(info.item_compare_func, "f") == 0)
11285 {
11286 info.item_compare_func = NULL;
11287 info.item_compare_float = TRUE;
11288 }
11289#endif
11290 else if (STRCMP(info.item_compare_func, "i") == 0)
11291 {
11292 info.item_compare_func = NULL;
11293 info.item_compare_ic = TRUE;
11294 }
11295 }
11296 }
11297
11298 if (argvars[2].v_type != VAR_UNKNOWN)
11299 {
11300 /* optional third argument: {dict} */
11301 if (argvars[2].v_type != VAR_DICT)
11302 {
11303 EMSG(_(e_dictreq));
11304 goto theend;
11305 }
11306 info.item_compare_selfdict = argvars[2].vval.v_dict;
11307 }
11308 }
11309
11310 /* Make an array with each entry pointing to an item in the List. */
11311 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11312 if (ptrs == NULL)
11313 goto theend;
11314
11315 i = 0;
11316 if (sort)
11317 {
11318 /* sort(): ptrs will be the list to sort */
11319 for (li = l->lv_first; li != NULL; li = li->li_next)
11320 {
11321 ptrs[i].item = li;
11322 ptrs[i].idx = i;
11323 ++i;
11324 }
11325
11326 info.item_compare_func_err = FALSE;
11327 info.item_compare_keep_zero = FALSE;
11328 /* test the compare function */
11329 if ((info.item_compare_func != NULL
11330 || info.item_compare_partial != NULL)
11331 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11332 == ITEM_COMPARE_FAIL)
11333 EMSG(_("E702: Sort compare function failed"));
11334 else
11335 {
11336 /* Sort the array with item pointers. */
11337 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11338 info.item_compare_func == NULL
11339 && info.item_compare_partial == NULL
11340 ? item_compare : item_compare2);
11341
11342 if (!info.item_compare_func_err)
11343 {
11344 /* Clear the List and append the items in sorted order. */
11345 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11346 l->lv_len = 0;
11347 for (i = 0; i < len; ++i)
11348 list_append(l, ptrs[i].item);
11349 }
11350 }
11351 }
11352 else
11353 {
11354 int (*item_compare_func_ptr)(const void *, const void *);
11355
11356 /* f_uniq(): ptrs will be a stack of items to remove */
11357 info.item_compare_func_err = FALSE;
11358 info.item_compare_keep_zero = TRUE;
11359 item_compare_func_ptr = info.item_compare_func != NULL
11360 || info.item_compare_partial != NULL
11361 ? item_compare2 : item_compare;
11362
11363 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11364 li = li->li_next)
11365 {
11366 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11367 == 0)
11368 ptrs[i++].item = li;
11369 if (info.item_compare_func_err)
11370 {
11371 EMSG(_("E882: Uniq compare function failed"));
11372 break;
11373 }
11374 }
11375
11376 if (!info.item_compare_func_err)
11377 {
11378 while (--i >= 0)
11379 {
11380 li = ptrs[i].item->li_next;
11381 ptrs[i].item->li_next = li->li_next;
11382 if (li->li_next != NULL)
11383 li->li_next->li_prev = ptrs[i].item;
11384 else
11385 l->lv_last = ptrs[i].item;
11386 list_fix_watch(l, li);
11387 listitem_free(li);
11388 l->lv_len--;
11389 }
11390 }
11391 }
11392
11393 vim_free(ptrs);
11394 }
11395theend:
11396 sortinfo = old_sortinfo;
11397}
11398
11399/*
11400 * "sort({list})" function
11401 */
11402 static void
11403f_sort(typval_T *argvars, typval_T *rettv)
11404{
11405 do_sort_uniq(argvars, rettv, TRUE);
11406}
11407
11408/*
11409 * "uniq({list})" function
11410 */
11411 static void
11412f_uniq(typval_T *argvars, typval_T *rettv)
11413{
11414 do_sort_uniq(argvars, rettv, FALSE);
11415}
11416
11417/*
11418 * "soundfold({word})" function
11419 */
11420 static void
11421f_soundfold(typval_T *argvars, typval_T *rettv)
11422{
11423 char_u *s;
11424
11425 rettv->v_type = VAR_STRING;
11426 s = get_tv_string(&argvars[0]);
11427#ifdef FEAT_SPELL
11428 rettv->vval.v_string = eval_soundfold(s);
11429#else
11430 rettv->vval.v_string = vim_strsave(s);
11431#endif
11432}
11433
11434/*
11435 * "spellbadword()" function
11436 */
11437 static void
11438f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11439{
11440 char_u *word = (char_u *)"";
11441 hlf_T attr = HLF_COUNT;
11442 int len = 0;
11443
11444 if (rettv_list_alloc(rettv) == FAIL)
11445 return;
11446
11447#ifdef FEAT_SPELL
11448 if (argvars[0].v_type == VAR_UNKNOWN)
11449 {
11450 /* Find the start and length of the badly spelled word. */
11451 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11452 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011453 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011454 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011455 curwin->w_set_curswant = TRUE;
11456 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011457 }
11458 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11459 {
11460 char_u *str = get_tv_string_chk(&argvars[0]);
11461 int capcol = -1;
11462
11463 if (str != NULL)
11464 {
11465 /* Check the argument for spelling. */
11466 while (*str != NUL)
11467 {
11468 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11469 if (attr != HLF_COUNT)
11470 {
11471 word = str;
11472 break;
11473 }
11474 str += len;
11475 }
11476 }
11477 }
11478#endif
11479
11480 list_append_string(rettv->vval.v_list, word, len);
11481 list_append_string(rettv->vval.v_list, (char_u *)(
11482 attr == HLF_SPB ? "bad" :
11483 attr == HLF_SPR ? "rare" :
11484 attr == HLF_SPL ? "local" :
11485 attr == HLF_SPC ? "caps" :
11486 ""), -1);
11487}
11488
11489/*
11490 * "spellsuggest()" function
11491 */
11492 static void
11493f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11494{
11495#ifdef FEAT_SPELL
11496 char_u *str;
11497 int typeerr = FALSE;
11498 int maxcount;
11499 garray_T ga;
11500 int i;
11501 listitem_T *li;
11502 int need_capital = FALSE;
11503#endif
11504
11505 if (rettv_list_alloc(rettv) == FAIL)
11506 return;
11507
11508#ifdef FEAT_SPELL
11509 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11510 {
11511 str = get_tv_string(&argvars[0]);
11512 if (argvars[1].v_type != VAR_UNKNOWN)
11513 {
11514 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11515 if (maxcount <= 0)
11516 return;
11517 if (argvars[2].v_type != VAR_UNKNOWN)
11518 {
11519 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11520 if (typeerr)
11521 return;
11522 }
11523 }
11524 else
11525 maxcount = 25;
11526
11527 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11528
11529 for (i = 0; i < ga.ga_len; ++i)
11530 {
11531 str = ((char_u **)ga.ga_data)[i];
11532
11533 li = listitem_alloc();
11534 if (li == NULL)
11535 vim_free(str);
11536 else
11537 {
11538 li->li_tv.v_type = VAR_STRING;
11539 li->li_tv.v_lock = 0;
11540 li->li_tv.vval.v_string = str;
11541 list_append(rettv->vval.v_list, li);
11542 }
11543 }
11544 ga_clear(&ga);
11545 }
11546#endif
11547}
11548
11549 static void
11550f_split(typval_T *argvars, typval_T *rettv)
11551{
11552 char_u *str;
11553 char_u *end;
11554 char_u *pat = NULL;
11555 regmatch_T regmatch;
11556 char_u patbuf[NUMBUFLEN];
11557 char_u *save_cpo;
11558 int match;
11559 colnr_T col = 0;
11560 int keepempty = FALSE;
11561 int typeerr = FALSE;
11562
11563 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11564 save_cpo = p_cpo;
11565 p_cpo = (char_u *)"";
11566
11567 str = get_tv_string(&argvars[0]);
11568 if (argvars[1].v_type != VAR_UNKNOWN)
11569 {
11570 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11571 if (pat == NULL)
11572 typeerr = TRUE;
11573 if (argvars[2].v_type != VAR_UNKNOWN)
11574 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11575 }
11576 if (pat == NULL || *pat == NUL)
11577 pat = (char_u *)"[\\x01- ]\\+";
11578
11579 if (rettv_list_alloc(rettv) == FAIL)
11580 return;
11581 if (typeerr)
11582 return;
11583
11584 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11585 if (regmatch.regprog != NULL)
11586 {
11587 regmatch.rm_ic = FALSE;
11588 while (*str != NUL || keepempty)
11589 {
11590 if (*str == NUL)
11591 match = FALSE; /* empty item at the end */
11592 else
11593 match = vim_regexec_nl(&regmatch, str, col);
11594 if (match)
11595 end = regmatch.startp[0];
11596 else
11597 end = str + STRLEN(str);
11598 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11599 && *str != NUL && match && end < regmatch.endp[0]))
11600 {
11601 if (list_append_string(rettv->vval.v_list, str,
11602 (int)(end - str)) == FAIL)
11603 break;
11604 }
11605 if (!match)
11606 break;
11607 /* Advance to just after the match. */
11608 if (regmatch.endp[0] > str)
11609 col = 0;
11610 else
11611 {
11612 /* Don't get stuck at the same match. */
11613#ifdef FEAT_MBYTE
11614 col = (*mb_ptr2len)(regmatch.endp[0]);
11615#else
11616 col = 1;
11617#endif
11618 }
11619 str = regmatch.endp[0];
11620 }
11621
11622 vim_regfree(regmatch.regprog);
11623 }
11624
11625 p_cpo = save_cpo;
11626}
11627
11628#ifdef FEAT_FLOAT
11629/*
11630 * "sqrt()" function
11631 */
11632 static void
11633f_sqrt(typval_T *argvars, typval_T *rettv)
11634{
11635 float_T f = 0.0;
11636
11637 rettv->v_type = VAR_FLOAT;
11638 if (get_float_arg(argvars, &f) == OK)
11639 rettv->vval.v_float = sqrt(f);
11640 else
11641 rettv->vval.v_float = 0.0;
11642}
11643
11644/*
11645 * "str2float()" function
11646 */
11647 static void
11648f_str2float(typval_T *argvars, typval_T *rettv)
11649{
11650 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011651 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011652
Bram Moolenaar08243d22017-01-10 16:12:29 +010011653 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011654 p = skipwhite(p + 1);
11655 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011656 if (isneg)
11657 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011658 rettv->v_type = VAR_FLOAT;
11659}
11660#endif
11661
11662/*
11663 * "str2nr()" function
11664 */
11665 static void
11666f_str2nr(typval_T *argvars, typval_T *rettv)
11667{
11668 int base = 10;
11669 char_u *p;
11670 varnumber_T n;
11671 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011672 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011673
11674 if (argvars[1].v_type != VAR_UNKNOWN)
11675 {
11676 base = (int)get_tv_number(&argvars[1]);
11677 if (base != 2 && base != 8 && base != 10 && base != 16)
11678 {
11679 EMSG(_(e_invarg));
11680 return;
11681 }
11682 }
11683
11684 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011685 isneg = (*p == '-');
11686 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011687 p = skipwhite(p + 1);
11688 switch (base)
11689 {
11690 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11691 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11692 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11693 default: what = 0;
11694 }
11695 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011696 if (isneg)
11697 rettv->vval.v_number = -n;
11698 else
11699 rettv->vval.v_number = n;
11700
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011701}
11702
11703#ifdef HAVE_STRFTIME
11704/*
11705 * "strftime({format}[, {time}])" function
11706 */
11707 static void
11708f_strftime(typval_T *argvars, typval_T *rettv)
11709{
11710 char_u result_buf[256];
11711 struct tm *curtime;
11712 time_t seconds;
11713 char_u *p;
11714
11715 rettv->v_type = VAR_STRING;
11716
11717 p = get_tv_string(&argvars[0]);
11718 if (argvars[1].v_type == VAR_UNKNOWN)
11719 seconds = time(NULL);
11720 else
11721 seconds = (time_t)get_tv_number(&argvars[1]);
11722 curtime = localtime(&seconds);
11723 /* MSVC returns NULL for an invalid value of seconds. */
11724 if (curtime == NULL)
11725 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11726 else
11727 {
11728# ifdef FEAT_MBYTE
11729 vimconv_T conv;
11730 char_u *enc;
11731
11732 conv.vc_type = CONV_NONE;
11733 enc = enc_locale();
11734 convert_setup(&conv, p_enc, enc);
11735 if (conv.vc_type != CONV_NONE)
11736 p = string_convert(&conv, p, NULL);
11737# endif
11738 if (p != NULL)
11739 (void)strftime((char *)result_buf, sizeof(result_buf),
11740 (char *)p, curtime);
11741 else
11742 result_buf[0] = NUL;
11743
11744# ifdef FEAT_MBYTE
11745 if (conv.vc_type != CONV_NONE)
11746 vim_free(p);
11747 convert_setup(&conv, enc, p_enc);
11748 if (conv.vc_type != CONV_NONE)
11749 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11750 else
11751# endif
11752 rettv->vval.v_string = vim_strsave(result_buf);
11753
11754# ifdef FEAT_MBYTE
11755 /* Release conversion descriptors */
11756 convert_setup(&conv, NULL, NULL);
11757 vim_free(enc);
11758# endif
11759 }
11760}
11761#endif
11762
11763/*
11764 * "strgetchar()" function
11765 */
11766 static void
11767f_strgetchar(typval_T *argvars, typval_T *rettv)
11768{
11769 char_u *str;
11770 int len;
11771 int error = FALSE;
11772 int charidx;
11773
11774 rettv->vval.v_number = -1;
11775 str = get_tv_string_chk(&argvars[0]);
11776 if (str == NULL)
11777 return;
11778 len = (int)STRLEN(str);
11779 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11780 if (error)
11781 return;
11782#ifdef FEAT_MBYTE
11783 {
11784 int byteidx = 0;
11785
11786 while (charidx >= 0 && byteidx < len)
11787 {
11788 if (charidx == 0)
11789 {
11790 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11791 break;
11792 }
11793 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011794 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011795 }
11796 }
11797#else
11798 if (charidx < len)
11799 rettv->vval.v_number = str[charidx];
11800#endif
11801}
11802
11803/*
11804 * "stridx()" function
11805 */
11806 static void
11807f_stridx(typval_T *argvars, typval_T *rettv)
11808{
11809 char_u buf[NUMBUFLEN];
11810 char_u *needle;
11811 char_u *haystack;
11812 char_u *save_haystack;
11813 char_u *pos;
11814 int start_idx;
11815
11816 needle = get_tv_string_chk(&argvars[1]);
11817 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11818 rettv->vval.v_number = -1;
11819 if (needle == NULL || haystack == NULL)
11820 return; /* type error; errmsg already given */
11821
11822 if (argvars[2].v_type != VAR_UNKNOWN)
11823 {
11824 int error = FALSE;
11825
11826 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11827 if (error || start_idx >= (int)STRLEN(haystack))
11828 return;
11829 if (start_idx >= 0)
11830 haystack += start_idx;
11831 }
11832
11833 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11834 if (pos != NULL)
11835 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11836}
11837
11838/*
11839 * "string()" function
11840 */
11841 static void
11842f_string(typval_T *argvars, typval_T *rettv)
11843{
11844 char_u *tofree;
11845 char_u numbuf[NUMBUFLEN];
11846
11847 rettv->v_type = VAR_STRING;
11848 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11849 get_copyID());
11850 /* Make a copy if we have a value but it's not in allocated memory. */
11851 if (rettv->vval.v_string != NULL && tofree == NULL)
11852 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11853}
11854
11855/*
11856 * "strlen()" function
11857 */
11858 static void
11859f_strlen(typval_T *argvars, typval_T *rettv)
11860{
11861 rettv->vval.v_number = (varnumber_T)(STRLEN(
11862 get_tv_string(&argvars[0])));
11863}
11864
11865/*
11866 * "strchars()" function
11867 */
11868 static void
11869f_strchars(typval_T *argvars, typval_T *rettv)
11870{
11871 char_u *s = get_tv_string(&argvars[0]);
11872 int skipcc = 0;
11873#ifdef FEAT_MBYTE
11874 varnumber_T len = 0;
11875 int (*func_mb_ptr2char_adv)(char_u **pp);
11876#endif
11877
11878 if (argvars[1].v_type != VAR_UNKNOWN)
11879 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11880 if (skipcc < 0 || skipcc > 1)
11881 EMSG(_(e_invarg));
11882 else
11883 {
11884#ifdef FEAT_MBYTE
11885 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11886 while (*s != NUL)
11887 {
11888 func_mb_ptr2char_adv(&s);
11889 ++len;
11890 }
11891 rettv->vval.v_number = len;
11892#else
11893 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11894#endif
11895 }
11896}
11897
11898/*
11899 * "strdisplaywidth()" function
11900 */
11901 static void
11902f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11903{
11904 char_u *s = get_tv_string(&argvars[0]);
11905 int col = 0;
11906
11907 if (argvars[1].v_type != VAR_UNKNOWN)
11908 col = (int)get_tv_number(&argvars[1]);
11909
11910 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11911}
11912
11913/*
11914 * "strwidth()" function
11915 */
11916 static void
11917f_strwidth(typval_T *argvars, typval_T *rettv)
11918{
11919 char_u *s = get_tv_string(&argvars[0]);
11920
11921 rettv->vval.v_number = (varnumber_T)(
11922#ifdef FEAT_MBYTE
11923 mb_string2cells(s, -1)
11924#else
11925 STRLEN(s)
11926#endif
11927 );
11928}
11929
11930/*
11931 * "strcharpart()" function
11932 */
11933 static void
11934f_strcharpart(typval_T *argvars, typval_T *rettv)
11935{
11936#ifdef FEAT_MBYTE
11937 char_u *p;
11938 int nchar;
11939 int nbyte = 0;
11940 int charlen;
11941 int len = 0;
11942 int slen;
11943 int error = FALSE;
11944
11945 p = get_tv_string(&argvars[0]);
11946 slen = (int)STRLEN(p);
11947
11948 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11949 if (!error)
11950 {
11951 if (nchar > 0)
11952 while (nchar > 0 && nbyte < slen)
11953 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011954 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011955 --nchar;
11956 }
11957 else
11958 nbyte = nchar;
11959 if (argvars[2].v_type != VAR_UNKNOWN)
11960 {
11961 charlen = (int)get_tv_number(&argvars[2]);
11962 while (charlen > 0 && nbyte + len < slen)
11963 {
11964 int off = nbyte + len;
11965
11966 if (off < 0)
11967 len += 1;
11968 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011969 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011970 --charlen;
11971 }
11972 }
11973 else
11974 len = slen - nbyte; /* default: all bytes that are available. */
11975 }
11976
11977 /*
11978 * Only return the overlap between the specified part and the actual
11979 * string.
11980 */
11981 if (nbyte < 0)
11982 {
11983 len += nbyte;
11984 nbyte = 0;
11985 }
11986 else if (nbyte > slen)
11987 nbyte = slen;
11988 if (len < 0)
11989 len = 0;
11990 else if (nbyte + len > slen)
11991 len = slen - nbyte;
11992
11993 rettv->v_type = VAR_STRING;
11994 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11995#else
11996 f_strpart(argvars, rettv);
11997#endif
11998}
11999
12000/*
12001 * "strpart()" function
12002 */
12003 static void
12004f_strpart(typval_T *argvars, typval_T *rettv)
12005{
12006 char_u *p;
12007 int n;
12008 int len;
12009 int slen;
12010 int error = FALSE;
12011
12012 p = get_tv_string(&argvars[0]);
12013 slen = (int)STRLEN(p);
12014
12015 n = (int)get_tv_number_chk(&argvars[1], &error);
12016 if (error)
12017 len = 0;
12018 else if (argvars[2].v_type != VAR_UNKNOWN)
12019 len = (int)get_tv_number(&argvars[2]);
12020 else
12021 len = slen - n; /* default len: all bytes that are available. */
12022
12023 /*
12024 * Only return the overlap between the specified part and the actual
12025 * string.
12026 */
12027 if (n < 0)
12028 {
12029 len += n;
12030 n = 0;
12031 }
12032 else if (n > slen)
12033 n = slen;
12034 if (len < 0)
12035 len = 0;
12036 else if (n + len > slen)
12037 len = slen - n;
12038
12039 rettv->v_type = VAR_STRING;
12040 rettv->vval.v_string = vim_strnsave(p + n, len);
12041}
12042
12043/*
12044 * "strridx()" function
12045 */
12046 static void
12047f_strridx(typval_T *argvars, typval_T *rettv)
12048{
12049 char_u buf[NUMBUFLEN];
12050 char_u *needle;
12051 char_u *haystack;
12052 char_u *rest;
12053 char_u *lastmatch = NULL;
12054 int haystack_len, end_idx;
12055
12056 needle = get_tv_string_chk(&argvars[1]);
12057 haystack = get_tv_string_buf_chk(&argvars[0], buf);
12058
12059 rettv->vval.v_number = -1;
12060 if (needle == NULL || haystack == NULL)
12061 return; /* type error; errmsg already given */
12062
12063 haystack_len = (int)STRLEN(haystack);
12064 if (argvars[2].v_type != VAR_UNKNOWN)
12065 {
12066 /* Third argument: upper limit for index */
12067 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
12068 if (end_idx < 0)
12069 return; /* can never find a match */
12070 }
12071 else
12072 end_idx = haystack_len;
12073
12074 if (*needle == NUL)
12075 {
12076 /* Empty string matches past the end. */
12077 lastmatch = haystack + end_idx;
12078 }
12079 else
12080 {
12081 for (rest = haystack; *rest != '\0'; ++rest)
12082 {
12083 rest = (char_u *)strstr((char *)rest, (char *)needle);
12084 if (rest == NULL || rest > haystack + end_idx)
12085 break;
12086 lastmatch = rest;
12087 }
12088 }
12089
12090 if (lastmatch == NULL)
12091 rettv->vval.v_number = -1;
12092 else
12093 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12094}
12095
12096/*
12097 * "strtrans()" function
12098 */
12099 static void
12100f_strtrans(typval_T *argvars, typval_T *rettv)
12101{
12102 rettv->v_type = VAR_STRING;
12103 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12104}
12105
12106/*
12107 * "submatch()" function
12108 */
12109 static void
12110f_submatch(typval_T *argvars, typval_T *rettv)
12111{
12112 int error = FALSE;
12113 int no;
12114 int retList = 0;
12115
12116 no = (int)get_tv_number_chk(&argvars[0], &error);
12117 if (error)
12118 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012119 if (no < 0 || no >= NSUBEXP)
12120 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012121 EMSGN(_("E935: invalid submatch number: %d"), no);
12122 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012123 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012124 if (argvars[1].v_type != VAR_UNKNOWN)
12125 retList = (int)get_tv_number_chk(&argvars[1], &error);
12126 if (error)
12127 return;
12128
12129 if (retList == 0)
12130 {
12131 rettv->v_type = VAR_STRING;
12132 rettv->vval.v_string = reg_submatch(no);
12133 }
12134 else
12135 {
12136 rettv->v_type = VAR_LIST;
12137 rettv->vval.v_list = reg_submatch_list(no);
12138 }
12139}
12140
12141/*
12142 * "substitute()" function
12143 */
12144 static void
12145f_substitute(typval_T *argvars, typval_T *rettv)
12146{
12147 char_u patbuf[NUMBUFLEN];
12148 char_u subbuf[NUMBUFLEN];
12149 char_u flagsbuf[NUMBUFLEN];
12150
12151 char_u *str = get_tv_string_chk(&argvars[0]);
12152 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012153 char_u *sub = NULL;
12154 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012155 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12156
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012157 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12158 expr = &argvars[2];
12159 else
12160 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12161
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012162 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012163 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12164 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012165 rettv->vval.v_string = NULL;
12166 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012167 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012168}
12169
12170/*
12171 * "synID(lnum, col, trans)" function
12172 */
12173 static void
12174f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12175{
12176 int id = 0;
12177#ifdef FEAT_SYN_HL
12178 linenr_T lnum;
12179 colnr_T col;
12180 int trans;
12181 int transerr = FALSE;
12182
12183 lnum = get_tv_lnum(argvars); /* -1 on type error */
12184 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12185 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12186
12187 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12188 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12189 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12190#endif
12191
12192 rettv->vval.v_number = id;
12193}
12194
12195/*
12196 * "synIDattr(id, what [, mode])" function
12197 */
12198 static void
12199f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12200{
12201 char_u *p = NULL;
12202#ifdef FEAT_SYN_HL
12203 int id;
12204 char_u *what;
12205 char_u *mode;
12206 char_u modebuf[NUMBUFLEN];
12207 int modec;
12208
12209 id = (int)get_tv_number(&argvars[0]);
12210 what = get_tv_string(&argvars[1]);
12211 if (argvars[2].v_type != VAR_UNKNOWN)
12212 {
12213 mode = get_tv_string_buf(&argvars[2], modebuf);
12214 modec = TOLOWER_ASC(mode[0]);
12215 if (modec != 't' && modec != 'c' && modec != 'g')
12216 modec = 0; /* replace invalid with current */
12217 }
12218 else
12219 {
12220#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12221 if (USE_24BIT)
12222 modec = 'g';
12223 else
12224#endif
12225 if (t_colors > 1)
12226 modec = 'c';
12227 else
12228 modec = 't';
12229 }
12230
12231
12232 switch (TOLOWER_ASC(what[0]))
12233 {
12234 case 'b':
12235 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12236 p = highlight_color(id, what, modec);
12237 else /* bold */
12238 p = highlight_has_attr(id, HL_BOLD, modec);
12239 break;
12240
12241 case 'f': /* fg[#] or font */
12242 p = highlight_color(id, what, modec);
12243 break;
12244
12245 case 'i':
12246 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12247 p = highlight_has_attr(id, HL_INVERSE, modec);
12248 else /* italic */
12249 p = highlight_has_attr(id, HL_ITALIC, modec);
12250 break;
12251
12252 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012253 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012254 break;
12255
12256 case 'r': /* reverse */
12257 p = highlight_has_attr(id, HL_INVERSE, modec);
12258 break;
12259
12260 case 's':
12261 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12262 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012263 /* strikeout */
12264 else if (TOLOWER_ASC(what[1]) == 't' &&
12265 TOLOWER_ASC(what[2]) == 'r')
12266 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012267 else /* standout */
12268 p = highlight_has_attr(id, HL_STANDOUT, modec);
12269 break;
12270
12271 case 'u':
12272 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12273 /* underline */
12274 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12275 else
12276 /* undercurl */
12277 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12278 break;
12279 }
12280
12281 if (p != NULL)
12282 p = vim_strsave(p);
12283#endif
12284 rettv->v_type = VAR_STRING;
12285 rettv->vval.v_string = p;
12286}
12287
12288/*
12289 * "synIDtrans(id)" function
12290 */
12291 static void
12292f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12293{
12294 int id;
12295
12296#ifdef FEAT_SYN_HL
12297 id = (int)get_tv_number(&argvars[0]);
12298
12299 if (id > 0)
12300 id = syn_get_final_id(id);
12301 else
12302#endif
12303 id = 0;
12304
12305 rettv->vval.v_number = id;
12306}
12307
12308/*
12309 * "synconcealed(lnum, col)" function
12310 */
12311 static void
12312f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12313{
12314#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12315 linenr_T lnum;
12316 colnr_T col;
12317 int syntax_flags = 0;
12318 int cchar;
12319 int matchid = 0;
12320 char_u str[NUMBUFLEN];
12321#endif
12322
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012323 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012324
12325#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12326 lnum = get_tv_lnum(argvars); /* -1 on type error */
12327 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12328
12329 vim_memset(str, NUL, sizeof(str));
12330
12331 if (rettv_list_alloc(rettv) != FAIL)
12332 {
12333 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12334 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12335 && curwin->w_p_cole > 0)
12336 {
12337 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12338 syntax_flags = get_syntax_info(&matchid);
12339
12340 /* get the conceal character */
12341 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12342 {
12343 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012344 if (cchar == NUL && curwin->w_p_cole == 1)
12345 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012346 if (cchar != NUL)
12347 {
12348# ifdef FEAT_MBYTE
12349 if (has_mbyte)
12350 (*mb_char2bytes)(cchar, str);
12351 else
12352# endif
12353 str[0] = cchar;
12354 }
12355 }
12356 }
12357
12358 list_append_number(rettv->vval.v_list,
12359 (syntax_flags & HL_CONCEAL) != 0);
12360 /* -1 to auto-determine strlen */
12361 list_append_string(rettv->vval.v_list, str, -1);
12362 list_append_number(rettv->vval.v_list, matchid);
12363 }
12364#endif
12365}
12366
12367/*
12368 * "synstack(lnum, col)" function
12369 */
12370 static void
12371f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12372{
12373#ifdef FEAT_SYN_HL
12374 linenr_T lnum;
12375 colnr_T col;
12376 int i;
12377 int id;
12378#endif
12379
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012380 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012381
12382#ifdef FEAT_SYN_HL
12383 lnum = get_tv_lnum(argvars); /* -1 on type error */
12384 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12385
12386 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12387 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12388 && rettv_list_alloc(rettv) != FAIL)
12389 {
12390 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12391 for (i = 0; ; ++i)
12392 {
12393 id = syn_get_stack_item(i);
12394 if (id < 0)
12395 break;
12396 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12397 break;
12398 }
12399 }
12400#endif
12401}
12402
12403 static void
12404get_cmd_output_as_rettv(
12405 typval_T *argvars,
12406 typval_T *rettv,
12407 int retlist)
12408{
12409 char_u *res = NULL;
12410 char_u *p;
12411 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012412 int err = FALSE;
12413 FILE *fd;
12414 list_T *list = NULL;
12415 int flags = SHELL_SILENT;
12416
12417 rettv->v_type = VAR_STRING;
12418 rettv->vval.v_string = NULL;
12419 if (check_restricted() || check_secure())
12420 goto errret;
12421
12422 if (argvars[1].v_type != VAR_UNKNOWN)
12423 {
12424 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012425 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012426 * command.
12427 */
12428 if ((infile = vim_tempname('i', TRUE)) == NULL)
12429 {
12430 EMSG(_(e_notmp));
12431 goto errret;
12432 }
12433
12434 fd = mch_fopen((char *)infile, WRITEBIN);
12435 if (fd == NULL)
12436 {
12437 EMSG2(_(e_notopen), infile);
12438 goto errret;
12439 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012440 if (argvars[1].v_type == VAR_NUMBER)
12441 {
12442 linenr_T lnum;
12443 buf_T *buf;
12444
12445 buf = buflist_findnr(argvars[1].vval.v_number);
12446 if (buf == NULL)
12447 {
12448 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012449 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012450 goto errret;
12451 }
12452
12453 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12454 {
12455 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12456 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12457 {
12458 err = TRUE;
12459 break;
12460 }
12461 if (putc(NL, fd) == EOF)
12462 {
12463 err = TRUE;
12464 break;
12465 }
12466 }
12467 }
12468 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012469 {
12470 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12471 err = TRUE;
12472 }
12473 else
12474 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012475 size_t len;
12476 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012477
12478 p = get_tv_string_buf_chk(&argvars[1], buf);
12479 if (p == NULL)
12480 {
12481 fclose(fd);
12482 goto errret; /* type error; errmsg already given */
12483 }
12484 len = STRLEN(p);
12485 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12486 err = TRUE;
12487 }
12488 if (fclose(fd) != 0)
12489 err = TRUE;
12490 if (err)
12491 {
12492 EMSG(_("E677: Error writing temp file"));
12493 goto errret;
12494 }
12495 }
12496
12497 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12498 * echoes typeahead, that messes up the display. */
12499 if (!msg_silent)
12500 flags += SHELL_COOKED;
12501
12502 if (retlist)
12503 {
12504 int len;
12505 listitem_T *li;
12506 char_u *s = NULL;
12507 char_u *start;
12508 char_u *end;
12509 int i;
12510
12511 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12512 if (res == NULL)
12513 goto errret;
12514
12515 list = list_alloc();
12516 if (list == NULL)
12517 goto errret;
12518
12519 for (i = 0; i < len; ++i)
12520 {
12521 start = res + i;
12522 while (i < len && res[i] != NL)
12523 ++i;
12524 end = res + i;
12525
12526 s = alloc((unsigned)(end - start + 1));
12527 if (s == NULL)
12528 goto errret;
12529
12530 for (p = s; start < end; ++p, ++start)
12531 *p = *start == NUL ? NL : *start;
12532 *p = NUL;
12533
12534 li = listitem_alloc();
12535 if (li == NULL)
12536 {
12537 vim_free(s);
12538 goto errret;
12539 }
12540 li->li_tv.v_type = VAR_STRING;
12541 li->li_tv.v_lock = 0;
12542 li->li_tv.vval.v_string = s;
12543 list_append(list, li);
12544 }
12545
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012546 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012547 list = NULL;
12548 }
12549 else
12550 {
12551 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12552#ifdef USE_CR
12553 /* translate <CR> into <NL> */
12554 if (res != NULL)
12555 {
12556 char_u *s;
12557
12558 for (s = res; *s; ++s)
12559 {
12560 if (*s == CAR)
12561 *s = NL;
12562 }
12563 }
12564#else
12565# ifdef USE_CRNL
12566 /* translate <CR><NL> into <NL> */
12567 if (res != NULL)
12568 {
12569 char_u *s, *d;
12570
12571 d = res;
12572 for (s = res; *s; ++s)
12573 {
12574 if (s[0] == CAR && s[1] == NL)
12575 ++s;
12576 *d++ = *s;
12577 }
12578 *d = NUL;
12579 }
12580# endif
12581#endif
12582 rettv->vval.v_string = res;
12583 res = NULL;
12584 }
12585
12586errret:
12587 if (infile != NULL)
12588 {
12589 mch_remove(infile);
12590 vim_free(infile);
12591 }
12592 if (res != NULL)
12593 vim_free(res);
12594 if (list != NULL)
12595 list_free(list);
12596}
12597
12598/*
12599 * "system()" function
12600 */
12601 static void
12602f_system(typval_T *argvars, typval_T *rettv)
12603{
12604 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12605}
12606
12607/*
12608 * "systemlist()" function
12609 */
12610 static void
12611f_systemlist(typval_T *argvars, typval_T *rettv)
12612{
12613 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12614}
12615
12616/*
12617 * "tabpagebuflist()" function
12618 */
12619 static void
12620f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12621{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012622 tabpage_T *tp;
12623 win_T *wp = NULL;
12624
12625 if (argvars[0].v_type == VAR_UNKNOWN)
12626 wp = firstwin;
12627 else
12628 {
12629 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12630 if (tp != NULL)
12631 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12632 }
12633 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12634 {
12635 for (; wp != NULL; wp = wp->w_next)
12636 if (list_append_number(rettv->vval.v_list,
12637 wp->w_buffer->b_fnum) == FAIL)
12638 break;
12639 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012640}
12641
12642
12643/*
12644 * "tabpagenr()" function
12645 */
12646 static void
12647f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12648{
12649 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012650 char_u *arg;
12651
12652 if (argvars[0].v_type != VAR_UNKNOWN)
12653 {
12654 arg = get_tv_string_chk(&argvars[0]);
12655 nr = 0;
12656 if (arg != NULL)
12657 {
12658 if (STRCMP(arg, "$") == 0)
12659 nr = tabpage_index(NULL) - 1;
12660 else
12661 EMSG2(_(e_invexpr2), arg);
12662 }
12663 }
12664 else
12665 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012666 rettv->vval.v_number = nr;
12667}
12668
12669
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012670static int get_winnr(tabpage_T *tp, typval_T *argvar);
12671
12672/*
12673 * Common code for tabpagewinnr() and winnr().
12674 */
12675 static int
12676get_winnr(tabpage_T *tp, typval_T *argvar)
12677{
12678 win_T *twin;
12679 int nr = 1;
12680 win_T *wp;
12681 char_u *arg;
12682
12683 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12684 if (argvar->v_type != VAR_UNKNOWN)
12685 {
12686 arg = get_tv_string_chk(argvar);
12687 if (arg == NULL)
12688 nr = 0; /* type error; errmsg already given */
12689 else if (STRCMP(arg, "$") == 0)
12690 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12691 else if (STRCMP(arg, "#") == 0)
12692 {
12693 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12694 if (twin == NULL)
12695 nr = 0;
12696 }
12697 else
12698 {
12699 EMSG2(_(e_invexpr2), arg);
12700 nr = 0;
12701 }
12702 }
12703
12704 if (nr > 0)
12705 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12706 wp != twin; wp = wp->w_next)
12707 {
12708 if (wp == NULL)
12709 {
12710 /* didn't find it in this tabpage */
12711 nr = 0;
12712 break;
12713 }
12714 ++nr;
12715 }
12716 return nr;
12717}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012718
12719/*
12720 * "tabpagewinnr()" function
12721 */
12722 static void
12723f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12724{
12725 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012726 tabpage_T *tp;
12727
12728 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12729 if (tp == NULL)
12730 nr = 0;
12731 else
12732 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012733 rettv->vval.v_number = nr;
12734}
12735
12736
12737/*
12738 * "tagfiles()" function
12739 */
12740 static void
12741f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12742{
12743 char_u *fname;
12744 tagname_T tn;
12745 int first;
12746
12747 if (rettv_list_alloc(rettv) == FAIL)
12748 return;
12749 fname = alloc(MAXPATHL);
12750 if (fname == NULL)
12751 return;
12752
12753 for (first = TRUE; ; first = FALSE)
12754 if (get_tagfname(&tn, first, fname) == FAIL
12755 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12756 break;
12757 tagname_free(&tn);
12758 vim_free(fname);
12759}
12760
12761/*
12762 * "taglist()" function
12763 */
12764 static void
12765f_taglist(typval_T *argvars, typval_T *rettv)
12766{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012767 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012768 char_u *tag_pattern;
12769
12770 tag_pattern = get_tv_string(&argvars[0]);
12771
12772 rettv->vval.v_number = FALSE;
12773 if (*tag_pattern == NUL)
12774 return;
12775
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012776 if (argvars[1].v_type != VAR_UNKNOWN)
12777 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012778 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012779 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012780}
12781
12782/*
12783 * "tempname()" function
12784 */
12785 static void
12786f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12787{
12788 static int x = 'A';
12789
12790 rettv->v_type = VAR_STRING;
12791 rettv->vval.v_string = vim_tempname(x, FALSE);
12792
12793 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12794 * names. Skip 'I' and 'O', they are used for shell redirection. */
12795 do
12796 {
12797 if (x == 'Z')
12798 x = '0';
12799 else if (x == '9')
12800 x = 'A';
12801 else
12802 {
12803#ifdef EBCDIC
12804 if (x == 'I')
12805 x = 'J';
12806 else if (x == 'R')
12807 x = 'S';
12808 else
12809#endif
12810 ++x;
12811 }
12812 } while (x == 'I' || x == 'O');
12813}
12814
12815#ifdef FEAT_FLOAT
12816/*
12817 * "tan()" function
12818 */
12819 static void
12820f_tan(typval_T *argvars, typval_T *rettv)
12821{
12822 float_T f = 0.0;
12823
12824 rettv->v_type = VAR_FLOAT;
12825 if (get_float_arg(argvars, &f) == OK)
12826 rettv->vval.v_float = tan(f);
12827 else
12828 rettv->vval.v_float = 0.0;
12829}
12830
12831/*
12832 * "tanh()" function
12833 */
12834 static void
12835f_tanh(typval_T *argvars, typval_T *rettv)
12836{
12837 float_T f = 0.0;
12838
12839 rettv->v_type = VAR_FLOAT;
12840 if (get_float_arg(argvars, &f) == OK)
12841 rettv->vval.v_float = tanh(f);
12842 else
12843 rettv->vval.v_float = 0.0;
12844}
12845#endif
12846
12847/*
12848 * "test_alloc_fail(id, countdown, repeat)" function
12849 */
12850 static void
12851f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12852{
12853 if (argvars[0].v_type != VAR_NUMBER
12854 || argvars[0].vval.v_number <= 0
12855 || argvars[1].v_type != VAR_NUMBER
12856 || argvars[1].vval.v_number < 0
12857 || argvars[2].v_type != VAR_NUMBER)
12858 EMSG(_(e_invarg));
12859 else
12860 {
12861 alloc_fail_id = argvars[0].vval.v_number;
12862 if (alloc_fail_id >= aid_last)
12863 EMSG(_(e_invarg));
12864 alloc_fail_countdown = argvars[1].vval.v_number;
12865 alloc_fail_repeat = argvars[2].vval.v_number;
12866 did_outofmem_msg = FALSE;
12867 }
12868}
12869
12870/*
12871 * "test_autochdir()"
12872 */
12873 static void
12874f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12875{
12876#if defined(FEAT_AUTOCHDIR)
12877 test_autochdir = TRUE;
12878#endif
12879}
12880
12881/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012882 * "test_feedinput()"
12883 */
12884 static void
12885f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12886{
12887#ifdef USE_INPUT_BUF
12888 char_u *val = get_tv_string_chk(&argvars[0]);
12889
12890 if (val != NULL)
12891 {
12892 trash_input_buf();
12893 add_to_input_buf_csi(val, (int)STRLEN(val));
12894 }
12895#endif
12896}
12897
12898/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012899 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012900 */
12901 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012902f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012903{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012904 char_u *name = (char_u *)"";
12905 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012906 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012907
12908 if (argvars[0].v_type != VAR_STRING
12909 || (argvars[1].v_type) != VAR_NUMBER)
12910 EMSG(_(e_invarg));
12911 else
12912 {
12913 name = get_tv_string_chk(&argvars[0]);
12914 val = (int)get_tv_number(&argvars[1]);
12915
12916 if (STRCMP(name, (char_u *)"redraw") == 0)
12917 disable_redraw_for_testing = val;
12918 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12919 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012920 else if (STRCMP(name, (char_u *)"starting") == 0)
12921 {
12922 if (val)
12923 {
12924 if (save_starting < 0)
12925 save_starting = starting;
12926 starting = 0;
12927 }
12928 else
12929 {
12930 starting = save_starting;
12931 save_starting = -1;
12932 }
12933 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012934 else if (STRCMP(name, (char_u *)"ALL") == 0)
12935 {
12936 disable_char_avail_for_testing = FALSE;
12937 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012938 if (save_starting >= 0)
12939 {
12940 starting = save_starting;
12941 save_starting = -1;
12942 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012943 }
12944 else
12945 EMSG2(_(e_invarg2), name);
12946 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012947}
12948
12949/*
12950 * "test_garbagecollect_now()" function
12951 */
12952 static void
12953f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12954{
12955 /* This is dangerous, any Lists and Dicts used internally may be freed
12956 * while still in use. */
12957 garbage_collect(TRUE);
12958}
12959
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012960/*
12961 * "test_ignore_error()" function
12962 */
12963 static void
12964f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12965{
12966 ignore_error_for_testing(get_tv_string(&argvars[0]));
12967}
12968
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012969#ifdef FEAT_JOB_CHANNEL
12970 static void
12971f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12972{
12973 rettv->v_type = VAR_CHANNEL;
12974 rettv->vval.v_channel = NULL;
12975}
12976#endif
12977
12978 static void
12979f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12980{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012981 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012982}
12983
12984#ifdef FEAT_JOB_CHANNEL
12985 static void
12986f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12987{
12988 rettv->v_type = VAR_JOB;
12989 rettv->vval.v_job = NULL;
12990}
12991#endif
12992
12993 static void
12994f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12995{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012996 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012997}
12998
12999 static void
13000f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13001{
13002 rettv->v_type = VAR_PARTIAL;
13003 rettv->vval.v_partial = NULL;
13004}
13005
13006 static void
13007f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13008{
13009 rettv->v_type = VAR_STRING;
13010 rettv->vval.v_string = NULL;
13011}
13012
13013 static void
13014f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13015{
13016 time_for_testing = (time_t)get_tv_number(&argvars[0]);
13017}
13018
13019#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13020/*
13021 * Get a callback from "arg". It can be a Funcref or a function name.
13022 * When "arg" is zero return an empty string.
13023 * Return NULL for an invalid argument.
13024 */
13025 char_u *
13026get_callback(typval_T *arg, partial_T **pp)
13027{
13028 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13029 {
13030 *pp = arg->vval.v_partial;
13031 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013032 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013033 }
13034 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013035 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013036 {
13037 func_ref(arg->vval.v_string);
13038 return arg->vval.v_string;
13039 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013040 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13041 return (char_u *)"";
13042 EMSG(_("E921: Invalid callback argument"));
13043 return NULL;
13044}
13045
13046/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013047 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013048 */
13049 void
13050free_callback(char_u *callback, partial_T *partial)
13051{
13052 if (partial != NULL)
13053 partial_unref(partial);
13054 else if (callback != NULL)
13055 {
13056 func_unref(callback);
13057 vim_free(callback);
13058 }
13059}
13060#endif
13061
13062#ifdef FEAT_TIMERS
13063/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013064 * "timer_info([timer])" function
13065 */
13066 static void
13067f_timer_info(typval_T *argvars, typval_T *rettv)
13068{
13069 timer_T *timer = NULL;
13070
13071 if (rettv_list_alloc(rettv) != OK)
13072 return;
13073 if (argvars[0].v_type != VAR_UNKNOWN)
13074 {
13075 if (argvars[0].v_type != VAR_NUMBER)
13076 EMSG(_(e_number_exp));
13077 else
13078 {
13079 timer = find_timer((int)get_tv_number(&argvars[0]));
13080 if (timer != NULL)
13081 add_timer_info(rettv, timer);
13082 }
13083 }
13084 else
13085 add_timer_info_all(rettv);
13086}
13087
13088/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013089 * "timer_pause(timer, paused)" function
13090 */
13091 static void
13092f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13093{
13094 timer_T *timer = NULL;
13095 int paused = (int)get_tv_number(&argvars[1]);
13096
13097 if (argvars[0].v_type != VAR_NUMBER)
13098 EMSG(_(e_number_exp));
13099 else
13100 {
13101 timer = find_timer((int)get_tv_number(&argvars[0]));
13102 if (timer != NULL)
13103 timer->tr_paused = paused;
13104 }
13105}
13106
13107/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013108 * "timer_start(time, callback [, options])" function
13109 */
13110 static void
13111f_timer_start(typval_T *argvars, typval_T *rettv)
13112{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013113 long msec = (long)get_tv_number(&argvars[0]);
13114 timer_T *timer;
13115 int repeat = 0;
13116 char_u *callback;
13117 dict_T *dict;
13118 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013119
Bram Moolenaar75537a92016-09-05 22:45:28 +020013120 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013121 if (check_secure())
13122 return;
13123 if (argvars[2].v_type != VAR_UNKNOWN)
13124 {
13125 if (argvars[2].v_type != VAR_DICT
13126 || (dict = argvars[2].vval.v_dict) == NULL)
13127 {
13128 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13129 return;
13130 }
13131 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13132 repeat = get_dict_number(dict, (char_u *)"repeat");
13133 }
13134
Bram Moolenaar75537a92016-09-05 22:45:28 +020013135 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013136 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013137 return;
13138
13139 timer = create_timer(msec, repeat);
13140 if (timer == NULL)
13141 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013142 else
13143 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013144 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013145 timer->tr_callback = vim_strsave(callback);
13146 else
13147 /* pointer into the partial */
13148 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013149 timer->tr_partial = partial;
13150 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013151 }
13152}
13153
13154/*
13155 * "timer_stop(timer)" function
13156 */
13157 static void
13158f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13159{
13160 timer_T *timer;
13161
13162 if (argvars[0].v_type != VAR_NUMBER)
13163 {
13164 EMSG(_(e_number_exp));
13165 return;
13166 }
13167 timer = find_timer((int)get_tv_number(&argvars[0]));
13168 if (timer != NULL)
13169 stop_timer(timer);
13170}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013171
13172/*
13173 * "timer_stopall()" function
13174 */
13175 static void
13176f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13177{
13178 stop_all_timers();
13179}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013180#endif
13181
13182/*
13183 * "tolower(string)" function
13184 */
13185 static void
13186f_tolower(typval_T *argvars, typval_T *rettv)
13187{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013188 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013189 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013190}
13191
13192/*
13193 * "toupper(string)" function
13194 */
13195 static void
13196f_toupper(typval_T *argvars, typval_T *rettv)
13197{
13198 rettv->v_type = VAR_STRING;
13199 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13200}
13201
13202/*
13203 * "tr(string, fromstr, tostr)" function
13204 */
13205 static void
13206f_tr(typval_T *argvars, typval_T *rettv)
13207{
13208 char_u *in_str;
13209 char_u *fromstr;
13210 char_u *tostr;
13211 char_u *p;
13212#ifdef FEAT_MBYTE
13213 int inlen;
13214 int fromlen;
13215 int tolen;
13216 int idx;
13217 char_u *cpstr;
13218 int cplen;
13219 int first = TRUE;
13220#endif
13221 char_u buf[NUMBUFLEN];
13222 char_u buf2[NUMBUFLEN];
13223 garray_T ga;
13224
13225 in_str = get_tv_string(&argvars[0]);
13226 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13227 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13228
13229 /* Default return value: empty string. */
13230 rettv->v_type = VAR_STRING;
13231 rettv->vval.v_string = NULL;
13232 if (fromstr == NULL || tostr == NULL)
13233 return; /* type error; errmsg already given */
13234 ga_init2(&ga, (int)sizeof(char), 80);
13235
13236#ifdef FEAT_MBYTE
13237 if (!has_mbyte)
13238#endif
13239 /* not multi-byte: fromstr and tostr must be the same length */
13240 if (STRLEN(fromstr) != STRLEN(tostr))
13241 {
13242#ifdef FEAT_MBYTE
13243error:
13244#endif
13245 EMSG2(_(e_invarg2), fromstr);
13246 ga_clear(&ga);
13247 return;
13248 }
13249
13250 /* fromstr and tostr have to contain the same number of chars */
13251 while (*in_str != NUL)
13252 {
13253#ifdef FEAT_MBYTE
13254 if (has_mbyte)
13255 {
13256 inlen = (*mb_ptr2len)(in_str);
13257 cpstr = in_str;
13258 cplen = inlen;
13259 idx = 0;
13260 for (p = fromstr; *p != NUL; p += fromlen)
13261 {
13262 fromlen = (*mb_ptr2len)(p);
13263 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13264 {
13265 for (p = tostr; *p != NUL; p += tolen)
13266 {
13267 tolen = (*mb_ptr2len)(p);
13268 if (idx-- == 0)
13269 {
13270 cplen = tolen;
13271 cpstr = p;
13272 break;
13273 }
13274 }
13275 if (*p == NUL) /* tostr is shorter than fromstr */
13276 goto error;
13277 break;
13278 }
13279 ++idx;
13280 }
13281
13282 if (first && cpstr == in_str)
13283 {
13284 /* Check that fromstr and tostr have the same number of
13285 * (multi-byte) characters. Done only once when a character
13286 * of in_str doesn't appear in fromstr. */
13287 first = FALSE;
13288 for (p = tostr; *p != NUL; p += tolen)
13289 {
13290 tolen = (*mb_ptr2len)(p);
13291 --idx;
13292 }
13293 if (idx != 0)
13294 goto error;
13295 }
13296
13297 (void)ga_grow(&ga, cplen);
13298 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13299 ga.ga_len += cplen;
13300
13301 in_str += inlen;
13302 }
13303 else
13304#endif
13305 {
13306 /* When not using multi-byte chars we can do it faster. */
13307 p = vim_strchr(fromstr, *in_str);
13308 if (p != NULL)
13309 ga_append(&ga, tostr[p - fromstr]);
13310 else
13311 ga_append(&ga, *in_str);
13312 ++in_str;
13313 }
13314 }
13315
13316 /* add a terminating NUL */
13317 (void)ga_grow(&ga, 1);
13318 ga_append(&ga, NUL);
13319
13320 rettv->vval.v_string = ga.ga_data;
13321}
13322
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013323/*
13324 * "trim({expr})" function
13325 */
13326 static void
13327f_trim(typval_T *argvars, typval_T *rettv)
13328{
13329 char_u buf1[NUMBUFLEN];
13330 char_u buf2[NUMBUFLEN];
13331 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13332 char_u *mask = NULL;
13333 char_u *tail;
13334 char_u *prev;
13335 char_u *p;
13336 int c1;
13337
13338 rettv->v_type = VAR_STRING;
13339 if (head == NULL)
13340 {
13341 rettv->vval.v_string = NULL;
13342 return;
13343 }
13344
13345 if (argvars[1].v_type == VAR_STRING)
13346 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13347
13348 while (*head != NUL)
13349 {
13350 c1 = PTR2CHAR(head);
13351 if (mask == NULL)
13352 {
13353 if (c1 > ' ' && c1 != 0xa0)
13354 break;
13355 }
13356 else
13357 {
13358 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13359 if (c1 == PTR2CHAR(p))
13360 break;
13361 if (*p == NUL)
13362 break;
13363 }
13364 MB_PTR_ADV(head);
13365 }
13366
13367 for (tail = head + STRLEN(head); tail > head; tail = prev)
13368 {
13369 prev = tail;
13370 MB_PTR_BACK(head, prev);
13371 c1 = PTR2CHAR(prev);
13372 if (mask == NULL)
13373 {
13374 if (c1 > ' ' && c1 != 0xa0)
13375 break;
13376 }
13377 else
13378 {
13379 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13380 if (c1 == PTR2CHAR(p))
13381 break;
13382 if (*p == NUL)
13383 break;
13384 }
13385 }
13386 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13387}
13388
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013389#ifdef FEAT_FLOAT
13390/*
13391 * "trunc({float})" function
13392 */
13393 static void
13394f_trunc(typval_T *argvars, typval_T *rettv)
13395{
13396 float_T f = 0.0;
13397
13398 rettv->v_type = VAR_FLOAT;
13399 if (get_float_arg(argvars, &f) == OK)
13400 /* trunc() is not in C90, use floor() or ceil() instead. */
13401 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13402 else
13403 rettv->vval.v_float = 0.0;
13404}
13405#endif
13406
13407/*
13408 * "type(expr)" function
13409 */
13410 static void
13411f_type(typval_T *argvars, typval_T *rettv)
13412{
13413 int n = -1;
13414
13415 switch (argvars[0].v_type)
13416 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013417 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13418 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013419 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013420 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13421 case VAR_LIST: n = VAR_TYPE_LIST; break;
13422 case VAR_DICT: n = VAR_TYPE_DICT; break;
13423 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013424 case VAR_SPECIAL:
13425 if (argvars[0].vval.v_number == VVAL_FALSE
13426 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013427 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013428 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013429 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013430 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013431 case VAR_JOB: n = VAR_TYPE_JOB; break;
13432 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013433 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013434 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013435 n = -1;
13436 break;
13437 }
13438 rettv->vval.v_number = n;
13439}
13440
13441/*
13442 * "undofile(name)" function
13443 */
13444 static void
13445f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13446{
13447 rettv->v_type = VAR_STRING;
13448#ifdef FEAT_PERSISTENT_UNDO
13449 {
13450 char_u *fname = get_tv_string(&argvars[0]);
13451
13452 if (*fname == NUL)
13453 {
13454 /* If there is no file name there will be no undo file. */
13455 rettv->vval.v_string = NULL;
13456 }
13457 else
13458 {
13459 char_u *ffname = FullName_save(fname, FALSE);
13460
13461 if (ffname != NULL)
13462 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13463 vim_free(ffname);
13464 }
13465 }
13466#else
13467 rettv->vval.v_string = NULL;
13468#endif
13469}
13470
13471/*
13472 * "undotree()" function
13473 */
13474 static void
13475f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13476{
13477 if (rettv_dict_alloc(rettv) == OK)
13478 {
13479 dict_T *dict = rettv->vval.v_dict;
13480 list_T *list;
13481
13482 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13483 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13484 dict_add_nr_str(dict, "save_last",
13485 (long)curbuf->b_u_save_nr_last, NULL);
13486 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13487 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13488 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13489
13490 list = list_alloc();
13491 if (list != NULL)
13492 {
13493 u_eval_tree(curbuf->b_u_oldhead, list);
13494 dict_add_list(dict, "entries", list);
13495 }
13496 }
13497}
13498
13499/*
13500 * "values(dict)" function
13501 */
13502 static void
13503f_values(typval_T *argvars, typval_T *rettv)
13504{
13505 dict_list(argvars, rettv, 1);
13506}
13507
13508/*
13509 * "virtcol(string)" function
13510 */
13511 static void
13512f_virtcol(typval_T *argvars, typval_T *rettv)
13513{
13514 colnr_T vcol = 0;
13515 pos_T *fp;
13516 int fnum = curbuf->b_fnum;
13517
13518 fp = var2fpos(&argvars[0], FALSE, &fnum);
13519 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13520 && fnum == curbuf->b_fnum)
13521 {
13522 getvvcol(curwin, fp, NULL, NULL, &vcol);
13523 ++vcol;
13524 }
13525
13526 rettv->vval.v_number = vcol;
13527}
13528
13529/*
13530 * "visualmode()" function
13531 */
13532 static void
13533f_visualmode(typval_T *argvars, typval_T *rettv)
13534{
13535 char_u str[2];
13536
13537 rettv->v_type = VAR_STRING;
13538 str[0] = curbuf->b_visual_mode_eval;
13539 str[1] = NUL;
13540 rettv->vval.v_string = vim_strsave(str);
13541
13542 /* A non-zero number or non-empty string argument: reset mode. */
13543 if (non_zero_arg(&argvars[0]))
13544 curbuf->b_visual_mode_eval = NUL;
13545}
13546
13547/*
13548 * "wildmenumode()" function
13549 */
13550 static void
13551f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13552{
13553#ifdef FEAT_WILDMENU
13554 if (wild_menu_showing)
13555 rettv->vval.v_number = 1;
13556#endif
13557}
13558
13559/*
13560 * "winbufnr(nr)" function
13561 */
13562 static void
13563f_winbufnr(typval_T *argvars, typval_T *rettv)
13564{
13565 win_T *wp;
13566
13567 wp = find_win_by_nr(&argvars[0], NULL);
13568 if (wp == NULL)
13569 rettv->vval.v_number = -1;
13570 else
13571 rettv->vval.v_number = wp->w_buffer->b_fnum;
13572}
13573
13574/*
13575 * "wincol()" function
13576 */
13577 static void
13578f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13579{
13580 validate_cursor();
13581 rettv->vval.v_number = curwin->w_wcol + 1;
13582}
13583
13584/*
13585 * "winheight(nr)" function
13586 */
13587 static void
13588f_winheight(typval_T *argvars, typval_T *rettv)
13589{
13590 win_T *wp;
13591
13592 wp = find_win_by_nr(&argvars[0], NULL);
13593 if (wp == NULL)
13594 rettv->vval.v_number = -1;
13595 else
13596 rettv->vval.v_number = wp->w_height;
13597}
13598
13599/*
13600 * "winline()" function
13601 */
13602 static void
13603f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13604{
13605 validate_cursor();
13606 rettv->vval.v_number = curwin->w_wrow + 1;
13607}
13608
13609/*
13610 * "winnr()" function
13611 */
13612 static void
13613f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13614{
13615 int nr = 1;
13616
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013617 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013618 rettv->vval.v_number = nr;
13619}
13620
13621/*
13622 * "winrestcmd()" function
13623 */
13624 static void
13625f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13626{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013627 win_T *wp;
13628 int winnr = 1;
13629 garray_T ga;
13630 char_u buf[50];
13631
13632 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013633 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013634 {
13635 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13636 ga_concat(&ga, buf);
13637 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13638 ga_concat(&ga, buf);
13639 ++winnr;
13640 }
13641 ga_append(&ga, NUL);
13642
13643 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013644 rettv->v_type = VAR_STRING;
13645}
13646
13647/*
13648 * "winrestview()" function
13649 */
13650 static void
13651f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13652{
13653 dict_T *dict;
13654
13655 if (argvars[0].v_type != VAR_DICT
13656 || (dict = argvars[0].vval.v_dict) == NULL)
13657 EMSG(_(e_invarg));
13658 else
13659 {
13660 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13661 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13662 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13663 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13664#ifdef FEAT_VIRTUALEDIT
13665 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13666 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13667#endif
13668 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13669 {
13670 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13671 curwin->w_set_curswant = FALSE;
13672 }
13673
13674 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13675 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13676#ifdef FEAT_DIFF
13677 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13678 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13679#endif
13680 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13681 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13682 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13683 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13684
13685 check_cursor();
13686 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013687 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013688 changed_window_setting();
13689
13690 if (curwin->w_topline <= 0)
13691 curwin->w_topline = 1;
13692 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13693 curwin->w_topline = curbuf->b_ml.ml_line_count;
13694#ifdef FEAT_DIFF
13695 check_topfill(curwin, TRUE);
13696#endif
13697 }
13698}
13699
13700/*
13701 * "winsaveview()" function
13702 */
13703 static void
13704f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13705{
13706 dict_T *dict;
13707
13708 if (rettv_dict_alloc(rettv) == FAIL)
13709 return;
13710 dict = rettv->vval.v_dict;
13711
13712 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13713 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13714#ifdef FEAT_VIRTUALEDIT
13715 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13716#endif
13717 update_curswant();
13718 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13719
13720 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13721#ifdef FEAT_DIFF
13722 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13723#endif
13724 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13725 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13726}
13727
13728/*
13729 * "winwidth(nr)" function
13730 */
13731 static void
13732f_winwidth(typval_T *argvars, typval_T *rettv)
13733{
13734 win_T *wp;
13735
13736 wp = find_win_by_nr(&argvars[0], NULL);
13737 if (wp == NULL)
13738 rettv->vval.v_number = -1;
13739 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013740 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013741}
13742
13743/*
13744 * "wordcount()" function
13745 */
13746 static void
13747f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13748{
13749 if (rettv_dict_alloc(rettv) == FAIL)
13750 return;
13751 cursor_pos_info(rettv->vval.v_dict);
13752}
13753
13754/*
13755 * "writefile()" function
13756 */
13757 static void
13758f_writefile(typval_T *argvars, typval_T *rettv)
13759{
13760 int binary = FALSE;
13761 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013762#ifdef HAVE_FSYNC
13763 int do_fsync = p_fs;
13764#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013765 char_u *fname;
13766 FILE *fd;
13767 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013768 listitem_T *li;
13769 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013770
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013771 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013772 if (check_restricted() || check_secure())
13773 return;
13774
13775 if (argvars[0].v_type != VAR_LIST)
13776 {
13777 EMSG2(_(e_listarg), "writefile()");
13778 return;
13779 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013780 list = argvars[0].vval.v_list;
13781 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013782 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013783 for (li = list->lv_first; li != NULL; li = li->li_next)
13784 if (get_tv_string_chk(&li->li_tv) == NULL)
13785 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013786
13787 if (argvars[2].v_type != VAR_UNKNOWN)
13788 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013789 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13790
13791 if (arg2 == NULL)
13792 return;
13793 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013794 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013795 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013796 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013797#ifdef HAVE_FSYNC
13798 if (vim_strchr(arg2, 's') != NULL)
13799 do_fsync = TRUE;
13800 else if (vim_strchr(arg2, 'S') != NULL)
13801 do_fsync = FALSE;
13802#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013803 }
13804
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013805 fname = get_tv_string_chk(&argvars[1]);
13806 if (fname == NULL)
13807 return;
13808
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013809 /* Always open the file in binary mode, library functions have a mind of
13810 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013811 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13812 append ? APPENDBIN : WRITEBIN)) == NULL)
13813 {
13814 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13815 ret = -1;
13816 }
13817 else
13818 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013819 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013820 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013821#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013822 else if (do_fsync)
13823 /* Ignore the error, the user wouldn't know what to do about it.
13824 * May happen for a device. */
13825 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013826#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013827 fclose(fd);
13828 }
13829
13830 rettv->vval.v_number = ret;
13831}
13832
13833/*
13834 * "xor(expr, expr)" function
13835 */
13836 static void
13837f_xor(typval_T *argvars, typval_T *rettv)
13838{
13839 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13840 ^ get_tv_number_chk(&argvars[1], NULL);
13841}
13842
13843
13844#endif /* FEAT_EVAL */