blob: f0a15f98ca1843be96476f024ee27036aeb32088 [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 Moolenaar6f02b002021-01-10 20:22:54 +010050static void f_charcol(typval_T *argvars, typval_T *rettv);
Bram Moolenaar17793ef2020-12-28 12:56:58 +010051static void f_charidx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020053static void f_confirm(typval_T *argvars, typval_T *rettv);
54static void f_copy(typval_T *argvars, typval_T *rettv);
55#ifdef FEAT_FLOAT
56static void f_cos(typval_T *argvars, typval_T *rettv);
57static void f_cosh(typval_T *argvars, typval_T *rettv);
58#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010060#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020061static void f_debugbreak(typval_T *argvars, typval_T *rettv);
62#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020063static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020064static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010065static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020066static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020067static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_escape(typval_T *argvars, typval_T *rettv);
69static void f_eval(typval_T *argvars, typval_T *rettv);
70static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_exists(typval_T *argvars, typval_T *rettv);
73#ifdef FEAT_FLOAT
74static void f_exp(typval_T *argvars, typval_T *rettv);
75#endif
76static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020077static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020078static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020079#ifdef FEAT_FLOAT
80static void f_float2nr(typval_T *argvars, typval_T *rettv);
81static void f_floor(typval_T *argvars, typval_T *rettv);
82static void f_fmod(typval_T *argvars, typval_T *rettv);
83#endif
84static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020085static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020086static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_function(typval_T *argvars, typval_T *rettv);
88static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
89static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010090static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010091static void f_getcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020092static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020093static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020094static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010095static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_getpid(typval_T *argvars, typval_T *rettv);
97static void f_getcurpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010098static void f_getcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020099static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200100static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200101static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100103static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +0200104static void f_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200105static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
106static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200107static void f_hlID(typval_T *argvars, typval_T *rettv);
108static void f_hlexists(typval_T *argvars, typval_T *rettv);
109static void f_hostname(typval_T *argvars, typval_T *rettv);
110static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200111static void f_index(typval_T *argvars, typval_T *rettv);
112static void f_input(typval_T *argvars, typval_T *rettv);
113static void f_inputdialog(typval_T *argvars, typval_T *rettv);
114static void f_inputlist(typval_T *argvars, typval_T *rettv);
115static void f_inputrestore(typval_T *argvars, typval_T *rettv);
116static void f_inputsave(typval_T *argvars, typval_T *rettv);
117static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +0100118static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200119static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120static void f_islocked(typval_T *argvars, typval_T *rettv);
121#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200122static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200123static void f_isnan(typval_T *argvars, typval_T *rettv);
124#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200125static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
126static void f_len(typval_T *argvars, typval_T *rettv);
127static void f_libcall(typval_T *argvars, typval_T *rettv);
128static void f_libcallnr(typval_T *argvars, typval_T *rettv);
129static void f_line(typval_T *argvars, typval_T *rettv);
130static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200131#ifdef FEAT_FLOAT
132static void f_log(typval_T *argvars, typval_T *rettv);
133static void f_log10(typval_T *argvars, typval_T *rettv);
134#endif
135#ifdef FEAT_LUA
136static void f_luaeval(typval_T *argvars, typval_T *rettv);
137#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200138static void f_maparg(typval_T *argvars, typval_T *rettv);
139static void f_mapcheck(typval_T *argvars, typval_T *rettv);
140static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200141static void f_matchend(typval_T *argvars, typval_T *rettv);
142static void f_matchlist(typval_T *argvars, typval_T *rettv);
143static void f_matchstr(typval_T *argvars, typval_T *rettv);
144static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
145static void f_max(typval_T *argvars, typval_T *rettv);
146static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200147#ifdef FEAT_MZSCHEME
148static void f_mzeval(typval_T *argvars, typval_T *rettv);
149#endif
150static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
151static void f_nr2char(typval_T *argvars, typval_T *rettv);
152static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200153#ifdef FEAT_PERL
154static void f_perleval(typval_T *argvars, typval_T *rettv);
155#endif
156#ifdef FEAT_FLOAT
157static void f_pow(typval_T *argvars, typval_T *rettv);
158#endif
159static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
160static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200161static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200162static void f_pumvisible(typval_T *argvars, typval_T *rettv);
163#ifdef FEAT_PYTHON3
164static void f_py3eval(typval_T *argvars, typval_T *rettv);
165#endif
166#ifdef FEAT_PYTHON
167static void f_pyeval(typval_T *argvars, typval_T *rettv);
168#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100169#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
170static void f_pyxeval(typval_T *argvars, typval_T *rettv);
171#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100172static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100173static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200175static void f_reg_executing(typval_T *argvars, typval_T *rettv);
176static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_rename(typval_T *argvars, typval_T *rettv);
178static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200179#ifdef FEAT_FLOAT
180static void f_round(typval_T *argvars, typval_T *rettv);
181#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100182#ifdef FEAT_RUBY
183static void f_rubyeval(typval_T *argvars, typval_T *rettv);
184#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200185static void f_screenattr(typval_T *argvars, typval_T *rettv);
186static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100187static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200188static void f_screencol(typval_T *argvars, typval_T *rettv);
189static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100190static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200191static void f_search(typval_T *argvars, typval_T *rettv);
192static void f_searchdecl(typval_T *argvars, typval_T *rettv);
193static void f_searchpair(typval_T *argvars, typval_T *rettv);
194static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
195static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100196static void f_setcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100198static void f_setcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200199static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100203static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200204#ifdef FEAT_CRYPT
205static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200206#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200207static void f_shellescape(typval_T *argvars, typval_T *rettv);
208static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200209#ifdef FEAT_FLOAT
210static void f_sin(typval_T *argvars, typval_T *rettv);
211static void f_sinh(typval_T *argvars, typval_T *rettv);
212#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200213static void f_soundfold(typval_T *argvars, typval_T *rettv);
214static void f_spellbadword(typval_T *argvars, typval_T *rettv);
215static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
216static void f_split(typval_T *argvars, typval_T *rettv);
217#ifdef FEAT_FLOAT
218static void f_sqrt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100219#endif
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100220static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100221#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200222static void f_str2float(typval_T *argvars, typval_T *rettv);
223#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200224static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200225static void f_str2nr(typval_T *argvars, typval_T *rettv);
226static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200227static void f_strgetchar(typval_T *argvars, typval_T *rettv);
228static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200229static void f_strlen(typval_T *argvars, typval_T *rettv);
230static void f_strcharpart(typval_T *argvars, typval_T *rettv);
231static void f_strpart(typval_T *argvars, typval_T *rettv);
232static void f_strridx(typval_T *argvars, typval_T *rettv);
233static void f_strtrans(typval_T *argvars, typval_T *rettv);
234static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
235static void f_strwidth(typval_T *argvars, typval_T *rettv);
236static void f_submatch(typval_T *argvars, typval_T *rettv);
237static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200238static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200239static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200240static void f_synID(typval_T *argvars, typval_T *rettv);
241static void f_synIDattr(typval_T *argvars, typval_T *rettv);
242static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
243static void f_synstack(typval_T *argvars, typval_T *rettv);
244static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200246static void f_taglist(typval_T *argvars, typval_T *rettv);
247static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200248#ifdef FEAT_FLOAT
249static void f_tan(typval_T *argvars, typval_T *rettv);
250static void f_tanh(typval_T *argvars, typval_T *rettv);
251#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200252static void f_tolower(typval_T *argvars, typval_T *rettv);
253static void f_toupper(typval_T *argvars, typval_T *rettv);
254static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100255static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200256#ifdef FEAT_FLOAT
257static void f_trunc(typval_T *argvars, typval_T *rettv);
258#endif
259static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200260static void f_virtcol(typval_T *argvars, typval_T *rettv);
261static void f_visualmode(typval_T *argvars, typval_T *rettv);
262static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100263static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200264static void f_wordcount(typval_T *argvars, typval_T *rettv);
265static void f_xor(typval_T *argvars, typval_T *rettv);
266
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100267
Bram Moolenaar94738d82020-10-21 14:25:07 +0200268/*
269 * Functions that check the argument type of a builtin function.
270 * Each function returns FAIL and gives an error message if the type is wrong.
271 */
272
273// Context passed to an arg_ function.
274typedef struct {
Bram Moolenaarca174532020-10-21 16:42:22 +0200275 int arg_count; // actual argument count
276 type_T **arg_types; // list of argument types
277 int arg_idx; // current argument index (first arg is zero)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100278 cctx_T *arg_cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +0200279} argcontext_T;
280
281// A function to check one argument type. The first argument is the type to
282// check. If needed, other argument types can be obtained with the context.
283// E.g. if "arg_idx" is 1, then (type - 1) is the first argument type.
284typedef int (*argcheck_T)(type_T *, argcontext_T *);
285
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100286/*
Bram Moolenaar351ead02021-01-16 16:07:01 +0100287 * Call need_type() to check an argument type.
288 */
289 static int
290check_arg_type(
291 type_T *expected,
292 type_T *actual,
293 argcontext_T *context)
294{
295 // TODO: would be useful to know if "actual" is a constant and pass it to
296 // need_type() to get a compile time error if possible.
297 return need_type(actual, expected,
298 context->arg_idx - context->arg_count, context->arg_idx + 1,
299 context->arg_cctx, FALSE, FALSE);
300}
301
302/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100303 * Check "type" is a float or a number.
304 */
Bram Moolenaar94738d82020-10-21 14:25:07 +0200305 static int
306arg_float_or_nr(type_T *type, argcontext_T *context)
307{
Bram Moolenaarca174532020-10-21 16:42:22 +0200308 if (type->tt_type == VAR_ANY
309 || type->tt_type == VAR_FLOAT || type->tt_type == VAR_NUMBER)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200310 return OK;
311 arg_type_mismatch(&t_number, type, context->arg_idx + 1);
312 return FAIL;
313}
314
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100315/*
316 * Check "type" is a number.
317 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200318 static int
319arg_number(type_T *type, argcontext_T *context)
320{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100321 return check_arg_type(&t_number, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200322}
323
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100324/*
325 * Check "type" is a string.
326 */
327 static int
328arg_string(type_T *type, argcontext_T *context)
329{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100330 return check_arg_type(&t_string, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100331}
332
333/*
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100334 * Check "type" is a bool or number 0 or 1.
335 */
336 static int
337arg_bool(type_T *type, argcontext_T *context)
338{
339 if (type->tt_type == VAR_ANY
340 || type->tt_type == VAR_NUMBER || type->tt_type == VAR_BOOL)
341 return OK;
342 return check_arg_type(&t_bool, type, context);
343}
344
345/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100346 * Check "type" is a list or a blob.
347 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200348 static int
349arg_list_or_blob(type_T *type, argcontext_T *context)
350{
351 if (type->tt_type == VAR_ANY
352 || type->tt_type == VAR_LIST || type->tt_type == VAR_BLOB)
353 return OK;
354 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
355 return FAIL;
356}
357
358/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100359 * Check "type" is a list or a dict.
360 */
361 static int
362arg_list_or_dict(type_T *type, argcontext_T *context)
363{
364 if (type->tt_type == VAR_ANY
365 || type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
366 return OK;
367 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
368 return FAIL;
369}
370
371/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100372 * Check "type" is the same type as the previous argument.
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100373 * Must not be used for the first argcheck_T entry.
374 */
375 static int
376arg_same_as_prev(type_T *type, argcontext_T *context)
377{
378 type_T *prev_type = context->arg_types[context->arg_idx - 1];
379
Bram Moolenaar351ead02021-01-16 16:07:01 +0100380 return check_arg_type(prev_type, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100381}
382
383/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100384 * Check "type" is the same basic type as the previous argument, checks list or
385 * dict vs other type, but not member type.
386 * Must not be used for the first argcheck_T entry.
387 */
388 static int
389arg_same_struct_as_prev(type_T *type, argcontext_T *context)
390{
391 type_T *prev_type = context->arg_types[context->arg_idx - 1];
392
393 if (prev_type->tt_type != context->arg_types[context->arg_idx]->tt_type)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100394 return check_arg_type(prev_type, type, context);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100395 return OK;
396}
397
398/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100399 * Check "type" is an item of the list or blob of the previous arg.
Bram Moolenaarca174532020-10-21 16:42:22 +0200400 * Must not be used for the first argcheck_T entry.
401 */
402 static int
403arg_item_of_prev(type_T *type, argcontext_T *context)
404{
405 type_T *prev_type = context->arg_types[context->arg_idx - 1];
406 type_T *expected;
407
408 if (prev_type->tt_type == VAR_LIST)
409 expected = prev_type->tt_member;
410 else if (prev_type->tt_type == VAR_BLOB)
411 expected = &t_number;
412 else
413 // probably VAR_ANY, can't check
414 return OK;
415
Bram Moolenaar351ead02021-01-16 16:07:01 +0100416 return check_arg_type(expected, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200417}
418
Bram Moolenaar94738d82020-10-21 14:25:07 +0200419/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100420 * Check "type" which is the third argument of extend().
421 */
422 static int
423arg_extend3(type_T *type, argcontext_T *context)
424{
425 type_T *first_type = context->arg_types[context->arg_idx - 2];
426
427 if (first_type->tt_type == VAR_LIST)
428 return arg_number(type, context);
429 if (first_type->tt_type == VAR_DICT)
430 return arg_string(type, context);
431 return OK;
432}
433
434
435/*
Bram Moolenaar94738d82020-10-21 14:25:07 +0200436 * Lists of functions that check the argument types of a builtin function.
437 */
Bram Moolenaar80ad3e22021-01-31 20:48:58 +0100438argcheck_T arg1_string[] = {arg_string};
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100439argcheck_T arg3_string_nr_bool[] = {arg_string, arg_number, arg_bool};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200440argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100441argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev};
442argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3};
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100443argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
Bram Moolenaarca174532020-10-21 16:42:22 +0200444argcheck_T arg3_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200445
446/*
447 * Functions that return the return type of a builtin function.
Bram Moolenaara1224cb2020-10-22 12:31:49 +0200448 * Note that "argtypes" is NULL if "argcount" is zero.
Bram Moolenaar94738d82020-10-21 14:25:07 +0200449 */
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100450 static type_T *
451ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
452{
453 return &t_void;
454}
455 static type_T *
456ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
457{
458 return &t_any;
459}
460 static type_T *
Bram Moolenaar403dc312020-10-17 19:29:51 +0200461ret_bool(int argcount UNUSED, type_T **argtypes UNUSED)
462{
463 return &t_bool;
464}
465 static type_T *
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100466ret_number_bool(int argcount UNUSED, type_T **argtypes UNUSED)
467{
468 return &t_number_bool;
469}
470 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100471ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
472{
473 return &t_number;
474}
475 static type_T *
476ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
477{
478 return &t_float;
479}
480 static type_T *
481ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
482{
483 return &t_string;
484}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200485 static type_T *
486ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100487{
488 return &t_list_any;
489}
490 static type_T *
491ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
492{
493 return &t_list_number;
494}
495 static type_T *
496ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
497{
498 return &t_list_string;
499}
500 static type_T *
501ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
502{
503 return &t_list_dict_any;
504}
505 static type_T *
506ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
507{
508 return &t_dict_any;
509}
510 static type_T *
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100511ret_job_info(int argcount, type_T **argtypes UNUSED)
512{
513 if (argcount == 0)
514 return &t_list_job;
515 return &t_dict_any;
516}
517 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100518ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
519{
520 return &t_dict_number;
521}
522 static type_T *
523ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
524{
525 return &t_dict_string;
526}
527 static type_T *
528ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
529{
530 return &t_blob;
531}
532 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200533ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100534{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200535 return &t_func_any;
536}
537 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100538ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
539{
540 return &t_channel;
541}
542 static type_T *
543ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
544{
545 return &t_job;
546}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200547 static type_T *
548ret_first_arg(int argcount, type_T **argtypes)
549{
550 if (argcount > 0)
551 return argtypes[0];
552 return &t_void;
553}
Bram Moolenaarea696852020-11-09 18:31:39 +0100554// for map(): returns first argument but item type may differ
555 static type_T *
556ret_first_cont(int argcount UNUSED, type_T **argtypes)
557{
558 if (argtypes[0]->tt_type == VAR_LIST)
559 return &t_list_any;
560 if (argtypes[0]->tt_type == VAR_DICT)
561 return &t_dict_any;
562 if (argtypes[0]->tt_type == VAR_BLOB)
563 return argtypes[0];
564 return &t_any;
565}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200566
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200567/*
568 * Used for getqflist(): returns list if there is no argument, dict if there is
569 * one.
570 */
571 static type_T *
572ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
573{
574 if (argcount > 0)
575 return &t_dict_any;
576 return &t_list_dict_any;
577}
578
579/*
580 * Used for getloclist(): returns list if there is one argument, dict if there
581 * are two.
582 */
583 static type_T *
584ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
585{
586 if (argcount > 1)
587 return &t_dict_any;
588 return &t_list_dict_any;
589}
590
Bram Moolenaar846178a2020-07-05 17:04:13 +0200591 static type_T *
592ret_argv(int argcount, type_T **argtypes UNUSED)
593{
594 // argv() returns list of strings
595 if (argcount == 0)
596 return &t_list_string;
597
598 // argv(0) returns a string, but argv(-1] returns a list
599 return &t_any;
600}
601
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200602 static type_T *
603ret_remove(int argcount UNUSED, type_T **argtypes)
604{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200605 if (argtypes != NULL)
606 {
607 if (argtypes[0]->tt_type == VAR_LIST
608 || argtypes[0]->tt_type == VAR_DICT)
609 return argtypes[0]->tt_member;
610 if (argtypes[0]->tt_type == VAR_BLOB)
611 return &t_number;
612 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200613 return &t_any;
614}
615
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200616 static type_T *
617ret_getreg(int argcount, type_T **argtypes UNUSED)
618{
619 // Assume that if the third argument is passed it's non-zero
620 if (argcount == 3)
621 return &t_list_string;
622 return &t_string;
623}
624
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200625 static type_T *
626ret_maparg(int argcount, type_T **argtypes UNUSED)
627{
628 // Assume that if the fourth argument is passed it's non-zero
629 if (argcount == 4)
630 return &t_dict_any;
631 return &t_string;
632}
633
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100634static type_T *ret_f_function(int argcount, type_T **argtypes);
635
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636/*
637 * Array with names and number of arguments of all internal functions
638 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
639 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200640typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200641{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200642 char *f_name; // function name
643 char f_min_argc; // minimal number of arguments
644 char f_max_argc; // maximal number of arguments
645 char f_argtype; // for method: FEARG_ values
Bram Moolenaar94738d82020-10-21 14:25:07 +0200646 argcheck_T *f_argcheck; // list of functions to check argument types
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100647 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
648 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200649 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200650 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200651} funcentry_T;
652
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200653// values for f_argtype; zero means it cannot be used as a method
654#define FEARG_1 1 // base is the first argument
655#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200656#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200657#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200658#define FEARG_LAST 9 // base is the last argument
659
Bram Moolenaar15c47602020-03-26 22:16:48 +0100660#ifdef FEAT_FLOAT
661# define FLOAT_FUNC(name) name
662#else
663# define FLOAT_FUNC(name) NULL
664#endif
665#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
666# define MATH_FUNC(name) name
667#else
668# define MATH_FUNC(name) NULL
669#endif
670#ifdef FEAT_TIMERS
671# define TIMER_FUNC(name) name
672#else
673# define TIMER_FUNC(name) NULL
674#endif
675#ifdef FEAT_JOB_CHANNEL
676# define JOB_FUNC(name) name
677#else
678# define JOB_FUNC(name) NULL
679#endif
680#ifdef FEAT_PROP_POPUP
681# define PROP_FUNC(name) name
682#else
683# define PROP_FUNC(name) NULL
684#endif
685#ifdef FEAT_SIGNS
686# define SIGN_FUNC(name) name
687#else
688# define SIGN_FUNC(name) NULL
689#endif
690#ifdef FEAT_SOUND
691# define SOUND_FUNC(name) name
692#else
693# define SOUND_FUNC(name) NULL
694#endif
695#ifdef FEAT_TERMINAL
696# define TERM_FUNC(name) name
697#else
698# define TERM_FUNC(name) NULL
699#endif
700
Bram Moolenaarac92e252019-08-03 21:58:38 +0200701static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200702{
Bram Moolenaar94738d82020-10-21 14:25:07 +0200703 {"abs", 1, 1, FEARG_1, arg1_float_or_nr,
704 ret_any, FLOAT_FUNC(f_abs)},
705 {"acos", 1, 1, FEARG_1, NULL,
706 ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100707 {"add", 2, 2, FEARG_1, NULL /* arg2_listblob_item */,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200708 ret_first_arg, f_add},
709 {"and", 2, 2, FEARG_1, NULL,
710 ret_number, f_and},
711 {"append", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100712 ret_number_bool, f_append},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200713 {"appendbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100714 ret_number_bool, f_appendbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200715 {"argc", 0, 1, 0, NULL,
716 ret_number, f_argc},
717 {"argidx", 0, 0, 0, NULL,
718 ret_number, f_argidx},
719 {"arglistid", 0, 2, 0, NULL,
720 ret_number, f_arglistid},
721 {"argv", 0, 2, 0, NULL,
722 ret_argv, f_argv},
723 {"asin", 1, 1, FEARG_1, NULL,
724 ret_float, FLOAT_FUNC(f_asin)},
725 {"assert_beeps", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100726 ret_number_bool, f_assert_beeps},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200727 {"assert_equal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100728 ret_number_bool, f_assert_equal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200729 {"assert_equalfile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100730 ret_number_bool, f_assert_equalfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200731 {"assert_exception", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100732 ret_number_bool, f_assert_exception},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200733 {"assert_fails", 1, 5, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100734 ret_number_bool, f_assert_fails},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200735 {"assert_false", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100736 ret_number_bool, f_assert_false},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200737 {"assert_inrange", 3, 4, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100738 ret_number_bool, f_assert_inrange},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200739 {"assert_match", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100740 ret_number_bool, f_assert_match},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200741 {"assert_notequal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100742 ret_number_bool, f_assert_notequal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200743 {"assert_notmatch", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100744 ret_number_bool, f_assert_notmatch},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200745 {"assert_report", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100746 ret_number_bool, f_assert_report},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200747 {"assert_true", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100748 ret_number_bool, f_assert_true},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200749 {"atan", 1, 1, FEARG_1, NULL,
750 ret_float, FLOAT_FUNC(f_atan)},
751 {"atan2", 2, 2, FEARG_1, NULL,
752 ret_float, FLOAT_FUNC(f_atan2)},
753 {"balloon_gettext", 0, 0, 0, NULL,
754 ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100755#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100756 f_balloon_gettext
757#else
758 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100759#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100760 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200761 {"balloon_show", 1, 1, FEARG_1, NULL,
762 ret_void,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100763#ifdef FEAT_BEVAL
764 f_balloon_show
765#else
766 NULL
767#endif
768 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200769 {"balloon_split", 1, 1, FEARG_1, NULL,
770 ret_list_string,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100771#if defined(FEAT_BEVAL_TERM)
772 f_balloon_split
773#else
774 NULL
775#endif
776 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200777 {"browse", 4, 4, 0, NULL,
778 ret_string, f_browse},
779 {"browsedir", 2, 2, 0, NULL,
780 ret_string, f_browsedir},
781 {"bufadd", 1, 1, FEARG_1, NULL,
782 ret_number, f_bufadd},
783 {"bufexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100784 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200785 {"buffer_exists", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100786 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200787 {"buffer_name", 0, 1, FEARG_1, NULL, // obsolete
788 ret_string, f_bufname},
789 {"buffer_number", 0, 1, FEARG_1, NULL, // obsolete
790 ret_number, f_bufnr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200791 {"buflisted", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100792 ret_number_bool, f_buflisted},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200793 {"bufload", 1, 1, FEARG_1, NULL,
794 ret_void, f_bufload},
795 {"bufloaded", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100796 ret_number_bool, f_bufloaded},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200797 {"bufname", 0, 1, FEARG_1, NULL,
798 ret_string, f_bufname},
799 {"bufnr", 0, 2, FEARG_1, NULL,
800 ret_number, f_bufnr},
801 {"bufwinid", 1, 1, FEARG_1, NULL,
802 ret_number, f_bufwinid},
803 {"bufwinnr", 1, 1, FEARG_1, NULL,
804 ret_number, f_bufwinnr},
805 {"byte2line", 1, 1, FEARG_1, NULL,
806 ret_number, f_byte2line},
807 {"byteidx", 2, 2, FEARG_1, NULL,
808 ret_number, f_byteidx},
809 {"byteidxcomp", 2, 2, FEARG_1, NULL,
810 ret_number, f_byteidxcomp},
811 {"call", 2, 3, FEARG_1, NULL,
812 ret_any, f_call},
813 {"ceil", 1, 1, FEARG_1, NULL,
814 ret_float, FLOAT_FUNC(f_ceil)},
815 {"ch_canread", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100816 ret_number_bool, JOB_FUNC(f_ch_canread)},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200817 {"ch_close", 1, 1, FEARG_1, NULL,
818 ret_void, JOB_FUNC(f_ch_close)},
819 {"ch_close_in", 1, 1, FEARG_1, NULL,
820 ret_void, JOB_FUNC(f_ch_close_in)},
821 {"ch_evalexpr", 2, 3, FEARG_1, NULL,
822 ret_any, JOB_FUNC(f_ch_evalexpr)},
823 {"ch_evalraw", 2, 3, FEARG_1, NULL,
824 ret_any, JOB_FUNC(f_ch_evalraw)},
825 {"ch_getbufnr", 2, 2, FEARG_1, NULL,
826 ret_number, JOB_FUNC(f_ch_getbufnr)},
827 {"ch_getjob", 1, 1, FEARG_1, NULL,
828 ret_job, JOB_FUNC(f_ch_getjob)},
829 {"ch_info", 1, 1, FEARG_1, NULL,
830 ret_dict_any, JOB_FUNC(f_ch_info)},
831 {"ch_log", 1, 2, FEARG_1, NULL,
832 ret_void, JOB_FUNC(f_ch_log)},
833 {"ch_logfile", 1, 2, FEARG_1, NULL,
834 ret_void, JOB_FUNC(f_ch_logfile)},
835 {"ch_open", 1, 2, FEARG_1, NULL,
836 ret_channel, JOB_FUNC(f_ch_open)},
837 {"ch_read", 1, 2, FEARG_1, NULL,
838 ret_string, JOB_FUNC(f_ch_read)},
839 {"ch_readblob", 1, 2, FEARG_1, NULL,
840 ret_blob, JOB_FUNC(f_ch_readblob)},
841 {"ch_readraw", 1, 2, FEARG_1, NULL,
842 ret_string, JOB_FUNC(f_ch_readraw)},
843 {"ch_sendexpr", 2, 3, FEARG_1, NULL,
844 ret_void, JOB_FUNC(f_ch_sendexpr)},
845 {"ch_sendraw", 2, 3, FEARG_1, NULL,
846 ret_void, JOB_FUNC(f_ch_sendraw)},
847 {"ch_setoptions", 2, 2, FEARG_1, NULL,
848 ret_void, JOB_FUNC(f_ch_setoptions)},
849 {"ch_status", 1, 2, FEARG_1, NULL,
850 ret_string, JOB_FUNC(f_ch_status)},
851 {"changenr", 0, 0, 0, NULL,
852 ret_number, f_changenr},
853 {"char2nr", 1, 2, FEARG_1, NULL,
854 ret_number, f_char2nr},
855 {"charclass", 1, 1, FEARG_1, NULL,
856 ret_number, f_charclass},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100857 {"charcol", 1, 1, FEARG_1, NULL,
858 ret_number, f_charcol},
Bram Moolenaar17793ef2020-12-28 12:56:58 +0100859 {"charidx", 2, 3, FEARG_1, NULL,
860 ret_number, f_charidx},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200861 {"chdir", 1, 1, FEARG_1, NULL,
862 ret_string, f_chdir},
863 {"cindent", 1, 1, FEARG_1, NULL,
864 ret_number, f_cindent},
865 {"clearmatches", 0, 1, FEARG_1, NULL,
866 ret_void, f_clearmatches},
867 {"col", 1, 1, FEARG_1, NULL,
868 ret_number, f_col},
869 {"complete", 2, 2, FEARG_2, NULL,
870 ret_void, f_complete},
871 {"complete_add", 1, 1, FEARG_1, NULL,
872 ret_number, f_complete_add},
873 {"complete_check", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100874 ret_number_bool, f_complete_check},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200875 {"complete_info", 0, 1, FEARG_1, NULL,
876 ret_dict_any, f_complete_info},
877 {"confirm", 1, 4, FEARG_1, NULL,
878 ret_number, f_confirm},
879 {"copy", 1, 1, FEARG_1, NULL,
880 ret_first_arg, f_copy},
881 {"cos", 1, 1, FEARG_1, NULL,
882 ret_float, FLOAT_FUNC(f_cos)},
883 {"cosh", 1, 1, FEARG_1, NULL,
884 ret_float, FLOAT_FUNC(f_cosh)},
885 {"count", 2, 4, FEARG_1, NULL,
886 ret_number, f_count},
887 {"cscope_connection",0,3, 0, NULL,
888 ret_number, f_cscope_connection},
889 {"cursor", 1, 3, FEARG_1, NULL,
890 ret_number, f_cursor},
891 {"debugbreak", 1, 1, FEARG_1, NULL,
892 ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100893#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100894 f_debugbreak
895#else
896 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200897#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100898 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200899 {"deepcopy", 1, 2, FEARG_1, NULL,
900 ret_first_arg, f_deepcopy},
901 {"delete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100902 ret_number_bool, f_delete},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200903 {"deletebufline", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100904 ret_number_bool, f_deletebufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200905 {"did_filetype", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100906 ret_number_bool, f_did_filetype},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200907 {"diff_filler", 1, 1, FEARG_1, NULL,
908 ret_number, f_diff_filler},
909 {"diff_hlID", 2, 2, FEARG_1, NULL,
910 ret_number, f_diff_hlID},
911 {"echoraw", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100912 ret_void, f_echoraw},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200913 {"empty", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100914 ret_number_bool, f_empty},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200915 {"environ", 0, 0, 0, NULL,
916 ret_dict_string, f_environ},
917 {"escape", 2, 2, FEARG_1, NULL,
918 ret_string, f_escape},
919 {"eval", 1, 1, FEARG_1, NULL,
920 ret_any, f_eval},
921 {"eventhandler", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100922 ret_number_bool, f_eventhandler},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200923 {"executable", 1, 1, FEARG_1, NULL,
924 ret_number, f_executable},
925 {"execute", 1, 2, FEARG_1, NULL,
926 ret_string, f_execute},
927 {"exepath", 1, 1, FEARG_1, NULL,
928 ret_string, f_exepath},
929 {"exists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100930 ret_number_bool, f_exists},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200931 {"exp", 1, 1, FEARG_1, NULL,
932 ret_float, FLOAT_FUNC(f_exp)},
933 {"expand", 1, 3, FEARG_1, NULL,
934 ret_any, f_expand},
935 {"expandcmd", 1, 1, FEARG_1, NULL,
936 ret_string, f_expandcmd},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100937 {"extend", 2, 3, FEARG_1, arg23_extend,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200938 ret_first_arg, f_extend},
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100939 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
940 ret_first_cont, f_extendnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200941 {"feedkeys", 1, 2, FEARG_1, NULL,
942 ret_void, f_feedkeys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200943 {"file_readable", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100944 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200945 {"filereadable", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100946 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200947 {"filewritable", 1, 1, FEARG_1, NULL,
948 ret_number, f_filewritable},
949 {"filter", 2, 2, FEARG_1, NULL,
950 ret_first_arg, f_filter},
951 {"finddir", 1, 3, FEARG_1, NULL,
952 ret_string, f_finddir},
953 {"findfile", 1, 3, FEARG_1, NULL,
954 ret_string, f_findfile},
955 {"flatten", 1, 2, FEARG_1, NULL,
956 ret_list_any, f_flatten},
957 {"float2nr", 1, 1, FEARG_1, NULL,
958 ret_number, FLOAT_FUNC(f_float2nr)},
959 {"floor", 1, 1, FEARG_1, NULL,
960 ret_float, FLOAT_FUNC(f_floor)},
961 {"fmod", 2, 2, FEARG_1, NULL,
962 ret_float, FLOAT_FUNC(f_fmod)},
963 {"fnameescape", 1, 1, FEARG_1, NULL,
964 ret_string, f_fnameescape},
965 {"fnamemodify", 2, 2, FEARG_1, NULL,
966 ret_string, f_fnamemodify},
967 {"foldclosed", 1, 1, FEARG_1, NULL,
968 ret_number, f_foldclosed},
969 {"foldclosedend", 1, 1, FEARG_1, NULL,
970 ret_number, f_foldclosedend},
971 {"foldlevel", 1, 1, FEARG_1, NULL,
972 ret_number, f_foldlevel},
973 {"foldtext", 0, 0, 0, NULL,
974 ret_string, f_foldtext},
975 {"foldtextresult", 1, 1, FEARG_1, NULL,
976 ret_string, f_foldtextresult},
977 {"foreground", 0, 0, 0, NULL,
978 ret_void, f_foreground},
979 {"funcref", 1, 3, FEARG_1, NULL,
980 ret_func_any, f_funcref},
981 {"function", 1, 3, FEARG_1, NULL,
982 ret_f_function, f_function},
983 {"garbagecollect", 0, 1, 0, NULL,
984 ret_void, f_garbagecollect},
985 {"get", 2, 3, FEARG_1, NULL,
986 ret_any, f_get},
987 {"getbufinfo", 0, 1, FEARG_1, NULL,
988 ret_list_dict_any, f_getbufinfo},
989 {"getbufline", 2, 3, FEARG_1, NULL,
990 ret_list_string, f_getbufline},
991 {"getbufvar", 2, 3, FEARG_1, NULL,
992 ret_any, f_getbufvar},
993 {"getchangelist", 0, 1, FEARG_1, NULL,
994 ret_list_any, f_getchangelist},
995 {"getchar", 0, 1, 0, NULL,
996 ret_number, f_getchar},
997 {"getcharmod", 0, 0, 0, NULL,
998 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100999 {"getcharpos", 1, 1, FEARG_1, NULL,
1000 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001001 {"getcharsearch", 0, 0, 0, NULL,
1002 ret_dict_any, f_getcharsearch},
1003 {"getcmdline", 0, 0, 0, NULL,
1004 ret_string, f_getcmdline},
1005 {"getcmdpos", 0, 0, 0, NULL,
1006 ret_number, f_getcmdpos},
1007 {"getcmdtype", 0, 0, 0, NULL,
1008 ret_string, f_getcmdtype},
1009 {"getcmdwintype", 0, 0, 0, NULL,
1010 ret_string, f_getcmdwintype},
1011 {"getcompletion", 2, 3, FEARG_1, NULL,
1012 ret_list_string, f_getcompletion},
1013 {"getcurpos", 0, 1, FEARG_1, NULL,
1014 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001015 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
1016 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001017 {"getcwd", 0, 2, FEARG_1, NULL,
1018 ret_string, f_getcwd},
1019 {"getenv", 1, 1, FEARG_1, NULL,
1020 ret_string, f_getenv},
1021 {"getfontname", 0, 1, 0, NULL,
1022 ret_string, f_getfontname},
1023 {"getfperm", 1, 1, FEARG_1, NULL,
1024 ret_string, f_getfperm},
1025 {"getfsize", 1, 1, FEARG_1, NULL,
1026 ret_number, f_getfsize},
1027 {"getftime", 1, 1, FEARG_1, NULL,
1028 ret_number, f_getftime},
1029 {"getftype", 1, 1, FEARG_1, NULL,
1030 ret_string, f_getftype},
1031 {"getimstatus", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001032 ret_number_bool, f_getimstatus},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001033 {"getjumplist", 0, 2, FEARG_1, NULL,
1034 ret_list_any, f_getjumplist},
1035 {"getline", 1, 2, FEARG_1, NULL,
1036 ret_f_getline, f_getline},
1037 {"getloclist", 1, 2, 0, NULL,
1038 ret_list_or_dict_1, f_getloclist},
1039 {"getmarklist", 0, 1, FEARG_1, NULL,
1040 ret_list_dict_any, f_getmarklist},
1041 {"getmatches", 0, 1, 0, NULL,
1042 ret_list_dict_any, f_getmatches},
1043 {"getmousepos", 0, 0, 0, NULL,
1044 ret_dict_number, f_getmousepos},
1045 {"getpid", 0, 0, 0, NULL,
1046 ret_number, f_getpid},
1047 {"getpos", 1, 1, FEARG_1, NULL,
1048 ret_list_number, f_getpos},
1049 {"getqflist", 0, 1, 0, NULL,
1050 ret_list_or_dict_0, f_getqflist},
1051 {"getreg", 0, 3, FEARG_1, NULL,
1052 ret_getreg, f_getreg},
1053 {"getreginfo", 0, 1, FEARG_1, NULL,
1054 ret_dict_any, f_getreginfo},
1055 {"getregtype", 0, 1, FEARG_1, NULL,
1056 ret_string, f_getregtype},
1057 {"gettabinfo", 0, 1, FEARG_1, NULL,
1058 ret_list_dict_any, f_gettabinfo},
1059 {"gettabvar", 2, 3, FEARG_1, NULL,
1060 ret_any, f_gettabvar},
1061 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1062 ret_any, f_gettabwinvar},
1063 {"gettagstack", 0, 1, FEARG_1, NULL,
1064 ret_dict_any, f_gettagstack},
1065 {"gettext", 1, 1, FEARG_1, NULL,
1066 ret_string, f_gettext},
1067 {"getwininfo", 0, 1, FEARG_1, NULL,
1068 ret_list_dict_any, f_getwininfo},
1069 {"getwinpos", 0, 1, FEARG_1, NULL,
1070 ret_list_number, f_getwinpos},
1071 {"getwinposx", 0, 0, 0, NULL,
1072 ret_number, f_getwinposx},
1073 {"getwinposy", 0, 0, 0, NULL,
1074 ret_number, f_getwinposy},
1075 {"getwinvar", 2, 3, FEARG_1, NULL,
1076 ret_any, f_getwinvar},
1077 {"glob", 1, 4, FEARG_1, NULL,
1078 ret_any, f_glob},
1079 {"glob2regpat", 1, 1, FEARG_1, NULL,
1080 ret_string, f_glob2regpat},
1081 {"globpath", 2, 5, FEARG_2, NULL,
1082 ret_any, f_globpath},
1083 {"has", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001084 ret_number_bool, f_has},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001085 {"has_key", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001086 ret_number_bool, f_has_key},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001087 {"haslocaldir", 0, 2, FEARG_1, NULL,
1088 ret_number, f_haslocaldir},
1089 {"hasmapto", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001090 ret_number_bool, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001091 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1092 ret_number, f_hlID},
1093 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001094 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001095 {"histadd", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001096 ret_number_bool, f_histadd},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001097 {"histdel", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001098 ret_number_bool, f_histdel},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001099 {"histget", 1, 2, FEARG_1, NULL,
1100 ret_string, f_histget},
1101 {"histnr", 1, 1, FEARG_1, NULL,
1102 ret_number, f_histnr},
1103 {"hlID", 1, 1, FEARG_1, NULL,
1104 ret_number, f_hlID},
1105 {"hlexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001106 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001107 {"hostname", 0, 0, 0, NULL,
1108 ret_string, f_hostname},
1109 {"iconv", 3, 3, FEARG_1, NULL,
1110 ret_string, f_iconv},
1111 {"indent", 1, 1, FEARG_1, NULL,
1112 ret_number, f_indent},
1113 {"index", 2, 4, FEARG_1, NULL,
1114 ret_number, f_index},
1115 {"input", 1, 3, FEARG_1, NULL,
1116 ret_string, f_input},
1117 {"inputdialog", 1, 3, FEARG_1, NULL,
1118 ret_string, f_inputdialog},
1119 {"inputlist", 1, 1, FEARG_1, NULL,
1120 ret_number, f_inputlist},
1121 {"inputrestore", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001122 ret_number_bool, f_inputrestore},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001123 {"inputsave", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001124 ret_number_bool, f_inputsave},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001125 {"inputsecret", 1, 2, FEARG_1, NULL,
1126 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001127 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001128 ret_first_arg, f_insert},
1129 {"interrupt", 0, 0, 0, NULL,
1130 ret_void, f_interrupt},
1131 {"invert", 1, 1, FEARG_1, NULL,
1132 ret_number, f_invert},
1133 {"isdirectory", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001134 ret_number_bool, f_isdirectory},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001135 {"isinf", 1, 1, FEARG_1, NULL,
1136 ret_number, MATH_FUNC(f_isinf)},
1137 {"islocked", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001138 ret_number_bool, f_islocked},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001139 {"isnan", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001140 ret_number_bool, MATH_FUNC(f_isnan)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001141 {"items", 1, 1, FEARG_1, NULL,
1142 ret_list_any, f_items},
1143 {"job_getchannel", 1, 1, FEARG_1, NULL,
1144 ret_channel, JOB_FUNC(f_job_getchannel)},
1145 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001146 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001147 {"job_setoptions", 2, 2, FEARG_1, NULL,
1148 ret_void, JOB_FUNC(f_job_setoptions)},
1149 {"job_start", 1, 2, FEARG_1, NULL,
1150 ret_job, JOB_FUNC(f_job_start)},
1151 {"job_status", 1, 1, FEARG_1, NULL,
1152 ret_string, JOB_FUNC(f_job_status)},
1153 {"job_stop", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001154 ret_number_bool, JOB_FUNC(f_job_stop)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001155 {"join", 1, 2, FEARG_1, NULL,
1156 ret_string, f_join},
1157 {"js_decode", 1, 1, FEARG_1, NULL,
1158 ret_any, f_js_decode},
1159 {"js_encode", 1, 1, FEARG_1, NULL,
1160 ret_string, f_js_encode},
1161 {"json_decode", 1, 1, FEARG_1, NULL,
1162 ret_any, f_json_decode},
1163 {"json_encode", 1, 1, FEARG_1, NULL,
1164 ret_string, f_json_encode},
1165 {"keys", 1, 1, FEARG_1, NULL,
1166 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001167 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1168 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001169 {"len", 1, 1, FEARG_1, NULL,
1170 ret_number, f_len},
1171 {"libcall", 3, 3, FEARG_3, NULL,
1172 ret_string, f_libcall},
1173 {"libcallnr", 3, 3, FEARG_3, NULL,
1174 ret_number, f_libcallnr},
1175 {"line", 1, 2, FEARG_1, NULL,
1176 ret_number, f_line},
1177 {"line2byte", 1, 1, FEARG_1, NULL,
1178 ret_number, f_line2byte},
1179 {"lispindent", 1, 1, FEARG_1, NULL,
1180 ret_number, f_lispindent},
1181 {"list2str", 1, 2, FEARG_1, NULL,
1182 ret_string, f_list2str},
1183 {"listener_add", 1, 2, FEARG_2, NULL,
1184 ret_number, f_listener_add},
1185 {"listener_flush", 0, 1, FEARG_1, NULL,
1186 ret_void, f_listener_flush},
1187 {"listener_remove", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001188 ret_number_bool, f_listener_remove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001189 {"localtime", 0, 0, 0, NULL,
1190 ret_number, f_localtime},
1191 {"log", 1, 1, FEARG_1, NULL,
1192 ret_float, FLOAT_FUNC(f_log)},
1193 {"log10", 1, 1, FEARG_1, NULL,
1194 ret_float, FLOAT_FUNC(f_log10)},
1195 {"luaeval", 1, 2, FEARG_1, NULL,
1196 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001197#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001198 f_luaeval
1199#else
1200 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001201#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001202 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001203 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001204 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001205 {"maparg", 1, 4, FEARG_1, NULL,
1206 ret_maparg, f_maparg},
1207 {"mapcheck", 1, 3, FEARG_1, NULL,
1208 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001209 {"mapnew", 2, 2, FEARG_1, NULL,
1210 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001211 {"mapset", 3, 3, FEARG_1, NULL,
1212 ret_void, f_mapset},
1213 {"match", 2, 4, FEARG_1, NULL,
1214 ret_any, f_match},
1215 {"matchadd", 2, 5, FEARG_1, NULL,
1216 ret_number, f_matchadd},
1217 {"matchaddpos", 2, 5, FEARG_1, NULL,
1218 ret_number, f_matchaddpos},
1219 {"matcharg", 1, 1, FEARG_1, NULL,
1220 ret_list_string, f_matcharg},
1221 {"matchdelete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001222 ret_number_bool, f_matchdelete},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001223 {"matchend", 2, 4, FEARG_1, NULL,
1224 ret_number, f_matchend},
1225 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1226 ret_list_string, f_matchfuzzy},
1227 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1228 ret_list_any, f_matchfuzzypos},
1229 {"matchlist", 2, 4, FEARG_1, NULL,
1230 ret_list_string, f_matchlist},
1231 {"matchstr", 2, 4, FEARG_1, NULL,
1232 ret_string, f_matchstr},
1233 {"matchstrpos", 2, 4, FEARG_1, NULL,
1234 ret_list_any, f_matchstrpos},
1235 {"max", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001236 ret_number, f_max},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001237 {"menu_info", 1, 2, FEARG_1, NULL,
1238 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001239#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001240 f_menu_info
1241#else
1242 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001243#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001244 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001245 {"min", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001246 ret_number, f_min},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001247 {"mkdir", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001248 ret_number_bool, f_mkdir},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001249 {"mode", 0, 1, FEARG_1, NULL,
1250 ret_string, f_mode},
1251 {"mzeval", 1, 1, FEARG_1, NULL,
1252 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001253#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001254 f_mzeval
1255#else
1256 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001257#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001258 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001259 {"nextnonblank", 1, 1, FEARG_1, NULL,
1260 ret_number, f_nextnonblank},
1261 {"nr2char", 1, 2, FEARG_1, NULL,
1262 ret_string, f_nr2char},
1263 {"or", 2, 2, FEARG_1, NULL,
1264 ret_number, f_or},
1265 {"pathshorten", 1, 2, FEARG_1, NULL,
1266 ret_string, f_pathshorten},
1267 {"perleval", 1, 1, FEARG_1, NULL,
1268 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001269#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001270 f_perleval
1271#else
1272 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001273#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001274 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001275 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1276 ret_number, PROP_FUNC(f_popup_atcursor)},
1277 {"popup_beval", 2, 2, FEARG_1, NULL,
1278 ret_number, PROP_FUNC(f_popup_beval)},
1279 {"popup_clear", 0, 1, 0, NULL,
1280 ret_void, PROP_FUNC(f_popup_clear)},
1281 {"popup_close", 1, 2, FEARG_1, NULL,
1282 ret_void, PROP_FUNC(f_popup_close)},
1283 {"popup_create", 2, 2, FEARG_1, NULL,
1284 ret_number, PROP_FUNC(f_popup_create)},
1285 {"popup_dialog", 2, 2, FEARG_1, NULL,
1286 ret_number, PROP_FUNC(f_popup_dialog)},
1287 {"popup_filter_menu", 2, 2, 0, NULL,
1288 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1289 {"popup_filter_yesno", 2, 2, 0, NULL,
1290 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1291 {"popup_findinfo", 0, 0, 0, NULL,
1292 ret_number, PROP_FUNC(f_popup_findinfo)},
1293 {"popup_findpreview", 0, 0, 0, NULL,
1294 ret_number, PROP_FUNC(f_popup_findpreview)},
1295 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1296 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1297 {"popup_getpos", 1, 1, FEARG_1, NULL,
1298 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1299 {"popup_hide", 1, 1, FEARG_1, NULL,
1300 ret_void, PROP_FUNC(f_popup_hide)},
1301 {"popup_list", 0, 0, 0, NULL,
1302 ret_list_number, PROP_FUNC(f_popup_list)},
1303 {"popup_locate", 2, 2, 0, NULL,
1304 ret_number, PROP_FUNC(f_popup_locate)},
1305 {"popup_menu", 2, 2, FEARG_1, NULL,
1306 ret_number, PROP_FUNC(f_popup_menu)},
1307 {"popup_move", 2, 2, FEARG_1, NULL,
1308 ret_void, PROP_FUNC(f_popup_move)},
1309 {"popup_notification", 2, 2, FEARG_1, NULL,
1310 ret_number, PROP_FUNC(f_popup_notification)},
1311 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1312 ret_void, PROP_FUNC(f_popup_setoptions)},
1313 {"popup_settext", 2, 2, FEARG_1, NULL,
1314 ret_void, PROP_FUNC(f_popup_settext)},
1315 {"popup_show", 1, 1, FEARG_1, NULL,
1316 ret_void, PROP_FUNC(f_popup_show)},
1317 {"pow", 2, 2, FEARG_1, NULL,
1318 ret_float, FLOAT_FUNC(f_pow)},
1319 {"prevnonblank", 1, 1, FEARG_1, NULL,
1320 ret_number, f_prevnonblank},
1321 {"printf", 1, 19, FEARG_2, NULL,
1322 ret_string, f_printf},
1323 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1324 ret_string, JOB_FUNC(f_prompt_getprompt)},
1325 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1326 ret_void, JOB_FUNC(f_prompt_setcallback)},
1327 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1328 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1329 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1330 ret_void, JOB_FUNC(f_prompt_setprompt)},
1331 {"prop_add", 3, 3, FEARG_1, NULL,
1332 ret_void, PROP_FUNC(f_prop_add)},
1333 {"prop_clear", 1, 3, FEARG_1, NULL,
1334 ret_void, PROP_FUNC(f_prop_clear)},
1335 {"prop_find", 1, 2, FEARG_1, NULL,
1336 ret_dict_any, PROP_FUNC(f_prop_find)},
1337 {"prop_list", 1, 2, FEARG_1, NULL,
1338 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1339 {"prop_remove", 1, 3, FEARG_1, NULL,
1340 ret_number, PROP_FUNC(f_prop_remove)},
1341 {"prop_type_add", 2, 2, FEARG_1, NULL,
1342 ret_void, PROP_FUNC(f_prop_type_add)},
1343 {"prop_type_change", 2, 2, FEARG_1, NULL,
1344 ret_void, PROP_FUNC(f_prop_type_change)},
1345 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1346 ret_void, PROP_FUNC(f_prop_type_delete)},
1347 {"prop_type_get", 1, 2, FEARG_1, NULL,
1348 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1349 {"prop_type_list", 0, 1, FEARG_1, NULL,
1350 ret_list_string, PROP_FUNC(f_prop_type_list)},
1351 {"pum_getpos", 0, 0, 0, NULL,
1352 ret_dict_number, f_pum_getpos},
1353 {"pumvisible", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001354 ret_number_bool, f_pumvisible},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001355 {"py3eval", 1, 1, FEARG_1, NULL,
1356 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001357#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001358 f_py3eval
1359#else
1360 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001361#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001362 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001363 {"pyeval", 1, 1, FEARG_1, NULL,
1364 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001365#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001366 f_pyeval
1367#else
1368 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001369#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001370 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001371 {"pyxeval", 1, 1, FEARG_1, NULL,
1372 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001373#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001374 f_pyxeval
1375#else
1376 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001377#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001378 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001379 {"rand", 0, 1, FEARG_1, NULL,
1380 ret_number, f_rand},
1381 {"range", 1, 3, FEARG_1, NULL,
1382 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001383 {"readblob", 1, 1, FEARG_1, NULL,
1384 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001385 {"readdir", 1, 3, FEARG_1, NULL,
1386 ret_list_string, f_readdir},
1387 {"readdirex", 1, 3, FEARG_1, NULL,
1388 ret_list_dict_any, f_readdirex},
1389 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001390 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001391 {"reduce", 2, 3, FEARG_1, NULL,
1392 ret_any, f_reduce},
1393 {"reg_executing", 0, 0, 0, NULL,
1394 ret_string, f_reg_executing},
1395 {"reg_recording", 0, 0, 0, NULL,
1396 ret_string, f_reg_recording},
1397 {"reltime", 0, 2, FEARG_1, NULL,
1398 ret_list_any, f_reltime},
1399 {"reltimefloat", 1, 1, FEARG_1, NULL,
1400 ret_float, FLOAT_FUNC(f_reltimefloat)},
1401 {"reltimestr", 1, 1, FEARG_1, NULL,
1402 ret_string, f_reltimestr},
1403 {"remote_expr", 2, 4, FEARG_1, NULL,
1404 ret_string, f_remote_expr},
1405 {"remote_foreground", 1, 1, FEARG_1, NULL,
1406 ret_string, f_remote_foreground},
1407 {"remote_peek", 1, 2, FEARG_1, NULL,
1408 ret_number, f_remote_peek},
1409 {"remote_read", 1, 2, FEARG_1, NULL,
1410 ret_string, f_remote_read},
1411 {"remote_send", 2, 3, FEARG_1, NULL,
1412 ret_string, f_remote_send},
1413 {"remote_startserver", 1, 1, FEARG_1, NULL,
1414 ret_void, f_remote_startserver},
1415 {"remove", 2, 3, FEARG_1, NULL,
1416 ret_remove, f_remove},
1417 {"rename", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001418 ret_number_bool, f_rename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001419 {"repeat", 2, 2, FEARG_1, NULL,
1420 ret_first_arg, f_repeat},
1421 {"resolve", 1, 1, FEARG_1, NULL,
1422 ret_string, f_resolve},
1423 {"reverse", 1, 1, FEARG_1, NULL,
1424 ret_first_arg, f_reverse},
1425 {"round", 1, 1, FEARG_1, NULL,
1426 ret_float, FLOAT_FUNC(f_round)},
1427 {"rubyeval", 1, 1, FEARG_1, NULL,
1428 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001429#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001430 f_rubyeval
1431#else
1432 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001433#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001434 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001435 {"screenattr", 2, 2, FEARG_1, NULL,
1436 ret_number, f_screenattr},
1437 {"screenchar", 2, 2, FEARG_1, NULL,
1438 ret_number, f_screenchar},
1439 {"screenchars", 2, 2, FEARG_1, NULL,
1440 ret_list_number, f_screenchars},
1441 {"screencol", 0, 0, 0, NULL,
1442 ret_number, f_screencol},
1443 {"screenpos", 3, 3, FEARG_1, NULL,
1444 ret_dict_number, f_screenpos},
1445 {"screenrow", 0, 0, 0, NULL,
1446 ret_number, f_screenrow},
1447 {"screenstring", 2, 2, FEARG_1, NULL,
1448 ret_string, f_screenstring},
1449 {"search", 1, 5, FEARG_1, NULL,
1450 ret_number, f_search},
1451 {"searchcount", 0, 1, FEARG_1, NULL,
1452 ret_dict_any, f_searchcount},
1453 {"searchdecl", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001454 ret_number_bool, f_searchdecl},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001455 {"searchpair", 3, 7, 0, NULL,
1456 ret_number, f_searchpair},
1457 {"searchpairpos", 3, 7, 0, NULL,
1458 ret_list_number, f_searchpairpos},
1459 {"searchpos", 1, 5, FEARG_1, NULL,
1460 ret_list_number, f_searchpos},
1461 {"server2client", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001462 ret_number_bool, f_server2client},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001463 {"serverlist", 0, 0, 0, NULL,
1464 ret_string, f_serverlist},
1465 {"setbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001466 ret_number_bool, f_setbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001467 {"setbufvar", 3, 3, FEARG_3, NULL,
1468 ret_void, f_setbufvar},
1469 {"setcellwidths", 1, 1, FEARG_1, NULL,
1470 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001471 {"setcharpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001472 ret_number_bool, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001473 {"setcharsearch", 1, 1, FEARG_1, NULL,
1474 ret_void, f_setcharsearch},
1475 {"setcmdpos", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001476 ret_number_bool, f_setcmdpos},
1477 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1478 ret_number_bool, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001479 {"setenv", 2, 2, FEARG_2, NULL,
1480 ret_void, f_setenv},
1481 {"setfperm", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001482 ret_number_bool, f_setfperm},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001483 {"setline", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001484 ret_number_bool, f_setline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001485 {"setloclist", 2, 4, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001486 ret_number_bool, f_setloclist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001487 {"setmatches", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001488 ret_number_bool, f_setmatches},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001489 {"setpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001490 ret_number_bool, f_setpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001491 {"setqflist", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001492 ret_number_bool, f_setqflist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001493 {"setreg", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001494 ret_number_bool, f_setreg},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001495 {"settabvar", 3, 3, FEARG_3, NULL,
1496 ret_void, f_settabvar},
1497 {"settabwinvar", 4, 4, FEARG_4, NULL,
1498 ret_void, f_settabwinvar},
1499 {"settagstack", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001500 ret_number_bool, f_settagstack},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001501 {"setwinvar", 3, 3, FEARG_3, NULL,
1502 ret_void, f_setwinvar},
1503 {"sha256", 1, 1, FEARG_1, NULL,
1504 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001505#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001506 f_sha256
1507#else
1508 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001509#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001510 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001511 {"shellescape", 1, 2, FEARG_1, NULL,
1512 ret_string, f_shellescape},
1513 {"shiftwidth", 0, 1, FEARG_1, NULL,
1514 ret_number, f_shiftwidth},
1515 {"sign_define", 1, 2, FEARG_1, NULL,
1516 ret_any, SIGN_FUNC(f_sign_define)},
1517 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1518 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1519 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1520 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1521 {"sign_jump", 3, 3, FEARG_1, NULL,
1522 ret_number, SIGN_FUNC(f_sign_jump)},
1523 {"sign_place", 4, 5, FEARG_1, NULL,
1524 ret_number, SIGN_FUNC(f_sign_place)},
1525 {"sign_placelist", 1, 1, FEARG_1, NULL,
1526 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1527 {"sign_undefine", 0, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001528 ret_number_bool, SIGN_FUNC(f_sign_undefine)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001529 {"sign_unplace", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001530 ret_number_bool, SIGN_FUNC(f_sign_unplace)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001531 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1532 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1533 {"simplify", 1, 1, FEARG_1, NULL,
1534 ret_string, f_simplify},
1535 {"sin", 1, 1, FEARG_1, NULL,
1536 ret_float, FLOAT_FUNC(f_sin)},
1537 {"sinh", 1, 1, FEARG_1, NULL,
1538 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001539 {"slice", 2, 3, FEARG_1, NULL,
1540 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001541 {"sort", 1, 3, FEARG_1, NULL,
1542 ret_first_arg, f_sort},
1543 {"sound_clear", 0, 0, 0, NULL,
1544 ret_void, SOUND_FUNC(f_sound_clear)},
1545 {"sound_playevent", 1, 2, FEARG_1, NULL,
1546 ret_number, SOUND_FUNC(f_sound_playevent)},
1547 {"sound_playfile", 1, 2, FEARG_1, NULL,
1548 ret_number, SOUND_FUNC(f_sound_playfile)},
1549 {"sound_stop", 1, 1, FEARG_1, NULL,
1550 ret_void, SOUND_FUNC(f_sound_stop)},
1551 {"soundfold", 1, 1, FEARG_1, NULL,
1552 ret_string, f_soundfold},
1553 {"spellbadword", 0, 1, FEARG_1, NULL,
1554 ret_list_string, f_spellbadword},
1555 {"spellsuggest", 1, 3, FEARG_1, NULL,
1556 ret_list_string, f_spellsuggest},
1557 {"split", 1, 3, FEARG_1, NULL,
1558 ret_list_string, f_split},
1559 {"sqrt", 1, 1, FEARG_1, NULL,
1560 ret_float, FLOAT_FUNC(f_sqrt)},
1561 {"srand", 0, 1, FEARG_1, NULL,
1562 ret_list_number, f_srand},
1563 {"state", 0, 1, FEARG_1, NULL,
1564 ret_string, f_state},
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001565 {"str2float", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001566 ret_float, FLOAT_FUNC(f_str2float)},
1567 {"str2list", 1, 2, FEARG_1, NULL,
1568 ret_list_number, f_str2list},
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001569 {"str2nr", 1, 3, FEARG_1, arg3_string_nr_bool,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001570 ret_number, f_str2nr},
1571 {"strcharpart", 2, 3, FEARG_1, NULL,
1572 ret_string, f_strcharpart},
1573 {"strchars", 1, 2, FEARG_1, NULL,
1574 ret_number, f_strchars},
1575 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1576 ret_number, f_strdisplaywidth},
1577 {"strftime", 1, 2, FEARG_1, NULL,
1578 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001579#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001580 f_strftime
1581#else
1582 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001583#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001584 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001585 {"strgetchar", 2, 2, FEARG_1, NULL,
1586 ret_number, f_strgetchar},
1587 {"stridx", 2, 3, FEARG_1, NULL,
1588 ret_number, f_stridx},
1589 {"string", 1, 1, FEARG_1, NULL,
1590 ret_string, f_string},
1591 {"strlen", 1, 1, FEARG_1, NULL,
1592 ret_number, f_strlen},
1593 {"strpart", 2, 4, FEARG_1, NULL,
1594 ret_string, f_strpart},
1595 {"strptime", 2, 2, FEARG_1, NULL,
1596 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001597#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001598 f_strptime
1599#else
1600 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001601#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001602 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001603 {"strridx", 2, 3, FEARG_1, NULL,
1604 ret_number, f_strridx},
1605 {"strtrans", 1, 1, FEARG_1, NULL,
1606 ret_string, f_strtrans},
1607 {"strwidth", 1, 1, FEARG_1, NULL,
1608 ret_number, f_strwidth},
1609 {"submatch", 1, 2, FEARG_1, NULL,
1610 ret_string, f_submatch},
1611 {"substitute", 4, 4, FEARG_1, NULL,
1612 ret_string, f_substitute},
1613 {"swapinfo", 1, 1, FEARG_1, NULL,
1614 ret_dict_any, f_swapinfo},
1615 {"swapname", 1, 1, FEARG_1, NULL,
1616 ret_string, f_swapname},
1617 {"synID", 3, 3, 0, NULL,
1618 ret_number, f_synID},
1619 {"synIDattr", 2, 3, FEARG_1, NULL,
1620 ret_string, f_synIDattr},
1621 {"synIDtrans", 1, 1, FEARG_1, NULL,
1622 ret_number, f_synIDtrans},
1623 {"synconcealed", 2, 2, 0, NULL,
1624 ret_list_any, f_synconcealed},
1625 {"synstack", 2, 2, 0, NULL,
1626 ret_list_number, f_synstack},
1627 {"system", 1, 2, FEARG_1, NULL,
1628 ret_string, f_system},
1629 {"systemlist", 1, 2, FEARG_1, NULL,
1630 ret_list_string, f_systemlist},
1631 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1632 ret_list_number, f_tabpagebuflist},
1633 {"tabpagenr", 0, 1, 0, NULL,
1634 ret_number, f_tabpagenr},
1635 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1636 ret_number, f_tabpagewinnr},
1637 {"tagfiles", 0, 0, 0, NULL,
1638 ret_list_string, f_tagfiles},
1639 {"taglist", 1, 2, FEARG_1, NULL,
1640 ret_list_dict_any, f_taglist},
1641 {"tan", 1, 1, FEARG_1, NULL,
1642 ret_float, FLOAT_FUNC(f_tan)},
1643 {"tanh", 1, 1, FEARG_1, NULL,
1644 ret_float, FLOAT_FUNC(f_tanh)},
1645 {"tempname", 0, 0, 0, NULL,
1646 ret_string, f_tempname},
1647 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1648 ret_number, TERM_FUNC(f_term_dumpdiff)},
1649 {"term_dumpload", 1, 2, FEARG_1, NULL,
1650 ret_number, TERM_FUNC(f_term_dumpload)},
1651 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1652 ret_void, TERM_FUNC(f_term_dumpwrite)},
1653 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1654 ret_number, TERM_FUNC(f_term_getaltscreen)},
1655 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1656 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001657#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001658 f_term_getansicolors
1659#else
1660 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001661#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001662 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001663 {"term_getattr", 2, 2, FEARG_1, NULL,
1664 ret_number, TERM_FUNC(f_term_getattr)},
1665 {"term_getcursor", 1, 1, FEARG_1, NULL,
1666 ret_list_any, TERM_FUNC(f_term_getcursor)},
1667 {"term_getjob", 1, 1, FEARG_1, NULL,
1668 ret_job, TERM_FUNC(f_term_getjob)},
1669 {"term_getline", 2, 2, FEARG_1, NULL,
1670 ret_string, TERM_FUNC(f_term_getline)},
1671 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1672 ret_number, TERM_FUNC(f_term_getscrolled)},
1673 {"term_getsize", 1, 1, FEARG_1, NULL,
1674 ret_list_number, TERM_FUNC(f_term_getsize)},
1675 {"term_getstatus", 1, 1, FEARG_1, NULL,
1676 ret_string, TERM_FUNC(f_term_getstatus)},
1677 {"term_gettitle", 1, 1, FEARG_1, NULL,
1678 ret_string, TERM_FUNC(f_term_gettitle)},
1679 {"term_gettty", 1, 2, FEARG_1, NULL,
1680 ret_string, TERM_FUNC(f_term_gettty)},
1681 {"term_list", 0, 0, 0, NULL,
1682 ret_list_number, TERM_FUNC(f_term_list)},
1683 {"term_scrape", 2, 2, FEARG_1, NULL,
1684 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1685 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1686 ret_void, TERM_FUNC(f_term_sendkeys)},
1687 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1688 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001689#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001690 f_term_setansicolors
1691#else
1692 NULL
1693#endif
1694 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001695 {"term_setapi", 2, 2, FEARG_1, NULL,
1696 ret_void, TERM_FUNC(f_term_setapi)},
1697 {"term_setkill", 2, 2, FEARG_1, NULL,
1698 ret_void, TERM_FUNC(f_term_setkill)},
1699 {"term_setrestore", 2, 2, FEARG_1, NULL,
1700 ret_void, TERM_FUNC(f_term_setrestore)},
1701 {"term_setsize", 3, 3, FEARG_1, NULL,
1702 ret_void, TERM_FUNC(f_term_setsize)},
1703 {"term_start", 1, 2, FEARG_1, NULL,
1704 ret_number, TERM_FUNC(f_term_start)},
1705 {"term_wait", 1, 2, FEARG_1, NULL,
1706 ret_void, TERM_FUNC(f_term_wait)},
1707 {"terminalprops", 0, 0, 0, NULL,
1708 ret_dict_string, f_terminalprops},
1709 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1710 ret_void, f_test_alloc_fail},
1711 {"test_autochdir", 0, 0, 0, NULL,
1712 ret_void, f_test_autochdir},
1713 {"test_feedinput", 1, 1, FEARG_1, NULL,
1714 ret_void, f_test_feedinput},
1715 {"test_garbagecollect_now", 0, 0, 0, NULL,
1716 ret_void, f_test_garbagecollect_now},
1717 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1718 ret_void, f_test_garbagecollect_soon},
1719 {"test_getvalue", 1, 1, FEARG_1, NULL,
1720 ret_number, f_test_getvalue},
1721 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1722 ret_void, f_test_ignore_error},
1723 {"test_null_blob", 0, 0, 0, NULL,
1724 ret_blob, f_test_null_blob},
1725 {"test_null_channel", 0, 0, 0, NULL,
1726 ret_channel, JOB_FUNC(f_test_null_channel)},
1727 {"test_null_dict", 0, 0, 0, NULL,
1728 ret_dict_any, f_test_null_dict},
1729 {"test_null_function", 0, 0, 0, NULL,
1730 ret_func_any, f_test_null_function},
1731 {"test_null_job", 0, 0, 0, NULL,
1732 ret_job, JOB_FUNC(f_test_null_job)},
1733 {"test_null_list", 0, 0, 0, NULL,
1734 ret_list_any, f_test_null_list},
1735 {"test_null_partial", 0, 0, 0, NULL,
1736 ret_func_any, f_test_null_partial},
1737 {"test_null_string", 0, 0, 0, NULL,
1738 ret_string, f_test_null_string},
1739 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1740 ret_void, f_test_option_not_set},
1741 {"test_override", 2, 2, FEARG_2, NULL,
1742 ret_void, f_test_override},
1743 {"test_refcount", 1, 1, FEARG_1, NULL,
1744 ret_number, f_test_refcount},
1745 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1746 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001747#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001748 f_test_scrollbar
1749#else
1750 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001751#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001752 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001753 {"test_setmouse", 2, 2, 0, NULL,
1754 ret_void, f_test_setmouse},
1755 {"test_settime", 1, 1, FEARG_1, NULL,
1756 ret_void, f_test_settime},
1757 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1758 ret_void, f_test_srand_seed},
1759 {"test_unknown", 0, 0, 0, NULL,
1760 ret_any, f_test_unknown},
1761 {"test_void", 0, 0, 0, NULL,
1762 ret_void, f_test_void},
1763 {"timer_info", 0, 1, FEARG_1, NULL,
1764 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1765 {"timer_pause", 2, 2, FEARG_1, NULL,
1766 ret_void, TIMER_FUNC(f_timer_pause)},
1767 {"timer_start", 2, 3, FEARG_1, NULL,
1768 ret_number, TIMER_FUNC(f_timer_start)},
1769 {"timer_stop", 1, 1, FEARG_1, NULL,
1770 ret_void, TIMER_FUNC(f_timer_stop)},
1771 {"timer_stopall", 0, 0, 0, NULL,
1772 ret_void, TIMER_FUNC(f_timer_stopall)},
1773 {"tolower", 1, 1, FEARG_1, NULL,
1774 ret_string, f_tolower},
1775 {"toupper", 1, 1, FEARG_1, NULL,
1776 ret_string, f_toupper},
1777 {"tr", 3, 3, FEARG_1, NULL,
1778 ret_string, f_tr},
1779 {"trim", 1, 3, FEARG_1, NULL,
1780 ret_string, f_trim},
1781 {"trunc", 1, 1, FEARG_1, NULL,
1782 ret_float, FLOAT_FUNC(f_trunc)},
1783 {"type", 1, 1, FEARG_1, NULL,
1784 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001785 {"typename", 1, 1, FEARG_1, NULL,
1786 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001787 {"undofile", 1, 1, FEARG_1, NULL,
1788 ret_string, f_undofile},
1789 {"undotree", 0, 0, 0, NULL,
1790 ret_dict_any, f_undotree},
1791 {"uniq", 1, 3, FEARG_1, NULL,
1792 ret_list_any, f_uniq},
1793 {"values", 1, 1, FEARG_1, NULL,
1794 ret_list_any, f_values},
1795 {"virtcol", 1, 1, FEARG_1, NULL,
1796 ret_number, f_virtcol},
1797 {"visualmode", 0, 1, 0, NULL,
1798 ret_string, f_visualmode},
1799 {"wildmenumode", 0, 0, 0, NULL,
1800 ret_number, f_wildmenumode},
1801 {"win_execute", 2, 3, FEARG_2, NULL,
1802 ret_string, f_win_execute},
1803 {"win_findbuf", 1, 1, FEARG_1, NULL,
1804 ret_list_number, f_win_findbuf},
1805 {"win_getid", 0, 2, FEARG_1, NULL,
1806 ret_number, f_win_getid},
1807 {"win_gettype", 0, 1, FEARG_1, NULL,
1808 ret_string, f_win_gettype},
1809 {"win_gotoid", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001810 ret_number_bool, f_win_gotoid},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001811 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1812 ret_list_number, f_win_id2tabwin},
1813 {"win_id2win", 1, 1, FEARG_1, NULL,
1814 ret_number, f_win_id2win},
1815 {"win_screenpos", 1, 1, FEARG_1, NULL,
1816 ret_list_number, f_win_screenpos},
1817 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001818 ret_number_bool, f_win_splitmove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001819 {"winbufnr", 1, 1, FEARG_1, NULL,
1820 ret_number, f_winbufnr},
1821 {"wincol", 0, 0, 0, NULL,
1822 ret_number, f_wincol},
1823 {"windowsversion", 0, 0, 0, NULL,
1824 ret_string, f_windowsversion},
1825 {"winheight", 1, 1, FEARG_1, NULL,
1826 ret_number, f_winheight},
1827 {"winlayout", 0, 1, FEARG_1, NULL,
1828 ret_list_any, f_winlayout},
1829 {"winline", 0, 0, 0, NULL,
1830 ret_number, f_winline},
1831 {"winnr", 0, 1, FEARG_1, NULL,
1832 ret_number, f_winnr},
1833 {"winrestcmd", 0, 0, 0, NULL,
1834 ret_string, f_winrestcmd},
1835 {"winrestview", 1, 1, FEARG_1, NULL,
1836 ret_void, f_winrestview},
1837 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001838 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001839 {"winwidth", 1, 1, FEARG_1, NULL,
1840 ret_number, f_winwidth},
1841 {"wordcount", 0, 0, 0, NULL,
1842 ret_dict_number, f_wordcount},
1843 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001844 ret_number_bool, f_writefile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001845 {"xor", 2, 2, FEARG_1, NULL,
1846 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001847};
1848
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001849/*
1850 * Function given to ExpandGeneric() to obtain the list of internal
1851 * or user defined function names.
1852 */
1853 char_u *
1854get_function_name(expand_T *xp, int idx)
1855{
1856 static int intidx = -1;
1857 char_u *name;
1858
1859 if (idx == 0)
1860 intidx = -1;
1861 if (intidx < 0)
1862 {
1863 name = get_user_func_name(xp, idx);
1864 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001865 {
1866 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1867 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001868 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001869 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001870 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001871 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001872 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001873 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001874 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001875 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001876 STRCAT(IObuff, ")");
1877 return IObuff;
1878 }
1879
1880 return NULL;
1881}
1882
1883/*
1884 * Function given to ExpandGeneric() to obtain the list of internal or
1885 * user defined variable or function names.
1886 */
1887 char_u *
1888get_expr_name(expand_T *xp, int idx)
1889{
1890 static int intidx = -1;
1891 char_u *name;
1892
1893 if (idx == 0)
1894 intidx = -1;
1895 if (intidx < 0)
1896 {
1897 name = get_function_name(xp, idx);
1898 if (name != NULL)
1899 return name;
1900 }
1901 return get_user_var_name(xp, ++intidx);
1902}
1903
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001904/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001905 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001906 * Return index, or -1 if not found or "implemented" is TRUE and the function
1907 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001908 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001909 static int
1910find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001911{
1912 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001913 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001914 int cmp;
1915 int x;
1916
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001917 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001918
1919 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001920 while (first <= last)
1921 {
1922 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001923 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001924 if (cmp < 0)
1925 last = x - 1;
1926 else if (cmp > 0)
1927 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001928 else if (implemented && global_functions[x].f_func == NULL)
1929 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001930 else
1931 return x;
1932 }
1933 return -1;
1934}
1935
Bram Moolenaar15c47602020-03-26 22:16:48 +01001936/*
1937 * Find internal function "name" in table "global_functions".
1938 * Return index, or -1 if not found or the function is not implemented.
1939 */
1940 int
1941find_internal_func(char_u *name)
1942{
1943 return find_internal_func_opt(name, TRUE);
1944}
1945
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001946 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001947has_internal_func(char_u *name)
1948{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001949 return find_internal_func_opt(name, TRUE) >= 0;
1950}
1951
1952 static int
1953has_internal_func_name(char_u *name)
1954{
1955 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001956}
1957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 char *
1959internal_func_name(int idx)
1960{
1961 return global_functions[idx].f_name;
1962}
1963
Bram Moolenaar94738d82020-10-21 14:25:07 +02001964/*
1965 * Check the argument types for builting function "idx".
1966 * Uses the list of types on the type stack: "types".
1967 * Return FAIL and gives an error message when a type is wrong.
1968 */
1969 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001970internal_func_check_arg_types(
1971 type_T **types,
1972 int idx,
1973 int argcount,
1974 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001975{
1976 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1977 int i;
1978
1979 if (argchecks != NULL)
1980 {
1981 argcontext_T context;
1982
1983 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001984 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001985 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001986 for (i = 0; i < argcount; ++i)
1987 if (argchecks[i] != NULL)
1988 {
1989 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001990 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001991 return FAIL;
1992 }
1993 }
1994 return OK;
1995}
1996
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001997/*
1998 * Call the "f_retfunc" function to obtain the return type of function "idx".
1999 * "argtypes" is the list of argument types or NULL when there are no
2000 * arguments.
2001 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002003internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002005 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006}
2007
2008/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002009 * Return TRUE if "idx" is for the map() function.
2010 */
2011 int
2012internal_func_is_map(int idx)
2013{
2014 return global_functions[idx].f_func == f_map;
2015}
2016
2017/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002018 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002019 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2020 * first argument, 2 if method base is second argument, etc. 9 if method base
2021 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 */
2023 int
2024check_internal_func(int idx, int argcount)
2025{
2026 int res;
2027 char *name;
2028
2029 if (argcount < global_functions[idx].f_min_argc)
2030 res = FCERR_TOOFEW;
2031 else if (argcount > global_functions[idx].f_max_argc)
2032 res = FCERR_TOOMANY;
2033 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002034 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035
2036 name = internal_func_name(idx);
2037 if (res == FCERR_TOOMANY)
2038 semsg(_(e_toomanyarg), name);
2039 else
2040 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002041 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042}
2043
Bram Moolenaarac92e252019-08-03 21:58:38 +02002044 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002045call_internal_func(
2046 char_u *name,
2047 int argcount,
2048 typval_T *argvars,
2049 typval_T *rettv)
2050{
2051 int i;
2052
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002053 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002054 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002055 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002056 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002057 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002058 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002059 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002060 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002061 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002062 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002063}
2064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 void
2066call_internal_func_by_idx(
2067 int idx,
2068 typval_T *argvars,
2069 typval_T *rettv)
2070{
2071 global_functions[idx].f_func(argvars, rettv);
2072}
2073
Bram Moolenaarac92e252019-08-03 21:58:38 +02002074/*
2075 * Invoke a method for base->method().
2076 */
2077 int
2078call_internal_method(
2079 char_u *name,
2080 int argcount,
2081 typval_T *argvars,
2082 typval_T *rettv,
2083 typval_T *basetv)
2084{
2085 int i;
2086 int fi;
2087 typval_T argv[MAX_FUNC_ARGS + 1];
2088
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002089 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002090 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002091 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002092 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002093 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002094 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002095 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002096 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002097 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002098
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002099 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002100 {
2101 // base value goes last
2102 for (i = 0; i < argcount; ++i)
2103 argv[i] = argvars[i];
2104 argv[argcount] = *basetv;
2105 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002106 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002107 {
2108 // base value goes second
2109 argv[0] = argvars[0];
2110 argv[1] = *basetv;
2111 for (i = 1; i < argcount; ++i)
2112 argv[i + 1] = argvars[i];
2113 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002114 else if (global_functions[fi].f_argtype == FEARG_3)
2115 {
2116 // base value goes third
2117 argv[0] = argvars[0];
2118 argv[1] = argvars[1];
2119 argv[2] = *basetv;
2120 for (i = 2; i < argcount; ++i)
2121 argv[i + 1] = argvars[i];
2122 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002123 else if (global_functions[fi].f_argtype == FEARG_4)
2124 {
2125 // base value goes fourth
2126 argv[0] = argvars[0];
2127 argv[1] = argvars[1];
2128 argv[2] = argvars[2];
2129 argv[3] = *basetv;
2130 for (i = 3; i < argcount; ++i)
2131 argv[i + 1] = argvars[i];
2132 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002133 else
2134 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002135 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002136 argv[0] = *basetv;
2137 for (i = 0; i < argcount; ++i)
2138 argv[i + 1] = argvars[i];
2139 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002140 argv[argcount + 1].v_type = VAR_UNKNOWN;
2141
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002142 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002143 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002144}
2145
2146/*
2147 * Return TRUE for a non-zero Number and a non-empty String.
2148 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002149 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002150non_zero_arg(typval_T *argvars)
2151{
2152 return ((argvars[0].v_type == VAR_NUMBER
2153 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002154 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002155 && argvars[0].vval.v_number == VVAL_TRUE)
2156 || (argvars[0].v_type == VAR_STRING
2157 && argvars[0].vval.v_string != NULL
2158 && *argvars[0].vval.v_string != NUL));
2159}
2160
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002161#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002162/*
2163 * Get the float value of "argvars[0]" into "f".
2164 * Returns FAIL when the argument is not a Number or Float.
2165 */
2166 static int
2167get_float_arg(typval_T *argvars, float_T *f)
2168{
2169 if (argvars[0].v_type == VAR_FLOAT)
2170 {
2171 *f = argvars[0].vval.v_float;
2172 return OK;
2173 }
2174 if (argvars[0].v_type == VAR_NUMBER)
2175 {
2176 *f = (float_T)argvars[0].vval.v_number;
2177 return OK;
2178 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002179 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002180 return FAIL;
2181}
2182
2183/*
2184 * "abs(expr)" function
2185 */
2186 static void
2187f_abs(typval_T *argvars, typval_T *rettv)
2188{
2189 if (argvars[0].v_type == VAR_FLOAT)
2190 {
2191 rettv->v_type = VAR_FLOAT;
2192 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
2193 }
2194 else
2195 {
2196 varnumber_T n;
2197 int error = FALSE;
2198
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002199 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002200 if (error)
2201 rettv->vval.v_number = -1;
2202 else if (n > 0)
2203 rettv->vval.v_number = n;
2204 else
2205 rettv->vval.v_number = -n;
2206 }
2207}
2208
2209/*
2210 * "acos()" function
2211 */
2212 static void
2213f_acos(typval_T *argvars, typval_T *rettv)
2214{
2215 float_T f = 0.0;
2216
2217 rettv->v_type = VAR_FLOAT;
2218 if (get_float_arg(argvars, &f) == OK)
2219 rettv->vval.v_float = acos(f);
2220 else
2221 rettv->vval.v_float = 0.0;
2222}
2223#endif
2224
2225/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002226 * "and(expr, expr)" function
2227 */
2228 static void
2229f_and(typval_T *argvars, typval_T *rettv)
2230{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002231 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2232 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002233}
2234
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002235#ifdef FEAT_FLOAT
2236/*
2237 * "asin()" function
2238 */
2239 static void
2240f_asin(typval_T *argvars, typval_T *rettv)
2241{
2242 float_T f = 0.0;
2243
2244 rettv->v_type = VAR_FLOAT;
2245 if (get_float_arg(argvars, &f) == OK)
2246 rettv->vval.v_float = asin(f);
2247 else
2248 rettv->vval.v_float = 0.0;
2249}
2250
2251/*
2252 * "atan()" function
2253 */
2254 static void
2255f_atan(typval_T *argvars, typval_T *rettv)
2256{
2257 float_T f = 0.0;
2258
2259 rettv->v_type = VAR_FLOAT;
2260 if (get_float_arg(argvars, &f) == OK)
2261 rettv->vval.v_float = atan(f);
2262 else
2263 rettv->vval.v_float = 0.0;
2264}
2265
2266/*
2267 * "atan2()" function
2268 */
2269 static void
2270f_atan2(typval_T *argvars, typval_T *rettv)
2271{
2272 float_T fx = 0.0, fy = 0.0;
2273
2274 rettv->v_type = VAR_FLOAT;
2275 if (get_float_arg(argvars, &fx) == OK
2276 && get_float_arg(&argvars[1], &fy) == OK)
2277 rettv->vval.v_float = atan2(fx, fy);
2278 else
2279 rettv->vval.v_float = 0.0;
2280}
2281#endif
2282
2283/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002284 * "balloon_show()" function
2285 */
2286#ifdef FEAT_BEVAL
2287 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002288f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2289{
2290 rettv->v_type = VAR_STRING;
2291 if (balloonEval != NULL)
2292 {
2293 if (balloonEval->msg == NULL)
2294 rettv->vval.v_string = NULL;
2295 else
2296 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2297 }
2298}
2299
2300 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002301f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2302{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002303 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002304 {
2305 if (argvars[0].v_type == VAR_LIST
2306# ifdef FEAT_GUI
2307 && !gui.in_use
2308# endif
2309 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002310 {
2311 list_T *l = argvars[0].vval.v_list;
2312
2313 // empty list removes the balloon
2314 post_balloon(balloonEval, NULL,
2315 l == NULL || l->lv_len == 0 ? NULL : l);
2316 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002317 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002318 {
2319 char_u *mesg = tv_get_string_chk(&argvars[0]);
2320
2321 if (mesg != NULL)
2322 // empty string removes the balloon
2323 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2324 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002325 }
2326}
2327
Bram Moolenaar669a8282017-11-19 20:13:05 +01002328# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002329 static void
2330f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2331{
2332 if (rettv_list_alloc(rettv) == OK)
2333 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002334 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002335
2336 if (msg != NULL)
2337 {
2338 pumitem_T *array;
2339 int size = split_message(msg, &array);
2340 int i;
2341
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002342 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002343 for (i = 1; i < size - 1; ++i)
2344 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002345 while (size > 0)
2346 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002347 vim_free(array);
2348 }
2349 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002350}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002351# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002352#endif
2353
2354/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002355 * Get the buffer from "arg" and give an error and return NULL if it is not
2356 * valid.
2357 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002358 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002359get_buf_arg(typval_T *arg)
2360{
2361 buf_T *buf;
2362
2363 ++emsg_off;
2364 buf = tv_get_buf(arg, FALSE);
2365 --emsg_off;
2366 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002367 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002368 return buf;
2369}
2370
2371/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002372 * "byte2line(byte)" function
2373 */
2374 static void
2375f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2376{
2377#ifndef FEAT_BYTEOFF
2378 rettv->vval.v_number = -1;
2379#else
2380 long boff = 0;
2381
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002382 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002383 if (boff < 0)
2384 rettv->vval.v_number = -1;
2385 else
2386 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2387 (linenr_T)0, &boff);
2388#endif
2389}
2390
2391 static void
2392byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2393{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002394 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002395 char_u *str;
2396 varnumber_T idx;
2397
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002398 str = tv_get_string_chk(&argvars[0]);
2399 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002400 rettv->vval.v_number = -1;
2401 if (str == NULL || idx < 0)
2402 return;
2403
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002404 t = str;
2405 for ( ; idx > 0; idx--)
2406 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002407 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002408 return;
2409 if (enc_utf8 && comp)
2410 t += utf_ptr2len(t);
2411 else
2412 t += (*mb_ptr2len)(t);
2413 }
2414 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002415}
2416
2417/*
2418 * "byteidx()" function
2419 */
2420 static void
2421f_byteidx(typval_T *argvars, typval_T *rettv)
2422{
2423 byteidx(argvars, rettv, FALSE);
2424}
2425
2426/*
2427 * "byteidxcomp()" function
2428 */
2429 static void
2430f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2431{
2432 byteidx(argvars, rettv, TRUE);
2433}
2434
2435/*
2436 * "call(func, arglist [, dict])" function
2437 */
2438 static void
2439f_call(typval_T *argvars, typval_T *rettv)
2440{
2441 char_u *func;
2442 partial_T *partial = NULL;
2443 dict_T *selfdict = NULL;
2444
2445 if (argvars[1].v_type != VAR_LIST)
2446 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002447 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002448 return;
2449 }
2450 if (argvars[1].vval.v_list == NULL)
2451 return;
2452
2453 if (argvars[0].v_type == VAR_FUNC)
2454 func = argvars[0].vval.v_string;
2455 else if (argvars[0].v_type == VAR_PARTIAL)
2456 {
2457 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002458 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002459 }
2460 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002461 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002462 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002463 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002464
2465 if (argvars[2].v_type != VAR_UNKNOWN)
2466 {
2467 if (argvars[2].v_type != VAR_DICT)
2468 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002469 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002470 return;
2471 }
2472 selfdict = argvars[2].vval.v_dict;
2473 }
2474
2475 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2476}
2477
2478#ifdef FEAT_FLOAT
2479/*
2480 * "ceil({float})" function
2481 */
2482 static void
2483f_ceil(typval_T *argvars, typval_T *rettv)
2484{
2485 float_T f = 0.0;
2486
2487 rettv->v_type = VAR_FLOAT;
2488 if (get_float_arg(argvars, &f) == OK)
2489 rettv->vval.v_float = ceil(f);
2490 else
2491 rettv->vval.v_float = 0.0;
2492}
2493#endif
2494
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002495/*
2496 * "changenr()" function
2497 */
2498 static void
2499f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2500{
2501 rettv->vval.v_number = curbuf->b_u_seq_cur;
2502}
2503
2504/*
2505 * "char2nr(string)" function
2506 */
2507 static void
2508f_char2nr(typval_T *argvars, typval_T *rettv)
2509{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002510 if (has_mbyte)
2511 {
2512 int utf8 = 0;
2513
2514 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002515 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002516
2517 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002518 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002519 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002520 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002521 }
2522 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002523 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002524}
2525
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002526/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002527 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2528 * returns the character index of the column. Otherwise, returns the byte index
2529 * of the column.
2530 */
2531 static void
2532get_col(typval_T *argvars, typval_T *rettv, int charcol)
2533{
2534 colnr_T col = 0;
2535 pos_T *fp;
2536 int fnum = curbuf->b_fnum;
2537
2538 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2539 if (fp != NULL && fnum == curbuf->b_fnum)
2540 {
2541 if (fp->col == MAXCOL)
2542 {
2543 // '> can be MAXCOL, get the length of the line then
2544 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2545 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2546 else
2547 col = MAXCOL;
2548 }
2549 else
2550 {
2551 col = fp->col + 1;
2552 // col(".") when the cursor is on the NUL at the end of the line
2553 // because of "coladd" can be seen as an extra column.
2554 if (virtual_active() && fp == &curwin->w_cursor)
2555 {
2556 char_u *p = ml_get_cursor();
2557
2558 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2559 curwin->w_virtcol - curwin->w_cursor.coladd))
2560 {
2561 int l;
2562
2563 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2564 col += l;
2565 }
2566 }
2567 }
2568 }
2569 rettv->vval.v_number = col;
2570}
2571
2572/*
2573 * "charcol()" function
2574 */
2575 static void
2576f_charcol(typval_T *argvars, typval_T *rettv)
2577{
2578 get_col(argvars, rettv, TRUE);
2579}
2580
2581/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002582 * "charidx()" function
2583 */
2584 static void
2585f_charidx(typval_T *argvars, typval_T *rettv)
2586{
2587 char_u *str;
2588 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002589 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002590 char_u *p;
2591 int len;
2592 int (*ptr2len)(char_u *);
2593
2594 rettv->vval.v_number = -1;
2595
2596 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2597 || (argvars[2].v_type != VAR_UNKNOWN
2598 && argvars[2].v_type != VAR_NUMBER))
2599 {
2600 emsg(_(e_invarg));
2601 return;
2602 }
2603
2604 str = tv_get_string_chk(&argvars[0]);
2605 idx = tv_get_number_chk(&argvars[1], NULL);
2606 if (str == NULL || idx < 0)
2607 return;
2608
2609 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002610 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002611 if (countcc < 0 || countcc > 1)
2612 {
2613 semsg(_(e_using_number_as_bool_nr), countcc);
2614 return;
2615 }
2616
2617 if (enc_utf8 && countcc)
2618 ptr2len = utf_ptr2len;
2619 else
2620 ptr2len = mb_ptr2len;
2621
2622 for (p = str, len = 0; p <= str + idx; len++)
2623 {
2624 if (*p == NUL)
2625 return;
2626 p += ptr2len(p);
2627 }
2628
2629 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2630}
2631
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002632 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002633get_optional_window(typval_T *argvars, int idx)
2634{
2635 win_T *win = curwin;
2636
2637 if (argvars[idx].v_type != VAR_UNKNOWN)
2638 {
2639 win = find_win_by_nr_or_id(&argvars[idx]);
2640 if (win == NULL)
2641 {
2642 emsg(_(e_invalwindow));
2643 return NULL;
2644 }
2645 }
2646 return win;
2647}
2648
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002649/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002650 * "col(string)" function
2651 */
2652 static void
2653f_col(typval_T *argvars, typval_T *rettv)
2654{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002655 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002656}
2657
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002658/*
2659 * "confirm(message, buttons[, default [, type]])" function
2660 */
2661 static void
2662f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2663{
2664#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2665 char_u *message;
2666 char_u *buttons = NULL;
2667 char_u buf[NUMBUFLEN];
2668 char_u buf2[NUMBUFLEN];
2669 int def = 1;
2670 int type = VIM_GENERIC;
2671 char_u *typestr;
2672 int error = FALSE;
2673
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002674 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002675 if (message == NULL)
2676 error = TRUE;
2677 if (argvars[1].v_type != VAR_UNKNOWN)
2678 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002679 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002680 if (buttons == NULL)
2681 error = TRUE;
2682 if (argvars[2].v_type != VAR_UNKNOWN)
2683 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002684 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002685 if (argvars[3].v_type != VAR_UNKNOWN)
2686 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002687 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 if (typestr == NULL)
2689 error = TRUE;
2690 else
2691 {
2692 switch (TOUPPER_ASC(*typestr))
2693 {
2694 case 'E': type = VIM_ERROR; break;
2695 case 'Q': type = VIM_QUESTION; break;
2696 case 'I': type = VIM_INFO; break;
2697 case 'W': type = VIM_WARNING; break;
2698 case 'G': type = VIM_GENERIC; break;
2699 }
2700 }
2701 }
2702 }
2703 }
2704
2705 if (buttons == NULL || *buttons == NUL)
2706 buttons = (char_u *)_("&Ok");
2707
2708 if (!error)
2709 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2710 def, NULL, FALSE);
2711#endif
2712}
2713
2714/*
2715 * "copy()" function
2716 */
2717 static void
2718f_copy(typval_T *argvars, typval_T *rettv)
2719{
2720 item_copy(&argvars[0], rettv, FALSE, 0);
2721}
2722
2723#ifdef FEAT_FLOAT
2724/*
2725 * "cos()" function
2726 */
2727 static void
2728f_cos(typval_T *argvars, typval_T *rettv)
2729{
2730 float_T f = 0.0;
2731
2732 rettv->v_type = VAR_FLOAT;
2733 if (get_float_arg(argvars, &f) == OK)
2734 rettv->vval.v_float = cos(f);
2735 else
2736 rettv->vval.v_float = 0.0;
2737}
2738
2739/*
2740 * "cosh()" function
2741 */
2742 static void
2743f_cosh(typval_T *argvars, typval_T *rettv)
2744{
2745 float_T f = 0.0;
2746
2747 rettv->v_type = VAR_FLOAT;
2748 if (get_float_arg(argvars, &f) == OK)
2749 rettv->vval.v_float = cosh(f);
2750 else
2751 rettv->vval.v_float = 0.0;
2752}
2753#endif
2754
2755/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002756 * Set the cursor position.
2757 * If 'charcol' is TRUE, then use the column number as a character offet.
2758 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002759 */
2760 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002761set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002762{
2763 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002764 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002765 int set_curswant = TRUE;
2766
2767 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002768 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002769 {
2770 pos_T pos;
2771 colnr_T curswant = -1;
2772
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002773 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002774 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002775 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002776 return;
2777 }
2778 line = pos.lnum;
2779 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002780 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002781 if (curswant >= 0)
2782 {
2783 curwin->w_curswant = curswant - 1;
2784 set_curswant = FALSE;
2785 }
2786 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002787 else if ((argvars[0].v_type == VAR_NUMBER ||
2788 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002789 && (argvars[1].v_type == VAR_NUMBER ||
2790 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002791 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002792 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002793 if (line < 0)
2794 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002795 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002796 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002797 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002798 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002799 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002800 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002801 else
2802 {
2803 emsg(_(e_invarg));
2804 return;
2805 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002806 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002807 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002808 if (line > 0)
2809 curwin->w_cursor.lnum = line;
2810 if (col > 0)
2811 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002812 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002813
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002814 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002815 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002816 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002817 if (has_mbyte)
2818 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002819
2820 curwin->w_set_curswant = set_curswant;
2821 rettv->vval.v_number = 0;
2822}
2823
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002824/*
2825 * "cursor(lnum, col)" function, or
2826 * "cursor(list)"
2827 *
2828 * Moves the cursor to the specified line and column.
2829 * Returns 0 when the position could be set, -1 otherwise.
2830 */
2831 static void
2832f_cursor(typval_T *argvars, typval_T *rettv)
2833{
2834 set_cursorpos(argvars, rettv, FALSE);
2835}
2836
Bram Moolenaar4f974752019-02-17 17:44:42 +01002837#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002838/*
2839 * "debugbreak()" function
2840 */
2841 static void
2842f_debugbreak(typval_T *argvars, typval_T *rettv)
2843{
2844 int pid;
2845
2846 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002847 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002848 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002849 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002850 else
2851 {
2852 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2853
2854 if (hProcess != NULL)
2855 {
2856 DebugBreakProcess(hProcess);
2857 CloseHandle(hProcess);
2858 rettv->vval.v_number = OK;
2859 }
2860 }
2861}
2862#endif
2863
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002864/*
2865 * "deepcopy()" function
2866 */
2867 static void
2868f_deepcopy(typval_T *argvars, typval_T *rettv)
2869{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002870 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002871 int copyID;
2872
2873 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002874 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002875 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002876 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002877 else
2878 {
2879 copyID = get_copyID();
2880 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2881 }
2882}
2883
2884/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002885 * "did_filetype()" function
2886 */
2887 static void
2888f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2889{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002890 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002891}
2892
2893/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002894 * "echoraw({expr})" function
2895 */
2896 static void
2897f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2898{
2899 char_u *str = tv_get_string_chk(&argvars[0]);
2900
2901 if (str != NULL && *str != NUL)
2902 {
2903 out_str(str);
2904 out_flush();
2905 }
2906}
2907
2908/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002909 * "empty({expr})" function
2910 */
2911 static void
2912f_empty(typval_T *argvars, typval_T *rettv)
2913{
2914 int n = FALSE;
2915
2916 switch (argvars[0].v_type)
2917 {
2918 case VAR_STRING:
2919 case VAR_FUNC:
2920 n = argvars[0].vval.v_string == NULL
2921 || *argvars[0].vval.v_string == NUL;
2922 break;
2923 case VAR_PARTIAL:
2924 n = FALSE;
2925 break;
2926 case VAR_NUMBER:
2927 n = argvars[0].vval.v_number == 0;
2928 break;
2929 case VAR_FLOAT:
2930#ifdef FEAT_FLOAT
2931 n = argvars[0].vval.v_float == 0.0;
2932 break;
2933#endif
2934 case VAR_LIST:
2935 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002936 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002937 break;
2938 case VAR_DICT:
2939 n = argvars[0].vval.v_dict == NULL
2940 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2941 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002942 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002943 case VAR_SPECIAL:
2944 n = argvars[0].vval.v_number != VVAL_TRUE;
2945 break;
2946
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002947 case VAR_BLOB:
2948 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002949 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2950 break;
2951
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002952 case VAR_JOB:
2953#ifdef FEAT_JOB_CHANNEL
2954 n = argvars[0].vval.v_job == NULL
2955 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2956 break;
2957#endif
2958 case VAR_CHANNEL:
2959#ifdef FEAT_JOB_CHANNEL
2960 n = argvars[0].vval.v_channel == NULL
2961 || !channel_is_open(argvars[0].vval.v_channel);
2962 break;
2963#endif
2964 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002965 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002967 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002968 n = TRUE;
2969 break;
2970 }
2971
2972 rettv->vval.v_number = n;
2973}
2974
2975/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002976 * "environ()" function
2977 */
2978 static void
2979f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2980{
2981#if !defined(AMIGA)
2982 int i = 0;
2983 char_u *entry, *value;
2984# ifdef MSWIN
2985 extern wchar_t **_wenviron;
2986# else
2987 extern char **environ;
2988# endif
2989
2990 if (rettv_dict_alloc(rettv) != OK)
2991 return;
2992
2993# ifdef MSWIN
2994 if (*_wenviron == NULL)
2995 return;
2996# else
2997 if (*environ == NULL)
2998 return;
2999# endif
3000
3001 for (i = 0; ; ++i)
3002 {
3003# ifdef MSWIN
3004 short_u *p;
3005
3006 if ((p = (short_u *)_wenviron[i]) == NULL)
3007 return;
3008 entry = utf16_to_enc(p, NULL);
3009# else
3010 if ((entry = (char_u *)environ[i]) == NULL)
3011 return;
3012 entry = vim_strsave(entry);
3013# endif
3014 if (entry == NULL) // out of memory
3015 return;
3016 if ((value = vim_strchr(entry, '=')) == NULL)
3017 {
3018 vim_free(entry);
3019 continue;
3020 }
3021 *value++ = NUL;
3022 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3023 vim_free(entry);
3024 }
3025#endif
3026}
3027
3028/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003029 * "escape({string}, {chars})" function
3030 */
3031 static void
3032f_escape(typval_T *argvars, typval_T *rettv)
3033{
3034 char_u buf[NUMBUFLEN];
3035
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003036 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3037 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003038 rettv->v_type = VAR_STRING;
3039}
3040
3041/*
3042 * "eval()" function
3043 */
3044 static void
3045f_eval(typval_T *argvars, typval_T *rettv)
3046{
3047 char_u *s, *p;
3048
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003049 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003050 if (s != NULL)
3051 s = skipwhite(s);
3052
3053 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003054 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003055 {
3056 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003057 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003058 need_clr_eos = FALSE;
3059 rettv->v_type = VAR_NUMBER;
3060 rettv->vval.v_number = 0;
3061 }
3062 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003063 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003064}
3065
3066/*
3067 * "eventhandler()" function
3068 */
3069 static void
3070f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3071{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003072 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003073}
3074
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003075static garray_T redir_execute_ga;
3076
3077/*
3078 * Append "value[value_len]" to the execute() output.
3079 */
3080 void
3081execute_redir_str(char_u *value, int value_len)
3082{
3083 int len;
3084
3085 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003086 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003087 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003088 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003089 if (ga_grow(&redir_execute_ga, len) == OK)
3090 {
3091 mch_memmove((char *)redir_execute_ga.ga_data
3092 + redir_execute_ga.ga_len, value, len);
3093 redir_execute_ga.ga_len += len;
3094 }
3095}
3096
3097/*
3098 * Get next line from a list.
3099 * Called by do_cmdline() to get the next line.
3100 * Returns allocated string, or NULL for end of function.
3101 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003102 static char_u *
3103get_list_line(
3104 int c UNUSED,
3105 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003106 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02003107 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003108{
3109 listitem_T **p = (listitem_T **)cookie;
3110 listitem_T *item = *p;
3111 char_u buf[NUMBUFLEN];
3112 char_u *s;
3113
3114 if (item == NULL)
3115 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003116 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003117 *p = item->li_next;
3118 return s == NULL ? NULL : vim_strsave(s);
3119}
3120
3121/*
3122 * "execute()" function
3123 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02003124 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003125execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003126{
3127 char_u *cmd = NULL;
3128 list_T *list = NULL;
3129 int save_msg_silent = msg_silent;
3130 int save_emsg_silent = emsg_silent;
3131 int save_emsg_noredir = emsg_noredir;
3132 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003133 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003134 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003135 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003136 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003137
3138 rettv->vval.v_string = NULL;
3139 rettv->v_type = VAR_STRING;
3140
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003141 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003142 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003143 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003144 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003145 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003146 return;
3147 ++list->lv_refcount;
3148 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003149 else if (argvars[arg_off].v_type == VAR_JOB
3150 || argvars[arg_off].v_type == VAR_CHANNEL)
3151 {
3152 emsg(_(e_inval_string));
3153 return;
3154 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003155 else
3156 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003157 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003158 if (cmd == NULL)
3159 return;
3160 }
3161
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003162 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003163 {
3164 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003165 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003166
3167 if (s == NULL)
3168 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003169 if (*s == NUL)
3170 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003171 if (STRNCMP(s, "silent", 6) == 0)
3172 ++msg_silent;
3173 if (STRCMP(s, "silent!") == 0)
3174 {
3175 emsg_silent = TRUE;
3176 emsg_noredir = TRUE;
3177 }
3178 }
3179 else
3180 ++msg_silent;
3181
3182 if (redir_execute)
3183 save_ga = redir_execute_ga;
3184 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3185 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003186 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003187 if (!echo_output)
3188 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003189
3190 if (cmd != NULL)
3191 do_cmdline_cmd(cmd);
3192 else
3193 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003194 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003195
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003196 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003197 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003198 do_cmdline(NULL, get_list_line, (void *)&item,
3199 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3200 --list->lv_refcount;
3201 }
3202
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003203 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003204 if (ga_grow(&redir_execute_ga, 1) == OK)
3205 {
3206 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3207 rettv->vval.v_string = redir_execute_ga.ga_data;
3208 }
3209 else
3210 {
3211 ga_clear(&redir_execute_ga);
3212 rettv->vval.v_string = NULL;
3213 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003214 msg_silent = save_msg_silent;
3215 emsg_silent = save_emsg_silent;
3216 emsg_noredir = save_emsg_noredir;
3217
3218 redir_execute = save_redir_execute;
3219 if (redir_execute)
3220 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003221 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003222
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003223 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003224 if (echo_output)
3225 // When not working silently: put it in column zero. A following
3226 // "echon" will overwrite the message, unavoidably.
3227 msg_col = 0;
3228 else
3229 // When working silently: Put it back where it was, since nothing
3230 // should have been written.
3231 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003232}
3233
3234/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003235 * "execute()" function
3236 */
3237 static void
3238f_execute(typval_T *argvars, typval_T *rettv)
3239{
3240 execute_common(argvars, rettv, 0);
3241}
3242
3243/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003244 * "exists()" function
3245 */
3246 static void
3247f_exists(typval_T *argvars, typval_T *rettv)
3248{
3249 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003250 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003251
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003252 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003253 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003254 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003255 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003256 if (mch_getenv(p + 1) != NULL)
3257 n = TRUE;
3258 else
3259 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003260 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003261 p = expand_env_save(p);
3262 if (p != NULL && *p != '$')
3263 n = TRUE;
3264 vim_free(p);
3265 }
3266 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003267 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003268 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003269 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003270 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003271 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003272 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003273 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003274 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003275 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003276 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003277 else if (*p == '?') // internal function only
3278 {
3279 n = has_internal_func_name(p + 1);
3280 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003281 else if (*p == ':')
3282 {
3283 n = cmd_exists(p + 1);
3284 }
3285 else if (*p == '#')
3286 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003287 if (p[1] == '#')
3288 n = autocmd_supported(p + 2);
3289 else
3290 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003291 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003292 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003293 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003294 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003295 }
3296
3297 rettv->vval.v_number = n;
3298}
3299
3300#ifdef FEAT_FLOAT
3301/*
3302 * "exp()" function
3303 */
3304 static void
3305f_exp(typval_T *argvars, typval_T *rettv)
3306{
3307 float_T f = 0.0;
3308
3309 rettv->v_type = VAR_FLOAT;
3310 if (get_float_arg(argvars, &f) == OK)
3311 rettv->vval.v_float = exp(f);
3312 else
3313 rettv->vval.v_float = 0.0;
3314}
3315#endif
3316
3317/*
3318 * "expand()" function
3319 */
3320 static void
3321f_expand(typval_T *argvars, typval_T *rettv)
3322{
3323 char_u *s;
3324 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003325 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003326 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3327 expand_T xpc;
3328 int error = FALSE;
3329 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003330#ifdef BACKSLASH_IN_FILENAME
3331 char_u *p_csl_save = p_csl;
3332
3333 // avoid using 'completeslash' here
3334 p_csl = empty_option;
3335#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003336
3337 rettv->v_type = VAR_STRING;
3338 if (argvars[1].v_type != VAR_UNKNOWN
3339 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003340 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003341 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003342 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003343
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003344 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003345 if (*s == '%' || *s == '#' || *s == '<')
3346 {
3347 ++emsg_off;
3348 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3349 --emsg_off;
3350 if (rettv->v_type == VAR_LIST)
3351 {
3352 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3353 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003354 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003355 }
3356 else
3357 rettv->vval.v_string = result;
3358 }
3359 else
3360 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003361 // When the optional second argument is non-zero, don't remove matches
3362 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003363 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003364 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003365 options |= WILD_KEEP_ALL;
3366 if (!error)
3367 {
3368 ExpandInit(&xpc);
3369 xpc.xp_context = EXPAND_FILES;
3370 if (p_wic)
3371 options += WILD_ICASE;
3372 if (rettv->v_type == VAR_STRING)
3373 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3374 options, WILD_ALL);
3375 else if (rettv_list_alloc(rettv) != FAIL)
3376 {
3377 int i;
3378
3379 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3380 for (i = 0; i < xpc.xp_numfiles; i++)
3381 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3382 ExpandCleanup(&xpc);
3383 }
3384 }
3385 else
3386 rettv->vval.v_string = NULL;
3387 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003388#ifdef BACKSLASH_IN_FILENAME
3389 p_csl = p_csl_save;
3390#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003391}
3392
3393/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003394 * "expandcmd()" function
3395 * Expand all the special characters in a command string.
3396 */
3397 static void
3398f_expandcmd(typval_T *argvars, typval_T *rettv)
3399{
3400 exarg_T eap;
3401 char_u *cmdstr;
3402 char *errormsg = NULL;
3403
3404 rettv->v_type = VAR_STRING;
3405 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3406
3407 memset(&eap, 0, sizeof(eap));
3408 eap.cmd = cmdstr;
3409 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003410 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003411 eap.usefilter = FALSE;
3412 eap.nextcmd = NULL;
3413 eap.cmdidx = CMD_USER;
3414
3415 expand_filename(&eap, &cmdstr, &errormsg);
3416 if (errormsg != NULL && *errormsg != NUL)
3417 emsg(errormsg);
3418
3419 rettv->vval.v_string = cmdstr;
3420}
3421
3422/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003423 * "feedkeys()" function
3424 */
3425 static void
3426f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3427{
3428 int remap = TRUE;
3429 int insert = FALSE;
3430 char_u *keys, *flags;
3431 char_u nbuf[NUMBUFLEN];
3432 int typed = FALSE;
3433 int execute = FALSE;
3434 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003435 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003436 char_u *keys_esc;
3437
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003438 // This is not allowed in the sandbox. If the commands would still be
3439 // executed in the sandbox it would be OK, but it probably happens later,
3440 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003441 if (check_secure())
3442 return;
3443
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003444 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003445
3446 if (argvars[1].v_type != VAR_UNKNOWN)
3447 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003448 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003449 for ( ; *flags != NUL; ++flags)
3450 {
3451 switch (*flags)
3452 {
3453 case 'n': remap = FALSE; break;
3454 case 'm': remap = TRUE; break;
3455 case 't': typed = TRUE; break;
3456 case 'i': insert = TRUE; break;
3457 case 'x': execute = TRUE; break;
3458 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003459 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003460 }
3461 }
3462 }
3463
3464 if (*keys != NUL || execute)
3465 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003466 // Need to escape K_SPECIAL and CSI before putting the string in the
3467 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003468 keys_esc = vim_strsave_escape_csi(keys);
3469 if (keys_esc != NULL)
3470 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003471 if (lowlevel)
3472 {
3473#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003474 int idx;
3475 int len = (int)STRLEN(keys);
3476
3477 for (idx = 0; idx < len; ++idx)
3478 {
3479 // if a CTRL-C was typed, set got_int, similar to what
3480 // happens in fill_input_buf()
3481 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3482 got_int = TRUE;
3483 add_to_input_buf(keys + idx, 1);
3484 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003485#else
3486 emsg(_("E980: lowlevel input not supported"));
3487#endif
3488 }
3489 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003490 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003491 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003492 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003493 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003494#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003495 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003496#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003497 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003498 typebuf_was_filled = TRUE;
3499 }
3500 vim_free(keys_esc);
3501
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003502 if (execute)
3503 {
3504 int save_msg_scroll = msg_scroll;
3505
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003506 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003507 msg_scroll = FALSE;
3508
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003509 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003510 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003511 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003512 ++in_feedkeys;
3513 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003514 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003515 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003516 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003517 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003518 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003519 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003520
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003521 msg_scroll |= save_msg_scroll;
3522 }
3523 }
3524 }
3525}
3526
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003527#ifdef FEAT_FLOAT
3528/*
3529 * "float2nr({float})" function
3530 */
3531 static void
3532f_float2nr(typval_T *argvars, typval_T *rettv)
3533{
3534 float_T f = 0.0;
3535
3536 if (get_float_arg(argvars, &f) == OK)
3537 {
Bram Moolenaar37184272020-05-23 19:30:05 +02003538 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003539 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02003540 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003541 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003542 else
3543 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003544 }
3545}
3546
3547/*
3548 * "floor({float})" function
3549 */
3550 static void
3551f_floor(typval_T *argvars, typval_T *rettv)
3552{
3553 float_T f = 0.0;
3554
3555 rettv->v_type = VAR_FLOAT;
3556 if (get_float_arg(argvars, &f) == OK)
3557 rettv->vval.v_float = floor(f);
3558 else
3559 rettv->vval.v_float = 0.0;
3560}
3561
3562/*
3563 * "fmod()" function
3564 */
3565 static void
3566f_fmod(typval_T *argvars, typval_T *rettv)
3567{
3568 float_T fx = 0.0, fy = 0.0;
3569
3570 rettv->v_type = VAR_FLOAT;
3571 if (get_float_arg(argvars, &fx) == OK
3572 && get_float_arg(&argvars[1], &fy) == OK)
3573 rettv->vval.v_float = fmod(fx, fy);
3574 else
3575 rettv->vval.v_float = 0.0;
3576}
3577#endif
3578
3579/*
3580 * "fnameescape({string})" function
3581 */
3582 static void
3583f_fnameescape(typval_T *argvars, typval_T *rettv)
3584{
3585 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003586 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003587 rettv->v_type = VAR_STRING;
3588}
3589
3590/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003591 * "foreground()" function
3592 */
3593 static void
3594f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3595{
3596#ifdef FEAT_GUI
3597 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003598 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003599 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003600 return;
3601 }
3602#endif
3603#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003604 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003605#endif
3606}
3607
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003608 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003609common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003610{
3611 char_u *s;
3612 char_u *name;
3613 int use_string = FALSE;
3614 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003615 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003616 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003617
3618 if (argvars[0].v_type == VAR_FUNC)
3619 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003620 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003621 s = argvars[0].vval.v_string;
3622 }
3623 else if (argvars[0].v_type == VAR_PARTIAL
3624 && argvars[0].vval.v_partial != NULL)
3625 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003626 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003627 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003628 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003629 }
3630 else
3631 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003632 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003633 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003634 use_string = TRUE;
3635 }
3636
Bram Moolenaar843b8842016-08-21 14:36:15 +02003637 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003638 {
3639 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003640 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003641 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3642 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003643 if (*name != NUL)
3644 s = NULL;
3645 }
3646
Bram Moolenaar843b8842016-08-21 14:36:15 +02003647 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3648 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003649 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003650 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003651 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003652 ? find_func(trans_name, is_global, NULL) == NULL
3653 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003654 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003655 else
3656 {
3657 int dict_idx = 0;
3658 int arg_idx = 0;
3659 list_T *list = NULL;
3660
3661 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3662 {
3663 char sid_buf[25];
3664 int off = *s == 's' ? 2 : 5;
3665
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003666 // Expand s: and <SID> into <SNR>nr_, so that the function can
3667 // also be called from another script. Using trans_function_name()
3668 // would also work, but some plugins depend on the name being
3669 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003670 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003671 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003672 if (name != NULL)
3673 {
3674 STRCPY(name, sid_buf);
3675 STRCAT(name, s + off);
3676 }
3677 }
3678 else
3679 name = vim_strsave(s);
3680
3681 if (argvars[1].v_type != VAR_UNKNOWN)
3682 {
3683 if (argvars[2].v_type != VAR_UNKNOWN)
3684 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003685 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003686 arg_idx = 1;
3687 dict_idx = 2;
3688 }
3689 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003690 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003691 dict_idx = 1;
3692 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003693 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003694 arg_idx = 1;
3695 if (dict_idx > 0)
3696 {
3697 if (argvars[dict_idx].v_type != VAR_DICT)
3698 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003699 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003700 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003701 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003702 }
3703 if (argvars[dict_idx].vval.v_dict == NULL)
3704 dict_idx = 0;
3705 }
3706 if (arg_idx > 0)
3707 {
3708 if (argvars[arg_idx].v_type != VAR_LIST)
3709 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003710 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003711 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003712 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003713 }
3714 list = argvars[arg_idx].vval.v_list;
3715 if (list == NULL || list->lv_len == 0)
3716 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003717 else if (list->lv_len > MAX_FUNC_ARGS)
3718 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003719 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003720 vim_free(name);
3721 goto theend;
3722 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003723 }
3724 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003725 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003726 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003727 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003728
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003729 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003730 if (pt == NULL)
3731 vim_free(name);
3732 else
3733 {
3734 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3735 {
3736 listitem_T *li;
3737 int i = 0;
3738 int arg_len = 0;
3739 int lv_len = 0;
3740
3741 if (arg_pt != NULL)
3742 arg_len = arg_pt->pt_argc;
3743 if (list != NULL)
3744 lv_len = list->lv_len;
3745 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003746 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003747 if (pt->pt_argv == NULL)
3748 {
3749 vim_free(pt);
3750 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003751 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003752 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003753 for (i = 0; i < arg_len; i++)
3754 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3755 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003756 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003757 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003758 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003759 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003760 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003761 }
3762
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003763 // For "function(dict.func, [], dict)" and "func" is a partial
3764 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003765 if (dict_idx > 0)
3766 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003767 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003768 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3769 ++pt->pt_dict->dv_refcount;
3770 }
3771 else if (arg_pt != NULL)
3772 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003773 // If the dict was bound automatically the result is also
3774 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003775 pt->pt_dict = arg_pt->pt_dict;
3776 pt->pt_auto = arg_pt->pt_auto;
3777 if (pt->pt_dict != NULL)
3778 ++pt->pt_dict->dv_refcount;
3779 }
3780
3781 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003782 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3783 {
3784 pt->pt_func = arg_pt->pt_func;
3785 func_ptr_ref(pt->pt_func);
3786 vim_free(name);
3787 }
3788 else if (is_funcref)
3789 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003790 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003791 func_ptr_ref(pt->pt_func);
3792 vim_free(name);
3793 }
3794 else
3795 {
3796 pt->pt_name = name;
3797 func_ref(name);
3798 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003799 }
3800 rettv->v_type = VAR_PARTIAL;
3801 rettv->vval.v_partial = pt;
3802 }
3803 else
3804 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003805 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003806 rettv->v_type = VAR_FUNC;
3807 rettv->vval.v_string = name;
3808 func_ref(name);
3809 }
3810 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003811theend:
3812 vim_free(trans_name);
3813}
3814
3815/*
3816 * "funcref()" function
3817 */
3818 static void
3819f_funcref(typval_T *argvars, typval_T *rettv)
3820{
3821 common_function(argvars, rettv, TRUE);
3822}
3823
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003824 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003825ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003826{
3827 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3828 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003829 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003830}
3831
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003832/*
3833 * "function()" function
3834 */
3835 static void
3836f_function(typval_T *argvars, typval_T *rettv)
3837{
3838 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003839}
3840
3841/*
3842 * "garbagecollect()" function
3843 */
3844 static void
3845f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3846{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003847 // This is postponed until we are back at the toplevel, because we may be
3848 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003849 want_garbage_collect = TRUE;
3850
Bram Moolenaar2df47312020-09-05 17:30:44 +02003851 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003852 garbage_collect_at_exit = TRUE;
3853}
3854
3855/*
3856 * "get()" function
3857 */
3858 static void
3859f_get(typval_T *argvars, typval_T *rettv)
3860{
3861 listitem_T *li;
3862 list_T *l;
3863 dictitem_T *di;
3864 dict_T *d;
3865 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003866 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003867
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003868 if (argvars[0].v_type == VAR_BLOB)
3869 {
3870 int error = FALSE;
3871 int idx = tv_get_number_chk(&argvars[1], &error);
3872
3873 if (!error)
3874 {
3875 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003876 if (idx < 0)
3877 idx = blob_len(argvars[0].vval.v_blob) + idx;
3878 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3879 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003880 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003881 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003882 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003883 tv = rettv;
3884 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003885 }
3886 }
3887 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003888 {
3889 if ((l = argvars[0].vval.v_list) != NULL)
3890 {
3891 int error = FALSE;
3892
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003893 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003894 if (!error && li != NULL)
3895 tv = &li->li_tv;
3896 }
3897 }
3898 else if (argvars[0].v_type == VAR_DICT)
3899 {
3900 if ((d = argvars[0].vval.v_dict) != NULL)
3901 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003902 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003903 if (di != NULL)
3904 tv = &di->di_tv;
3905 }
3906 }
3907 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3908 {
3909 partial_T *pt;
3910 partial_T fref_pt;
3911
3912 if (argvars[0].v_type == VAR_PARTIAL)
3913 pt = argvars[0].vval.v_partial;
3914 else
3915 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003916 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003917 fref_pt.pt_name = argvars[0].vval.v_string;
3918 pt = &fref_pt;
3919 }
3920
3921 if (pt != NULL)
3922 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003923 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003924 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003925
3926 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3927 {
3928 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003929 n = partial_name(pt);
3930 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003931 rettv->vval.v_string = NULL;
3932 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003933 {
3934 rettv->vval.v_string = vim_strsave(n);
3935 if (rettv->v_type == VAR_FUNC)
3936 func_ref(rettv->vval.v_string);
3937 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003938 }
3939 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003940 {
3941 what_is_dict = TRUE;
3942 if (pt->pt_dict != NULL)
3943 rettv_dict_set(rettv, pt->pt_dict);
3944 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003945 else if (STRCMP(what, "args") == 0)
3946 {
3947 rettv->v_type = VAR_LIST;
3948 if (rettv_list_alloc(rettv) == OK)
3949 {
3950 int i;
3951
3952 for (i = 0; i < pt->pt_argc; ++i)
3953 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3954 }
3955 }
3956 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003957 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003958
3959 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3960 // third argument
3961 if (!what_is_dict)
3962 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003963 }
3964 }
3965 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003966 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003967
3968 if (tv == NULL)
3969 {
3970 if (argvars[2].v_type != VAR_UNKNOWN)
3971 copy_tv(&argvars[2], rettv);
3972 }
3973 else
3974 copy_tv(tv, rettv);
3975}
3976
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003977/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003978 * "getchangelist()" function
3979 */
3980 static void
3981f_getchangelist(typval_T *argvars, typval_T *rettv)
3982{
3983#ifdef FEAT_JUMPLIST
3984 buf_T *buf;
3985 int i;
3986 list_T *l;
3987 dict_T *d;
3988#endif
3989
3990 if (rettv_list_alloc(rettv) != OK)
3991 return;
3992
3993#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003994 if (argvars[0].v_type == VAR_UNKNOWN)
3995 buf = curbuf;
3996 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003997 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003998 if (buf == NULL)
3999 return;
4000
4001 l = list_alloc();
4002 if (l == NULL)
4003 return;
4004
4005 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4006 return;
4007 /*
4008 * The current window change list index tracks only the position in the
4009 * current buffer change list. For other buffers, use the change list
4010 * length as the current index.
4011 */
4012 list_append_number(rettv->vval.v_list,
4013 (varnumber_T)((buf == curwin->w_buffer)
4014 ? curwin->w_changelistidx : buf->b_changelistlen));
4015
4016 for (i = 0; i < buf->b_changelistlen; ++i)
4017 {
4018 if (buf->b_changelist[i].lnum == 0)
4019 continue;
4020 if ((d = dict_alloc()) == NULL)
4021 return;
4022 if (list_append_dict(l, d) == FAIL)
4023 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004024 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4025 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004026 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004027 }
4028#endif
4029}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004030
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004031 static void
4032getpos_both(
4033 typval_T *argvars,
4034 typval_T *rettv,
4035 int getcurpos,
4036 int charcol)
4037{
4038 pos_T *fp = NULL;
4039 pos_T pos;
4040 win_T *wp = curwin;
4041 list_T *l;
4042 int fnum = -1;
4043
4044 if (rettv_list_alloc(rettv) == OK)
4045 {
4046 l = rettv->vval.v_list;
4047 if (getcurpos)
4048 {
4049 if (argvars[0].v_type != VAR_UNKNOWN)
4050 {
4051 wp = find_win_by_nr_or_id(&argvars[0]);
4052 if (wp != NULL)
4053 fp = &wp->w_cursor;
4054 }
4055 else
4056 fp = &curwin->w_cursor;
4057 if (fp != NULL && charcol)
4058 {
4059 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01004060 pos.col =
4061 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004062 fp = &pos;
4063 }
4064 }
4065 else
4066 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
4067 if (fnum != -1)
4068 list_append_number(l, (varnumber_T)fnum);
4069 else
4070 list_append_number(l, (varnumber_T)0);
4071 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4072 : (varnumber_T)0);
4073 list_append_number(l, (fp != NULL)
4074 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4075 : (varnumber_T)0);
4076 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
4077 (varnumber_T)0);
4078 if (getcurpos)
4079 {
4080 int save_set_curswant = curwin->w_set_curswant;
4081 colnr_T save_curswant = curwin->w_curswant;
4082 colnr_T save_virtcol = curwin->w_virtcol;
4083
4084 if (wp == curwin)
4085 update_curswant();
4086 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
4087 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
4088
4089 // Do not change "curswant", as it is unexpected that a get
4090 // function has a side effect.
4091 if (wp == curwin && save_set_curswant)
4092 {
4093 curwin->w_set_curswant = save_set_curswant;
4094 curwin->w_curswant = save_curswant;
4095 curwin->w_virtcol = save_virtcol;
4096 curwin->w_valid &= ~VALID_VIRTCOL;
4097 }
4098 }
4099 }
4100 else
4101 rettv->vval.v_number = FALSE;
4102}
4103
4104/*
4105 * "getcharpos()" function
4106 */
4107 static void
4108f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
4109{
4110 getpos_both(argvars, rettv, FALSE, TRUE);
4111}
4112
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004113/*
4114 * "getcharsearch()" function
4115 */
4116 static void
4117f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4118{
4119 if (rettv_dict_alloc(rettv) != FAIL)
4120 {
4121 dict_T *dict = rettv->vval.v_dict;
4122
Bram Moolenaare0be1672018-07-08 16:50:37 +02004123 dict_add_string(dict, "char", last_csearch());
4124 dict_add_number(dict, "forward", last_csearch_forward());
4125 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004126 }
4127}
4128
4129/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004130 * "getenv()" function
4131 */
4132 static void
4133f_getenv(typval_T *argvars, typval_T *rettv)
4134{
4135 int mustfree = FALSE;
4136 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4137
4138 if (p == NULL)
4139 {
4140 rettv->v_type = VAR_SPECIAL;
4141 rettv->vval.v_number = VVAL_NULL;
4142 return;
4143 }
4144 if (!mustfree)
4145 p = vim_strsave(p);
4146 rettv->vval.v_string = p;
4147 rettv->v_type = VAR_STRING;
4148}
4149
4150/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004151 * "getfontname()" function
4152 */
4153 static void
4154f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4155{
4156 rettv->v_type = VAR_STRING;
4157 rettv->vval.v_string = NULL;
4158#ifdef FEAT_GUI
4159 if (gui.in_use)
4160 {
4161 GuiFont font;
4162 char_u *name = NULL;
4163
4164 if (argvars[0].v_type == VAR_UNKNOWN)
4165 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004166 // Get the "Normal" font. Either the name saved by
4167 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004168 font = gui.norm_font;
4169 name = hl_get_font_name();
4170 }
4171 else
4172 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004173 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004174 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004175 return;
4176 font = gui_mch_get_font(name, FALSE);
4177 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004178 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004179 }
4180 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4181 if (argvars[0].v_type != VAR_UNKNOWN)
4182 gui_mch_free_font(font);
4183 }
4184#endif
4185}
4186
4187/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004188 * "getjumplist()" function
4189 */
4190 static void
4191f_getjumplist(typval_T *argvars, typval_T *rettv)
4192{
4193#ifdef FEAT_JUMPLIST
4194 win_T *wp;
4195 int i;
4196 list_T *l;
4197 dict_T *d;
4198#endif
4199
4200 if (rettv_list_alloc(rettv) != OK)
4201 return;
4202
4203#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004204 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004205 if (wp == NULL)
4206 return;
4207
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004208 cleanup_jumplist(wp, TRUE);
4209
Bram Moolenaar4f505882018-02-10 21:06:32 +01004210 l = list_alloc();
4211 if (l == NULL)
4212 return;
4213
4214 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4215 return;
4216 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4217
4218 for (i = 0; i < wp->w_jumplistlen; ++i)
4219 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004220 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4221 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004222 if ((d = dict_alloc()) == NULL)
4223 return;
4224 if (list_append_dict(l, d) == FAIL)
4225 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004226 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4227 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004228 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004229 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004230 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004231 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004232 }
4233#endif
4234}
4235
4236/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004237 * "getpid()" function
4238 */
4239 static void
4240f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4241{
4242 rettv->vval.v_number = mch_get_pid();
4243}
4244
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004245/*
4246 * "getcurpos()" function
4247 */
4248 static void
4249f_getcurpos(typval_T *argvars, typval_T *rettv)
4250{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004251 getpos_both(argvars, rettv, TRUE, FALSE);
4252}
4253
4254 static void
4255f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4256{
4257 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004258}
4259
4260/*
4261 * "getpos(string)" function
4262 */
4263 static void
4264f_getpos(typval_T *argvars, typval_T *rettv)
4265{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004266 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004267}
4268
4269/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004270 * "getreg()" function
4271 */
4272 static void
4273f_getreg(typval_T *argvars, typval_T *rettv)
4274{
4275 char_u *strregname;
4276 int regname;
4277 int arg2 = FALSE;
4278 int return_list = FALSE;
4279 int error = FALSE;
4280
4281 if (argvars[0].v_type != VAR_UNKNOWN)
4282 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004283 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004284 error = strregname == NULL;
4285 if (argvars[1].v_type != VAR_UNKNOWN)
4286 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004287 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004288 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004289 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004290 }
4291 }
4292 else
4293 strregname = get_vim_var_str(VV_REG);
4294
4295 if (error)
4296 return;
4297
4298 regname = (strregname == NULL ? '"' : *strregname);
4299 if (regname == 0)
4300 regname = '"';
4301
4302 if (return_list)
4303 {
4304 rettv->v_type = VAR_LIST;
4305 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4306 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4307 if (rettv->vval.v_list == NULL)
4308 (void)rettv_list_alloc(rettv);
4309 else
4310 ++rettv->vval.v_list->lv_refcount;
4311 }
4312 else
4313 {
4314 rettv->v_type = VAR_STRING;
4315 rettv->vval.v_string = get_reg_contents(regname,
4316 arg2 ? GREG_EXPR_SRC : 0);
4317 }
4318}
4319
4320/*
4321 * "getregtype()" function
4322 */
4323 static void
4324f_getregtype(typval_T *argvars, typval_T *rettv)
4325{
4326 char_u *strregname;
4327 int regname;
4328 char_u buf[NUMBUFLEN + 2];
4329 long reglen = 0;
4330
4331 if (argvars[0].v_type != VAR_UNKNOWN)
4332 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004333 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004334 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004335 {
4336 rettv->v_type = VAR_STRING;
4337 rettv->vval.v_string = NULL;
4338 return;
4339 }
4340 }
4341 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004342 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004343 strregname = get_vim_var_str(VV_REG);
4344
4345 regname = (strregname == NULL ? '"' : *strregname);
4346 if (regname == 0)
4347 regname = '"';
4348
4349 buf[0] = NUL;
4350 buf[1] = NUL;
4351 switch (get_reg_type(regname, &reglen))
4352 {
4353 case MLINE: buf[0] = 'V'; break;
4354 case MCHAR: buf[0] = 'v'; break;
4355 case MBLOCK:
4356 buf[0] = Ctrl_V;
4357 sprintf((char *)buf + 1, "%ld", reglen + 1);
4358 break;
4359 }
4360 rettv->v_type = VAR_STRING;
4361 rettv->vval.v_string = vim_strsave(buf);
4362}
4363
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004364/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004365 * "gettagstack()" function
4366 */
4367 static void
4368f_gettagstack(typval_T *argvars, typval_T *rettv)
4369{
4370 win_T *wp = curwin; // default is current window
4371
4372 if (rettv_dict_alloc(rettv) != OK)
4373 return;
4374
4375 if (argvars[0].v_type != VAR_UNKNOWN)
4376 {
4377 wp = find_win_by_nr_or_id(&argvars[0]);
4378 if (wp == NULL)
4379 return;
4380 }
4381
4382 get_tagstack(wp, rettv->vval.v_dict);
4383}
4384
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004385/*
4386 * "gettext()" function
4387 */
4388 static void
4389f_gettext(typval_T *argvars, typval_T *rettv)
4390{
4391 if (argvars[0].v_type != VAR_STRING
4392 || argvars[0].vval.v_string == NULL
4393 || *argvars[0].vval.v_string == NUL)
4394 {
4395 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4396 }
4397 else
4398 {
4399 rettv->v_type = VAR_STRING;
4400 rettv->vval.v_string = vim_strsave(
4401 (char_u *)_(argvars[0].vval.v_string));
4402 }
4403}
4404
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004405// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004406#include "version.h"
4407
4408/*
4409 * "has()" function
4410 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004411 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004412f_has(typval_T *argvars, typval_T *rettv)
4413{
4414 int i;
4415 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004416 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004417 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004418 typedef struct {
4419 char *name;
4420 short present;
4421 } has_item_T;
4422 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004423 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004424 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004425#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004426 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004427#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004428 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004429#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004430 },
4431 {"arp",
4432#if defined(AMIGA) && defined(FEAT_ARP)
4433 1
4434#else
4435 0
4436#endif
4437 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004438 {"haiku",
4439#ifdef __HAIKU__
4440 1
4441#else
4442 0
4443#endif
4444 },
4445 {"bsd",
4446#if defined(BSD) && !defined(MACOS_X)
4447 1
4448#else
4449 0
4450#endif
4451 },
4452 {"hpux",
4453#ifdef hpux
4454 1
4455#else
4456 0
4457#endif
4458 },
4459 {"linux",
4460#ifdef __linux__
4461 1
4462#else
4463 0
4464#endif
4465 },
4466 {"mac", // Mac OS X (and, once, Mac OS Classic)
4467#ifdef MACOS_X
4468 1
4469#else
4470 0
4471#endif
4472 },
4473 {"osx", // Mac OS X
4474#ifdef MACOS_X
4475 1
4476#else
4477 0
4478#endif
4479 },
4480 {"macunix", // Mac OS X, with the darwin feature
4481#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4482 1
4483#else
4484 0
4485#endif
4486 },
4487 {"osxdarwin", // synonym for macunix
4488#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4489 1
4490#else
4491 0
4492#endif
4493 },
4494 {"qnx",
4495#ifdef __QNX__
4496 1
4497#else
4498 0
4499#endif
4500 },
4501 {"sun",
4502#ifdef SUN_SYSTEM
4503 1
4504#else
4505 0
4506#endif
4507 },
4508 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004509#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004510 1
4511#else
4512 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004513#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004514 },
4515 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004516#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004517 1
4518#else
4519 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004520#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004521 },
4522 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004523#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004524 1
4525#else
4526 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004527#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004528 },
4529 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004530#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004531 1
4532#else
4533 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004534#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004535 },
4536 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004537#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004538 1
4539#else
4540 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004541#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004542 },
4543 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004544#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004545 1
4546#else
4547 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004548#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004549 },
4550 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004551#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004552 1
4553#else
4554 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004555#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004556 },
4557 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004558#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004559 1
4560#else
4561 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004562#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004563 },
4564 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004565#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004566 1
4567#else
4568 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004569#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004570 },
4571 {"autocmd", 1},
4572 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004573#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004574 1
4575#else
4576 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004577#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004578 },
4579 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004580#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004581 1
4582#else
4583 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004584#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004585 },
4586 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004587#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004588 1
4589#else
4590 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004591#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004592 },
4593 {"balloon_multiline",
4594#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4595 // MS-Windows requires runtime check, see below
4596 1
4597#else
4598 0
4599#endif
4600 },
4601 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004602#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004603 1
4604#else
4605 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004606#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004607 },
4608 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004609#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004610 1
4611#else
4612 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004613#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004614 },
4615 {"all_builtin_terms",
4616#if defined(ALL_BUILTIN_TCAPS)
4617 1
4618#else
4619 0
4620#endif
4621 },
4622 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004623#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004624 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004625 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004626 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004627#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004628 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004629#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004630 },
4631 {"byte_offset",
4632#ifdef FEAT_BYTEOFF
4633 1
4634#else
4635 0
4636#endif
4637 },
4638 {"channel",
4639#ifdef FEAT_JOB_CHANNEL
4640 1
4641#else
4642 0
4643#endif
4644 },
4645 {"cindent",
4646#ifdef FEAT_CINDENT
4647 1
4648#else
4649 0
4650#endif
4651 },
4652 {"clientserver",
4653#ifdef FEAT_CLIENTSERVER
4654 1
4655#else
4656 0
4657#endif
4658 },
4659 {"clipboard",
4660#ifdef FEAT_CLIPBOARD
4661 1
4662#else
4663 0
4664#endif
4665 },
4666 {"cmdline_compl", 1},
4667 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004668 {"cmdwin",
4669#ifdef FEAT_CMDWIN
4670 1
4671#else
4672 0
4673#endif
4674 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004675 {"comments", 1},
4676 {"conceal",
4677#ifdef FEAT_CONCEAL
4678 1
4679#else
4680 0
4681#endif
4682 },
4683 {"cryptv",
4684#ifdef FEAT_CRYPT
4685 1
4686#else
4687 0
4688#endif
4689 },
4690 {"crypt-blowfish",
4691#ifdef FEAT_CRYPT
4692 1
4693#else
4694 0
4695#endif
4696 },
4697 {"crypt-blowfish2",
4698#ifdef FEAT_CRYPT
4699 1
4700#else
4701 0
4702#endif
4703 },
4704 {"cscope",
4705#ifdef FEAT_CSCOPE
4706 1
4707#else
4708 0
4709#endif
4710 },
4711 {"cursorbind", 1},
4712 {"cursorshape",
4713#ifdef CURSOR_SHAPE
4714 1
4715#else
4716 0
4717#endif
4718 },
4719 {"debug",
4720#ifdef DEBUG
4721 1
4722#else
4723 0
4724#endif
4725 },
4726 {"dialog_con",
4727#ifdef FEAT_CON_DIALOG
4728 1
4729#else
4730 0
4731#endif
4732 },
4733 {"dialog_gui",
4734#ifdef FEAT_GUI_DIALOG
4735 1
4736#else
4737 0
4738#endif
4739 },
4740 {"diff",
4741#ifdef FEAT_DIFF
4742 1
4743#else
4744 0
4745#endif
4746 },
4747 {"digraphs",
4748#ifdef FEAT_DIGRAPHS
4749 1
4750#else
4751 0
4752#endif
4753 },
4754 {"directx",
4755#ifdef FEAT_DIRECTX
4756 1
4757#else
4758 0
4759#endif
4760 },
4761 {"dnd",
4762#ifdef FEAT_DND
4763 1
4764#else
4765 0
4766#endif
4767 },
4768 {"emacs_tags",
4769#ifdef FEAT_EMACS_TAGS
4770 1
4771#else
4772 0
4773#endif
4774 },
4775 {"eval", 1}, // always present, of course!
4776 {"ex_extra", 1}, // graduated feature
4777 {"extra_search",
4778#ifdef FEAT_SEARCH_EXTRA
4779 1
4780#else
4781 0
4782#endif
4783 },
4784 {"file_in_path",
4785#ifdef FEAT_SEARCHPATH
4786 1
4787#else
4788 0
4789#endif
4790 },
4791 {"filterpipe",
4792#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4793 1
4794#else
4795 0
4796#endif
4797 },
4798 {"find_in_path",
4799#ifdef FEAT_FIND_ID
4800 1
4801#else
4802 0
4803#endif
4804 },
4805 {"float",
4806#ifdef FEAT_FLOAT
4807 1
4808#else
4809 0
4810#endif
4811 },
4812 {"folding",
4813#ifdef FEAT_FOLDING
4814 1
4815#else
4816 0
4817#endif
4818 },
4819 {"footer",
4820#ifdef FEAT_FOOTER
4821 1
4822#else
4823 0
4824#endif
4825 },
4826 {"fork",
4827#if !defined(USE_SYSTEM) && defined(UNIX)
4828 1
4829#else
4830 0
4831#endif
4832 },
4833 {"gettext",
4834#ifdef FEAT_GETTEXT
4835 1
4836#else
4837 0
4838#endif
4839 },
4840 {"gui",
4841#ifdef FEAT_GUI
4842 1
4843#else
4844 0
4845#endif
4846 },
4847 {"gui_neXtaw",
4848#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4849 1
4850#else
4851 0
4852#endif
4853 },
4854 {"gui_athena",
4855#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4856 1
4857#else
4858 0
4859#endif
4860 },
4861 {"gui_gtk",
4862#ifdef FEAT_GUI_GTK
4863 1
4864#else
4865 0
4866#endif
4867 },
4868 {"gui_gtk2",
4869#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4870 1
4871#else
4872 0
4873#endif
4874 },
4875 {"gui_gtk3",
4876#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4877 1
4878#else
4879 0
4880#endif
4881 },
4882 {"gui_gnome",
4883#ifdef FEAT_GUI_GNOME
4884 1
4885#else
4886 0
4887#endif
4888 },
4889 {"gui_haiku",
4890#ifdef FEAT_GUI_HAIKU
4891 1
4892#else
4893 0
4894#endif
4895 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004896 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004897 {"gui_motif",
4898#ifdef FEAT_GUI_MOTIF
4899 1
4900#else
4901 0
4902#endif
4903 },
4904 {"gui_photon",
4905#ifdef FEAT_GUI_PHOTON
4906 1
4907#else
4908 0
4909#endif
4910 },
4911 {"gui_win32",
4912#ifdef FEAT_GUI_MSWIN
4913 1
4914#else
4915 0
4916#endif
4917 },
4918 {"iconv",
4919#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4920 1
4921#else
4922 0
4923#endif
4924 },
4925 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004926 {"ipv6",
4927#ifdef FEAT_IPV6
4928 1
4929#else
4930 0
4931#endif
4932 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004933 {"job",
4934#ifdef FEAT_JOB_CHANNEL
4935 1
4936#else
4937 0
4938#endif
4939 },
4940 {"jumplist",
4941#ifdef FEAT_JUMPLIST
4942 1
4943#else
4944 0
4945#endif
4946 },
4947 {"keymap",
4948#ifdef FEAT_KEYMAP
4949 1
4950#else
4951 0
4952#endif
4953 },
4954 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4955 {"langmap",
4956#ifdef FEAT_LANGMAP
4957 1
4958#else
4959 0
4960#endif
4961 },
4962 {"libcall",
4963#ifdef FEAT_LIBCALL
4964 1
4965#else
4966 0
4967#endif
4968 },
4969 {"linebreak",
4970#ifdef FEAT_LINEBREAK
4971 1
4972#else
4973 0
4974#endif
4975 },
4976 {"lispindent",
4977#ifdef FEAT_LISP
4978 1
4979#else
4980 0
4981#endif
4982 },
4983 {"listcmds", 1},
4984 {"localmap", 1},
4985 {"lua",
4986#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4987 1
4988#else
4989 0
4990#endif
4991 },
4992 {"menu",
4993#ifdef FEAT_MENU
4994 1
4995#else
4996 0
4997#endif
4998 },
4999 {"mksession",
5000#ifdef FEAT_SESSION
5001 1
5002#else
5003 0
5004#endif
5005 },
5006 {"modify_fname", 1},
5007 {"mouse", 1},
5008 {"mouseshape",
5009#ifdef FEAT_MOUSESHAPE
5010 1
5011#else
5012 0
5013#endif
5014 },
5015 {"mouse_dec",
5016#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
5017 1
5018#else
5019 0
5020#endif
5021 },
5022 {"mouse_gpm",
5023#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
5024 1
5025#else
5026 0
5027#endif
5028 },
5029 {"mouse_jsbterm",
5030#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
5031 1
5032#else
5033 0
5034#endif
5035 },
5036 {"mouse_netterm",
5037#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
5038 1
5039#else
5040 0
5041#endif
5042 },
5043 {"mouse_pterm",
5044#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
5045 1
5046#else
5047 0
5048#endif
5049 },
5050 {"mouse_sgr",
5051#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5052 1
5053#else
5054 0
5055#endif
5056 },
5057 {"mouse_sysmouse",
5058#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
5059 1
5060#else
5061 0
5062#endif
5063 },
5064 {"mouse_urxvt",
5065#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
5066 1
5067#else
5068 0
5069#endif
5070 },
5071 {"mouse_xterm",
5072#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5073 1
5074#else
5075 0
5076#endif
5077 },
5078 {"multi_byte", 1},
5079 {"multi_byte_ime",
5080#ifdef FEAT_MBYTE_IME
5081 1
5082#else
5083 0
5084#endif
5085 },
5086 {"multi_lang",
5087#ifdef FEAT_MULTI_LANG
5088 1
5089#else
5090 0
5091#endif
5092 },
5093 {"mzscheme",
5094#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
5095 1
5096#else
5097 0
5098#endif
5099 },
5100 {"num64", 1},
5101 {"ole",
5102#ifdef FEAT_OLE
5103 1
5104#else
5105 0
5106#endif
5107 },
5108 {"packages",
5109#ifdef FEAT_EVAL
5110 1
5111#else
5112 0
5113#endif
5114 },
5115 {"path_extra",
5116#ifdef FEAT_PATH_EXTRA
5117 1
5118#else
5119 0
5120#endif
5121 },
5122 {"perl",
5123#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
5124 1
5125#else
5126 0
5127#endif
5128 },
5129 {"persistent_undo",
5130#ifdef FEAT_PERSISTENT_UNDO
5131 1
5132#else
5133 0
5134#endif
5135 },
5136 {"python_compiled",
5137#if defined(FEAT_PYTHON)
5138 1
5139#else
5140 0
5141#endif
5142 },
5143 {"python_dynamic",
5144#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
5145 1
5146#else
5147 0
5148#endif
5149 },
5150 {"python",
5151#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
5152 1
5153#else
5154 0
5155#endif
5156 },
5157 {"pythonx",
5158#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
5159 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
5160 1
5161#else
5162 0
5163#endif
5164 },
5165 {"python3_compiled",
5166#if defined(FEAT_PYTHON3)
5167 1
5168#else
5169 0
5170#endif
5171 },
5172 {"python3_dynamic",
5173#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
5174 1
5175#else
5176 0
5177#endif
5178 },
5179 {"python3",
5180#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
5181 1
5182#else
5183 0
5184#endif
5185 },
5186 {"popupwin",
5187#ifdef FEAT_PROP_POPUP
5188 1
5189#else
5190 0
5191#endif
5192 },
5193 {"postscript",
5194#ifdef FEAT_POSTSCRIPT
5195 1
5196#else
5197 0
5198#endif
5199 },
5200 {"printer",
5201#ifdef FEAT_PRINTER
5202 1
5203#else
5204 0
5205#endif
5206 },
5207 {"profile",
5208#ifdef FEAT_PROFILE
5209 1
5210#else
5211 0
5212#endif
5213 },
5214 {"reltime",
5215#ifdef FEAT_RELTIME
5216 1
5217#else
5218 0
5219#endif
5220 },
5221 {"quickfix",
5222#ifdef FEAT_QUICKFIX
5223 1
5224#else
5225 0
5226#endif
5227 },
5228 {"rightleft",
5229#ifdef FEAT_RIGHTLEFT
5230 1
5231#else
5232 0
5233#endif
5234 },
5235 {"ruby",
5236#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5237 1
5238#else
5239 0
5240#endif
5241 },
5242 {"scrollbind", 1},
5243 {"showcmd",
5244#ifdef FEAT_CMDL_INFO
5245 1
5246#else
5247 0
5248#endif
5249 },
5250 {"cmdline_info",
5251#ifdef FEAT_CMDL_INFO
5252 1
5253#else
5254 0
5255#endif
5256 },
5257 {"signs",
5258#ifdef FEAT_SIGNS
5259 1
5260#else
5261 0
5262#endif
5263 },
5264 {"smartindent",
5265#ifdef FEAT_SMARTINDENT
5266 1
5267#else
5268 0
5269#endif
5270 },
5271 {"startuptime",
5272#ifdef STARTUPTIME
5273 1
5274#else
5275 0
5276#endif
5277 },
5278 {"statusline",
5279#ifdef FEAT_STL_OPT
5280 1
5281#else
5282 0
5283#endif
5284 },
5285 {"netbeans_intg",
5286#ifdef FEAT_NETBEANS_INTG
5287 1
5288#else
5289 0
5290#endif
5291 },
5292 {"sound",
5293#ifdef FEAT_SOUND
5294 1
5295#else
5296 0
5297#endif
5298 },
5299 {"spell",
5300#ifdef FEAT_SPELL
5301 1
5302#else
5303 0
5304#endif
5305 },
5306 {"syntax",
5307#ifdef FEAT_SYN_HL
5308 1
5309#else
5310 0
5311#endif
5312 },
5313 {"system",
5314#if defined(USE_SYSTEM) || !defined(UNIX)
5315 1
5316#else
5317 0
5318#endif
5319 },
5320 {"tag_binary",
5321#ifdef FEAT_TAG_BINS
5322 1
5323#else
5324 0
5325#endif
5326 },
5327 {"tcl",
5328#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5329 1
5330#else
5331 0
5332#endif
5333 },
5334 {"termguicolors",
5335#ifdef FEAT_TERMGUICOLORS
5336 1
5337#else
5338 0
5339#endif
5340 },
5341 {"terminal",
5342#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5343 1
5344#else
5345 0
5346#endif
5347 },
5348 {"terminfo",
5349#ifdef TERMINFO
5350 1
5351#else
5352 0
5353#endif
5354 },
5355 {"termresponse",
5356#ifdef FEAT_TERMRESPONSE
5357 1
5358#else
5359 0
5360#endif
5361 },
5362 {"textobjects",
5363#ifdef FEAT_TEXTOBJ
5364 1
5365#else
5366 0
5367#endif
5368 },
5369 {"textprop",
5370#ifdef FEAT_PROP_POPUP
5371 1
5372#else
5373 0
5374#endif
5375 },
5376 {"tgetent",
5377#ifdef HAVE_TGETENT
5378 1
5379#else
5380 0
5381#endif
5382 },
5383 {"timers",
5384#ifdef FEAT_TIMERS
5385 1
5386#else
5387 0
5388#endif
5389 },
5390 {"title",
5391#ifdef FEAT_TITLE
5392 1
5393#else
5394 0
5395#endif
5396 },
5397 {"toolbar",
5398#ifdef FEAT_TOOLBAR
5399 1
5400#else
5401 0
5402#endif
5403 },
5404 {"unnamedplus",
5405#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5406 1
5407#else
5408 0
5409#endif
5410 },
5411 {"user-commands", 1}, // was accidentally included in 5.4
5412 {"user_commands", 1},
5413 {"vartabs",
5414#ifdef FEAT_VARTABS
5415 1
5416#else
5417 0
5418#endif
5419 },
5420 {"vertsplit", 1},
5421 {"viminfo",
5422#ifdef FEAT_VIMINFO
5423 1
5424#else
5425 0
5426#endif
5427 },
5428 {"vimscript-1", 1},
5429 {"vimscript-2", 1},
5430 {"vimscript-3", 1},
5431 {"vimscript-4", 1},
5432 {"virtualedit", 1},
5433 {"visual", 1},
5434 {"visualextra", 1},
5435 {"vreplace", 1},
5436 {"vtp",
5437#ifdef FEAT_VTP
5438 1
5439#else
5440 0
5441#endif
5442 },
5443 {"wildignore",
5444#ifdef FEAT_WILDIGN
5445 1
5446#else
5447 0
5448#endif
5449 },
5450 {"wildmenu",
5451#ifdef FEAT_WILDMENU
5452 1
5453#else
5454 0
5455#endif
5456 },
5457 {"windows", 1},
5458 {"winaltkeys",
5459#ifdef FEAT_WAK
5460 1
5461#else
5462 0
5463#endif
5464 },
5465 {"writebackup",
5466#ifdef FEAT_WRITEBACKUP
5467 1
5468#else
5469 0
5470#endif
5471 },
5472 {"xim",
5473#ifdef FEAT_XIM
5474 1
5475#else
5476 0
5477#endif
5478 },
5479 {"xfontset",
5480#ifdef FEAT_XFONTSET
5481 1
5482#else
5483 0
5484#endif
5485 },
5486 {"xpm",
5487#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5488 1
5489#else
5490 0
5491#endif
5492 },
5493 {"xpm_w32", // for backward compatibility
5494#ifdef FEAT_XPM_W32
5495 1
5496#else
5497 0
5498#endif
5499 },
5500 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005501#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005502 1
5503#else
5504 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005505#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005506 },
5507 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005508#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005509 1
5510#else
5511 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005512#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005513 },
5514 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005515#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005516 1
5517#else
5518 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005519#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005520 },
5521 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005522#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005523 1
5524#else
5525 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005526#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005527 },
5528 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005529#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005530 1
5531#else
5532 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005533#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005534 },
5535 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005536 };
5537
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005538 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005539 for (i = 0; has_list[i].name != NULL; ++i)
5540 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005541 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005542 x = TRUE;
5543 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005544 break;
5545 }
5546
Bram Moolenaar79296512020-03-22 16:17:14 +01005547 // features also in has_list[] but sometimes enabled at runtime
5548 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005549 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005550 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005551 {
5552 // intentionally empty
5553 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005554#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005555 else if (STRICMP(name, "balloon_multiline") == 0)
5556 n = multiline_balloon_available();
5557#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005558#ifdef VIMDLL
5559 else if (STRICMP(name, "filterpipe") == 0)
5560 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005561#endif
5562#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5563 else if (STRICMP(name, "iconv") == 0)
5564 n = iconv_enabled(FALSE);
5565#endif
5566#ifdef DYNAMIC_LUA
5567 else if (STRICMP(name, "lua") == 0)
5568 n = lua_enabled(FALSE);
5569#endif
5570#ifdef DYNAMIC_MZSCHEME
5571 else if (STRICMP(name, "mzscheme") == 0)
5572 n = mzscheme_enabled(FALSE);
5573#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005574#ifdef DYNAMIC_PERL
5575 else if (STRICMP(name, "perl") == 0)
5576 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005577#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005578#ifdef DYNAMIC_PYTHON
5579 else if (STRICMP(name, "python") == 0)
5580 n = python_enabled(FALSE);
5581#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005582#ifdef DYNAMIC_PYTHON3
5583 else if (STRICMP(name, "python3") == 0)
5584 n = python3_enabled(FALSE);
5585#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005586#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5587 else if (STRICMP(name, "pythonx") == 0)
5588 {
5589# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5590 if (p_pyx == 0)
5591 n = python3_enabled(FALSE) || python_enabled(FALSE);
5592 else if (p_pyx == 3)
5593 n = python3_enabled(FALSE);
5594 else if (p_pyx == 2)
5595 n = python_enabled(FALSE);
5596# elif defined(DYNAMIC_PYTHON)
5597 n = python_enabled(FALSE);
5598# elif defined(DYNAMIC_PYTHON3)
5599 n = python3_enabled(FALSE);
5600# endif
5601 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005602#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005603#ifdef DYNAMIC_RUBY
5604 else if (STRICMP(name, "ruby") == 0)
5605 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005606#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005607#ifdef DYNAMIC_TCL
5608 else if (STRICMP(name, "tcl") == 0)
5609 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005610#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005611#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005612 else if (STRICMP(name, "terminal") == 0)
5613 n = terminal_enabled();
5614#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005615 }
5616
Bram Moolenaar79296512020-03-22 16:17:14 +01005617 // features not in has_list[]
5618 if (x == FALSE)
5619 {
5620 if (STRNICMP(name, "patch", 5) == 0)
5621 {
5622 x = TRUE;
5623 if (name[5] == '-'
5624 && STRLEN(name) >= 11
5625 && vim_isdigit(name[6])
5626 && vim_isdigit(name[8])
5627 && vim_isdigit(name[10]))
5628 {
5629 int major = atoi((char *)name + 6);
5630 int minor = atoi((char *)name + 8);
5631
5632 // Expect "patch-9.9.01234".
5633 n = (major < VIM_VERSION_MAJOR
5634 || (major == VIM_VERSION_MAJOR
5635 && (minor < VIM_VERSION_MINOR
5636 || (minor == VIM_VERSION_MINOR
5637 && has_patch(atoi((char *)name + 10))))));
5638 }
5639 else
5640 n = has_patch(atoi((char *)name + 5));
5641 }
5642 else if (STRICMP(name, "vim_starting") == 0)
5643 {
5644 x = TRUE;
5645 n = (starting != 0);
5646 }
5647 else if (STRICMP(name, "ttyin") == 0)
5648 {
5649 x = TRUE;
5650 n = mch_input_isatty();
5651 }
5652 else if (STRICMP(name, "ttyout") == 0)
5653 {
5654 x = TRUE;
5655 n = stdout_isatty;
5656 }
5657 else if (STRICMP(name, "multi_byte_encoding") == 0)
5658 {
5659 x = TRUE;
5660 n = has_mbyte;
5661 }
5662 else if (STRICMP(name, "gui_running") == 0)
5663 {
5664 x = TRUE;
5665#ifdef FEAT_GUI
5666 n = (gui.in_use || gui.starting);
5667#endif
5668 }
5669 else if (STRICMP(name, "browse") == 0)
5670 {
5671 x = TRUE;
5672#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5673 n = gui.in_use; // gui_mch_browse() works when GUI is running
5674#endif
5675 }
5676 else if (STRICMP(name, "syntax_items") == 0)
5677 {
5678 x = TRUE;
5679#ifdef FEAT_SYN_HL
5680 n = syntax_present(curwin);
5681#endif
5682 }
5683 else if (STRICMP(name, "vcon") == 0)
5684 {
5685 x = TRUE;
5686#ifdef FEAT_VTP
5687 n = is_term_win32() && has_vtp_working();
5688#endif
5689 }
5690 else if (STRICMP(name, "netbeans_enabled") == 0)
5691 {
5692 x = TRUE;
5693#ifdef FEAT_NETBEANS_INTG
5694 n = netbeans_active();
5695#endif
5696 }
5697 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5698 {
5699 x = TRUE;
5700#ifdef FEAT_MOUSE_GPM
5701 n = gpm_enabled();
5702#endif
5703 }
5704 else if (STRICMP(name, "conpty") == 0)
5705 {
5706 x = TRUE;
5707#if defined(FEAT_TERMINAL) && defined(MSWIN)
5708 n = use_conpty();
5709#endif
5710 }
5711 else if (STRICMP(name, "clipboard_working") == 0)
5712 {
5713 x = TRUE;
5714#ifdef FEAT_CLIPBOARD
5715 n = clip_star.available;
5716#endif
5717 }
5718 }
5719
Bram Moolenaar04637e22020-09-05 18:45:29 +02005720 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005721 // return whether feature could ever be enabled
5722 rettv->vval.v_number = x;
5723 else
5724 // return whether feature is enabled
5725 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005726}
5727
5728/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005729 * Return TRUE if "feature" can change later.
5730 * Also when checking for the feature has side effects, such as loading a DLL.
5731 */
5732 int
5733dynamic_feature(char_u *feature)
5734{
5735 return (feature == NULL
5736#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5737 || STRICMP(feature, "balloon_multiline") == 0
5738#endif
5739#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5740 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5741#endif
5742#ifdef VIMDLL
5743 || STRICMP(feature, "filterpipe") == 0
5744#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005745#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005746 // this can only change on Unix where the ":gui" command could be
5747 // used.
5748 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5749#endif
5750#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5751 || STRICMP(feature, "iconv") == 0
5752#endif
5753#ifdef DYNAMIC_LUA
5754 || STRICMP(feature, "lua") == 0
5755#endif
5756#ifdef FEAT_MOUSE_GPM
5757 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5758#endif
5759#ifdef DYNAMIC_MZSCHEME
5760 || STRICMP(feature, "mzscheme") == 0
5761#endif
5762#ifdef FEAT_NETBEANS_INTG
5763 || STRICMP(feature, "netbeans_enabled") == 0
5764#endif
5765#ifdef DYNAMIC_PERL
5766 || STRICMP(feature, "perl") == 0
5767#endif
5768#ifdef DYNAMIC_PYTHON
5769 || STRICMP(feature, "python") == 0
5770#endif
5771#ifdef DYNAMIC_PYTHON3
5772 || STRICMP(feature, "python3") == 0
5773#endif
5774#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5775 || STRICMP(feature, "pythonx") == 0
5776#endif
5777#ifdef DYNAMIC_RUBY
5778 || STRICMP(feature, "ruby") == 0
5779#endif
5780#ifdef FEAT_SYN_HL
5781 || STRICMP(feature, "syntax_items") == 0
5782#endif
5783#ifdef DYNAMIC_TCL
5784 || STRICMP(feature, "tcl") == 0
5785#endif
5786 // once "starting" is zero it will stay that way
5787 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5788 || STRICMP(feature, "multi_byte_encoding") == 0
5789#if defined(FEAT_TERMINAL) && defined(MSWIN)
5790 || STRICMP(feature, "conpty") == 0
5791#endif
5792 );
5793}
5794
5795/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005796 * "haslocaldir()" function
5797 */
5798 static void
5799f_haslocaldir(typval_T *argvars, typval_T *rettv)
5800{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005801 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005802 win_T *wp = NULL;
5803
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005804 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5805
5806 // Check for window-local and tab-local directories
5807 if (wp != NULL && wp->w_localdir != NULL)
5808 rettv->vval.v_number = 1;
5809 else if (tp != NULL && tp->tp_localdir != NULL)
5810 rettv->vval.v_number = 2;
5811 else
5812 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005813}
5814
5815/*
5816 * "hasmapto()" function
5817 */
5818 static void
5819f_hasmapto(typval_T *argvars, typval_T *rettv)
5820{
5821 char_u *name;
5822 char_u *mode;
5823 char_u buf[NUMBUFLEN];
5824 int abbr = FALSE;
5825
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005826 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005827 if (argvars[1].v_type == VAR_UNKNOWN)
5828 mode = (char_u *)"nvo";
5829 else
5830 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005831 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005832 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005833 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005834 }
5835
5836 if (map_to_exists(name, mode, abbr))
5837 rettv->vval.v_number = TRUE;
5838 else
5839 rettv->vval.v_number = FALSE;
5840}
5841
5842/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005843 * "highlightID(name)" function
5844 */
5845 static void
5846f_hlID(typval_T *argvars, typval_T *rettv)
5847{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005848 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005849}
5850
5851/*
5852 * "highlight_exists()" function
5853 */
5854 static void
5855f_hlexists(typval_T *argvars, typval_T *rettv)
5856{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005857 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005858}
5859
5860/*
5861 * "hostname()" function
5862 */
5863 static void
5864f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5865{
5866 char_u hostname[256];
5867
5868 mch_get_host_name(hostname, 256);
5869 rettv->v_type = VAR_STRING;
5870 rettv->vval.v_string = vim_strsave(hostname);
5871}
5872
5873/*
5874 * iconv() function
5875 */
5876 static void
5877f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5878{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005879 char_u buf1[NUMBUFLEN];
5880 char_u buf2[NUMBUFLEN];
5881 char_u *from, *to, *str;
5882 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005883
5884 rettv->v_type = VAR_STRING;
5885 rettv->vval.v_string = NULL;
5886
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005887 str = tv_get_string(&argvars[0]);
5888 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5889 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005890 vimconv.vc_type = CONV_NONE;
5891 convert_setup(&vimconv, from, to);
5892
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005893 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005894 if (vimconv.vc_type == CONV_NONE)
5895 rettv->vval.v_string = vim_strsave(str);
5896 else
5897 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5898
5899 convert_setup(&vimconv, NULL, NULL);
5900 vim_free(from);
5901 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005902}
5903
5904/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005905 * "index()" function
5906 */
5907 static void
5908f_index(typval_T *argvars, typval_T *rettv)
5909{
5910 list_T *l;
5911 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005912 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005913 long idx = 0;
5914 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005915 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005916
5917 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005918 if (argvars[0].v_type == VAR_BLOB)
5919 {
5920 typval_T tv;
5921 int start = 0;
5922
5923 if (argvars[2].v_type != VAR_UNKNOWN)
5924 {
5925 start = tv_get_number_chk(&argvars[2], &error);
5926 if (error)
5927 return;
5928 }
5929 b = argvars[0].vval.v_blob;
5930 if (b == NULL)
5931 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005932 if (start < 0)
5933 {
5934 start = blob_len(b) + start;
5935 if (start < 0)
5936 start = 0;
5937 }
5938
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005939 for (idx = start; idx < blob_len(b); ++idx)
5940 {
5941 tv.v_type = VAR_NUMBER;
5942 tv.vval.v_number = blob_get(b, idx);
5943 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5944 {
5945 rettv->vval.v_number = idx;
5946 return;
5947 }
5948 }
5949 return;
5950 }
5951 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005952 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005953 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005954 return;
5955 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005956
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005957 l = argvars[0].vval.v_list;
5958 if (l != NULL)
5959 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005960 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005961 item = l->lv_first;
5962 if (argvars[2].v_type != VAR_UNKNOWN)
5963 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005964 // Start at specified item. Use the cached index that list_find()
5965 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005966 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005967 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005968 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005969 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005970 if (error)
5971 item = NULL;
5972 }
5973
5974 for ( ; item != NULL; item = item->li_next, ++idx)
5975 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5976 {
5977 rettv->vval.v_number = idx;
5978 break;
5979 }
5980 }
5981}
5982
5983static int inputsecret_flag = 0;
5984
5985/*
5986 * "input()" function
5987 * Also handles inputsecret() when inputsecret is set.
5988 */
5989 static void
5990f_input(typval_T *argvars, typval_T *rettv)
5991{
5992 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5993}
5994
5995/*
5996 * "inputdialog()" function
5997 */
5998 static void
5999f_inputdialog(typval_T *argvars, typval_T *rettv)
6000{
6001#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006002 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006003 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6004 {
6005 char_u *message;
6006 char_u buf[NUMBUFLEN];
6007 char_u *defstr = (char_u *)"";
6008
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006009 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006010 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006011 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006012 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6013 else
6014 IObuff[0] = NUL;
6015 if (message != NULL && defstr != NULL
6016 && do_dialog(VIM_QUESTION, NULL, message,
6017 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6018 rettv->vval.v_string = vim_strsave(IObuff);
6019 else
6020 {
6021 if (message != NULL && defstr != NULL
6022 && argvars[1].v_type != VAR_UNKNOWN
6023 && argvars[2].v_type != VAR_UNKNOWN)
6024 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006025 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006026 else
6027 rettv->vval.v_string = NULL;
6028 }
6029 rettv->v_type = VAR_STRING;
6030 }
6031 else
6032#endif
6033 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6034}
6035
6036/*
6037 * "inputlist()" function
6038 */
6039 static void
6040f_inputlist(typval_T *argvars, typval_T *rettv)
6041{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006042 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006043 listitem_T *li;
6044 int selected;
6045 int mouse_used;
6046
6047#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006048 // While starting up, there is no place to enter text. When running tests
6049 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006050 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006051 return;
6052#endif
6053 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6054 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006055 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006056 return;
6057 }
6058
6059 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006060 msg_row = Rows - 1; // for when 'cmdheight' > 1
6061 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006062 msg_scroll = TRUE;
6063 msg_clr_eos();
6064
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006065 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006066 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006067 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006068 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006069 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006070 msg_putchar('\n');
6071 }
6072
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006073 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006074 selected = prompt_for_number(&mouse_used);
6075 if (mouse_used)
6076 selected -= lines_left;
6077
6078 rettv->vval.v_number = selected;
6079}
6080
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006081static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6082
6083/*
6084 * "inputrestore()" function
6085 */
6086 static void
6087f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6088{
6089 if (ga_userinput.ga_len > 0)
6090 {
6091 --ga_userinput.ga_len;
6092 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6093 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006094 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006095 }
6096 else if (p_verbose > 1)
6097 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006098 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006099 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006100 }
6101}
6102
6103/*
6104 * "inputsave()" function
6105 */
6106 static void
6107f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6108{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006109 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006110 if (ga_grow(&ga_userinput, 1) == OK)
6111 {
6112 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6113 + ga_userinput.ga_len);
6114 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006115 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006116 }
6117 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006118 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006119}
6120
6121/*
6122 * "inputsecret()" function
6123 */
6124 static void
6125f_inputsecret(typval_T *argvars, typval_T *rettv)
6126{
6127 ++cmdline_star;
6128 ++inputsecret_flag;
6129 f_input(argvars, rettv);
6130 --cmdline_star;
6131 --inputsecret_flag;
6132}
6133
6134/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01006135 * "interrupt()" function
6136 */
6137 static void
6138f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6139{
6140 got_int = TRUE;
6141}
6142
6143/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006144 * "invert(expr)" function
6145 */
6146 static void
6147f_invert(typval_T *argvars, typval_T *rettv)
6148{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006149 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006150}
6151
6152/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006153 * "islocked()" function
6154 */
6155 static void
6156f_islocked(typval_T *argvars, typval_T *rettv)
6157{
6158 lval_T lv;
6159 char_u *end;
6160 dictitem_T *di;
6161
6162 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006163 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006164 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006165 if (end != NULL && lv.ll_name != NULL)
6166 {
6167 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02006168 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006169 else
6170 {
6171 if (lv.ll_tv == NULL)
6172 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006173 di = find_var(lv.ll_name, NULL, TRUE);
6174 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006175 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006176 // Consider a variable locked when:
6177 // 1. the variable itself is locked
6178 // 2. the value of the variable is locked.
6179 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01006180 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6181 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006182 }
6183 }
6184 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006185 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006186 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006187 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006188 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006189 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006190 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6191 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006192 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006193 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6194 }
6195 }
6196
6197 clear_lval(&lv);
6198}
6199
6200#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6201/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006202 * "isinf()" function
6203 */
6204 static void
6205f_isinf(typval_T *argvars, typval_T *rettv)
6206{
6207 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6208 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6209}
6210
6211/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006212 * "isnan()" function
6213 */
6214 static void
6215f_isnan(typval_T *argvars, typval_T *rettv)
6216{
6217 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6218 && isnan(argvars[0].vval.v_float);
6219}
6220#endif
6221
6222/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006223 * "last_buffer_nr()" function.
6224 */
6225 static void
6226f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6227{
6228 int n = 0;
6229 buf_T *buf;
6230
Bram Moolenaar29323592016-07-24 22:04:11 +02006231 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006232 if (n < buf->b_fnum)
6233 n = buf->b_fnum;
6234
6235 rettv->vval.v_number = n;
6236}
6237
6238/*
6239 * "len()" function
6240 */
6241 static void
6242f_len(typval_T *argvars, typval_T *rettv)
6243{
6244 switch (argvars[0].v_type)
6245 {
6246 case VAR_STRING:
6247 case VAR_NUMBER:
6248 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006249 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006250 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006251 case VAR_BLOB:
6252 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6253 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006254 case VAR_LIST:
6255 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6256 break;
6257 case VAR_DICT:
6258 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6259 break;
6260 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006261 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006262 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006263 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006264 case VAR_SPECIAL:
6265 case VAR_FLOAT:
6266 case VAR_FUNC:
6267 case VAR_PARTIAL:
6268 case VAR_JOB:
6269 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006270 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006271 break;
6272 }
6273}
6274
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006275 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006276libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006277{
6278#ifdef FEAT_LIBCALL
6279 char_u *string_in;
6280 char_u **string_result;
6281 int nr_result;
6282#endif
6283
6284 rettv->v_type = type;
6285 if (type != VAR_NUMBER)
6286 rettv->vval.v_string = NULL;
6287
6288 if (check_restricted() || check_secure())
6289 return;
6290
6291#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006292 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006293 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6294 {
6295 string_in = NULL;
6296 if (argvars[2].v_type == VAR_STRING)
6297 string_in = argvars[2].vval.v_string;
6298 if (type == VAR_NUMBER)
6299 string_result = NULL;
6300 else
6301 string_result = &rettv->vval.v_string;
6302 if (mch_libcall(argvars[0].vval.v_string,
6303 argvars[1].vval.v_string,
6304 string_in,
6305 argvars[2].vval.v_number,
6306 string_result,
6307 &nr_result) == OK
6308 && type == VAR_NUMBER)
6309 rettv->vval.v_number = nr_result;
6310 }
6311#endif
6312}
6313
6314/*
6315 * "libcall()" function
6316 */
6317 static void
6318f_libcall(typval_T *argvars, typval_T *rettv)
6319{
6320 libcall_common(argvars, rettv, VAR_STRING);
6321}
6322
6323/*
6324 * "libcallnr()" function
6325 */
6326 static void
6327f_libcallnr(typval_T *argvars, typval_T *rettv)
6328{
6329 libcall_common(argvars, rettv, VAR_NUMBER);
6330}
6331
6332/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006333 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006334 */
6335 static void
6336f_line(typval_T *argvars, typval_T *rettv)
6337{
6338 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006339 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006340 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006341 int id;
6342 tabpage_T *tp;
6343 win_T *wp;
6344 win_T *save_curwin;
6345 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006346
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006347 if (argvars[1].v_type != VAR_UNKNOWN)
6348 {
6349 // use window specified in the second argument
6350 id = (int)tv_get_number(&argvars[1]);
6351 wp = win_id2wp_tp(id, &tp);
6352 if (wp != NULL && tp != NULL)
6353 {
6354 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6355 == OK)
6356 {
6357 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006358 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006359 }
6360 restore_win_noblock(save_curwin, save_curtab, TRUE);
6361 }
6362 }
6363 else
6364 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006365 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006366
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006367 if (fp != NULL)
6368 lnum = fp->lnum;
6369 rettv->vval.v_number = lnum;
6370}
6371
6372/*
6373 * "line2byte(lnum)" function
6374 */
6375 static void
6376f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6377{
6378#ifndef FEAT_BYTEOFF
6379 rettv->vval.v_number = -1;
6380#else
6381 linenr_T lnum;
6382
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006383 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006384 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6385 rettv->vval.v_number = -1;
6386 else
6387 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6388 if (rettv->vval.v_number >= 0)
6389 ++rettv->vval.v_number;
6390#endif
6391}
6392
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006393#ifdef FEAT_FLOAT
6394/*
6395 * "log()" function
6396 */
6397 static void
6398f_log(typval_T *argvars, typval_T *rettv)
6399{
6400 float_T f = 0.0;
6401
6402 rettv->v_type = VAR_FLOAT;
6403 if (get_float_arg(argvars, &f) == OK)
6404 rettv->vval.v_float = log(f);
6405 else
6406 rettv->vval.v_float = 0.0;
6407}
6408
6409/*
6410 * "log10()" function
6411 */
6412 static void
6413f_log10(typval_T *argvars, typval_T *rettv)
6414{
6415 float_T f = 0.0;
6416
6417 rettv->v_type = VAR_FLOAT;
6418 if (get_float_arg(argvars, &f) == OK)
6419 rettv->vval.v_float = log10(f);
6420 else
6421 rettv->vval.v_float = 0.0;
6422}
6423#endif
6424
6425#ifdef FEAT_LUA
6426/*
6427 * "luaeval()" function
6428 */
6429 static void
6430f_luaeval(typval_T *argvars, typval_T *rettv)
6431{
6432 char_u *str;
6433 char_u buf[NUMBUFLEN];
6434
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006435 if (check_restricted() || check_secure())
6436 return;
6437
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006438 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006439 do_luaeval(str, argvars + 1, rettv);
6440}
6441#endif
6442
6443/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006444 * "maparg()" function
6445 */
6446 static void
6447f_maparg(typval_T *argvars, typval_T *rettv)
6448{
6449 get_maparg(argvars, rettv, TRUE);
6450}
6451
6452/*
6453 * "mapcheck()" function
6454 */
6455 static void
6456f_mapcheck(typval_T *argvars, typval_T *rettv)
6457{
6458 get_maparg(argvars, rettv, FALSE);
6459}
6460
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006461typedef enum
6462{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006463 MATCH_END, // matchend()
6464 MATCH_MATCH, // match()
6465 MATCH_STR, // matchstr()
6466 MATCH_LIST, // matchlist()
6467 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006468} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006469
6470 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006471find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006472{
6473 char_u *str = NULL;
6474 long len = 0;
6475 char_u *expr = NULL;
6476 char_u *pat;
6477 regmatch_T regmatch;
6478 char_u patbuf[NUMBUFLEN];
6479 char_u strbuf[NUMBUFLEN];
6480 char_u *save_cpo;
6481 long start = 0;
6482 long nth = 1;
6483 colnr_T startcol = 0;
6484 int match = 0;
6485 list_T *l = NULL;
6486 listitem_T *li = NULL;
6487 long idx = 0;
6488 char_u *tofree = NULL;
6489
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006490 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006491 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006492 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006493
6494 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006495 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006496 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006497 // type MATCH_LIST: return empty list when there are no matches.
6498 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006499 if (rettv_list_alloc(rettv) == FAIL)
6500 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006501 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006502 && (list_append_string(rettv->vval.v_list,
6503 (char_u *)"", 0) == FAIL
6504 || list_append_number(rettv->vval.v_list,
6505 (varnumber_T)-1) == FAIL
6506 || list_append_number(rettv->vval.v_list,
6507 (varnumber_T)-1) == FAIL
6508 || list_append_number(rettv->vval.v_list,
6509 (varnumber_T)-1) == FAIL))
6510 {
6511 list_free(rettv->vval.v_list);
6512 rettv->vval.v_list = NULL;
6513 goto theend;
6514 }
6515 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006516 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006517 {
6518 rettv->v_type = VAR_STRING;
6519 rettv->vval.v_string = NULL;
6520 }
6521
6522 if (argvars[0].v_type == VAR_LIST)
6523 {
6524 if ((l = argvars[0].vval.v_list) == NULL)
6525 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006526 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006527 li = l->lv_first;
6528 }
6529 else
6530 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006531 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006532 len = (long)STRLEN(str);
6533 }
6534
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006535 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006536 if (pat == NULL)
6537 goto theend;
6538
6539 if (argvars[2].v_type != VAR_UNKNOWN)
6540 {
6541 int error = FALSE;
6542
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006543 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006544 if (error)
6545 goto theend;
6546 if (l != NULL)
6547 {
6548 li = list_find(l, start);
6549 if (li == NULL)
6550 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006551 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006552 }
6553 else
6554 {
6555 if (start < 0)
6556 start = 0;
6557 if (start > len)
6558 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006559 // When "count" argument is there ignore matches before "start",
6560 // otherwise skip part of the string. Differs when pattern is "^"
6561 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006562 if (argvars[3].v_type != VAR_UNKNOWN)
6563 startcol = start;
6564 else
6565 {
6566 str += start;
6567 len -= start;
6568 }
6569 }
6570
6571 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006572 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006573 if (error)
6574 goto theend;
6575 }
6576
6577 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6578 if (regmatch.regprog != NULL)
6579 {
6580 regmatch.rm_ic = p_ic;
6581
6582 for (;;)
6583 {
6584 if (l != NULL)
6585 {
6586 if (li == NULL)
6587 {
6588 match = FALSE;
6589 break;
6590 }
6591 vim_free(tofree);
6592 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6593 if (str == NULL)
6594 break;
6595 }
6596
6597 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6598
6599 if (match && --nth <= 0)
6600 break;
6601 if (l == NULL && !match)
6602 break;
6603
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006604 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006605 if (l != NULL)
6606 {
6607 li = li->li_next;
6608 ++idx;
6609 }
6610 else
6611 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006612 startcol = (colnr_T)(regmatch.startp[0]
6613 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006614 if (startcol > (colnr_T)len
6615 || str + startcol <= regmatch.startp[0])
6616 {
6617 match = FALSE;
6618 break;
6619 }
6620 }
6621 }
6622
6623 if (match)
6624 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006625 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006626 {
6627 listitem_T *li1 = rettv->vval.v_list->lv_first;
6628 listitem_T *li2 = li1->li_next;
6629 listitem_T *li3 = li2->li_next;
6630 listitem_T *li4 = li3->li_next;
6631
6632 vim_free(li1->li_tv.vval.v_string);
6633 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006634 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006635 li3->li_tv.vval.v_number =
6636 (varnumber_T)(regmatch.startp[0] - expr);
6637 li4->li_tv.vval.v_number =
6638 (varnumber_T)(regmatch.endp[0] - expr);
6639 if (l != NULL)
6640 li2->li_tv.vval.v_number = (varnumber_T)idx;
6641 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006642 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006643 {
6644 int i;
6645
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006646 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006647 for (i = 0; i < NSUBEXP; ++i)
6648 {
6649 if (regmatch.endp[i] == NULL)
6650 {
6651 if (list_append_string(rettv->vval.v_list,
6652 (char_u *)"", 0) == FAIL)
6653 break;
6654 }
6655 else if (list_append_string(rettv->vval.v_list,
6656 regmatch.startp[i],
6657 (int)(regmatch.endp[i] - regmatch.startp[i]))
6658 == FAIL)
6659 break;
6660 }
6661 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006662 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006663 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006664 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006665 if (l != NULL)
6666 copy_tv(&li->li_tv, rettv);
6667 else
6668 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006669 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006670 }
6671 else if (l != NULL)
6672 rettv->vval.v_number = idx;
6673 else
6674 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006675 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006676 rettv->vval.v_number =
6677 (varnumber_T)(regmatch.startp[0] - str);
6678 else
6679 rettv->vval.v_number =
6680 (varnumber_T)(regmatch.endp[0] - str);
6681 rettv->vval.v_number += (varnumber_T)(str - expr);
6682 }
6683 }
6684 vim_regfree(regmatch.regprog);
6685 }
6686
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006687theend:
6688 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006689 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006690 listitem_remove(rettv->vval.v_list,
6691 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006692 vim_free(tofree);
6693 p_cpo = save_cpo;
6694}
6695
6696/*
6697 * "match()" function
6698 */
6699 static void
6700f_match(typval_T *argvars, typval_T *rettv)
6701{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006702 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006703}
6704
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006705/*
6706 * "matchend()" function
6707 */
6708 static void
6709f_matchend(typval_T *argvars, typval_T *rettv)
6710{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006711 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006712}
6713
6714/*
6715 * "matchlist()" function
6716 */
6717 static void
6718f_matchlist(typval_T *argvars, typval_T *rettv)
6719{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006720 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006721}
6722
6723/*
6724 * "matchstr()" function
6725 */
6726 static void
6727f_matchstr(typval_T *argvars, typval_T *rettv)
6728{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006729 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006730}
6731
6732/*
6733 * "matchstrpos()" function
6734 */
6735 static void
6736f_matchstrpos(typval_T *argvars, typval_T *rettv)
6737{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006738 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006739}
6740
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006741 static void
6742max_min(typval_T *argvars, typval_T *rettv, int domax)
6743{
6744 varnumber_T n = 0;
6745 varnumber_T i;
6746 int error = FALSE;
6747
6748 if (argvars[0].v_type == VAR_LIST)
6749 {
6750 list_T *l;
6751 listitem_T *li;
6752
6753 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006754 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006755 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006756 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006757 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006758 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6759 n = l->lv_u.nonmat.lv_start;
6760 else
6761 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6762 * l->lv_u.nonmat.lv_stride;
6763 }
6764 else
6765 {
6766 li = l->lv_first;
6767 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006768 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006769 n = tv_get_number_chk(&li->li_tv, &error);
6770 for (;;)
6771 {
6772 li = li->li_next;
6773 if (li == NULL)
6774 break;
6775 i = tv_get_number_chk(&li->li_tv, &error);
6776 if (domax ? i > n : i < n)
6777 n = i;
6778 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006779 }
6780 }
6781 }
6782 }
6783 else if (argvars[0].v_type == VAR_DICT)
6784 {
6785 dict_T *d;
6786 int first = TRUE;
6787 hashitem_T *hi;
6788 int todo;
6789
6790 d = argvars[0].vval.v_dict;
6791 if (d != NULL)
6792 {
6793 todo = (int)d->dv_hashtab.ht_used;
6794 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6795 {
6796 if (!HASHITEM_EMPTY(hi))
6797 {
6798 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006799 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006800 if (first)
6801 {
6802 n = i;
6803 first = FALSE;
6804 }
6805 else if (domax ? i > n : i < n)
6806 n = i;
6807 }
6808 }
6809 }
6810 }
6811 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006812 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006813 rettv->vval.v_number = error ? 0 : n;
6814}
6815
6816/*
6817 * "max()" function
6818 */
6819 static void
6820f_max(typval_T *argvars, typval_T *rettv)
6821{
6822 max_min(argvars, rettv, TRUE);
6823}
6824
6825/*
6826 * "min()" function
6827 */
6828 static void
6829f_min(typval_T *argvars, typval_T *rettv)
6830{
6831 max_min(argvars, rettv, FALSE);
6832}
6833
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006834#if defined(FEAT_MZSCHEME) || defined(PROTO)
6835/*
6836 * "mzeval()" function
6837 */
6838 static void
6839f_mzeval(typval_T *argvars, typval_T *rettv)
6840{
6841 char_u *str;
6842 char_u buf[NUMBUFLEN];
6843
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006844 if (check_restricted() || check_secure())
6845 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006846 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006847 do_mzeval(str, rettv);
6848}
6849
6850 void
6851mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6852{
6853 typval_T argvars[3];
6854
6855 argvars[0].v_type = VAR_STRING;
6856 argvars[0].vval.v_string = name;
6857 copy_tv(args, &argvars[1]);
6858 argvars[2].v_type = VAR_UNKNOWN;
6859 f_call(argvars, rettv);
6860 clear_tv(&argvars[1]);
6861}
6862#endif
6863
6864/*
6865 * "nextnonblank()" function
6866 */
6867 static void
6868f_nextnonblank(typval_T *argvars, typval_T *rettv)
6869{
6870 linenr_T lnum;
6871
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006872 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006873 {
6874 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6875 {
6876 lnum = 0;
6877 break;
6878 }
6879 if (*skipwhite(ml_get(lnum)) != NUL)
6880 break;
6881 }
6882 rettv->vval.v_number = lnum;
6883}
6884
6885/*
6886 * "nr2char()" function
6887 */
6888 static void
6889f_nr2char(typval_T *argvars, typval_T *rettv)
6890{
6891 char_u buf[NUMBUFLEN];
6892
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006893 if (has_mbyte)
6894 {
6895 int utf8 = 0;
6896
6897 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006898 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006899 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006900 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006902 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006903 }
6904 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006905 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006906 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006907 buf[1] = NUL;
6908 }
6909 rettv->v_type = VAR_STRING;
6910 rettv->vval.v_string = vim_strsave(buf);
6911}
6912
6913/*
6914 * "or(expr, expr)" function
6915 */
6916 static void
6917f_or(typval_T *argvars, typval_T *rettv)
6918{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006919 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6920 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006921}
6922
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923#ifdef FEAT_PERL
6924/*
6925 * "perleval()" function
6926 */
6927 static void
6928f_perleval(typval_T *argvars, typval_T *rettv)
6929{
6930 char_u *str;
6931 char_u buf[NUMBUFLEN];
6932
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006933 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006934 do_perleval(str, rettv);
6935}
6936#endif
6937
6938#ifdef FEAT_FLOAT
6939/*
6940 * "pow()" function
6941 */
6942 static void
6943f_pow(typval_T *argvars, typval_T *rettv)
6944{
6945 float_T fx = 0.0, fy = 0.0;
6946
6947 rettv->v_type = VAR_FLOAT;
6948 if (get_float_arg(argvars, &fx) == OK
6949 && get_float_arg(&argvars[1], &fy) == OK)
6950 rettv->vval.v_float = pow(fx, fy);
6951 else
6952 rettv->vval.v_float = 0.0;
6953}
6954#endif
6955
6956/*
6957 * "prevnonblank()" function
6958 */
6959 static void
6960f_prevnonblank(typval_T *argvars, typval_T *rettv)
6961{
6962 linenr_T lnum;
6963
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006964 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006965 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6966 lnum = 0;
6967 else
6968 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6969 --lnum;
6970 rettv->vval.v_number = lnum;
6971}
6972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006973// This dummy va_list is here because:
6974// - passing a NULL pointer doesn't work when va_list isn't a pointer
6975// - locally in the function results in a "used before set" warning
6976// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006977static va_list ap;
6978
6979/*
6980 * "printf()" function
6981 */
6982 static void
6983f_printf(typval_T *argvars, typval_T *rettv)
6984{
6985 char_u buf[NUMBUFLEN];
6986 int len;
6987 char_u *s;
6988 int saved_did_emsg = did_emsg;
6989 char *fmt;
6990
6991 rettv->v_type = VAR_STRING;
6992 rettv->vval.v_string = NULL;
6993
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006994 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006995 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006996 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006997 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006998 if (!did_emsg)
6999 {
7000 s = alloc(len + 1);
7001 if (s != NULL)
7002 {
7003 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007004 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
7005 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007006 }
7007 }
7008 did_emsg |= saved_did_emsg;
7009}
7010
7011/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007012 * "pum_getpos()" function
7013 */
7014 static void
7015f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7016{
7017 if (rettv_dict_alloc(rettv) != OK)
7018 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007019 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007020}
7021
7022/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007023 * "pumvisible()" function
7024 */
7025 static void
7026f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7027{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028 if (pum_visible())
7029 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007030}
7031
7032#ifdef FEAT_PYTHON3
7033/*
7034 * "py3eval()" function
7035 */
7036 static void
7037f_py3eval(typval_T *argvars, typval_T *rettv)
7038{
7039 char_u *str;
7040 char_u buf[NUMBUFLEN];
7041
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007042 if (check_restricted() || check_secure())
7043 return;
7044
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007045 if (p_pyx == 0)
7046 p_pyx = 3;
7047
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007048 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007049 do_py3eval(str, rettv);
7050}
7051#endif
7052
7053#ifdef FEAT_PYTHON
7054/*
7055 * "pyeval()" function
7056 */
7057 static void
7058f_pyeval(typval_T *argvars, typval_T *rettv)
7059{
7060 char_u *str;
7061 char_u buf[NUMBUFLEN];
7062
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007063 if (check_restricted() || check_secure())
7064 return;
7065
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007066 if (p_pyx == 0)
7067 p_pyx = 2;
7068
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007069 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007070 do_pyeval(str, rettv);
7071}
7072#endif
7073
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007074#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
7075/*
7076 * "pyxeval()" function
7077 */
7078 static void
7079f_pyxeval(typval_T *argvars, typval_T *rettv)
7080{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007081 if (check_restricted() || check_secure())
7082 return;
7083
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007084# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
7085 init_pyxversion();
7086 if (p_pyx == 2)
7087 f_pyeval(argvars, rettv);
7088 else
7089 f_py3eval(argvars, rettv);
7090# elif defined(FEAT_PYTHON)
7091 f_pyeval(argvars, rettv);
7092# elif defined(FEAT_PYTHON3)
7093 f_py3eval(argvars, rettv);
7094# endif
7095}
7096#endif
7097
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007098static UINT32_T srand_seed_for_testing = 0;
7099static int srand_seed_for_testing_is_used = FALSE;
7100
7101 static void
7102f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
7103{
7104 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007105 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007106 else
7107 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007108 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
7109 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007110 }
7111}
7112
7113 static void
7114init_srand(UINT32_T *x)
7115{
7116#ifndef MSWIN
7117 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
7118#endif
7119
7120 if (srand_seed_for_testing_is_used)
7121 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007122 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007123 return;
7124 }
7125#ifndef MSWIN
7126 if (dev_urandom_state != FAIL)
7127 {
7128 int fd = open("/dev/urandom", O_RDONLY);
7129 struct {
7130 union {
7131 UINT32_T number;
7132 char bytes[sizeof(UINT32_T)];
7133 } contents;
7134 } buf;
7135
7136 // Attempt reading /dev/urandom.
7137 if (fd == -1)
7138 dev_urandom_state = FAIL;
7139 else
7140 {
7141 buf.contents.number = 0;
7142 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
7143 != sizeof(UINT32_T))
7144 dev_urandom_state = FAIL;
7145 else
7146 {
7147 dev_urandom_state = OK;
7148 *x = buf.contents.number;
7149 }
7150 close(fd);
7151 }
7152 }
7153 if (dev_urandom_state != OK)
7154 // Reading /dev/urandom doesn't work, fall back to time().
7155#endif
7156 *x = vim_time();
7157}
7158
7159#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
7160#define SPLITMIX32(x, z) ( \
7161 z = (x += 0x9e3779b9), \
7162 z = (z ^ (z >> 16)) * 0x85ebca6b, \
7163 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
7164 z ^ (z >> 16) \
7165 )
7166#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
7167 result = ROTL(y * 5, 7) * 9; \
7168 t = y << 9; \
7169 z ^= x; \
7170 w ^= y; \
7171 y ^= z, x ^= w; \
7172 z ^= t; \
7173 w = ROTL(w, 11);
7174
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007175/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007176 * "rand()" function
7177 */
7178 static void
7179f_rand(typval_T *argvars, typval_T *rettv)
7180{
7181 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007182 static UINT32_T gx, gy, gz, gw;
7183 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007184 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01007185 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007186
7187 if (argvars[0].v_type == VAR_UNKNOWN)
7188 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007189 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007190 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007191 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007192 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007193 init_srand(&x);
7194
7195 gx = SPLITMIX32(x, z);
7196 gy = SPLITMIX32(x, z);
7197 gz = SPLITMIX32(x, z);
7198 gw = SPLITMIX32(x, z);
7199 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007200 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007201
7202 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007203 }
7204 else if (argvars[0].v_type == VAR_LIST)
7205 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007206 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007207 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007208 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007209
7210 lx = list_find(l, 0L);
7211 ly = list_find(l, 1L);
7212 lz = list_find(l, 2L);
7213 lw = list_find(l, 3L);
7214 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
7215 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
7216 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
7217 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
7218 x = (UINT32_T)lx->li_tv.vval.v_number;
7219 y = (UINT32_T)ly->li_tv.vval.v_number;
7220 z = (UINT32_T)lz->li_tv.vval.v_number;
7221 w = (UINT32_T)lw->li_tv.vval.v_number;
7222
7223 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
7224
7225 lx->li_tv.vval.v_number = (varnumber_T)x;
7226 ly->li_tv.vval.v_number = (varnumber_T)y;
7227 lz->li_tv.vval.v_number = (varnumber_T)z;
7228 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007229 }
7230 else
7231 goto theend;
7232
7233 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007234 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007235 return;
7236
7237theend:
7238 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007239 rettv->v_type = VAR_NUMBER;
7240 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007241}
7242
7243/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007244 * "srand()" function
7245 */
7246 static void
7247f_srand(typval_T *argvars, typval_T *rettv)
7248{
7249 UINT32_T x = 0, z;
7250
7251 if (rettv_list_alloc(rettv) == FAIL)
7252 return;
7253 if (argvars[0].v_type == VAR_UNKNOWN)
7254 {
7255 init_srand(&x);
7256 }
7257 else
7258 {
7259 int error = FALSE;
7260
7261 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7262 if (error)
7263 return;
7264 }
7265
7266 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7267 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7268 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7269 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7270}
7271
7272#undef ROTL
7273#undef SPLITMIX32
7274#undef SHUFFLE_XOSHIRO128STARSTAR
7275
7276/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007277 * "range()" function
7278 */
7279 static void
7280f_range(typval_T *argvars, typval_T *rettv)
7281{
7282 varnumber_T start;
7283 varnumber_T end;
7284 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007285 int error = FALSE;
7286
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007287 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007288 if (argvars[1].v_type == VAR_UNKNOWN)
7289 {
7290 end = start - 1;
7291 start = 0;
7292 }
7293 else
7294 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007295 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007296 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007297 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007298 }
7299
7300 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007301 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007302 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007303 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007304 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007305 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007306 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007307 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007308 list_T *list = rettv->vval.v_list;
7309
7310 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007311 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007312 // be called.
7313 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007314 list->lv_u.nonmat.lv_start = start;
7315 list->lv_u.nonmat.lv_end = end;
7316 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007317 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007318 }
7319}
7320
7321/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007322 * Materialize "list".
7323 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007324 */
7325 void
7326range_list_materialize(list_T *list)
7327{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007328 varnumber_T start = list->lv_u.nonmat.lv_start;
7329 varnumber_T end = list->lv_u.nonmat.lv_end;
7330 int stride = list->lv_u.nonmat.lv_stride;
7331 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007332
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007333 list->lv_first = NULL;
7334 list->lv_u.mat.lv_last = NULL;
7335 list->lv_len = 0;
7336 list->lv_u.mat.lv_idx_item = NULL;
7337 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7338 if (list_append_number(list, (varnumber_T)i) == FAIL)
7339 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007340}
7341
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007342/*
7343 * "getreginfo()" function
7344 */
7345 static void
7346f_getreginfo(typval_T *argvars, typval_T *rettv)
7347{
7348 char_u *strregname;
7349 int regname;
7350 char_u buf[NUMBUFLEN + 2];
7351 long reglen = 0;
7352 dict_T *dict;
7353 list_T *list;
7354
7355 if (argvars[0].v_type != VAR_UNKNOWN)
7356 {
7357 strregname = tv_get_string_chk(&argvars[0]);
7358 if (strregname == NULL)
7359 return;
7360 }
7361 else
7362 strregname = get_vim_var_str(VV_REG);
7363
7364 regname = (strregname == NULL ? '"' : *strregname);
7365 if (regname == 0 || regname == '@')
7366 regname = '"';
7367
7368 if (rettv_dict_alloc(rettv) == FAIL)
7369 return;
7370 dict = rettv->vval.v_dict;
7371
7372 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7373 if (list == NULL)
7374 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007375 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007376
7377 buf[0] = NUL;
7378 buf[1] = NUL;
7379 switch (get_reg_type(regname, &reglen))
7380 {
7381 case MLINE: buf[0] = 'V'; break;
7382 case MCHAR: buf[0] = 'v'; break;
7383 case MBLOCK:
7384 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7385 reglen + 1);
7386 break;
7387 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007388 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007389
7390 buf[0] = get_register_name(get_unname_register());
7391 buf[1] = NUL;
7392 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007393 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007394 else
7395 {
7396 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7397
7398 if (item != NULL)
7399 {
7400 item->di_tv.v_type = VAR_SPECIAL;
7401 item->di_tv.vval.v_number = regname == buf[0]
7402 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007403 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007404 }
7405 }
7406}
7407
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007408 static void
7409return_register(int regname, typval_T *rettv)
7410{
7411 char_u buf[2] = {0, 0};
7412
7413 buf[0] = (char_u)regname;
7414 rettv->v_type = VAR_STRING;
7415 rettv->vval.v_string = vim_strsave(buf);
7416}
7417
7418/*
7419 * "reg_executing()" function
7420 */
7421 static void
7422f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7423{
7424 return_register(reg_executing, rettv);
7425}
7426
7427/*
7428 * "reg_recording()" function
7429 */
7430 static void
7431f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7432{
7433 return_register(reg_recording, rettv);
7434}
7435
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007436/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007437 * "rename({from}, {to})" function
7438 */
7439 static void
7440f_rename(typval_T *argvars, typval_T *rettv)
7441{
7442 char_u buf[NUMBUFLEN];
7443
7444 if (check_restricted() || check_secure())
7445 rettv->vval.v_number = -1;
7446 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007447 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7448 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007449}
7450
7451/*
7452 * "repeat()" function
7453 */
7454 static void
7455f_repeat(typval_T *argvars, typval_T *rettv)
7456{
7457 char_u *p;
7458 int n;
7459 int slen;
7460 int len;
7461 char_u *r;
7462 int i;
7463
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007464 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007465 if (argvars[0].v_type == VAR_LIST)
7466 {
7467 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7468 while (n-- > 0)
7469 if (list_extend(rettv->vval.v_list,
7470 argvars[0].vval.v_list, NULL) == FAIL)
7471 break;
7472 }
7473 else
7474 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007475 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007476 rettv->v_type = VAR_STRING;
7477 rettv->vval.v_string = NULL;
7478
7479 slen = (int)STRLEN(p);
7480 len = slen * n;
7481 if (len <= 0)
7482 return;
7483
7484 r = alloc(len + 1);
7485 if (r != NULL)
7486 {
7487 for (i = 0; i < n; i++)
7488 mch_memmove(r + i * slen, p, (size_t)slen);
7489 r[len] = NUL;
7490 }
7491
7492 rettv->vval.v_string = r;
7493 }
7494}
7495
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007496#define SP_NOMOVE 0x01 // don't move cursor
7497#define SP_REPEAT 0x02 // repeat to find outer pair
7498#define SP_RETCOUNT 0x04 // return matchcount
7499#define SP_SETPCMARK 0x08 // set previous context mark
7500#define SP_START 0x10 // accept match at start position
7501#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7502#define SP_END 0x40 // leave cursor at end of match
7503#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007504
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007505/*
7506 * Get flags for a search function.
7507 * Possibly sets "p_ws".
7508 * Returns BACKWARD, FORWARD or zero (for an error).
7509 */
7510 static int
7511get_search_arg(typval_T *varp, int *flagsp)
7512{
7513 int dir = FORWARD;
7514 char_u *flags;
7515 char_u nbuf[NUMBUFLEN];
7516 int mask;
7517
7518 if (varp->v_type != VAR_UNKNOWN)
7519 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007520 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007521 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007522 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007523 while (*flags != NUL)
7524 {
7525 switch (*flags)
7526 {
7527 case 'b': dir = BACKWARD; break;
7528 case 'w': p_ws = TRUE; break;
7529 case 'W': p_ws = FALSE; break;
7530 default: mask = 0;
7531 if (flagsp != NULL)
7532 switch (*flags)
7533 {
7534 case 'c': mask = SP_START; break;
7535 case 'e': mask = SP_END; break;
7536 case 'm': mask = SP_RETCOUNT; break;
7537 case 'n': mask = SP_NOMOVE; break;
7538 case 'p': mask = SP_SUBPAT; break;
7539 case 'r': mask = SP_REPEAT; break;
7540 case 's': mask = SP_SETPCMARK; break;
7541 case 'z': mask = SP_COLUMN; break;
7542 }
7543 if (mask == 0)
7544 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007545 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007546 dir = 0;
7547 }
7548 else
7549 *flagsp |= mask;
7550 }
7551 if (dir == 0)
7552 break;
7553 ++flags;
7554 }
7555 }
7556 return dir;
7557}
7558
7559/*
7560 * Shared by search() and searchpos() functions.
7561 */
7562 static int
7563search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7564{
7565 int flags;
7566 char_u *pat;
7567 pos_T pos;
7568 pos_T save_cursor;
7569 int save_p_ws = p_ws;
7570 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007571 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007572 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007573#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007574 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007575 long time_limit = 0;
7576#endif
7577 int options = SEARCH_KEEP;
7578 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007579 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007580 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007581 pos_T firstpos;
7582
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007583 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007584 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007585 if (dir == 0)
7586 goto theend;
7587 flags = *flagsp;
7588 if (flags & SP_START)
7589 options |= SEARCH_START;
7590 if (flags & SP_END)
7591 options |= SEARCH_END;
7592 if (flags & SP_COLUMN)
7593 options |= SEARCH_COL;
7594
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007595 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007596 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7597 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007598 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007599 if (lnum_stop < 0)
7600 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007601 if (argvars[3].v_type != VAR_UNKNOWN)
7602 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007603#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007604 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007605 if (time_limit < 0)
7606 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007607#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007608 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007609 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007610 }
7611
7612#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007613 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007614 profile_setlimit(time_limit, &tm);
7615#endif
7616
7617 /*
7618 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7619 * Check to make sure only those flags are set.
7620 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7621 * flags cannot be set. Check for that condition also.
7622 */
7623 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7624 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7625 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007626 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007627 goto theend;
7628 }
7629
7630 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007631 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007632 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007633 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7634#ifdef FEAT_RELTIME
7635 sia.sa_tm = &tm;
7636#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007637
7638 // Repeat until {skip} returns FALSE.
7639 for (;;)
7640 {
7641 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007642 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007643 // finding the first match again means there is no match where {skip}
7644 // evaluates to zero.
7645 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7646 subpatnum = FAIL;
7647
Bram Moolenaara9c01042020-06-07 14:50:50 +02007648 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007649 // didn't find it or no skip argument
7650 break;
7651 firstpos = pos;
7652
Bram Moolenaara9c01042020-06-07 14:50:50 +02007653 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007654 {
7655 int do_skip;
7656 int err;
7657 pos_T save_pos = curwin->w_cursor;
7658
7659 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007660 err = FALSE;
7661 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007662 curwin->w_cursor = save_pos;
7663 if (err)
7664 {
7665 // Evaluating {skip} caused an error, break here.
7666 subpatnum = FAIL;
7667 break;
7668 }
7669 if (!do_skip)
7670 break;
7671 }
7672 }
7673
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007674 if (subpatnum != FAIL)
7675 {
7676 if (flags & SP_SUBPAT)
7677 retval = subpatnum;
7678 else
7679 retval = pos.lnum;
7680 if (flags & SP_SETPCMARK)
7681 setpcmark();
7682 curwin->w_cursor = pos;
7683 if (match_pos != NULL)
7684 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007685 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007686 match_pos->lnum = pos.lnum;
7687 match_pos->col = pos.col + 1;
7688 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007689 // "/$" will put the cursor after the end of the line, may need to
7690 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007691 check_cursor();
7692 }
7693
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007694 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007695 if (flags & SP_NOMOVE)
7696 curwin->w_cursor = save_cursor;
7697 else
7698 curwin->w_set_curswant = TRUE;
7699theend:
7700 p_ws = save_p_ws;
7701
7702 return retval;
7703}
7704
7705#ifdef FEAT_FLOAT
7706
7707/*
7708 * round() is not in C90, use ceil() or floor() instead.
7709 */
7710 float_T
7711vim_round(float_T f)
7712{
7713 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
7714}
7715
7716/*
7717 * "round({float})" function
7718 */
7719 static void
7720f_round(typval_T *argvars, typval_T *rettv)
7721{
7722 float_T f = 0.0;
7723
7724 rettv->v_type = VAR_FLOAT;
7725 if (get_float_arg(argvars, &f) == OK)
7726 rettv->vval.v_float = vim_round(f);
7727 else
7728 rettv->vval.v_float = 0.0;
7729}
7730#endif
7731
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007732#ifdef FEAT_RUBY
7733/*
7734 * "rubyeval()" function
7735 */
7736 static void
7737f_rubyeval(typval_T *argvars, typval_T *rettv)
7738{
7739 char_u *str;
7740 char_u buf[NUMBUFLEN];
7741
7742 str = tv_get_string_buf(&argvars[0], buf);
7743 do_rubyeval(str, rettv);
7744}
7745#endif
7746
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007747/*
7748 * "screenattr()" function
7749 */
7750 static void
7751f_screenattr(typval_T *argvars, typval_T *rettv)
7752{
7753 int row;
7754 int col;
7755 int c;
7756
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007757 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7758 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007759 if (row < 0 || row >= screen_Rows
7760 || col < 0 || col >= screen_Columns)
7761 c = -1;
7762 else
7763 c = ScreenAttrs[LineOffset[row] + col];
7764 rettv->vval.v_number = c;
7765}
7766
7767/*
7768 * "screenchar()" function
7769 */
7770 static void
7771f_screenchar(typval_T *argvars, typval_T *rettv)
7772{
7773 int row;
7774 int col;
7775 int off;
7776 int c;
7777
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007778 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7779 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007780 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007781 c = -1;
7782 else
7783 {
7784 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007785 if (enc_utf8 && ScreenLinesUC[off] != 0)
7786 c = ScreenLinesUC[off];
7787 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007788 c = ScreenLines[off];
7789 }
7790 rettv->vval.v_number = c;
7791}
7792
7793/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007794 * "screenchars()" function
7795 */
7796 static void
7797f_screenchars(typval_T *argvars, typval_T *rettv)
7798{
7799 int row;
7800 int col;
7801 int off;
7802 int c;
7803 int i;
7804
7805 if (rettv_list_alloc(rettv) == FAIL)
7806 return;
7807 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7808 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7809 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7810 return;
7811
7812 off = LineOffset[row] + col;
7813 if (enc_utf8 && ScreenLinesUC[off] != 0)
7814 c = ScreenLinesUC[off];
7815 else
7816 c = ScreenLines[off];
7817 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7818
7819 if (enc_utf8)
7820
7821 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7822 list_append_number(rettv->vval.v_list,
7823 (varnumber_T)ScreenLinesC[i][off]);
7824}
7825
7826/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007827 * "screencol()" function
7828 *
7829 * First column is 1 to be consistent with virtcol().
7830 */
7831 static void
7832f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7833{
7834 rettv->vval.v_number = screen_screencol() + 1;
7835}
7836
7837/*
7838 * "screenrow()" function
7839 */
7840 static void
7841f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7842{
7843 rettv->vval.v_number = screen_screenrow() + 1;
7844}
7845
7846/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007847 * "screenstring()" function
7848 */
7849 static void
7850f_screenstring(typval_T *argvars, typval_T *rettv)
7851{
7852 int row;
7853 int col;
7854 int off;
7855 int c;
7856 int i;
7857 char_u buf[MB_MAXBYTES + 1];
7858 int buflen = 0;
7859
7860 rettv->vval.v_string = NULL;
7861 rettv->v_type = VAR_STRING;
7862
7863 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7864 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7865 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7866 return;
7867
7868 off = LineOffset[row] + col;
7869 if (enc_utf8 && ScreenLinesUC[off] != 0)
7870 c = ScreenLinesUC[off];
7871 else
7872 c = ScreenLines[off];
7873 buflen += mb_char2bytes(c, buf);
7874
7875 if (enc_utf8)
7876 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7877 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7878
7879 buf[buflen] = NUL;
7880 rettv->vval.v_string = vim_strsave(buf);
7881}
7882
7883/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007884 * "search()" function
7885 */
7886 static void
7887f_search(typval_T *argvars, typval_T *rettv)
7888{
7889 int flags = 0;
7890
7891 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7892}
7893
7894/*
7895 * "searchdecl()" function
7896 */
7897 static void
7898f_searchdecl(typval_T *argvars, typval_T *rettv)
7899{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007900 int locally = TRUE;
7901 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007902 int error = FALSE;
7903 char_u *name;
7904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007905 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007906
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007907 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007908 if (argvars[1].v_type != VAR_UNKNOWN)
7909 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007910 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007911 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007912 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007913 }
7914 if (!error && name != NULL)
7915 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7916 locally, thisblock, SEARCH_KEEP) == FAIL;
7917}
7918
7919/*
7920 * Used by searchpair() and searchpairpos()
7921 */
7922 static int
7923searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7924{
7925 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007926 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007927 int save_p_ws = p_ws;
7928 int dir;
7929 int flags = 0;
7930 char_u nbuf1[NUMBUFLEN];
7931 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007932 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007933 long lnum_stop = 0;
7934 long time_limit = 0;
7935
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007936 // Get the three pattern arguments: start, middle, end. Will result in an
7937 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007938 spat = tv_get_string_chk(&argvars[0]);
7939 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7940 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007941 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007942 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007944 // Handle the optional fourth argument: flags
7945 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007946 if (dir == 0)
7947 goto theend;
7948
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007949 // Don't accept SP_END or SP_SUBPAT.
7950 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007951 if ((flags & (SP_END | SP_SUBPAT)) != 0
7952 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7953 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007954 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007955 goto theend;
7956 }
7957
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007958 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007959 if (flags & SP_REPEAT)
7960 p_ws = FALSE;
7961
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007962 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007963 if (argvars[3].v_type == VAR_UNKNOWN
7964 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007965 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007966 else
7967 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007968 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007969 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007970
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007971 if (argvars[5].v_type != VAR_UNKNOWN)
7972 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007973 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007974 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007975 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007976 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007977 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007978 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007979#ifdef FEAT_RELTIME
7980 if (argvars[6].v_type != VAR_UNKNOWN)
7981 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007982 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007983 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007984 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007985 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007986 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007987 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007988 }
7989#endif
7990 }
7991 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007992
7993 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
7994 match_pos, lnum_stop, time_limit);
7995
7996theend:
7997 p_ws = save_p_ws;
7998
7999 return retval;
8000}
8001
8002/*
8003 * "searchpair()" function
8004 */
8005 static void
8006f_searchpair(typval_T *argvars, typval_T *rettv)
8007{
8008 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
8009}
8010
8011/*
8012 * "searchpairpos()" function
8013 */
8014 static void
8015f_searchpairpos(typval_T *argvars, typval_T *rettv)
8016{
8017 pos_T match_pos;
8018 int lnum = 0;
8019 int col = 0;
8020
8021 if (rettv_list_alloc(rettv) == FAIL)
8022 return;
8023
8024 if (searchpair_cmn(argvars, &match_pos) > 0)
8025 {
8026 lnum = match_pos.lnum;
8027 col = match_pos.col;
8028 }
8029
8030 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8031 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8032}
8033
8034/*
8035 * Search for a start/middle/end thing.
8036 * Used by searchpair(), see its documentation for the details.
8037 * Returns 0 or -1 for no match,
8038 */
8039 long
8040do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008041 char_u *spat, // start pattern
8042 char_u *mpat, // middle pattern
8043 char_u *epat, // end pattern
8044 int dir, // BACKWARD or FORWARD
8045 typval_T *skip, // skip expression
8046 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008047 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008048 linenr_T lnum_stop, // stop at this line if not zero
8049 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008050{
8051 char_u *save_cpo;
8052 char_u *pat, *pat2 = NULL, *pat3 = NULL;
8053 long retval = 0;
8054 pos_T pos;
8055 pos_T firstpos;
8056 pos_T foundpos;
8057 pos_T save_cursor;
8058 pos_T save_pos;
8059 int n;
8060 int r;
8061 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01008062 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008063 int err;
8064 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008065#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008066 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008067#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008069 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008070 save_cpo = p_cpo;
8071 p_cpo = empty_option;
8072
8073#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008074 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008075 profile_setlimit(time_limit, &tm);
8076#endif
8077
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008078 // Make two search patterns: start/end (pat2, for in nested pairs) and
8079 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02008080 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
8081 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008082 if (pat2 == NULL || pat3 == NULL)
8083 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008084 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008085 if (*mpat == NUL)
8086 STRCPY(pat3, pat2);
8087 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008088 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008089 spat, epat, mpat);
8090 if (flags & SP_START)
8091 options |= SEARCH_START;
8092
Bram Moolenaar48570482017-10-30 21:48:41 +01008093 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02008094 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01008095
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008096 save_cursor = curwin->w_cursor;
8097 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008098 CLEAR_POS(&firstpos);
8099 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008100 pat = pat3;
8101 for (;;)
8102 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008103 searchit_arg_T sia;
8104
Bram Moolenaara80faa82020-04-12 19:37:17 +02008105 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008106 sia.sa_stop_lnum = lnum_stop;
8107#ifdef FEAT_RELTIME
8108 sia.sa_tm = &tm;
8109#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01008110 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008111 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008112 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008113 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008114 break;
8115
8116 if (firstpos.lnum == 0)
8117 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008118 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008119 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008120 // Found the same position again. Can happen with a pattern that
8121 // has "\zs" at the end and searching backwards. Advance one
8122 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008123 if (dir == BACKWARD)
8124 decl(&pos);
8125 else
8126 incl(&pos);
8127 }
8128 foundpos = pos;
8129
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008130 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008131 options &= ~SEARCH_START;
8132
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008133 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01008134 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008135 {
8136 save_pos = curwin->w_cursor;
8137 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01008138 err = FALSE;
8139 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008140 curwin->w_cursor = save_pos;
8141 if (err)
8142 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008143 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008144 curwin->w_cursor = save_cursor;
8145 retval = -1;
8146 break;
8147 }
8148 if (r)
8149 continue;
8150 }
8151
8152 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8153 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008154 // Found end when searching backwards or start when searching
8155 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008156 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008157 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008158 }
8159 else
8160 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008161 // Found end when searching forward or start when searching
8162 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008163 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008164 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165 }
8166
8167 if (nest == 0)
8168 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008169 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008170 if (flags & SP_RETCOUNT)
8171 ++retval;
8172 else
8173 retval = pos.lnum;
8174 if (flags & SP_SETPCMARK)
8175 setpcmark();
8176 curwin->w_cursor = pos;
8177 if (!(flags & SP_REPEAT))
8178 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008179 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008180 }
8181 }
8182
8183 if (match_pos != NULL)
8184 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008185 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008186 match_pos->lnum = curwin->w_cursor.lnum;
8187 match_pos->col = curwin->w_cursor.col + 1;
8188 }
8189
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008190 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008191 if ((flags & SP_NOMOVE) || retval == 0)
8192 curwin->w_cursor = save_cursor;
8193
8194theend:
8195 vim_free(pat2);
8196 vim_free(pat3);
8197 if (p_cpo == empty_option)
8198 p_cpo = save_cpo;
8199 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008200 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008201 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008202 // If it's still empty it was changed and restored, need to restore in
8203 // the complicated way.
8204 if (*p_cpo == NUL)
8205 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008206 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008207 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008208
8209 return retval;
8210}
8211
8212/*
8213 * "searchpos()" function
8214 */
8215 static void
8216f_searchpos(typval_T *argvars, typval_T *rettv)
8217{
8218 pos_T match_pos;
8219 int lnum = 0;
8220 int col = 0;
8221 int n;
8222 int flags = 0;
8223
8224 if (rettv_list_alloc(rettv) == FAIL)
8225 return;
8226
8227 n = search_cmn(argvars, &match_pos, &flags);
8228 if (n > 0)
8229 {
8230 lnum = match_pos.lnum;
8231 col = match_pos.col;
8232 }
8233
8234 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8235 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8236 if (flags & SP_SUBPAT)
8237 list_append_number(rettv->vval.v_list, (varnumber_T)n);
8238}
8239
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008240/*
8241 * Set the cursor or mark position.
8242 * If 'charpos' is TRUE, then use the column number as a character offet.
8243 * Otherwise use the column number as a byte offset.
8244 */
8245 static void
8246set_position(typval_T *argvars, typval_T *rettv, int charpos)
8247{
8248 pos_T pos;
8249 int fnum;
8250 char_u *name;
8251 colnr_T curswant = -1;
8252
8253 rettv->vval.v_number = -1;
8254
8255 name = tv_get_string_chk(argvars);
8256 if (name != NULL)
8257 {
8258 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
8259 {
8260 if (pos.col != MAXCOL && --pos.col < 0)
8261 pos.col = 0;
8262 if ((name[0] == '.' && name[1] == NUL))
8263 {
8264 // set cursor; "fnum" is ignored
8265 curwin->w_cursor = pos;
8266 if (curswant >= 0)
8267 {
8268 curwin->w_curswant = curswant - 1;
8269 curwin->w_set_curswant = FALSE;
8270 }
8271 check_cursor();
8272 rettv->vval.v_number = 0;
8273 }
8274 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8275 {
8276 // set mark
8277 if (setmark_pos(name[1], &pos, fnum) == OK)
8278 rettv->vval.v_number = 0;
8279 }
8280 else
8281 emsg(_(e_invarg));
8282 }
8283 }
8284}
8285/*
8286 * "setcharpos()" function
8287 */
8288 static void
8289f_setcharpos(typval_T *argvars, typval_T *rettv)
8290{
8291 set_position(argvars, rettv, TRUE);
8292}
8293
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008294 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008295f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8296{
8297 dict_T *d;
8298 dictitem_T *di;
8299 char_u *csearch;
8300
8301 if (argvars[0].v_type != VAR_DICT)
8302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008303 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008304 return;
8305 }
8306
8307 if ((d = argvars[0].vval.v_dict) != NULL)
8308 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008309 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008310 if (csearch != NULL)
8311 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008312 if (enc_utf8)
8313 {
8314 int pcc[MAX_MCO];
8315 int c = utfc_ptr2char(csearch, pcc);
8316
8317 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8318 }
8319 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008320 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008321 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008322 }
8323
8324 di = dict_find(d, (char_u *)"forward", -1);
8325 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008326 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008327 ? FORWARD : BACKWARD);
8328
8329 di = dict_find(d, (char_u *)"until", -1);
8330 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008331 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008332 }
8333}
8334
8335/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008336 * "setcursorcharpos" function
8337 */
8338 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008339f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008340{
8341 set_cursorpos(argvars, rettv, TRUE);
8342}
8343
8344/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008345 * "setenv()" function
8346 */
8347 static void
8348f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8349{
8350 char_u namebuf[NUMBUFLEN];
8351 char_u valbuf[NUMBUFLEN];
8352 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8353
8354 if (argvars[1].v_type == VAR_SPECIAL
8355 && argvars[1].vval.v_number == VVAL_NULL)
8356 vim_unsetenv(name);
8357 else
8358 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8359}
8360
8361/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362 * "setfperm({fname}, {mode})" function
8363 */
8364 static void
8365f_setfperm(typval_T *argvars, typval_T *rettv)
8366{
8367 char_u *fname;
8368 char_u modebuf[NUMBUFLEN];
8369 char_u *mode_str;
8370 int i;
8371 int mask;
8372 int mode = 0;
8373
8374 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008375 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008376 if (fname == NULL)
8377 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008378 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008379 if (mode_str == NULL)
8380 return;
8381 if (STRLEN(mode_str) != 9)
8382 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008383 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008384 return;
8385 }
8386
8387 mask = 1;
8388 for (i = 8; i >= 0; --i)
8389 {
8390 if (mode_str[i] != '-')
8391 mode |= mask;
8392 mask = mask << 1;
8393 }
8394 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8395}
8396
8397/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008398 * "setpos()" function
8399 */
8400 static void
8401f_setpos(typval_T *argvars, typval_T *rettv)
8402{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008403 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008404}
8405
8406/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008407 * Translate a register type string to the yank type and block length
8408 */
8409 static int
8410get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8411{
8412 char_u *stropt = *pp;
8413 switch (*stropt)
8414 {
8415 case 'v': case 'c': // character-wise selection
8416 *yank_type = MCHAR;
8417 break;
8418 case 'V': case 'l': // line-wise selection
8419 *yank_type = MLINE;
8420 break;
8421 case 'b': case Ctrl_V: // block-wise selection
8422 *yank_type = MBLOCK;
8423 if (VIM_ISDIGIT(stropt[1]))
8424 {
8425 ++stropt;
8426 *block_len = getdigits(&stropt) - 1;
8427 --stropt;
8428 }
8429 break;
8430 default:
8431 return FAIL;
8432 }
8433 *pp = stropt;
8434 return OK;
8435}
8436
8437/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008438 * "setreg()" function
8439 */
8440 static void
8441f_setreg(typval_T *argvars, typval_T *rettv)
8442{
8443 int regname;
8444 char_u *strregname;
8445 char_u *stropt;
8446 char_u *strval;
8447 int append;
8448 char_u yank_type;
8449 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008450 typval_T *regcontents;
8451 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008452
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008453 pointreg = 0;
8454 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008455 block_len = -1;
8456 yank_type = MAUTO;
8457 append = FALSE;
8458
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008459 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008460 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008461
8462 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008463 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008464 regname = *strregname;
8465 if (regname == 0 || regname == '@')
8466 regname = '"';
8467
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008468 if (argvars[1].v_type == VAR_DICT)
8469 {
8470 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008471 dictitem_T *di;
8472
8473 if (d == NULL || d->dv_hashtab.ht_used == 0)
8474 {
8475 // Empty dict, clear the register (like setreg(0, []))
8476 char_u *lstval[2] = {NULL, NULL};
8477 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8478 return;
8479 }
8480
8481 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008482 if (di != NULL)
8483 regcontents = &di->di_tv;
8484
8485 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8486 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008487 {
8488 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8489
8490 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008491 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008492 semsg(_(e_invargval), "value");
8493 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008494 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008495 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008496
8497 if (regname == '"')
8498 {
8499 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8500 if (stropt != NULL)
8501 {
8502 pointreg = *stropt;
8503 regname = pointreg;
8504 }
8505 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008506 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008507 pointreg = regname;
8508 }
8509 else
8510 regcontents = &argvars[1];
8511
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008512 if (argvars[2].v_type != VAR_UNKNOWN)
8513 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008514 if (yank_type != MAUTO)
8515 {
8516 semsg(_(e_toomanyarg), "setreg");
8517 return;
8518 }
8519
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008520 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008521 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008522 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008523 for (; *stropt != NUL; ++stropt)
8524 switch (*stropt)
8525 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008526 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008527 append = TRUE;
8528 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008529 default:
8530 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008531 }
8532 }
8533
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008534 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008535 {
8536 char_u **lstval;
8537 char_u **allocval;
8538 char_u buf[NUMBUFLEN];
8539 char_u **curval;
8540 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008541 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008542 listitem_T *li;
8543 int len;
8544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008545 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008546 len = ll == NULL ? 0 : ll->lv_len;
8547
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008548 // First half: use for pointers to result lines; second half: use for
8549 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008550 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008551 if (lstval == NULL)
8552 return;
8553 curval = lstval;
8554 allocval = lstval + len + 2;
8555 curallocval = allocval;
8556
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008557 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008558 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008559 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008560 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008561 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008562 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008563 if (strval == NULL)
8564 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008565 if (strval == buf)
8566 {
8567 // Need to make a copy, next tv_get_string_buf_chk() will
8568 // overwrite the string.
8569 strval = vim_strsave(buf);
8570 if (strval == NULL)
8571 goto free_lstval;
8572 *curallocval++ = strval;
8573 }
8574 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008575 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008576 }
8577 *curval++ = NULL;
8578
8579 write_reg_contents_lst(regname, lstval, -1,
8580 append, yank_type, block_len);
8581free_lstval:
8582 while (curallocval > allocval)
8583 vim_free(*--curallocval);
8584 vim_free(lstval);
8585 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008586 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008587 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008588 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008589 if (strval == NULL)
8590 return;
8591 write_reg_contents_ex(regname, strval, -1,
8592 append, yank_type, block_len);
8593 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008594 if (pointreg != 0)
8595 get_yank_register(pointreg, TRUE);
8596
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008597 rettv->vval.v_number = 0;
8598}
8599
8600/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008601 * "settagstack()" function
8602 */
8603 static void
8604f_settagstack(typval_T *argvars, typval_T *rettv)
8605{
8606 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8607 win_T *wp;
8608 dict_T *d;
8609 int action = 'r';
8610
8611 rettv->vval.v_number = -1;
8612
8613 // first argument: window number or id
8614 wp = find_win_by_nr_or_id(&argvars[0]);
8615 if (wp == NULL)
8616 return;
8617
8618 // second argument: dict with items to set in the tag stack
8619 if (argvars[1].v_type != VAR_DICT)
8620 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008621 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008622 return;
8623 }
8624 d = argvars[1].vval.v_dict;
8625 if (d == NULL)
8626 return;
8627
8628 // third argument: action - 'a' for append and 'r' for replace.
8629 // default is to replace the stack.
8630 if (argvars[2].v_type == VAR_UNKNOWN)
8631 action = 'r';
8632 else if (argvars[2].v_type == VAR_STRING)
8633 {
8634 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008635 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008636 if (actstr == NULL)
8637 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008638 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8639 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008640 action = *actstr;
8641 else
8642 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008643 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008644 return;
8645 }
8646 }
8647 else
8648 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008649 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008650 return;
8651 }
8652
8653 if (set_tagstack(wp, d, action) == OK)
8654 rettv->vval.v_number = 0;
8655}
8656
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008657#ifdef FEAT_CRYPT
8658/*
8659 * "sha256({string})" function
8660 */
8661 static void
8662f_sha256(typval_T *argvars, typval_T *rettv)
8663{
8664 char_u *p;
8665
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008666 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008667 rettv->vval.v_string = vim_strsave(
8668 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8669 rettv->v_type = VAR_STRING;
8670}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008671#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008672
8673/*
8674 * "shellescape({string})" function
8675 */
8676 static void
8677f_shellescape(typval_T *argvars, typval_T *rettv)
8678{
Bram Moolenaar20615522017-06-05 18:46:26 +02008679 int do_special = non_zero_arg(&argvars[1]);
8680
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008681 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008682 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008683 rettv->v_type = VAR_STRING;
8684}
8685
8686/*
8687 * shiftwidth() function
8688 */
8689 static void
8690f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8691{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008692 rettv->vval.v_number = 0;
8693
8694 if (argvars[0].v_type != VAR_UNKNOWN)
8695 {
8696 long col;
8697
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008698 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008699 if (col < 0)
8700 return; // type error; errmsg already given
8701#ifdef FEAT_VARTABS
8702 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8703 return;
8704#endif
8705 }
8706
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008707 rettv->vval.v_number = get_sw_value(curbuf);
8708}
8709
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008710#ifdef FEAT_FLOAT
8711/*
8712 * "sin()" function
8713 */
8714 static void
8715f_sin(typval_T *argvars, typval_T *rettv)
8716{
8717 float_T f = 0.0;
8718
8719 rettv->v_type = VAR_FLOAT;
8720 if (get_float_arg(argvars, &f) == OK)
8721 rettv->vval.v_float = sin(f);
8722 else
8723 rettv->vval.v_float = 0.0;
8724}
8725
8726/*
8727 * "sinh()" function
8728 */
8729 static void
8730f_sinh(typval_T *argvars, typval_T *rettv)
8731{
8732 float_T f = 0.0;
8733
8734 rettv->v_type = VAR_FLOAT;
8735 if (get_float_arg(argvars, &f) == OK)
8736 rettv->vval.v_float = sinh(f);
8737 else
8738 rettv->vval.v_float = 0.0;
8739}
8740#endif
8741
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008742/*
8743 * "soundfold({word})" function
8744 */
8745 static void
8746f_soundfold(typval_T *argvars, typval_T *rettv)
8747{
8748 char_u *s;
8749
8750 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008751 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008752#ifdef FEAT_SPELL
8753 rettv->vval.v_string = eval_soundfold(s);
8754#else
8755 rettv->vval.v_string = vim_strsave(s);
8756#endif
8757}
8758
8759/*
8760 * "spellbadword()" function
8761 */
8762 static void
8763f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8764{
8765 char_u *word = (char_u *)"";
8766 hlf_T attr = HLF_COUNT;
8767 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008768#ifdef FEAT_SPELL
8769 int wo_spell_save = curwin->w_p_spell;
8770
8771 if (!curwin->w_p_spell)
8772 {
8773 did_set_spelllang(curwin);
8774 curwin->w_p_spell = TRUE;
8775 }
8776
8777 if (*curwin->w_s->b_p_spl == NUL)
8778 {
8779 emsg(_(e_no_spell));
8780 curwin->w_p_spell = wo_spell_save;
8781 return;
8782 }
8783#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008784
8785 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008786 {
8787#ifdef FEAT_SPELL
8788 curwin->w_p_spell = wo_spell_save;
8789#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008790 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008791 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008792
8793#ifdef FEAT_SPELL
8794 if (argvars[0].v_type == VAR_UNKNOWN)
8795 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008796 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008797 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8798 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008799 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008800 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008801 curwin->w_set_curswant = TRUE;
8802 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008803 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008804 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008805 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008806 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008807 int capcol = -1;
8808
8809 if (str != NULL)
8810 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008811 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008812 while (*str != NUL)
8813 {
8814 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8815 if (attr != HLF_COUNT)
8816 {
8817 word = str;
8818 break;
8819 }
8820 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008821 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008822 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008823 }
8824 }
8825 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008826 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008827#endif
8828
8829 list_append_string(rettv->vval.v_list, word, len);
8830 list_append_string(rettv->vval.v_list, (char_u *)(
8831 attr == HLF_SPB ? "bad" :
8832 attr == HLF_SPR ? "rare" :
8833 attr == HLF_SPL ? "local" :
8834 attr == HLF_SPC ? "caps" :
8835 ""), -1);
8836}
8837
8838/*
8839 * "spellsuggest()" function
8840 */
8841 static void
8842f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8843{
8844#ifdef FEAT_SPELL
8845 char_u *str;
8846 int typeerr = FALSE;
8847 int maxcount;
8848 garray_T ga;
8849 int i;
8850 listitem_T *li;
8851 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008852 int wo_spell_save = curwin->w_p_spell;
8853
8854 if (!curwin->w_p_spell)
8855 {
8856 did_set_spelllang(curwin);
8857 curwin->w_p_spell = TRUE;
8858 }
8859
8860 if (*curwin->w_s->b_p_spl == NUL)
8861 {
8862 emsg(_(e_no_spell));
8863 curwin->w_p_spell = wo_spell_save;
8864 return;
8865 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008866#endif
8867
8868 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008869 {
8870#ifdef FEAT_SPELL
8871 curwin->w_p_spell = wo_spell_save;
8872#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008873 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008874 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875
8876#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008877 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008878 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008879 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008880 if (argvars[1].v_type != VAR_UNKNOWN)
8881 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008882 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008883 if (maxcount <= 0)
8884 return;
8885 if (argvars[2].v_type != VAR_UNKNOWN)
8886 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008887 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008888 if (typeerr)
8889 return;
8890 }
8891 }
8892 else
8893 maxcount = 25;
8894
8895 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8896
8897 for (i = 0; i < ga.ga_len; ++i)
8898 {
8899 str = ((char_u **)ga.ga_data)[i];
8900
8901 li = listitem_alloc();
8902 if (li == NULL)
8903 vim_free(str);
8904 else
8905 {
8906 li->li_tv.v_type = VAR_STRING;
8907 li->li_tv.v_lock = 0;
8908 li->li_tv.vval.v_string = str;
8909 list_append(rettv->vval.v_list, li);
8910 }
8911 }
8912 ga_clear(&ga);
8913 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008914 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008915#endif
8916}
8917
8918 static void
8919f_split(typval_T *argvars, typval_T *rettv)
8920{
8921 char_u *str;
8922 char_u *end;
8923 char_u *pat = NULL;
8924 regmatch_T regmatch;
8925 char_u patbuf[NUMBUFLEN];
8926 char_u *save_cpo;
8927 int match;
8928 colnr_T col = 0;
8929 int keepempty = FALSE;
8930 int typeerr = FALSE;
8931
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008932 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008933 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008934 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008935
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008936 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008937 if (argvars[1].v_type != VAR_UNKNOWN)
8938 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008939 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008940 if (pat == NULL)
8941 typeerr = TRUE;
8942 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008943 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008944 }
8945 if (pat == NULL || *pat == NUL)
8946 pat = (char_u *)"[\\x01- ]\\+";
8947
8948 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008949 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008950 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008951 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008952
8953 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8954 if (regmatch.regprog != NULL)
8955 {
8956 regmatch.rm_ic = FALSE;
8957 while (*str != NUL || keepempty)
8958 {
8959 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008960 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008961 else
8962 match = vim_regexec_nl(&regmatch, str, col);
8963 if (match)
8964 end = regmatch.startp[0];
8965 else
8966 end = str + STRLEN(str);
8967 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
8968 && *str != NUL && match && end < regmatch.endp[0]))
8969 {
8970 if (list_append_string(rettv->vval.v_list, str,
8971 (int)(end - str)) == FAIL)
8972 break;
8973 }
8974 if (!match)
8975 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01008976 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008977 if (regmatch.endp[0] > str)
8978 col = 0;
8979 else
Bram Moolenaar13505972019-01-24 15:04:48 +01008980 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008981 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008982 str = regmatch.endp[0];
8983 }
8984
8985 vim_regfree(regmatch.regprog);
8986 }
8987
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008988theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008989 p_cpo = save_cpo;
8990}
8991
8992#ifdef FEAT_FLOAT
8993/*
8994 * "sqrt()" function
8995 */
8996 static void
8997f_sqrt(typval_T *argvars, typval_T *rettv)
8998{
8999 float_T f = 0.0;
9000
9001 rettv->v_type = VAR_FLOAT;
9002 if (get_float_arg(argvars, &f) == OK)
9003 rettv->vval.v_float = sqrt(f);
9004 else
9005 rettv->vval.v_float = 0.0;
9006}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009007#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009008
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009009#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01009010/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009011 * "str2float()" function
9012 */
9013 static void
9014f_str2float(typval_T *argvars, typval_T *rettv)
9015{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009016 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009017 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009018
Bram Moolenaar08243d22017-01-10 16:12:29 +01009019 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009020 p = skipwhite(p + 1);
9021 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01009022 if (isneg)
9023 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009024 rettv->v_type = VAR_FLOAT;
9025}
9026#endif
9027
9028/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02009029 * "str2list()" function
9030 */
9031 static void
9032f_str2list(typval_T *argvars, typval_T *rettv)
9033{
9034 char_u *p;
9035 int utf8 = FALSE;
9036
9037 if (rettv_list_alloc(rettv) == FAIL)
9038 return;
9039
9040 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02009041 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02009042
9043 p = tv_get_string(&argvars[0]);
9044
9045 if (has_mbyte || utf8)
9046 {
9047 int (*ptr2len)(char_u *);
9048 int (*ptr2char)(char_u *);
9049
9050 if (utf8 || enc_utf8)
9051 {
9052 ptr2len = utf_ptr2len;
9053 ptr2char = utf_ptr2char;
9054 }
9055 else
9056 {
9057 ptr2len = mb_ptr2len;
9058 ptr2char = mb_ptr2char;
9059 }
9060
9061 for ( ; *p != NUL; p += (*ptr2len)(p))
9062 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
9063 }
9064 else
9065 for ( ; *p != NUL; ++p)
9066 list_append_number(rettv->vval.v_list, *p);
9067}
9068
9069/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009070 * "str2nr()" function
9071 */
9072 static void
9073f_str2nr(typval_T *argvars, typval_T *rettv)
9074{
9075 int base = 10;
9076 char_u *p;
9077 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009078 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01009079 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009080
9081 if (argvars[1].v_type != VAR_UNKNOWN)
9082 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009083 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009084 if (base != 2 && base != 8 && base != 10 && base != 16)
9085 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009086 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009087 return;
9088 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02009089 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009090 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009091 }
9092
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01009093 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009094 isneg = (*p == '-');
9095 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009096 p = skipwhite(p + 1);
9097 switch (base)
9098 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009099 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02009100 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009101 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009102 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02009103 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
9104 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01009105 if (isneg)
9106 rettv->vval.v_number = -n;
9107 else
9108 rettv->vval.v_number = n;
9109
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009110}
9111
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112/*
9113 * "strgetchar()" function
9114 */
9115 static void
9116f_strgetchar(typval_T *argvars, typval_T *rettv)
9117{
9118 char_u *str;
9119 int len;
9120 int error = FALSE;
9121 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01009122 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009123
9124 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009125 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009126 if (str == NULL)
9127 return;
9128 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009129 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009130 if (error)
9131 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009132
Bram Moolenaar13505972019-01-24 15:04:48 +01009133 while (charidx >= 0 && byteidx < len)
9134 {
9135 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009136 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009137 rettv->vval.v_number = mb_ptr2char(str + byteidx);
9138 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009139 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009140 --charidx;
9141 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009142 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009143}
9144
9145/*
9146 * "stridx()" function
9147 */
9148 static void
9149f_stridx(typval_T *argvars, typval_T *rettv)
9150{
9151 char_u buf[NUMBUFLEN];
9152 char_u *needle;
9153 char_u *haystack;
9154 char_u *save_haystack;
9155 char_u *pos;
9156 int start_idx;
9157
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009158 needle = tv_get_string_chk(&argvars[1]);
9159 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009160 rettv->vval.v_number = -1;
9161 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009162 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009163
9164 if (argvars[2].v_type != VAR_UNKNOWN)
9165 {
9166 int error = FALSE;
9167
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009168 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009169 if (error || start_idx >= (int)STRLEN(haystack))
9170 return;
9171 if (start_idx >= 0)
9172 haystack += start_idx;
9173 }
9174
9175 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9176 if (pos != NULL)
9177 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
9178}
9179
9180/*
9181 * "string()" function
9182 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009183 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009184f_string(typval_T *argvars, typval_T *rettv)
9185{
9186 char_u *tofree;
9187 char_u numbuf[NUMBUFLEN];
9188
9189 rettv->v_type = VAR_STRING;
9190 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
9191 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009192 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009193 if (rettv->vval.v_string != NULL && tofree == NULL)
9194 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
9195}
9196
9197/*
9198 * "strlen()" function
9199 */
9200 static void
9201f_strlen(typval_T *argvars, typval_T *rettv)
9202{
9203 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009204 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009205}
9206
9207/*
9208 * "strchars()" function
9209 */
9210 static void
9211f_strchars(typval_T *argvars, typval_T *rettv)
9212{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009213 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009214 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009215 varnumber_T len = 0;
9216 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009217
9218 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009219 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009220 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02009221 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009222 else
9223 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009224 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
9225 while (*s != NUL)
9226 {
9227 func_mb_ptr2char_adv(&s);
9228 ++len;
9229 }
9230 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009231 }
9232}
9233
9234/*
9235 * "strdisplaywidth()" function
9236 */
9237 static void
9238f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
9239{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009240 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009241 int col = 0;
9242
9243 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009244 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009245
9246 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
9247}
9248
9249/*
9250 * "strwidth()" function
9251 */
9252 static void
9253f_strwidth(typval_T *argvars, typval_T *rettv)
9254{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009255 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009256
Bram Moolenaar13505972019-01-24 15:04:48 +01009257 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009258}
9259
9260/*
9261 * "strcharpart()" function
9262 */
9263 static void
9264f_strcharpart(typval_T *argvars, typval_T *rettv)
9265{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009266 char_u *p;
9267 int nchar;
9268 int nbyte = 0;
9269 int charlen;
9270 int len = 0;
9271 int slen;
9272 int error = FALSE;
9273
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009274 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009275 slen = (int)STRLEN(p);
9276
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009277 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009278 if (!error)
9279 {
9280 if (nchar > 0)
9281 while (nchar > 0 && nbyte < slen)
9282 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009283 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009284 --nchar;
9285 }
9286 else
9287 nbyte = nchar;
9288 if (argvars[2].v_type != VAR_UNKNOWN)
9289 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009290 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009291 while (charlen > 0 && nbyte + len < slen)
9292 {
9293 int off = nbyte + len;
9294
9295 if (off < 0)
9296 len += 1;
9297 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009298 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009299 --charlen;
9300 }
9301 }
9302 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009303 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009304 }
9305
9306 /*
9307 * Only return the overlap between the specified part and the actual
9308 * string.
9309 */
9310 if (nbyte < 0)
9311 {
9312 len += nbyte;
9313 nbyte = 0;
9314 }
9315 else if (nbyte > slen)
9316 nbyte = slen;
9317 if (len < 0)
9318 len = 0;
9319 else if (nbyte + len > slen)
9320 len = slen - nbyte;
9321
9322 rettv->v_type = VAR_STRING;
9323 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009324}
9325
9326/*
9327 * "strpart()" function
9328 */
9329 static void
9330f_strpart(typval_T *argvars, typval_T *rettv)
9331{
9332 char_u *p;
9333 int n;
9334 int len;
9335 int slen;
9336 int error = FALSE;
9337
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009338 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009339 slen = (int)STRLEN(p);
9340
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009341 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009342 if (error)
9343 len = 0;
9344 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009345 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009346 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009347 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009348
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009349 // Only return the overlap between the specified part and the actual
9350 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009351 if (n < 0)
9352 {
9353 len += n;
9354 n = 0;
9355 }
9356 else if (n > slen)
9357 n = slen;
9358 if (len < 0)
9359 len = 0;
9360 else if (n + len > slen)
9361 len = slen - n;
9362
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009363 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9364 {
9365 int off;
9366
9367 // length in characters
9368 for (off = n; off < slen && len > 0; --len)
9369 off += mb_ptr2len(p + off);
9370 len = off - n;
9371 }
9372
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009373 rettv->v_type = VAR_STRING;
9374 rettv->vval.v_string = vim_strnsave(p + n, len);
9375}
9376
9377/*
9378 * "strridx()" function
9379 */
9380 static void
9381f_strridx(typval_T *argvars, typval_T *rettv)
9382{
9383 char_u buf[NUMBUFLEN];
9384 char_u *needle;
9385 char_u *haystack;
9386 char_u *rest;
9387 char_u *lastmatch = NULL;
9388 int haystack_len, end_idx;
9389
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009390 needle = tv_get_string_chk(&argvars[1]);
9391 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009392
9393 rettv->vval.v_number = -1;
9394 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009395 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009396
9397 haystack_len = (int)STRLEN(haystack);
9398 if (argvars[2].v_type != VAR_UNKNOWN)
9399 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009400 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009401 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009402 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009403 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009404 }
9405 else
9406 end_idx = haystack_len;
9407
9408 if (*needle == NUL)
9409 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009410 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009411 lastmatch = haystack + end_idx;
9412 }
9413 else
9414 {
9415 for (rest = haystack; *rest != '\0'; ++rest)
9416 {
9417 rest = (char_u *)strstr((char *)rest, (char *)needle);
9418 if (rest == NULL || rest > haystack + end_idx)
9419 break;
9420 lastmatch = rest;
9421 }
9422 }
9423
9424 if (lastmatch == NULL)
9425 rettv->vval.v_number = -1;
9426 else
9427 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9428}
9429
9430/*
9431 * "strtrans()" function
9432 */
9433 static void
9434f_strtrans(typval_T *argvars, typval_T *rettv)
9435{
9436 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009437 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009438}
9439
9440/*
9441 * "submatch()" function
9442 */
9443 static void
9444f_submatch(typval_T *argvars, typval_T *rettv)
9445{
9446 int error = FALSE;
9447 int no;
9448 int retList = 0;
9449
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009450 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009451 if (error)
9452 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009453 if (no < 0 || no >= NSUBEXP)
9454 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009455 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009456 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009457 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009458 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009459 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009460 if (error)
9461 return;
9462
9463 if (retList == 0)
9464 {
9465 rettv->v_type = VAR_STRING;
9466 rettv->vval.v_string = reg_submatch(no);
9467 }
9468 else
9469 {
9470 rettv->v_type = VAR_LIST;
9471 rettv->vval.v_list = reg_submatch_list(no);
9472 }
9473}
9474
9475/*
9476 * "substitute()" function
9477 */
9478 static void
9479f_substitute(typval_T *argvars, typval_T *rettv)
9480{
9481 char_u patbuf[NUMBUFLEN];
9482 char_u subbuf[NUMBUFLEN];
9483 char_u flagsbuf[NUMBUFLEN];
9484
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009485 char_u *str = tv_get_string_chk(&argvars[0]);
9486 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009487 char_u *sub = NULL;
9488 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009489 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009490
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009491 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9492 expr = &argvars[2];
9493 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009494 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009495
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009496 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009497 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9498 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009499 rettv->vval.v_string = NULL;
9500 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009501 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009502}
9503
9504/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009505 * "swapinfo(swap_filename)" function
9506 */
9507 static void
9508f_swapinfo(typval_T *argvars, typval_T *rettv)
9509{
9510 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009511 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009512}
9513
9514/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009515 * "swapname(expr)" function
9516 */
9517 static void
9518f_swapname(typval_T *argvars, typval_T *rettv)
9519{
9520 buf_T *buf;
9521
9522 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009523 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009524 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9525 || buf->b_ml.ml_mfp->mf_fname == NULL)
9526 rettv->vval.v_string = NULL;
9527 else
9528 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9529}
9530
9531/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009532 * "synID(lnum, col, trans)" function
9533 */
9534 static void
9535f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9536{
9537 int id = 0;
9538#ifdef FEAT_SYN_HL
9539 linenr_T lnum;
9540 colnr_T col;
9541 int trans;
9542 int transerr = FALSE;
9543
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009544 lnum = tv_get_lnum(argvars); // -1 on type error
9545 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009546 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009547
9548 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9549 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9550 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9551#endif
9552
9553 rettv->vval.v_number = id;
9554}
9555
9556/*
9557 * "synIDattr(id, what [, mode])" function
9558 */
9559 static void
9560f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9561{
9562 char_u *p = NULL;
9563#ifdef FEAT_SYN_HL
9564 int id;
9565 char_u *what;
9566 char_u *mode;
9567 char_u modebuf[NUMBUFLEN];
9568 int modec;
9569
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009570 id = (int)tv_get_number(&argvars[0]);
9571 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009572 if (argvars[2].v_type != VAR_UNKNOWN)
9573 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009574 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009575 modec = TOLOWER_ASC(mode[0]);
9576 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009577 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009578 }
9579 else
9580 {
9581#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9582 if (USE_24BIT)
9583 modec = 'g';
9584 else
9585#endif
9586 if (t_colors > 1)
9587 modec = 'c';
9588 else
9589 modec = 't';
9590 }
9591
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009592 switch (TOLOWER_ASC(what[0]))
9593 {
9594 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009595 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009596 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009597 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009598 p = highlight_has_attr(id, HL_BOLD, modec);
9599 break;
9600
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009601 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009602 p = highlight_color(id, what, modec);
9603 break;
9604
9605 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009606 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009607 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009608 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009609 p = highlight_has_attr(id, HL_ITALIC, modec);
9610 break;
9611
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009612 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009613 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009614 break;
9615
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009616 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009617 p = highlight_has_attr(id, HL_INVERSE, modec);
9618 break;
9619
9620 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009621 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009622 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009623 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009624 else if (TOLOWER_ASC(what[1]) == 't' &&
9625 TOLOWER_ASC(what[2]) == 'r')
9626 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009627 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009628 p = highlight_has_attr(id, HL_STANDOUT, modec);
9629 break;
9630
9631 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009632 if (TOLOWER_ASC(what[1]) == 'l') // ul
9633 p = highlight_color(id, what, modec);
9634 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009635 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009636 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9637 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009638 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009639 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9640 break;
9641 }
9642
9643 if (p != NULL)
9644 p = vim_strsave(p);
9645#endif
9646 rettv->v_type = VAR_STRING;
9647 rettv->vval.v_string = p;
9648}
9649
9650/*
9651 * "synIDtrans(id)" function
9652 */
9653 static void
9654f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9655{
9656 int id;
9657
9658#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009659 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009660
9661 if (id > 0)
9662 id = syn_get_final_id(id);
9663 else
9664#endif
9665 id = 0;
9666
9667 rettv->vval.v_number = id;
9668}
9669
9670/*
9671 * "synconcealed(lnum, col)" function
9672 */
9673 static void
9674f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9675{
9676#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9677 linenr_T lnum;
9678 colnr_T col;
9679 int syntax_flags = 0;
9680 int cchar;
9681 int matchid = 0;
9682 char_u str[NUMBUFLEN];
9683#endif
9684
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009685 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009686
9687#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009688 lnum = tv_get_lnum(argvars); // -1 on type error
9689 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009690
Bram Moolenaara80faa82020-04-12 19:37:17 +02009691 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009692
9693 if (rettv_list_alloc(rettv) != FAIL)
9694 {
9695 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9696 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9697 && curwin->w_p_cole > 0)
9698 {
9699 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9700 syntax_flags = get_syntax_info(&matchid);
9701
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009702 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009703 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9704 {
9705 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009706 if (cchar == NUL && curwin->w_p_cole == 1)
9707 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009708 if (cchar != NUL)
9709 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009710 if (has_mbyte)
9711 (*mb_char2bytes)(cchar, str);
9712 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009713 str[0] = cchar;
9714 }
9715 }
9716 }
9717
9718 list_append_number(rettv->vval.v_list,
9719 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009720 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009721 list_append_string(rettv->vval.v_list, str, -1);
9722 list_append_number(rettv->vval.v_list, matchid);
9723 }
9724#endif
9725}
9726
9727/*
9728 * "synstack(lnum, col)" function
9729 */
9730 static void
9731f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9732{
9733#ifdef FEAT_SYN_HL
9734 linenr_T lnum;
9735 colnr_T col;
9736 int i;
9737 int id;
9738#endif
9739
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009740 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009741
9742#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009743 lnum = tv_get_lnum(argvars); // -1 on type error
9744 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009745
9746 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9747 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9748 && rettv_list_alloc(rettv) != FAIL)
9749 {
9750 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9751 for (i = 0; ; ++i)
9752 {
9753 id = syn_get_stack_item(i);
9754 if (id < 0)
9755 break;
9756 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9757 break;
9758 }
9759 }
9760#endif
9761}
9762
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009763/*
9764 * "tabpagebuflist()" function
9765 */
9766 static void
9767f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9768{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009769 tabpage_T *tp;
9770 win_T *wp = NULL;
9771
9772 if (argvars[0].v_type == VAR_UNKNOWN)
9773 wp = firstwin;
9774 else
9775 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009776 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009777 if (tp != NULL)
9778 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9779 }
9780 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9781 {
9782 for (; wp != NULL; wp = wp->w_next)
9783 if (list_append_number(rettv->vval.v_list,
9784 wp->w_buffer->b_fnum) == FAIL)
9785 break;
9786 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009787}
9788
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009789/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009790 * "tagfiles()" function
9791 */
9792 static void
9793f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9794{
9795 char_u *fname;
9796 tagname_T tn;
9797 int first;
9798
9799 if (rettv_list_alloc(rettv) == FAIL)
9800 return;
9801 fname = alloc(MAXPATHL);
9802 if (fname == NULL)
9803 return;
9804
9805 for (first = TRUE; ; first = FALSE)
9806 if (get_tagfname(&tn, first, fname) == FAIL
9807 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9808 break;
9809 tagname_free(&tn);
9810 vim_free(fname);
9811}
9812
9813/*
9814 * "taglist()" function
9815 */
9816 static void
9817f_taglist(typval_T *argvars, typval_T *rettv)
9818{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009819 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009820 char_u *tag_pattern;
9821
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009822 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009823
9824 rettv->vval.v_number = FALSE;
9825 if (*tag_pattern == NUL)
9826 return;
9827
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009828 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009829 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009830 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009831 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009832}
9833
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009834#ifdef FEAT_FLOAT
9835/*
9836 * "tan()" function
9837 */
9838 static void
9839f_tan(typval_T *argvars, typval_T *rettv)
9840{
9841 float_T f = 0.0;
9842
9843 rettv->v_type = VAR_FLOAT;
9844 if (get_float_arg(argvars, &f) == OK)
9845 rettv->vval.v_float = tan(f);
9846 else
9847 rettv->vval.v_float = 0.0;
9848}
9849
9850/*
9851 * "tanh()" function
9852 */
9853 static void
9854f_tanh(typval_T *argvars, typval_T *rettv)
9855{
9856 float_T f = 0.0;
9857
9858 rettv->v_type = VAR_FLOAT;
9859 if (get_float_arg(argvars, &f) == OK)
9860 rettv->vval.v_float = tanh(f);
9861 else
9862 rettv->vval.v_float = 0.0;
9863}
9864#endif
9865
9866/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009867 * "tolower(string)" function
9868 */
9869 static void
9870f_tolower(typval_T *argvars, typval_T *rettv)
9871{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009872 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009873 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009874}
9875
9876/*
9877 * "toupper(string)" function
9878 */
9879 static void
9880f_toupper(typval_T *argvars, typval_T *rettv)
9881{
9882 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009883 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009884}
9885
9886/*
9887 * "tr(string, fromstr, tostr)" function
9888 */
9889 static void
9890f_tr(typval_T *argvars, typval_T *rettv)
9891{
9892 char_u *in_str;
9893 char_u *fromstr;
9894 char_u *tostr;
9895 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009896 int inlen;
9897 int fromlen;
9898 int tolen;
9899 int idx;
9900 char_u *cpstr;
9901 int cplen;
9902 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009903 char_u buf[NUMBUFLEN];
9904 char_u buf2[NUMBUFLEN];
9905 garray_T ga;
9906
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009907 in_str = tv_get_string(&argvars[0]);
9908 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9909 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009910
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009911 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009912 rettv->v_type = VAR_STRING;
9913 rettv->vval.v_string = NULL;
9914 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009915 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009916 ga_init2(&ga, (int)sizeof(char), 80);
9917
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009918 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009919 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009920 if (STRLEN(fromstr) != STRLEN(tostr))
9921 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009922error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009923 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009924 ga_clear(&ga);
9925 return;
9926 }
9927
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009928 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009929 while (*in_str != NUL)
9930 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009931 if (has_mbyte)
9932 {
9933 inlen = (*mb_ptr2len)(in_str);
9934 cpstr = in_str;
9935 cplen = inlen;
9936 idx = 0;
9937 for (p = fromstr; *p != NUL; p += fromlen)
9938 {
9939 fromlen = (*mb_ptr2len)(p);
9940 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9941 {
9942 for (p = tostr; *p != NUL; p += tolen)
9943 {
9944 tolen = (*mb_ptr2len)(p);
9945 if (idx-- == 0)
9946 {
9947 cplen = tolen;
9948 cpstr = p;
9949 break;
9950 }
9951 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009952 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009953 goto error;
9954 break;
9955 }
9956 ++idx;
9957 }
9958
9959 if (first && cpstr == in_str)
9960 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009961 // Check that fromstr and tostr have the same number of
9962 // (multi-byte) characters. Done only once when a character
9963 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009964 first = FALSE;
9965 for (p = tostr; *p != NUL; p += tolen)
9966 {
9967 tolen = (*mb_ptr2len)(p);
9968 --idx;
9969 }
9970 if (idx != 0)
9971 goto error;
9972 }
9973
9974 (void)ga_grow(&ga, cplen);
9975 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
9976 ga.ga_len += cplen;
9977
9978 in_str += inlen;
9979 }
9980 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009981 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009982 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009983 p = vim_strchr(fromstr, *in_str);
9984 if (p != NULL)
9985 ga_append(&ga, tostr[p - fromstr]);
9986 else
9987 ga_append(&ga, *in_str);
9988 ++in_str;
9989 }
9990 }
9991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009992 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009993 (void)ga_grow(&ga, 1);
9994 ga_append(&ga, NUL);
9995
9996 rettv->vval.v_string = ga.ga_data;
9997}
9998
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009999/*
10000 * "trim({expr})" function
10001 */
10002 static void
10003f_trim(typval_T *argvars, typval_T *rettv)
10004{
10005 char_u buf1[NUMBUFLEN];
10006 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010007 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010008 char_u *mask = NULL;
10009 char_u *tail;
10010 char_u *prev;
10011 char_u *p;
10012 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010013 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010014
10015 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010016 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010017 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010018 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010019
10020 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010021 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010022 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010023
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010024 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010025 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010026 int error = 0;
10027
10028 // leading or trailing characters to trim
10029 dir = (int)tv_get_number_chk(&argvars[2], &error);
10030 if (error)
10031 return;
10032 if (dir < 0 || dir > 2)
10033 {
10034 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
10035 return;
10036 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010037 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010038 }
10039
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010040 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010041 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010042 // Trim leading characters
10043 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010044 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010045 c1 = PTR2CHAR(head);
10046 if (mask == NULL)
10047 {
10048 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010049 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010050 }
10051 else
10052 {
10053 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10054 if (c1 == PTR2CHAR(p))
10055 break;
10056 if (*p == NUL)
10057 break;
10058 }
10059 MB_PTR_ADV(head);
10060 }
10061 }
10062
10063 tail = head + STRLEN(head);
10064 if (dir == 0 || dir == 2)
10065 {
10066 // Trim trailing characters
10067 for (; tail > head; tail = prev)
10068 {
10069 prev = tail;
10070 MB_PTR_BACK(head, prev);
10071 c1 = PTR2CHAR(prev);
10072 if (mask == NULL)
10073 {
10074 if (c1 > ' ' && c1 != 0xa0)
10075 break;
10076 }
10077 else
10078 {
10079 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10080 if (c1 == PTR2CHAR(p))
10081 break;
10082 if (*p == NUL)
10083 break;
10084 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010085 }
10086 }
Bram Moolenaardf44a272020-06-07 20:49:05 +020010087 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010088}
10089
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010090#ifdef FEAT_FLOAT
10091/*
10092 * "trunc({float})" function
10093 */
10094 static void
10095f_trunc(typval_T *argvars, typval_T *rettv)
10096{
10097 float_T f = 0.0;
10098
10099 rettv->v_type = VAR_FLOAT;
10100 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010101 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010102 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
10103 else
10104 rettv->vval.v_float = 0.0;
10105}
10106#endif
10107
10108/*
10109 * "type(expr)" function
10110 */
10111 static void
10112f_type(typval_T *argvars, typval_T *rettv)
10113{
10114 int n = -1;
10115
10116 switch (argvars[0].v_type)
10117 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010118 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
10119 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010120 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010121 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
10122 case VAR_LIST: n = VAR_TYPE_LIST; break;
10123 case VAR_DICT: n = VAR_TYPE_DICT; break;
10124 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
10125 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
10126 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020010127 case VAR_JOB: n = VAR_TYPE_JOB; break;
10128 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010129 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010130 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +020010131 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010132 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +010010133 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010134 n = -1;
10135 break;
10136 }
10137 rettv->vval.v_number = n;
10138}
10139
10140/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010141 * "virtcol(string)" function
10142 */
10143 static void
10144f_virtcol(typval_T *argvars, typval_T *rettv)
10145{
10146 colnr_T vcol = 0;
10147 pos_T *fp;
10148 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010149 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010150
Bram Moolenaar6f02b002021-01-10 20:22:54 +010010151 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010152 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
10153 && fnum == curbuf->b_fnum)
10154 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010155 // Limit the column to a valid value, getvvcol() doesn't check.
10156 if (fp->col < 0)
10157 fp->col = 0;
10158 else
10159 {
10160 len = (int)STRLEN(ml_get(fp->lnum));
10161 if (fp->col > len)
10162 fp->col = len;
10163 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010164 getvvcol(curwin, fp, NULL, NULL, &vcol);
10165 ++vcol;
10166 }
10167
10168 rettv->vval.v_number = vcol;
10169}
10170
10171/*
10172 * "visualmode()" function
10173 */
10174 static void
10175f_visualmode(typval_T *argvars, typval_T *rettv)
10176{
10177 char_u str[2];
10178
10179 rettv->v_type = VAR_STRING;
10180 str[0] = curbuf->b_visual_mode_eval;
10181 str[1] = NUL;
10182 rettv->vval.v_string = vim_strsave(str);
10183
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010184 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010185 if (non_zero_arg(&argvars[0]))
10186 curbuf->b_visual_mode_eval = NUL;
10187}
10188
10189/*
10190 * "wildmenumode()" function
10191 */
10192 static void
10193f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10194{
10195#ifdef FEAT_WILDMENU
10196 if (wild_menu_showing)
10197 rettv->vval.v_number = 1;
10198#endif
10199}
10200
10201/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +010010202 * "windowsversion()" function
10203 */
10204 static void
10205f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10206{
10207 rettv->v_type = VAR_STRING;
10208 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
10209}
10210
10211/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010212 * "wordcount()" function
10213 */
10214 static void
10215f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
10216{
10217 if (rettv_dict_alloc(rettv) == FAIL)
10218 return;
10219 cursor_pos_info(rettv->vval.v_dict);
10220}
10221
10222/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010223 * "xor(expr, expr)" function
10224 */
10225 static void
10226f_xor(typval_T *argvars, typval_T *rettv)
10227{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010228 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
10229 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010230}
10231
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010232#endif // FEAT_EVAL