blob: b015f6dad14e2b8f6cfa07ae15cbcba1430a4b31 [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
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020023#ifdef FEAT_FLOAT
24static void f_abs(typval_T *argvars, typval_T *rettv);
25static void f_acos(typval_T *argvars, typval_T *rettv);
26#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020027static void f_and(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028#ifdef FEAT_FLOAT
29static void f_asin(typval_T *argvars, typval_T *rettv);
30static void f_atan(typval_T *argvars, typval_T *rettv);
31static void f_atan2(typval_T *argvars, typval_T *rettv);
32#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010033#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020034static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010035static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010036# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010037static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010038# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010039#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_byte2line(typval_T *argvars, typval_T *rettv);
41static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
42static void f_byteidx(typval_T *argvars, typval_T *rettv);
43static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
44static void f_call(typval_T *argvars, typval_T *rettv);
45#ifdef FEAT_FLOAT
46static void f_ceil(typval_T *argvars, typval_T *rettv);
47#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_changenr(typval_T *argvars, typval_T *rettv);
49static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_confirm(typval_T *argvars, typval_T *rettv);
52static void f_copy(typval_T *argvars, typval_T *rettv);
53#ifdef FEAT_FLOAT
54static void f_cos(typval_T *argvars, typval_T *rettv);
55static void f_cosh(typval_T *argvars, typval_T *rettv);
56#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010058#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020059static void f_debugbreak(typval_T *argvars, typval_T *rettv);
60#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020061static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020062static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010063static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020064static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020065static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020066static void f_escape(typval_T *argvars, typval_T *rettv);
67static void f_eval(typval_T *argvars, typval_T *rettv);
68static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020069static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_exists(typval_T *argvars, typval_T *rettv);
71#ifdef FEAT_FLOAT
72static void f_exp(typval_T *argvars, typval_T *rettv);
73#endif
74static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020075static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020076static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020077#ifdef FEAT_FLOAT
78static void f_float2nr(typval_T *argvars, typval_T *rettv);
79static void f_floor(typval_T *argvars, typval_T *rettv);
80static void f_fmod(typval_T *argvars, typval_T *rettv);
81#endif
82static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020083static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020084static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020085static void f_function(typval_T *argvars, typval_T *rettv);
86static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
87static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010088static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020090static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010092static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020093static void f_getpid(typval_T *argvars, typval_T *rettv);
94static void f_getcurpos(typval_T *argvars, typval_T *rettv);
95static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +020097static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020098static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010099static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200100static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
101static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102static void f_hlID(typval_T *argvars, typval_T *rettv);
103static void f_hlexists(typval_T *argvars, typval_T *rettv);
104static void f_hostname(typval_T *argvars, typval_T *rettv);
105static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200106static void f_index(typval_T *argvars, typval_T *rettv);
107static void f_input(typval_T *argvars, typval_T *rettv);
108static void f_inputdialog(typval_T *argvars, typval_T *rettv);
109static void f_inputlist(typval_T *argvars, typval_T *rettv);
110static void f_inputrestore(typval_T *argvars, typval_T *rettv);
111static void f_inputsave(typval_T *argvars, typval_T *rettv);
112static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +0100113static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200114static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200115static void f_islocked(typval_T *argvars, typval_T *rettv);
116#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200117static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200118static void f_isnan(typval_T *argvars, typval_T *rettv);
119#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
121static void f_len(typval_T *argvars, typval_T *rettv);
122static void f_libcall(typval_T *argvars, typval_T *rettv);
123static void f_libcallnr(typval_T *argvars, typval_T *rettv);
124static void f_line(typval_T *argvars, typval_T *rettv);
125static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200126#ifdef FEAT_FLOAT
127static void f_log(typval_T *argvars, typval_T *rettv);
128static void f_log10(typval_T *argvars, typval_T *rettv);
129#endif
130#ifdef FEAT_LUA
131static void f_luaeval(typval_T *argvars, typval_T *rettv);
132#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200133static void f_maparg(typval_T *argvars, typval_T *rettv);
134static void f_mapcheck(typval_T *argvars, typval_T *rettv);
135static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200136static void f_matchend(typval_T *argvars, typval_T *rettv);
137static void f_matchlist(typval_T *argvars, typval_T *rettv);
138static void f_matchstr(typval_T *argvars, typval_T *rettv);
139static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
140static void f_max(typval_T *argvars, typval_T *rettv);
141static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200142#ifdef FEAT_MZSCHEME
143static void f_mzeval(typval_T *argvars, typval_T *rettv);
144#endif
145static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
146static void f_nr2char(typval_T *argvars, typval_T *rettv);
147static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200148#ifdef FEAT_PERL
149static void f_perleval(typval_T *argvars, typval_T *rettv);
150#endif
151#ifdef FEAT_FLOAT
152static void f_pow(typval_T *argvars, typval_T *rettv);
153#endif
154static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
155static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200156static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200157static void f_pumvisible(typval_T *argvars, typval_T *rettv);
158#ifdef FEAT_PYTHON3
159static void f_py3eval(typval_T *argvars, typval_T *rettv);
160#endif
161#ifdef FEAT_PYTHON
162static void f_pyeval(typval_T *argvars, typval_T *rettv);
163#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100164#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
165static void f_pyxeval(typval_T *argvars, typval_T *rettv);
166#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100167static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100168static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200169static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200170static void f_reg_executing(typval_T *argvars, typval_T *rettv);
171static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200172static void f_rename(typval_T *argvars, typval_T *rettv);
173static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174#ifdef FEAT_FLOAT
175static void f_round(typval_T *argvars, typval_T *rettv);
176#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100177#ifdef FEAT_RUBY
178static void f_rubyeval(typval_T *argvars, typval_T *rettv);
179#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200180static void f_screenattr(typval_T *argvars, typval_T *rettv);
181static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100182static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200183static void f_screencol(typval_T *argvars, typval_T *rettv);
184static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100185static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200186static void f_search(typval_T *argvars, typval_T *rettv);
187static void f_searchdecl(typval_T *argvars, typval_T *rettv);
188static void f_searchpair(typval_T *argvars, typval_T *rettv);
189static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
190static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200191static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200192static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200195static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100196static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197#ifdef FEAT_CRYPT
198static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200199#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_shellescape(typval_T *argvars, typval_T *rettv);
201static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202#ifdef FEAT_FLOAT
203static void f_sin(typval_T *argvars, typval_T *rettv);
204static void f_sinh(typval_T *argvars, typval_T *rettv);
205#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200206static void f_soundfold(typval_T *argvars, typval_T *rettv);
207static void f_spellbadword(typval_T *argvars, typval_T *rettv);
208static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
209static void f_split(typval_T *argvars, typval_T *rettv);
210#ifdef FEAT_FLOAT
211static void f_sqrt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100212#endif
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100213static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100214#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200215static void f_str2float(typval_T *argvars, typval_T *rettv);
216#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200217static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200218static void f_str2nr(typval_T *argvars, typval_T *rettv);
219static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200220static void f_strgetchar(typval_T *argvars, typval_T *rettv);
221static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200222static void f_strlen(typval_T *argvars, typval_T *rettv);
223static void f_strcharpart(typval_T *argvars, typval_T *rettv);
224static void f_strpart(typval_T *argvars, typval_T *rettv);
225static void f_strridx(typval_T *argvars, typval_T *rettv);
226static void f_strtrans(typval_T *argvars, typval_T *rettv);
227static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
228static void f_strwidth(typval_T *argvars, typval_T *rettv);
229static void f_submatch(typval_T *argvars, typval_T *rettv);
230static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200231static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200232static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200233static void f_synID(typval_T *argvars, typval_T *rettv);
234static void f_synIDattr(typval_T *argvars, typval_T *rettv);
235static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
236static void f_synstack(typval_T *argvars, typval_T *rettv);
237static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200238static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200239static void f_taglist(typval_T *argvars, typval_T *rettv);
240static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200241#ifdef FEAT_FLOAT
242static void f_tan(typval_T *argvars, typval_T *rettv);
243static void f_tanh(typval_T *argvars, typval_T *rettv);
244#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_tolower(typval_T *argvars, typval_T *rettv);
246static void f_toupper(typval_T *argvars, typval_T *rettv);
247static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100248static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200249#ifdef FEAT_FLOAT
250static void f_trunc(typval_T *argvars, typval_T *rettv);
251#endif
252static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200253static void f_virtcol(typval_T *argvars, typval_T *rettv);
254static void f_visualmode(typval_T *argvars, typval_T *rettv);
255static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100256static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200257static void f_wordcount(typval_T *argvars, typval_T *rettv);
258static void f_xor(typval_T *argvars, typval_T *rettv);
259
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100260
261 static type_T *
262ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
263{
264 return &t_void;
265}
266 static type_T *
267ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
268{
269 return &t_any;
270}
271 static type_T *
272ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
273{
274 return &t_number;
275}
276 static type_T *
277ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
278{
279 return &t_float;
280}
281 static type_T *
282ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
283{
284 return &t_string;
285}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200286 static type_T *
287ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100288{
289 return &t_list_any;
290}
291 static type_T *
292ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
293{
294 return &t_list_number;
295}
296 static type_T *
297ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
298{
299 return &t_list_string;
300}
301 static type_T *
302ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
303{
304 return &t_list_dict_any;
305}
306 static type_T *
307ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
308{
309 return &t_dict_any;
310}
311 static type_T *
312ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
313{
314 return &t_dict_number;
315}
316 static type_T *
317ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
318{
319 return &t_dict_string;
320}
321 static type_T *
322ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
323{
324 return &t_blob;
325}
326 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200327ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100328{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200329 return &t_func_any;
330}
331 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100332ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
333{
334 return &t_channel;
335}
336 static type_T *
337ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
338{
339 return &t_job;
340}
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100341
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200342 static type_T *
343ret_first_arg(int argcount, type_T **argtypes)
344{
345 if (argcount > 0)
346 return argtypes[0];
347 return &t_void;
348}
349
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200350/*
351 * Used for getqflist(): returns list if there is no argument, dict if there is
352 * one.
353 */
354 static type_T *
355ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
356{
357 if (argcount > 0)
358 return &t_dict_any;
359 return &t_list_dict_any;
360}
361
362/*
363 * Used for getloclist(): returns list if there is one argument, dict if there
364 * are two.
365 */
366 static type_T *
367ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
368{
369 if (argcount > 1)
370 return &t_dict_any;
371 return &t_list_dict_any;
372}
373
Bram Moolenaar846178a2020-07-05 17:04:13 +0200374 static type_T *
375ret_argv(int argcount, type_T **argtypes UNUSED)
376{
377 // argv() returns list of strings
378 if (argcount == 0)
379 return &t_list_string;
380
381 // argv(0) returns a string, but argv(-1] returns a list
382 return &t_any;
383}
384
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100385static type_T *ret_f_function(int argcount, type_T **argtypes);
386
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200387/*
388 * Array with names and number of arguments of all internal functions
389 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
390 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200391typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200392{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200393 char *f_name; // function name
394 char f_min_argc; // minimal number of arguments
395 char f_max_argc; // maximal number of arguments
396 char f_argtype; // for method: FEARG_ values
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100397 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
398 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200399 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200400 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200401} funcentry_T;
402
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200403// values for f_argtype; zero means it cannot be used as a method
404#define FEARG_1 1 // base is the first argument
405#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200406#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200407#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200408#define FEARG_LAST 9 // base is the last argument
409
Bram Moolenaar15c47602020-03-26 22:16:48 +0100410#ifdef FEAT_FLOAT
411# define FLOAT_FUNC(name) name
412#else
413# define FLOAT_FUNC(name) NULL
414#endif
415#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
416# define MATH_FUNC(name) name
417#else
418# define MATH_FUNC(name) NULL
419#endif
420#ifdef FEAT_TIMERS
421# define TIMER_FUNC(name) name
422#else
423# define TIMER_FUNC(name) NULL
424#endif
425#ifdef FEAT_JOB_CHANNEL
426# define JOB_FUNC(name) name
427#else
428# define JOB_FUNC(name) NULL
429#endif
430#ifdef FEAT_PROP_POPUP
431# define PROP_FUNC(name) name
432#else
433# define PROP_FUNC(name) NULL
434#endif
435#ifdef FEAT_SIGNS
436# define SIGN_FUNC(name) name
437#else
438# define SIGN_FUNC(name) NULL
439#endif
440#ifdef FEAT_SOUND
441# define SOUND_FUNC(name) name
442#else
443# define SOUND_FUNC(name) NULL
444#endif
445#ifdef FEAT_TERMINAL
446# define TERM_FUNC(name) name
447#else
448# define TERM_FUNC(name) NULL
449#endif
450
Bram Moolenaarac92e252019-08-03 21:58:38 +0200451static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200452{
Bram Moolenaar15c47602020-03-26 22:16:48 +0100453 {"abs", 1, 1, FEARG_1, ret_any, FLOAT_FUNC(f_abs)},
454 {"acos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfce82b32020-07-05 16:07:21 +0200455 {"add", 2, 2, FEARG_1, ret_first_arg, f_add},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100456 {"and", 2, 2, FEARG_1, ret_number, f_and},
457 {"append", 2, 2, FEARG_LAST, ret_number, f_append},
458 {"appendbufline", 3, 3, FEARG_LAST, ret_number, f_appendbufline},
459 {"argc", 0, 1, 0, ret_number, f_argc},
460 {"argidx", 0, 0, 0, ret_number, f_argidx},
461 {"arglistid", 0, 2, 0, ret_number, f_arglistid},
Bram Moolenaar846178a2020-07-05 17:04:13 +0200462 {"argv", 0, 2, 0, ret_argv, f_argv},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100463 {"asin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_asin)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100464 {"assert_beeps", 1, 2, FEARG_1, ret_number, f_assert_beeps},
465 {"assert_equal", 2, 3, FEARG_2, ret_number, f_assert_equal},
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200466 {"assert_equalfile", 2, 3, FEARG_1, ret_number, f_assert_equalfile},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100467 {"assert_exception", 1, 2, 0, ret_number, f_assert_exception},
468 {"assert_fails", 1, 3, FEARG_1, ret_number, f_assert_fails},
469 {"assert_false", 1, 2, FEARG_1, ret_number, f_assert_false},
470 {"assert_inrange", 3, 4, FEARG_3, ret_number, f_assert_inrange},
471 {"assert_match", 2, 3, FEARG_2, ret_number, f_assert_match},
472 {"assert_notequal", 2, 3, FEARG_2, ret_number, f_assert_notequal},
473 {"assert_notmatch", 2, 3, FEARG_2, ret_number, f_assert_notmatch},
474 {"assert_report", 1, 1, FEARG_1, ret_number, f_assert_report},
475 {"assert_true", 1, 2, FEARG_1, ret_number, f_assert_true},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100476 {"atan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_atan)},
477 {"atan2", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_atan2)},
478 {"balloon_gettext", 0, 0, 0, ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100479#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100480 f_balloon_gettext
481#else
482 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100483#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100484 },
485 {"balloon_show", 1, 1, FEARG_1, ret_void,
486#ifdef FEAT_BEVAL
487 f_balloon_show
488#else
489 NULL
490#endif
491 },
492 {"balloon_split", 1, 1, FEARG_1, ret_list_string,
493#if defined(FEAT_BEVAL_TERM)
494 f_balloon_split
495#else
496 NULL
497#endif
498 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100499 {"browse", 4, 4, 0, ret_string, f_browse},
500 {"browsedir", 2, 2, 0, ret_string, f_browsedir},
501 {"bufadd", 1, 1, FEARG_1, ret_number, f_bufadd},
502 {"bufexists", 1, 1, FEARG_1, ret_number, f_bufexists},
503 {"buffer_exists", 1, 1, FEARG_1, ret_number, f_bufexists}, // obsolete
504 {"buffer_name", 0, 1, FEARG_1, ret_string, f_bufname}, // obsolete
505 {"buffer_number", 0, 1, FEARG_1, ret_number, f_bufnr}, // obsolete
506 {"buflisted", 1, 1, FEARG_1, ret_number, f_buflisted},
507 {"bufload", 1, 1, FEARG_1, ret_void, f_bufload},
508 {"bufloaded", 1, 1, FEARG_1, ret_number, f_bufloaded},
509 {"bufname", 0, 1, FEARG_1, ret_string, f_bufname},
510 {"bufnr", 0, 2, FEARG_1, ret_number, f_bufnr},
511 {"bufwinid", 1, 1, FEARG_1, ret_number, f_bufwinid},
512 {"bufwinnr", 1, 1, FEARG_1, ret_number, f_bufwinnr},
513 {"byte2line", 1, 1, FEARG_1, ret_number, f_byte2line},
514 {"byteidx", 2, 2, FEARG_1, ret_number, f_byteidx},
515 {"byteidxcomp", 2, 2, FEARG_1, ret_number, f_byteidxcomp},
516 {"call", 2, 3, FEARG_1, ret_any, f_call},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100517 {"ceil", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_ceil)},
518 {"ch_canread", 1, 1, FEARG_1, ret_number, JOB_FUNC(f_ch_canread)},
519 {"ch_close", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close)},
520 {"ch_close_in", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close_in)},
521 {"ch_evalexpr", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalexpr)},
522 {"ch_evalraw", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalraw)},
523 {"ch_getbufnr", 2, 2, FEARG_1, ret_number, JOB_FUNC(f_ch_getbufnr)},
524 {"ch_getjob", 1, 1, FEARG_1, ret_job, JOB_FUNC(f_ch_getjob)},
525 {"ch_info", 1, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_ch_info)},
526 {"ch_log", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_log)},
527 {"ch_logfile", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_logfile)},
528 {"ch_open", 1, 2, FEARG_1, ret_channel, JOB_FUNC(f_ch_open)},
529 {"ch_read", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_read)},
530 {"ch_readblob", 1, 2, FEARG_1, ret_blob, JOB_FUNC(f_ch_readblob)},
531 {"ch_readraw", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_readraw)},
532 {"ch_sendexpr", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendexpr)},
533 {"ch_sendraw", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendraw)},
534 {"ch_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_setoptions)},
535 {"ch_status", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_status)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100536 {"changenr", 0, 0, 0, ret_number, f_changenr},
537 {"char2nr", 1, 2, FEARG_1, ret_number, f_char2nr},
538 {"chdir", 1, 1, FEARG_1, ret_string, f_chdir},
539 {"cindent", 1, 1, FEARG_1, ret_number, f_cindent},
540 {"clearmatches", 0, 1, FEARG_1, ret_void, f_clearmatches},
541 {"col", 1, 1, FEARG_1, ret_number, f_col},
542 {"complete", 2, 2, FEARG_2, ret_void, f_complete},
543 {"complete_add", 1, 1, FEARG_1, ret_number, f_complete_add},
544 {"complete_check", 0, 0, 0, ret_number, f_complete_check},
545 {"complete_info", 0, 1, FEARG_1, ret_dict_any, f_complete_info},
546 {"confirm", 1, 4, FEARG_1, ret_number, f_confirm},
Bram Moolenaara66ba012020-07-05 18:41:08 +0200547 {"copy", 1, 1, FEARG_1, ret_first_arg, f_copy},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100548 {"cos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cos)},
549 {"cosh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cosh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100550 {"count", 2, 4, FEARG_1, ret_number, f_count},
551 {"cscope_connection",0,3, 0, ret_number, f_cscope_connection},
552 {"cursor", 1, 3, FEARG_1, ret_number, f_cursor},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100553 {"debugbreak", 1, 1, FEARG_1, ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100554#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100555 f_debugbreak
556#else
557 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200558#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100559 },
Bram Moolenaara66ba012020-07-05 18:41:08 +0200560 {"deepcopy", 1, 2, FEARG_1, ret_first_arg, f_deepcopy},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100561 {"delete", 1, 2, FEARG_1, ret_number, f_delete},
562 {"deletebufline", 2, 3, FEARG_1, ret_number, f_deletebufline},
563 {"did_filetype", 0, 0, 0, ret_number, f_did_filetype},
564 {"diff_filler", 1, 1, FEARG_1, ret_number, f_diff_filler},
565 {"diff_hlID", 2, 2, FEARG_1, ret_number, f_diff_hlID},
566 {"echoraw", 1, 1, FEARG_1, ret_number, f_echoraw},
567 {"empty", 1, 1, FEARG_1, ret_number, f_empty},
568 {"environ", 0, 0, 0, ret_dict_string, f_environ},
569 {"escape", 2, 2, FEARG_1, ret_string, f_escape},
570 {"eval", 1, 1, FEARG_1, ret_any, f_eval},
571 {"eventhandler", 0, 0, 0, ret_number, f_eventhandler},
572 {"executable", 1, 1, FEARG_1, ret_number, f_executable},
573 {"execute", 1, 2, FEARG_1, ret_string, f_execute},
574 {"exepath", 1, 1, FEARG_1, ret_string, f_exepath},
575 {"exists", 1, 1, FEARG_1, ret_number, f_exists},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100576 {"exp", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_exp)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100577 {"expand", 1, 3, FEARG_1, ret_any, f_expand},
578 {"expandcmd", 1, 1, FEARG_1, ret_string, f_expandcmd},
Bram Moolenaarb3c019c2020-07-05 20:08:39 +0200579 {"extend", 2, 3, FEARG_1, ret_first_arg, f_extend},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100580 {"feedkeys", 1, 2, FEARG_1, ret_void, f_feedkeys},
581 {"file_readable", 1, 1, FEARG_1, ret_number, f_filereadable}, // obsolete
582 {"filereadable", 1, 1, FEARG_1, ret_number, f_filereadable},
583 {"filewritable", 1, 1, FEARG_1, ret_number, f_filewritable},
Bram Moolenaar0d94ad62020-07-05 20:16:41 +0200584 {"filter", 2, 2, FEARG_1, ret_first_arg, f_filter},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100585 {"finddir", 1, 3, FEARG_1, ret_string, f_finddir},
586 {"findfile", 1, 3, FEARG_1, ret_string, f_findfile},
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200587 {"flatten", 1, 2, FEARG_1, ret_list_any, f_flatten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100588 {"float2nr", 1, 1, FEARG_1, ret_number, FLOAT_FUNC(f_float2nr)},
589 {"floor", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_floor)},
590 {"fmod", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_fmod)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100591 {"fnameescape", 1, 1, FEARG_1, ret_string, f_fnameescape},
592 {"fnamemodify", 2, 2, FEARG_1, ret_string, f_fnamemodify},
593 {"foldclosed", 1, 1, FEARG_1, ret_number, f_foldclosed},
594 {"foldclosedend", 1, 1, FEARG_1, ret_number, f_foldclosedend},
595 {"foldlevel", 1, 1, FEARG_1, ret_number, f_foldlevel},
596 {"foldtext", 0, 0, 0, ret_string, f_foldtext},
597 {"foldtextresult", 1, 1, FEARG_1, ret_string, f_foldtextresult},
598 {"foreground", 0, 0, 0, ret_void, f_foreground},
Bram Moolenaard77a8522020-04-03 21:59:57 +0200599 {"funcref", 1, 3, FEARG_1, ret_func_any, f_funcref},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100600 {"function", 1, 3, FEARG_1, ret_f_function, f_function},
601 {"garbagecollect", 0, 1, 0, ret_void, f_garbagecollect},
602 {"get", 2, 3, FEARG_1, ret_any, f_get},
603 {"getbufinfo", 0, 1, 0, ret_list_dict_any, f_getbufinfo},
604 {"getbufline", 2, 3, FEARG_1, ret_list_string, f_getbufline},
605 {"getbufvar", 2, 3, FEARG_1, ret_any, f_getbufvar},
606 {"getchangelist", 0, 1, FEARG_1, ret_list_any, f_getchangelist},
607 {"getchar", 0, 1, 0, ret_number, f_getchar},
608 {"getcharmod", 0, 0, 0, ret_number, f_getcharmod},
609 {"getcharsearch", 0, 0, 0, ret_dict_any, f_getcharsearch},
610 {"getcmdline", 0, 0, 0, ret_string, f_getcmdline},
611 {"getcmdpos", 0, 0, 0, ret_number, f_getcmdpos},
612 {"getcmdtype", 0, 0, 0, ret_string, f_getcmdtype},
613 {"getcmdwintype", 0, 0, 0, ret_string, f_getcmdwintype},
614 {"getcompletion", 2, 3, FEARG_1, ret_list_string, f_getcompletion},
615 {"getcurpos", 0, 0, 0, ret_list_number, f_getcurpos},
616 {"getcwd", 0, 2, FEARG_1, ret_string, f_getcwd},
617 {"getenv", 1, 1, FEARG_1, ret_string, f_getenv},
618 {"getfontname", 0, 1, 0, ret_string, f_getfontname},
619 {"getfperm", 1, 1, FEARG_1, ret_string, f_getfperm},
620 {"getfsize", 1, 1, FEARG_1, ret_number, f_getfsize},
621 {"getftime", 1, 1, FEARG_1, ret_number, f_getftime},
622 {"getftype", 1, 1, FEARG_1, ret_string, f_getftype},
623 {"getimstatus", 0, 0, 0, ret_number, f_getimstatus},
624 {"getjumplist", 0, 2, FEARG_1, ret_list_any, f_getjumplist},
625 {"getline", 1, 2, FEARG_1, ret_f_getline, f_getline},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200626 {"getloclist", 1, 2, 0, ret_list_or_dict_1, f_getloclist},
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200627 {"getmarklist", 0, 1, FEARG_1, ret_list_dict_any, f_getmarklist},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100628 {"getmatches", 0, 1, 0, ret_list_dict_any, f_getmatches},
629 {"getmousepos", 0, 0, 0, ret_dict_number, f_getmousepos},
630 {"getpid", 0, 0, 0, ret_number, f_getpid},
631 {"getpos", 1, 1, FEARG_1, ret_list_number, f_getpos},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200632 {"getqflist", 0, 1, 0, ret_list_or_dict_0, f_getqflist},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100633 {"getreg", 0, 3, FEARG_1, ret_string, f_getreg},
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200634 {"getreginfo", 0, 1, FEARG_1, ret_dict_any, f_getreginfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100635 {"getregtype", 0, 1, FEARG_1, ret_string, f_getregtype},
636 {"gettabinfo", 0, 1, FEARG_1, ret_list_dict_any, f_gettabinfo},
637 {"gettabvar", 2, 3, FEARG_1, ret_any, f_gettabvar},
638 {"gettabwinvar", 3, 4, FEARG_1, ret_any, f_gettabwinvar},
639 {"gettagstack", 0, 1, FEARG_1, ret_dict_any, f_gettagstack},
640 {"getwininfo", 0, 1, FEARG_1, ret_list_dict_any, f_getwininfo},
641 {"getwinpos", 0, 1, FEARG_1, ret_list_number, f_getwinpos},
642 {"getwinposx", 0, 0, 0, ret_number, f_getwinposx},
643 {"getwinposy", 0, 0, 0, ret_number, f_getwinposy},
644 {"getwinvar", 2, 3, FEARG_1, ret_any, f_getwinvar},
645 {"glob", 1, 4, FEARG_1, ret_any, f_glob},
646 {"glob2regpat", 1, 1, FEARG_1, ret_string, f_glob2regpat},
647 {"globpath", 2, 5, FEARG_2, ret_any, f_globpath},
Bram Moolenaar79296512020-03-22 16:17:14 +0100648 {"has", 1, 2, 0, ret_number, f_has},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100649 {"has_key", 2, 2, FEARG_1, ret_number, f_has_key},
650 {"haslocaldir", 0, 2, FEARG_1, ret_number, f_haslocaldir},
651 {"hasmapto", 1, 3, FEARG_1, ret_number, f_hasmapto},
652 {"highlightID", 1, 1, FEARG_1, ret_number, f_hlID}, // obsolete
653 {"highlight_exists",1, 1, FEARG_1, ret_number, f_hlexists}, // obsolete
654 {"histadd", 2, 2, FEARG_2, ret_number, f_histadd},
655 {"histdel", 1, 2, FEARG_1, ret_number, f_histdel},
656 {"histget", 1, 2, FEARG_1, ret_string, f_histget},
657 {"histnr", 1, 1, FEARG_1, ret_number, f_histnr},
658 {"hlID", 1, 1, FEARG_1, ret_number, f_hlID},
659 {"hlexists", 1, 1, FEARG_1, ret_number, f_hlexists},
660 {"hostname", 0, 0, 0, ret_string, f_hostname},
661 {"iconv", 3, 3, FEARG_1, ret_string, f_iconv},
662 {"indent", 1, 1, FEARG_1, ret_number, f_indent},
663 {"index", 2, 4, FEARG_1, ret_number, f_index},
664 {"input", 1, 3, FEARG_1, ret_string, f_input},
665 {"inputdialog", 1, 3, FEARG_1, ret_string, f_inputdialog},
666 {"inputlist", 1, 1, FEARG_1, ret_number, f_inputlist},
667 {"inputrestore", 0, 0, 0, ret_number, f_inputrestore},
668 {"inputsave", 0, 0, 0, ret_number, f_inputsave},
669 {"inputsecret", 1, 2, FEARG_1, ret_string, f_inputsecret},
670 {"insert", 2, 3, FEARG_1, ret_any, f_insert},
671 {"interrupt", 0, 0, 0, ret_void, f_interrupt},
672 {"invert", 1, 1, FEARG_1, ret_number, f_invert},
673 {"isdirectory", 1, 1, FEARG_1, ret_number, f_isdirectory},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100674 {"isinf", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isinf)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100675 {"islocked", 1, 1, FEARG_1, ret_number, f_islocked},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100676 {"isnan", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isnan)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100677 {"items", 1, 1, FEARG_1, ret_list_any, f_items},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100678 {"job_getchannel", 1, 1, FEARG_1, ret_channel, JOB_FUNC(f_job_getchannel)},
679 {"job_info", 0, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_job_info)},
680 {"job_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_job_setoptions)},
681 {"job_start", 1, 2, FEARG_1, ret_job, JOB_FUNC(f_job_start)},
682 {"job_status", 1, 1, FEARG_1, ret_string, JOB_FUNC(f_job_status)},
683 {"job_stop", 1, 2, FEARG_1, ret_number, JOB_FUNC(f_job_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100684 {"join", 1, 2, FEARG_1, ret_string, f_join},
685 {"js_decode", 1, 1, FEARG_1, ret_any, f_js_decode},
686 {"js_encode", 1, 1, FEARG_1, ret_string, f_js_encode},
687 {"json_decode", 1, 1, FEARG_1, ret_any, f_json_decode},
688 {"json_encode", 1, 1, FEARG_1, ret_string, f_json_encode},
689 {"keys", 1, 1, FEARG_1, ret_list_any, f_keys},
690 {"last_buffer_nr", 0, 0, 0, ret_number, f_last_buffer_nr}, // obsolete
691 {"len", 1, 1, FEARG_1, ret_number, f_len},
692 {"libcall", 3, 3, FEARG_3, ret_string, f_libcall},
693 {"libcallnr", 3, 3, FEARG_3, ret_number, f_libcallnr},
694 {"line", 1, 2, FEARG_1, ret_number, f_line},
695 {"line2byte", 1, 1, FEARG_1, ret_number, f_line2byte},
696 {"lispindent", 1, 1, FEARG_1, ret_number, f_lispindent},
697 {"list2str", 1, 2, FEARG_1, ret_string, f_list2str},
698 {"listener_add", 1, 2, FEARG_2, ret_number, f_listener_add},
699 {"listener_flush", 0, 1, FEARG_1, ret_void, f_listener_flush},
700 {"listener_remove", 1, 1, FEARG_1, ret_number, f_listener_remove},
701 {"localtime", 0, 0, 0, ret_number, f_localtime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100702 {"log", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log)},
703 {"log10", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log10)},
704 {"luaeval", 1, 2, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200705#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +0100706 f_luaeval
707#else
708 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200709#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100710 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100711 {"map", 2, 2, FEARG_1, ret_any, f_map},
712 {"maparg", 1, 4, FEARG_1, ret_string, f_maparg},
713 {"mapcheck", 1, 3, FEARG_1, ret_string, f_mapcheck},
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200714 {"mapset", 3, 3, FEARG_1, ret_void, f_mapset},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100715 {"match", 2, 4, FEARG_1, ret_any, f_match},
716 {"matchadd", 2, 5, FEARG_1, ret_number, f_matchadd},
717 {"matchaddpos", 2, 5, FEARG_1, ret_number, f_matchaddpos},
718 {"matcharg", 1, 1, FEARG_1, ret_list_string, f_matcharg},
719 {"matchdelete", 1, 2, FEARG_1, ret_number, f_matchdelete},
720 {"matchend", 2, 4, FEARG_1, ret_number, f_matchend},
721 {"matchlist", 2, 4, FEARG_1, ret_list_string, f_matchlist},
722 {"matchstr", 2, 4, FEARG_1, ret_string, f_matchstr},
723 {"matchstrpos", 2, 4, FEARG_1, ret_list_any, f_matchstrpos},
724 {"max", 1, 1, FEARG_1, ret_any, f_max},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100725 {"menu_info", 1, 2, FEARG_1, ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100726#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +0100727 f_menu_info
728#else
729 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100730#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100731 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100732 {"min", 1, 1, FEARG_1, ret_any, f_min},
733 {"mkdir", 1, 3, FEARG_1, ret_number, f_mkdir},
734 {"mode", 0, 1, FEARG_1, ret_string, f_mode},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100735 {"mzeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200736#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100737 f_mzeval
738#else
739 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200740#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100741 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100742 {"nextnonblank", 1, 1, FEARG_1, ret_number, f_nextnonblank},
743 {"nr2char", 1, 2, FEARG_1, ret_string, f_nr2char},
744 {"or", 2, 2, FEARG_1, ret_number, f_or},
745 {"pathshorten", 1, 1, FEARG_1, ret_string, f_pathshorten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100746 {"perleval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200747#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100748 f_perleval
749#else
750 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200751#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100752 },
753 {"popup_atcursor", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_atcursor)},
754 {"popup_beval", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_beval)},
Bram Moolenaar03a9f842020-05-13 13:40:16 +0200755 {"popup_clear", 0, 1, 0, ret_void, PROP_FUNC(f_popup_clear)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100756 {"popup_close", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_close)},
757 {"popup_create", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_create)},
758 {"popup_dialog", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_dialog)},
759 {"popup_filter_menu", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_menu)},
760 {"popup_filter_yesno", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_yesno)},
761 {"popup_findinfo", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findinfo)},
762 {"popup_findpreview", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findpreview)},
763 {"popup_getoptions", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getoptions)},
764 {"popup_getpos", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getpos)},
765 {"popup_hide", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_hide)},
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200766 {"popup_list", 0, 0, 0, ret_list_number, PROP_FUNC(f_popup_list)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100767 {"popup_locate", 2, 2, 0, ret_number, PROP_FUNC(f_popup_locate)},
768 {"popup_menu", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_menu)},
769 {"popup_move", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_move)},
770 {"popup_notification", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_notification)},
771 {"popup_setoptions", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_setoptions)},
772 {"popup_settext", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_settext)},
773 {"popup_show", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_show)},
774 {"pow", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_pow)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100775 {"prevnonblank", 1, 1, FEARG_1, ret_number, f_prevnonblank},
776 {"printf", 1, 19, FEARG_2, ret_string, f_printf},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100777 {"prompt_setcallback", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setcallback)},
778 {"prompt_setinterrupt", 2, 2, FEARG_1,ret_void, JOB_FUNC(f_prompt_setinterrupt)},
779 {"prompt_setprompt", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setprompt)},
780 {"prop_add", 3, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_add)},
781 {"prop_clear", 1, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_clear)},
782 {"prop_find", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_find)},
783 {"prop_list", 1, 2, FEARG_1, ret_list_dict_any, PROP_FUNC(f_prop_list)},
784 {"prop_remove", 1, 3, FEARG_1, ret_number, PROP_FUNC(f_prop_remove)},
785 {"prop_type_add", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_add)},
786 {"prop_type_change", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_change)},
787 {"prop_type_delete", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_delete)},
788 {"prop_type_get", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_type_get)},
789 {"prop_type_list", 0, 1, FEARG_1, ret_list_string, PROP_FUNC(f_prop_type_list)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100790 {"pum_getpos", 0, 0, 0, ret_dict_number, f_pum_getpos},
791 {"pumvisible", 0, 0, 0, ret_number, f_pumvisible},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100792 {"py3eval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200793#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +0100794 f_py3eval
795#else
796 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200797#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100798 },
799 {"pyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200800#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +0100801 f_pyeval
802#else
803 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200804#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100805 },
806 {"pyxeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100807#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +0100808 f_pyxeval
809#else
810 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100811#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100812 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100813 {"rand", 0, 1, FEARG_1, ret_number, f_rand},
814 {"range", 1, 3, FEARG_1, ret_list_number, f_range},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200815 {"readdir", 1, 3, FEARG_1, ret_list_string, f_readdir},
816 {"readdirex", 1, 3, FEARG_1, ret_list_dict_any, f_readdirex},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100817 {"readfile", 1, 3, FEARG_1, ret_any, f_readfile},
Bram Moolenaar85629982020-06-01 18:39:20 +0200818 {"reduce", 2, 3, FEARG_1, ret_any, f_reduce},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100819 {"reg_executing", 0, 0, 0, ret_string, f_reg_executing},
820 {"reg_recording", 0, 0, 0, ret_string, f_reg_recording},
821 {"reltime", 0, 2, FEARG_1, ret_list_any, f_reltime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100822 {"reltimefloat", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_reltimefloat)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100823 {"reltimestr", 1, 1, FEARG_1, ret_string, f_reltimestr},
824 {"remote_expr", 2, 4, FEARG_1, ret_string, f_remote_expr},
825 {"remote_foreground", 1, 1, FEARG_1, ret_string, f_remote_foreground},
826 {"remote_peek", 1, 2, FEARG_1, ret_number, f_remote_peek},
827 {"remote_read", 1, 2, FEARG_1, ret_string, f_remote_read},
828 {"remote_send", 2, 3, FEARG_1, ret_string, f_remote_send},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200829 {"remote_startserver", 1, 1, FEARG_1, ret_void, f_remote_startserver},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100830 {"remove", 2, 3, FEARG_1, ret_any, f_remove},
831 {"rename", 2, 2, FEARG_1, ret_number, f_rename},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200832 {"repeat", 2, 2, FEARG_1, ret_first_arg, f_repeat},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100833 {"resolve", 1, 1, FEARG_1, ret_string, f_resolve},
834 {"reverse", 1, 1, FEARG_1, ret_any, f_reverse},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100835 {"round", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_round)},
836 {"rubyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100837#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +0100838 f_rubyeval
839#else
840 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100841#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100842 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100843 {"screenattr", 2, 2, FEARG_1, ret_number, f_screenattr},
844 {"screenchar", 2, 2, FEARG_1, ret_number, f_screenchar},
845 {"screenchars", 2, 2, FEARG_1, ret_list_number, f_screenchars},
846 {"screencol", 0, 0, 0, ret_number, f_screencol},
847 {"screenpos", 3, 3, FEARG_1, ret_dict_number, f_screenpos},
848 {"screenrow", 0, 0, 0, ret_number, f_screenrow},
849 {"screenstring", 2, 2, FEARG_1, ret_string, f_screenstring},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200850 {"search", 1, 5, FEARG_1, ret_number, f_search},
Bram Moolenaare8f5ec02020-06-01 17:28:35 +0200851 {"searchcount", 0, 1, FEARG_1, ret_dict_any, f_searchcount},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100852 {"searchdecl", 1, 3, FEARG_1, ret_number, f_searchdecl},
853 {"searchpair", 3, 7, 0, ret_number, f_searchpair},
854 {"searchpairpos", 3, 7, 0, ret_list_number, f_searchpairpos},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200855 {"searchpos", 1, 5, FEARG_1, ret_list_number, f_searchpos},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100856 {"server2client", 2, 2, FEARG_1, ret_number, f_server2client},
857 {"serverlist", 0, 0, 0, ret_string, f_serverlist},
858 {"setbufline", 3, 3, FEARG_3, ret_number, f_setbufline},
859 {"setbufvar", 3, 3, FEARG_3, ret_void, f_setbufvar},
860 {"setcharsearch", 1, 1, FEARG_1, ret_void, f_setcharsearch},
861 {"setcmdpos", 1, 1, FEARG_1, ret_number, f_setcmdpos},
862 {"setenv", 2, 2, FEARG_2, ret_void, f_setenv},
863 {"setfperm", 2, 2, FEARG_1, ret_number, f_setfperm},
864 {"setline", 2, 2, FEARG_2, ret_number, f_setline},
865 {"setloclist", 2, 4, FEARG_2, ret_number, f_setloclist},
866 {"setmatches", 1, 2, FEARG_1, ret_number, f_setmatches},
867 {"setpos", 2, 2, FEARG_2, ret_number, f_setpos},
868 {"setqflist", 1, 3, FEARG_1, ret_number, f_setqflist},
869 {"setreg", 2, 3, FEARG_2, ret_number, f_setreg},
870 {"settabvar", 3, 3, FEARG_3, ret_void, f_settabvar},
871 {"settabwinvar", 4, 4, FEARG_4, ret_void, f_settabwinvar},
872 {"settagstack", 2, 3, FEARG_2, ret_number, f_settagstack},
873 {"setwinvar", 3, 3, FEARG_3, ret_void, f_setwinvar},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100874 {"sha256", 1, 1, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200875#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +0100876 f_sha256
877#else
878 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200879#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100880 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100881 {"shellescape", 1, 2, FEARG_1, ret_string, f_shellescape},
882 {"shiftwidth", 0, 1, FEARG_1, ret_number, f_shiftwidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100883 {"sign_define", 1, 2, FEARG_1, ret_any, SIGN_FUNC(f_sign_define)},
884 {"sign_getdefined", 0, 1, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
885 {"sign_getplaced", 0, 2, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
886 {"sign_jump", 3, 3, FEARG_1, ret_number, SIGN_FUNC(f_sign_jump)},
887 {"sign_place", 4, 5, FEARG_1, ret_number, SIGN_FUNC(f_sign_place)},
888 {"sign_placelist", 1, 1, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_placelist)},
889 {"sign_undefine", 0, 1, FEARG_1, ret_number, SIGN_FUNC(f_sign_undefine)},
890 {"sign_unplace", 1, 2, FEARG_1, ret_number, SIGN_FUNC(f_sign_unplace)},
891 {"sign_unplacelist", 1, 2, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200892 {"simplify", 1, 1, FEARG_1, ret_string, f_simplify},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100893 {"sin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sin)},
894 {"sinh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200895 {"sort", 1, 3, FEARG_1, ret_first_arg, f_sort},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100896 {"sound_clear", 0, 0, 0, ret_void, SOUND_FUNC(f_sound_clear)},
897 {"sound_playevent", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playevent)},
898 {"sound_playfile", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playfile)},
899 {"sound_stop", 1, 1, FEARG_1, ret_void, SOUND_FUNC(f_sound_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100900 {"soundfold", 1, 1, FEARG_1, ret_string, f_soundfold},
901 {"spellbadword", 0, 1, FEARG_1, ret_list_string, f_spellbadword},
902 {"spellsuggest", 1, 3, FEARG_1, ret_list_string, f_spellsuggest},
903 {"split", 1, 3, FEARG_1, ret_list_string, f_split},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100904 {"sqrt", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sqrt)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100905 {"srand", 0, 1, FEARG_1, ret_list_number, f_srand},
906 {"state", 0, 1, FEARG_1, ret_string, f_state},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100907 {"str2float", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_str2float)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100908 {"str2list", 1, 2, FEARG_1, ret_list_number, f_str2list},
909 {"str2nr", 1, 3, FEARG_1, ret_number, f_str2nr},
910 {"strcharpart", 2, 3, FEARG_1, ret_string, f_strcharpart},
911 {"strchars", 1, 2, FEARG_1, ret_number, f_strchars},
912 {"strdisplaywidth", 1, 2, FEARG_1, ret_number, f_strdisplaywidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100913 {"strftime", 1, 2, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200914#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100915 f_strftime
916#else
917 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200918#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100919 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100920 {"strgetchar", 2, 2, FEARG_1, ret_number, f_strgetchar},
921 {"stridx", 2, 3, FEARG_1, ret_number, f_stridx},
922 {"string", 1, 1, FEARG_1, ret_string, f_string},
923 {"strlen", 1, 1, FEARG_1, ret_number, f_strlen},
924 {"strpart", 2, 3, FEARG_1, ret_string, f_strpart},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100925 {"strptime", 2, 2, FEARG_1, ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +0100926#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100927 f_strptime
928#else
929 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +0100930#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100931 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100932 {"strridx", 2, 3, FEARG_1, ret_number, f_strridx},
933 {"strtrans", 1, 1, FEARG_1, ret_string, f_strtrans},
934 {"strwidth", 1, 1, FEARG_1, ret_number, f_strwidth},
935 {"submatch", 1, 2, FEARG_1, ret_string, f_submatch},
936 {"substitute", 4, 4, FEARG_1, ret_string, f_substitute},
937 {"swapinfo", 1, 1, FEARG_1, ret_dict_any, f_swapinfo},
938 {"swapname", 1, 1, FEARG_1, ret_string, f_swapname},
939 {"synID", 3, 3, 0, ret_number, f_synID},
940 {"synIDattr", 2, 3, FEARG_1, ret_string, f_synIDattr},
941 {"synIDtrans", 1, 1, FEARG_1, ret_number, f_synIDtrans},
942 {"synconcealed", 2, 2, 0, ret_list_any, f_synconcealed},
943 {"synstack", 2, 2, 0, ret_list_number, f_synstack},
944 {"system", 1, 2, FEARG_1, ret_string, f_system},
945 {"systemlist", 1, 2, FEARG_1, ret_list_string, f_systemlist},
946 {"tabpagebuflist", 0, 1, FEARG_1, ret_list_number, f_tabpagebuflist},
947 {"tabpagenr", 0, 1, 0, ret_number, f_tabpagenr},
948 {"tabpagewinnr", 1, 2, FEARG_1, ret_number, f_tabpagewinnr},
949 {"tagfiles", 0, 0, 0, ret_list_string, f_tagfiles},
950 {"taglist", 1, 2, FEARG_1, ret_list_dict_any, f_taglist},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100951 {"tan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tan)},
952 {"tanh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tanh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100953 {"tempname", 0, 0, 0, ret_string, f_tempname},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100954 {"term_dumpdiff", 2, 3, FEARG_1, ret_number, TERM_FUNC(f_term_dumpdiff)},
955 {"term_dumpload", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_dumpload)},
956 {"term_dumpwrite", 2, 3, FEARG_2, ret_void, TERM_FUNC(f_term_dumpwrite)},
957 {"term_getaltscreen", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getaltscreen)},
958 {"term_getansicolors", 1, 1, FEARG_1, ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100959#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100960 f_term_getansicolors
961#else
962 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200963#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100964 },
965 {"term_getattr", 2, 2, FEARG_1, ret_number, TERM_FUNC(f_term_getattr)},
966 {"term_getcursor", 1, 1, FEARG_1, ret_list_any, TERM_FUNC(f_term_getcursor)},
967 {"term_getjob", 1, 1, FEARG_1, ret_job, TERM_FUNC(f_term_getjob)},
968 {"term_getline", 2, 2, FEARG_1, ret_string, TERM_FUNC(f_term_getline)},
969 {"term_getscrolled", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getscrolled)},
970 {"term_getsize", 1, 1, FEARG_1, ret_list_number, TERM_FUNC(f_term_getsize)},
971 {"term_getstatus", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_getstatus)},
972 {"term_gettitle", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_gettitle)},
973 {"term_gettty", 1, 2, FEARG_1, ret_string, TERM_FUNC(f_term_gettty)},
974 {"term_list", 0, 0, 0, ret_list_number, TERM_FUNC(f_term_list)},
975 {"term_scrape", 2, 2, FEARG_1, ret_list_dict_any, TERM_FUNC(f_term_scrape)},
976 {"term_sendkeys", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_sendkeys)},
977 {"term_setansicolors", 2, 2, FEARG_1, ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100978#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100979 f_term_setansicolors
980#else
981 NULL
982#endif
983 },
984 {"term_setapi", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setapi)},
985 {"term_setkill", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setkill)},
986 {"term_setrestore", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setrestore)},
987 {"term_setsize", 3, 3, FEARG_1, ret_void, TERM_FUNC(f_term_setsize)},
988 {"term_start", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_start)},
989 {"term_wait", 1, 2, FEARG_1, ret_void, TERM_FUNC(f_term_wait)},
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200990 {"terminalprops", 0, 0, 0, ret_dict_string, f_terminalprops},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100991 {"test_alloc_fail", 3, 3, FEARG_1, ret_void, f_test_alloc_fail},
992 {"test_autochdir", 0, 0, 0, ret_void, f_test_autochdir},
993 {"test_feedinput", 1, 1, FEARG_1, ret_void, f_test_feedinput},
994 {"test_garbagecollect_now", 0, 0, 0, ret_void, f_test_garbagecollect_now},
995 {"test_garbagecollect_soon", 0, 0, 0, ret_void, f_test_garbagecollect_soon},
996 {"test_getvalue", 1, 1, FEARG_1, ret_number, f_test_getvalue},
997 {"test_ignore_error", 1, 1, FEARG_1, ret_void, f_test_ignore_error},
998 {"test_null_blob", 0, 0, 0, ret_blob, f_test_null_blob},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100999 {"test_null_channel", 0, 0, 0, ret_channel, JOB_FUNC(f_test_null_channel)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001000 {"test_null_dict", 0, 0, 0, ret_dict_any, f_test_null_dict},
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001001 {"test_null_function", 0, 0, 0, ret_func_any, f_test_null_function},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001002 {"test_null_job", 0, 0, 0, ret_job, JOB_FUNC(f_test_null_job)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001003 {"test_null_list", 0, 0, 0, ret_list_any, f_test_null_list},
Bram Moolenaard77a8522020-04-03 21:59:57 +02001004 {"test_null_partial", 0, 0, 0, ret_func_any, f_test_null_partial},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001005 {"test_null_string", 0, 0, 0, ret_string, f_test_null_string},
1006 {"test_option_not_set", 1, 1, FEARG_1,ret_void, f_test_option_not_set},
1007 {"test_override", 2, 2, FEARG_2, ret_void, f_test_override},
1008 {"test_refcount", 1, 1, FEARG_1, ret_number, f_test_refcount},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001009 {"test_scrollbar", 3, 3, FEARG_2, ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001010#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001011 f_test_scrollbar
1012#else
1013 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001014#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001015 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001016 {"test_setmouse", 2, 2, 0, ret_void, f_test_setmouse},
1017 {"test_settime", 1, 1, FEARG_1, ret_void, f_test_settime},
1018 {"test_srand_seed", 0, 1, FEARG_1, ret_void, f_test_srand_seed},
1019 {"test_unknown", 0, 0, 0, ret_any, f_test_unknown},
1020 {"test_void", 0, 0, 0, ret_any, f_test_void},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001021 {"timer_info", 0, 1, FEARG_1, ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1022 {"timer_pause", 2, 2, FEARG_1, ret_void, TIMER_FUNC(f_timer_pause)},
1023 {"timer_start", 2, 3, FEARG_1, ret_number, TIMER_FUNC(f_timer_start)},
1024 {"timer_stop", 1, 1, FEARG_1, ret_void, TIMER_FUNC(f_timer_stop)},
1025 {"timer_stopall", 0, 0, 0, ret_void, TIMER_FUNC(f_timer_stopall)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001026 {"tolower", 1, 1, FEARG_1, ret_string, f_tolower},
1027 {"toupper", 1, 1, FEARG_1, ret_string, f_toupper},
1028 {"tr", 3, 3, FEARG_1, ret_string, f_tr},
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001029 {"trim", 1, 3, FEARG_1, ret_string, f_trim},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001030 {"trunc", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_trunc)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001031 {"type", 1, 1, FEARG_1, ret_number, f_type},
1032 {"undofile", 1, 1, FEARG_1, ret_string, f_undofile},
1033 {"undotree", 0, 0, 0, ret_dict_any, f_undotree},
1034 {"uniq", 1, 3, FEARG_1, ret_list_any, f_uniq},
1035 {"values", 1, 1, FEARG_1, ret_list_any, f_values},
1036 {"virtcol", 1, 1, FEARG_1, ret_number, f_virtcol},
1037 {"visualmode", 0, 1, 0, ret_string, f_visualmode},
1038 {"wildmenumode", 0, 0, 0, ret_number, f_wildmenumode},
1039 {"win_execute", 2, 3, FEARG_2, ret_string, f_win_execute},
1040 {"win_findbuf", 1, 1, FEARG_1, ret_list_number, f_win_findbuf},
1041 {"win_getid", 0, 2, FEARG_1, ret_number, f_win_getid},
1042 {"win_gettype", 0, 1, FEARG_1, ret_string, f_win_gettype},
1043 {"win_gotoid", 1, 1, FEARG_1, ret_number, f_win_gotoid},
1044 {"win_id2tabwin", 1, 1, FEARG_1, ret_list_number, f_win_id2tabwin},
1045 {"win_id2win", 1, 1, FEARG_1, ret_number, f_win_id2win},
1046 {"win_screenpos", 1, 1, FEARG_1, ret_list_number, f_win_screenpos},
1047 {"win_splitmove", 2, 3, FEARG_1, ret_number, f_win_splitmove},
1048 {"winbufnr", 1, 1, FEARG_1, ret_number, f_winbufnr},
1049 {"wincol", 0, 0, 0, ret_number, f_wincol},
1050 {"windowsversion", 0, 0, 0, ret_string, f_windowsversion},
1051 {"winheight", 1, 1, FEARG_1, ret_number, f_winheight},
1052 {"winlayout", 0, 1, FEARG_1, ret_list_any, f_winlayout},
1053 {"winline", 0, 0, 0, ret_number, f_winline},
1054 {"winnr", 0, 1, FEARG_1, ret_number, f_winnr},
1055 {"winrestcmd", 0, 0, 0, ret_string, f_winrestcmd},
1056 {"winrestview", 1, 1, FEARG_1, ret_void, f_winrestview},
1057 {"winsaveview", 0, 0, 0, ret_dict_any, f_winsaveview},
1058 {"winwidth", 1, 1, FEARG_1, ret_number, f_winwidth},
1059 {"wordcount", 0, 0, 0, ret_dict_number, f_wordcount},
1060 {"writefile", 2, 3, FEARG_1, ret_number, f_writefile},
1061 {"xor", 2, 2, FEARG_1, ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001062};
1063
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001064/*
1065 * Function given to ExpandGeneric() to obtain the list of internal
1066 * or user defined function names.
1067 */
1068 char_u *
1069get_function_name(expand_T *xp, int idx)
1070{
1071 static int intidx = -1;
1072 char_u *name;
1073
1074 if (idx == 0)
1075 intidx = -1;
1076 if (intidx < 0)
1077 {
1078 name = get_user_func_name(xp, idx);
1079 if (name != NULL)
1080 return name;
1081 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001082 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001083 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001084 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001085 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001086 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001087 STRCAT(IObuff, ")");
1088 return IObuff;
1089 }
1090
1091 return NULL;
1092}
1093
1094/*
1095 * Function given to ExpandGeneric() to obtain the list of internal or
1096 * user defined variable or function names.
1097 */
1098 char_u *
1099get_expr_name(expand_T *xp, int idx)
1100{
1101 static int intidx = -1;
1102 char_u *name;
1103
1104 if (idx == 0)
1105 intidx = -1;
1106 if (intidx < 0)
1107 {
1108 name = get_function_name(xp, idx);
1109 if (name != NULL)
1110 return name;
1111 }
1112 return get_user_var_name(xp, ++intidx);
1113}
1114
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001115/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001116 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001117 * Return index, or -1 if not found or "implemented" is TRUE and the function
1118 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001119 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001120 static int
1121find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001122{
1123 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001124 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001125 int cmp;
1126 int x;
1127
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001128 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001129
1130 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001131 while (first <= last)
1132 {
1133 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001134 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001135 if (cmp < 0)
1136 last = x - 1;
1137 else if (cmp > 0)
1138 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001139 else if (implemented && global_functions[x].f_func == NULL)
1140 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001141 else
1142 return x;
1143 }
1144 return -1;
1145}
1146
Bram Moolenaar15c47602020-03-26 22:16:48 +01001147/*
1148 * Find internal function "name" in table "global_functions".
1149 * Return index, or -1 if not found or the function is not implemented.
1150 */
1151 int
1152find_internal_func(char_u *name)
1153{
1154 return find_internal_func_opt(name, TRUE);
1155}
1156
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001157 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001158has_internal_func(char_u *name)
1159{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001160 return find_internal_func_opt(name, TRUE) >= 0;
1161}
1162
1163 static int
1164has_internal_func_name(char_u *name)
1165{
1166 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001167}
1168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001169 char *
1170internal_func_name(int idx)
1171{
1172 return global_functions[idx].f_name;
1173}
1174
1175 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001176internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001178 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179}
1180
1181/*
1182 * Check the argument count to use for internal function "idx".
1183 * Returns OK or FAIL;
1184 */
1185 int
1186check_internal_func(int idx, int argcount)
1187{
1188 int res;
1189 char *name;
1190
1191 if (argcount < global_functions[idx].f_min_argc)
1192 res = FCERR_TOOFEW;
1193 else if (argcount > global_functions[idx].f_max_argc)
1194 res = FCERR_TOOMANY;
1195 else
1196 return OK;
1197
1198 name = internal_func_name(idx);
1199 if (res == FCERR_TOOMANY)
1200 semsg(_(e_toomanyarg), name);
1201 else
1202 semsg(_(e_toofewarg), name);
1203 return FAIL;
1204}
1205
Bram Moolenaarac92e252019-08-03 21:58:38 +02001206 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001207call_internal_func(
1208 char_u *name,
1209 int argcount,
1210 typval_T *argvars,
1211 typval_T *rettv)
1212{
1213 int i;
1214
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001215 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001216 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001217 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001218 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001219 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001220 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001221 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001222 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001223 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001224 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001225}
1226
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001227 void
1228call_internal_func_by_idx(
1229 int idx,
1230 typval_T *argvars,
1231 typval_T *rettv)
1232{
1233 global_functions[idx].f_func(argvars, rettv);
1234}
1235
Bram Moolenaarac92e252019-08-03 21:58:38 +02001236/*
1237 * Invoke a method for base->method().
1238 */
1239 int
1240call_internal_method(
1241 char_u *name,
1242 int argcount,
1243 typval_T *argvars,
1244 typval_T *rettv,
1245 typval_T *basetv)
1246{
1247 int i;
1248 int fi;
1249 typval_T argv[MAX_FUNC_ARGS + 1];
1250
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001251 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02001252 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001253 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02001254 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001255 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001256 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001257 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001258 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001259 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001260
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001261 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001262 {
1263 // base value goes last
1264 for (i = 0; i < argcount; ++i)
1265 argv[i] = argvars[i];
1266 argv[argcount] = *basetv;
1267 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001268 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001269 {
1270 // base value goes second
1271 argv[0] = argvars[0];
1272 argv[1] = *basetv;
1273 for (i = 1; i < argcount; ++i)
1274 argv[i + 1] = argvars[i];
1275 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02001276 else if (global_functions[fi].f_argtype == FEARG_3)
1277 {
1278 // base value goes third
1279 argv[0] = argvars[0];
1280 argv[1] = argvars[1];
1281 argv[2] = *basetv;
1282 for (i = 2; i < argcount; ++i)
1283 argv[i + 1] = argvars[i];
1284 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001285 else if (global_functions[fi].f_argtype == FEARG_4)
1286 {
1287 // base value goes fourth
1288 argv[0] = argvars[0];
1289 argv[1] = argvars[1];
1290 argv[2] = argvars[2];
1291 argv[3] = *basetv;
1292 for (i = 3; i < argcount; ++i)
1293 argv[i + 1] = argvars[i];
1294 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02001295 else
1296 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001297 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02001298 argv[0] = *basetv;
1299 for (i = 0; i < argcount; ++i)
1300 argv[i + 1] = argvars[i];
1301 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001302 argv[argcount + 1].v_type = VAR_UNKNOWN;
1303
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001304 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001305 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001306}
1307
1308/*
1309 * Return TRUE for a non-zero Number and a non-empty String.
1310 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001311 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001312non_zero_arg(typval_T *argvars)
1313{
1314 return ((argvars[0].v_type == VAR_NUMBER
1315 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001316 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001317 && argvars[0].vval.v_number == VVAL_TRUE)
1318 || (argvars[0].v_type == VAR_STRING
1319 && argvars[0].vval.v_string != NULL
1320 && *argvars[0].vval.v_string != NUL));
1321}
1322
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001323#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001324/*
1325 * Get the float value of "argvars[0]" into "f".
1326 * Returns FAIL when the argument is not a Number or Float.
1327 */
1328 static int
1329get_float_arg(typval_T *argvars, float_T *f)
1330{
1331 if (argvars[0].v_type == VAR_FLOAT)
1332 {
1333 *f = argvars[0].vval.v_float;
1334 return OK;
1335 }
1336 if (argvars[0].v_type == VAR_NUMBER)
1337 {
1338 *f = (float_T)argvars[0].vval.v_number;
1339 return OK;
1340 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001341 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001342 return FAIL;
1343}
1344
1345/*
1346 * "abs(expr)" function
1347 */
1348 static void
1349f_abs(typval_T *argvars, typval_T *rettv)
1350{
1351 if (argvars[0].v_type == VAR_FLOAT)
1352 {
1353 rettv->v_type = VAR_FLOAT;
1354 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1355 }
1356 else
1357 {
1358 varnumber_T n;
1359 int error = FALSE;
1360
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001361 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001362 if (error)
1363 rettv->vval.v_number = -1;
1364 else if (n > 0)
1365 rettv->vval.v_number = n;
1366 else
1367 rettv->vval.v_number = -n;
1368 }
1369}
1370
1371/*
1372 * "acos()" function
1373 */
1374 static void
1375f_acos(typval_T *argvars, typval_T *rettv)
1376{
1377 float_T f = 0.0;
1378
1379 rettv->v_type = VAR_FLOAT;
1380 if (get_float_arg(argvars, &f) == OK)
1381 rettv->vval.v_float = acos(f);
1382 else
1383 rettv->vval.v_float = 0.0;
1384}
1385#endif
1386
1387/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001388 * "and(expr, expr)" function
1389 */
1390 static void
1391f_and(typval_T *argvars, typval_T *rettv)
1392{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001393 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1394 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001395}
1396
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001397#ifdef FEAT_FLOAT
1398/*
1399 * "asin()" function
1400 */
1401 static void
1402f_asin(typval_T *argvars, typval_T *rettv)
1403{
1404 float_T f = 0.0;
1405
1406 rettv->v_type = VAR_FLOAT;
1407 if (get_float_arg(argvars, &f) == OK)
1408 rettv->vval.v_float = asin(f);
1409 else
1410 rettv->vval.v_float = 0.0;
1411}
1412
1413/*
1414 * "atan()" function
1415 */
1416 static void
1417f_atan(typval_T *argvars, typval_T *rettv)
1418{
1419 float_T f = 0.0;
1420
1421 rettv->v_type = VAR_FLOAT;
1422 if (get_float_arg(argvars, &f) == OK)
1423 rettv->vval.v_float = atan(f);
1424 else
1425 rettv->vval.v_float = 0.0;
1426}
1427
1428/*
1429 * "atan2()" function
1430 */
1431 static void
1432f_atan2(typval_T *argvars, typval_T *rettv)
1433{
1434 float_T fx = 0.0, fy = 0.0;
1435
1436 rettv->v_type = VAR_FLOAT;
1437 if (get_float_arg(argvars, &fx) == OK
1438 && get_float_arg(&argvars[1], &fy) == OK)
1439 rettv->vval.v_float = atan2(fx, fy);
1440 else
1441 rettv->vval.v_float = 0.0;
1442}
1443#endif
1444
1445/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001446 * "balloon_show()" function
1447 */
1448#ifdef FEAT_BEVAL
1449 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001450f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1451{
1452 rettv->v_type = VAR_STRING;
1453 if (balloonEval != NULL)
1454 {
1455 if (balloonEval->msg == NULL)
1456 rettv->vval.v_string = NULL;
1457 else
1458 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1459 }
1460}
1461
1462 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001463f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1464{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001465 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001466 {
1467 if (argvars[0].v_type == VAR_LIST
1468# ifdef FEAT_GUI
1469 && !gui.in_use
1470# endif
1471 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001472 {
1473 list_T *l = argvars[0].vval.v_list;
1474
1475 // empty list removes the balloon
1476 post_balloon(balloonEval, NULL,
1477 l == NULL || l->lv_len == 0 ? NULL : l);
1478 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001479 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001480 {
1481 char_u *mesg = tv_get_string_chk(&argvars[0]);
1482
1483 if (mesg != NULL)
1484 // empty string removes the balloon
1485 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1486 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001487 }
1488}
1489
Bram Moolenaar669a8282017-11-19 20:13:05 +01001490# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001491 static void
1492f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1493{
1494 if (rettv_list_alloc(rettv) == OK)
1495 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001496 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001497
1498 if (msg != NULL)
1499 {
1500 pumitem_T *array;
1501 int size = split_message(msg, &array);
1502 int i;
1503
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001504 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001505 for (i = 1; i < size - 1; ++i)
1506 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001507 while (size > 0)
1508 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001509 vim_free(array);
1510 }
1511 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001512}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001513# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001514#endif
1515
1516/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001517 * Get the buffer from "arg" and give an error and return NULL if it is not
1518 * valid.
1519 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001520 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001521get_buf_arg(typval_T *arg)
1522{
1523 buf_T *buf;
1524
1525 ++emsg_off;
1526 buf = tv_get_buf(arg, FALSE);
1527 --emsg_off;
1528 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001529 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001530 return buf;
1531}
1532
1533/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001534 * "byte2line(byte)" function
1535 */
1536 static void
1537f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1538{
1539#ifndef FEAT_BYTEOFF
1540 rettv->vval.v_number = -1;
1541#else
1542 long boff = 0;
1543
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001544 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001545 if (boff < 0)
1546 rettv->vval.v_number = -1;
1547 else
1548 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1549 (linenr_T)0, &boff);
1550#endif
1551}
1552
1553 static void
1554byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1555{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001556 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001557 char_u *str;
1558 varnumber_T idx;
1559
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001560 str = tv_get_string_chk(&argvars[0]);
1561 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001562 rettv->vval.v_number = -1;
1563 if (str == NULL || idx < 0)
1564 return;
1565
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001566 t = str;
1567 for ( ; idx > 0; idx--)
1568 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001569 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001570 return;
1571 if (enc_utf8 && comp)
1572 t += utf_ptr2len(t);
1573 else
1574 t += (*mb_ptr2len)(t);
1575 }
1576 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001577}
1578
1579/*
1580 * "byteidx()" function
1581 */
1582 static void
1583f_byteidx(typval_T *argvars, typval_T *rettv)
1584{
1585 byteidx(argvars, rettv, FALSE);
1586}
1587
1588/*
1589 * "byteidxcomp()" function
1590 */
1591 static void
1592f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1593{
1594 byteidx(argvars, rettv, TRUE);
1595}
1596
1597/*
1598 * "call(func, arglist [, dict])" function
1599 */
1600 static void
1601f_call(typval_T *argvars, typval_T *rettv)
1602{
1603 char_u *func;
1604 partial_T *partial = NULL;
1605 dict_T *selfdict = NULL;
1606
1607 if (argvars[1].v_type != VAR_LIST)
1608 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001609 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001610 return;
1611 }
1612 if (argvars[1].vval.v_list == NULL)
1613 return;
1614
1615 if (argvars[0].v_type == VAR_FUNC)
1616 func = argvars[0].vval.v_string;
1617 else if (argvars[0].v_type == VAR_PARTIAL)
1618 {
1619 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001620 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001621 }
1622 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001623 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001624 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001625 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001626
1627 if (argvars[2].v_type != VAR_UNKNOWN)
1628 {
1629 if (argvars[2].v_type != VAR_DICT)
1630 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001631 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001632 return;
1633 }
1634 selfdict = argvars[2].vval.v_dict;
1635 }
1636
1637 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1638}
1639
1640#ifdef FEAT_FLOAT
1641/*
1642 * "ceil({float})" function
1643 */
1644 static void
1645f_ceil(typval_T *argvars, typval_T *rettv)
1646{
1647 float_T f = 0.0;
1648
1649 rettv->v_type = VAR_FLOAT;
1650 if (get_float_arg(argvars, &f) == OK)
1651 rettv->vval.v_float = ceil(f);
1652 else
1653 rettv->vval.v_float = 0.0;
1654}
1655#endif
1656
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001657/*
1658 * "changenr()" function
1659 */
1660 static void
1661f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
1662{
1663 rettv->vval.v_number = curbuf->b_u_seq_cur;
1664}
1665
1666/*
1667 * "char2nr(string)" function
1668 */
1669 static void
1670f_char2nr(typval_T *argvars, typval_T *rettv)
1671{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001672 if (has_mbyte)
1673 {
1674 int utf8 = 0;
1675
1676 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001677 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001678
1679 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01001680 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001681 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001682 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001683 }
1684 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001685 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001686}
1687
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001688 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01001689get_optional_window(typval_T *argvars, int idx)
1690{
1691 win_T *win = curwin;
1692
1693 if (argvars[idx].v_type != VAR_UNKNOWN)
1694 {
1695 win = find_win_by_nr_or_id(&argvars[idx]);
1696 if (win == NULL)
1697 {
1698 emsg(_(e_invalwindow));
1699 return NULL;
1700 }
1701 }
1702 return win;
1703}
1704
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001705/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001706 * "col(string)" function
1707 */
1708 static void
1709f_col(typval_T *argvars, typval_T *rettv)
1710{
1711 colnr_T col = 0;
1712 pos_T *fp;
1713 int fnum = curbuf->b_fnum;
1714
1715 fp = var2fpos(&argvars[0], FALSE, &fnum);
1716 if (fp != NULL && fnum == curbuf->b_fnum)
1717 {
1718 if (fp->col == MAXCOL)
1719 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001720 // '> can be MAXCOL, get the length of the line then
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001721 if (fp->lnum <= curbuf->b_ml.ml_line_count)
1722 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
1723 else
1724 col = MAXCOL;
1725 }
1726 else
1727 {
1728 col = fp->col + 1;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001729 // col(".") when the cursor is on the NUL at the end of the line
1730 // because of "coladd" can be seen as an extra column.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001731 if (virtual_active() && fp == &curwin->w_cursor)
1732 {
1733 char_u *p = ml_get_cursor();
1734
1735 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
1736 curwin->w_virtcol - curwin->w_cursor.coladd))
1737 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001738 int l;
1739
1740 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
1741 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001742 }
1743 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001744 }
1745 }
1746 rettv->vval.v_number = col;
1747}
1748
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001749/*
1750 * "confirm(message, buttons[, default [, type]])" function
1751 */
1752 static void
1753f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1754{
1755#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1756 char_u *message;
1757 char_u *buttons = NULL;
1758 char_u buf[NUMBUFLEN];
1759 char_u buf2[NUMBUFLEN];
1760 int def = 1;
1761 int type = VIM_GENERIC;
1762 char_u *typestr;
1763 int error = FALSE;
1764
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001765 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001766 if (message == NULL)
1767 error = TRUE;
1768 if (argvars[1].v_type != VAR_UNKNOWN)
1769 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001770 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001771 if (buttons == NULL)
1772 error = TRUE;
1773 if (argvars[2].v_type != VAR_UNKNOWN)
1774 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001775 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001776 if (argvars[3].v_type != VAR_UNKNOWN)
1777 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001778 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001779 if (typestr == NULL)
1780 error = TRUE;
1781 else
1782 {
1783 switch (TOUPPER_ASC(*typestr))
1784 {
1785 case 'E': type = VIM_ERROR; break;
1786 case 'Q': type = VIM_QUESTION; break;
1787 case 'I': type = VIM_INFO; break;
1788 case 'W': type = VIM_WARNING; break;
1789 case 'G': type = VIM_GENERIC; break;
1790 }
1791 }
1792 }
1793 }
1794 }
1795
1796 if (buttons == NULL || *buttons == NUL)
1797 buttons = (char_u *)_("&Ok");
1798
1799 if (!error)
1800 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
1801 def, NULL, FALSE);
1802#endif
1803}
1804
1805/*
1806 * "copy()" function
1807 */
1808 static void
1809f_copy(typval_T *argvars, typval_T *rettv)
1810{
1811 item_copy(&argvars[0], rettv, FALSE, 0);
1812}
1813
1814#ifdef FEAT_FLOAT
1815/*
1816 * "cos()" function
1817 */
1818 static void
1819f_cos(typval_T *argvars, typval_T *rettv)
1820{
1821 float_T f = 0.0;
1822
1823 rettv->v_type = VAR_FLOAT;
1824 if (get_float_arg(argvars, &f) == OK)
1825 rettv->vval.v_float = cos(f);
1826 else
1827 rettv->vval.v_float = 0.0;
1828}
1829
1830/*
1831 * "cosh()" function
1832 */
1833 static void
1834f_cosh(typval_T *argvars, typval_T *rettv)
1835{
1836 float_T f = 0.0;
1837
1838 rettv->v_type = VAR_FLOAT;
1839 if (get_float_arg(argvars, &f) == OK)
1840 rettv->vval.v_float = cosh(f);
1841 else
1842 rettv->vval.v_float = 0.0;
1843}
1844#endif
1845
1846/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001847 * "cursor(lnum, col)" function, or
1848 * "cursor(list)"
1849 *
1850 * Moves the cursor to the specified line and column.
1851 * Returns 0 when the position could be set, -1 otherwise.
1852 */
1853 static void
1854f_cursor(typval_T *argvars, typval_T *rettv)
1855{
1856 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001857 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001858 int set_curswant = TRUE;
1859
1860 rettv->vval.v_number = -1;
1861 if (argvars[1].v_type == VAR_UNKNOWN)
1862 {
1863 pos_T pos;
1864 colnr_T curswant = -1;
1865
1866 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
1867 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001868 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001869 return;
1870 }
1871 line = pos.lnum;
1872 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001873 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001874 if (curswant >= 0)
1875 {
1876 curwin->w_curswant = curswant - 1;
1877 set_curswant = FALSE;
1878 }
1879 }
1880 else
1881 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001882 line = tv_get_lnum(argvars);
1883 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001884 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001885 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001886 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001887 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001888 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001889 if (line > 0)
1890 curwin->w_cursor.lnum = line;
1891 if (col > 0)
1892 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001893 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001895 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001896 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001897 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001898 if (has_mbyte)
1899 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001900
1901 curwin->w_set_curswant = set_curswant;
1902 rettv->vval.v_number = 0;
1903}
1904
Bram Moolenaar4f974752019-02-17 17:44:42 +01001905#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001906/*
1907 * "debugbreak()" function
1908 */
1909 static void
1910f_debugbreak(typval_T *argvars, typval_T *rettv)
1911{
1912 int pid;
1913
1914 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001915 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001916 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001917 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001918 else
1919 {
1920 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
1921
1922 if (hProcess != NULL)
1923 {
1924 DebugBreakProcess(hProcess);
1925 CloseHandle(hProcess);
1926 rettv->vval.v_number = OK;
1927 }
1928 }
1929}
1930#endif
1931
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001932/*
1933 * "deepcopy()" function
1934 */
1935 static void
1936f_deepcopy(typval_T *argvars, typval_T *rettv)
1937{
1938 int noref = 0;
1939 int copyID;
1940
1941 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001942 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001943 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001944 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001945 else
1946 {
1947 copyID = get_copyID();
1948 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
1949 }
1950}
1951
1952/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001953 * "did_filetype()" function
1954 */
1955 static void
1956f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1957{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001958 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001959}
1960
1961/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01001962 * "echoraw({expr})" function
1963 */
1964 static void
1965f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
1966{
1967 char_u *str = tv_get_string_chk(&argvars[0]);
1968
1969 if (str != NULL && *str != NUL)
1970 {
1971 out_str(str);
1972 out_flush();
1973 }
1974}
1975
1976/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001977 * "empty({expr})" function
1978 */
1979 static void
1980f_empty(typval_T *argvars, typval_T *rettv)
1981{
1982 int n = FALSE;
1983
1984 switch (argvars[0].v_type)
1985 {
1986 case VAR_STRING:
1987 case VAR_FUNC:
1988 n = argvars[0].vval.v_string == NULL
1989 || *argvars[0].vval.v_string == NUL;
1990 break;
1991 case VAR_PARTIAL:
1992 n = FALSE;
1993 break;
1994 case VAR_NUMBER:
1995 n = argvars[0].vval.v_number == 0;
1996 break;
1997 case VAR_FLOAT:
1998#ifdef FEAT_FLOAT
1999 n = argvars[0].vval.v_float == 0.0;
2000 break;
2001#endif
2002 case VAR_LIST:
2003 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002004 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002005 break;
2006 case VAR_DICT:
2007 n = argvars[0].vval.v_dict == NULL
2008 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2009 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002010 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002011 case VAR_SPECIAL:
2012 n = argvars[0].vval.v_number != VVAL_TRUE;
2013 break;
2014
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002015 case VAR_BLOB:
2016 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002017 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2018 break;
2019
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002020 case VAR_JOB:
2021#ifdef FEAT_JOB_CHANNEL
2022 n = argvars[0].vval.v_job == NULL
2023 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2024 break;
2025#endif
2026 case VAR_CHANNEL:
2027#ifdef FEAT_JOB_CHANNEL
2028 n = argvars[0].vval.v_channel == NULL
2029 || !channel_is_open(argvars[0].vval.v_channel);
2030 break;
2031#endif
2032 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002033 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002035 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002036 n = TRUE;
2037 break;
2038 }
2039
2040 rettv->vval.v_number = n;
2041}
2042
2043/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002044 * "environ()" function
2045 */
2046 static void
2047f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2048{
2049#if !defined(AMIGA)
2050 int i = 0;
2051 char_u *entry, *value;
2052# ifdef MSWIN
2053 extern wchar_t **_wenviron;
2054# else
2055 extern char **environ;
2056# endif
2057
2058 if (rettv_dict_alloc(rettv) != OK)
2059 return;
2060
2061# ifdef MSWIN
2062 if (*_wenviron == NULL)
2063 return;
2064# else
2065 if (*environ == NULL)
2066 return;
2067# endif
2068
2069 for (i = 0; ; ++i)
2070 {
2071# ifdef MSWIN
2072 short_u *p;
2073
2074 if ((p = (short_u *)_wenviron[i]) == NULL)
2075 return;
2076 entry = utf16_to_enc(p, NULL);
2077# else
2078 if ((entry = (char_u *)environ[i]) == NULL)
2079 return;
2080 entry = vim_strsave(entry);
2081# endif
2082 if (entry == NULL) // out of memory
2083 return;
2084 if ((value = vim_strchr(entry, '=')) == NULL)
2085 {
2086 vim_free(entry);
2087 continue;
2088 }
2089 *value++ = NUL;
2090 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2091 vim_free(entry);
2092 }
2093#endif
2094}
2095
2096/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002097 * "escape({string}, {chars})" function
2098 */
2099 static void
2100f_escape(typval_T *argvars, typval_T *rettv)
2101{
2102 char_u buf[NUMBUFLEN];
2103
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002104 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2105 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002106 rettv->v_type = VAR_STRING;
2107}
2108
2109/*
2110 * "eval()" function
2111 */
2112 static void
2113f_eval(typval_T *argvars, typval_T *rettv)
2114{
2115 char_u *s, *p;
2116
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002117 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002118 if (s != NULL)
2119 s = skipwhite(s);
2120
2121 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002122 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002123 {
2124 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002125 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002126 need_clr_eos = FALSE;
2127 rettv->v_type = VAR_NUMBER;
2128 rettv->vval.v_number = 0;
2129 }
2130 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002131 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002132}
2133
2134/*
2135 * "eventhandler()" function
2136 */
2137 static void
2138f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2139{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002140 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002141}
2142
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002143static garray_T redir_execute_ga;
2144
2145/*
2146 * Append "value[value_len]" to the execute() output.
2147 */
2148 void
2149execute_redir_str(char_u *value, int value_len)
2150{
2151 int len;
2152
2153 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002154 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002155 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002156 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157 if (ga_grow(&redir_execute_ga, len) == OK)
2158 {
2159 mch_memmove((char *)redir_execute_ga.ga_data
2160 + redir_execute_ga.ga_len, value, len);
2161 redir_execute_ga.ga_len += len;
2162 }
2163}
2164
2165/*
2166 * Get next line from a list.
2167 * Called by do_cmdline() to get the next line.
2168 * Returns allocated string, or NULL for end of function.
2169 */
2170
2171 static char_u *
2172get_list_line(
2173 int c UNUSED,
2174 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002175 int indent UNUSED,
2176 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002177{
2178 listitem_T **p = (listitem_T **)cookie;
2179 listitem_T *item = *p;
2180 char_u buf[NUMBUFLEN];
2181 char_u *s;
2182
2183 if (item == NULL)
2184 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002185 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002186 *p = item->li_next;
2187 return s == NULL ? NULL : vim_strsave(s);
2188}
2189
2190/*
2191 * "execute()" function
2192 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002193 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002194execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002195{
2196 char_u *cmd = NULL;
2197 list_T *list = NULL;
2198 int save_msg_silent = msg_silent;
2199 int save_emsg_silent = emsg_silent;
2200 int save_emsg_noredir = emsg_noredir;
2201 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002202 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002203 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002204 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002205 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002206
2207 rettv->vval.v_string = NULL;
2208 rettv->v_type = VAR_STRING;
2209
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002210 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002211 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002212 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002213 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002214 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002215 return;
2216 ++list->lv_refcount;
2217 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002218 else if (argvars[arg_off].v_type == VAR_JOB
2219 || argvars[arg_off].v_type == VAR_CHANNEL)
2220 {
2221 emsg(_(e_inval_string));
2222 return;
2223 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002224 else
2225 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002226 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002227 if (cmd == NULL)
2228 return;
2229 }
2230
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002231 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002232 {
2233 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002234 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002235
2236 if (s == NULL)
2237 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002238 if (*s == NUL)
2239 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002240 if (STRNCMP(s, "silent", 6) == 0)
2241 ++msg_silent;
2242 if (STRCMP(s, "silent!") == 0)
2243 {
2244 emsg_silent = TRUE;
2245 emsg_noredir = TRUE;
2246 }
2247 }
2248 else
2249 ++msg_silent;
2250
2251 if (redir_execute)
2252 save_ga = redir_execute_ga;
2253 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2254 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002255 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002256 if (!echo_output)
2257 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002258
2259 if (cmd != NULL)
2260 do_cmdline_cmd(cmd);
2261 else
2262 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002263 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002264
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002265 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002266 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002267 do_cmdline(NULL, get_list_line, (void *)&item,
2268 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2269 --list->lv_refcount;
2270 }
2271
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002272 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01002273 if (ga_grow(&redir_execute_ga, 1) == OK)
2274 {
2275 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2276 rettv->vval.v_string = redir_execute_ga.ga_data;
2277 }
2278 else
2279 {
2280 ga_clear(&redir_execute_ga);
2281 rettv->vval.v_string = NULL;
2282 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002283 msg_silent = save_msg_silent;
2284 emsg_silent = save_emsg_silent;
2285 emsg_noredir = save_emsg_noredir;
2286
2287 redir_execute = save_redir_execute;
2288 if (redir_execute)
2289 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002290 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002291
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002292 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002293 if (echo_output)
2294 // When not working silently: put it in column zero. A following
2295 // "echon" will overwrite the message, unavoidably.
2296 msg_col = 0;
2297 else
2298 // When working silently: Put it back where it was, since nothing
2299 // should have been written.
2300 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002301}
2302
2303/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002304 * "execute()" function
2305 */
2306 static void
2307f_execute(typval_T *argvars, typval_T *rettv)
2308{
2309 execute_common(argvars, rettv, 0);
2310}
2311
2312/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002313 * "exists()" function
2314 */
2315 static void
2316f_exists(typval_T *argvars, typval_T *rettv)
2317{
2318 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002319 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002320
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002321 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002322 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002323 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002324 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002325 if (mch_getenv(p + 1) != NULL)
2326 n = TRUE;
2327 else
2328 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002329 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002330 p = expand_env_save(p);
2331 if (p != NULL && *p != '$')
2332 n = TRUE;
2333 vim_free(p);
2334 }
2335 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002336 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002337 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002338 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002339 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002340 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002341 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002342 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002343 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002344 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002345 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01002346 else if (*p == '?') // internal function only
2347 {
2348 n = has_internal_func_name(p + 1);
2349 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002350 else if (*p == ':')
2351 {
2352 n = cmd_exists(p + 1);
2353 }
2354 else if (*p == '#')
2355 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002356 if (p[1] == '#')
2357 n = autocmd_supported(p + 2);
2358 else
2359 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002360 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002361 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002362 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01002363 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002364 }
2365
2366 rettv->vval.v_number = n;
2367}
2368
2369#ifdef FEAT_FLOAT
2370/*
2371 * "exp()" function
2372 */
2373 static void
2374f_exp(typval_T *argvars, typval_T *rettv)
2375{
2376 float_T f = 0.0;
2377
2378 rettv->v_type = VAR_FLOAT;
2379 if (get_float_arg(argvars, &f) == OK)
2380 rettv->vval.v_float = exp(f);
2381 else
2382 rettv->vval.v_float = 0.0;
2383}
2384#endif
2385
2386/*
2387 * "expand()" function
2388 */
2389 static void
2390f_expand(typval_T *argvars, typval_T *rettv)
2391{
2392 char_u *s;
2393 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002394 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002395 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2396 expand_T xpc;
2397 int error = FALSE;
2398 char_u *result;
2399
2400 rettv->v_type = VAR_STRING;
2401 if (argvars[1].v_type != VAR_UNKNOWN
2402 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002403 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002404 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02002405 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002406
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002407 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002408 if (*s == '%' || *s == '#' || *s == '<')
2409 {
2410 ++emsg_off;
2411 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
2412 --emsg_off;
2413 if (rettv->v_type == VAR_LIST)
2414 {
2415 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
2416 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02002417 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002418 }
2419 else
2420 rettv->vval.v_string = result;
2421 }
2422 else
2423 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002424 // When the optional second argument is non-zero, don't remove matches
2425 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002426 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002427 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002428 options |= WILD_KEEP_ALL;
2429 if (!error)
2430 {
2431 ExpandInit(&xpc);
2432 xpc.xp_context = EXPAND_FILES;
2433 if (p_wic)
2434 options += WILD_ICASE;
2435 if (rettv->v_type == VAR_STRING)
2436 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
2437 options, WILD_ALL);
2438 else if (rettv_list_alloc(rettv) != FAIL)
2439 {
2440 int i;
2441
2442 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
2443 for (i = 0; i < xpc.xp_numfiles; i++)
2444 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2445 ExpandCleanup(&xpc);
2446 }
2447 }
2448 else
2449 rettv->vval.v_string = NULL;
2450 }
2451}
2452
2453/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02002454 * "expandcmd()" function
2455 * Expand all the special characters in a command string.
2456 */
2457 static void
2458f_expandcmd(typval_T *argvars, typval_T *rettv)
2459{
2460 exarg_T eap;
2461 char_u *cmdstr;
2462 char *errormsg = NULL;
2463
2464 rettv->v_type = VAR_STRING;
2465 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
2466
2467 memset(&eap, 0, sizeof(eap));
2468 eap.cmd = cmdstr;
2469 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002470 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02002471 eap.usefilter = FALSE;
2472 eap.nextcmd = NULL;
2473 eap.cmdidx = CMD_USER;
2474
2475 expand_filename(&eap, &cmdstr, &errormsg);
2476 if (errormsg != NULL && *errormsg != NUL)
2477 emsg(errormsg);
2478
2479 rettv->vval.v_string = cmdstr;
2480}
2481
2482/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002483 * "feedkeys()" function
2484 */
2485 static void
2486f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
2487{
2488 int remap = TRUE;
2489 int insert = FALSE;
2490 char_u *keys, *flags;
2491 char_u nbuf[NUMBUFLEN];
2492 int typed = FALSE;
2493 int execute = FALSE;
2494 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002495 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002496 char_u *keys_esc;
2497
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002498 // This is not allowed in the sandbox. If the commands would still be
2499 // executed in the sandbox it would be OK, but it probably happens later,
2500 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002501 if (check_secure())
2502 return;
2503
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002504 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002505
2506 if (argvars[1].v_type != VAR_UNKNOWN)
2507 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002508 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002509 for ( ; *flags != NUL; ++flags)
2510 {
2511 switch (*flags)
2512 {
2513 case 'n': remap = FALSE; break;
2514 case 'm': remap = TRUE; break;
2515 case 't': typed = TRUE; break;
2516 case 'i': insert = TRUE; break;
2517 case 'x': execute = TRUE; break;
2518 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002519 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002520 }
2521 }
2522 }
2523
2524 if (*keys != NUL || execute)
2525 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002526 // Need to escape K_SPECIAL and CSI before putting the string in the
2527 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002528 keys_esc = vim_strsave_escape_csi(keys);
2529 if (keys_esc != NULL)
2530 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002531 if (lowlevel)
2532 {
2533#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002534 int idx;
2535 int len = (int)STRLEN(keys);
2536
2537 for (idx = 0; idx < len; ++idx)
2538 {
2539 // if a CTRL-C was typed, set got_int, similar to what
2540 // happens in fill_input_buf()
2541 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
2542 got_int = TRUE;
2543 add_to_input_buf(keys + idx, 1);
2544 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002545#else
2546 emsg(_("E980: lowlevel input not supported"));
2547#endif
2548 }
2549 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002550 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002551 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002552 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002553 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002554#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002555 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002556#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002557 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002558 typebuf_was_filled = TRUE;
2559 }
2560 vim_free(keys_esc);
2561
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002562 if (execute)
2563 {
2564 int save_msg_scroll = msg_scroll;
2565
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002566 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002567 msg_scroll = FALSE;
2568
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002569 if (!dangerous)
2570 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02002571 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002572 if (!dangerous)
2573 --ex_normal_busy;
2574
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002575 msg_scroll |= save_msg_scroll;
2576 }
2577 }
2578 }
2579}
2580
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002581#ifdef FEAT_FLOAT
2582/*
2583 * "float2nr({float})" function
2584 */
2585 static void
2586f_float2nr(typval_T *argvars, typval_T *rettv)
2587{
2588 float_T f = 0.0;
2589
2590 if (get_float_arg(argvars, &f) == OK)
2591 {
Bram Moolenaar37184272020-05-23 19:30:05 +02002592 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002593 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02002594 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002595 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002596 else
2597 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002598 }
2599}
2600
2601/*
2602 * "floor({float})" function
2603 */
2604 static void
2605f_floor(typval_T *argvars, typval_T *rettv)
2606{
2607 float_T f = 0.0;
2608
2609 rettv->v_type = VAR_FLOAT;
2610 if (get_float_arg(argvars, &f) == OK)
2611 rettv->vval.v_float = floor(f);
2612 else
2613 rettv->vval.v_float = 0.0;
2614}
2615
2616/*
2617 * "fmod()" function
2618 */
2619 static void
2620f_fmod(typval_T *argvars, typval_T *rettv)
2621{
2622 float_T fx = 0.0, fy = 0.0;
2623
2624 rettv->v_type = VAR_FLOAT;
2625 if (get_float_arg(argvars, &fx) == OK
2626 && get_float_arg(&argvars[1], &fy) == OK)
2627 rettv->vval.v_float = fmod(fx, fy);
2628 else
2629 rettv->vval.v_float = 0.0;
2630}
2631#endif
2632
2633/*
2634 * "fnameescape({string})" function
2635 */
2636 static void
2637f_fnameescape(typval_T *argvars, typval_T *rettv)
2638{
2639 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002640 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002641 rettv->v_type = VAR_STRING;
2642}
2643
2644/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002645 * "foreground()" function
2646 */
2647 static void
2648f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2649{
2650#ifdef FEAT_GUI
2651 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002652 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002653 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002654 return;
2655 }
2656#endif
2657#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002658 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002659#endif
2660}
2661
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002662 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002663common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002664{
2665 char_u *s;
2666 char_u *name;
2667 int use_string = FALSE;
2668 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002669 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002670 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002671
2672 if (argvars[0].v_type == VAR_FUNC)
2673 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002674 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002675 s = argvars[0].vval.v_string;
2676 }
2677 else if (argvars[0].v_type == VAR_PARTIAL
2678 && argvars[0].vval.v_partial != NULL)
2679 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002680 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002681 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002682 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002683 }
2684 else
2685 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002686 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002687 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 use_string = TRUE;
2689 }
2690
Bram Moolenaar843b8842016-08-21 14:36:15 +02002691 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002692 {
2693 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002694 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002695 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
2696 if (*name != NUL)
2697 s = NULL;
2698 }
2699
Bram Moolenaar843b8842016-08-21 14:36:15 +02002700 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
2701 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002702 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002703 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002704 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002705 ? find_func(trans_name, is_global, NULL) == NULL
2706 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002707 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002708 else
2709 {
2710 int dict_idx = 0;
2711 int arg_idx = 0;
2712 list_T *list = NULL;
2713
2714 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
2715 {
2716 char sid_buf[25];
2717 int off = *s == 's' ? 2 : 5;
2718
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002719 // Expand s: and <SID> into <SNR>nr_, so that the function can
2720 // also be called from another script. Using trans_function_name()
2721 // would also work, but some plugins depend on the name being
2722 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002723 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02002724 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002725 if (name != NULL)
2726 {
2727 STRCPY(name, sid_buf);
2728 STRCAT(name, s + off);
2729 }
2730 }
2731 else
2732 name = vim_strsave(s);
2733
2734 if (argvars[1].v_type != VAR_UNKNOWN)
2735 {
2736 if (argvars[2].v_type != VAR_UNKNOWN)
2737 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002738 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002739 arg_idx = 1;
2740 dict_idx = 2;
2741 }
2742 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002743 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002744 dict_idx = 1;
2745 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002746 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002747 arg_idx = 1;
2748 if (dict_idx > 0)
2749 {
2750 if (argvars[dict_idx].v_type != VAR_DICT)
2751 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002752 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002753 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002754 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002755 }
2756 if (argvars[dict_idx].vval.v_dict == NULL)
2757 dict_idx = 0;
2758 }
2759 if (arg_idx > 0)
2760 {
2761 if (argvars[arg_idx].v_type != VAR_LIST)
2762 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002763 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002764 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002765 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002766 }
2767 list = argvars[arg_idx].vval.v_list;
2768 if (list == NULL || list->lv_len == 0)
2769 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002770 else if (list->lv_len > MAX_FUNC_ARGS)
2771 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01002772 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002773 vim_free(name);
2774 goto theend;
2775 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002776 }
2777 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002778 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002779 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002780 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002781
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002782 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002783 if (pt == NULL)
2784 vim_free(name);
2785 else
2786 {
2787 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
2788 {
2789 listitem_T *li;
2790 int i = 0;
2791 int arg_len = 0;
2792 int lv_len = 0;
2793
2794 if (arg_pt != NULL)
2795 arg_len = arg_pt->pt_argc;
2796 if (list != NULL)
2797 lv_len = list->lv_len;
2798 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002799 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002800 if (pt->pt_argv == NULL)
2801 {
2802 vim_free(pt);
2803 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002804 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002805 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002806 for (i = 0; i < arg_len; i++)
2807 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
2808 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002809 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002810 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002811 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002812 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002813 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002814 }
2815
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002816 // For "function(dict.func, [], dict)" and "func" is a partial
2817 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002818 if (dict_idx > 0)
2819 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002820 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002821 pt->pt_dict = argvars[dict_idx].vval.v_dict;
2822 ++pt->pt_dict->dv_refcount;
2823 }
2824 else if (arg_pt != NULL)
2825 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002826 // If the dict was bound automatically the result is also
2827 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002828 pt->pt_dict = arg_pt->pt_dict;
2829 pt->pt_auto = arg_pt->pt_auto;
2830 if (pt->pt_dict != NULL)
2831 ++pt->pt_dict->dv_refcount;
2832 }
2833
2834 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002835 if (arg_pt != NULL && arg_pt->pt_func != NULL)
2836 {
2837 pt->pt_func = arg_pt->pt_func;
2838 func_ptr_ref(pt->pt_func);
2839 vim_free(name);
2840 }
2841 else if (is_funcref)
2842 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002843 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002844 func_ptr_ref(pt->pt_func);
2845 vim_free(name);
2846 }
2847 else
2848 {
2849 pt->pt_name = name;
2850 func_ref(name);
2851 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002852 }
2853 rettv->v_type = VAR_PARTIAL;
2854 rettv->vval.v_partial = pt;
2855 }
2856 else
2857 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002858 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002859 rettv->v_type = VAR_FUNC;
2860 rettv->vval.v_string = name;
2861 func_ref(name);
2862 }
2863 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002864theend:
2865 vim_free(trans_name);
2866}
2867
2868/*
2869 * "funcref()" function
2870 */
2871 static void
2872f_funcref(typval_T *argvars, typval_T *rettv)
2873{
2874 common_function(argvars, rettv, TRUE);
2875}
2876
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002877 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002878ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002879{
2880 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
2881 return &t_func_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002882 return &t_func_void;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002883}
2884
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002885/*
2886 * "function()" function
2887 */
2888 static void
2889f_function(typval_T *argvars, typval_T *rettv)
2890{
2891 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002892}
2893
2894/*
2895 * "garbagecollect()" function
2896 */
2897 static void
2898f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
2899{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002900 // This is postponed until we are back at the toplevel, because we may be
2901 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002902 want_garbage_collect = TRUE;
2903
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002904 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002905 garbage_collect_at_exit = TRUE;
2906}
2907
2908/*
2909 * "get()" function
2910 */
2911 static void
2912f_get(typval_T *argvars, typval_T *rettv)
2913{
2914 listitem_T *li;
2915 list_T *l;
2916 dictitem_T *di;
2917 dict_T *d;
2918 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02002919 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002920
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002921 if (argvars[0].v_type == VAR_BLOB)
2922 {
2923 int error = FALSE;
2924 int idx = tv_get_number_chk(&argvars[1], &error);
2925
2926 if (!error)
2927 {
2928 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002929 if (idx < 0)
2930 idx = blob_len(argvars[0].vval.v_blob) + idx;
2931 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
2932 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002933 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002934 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002935 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002936 tv = rettv;
2937 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002938 }
2939 }
2940 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002941 {
2942 if ((l = argvars[0].vval.v_list) != NULL)
2943 {
2944 int error = FALSE;
2945
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002946 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002947 if (!error && li != NULL)
2948 tv = &li->li_tv;
2949 }
2950 }
2951 else if (argvars[0].v_type == VAR_DICT)
2952 {
2953 if ((d = argvars[0].vval.v_dict) != NULL)
2954 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002955 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002956 if (di != NULL)
2957 tv = &di->di_tv;
2958 }
2959 }
2960 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
2961 {
2962 partial_T *pt;
2963 partial_T fref_pt;
2964
2965 if (argvars[0].v_type == VAR_PARTIAL)
2966 pt = argvars[0].vval.v_partial;
2967 else
2968 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02002969 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002970 fref_pt.pt_name = argvars[0].vval.v_string;
2971 pt = &fref_pt;
2972 }
2973
2974 if (pt != NULL)
2975 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002976 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002977 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002978
2979 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
2980 {
2981 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002982 n = partial_name(pt);
2983 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002984 rettv->vval.v_string = NULL;
2985 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002986 {
2987 rettv->vval.v_string = vim_strsave(n);
2988 if (rettv->v_type == VAR_FUNC)
2989 func_ref(rettv->vval.v_string);
2990 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002991 }
2992 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02002993 {
2994 what_is_dict = TRUE;
2995 if (pt->pt_dict != NULL)
2996 rettv_dict_set(rettv, pt->pt_dict);
2997 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002998 else if (STRCMP(what, "args") == 0)
2999 {
3000 rettv->v_type = VAR_LIST;
3001 if (rettv_list_alloc(rettv) == OK)
3002 {
3003 int i;
3004
3005 for (i = 0; i < pt->pt_argc; ++i)
3006 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3007 }
3008 }
3009 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003010 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003011
3012 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3013 // third argument
3014 if (!what_is_dict)
3015 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003016 }
3017 }
3018 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003019 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003020
3021 if (tv == NULL)
3022 {
3023 if (argvars[2].v_type != VAR_UNKNOWN)
3024 copy_tv(&argvars[2], rettv);
3025 }
3026 else
3027 copy_tv(tv, rettv);
3028}
3029
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003030/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003031 * "getchangelist()" function
3032 */
3033 static void
3034f_getchangelist(typval_T *argvars, typval_T *rettv)
3035{
3036#ifdef FEAT_JUMPLIST
3037 buf_T *buf;
3038 int i;
3039 list_T *l;
3040 dict_T *d;
3041#endif
3042
3043 if (rettv_list_alloc(rettv) != OK)
3044 return;
3045
3046#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003047 if (argvars[0].v_type == VAR_UNKNOWN)
3048 buf = curbuf;
3049 else
3050 {
3051 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3052 ++emsg_off;
3053 buf = tv_get_buf(&argvars[0], FALSE);
3054 --emsg_off;
3055 }
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003056 if (buf == NULL)
3057 return;
3058
3059 l = list_alloc();
3060 if (l == NULL)
3061 return;
3062
3063 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3064 return;
3065 /*
3066 * The current window change list index tracks only the position in the
3067 * current buffer change list. For other buffers, use the change list
3068 * length as the current index.
3069 */
3070 list_append_number(rettv->vval.v_list,
3071 (varnumber_T)((buf == curwin->w_buffer)
3072 ? curwin->w_changelistidx : buf->b_changelistlen));
3073
3074 for (i = 0; i < buf->b_changelistlen; ++i)
3075 {
3076 if (buf->b_changelist[i].lnum == 0)
3077 continue;
3078 if ((d = dict_alloc()) == NULL)
3079 return;
3080 if (list_append_dict(l, d) == FAIL)
3081 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003082 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3083 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003084 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003085 }
3086#endif
3087}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003088
3089/*
3090 * "getcharsearch()" function
3091 */
3092 static void
3093f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3094{
3095 if (rettv_dict_alloc(rettv) != FAIL)
3096 {
3097 dict_T *dict = rettv->vval.v_dict;
3098
Bram Moolenaare0be1672018-07-08 16:50:37 +02003099 dict_add_string(dict, "char", last_csearch());
3100 dict_add_number(dict, "forward", last_csearch_forward());
3101 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003102 }
3103}
3104
3105/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003106 * "getenv()" function
3107 */
3108 static void
3109f_getenv(typval_T *argvars, typval_T *rettv)
3110{
3111 int mustfree = FALSE;
3112 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3113
3114 if (p == NULL)
3115 {
3116 rettv->v_type = VAR_SPECIAL;
3117 rettv->vval.v_number = VVAL_NULL;
3118 return;
3119 }
3120 if (!mustfree)
3121 p = vim_strsave(p);
3122 rettv->vval.v_string = p;
3123 rettv->v_type = VAR_STRING;
3124}
3125
3126/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003127 * "getfontname()" function
3128 */
3129 static void
3130f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3131{
3132 rettv->v_type = VAR_STRING;
3133 rettv->vval.v_string = NULL;
3134#ifdef FEAT_GUI
3135 if (gui.in_use)
3136 {
3137 GuiFont font;
3138 char_u *name = NULL;
3139
3140 if (argvars[0].v_type == VAR_UNKNOWN)
3141 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003142 // Get the "Normal" font. Either the name saved by
3143 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003144 font = gui.norm_font;
3145 name = hl_get_font_name();
3146 }
3147 else
3148 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003149 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003150 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003151 return;
3152 font = gui_mch_get_font(name, FALSE);
3153 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003154 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003155 }
3156 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3157 if (argvars[0].v_type != VAR_UNKNOWN)
3158 gui_mch_free_font(font);
3159 }
3160#endif
3161}
3162
3163/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003164 * "getjumplist()" function
3165 */
3166 static void
3167f_getjumplist(typval_T *argvars, typval_T *rettv)
3168{
3169#ifdef FEAT_JUMPLIST
3170 win_T *wp;
3171 int i;
3172 list_T *l;
3173 dict_T *d;
3174#endif
3175
3176 if (rettv_list_alloc(rettv) != OK)
3177 return;
3178
3179#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003180 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003181 if (wp == NULL)
3182 return;
3183
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003184 cleanup_jumplist(wp, TRUE);
3185
Bram Moolenaar4f505882018-02-10 21:06:32 +01003186 l = list_alloc();
3187 if (l == NULL)
3188 return;
3189
3190 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3191 return;
3192 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3193
3194 for (i = 0; i < wp->w_jumplistlen; ++i)
3195 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003196 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3197 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003198 if ((d = dict_alloc()) == NULL)
3199 return;
3200 if (list_append_dict(l, d) == FAIL)
3201 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003202 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3203 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003204 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003205 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003206 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003207 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003208 }
3209#endif
3210}
3211
3212/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003213 * "getpid()" function
3214 */
3215 static void
3216f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
3217{
3218 rettv->vval.v_number = mch_get_pid();
3219}
3220
3221 static void
3222getpos_both(
3223 typval_T *argvars,
3224 typval_T *rettv,
3225 int getcurpos)
3226{
3227 pos_T *fp;
3228 list_T *l;
3229 int fnum = -1;
3230
3231 if (rettv_list_alloc(rettv) == OK)
3232 {
3233 l = rettv->vval.v_list;
3234 if (getcurpos)
3235 fp = &curwin->w_cursor;
3236 else
3237 fp = var2fpos(&argvars[0], TRUE, &fnum);
3238 if (fnum != -1)
3239 list_append_number(l, (varnumber_T)fnum);
3240 else
3241 list_append_number(l, (varnumber_T)0);
3242 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3243 : (varnumber_T)0);
3244 list_append_number(l, (fp != NULL)
3245 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3246 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003247 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003248 (varnumber_T)0);
3249 if (getcurpos)
3250 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01003251 int save_set_curswant = curwin->w_set_curswant;
3252 colnr_T save_curswant = curwin->w_curswant;
3253 colnr_T save_virtcol = curwin->w_virtcol;
3254
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003255 update_curswant();
3256 list_append_number(l, curwin->w_curswant == MAXCOL ?
3257 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01003258
3259 // Do not change "curswant", as it is unexpected that a get
3260 // function has a side effect.
3261 if (save_set_curswant)
3262 {
3263 curwin->w_set_curswant = save_set_curswant;
3264 curwin->w_curswant = save_curswant;
3265 curwin->w_virtcol = save_virtcol;
3266 curwin->w_valid &= ~VALID_VIRTCOL;
3267 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003268 }
3269 }
3270 else
3271 rettv->vval.v_number = FALSE;
3272}
3273
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003274/*
3275 * "getcurpos()" function
3276 */
3277 static void
3278f_getcurpos(typval_T *argvars, typval_T *rettv)
3279{
3280 getpos_both(argvars, rettv, TRUE);
3281}
3282
3283/*
3284 * "getpos(string)" function
3285 */
3286 static void
3287f_getpos(typval_T *argvars, typval_T *rettv)
3288{
3289 getpos_both(argvars, rettv, FALSE);
3290}
3291
3292/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003293 * "getreg()" function
3294 */
3295 static void
3296f_getreg(typval_T *argvars, typval_T *rettv)
3297{
3298 char_u *strregname;
3299 int regname;
3300 int arg2 = FALSE;
3301 int return_list = FALSE;
3302 int error = FALSE;
3303
3304 if (argvars[0].v_type != VAR_UNKNOWN)
3305 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003306 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003307 error = strregname == NULL;
3308 if (argvars[1].v_type != VAR_UNKNOWN)
3309 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003310 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003311 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003312 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003313 }
3314 }
3315 else
3316 strregname = get_vim_var_str(VV_REG);
3317
3318 if (error)
3319 return;
3320
3321 regname = (strregname == NULL ? '"' : *strregname);
3322 if (regname == 0)
3323 regname = '"';
3324
3325 if (return_list)
3326 {
3327 rettv->v_type = VAR_LIST;
3328 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
3329 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
3330 if (rettv->vval.v_list == NULL)
3331 (void)rettv_list_alloc(rettv);
3332 else
3333 ++rettv->vval.v_list->lv_refcount;
3334 }
3335 else
3336 {
3337 rettv->v_type = VAR_STRING;
3338 rettv->vval.v_string = get_reg_contents(regname,
3339 arg2 ? GREG_EXPR_SRC : 0);
3340 }
3341}
3342
3343/*
3344 * "getregtype()" function
3345 */
3346 static void
3347f_getregtype(typval_T *argvars, typval_T *rettv)
3348{
3349 char_u *strregname;
3350 int regname;
3351 char_u buf[NUMBUFLEN + 2];
3352 long reglen = 0;
3353
3354 if (argvars[0].v_type != VAR_UNKNOWN)
3355 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003356 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003357 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003358 {
3359 rettv->v_type = VAR_STRING;
3360 rettv->vval.v_string = NULL;
3361 return;
3362 }
3363 }
3364 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003365 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003366 strregname = get_vim_var_str(VV_REG);
3367
3368 regname = (strregname == NULL ? '"' : *strregname);
3369 if (regname == 0)
3370 regname = '"';
3371
3372 buf[0] = NUL;
3373 buf[1] = NUL;
3374 switch (get_reg_type(regname, &reglen))
3375 {
3376 case MLINE: buf[0] = 'V'; break;
3377 case MCHAR: buf[0] = 'v'; break;
3378 case MBLOCK:
3379 buf[0] = Ctrl_V;
3380 sprintf((char *)buf + 1, "%ld", reglen + 1);
3381 break;
3382 }
3383 rettv->v_type = VAR_STRING;
3384 rettv->vval.v_string = vim_strsave(buf);
3385}
3386
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003387/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01003388 * "gettagstack()" function
3389 */
3390 static void
3391f_gettagstack(typval_T *argvars, typval_T *rettv)
3392{
3393 win_T *wp = curwin; // default is current window
3394
3395 if (rettv_dict_alloc(rettv) != OK)
3396 return;
3397
3398 if (argvars[0].v_type != VAR_UNKNOWN)
3399 {
3400 wp = find_win_by_nr_or_id(&argvars[0]);
3401 if (wp == NULL)
3402 return;
3403 }
3404
3405 get_tagstack(wp, rettv->vval.v_dict);
3406}
3407
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003408// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003409#include "version.h"
3410
3411/*
3412 * "has()" function
3413 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003414 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003415f_has(typval_T *argvars, typval_T *rettv)
3416{
3417 int i;
3418 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01003419 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003420 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01003421 typedef struct {
3422 char *name;
3423 short present;
3424 } has_item_T;
3425 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003426 {
Bram Moolenaar79296512020-03-22 16:17:14 +01003427 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003428#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01003429 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003430#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003431 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003432#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003433 },
3434 {"arp",
3435#if defined(AMIGA) && defined(FEAT_ARP)
3436 1
3437#else
3438 0
3439#endif
3440 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003441 {"haiku",
3442#ifdef __HAIKU__
3443 1
3444#else
3445 0
3446#endif
3447 },
3448 {"bsd",
3449#if defined(BSD) && !defined(MACOS_X)
3450 1
3451#else
3452 0
3453#endif
3454 },
3455 {"hpux",
3456#ifdef hpux
3457 1
3458#else
3459 0
3460#endif
3461 },
3462 {"linux",
3463#ifdef __linux__
3464 1
3465#else
3466 0
3467#endif
3468 },
3469 {"mac", // Mac OS X (and, once, Mac OS Classic)
3470#ifdef MACOS_X
3471 1
3472#else
3473 0
3474#endif
3475 },
3476 {"osx", // Mac OS X
3477#ifdef MACOS_X
3478 1
3479#else
3480 0
3481#endif
3482 },
3483 {"macunix", // Mac OS X, with the darwin feature
3484#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3485 1
3486#else
3487 0
3488#endif
3489 },
3490 {"osxdarwin", // synonym for macunix
3491#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3492 1
3493#else
3494 0
3495#endif
3496 },
3497 {"qnx",
3498#ifdef __QNX__
3499 1
3500#else
3501 0
3502#endif
3503 },
3504 {"sun",
3505#ifdef SUN_SYSTEM
3506 1
3507#else
3508 0
3509#endif
3510 },
3511 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003512#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01003513 1
3514#else
3515 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003516#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003517 },
3518 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003519#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01003520 1
3521#else
3522 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003523#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003524 },
3525 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01003526#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01003527 1
3528#else
3529 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003530#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003531 },
3532 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01003533#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01003534 1
3535#else
3536 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003537#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003538 },
3539 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01003540#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01003541 1
3542#else
3543 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003544#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003545 },
3546 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003547#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003548 1
3549#else
3550 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003551#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003552 },
3553 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003554#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003555 1
3556#else
3557 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003558#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003559 },
3560 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01003562 1
3563#else
3564 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003565#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003566 },
3567 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003568#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003569 1
3570#else
3571 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003572#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003573 },
3574 {"autocmd", 1},
3575 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003576#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01003577 1
3578#else
3579 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003580#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003581 },
3582 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003583#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003584 1
3585#else
3586 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003587#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003588 },
3589 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003590#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01003591 1
3592#else
3593 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003594#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003595 },
3596 {"balloon_multiline",
3597#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
3598 // MS-Windows requires runtime check, see below
3599 1
3600#else
3601 0
3602#endif
3603 },
3604 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003605#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01003606 1
3607#else
3608 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003609#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003610 },
3611 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003612#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01003613 1
3614#else
3615 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003616#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003617 },
3618 {"all_builtin_terms",
3619#if defined(ALL_BUILTIN_TCAPS)
3620 1
3621#else
3622 0
3623#endif
3624 },
3625 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003626#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01003627 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003628 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01003629 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003630#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003631 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003632#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003633 },
3634 {"byte_offset",
3635#ifdef FEAT_BYTEOFF
3636 1
3637#else
3638 0
3639#endif
3640 },
3641 {"channel",
3642#ifdef FEAT_JOB_CHANNEL
3643 1
3644#else
3645 0
3646#endif
3647 },
3648 {"cindent",
3649#ifdef FEAT_CINDENT
3650 1
3651#else
3652 0
3653#endif
3654 },
3655 {"clientserver",
3656#ifdef FEAT_CLIENTSERVER
3657 1
3658#else
3659 0
3660#endif
3661 },
3662 {"clipboard",
3663#ifdef FEAT_CLIPBOARD
3664 1
3665#else
3666 0
3667#endif
3668 },
3669 {"cmdline_compl", 1},
3670 {"cmdline_hist", 1},
3671 {"comments", 1},
3672 {"conceal",
3673#ifdef FEAT_CONCEAL
3674 1
3675#else
3676 0
3677#endif
3678 },
3679 {"cryptv",
3680#ifdef FEAT_CRYPT
3681 1
3682#else
3683 0
3684#endif
3685 },
3686 {"crypt-blowfish",
3687#ifdef FEAT_CRYPT
3688 1
3689#else
3690 0
3691#endif
3692 },
3693 {"crypt-blowfish2",
3694#ifdef FEAT_CRYPT
3695 1
3696#else
3697 0
3698#endif
3699 },
3700 {"cscope",
3701#ifdef FEAT_CSCOPE
3702 1
3703#else
3704 0
3705#endif
3706 },
3707 {"cursorbind", 1},
3708 {"cursorshape",
3709#ifdef CURSOR_SHAPE
3710 1
3711#else
3712 0
3713#endif
3714 },
3715 {"debug",
3716#ifdef DEBUG
3717 1
3718#else
3719 0
3720#endif
3721 },
3722 {"dialog_con",
3723#ifdef FEAT_CON_DIALOG
3724 1
3725#else
3726 0
3727#endif
3728 },
3729 {"dialog_gui",
3730#ifdef FEAT_GUI_DIALOG
3731 1
3732#else
3733 0
3734#endif
3735 },
3736 {"diff",
3737#ifdef FEAT_DIFF
3738 1
3739#else
3740 0
3741#endif
3742 },
3743 {"digraphs",
3744#ifdef FEAT_DIGRAPHS
3745 1
3746#else
3747 0
3748#endif
3749 },
3750 {"directx",
3751#ifdef FEAT_DIRECTX
3752 1
3753#else
3754 0
3755#endif
3756 },
3757 {"dnd",
3758#ifdef FEAT_DND
3759 1
3760#else
3761 0
3762#endif
3763 },
3764 {"emacs_tags",
3765#ifdef FEAT_EMACS_TAGS
3766 1
3767#else
3768 0
3769#endif
3770 },
3771 {"eval", 1}, // always present, of course!
3772 {"ex_extra", 1}, // graduated feature
3773 {"extra_search",
3774#ifdef FEAT_SEARCH_EXTRA
3775 1
3776#else
3777 0
3778#endif
3779 },
3780 {"file_in_path",
3781#ifdef FEAT_SEARCHPATH
3782 1
3783#else
3784 0
3785#endif
3786 },
3787 {"filterpipe",
3788#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
3789 1
3790#else
3791 0
3792#endif
3793 },
3794 {"find_in_path",
3795#ifdef FEAT_FIND_ID
3796 1
3797#else
3798 0
3799#endif
3800 },
3801 {"float",
3802#ifdef FEAT_FLOAT
3803 1
3804#else
3805 0
3806#endif
3807 },
3808 {"folding",
3809#ifdef FEAT_FOLDING
3810 1
3811#else
3812 0
3813#endif
3814 },
3815 {"footer",
3816#ifdef FEAT_FOOTER
3817 1
3818#else
3819 0
3820#endif
3821 },
3822 {"fork",
3823#if !defined(USE_SYSTEM) && defined(UNIX)
3824 1
3825#else
3826 0
3827#endif
3828 },
3829 {"gettext",
3830#ifdef FEAT_GETTEXT
3831 1
3832#else
3833 0
3834#endif
3835 },
3836 {"gui",
3837#ifdef FEAT_GUI
3838 1
3839#else
3840 0
3841#endif
3842 },
3843 {"gui_neXtaw",
3844#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
3845 1
3846#else
3847 0
3848#endif
3849 },
3850 {"gui_athena",
3851#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
3852 1
3853#else
3854 0
3855#endif
3856 },
3857 {"gui_gtk",
3858#ifdef FEAT_GUI_GTK
3859 1
3860#else
3861 0
3862#endif
3863 },
3864 {"gui_gtk2",
3865#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
3866 1
3867#else
3868 0
3869#endif
3870 },
3871 {"gui_gtk3",
3872#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
3873 1
3874#else
3875 0
3876#endif
3877 },
3878 {"gui_gnome",
3879#ifdef FEAT_GUI_GNOME
3880 1
3881#else
3882 0
3883#endif
3884 },
3885 {"gui_haiku",
3886#ifdef FEAT_GUI_HAIKU
3887 1
3888#else
3889 0
3890#endif
3891 },
3892 {"gui_mac",
3893#ifdef FEAT_GUI_MAC
3894 1
3895#else
3896 0
3897#endif
3898 },
3899 {"gui_motif",
3900#ifdef FEAT_GUI_MOTIF
3901 1
3902#else
3903 0
3904#endif
3905 },
3906 {"gui_photon",
3907#ifdef FEAT_GUI_PHOTON
3908 1
3909#else
3910 0
3911#endif
3912 },
3913 {"gui_win32",
3914#ifdef FEAT_GUI_MSWIN
3915 1
3916#else
3917 0
3918#endif
3919 },
3920 {"iconv",
3921#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
3922 1
3923#else
3924 0
3925#endif
3926 },
3927 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02003928 {"ipv6",
3929#ifdef FEAT_IPV6
3930 1
3931#else
3932 0
3933#endif
3934 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003935 {"job",
3936#ifdef FEAT_JOB_CHANNEL
3937 1
3938#else
3939 0
3940#endif
3941 },
3942 {"jumplist",
3943#ifdef FEAT_JUMPLIST
3944 1
3945#else
3946 0
3947#endif
3948 },
3949 {"keymap",
3950#ifdef FEAT_KEYMAP
3951 1
3952#else
3953 0
3954#endif
3955 },
3956 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
3957 {"langmap",
3958#ifdef FEAT_LANGMAP
3959 1
3960#else
3961 0
3962#endif
3963 },
3964 {"libcall",
3965#ifdef FEAT_LIBCALL
3966 1
3967#else
3968 0
3969#endif
3970 },
3971 {"linebreak",
3972#ifdef FEAT_LINEBREAK
3973 1
3974#else
3975 0
3976#endif
3977 },
3978 {"lispindent",
3979#ifdef FEAT_LISP
3980 1
3981#else
3982 0
3983#endif
3984 },
3985 {"listcmds", 1},
3986 {"localmap", 1},
3987 {"lua",
3988#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
3989 1
3990#else
3991 0
3992#endif
3993 },
3994 {"menu",
3995#ifdef FEAT_MENU
3996 1
3997#else
3998 0
3999#endif
4000 },
4001 {"mksession",
4002#ifdef FEAT_SESSION
4003 1
4004#else
4005 0
4006#endif
4007 },
4008 {"modify_fname", 1},
4009 {"mouse", 1},
4010 {"mouseshape",
4011#ifdef FEAT_MOUSESHAPE
4012 1
4013#else
4014 0
4015#endif
4016 },
4017 {"mouse_dec",
4018#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4019 1
4020#else
4021 0
4022#endif
4023 },
4024 {"mouse_gpm",
4025#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4026 1
4027#else
4028 0
4029#endif
4030 },
4031 {"mouse_jsbterm",
4032#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4033 1
4034#else
4035 0
4036#endif
4037 },
4038 {"mouse_netterm",
4039#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4040 1
4041#else
4042 0
4043#endif
4044 },
4045 {"mouse_pterm",
4046#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4047 1
4048#else
4049 0
4050#endif
4051 },
4052 {"mouse_sgr",
4053#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4054 1
4055#else
4056 0
4057#endif
4058 },
4059 {"mouse_sysmouse",
4060#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4061 1
4062#else
4063 0
4064#endif
4065 },
4066 {"mouse_urxvt",
4067#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4068 1
4069#else
4070 0
4071#endif
4072 },
4073 {"mouse_xterm",
4074#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4075 1
4076#else
4077 0
4078#endif
4079 },
4080 {"multi_byte", 1},
4081 {"multi_byte_ime",
4082#ifdef FEAT_MBYTE_IME
4083 1
4084#else
4085 0
4086#endif
4087 },
4088 {"multi_lang",
4089#ifdef FEAT_MULTI_LANG
4090 1
4091#else
4092 0
4093#endif
4094 },
4095 {"mzscheme",
4096#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4097 1
4098#else
4099 0
4100#endif
4101 },
4102 {"num64", 1},
4103 {"ole",
4104#ifdef FEAT_OLE
4105 1
4106#else
4107 0
4108#endif
4109 },
4110 {"packages",
4111#ifdef FEAT_EVAL
4112 1
4113#else
4114 0
4115#endif
4116 },
4117 {"path_extra",
4118#ifdef FEAT_PATH_EXTRA
4119 1
4120#else
4121 0
4122#endif
4123 },
4124 {"perl",
4125#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4126 1
4127#else
4128 0
4129#endif
4130 },
4131 {"persistent_undo",
4132#ifdef FEAT_PERSISTENT_UNDO
4133 1
4134#else
4135 0
4136#endif
4137 },
4138 {"python_compiled",
4139#if defined(FEAT_PYTHON)
4140 1
4141#else
4142 0
4143#endif
4144 },
4145 {"python_dynamic",
4146#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4147 1
4148#else
4149 0
4150#endif
4151 },
4152 {"python",
4153#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4154 1
4155#else
4156 0
4157#endif
4158 },
4159 {"pythonx",
4160#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4161 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4162 1
4163#else
4164 0
4165#endif
4166 },
4167 {"python3_compiled",
4168#if defined(FEAT_PYTHON3)
4169 1
4170#else
4171 0
4172#endif
4173 },
4174 {"python3_dynamic",
4175#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4176 1
4177#else
4178 0
4179#endif
4180 },
4181 {"python3",
4182#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4183 1
4184#else
4185 0
4186#endif
4187 },
4188 {"popupwin",
4189#ifdef FEAT_PROP_POPUP
4190 1
4191#else
4192 0
4193#endif
4194 },
4195 {"postscript",
4196#ifdef FEAT_POSTSCRIPT
4197 1
4198#else
4199 0
4200#endif
4201 },
4202 {"printer",
4203#ifdef FEAT_PRINTER
4204 1
4205#else
4206 0
4207#endif
4208 },
4209 {"profile",
4210#ifdef FEAT_PROFILE
4211 1
4212#else
4213 0
4214#endif
4215 },
4216 {"reltime",
4217#ifdef FEAT_RELTIME
4218 1
4219#else
4220 0
4221#endif
4222 },
4223 {"quickfix",
4224#ifdef FEAT_QUICKFIX
4225 1
4226#else
4227 0
4228#endif
4229 },
4230 {"rightleft",
4231#ifdef FEAT_RIGHTLEFT
4232 1
4233#else
4234 0
4235#endif
4236 },
4237 {"ruby",
4238#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
4239 1
4240#else
4241 0
4242#endif
4243 },
4244 {"scrollbind", 1},
4245 {"showcmd",
4246#ifdef FEAT_CMDL_INFO
4247 1
4248#else
4249 0
4250#endif
4251 },
4252 {"cmdline_info",
4253#ifdef FEAT_CMDL_INFO
4254 1
4255#else
4256 0
4257#endif
4258 },
4259 {"signs",
4260#ifdef FEAT_SIGNS
4261 1
4262#else
4263 0
4264#endif
4265 },
4266 {"smartindent",
4267#ifdef FEAT_SMARTINDENT
4268 1
4269#else
4270 0
4271#endif
4272 },
4273 {"startuptime",
4274#ifdef STARTUPTIME
4275 1
4276#else
4277 0
4278#endif
4279 },
4280 {"statusline",
4281#ifdef FEAT_STL_OPT
4282 1
4283#else
4284 0
4285#endif
4286 },
4287 {"netbeans_intg",
4288#ifdef FEAT_NETBEANS_INTG
4289 1
4290#else
4291 0
4292#endif
4293 },
4294 {"sound",
4295#ifdef FEAT_SOUND
4296 1
4297#else
4298 0
4299#endif
4300 },
4301 {"spell",
4302#ifdef FEAT_SPELL
4303 1
4304#else
4305 0
4306#endif
4307 },
4308 {"syntax",
4309#ifdef FEAT_SYN_HL
4310 1
4311#else
4312 0
4313#endif
4314 },
4315 {"system",
4316#if defined(USE_SYSTEM) || !defined(UNIX)
4317 1
4318#else
4319 0
4320#endif
4321 },
4322 {"tag_binary",
4323#ifdef FEAT_TAG_BINS
4324 1
4325#else
4326 0
4327#endif
4328 },
4329 {"tcl",
4330#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
4331 1
4332#else
4333 0
4334#endif
4335 },
4336 {"termguicolors",
4337#ifdef FEAT_TERMGUICOLORS
4338 1
4339#else
4340 0
4341#endif
4342 },
4343 {"terminal",
4344#if defined(FEAT_TERMINAL) && !defined(MSWIN)
4345 1
4346#else
4347 0
4348#endif
4349 },
4350 {"terminfo",
4351#ifdef TERMINFO
4352 1
4353#else
4354 0
4355#endif
4356 },
4357 {"termresponse",
4358#ifdef FEAT_TERMRESPONSE
4359 1
4360#else
4361 0
4362#endif
4363 },
4364 {"textobjects",
4365#ifdef FEAT_TEXTOBJ
4366 1
4367#else
4368 0
4369#endif
4370 },
4371 {"textprop",
4372#ifdef FEAT_PROP_POPUP
4373 1
4374#else
4375 0
4376#endif
4377 },
4378 {"tgetent",
4379#ifdef HAVE_TGETENT
4380 1
4381#else
4382 0
4383#endif
4384 },
4385 {"timers",
4386#ifdef FEAT_TIMERS
4387 1
4388#else
4389 0
4390#endif
4391 },
4392 {"title",
4393#ifdef FEAT_TITLE
4394 1
4395#else
4396 0
4397#endif
4398 },
4399 {"toolbar",
4400#ifdef FEAT_TOOLBAR
4401 1
4402#else
4403 0
4404#endif
4405 },
4406 {"unnamedplus",
4407#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4408 1
4409#else
4410 0
4411#endif
4412 },
4413 {"user-commands", 1}, // was accidentally included in 5.4
4414 {"user_commands", 1},
4415 {"vartabs",
4416#ifdef FEAT_VARTABS
4417 1
4418#else
4419 0
4420#endif
4421 },
4422 {"vertsplit", 1},
4423 {"viminfo",
4424#ifdef FEAT_VIMINFO
4425 1
4426#else
4427 0
4428#endif
4429 },
4430 {"vimscript-1", 1},
4431 {"vimscript-2", 1},
4432 {"vimscript-3", 1},
4433 {"vimscript-4", 1},
4434 {"virtualedit", 1},
4435 {"visual", 1},
4436 {"visualextra", 1},
4437 {"vreplace", 1},
4438 {"vtp",
4439#ifdef FEAT_VTP
4440 1
4441#else
4442 0
4443#endif
4444 },
4445 {"wildignore",
4446#ifdef FEAT_WILDIGN
4447 1
4448#else
4449 0
4450#endif
4451 },
4452 {"wildmenu",
4453#ifdef FEAT_WILDMENU
4454 1
4455#else
4456 0
4457#endif
4458 },
4459 {"windows", 1},
4460 {"winaltkeys",
4461#ifdef FEAT_WAK
4462 1
4463#else
4464 0
4465#endif
4466 },
4467 {"writebackup",
4468#ifdef FEAT_WRITEBACKUP
4469 1
4470#else
4471 0
4472#endif
4473 },
4474 {"xim",
4475#ifdef FEAT_XIM
4476 1
4477#else
4478 0
4479#endif
4480 },
4481 {"xfontset",
4482#ifdef FEAT_XFONTSET
4483 1
4484#else
4485 0
4486#endif
4487 },
4488 {"xpm",
4489#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
4490 1
4491#else
4492 0
4493#endif
4494 },
4495 {"xpm_w32", // for backward compatibility
4496#ifdef FEAT_XPM_W32
4497 1
4498#else
4499 0
4500#endif
4501 },
4502 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004503#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01004504 1
4505#else
4506 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004507#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004508 },
4509 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004510#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01004511 1
4512#else
4513 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004514#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004515 },
4516 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004517#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01004518 1
4519#else
4520 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004521#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004522 },
4523 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004524#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01004525 1
4526#else
4527 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004528#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004529 },
4530 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004531#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01004532 1
4533#else
4534 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004535#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004536 },
4537 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004538 };
4539
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004540 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01004541 for (i = 0; has_list[i].name != NULL; ++i)
4542 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004543 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004544 x = TRUE;
4545 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004546 break;
4547 }
4548
Bram Moolenaar79296512020-03-22 16:17:14 +01004549 // features also in has_list[] but sometimes enabled at runtime
4550 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004551 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004552 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02004553 {
4554 // intentionally empty
4555 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004556#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004557 else if (STRICMP(name, "balloon_multiline") == 0)
4558 n = multiline_balloon_available();
4559#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004560#ifdef VIMDLL
4561 else if (STRICMP(name, "filterpipe") == 0)
4562 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004563#endif
4564#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
4565 else if (STRICMP(name, "iconv") == 0)
4566 n = iconv_enabled(FALSE);
4567#endif
4568#ifdef DYNAMIC_LUA
4569 else if (STRICMP(name, "lua") == 0)
4570 n = lua_enabled(FALSE);
4571#endif
4572#ifdef DYNAMIC_MZSCHEME
4573 else if (STRICMP(name, "mzscheme") == 0)
4574 n = mzscheme_enabled(FALSE);
4575#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004576#ifdef DYNAMIC_PERL
4577 else if (STRICMP(name, "perl") == 0)
4578 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004579#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004580#ifdef DYNAMIC_PYTHON
4581 else if (STRICMP(name, "python") == 0)
4582 n = python_enabled(FALSE);
4583#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004584#ifdef DYNAMIC_PYTHON3
4585 else if (STRICMP(name, "python3") == 0)
4586 n = python3_enabled(FALSE);
4587#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004588#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
4589 else if (STRICMP(name, "pythonx") == 0)
4590 {
4591# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
4592 if (p_pyx == 0)
4593 n = python3_enabled(FALSE) || python_enabled(FALSE);
4594 else if (p_pyx == 3)
4595 n = python3_enabled(FALSE);
4596 else if (p_pyx == 2)
4597 n = python_enabled(FALSE);
4598# elif defined(DYNAMIC_PYTHON)
4599 n = python_enabled(FALSE);
4600# elif defined(DYNAMIC_PYTHON3)
4601 n = python3_enabled(FALSE);
4602# endif
4603 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004604#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004605#ifdef DYNAMIC_RUBY
4606 else if (STRICMP(name, "ruby") == 0)
4607 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004608#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004609#ifdef DYNAMIC_TCL
4610 else if (STRICMP(name, "tcl") == 0)
4611 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02004612#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01004613#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02004614 else if (STRICMP(name, "terminal") == 0)
4615 n = terminal_enabled();
4616#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004617 }
4618
Bram Moolenaar79296512020-03-22 16:17:14 +01004619 // features not in has_list[]
4620 if (x == FALSE)
4621 {
4622 if (STRNICMP(name, "patch", 5) == 0)
4623 {
4624 x = TRUE;
4625 if (name[5] == '-'
4626 && STRLEN(name) >= 11
4627 && vim_isdigit(name[6])
4628 && vim_isdigit(name[8])
4629 && vim_isdigit(name[10]))
4630 {
4631 int major = atoi((char *)name + 6);
4632 int minor = atoi((char *)name + 8);
4633
4634 // Expect "patch-9.9.01234".
4635 n = (major < VIM_VERSION_MAJOR
4636 || (major == VIM_VERSION_MAJOR
4637 && (minor < VIM_VERSION_MINOR
4638 || (minor == VIM_VERSION_MINOR
4639 && has_patch(atoi((char *)name + 10))))));
4640 }
4641 else
4642 n = has_patch(atoi((char *)name + 5));
4643 }
4644 else if (STRICMP(name, "vim_starting") == 0)
4645 {
4646 x = TRUE;
4647 n = (starting != 0);
4648 }
4649 else if (STRICMP(name, "ttyin") == 0)
4650 {
4651 x = TRUE;
4652 n = mch_input_isatty();
4653 }
4654 else if (STRICMP(name, "ttyout") == 0)
4655 {
4656 x = TRUE;
4657 n = stdout_isatty;
4658 }
4659 else if (STRICMP(name, "multi_byte_encoding") == 0)
4660 {
4661 x = TRUE;
4662 n = has_mbyte;
4663 }
4664 else if (STRICMP(name, "gui_running") == 0)
4665 {
4666 x = TRUE;
4667#ifdef FEAT_GUI
4668 n = (gui.in_use || gui.starting);
4669#endif
4670 }
4671 else if (STRICMP(name, "browse") == 0)
4672 {
4673 x = TRUE;
4674#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
4675 n = gui.in_use; // gui_mch_browse() works when GUI is running
4676#endif
4677 }
4678 else if (STRICMP(name, "syntax_items") == 0)
4679 {
4680 x = TRUE;
4681#ifdef FEAT_SYN_HL
4682 n = syntax_present(curwin);
4683#endif
4684 }
4685 else if (STRICMP(name, "vcon") == 0)
4686 {
4687 x = TRUE;
4688#ifdef FEAT_VTP
4689 n = is_term_win32() && has_vtp_working();
4690#endif
4691 }
4692 else if (STRICMP(name, "netbeans_enabled") == 0)
4693 {
4694 x = TRUE;
4695#ifdef FEAT_NETBEANS_INTG
4696 n = netbeans_active();
4697#endif
4698 }
4699 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
4700 {
4701 x = TRUE;
4702#ifdef FEAT_MOUSE_GPM
4703 n = gpm_enabled();
4704#endif
4705 }
4706 else if (STRICMP(name, "conpty") == 0)
4707 {
4708 x = TRUE;
4709#if defined(FEAT_TERMINAL) && defined(MSWIN)
4710 n = use_conpty();
4711#endif
4712 }
4713 else if (STRICMP(name, "clipboard_working") == 0)
4714 {
4715 x = TRUE;
4716#ifdef FEAT_CLIPBOARD
4717 n = clip_star.available;
4718#endif
4719 }
4720 }
4721
4722 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_number(&argvars[1]) != 0)
4723 // return whether feature could ever be enabled
4724 rettv->vval.v_number = x;
4725 else
4726 // return whether feature is enabled
4727 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004728}
4729
4730/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004731 * "haslocaldir()" function
4732 */
4733 static void
4734f_haslocaldir(typval_T *argvars, typval_T *rettv)
4735{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004736 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004737 win_T *wp = NULL;
4738
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004739 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
4740
4741 // Check for window-local and tab-local directories
4742 if (wp != NULL && wp->w_localdir != NULL)
4743 rettv->vval.v_number = 1;
4744 else if (tp != NULL && tp->tp_localdir != NULL)
4745 rettv->vval.v_number = 2;
4746 else
4747 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004748}
4749
4750/*
4751 * "hasmapto()" function
4752 */
4753 static void
4754f_hasmapto(typval_T *argvars, typval_T *rettv)
4755{
4756 char_u *name;
4757 char_u *mode;
4758 char_u buf[NUMBUFLEN];
4759 int abbr = FALSE;
4760
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004761 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004762 if (argvars[1].v_type == VAR_UNKNOWN)
4763 mode = (char_u *)"nvo";
4764 else
4765 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004766 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004767 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004768 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004769 }
4770
4771 if (map_to_exists(name, mode, abbr))
4772 rettv->vval.v_number = TRUE;
4773 else
4774 rettv->vval.v_number = FALSE;
4775}
4776
4777/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004778 * "highlightID(name)" function
4779 */
4780 static void
4781f_hlID(typval_T *argvars, typval_T *rettv)
4782{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004783 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004784}
4785
4786/*
4787 * "highlight_exists()" function
4788 */
4789 static void
4790f_hlexists(typval_T *argvars, typval_T *rettv)
4791{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004792 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004793}
4794
4795/*
4796 * "hostname()" function
4797 */
4798 static void
4799f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
4800{
4801 char_u hostname[256];
4802
4803 mch_get_host_name(hostname, 256);
4804 rettv->v_type = VAR_STRING;
4805 rettv->vval.v_string = vim_strsave(hostname);
4806}
4807
4808/*
4809 * iconv() function
4810 */
4811 static void
4812f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
4813{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004814 char_u buf1[NUMBUFLEN];
4815 char_u buf2[NUMBUFLEN];
4816 char_u *from, *to, *str;
4817 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004818
4819 rettv->v_type = VAR_STRING;
4820 rettv->vval.v_string = NULL;
4821
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004822 str = tv_get_string(&argvars[0]);
4823 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
4824 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004825 vimconv.vc_type = CONV_NONE;
4826 convert_setup(&vimconv, from, to);
4827
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004828 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004829 if (vimconv.vc_type == CONV_NONE)
4830 rettv->vval.v_string = vim_strsave(str);
4831 else
4832 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
4833
4834 convert_setup(&vimconv, NULL, NULL);
4835 vim_free(from);
4836 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004837}
4838
4839/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004840 * "index()" function
4841 */
4842 static void
4843f_index(typval_T *argvars, typval_T *rettv)
4844{
4845 list_T *l;
4846 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004847 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004848 long idx = 0;
4849 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004850 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004851
4852 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004853 if (argvars[0].v_type == VAR_BLOB)
4854 {
4855 typval_T tv;
4856 int start = 0;
4857
4858 if (argvars[2].v_type != VAR_UNKNOWN)
4859 {
4860 start = tv_get_number_chk(&argvars[2], &error);
4861 if (error)
4862 return;
4863 }
4864 b = argvars[0].vval.v_blob;
4865 if (b == NULL)
4866 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01004867 if (start < 0)
4868 {
4869 start = blob_len(b) + start;
4870 if (start < 0)
4871 start = 0;
4872 }
4873
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004874 for (idx = start; idx < blob_len(b); ++idx)
4875 {
4876 tv.v_type = VAR_NUMBER;
4877 tv.vval.v_number = blob_get(b, idx);
4878 if (tv_equal(&tv, &argvars[1], ic, FALSE))
4879 {
4880 rettv->vval.v_number = idx;
4881 return;
4882 }
4883 }
4884 return;
4885 }
4886 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004887 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004888 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004889 return;
4890 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004891
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004892 l = argvars[0].vval.v_list;
4893 if (l != NULL)
4894 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02004895 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004896 item = l->lv_first;
4897 if (argvars[2].v_type != VAR_UNKNOWN)
4898 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004899 // Start at specified item. Use the cached index that list_find()
4900 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004901 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004902 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004903 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004904 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004905 if (error)
4906 item = NULL;
4907 }
4908
4909 for ( ; item != NULL; item = item->li_next, ++idx)
4910 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
4911 {
4912 rettv->vval.v_number = idx;
4913 break;
4914 }
4915 }
4916}
4917
4918static int inputsecret_flag = 0;
4919
4920/*
4921 * "input()" function
4922 * Also handles inputsecret() when inputsecret is set.
4923 */
4924 static void
4925f_input(typval_T *argvars, typval_T *rettv)
4926{
4927 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
4928}
4929
4930/*
4931 * "inputdialog()" function
4932 */
4933 static void
4934f_inputdialog(typval_T *argvars, typval_T *rettv)
4935{
4936#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004937 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004938 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4939 {
4940 char_u *message;
4941 char_u buf[NUMBUFLEN];
4942 char_u *defstr = (char_u *)"";
4943
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004944 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004945 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004946 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004947 vim_strncpy(IObuff, defstr, IOSIZE - 1);
4948 else
4949 IObuff[0] = NUL;
4950 if (message != NULL && defstr != NULL
4951 && do_dialog(VIM_QUESTION, NULL, message,
4952 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
4953 rettv->vval.v_string = vim_strsave(IObuff);
4954 else
4955 {
4956 if (message != NULL && defstr != NULL
4957 && argvars[1].v_type != VAR_UNKNOWN
4958 && argvars[2].v_type != VAR_UNKNOWN)
4959 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004960 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004961 else
4962 rettv->vval.v_string = NULL;
4963 }
4964 rettv->v_type = VAR_STRING;
4965 }
4966 else
4967#endif
4968 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
4969}
4970
4971/*
4972 * "inputlist()" function
4973 */
4974 static void
4975f_inputlist(typval_T *argvars, typval_T *rettv)
4976{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004977 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004978 listitem_T *li;
4979 int selected;
4980 int mouse_used;
4981
4982#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004983 // While starting up, there is no place to enter text. When running tests
4984 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02004985 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004986 return;
4987#endif
4988 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
4989 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004990 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004991 return;
4992 }
4993
4994 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004995 msg_row = Rows - 1; // for when 'cmdheight' > 1
4996 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004997 msg_scroll = TRUE;
4998 msg_clr_eos();
4999
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005000 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005001 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005002 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005003 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005004 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005005 msg_putchar('\n');
5006 }
5007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005008 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005009 selected = prompt_for_number(&mouse_used);
5010 if (mouse_used)
5011 selected -= lines_left;
5012
5013 rettv->vval.v_number = selected;
5014}
5015
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005016static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5017
5018/*
5019 * "inputrestore()" function
5020 */
5021 static void
5022f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5023{
5024 if (ga_userinput.ga_len > 0)
5025 {
5026 --ga_userinput.ga_len;
5027 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5028 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005029 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005030 }
5031 else if (p_verbose > 1)
5032 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005033 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005034 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005035 }
5036}
5037
5038/*
5039 * "inputsave()" function
5040 */
5041 static void
5042f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5043{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005044 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005045 if (ga_grow(&ga_userinput, 1) == OK)
5046 {
5047 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5048 + ga_userinput.ga_len);
5049 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005050 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005051 }
5052 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005053 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005054}
5055
5056/*
5057 * "inputsecret()" function
5058 */
5059 static void
5060f_inputsecret(typval_T *argvars, typval_T *rettv)
5061{
5062 ++cmdline_star;
5063 ++inputsecret_flag;
5064 f_input(argvars, rettv);
5065 --cmdline_star;
5066 --inputsecret_flag;
5067}
5068
5069/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005070 * "interrupt()" function
5071 */
5072 static void
5073f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5074{
5075 got_int = TRUE;
5076}
5077
5078/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005079 * "invert(expr)" function
5080 */
5081 static void
5082f_invert(typval_T *argvars, typval_T *rettv)
5083{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005084 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005085}
5086
5087/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005088 * "islocked()" function
5089 */
5090 static void
5091f_islocked(typval_T *argvars, typval_T *rettv)
5092{
5093 lval_T lv;
5094 char_u *end;
5095 dictitem_T *di;
5096
5097 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005098 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005099 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005100 if (end != NULL && lv.ll_name != NULL)
5101 {
5102 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005103 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005104 else
5105 {
5106 if (lv.ll_tv == NULL)
5107 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005108 di = find_var(lv.ll_name, NULL, TRUE);
5109 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005110 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005111 // Consider a variable locked when:
5112 // 1. the variable itself is locked
5113 // 2. the value of the variable is locked.
5114 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005115 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5116 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005117 }
5118 }
5119 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005120 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005121 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005122 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005123 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005124 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005125 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5126 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005127 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005128 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5129 }
5130 }
5131
5132 clear_lval(&lv);
5133}
5134
5135#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
5136/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02005137 * "isinf()" function
5138 */
5139 static void
5140f_isinf(typval_T *argvars, typval_T *rettv)
5141{
5142 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
5143 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
5144}
5145
5146/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005147 * "isnan()" function
5148 */
5149 static void
5150f_isnan(typval_T *argvars, typval_T *rettv)
5151{
5152 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
5153 && isnan(argvars[0].vval.v_float);
5154}
5155#endif
5156
5157/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005158 * "last_buffer_nr()" function.
5159 */
5160 static void
5161f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5162{
5163 int n = 0;
5164 buf_T *buf;
5165
Bram Moolenaar29323592016-07-24 22:04:11 +02005166 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005167 if (n < buf->b_fnum)
5168 n = buf->b_fnum;
5169
5170 rettv->vval.v_number = n;
5171}
5172
5173/*
5174 * "len()" function
5175 */
5176 static void
5177f_len(typval_T *argvars, typval_T *rettv)
5178{
5179 switch (argvars[0].v_type)
5180 {
5181 case VAR_STRING:
5182 case VAR_NUMBER:
5183 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005184 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005185 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005186 case VAR_BLOB:
5187 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
5188 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005189 case VAR_LIST:
5190 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
5191 break;
5192 case VAR_DICT:
5193 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
5194 break;
5195 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005196 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005198 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005199 case VAR_SPECIAL:
5200 case VAR_FLOAT:
5201 case VAR_FUNC:
5202 case VAR_PARTIAL:
5203 case VAR_JOB:
5204 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005205 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005206 break;
5207 }
5208}
5209
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005210 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01005211libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005212{
5213#ifdef FEAT_LIBCALL
5214 char_u *string_in;
5215 char_u **string_result;
5216 int nr_result;
5217#endif
5218
5219 rettv->v_type = type;
5220 if (type != VAR_NUMBER)
5221 rettv->vval.v_string = NULL;
5222
5223 if (check_restricted() || check_secure())
5224 return;
5225
5226#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005227 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005228 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
5229 {
5230 string_in = NULL;
5231 if (argvars[2].v_type == VAR_STRING)
5232 string_in = argvars[2].vval.v_string;
5233 if (type == VAR_NUMBER)
5234 string_result = NULL;
5235 else
5236 string_result = &rettv->vval.v_string;
5237 if (mch_libcall(argvars[0].vval.v_string,
5238 argvars[1].vval.v_string,
5239 string_in,
5240 argvars[2].vval.v_number,
5241 string_result,
5242 &nr_result) == OK
5243 && type == VAR_NUMBER)
5244 rettv->vval.v_number = nr_result;
5245 }
5246#endif
5247}
5248
5249/*
5250 * "libcall()" function
5251 */
5252 static void
5253f_libcall(typval_T *argvars, typval_T *rettv)
5254{
5255 libcall_common(argvars, rettv, VAR_STRING);
5256}
5257
5258/*
5259 * "libcallnr()" function
5260 */
5261 static void
5262f_libcallnr(typval_T *argvars, typval_T *rettv)
5263{
5264 libcall_common(argvars, rettv, VAR_NUMBER);
5265}
5266
5267/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005268 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005269 */
5270 static void
5271f_line(typval_T *argvars, typval_T *rettv)
5272{
5273 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005274 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005275 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005276 int id;
5277 tabpage_T *tp;
5278 win_T *wp;
5279 win_T *save_curwin;
5280 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005281
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005282 if (argvars[1].v_type != VAR_UNKNOWN)
5283 {
5284 // use window specified in the second argument
5285 id = (int)tv_get_number(&argvars[1]);
5286 wp = win_id2wp_tp(id, &tp);
5287 if (wp != NULL && tp != NULL)
5288 {
5289 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
5290 == OK)
5291 {
5292 check_cursor();
5293 fp = var2fpos(&argvars[0], TRUE, &fnum);
5294 }
5295 restore_win_noblock(save_curwin, save_curtab, TRUE);
5296 }
5297 }
5298 else
5299 // use current window
5300 fp = var2fpos(&argvars[0], TRUE, &fnum);
5301
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005302 if (fp != NULL)
5303 lnum = fp->lnum;
5304 rettv->vval.v_number = lnum;
5305}
5306
5307/*
5308 * "line2byte(lnum)" function
5309 */
5310 static void
5311f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
5312{
5313#ifndef FEAT_BYTEOFF
5314 rettv->vval.v_number = -1;
5315#else
5316 linenr_T lnum;
5317
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005318 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005319 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
5320 rettv->vval.v_number = -1;
5321 else
5322 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
5323 if (rettv->vval.v_number >= 0)
5324 ++rettv->vval.v_number;
5325#endif
5326}
5327
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005328#ifdef FEAT_FLOAT
5329/*
5330 * "log()" function
5331 */
5332 static void
5333f_log(typval_T *argvars, typval_T *rettv)
5334{
5335 float_T f = 0.0;
5336
5337 rettv->v_type = VAR_FLOAT;
5338 if (get_float_arg(argvars, &f) == OK)
5339 rettv->vval.v_float = log(f);
5340 else
5341 rettv->vval.v_float = 0.0;
5342}
5343
5344/*
5345 * "log10()" function
5346 */
5347 static void
5348f_log10(typval_T *argvars, typval_T *rettv)
5349{
5350 float_T f = 0.0;
5351
5352 rettv->v_type = VAR_FLOAT;
5353 if (get_float_arg(argvars, &f) == OK)
5354 rettv->vval.v_float = log10(f);
5355 else
5356 rettv->vval.v_float = 0.0;
5357}
5358#endif
5359
5360#ifdef FEAT_LUA
5361/*
5362 * "luaeval()" function
5363 */
5364 static void
5365f_luaeval(typval_T *argvars, typval_T *rettv)
5366{
5367 char_u *str;
5368 char_u buf[NUMBUFLEN];
5369
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005370 if (check_restricted() || check_secure())
5371 return;
5372
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005373 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005374 do_luaeval(str, argvars + 1, rettv);
5375}
5376#endif
5377
5378/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005379 * "maparg()" function
5380 */
5381 static void
5382f_maparg(typval_T *argvars, typval_T *rettv)
5383{
5384 get_maparg(argvars, rettv, TRUE);
5385}
5386
5387/*
5388 * "mapcheck()" function
5389 */
5390 static void
5391f_mapcheck(typval_T *argvars, typval_T *rettv)
5392{
5393 get_maparg(argvars, rettv, FALSE);
5394}
5395
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005396typedef enum
5397{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005398 MATCH_END, // matchend()
5399 MATCH_MATCH, // match()
5400 MATCH_STR, // matchstr()
5401 MATCH_LIST, // matchlist()
5402 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005403} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005404
5405 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005406find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005407{
5408 char_u *str = NULL;
5409 long len = 0;
5410 char_u *expr = NULL;
5411 char_u *pat;
5412 regmatch_T regmatch;
5413 char_u patbuf[NUMBUFLEN];
5414 char_u strbuf[NUMBUFLEN];
5415 char_u *save_cpo;
5416 long start = 0;
5417 long nth = 1;
5418 colnr_T startcol = 0;
5419 int match = 0;
5420 list_T *l = NULL;
5421 listitem_T *li = NULL;
5422 long idx = 0;
5423 char_u *tofree = NULL;
5424
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005425 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005426 save_cpo = p_cpo;
5427 p_cpo = (char_u *)"";
5428
5429 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005430 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005431 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005432 // type MATCH_LIST: return empty list when there are no matches.
5433 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005434 if (rettv_list_alloc(rettv) == FAIL)
5435 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005436 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005437 && (list_append_string(rettv->vval.v_list,
5438 (char_u *)"", 0) == FAIL
5439 || list_append_number(rettv->vval.v_list,
5440 (varnumber_T)-1) == FAIL
5441 || list_append_number(rettv->vval.v_list,
5442 (varnumber_T)-1) == FAIL
5443 || list_append_number(rettv->vval.v_list,
5444 (varnumber_T)-1) == FAIL))
5445 {
5446 list_free(rettv->vval.v_list);
5447 rettv->vval.v_list = NULL;
5448 goto theend;
5449 }
5450 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005451 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005452 {
5453 rettv->v_type = VAR_STRING;
5454 rettv->vval.v_string = NULL;
5455 }
5456
5457 if (argvars[0].v_type == VAR_LIST)
5458 {
5459 if ((l = argvars[0].vval.v_list) == NULL)
5460 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005461 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005462 li = l->lv_first;
5463 }
5464 else
5465 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005466 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005467 len = (long)STRLEN(str);
5468 }
5469
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005470 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005471 if (pat == NULL)
5472 goto theend;
5473
5474 if (argvars[2].v_type != VAR_UNKNOWN)
5475 {
5476 int error = FALSE;
5477
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005478 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005479 if (error)
5480 goto theend;
5481 if (l != NULL)
5482 {
5483 li = list_find(l, start);
5484 if (li == NULL)
5485 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005486 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005487 }
5488 else
5489 {
5490 if (start < 0)
5491 start = 0;
5492 if (start > len)
5493 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005494 // When "count" argument is there ignore matches before "start",
5495 // otherwise skip part of the string. Differs when pattern is "^"
5496 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005497 if (argvars[3].v_type != VAR_UNKNOWN)
5498 startcol = start;
5499 else
5500 {
5501 str += start;
5502 len -= start;
5503 }
5504 }
5505
5506 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005507 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005508 if (error)
5509 goto theend;
5510 }
5511
5512 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5513 if (regmatch.regprog != NULL)
5514 {
5515 regmatch.rm_ic = p_ic;
5516
5517 for (;;)
5518 {
5519 if (l != NULL)
5520 {
5521 if (li == NULL)
5522 {
5523 match = FALSE;
5524 break;
5525 }
5526 vim_free(tofree);
5527 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
5528 if (str == NULL)
5529 break;
5530 }
5531
5532 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
5533
5534 if (match && --nth <= 0)
5535 break;
5536 if (l == NULL && !match)
5537 break;
5538
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005539 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005540 if (l != NULL)
5541 {
5542 li = li->li_next;
5543 ++idx;
5544 }
5545 else
5546 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005547 startcol = (colnr_T)(regmatch.startp[0]
5548 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005549 if (startcol > (colnr_T)len
5550 || str + startcol <= regmatch.startp[0])
5551 {
5552 match = FALSE;
5553 break;
5554 }
5555 }
5556 }
5557
5558 if (match)
5559 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005560 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005561 {
5562 listitem_T *li1 = rettv->vval.v_list->lv_first;
5563 listitem_T *li2 = li1->li_next;
5564 listitem_T *li3 = li2->li_next;
5565 listitem_T *li4 = li3->li_next;
5566
5567 vim_free(li1->li_tv.vval.v_string);
5568 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005569 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005570 li3->li_tv.vval.v_number =
5571 (varnumber_T)(regmatch.startp[0] - expr);
5572 li4->li_tv.vval.v_number =
5573 (varnumber_T)(regmatch.endp[0] - expr);
5574 if (l != NULL)
5575 li2->li_tv.vval.v_number = (varnumber_T)idx;
5576 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005577 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005578 {
5579 int i;
5580
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005581 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005582 for (i = 0; i < NSUBEXP; ++i)
5583 {
5584 if (regmatch.endp[i] == NULL)
5585 {
5586 if (list_append_string(rettv->vval.v_list,
5587 (char_u *)"", 0) == FAIL)
5588 break;
5589 }
5590 else if (list_append_string(rettv->vval.v_list,
5591 regmatch.startp[i],
5592 (int)(regmatch.endp[i] - regmatch.startp[i]))
5593 == FAIL)
5594 break;
5595 }
5596 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005597 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005598 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005599 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005600 if (l != NULL)
5601 copy_tv(&li->li_tv, rettv);
5602 else
5603 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005604 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005605 }
5606 else if (l != NULL)
5607 rettv->vval.v_number = idx;
5608 else
5609 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005610 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005611 rettv->vval.v_number =
5612 (varnumber_T)(regmatch.startp[0] - str);
5613 else
5614 rettv->vval.v_number =
5615 (varnumber_T)(regmatch.endp[0] - str);
5616 rettv->vval.v_number += (varnumber_T)(str - expr);
5617 }
5618 }
5619 vim_regfree(regmatch.regprog);
5620 }
5621
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005622theend:
5623 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005624 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005625 listitem_remove(rettv->vval.v_list,
5626 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005627 vim_free(tofree);
5628 p_cpo = save_cpo;
5629}
5630
5631/*
5632 * "match()" function
5633 */
5634 static void
5635f_match(typval_T *argvars, typval_T *rettv)
5636{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005637 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005638}
5639
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005640/*
5641 * "matchend()" function
5642 */
5643 static void
5644f_matchend(typval_T *argvars, typval_T *rettv)
5645{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005646 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005647}
5648
5649/*
5650 * "matchlist()" function
5651 */
5652 static void
5653f_matchlist(typval_T *argvars, typval_T *rettv)
5654{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005655 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005656}
5657
5658/*
5659 * "matchstr()" function
5660 */
5661 static void
5662f_matchstr(typval_T *argvars, typval_T *rettv)
5663{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005664 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005665}
5666
5667/*
5668 * "matchstrpos()" function
5669 */
5670 static void
5671f_matchstrpos(typval_T *argvars, typval_T *rettv)
5672{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005673 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005674}
5675
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005676 static void
5677max_min(typval_T *argvars, typval_T *rettv, int domax)
5678{
5679 varnumber_T n = 0;
5680 varnumber_T i;
5681 int error = FALSE;
5682
5683 if (argvars[0].v_type == VAR_LIST)
5684 {
5685 list_T *l;
5686 listitem_T *li;
5687
5688 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005689 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005690 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005691 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005692 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005693 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
5694 n = l->lv_u.nonmat.lv_start;
5695 else
5696 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
5697 * l->lv_u.nonmat.lv_stride;
5698 }
5699 else
5700 {
5701 li = l->lv_first;
5702 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005703 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005704 n = tv_get_number_chk(&li->li_tv, &error);
5705 for (;;)
5706 {
5707 li = li->li_next;
5708 if (li == NULL)
5709 break;
5710 i = tv_get_number_chk(&li->li_tv, &error);
5711 if (domax ? i > n : i < n)
5712 n = i;
5713 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005714 }
5715 }
5716 }
5717 }
5718 else if (argvars[0].v_type == VAR_DICT)
5719 {
5720 dict_T *d;
5721 int first = TRUE;
5722 hashitem_T *hi;
5723 int todo;
5724
5725 d = argvars[0].vval.v_dict;
5726 if (d != NULL)
5727 {
5728 todo = (int)d->dv_hashtab.ht_used;
5729 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
5730 {
5731 if (!HASHITEM_EMPTY(hi))
5732 {
5733 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005734 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005735 if (first)
5736 {
5737 n = i;
5738 first = FALSE;
5739 }
5740 else if (domax ? i > n : i < n)
5741 n = i;
5742 }
5743 }
5744 }
5745 }
5746 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005747 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005748 rettv->vval.v_number = error ? 0 : n;
5749}
5750
5751/*
5752 * "max()" function
5753 */
5754 static void
5755f_max(typval_T *argvars, typval_T *rettv)
5756{
5757 max_min(argvars, rettv, TRUE);
5758}
5759
5760/*
5761 * "min()" function
5762 */
5763 static void
5764f_min(typval_T *argvars, typval_T *rettv)
5765{
5766 max_min(argvars, rettv, FALSE);
5767}
5768
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005769#if defined(FEAT_MZSCHEME) || defined(PROTO)
5770/*
5771 * "mzeval()" function
5772 */
5773 static void
5774f_mzeval(typval_T *argvars, typval_T *rettv)
5775{
5776 char_u *str;
5777 char_u buf[NUMBUFLEN];
5778
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005779 if (check_restricted() || check_secure())
5780 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005781 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005782 do_mzeval(str, rettv);
5783}
5784
5785 void
5786mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
5787{
5788 typval_T argvars[3];
5789
5790 argvars[0].v_type = VAR_STRING;
5791 argvars[0].vval.v_string = name;
5792 copy_tv(args, &argvars[1]);
5793 argvars[2].v_type = VAR_UNKNOWN;
5794 f_call(argvars, rettv);
5795 clear_tv(&argvars[1]);
5796}
5797#endif
5798
5799/*
5800 * "nextnonblank()" function
5801 */
5802 static void
5803f_nextnonblank(typval_T *argvars, typval_T *rettv)
5804{
5805 linenr_T lnum;
5806
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005807 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005808 {
5809 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
5810 {
5811 lnum = 0;
5812 break;
5813 }
5814 if (*skipwhite(ml_get(lnum)) != NUL)
5815 break;
5816 }
5817 rettv->vval.v_number = lnum;
5818}
5819
5820/*
5821 * "nr2char()" function
5822 */
5823 static void
5824f_nr2char(typval_T *argvars, typval_T *rettv)
5825{
5826 char_u buf[NUMBUFLEN];
5827
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005828 if (has_mbyte)
5829 {
5830 int utf8 = 0;
5831
5832 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005833 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005834 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01005835 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005836 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005837 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005838 }
5839 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005840 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005841 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005842 buf[1] = NUL;
5843 }
5844 rettv->v_type = VAR_STRING;
5845 rettv->vval.v_string = vim_strsave(buf);
5846}
5847
5848/*
5849 * "or(expr, expr)" function
5850 */
5851 static void
5852f_or(typval_T *argvars, typval_T *rettv)
5853{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005854 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
5855 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005856}
5857
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005858#ifdef FEAT_PERL
5859/*
5860 * "perleval()" function
5861 */
5862 static void
5863f_perleval(typval_T *argvars, typval_T *rettv)
5864{
5865 char_u *str;
5866 char_u buf[NUMBUFLEN];
5867
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005868 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005869 do_perleval(str, rettv);
5870}
5871#endif
5872
5873#ifdef FEAT_FLOAT
5874/*
5875 * "pow()" function
5876 */
5877 static void
5878f_pow(typval_T *argvars, typval_T *rettv)
5879{
5880 float_T fx = 0.0, fy = 0.0;
5881
5882 rettv->v_type = VAR_FLOAT;
5883 if (get_float_arg(argvars, &fx) == OK
5884 && get_float_arg(&argvars[1], &fy) == OK)
5885 rettv->vval.v_float = pow(fx, fy);
5886 else
5887 rettv->vval.v_float = 0.0;
5888}
5889#endif
5890
5891/*
5892 * "prevnonblank()" function
5893 */
5894 static void
5895f_prevnonblank(typval_T *argvars, typval_T *rettv)
5896{
5897 linenr_T lnum;
5898
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005899 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005900 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5901 lnum = 0;
5902 else
5903 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
5904 --lnum;
5905 rettv->vval.v_number = lnum;
5906}
5907
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005908// This dummy va_list is here because:
5909// - passing a NULL pointer doesn't work when va_list isn't a pointer
5910// - locally in the function results in a "used before set" warning
5911// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005912static va_list ap;
5913
5914/*
5915 * "printf()" function
5916 */
5917 static void
5918f_printf(typval_T *argvars, typval_T *rettv)
5919{
5920 char_u buf[NUMBUFLEN];
5921 int len;
5922 char_u *s;
5923 int saved_did_emsg = did_emsg;
5924 char *fmt;
5925
5926 rettv->v_type = VAR_STRING;
5927 rettv->vval.v_string = NULL;
5928
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005929 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005930 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005931 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005932 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005933 if (!did_emsg)
5934 {
5935 s = alloc(len + 1);
5936 if (s != NULL)
5937 {
5938 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005939 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
5940 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005941 }
5942 }
5943 did_emsg |= saved_did_emsg;
5944}
5945
5946/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005947 * "pum_getpos()" function
5948 */
5949 static void
5950f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5951{
5952 if (rettv_dict_alloc(rettv) != OK)
5953 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005954 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005955}
5956
5957/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005958 * "pumvisible()" function
5959 */
5960 static void
5961f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5962{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005963 if (pum_visible())
5964 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005965}
5966
5967#ifdef FEAT_PYTHON3
5968/*
5969 * "py3eval()" function
5970 */
5971 static void
5972f_py3eval(typval_T *argvars, typval_T *rettv)
5973{
5974 char_u *str;
5975 char_u buf[NUMBUFLEN];
5976
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005977 if (check_restricted() || check_secure())
5978 return;
5979
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005980 if (p_pyx == 0)
5981 p_pyx = 3;
5982
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005983 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005984 do_py3eval(str, rettv);
5985}
5986#endif
5987
5988#ifdef FEAT_PYTHON
5989/*
5990 * "pyeval()" function
5991 */
5992 static void
5993f_pyeval(typval_T *argvars, typval_T *rettv)
5994{
5995 char_u *str;
5996 char_u buf[NUMBUFLEN];
5997
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005998 if (check_restricted() || check_secure())
5999 return;
6000
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006001 if (p_pyx == 0)
6002 p_pyx = 2;
6003
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006004 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006005 do_pyeval(str, rettv);
6006}
6007#endif
6008
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006009#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6010/*
6011 * "pyxeval()" function
6012 */
6013 static void
6014f_pyxeval(typval_T *argvars, typval_T *rettv)
6015{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006016 if (check_restricted() || check_secure())
6017 return;
6018
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006019# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6020 init_pyxversion();
6021 if (p_pyx == 2)
6022 f_pyeval(argvars, rettv);
6023 else
6024 f_py3eval(argvars, rettv);
6025# elif defined(FEAT_PYTHON)
6026 f_pyeval(argvars, rettv);
6027# elif defined(FEAT_PYTHON3)
6028 f_py3eval(argvars, rettv);
6029# endif
6030}
6031#endif
6032
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006033static UINT32_T srand_seed_for_testing = 0;
6034static int srand_seed_for_testing_is_used = FALSE;
6035
6036 static void
6037f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6038{
6039 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006040 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006041 else
6042 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006043 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6044 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006045 }
6046}
6047
6048 static void
6049init_srand(UINT32_T *x)
6050{
6051#ifndef MSWIN
6052 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6053#endif
6054
6055 if (srand_seed_for_testing_is_used)
6056 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006057 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006058 return;
6059 }
6060#ifndef MSWIN
6061 if (dev_urandom_state != FAIL)
6062 {
6063 int fd = open("/dev/urandom", O_RDONLY);
6064 struct {
6065 union {
6066 UINT32_T number;
6067 char bytes[sizeof(UINT32_T)];
6068 } contents;
6069 } buf;
6070
6071 // Attempt reading /dev/urandom.
6072 if (fd == -1)
6073 dev_urandom_state = FAIL;
6074 else
6075 {
6076 buf.contents.number = 0;
6077 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6078 != sizeof(UINT32_T))
6079 dev_urandom_state = FAIL;
6080 else
6081 {
6082 dev_urandom_state = OK;
6083 *x = buf.contents.number;
6084 }
6085 close(fd);
6086 }
6087 }
6088 if (dev_urandom_state != OK)
6089 // Reading /dev/urandom doesn't work, fall back to time().
6090#endif
6091 *x = vim_time();
6092}
6093
6094#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6095#define SPLITMIX32(x, z) ( \
6096 z = (x += 0x9e3779b9), \
6097 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6098 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6099 z ^ (z >> 16) \
6100 )
6101#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6102 result = ROTL(y * 5, 7) * 9; \
6103 t = y << 9; \
6104 z ^= x; \
6105 w ^= y; \
6106 y ^= z, x ^= w; \
6107 z ^= t; \
6108 w = ROTL(w, 11);
6109
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006110/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006111 * "rand()" function
6112 */
6113 static void
6114f_rand(typval_T *argvars, typval_T *rettv)
6115{
6116 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006117 static UINT32_T gx, gy, gz, gw;
6118 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006119 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006120 UINT32_T x, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006121
6122 if (argvars[0].v_type == VAR_UNKNOWN)
6123 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006124 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006125 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006126 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006127 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006128 init_srand(&x);
6129
6130 gx = SPLITMIX32(x, z);
6131 gy = SPLITMIX32(x, z);
6132 gz = SPLITMIX32(x, z);
6133 gw = SPLITMIX32(x, z);
6134 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006135 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006136
6137 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006138 }
6139 else if (argvars[0].v_type == VAR_LIST)
6140 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006141 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006142 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006143 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006144
6145 lx = list_find(l, 0L);
6146 ly = list_find(l, 1L);
6147 lz = list_find(l, 2L);
6148 lw = list_find(l, 3L);
6149 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6150 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6151 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6152 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6153 x = (UINT32_T)lx->li_tv.vval.v_number;
6154 y = (UINT32_T)ly->li_tv.vval.v_number;
6155 z = (UINT32_T)lz->li_tv.vval.v_number;
6156 w = (UINT32_T)lw->li_tv.vval.v_number;
6157
6158 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6159
6160 lx->li_tv.vval.v_number = (varnumber_T)x;
6161 ly->li_tv.vval.v_number = (varnumber_T)y;
6162 lz->li_tv.vval.v_number = (varnumber_T)z;
6163 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006164 }
6165 else
6166 goto theend;
6167
6168 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006169 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006170 return;
6171
6172theend:
6173 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006174 rettv->v_type = VAR_NUMBER;
6175 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006176}
6177
6178/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006179 * "srand()" function
6180 */
6181 static void
6182f_srand(typval_T *argvars, typval_T *rettv)
6183{
6184 UINT32_T x = 0, z;
6185
6186 if (rettv_list_alloc(rettv) == FAIL)
6187 return;
6188 if (argvars[0].v_type == VAR_UNKNOWN)
6189 {
6190 init_srand(&x);
6191 }
6192 else
6193 {
6194 int error = FALSE;
6195
6196 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6197 if (error)
6198 return;
6199 }
6200
6201 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6202 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6203 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6204 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6205}
6206
6207#undef ROTL
6208#undef SPLITMIX32
6209#undef SHUFFLE_XOSHIRO128STARSTAR
6210
6211/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006212 * "range()" function
6213 */
6214 static void
6215f_range(typval_T *argvars, typval_T *rettv)
6216{
6217 varnumber_T start;
6218 varnumber_T end;
6219 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006220 int error = FALSE;
6221
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006222 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006223 if (argvars[1].v_type == VAR_UNKNOWN)
6224 {
6225 end = start - 1;
6226 start = 0;
6227 }
6228 else
6229 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006230 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006231 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006232 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006233 }
6234
6235 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006236 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006237 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006238 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006239 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006240 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006241 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006242 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006243 list_T *list = rettv->vval.v_list;
6244
6245 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006246 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006247 // be called.
6248 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006249 list->lv_u.nonmat.lv_start = start;
6250 list->lv_u.nonmat.lv_end = end;
6251 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006252 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006253 }
6254}
6255
6256/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006257 * Materialize "list".
6258 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259 */
6260 void
6261range_list_materialize(list_T *list)
6262{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006263 varnumber_T start = list->lv_u.nonmat.lv_start;
6264 varnumber_T end = list->lv_u.nonmat.lv_end;
6265 int stride = list->lv_u.nonmat.lv_stride;
6266 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006268 list->lv_first = NULL;
6269 list->lv_u.mat.lv_last = NULL;
6270 list->lv_len = 0;
6271 list->lv_u.mat.lv_idx_item = NULL;
6272 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
6273 if (list_append_number(list, (varnumber_T)i) == FAIL)
6274 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006275}
6276
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006277/*
6278 * "getreginfo()" function
6279 */
6280 static void
6281f_getreginfo(typval_T *argvars, typval_T *rettv)
6282{
6283 char_u *strregname;
6284 int regname;
6285 char_u buf[NUMBUFLEN + 2];
6286 long reglen = 0;
6287 dict_T *dict;
6288 list_T *list;
6289
6290 if (argvars[0].v_type != VAR_UNKNOWN)
6291 {
6292 strregname = tv_get_string_chk(&argvars[0]);
6293 if (strregname == NULL)
6294 return;
6295 }
6296 else
6297 strregname = get_vim_var_str(VV_REG);
6298
6299 regname = (strregname == NULL ? '"' : *strregname);
6300 if (regname == 0 || regname == '@')
6301 regname = '"';
6302
6303 if (rettv_dict_alloc(rettv) == FAIL)
6304 return;
6305 dict = rettv->vval.v_dict;
6306
6307 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
6308 if (list == NULL)
6309 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02006310 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006311
6312 buf[0] = NUL;
6313 buf[1] = NUL;
6314 switch (get_reg_type(regname, &reglen))
6315 {
6316 case MLINE: buf[0] = 'V'; break;
6317 case MCHAR: buf[0] = 'v'; break;
6318 case MBLOCK:
6319 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
6320 reglen + 1);
6321 break;
6322 }
Bram Moolenaar91639192020-06-29 19:55:58 +02006323 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006324
6325 buf[0] = get_register_name(get_unname_register());
6326 buf[1] = NUL;
6327 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02006328 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006329 else
6330 {
6331 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
6332
6333 if (item != NULL)
6334 {
6335 item->di_tv.v_type = VAR_SPECIAL;
6336 item->di_tv.vval.v_number = regname == buf[0]
6337 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02006338 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006339 }
6340 }
6341}
6342
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02006343 static void
6344return_register(int regname, typval_T *rettv)
6345{
6346 char_u buf[2] = {0, 0};
6347
6348 buf[0] = (char_u)regname;
6349 rettv->v_type = VAR_STRING;
6350 rettv->vval.v_string = vim_strsave(buf);
6351}
6352
6353/*
6354 * "reg_executing()" function
6355 */
6356 static void
6357f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
6358{
6359 return_register(reg_executing, rettv);
6360}
6361
6362/*
6363 * "reg_recording()" function
6364 */
6365 static void
6366f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
6367{
6368 return_register(reg_recording, rettv);
6369}
6370
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01006371/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006372 * "rename({from}, {to})" function
6373 */
6374 static void
6375f_rename(typval_T *argvars, typval_T *rettv)
6376{
6377 char_u buf[NUMBUFLEN];
6378
6379 if (check_restricted() || check_secure())
6380 rettv->vval.v_number = -1;
6381 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006382 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
6383 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006384}
6385
6386/*
6387 * "repeat()" function
6388 */
6389 static void
6390f_repeat(typval_T *argvars, typval_T *rettv)
6391{
6392 char_u *p;
6393 int n;
6394 int slen;
6395 int len;
6396 char_u *r;
6397 int i;
6398
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006399 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006400 if (argvars[0].v_type == VAR_LIST)
6401 {
6402 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
6403 while (n-- > 0)
6404 if (list_extend(rettv->vval.v_list,
6405 argvars[0].vval.v_list, NULL) == FAIL)
6406 break;
6407 }
6408 else
6409 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006410 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006411 rettv->v_type = VAR_STRING;
6412 rettv->vval.v_string = NULL;
6413
6414 slen = (int)STRLEN(p);
6415 len = slen * n;
6416 if (len <= 0)
6417 return;
6418
6419 r = alloc(len + 1);
6420 if (r != NULL)
6421 {
6422 for (i = 0; i < n; i++)
6423 mch_memmove(r + i * slen, p, (size_t)slen);
6424 r[len] = NUL;
6425 }
6426
6427 rettv->vval.v_string = r;
6428 }
6429}
6430
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006431#define SP_NOMOVE 0x01 // don't move cursor
6432#define SP_REPEAT 0x02 // repeat to find outer pair
6433#define SP_RETCOUNT 0x04 // return matchcount
6434#define SP_SETPCMARK 0x08 // set previous context mark
6435#define SP_START 0x10 // accept match at start position
6436#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
6437#define SP_END 0x40 // leave cursor at end of match
6438#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006439
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006440/*
6441 * Get flags for a search function.
6442 * Possibly sets "p_ws".
6443 * Returns BACKWARD, FORWARD or zero (for an error).
6444 */
6445 static int
6446get_search_arg(typval_T *varp, int *flagsp)
6447{
6448 int dir = FORWARD;
6449 char_u *flags;
6450 char_u nbuf[NUMBUFLEN];
6451 int mask;
6452
6453 if (varp->v_type != VAR_UNKNOWN)
6454 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006455 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006456 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006457 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006458 while (*flags != NUL)
6459 {
6460 switch (*flags)
6461 {
6462 case 'b': dir = BACKWARD; break;
6463 case 'w': p_ws = TRUE; break;
6464 case 'W': p_ws = FALSE; break;
6465 default: mask = 0;
6466 if (flagsp != NULL)
6467 switch (*flags)
6468 {
6469 case 'c': mask = SP_START; break;
6470 case 'e': mask = SP_END; break;
6471 case 'm': mask = SP_RETCOUNT; break;
6472 case 'n': mask = SP_NOMOVE; break;
6473 case 'p': mask = SP_SUBPAT; break;
6474 case 'r': mask = SP_REPEAT; break;
6475 case 's': mask = SP_SETPCMARK; break;
6476 case 'z': mask = SP_COLUMN; break;
6477 }
6478 if (mask == 0)
6479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006480 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006481 dir = 0;
6482 }
6483 else
6484 *flagsp |= mask;
6485 }
6486 if (dir == 0)
6487 break;
6488 ++flags;
6489 }
6490 }
6491 return dir;
6492}
6493
6494/*
6495 * Shared by search() and searchpos() functions.
6496 */
6497 static int
6498search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
6499{
6500 int flags;
6501 char_u *pat;
6502 pos_T pos;
6503 pos_T save_cursor;
6504 int save_p_ws = p_ws;
6505 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006506 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006507 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006508#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006509 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006510 long time_limit = 0;
6511#endif
6512 int options = SEARCH_KEEP;
6513 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006514 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006515 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006516 pos_T firstpos;
6517
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006518 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006519 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006520 if (dir == 0)
6521 goto theend;
6522 flags = *flagsp;
6523 if (flags & SP_START)
6524 options |= SEARCH_START;
6525 if (flags & SP_END)
6526 options |= SEARCH_END;
6527 if (flags & SP_COLUMN)
6528 options |= SEARCH_COL;
6529
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006530 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006531 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
6532 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006533 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006534 if (lnum_stop < 0)
6535 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006536 if (argvars[3].v_type != VAR_UNKNOWN)
6537 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006538#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006539 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006540 if (time_limit < 0)
6541 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006542#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02006543 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006544 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006545 }
6546
6547#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006548 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006549 profile_setlimit(time_limit, &tm);
6550#endif
6551
6552 /*
6553 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
6554 * Check to make sure only those flags are set.
6555 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
6556 * flags cannot be set. Check for that condition also.
6557 */
6558 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
6559 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6560 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006561 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006562 goto theend;
6563 }
6564
6565 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006566 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02006567 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006568 sia.sa_stop_lnum = (linenr_T)lnum_stop;
6569#ifdef FEAT_RELTIME
6570 sia.sa_tm = &tm;
6571#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006572
6573 // Repeat until {skip} returns FALSE.
6574 for (;;)
6575 {
6576 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006577 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006578 // finding the first match again means there is no match where {skip}
6579 // evaluates to zero.
6580 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
6581 subpatnum = FAIL;
6582
Bram Moolenaara9c01042020-06-07 14:50:50 +02006583 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006584 // didn't find it or no skip argument
6585 break;
6586 firstpos = pos;
6587
Bram Moolenaara9c01042020-06-07 14:50:50 +02006588 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006589 {
6590 int do_skip;
6591 int err;
6592 pos_T save_pos = curwin->w_cursor;
6593
6594 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006595 err = FALSE;
6596 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006597 curwin->w_cursor = save_pos;
6598 if (err)
6599 {
6600 // Evaluating {skip} caused an error, break here.
6601 subpatnum = FAIL;
6602 break;
6603 }
6604 if (!do_skip)
6605 break;
6606 }
6607 }
6608
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006609 if (subpatnum != FAIL)
6610 {
6611 if (flags & SP_SUBPAT)
6612 retval = subpatnum;
6613 else
6614 retval = pos.lnum;
6615 if (flags & SP_SETPCMARK)
6616 setpcmark();
6617 curwin->w_cursor = pos;
6618 if (match_pos != NULL)
6619 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006620 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006621 match_pos->lnum = pos.lnum;
6622 match_pos->col = pos.col + 1;
6623 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006624 // "/$" will put the cursor after the end of the line, may need to
6625 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006626 check_cursor();
6627 }
6628
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006629 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006630 if (flags & SP_NOMOVE)
6631 curwin->w_cursor = save_cursor;
6632 else
6633 curwin->w_set_curswant = TRUE;
6634theend:
6635 p_ws = save_p_ws;
6636
6637 return retval;
6638}
6639
6640#ifdef FEAT_FLOAT
6641
6642/*
6643 * round() is not in C90, use ceil() or floor() instead.
6644 */
6645 float_T
6646vim_round(float_T f)
6647{
6648 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
6649}
6650
6651/*
6652 * "round({float})" function
6653 */
6654 static void
6655f_round(typval_T *argvars, typval_T *rettv)
6656{
6657 float_T f = 0.0;
6658
6659 rettv->v_type = VAR_FLOAT;
6660 if (get_float_arg(argvars, &f) == OK)
6661 rettv->vval.v_float = vim_round(f);
6662 else
6663 rettv->vval.v_float = 0.0;
6664}
6665#endif
6666
Bram Moolenaare99be0e2019-03-26 22:51:09 +01006667#ifdef FEAT_RUBY
6668/*
6669 * "rubyeval()" function
6670 */
6671 static void
6672f_rubyeval(typval_T *argvars, typval_T *rettv)
6673{
6674 char_u *str;
6675 char_u buf[NUMBUFLEN];
6676
6677 str = tv_get_string_buf(&argvars[0], buf);
6678 do_rubyeval(str, rettv);
6679}
6680#endif
6681
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006682/*
6683 * "screenattr()" function
6684 */
6685 static void
6686f_screenattr(typval_T *argvars, typval_T *rettv)
6687{
6688 int row;
6689 int col;
6690 int c;
6691
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006692 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6693 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694 if (row < 0 || row >= screen_Rows
6695 || col < 0 || col >= screen_Columns)
6696 c = -1;
6697 else
6698 c = ScreenAttrs[LineOffset[row] + col];
6699 rettv->vval.v_number = c;
6700}
6701
6702/*
6703 * "screenchar()" function
6704 */
6705 static void
6706f_screenchar(typval_T *argvars, typval_T *rettv)
6707{
6708 int row;
6709 int col;
6710 int off;
6711 int c;
6712
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006713 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6714 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006715 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006716 c = -1;
6717 else
6718 {
6719 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006720 if (enc_utf8 && ScreenLinesUC[off] != 0)
6721 c = ScreenLinesUC[off];
6722 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006723 c = ScreenLines[off];
6724 }
6725 rettv->vval.v_number = c;
6726}
6727
6728/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006729 * "screenchars()" function
6730 */
6731 static void
6732f_screenchars(typval_T *argvars, typval_T *rettv)
6733{
6734 int row;
6735 int col;
6736 int off;
6737 int c;
6738 int i;
6739
6740 if (rettv_list_alloc(rettv) == FAIL)
6741 return;
6742 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6743 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6744 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6745 return;
6746
6747 off = LineOffset[row] + col;
6748 if (enc_utf8 && ScreenLinesUC[off] != 0)
6749 c = ScreenLinesUC[off];
6750 else
6751 c = ScreenLines[off];
6752 list_append_number(rettv->vval.v_list, (varnumber_T)c);
6753
6754 if (enc_utf8)
6755
6756 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6757 list_append_number(rettv->vval.v_list,
6758 (varnumber_T)ScreenLinesC[i][off]);
6759}
6760
6761/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006762 * "screencol()" function
6763 *
6764 * First column is 1 to be consistent with virtcol().
6765 */
6766 static void
6767f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
6768{
6769 rettv->vval.v_number = screen_screencol() + 1;
6770}
6771
6772/*
6773 * "screenrow()" function
6774 */
6775 static void
6776f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
6777{
6778 rettv->vval.v_number = screen_screenrow() + 1;
6779}
6780
6781/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006782 * "screenstring()" function
6783 */
6784 static void
6785f_screenstring(typval_T *argvars, typval_T *rettv)
6786{
6787 int row;
6788 int col;
6789 int off;
6790 int c;
6791 int i;
6792 char_u buf[MB_MAXBYTES + 1];
6793 int buflen = 0;
6794
6795 rettv->vval.v_string = NULL;
6796 rettv->v_type = VAR_STRING;
6797
6798 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6799 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6800 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6801 return;
6802
6803 off = LineOffset[row] + col;
6804 if (enc_utf8 && ScreenLinesUC[off] != 0)
6805 c = ScreenLinesUC[off];
6806 else
6807 c = ScreenLines[off];
6808 buflen += mb_char2bytes(c, buf);
6809
6810 if (enc_utf8)
6811 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6812 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
6813
6814 buf[buflen] = NUL;
6815 rettv->vval.v_string = vim_strsave(buf);
6816}
6817
6818/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006819 * "search()" function
6820 */
6821 static void
6822f_search(typval_T *argvars, typval_T *rettv)
6823{
6824 int flags = 0;
6825
6826 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
6827}
6828
6829/*
6830 * "searchdecl()" function
6831 */
6832 static void
6833f_searchdecl(typval_T *argvars, typval_T *rettv)
6834{
6835 int locally = 1;
6836 int thisblock = 0;
6837 int error = FALSE;
6838 char_u *name;
6839
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006840 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006841
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006842 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006843 if (argvars[1].v_type != VAR_UNKNOWN)
6844 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006845 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006846 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006847 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006848 }
6849 if (!error && name != NULL)
6850 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
6851 locally, thisblock, SEARCH_KEEP) == FAIL;
6852}
6853
6854/*
6855 * Used by searchpair() and searchpairpos()
6856 */
6857 static int
6858searchpair_cmn(typval_T *argvars, pos_T *match_pos)
6859{
6860 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01006861 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006862 int save_p_ws = p_ws;
6863 int dir;
6864 int flags = 0;
6865 char_u nbuf1[NUMBUFLEN];
6866 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006867 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006868 long lnum_stop = 0;
6869 long time_limit = 0;
6870
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006871 // Get the three pattern arguments: start, middle, end. Will result in an
6872 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006873 spat = tv_get_string_chk(&argvars[0]);
6874 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
6875 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006876 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006877 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006878
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006879 // Handle the optional fourth argument: flags
6880 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006881 if (dir == 0)
6882 goto theend;
6883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006884 // Don't accept SP_END or SP_SUBPAT.
6885 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006886 if ((flags & (SP_END | SP_SUBPAT)) != 0
6887 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6888 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006889 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006890 goto theend;
6891 }
6892
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006893 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006894 if (flags & SP_REPEAT)
6895 p_ws = FALSE;
6896
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006897 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006898 if (argvars[3].v_type == VAR_UNKNOWN
6899 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01006900 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901 else
6902 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02006903 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01006904 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02006905
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006906 if (argvars[5].v_type != VAR_UNKNOWN)
6907 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006908 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006909 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006910 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006911 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006912 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006913 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006914#ifdef FEAT_RELTIME
6915 if (argvars[6].v_type != VAR_UNKNOWN)
6916 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006917 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006918 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006919 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006920 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006921 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006922 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923 }
6924#endif
6925 }
6926 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006927
6928 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
6929 match_pos, lnum_stop, time_limit);
6930
6931theend:
6932 p_ws = save_p_ws;
6933
6934 return retval;
6935}
6936
6937/*
6938 * "searchpair()" function
6939 */
6940 static void
6941f_searchpair(typval_T *argvars, typval_T *rettv)
6942{
6943 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
6944}
6945
6946/*
6947 * "searchpairpos()" function
6948 */
6949 static void
6950f_searchpairpos(typval_T *argvars, typval_T *rettv)
6951{
6952 pos_T match_pos;
6953 int lnum = 0;
6954 int col = 0;
6955
6956 if (rettv_list_alloc(rettv) == FAIL)
6957 return;
6958
6959 if (searchpair_cmn(argvars, &match_pos) > 0)
6960 {
6961 lnum = match_pos.lnum;
6962 col = match_pos.col;
6963 }
6964
6965 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
6966 list_append_number(rettv->vval.v_list, (varnumber_T)col);
6967}
6968
6969/*
6970 * Search for a start/middle/end thing.
6971 * Used by searchpair(), see its documentation for the details.
6972 * Returns 0 or -1 for no match,
6973 */
6974 long
6975do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006976 char_u *spat, // start pattern
6977 char_u *mpat, // middle pattern
6978 char_u *epat, // end pattern
6979 int dir, // BACKWARD or FORWARD
6980 typval_T *skip, // skip expression
6981 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006982 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006983 linenr_T lnum_stop, // stop at this line if not zero
6984 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006985{
6986 char_u *save_cpo;
6987 char_u *pat, *pat2 = NULL, *pat3 = NULL;
6988 long retval = 0;
6989 pos_T pos;
6990 pos_T firstpos;
6991 pos_T foundpos;
6992 pos_T save_cursor;
6993 pos_T save_pos;
6994 int n;
6995 int r;
6996 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01006997 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006998 int err;
6999 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007000#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007001 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007002#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007004 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007005 save_cpo = p_cpo;
7006 p_cpo = empty_option;
7007
7008#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007009 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007010 profile_setlimit(time_limit, &tm);
7011#endif
7012
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007013 // Make two search patterns: start/end (pat2, for in nested pairs) and
7014 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007015 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7016 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007017 if (pat2 == NULL || pat3 == NULL)
7018 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007019 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007020 if (*mpat == NUL)
7021 STRCPY(pat3, pat2);
7022 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007023 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007024 spat, epat, mpat);
7025 if (flags & SP_START)
7026 options |= SEARCH_START;
7027
Bram Moolenaar48570482017-10-30 21:48:41 +01007028 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007029 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007030
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007031 save_cursor = curwin->w_cursor;
7032 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007033 CLEAR_POS(&firstpos);
7034 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007035 pat = pat3;
7036 for (;;)
7037 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007038 searchit_arg_T sia;
7039
Bram Moolenaara80faa82020-04-12 19:37:17 +02007040 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007041 sia.sa_stop_lnum = lnum_stop;
7042#ifdef FEAT_RELTIME
7043 sia.sa_tm = &tm;
7044#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007045 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007046 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007047 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007048 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007049 break;
7050
7051 if (firstpos.lnum == 0)
7052 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007053 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007054 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007055 // Found the same position again. Can happen with a pattern that
7056 // has "\zs" at the end and searching backwards. Advance one
7057 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007058 if (dir == BACKWARD)
7059 decl(&pos);
7060 else
7061 incl(&pos);
7062 }
7063 foundpos = pos;
7064
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007065 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007066 options &= ~SEARCH_START;
7067
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007068 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007069 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007070 {
7071 save_pos = curwin->w_cursor;
7072 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007073 err = FALSE;
7074 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007075 curwin->w_cursor = save_pos;
7076 if (err)
7077 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007078 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007079 curwin->w_cursor = save_cursor;
7080 retval = -1;
7081 break;
7082 }
7083 if (r)
7084 continue;
7085 }
7086
7087 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7088 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007089 // Found end when searching backwards or start when searching
7090 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007091 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007092 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007093 }
7094 else
7095 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007096 // Found end when searching forward or start when searching
7097 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007098 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007099 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007100 }
7101
7102 if (nest == 0)
7103 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007104 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007105 if (flags & SP_RETCOUNT)
7106 ++retval;
7107 else
7108 retval = pos.lnum;
7109 if (flags & SP_SETPCMARK)
7110 setpcmark();
7111 curwin->w_cursor = pos;
7112 if (!(flags & SP_REPEAT))
7113 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007114 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007115 }
7116 }
7117
7118 if (match_pos != NULL)
7119 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007120 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007121 match_pos->lnum = curwin->w_cursor.lnum;
7122 match_pos->col = curwin->w_cursor.col + 1;
7123 }
7124
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007125 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007126 if ((flags & SP_NOMOVE) || retval == 0)
7127 curwin->w_cursor = save_cursor;
7128
7129theend:
7130 vim_free(pat2);
7131 vim_free(pat3);
7132 if (p_cpo == empty_option)
7133 p_cpo = save_cpo;
7134 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007135 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007136 free_string_option(save_cpo);
7137
7138 return retval;
7139}
7140
7141/*
7142 * "searchpos()" function
7143 */
7144 static void
7145f_searchpos(typval_T *argvars, typval_T *rettv)
7146{
7147 pos_T match_pos;
7148 int lnum = 0;
7149 int col = 0;
7150 int n;
7151 int flags = 0;
7152
7153 if (rettv_list_alloc(rettv) == FAIL)
7154 return;
7155
7156 n = search_cmn(argvars, &match_pos, &flags);
7157 if (n > 0)
7158 {
7159 lnum = match_pos.lnum;
7160 col = match_pos.col;
7161 }
7162
7163 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7164 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7165 if (flags & SP_SUBPAT)
7166 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7167}
7168
7169 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007170f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7171{
7172 dict_T *d;
7173 dictitem_T *di;
7174 char_u *csearch;
7175
7176 if (argvars[0].v_type != VAR_DICT)
7177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007178 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007179 return;
7180 }
7181
7182 if ((d = argvars[0].vval.v_dict) != NULL)
7183 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01007184 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007185 if (csearch != NULL)
7186 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007187 if (enc_utf8)
7188 {
7189 int pcc[MAX_MCO];
7190 int c = utfc_ptr2char(csearch, pcc);
7191
7192 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
7193 }
7194 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007195 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02007196 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007197 }
7198
7199 di = dict_find(d, (char_u *)"forward", -1);
7200 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007201 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007202 ? FORWARD : BACKWARD);
7203
7204 di = dict_find(d, (char_u *)"until", -1);
7205 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007206 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007207 }
7208}
7209
7210/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02007211 * "setenv()" function
7212 */
7213 static void
7214f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
7215{
7216 char_u namebuf[NUMBUFLEN];
7217 char_u valbuf[NUMBUFLEN];
7218 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
7219
7220 if (argvars[1].v_type == VAR_SPECIAL
7221 && argvars[1].vval.v_number == VVAL_NULL)
7222 vim_unsetenv(name);
7223 else
7224 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
7225}
7226
7227/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007228 * "setfperm({fname}, {mode})" function
7229 */
7230 static void
7231f_setfperm(typval_T *argvars, typval_T *rettv)
7232{
7233 char_u *fname;
7234 char_u modebuf[NUMBUFLEN];
7235 char_u *mode_str;
7236 int i;
7237 int mask;
7238 int mode = 0;
7239
7240 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007241 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007242 if (fname == NULL)
7243 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007244 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245 if (mode_str == NULL)
7246 return;
7247 if (STRLEN(mode_str) != 9)
7248 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007249 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007250 return;
7251 }
7252
7253 mask = 1;
7254 for (i = 8; i >= 0; --i)
7255 {
7256 if (mode_str[i] != '-')
7257 mode |= mask;
7258 mask = mask << 1;
7259 }
7260 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
7261}
7262
7263/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007264 * "setpos()" function
7265 */
7266 static void
7267f_setpos(typval_T *argvars, typval_T *rettv)
7268{
7269 pos_T pos;
7270 int fnum;
7271 char_u *name;
7272 colnr_T curswant = -1;
7273
7274 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007275 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007276 if (name != NULL)
7277 {
7278 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
7279 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01007280 if (pos.col != MAXCOL && --pos.col < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007281 pos.col = 0;
7282 if (name[0] == '.' && name[1] == NUL)
7283 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007284 // set cursor; "fnum" is ignored
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007285 curwin->w_cursor = pos;
7286 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007287 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007288 curwin->w_curswant = curswant - 1;
7289 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007290 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007291 check_cursor();
7292 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007293 }
7294 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7295 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007296 // set mark
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007297 if (setmark_pos(name[1], &pos, fnum) == OK)
7298 rettv->vval.v_number = 0;
7299 }
7300 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007301 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007302 }
7303 }
7304}
7305
7306/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007307 * Translate a register type string to the yank type and block length
7308 */
7309 static int
7310get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
7311{
7312 char_u *stropt = *pp;
7313 switch (*stropt)
7314 {
7315 case 'v': case 'c': // character-wise selection
7316 *yank_type = MCHAR;
7317 break;
7318 case 'V': case 'l': // line-wise selection
7319 *yank_type = MLINE;
7320 break;
7321 case 'b': case Ctrl_V: // block-wise selection
7322 *yank_type = MBLOCK;
7323 if (VIM_ISDIGIT(stropt[1]))
7324 {
7325 ++stropt;
7326 *block_len = getdigits(&stropt) - 1;
7327 --stropt;
7328 }
7329 break;
7330 default:
7331 return FAIL;
7332 }
7333 *pp = stropt;
7334 return OK;
7335}
7336
7337/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007338 * "setreg()" function
7339 */
7340 static void
7341f_setreg(typval_T *argvars, typval_T *rettv)
7342{
7343 int regname;
7344 char_u *strregname;
7345 char_u *stropt;
7346 char_u *strval;
7347 int append;
7348 char_u yank_type;
7349 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007350 typval_T *regcontents;
7351 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007352
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007353 pointreg = 0;
7354 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007355 block_len = -1;
7356 yank_type = MAUTO;
7357 append = FALSE;
7358
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007359 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007360 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007361
7362 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007363 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007364 regname = *strregname;
7365 if (regname == 0 || regname == '@')
7366 regname = '"';
7367
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007368 if (argvars[1].v_type == VAR_DICT)
7369 {
7370 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007371 dictitem_T *di;
7372
7373 if (d == NULL || d->dv_hashtab.ht_used == 0)
7374 {
7375 // Empty dict, clear the register (like setreg(0, []))
7376 char_u *lstval[2] = {NULL, NULL};
7377 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
7378 return;
7379 }
7380
7381 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007382 if (di != NULL)
7383 regcontents = &di->di_tv;
7384
7385 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
7386 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007387 {
7388 int ret = get_yank_type(&stropt, &yank_type, &block_len);
7389
7390 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007391 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007392 semsg(_(e_invargval), "value");
7393 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007394 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007395 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007396
7397 if (regname == '"')
7398 {
7399 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
7400 if (stropt != NULL)
7401 {
7402 pointreg = *stropt;
7403 regname = pointreg;
7404 }
7405 }
7406 else if (dict_get_number(d, (char_u *)"isunnamed"))
7407 pointreg = regname;
7408 }
7409 else
7410 regcontents = &argvars[1];
7411
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007412 if (argvars[2].v_type != VAR_UNKNOWN)
7413 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007414 if (yank_type != MAUTO)
7415 {
7416 semsg(_(e_toomanyarg), "setreg");
7417 return;
7418 }
7419
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007420 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007421 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007422 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007423 for (; *stropt != NUL; ++stropt)
7424 switch (*stropt)
7425 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007426 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007427 append = TRUE;
7428 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007429 default:
7430 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007431 }
7432 }
7433
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007434 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007435 {
7436 char_u **lstval;
7437 char_u **allocval;
7438 char_u buf[NUMBUFLEN];
7439 char_u **curval;
7440 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007441 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007442 listitem_T *li;
7443 int len;
7444
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007445 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007446 len = ll == NULL ? 0 : ll->lv_len;
7447
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007448 // First half: use for pointers to result lines; second half: use for
7449 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007450 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007451 if (lstval == NULL)
7452 return;
7453 curval = lstval;
7454 allocval = lstval + len + 2;
7455 curallocval = allocval;
7456
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007457 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007458 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007459 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007460 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007461 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007462 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007463 if (strval == NULL)
7464 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007465 if (strval == buf)
7466 {
7467 // Need to make a copy, next tv_get_string_buf_chk() will
7468 // overwrite the string.
7469 strval = vim_strsave(buf);
7470 if (strval == NULL)
7471 goto free_lstval;
7472 *curallocval++ = strval;
7473 }
7474 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007475 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007476 }
7477 *curval++ = NULL;
7478
7479 write_reg_contents_lst(regname, lstval, -1,
7480 append, yank_type, block_len);
7481free_lstval:
7482 while (curallocval > allocval)
7483 vim_free(*--curallocval);
7484 vim_free(lstval);
7485 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007486 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007487 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007488 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007489 if (strval == NULL)
7490 return;
7491 write_reg_contents_ex(regname, strval, -1,
7492 append, yank_type, block_len);
7493 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007494 if (pointreg != 0)
7495 get_yank_register(pointreg, TRUE);
7496
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007497 rettv->vval.v_number = 0;
7498}
7499
7500/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007501 * "settagstack()" function
7502 */
7503 static void
7504f_settagstack(typval_T *argvars, typval_T *rettv)
7505{
7506 static char *e_invact2 = N_("E962: Invalid action: '%s'");
7507 win_T *wp;
7508 dict_T *d;
7509 int action = 'r';
7510
7511 rettv->vval.v_number = -1;
7512
7513 // first argument: window number or id
7514 wp = find_win_by_nr_or_id(&argvars[0]);
7515 if (wp == NULL)
7516 return;
7517
7518 // second argument: dict with items to set in the tag stack
7519 if (argvars[1].v_type != VAR_DICT)
7520 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007521 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007522 return;
7523 }
7524 d = argvars[1].vval.v_dict;
7525 if (d == NULL)
7526 return;
7527
7528 // third argument: action - 'a' for append and 'r' for replace.
7529 // default is to replace the stack.
7530 if (argvars[2].v_type == VAR_UNKNOWN)
7531 action = 'r';
7532 else if (argvars[2].v_type == VAR_STRING)
7533 {
7534 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007535 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007536 if (actstr == NULL)
7537 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01007538 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
7539 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007540 action = *actstr;
7541 else
7542 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007543 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007544 return;
7545 }
7546 }
7547 else
7548 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007549 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007550 return;
7551 }
7552
7553 if (set_tagstack(wp, d, action) == OK)
7554 rettv->vval.v_number = 0;
7555}
7556
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007557#ifdef FEAT_CRYPT
7558/*
7559 * "sha256({string})" function
7560 */
7561 static void
7562f_sha256(typval_T *argvars, typval_T *rettv)
7563{
7564 char_u *p;
7565
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007566 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007567 rettv->vval.v_string = vim_strsave(
7568 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
7569 rettv->v_type = VAR_STRING;
7570}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007571#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007572
7573/*
7574 * "shellescape({string})" function
7575 */
7576 static void
7577f_shellescape(typval_T *argvars, typval_T *rettv)
7578{
Bram Moolenaar20615522017-06-05 18:46:26 +02007579 int do_special = non_zero_arg(&argvars[1]);
7580
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007581 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007582 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007583 rettv->v_type = VAR_STRING;
7584}
7585
7586/*
7587 * shiftwidth() function
7588 */
7589 static void
7590f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
7591{
Bram Moolenaarf9514162018-11-22 03:08:29 +01007592 rettv->vval.v_number = 0;
7593
7594 if (argvars[0].v_type != VAR_UNKNOWN)
7595 {
7596 long col;
7597
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007598 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01007599 if (col < 0)
7600 return; // type error; errmsg already given
7601#ifdef FEAT_VARTABS
7602 rettv->vval.v_number = get_sw_value_col(curbuf, col);
7603 return;
7604#endif
7605 }
7606
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007607 rettv->vval.v_number = get_sw_value(curbuf);
7608}
7609
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007610#ifdef FEAT_FLOAT
7611/*
7612 * "sin()" function
7613 */
7614 static void
7615f_sin(typval_T *argvars, typval_T *rettv)
7616{
7617 float_T f = 0.0;
7618
7619 rettv->v_type = VAR_FLOAT;
7620 if (get_float_arg(argvars, &f) == OK)
7621 rettv->vval.v_float = sin(f);
7622 else
7623 rettv->vval.v_float = 0.0;
7624}
7625
7626/*
7627 * "sinh()" function
7628 */
7629 static void
7630f_sinh(typval_T *argvars, typval_T *rettv)
7631{
7632 float_T f = 0.0;
7633
7634 rettv->v_type = VAR_FLOAT;
7635 if (get_float_arg(argvars, &f) == OK)
7636 rettv->vval.v_float = sinh(f);
7637 else
7638 rettv->vval.v_float = 0.0;
7639}
7640#endif
7641
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007642/*
7643 * "soundfold({word})" function
7644 */
7645 static void
7646f_soundfold(typval_T *argvars, typval_T *rettv)
7647{
7648 char_u *s;
7649
7650 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007651 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007652#ifdef FEAT_SPELL
7653 rettv->vval.v_string = eval_soundfold(s);
7654#else
7655 rettv->vval.v_string = vim_strsave(s);
7656#endif
7657}
7658
7659/*
7660 * "spellbadword()" function
7661 */
7662 static void
7663f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
7664{
7665 char_u *word = (char_u *)"";
7666 hlf_T attr = HLF_COUNT;
7667 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007668#ifdef FEAT_SPELL
7669 int wo_spell_save = curwin->w_p_spell;
7670
7671 if (!curwin->w_p_spell)
7672 {
7673 did_set_spelllang(curwin);
7674 curwin->w_p_spell = TRUE;
7675 }
7676
7677 if (*curwin->w_s->b_p_spl == NUL)
7678 {
7679 emsg(_(e_no_spell));
7680 curwin->w_p_spell = wo_spell_save;
7681 return;
7682 }
7683#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007684
7685 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007686 {
7687#ifdef FEAT_SPELL
7688 curwin->w_p_spell = wo_spell_save;
7689#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007690 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007691 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007692
7693#ifdef FEAT_SPELL
7694 if (argvars[0].v_type == VAR_UNKNOWN)
7695 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007696 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007697 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
7698 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007699 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007700 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007701 curwin->w_set_curswant = TRUE;
7702 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007703 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007704 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007705 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007706 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007707 int capcol = -1;
7708
7709 if (str != NULL)
7710 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007711 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007712 while (*str != NUL)
7713 {
7714 len = spell_check(curwin, str, &attr, &capcol, FALSE);
7715 if (attr != HLF_COUNT)
7716 {
7717 word = str;
7718 break;
7719 }
7720 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02007721 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02007722 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007723 }
7724 }
7725 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007726 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007727#endif
7728
7729 list_append_string(rettv->vval.v_list, word, len);
7730 list_append_string(rettv->vval.v_list, (char_u *)(
7731 attr == HLF_SPB ? "bad" :
7732 attr == HLF_SPR ? "rare" :
7733 attr == HLF_SPL ? "local" :
7734 attr == HLF_SPC ? "caps" :
7735 ""), -1);
7736}
7737
7738/*
7739 * "spellsuggest()" function
7740 */
7741 static void
7742f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
7743{
7744#ifdef FEAT_SPELL
7745 char_u *str;
7746 int typeerr = FALSE;
7747 int maxcount;
7748 garray_T ga;
7749 int i;
7750 listitem_T *li;
7751 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007752 int wo_spell_save = curwin->w_p_spell;
7753
7754 if (!curwin->w_p_spell)
7755 {
7756 did_set_spelllang(curwin);
7757 curwin->w_p_spell = TRUE;
7758 }
7759
7760 if (*curwin->w_s->b_p_spl == NUL)
7761 {
7762 emsg(_(e_no_spell));
7763 curwin->w_p_spell = wo_spell_save;
7764 return;
7765 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007766#endif
7767
7768 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007769 {
7770#ifdef FEAT_SPELL
7771 curwin->w_p_spell = wo_spell_save;
7772#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007773 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007774 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007775
7776#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007777 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007778 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007779 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007780 if (argvars[1].v_type != VAR_UNKNOWN)
7781 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007782 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007783 if (maxcount <= 0)
7784 return;
7785 if (argvars[2].v_type != VAR_UNKNOWN)
7786 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007787 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007788 if (typeerr)
7789 return;
7790 }
7791 }
7792 else
7793 maxcount = 25;
7794
7795 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
7796
7797 for (i = 0; i < ga.ga_len; ++i)
7798 {
7799 str = ((char_u **)ga.ga_data)[i];
7800
7801 li = listitem_alloc();
7802 if (li == NULL)
7803 vim_free(str);
7804 else
7805 {
7806 li->li_tv.v_type = VAR_STRING;
7807 li->li_tv.v_lock = 0;
7808 li->li_tv.vval.v_string = str;
7809 list_append(rettv->vval.v_list, li);
7810 }
7811 }
7812 ga_clear(&ga);
7813 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007814 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007815#endif
7816}
7817
7818 static void
7819f_split(typval_T *argvars, typval_T *rettv)
7820{
7821 char_u *str;
7822 char_u *end;
7823 char_u *pat = NULL;
7824 regmatch_T regmatch;
7825 char_u patbuf[NUMBUFLEN];
7826 char_u *save_cpo;
7827 int match;
7828 colnr_T col = 0;
7829 int keepempty = FALSE;
7830 int typeerr = FALSE;
7831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007832 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007833 save_cpo = p_cpo;
7834 p_cpo = (char_u *)"";
7835
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007836 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007837 if (argvars[1].v_type != VAR_UNKNOWN)
7838 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007839 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007840 if (pat == NULL)
7841 typeerr = TRUE;
7842 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007843 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007844 }
7845 if (pat == NULL || *pat == NUL)
7846 pat = (char_u *)"[\\x01- ]\\+";
7847
7848 if (rettv_list_alloc(rettv) == FAIL)
7849 return;
7850 if (typeerr)
7851 return;
7852
7853 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7854 if (regmatch.regprog != NULL)
7855 {
7856 regmatch.rm_ic = FALSE;
7857 while (*str != NUL || keepempty)
7858 {
7859 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007860 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007861 else
7862 match = vim_regexec_nl(&regmatch, str, col);
7863 if (match)
7864 end = regmatch.startp[0];
7865 else
7866 end = str + STRLEN(str);
7867 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
7868 && *str != NUL && match && end < regmatch.endp[0]))
7869 {
7870 if (list_append_string(rettv->vval.v_list, str,
7871 (int)(end - str)) == FAIL)
7872 break;
7873 }
7874 if (!match)
7875 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01007876 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007877 if (regmatch.endp[0] > str)
7878 col = 0;
7879 else
Bram Moolenaar13505972019-01-24 15:04:48 +01007880 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007881 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007882 str = regmatch.endp[0];
7883 }
7884
7885 vim_regfree(regmatch.regprog);
7886 }
7887
7888 p_cpo = save_cpo;
7889}
7890
7891#ifdef FEAT_FLOAT
7892/*
7893 * "sqrt()" function
7894 */
7895 static void
7896f_sqrt(typval_T *argvars, typval_T *rettv)
7897{
7898 float_T f = 0.0;
7899
7900 rettv->v_type = VAR_FLOAT;
7901 if (get_float_arg(argvars, &f) == OK)
7902 rettv->vval.v_float = sqrt(f);
7903 else
7904 rettv->vval.v_float = 0.0;
7905}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007906#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007907
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007908#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007909/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007910 * "str2float()" function
7911 */
7912 static void
7913f_str2float(typval_T *argvars, typval_T *rettv)
7914{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007915 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01007916 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007917
Bram Moolenaar08243d22017-01-10 16:12:29 +01007918 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007919 p = skipwhite(p + 1);
7920 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01007921 if (isneg)
7922 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007923 rettv->v_type = VAR_FLOAT;
7924}
7925#endif
7926
7927/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007928 * "str2list()" function
7929 */
7930 static void
7931f_str2list(typval_T *argvars, typval_T *rettv)
7932{
7933 char_u *p;
7934 int utf8 = FALSE;
7935
7936 if (rettv_list_alloc(rettv) == FAIL)
7937 return;
7938
7939 if (argvars[1].v_type != VAR_UNKNOWN)
7940 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7941
7942 p = tv_get_string(&argvars[0]);
7943
7944 if (has_mbyte || utf8)
7945 {
7946 int (*ptr2len)(char_u *);
7947 int (*ptr2char)(char_u *);
7948
7949 if (utf8 || enc_utf8)
7950 {
7951 ptr2len = utf_ptr2len;
7952 ptr2char = utf_ptr2char;
7953 }
7954 else
7955 {
7956 ptr2len = mb_ptr2len;
7957 ptr2char = mb_ptr2char;
7958 }
7959
7960 for ( ; *p != NUL; p += (*ptr2len)(p))
7961 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
7962 }
7963 else
7964 for ( ; *p != NUL; ++p)
7965 list_append_number(rettv->vval.v_list, *p);
7966}
7967
7968/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007969 * "str2nr()" function
7970 */
7971 static void
7972f_str2nr(typval_T *argvars, typval_T *rettv)
7973{
7974 int base = 10;
7975 char_u *p;
7976 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02007977 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01007978 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007979
7980 if (argvars[1].v_type != VAR_UNKNOWN)
7981 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007982 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007983 if (base != 2 && base != 8 && base != 10 && base != 16)
7984 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007985 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007986 return;
7987 }
Bram Moolenaar60a8de22019-09-15 14:33:22 +02007988 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_number(&argvars[2]))
7989 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007990 }
7991
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007992 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01007993 isneg = (*p == '-');
7994 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007995 p = skipwhite(p + 1);
7996 switch (base)
7997 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02007998 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02007999 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008000 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008001 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008002 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8003 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008004 if (isneg)
8005 rettv->vval.v_number = -n;
8006 else
8007 rettv->vval.v_number = n;
8008
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008009}
8010
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008011/*
8012 * "strgetchar()" function
8013 */
8014 static void
8015f_strgetchar(typval_T *argvars, typval_T *rettv)
8016{
8017 char_u *str;
8018 int len;
8019 int error = FALSE;
8020 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008021 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008022
8023 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008024 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008025 if (str == NULL)
8026 return;
8027 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008028 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008029 if (error)
8030 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008031
Bram Moolenaar13505972019-01-24 15:04:48 +01008032 while (charidx >= 0 && byteidx < len)
8033 {
8034 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008035 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008036 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8037 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008038 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008039 --charidx;
8040 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008041 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008042}
8043
8044/*
8045 * "stridx()" function
8046 */
8047 static void
8048f_stridx(typval_T *argvars, typval_T *rettv)
8049{
8050 char_u buf[NUMBUFLEN];
8051 char_u *needle;
8052 char_u *haystack;
8053 char_u *save_haystack;
8054 char_u *pos;
8055 int start_idx;
8056
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008057 needle = tv_get_string_chk(&argvars[1]);
8058 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008059 rettv->vval.v_number = -1;
8060 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008061 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008062
8063 if (argvars[2].v_type != VAR_UNKNOWN)
8064 {
8065 int error = FALSE;
8066
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008067 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068 if (error || start_idx >= (int)STRLEN(haystack))
8069 return;
8070 if (start_idx >= 0)
8071 haystack += start_idx;
8072 }
8073
8074 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8075 if (pos != NULL)
8076 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8077}
8078
8079/*
8080 * "string()" function
8081 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008082 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008083f_string(typval_T *argvars, typval_T *rettv)
8084{
8085 char_u *tofree;
8086 char_u numbuf[NUMBUFLEN];
8087
8088 rettv->v_type = VAR_STRING;
8089 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8090 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008091 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008092 if (rettv->vval.v_string != NULL && tofree == NULL)
8093 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8094}
8095
8096/*
8097 * "strlen()" function
8098 */
8099 static void
8100f_strlen(typval_T *argvars, typval_T *rettv)
8101{
8102 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008103 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008104}
8105
8106/*
8107 * "strchars()" function
8108 */
8109 static void
8110f_strchars(typval_T *argvars, typval_T *rettv)
8111{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008112 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008113 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008114 varnumber_T len = 0;
8115 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008116
8117 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008118 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008119 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008120 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008121 else
8122 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008123 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8124 while (*s != NUL)
8125 {
8126 func_mb_ptr2char_adv(&s);
8127 ++len;
8128 }
8129 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008130 }
8131}
8132
8133/*
8134 * "strdisplaywidth()" function
8135 */
8136 static void
8137f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8138{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008139 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008140 int col = 0;
8141
8142 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008143 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008144
8145 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8146}
8147
8148/*
8149 * "strwidth()" function
8150 */
8151 static void
8152f_strwidth(typval_T *argvars, typval_T *rettv)
8153{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008154 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008155
Bram Moolenaar13505972019-01-24 15:04:48 +01008156 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008157}
8158
8159/*
8160 * "strcharpart()" function
8161 */
8162 static void
8163f_strcharpart(typval_T *argvars, typval_T *rettv)
8164{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165 char_u *p;
8166 int nchar;
8167 int nbyte = 0;
8168 int charlen;
8169 int len = 0;
8170 int slen;
8171 int error = FALSE;
8172
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008173 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008174 slen = (int)STRLEN(p);
8175
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008176 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008177 if (!error)
8178 {
8179 if (nchar > 0)
8180 while (nchar > 0 && nbyte < slen)
8181 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008182 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008183 --nchar;
8184 }
8185 else
8186 nbyte = nchar;
8187 if (argvars[2].v_type != VAR_UNKNOWN)
8188 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008189 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008190 while (charlen > 0 && nbyte + len < slen)
8191 {
8192 int off = nbyte + len;
8193
8194 if (off < 0)
8195 len += 1;
8196 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008197 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008198 --charlen;
8199 }
8200 }
8201 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008202 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008203 }
8204
8205 /*
8206 * Only return the overlap between the specified part and the actual
8207 * string.
8208 */
8209 if (nbyte < 0)
8210 {
8211 len += nbyte;
8212 nbyte = 0;
8213 }
8214 else if (nbyte > slen)
8215 nbyte = slen;
8216 if (len < 0)
8217 len = 0;
8218 else if (nbyte + len > slen)
8219 len = slen - nbyte;
8220
8221 rettv->v_type = VAR_STRING;
8222 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008223}
8224
8225/*
8226 * "strpart()" function
8227 */
8228 static void
8229f_strpart(typval_T *argvars, typval_T *rettv)
8230{
8231 char_u *p;
8232 int n;
8233 int len;
8234 int slen;
8235 int error = FALSE;
8236
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008237 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008238 slen = (int)STRLEN(p);
8239
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008240 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008241 if (error)
8242 len = 0;
8243 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008244 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008245 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008246 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008247
8248 /*
8249 * Only return the overlap between the specified part and the actual
8250 * string.
8251 */
8252 if (n < 0)
8253 {
8254 len += n;
8255 n = 0;
8256 }
8257 else if (n > slen)
8258 n = slen;
8259 if (len < 0)
8260 len = 0;
8261 else if (n + len > slen)
8262 len = slen - n;
8263
8264 rettv->v_type = VAR_STRING;
8265 rettv->vval.v_string = vim_strnsave(p + n, len);
8266}
8267
8268/*
8269 * "strridx()" function
8270 */
8271 static void
8272f_strridx(typval_T *argvars, typval_T *rettv)
8273{
8274 char_u buf[NUMBUFLEN];
8275 char_u *needle;
8276 char_u *haystack;
8277 char_u *rest;
8278 char_u *lastmatch = NULL;
8279 int haystack_len, end_idx;
8280
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008281 needle = tv_get_string_chk(&argvars[1]);
8282 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008283
8284 rettv->vval.v_number = -1;
8285 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008286 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008287
8288 haystack_len = (int)STRLEN(haystack);
8289 if (argvars[2].v_type != VAR_UNKNOWN)
8290 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008291 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008292 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008293 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008294 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008295 }
8296 else
8297 end_idx = haystack_len;
8298
8299 if (*needle == NUL)
8300 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008301 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008302 lastmatch = haystack + end_idx;
8303 }
8304 else
8305 {
8306 for (rest = haystack; *rest != '\0'; ++rest)
8307 {
8308 rest = (char_u *)strstr((char *)rest, (char *)needle);
8309 if (rest == NULL || rest > haystack + end_idx)
8310 break;
8311 lastmatch = rest;
8312 }
8313 }
8314
8315 if (lastmatch == NULL)
8316 rettv->vval.v_number = -1;
8317 else
8318 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
8319}
8320
8321/*
8322 * "strtrans()" function
8323 */
8324 static void
8325f_strtrans(typval_T *argvars, typval_T *rettv)
8326{
8327 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008328 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008329}
8330
8331/*
8332 * "submatch()" function
8333 */
8334 static void
8335f_submatch(typval_T *argvars, typval_T *rettv)
8336{
8337 int error = FALSE;
8338 int no;
8339 int retList = 0;
8340
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008341 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008342 if (error)
8343 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008344 if (no < 0 || no >= NSUBEXP)
8345 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008346 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01008347 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008348 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008349 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008350 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008351 if (error)
8352 return;
8353
8354 if (retList == 0)
8355 {
8356 rettv->v_type = VAR_STRING;
8357 rettv->vval.v_string = reg_submatch(no);
8358 }
8359 else
8360 {
8361 rettv->v_type = VAR_LIST;
8362 rettv->vval.v_list = reg_submatch_list(no);
8363 }
8364}
8365
8366/*
8367 * "substitute()" function
8368 */
8369 static void
8370f_substitute(typval_T *argvars, typval_T *rettv)
8371{
8372 char_u patbuf[NUMBUFLEN];
8373 char_u subbuf[NUMBUFLEN];
8374 char_u flagsbuf[NUMBUFLEN];
8375
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008376 char_u *str = tv_get_string_chk(&argvars[0]);
8377 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008378 char_u *sub = NULL;
8379 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008380 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008381
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008382 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
8383 expr = &argvars[2];
8384 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008385 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008386
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008387 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008388 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
8389 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008390 rettv->vval.v_string = NULL;
8391 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008392 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008393}
8394
8395/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008396 * "swapinfo(swap_filename)" function
8397 */
8398 static void
8399f_swapinfo(typval_T *argvars, typval_T *rettv)
8400{
8401 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008402 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008403}
8404
8405/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02008406 * "swapname(expr)" function
8407 */
8408 static void
8409f_swapname(typval_T *argvars, typval_T *rettv)
8410{
8411 buf_T *buf;
8412
8413 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008414 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02008415 if (buf == NULL || buf->b_ml.ml_mfp == NULL
8416 || buf->b_ml.ml_mfp->mf_fname == NULL)
8417 rettv->vval.v_string = NULL;
8418 else
8419 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
8420}
8421
8422/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008423 * "synID(lnum, col, trans)" function
8424 */
8425 static void
8426f_synID(typval_T *argvars UNUSED, typval_T *rettv)
8427{
8428 int id = 0;
8429#ifdef FEAT_SYN_HL
8430 linenr_T lnum;
8431 colnr_T col;
8432 int trans;
8433 int transerr = FALSE;
8434
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008435 lnum = tv_get_lnum(argvars); // -1 on type error
8436 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008437 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008438
8439 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8440 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
8441 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
8442#endif
8443
8444 rettv->vval.v_number = id;
8445}
8446
8447/*
8448 * "synIDattr(id, what [, mode])" function
8449 */
8450 static void
8451f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
8452{
8453 char_u *p = NULL;
8454#ifdef FEAT_SYN_HL
8455 int id;
8456 char_u *what;
8457 char_u *mode;
8458 char_u modebuf[NUMBUFLEN];
8459 int modec;
8460
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008461 id = (int)tv_get_number(&argvars[0]);
8462 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008463 if (argvars[2].v_type != VAR_UNKNOWN)
8464 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008465 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008466 modec = TOLOWER_ASC(mode[0]);
8467 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008468 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008469 }
8470 else
8471 {
8472#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
8473 if (USE_24BIT)
8474 modec = 'g';
8475 else
8476#endif
8477 if (t_colors > 1)
8478 modec = 'c';
8479 else
8480 modec = 't';
8481 }
8482
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008483 switch (TOLOWER_ASC(what[0]))
8484 {
8485 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008486 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008487 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008488 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008489 p = highlight_has_attr(id, HL_BOLD, modec);
8490 break;
8491
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008492 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008493 p = highlight_color(id, what, modec);
8494 break;
8495
8496 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008497 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008498 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008499 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008500 p = highlight_has_attr(id, HL_ITALIC, modec);
8501 break;
8502
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008503 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008504 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008505 break;
8506
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008507 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008508 p = highlight_has_attr(id, HL_INVERSE, modec);
8509 break;
8510
8511 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008512 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008513 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008514 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008515 else if (TOLOWER_ASC(what[1]) == 't' &&
8516 TOLOWER_ASC(what[2]) == 'r')
8517 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008518 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008519 p = highlight_has_attr(id, HL_STANDOUT, modec);
8520 break;
8521
8522 case 'u':
8523 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008524 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008525 p = highlight_has_attr(id, HL_UNDERLINE, modec);
8526 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008527 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008528 p = highlight_has_attr(id, HL_UNDERCURL, modec);
8529 break;
8530 }
8531
8532 if (p != NULL)
8533 p = vim_strsave(p);
8534#endif
8535 rettv->v_type = VAR_STRING;
8536 rettv->vval.v_string = p;
8537}
8538
8539/*
8540 * "synIDtrans(id)" function
8541 */
8542 static void
8543f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
8544{
8545 int id;
8546
8547#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008548 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008549
8550 if (id > 0)
8551 id = syn_get_final_id(id);
8552 else
8553#endif
8554 id = 0;
8555
8556 rettv->vval.v_number = id;
8557}
8558
8559/*
8560 * "synconcealed(lnum, col)" function
8561 */
8562 static void
8563f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
8564{
8565#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
8566 linenr_T lnum;
8567 colnr_T col;
8568 int syntax_flags = 0;
8569 int cchar;
8570 int matchid = 0;
8571 char_u str[NUMBUFLEN];
8572#endif
8573
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008574 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008575
8576#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008577 lnum = tv_get_lnum(argvars); // -1 on type error
8578 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008579
Bram Moolenaara80faa82020-04-12 19:37:17 +02008580 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008581
8582 if (rettv_list_alloc(rettv) != FAIL)
8583 {
8584 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8585 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8586 && curwin->w_p_cole > 0)
8587 {
8588 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
8589 syntax_flags = get_syntax_info(&matchid);
8590
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008591 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008592 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
8593 {
8594 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02008595 if (cchar == NUL && curwin->w_p_cole == 1)
8596 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008597 if (cchar != NUL)
8598 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008599 if (has_mbyte)
8600 (*mb_char2bytes)(cchar, str);
8601 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602 str[0] = cchar;
8603 }
8604 }
8605 }
8606
8607 list_append_number(rettv->vval.v_list,
8608 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008609 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008610 list_append_string(rettv->vval.v_list, str, -1);
8611 list_append_number(rettv->vval.v_list, matchid);
8612 }
8613#endif
8614}
8615
8616/*
8617 * "synstack(lnum, col)" function
8618 */
8619 static void
8620f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
8621{
8622#ifdef FEAT_SYN_HL
8623 linenr_T lnum;
8624 colnr_T col;
8625 int i;
8626 int id;
8627#endif
8628
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008629 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008630
8631#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008632 lnum = tv_get_lnum(argvars); // -1 on type error
8633 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008634
8635 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8636 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8637 && rettv_list_alloc(rettv) != FAIL)
8638 {
8639 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
8640 for (i = 0; ; ++i)
8641 {
8642 id = syn_get_stack_item(i);
8643 if (id < 0)
8644 break;
8645 if (list_append_number(rettv->vval.v_list, id) == FAIL)
8646 break;
8647 }
8648 }
8649#endif
8650}
8651
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008652/*
8653 * "tabpagebuflist()" function
8654 */
8655 static void
8656f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8657{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008658 tabpage_T *tp;
8659 win_T *wp = NULL;
8660
8661 if (argvars[0].v_type == VAR_UNKNOWN)
8662 wp = firstwin;
8663 else
8664 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008665 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008666 if (tp != NULL)
8667 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
8668 }
8669 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
8670 {
8671 for (; wp != NULL; wp = wp->w_next)
8672 if (list_append_number(rettv->vval.v_list,
8673 wp->w_buffer->b_fnum) == FAIL)
8674 break;
8675 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008676}
8677
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008678/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008679 * "tagfiles()" function
8680 */
8681 static void
8682f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
8683{
8684 char_u *fname;
8685 tagname_T tn;
8686 int first;
8687
8688 if (rettv_list_alloc(rettv) == FAIL)
8689 return;
8690 fname = alloc(MAXPATHL);
8691 if (fname == NULL)
8692 return;
8693
8694 for (first = TRUE; ; first = FALSE)
8695 if (get_tagfname(&tn, first, fname) == FAIL
8696 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
8697 break;
8698 tagname_free(&tn);
8699 vim_free(fname);
8700}
8701
8702/*
8703 * "taglist()" function
8704 */
8705 static void
8706f_taglist(typval_T *argvars, typval_T *rettv)
8707{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008708 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709 char_u *tag_pattern;
8710
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008711 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008712
8713 rettv->vval.v_number = FALSE;
8714 if (*tag_pattern == NUL)
8715 return;
8716
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008717 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008718 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008719 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008720 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008721}
8722
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723#ifdef FEAT_FLOAT
8724/*
8725 * "tan()" function
8726 */
8727 static void
8728f_tan(typval_T *argvars, typval_T *rettv)
8729{
8730 float_T f = 0.0;
8731
8732 rettv->v_type = VAR_FLOAT;
8733 if (get_float_arg(argvars, &f) == OK)
8734 rettv->vval.v_float = tan(f);
8735 else
8736 rettv->vval.v_float = 0.0;
8737}
8738
8739/*
8740 * "tanh()" function
8741 */
8742 static void
8743f_tanh(typval_T *argvars, typval_T *rettv)
8744{
8745 float_T f = 0.0;
8746
8747 rettv->v_type = VAR_FLOAT;
8748 if (get_float_arg(argvars, &f) == OK)
8749 rettv->vval.v_float = tanh(f);
8750 else
8751 rettv->vval.v_float = 0.0;
8752}
8753#endif
8754
8755/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008756 * "tolower(string)" function
8757 */
8758 static void
8759f_tolower(typval_T *argvars, typval_T *rettv)
8760{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008761 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008762 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008763}
8764
8765/*
8766 * "toupper(string)" function
8767 */
8768 static void
8769f_toupper(typval_T *argvars, typval_T *rettv)
8770{
8771 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008772 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008773}
8774
8775/*
8776 * "tr(string, fromstr, tostr)" function
8777 */
8778 static void
8779f_tr(typval_T *argvars, typval_T *rettv)
8780{
8781 char_u *in_str;
8782 char_u *fromstr;
8783 char_u *tostr;
8784 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008785 int inlen;
8786 int fromlen;
8787 int tolen;
8788 int idx;
8789 char_u *cpstr;
8790 int cplen;
8791 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008792 char_u buf[NUMBUFLEN];
8793 char_u buf2[NUMBUFLEN];
8794 garray_T ga;
8795
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008796 in_str = tv_get_string(&argvars[0]);
8797 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
8798 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008799
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008800 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008801 rettv->v_type = VAR_STRING;
8802 rettv->vval.v_string = NULL;
8803 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008804 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008805 ga_init2(&ga, (int)sizeof(char), 80);
8806
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008807 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008808 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008809 if (STRLEN(fromstr) != STRLEN(tostr))
8810 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008811error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008812 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008813 ga_clear(&ga);
8814 return;
8815 }
8816
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008817 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008818 while (*in_str != NUL)
8819 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008820 if (has_mbyte)
8821 {
8822 inlen = (*mb_ptr2len)(in_str);
8823 cpstr = in_str;
8824 cplen = inlen;
8825 idx = 0;
8826 for (p = fromstr; *p != NUL; p += fromlen)
8827 {
8828 fromlen = (*mb_ptr2len)(p);
8829 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
8830 {
8831 for (p = tostr; *p != NUL; p += tolen)
8832 {
8833 tolen = (*mb_ptr2len)(p);
8834 if (idx-- == 0)
8835 {
8836 cplen = tolen;
8837 cpstr = p;
8838 break;
8839 }
8840 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008841 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008842 goto error;
8843 break;
8844 }
8845 ++idx;
8846 }
8847
8848 if (first && cpstr == in_str)
8849 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008850 // Check that fromstr and tostr have the same number of
8851 // (multi-byte) characters. Done only once when a character
8852 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008853 first = FALSE;
8854 for (p = tostr; *p != NUL; p += tolen)
8855 {
8856 tolen = (*mb_ptr2len)(p);
8857 --idx;
8858 }
8859 if (idx != 0)
8860 goto error;
8861 }
8862
8863 (void)ga_grow(&ga, cplen);
8864 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
8865 ga.ga_len += cplen;
8866
8867 in_str += inlen;
8868 }
8869 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008870 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008871 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008872 p = vim_strchr(fromstr, *in_str);
8873 if (p != NULL)
8874 ga_append(&ga, tostr[p - fromstr]);
8875 else
8876 ga_append(&ga, *in_str);
8877 ++in_str;
8878 }
8879 }
8880
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008881 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008882 (void)ga_grow(&ga, 1);
8883 ga_append(&ga, NUL);
8884
8885 rettv->vval.v_string = ga.ga_data;
8886}
8887
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008888/*
8889 * "trim({expr})" function
8890 */
8891 static void
8892f_trim(typval_T *argvars, typval_T *rettv)
8893{
8894 char_u buf1[NUMBUFLEN];
8895 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008896 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008897 char_u *mask = NULL;
8898 char_u *tail;
8899 char_u *prev;
8900 char_u *p;
8901 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008902 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008903
8904 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008905 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008906 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008907 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008908
8909 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008910 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008911 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008912
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008913 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008914 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008915 int error = 0;
8916
8917 // leading or trailing characters to trim
8918 dir = (int)tv_get_number_chk(&argvars[2], &error);
8919 if (error)
8920 return;
8921 if (dir < 0 || dir > 2)
8922 {
8923 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
8924 return;
8925 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008926 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008927 }
8928
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008929 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008930 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008931 // Trim leading characters
8932 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008933 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008934 c1 = PTR2CHAR(head);
8935 if (mask == NULL)
8936 {
8937 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008938 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008939 }
8940 else
8941 {
8942 for (p = mask; *p != NUL; MB_PTR_ADV(p))
8943 if (c1 == PTR2CHAR(p))
8944 break;
8945 if (*p == NUL)
8946 break;
8947 }
8948 MB_PTR_ADV(head);
8949 }
8950 }
8951
8952 tail = head + STRLEN(head);
8953 if (dir == 0 || dir == 2)
8954 {
8955 // Trim trailing characters
8956 for (; tail > head; tail = prev)
8957 {
8958 prev = tail;
8959 MB_PTR_BACK(head, prev);
8960 c1 = PTR2CHAR(prev);
8961 if (mask == NULL)
8962 {
8963 if (c1 > ' ' && c1 != 0xa0)
8964 break;
8965 }
8966 else
8967 {
8968 for (p = mask; *p != NUL; MB_PTR_ADV(p))
8969 if (c1 == PTR2CHAR(p))
8970 break;
8971 if (*p == NUL)
8972 break;
8973 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008974 }
8975 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02008976 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008977}
8978
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008979#ifdef FEAT_FLOAT
8980/*
8981 * "trunc({float})" function
8982 */
8983 static void
8984f_trunc(typval_T *argvars, typval_T *rettv)
8985{
8986 float_T f = 0.0;
8987
8988 rettv->v_type = VAR_FLOAT;
8989 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008990 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008991 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
8992 else
8993 rettv->vval.v_float = 0.0;
8994}
8995#endif
8996
8997/*
8998 * "type(expr)" function
8999 */
9000 static void
9001f_type(typval_T *argvars, typval_T *rettv)
9002{
9003 int n = -1;
9004
9005 switch (argvars[0].v_type)
9006 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009007 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9008 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009009 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009010 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9011 case VAR_LIST: n = VAR_TYPE_LIST; break;
9012 case VAR_DICT: n = VAR_TYPE_DICT; break;
9013 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9014 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9015 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009016 case VAR_JOB: n = VAR_TYPE_JOB; break;
9017 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009018 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009019 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009020 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009021 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009022 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009023 n = -1;
9024 break;
9025 }
9026 rettv->vval.v_number = n;
9027}
9028
9029/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009030 * "virtcol(string)" function
9031 */
9032 static void
9033f_virtcol(typval_T *argvars, typval_T *rettv)
9034{
9035 colnr_T vcol = 0;
9036 pos_T *fp;
9037 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009038 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009039
9040 fp = var2fpos(&argvars[0], FALSE, &fnum);
9041 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9042 && fnum == curbuf->b_fnum)
9043 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009044 // Limit the column to a valid value, getvvcol() doesn't check.
9045 if (fp->col < 0)
9046 fp->col = 0;
9047 else
9048 {
9049 len = (int)STRLEN(ml_get(fp->lnum));
9050 if (fp->col > len)
9051 fp->col = len;
9052 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009053 getvvcol(curwin, fp, NULL, NULL, &vcol);
9054 ++vcol;
9055 }
9056
9057 rettv->vval.v_number = vcol;
9058}
9059
9060/*
9061 * "visualmode()" function
9062 */
9063 static void
9064f_visualmode(typval_T *argvars, typval_T *rettv)
9065{
9066 char_u str[2];
9067
9068 rettv->v_type = VAR_STRING;
9069 str[0] = curbuf->b_visual_mode_eval;
9070 str[1] = NUL;
9071 rettv->vval.v_string = vim_strsave(str);
9072
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009073 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009074 if (non_zero_arg(&argvars[0]))
9075 curbuf->b_visual_mode_eval = NUL;
9076}
9077
9078/*
9079 * "wildmenumode()" function
9080 */
9081 static void
9082f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9083{
9084#ifdef FEAT_WILDMENU
9085 if (wild_menu_showing)
9086 rettv->vval.v_number = 1;
9087#endif
9088}
9089
9090/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009091 * "windowsversion()" function
9092 */
9093 static void
9094f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9095{
9096 rettv->v_type = VAR_STRING;
9097 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9098}
9099
9100/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009101 * "wordcount()" function
9102 */
9103 static void
9104f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9105{
9106 if (rettv_dict_alloc(rettv) == FAIL)
9107 return;
9108 cursor_pos_info(rettv->vval.v_dict);
9109}
9110
9111/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112 * "xor(expr, expr)" function
9113 */
9114 static void
9115f_xor(typval_T *argvars, typval_T *rettv)
9116{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009117 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9118 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009119}
9120
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009121#endif // FEAT_EVAL