blob: f15139f362c7dfb2f6e17ccab2f045e081f84fc5 [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},
Bram Moolenaar3b690062021-02-01 20:14:51 +0100957 {"flattennew", 1, 2, FEARG_1, NULL,
958 ret_list_any, f_flattennew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200959 {"float2nr", 1, 1, FEARG_1, NULL,
960 ret_number, FLOAT_FUNC(f_float2nr)},
961 {"floor", 1, 1, FEARG_1, NULL,
962 ret_float, FLOAT_FUNC(f_floor)},
963 {"fmod", 2, 2, FEARG_1, NULL,
964 ret_float, FLOAT_FUNC(f_fmod)},
965 {"fnameescape", 1, 1, FEARG_1, NULL,
966 ret_string, f_fnameescape},
967 {"fnamemodify", 2, 2, FEARG_1, NULL,
968 ret_string, f_fnamemodify},
969 {"foldclosed", 1, 1, FEARG_1, NULL,
970 ret_number, f_foldclosed},
971 {"foldclosedend", 1, 1, FEARG_1, NULL,
972 ret_number, f_foldclosedend},
973 {"foldlevel", 1, 1, FEARG_1, NULL,
974 ret_number, f_foldlevel},
975 {"foldtext", 0, 0, 0, NULL,
976 ret_string, f_foldtext},
977 {"foldtextresult", 1, 1, FEARG_1, NULL,
978 ret_string, f_foldtextresult},
979 {"foreground", 0, 0, 0, NULL,
980 ret_void, f_foreground},
981 {"funcref", 1, 3, FEARG_1, NULL,
982 ret_func_any, f_funcref},
983 {"function", 1, 3, FEARG_1, NULL,
984 ret_f_function, f_function},
985 {"garbagecollect", 0, 1, 0, NULL,
986 ret_void, f_garbagecollect},
987 {"get", 2, 3, FEARG_1, NULL,
988 ret_any, f_get},
989 {"getbufinfo", 0, 1, FEARG_1, NULL,
990 ret_list_dict_any, f_getbufinfo},
991 {"getbufline", 2, 3, FEARG_1, NULL,
992 ret_list_string, f_getbufline},
993 {"getbufvar", 2, 3, FEARG_1, NULL,
994 ret_any, f_getbufvar},
995 {"getchangelist", 0, 1, FEARG_1, NULL,
996 ret_list_any, f_getchangelist},
997 {"getchar", 0, 1, 0, NULL,
998 ret_number, f_getchar},
999 {"getcharmod", 0, 0, 0, NULL,
1000 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001001 {"getcharpos", 1, 1, FEARG_1, NULL,
1002 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001003 {"getcharsearch", 0, 0, 0, NULL,
1004 ret_dict_any, f_getcharsearch},
1005 {"getcmdline", 0, 0, 0, NULL,
1006 ret_string, f_getcmdline},
1007 {"getcmdpos", 0, 0, 0, NULL,
1008 ret_number, f_getcmdpos},
1009 {"getcmdtype", 0, 0, 0, NULL,
1010 ret_string, f_getcmdtype},
1011 {"getcmdwintype", 0, 0, 0, NULL,
1012 ret_string, f_getcmdwintype},
1013 {"getcompletion", 2, 3, FEARG_1, NULL,
1014 ret_list_string, f_getcompletion},
1015 {"getcurpos", 0, 1, FEARG_1, NULL,
1016 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001017 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
1018 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001019 {"getcwd", 0, 2, FEARG_1, NULL,
1020 ret_string, f_getcwd},
1021 {"getenv", 1, 1, FEARG_1, NULL,
1022 ret_string, f_getenv},
1023 {"getfontname", 0, 1, 0, NULL,
1024 ret_string, f_getfontname},
1025 {"getfperm", 1, 1, FEARG_1, NULL,
1026 ret_string, f_getfperm},
1027 {"getfsize", 1, 1, FEARG_1, NULL,
1028 ret_number, f_getfsize},
1029 {"getftime", 1, 1, FEARG_1, NULL,
1030 ret_number, f_getftime},
1031 {"getftype", 1, 1, FEARG_1, NULL,
1032 ret_string, f_getftype},
1033 {"getimstatus", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001034 ret_number_bool, f_getimstatus},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001035 {"getjumplist", 0, 2, FEARG_1, NULL,
1036 ret_list_any, f_getjumplist},
1037 {"getline", 1, 2, FEARG_1, NULL,
1038 ret_f_getline, f_getline},
1039 {"getloclist", 1, 2, 0, NULL,
1040 ret_list_or_dict_1, f_getloclist},
1041 {"getmarklist", 0, 1, FEARG_1, NULL,
1042 ret_list_dict_any, f_getmarklist},
1043 {"getmatches", 0, 1, 0, NULL,
1044 ret_list_dict_any, f_getmatches},
1045 {"getmousepos", 0, 0, 0, NULL,
1046 ret_dict_number, f_getmousepos},
1047 {"getpid", 0, 0, 0, NULL,
1048 ret_number, f_getpid},
1049 {"getpos", 1, 1, FEARG_1, NULL,
1050 ret_list_number, f_getpos},
1051 {"getqflist", 0, 1, 0, NULL,
1052 ret_list_or_dict_0, f_getqflist},
1053 {"getreg", 0, 3, FEARG_1, NULL,
1054 ret_getreg, f_getreg},
1055 {"getreginfo", 0, 1, FEARG_1, NULL,
1056 ret_dict_any, f_getreginfo},
1057 {"getregtype", 0, 1, FEARG_1, NULL,
1058 ret_string, f_getregtype},
1059 {"gettabinfo", 0, 1, FEARG_1, NULL,
1060 ret_list_dict_any, f_gettabinfo},
1061 {"gettabvar", 2, 3, FEARG_1, NULL,
1062 ret_any, f_gettabvar},
1063 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1064 ret_any, f_gettabwinvar},
1065 {"gettagstack", 0, 1, FEARG_1, NULL,
1066 ret_dict_any, f_gettagstack},
1067 {"gettext", 1, 1, FEARG_1, NULL,
1068 ret_string, f_gettext},
1069 {"getwininfo", 0, 1, FEARG_1, NULL,
1070 ret_list_dict_any, f_getwininfo},
1071 {"getwinpos", 0, 1, FEARG_1, NULL,
1072 ret_list_number, f_getwinpos},
1073 {"getwinposx", 0, 0, 0, NULL,
1074 ret_number, f_getwinposx},
1075 {"getwinposy", 0, 0, 0, NULL,
1076 ret_number, f_getwinposy},
1077 {"getwinvar", 2, 3, FEARG_1, NULL,
1078 ret_any, f_getwinvar},
1079 {"glob", 1, 4, FEARG_1, NULL,
1080 ret_any, f_glob},
1081 {"glob2regpat", 1, 1, FEARG_1, NULL,
1082 ret_string, f_glob2regpat},
1083 {"globpath", 2, 5, FEARG_2, NULL,
1084 ret_any, f_globpath},
1085 {"has", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001086 ret_number_bool, f_has},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001087 {"has_key", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001088 ret_number_bool, f_has_key},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001089 {"haslocaldir", 0, 2, FEARG_1, NULL,
1090 ret_number, f_haslocaldir},
1091 {"hasmapto", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001092 ret_number_bool, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001093 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1094 ret_number, f_hlID},
1095 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001096 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001097 {"histadd", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001098 ret_number_bool, f_histadd},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001099 {"histdel", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001100 ret_number_bool, f_histdel},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001101 {"histget", 1, 2, FEARG_1, NULL,
1102 ret_string, f_histget},
1103 {"histnr", 1, 1, FEARG_1, NULL,
1104 ret_number, f_histnr},
1105 {"hlID", 1, 1, FEARG_1, NULL,
1106 ret_number, f_hlID},
1107 {"hlexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001108 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001109 {"hostname", 0, 0, 0, NULL,
1110 ret_string, f_hostname},
1111 {"iconv", 3, 3, FEARG_1, NULL,
1112 ret_string, f_iconv},
1113 {"indent", 1, 1, FEARG_1, NULL,
1114 ret_number, f_indent},
1115 {"index", 2, 4, FEARG_1, NULL,
1116 ret_number, f_index},
1117 {"input", 1, 3, FEARG_1, NULL,
1118 ret_string, f_input},
1119 {"inputdialog", 1, 3, FEARG_1, NULL,
1120 ret_string, f_inputdialog},
1121 {"inputlist", 1, 1, FEARG_1, NULL,
1122 ret_number, f_inputlist},
1123 {"inputrestore", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001124 ret_number_bool, f_inputrestore},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001125 {"inputsave", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001126 ret_number_bool, f_inputsave},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001127 {"inputsecret", 1, 2, FEARG_1, NULL,
1128 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001129 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001130 ret_first_arg, f_insert},
1131 {"interrupt", 0, 0, 0, NULL,
1132 ret_void, f_interrupt},
1133 {"invert", 1, 1, FEARG_1, NULL,
1134 ret_number, f_invert},
1135 {"isdirectory", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001136 ret_number_bool, f_isdirectory},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001137 {"isinf", 1, 1, FEARG_1, NULL,
1138 ret_number, MATH_FUNC(f_isinf)},
1139 {"islocked", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001140 ret_number_bool, f_islocked},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001141 {"isnan", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001142 ret_number_bool, MATH_FUNC(f_isnan)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001143 {"items", 1, 1, FEARG_1, NULL,
1144 ret_list_any, f_items},
1145 {"job_getchannel", 1, 1, FEARG_1, NULL,
1146 ret_channel, JOB_FUNC(f_job_getchannel)},
1147 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001148 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001149 {"job_setoptions", 2, 2, FEARG_1, NULL,
1150 ret_void, JOB_FUNC(f_job_setoptions)},
1151 {"job_start", 1, 2, FEARG_1, NULL,
1152 ret_job, JOB_FUNC(f_job_start)},
1153 {"job_status", 1, 1, FEARG_1, NULL,
1154 ret_string, JOB_FUNC(f_job_status)},
1155 {"job_stop", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001156 ret_number_bool, JOB_FUNC(f_job_stop)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001157 {"join", 1, 2, FEARG_1, NULL,
1158 ret_string, f_join},
1159 {"js_decode", 1, 1, FEARG_1, NULL,
1160 ret_any, f_js_decode},
1161 {"js_encode", 1, 1, FEARG_1, NULL,
1162 ret_string, f_js_encode},
1163 {"json_decode", 1, 1, FEARG_1, NULL,
1164 ret_any, f_json_decode},
1165 {"json_encode", 1, 1, FEARG_1, NULL,
1166 ret_string, f_json_encode},
1167 {"keys", 1, 1, FEARG_1, NULL,
1168 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001169 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1170 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001171 {"len", 1, 1, FEARG_1, NULL,
1172 ret_number, f_len},
1173 {"libcall", 3, 3, FEARG_3, NULL,
1174 ret_string, f_libcall},
1175 {"libcallnr", 3, 3, FEARG_3, NULL,
1176 ret_number, f_libcallnr},
1177 {"line", 1, 2, FEARG_1, NULL,
1178 ret_number, f_line},
1179 {"line2byte", 1, 1, FEARG_1, NULL,
1180 ret_number, f_line2byte},
1181 {"lispindent", 1, 1, FEARG_1, NULL,
1182 ret_number, f_lispindent},
1183 {"list2str", 1, 2, FEARG_1, NULL,
1184 ret_string, f_list2str},
1185 {"listener_add", 1, 2, FEARG_2, NULL,
1186 ret_number, f_listener_add},
1187 {"listener_flush", 0, 1, FEARG_1, NULL,
1188 ret_void, f_listener_flush},
1189 {"listener_remove", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001190 ret_number_bool, f_listener_remove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001191 {"localtime", 0, 0, 0, NULL,
1192 ret_number, f_localtime},
1193 {"log", 1, 1, FEARG_1, NULL,
1194 ret_float, FLOAT_FUNC(f_log)},
1195 {"log10", 1, 1, FEARG_1, NULL,
1196 ret_float, FLOAT_FUNC(f_log10)},
1197 {"luaeval", 1, 2, FEARG_1, NULL,
1198 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001199#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001200 f_luaeval
1201#else
1202 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001203#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001204 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001205 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001206 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001207 {"maparg", 1, 4, FEARG_1, NULL,
1208 ret_maparg, f_maparg},
1209 {"mapcheck", 1, 3, FEARG_1, NULL,
1210 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001211 {"mapnew", 2, 2, FEARG_1, NULL,
1212 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001213 {"mapset", 3, 3, FEARG_1, NULL,
1214 ret_void, f_mapset},
1215 {"match", 2, 4, FEARG_1, NULL,
1216 ret_any, f_match},
1217 {"matchadd", 2, 5, FEARG_1, NULL,
1218 ret_number, f_matchadd},
1219 {"matchaddpos", 2, 5, FEARG_1, NULL,
1220 ret_number, f_matchaddpos},
1221 {"matcharg", 1, 1, FEARG_1, NULL,
1222 ret_list_string, f_matcharg},
1223 {"matchdelete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001224 ret_number_bool, f_matchdelete},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001225 {"matchend", 2, 4, FEARG_1, NULL,
1226 ret_number, f_matchend},
1227 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1228 ret_list_string, f_matchfuzzy},
1229 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1230 ret_list_any, f_matchfuzzypos},
1231 {"matchlist", 2, 4, FEARG_1, NULL,
1232 ret_list_string, f_matchlist},
1233 {"matchstr", 2, 4, FEARG_1, NULL,
1234 ret_string, f_matchstr},
1235 {"matchstrpos", 2, 4, FEARG_1, NULL,
1236 ret_list_any, f_matchstrpos},
1237 {"max", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001238 ret_number, f_max},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001239 {"menu_info", 1, 2, FEARG_1, NULL,
1240 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001241#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001242 f_menu_info
1243#else
1244 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001245#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001246 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001247 {"min", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001248 ret_number, f_min},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001249 {"mkdir", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001250 ret_number_bool, f_mkdir},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001251 {"mode", 0, 1, FEARG_1, NULL,
1252 ret_string, f_mode},
1253 {"mzeval", 1, 1, FEARG_1, NULL,
1254 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001255#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001256 f_mzeval
1257#else
1258 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001259#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001260 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001261 {"nextnonblank", 1, 1, FEARG_1, NULL,
1262 ret_number, f_nextnonblank},
1263 {"nr2char", 1, 2, FEARG_1, NULL,
1264 ret_string, f_nr2char},
1265 {"or", 2, 2, FEARG_1, NULL,
1266 ret_number, f_or},
1267 {"pathshorten", 1, 2, FEARG_1, NULL,
1268 ret_string, f_pathshorten},
1269 {"perleval", 1, 1, FEARG_1, NULL,
1270 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001271#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001272 f_perleval
1273#else
1274 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001275#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001276 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001277 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1278 ret_number, PROP_FUNC(f_popup_atcursor)},
1279 {"popup_beval", 2, 2, FEARG_1, NULL,
1280 ret_number, PROP_FUNC(f_popup_beval)},
1281 {"popup_clear", 0, 1, 0, NULL,
1282 ret_void, PROP_FUNC(f_popup_clear)},
1283 {"popup_close", 1, 2, FEARG_1, NULL,
1284 ret_void, PROP_FUNC(f_popup_close)},
1285 {"popup_create", 2, 2, FEARG_1, NULL,
1286 ret_number, PROP_FUNC(f_popup_create)},
1287 {"popup_dialog", 2, 2, FEARG_1, NULL,
1288 ret_number, PROP_FUNC(f_popup_dialog)},
1289 {"popup_filter_menu", 2, 2, 0, NULL,
1290 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1291 {"popup_filter_yesno", 2, 2, 0, NULL,
1292 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1293 {"popup_findinfo", 0, 0, 0, NULL,
1294 ret_number, PROP_FUNC(f_popup_findinfo)},
1295 {"popup_findpreview", 0, 0, 0, NULL,
1296 ret_number, PROP_FUNC(f_popup_findpreview)},
1297 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1298 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1299 {"popup_getpos", 1, 1, FEARG_1, NULL,
1300 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1301 {"popup_hide", 1, 1, FEARG_1, NULL,
1302 ret_void, PROP_FUNC(f_popup_hide)},
1303 {"popup_list", 0, 0, 0, NULL,
1304 ret_list_number, PROP_FUNC(f_popup_list)},
1305 {"popup_locate", 2, 2, 0, NULL,
1306 ret_number, PROP_FUNC(f_popup_locate)},
1307 {"popup_menu", 2, 2, FEARG_1, NULL,
1308 ret_number, PROP_FUNC(f_popup_menu)},
1309 {"popup_move", 2, 2, FEARG_1, NULL,
1310 ret_void, PROP_FUNC(f_popup_move)},
1311 {"popup_notification", 2, 2, FEARG_1, NULL,
1312 ret_number, PROP_FUNC(f_popup_notification)},
1313 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1314 ret_void, PROP_FUNC(f_popup_setoptions)},
1315 {"popup_settext", 2, 2, FEARG_1, NULL,
1316 ret_void, PROP_FUNC(f_popup_settext)},
1317 {"popup_show", 1, 1, FEARG_1, NULL,
1318 ret_void, PROP_FUNC(f_popup_show)},
1319 {"pow", 2, 2, FEARG_1, NULL,
1320 ret_float, FLOAT_FUNC(f_pow)},
1321 {"prevnonblank", 1, 1, FEARG_1, NULL,
1322 ret_number, f_prevnonblank},
1323 {"printf", 1, 19, FEARG_2, NULL,
1324 ret_string, f_printf},
1325 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1326 ret_string, JOB_FUNC(f_prompt_getprompt)},
1327 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1328 ret_void, JOB_FUNC(f_prompt_setcallback)},
1329 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1330 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1331 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1332 ret_void, JOB_FUNC(f_prompt_setprompt)},
1333 {"prop_add", 3, 3, FEARG_1, NULL,
1334 ret_void, PROP_FUNC(f_prop_add)},
1335 {"prop_clear", 1, 3, FEARG_1, NULL,
1336 ret_void, PROP_FUNC(f_prop_clear)},
1337 {"prop_find", 1, 2, FEARG_1, NULL,
1338 ret_dict_any, PROP_FUNC(f_prop_find)},
1339 {"prop_list", 1, 2, FEARG_1, NULL,
1340 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1341 {"prop_remove", 1, 3, FEARG_1, NULL,
1342 ret_number, PROP_FUNC(f_prop_remove)},
1343 {"prop_type_add", 2, 2, FEARG_1, NULL,
1344 ret_void, PROP_FUNC(f_prop_type_add)},
1345 {"prop_type_change", 2, 2, FEARG_1, NULL,
1346 ret_void, PROP_FUNC(f_prop_type_change)},
1347 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1348 ret_void, PROP_FUNC(f_prop_type_delete)},
1349 {"prop_type_get", 1, 2, FEARG_1, NULL,
1350 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1351 {"prop_type_list", 0, 1, FEARG_1, NULL,
1352 ret_list_string, PROP_FUNC(f_prop_type_list)},
1353 {"pum_getpos", 0, 0, 0, NULL,
1354 ret_dict_number, f_pum_getpos},
1355 {"pumvisible", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001356 ret_number_bool, f_pumvisible},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001357 {"py3eval", 1, 1, FEARG_1, NULL,
1358 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001359#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001360 f_py3eval
1361#else
1362 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001363#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001364 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001365 {"pyeval", 1, 1, FEARG_1, NULL,
1366 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001367#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001368 f_pyeval
1369#else
1370 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001371#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001372 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001373 {"pyxeval", 1, 1, FEARG_1, NULL,
1374 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001375#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001376 f_pyxeval
1377#else
1378 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001379#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001380 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001381 {"rand", 0, 1, FEARG_1, NULL,
1382 ret_number, f_rand},
1383 {"range", 1, 3, FEARG_1, NULL,
1384 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001385 {"readblob", 1, 1, FEARG_1, NULL,
1386 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001387 {"readdir", 1, 3, FEARG_1, NULL,
1388 ret_list_string, f_readdir},
1389 {"readdirex", 1, 3, FEARG_1, NULL,
1390 ret_list_dict_any, f_readdirex},
1391 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001392 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001393 {"reduce", 2, 3, FEARG_1, NULL,
1394 ret_any, f_reduce},
1395 {"reg_executing", 0, 0, 0, NULL,
1396 ret_string, f_reg_executing},
1397 {"reg_recording", 0, 0, 0, NULL,
1398 ret_string, f_reg_recording},
1399 {"reltime", 0, 2, FEARG_1, NULL,
1400 ret_list_any, f_reltime},
1401 {"reltimefloat", 1, 1, FEARG_1, NULL,
1402 ret_float, FLOAT_FUNC(f_reltimefloat)},
1403 {"reltimestr", 1, 1, FEARG_1, NULL,
1404 ret_string, f_reltimestr},
1405 {"remote_expr", 2, 4, FEARG_1, NULL,
1406 ret_string, f_remote_expr},
1407 {"remote_foreground", 1, 1, FEARG_1, NULL,
1408 ret_string, f_remote_foreground},
1409 {"remote_peek", 1, 2, FEARG_1, NULL,
1410 ret_number, f_remote_peek},
1411 {"remote_read", 1, 2, FEARG_1, NULL,
1412 ret_string, f_remote_read},
1413 {"remote_send", 2, 3, FEARG_1, NULL,
1414 ret_string, f_remote_send},
1415 {"remote_startserver", 1, 1, FEARG_1, NULL,
1416 ret_void, f_remote_startserver},
1417 {"remove", 2, 3, FEARG_1, NULL,
1418 ret_remove, f_remove},
1419 {"rename", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001420 ret_number_bool, f_rename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001421 {"repeat", 2, 2, FEARG_1, NULL,
1422 ret_first_arg, f_repeat},
1423 {"resolve", 1, 1, FEARG_1, NULL,
1424 ret_string, f_resolve},
1425 {"reverse", 1, 1, FEARG_1, NULL,
1426 ret_first_arg, f_reverse},
1427 {"round", 1, 1, FEARG_1, NULL,
1428 ret_float, FLOAT_FUNC(f_round)},
1429 {"rubyeval", 1, 1, FEARG_1, NULL,
1430 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001431#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001432 f_rubyeval
1433#else
1434 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001435#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001436 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001437 {"screenattr", 2, 2, FEARG_1, NULL,
1438 ret_number, f_screenattr},
1439 {"screenchar", 2, 2, FEARG_1, NULL,
1440 ret_number, f_screenchar},
1441 {"screenchars", 2, 2, FEARG_1, NULL,
1442 ret_list_number, f_screenchars},
1443 {"screencol", 0, 0, 0, NULL,
1444 ret_number, f_screencol},
1445 {"screenpos", 3, 3, FEARG_1, NULL,
1446 ret_dict_number, f_screenpos},
1447 {"screenrow", 0, 0, 0, NULL,
1448 ret_number, f_screenrow},
1449 {"screenstring", 2, 2, FEARG_1, NULL,
1450 ret_string, f_screenstring},
1451 {"search", 1, 5, FEARG_1, NULL,
1452 ret_number, f_search},
1453 {"searchcount", 0, 1, FEARG_1, NULL,
1454 ret_dict_any, f_searchcount},
1455 {"searchdecl", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001456 ret_number_bool, f_searchdecl},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001457 {"searchpair", 3, 7, 0, NULL,
1458 ret_number, f_searchpair},
1459 {"searchpairpos", 3, 7, 0, NULL,
1460 ret_list_number, f_searchpairpos},
1461 {"searchpos", 1, 5, FEARG_1, NULL,
1462 ret_list_number, f_searchpos},
1463 {"server2client", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001464 ret_number_bool, f_server2client},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001465 {"serverlist", 0, 0, 0, NULL,
1466 ret_string, f_serverlist},
1467 {"setbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001468 ret_number_bool, f_setbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001469 {"setbufvar", 3, 3, FEARG_3, NULL,
1470 ret_void, f_setbufvar},
1471 {"setcellwidths", 1, 1, FEARG_1, NULL,
1472 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001473 {"setcharpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001474 ret_number_bool, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001475 {"setcharsearch", 1, 1, FEARG_1, NULL,
1476 ret_void, f_setcharsearch},
1477 {"setcmdpos", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001478 ret_number_bool, f_setcmdpos},
1479 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1480 ret_number_bool, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001481 {"setenv", 2, 2, FEARG_2, NULL,
1482 ret_void, f_setenv},
1483 {"setfperm", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001484 ret_number_bool, f_setfperm},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001485 {"setline", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001486 ret_number_bool, f_setline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001487 {"setloclist", 2, 4, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001488 ret_number_bool, f_setloclist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001489 {"setmatches", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001490 ret_number_bool, f_setmatches},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001491 {"setpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001492 ret_number_bool, f_setpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001493 {"setqflist", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001494 ret_number_bool, f_setqflist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001495 {"setreg", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001496 ret_number_bool, f_setreg},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001497 {"settabvar", 3, 3, FEARG_3, NULL,
1498 ret_void, f_settabvar},
1499 {"settabwinvar", 4, 4, FEARG_4, NULL,
1500 ret_void, f_settabwinvar},
1501 {"settagstack", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001502 ret_number_bool, f_settagstack},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001503 {"setwinvar", 3, 3, FEARG_3, NULL,
1504 ret_void, f_setwinvar},
1505 {"sha256", 1, 1, FEARG_1, NULL,
1506 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001507#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001508 f_sha256
1509#else
1510 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001511#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001512 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001513 {"shellescape", 1, 2, FEARG_1, NULL,
1514 ret_string, f_shellescape},
1515 {"shiftwidth", 0, 1, FEARG_1, NULL,
1516 ret_number, f_shiftwidth},
1517 {"sign_define", 1, 2, FEARG_1, NULL,
1518 ret_any, SIGN_FUNC(f_sign_define)},
1519 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1520 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1521 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1522 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1523 {"sign_jump", 3, 3, FEARG_1, NULL,
1524 ret_number, SIGN_FUNC(f_sign_jump)},
1525 {"sign_place", 4, 5, FEARG_1, NULL,
1526 ret_number, SIGN_FUNC(f_sign_place)},
1527 {"sign_placelist", 1, 1, FEARG_1, NULL,
1528 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1529 {"sign_undefine", 0, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001530 ret_number_bool, SIGN_FUNC(f_sign_undefine)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001531 {"sign_unplace", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001532 ret_number_bool, SIGN_FUNC(f_sign_unplace)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001533 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1534 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1535 {"simplify", 1, 1, FEARG_1, NULL,
1536 ret_string, f_simplify},
1537 {"sin", 1, 1, FEARG_1, NULL,
1538 ret_float, FLOAT_FUNC(f_sin)},
1539 {"sinh", 1, 1, FEARG_1, NULL,
1540 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001541 {"slice", 2, 3, FEARG_1, NULL,
1542 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001543 {"sort", 1, 3, FEARG_1, NULL,
1544 ret_first_arg, f_sort},
1545 {"sound_clear", 0, 0, 0, NULL,
1546 ret_void, SOUND_FUNC(f_sound_clear)},
1547 {"sound_playevent", 1, 2, FEARG_1, NULL,
1548 ret_number, SOUND_FUNC(f_sound_playevent)},
1549 {"sound_playfile", 1, 2, FEARG_1, NULL,
1550 ret_number, SOUND_FUNC(f_sound_playfile)},
1551 {"sound_stop", 1, 1, FEARG_1, NULL,
1552 ret_void, SOUND_FUNC(f_sound_stop)},
1553 {"soundfold", 1, 1, FEARG_1, NULL,
1554 ret_string, f_soundfold},
1555 {"spellbadword", 0, 1, FEARG_1, NULL,
1556 ret_list_string, f_spellbadword},
1557 {"spellsuggest", 1, 3, FEARG_1, NULL,
1558 ret_list_string, f_spellsuggest},
1559 {"split", 1, 3, FEARG_1, NULL,
1560 ret_list_string, f_split},
1561 {"sqrt", 1, 1, FEARG_1, NULL,
1562 ret_float, FLOAT_FUNC(f_sqrt)},
1563 {"srand", 0, 1, FEARG_1, NULL,
1564 ret_list_number, f_srand},
1565 {"state", 0, 1, FEARG_1, NULL,
1566 ret_string, f_state},
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001567 {"str2float", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001568 ret_float, FLOAT_FUNC(f_str2float)},
1569 {"str2list", 1, 2, FEARG_1, NULL,
1570 ret_list_number, f_str2list},
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001571 {"str2nr", 1, 3, FEARG_1, arg3_string_nr_bool,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001572 ret_number, f_str2nr},
1573 {"strcharpart", 2, 3, FEARG_1, NULL,
1574 ret_string, f_strcharpart},
1575 {"strchars", 1, 2, FEARG_1, NULL,
1576 ret_number, f_strchars},
1577 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1578 ret_number, f_strdisplaywidth},
1579 {"strftime", 1, 2, FEARG_1, NULL,
1580 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001581#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001582 f_strftime
1583#else
1584 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001585#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001586 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001587 {"strgetchar", 2, 2, FEARG_1, NULL,
1588 ret_number, f_strgetchar},
1589 {"stridx", 2, 3, FEARG_1, NULL,
1590 ret_number, f_stridx},
1591 {"string", 1, 1, FEARG_1, NULL,
1592 ret_string, f_string},
1593 {"strlen", 1, 1, FEARG_1, NULL,
1594 ret_number, f_strlen},
1595 {"strpart", 2, 4, FEARG_1, NULL,
1596 ret_string, f_strpart},
1597 {"strptime", 2, 2, FEARG_1, NULL,
1598 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001599#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001600 f_strptime
1601#else
1602 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001603#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001604 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001605 {"strridx", 2, 3, FEARG_1, NULL,
1606 ret_number, f_strridx},
1607 {"strtrans", 1, 1, FEARG_1, NULL,
1608 ret_string, f_strtrans},
1609 {"strwidth", 1, 1, FEARG_1, NULL,
1610 ret_number, f_strwidth},
1611 {"submatch", 1, 2, FEARG_1, NULL,
1612 ret_string, f_submatch},
1613 {"substitute", 4, 4, FEARG_1, NULL,
1614 ret_string, f_substitute},
1615 {"swapinfo", 1, 1, FEARG_1, NULL,
1616 ret_dict_any, f_swapinfo},
1617 {"swapname", 1, 1, FEARG_1, NULL,
1618 ret_string, f_swapname},
1619 {"synID", 3, 3, 0, NULL,
1620 ret_number, f_synID},
1621 {"synIDattr", 2, 3, FEARG_1, NULL,
1622 ret_string, f_synIDattr},
1623 {"synIDtrans", 1, 1, FEARG_1, NULL,
1624 ret_number, f_synIDtrans},
1625 {"synconcealed", 2, 2, 0, NULL,
1626 ret_list_any, f_synconcealed},
1627 {"synstack", 2, 2, 0, NULL,
1628 ret_list_number, f_synstack},
1629 {"system", 1, 2, FEARG_1, NULL,
1630 ret_string, f_system},
1631 {"systemlist", 1, 2, FEARG_1, NULL,
1632 ret_list_string, f_systemlist},
1633 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1634 ret_list_number, f_tabpagebuflist},
1635 {"tabpagenr", 0, 1, 0, NULL,
1636 ret_number, f_tabpagenr},
1637 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1638 ret_number, f_tabpagewinnr},
1639 {"tagfiles", 0, 0, 0, NULL,
1640 ret_list_string, f_tagfiles},
1641 {"taglist", 1, 2, FEARG_1, NULL,
1642 ret_list_dict_any, f_taglist},
1643 {"tan", 1, 1, FEARG_1, NULL,
1644 ret_float, FLOAT_FUNC(f_tan)},
1645 {"tanh", 1, 1, FEARG_1, NULL,
1646 ret_float, FLOAT_FUNC(f_tanh)},
1647 {"tempname", 0, 0, 0, NULL,
1648 ret_string, f_tempname},
1649 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1650 ret_number, TERM_FUNC(f_term_dumpdiff)},
1651 {"term_dumpload", 1, 2, FEARG_1, NULL,
1652 ret_number, TERM_FUNC(f_term_dumpload)},
1653 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1654 ret_void, TERM_FUNC(f_term_dumpwrite)},
1655 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1656 ret_number, TERM_FUNC(f_term_getaltscreen)},
1657 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1658 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001659#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001660 f_term_getansicolors
1661#else
1662 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001663#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001664 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001665 {"term_getattr", 2, 2, FEARG_1, NULL,
1666 ret_number, TERM_FUNC(f_term_getattr)},
1667 {"term_getcursor", 1, 1, FEARG_1, NULL,
1668 ret_list_any, TERM_FUNC(f_term_getcursor)},
1669 {"term_getjob", 1, 1, FEARG_1, NULL,
1670 ret_job, TERM_FUNC(f_term_getjob)},
1671 {"term_getline", 2, 2, FEARG_1, NULL,
1672 ret_string, TERM_FUNC(f_term_getline)},
1673 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1674 ret_number, TERM_FUNC(f_term_getscrolled)},
1675 {"term_getsize", 1, 1, FEARG_1, NULL,
1676 ret_list_number, TERM_FUNC(f_term_getsize)},
1677 {"term_getstatus", 1, 1, FEARG_1, NULL,
1678 ret_string, TERM_FUNC(f_term_getstatus)},
1679 {"term_gettitle", 1, 1, FEARG_1, NULL,
1680 ret_string, TERM_FUNC(f_term_gettitle)},
1681 {"term_gettty", 1, 2, FEARG_1, NULL,
1682 ret_string, TERM_FUNC(f_term_gettty)},
1683 {"term_list", 0, 0, 0, NULL,
1684 ret_list_number, TERM_FUNC(f_term_list)},
1685 {"term_scrape", 2, 2, FEARG_1, NULL,
1686 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1687 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1688 ret_void, TERM_FUNC(f_term_sendkeys)},
1689 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1690 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001691#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001692 f_term_setansicolors
1693#else
1694 NULL
1695#endif
1696 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001697 {"term_setapi", 2, 2, FEARG_1, NULL,
1698 ret_void, TERM_FUNC(f_term_setapi)},
1699 {"term_setkill", 2, 2, FEARG_1, NULL,
1700 ret_void, TERM_FUNC(f_term_setkill)},
1701 {"term_setrestore", 2, 2, FEARG_1, NULL,
1702 ret_void, TERM_FUNC(f_term_setrestore)},
1703 {"term_setsize", 3, 3, FEARG_1, NULL,
1704 ret_void, TERM_FUNC(f_term_setsize)},
1705 {"term_start", 1, 2, FEARG_1, NULL,
1706 ret_number, TERM_FUNC(f_term_start)},
1707 {"term_wait", 1, 2, FEARG_1, NULL,
1708 ret_void, TERM_FUNC(f_term_wait)},
1709 {"terminalprops", 0, 0, 0, NULL,
1710 ret_dict_string, f_terminalprops},
1711 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1712 ret_void, f_test_alloc_fail},
1713 {"test_autochdir", 0, 0, 0, NULL,
1714 ret_void, f_test_autochdir},
1715 {"test_feedinput", 1, 1, FEARG_1, NULL,
1716 ret_void, f_test_feedinput},
1717 {"test_garbagecollect_now", 0, 0, 0, NULL,
1718 ret_void, f_test_garbagecollect_now},
1719 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1720 ret_void, f_test_garbagecollect_soon},
1721 {"test_getvalue", 1, 1, FEARG_1, NULL,
1722 ret_number, f_test_getvalue},
1723 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1724 ret_void, f_test_ignore_error},
1725 {"test_null_blob", 0, 0, 0, NULL,
1726 ret_blob, f_test_null_blob},
1727 {"test_null_channel", 0, 0, 0, NULL,
1728 ret_channel, JOB_FUNC(f_test_null_channel)},
1729 {"test_null_dict", 0, 0, 0, NULL,
1730 ret_dict_any, f_test_null_dict},
1731 {"test_null_function", 0, 0, 0, NULL,
1732 ret_func_any, f_test_null_function},
1733 {"test_null_job", 0, 0, 0, NULL,
1734 ret_job, JOB_FUNC(f_test_null_job)},
1735 {"test_null_list", 0, 0, 0, NULL,
1736 ret_list_any, f_test_null_list},
1737 {"test_null_partial", 0, 0, 0, NULL,
1738 ret_func_any, f_test_null_partial},
1739 {"test_null_string", 0, 0, 0, NULL,
1740 ret_string, f_test_null_string},
1741 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1742 ret_void, f_test_option_not_set},
1743 {"test_override", 2, 2, FEARG_2, NULL,
1744 ret_void, f_test_override},
1745 {"test_refcount", 1, 1, FEARG_1, NULL,
1746 ret_number, f_test_refcount},
1747 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1748 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001749#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001750 f_test_scrollbar
1751#else
1752 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001753#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001754 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001755 {"test_setmouse", 2, 2, 0, NULL,
1756 ret_void, f_test_setmouse},
1757 {"test_settime", 1, 1, FEARG_1, NULL,
1758 ret_void, f_test_settime},
1759 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1760 ret_void, f_test_srand_seed},
1761 {"test_unknown", 0, 0, 0, NULL,
1762 ret_any, f_test_unknown},
1763 {"test_void", 0, 0, 0, NULL,
1764 ret_void, f_test_void},
1765 {"timer_info", 0, 1, FEARG_1, NULL,
1766 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1767 {"timer_pause", 2, 2, FEARG_1, NULL,
1768 ret_void, TIMER_FUNC(f_timer_pause)},
1769 {"timer_start", 2, 3, FEARG_1, NULL,
1770 ret_number, TIMER_FUNC(f_timer_start)},
1771 {"timer_stop", 1, 1, FEARG_1, NULL,
1772 ret_void, TIMER_FUNC(f_timer_stop)},
1773 {"timer_stopall", 0, 0, 0, NULL,
1774 ret_void, TIMER_FUNC(f_timer_stopall)},
1775 {"tolower", 1, 1, FEARG_1, NULL,
1776 ret_string, f_tolower},
1777 {"toupper", 1, 1, FEARG_1, NULL,
1778 ret_string, f_toupper},
1779 {"tr", 3, 3, FEARG_1, NULL,
1780 ret_string, f_tr},
1781 {"trim", 1, 3, FEARG_1, NULL,
1782 ret_string, f_trim},
1783 {"trunc", 1, 1, FEARG_1, NULL,
1784 ret_float, FLOAT_FUNC(f_trunc)},
1785 {"type", 1, 1, FEARG_1, NULL,
1786 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001787 {"typename", 1, 1, FEARG_1, NULL,
1788 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001789 {"undofile", 1, 1, FEARG_1, NULL,
1790 ret_string, f_undofile},
1791 {"undotree", 0, 0, 0, NULL,
1792 ret_dict_any, f_undotree},
1793 {"uniq", 1, 3, FEARG_1, NULL,
1794 ret_list_any, f_uniq},
1795 {"values", 1, 1, FEARG_1, NULL,
1796 ret_list_any, f_values},
1797 {"virtcol", 1, 1, FEARG_1, NULL,
1798 ret_number, f_virtcol},
1799 {"visualmode", 0, 1, 0, NULL,
1800 ret_string, f_visualmode},
1801 {"wildmenumode", 0, 0, 0, NULL,
1802 ret_number, f_wildmenumode},
1803 {"win_execute", 2, 3, FEARG_2, NULL,
1804 ret_string, f_win_execute},
1805 {"win_findbuf", 1, 1, FEARG_1, NULL,
1806 ret_list_number, f_win_findbuf},
1807 {"win_getid", 0, 2, FEARG_1, NULL,
1808 ret_number, f_win_getid},
1809 {"win_gettype", 0, 1, FEARG_1, NULL,
1810 ret_string, f_win_gettype},
1811 {"win_gotoid", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001812 ret_number_bool, f_win_gotoid},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001813 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1814 ret_list_number, f_win_id2tabwin},
1815 {"win_id2win", 1, 1, FEARG_1, NULL,
1816 ret_number, f_win_id2win},
1817 {"win_screenpos", 1, 1, FEARG_1, NULL,
1818 ret_list_number, f_win_screenpos},
1819 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001820 ret_number_bool, f_win_splitmove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001821 {"winbufnr", 1, 1, FEARG_1, NULL,
1822 ret_number, f_winbufnr},
1823 {"wincol", 0, 0, 0, NULL,
1824 ret_number, f_wincol},
1825 {"windowsversion", 0, 0, 0, NULL,
1826 ret_string, f_windowsversion},
1827 {"winheight", 1, 1, FEARG_1, NULL,
1828 ret_number, f_winheight},
1829 {"winlayout", 0, 1, FEARG_1, NULL,
1830 ret_list_any, f_winlayout},
1831 {"winline", 0, 0, 0, NULL,
1832 ret_number, f_winline},
1833 {"winnr", 0, 1, FEARG_1, NULL,
1834 ret_number, f_winnr},
1835 {"winrestcmd", 0, 0, 0, NULL,
1836 ret_string, f_winrestcmd},
1837 {"winrestview", 1, 1, FEARG_1, NULL,
1838 ret_void, f_winrestview},
1839 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001840 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001841 {"winwidth", 1, 1, FEARG_1, NULL,
1842 ret_number, f_winwidth},
1843 {"wordcount", 0, 0, 0, NULL,
1844 ret_dict_number, f_wordcount},
1845 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001846 ret_number_bool, f_writefile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001847 {"xor", 2, 2, FEARG_1, NULL,
1848 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001849};
1850
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001851/*
1852 * Function given to ExpandGeneric() to obtain the list of internal
1853 * or user defined function names.
1854 */
1855 char_u *
1856get_function_name(expand_T *xp, int idx)
1857{
1858 static int intidx = -1;
1859 char_u *name;
1860
1861 if (idx == 0)
1862 intidx = -1;
1863 if (intidx < 0)
1864 {
1865 name = get_user_func_name(xp, idx);
1866 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001867 {
1868 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1869 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001870 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001871 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001872 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001873 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001874 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001875 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001876 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001877 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001878 STRCAT(IObuff, ")");
1879 return IObuff;
1880 }
1881
1882 return NULL;
1883}
1884
1885/*
1886 * Function given to ExpandGeneric() to obtain the list of internal or
1887 * user defined variable or function names.
1888 */
1889 char_u *
1890get_expr_name(expand_T *xp, int idx)
1891{
1892 static int intidx = -1;
1893 char_u *name;
1894
1895 if (idx == 0)
1896 intidx = -1;
1897 if (intidx < 0)
1898 {
1899 name = get_function_name(xp, idx);
1900 if (name != NULL)
1901 return name;
1902 }
1903 return get_user_var_name(xp, ++intidx);
1904}
1905
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001906/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001907 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001908 * Return index, or -1 if not found or "implemented" is TRUE and the function
1909 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001910 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001911 static int
1912find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001913{
1914 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001915 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001916 int cmp;
1917 int x;
1918
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001919 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001920
1921 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001922 while (first <= last)
1923 {
1924 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001925 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001926 if (cmp < 0)
1927 last = x - 1;
1928 else if (cmp > 0)
1929 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001930 else if (implemented && global_functions[x].f_func == NULL)
1931 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001932 else
1933 return x;
1934 }
1935 return -1;
1936}
1937
Bram Moolenaar15c47602020-03-26 22:16:48 +01001938/*
1939 * Find internal function "name" in table "global_functions".
1940 * Return index, or -1 if not found or the function is not implemented.
1941 */
1942 int
1943find_internal_func(char_u *name)
1944{
1945 return find_internal_func_opt(name, TRUE);
1946}
1947
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001948 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001949has_internal_func(char_u *name)
1950{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001951 return find_internal_func_opt(name, TRUE) >= 0;
1952}
1953
1954 static int
1955has_internal_func_name(char_u *name)
1956{
1957 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001958}
1959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 char *
1961internal_func_name(int idx)
1962{
1963 return global_functions[idx].f_name;
1964}
1965
Bram Moolenaar94738d82020-10-21 14:25:07 +02001966/*
1967 * Check the argument types for builting function "idx".
1968 * Uses the list of types on the type stack: "types".
1969 * Return FAIL and gives an error message when a type is wrong.
1970 */
1971 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001972internal_func_check_arg_types(
1973 type_T **types,
1974 int idx,
1975 int argcount,
1976 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001977{
1978 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1979 int i;
1980
1981 if (argchecks != NULL)
1982 {
1983 argcontext_T context;
1984
1985 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001986 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001987 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001988 for (i = 0; i < argcount; ++i)
1989 if (argchecks[i] != NULL)
1990 {
1991 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001992 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001993 return FAIL;
1994 }
1995 }
1996 return OK;
1997}
1998
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001999/*
2000 * Call the "f_retfunc" function to obtain the return type of function "idx".
2001 * "argtypes" is the list of argument types or NULL when there are no
2002 * arguments.
2003 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002005internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002007 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008}
2009
2010/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002011 * Return TRUE if "idx" is for the map() function.
2012 */
2013 int
2014internal_func_is_map(int idx)
2015{
2016 return global_functions[idx].f_func == f_map;
2017}
2018
2019/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002021 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2022 * first argument, 2 if method base is second argument, etc. 9 if method base
2023 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 */
2025 int
2026check_internal_func(int idx, int argcount)
2027{
2028 int res;
2029 char *name;
2030
2031 if (argcount < global_functions[idx].f_min_argc)
2032 res = FCERR_TOOFEW;
2033 else if (argcount > global_functions[idx].f_max_argc)
2034 res = FCERR_TOOMANY;
2035 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002036 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037
2038 name = internal_func_name(idx);
2039 if (res == FCERR_TOOMANY)
2040 semsg(_(e_toomanyarg), name);
2041 else
2042 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002043 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044}
2045
Bram Moolenaarac92e252019-08-03 21:58:38 +02002046 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002047call_internal_func(
2048 char_u *name,
2049 int argcount,
2050 typval_T *argvars,
2051 typval_T *rettv)
2052{
2053 int i;
2054
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002055 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002056 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002057 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002058 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002059 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002060 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002061 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002062 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002063 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002064 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002065}
2066
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 void
2068call_internal_func_by_idx(
2069 int idx,
2070 typval_T *argvars,
2071 typval_T *rettv)
2072{
2073 global_functions[idx].f_func(argvars, rettv);
2074}
2075
Bram Moolenaarac92e252019-08-03 21:58:38 +02002076/*
2077 * Invoke a method for base->method().
2078 */
2079 int
2080call_internal_method(
2081 char_u *name,
2082 int argcount,
2083 typval_T *argvars,
2084 typval_T *rettv,
2085 typval_T *basetv)
2086{
2087 int i;
2088 int fi;
2089 typval_T argv[MAX_FUNC_ARGS + 1];
2090
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002091 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002092 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002093 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002094 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002095 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002096 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002097 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002098 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002099 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002100
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002101 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002102 {
2103 // base value goes last
2104 for (i = 0; i < argcount; ++i)
2105 argv[i] = argvars[i];
2106 argv[argcount] = *basetv;
2107 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002108 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002109 {
2110 // base value goes second
2111 argv[0] = argvars[0];
2112 argv[1] = *basetv;
2113 for (i = 1; i < argcount; ++i)
2114 argv[i + 1] = argvars[i];
2115 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002116 else if (global_functions[fi].f_argtype == FEARG_3)
2117 {
2118 // base value goes third
2119 argv[0] = argvars[0];
2120 argv[1] = argvars[1];
2121 argv[2] = *basetv;
2122 for (i = 2; i < argcount; ++i)
2123 argv[i + 1] = argvars[i];
2124 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002125 else if (global_functions[fi].f_argtype == FEARG_4)
2126 {
2127 // base value goes fourth
2128 argv[0] = argvars[0];
2129 argv[1] = argvars[1];
2130 argv[2] = argvars[2];
2131 argv[3] = *basetv;
2132 for (i = 3; i < argcount; ++i)
2133 argv[i + 1] = argvars[i];
2134 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002135 else
2136 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002137 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002138 argv[0] = *basetv;
2139 for (i = 0; i < argcount; ++i)
2140 argv[i + 1] = argvars[i];
2141 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002142 argv[argcount + 1].v_type = VAR_UNKNOWN;
2143
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002144 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002145 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002146}
2147
2148/*
2149 * Return TRUE for a non-zero Number and a non-empty String.
2150 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002151 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002152non_zero_arg(typval_T *argvars)
2153{
2154 return ((argvars[0].v_type == VAR_NUMBER
2155 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002156 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157 && argvars[0].vval.v_number == VVAL_TRUE)
2158 || (argvars[0].v_type == VAR_STRING
2159 && argvars[0].vval.v_string != NULL
2160 && *argvars[0].vval.v_string != NUL));
2161}
2162
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002163#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002164/*
2165 * Get the float value of "argvars[0]" into "f".
2166 * Returns FAIL when the argument is not a Number or Float.
2167 */
2168 static int
2169get_float_arg(typval_T *argvars, float_T *f)
2170{
2171 if (argvars[0].v_type == VAR_FLOAT)
2172 {
2173 *f = argvars[0].vval.v_float;
2174 return OK;
2175 }
2176 if (argvars[0].v_type == VAR_NUMBER)
2177 {
2178 *f = (float_T)argvars[0].vval.v_number;
2179 return OK;
2180 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002181 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002182 return FAIL;
2183}
2184
2185/*
2186 * "abs(expr)" function
2187 */
2188 static void
2189f_abs(typval_T *argvars, typval_T *rettv)
2190{
2191 if (argvars[0].v_type == VAR_FLOAT)
2192 {
2193 rettv->v_type = VAR_FLOAT;
2194 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
2195 }
2196 else
2197 {
2198 varnumber_T n;
2199 int error = FALSE;
2200
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002201 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002202 if (error)
2203 rettv->vval.v_number = -1;
2204 else if (n > 0)
2205 rettv->vval.v_number = n;
2206 else
2207 rettv->vval.v_number = -n;
2208 }
2209}
2210
2211/*
2212 * "acos()" function
2213 */
2214 static void
2215f_acos(typval_T *argvars, typval_T *rettv)
2216{
2217 float_T f = 0.0;
2218
2219 rettv->v_type = VAR_FLOAT;
2220 if (get_float_arg(argvars, &f) == OK)
2221 rettv->vval.v_float = acos(f);
2222 else
2223 rettv->vval.v_float = 0.0;
2224}
2225#endif
2226
2227/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002228 * "and(expr, expr)" function
2229 */
2230 static void
2231f_and(typval_T *argvars, typval_T *rettv)
2232{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002233 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2234 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002235}
2236
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002237#ifdef FEAT_FLOAT
2238/*
2239 * "asin()" function
2240 */
2241 static void
2242f_asin(typval_T *argvars, typval_T *rettv)
2243{
2244 float_T f = 0.0;
2245
2246 rettv->v_type = VAR_FLOAT;
2247 if (get_float_arg(argvars, &f) == OK)
2248 rettv->vval.v_float = asin(f);
2249 else
2250 rettv->vval.v_float = 0.0;
2251}
2252
2253/*
2254 * "atan()" function
2255 */
2256 static void
2257f_atan(typval_T *argvars, typval_T *rettv)
2258{
2259 float_T f = 0.0;
2260
2261 rettv->v_type = VAR_FLOAT;
2262 if (get_float_arg(argvars, &f) == OK)
2263 rettv->vval.v_float = atan(f);
2264 else
2265 rettv->vval.v_float = 0.0;
2266}
2267
2268/*
2269 * "atan2()" function
2270 */
2271 static void
2272f_atan2(typval_T *argvars, typval_T *rettv)
2273{
2274 float_T fx = 0.0, fy = 0.0;
2275
2276 rettv->v_type = VAR_FLOAT;
2277 if (get_float_arg(argvars, &fx) == OK
2278 && get_float_arg(&argvars[1], &fy) == OK)
2279 rettv->vval.v_float = atan2(fx, fy);
2280 else
2281 rettv->vval.v_float = 0.0;
2282}
2283#endif
2284
2285/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002286 * "balloon_show()" function
2287 */
2288#ifdef FEAT_BEVAL
2289 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002290f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2291{
2292 rettv->v_type = VAR_STRING;
2293 if (balloonEval != NULL)
2294 {
2295 if (balloonEval->msg == NULL)
2296 rettv->vval.v_string = NULL;
2297 else
2298 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2299 }
2300}
2301
2302 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002303f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2304{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002305 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002306 {
2307 if (argvars[0].v_type == VAR_LIST
2308# ifdef FEAT_GUI
2309 && !gui.in_use
2310# endif
2311 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002312 {
2313 list_T *l = argvars[0].vval.v_list;
2314
2315 // empty list removes the balloon
2316 post_balloon(balloonEval, NULL,
2317 l == NULL || l->lv_len == 0 ? NULL : l);
2318 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002319 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002320 {
2321 char_u *mesg = tv_get_string_chk(&argvars[0]);
2322
2323 if (mesg != NULL)
2324 // empty string removes the balloon
2325 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2326 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002327 }
2328}
2329
Bram Moolenaar669a8282017-11-19 20:13:05 +01002330# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002331 static void
2332f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2333{
2334 if (rettv_list_alloc(rettv) == OK)
2335 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002336 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002337
2338 if (msg != NULL)
2339 {
2340 pumitem_T *array;
2341 int size = split_message(msg, &array);
2342 int i;
2343
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002344 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002345 for (i = 1; i < size - 1; ++i)
2346 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002347 while (size > 0)
2348 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002349 vim_free(array);
2350 }
2351 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002352}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002353# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002354#endif
2355
2356/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002357 * Get the buffer from "arg" and give an error and return NULL if it is not
2358 * valid.
2359 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002360 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002361get_buf_arg(typval_T *arg)
2362{
2363 buf_T *buf;
2364
2365 ++emsg_off;
2366 buf = tv_get_buf(arg, FALSE);
2367 --emsg_off;
2368 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002369 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002370 return buf;
2371}
2372
2373/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002374 * "byte2line(byte)" function
2375 */
2376 static void
2377f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2378{
2379#ifndef FEAT_BYTEOFF
2380 rettv->vval.v_number = -1;
2381#else
2382 long boff = 0;
2383
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002384 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002385 if (boff < 0)
2386 rettv->vval.v_number = -1;
2387 else
2388 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2389 (linenr_T)0, &boff);
2390#endif
2391}
2392
2393 static void
2394byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2395{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002396 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002397 char_u *str;
2398 varnumber_T idx;
2399
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002400 str = tv_get_string_chk(&argvars[0]);
2401 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002402 rettv->vval.v_number = -1;
2403 if (str == NULL || idx < 0)
2404 return;
2405
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002406 t = str;
2407 for ( ; idx > 0; idx--)
2408 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002409 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002410 return;
2411 if (enc_utf8 && comp)
2412 t += utf_ptr2len(t);
2413 else
2414 t += (*mb_ptr2len)(t);
2415 }
2416 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002417}
2418
2419/*
2420 * "byteidx()" function
2421 */
2422 static void
2423f_byteidx(typval_T *argvars, typval_T *rettv)
2424{
2425 byteidx(argvars, rettv, FALSE);
2426}
2427
2428/*
2429 * "byteidxcomp()" function
2430 */
2431 static void
2432f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2433{
2434 byteidx(argvars, rettv, TRUE);
2435}
2436
2437/*
2438 * "call(func, arglist [, dict])" function
2439 */
2440 static void
2441f_call(typval_T *argvars, typval_T *rettv)
2442{
2443 char_u *func;
2444 partial_T *partial = NULL;
2445 dict_T *selfdict = NULL;
2446
2447 if (argvars[1].v_type != VAR_LIST)
2448 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002449 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002450 return;
2451 }
2452 if (argvars[1].vval.v_list == NULL)
2453 return;
2454
2455 if (argvars[0].v_type == VAR_FUNC)
2456 func = argvars[0].vval.v_string;
2457 else if (argvars[0].v_type == VAR_PARTIAL)
2458 {
2459 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002460 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002461 }
2462 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002463 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002464 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002465 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002466
2467 if (argvars[2].v_type != VAR_UNKNOWN)
2468 {
2469 if (argvars[2].v_type != VAR_DICT)
2470 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002471 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002472 return;
2473 }
2474 selfdict = argvars[2].vval.v_dict;
2475 }
2476
2477 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2478}
2479
2480#ifdef FEAT_FLOAT
2481/*
2482 * "ceil({float})" function
2483 */
2484 static void
2485f_ceil(typval_T *argvars, typval_T *rettv)
2486{
2487 float_T f = 0.0;
2488
2489 rettv->v_type = VAR_FLOAT;
2490 if (get_float_arg(argvars, &f) == OK)
2491 rettv->vval.v_float = ceil(f);
2492 else
2493 rettv->vval.v_float = 0.0;
2494}
2495#endif
2496
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002497/*
2498 * "changenr()" function
2499 */
2500 static void
2501f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2502{
2503 rettv->vval.v_number = curbuf->b_u_seq_cur;
2504}
2505
2506/*
2507 * "char2nr(string)" function
2508 */
2509 static void
2510f_char2nr(typval_T *argvars, typval_T *rettv)
2511{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002512 if (has_mbyte)
2513 {
2514 int utf8 = 0;
2515
2516 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002517 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002518
2519 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002520 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002521 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002522 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002523 }
2524 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002525 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002526}
2527
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002528/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002529 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2530 * returns the character index of the column. Otherwise, returns the byte index
2531 * of the column.
2532 */
2533 static void
2534get_col(typval_T *argvars, typval_T *rettv, int charcol)
2535{
2536 colnr_T col = 0;
2537 pos_T *fp;
2538 int fnum = curbuf->b_fnum;
2539
2540 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2541 if (fp != NULL && fnum == curbuf->b_fnum)
2542 {
2543 if (fp->col == MAXCOL)
2544 {
2545 // '> can be MAXCOL, get the length of the line then
2546 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2547 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2548 else
2549 col = MAXCOL;
2550 }
2551 else
2552 {
2553 col = fp->col + 1;
2554 // col(".") when the cursor is on the NUL at the end of the line
2555 // because of "coladd" can be seen as an extra column.
2556 if (virtual_active() && fp == &curwin->w_cursor)
2557 {
2558 char_u *p = ml_get_cursor();
2559
2560 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2561 curwin->w_virtcol - curwin->w_cursor.coladd))
2562 {
2563 int l;
2564
2565 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2566 col += l;
2567 }
2568 }
2569 }
2570 }
2571 rettv->vval.v_number = col;
2572}
2573
2574/*
2575 * "charcol()" function
2576 */
2577 static void
2578f_charcol(typval_T *argvars, typval_T *rettv)
2579{
2580 get_col(argvars, rettv, TRUE);
2581}
2582
2583/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002584 * "charidx()" function
2585 */
2586 static void
2587f_charidx(typval_T *argvars, typval_T *rettv)
2588{
2589 char_u *str;
2590 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002591 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002592 char_u *p;
2593 int len;
2594 int (*ptr2len)(char_u *);
2595
2596 rettv->vval.v_number = -1;
2597
2598 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2599 || (argvars[2].v_type != VAR_UNKNOWN
2600 && argvars[2].v_type != VAR_NUMBER))
2601 {
2602 emsg(_(e_invarg));
2603 return;
2604 }
2605
2606 str = tv_get_string_chk(&argvars[0]);
2607 idx = tv_get_number_chk(&argvars[1], NULL);
2608 if (str == NULL || idx < 0)
2609 return;
2610
2611 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002612 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002613 if (countcc < 0 || countcc > 1)
2614 {
2615 semsg(_(e_using_number_as_bool_nr), countcc);
2616 return;
2617 }
2618
2619 if (enc_utf8 && countcc)
2620 ptr2len = utf_ptr2len;
2621 else
2622 ptr2len = mb_ptr2len;
2623
2624 for (p = str, len = 0; p <= str + idx; len++)
2625 {
2626 if (*p == NUL)
2627 return;
2628 p += ptr2len(p);
2629 }
2630
2631 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2632}
2633
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002634 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002635get_optional_window(typval_T *argvars, int idx)
2636{
2637 win_T *win = curwin;
2638
2639 if (argvars[idx].v_type != VAR_UNKNOWN)
2640 {
2641 win = find_win_by_nr_or_id(&argvars[idx]);
2642 if (win == NULL)
2643 {
2644 emsg(_(e_invalwindow));
2645 return NULL;
2646 }
2647 }
2648 return win;
2649}
2650
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002651/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002652 * "col(string)" function
2653 */
2654 static void
2655f_col(typval_T *argvars, typval_T *rettv)
2656{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002657 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002658}
2659
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002660/*
2661 * "confirm(message, buttons[, default [, type]])" function
2662 */
2663 static void
2664f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2665{
2666#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2667 char_u *message;
2668 char_u *buttons = NULL;
2669 char_u buf[NUMBUFLEN];
2670 char_u buf2[NUMBUFLEN];
2671 int def = 1;
2672 int type = VIM_GENERIC;
2673 char_u *typestr;
2674 int error = FALSE;
2675
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002676 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002677 if (message == NULL)
2678 error = TRUE;
2679 if (argvars[1].v_type != VAR_UNKNOWN)
2680 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002681 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002682 if (buttons == NULL)
2683 error = TRUE;
2684 if (argvars[2].v_type != VAR_UNKNOWN)
2685 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002686 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002687 if (argvars[3].v_type != VAR_UNKNOWN)
2688 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002689 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002690 if (typestr == NULL)
2691 error = TRUE;
2692 else
2693 {
2694 switch (TOUPPER_ASC(*typestr))
2695 {
2696 case 'E': type = VIM_ERROR; break;
2697 case 'Q': type = VIM_QUESTION; break;
2698 case 'I': type = VIM_INFO; break;
2699 case 'W': type = VIM_WARNING; break;
2700 case 'G': type = VIM_GENERIC; break;
2701 }
2702 }
2703 }
2704 }
2705 }
2706
2707 if (buttons == NULL || *buttons == NUL)
2708 buttons = (char_u *)_("&Ok");
2709
2710 if (!error)
2711 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2712 def, NULL, FALSE);
2713#endif
2714}
2715
2716/*
2717 * "copy()" function
2718 */
2719 static void
2720f_copy(typval_T *argvars, typval_T *rettv)
2721{
2722 item_copy(&argvars[0], rettv, FALSE, 0);
2723}
2724
2725#ifdef FEAT_FLOAT
2726/*
2727 * "cos()" function
2728 */
2729 static void
2730f_cos(typval_T *argvars, typval_T *rettv)
2731{
2732 float_T f = 0.0;
2733
2734 rettv->v_type = VAR_FLOAT;
2735 if (get_float_arg(argvars, &f) == OK)
2736 rettv->vval.v_float = cos(f);
2737 else
2738 rettv->vval.v_float = 0.0;
2739}
2740
2741/*
2742 * "cosh()" function
2743 */
2744 static void
2745f_cosh(typval_T *argvars, typval_T *rettv)
2746{
2747 float_T f = 0.0;
2748
2749 rettv->v_type = VAR_FLOAT;
2750 if (get_float_arg(argvars, &f) == OK)
2751 rettv->vval.v_float = cosh(f);
2752 else
2753 rettv->vval.v_float = 0.0;
2754}
2755#endif
2756
2757/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002758 * Set the cursor position.
2759 * If 'charcol' is TRUE, then use the column number as a character offet.
2760 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002761 */
2762 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002763set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002764{
2765 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002766 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002767 int set_curswant = TRUE;
2768
2769 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002770 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002771 {
2772 pos_T pos;
2773 colnr_T curswant = -1;
2774
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002775 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002776 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002777 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002778 return;
2779 }
2780 line = pos.lnum;
2781 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002783 if (curswant >= 0)
2784 {
2785 curwin->w_curswant = curswant - 1;
2786 set_curswant = FALSE;
2787 }
2788 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002789 else if ((argvars[0].v_type == VAR_NUMBER ||
2790 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002791 && (argvars[1].v_type == VAR_NUMBER ||
2792 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002793 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002794 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002795 if (line < 0)
2796 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002797 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002798 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002799 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002800 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002801 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002802 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002803 else
2804 {
2805 emsg(_(e_invarg));
2806 return;
2807 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002808 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002809 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002810 if (line > 0)
2811 curwin->w_cursor.lnum = line;
2812 if (col > 0)
2813 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002814 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002815
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002816 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002817 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002818 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002819 if (has_mbyte)
2820 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002821
2822 curwin->w_set_curswant = set_curswant;
2823 rettv->vval.v_number = 0;
2824}
2825
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002826/*
2827 * "cursor(lnum, col)" function, or
2828 * "cursor(list)"
2829 *
2830 * Moves the cursor to the specified line and column.
2831 * Returns 0 when the position could be set, -1 otherwise.
2832 */
2833 static void
2834f_cursor(typval_T *argvars, typval_T *rettv)
2835{
2836 set_cursorpos(argvars, rettv, FALSE);
2837}
2838
Bram Moolenaar4f974752019-02-17 17:44:42 +01002839#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002840/*
2841 * "debugbreak()" function
2842 */
2843 static void
2844f_debugbreak(typval_T *argvars, typval_T *rettv)
2845{
2846 int pid;
2847
2848 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002849 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002850 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002851 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002852 else
2853 {
2854 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2855
2856 if (hProcess != NULL)
2857 {
2858 DebugBreakProcess(hProcess);
2859 CloseHandle(hProcess);
2860 rettv->vval.v_number = OK;
2861 }
2862 }
2863}
2864#endif
2865
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002866/*
2867 * "deepcopy()" function
2868 */
2869 static void
2870f_deepcopy(typval_T *argvars, typval_T *rettv)
2871{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002872 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002873 int copyID;
2874
2875 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002876 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002877 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002878 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002879 else
2880 {
2881 copyID = get_copyID();
2882 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2883 }
2884}
2885
2886/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002887 * "did_filetype()" function
2888 */
2889 static void
2890f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2891{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002892 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002893}
2894
2895/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002896 * "echoraw({expr})" function
2897 */
2898 static void
2899f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2900{
2901 char_u *str = tv_get_string_chk(&argvars[0]);
2902
2903 if (str != NULL && *str != NUL)
2904 {
2905 out_str(str);
2906 out_flush();
2907 }
2908}
2909
2910/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002911 * "empty({expr})" function
2912 */
2913 static void
2914f_empty(typval_T *argvars, typval_T *rettv)
2915{
2916 int n = FALSE;
2917
2918 switch (argvars[0].v_type)
2919 {
2920 case VAR_STRING:
2921 case VAR_FUNC:
2922 n = argvars[0].vval.v_string == NULL
2923 || *argvars[0].vval.v_string == NUL;
2924 break;
2925 case VAR_PARTIAL:
2926 n = FALSE;
2927 break;
2928 case VAR_NUMBER:
2929 n = argvars[0].vval.v_number == 0;
2930 break;
2931 case VAR_FLOAT:
2932#ifdef FEAT_FLOAT
2933 n = argvars[0].vval.v_float == 0.0;
2934 break;
2935#endif
2936 case VAR_LIST:
2937 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002938 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002939 break;
2940 case VAR_DICT:
2941 n = argvars[0].vval.v_dict == NULL
2942 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2943 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002944 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002945 case VAR_SPECIAL:
2946 n = argvars[0].vval.v_number != VVAL_TRUE;
2947 break;
2948
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002949 case VAR_BLOB:
2950 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002951 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2952 break;
2953
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002954 case VAR_JOB:
2955#ifdef FEAT_JOB_CHANNEL
2956 n = argvars[0].vval.v_job == NULL
2957 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2958 break;
2959#endif
2960 case VAR_CHANNEL:
2961#ifdef FEAT_JOB_CHANNEL
2962 n = argvars[0].vval.v_channel == NULL
2963 || !channel_is_open(argvars[0].vval.v_channel);
2964 break;
2965#endif
2966 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002967 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002969 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002970 n = TRUE;
2971 break;
2972 }
2973
2974 rettv->vval.v_number = n;
2975}
2976
2977/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002978 * "environ()" function
2979 */
2980 static void
2981f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2982{
2983#if !defined(AMIGA)
2984 int i = 0;
2985 char_u *entry, *value;
2986# ifdef MSWIN
2987 extern wchar_t **_wenviron;
2988# else
2989 extern char **environ;
2990# endif
2991
2992 if (rettv_dict_alloc(rettv) != OK)
2993 return;
2994
2995# ifdef MSWIN
2996 if (*_wenviron == NULL)
2997 return;
2998# else
2999 if (*environ == NULL)
3000 return;
3001# endif
3002
3003 for (i = 0; ; ++i)
3004 {
3005# ifdef MSWIN
3006 short_u *p;
3007
3008 if ((p = (short_u *)_wenviron[i]) == NULL)
3009 return;
3010 entry = utf16_to_enc(p, NULL);
3011# else
3012 if ((entry = (char_u *)environ[i]) == NULL)
3013 return;
3014 entry = vim_strsave(entry);
3015# endif
3016 if (entry == NULL) // out of memory
3017 return;
3018 if ((value = vim_strchr(entry, '=')) == NULL)
3019 {
3020 vim_free(entry);
3021 continue;
3022 }
3023 *value++ = NUL;
3024 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3025 vim_free(entry);
3026 }
3027#endif
3028}
3029
3030/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003031 * "escape({string}, {chars})" function
3032 */
3033 static void
3034f_escape(typval_T *argvars, typval_T *rettv)
3035{
3036 char_u buf[NUMBUFLEN];
3037
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003038 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3039 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003040 rettv->v_type = VAR_STRING;
3041}
3042
3043/*
3044 * "eval()" function
3045 */
3046 static void
3047f_eval(typval_T *argvars, typval_T *rettv)
3048{
3049 char_u *s, *p;
3050
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003051 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003052 if (s != NULL)
3053 s = skipwhite(s);
3054
3055 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003056 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003057 {
3058 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003059 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003060 need_clr_eos = FALSE;
3061 rettv->v_type = VAR_NUMBER;
3062 rettv->vval.v_number = 0;
3063 }
3064 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003065 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003066}
3067
3068/*
3069 * "eventhandler()" function
3070 */
3071 static void
3072f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3073{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003074 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003075}
3076
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003077static garray_T redir_execute_ga;
3078
3079/*
3080 * Append "value[value_len]" to the execute() output.
3081 */
3082 void
3083execute_redir_str(char_u *value, int value_len)
3084{
3085 int len;
3086
3087 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003088 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003089 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003090 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003091 if (ga_grow(&redir_execute_ga, len) == OK)
3092 {
3093 mch_memmove((char *)redir_execute_ga.ga_data
3094 + redir_execute_ga.ga_len, value, len);
3095 redir_execute_ga.ga_len += len;
3096 }
3097}
3098
3099/*
3100 * Get next line from a list.
3101 * Called by do_cmdline() to get the next line.
3102 * Returns allocated string, or NULL for end of function.
3103 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003104 static char_u *
3105get_list_line(
3106 int c UNUSED,
3107 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003108 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02003109 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003110{
3111 listitem_T **p = (listitem_T **)cookie;
3112 listitem_T *item = *p;
3113 char_u buf[NUMBUFLEN];
3114 char_u *s;
3115
3116 if (item == NULL)
3117 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003118 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003119 *p = item->li_next;
3120 return s == NULL ? NULL : vim_strsave(s);
3121}
3122
3123/*
3124 * "execute()" function
3125 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02003126 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003127execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003128{
3129 char_u *cmd = NULL;
3130 list_T *list = NULL;
3131 int save_msg_silent = msg_silent;
3132 int save_emsg_silent = emsg_silent;
3133 int save_emsg_noredir = emsg_noredir;
3134 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003135 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003136 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003137 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003138 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003139
3140 rettv->vval.v_string = NULL;
3141 rettv->v_type = VAR_STRING;
3142
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003143 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003144 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003145 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003146 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003147 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003148 return;
3149 ++list->lv_refcount;
3150 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003151 else if (argvars[arg_off].v_type == VAR_JOB
3152 || argvars[arg_off].v_type == VAR_CHANNEL)
3153 {
3154 emsg(_(e_inval_string));
3155 return;
3156 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003157 else
3158 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003159 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003160 if (cmd == NULL)
3161 return;
3162 }
3163
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003164 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003165 {
3166 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003167 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003168
3169 if (s == NULL)
3170 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003171 if (*s == NUL)
3172 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003173 if (STRNCMP(s, "silent", 6) == 0)
3174 ++msg_silent;
3175 if (STRCMP(s, "silent!") == 0)
3176 {
3177 emsg_silent = TRUE;
3178 emsg_noredir = TRUE;
3179 }
3180 }
3181 else
3182 ++msg_silent;
3183
3184 if (redir_execute)
3185 save_ga = redir_execute_ga;
3186 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3187 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003188 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003189 if (!echo_output)
3190 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003191
3192 if (cmd != NULL)
3193 do_cmdline_cmd(cmd);
3194 else
3195 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003196 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003197
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003198 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003199 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003200 do_cmdline(NULL, get_list_line, (void *)&item,
3201 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3202 --list->lv_refcount;
3203 }
3204
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003205 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003206 if (ga_grow(&redir_execute_ga, 1) == OK)
3207 {
3208 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3209 rettv->vval.v_string = redir_execute_ga.ga_data;
3210 }
3211 else
3212 {
3213 ga_clear(&redir_execute_ga);
3214 rettv->vval.v_string = NULL;
3215 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003216 msg_silent = save_msg_silent;
3217 emsg_silent = save_emsg_silent;
3218 emsg_noredir = save_emsg_noredir;
3219
3220 redir_execute = save_redir_execute;
3221 if (redir_execute)
3222 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003223 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003224
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003225 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003226 if (echo_output)
3227 // When not working silently: put it in column zero. A following
3228 // "echon" will overwrite the message, unavoidably.
3229 msg_col = 0;
3230 else
3231 // When working silently: Put it back where it was, since nothing
3232 // should have been written.
3233 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003234}
3235
3236/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003237 * "execute()" function
3238 */
3239 static void
3240f_execute(typval_T *argvars, typval_T *rettv)
3241{
3242 execute_common(argvars, rettv, 0);
3243}
3244
3245/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003246 * "exists()" function
3247 */
3248 static void
3249f_exists(typval_T *argvars, typval_T *rettv)
3250{
3251 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003252 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003253
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003254 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003255 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003256 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003257 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003258 if (mch_getenv(p + 1) != NULL)
3259 n = TRUE;
3260 else
3261 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003262 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003263 p = expand_env_save(p);
3264 if (p != NULL && *p != '$')
3265 n = TRUE;
3266 vim_free(p);
3267 }
3268 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003269 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003270 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003271 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003272 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003273 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003274 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003275 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003276 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003277 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003278 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003279 else if (*p == '?') // internal function only
3280 {
3281 n = has_internal_func_name(p + 1);
3282 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003283 else if (*p == ':')
3284 {
3285 n = cmd_exists(p + 1);
3286 }
3287 else if (*p == '#')
3288 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003289 if (p[1] == '#')
3290 n = autocmd_supported(p + 2);
3291 else
3292 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003293 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003294 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003295 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003296 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003297 }
3298
3299 rettv->vval.v_number = n;
3300}
3301
3302#ifdef FEAT_FLOAT
3303/*
3304 * "exp()" function
3305 */
3306 static void
3307f_exp(typval_T *argvars, typval_T *rettv)
3308{
3309 float_T f = 0.0;
3310
3311 rettv->v_type = VAR_FLOAT;
3312 if (get_float_arg(argvars, &f) == OK)
3313 rettv->vval.v_float = exp(f);
3314 else
3315 rettv->vval.v_float = 0.0;
3316}
3317#endif
3318
3319/*
3320 * "expand()" function
3321 */
3322 static void
3323f_expand(typval_T *argvars, typval_T *rettv)
3324{
3325 char_u *s;
3326 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003327 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003328 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3329 expand_T xpc;
3330 int error = FALSE;
3331 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003332#ifdef BACKSLASH_IN_FILENAME
3333 char_u *p_csl_save = p_csl;
3334
3335 // avoid using 'completeslash' here
3336 p_csl = empty_option;
3337#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003338
3339 rettv->v_type = VAR_STRING;
3340 if (argvars[1].v_type != VAR_UNKNOWN
3341 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003342 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003343 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003344 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003345
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003346 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003347 if (*s == '%' || *s == '#' || *s == '<')
3348 {
3349 ++emsg_off;
3350 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3351 --emsg_off;
3352 if (rettv->v_type == VAR_LIST)
3353 {
3354 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3355 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003356 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003357 }
3358 else
3359 rettv->vval.v_string = result;
3360 }
3361 else
3362 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003363 // When the optional second argument is non-zero, don't remove matches
3364 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003365 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003366 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003367 options |= WILD_KEEP_ALL;
3368 if (!error)
3369 {
3370 ExpandInit(&xpc);
3371 xpc.xp_context = EXPAND_FILES;
3372 if (p_wic)
3373 options += WILD_ICASE;
3374 if (rettv->v_type == VAR_STRING)
3375 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3376 options, WILD_ALL);
3377 else if (rettv_list_alloc(rettv) != FAIL)
3378 {
3379 int i;
3380
3381 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3382 for (i = 0; i < xpc.xp_numfiles; i++)
3383 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3384 ExpandCleanup(&xpc);
3385 }
3386 }
3387 else
3388 rettv->vval.v_string = NULL;
3389 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003390#ifdef BACKSLASH_IN_FILENAME
3391 p_csl = p_csl_save;
3392#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003393}
3394
3395/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003396 * "expandcmd()" function
3397 * Expand all the special characters in a command string.
3398 */
3399 static void
3400f_expandcmd(typval_T *argvars, typval_T *rettv)
3401{
3402 exarg_T eap;
3403 char_u *cmdstr;
3404 char *errormsg = NULL;
3405
3406 rettv->v_type = VAR_STRING;
3407 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3408
3409 memset(&eap, 0, sizeof(eap));
3410 eap.cmd = cmdstr;
3411 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003412 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003413 eap.usefilter = FALSE;
3414 eap.nextcmd = NULL;
3415 eap.cmdidx = CMD_USER;
3416
3417 expand_filename(&eap, &cmdstr, &errormsg);
3418 if (errormsg != NULL && *errormsg != NUL)
3419 emsg(errormsg);
3420
3421 rettv->vval.v_string = cmdstr;
3422}
3423
3424/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003425 * "feedkeys()" function
3426 */
3427 static void
3428f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3429{
3430 int remap = TRUE;
3431 int insert = FALSE;
3432 char_u *keys, *flags;
3433 char_u nbuf[NUMBUFLEN];
3434 int typed = FALSE;
3435 int execute = FALSE;
3436 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003437 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003438 char_u *keys_esc;
3439
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003440 // This is not allowed in the sandbox. If the commands would still be
3441 // executed in the sandbox it would be OK, but it probably happens later,
3442 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003443 if (check_secure())
3444 return;
3445
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003446 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003447
3448 if (argvars[1].v_type != VAR_UNKNOWN)
3449 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003450 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003451 for ( ; *flags != NUL; ++flags)
3452 {
3453 switch (*flags)
3454 {
3455 case 'n': remap = FALSE; break;
3456 case 'm': remap = TRUE; break;
3457 case 't': typed = TRUE; break;
3458 case 'i': insert = TRUE; break;
3459 case 'x': execute = TRUE; break;
3460 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003461 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003462 }
3463 }
3464 }
3465
3466 if (*keys != NUL || execute)
3467 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003468 // Need to escape K_SPECIAL and CSI before putting the string in the
3469 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003470 keys_esc = vim_strsave_escape_csi(keys);
3471 if (keys_esc != NULL)
3472 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003473 if (lowlevel)
3474 {
3475#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003476 int idx;
3477 int len = (int)STRLEN(keys);
3478
3479 for (idx = 0; idx < len; ++idx)
3480 {
3481 // if a CTRL-C was typed, set got_int, similar to what
3482 // happens in fill_input_buf()
3483 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3484 got_int = TRUE;
3485 add_to_input_buf(keys + idx, 1);
3486 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003487#else
3488 emsg(_("E980: lowlevel input not supported"));
3489#endif
3490 }
3491 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003492 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003493 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003494 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003495 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003496#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003497 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003498#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003499 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003500 typebuf_was_filled = TRUE;
3501 }
3502 vim_free(keys_esc);
3503
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003504 if (execute)
3505 {
3506 int save_msg_scroll = msg_scroll;
3507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003508 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003509 msg_scroll = FALSE;
3510
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003511 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003512 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003513 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003514 ++in_feedkeys;
3515 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003516 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003517 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003518 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003519 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003520 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003521 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003522
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003523 msg_scroll |= save_msg_scroll;
3524 }
3525 }
3526 }
3527}
3528
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003529#ifdef FEAT_FLOAT
3530/*
3531 * "float2nr({float})" function
3532 */
3533 static void
3534f_float2nr(typval_T *argvars, typval_T *rettv)
3535{
3536 float_T f = 0.0;
3537
3538 if (get_float_arg(argvars, &f) == OK)
3539 {
Bram Moolenaar37184272020-05-23 19:30:05 +02003540 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003541 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02003542 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003543 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003544 else
3545 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003546 }
3547}
3548
3549/*
3550 * "floor({float})" function
3551 */
3552 static void
3553f_floor(typval_T *argvars, typval_T *rettv)
3554{
3555 float_T f = 0.0;
3556
3557 rettv->v_type = VAR_FLOAT;
3558 if (get_float_arg(argvars, &f) == OK)
3559 rettv->vval.v_float = floor(f);
3560 else
3561 rettv->vval.v_float = 0.0;
3562}
3563
3564/*
3565 * "fmod()" function
3566 */
3567 static void
3568f_fmod(typval_T *argvars, typval_T *rettv)
3569{
3570 float_T fx = 0.0, fy = 0.0;
3571
3572 rettv->v_type = VAR_FLOAT;
3573 if (get_float_arg(argvars, &fx) == OK
3574 && get_float_arg(&argvars[1], &fy) == OK)
3575 rettv->vval.v_float = fmod(fx, fy);
3576 else
3577 rettv->vval.v_float = 0.0;
3578}
3579#endif
3580
3581/*
3582 * "fnameescape({string})" function
3583 */
3584 static void
3585f_fnameescape(typval_T *argvars, typval_T *rettv)
3586{
3587 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003588 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003589 rettv->v_type = VAR_STRING;
3590}
3591
3592/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003593 * "foreground()" function
3594 */
3595 static void
3596f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3597{
3598#ifdef FEAT_GUI
3599 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003600 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003601 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003602 return;
3603 }
3604#endif
3605#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003606 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003607#endif
3608}
3609
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003610 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003611common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003612{
3613 char_u *s;
3614 char_u *name;
3615 int use_string = FALSE;
3616 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003617 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003618 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003619
3620 if (argvars[0].v_type == VAR_FUNC)
3621 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003622 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003623 s = argvars[0].vval.v_string;
3624 }
3625 else if (argvars[0].v_type == VAR_PARTIAL
3626 && argvars[0].vval.v_partial != NULL)
3627 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003628 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003629 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003630 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003631 }
3632 else
3633 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003634 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003635 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003636 use_string = TRUE;
3637 }
3638
Bram Moolenaar843b8842016-08-21 14:36:15 +02003639 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003640 {
3641 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003642 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003643 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3644 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003645 if (*name != NUL)
3646 s = NULL;
3647 }
3648
Bram Moolenaar843b8842016-08-21 14:36:15 +02003649 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3650 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003651 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003652 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003653 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003654 ? find_func(trans_name, is_global, NULL) == NULL
3655 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003656 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003657 else
3658 {
3659 int dict_idx = 0;
3660 int arg_idx = 0;
3661 list_T *list = NULL;
3662
3663 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3664 {
3665 char sid_buf[25];
3666 int off = *s == 's' ? 2 : 5;
3667
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003668 // Expand s: and <SID> into <SNR>nr_, so that the function can
3669 // also be called from another script. Using trans_function_name()
3670 // would also work, but some plugins depend on the name being
3671 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003672 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003673 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003674 if (name != NULL)
3675 {
3676 STRCPY(name, sid_buf);
3677 STRCAT(name, s + off);
3678 }
3679 }
3680 else
3681 name = vim_strsave(s);
3682
3683 if (argvars[1].v_type != VAR_UNKNOWN)
3684 {
3685 if (argvars[2].v_type != VAR_UNKNOWN)
3686 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003687 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003688 arg_idx = 1;
3689 dict_idx = 2;
3690 }
3691 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003692 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003693 dict_idx = 1;
3694 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003695 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003696 arg_idx = 1;
3697 if (dict_idx > 0)
3698 {
3699 if (argvars[dict_idx].v_type != VAR_DICT)
3700 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003701 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003702 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003703 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003704 }
3705 if (argvars[dict_idx].vval.v_dict == NULL)
3706 dict_idx = 0;
3707 }
3708 if (arg_idx > 0)
3709 {
3710 if (argvars[arg_idx].v_type != VAR_LIST)
3711 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003712 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003713 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003714 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003715 }
3716 list = argvars[arg_idx].vval.v_list;
3717 if (list == NULL || list->lv_len == 0)
3718 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003719 else if (list->lv_len > MAX_FUNC_ARGS)
3720 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003721 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003722 vim_free(name);
3723 goto theend;
3724 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003725 }
3726 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003727 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003728 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003729 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003730
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003731 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003732 if (pt == NULL)
3733 vim_free(name);
3734 else
3735 {
3736 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3737 {
3738 listitem_T *li;
3739 int i = 0;
3740 int arg_len = 0;
3741 int lv_len = 0;
3742
3743 if (arg_pt != NULL)
3744 arg_len = arg_pt->pt_argc;
3745 if (list != NULL)
3746 lv_len = list->lv_len;
3747 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003748 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003749 if (pt->pt_argv == NULL)
3750 {
3751 vim_free(pt);
3752 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003753 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003754 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003755 for (i = 0; i < arg_len; i++)
3756 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3757 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003758 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003759 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003760 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003761 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003762 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003763 }
3764
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003765 // For "function(dict.func, [], dict)" and "func" is a partial
3766 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003767 if (dict_idx > 0)
3768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003769 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003770 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3771 ++pt->pt_dict->dv_refcount;
3772 }
3773 else if (arg_pt != NULL)
3774 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003775 // If the dict was bound automatically the result is also
3776 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003777 pt->pt_dict = arg_pt->pt_dict;
3778 pt->pt_auto = arg_pt->pt_auto;
3779 if (pt->pt_dict != NULL)
3780 ++pt->pt_dict->dv_refcount;
3781 }
3782
3783 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003784 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3785 {
3786 pt->pt_func = arg_pt->pt_func;
3787 func_ptr_ref(pt->pt_func);
3788 vim_free(name);
3789 }
3790 else if (is_funcref)
3791 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003792 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003793 func_ptr_ref(pt->pt_func);
3794 vim_free(name);
3795 }
3796 else
3797 {
3798 pt->pt_name = name;
3799 func_ref(name);
3800 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003801 }
3802 rettv->v_type = VAR_PARTIAL;
3803 rettv->vval.v_partial = pt;
3804 }
3805 else
3806 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003807 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003808 rettv->v_type = VAR_FUNC;
3809 rettv->vval.v_string = name;
3810 func_ref(name);
3811 }
3812 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003813theend:
3814 vim_free(trans_name);
3815}
3816
3817/*
3818 * "funcref()" function
3819 */
3820 static void
3821f_funcref(typval_T *argvars, typval_T *rettv)
3822{
3823 common_function(argvars, rettv, TRUE);
3824}
3825
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003826 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003827ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003828{
3829 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3830 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003831 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003832}
3833
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003834/*
3835 * "function()" function
3836 */
3837 static void
3838f_function(typval_T *argvars, typval_T *rettv)
3839{
3840 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003841}
3842
3843/*
3844 * "garbagecollect()" function
3845 */
3846 static void
3847f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3848{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003849 // This is postponed until we are back at the toplevel, because we may be
3850 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003851 want_garbage_collect = TRUE;
3852
Bram Moolenaar2df47312020-09-05 17:30:44 +02003853 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003854 garbage_collect_at_exit = TRUE;
3855}
3856
3857/*
3858 * "get()" function
3859 */
3860 static void
3861f_get(typval_T *argvars, typval_T *rettv)
3862{
3863 listitem_T *li;
3864 list_T *l;
3865 dictitem_T *di;
3866 dict_T *d;
3867 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003868 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003869
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003870 if (argvars[0].v_type == VAR_BLOB)
3871 {
3872 int error = FALSE;
3873 int idx = tv_get_number_chk(&argvars[1], &error);
3874
3875 if (!error)
3876 {
3877 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003878 if (idx < 0)
3879 idx = blob_len(argvars[0].vval.v_blob) + idx;
3880 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3881 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003882 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003883 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003884 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003885 tv = rettv;
3886 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003887 }
3888 }
3889 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003890 {
3891 if ((l = argvars[0].vval.v_list) != NULL)
3892 {
3893 int error = FALSE;
3894
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003895 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003896 if (!error && li != NULL)
3897 tv = &li->li_tv;
3898 }
3899 }
3900 else if (argvars[0].v_type == VAR_DICT)
3901 {
3902 if ((d = argvars[0].vval.v_dict) != NULL)
3903 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003904 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003905 if (di != NULL)
3906 tv = &di->di_tv;
3907 }
3908 }
3909 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3910 {
3911 partial_T *pt;
3912 partial_T fref_pt;
3913
3914 if (argvars[0].v_type == VAR_PARTIAL)
3915 pt = argvars[0].vval.v_partial;
3916 else
3917 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003918 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003919 fref_pt.pt_name = argvars[0].vval.v_string;
3920 pt = &fref_pt;
3921 }
3922
3923 if (pt != NULL)
3924 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003925 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003926 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003927
3928 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3929 {
3930 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003931 n = partial_name(pt);
3932 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003933 rettv->vval.v_string = NULL;
3934 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003935 {
3936 rettv->vval.v_string = vim_strsave(n);
3937 if (rettv->v_type == VAR_FUNC)
3938 func_ref(rettv->vval.v_string);
3939 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003940 }
3941 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003942 {
3943 what_is_dict = TRUE;
3944 if (pt->pt_dict != NULL)
3945 rettv_dict_set(rettv, pt->pt_dict);
3946 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003947 else if (STRCMP(what, "args") == 0)
3948 {
3949 rettv->v_type = VAR_LIST;
3950 if (rettv_list_alloc(rettv) == OK)
3951 {
3952 int i;
3953
3954 for (i = 0; i < pt->pt_argc; ++i)
3955 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3956 }
3957 }
3958 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003959 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003960
3961 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3962 // third argument
3963 if (!what_is_dict)
3964 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003965 }
3966 }
3967 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003968 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003969
3970 if (tv == NULL)
3971 {
3972 if (argvars[2].v_type != VAR_UNKNOWN)
3973 copy_tv(&argvars[2], rettv);
3974 }
3975 else
3976 copy_tv(tv, rettv);
3977}
3978
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003979/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003980 * "getchangelist()" function
3981 */
3982 static void
3983f_getchangelist(typval_T *argvars, typval_T *rettv)
3984{
3985#ifdef FEAT_JUMPLIST
3986 buf_T *buf;
3987 int i;
3988 list_T *l;
3989 dict_T *d;
3990#endif
3991
3992 if (rettv_list_alloc(rettv) != OK)
3993 return;
3994
3995#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003996 if (argvars[0].v_type == VAR_UNKNOWN)
3997 buf = curbuf;
3998 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003999 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004000 if (buf == NULL)
4001 return;
4002
4003 l = list_alloc();
4004 if (l == NULL)
4005 return;
4006
4007 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4008 return;
4009 /*
4010 * The current window change list index tracks only the position in the
4011 * current buffer change list. For other buffers, use the change list
4012 * length as the current index.
4013 */
4014 list_append_number(rettv->vval.v_list,
4015 (varnumber_T)((buf == curwin->w_buffer)
4016 ? curwin->w_changelistidx : buf->b_changelistlen));
4017
4018 for (i = 0; i < buf->b_changelistlen; ++i)
4019 {
4020 if (buf->b_changelist[i].lnum == 0)
4021 continue;
4022 if ((d = dict_alloc()) == NULL)
4023 return;
4024 if (list_append_dict(l, d) == FAIL)
4025 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004026 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4027 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004028 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004029 }
4030#endif
4031}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004032
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004033 static void
4034getpos_both(
4035 typval_T *argvars,
4036 typval_T *rettv,
4037 int getcurpos,
4038 int charcol)
4039{
4040 pos_T *fp = NULL;
4041 pos_T pos;
4042 win_T *wp = curwin;
4043 list_T *l;
4044 int fnum = -1;
4045
4046 if (rettv_list_alloc(rettv) == OK)
4047 {
4048 l = rettv->vval.v_list;
4049 if (getcurpos)
4050 {
4051 if (argvars[0].v_type != VAR_UNKNOWN)
4052 {
4053 wp = find_win_by_nr_or_id(&argvars[0]);
4054 if (wp != NULL)
4055 fp = &wp->w_cursor;
4056 }
4057 else
4058 fp = &curwin->w_cursor;
4059 if (fp != NULL && charcol)
4060 {
4061 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01004062 pos.col =
4063 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004064 fp = &pos;
4065 }
4066 }
4067 else
4068 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
4069 if (fnum != -1)
4070 list_append_number(l, (varnumber_T)fnum);
4071 else
4072 list_append_number(l, (varnumber_T)0);
4073 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4074 : (varnumber_T)0);
4075 list_append_number(l, (fp != NULL)
4076 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4077 : (varnumber_T)0);
4078 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
4079 (varnumber_T)0);
4080 if (getcurpos)
4081 {
4082 int save_set_curswant = curwin->w_set_curswant;
4083 colnr_T save_curswant = curwin->w_curswant;
4084 colnr_T save_virtcol = curwin->w_virtcol;
4085
4086 if (wp == curwin)
4087 update_curswant();
4088 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
4089 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
4090
4091 // Do not change "curswant", as it is unexpected that a get
4092 // function has a side effect.
4093 if (wp == curwin && save_set_curswant)
4094 {
4095 curwin->w_set_curswant = save_set_curswant;
4096 curwin->w_curswant = save_curswant;
4097 curwin->w_virtcol = save_virtcol;
4098 curwin->w_valid &= ~VALID_VIRTCOL;
4099 }
4100 }
4101 }
4102 else
4103 rettv->vval.v_number = FALSE;
4104}
4105
4106/*
4107 * "getcharpos()" function
4108 */
4109 static void
4110f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
4111{
4112 getpos_both(argvars, rettv, FALSE, TRUE);
4113}
4114
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004115/*
4116 * "getcharsearch()" function
4117 */
4118 static void
4119f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4120{
4121 if (rettv_dict_alloc(rettv) != FAIL)
4122 {
4123 dict_T *dict = rettv->vval.v_dict;
4124
Bram Moolenaare0be1672018-07-08 16:50:37 +02004125 dict_add_string(dict, "char", last_csearch());
4126 dict_add_number(dict, "forward", last_csearch_forward());
4127 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004128 }
4129}
4130
4131/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004132 * "getenv()" function
4133 */
4134 static void
4135f_getenv(typval_T *argvars, typval_T *rettv)
4136{
4137 int mustfree = FALSE;
4138 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4139
4140 if (p == NULL)
4141 {
4142 rettv->v_type = VAR_SPECIAL;
4143 rettv->vval.v_number = VVAL_NULL;
4144 return;
4145 }
4146 if (!mustfree)
4147 p = vim_strsave(p);
4148 rettv->vval.v_string = p;
4149 rettv->v_type = VAR_STRING;
4150}
4151
4152/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004153 * "getfontname()" function
4154 */
4155 static void
4156f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4157{
4158 rettv->v_type = VAR_STRING;
4159 rettv->vval.v_string = NULL;
4160#ifdef FEAT_GUI
4161 if (gui.in_use)
4162 {
4163 GuiFont font;
4164 char_u *name = NULL;
4165
4166 if (argvars[0].v_type == VAR_UNKNOWN)
4167 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004168 // Get the "Normal" font. Either the name saved by
4169 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004170 font = gui.norm_font;
4171 name = hl_get_font_name();
4172 }
4173 else
4174 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004175 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004176 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004177 return;
4178 font = gui_mch_get_font(name, FALSE);
4179 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004180 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004181 }
4182 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4183 if (argvars[0].v_type != VAR_UNKNOWN)
4184 gui_mch_free_font(font);
4185 }
4186#endif
4187}
4188
4189/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004190 * "getjumplist()" function
4191 */
4192 static void
4193f_getjumplist(typval_T *argvars, typval_T *rettv)
4194{
4195#ifdef FEAT_JUMPLIST
4196 win_T *wp;
4197 int i;
4198 list_T *l;
4199 dict_T *d;
4200#endif
4201
4202 if (rettv_list_alloc(rettv) != OK)
4203 return;
4204
4205#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004206 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004207 if (wp == NULL)
4208 return;
4209
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004210 cleanup_jumplist(wp, TRUE);
4211
Bram Moolenaar4f505882018-02-10 21:06:32 +01004212 l = list_alloc();
4213 if (l == NULL)
4214 return;
4215
4216 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4217 return;
4218 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4219
4220 for (i = 0; i < wp->w_jumplistlen; ++i)
4221 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004222 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4223 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004224 if ((d = dict_alloc()) == NULL)
4225 return;
4226 if (list_append_dict(l, d) == FAIL)
4227 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004228 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4229 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004230 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004231 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004232 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004233 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004234 }
4235#endif
4236}
4237
4238/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004239 * "getpid()" function
4240 */
4241 static void
4242f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4243{
4244 rettv->vval.v_number = mch_get_pid();
4245}
4246
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004247/*
4248 * "getcurpos()" function
4249 */
4250 static void
4251f_getcurpos(typval_T *argvars, typval_T *rettv)
4252{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004253 getpos_both(argvars, rettv, TRUE, FALSE);
4254}
4255
4256 static void
4257f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4258{
4259 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004260}
4261
4262/*
4263 * "getpos(string)" function
4264 */
4265 static void
4266f_getpos(typval_T *argvars, typval_T *rettv)
4267{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004268 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004269}
4270
4271/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004272 * "getreg()" function
4273 */
4274 static void
4275f_getreg(typval_T *argvars, typval_T *rettv)
4276{
4277 char_u *strregname;
4278 int regname;
4279 int arg2 = FALSE;
4280 int return_list = FALSE;
4281 int error = FALSE;
4282
4283 if (argvars[0].v_type != VAR_UNKNOWN)
4284 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004285 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004286 error = strregname == NULL;
4287 if (argvars[1].v_type != VAR_UNKNOWN)
4288 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004289 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004290 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004291 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004292 }
4293 }
4294 else
4295 strregname = get_vim_var_str(VV_REG);
4296
4297 if (error)
4298 return;
4299
4300 regname = (strregname == NULL ? '"' : *strregname);
4301 if (regname == 0)
4302 regname = '"';
4303
4304 if (return_list)
4305 {
4306 rettv->v_type = VAR_LIST;
4307 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4308 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4309 if (rettv->vval.v_list == NULL)
4310 (void)rettv_list_alloc(rettv);
4311 else
4312 ++rettv->vval.v_list->lv_refcount;
4313 }
4314 else
4315 {
4316 rettv->v_type = VAR_STRING;
4317 rettv->vval.v_string = get_reg_contents(regname,
4318 arg2 ? GREG_EXPR_SRC : 0);
4319 }
4320}
4321
4322/*
4323 * "getregtype()" function
4324 */
4325 static void
4326f_getregtype(typval_T *argvars, typval_T *rettv)
4327{
4328 char_u *strregname;
4329 int regname;
4330 char_u buf[NUMBUFLEN + 2];
4331 long reglen = 0;
4332
4333 if (argvars[0].v_type != VAR_UNKNOWN)
4334 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004335 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004336 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004337 {
4338 rettv->v_type = VAR_STRING;
4339 rettv->vval.v_string = NULL;
4340 return;
4341 }
4342 }
4343 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004344 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004345 strregname = get_vim_var_str(VV_REG);
4346
4347 regname = (strregname == NULL ? '"' : *strregname);
4348 if (regname == 0)
4349 regname = '"';
4350
4351 buf[0] = NUL;
4352 buf[1] = NUL;
4353 switch (get_reg_type(regname, &reglen))
4354 {
4355 case MLINE: buf[0] = 'V'; break;
4356 case MCHAR: buf[0] = 'v'; break;
4357 case MBLOCK:
4358 buf[0] = Ctrl_V;
4359 sprintf((char *)buf + 1, "%ld", reglen + 1);
4360 break;
4361 }
4362 rettv->v_type = VAR_STRING;
4363 rettv->vval.v_string = vim_strsave(buf);
4364}
4365
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004366/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004367 * "gettagstack()" function
4368 */
4369 static void
4370f_gettagstack(typval_T *argvars, typval_T *rettv)
4371{
4372 win_T *wp = curwin; // default is current window
4373
4374 if (rettv_dict_alloc(rettv) != OK)
4375 return;
4376
4377 if (argvars[0].v_type != VAR_UNKNOWN)
4378 {
4379 wp = find_win_by_nr_or_id(&argvars[0]);
4380 if (wp == NULL)
4381 return;
4382 }
4383
4384 get_tagstack(wp, rettv->vval.v_dict);
4385}
4386
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004387/*
4388 * "gettext()" function
4389 */
4390 static void
4391f_gettext(typval_T *argvars, typval_T *rettv)
4392{
4393 if (argvars[0].v_type != VAR_STRING
4394 || argvars[0].vval.v_string == NULL
4395 || *argvars[0].vval.v_string == NUL)
4396 {
4397 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4398 }
4399 else
4400 {
4401 rettv->v_type = VAR_STRING;
4402 rettv->vval.v_string = vim_strsave(
4403 (char_u *)_(argvars[0].vval.v_string));
4404 }
4405}
4406
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004407// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004408#include "version.h"
4409
4410/*
4411 * "has()" function
4412 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004413 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004414f_has(typval_T *argvars, typval_T *rettv)
4415{
4416 int i;
4417 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004418 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004419 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004420 typedef struct {
4421 char *name;
4422 short present;
4423 } has_item_T;
4424 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004425 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004426 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004427#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004428 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004429#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004430 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004431#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004432 },
4433 {"arp",
4434#if defined(AMIGA) && defined(FEAT_ARP)
4435 1
4436#else
4437 0
4438#endif
4439 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004440 {"haiku",
4441#ifdef __HAIKU__
4442 1
4443#else
4444 0
4445#endif
4446 },
4447 {"bsd",
4448#if defined(BSD) && !defined(MACOS_X)
4449 1
4450#else
4451 0
4452#endif
4453 },
4454 {"hpux",
4455#ifdef hpux
4456 1
4457#else
4458 0
4459#endif
4460 },
4461 {"linux",
4462#ifdef __linux__
4463 1
4464#else
4465 0
4466#endif
4467 },
4468 {"mac", // Mac OS X (and, once, Mac OS Classic)
4469#ifdef MACOS_X
4470 1
4471#else
4472 0
4473#endif
4474 },
4475 {"osx", // Mac OS X
4476#ifdef MACOS_X
4477 1
4478#else
4479 0
4480#endif
4481 },
4482 {"macunix", // Mac OS X, with the darwin feature
4483#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4484 1
4485#else
4486 0
4487#endif
4488 },
4489 {"osxdarwin", // synonym for macunix
4490#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4491 1
4492#else
4493 0
4494#endif
4495 },
4496 {"qnx",
4497#ifdef __QNX__
4498 1
4499#else
4500 0
4501#endif
4502 },
4503 {"sun",
4504#ifdef SUN_SYSTEM
4505 1
4506#else
4507 0
4508#endif
4509 },
4510 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004511#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004512 1
4513#else
4514 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004515#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004516 },
4517 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004518#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004519 1
4520#else
4521 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004522#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004523 },
4524 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004525#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004526 1
4527#else
4528 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004529#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004530 },
4531 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004532#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004533 1
4534#else
4535 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004536#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004537 },
4538 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004539#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004540 1
4541#else
4542 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004543#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004544 },
4545 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004546#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004547 1
4548#else
4549 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004550#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004551 },
4552 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004553#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004554 1
4555#else
4556 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004557#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004558 },
4559 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004560#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004561 1
4562#else
4563 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004564#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004565 },
4566 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004567#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004568 1
4569#else
4570 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004571#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004572 },
4573 {"autocmd", 1},
4574 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004575#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004576 1
4577#else
4578 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004579#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004580 },
4581 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004582#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004583 1
4584#else
4585 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004586#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004587 },
4588 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004589#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004590 1
4591#else
4592 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004593#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004594 },
4595 {"balloon_multiline",
4596#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4597 // MS-Windows requires runtime check, see below
4598 1
4599#else
4600 0
4601#endif
4602 },
4603 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004604#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004605 1
4606#else
4607 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004608#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004609 },
4610 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004611#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004612 1
4613#else
4614 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004615#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004616 },
4617 {"all_builtin_terms",
4618#if defined(ALL_BUILTIN_TCAPS)
4619 1
4620#else
4621 0
4622#endif
4623 },
4624 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004625#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004626 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004627 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004628 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004629#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004630 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004631#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004632 },
4633 {"byte_offset",
4634#ifdef FEAT_BYTEOFF
4635 1
4636#else
4637 0
4638#endif
4639 },
4640 {"channel",
4641#ifdef FEAT_JOB_CHANNEL
4642 1
4643#else
4644 0
4645#endif
4646 },
4647 {"cindent",
4648#ifdef FEAT_CINDENT
4649 1
4650#else
4651 0
4652#endif
4653 },
4654 {"clientserver",
4655#ifdef FEAT_CLIENTSERVER
4656 1
4657#else
4658 0
4659#endif
4660 },
4661 {"clipboard",
4662#ifdef FEAT_CLIPBOARD
4663 1
4664#else
4665 0
4666#endif
4667 },
4668 {"cmdline_compl", 1},
4669 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004670 {"cmdwin",
4671#ifdef FEAT_CMDWIN
4672 1
4673#else
4674 0
4675#endif
4676 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004677 {"comments", 1},
4678 {"conceal",
4679#ifdef FEAT_CONCEAL
4680 1
4681#else
4682 0
4683#endif
4684 },
4685 {"cryptv",
4686#ifdef FEAT_CRYPT
4687 1
4688#else
4689 0
4690#endif
4691 },
4692 {"crypt-blowfish",
4693#ifdef FEAT_CRYPT
4694 1
4695#else
4696 0
4697#endif
4698 },
4699 {"crypt-blowfish2",
4700#ifdef FEAT_CRYPT
4701 1
4702#else
4703 0
4704#endif
4705 },
4706 {"cscope",
4707#ifdef FEAT_CSCOPE
4708 1
4709#else
4710 0
4711#endif
4712 },
4713 {"cursorbind", 1},
4714 {"cursorshape",
4715#ifdef CURSOR_SHAPE
4716 1
4717#else
4718 0
4719#endif
4720 },
4721 {"debug",
4722#ifdef DEBUG
4723 1
4724#else
4725 0
4726#endif
4727 },
4728 {"dialog_con",
4729#ifdef FEAT_CON_DIALOG
4730 1
4731#else
4732 0
4733#endif
4734 },
4735 {"dialog_gui",
4736#ifdef FEAT_GUI_DIALOG
4737 1
4738#else
4739 0
4740#endif
4741 },
4742 {"diff",
4743#ifdef FEAT_DIFF
4744 1
4745#else
4746 0
4747#endif
4748 },
4749 {"digraphs",
4750#ifdef FEAT_DIGRAPHS
4751 1
4752#else
4753 0
4754#endif
4755 },
4756 {"directx",
4757#ifdef FEAT_DIRECTX
4758 1
4759#else
4760 0
4761#endif
4762 },
4763 {"dnd",
4764#ifdef FEAT_DND
4765 1
4766#else
4767 0
4768#endif
4769 },
4770 {"emacs_tags",
4771#ifdef FEAT_EMACS_TAGS
4772 1
4773#else
4774 0
4775#endif
4776 },
4777 {"eval", 1}, // always present, of course!
4778 {"ex_extra", 1}, // graduated feature
4779 {"extra_search",
4780#ifdef FEAT_SEARCH_EXTRA
4781 1
4782#else
4783 0
4784#endif
4785 },
4786 {"file_in_path",
4787#ifdef FEAT_SEARCHPATH
4788 1
4789#else
4790 0
4791#endif
4792 },
4793 {"filterpipe",
4794#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4795 1
4796#else
4797 0
4798#endif
4799 },
4800 {"find_in_path",
4801#ifdef FEAT_FIND_ID
4802 1
4803#else
4804 0
4805#endif
4806 },
4807 {"float",
4808#ifdef FEAT_FLOAT
4809 1
4810#else
4811 0
4812#endif
4813 },
4814 {"folding",
4815#ifdef FEAT_FOLDING
4816 1
4817#else
4818 0
4819#endif
4820 },
4821 {"footer",
4822#ifdef FEAT_FOOTER
4823 1
4824#else
4825 0
4826#endif
4827 },
4828 {"fork",
4829#if !defined(USE_SYSTEM) && defined(UNIX)
4830 1
4831#else
4832 0
4833#endif
4834 },
4835 {"gettext",
4836#ifdef FEAT_GETTEXT
4837 1
4838#else
4839 0
4840#endif
4841 },
4842 {"gui",
4843#ifdef FEAT_GUI
4844 1
4845#else
4846 0
4847#endif
4848 },
4849 {"gui_neXtaw",
4850#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4851 1
4852#else
4853 0
4854#endif
4855 },
4856 {"gui_athena",
4857#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4858 1
4859#else
4860 0
4861#endif
4862 },
4863 {"gui_gtk",
4864#ifdef FEAT_GUI_GTK
4865 1
4866#else
4867 0
4868#endif
4869 },
4870 {"gui_gtk2",
4871#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4872 1
4873#else
4874 0
4875#endif
4876 },
4877 {"gui_gtk3",
4878#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4879 1
4880#else
4881 0
4882#endif
4883 },
4884 {"gui_gnome",
4885#ifdef FEAT_GUI_GNOME
4886 1
4887#else
4888 0
4889#endif
4890 },
4891 {"gui_haiku",
4892#ifdef FEAT_GUI_HAIKU
4893 1
4894#else
4895 0
4896#endif
4897 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004898 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004899 {"gui_motif",
4900#ifdef FEAT_GUI_MOTIF
4901 1
4902#else
4903 0
4904#endif
4905 },
4906 {"gui_photon",
4907#ifdef FEAT_GUI_PHOTON
4908 1
4909#else
4910 0
4911#endif
4912 },
4913 {"gui_win32",
4914#ifdef FEAT_GUI_MSWIN
4915 1
4916#else
4917 0
4918#endif
4919 },
4920 {"iconv",
4921#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4922 1
4923#else
4924 0
4925#endif
4926 },
4927 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004928 {"ipv6",
4929#ifdef FEAT_IPV6
4930 1
4931#else
4932 0
4933#endif
4934 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004935 {"job",
4936#ifdef FEAT_JOB_CHANNEL
4937 1
4938#else
4939 0
4940#endif
4941 },
4942 {"jumplist",
4943#ifdef FEAT_JUMPLIST
4944 1
4945#else
4946 0
4947#endif
4948 },
4949 {"keymap",
4950#ifdef FEAT_KEYMAP
4951 1
4952#else
4953 0
4954#endif
4955 },
4956 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4957 {"langmap",
4958#ifdef FEAT_LANGMAP
4959 1
4960#else
4961 0
4962#endif
4963 },
4964 {"libcall",
4965#ifdef FEAT_LIBCALL
4966 1
4967#else
4968 0
4969#endif
4970 },
4971 {"linebreak",
4972#ifdef FEAT_LINEBREAK
4973 1
4974#else
4975 0
4976#endif
4977 },
4978 {"lispindent",
4979#ifdef FEAT_LISP
4980 1
4981#else
4982 0
4983#endif
4984 },
4985 {"listcmds", 1},
4986 {"localmap", 1},
4987 {"lua",
4988#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4989 1
4990#else
4991 0
4992#endif
4993 },
4994 {"menu",
4995#ifdef FEAT_MENU
4996 1
4997#else
4998 0
4999#endif
5000 },
5001 {"mksession",
5002#ifdef FEAT_SESSION
5003 1
5004#else
5005 0
5006#endif
5007 },
5008 {"modify_fname", 1},
5009 {"mouse", 1},
5010 {"mouseshape",
5011#ifdef FEAT_MOUSESHAPE
5012 1
5013#else
5014 0
5015#endif
5016 },
5017 {"mouse_dec",
5018#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
5019 1
5020#else
5021 0
5022#endif
5023 },
5024 {"mouse_gpm",
5025#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
5026 1
5027#else
5028 0
5029#endif
5030 },
5031 {"mouse_jsbterm",
5032#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
5033 1
5034#else
5035 0
5036#endif
5037 },
5038 {"mouse_netterm",
5039#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
5040 1
5041#else
5042 0
5043#endif
5044 },
5045 {"mouse_pterm",
5046#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
5047 1
5048#else
5049 0
5050#endif
5051 },
5052 {"mouse_sgr",
5053#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5054 1
5055#else
5056 0
5057#endif
5058 },
5059 {"mouse_sysmouse",
5060#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
5061 1
5062#else
5063 0
5064#endif
5065 },
5066 {"mouse_urxvt",
5067#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
5068 1
5069#else
5070 0
5071#endif
5072 },
5073 {"mouse_xterm",
5074#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5075 1
5076#else
5077 0
5078#endif
5079 },
5080 {"multi_byte", 1},
5081 {"multi_byte_ime",
5082#ifdef FEAT_MBYTE_IME
5083 1
5084#else
5085 0
5086#endif
5087 },
5088 {"multi_lang",
5089#ifdef FEAT_MULTI_LANG
5090 1
5091#else
5092 0
5093#endif
5094 },
5095 {"mzscheme",
5096#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
5097 1
5098#else
5099 0
5100#endif
5101 },
5102 {"num64", 1},
5103 {"ole",
5104#ifdef FEAT_OLE
5105 1
5106#else
5107 0
5108#endif
5109 },
5110 {"packages",
5111#ifdef FEAT_EVAL
5112 1
5113#else
5114 0
5115#endif
5116 },
5117 {"path_extra",
5118#ifdef FEAT_PATH_EXTRA
5119 1
5120#else
5121 0
5122#endif
5123 },
5124 {"perl",
5125#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
5126 1
5127#else
5128 0
5129#endif
5130 },
5131 {"persistent_undo",
5132#ifdef FEAT_PERSISTENT_UNDO
5133 1
5134#else
5135 0
5136#endif
5137 },
5138 {"python_compiled",
5139#if defined(FEAT_PYTHON)
5140 1
5141#else
5142 0
5143#endif
5144 },
5145 {"python_dynamic",
5146#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
5147 1
5148#else
5149 0
5150#endif
5151 },
5152 {"python",
5153#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
5154 1
5155#else
5156 0
5157#endif
5158 },
5159 {"pythonx",
5160#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
5161 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
5162 1
5163#else
5164 0
5165#endif
5166 },
5167 {"python3_compiled",
5168#if defined(FEAT_PYTHON3)
5169 1
5170#else
5171 0
5172#endif
5173 },
5174 {"python3_dynamic",
5175#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
5176 1
5177#else
5178 0
5179#endif
5180 },
5181 {"python3",
5182#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
5183 1
5184#else
5185 0
5186#endif
5187 },
5188 {"popupwin",
5189#ifdef FEAT_PROP_POPUP
5190 1
5191#else
5192 0
5193#endif
5194 },
5195 {"postscript",
5196#ifdef FEAT_POSTSCRIPT
5197 1
5198#else
5199 0
5200#endif
5201 },
5202 {"printer",
5203#ifdef FEAT_PRINTER
5204 1
5205#else
5206 0
5207#endif
5208 },
5209 {"profile",
5210#ifdef FEAT_PROFILE
5211 1
5212#else
5213 0
5214#endif
5215 },
5216 {"reltime",
5217#ifdef FEAT_RELTIME
5218 1
5219#else
5220 0
5221#endif
5222 },
5223 {"quickfix",
5224#ifdef FEAT_QUICKFIX
5225 1
5226#else
5227 0
5228#endif
5229 },
5230 {"rightleft",
5231#ifdef FEAT_RIGHTLEFT
5232 1
5233#else
5234 0
5235#endif
5236 },
5237 {"ruby",
5238#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5239 1
5240#else
5241 0
5242#endif
5243 },
5244 {"scrollbind", 1},
5245 {"showcmd",
5246#ifdef FEAT_CMDL_INFO
5247 1
5248#else
5249 0
5250#endif
5251 },
5252 {"cmdline_info",
5253#ifdef FEAT_CMDL_INFO
5254 1
5255#else
5256 0
5257#endif
5258 },
5259 {"signs",
5260#ifdef FEAT_SIGNS
5261 1
5262#else
5263 0
5264#endif
5265 },
5266 {"smartindent",
5267#ifdef FEAT_SMARTINDENT
5268 1
5269#else
5270 0
5271#endif
5272 },
5273 {"startuptime",
5274#ifdef STARTUPTIME
5275 1
5276#else
5277 0
5278#endif
5279 },
5280 {"statusline",
5281#ifdef FEAT_STL_OPT
5282 1
5283#else
5284 0
5285#endif
5286 },
5287 {"netbeans_intg",
5288#ifdef FEAT_NETBEANS_INTG
5289 1
5290#else
5291 0
5292#endif
5293 },
5294 {"sound",
5295#ifdef FEAT_SOUND
5296 1
5297#else
5298 0
5299#endif
5300 },
5301 {"spell",
5302#ifdef FEAT_SPELL
5303 1
5304#else
5305 0
5306#endif
5307 },
5308 {"syntax",
5309#ifdef FEAT_SYN_HL
5310 1
5311#else
5312 0
5313#endif
5314 },
5315 {"system",
5316#if defined(USE_SYSTEM) || !defined(UNIX)
5317 1
5318#else
5319 0
5320#endif
5321 },
5322 {"tag_binary",
5323#ifdef FEAT_TAG_BINS
5324 1
5325#else
5326 0
5327#endif
5328 },
5329 {"tcl",
5330#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5331 1
5332#else
5333 0
5334#endif
5335 },
5336 {"termguicolors",
5337#ifdef FEAT_TERMGUICOLORS
5338 1
5339#else
5340 0
5341#endif
5342 },
5343 {"terminal",
5344#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5345 1
5346#else
5347 0
5348#endif
5349 },
5350 {"terminfo",
5351#ifdef TERMINFO
5352 1
5353#else
5354 0
5355#endif
5356 },
5357 {"termresponse",
5358#ifdef FEAT_TERMRESPONSE
5359 1
5360#else
5361 0
5362#endif
5363 },
5364 {"textobjects",
5365#ifdef FEAT_TEXTOBJ
5366 1
5367#else
5368 0
5369#endif
5370 },
5371 {"textprop",
5372#ifdef FEAT_PROP_POPUP
5373 1
5374#else
5375 0
5376#endif
5377 },
5378 {"tgetent",
5379#ifdef HAVE_TGETENT
5380 1
5381#else
5382 0
5383#endif
5384 },
5385 {"timers",
5386#ifdef FEAT_TIMERS
5387 1
5388#else
5389 0
5390#endif
5391 },
5392 {"title",
5393#ifdef FEAT_TITLE
5394 1
5395#else
5396 0
5397#endif
5398 },
5399 {"toolbar",
5400#ifdef FEAT_TOOLBAR
5401 1
5402#else
5403 0
5404#endif
5405 },
5406 {"unnamedplus",
5407#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5408 1
5409#else
5410 0
5411#endif
5412 },
5413 {"user-commands", 1}, // was accidentally included in 5.4
5414 {"user_commands", 1},
5415 {"vartabs",
5416#ifdef FEAT_VARTABS
5417 1
5418#else
5419 0
5420#endif
5421 },
5422 {"vertsplit", 1},
5423 {"viminfo",
5424#ifdef FEAT_VIMINFO
5425 1
5426#else
5427 0
5428#endif
5429 },
5430 {"vimscript-1", 1},
5431 {"vimscript-2", 1},
5432 {"vimscript-3", 1},
5433 {"vimscript-4", 1},
5434 {"virtualedit", 1},
5435 {"visual", 1},
5436 {"visualextra", 1},
5437 {"vreplace", 1},
5438 {"vtp",
5439#ifdef FEAT_VTP
5440 1
5441#else
5442 0
5443#endif
5444 },
5445 {"wildignore",
5446#ifdef FEAT_WILDIGN
5447 1
5448#else
5449 0
5450#endif
5451 },
5452 {"wildmenu",
5453#ifdef FEAT_WILDMENU
5454 1
5455#else
5456 0
5457#endif
5458 },
5459 {"windows", 1},
5460 {"winaltkeys",
5461#ifdef FEAT_WAK
5462 1
5463#else
5464 0
5465#endif
5466 },
5467 {"writebackup",
5468#ifdef FEAT_WRITEBACKUP
5469 1
5470#else
5471 0
5472#endif
5473 },
5474 {"xim",
5475#ifdef FEAT_XIM
5476 1
5477#else
5478 0
5479#endif
5480 },
5481 {"xfontset",
5482#ifdef FEAT_XFONTSET
5483 1
5484#else
5485 0
5486#endif
5487 },
5488 {"xpm",
5489#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5490 1
5491#else
5492 0
5493#endif
5494 },
5495 {"xpm_w32", // for backward compatibility
5496#ifdef FEAT_XPM_W32
5497 1
5498#else
5499 0
5500#endif
5501 },
5502 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005503#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005504 1
5505#else
5506 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005507#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005508 },
5509 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005510#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005511 1
5512#else
5513 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005514#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005515 },
5516 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005517#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005518 1
5519#else
5520 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005521#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005522 },
5523 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005524#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005525 1
5526#else
5527 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005528#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005529 },
5530 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005531#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005532 1
5533#else
5534 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005535#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005536 },
5537 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005538 };
5539
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005540 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005541 for (i = 0; has_list[i].name != NULL; ++i)
5542 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005543 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005544 x = TRUE;
5545 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005546 break;
5547 }
5548
Bram Moolenaar79296512020-03-22 16:17:14 +01005549 // features also in has_list[] but sometimes enabled at runtime
5550 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005551 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005552 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005553 {
5554 // intentionally empty
5555 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005556#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005557 else if (STRICMP(name, "balloon_multiline") == 0)
5558 n = multiline_balloon_available();
5559#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005560#ifdef VIMDLL
5561 else if (STRICMP(name, "filterpipe") == 0)
5562 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005563#endif
5564#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5565 else if (STRICMP(name, "iconv") == 0)
5566 n = iconv_enabled(FALSE);
5567#endif
5568#ifdef DYNAMIC_LUA
5569 else if (STRICMP(name, "lua") == 0)
5570 n = lua_enabled(FALSE);
5571#endif
5572#ifdef DYNAMIC_MZSCHEME
5573 else if (STRICMP(name, "mzscheme") == 0)
5574 n = mzscheme_enabled(FALSE);
5575#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005576#ifdef DYNAMIC_PERL
5577 else if (STRICMP(name, "perl") == 0)
5578 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005579#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005580#ifdef DYNAMIC_PYTHON
5581 else if (STRICMP(name, "python") == 0)
5582 n = python_enabled(FALSE);
5583#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005584#ifdef DYNAMIC_PYTHON3
5585 else if (STRICMP(name, "python3") == 0)
5586 n = python3_enabled(FALSE);
5587#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005588#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5589 else if (STRICMP(name, "pythonx") == 0)
5590 {
5591# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5592 if (p_pyx == 0)
5593 n = python3_enabled(FALSE) || python_enabled(FALSE);
5594 else if (p_pyx == 3)
5595 n = python3_enabled(FALSE);
5596 else if (p_pyx == 2)
5597 n = python_enabled(FALSE);
5598# elif defined(DYNAMIC_PYTHON)
5599 n = python_enabled(FALSE);
5600# elif defined(DYNAMIC_PYTHON3)
5601 n = python3_enabled(FALSE);
5602# endif
5603 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005604#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005605#ifdef DYNAMIC_RUBY
5606 else if (STRICMP(name, "ruby") == 0)
5607 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005608#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005609#ifdef DYNAMIC_TCL
5610 else if (STRICMP(name, "tcl") == 0)
5611 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005612#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005613#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005614 else if (STRICMP(name, "terminal") == 0)
5615 n = terminal_enabled();
5616#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005617 }
5618
Bram Moolenaar79296512020-03-22 16:17:14 +01005619 // features not in has_list[]
5620 if (x == FALSE)
5621 {
5622 if (STRNICMP(name, "patch", 5) == 0)
5623 {
5624 x = TRUE;
5625 if (name[5] == '-'
5626 && STRLEN(name) >= 11
5627 && vim_isdigit(name[6])
5628 && vim_isdigit(name[8])
5629 && vim_isdigit(name[10]))
5630 {
5631 int major = atoi((char *)name + 6);
5632 int minor = atoi((char *)name + 8);
5633
5634 // Expect "patch-9.9.01234".
5635 n = (major < VIM_VERSION_MAJOR
5636 || (major == VIM_VERSION_MAJOR
5637 && (minor < VIM_VERSION_MINOR
5638 || (minor == VIM_VERSION_MINOR
5639 && has_patch(atoi((char *)name + 10))))));
5640 }
5641 else
5642 n = has_patch(atoi((char *)name + 5));
5643 }
5644 else if (STRICMP(name, "vim_starting") == 0)
5645 {
5646 x = TRUE;
5647 n = (starting != 0);
5648 }
5649 else if (STRICMP(name, "ttyin") == 0)
5650 {
5651 x = TRUE;
5652 n = mch_input_isatty();
5653 }
5654 else if (STRICMP(name, "ttyout") == 0)
5655 {
5656 x = TRUE;
5657 n = stdout_isatty;
5658 }
5659 else if (STRICMP(name, "multi_byte_encoding") == 0)
5660 {
5661 x = TRUE;
5662 n = has_mbyte;
5663 }
5664 else if (STRICMP(name, "gui_running") == 0)
5665 {
5666 x = TRUE;
5667#ifdef FEAT_GUI
5668 n = (gui.in_use || gui.starting);
5669#endif
5670 }
5671 else if (STRICMP(name, "browse") == 0)
5672 {
5673 x = TRUE;
5674#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5675 n = gui.in_use; // gui_mch_browse() works when GUI is running
5676#endif
5677 }
5678 else if (STRICMP(name, "syntax_items") == 0)
5679 {
5680 x = TRUE;
5681#ifdef FEAT_SYN_HL
5682 n = syntax_present(curwin);
5683#endif
5684 }
5685 else if (STRICMP(name, "vcon") == 0)
5686 {
5687 x = TRUE;
5688#ifdef FEAT_VTP
5689 n = is_term_win32() && has_vtp_working();
5690#endif
5691 }
5692 else if (STRICMP(name, "netbeans_enabled") == 0)
5693 {
5694 x = TRUE;
5695#ifdef FEAT_NETBEANS_INTG
5696 n = netbeans_active();
5697#endif
5698 }
5699 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5700 {
5701 x = TRUE;
5702#ifdef FEAT_MOUSE_GPM
5703 n = gpm_enabled();
5704#endif
5705 }
5706 else if (STRICMP(name, "conpty") == 0)
5707 {
5708 x = TRUE;
5709#if defined(FEAT_TERMINAL) && defined(MSWIN)
5710 n = use_conpty();
5711#endif
5712 }
5713 else if (STRICMP(name, "clipboard_working") == 0)
5714 {
5715 x = TRUE;
5716#ifdef FEAT_CLIPBOARD
5717 n = clip_star.available;
5718#endif
5719 }
5720 }
5721
Bram Moolenaar04637e22020-09-05 18:45:29 +02005722 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005723 // return whether feature could ever be enabled
5724 rettv->vval.v_number = x;
5725 else
5726 // return whether feature is enabled
5727 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005728}
5729
5730/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005731 * Return TRUE if "feature" can change later.
5732 * Also when checking for the feature has side effects, such as loading a DLL.
5733 */
5734 int
5735dynamic_feature(char_u *feature)
5736{
5737 return (feature == NULL
5738#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5739 || STRICMP(feature, "balloon_multiline") == 0
5740#endif
5741#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5742 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5743#endif
5744#ifdef VIMDLL
5745 || STRICMP(feature, "filterpipe") == 0
5746#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005747#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005748 // this can only change on Unix where the ":gui" command could be
5749 // used.
5750 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5751#endif
5752#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5753 || STRICMP(feature, "iconv") == 0
5754#endif
5755#ifdef DYNAMIC_LUA
5756 || STRICMP(feature, "lua") == 0
5757#endif
5758#ifdef FEAT_MOUSE_GPM
5759 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5760#endif
5761#ifdef DYNAMIC_MZSCHEME
5762 || STRICMP(feature, "mzscheme") == 0
5763#endif
5764#ifdef FEAT_NETBEANS_INTG
5765 || STRICMP(feature, "netbeans_enabled") == 0
5766#endif
5767#ifdef DYNAMIC_PERL
5768 || STRICMP(feature, "perl") == 0
5769#endif
5770#ifdef DYNAMIC_PYTHON
5771 || STRICMP(feature, "python") == 0
5772#endif
5773#ifdef DYNAMIC_PYTHON3
5774 || STRICMP(feature, "python3") == 0
5775#endif
5776#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5777 || STRICMP(feature, "pythonx") == 0
5778#endif
5779#ifdef DYNAMIC_RUBY
5780 || STRICMP(feature, "ruby") == 0
5781#endif
5782#ifdef FEAT_SYN_HL
5783 || STRICMP(feature, "syntax_items") == 0
5784#endif
5785#ifdef DYNAMIC_TCL
5786 || STRICMP(feature, "tcl") == 0
5787#endif
5788 // once "starting" is zero it will stay that way
5789 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5790 || STRICMP(feature, "multi_byte_encoding") == 0
5791#if defined(FEAT_TERMINAL) && defined(MSWIN)
5792 || STRICMP(feature, "conpty") == 0
5793#endif
5794 );
5795}
5796
5797/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005798 * "haslocaldir()" function
5799 */
5800 static void
5801f_haslocaldir(typval_T *argvars, typval_T *rettv)
5802{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005803 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005804 win_T *wp = NULL;
5805
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005806 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5807
5808 // Check for window-local and tab-local directories
5809 if (wp != NULL && wp->w_localdir != NULL)
5810 rettv->vval.v_number = 1;
5811 else if (tp != NULL && tp->tp_localdir != NULL)
5812 rettv->vval.v_number = 2;
5813 else
5814 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005815}
5816
5817/*
5818 * "hasmapto()" function
5819 */
5820 static void
5821f_hasmapto(typval_T *argvars, typval_T *rettv)
5822{
5823 char_u *name;
5824 char_u *mode;
5825 char_u buf[NUMBUFLEN];
5826 int abbr = FALSE;
5827
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005828 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005829 if (argvars[1].v_type == VAR_UNKNOWN)
5830 mode = (char_u *)"nvo";
5831 else
5832 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005833 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005834 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005835 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005836 }
5837
5838 if (map_to_exists(name, mode, abbr))
5839 rettv->vval.v_number = TRUE;
5840 else
5841 rettv->vval.v_number = FALSE;
5842}
5843
5844/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005845 * "highlightID(name)" function
5846 */
5847 static void
5848f_hlID(typval_T *argvars, typval_T *rettv)
5849{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005850 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005851}
5852
5853/*
5854 * "highlight_exists()" function
5855 */
5856 static void
5857f_hlexists(typval_T *argvars, typval_T *rettv)
5858{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005859 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005860}
5861
5862/*
5863 * "hostname()" function
5864 */
5865 static void
5866f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5867{
5868 char_u hostname[256];
5869
5870 mch_get_host_name(hostname, 256);
5871 rettv->v_type = VAR_STRING;
5872 rettv->vval.v_string = vim_strsave(hostname);
5873}
5874
5875/*
5876 * iconv() function
5877 */
5878 static void
5879f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5880{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005881 char_u buf1[NUMBUFLEN];
5882 char_u buf2[NUMBUFLEN];
5883 char_u *from, *to, *str;
5884 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005885
5886 rettv->v_type = VAR_STRING;
5887 rettv->vval.v_string = NULL;
5888
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005889 str = tv_get_string(&argvars[0]);
5890 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5891 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005892 vimconv.vc_type = CONV_NONE;
5893 convert_setup(&vimconv, from, to);
5894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005895 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005896 if (vimconv.vc_type == CONV_NONE)
5897 rettv->vval.v_string = vim_strsave(str);
5898 else
5899 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5900
5901 convert_setup(&vimconv, NULL, NULL);
5902 vim_free(from);
5903 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005904}
5905
5906/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005907 * "index()" function
5908 */
5909 static void
5910f_index(typval_T *argvars, typval_T *rettv)
5911{
5912 list_T *l;
5913 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005914 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005915 long idx = 0;
5916 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005917 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005918
5919 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005920 if (argvars[0].v_type == VAR_BLOB)
5921 {
5922 typval_T tv;
5923 int start = 0;
5924
5925 if (argvars[2].v_type != VAR_UNKNOWN)
5926 {
5927 start = tv_get_number_chk(&argvars[2], &error);
5928 if (error)
5929 return;
5930 }
5931 b = argvars[0].vval.v_blob;
5932 if (b == NULL)
5933 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005934 if (start < 0)
5935 {
5936 start = blob_len(b) + start;
5937 if (start < 0)
5938 start = 0;
5939 }
5940
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005941 for (idx = start; idx < blob_len(b); ++idx)
5942 {
5943 tv.v_type = VAR_NUMBER;
5944 tv.vval.v_number = blob_get(b, idx);
5945 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5946 {
5947 rettv->vval.v_number = idx;
5948 return;
5949 }
5950 }
5951 return;
5952 }
5953 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005954 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005955 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005956 return;
5957 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005958
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005959 l = argvars[0].vval.v_list;
5960 if (l != NULL)
5961 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005962 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005963 item = l->lv_first;
5964 if (argvars[2].v_type != VAR_UNKNOWN)
5965 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005966 // Start at specified item. Use the cached index that list_find()
5967 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005968 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005969 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005970 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005971 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005972 if (error)
5973 item = NULL;
5974 }
5975
5976 for ( ; item != NULL; item = item->li_next, ++idx)
5977 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5978 {
5979 rettv->vval.v_number = idx;
5980 break;
5981 }
5982 }
5983}
5984
5985static int inputsecret_flag = 0;
5986
5987/*
5988 * "input()" function
5989 * Also handles inputsecret() when inputsecret is set.
5990 */
5991 static void
5992f_input(typval_T *argvars, typval_T *rettv)
5993{
5994 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5995}
5996
5997/*
5998 * "inputdialog()" function
5999 */
6000 static void
6001f_inputdialog(typval_T *argvars, typval_T *rettv)
6002{
6003#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006004 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006005 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6006 {
6007 char_u *message;
6008 char_u buf[NUMBUFLEN];
6009 char_u *defstr = (char_u *)"";
6010
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006011 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006012 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006013 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006014 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6015 else
6016 IObuff[0] = NUL;
6017 if (message != NULL && defstr != NULL
6018 && do_dialog(VIM_QUESTION, NULL, message,
6019 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6020 rettv->vval.v_string = vim_strsave(IObuff);
6021 else
6022 {
6023 if (message != NULL && defstr != NULL
6024 && argvars[1].v_type != VAR_UNKNOWN
6025 && argvars[2].v_type != VAR_UNKNOWN)
6026 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006027 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006028 else
6029 rettv->vval.v_string = NULL;
6030 }
6031 rettv->v_type = VAR_STRING;
6032 }
6033 else
6034#endif
6035 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6036}
6037
6038/*
6039 * "inputlist()" function
6040 */
6041 static void
6042f_inputlist(typval_T *argvars, typval_T *rettv)
6043{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006044 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006045 listitem_T *li;
6046 int selected;
6047 int mouse_used;
6048
6049#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006050 // While starting up, there is no place to enter text. When running tests
6051 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006052 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006053 return;
6054#endif
6055 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6056 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006057 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006058 return;
6059 }
6060
6061 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006062 msg_row = Rows - 1; // for when 'cmdheight' > 1
6063 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006064 msg_scroll = TRUE;
6065 msg_clr_eos();
6066
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006067 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006068 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006069 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006070 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006071 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006072 msg_putchar('\n');
6073 }
6074
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006075 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006076 selected = prompt_for_number(&mouse_used);
6077 if (mouse_used)
6078 selected -= lines_left;
6079
6080 rettv->vval.v_number = selected;
6081}
6082
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006083static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6084
6085/*
6086 * "inputrestore()" function
6087 */
6088 static void
6089f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6090{
6091 if (ga_userinput.ga_len > 0)
6092 {
6093 --ga_userinput.ga_len;
6094 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6095 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006096 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006097 }
6098 else if (p_verbose > 1)
6099 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006100 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006101 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006102 }
6103}
6104
6105/*
6106 * "inputsave()" function
6107 */
6108 static void
6109f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6110{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006111 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006112 if (ga_grow(&ga_userinput, 1) == OK)
6113 {
6114 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6115 + ga_userinput.ga_len);
6116 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006117 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006118 }
6119 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006120 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006121}
6122
6123/*
6124 * "inputsecret()" function
6125 */
6126 static void
6127f_inputsecret(typval_T *argvars, typval_T *rettv)
6128{
6129 ++cmdline_star;
6130 ++inputsecret_flag;
6131 f_input(argvars, rettv);
6132 --cmdline_star;
6133 --inputsecret_flag;
6134}
6135
6136/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01006137 * "interrupt()" function
6138 */
6139 static void
6140f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6141{
6142 got_int = TRUE;
6143}
6144
6145/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006146 * "invert(expr)" function
6147 */
6148 static void
6149f_invert(typval_T *argvars, typval_T *rettv)
6150{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006151 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006152}
6153
6154/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006155 * "islocked()" function
6156 */
6157 static void
6158f_islocked(typval_T *argvars, typval_T *rettv)
6159{
6160 lval_T lv;
6161 char_u *end;
6162 dictitem_T *di;
6163
6164 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006165 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006166 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006167 if (end != NULL && lv.ll_name != NULL)
6168 {
6169 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02006170 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006171 else
6172 {
6173 if (lv.ll_tv == NULL)
6174 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006175 di = find_var(lv.ll_name, NULL, TRUE);
6176 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006177 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006178 // Consider a variable locked when:
6179 // 1. the variable itself is locked
6180 // 2. the value of the variable is locked.
6181 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01006182 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6183 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006184 }
6185 }
6186 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006187 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006188 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006189 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006190 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006191 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006192 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6193 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006194 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006195 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6196 }
6197 }
6198
6199 clear_lval(&lv);
6200}
6201
6202#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6203/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006204 * "isinf()" function
6205 */
6206 static void
6207f_isinf(typval_T *argvars, typval_T *rettv)
6208{
6209 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6210 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6211}
6212
6213/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006214 * "isnan()" function
6215 */
6216 static void
6217f_isnan(typval_T *argvars, typval_T *rettv)
6218{
6219 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6220 && isnan(argvars[0].vval.v_float);
6221}
6222#endif
6223
6224/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006225 * "last_buffer_nr()" function.
6226 */
6227 static void
6228f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6229{
6230 int n = 0;
6231 buf_T *buf;
6232
Bram Moolenaar29323592016-07-24 22:04:11 +02006233 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006234 if (n < buf->b_fnum)
6235 n = buf->b_fnum;
6236
6237 rettv->vval.v_number = n;
6238}
6239
6240/*
6241 * "len()" function
6242 */
6243 static void
6244f_len(typval_T *argvars, typval_T *rettv)
6245{
6246 switch (argvars[0].v_type)
6247 {
6248 case VAR_STRING:
6249 case VAR_NUMBER:
6250 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006251 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006252 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006253 case VAR_BLOB:
6254 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6255 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006256 case VAR_LIST:
6257 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6258 break;
6259 case VAR_DICT:
6260 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6261 break;
6262 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006263 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006264 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006265 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006266 case VAR_SPECIAL:
6267 case VAR_FLOAT:
6268 case VAR_FUNC:
6269 case VAR_PARTIAL:
6270 case VAR_JOB:
6271 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006272 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006273 break;
6274 }
6275}
6276
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006277 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006278libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006279{
6280#ifdef FEAT_LIBCALL
6281 char_u *string_in;
6282 char_u **string_result;
6283 int nr_result;
6284#endif
6285
6286 rettv->v_type = type;
6287 if (type != VAR_NUMBER)
6288 rettv->vval.v_string = NULL;
6289
6290 if (check_restricted() || check_secure())
6291 return;
6292
6293#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006294 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006295 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6296 {
6297 string_in = NULL;
6298 if (argvars[2].v_type == VAR_STRING)
6299 string_in = argvars[2].vval.v_string;
6300 if (type == VAR_NUMBER)
6301 string_result = NULL;
6302 else
6303 string_result = &rettv->vval.v_string;
6304 if (mch_libcall(argvars[0].vval.v_string,
6305 argvars[1].vval.v_string,
6306 string_in,
6307 argvars[2].vval.v_number,
6308 string_result,
6309 &nr_result) == OK
6310 && type == VAR_NUMBER)
6311 rettv->vval.v_number = nr_result;
6312 }
6313#endif
6314}
6315
6316/*
6317 * "libcall()" function
6318 */
6319 static void
6320f_libcall(typval_T *argvars, typval_T *rettv)
6321{
6322 libcall_common(argvars, rettv, VAR_STRING);
6323}
6324
6325/*
6326 * "libcallnr()" function
6327 */
6328 static void
6329f_libcallnr(typval_T *argvars, typval_T *rettv)
6330{
6331 libcall_common(argvars, rettv, VAR_NUMBER);
6332}
6333
6334/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006335 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006336 */
6337 static void
6338f_line(typval_T *argvars, typval_T *rettv)
6339{
6340 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006341 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006342 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006343 int id;
6344 tabpage_T *tp;
6345 win_T *wp;
6346 win_T *save_curwin;
6347 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006348
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006349 if (argvars[1].v_type != VAR_UNKNOWN)
6350 {
6351 // use window specified in the second argument
6352 id = (int)tv_get_number(&argvars[1]);
6353 wp = win_id2wp_tp(id, &tp);
6354 if (wp != NULL && tp != NULL)
6355 {
6356 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6357 == OK)
6358 {
6359 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006360 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006361 }
6362 restore_win_noblock(save_curwin, save_curtab, TRUE);
6363 }
6364 }
6365 else
6366 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006367 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006368
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006369 if (fp != NULL)
6370 lnum = fp->lnum;
6371 rettv->vval.v_number = lnum;
6372}
6373
6374/*
6375 * "line2byte(lnum)" function
6376 */
6377 static void
6378f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6379{
6380#ifndef FEAT_BYTEOFF
6381 rettv->vval.v_number = -1;
6382#else
6383 linenr_T lnum;
6384
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006385 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006386 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6387 rettv->vval.v_number = -1;
6388 else
6389 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6390 if (rettv->vval.v_number >= 0)
6391 ++rettv->vval.v_number;
6392#endif
6393}
6394
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006395#ifdef FEAT_FLOAT
6396/*
6397 * "log()" function
6398 */
6399 static void
6400f_log(typval_T *argvars, typval_T *rettv)
6401{
6402 float_T f = 0.0;
6403
6404 rettv->v_type = VAR_FLOAT;
6405 if (get_float_arg(argvars, &f) == OK)
6406 rettv->vval.v_float = log(f);
6407 else
6408 rettv->vval.v_float = 0.0;
6409}
6410
6411/*
6412 * "log10()" function
6413 */
6414 static void
6415f_log10(typval_T *argvars, typval_T *rettv)
6416{
6417 float_T f = 0.0;
6418
6419 rettv->v_type = VAR_FLOAT;
6420 if (get_float_arg(argvars, &f) == OK)
6421 rettv->vval.v_float = log10(f);
6422 else
6423 rettv->vval.v_float = 0.0;
6424}
6425#endif
6426
6427#ifdef FEAT_LUA
6428/*
6429 * "luaeval()" function
6430 */
6431 static void
6432f_luaeval(typval_T *argvars, typval_T *rettv)
6433{
6434 char_u *str;
6435 char_u buf[NUMBUFLEN];
6436
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006437 if (check_restricted() || check_secure())
6438 return;
6439
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006440 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006441 do_luaeval(str, argvars + 1, rettv);
6442}
6443#endif
6444
6445/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006446 * "maparg()" function
6447 */
6448 static void
6449f_maparg(typval_T *argvars, typval_T *rettv)
6450{
6451 get_maparg(argvars, rettv, TRUE);
6452}
6453
6454/*
6455 * "mapcheck()" function
6456 */
6457 static void
6458f_mapcheck(typval_T *argvars, typval_T *rettv)
6459{
6460 get_maparg(argvars, rettv, FALSE);
6461}
6462
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006463typedef enum
6464{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006465 MATCH_END, // matchend()
6466 MATCH_MATCH, // match()
6467 MATCH_STR, // matchstr()
6468 MATCH_LIST, // matchlist()
6469 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006470} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006471
6472 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006473find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006474{
6475 char_u *str = NULL;
6476 long len = 0;
6477 char_u *expr = NULL;
6478 char_u *pat;
6479 regmatch_T regmatch;
6480 char_u patbuf[NUMBUFLEN];
6481 char_u strbuf[NUMBUFLEN];
6482 char_u *save_cpo;
6483 long start = 0;
6484 long nth = 1;
6485 colnr_T startcol = 0;
6486 int match = 0;
6487 list_T *l = NULL;
6488 listitem_T *li = NULL;
6489 long idx = 0;
6490 char_u *tofree = NULL;
6491
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006492 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006493 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006494 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006495
6496 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006497 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006498 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006499 // type MATCH_LIST: return empty list when there are no matches.
6500 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006501 if (rettv_list_alloc(rettv) == FAIL)
6502 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006503 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006504 && (list_append_string(rettv->vval.v_list,
6505 (char_u *)"", 0) == 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 || list_append_number(rettv->vval.v_list,
6511 (varnumber_T)-1) == FAIL))
6512 {
6513 list_free(rettv->vval.v_list);
6514 rettv->vval.v_list = NULL;
6515 goto theend;
6516 }
6517 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006518 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006519 {
6520 rettv->v_type = VAR_STRING;
6521 rettv->vval.v_string = NULL;
6522 }
6523
6524 if (argvars[0].v_type == VAR_LIST)
6525 {
6526 if ((l = argvars[0].vval.v_list) == NULL)
6527 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006528 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006529 li = l->lv_first;
6530 }
6531 else
6532 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006533 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006534 len = (long)STRLEN(str);
6535 }
6536
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006537 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006538 if (pat == NULL)
6539 goto theend;
6540
6541 if (argvars[2].v_type != VAR_UNKNOWN)
6542 {
6543 int error = FALSE;
6544
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006545 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006546 if (error)
6547 goto theend;
6548 if (l != NULL)
6549 {
6550 li = list_find(l, start);
6551 if (li == NULL)
6552 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006553 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006554 }
6555 else
6556 {
6557 if (start < 0)
6558 start = 0;
6559 if (start > len)
6560 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006561 // When "count" argument is there ignore matches before "start",
6562 // otherwise skip part of the string. Differs when pattern is "^"
6563 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006564 if (argvars[3].v_type != VAR_UNKNOWN)
6565 startcol = start;
6566 else
6567 {
6568 str += start;
6569 len -= start;
6570 }
6571 }
6572
6573 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006574 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006575 if (error)
6576 goto theend;
6577 }
6578
6579 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6580 if (regmatch.regprog != NULL)
6581 {
6582 regmatch.rm_ic = p_ic;
6583
6584 for (;;)
6585 {
6586 if (l != NULL)
6587 {
6588 if (li == NULL)
6589 {
6590 match = FALSE;
6591 break;
6592 }
6593 vim_free(tofree);
6594 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6595 if (str == NULL)
6596 break;
6597 }
6598
6599 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6600
6601 if (match && --nth <= 0)
6602 break;
6603 if (l == NULL && !match)
6604 break;
6605
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006606 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006607 if (l != NULL)
6608 {
6609 li = li->li_next;
6610 ++idx;
6611 }
6612 else
6613 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006614 startcol = (colnr_T)(regmatch.startp[0]
6615 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006616 if (startcol > (colnr_T)len
6617 || str + startcol <= regmatch.startp[0])
6618 {
6619 match = FALSE;
6620 break;
6621 }
6622 }
6623 }
6624
6625 if (match)
6626 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006627 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006628 {
6629 listitem_T *li1 = rettv->vval.v_list->lv_first;
6630 listitem_T *li2 = li1->li_next;
6631 listitem_T *li3 = li2->li_next;
6632 listitem_T *li4 = li3->li_next;
6633
6634 vim_free(li1->li_tv.vval.v_string);
6635 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006636 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006637 li3->li_tv.vval.v_number =
6638 (varnumber_T)(regmatch.startp[0] - expr);
6639 li4->li_tv.vval.v_number =
6640 (varnumber_T)(regmatch.endp[0] - expr);
6641 if (l != NULL)
6642 li2->li_tv.vval.v_number = (varnumber_T)idx;
6643 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006644 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006645 {
6646 int i;
6647
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006648 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006649 for (i = 0; i < NSUBEXP; ++i)
6650 {
6651 if (regmatch.endp[i] == NULL)
6652 {
6653 if (list_append_string(rettv->vval.v_list,
6654 (char_u *)"", 0) == FAIL)
6655 break;
6656 }
6657 else if (list_append_string(rettv->vval.v_list,
6658 regmatch.startp[i],
6659 (int)(regmatch.endp[i] - regmatch.startp[i]))
6660 == FAIL)
6661 break;
6662 }
6663 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006664 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006665 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006666 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006667 if (l != NULL)
6668 copy_tv(&li->li_tv, rettv);
6669 else
6670 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006671 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006672 }
6673 else if (l != NULL)
6674 rettv->vval.v_number = idx;
6675 else
6676 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006677 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006678 rettv->vval.v_number =
6679 (varnumber_T)(regmatch.startp[0] - str);
6680 else
6681 rettv->vval.v_number =
6682 (varnumber_T)(regmatch.endp[0] - str);
6683 rettv->vval.v_number += (varnumber_T)(str - expr);
6684 }
6685 }
6686 vim_regfree(regmatch.regprog);
6687 }
6688
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006689theend:
6690 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006691 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006692 listitem_remove(rettv->vval.v_list,
6693 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694 vim_free(tofree);
6695 p_cpo = save_cpo;
6696}
6697
6698/*
6699 * "match()" function
6700 */
6701 static void
6702f_match(typval_T *argvars, typval_T *rettv)
6703{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006704 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006705}
6706
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006707/*
6708 * "matchend()" function
6709 */
6710 static void
6711f_matchend(typval_T *argvars, typval_T *rettv)
6712{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006713 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006714}
6715
6716/*
6717 * "matchlist()" function
6718 */
6719 static void
6720f_matchlist(typval_T *argvars, typval_T *rettv)
6721{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006722 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006723}
6724
6725/*
6726 * "matchstr()" function
6727 */
6728 static void
6729f_matchstr(typval_T *argvars, typval_T *rettv)
6730{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006731 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006732}
6733
6734/*
6735 * "matchstrpos()" function
6736 */
6737 static void
6738f_matchstrpos(typval_T *argvars, typval_T *rettv)
6739{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006740 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006741}
6742
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006743 static void
6744max_min(typval_T *argvars, typval_T *rettv, int domax)
6745{
6746 varnumber_T n = 0;
6747 varnumber_T i;
6748 int error = FALSE;
6749
6750 if (argvars[0].v_type == VAR_LIST)
6751 {
6752 list_T *l;
6753 listitem_T *li;
6754
6755 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006756 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006757 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006758 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006759 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006760 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6761 n = l->lv_u.nonmat.lv_start;
6762 else
6763 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6764 * l->lv_u.nonmat.lv_stride;
6765 }
6766 else
6767 {
6768 li = l->lv_first;
6769 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006770 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006771 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006772 if (error)
6773 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006774 for (;;)
6775 {
6776 li = li->li_next;
6777 if (li == NULL)
6778 break;
6779 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006780 if (error)
6781 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006782 if (domax ? i > n : i < n)
6783 n = i;
6784 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006785 }
6786 }
6787 }
6788 }
6789 else if (argvars[0].v_type == VAR_DICT)
6790 {
6791 dict_T *d;
6792 int first = TRUE;
6793 hashitem_T *hi;
6794 int todo;
6795
6796 d = argvars[0].vval.v_dict;
6797 if (d != NULL)
6798 {
6799 todo = (int)d->dv_hashtab.ht_used;
6800 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6801 {
6802 if (!HASHITEM_EMPTY(hi))
6803 {
6804 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006805 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006806 if (error)
6807 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006808 if (first)
6809 {
6810 n = i;
6811 first = FALSE;
6812 }
6813 else if (domax ? i > n : i < n)
6814 n = i;
6815 }
6816 }
6817 }
6818 }
6819 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006820 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006821
6822 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006823}
6824
6825/*
6826 * "max()" function
6827 */
6828 static void
6829f_max(typval_T *argvars, typval_T *rettv)
6830{
6831 max_min(argvars, rettv, TRUE);
6832}
6833
6834/*
6835 * "min()" function
6836 */
6837 static void
6838f_min(typval_T *argvars, typval_T *rettv)
6839{
6840 max_min(argvars, rettv, FALSE);
6841}
6842
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006843#if defined(FEAT_MZSCHEME) || defined(PROTO)
6844/*
6845 * "mzeval()" function
6846 */
6847 static void
6848f_mzeval(typval_T *argvars, typval_T *rettv)
6849{
6850 char_u *str;
6851 char_u buf[NUMBUFLEN];
6852
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006853 if (check_restricted() || check_secure())
6854 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006855 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006856 do_mzeval(str, rettv);
6857}
6858
6859 void
6860mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6861{
6862 typval_T argvars[3];
6863
6864 argvars[0].v_type = VAR_STRING;
6865 argvars[0].vval.v_string = name;
6866 copy_tv(args, &argvars[1]);
6867 argvars[2].v_type = VAR_UNKNOWN;
6868 f_call(argvars, rettv);
6869 clear_tv(&argvars[1]);
6870}
6871#endif
6872
6873/*
6874 * "nextnonblank()" function
6875 */
6876 static void
6877f_nextnonblank(typval_T *argvars, typval_T *rettv)
6878{
6879 linenr_T lnum;
6880
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006881 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006882 {
6883 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6884 {
6885 lnum = 0;
6886 break;
6887 }
6888 if (*skipwhite(ml_get(lnum)) != NUL)
6889 break;
6890 }
6891 rettv->vval.v_number = lnum;
6892}
6893
6894/*
6895 * "nr2char()" function
6896 */
6897 static void
6898f_nr2char(typval_T *argvars, typval_T *rettv)
6899{
6900 char_u buf[NUMBUFLEN];
6901
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006902 if (has_mbyte)
6903 {
6904 int utf8 = 0;
6905
6906 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006907 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006908 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006909 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006910 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006911 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006912 }
6913 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006914 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006915 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006916 buf[1] = NUL;
6917 }
6918 rettv->v_type = VAR_STRING;
6919 rettv->vval.v_string = vim_strsave(buf);
6920}
6921
6922/*
6923 * "or(expr, expr)" function
6924 */
6925 static void
6926f_or(typval_T *argvars, typval_T *rettv)
6927{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006928 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6929 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006930}
6931
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006932#ifdef FEAT_PERL
6933/*
6934 * "perleval()" function
6935 */
6936 static void
6937f_perleval(typval_T *argvars, typval_T *rettv)
6938{
6939 char_u *str;
6940 char_u buf[NUMBUFLEN];
6941
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006942 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006943 do_perleval(str, rettv);
6944}
6945#endif
6946
6947#ifdef FEAT_FLOAT
6948/*
6949 * "pow()" function
6950 */
6951 static void
6952f_pow(typval_T *argvars, typval_T *rettv)
6953{
6954 float_T fx = 0.0, fy = 0.0;
6955
6956 rettv->v_type = VAR_FLOAT;
6957 if (get_float_arg(argvars, &fx) == OK
6958 && get_float_arg(&argvars[1], &fy) == OK)
6959 rettv->vval.v_float = pow(fx, fy);
6960 else
6961 rettv->vval.v_float = 0.0;
6962}
6963#endif
6964
6965/*
6966 * "prevnonblank()" function
6967 */
6968 static void
6969f_prevnonblank(typval_T *argvars, typval_T *rettv)
6970{
6971 linenr_T lnum;
6972
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006973 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006974 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6975 lnum = 0;
6976 else
6977 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6978 --lnum;
6979 rettv->vval.v_number = lnum;
6980}
6981
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006982// This dummy va_list is here because:
6983// - passing a NULL pointer doesn't work when va_list isn't a pointer
6984// - locally in the function results in a "used before set" warning
6985// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006986static va_list ap;
6987
6988/*
6989 * "printf()" function
6990 */
6991 static void
6992f_printf(typval_T *argvars, typval_T *rettv)
6993{
6994 char_u buf[NUMBUFLEN];
6995 int len;
6996 char_u *s;
6997 int saved_did_emsg = did_emsg;
6998 char *fmt;
6999
7000 rettv->v_type = VAR_STRING;
7001 rettv->vval.v_string = NULL;
7002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007003 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007004 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007005 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007006 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007007 if (!did_emsg)
7008 {
7009 s = alloc(len + 1);
7010 if (s != NULL)
7011 {
7012 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007013 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
7014 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007015 }
7016 }
7017 did_emsg |= saved_did_emsg;
7018}
7019
7020/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007021 * "pum_getpos()" function
7022 */
7023 static void
7024f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7025{
7026 if (rettv_dict_alloc(rettv) != OK)
7027 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007028 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007029}
7030
7031/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007032 * "pumvisible()" function
7033 */
7034 static void
7035f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7036{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007037 if (pum_visible())
7038 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007039}
7040
7041#ifdef FEAT_PYTHON3
7042/*
7043 * "py3eval()" function
7044 */
7045 static void
7046f_py3eval(typval_T *argvars, typval_T *rettv)
7047{
7048 char_u *str;
7049 char_u buf[NUMBUFLEN];
7050
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007051 if (check_restricted() || check_secure())
7052 return;
7053
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007054 if (p_pyx == 0)
7055 p_pyx = 3;
7056
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007057 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007058 do_py3eval(str, rettv);
7059}
7060#endif
7061
7062#ifdef FEAT_PYTHON
7063/*
7064 * "pyeval()" function
7065 */
7066 static void
7067f_pyeval(typval_T *argvars, typval_T *rettv)
7068{
7069 char_u *str;
7070 char_u buf[NUMBUFLEN];
7071
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007072 if (check_restricted() || check_secure())
7073 return;
7074
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007075 if (p_pyx == 0)
7076 p_pyx = 2;
7077
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007078 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007079 do_pyeval(str, rettv);
7080}
7081#endif
7082
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007083#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
7084/*
7085 * "pyxeval()" function
7086 */
7087 static void
7088f_pyxeval(typval_T *argvars, typval_T *rettv)
7089{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007090 if (check_restricted() || check_secure())
7091 return;
7092
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007093# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
7094 init_pyxversion();
7095 if (p_pyx == 2)
7096 f_pyeval(argvars, rettv);
7097 else
7098 f_py3eval(argvars, rettv);
7099# elif defined(FEAT_PYTHON)
7100 f_pyeval(argvars, rettv);
7101# elif defined(FEAT_PYTHON3)
7102 f_py3eval(argvars, rettv);
7103# endif
7104}
7105#endif
7106
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007107static UINT32_T srand_seed_for_testing = 0;
7108static int srand_seed_for_testing_is_used = FALSE;
7109
7110 static void
7111f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
7112{
7113 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007114 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007115 else
7116 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007117 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
7118 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007119 }
7120}
7121
7122 static void
7123init_srand(UINT32_T *x)
7124{
7125#ifndef MSWIN
7126 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
7127#endif
7128
7129 if (srand_seed_for_testing_is_used)
7130 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007131 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007132 return;
7133 }
7134#ifndef MSWIN
7135 if (dev_urandom_state != FAIL)
7136 {
7137 int fd = open("/dev/urandom", O_RDONLY);
7138 struct {
7139 union {
7140 UINT32_T number;
7141 char bytes[sizeof(UINT32_T)];
7142 } contents;
7143 } buf;
7144
7145 // Attempt reading /dev/urandom.
7146 if (fd == -1)
7147 dev_urandom_state = FAIL;
7148 else
7149 {
7150 buf.contents.number = 0;
7151 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
7152 != sizeof(UINT32_T))
7153 dev_urandom_state = FAIL;
7154 else
7155 {
7156 dev_urandom_state = OK;
7157 *x = buf.contents.number;
7158 }
7159 close(fd);
7160 }
7161 }
7162 if (dev_urandom_state != OK)
7163 // Reading /dev/urandom doesn't work, fall back to time().
7164#endif
7165 *x = vim_time();
7166}
7167
7168#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
7169#define SPLITMIX32(x, z) ( \
7170 z = (x += 0x9e3779b9), \
7171 z = (z ^ (z >> 16)) * 0x85ebca6b, \
7172 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
7173 z ^ (z >> 16) \
7174 )
7175#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
7176 result = ROTL(y * 5, 7) * 9; \
7177 t = y << 9; \
7178 z ^= x; \
7179 w ^= y; \
7180 y ^= z, x ^= w; \
7181 z ^= t; \
7182 w = ROTL(w, 11);
7183
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007184/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007185 * "rand()" function
7186 */
7187 static void
7188f_rand(typval_T *argvars, typval_T *rettv)
7189{
7190 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007191 static UINT32_T gx, gy, gz, gw;
7192 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007193 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01007194 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007195
7196 if (argvars[0].v_type == VAR_UNKNOWN)
7197 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007198 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007199 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007200 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007201 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007202 init_srand(&x);
7203
7204 gx = SPLITMIX32(x, z);
7205 gy = SPLITMIX32(x, z);
7206 gz = SPLITMIX32(x, z);
7207 gw = SPLITMIX32(x, z);
7208 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007209 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007210
7211 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007212 }
7213 else if (argvars[0].v_type == VAR_LIST)
7214 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007215 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007216 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007217 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007218
7219 lx = list_find(l, 0L);
7220 ly = list_find(l, 1L);
7221 lz = list_find(l, 2L);
7222 lw = list_find(l, 3L);
7223 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
7224 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
7225 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
7226 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
7227 x = (UINT32_T)lx->li_tv.vval.v_number;
7228 y = (UINT32_T)ly->li_tv.vval.v_number;
7229 z = (UINT32_T)lz->li_tv.vval.v_number;
7230 w = (UINT32_T)lw->li_tv.vval.v_number;
7231
7232 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
7233
7234 lx->li_tv.vval.v_number = (varnumber_T)x;
7235 ly->li_tv.vval.v_number = (varnumber_T)y;
7236 lz->li_tv.vval.v_number = (varnumber_T)z;
7237 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007238 }
7239 else
7240 goto theend;
7241
7242 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007243 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007244 return;
7245
7246theend:
7247 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007248 rettv->v_type = VAR_NUMBER;
7249 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007250}
7251
7252/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007253 * "srand()" function
7254 */
7255 static void
7256f_srand(typval_T *argvars, typval_T *rettv)
7257{
7258 UINT32_T x = 0, z;
7259
7260 if (rettv_list_alloc(rettv) == FAIL)
7261 return;
7262 if (argvars[0].v_type == VAR_UNKNOWN)
7263 {
7264 init_srand(&x);
7265 }
7266 else
7267 {
7268 int error = FALSE;
7269
7270 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7271 if (error)
7272 return;
7273 }
7274
7275 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7276 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7277 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7278 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7279}
7280
7281#undef ROTL
7282#undef SPLITMIX32
7283#undef SHUFFLE_XOSHIRO128STARSTAR
7284
7285/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007286 * "range()" function
7287 */
7288 static void
7289f_range(typval_T *argvars, typval_T *rettv)
7290{
7291 varnumber_T start;
7292 varnumber_T end;
7293 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007294 int error = FALSE;
7295
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007296 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007297 if (argvars[1].v_type == VAR_UNKNOWN)
7298 {
7299 end = start - 1;
7300 start = 0;
7301 }
7302 else
7303 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007304 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007305 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007306 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007307 }
7308
7309 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007310 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007311 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007312 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007313 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007314 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007315 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007316 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007317 list_T *list = rettv->vval.v_list;
7318
7319 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007320 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321 // be called.
7322 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007323 list->lv_u.nonmat.lv_start = start;
7324 list->lv_u.nonmat.lv_end = end;
7325 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007326 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007327 }
7328}
7329
7330/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007331 * Materialize "list".
7332 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007333 */
7334 void
7335range_list_materialize(list_T *list)
7336{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007337 varnumber_T start = list->lv_u.nonmat.lv_start;
7338 varnumber_T end = list->lv_u.nonmat.lv_end;
7339 int stride = list->lv_u.nonmat.lv_stride;
7340 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007341
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007342 list->lv_first = NULL;
7343 list->lv_u.mat.lv_last = NULL;
7344 list->lv_len = 0;
7345 list->lv_u.mat.lv_idx_item = NULL;
7346 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7347 if (list_append_number(list, (varnumber_T)i) == FAIL)
7348 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007349}
7350
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007351/*
7352 * "getreginfo()" function
7353 */
7354 static void
7355f_getreginfo(typval_T *argvars, typval_T *rettv)
7356{
7357 char_u *strregname;
7358 int regname;
7359 char_u buf[NUMBUFLEN + 2];
7360 long reglen = 0;
7361 dict_T *dict;
7362 list_T *list;
7363
7364 if (argvars[0].v_type != VAR_UNKNOWN)
7365 {
7366 strregname = tv_get_string_chk(&argvars[0]);
7367 if (strregname == NULL)
7368 return;
7369 }
7370 else
7371 strregname = get_vim_var_str(VV_REG);
7372
7373 regname = (strregname == NULL ? '"' : *strregname);
7374 if (regname == 0 || regname == '@')
7375 regname = '"';
7376
7377 if (rettv_dict_alloc(rettv) == FAIL)
7378 return;
7379 dict = rettv->vval.v_dict;
7380
7381 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7382 if (list == NULL)
7383 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007384 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007385
7386 buf[0] = NUL;
7387 buf[1] = NUL;
7388 switch (get_reg_type(regname, &reglen))
7389 {
7390 case MLINE: buf[0] = 'V'; break;
7391 case MCHAR: buf[0] = 'v'; break;
7392 case MBLOCK:
7393 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7394 reglen + 1);
7395 break;
7396 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007397 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007398
7399 buf[0] = get_register_name(get_unname_register());
7400 buf[1] = NUL;
7401 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007402 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007403 else
7404 {
7405 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7406
7407 if (item != NULL)
7408 {
7409 item->di_tv.v_type = VAR_SPECIAL;
7410 item->di_tv.vval.v_number = regname == buf[0]
7411 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007412 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007413 }
7414 }
7415}
7416
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007417 static void
7418return_register(int regname, typval_T *rettv)
7419{
7420 char_u buf[2] = {0, 0};
7421
7422 buf[0] = (char_u)regname;
7423 rettv->v_type = VAR_STRING;
7424 rettv->vval.v_string = vim_strsave(buf);
7425}
7426
7427/*
7428 * "reg_executing()" function
7429 */
7430 static void
7431f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7432{
7433 return_register(reg_executing, rettv);
7434}
7435
7436/*
7437 * "reg_recording()" function
7438 */
7439 static void
7440f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7441{
7442 return_register(reg_recording, rettv);
7443}
7444
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007445/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007446 * "rename({from}, {to})" function
7447 */
7448 static void
7449f_rename(typval_T *argvars, typval_T *rettv)
7450{
7451 char_u buf[NUMBUFLEN];
7452
7453 if (check_restricted() || check_secure())
7454 rettv->vval.v_number = -1;
7455 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007456 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7457 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007458}
7459
7460/*
7461 * "repeat()" function
7462 */
7463 static void
7464f_repeat(typval_T *argvars, typval_T *rettv)
7465{
7466 char_u *p;
7467 int n;
7468 int slen;
7469 int len;
7470 char_u *r;
7471 int i;
7472
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007473 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007474 if (argvars[0].v_type == VAR_LIST)
7475 {
7476 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7477 while (n-- > 0)
7478 if (list_extend(rettv->vval.v_list,
7479 argvars[0].vval.v_list, NULL) == FAIL)
7480 break;
7481 }
7482 else
7483 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007484 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007485 rettv->v_type = VAR_STRING;
7486 rettv->vval.v_string = NULL;
7487
7488 slen = (int)STRLEN(p);
7489 len = slen * n;
7490 if (len <= 0)
7491 return;
7492
7493 r = alloc(len + 1);
7494 if (r != NULL)
7495 {
7496 for (i = 0; i < n; i++)
7497 mch_memmove(r + i * slen, p, (size_t)slen);
7498 r[len] = NUL;
7499 }
7500
7501 rettv->vval.v_string = r;
7502 }
7503}
7504
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007505#define SP_NOMOVE 0x01 // don't move cursor
7506#define SP_REPEAT 0x02 // repeat to find outer pair
7507#define SP_RETCOUNT 0x04 // return matchcount
7508#define SP_SETPCMARK 0x08 // set previous context mark
7509#define SP_START 0x10 // accept match at start position
7510#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7511#define SP_END 0x40 // leave cursor at end of match
7512#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007513
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007514/*
7515 * Get flags for a search function.
7516 * Possibly sets "p_ws".
7517 * Returns BACKWARD, FORWARD or zero (for an error).
7518 */
7519 static int
7520get_search_arg(typval_T *varp, int *flagsp)
7521{
7522 int dir = FORWARD;
7523 char_u *flags;
7524 char_u nbuf[NUMBUFLEN];
7525 int mask;
7526
7527 if (varp->v_type != VAR_UNKNOWN)
7528 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007529 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007530 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007531 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007532 while (*flags != NUL)
7533 {
7534 switch (*flags)
7535 {
7536 case 'b': dir = BACKWARD; break;
7537 case 'w': p_ws = TRUE; break;
7538 case 'W': p_ws = FALSE; break;
7539 default: mask = 0;
7540 if (flagsp != NULL)
7541 switch (*flags)
7542 {
7543 case 'c': mask = SP_START; break;
7544 case 'e': mask = SP_END; break;
7545 case 'm': mask = SP_RETCOUNT; break;
7546 case 'n': mask = SP_NOMOVE; break;
7547 case 'p': mask = SP_SUBPAT; break;
7548 case 'r': mask = SP_REPEAT; break;
7549 case 's': mask = SP_SETPCMARK; break;
7550 case 'z': mask = SP_COLUMN; break;
7551 }
7552 if (mask == 0)
7553 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007554 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007555 dir = 0;
7556 }
7557 else
7558 *flagsp |= mask;
7559 }
7560 if (dir == 0)
7561 break;
7562 ++flags;
7563 }
7564 }
7565 return dir;
7566}
7567
7568/*
7569 * Shared by search() and searchpos() functions.
7570 */
7571 static int
7572search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7573{
7574 int flags;
7575 char_u *pat;
7576 pos_T pos;
7577 pos_T save_cursor;
7578 int save_p_ws = p_ws;
7579 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007580 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007581 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007582#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007583 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007584 long time_limit = 0;
7585#endif
7586 int options = SEARCH_KEEP;
7587 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007588 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007589 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007590 pos_T firstpos;
7591
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007592 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007593 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007594 if (dir == 0)
7595 goto theend;
7596 flags = *flagsp;
7597 if (flags & SP_START)
7598 options |= SEARCH_START;
7599 if (flags & SP_END)
7600 options |= SEARCH_END;
7601 if (flags & SP_COLUMN)
7602 options |= SEARCH_COL;
7603
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007604 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007605 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7606 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007607 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007608 if (lnum_stop < 0)
7609 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007610 if (argvars[3].v_type != VAR_UNKNOWN)
7611 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007612#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007613 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007614 if (time_limit < 0)
7615 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007616#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007617 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007618 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007619 }
7620
7621#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007622 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007623 profile_setlimit(time_limit, &tm);
7624#endif
7625
7626 /*
7627 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7628 * Check to make sure only those flags are set.
7629 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7630 * flags cannot be set. Check for that condition also.
7631 */
7632 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7633 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7634 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007635 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007636 goto theend;
7637 }
7638
7639 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007640 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007641 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007642 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7643#ifdef FEAT_RELTIME
7644 sia.sa_tm = &tm;
7645#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007646
7647 // Repeat until {skip} returns FALSE.
7648 for (;;)
7649 {
7650 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007651 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007652 // finding the first match again means there is no match where {skip}
7653 // evaluates to zero.
7654 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7655 subpatnum = FAIL;
7656
Bram Moolenaara9c01042020-06-07 14:50:50 +02007657 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007658 // didn't find it or no skip argument
7659 break;
7660 firstpos = pos;
7661
Bram Moolenaara9c01042020-06-07 14:50:50 +02007662 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007663 {
7664 int do_skip;
7665 int err;
7666 pos_T save_pos = curwin->w_cursor;
7667
7668 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007669 err = FALSE;
7670 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007671 curwin->w_cursor = save_pos;
7672 if (err)
7673 {
7674 // Evaluating {skip} caused an error, break here.
7675 subpatnum = FAIL;
7676 break;
7677 }
7678 if (!do_skip)
7679 break;
7680 }
7681 }
7682
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007683 if (subpatnum != FAIL)
7684 {
7685 if (flags & SP_SUBPAT)
7686 retval = subpatnum;
7687 else
7688 retval = pos.lnum;
7689 if (flags & SP_SETPCMARK)
7690 setpcmark();
7691 curwin->w_cursor = pos;
7692 if (match_pos != NULL)
7693 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007694 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007695 match_pos->lnum = pos.lnum;
7696 match_pos->col = pos.col + 1;
7697 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007698 // "/$" will put the cursor after the end of the line, may need to
7699 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007700 check_cursor();
7701 }
7702
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007703 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007704 if (flags & SP_NOMOVE)
7705 curwin->w_cursor = save_cursor;
7706 else
7707 curwin->w_set_curswant = TRUE;
7708theend:
7709 p_ws = save_p_ws;
7710
7711 return retval;
7712}
7713
7714#ifdef FEAT_FLOAT
7715
7716/*
7717 * round() is not in C90, use ceil() or floor() instead.
7718 */
7719 float_T
7720vim_round(float_T f)
7721{
7722 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
7723}
7724
7725/*
7726 * "round({float})" function
7727 */
7728 static void
7729f_round(typval_T *argvars, typval_T *rettv)
7730{
7731 float_T f = 0.0;
7732
7733 rettv->v_type = VAR_FLOAT;
7734 if (get_float_arg(argvars, &f) == OK)
7735 rettv->vval.v_float = vim_round(f);
7736 else
7737 rettv->vval.v_float = 0.0;
7738}
7739#endif
7740
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007741#ifdef FEAT_RUBY
7742/*
7743 * "rubyeval()" function
7744 */
7745 static void
7746f_rubyeval(typval_T *argvars, typval_T *rettv)
7747{
7748 char_u *str;
7749 char_u buf[NUMBUFLEN];
7750
7751 str = tv_get_string_buf(&argvars[0], buf);
7752 do_rubyeval(str, rettv);
7753}
7754#endif
7755
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007756/*
7757 * "screenattr()" function
7758 */
7759 static void
7760f_screenattr(typval_T *argvars, typval_T *rettv)
7761{
7762 int row;
7763 int col;
7764 int c;
7765
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007766 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7767 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007768 if (row < 0 || row >= screen_Rows
7769 || col < 0 || col >= screen_Columns)
7770 c = -1;
7771 else
7772 c = ScreenAttrs[LineOffset[row] + col];
7773 rettv->vval.v_number = c;
7774}
7775
7776/*
7777 * "screenchar()" function
7778 */
7779 static void
7780f_screenchar(typval_T *argvars, typval_T *rettv)
7781{
7782 int row;
7783 int col;
7784 int off;
7785 int c;
7786
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007787 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7788 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007789 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007790 c = -1;
7791 else
7792 {
7793 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007794 if (enc_utf8 && ScreenLinesUC[off] != 0)
7795 c = ScreenLinesUC[off];
7796 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007797 c = ScreenLines[off];
7798 }
7799 rettv->vval.v_number = c;
7800}
7801
7802/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007803 * "screenchars()" function
7804 */
7805 static void
7806f_screenchars(typval_T *argvars, typval_T *rettv)
7807{
7808 int row;
7809 int col;
7810 int off;
7811 int c;
7812 int i;
7813
7814 if (rettv_list_alloc(rettv) == FAIL)
7815 return;
7816 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7817 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7818 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7819 return;
7820
7821 off = LineOffset[row] + col;
7822 if (enc_utf8 && ScreenLinesUC[off] != 0)
7823 c = ScreenLinesUC[off];
7824 else
7825 c = ScreenLines[off];
7826 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7827
7828 if (enc_utf8)
7829
7830 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7831 list_append_number(rettv->vval.v_list,
7832 (varnumber_T)ScreenLinesC[i][off]);
7833}
7834
7835/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007836 * "screencol()" function
7837 *
7838 * First column is 1 to be consistent with virtcol().
7839 */
7840 static void
7841f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7842{
7843 rettv->vval.v_number = screen_screencol() + 1;
7844}
7845
7846/*
7847 * "screenrow()" function
7848 */
7849 static void
7850f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7851{
7852 rettv->vval.v_number = screen_screenrow() + 1;
7853}
7854
7855/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007856 * "screenstring()" function
7857 */
7858 static void
7859f_screenstring(typval_T *argvars, typval_T *rettv)
7860{
7861 int row;
7862 int col;
7863 int off;
7864 int c;
7865 int i;
7866 char_u buf[MB_MAXBYTES + 1];
7867 int buflen = 0;
7868
7869 rettv->vval.v_string = NULL;
7870 rettv->v_type = VAR_STRING;
7871
7872 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7873 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7874 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7875 return;
7876
7877 off = LineOffset[row] + col;
7878 if (enc_utf8 && ScreenLinesUC[off] != 0)
7879 c = ScreenLinesUC[off];
7880 else
7881 c = ScreenLines[off];
7882 buflen += mb_char2bytes(c, buf);
7883
7884 if (enc_utf8)
7885 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7886 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7887
7888 buf[buflen] = NUL;
7889 rettv->vval.v_string = vim_strsave(buf);
7890}
7891
7892/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007893 * "search()" function
7894 */
7895 static void
7896f_search(typval_T *argvars, typval_T *rettv)
7897{
7898 int flags = 0;
7899
7900 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7901}
7902
7903/*
7904 * "searchdecl()" function
7905 */
7906 static void
7907f_searchdecl(typval_T *argvars, typval_T *rettv)
7908{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007909 int locally = TRUE;
7910 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007911 int error = FALSE;
7912 char_u *name;
7913
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007914 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007915
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007916 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007917 if (argvars[1].v_type != VAR_UNKNOWN)
7918 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007919 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007920 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007921 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007922 }
7923 if (!error && name != NULL)
7924 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7925 locally, thisblock, SEARCH_KEEP) == FAIL;
7926}
7927
7928/*
7929 * Used by searchpair() and searchpairpos()
7930 */
7931 static int
7932searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7933{
7934 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007935 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007936 int save_p_ws = p_ws;
7937 int dir;
7938 int flags = 0;
7939 char_u nbuf1[NUMBUFLEN];
7940 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007941 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007942 long lnum_stop = 0;
7943 long time_limit = 0;
7944
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007945 // Get the three pattern arguments: start, middle, end. Will result in an
7946 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007947 spat = tv_get_string_chk(&argvars[0]);
7948 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7949 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007950 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007951 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007953 // Handle the optional fourth argument: flags
7954 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007955 if (dir == 0)
7956 goto theend;
7957
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007958 // Don't accept SP_END or SP_SUBPAT.
7959 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007960 if ((flags & (SP_END | SP_SUBPAT)) != 0
7961 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7962 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007963 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007964 goto theend;
7965 }
7966
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007967 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007968 if (flags & SP_REPEAT)
7969 p_ws = FALSE;
7970
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007971 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007972 if (argvars[3].v_type == VAR_UNKNOWN
7973 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007974 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007975 else
7976 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007977 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007978 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007979
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007980 if (argvars[5].v_type != VAR_UNKNOWN)
7981 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007982 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007983 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007984 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007985 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
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#ifdef FEAT_RELTIME
7989 if (argvars[6].v_type != VAR_UNKNOWN)
7990 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007991 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007992 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007993 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007994 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007995 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007996 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007997 }
7998#endif
7999 }
8000 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008001
8002 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
8003 match_pos, lnum_stop, time_limit);
8004
8005theend:
8006 p_ws = save_p_ws;
8007
8008 return retval;
8009}
8010
8011/*
8012 * "searchpair()" function
8013 */
8014 static void
8015f_searchpair(typval_T *argvars, typval_T *rettv)
8016{
8017 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
8018}
8019
8020/*
8021 * "searchpairpos()" function
8022 */
8023 static void
8024f_searchpairpos(typval_T *argvars, typval_T *rettv)
8025{
8026 pos_T match_pos;
8027 int lnum = 0;
8028 int col = 0;
8029
8030 if (rettv_list_alloc(rettv) == FAIL)
8031 return;
8032
8033 if (searchpair_cmn(argvars, &match_pos) > 0)
8034 {
8035 lnum = match_pos.lnum;
8036 col = match_pos.col;
8037 }
8038
8039 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8040 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8041}
8042
8043/*
8044 * Search for a start/middle/end thing.
8045 * Used by searchpair(), see its documentation for the details.
8046 * Returns 0 or -1 for no match,
8047 */
8048 long
8049do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008050 char_u *spat, // start pattern
8051 char_u *mpat, // middle pattern
8052 char_u *epat, // end pattern
8053 int dir, // BACKWARD or FORWARD
8054 typval_T *skip, // skip expression
8055 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008056 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008057 linenr_T lnum_stop, // stop at this line if not zero
8058 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008059{
8060 char_u *save_cpo;
8061 char_u *pat, *pat2 = NULL, *pat3 = NULL;
8062 long retval = 0;
8063 pos_T pos;
8064 pos_T firstpos;
8065 pos_T foundpos;
8066 pos_T save_cursor;
8067 pos_T save_pos;
8068 int n;
8069 int r;
8070 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01008071 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008072 int err;
8073 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008074#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008075 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008076#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008077
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008078 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008079 save_cpo = p_cpo;
8080 p_cpo = empty_option;
8081
8082#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008083 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008084 profile_setlimit(time_limit, &tm);
8085#endif
8086
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008087 // Make two search patterns: start/end (pat2, for in nested pairs) and
8088 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02008089 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
8090 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008091 if (pat2 == NULL || pat3 == NULL)
8092 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008093 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008094 if (*mpat == NUL)
8095 STRCPY(pat3, pat2);
8096 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008097 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008098 spat, epat, mpat);
8099 if (flags & SP_START)
8100 options |= SEARCH_START;
8101
Bram Moolenaar48570482017-10-30 21:48:41 +01008102 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02008103 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01008104
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008105 save_cursor = curwin->w_cursor;
8106 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008107 CLEAR_POS(&firstpos);
8108 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008109 pat = pat3;
8110 for (;;)
8111 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008112 searchit_arg_T sia;
8113
Bram Moolenaara80faa82020-04-12 19:37:17 +02008114 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008115 sia.sa_stop_lnum = lnum_stop;
8116#ifdef FEAT_RELTIME
8117 sia.sa_tm = &tm;
8118#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01008119 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008120 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008121 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008122 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008123 break;
8124
8125 if (firstpos.lnum == 0)
8126 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008127 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008128 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008129 // Found the same position again. Can happen with a pattern that
8130 // has "\zs" at the end and searching backwards. Advance one
8131 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008132 if (dir == BACKWARD)
8133 decl(&pos);
8134 else
8135 incl(&pos);
8136 }
8137 foundpos = pos;
8138
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008139 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008140 options &= ~SEARCH_START;
8141
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008142 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01008143 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008144 {
8145 save_pos = curwin->w_cursor;
8146 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01008147 err = FALSE;
8148 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008149 curwin->w_cursor = save_pos;
8150 if (err)
8151 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008152 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008153 curwin->w_cursor = save_cursor;
8154 retval = -1;
8155 break;
8156 }
8157 if (r)
8158 continue;
8159 }
8160
8161 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8162 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008163 // Found end when searching backwards or start when searching
8164 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008166 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008167 }
8168 else
8169 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008170 // Found end when searching forward or start when searching
8171 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008172 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008173 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008174 }
8175
8176 if (nest == 0)
8177 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008178 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008179 if (flags & SP_RETCOUNT)
8180 ++retval;
8181 else
8182 retval = pos.lnum;
8183 if (flags & SP_SETPCMARK)
8184 setpcmark();
8185 curwin->w_cursor = pos;
8186 if (!(flags & SP_REPEAT))
8187 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008188 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008189 }
8190 }
8191
8192 if (match_pos != NULL)
8193 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008194 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008195 match_pos->lnum = curwin->w_cursor.lnum;
8196 match_pos->col = curwin->w_cursor.col + 1;
8197 }
8198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008199 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008200 if ((flags & SP_NOMOVE) || retval == 0)
8201 curwin->w_cursor = save_cursor;
8202
8203theend:
8204 vim_free(pat2);
8205 vim_free(pat3);
8206 if (p_cpo == empty_option)
8207 p_cpo = save_cpo;
8208 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008209 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008210 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008211 // If it's still empty it was changed and restored, need to restore in
8212 // the complicated way.
8213 if (*p_cpo == NUL)
8214 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008215 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008216 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008217
8218 return retval;
8219}
8220
8221/*
8222 * "searchpos()" function
8223 */
8224 static void
8225f_searchpos(typval_T *argvars, typval_T *rettv)
8226{
8227 pos_T match_pos;
8228 int lnum = 0;
8229 int col = 0;
8230 int n;
8231 int flags = 0;
8232
8233 if (rettv_list_alloc(rettv) == FAIL)
8234 return;
8235
8236 n = search_cmn(argvars, &match_pos, &flags);
8237 if (n > 0)
8238 {
8239 lnum = match_pos.lnum;
8240 col = match_pos.col;
8241 }
8242
8243 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8244 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8245 if (flags & SP_SUBPAT)
8246 list_append_number(rettv->vval.v_list, (varnumber_T)n);
8247}
8248
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008249/*
8250 * Set the cursor or mark position.
8251 * If 'charpos' is TRUE, then use the column number as a character offet.
8252 * Otherwise use the column number as a byte offset.
8253 */
8254 static void
8255set_position(typval_T *argvars, typval_T *rettv, int charpos)
8256{
8257 pos_T pos;
8258 int fnum;
8259 char_u *name;
8260 colnr_T curswant = -1;
8261
8262 rettv->vval.v_number = -1;
8263
8264 name = tv_get_string_chk(argvars);
8265 if (name != NULL)
8266 {
8267 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
8268 {
8269 if (pos.col != MAXCOL && --pos.col < 0)
8270 pos.col = 0;
8271 if ((name[0] == '.' && name[1] == NUL))
8272 {
8273 // set cursor; "fnum" is ignored
8274 curwin->w_cursor = pos;
8275 if (curswant >= 0)
8276 {
8277 curwin->w_curswant = curswant - 1;
8278 curwin->w_set_curswant = FALSE;
8279 }
8280 check_cursor();
8281 rettv->vval.v_number = 0;
8282 }
8283 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8284 {
8285 // set mark
8286 if (setmark_pos(name[1], &pos, fnum) == OK)
8287 rettv->vval.v_number = 0;
8288 }
8289 else
8290 emsg(_(e_invarg));
8291 }
8292 }
8293}
8294/*
8295 * "setcharpos()" function
8296 */
8297 static void
8298f_setcharpos(typval_T *argvars, typval_T *rettv)
8299{
8300 set_position(argvars, rettv, TRUE);
8301}
8302
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008303 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008304f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8305{
8306 dict_T *d;
8307 dictitem_T *di;
8308 char_u *csearch;
8309
8310 if (argvars[0].v_type != VAR_DICT)
8311 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008312 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008313 return;
8314 }
8315
8316 if ((d = argvars[0].vval.v_dict) != NULL)
8317 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008318 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008319 if (csearch != NULL)
8320 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008321 if (enc_utf8)
8322 {
8323 int pcc[MAX_MCO];
8324 int c = utfc_ptr2char(csearch, pcc);
8325
8326 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8327 }
8328 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008329 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008330 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008331 }
8332
8333 di = dict_find(d, (char_u *)"forward", -1);
8334 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008335 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008336 ? FORWARD : BACKWARD);
8337
8338 di = dict_find(d, (char_u *)"until", -1);
8339 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008340 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008341 }
8342}
8343
8344/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008345 * "setcursorcharpos" function
8346 */
8347 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008348f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008349{
8350 set_cursorpos(argvars, rettv, TRUE);
8351}
8352
8353/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008354 * "setenv()" function
8355 */
8356 static void
8357f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8358{
8359 char_u namebuf[NUMBUFLEN];
8360 char_u valbuf[NUMBUFLEN];
8361 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8362
8363 if (argvars[1].v_type == VAR_SPECIAL
8364 && argvars[1].vval.v_number == VVAL_NULL)
8365 vim_unsetenv(name);
8366 else
8367 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8368}
8369
8370/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008371 * "setfperm({fname}, {mode})" function
8372 */
8373 static void
8374f_setfperm(typval_T *argvars, typval_T *rettv)
8375{
8376 char_u *fname;
8377 char_u modebuf[NUMBUFLEN];
8378 char_u *mode_str;
8379 int i;
8380 int mask;
8381 int mode = 0;
8382
8383 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008384 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008385 if (fname == NULL)
8386 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008387 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008388 if (mode_str == NULL)
8389 return;
8390 if (STRLEN(mode_str) != 9)
8391 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008392 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008393 return;
8394 }
8395
8396 mask = 1;
8397 for (i = 8; i >= 0; --i)
8398 {
8399 if (mode_str[i] != '-')
8400 mode |= mask;
8401 mask = mask << 1;
8402 }
8403 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8404}
8405
8406/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008407 * "setpos()" function
8408 */
8409 static void
8410f_setpos(typval_T *argvars, typval_T *rettv)
8411{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008412 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413}
8414
8415/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008416 * Translate a register type string to the yank type and block length
8417 */
8418 static int
8419get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8420{
8421 char_u *stropt = *pp;
8422 switch (*stropt)
8423 {
8424 case 'v': case 'c': // character-wise selection
8425 *yank_type = MCHAR;
8426 break;
8427 case 'V': case 'l': // line-wise selection
8428 *yank_type = MLINE;
8429 break;
8430 case 'b': case Ctrl_V: // block-wise selection
8431 *yank_type = MBLOCK;
8432 if (VIM_ISDIGIT(stropt[1]))
8433 {
8434 ++stropt;
8435 *block_len = getdigits(&stropt) - 1;
8436 --stropt;
8437 }
8438 break;
8439 default:
8440 return FAIL;
8441 }
8442 *pp = stropt;
8443 return OK;
8444}
8445
8446/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008447 * "setreg()" function
8448 */
8449 static void
8450f_setreg(typval_T *argvars, typval_T *rettv)
8451{
8452 int regname;
8453 char_u *strregname;
8454 char_u *stropt;
8455 char_u *strval;
8456 int append;
8457 char_u yank_type;
8458 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008459 typval_T *regcontents;
8460 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008461
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008462 pointreg = 0;
8463 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008464 block_len = -1;
8465 yank_type = MAUTO;
8466 append = FALSE;
8467
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008468 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008469 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008470
8471 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008472 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008473 regname = *strregname;
8474 if (regname == 0 || regname == '@')
8475 regname = '"';
8476
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008477 if (argvars[1].v_type == VAR_DICT)
8478 {
8479 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008480 dictitem_T *di;
8481
8482 if (d == NULL || d->dv_hashtab.ht_used == 0)
8483 {
8484 // Empty dict, clear the register (like setreg(0, []))
8485 char_u *lstval[2] = {NULL, NULL};
8486 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8487 return;
8488 }
8489
8490 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008491 if (di != NULL)
8492 regcontents = &di->di_tv;
8493
8494 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8495 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008496 {
8497 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8498
8499 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008500 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008501 semsg(_(e_invargval), "value");
8502 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008503 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008504 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008505
8506 if (regname == '"')
8507 {
8508 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8509 if (stropt != NULL)
8510 {
8511 pointreg = *stropt;
8512 regname = pointreg;
8513 }
8514 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008515 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008516 pointreg = regname;
8517 }
8518 else
8519 regcontents = &argvars[1];
8520
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008521 if (argvars[2].v_type != VAR_UNKNOWN)
8522 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008523 if (yank_type != MAUTO)
8524 {
8525 semsg(_(e_toomanyarg), "setreg");
8526 return;
8527 }
8528
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008529 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008530 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008531 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532 for (; *stropt != NUL; ++stropt)
8533 switch (*stropt)
8534 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008535 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008536 append = TRUE;
8537 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008538 default:
8539 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008540 }
8541 }
8542
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008543 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008544 {
8545 char_u **lstval;
8546 char_u **allocval;
8547 char_u buf[NUMBUFLEN];
8548 char_u **curval;
8549 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008550 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008551 listitem_T *li;
8552 int len;
8553
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008554 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008555 len = ll == NULL ? 0 : ll->lv_len;
8556
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008557 // First half: use for pointers to result lines; second half: use for
8558 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008559 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008560 if (lstval == NULL)
8561 return;
8562 curval = lstval;
8563 allocval = lstval + len + 2;
8564 curallocval = allocval;
8565
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008566 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008567 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008568 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008569 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008570 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008571 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008572 if (strval == NULL)
8573 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008574 if (strval == buf)
8575 {
8576 // Need to make a copy, next tv_get_string_buf_chk() will
8577 // overwrite the string.
8578 strval = vim_strsave(buf);
8579 if (strval == NULL)
8580 goto free_lstval;
8581 *curallocval++ = strval;
8582 }
8583 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008584 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008585 }
8586 *curval++ = NULL;
8587
8588 write_reg_contents_lst(regname, lstval, -1,
8589 append, yank_type, block_len);
8590free_lstval:
8591 while (curallocval > allocval)
8592 vim_free(*--curallocval);
8593 vim_free(lstval);
8594 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008595 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008596 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008597 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008598 if (strval == NULL)
8599 return;
8600 write_reg_contents_ex(regname, strval, -1,
8601 append, yank_type, block_len);
8602 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008603 if (pointreg != 0)
8604 get_yank_register(pointreg, TRUE);
8605
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008606 rettv->vval.v_number = 0;
8607}
8608
8609/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008610 * "settagstack()" function
8611 */
8612 static void
8613f_settagstack(typval_T *argvars, typval_T *rettv)
8614{
8615 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8616 win_T *wp;
8617 dict_T *d;
8618 int action = 'r';
8619
8620 rettv->vval.v_number = -1;
8621
8622 // first argument: window number or id
8623 wp = find_win_by_nr_or_id(&argvars[0]);
8624 if (wp == NULL)
8625 return;
8626
8627 // second argument: dict with items to set in the tag stack
8628 if (argvars[1].v_type != VAR_DICT)
8629 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008630 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008631 return;
8632 }
8633 d = argvars[1].vval.v_dict;
8634 if (d == NULL)
8635 return;
8636
8637 // third argument: action - 'a' for append and 'r' for replace.
8638 // default is to replace the stack.
8639 if (argvars[2].v_type == VAR_UNKNOWN)
8640 action = 'r';
8641 else if (argvars[2].v_type == VAR_STRING)
8642 {
8643 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008644 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008645 if (actstr == NULL)
8646 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008647 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8648 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008649 action = *actstr;
8650 else
8651 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008652 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008653 return;
8654 }
8655 }
8656 else
8657 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008658 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008659 return;
8660 }
8661
8662 if (set_tagstack(wp, d, action) == OK)
8663 rettv->vval.v_number = 0;
8664}
8665
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008666#ifdef FEAT_CRYPT
8667/*
8668 * "sha256({string})" function
8669 */
8670 static void
8671f_sha256(typval_T *argvars, typval_T *rettv)
8672{
8673 char_u *p;
8674
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008675 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008676 rettv->vval.v_string = vim_strsave(
8677 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8678 rettv->v_type = VAR_STRING;
8679}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008680#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008681
8682/*
8683 * "shellescape({string})" function
8684 */
8685 static void
8686f_shellescape(typval_T *argvars, typval_T *rettv)
8687{
Bram Moolenaar20615522017-06-05 18:46:26 +02008688 int do_special = non_zero_arg(&argvars[1]);
8689
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008690 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008691 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008692 rettv->v_type = VAR_STRING;
8693}
8694
8695/*
8696 * shiftwidth() function
8697 */
8698 static void
8699f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8700{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008701 rettv->vval.v_number = 0;
8702
8703 if (argvars[0].v_type != VAR_UNKNOWN)
8704 {
8705 long col;
8706
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008707 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008708 if (col < 0)
8709 return; // type error; errmsg already given
8710#ifdef FEAT_VARTABS
8711 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8712 return;
8713#endif
8714 }
8715
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008716 rettv->vval.v_number = get_sw_value(curbuf);
8717}
8718
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008719#ifdef FEAT_FLOAT
8720/*
8721 * "sin()" function
8722 */
8723 static void
8724f_sin(typval_T *argvars, typval_T *rettv)
8725{
8726 float_T f = 0.0;
8727
8728 rettv->v_type = VAR_FLOAT;
8729 if (get_float_arg(argvars, &f) == OK)
8730 rettv->vval.v_float = sin(f);
8731 else
8732 rettv->vval.v_float = 0.0;
8733}
8734
8735/*
8736 * "sinh()" function
8737 */
8738 static void
8739f_sinh(typval_T *argvars, typval_T *rettv)
8740{
8741 float_T f = 0.0;
8742
8743 rettv->v_type = VAR_FLOAT;
8744 if (get_float_arg(argvars, &f) == OK)
8745 rettv->vval.v_float = sinh(f);
8746 else
8747 rettv->vval.v_float = 0.0;
8748}
8749#endif
8750
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008751/*
8752 * "soundfold({word})" function
8753 */
8754 static void
8755f_soundfold(typval_T *argvars, typval_T *rettv)
8756{
8757 char_u *s;
8758
8759 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008760 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008761#ifdef FEAT_SPELL
8762 rettv->vval.v_string = eval_soundfold(s);
8763#else
8764 rettv->vval.v_string = vim_strsave(s);
8765#endif
8766}
8767
8768/*
8769 * "spellbadword()" function
8770 */
8771 static void
8772f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8773{
8774 char_u *word = (char_u *)"";
8775 hlf_T attr = HLF_COUNT;
8776 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008777#ifdef FEAT_SPELL
8778 int wo_spell_save = curwin->w_p_spell;
8779
8780 if (!curwin->w_p_spell)
8781 {
8782 did_set_spelllang(curwin);
8783 curwin->w_p_spell = TRUE;
8784 }
8785
8786 if (*curwin->w_s->b_p_spl == NUL)
8787 {
8788 emsg(_(e_no_spell));
8789 curwin->w_p_spell = wo_spell_save;
8790 return;
8791 }
8792#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008793
8794 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008795 {
8796#ifdef FEAT_SPELL
8797 curwin->w_p_spell = wo_spell_save;
8798#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008799 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008800 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008801
8802#ifdef FEAT_SPELL
8803 if (argvars[0].v_type == VAR_UNKNOWN)
8804 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008805 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008806 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8807 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008808 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008809 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008810 curwin->w_set_curswant = TRUE;
8811 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008812 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008813 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008814 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008815 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008816 int capcol = -1;
8817
8818 if (str != NULL)
8819 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008820 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008821 while (*str != NUL)
8822 {
8823 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8824 if (attr != HLF_COUNT)
8825 {
8826 word = str;
8827 break;
8828 }
8829 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008830 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008831 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008832 }
8833 }
8834 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008835 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008836#endif
8837
8838 list_append_string(rettv->vval.v_list, word, len);
8839 list_append_string(rettv->vval.v_list, (char_u *)(
8840 attr == HLF_SPB ? "bad" :
8841 attr == HLF_SPR ? "rare" :
8842 attr == HLF_SPL ? "local" :
8843 attr == HLF_SPC ? "caps" :
8844 ""), -1);
8845}
8846
8847/*
8848 * "spellsuggest()" function
8849 */
8850 static void
8851f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8852{
8853#ifdef FEAT_SPELL
8854 char_u *str;
8855 int typeerr = FALSE;
8856 int maxcount;
8857 garray_T ga;
8858 int i;
8859 listitem_T *li;
8860 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008861 int wo_spell_save = curwin->w_p_spell;
8862
8863 if (!curwin->w_p_spell)
8864 {
8865 did_set_spelllang(curwin);
8866 curwin->w_p_spell = TRUE;
8867 }
8868
8869 if (*curwin->w_s->b_p_spl == NUL)
8870 {
8871 emsg(_(e_no_spell));
8872 curwin->w_p_spell = wo_spell_save;
8873 return;
8874 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875#endif
8876
8877 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008878 {
8879#ifdef FEAT_SPELL
8880 curwin->w_p_spell = wo_spell_save;
8881#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008882 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008883 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008884
8885#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008886 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008887 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008888 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008889 if (argvars[1].v_type != VAR_UNKNOWN)
8890 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008891 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008892 if (maxcount <= 0)
8893 return;
8894 if (argvars[2].v_type != VAR_UNKNOWN)
8895 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008896 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008897 if (typeerr)
8898 return;
8899 }
8900 }
8901 else
8902 maxcount = 25;
8903
8904 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8905
8906 for (i = 0; i < ga.ga_len; ++i)
8907 {
8908 str = ((char_u **)ga.ga_data)[i];
8909
8910 li = listitem_alloc();
8911 if (li == NULL)
8912 vim_free(str);
8913 else
8914 {
8915 li->li_tv.v_type = VAR_STRING;
8916 li->li_tv.v_lock = 0;
8917 li->li_tv.vval.v_string = str;
8918 list_append(rettv->vval.v_list, li);
8919 }
8920 }
8921 ga_clear(&ga);
8922 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008923 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924#endif
8925}
8926
8927 static void
8928f_split(typval_T *argvars, typval_T *rettv)
8929{
8930 char_u *str;
8931 char_u *end;
8932 char_u *pat = NULL;
8933 regmatch_T regmatch;
8934 char_u patbuf[NUMBUFLEN];
8935 char_u *save_cpo;
8936 int match;
8937 colnr_T col = 0;
8938 int keepempty = FALSE;
8939 int typeerr = FALSE;
8940
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008941 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008942 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008943 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008944
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008945 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008946 if (argvars[1].v_type != VAR_UNKNOWN)
8947 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008948 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008949 if (pat == NULL)
8950 typeerr = TRUE;
8951 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008952 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008953 }
8954 if (pat == NULL || *pat == NUL)
8955 pat = (char_u *)"[\\x01- ]\\+";
8956
8957 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008958 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008959 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008960 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008961
8962 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8963 if (regmatch.regprog != NULL)
8964 {
8965 regmatch.rm_ic = FALSE;
8966 while (*str != NUL || keepempty)
8967 {
8968 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008969 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008970 else
8971 match = vim_regexec_nl(&regmatch, str, col);
8972 if (match)
8973 end = regmatch.startp[0];
8974 else
8975 end = str + STRLEN(str);
8976 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
8977 && *str != NUL && match && end < regmatch.endp[0]))
8978 {
8979 if (list_append_string(rettv->vval.v_list, str,
8980 (int)(end - str)) == FAIL)
8981 break;
8982 }
8983 if (!match)
8984 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01008985 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008986 if (regmatch.endp[0] > str)
8987 col = 0;
8988 else
Bram Moolenaar13505972019-01-24 15:04:48 +01008989 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008990 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008991 str = regmatch.endp[0];
8992 }
8993
8994 vim_regfree(regmatch.regprog);
8995 }
8996
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008997theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008998 p_cpo = save_cpo;
8999}
9000
9001#ifdef FEAT_FLOAT
9002/*
9003 * "sqrt()" function
9004 */
9005 static void
9006f_sqrt(typval_T *argvars, typval_T *rettv)
9007{
9008 float_T f = 0.0;
9009
9010 rettv->v_type = VAR_FLOAT;
9011 if (get_float_arg(argvars, &f) == OK)
9012 rettv->vval.v_float = sqrt(f);
9013 else
9014 rettv->vval.v_float = 0.0;
9015}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009016#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009017
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009018#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01009019/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009020 * "str2float()" function
9021 */
9022 static void
9023f_str2float(typval_T *argvars, typval_T *rettv)
9024{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009025 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009026 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009027
Bram Moolenaar08243d22017-01-10 16:12:29 +01009028 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009029 p = skipwhite(p + 1);
9030 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01009031 if (isneg)
9032 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033 rettv->v_type = VAR_FLOAT;
9034}
9035#endif
9036
9037/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02009038 * "str2list()" function
9039 */
9040 static void
9041f_str2list(typval_T *argvars, typval_T *rettv)
9042{
9043 char_u *p;
9044 int utf8 = FALSE;
9045
9046 if (rettv_list_alloc(rettv) == FAIL)
9047 return;
9048
9049 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02009050 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02009051
9052 p = tv_get_string(&argvars[0]);
9053
9054 if (has_mbyte || utf8)
9055 {
9056 int (*ptr2len)(char_u *);
9057 int (*ptr2char)(char_u *);
9058
9059 if (utf8 || enc_utf8)
9060 {
9061 ptr2len = utf_ptr2len;
9062 ptr2char = utf_ptr2char;
9063 }
9064 else
9065 {
9066 ptr2len = mb_ptr2len;
9067 ptr2char = mb_ptr2char;
9068 }
9069
9070 for ( ; *p != NUL; p += (*ptr2len)(p))
9071 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
9072 }
9073 else
9074 for ( ; *p != NUL; ++p)
9075 list_append_number(rettv->vval.v_list, *p);
9076}
9077
9078/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009079 * "str2nr()" function
9080 */
9081 static void
9082f_str2nr(typval_T *argvars, typval_T *rettv)
9083{
9084 int base = 10;
9085 char_u *p;
9086 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009087 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01009088 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009089
9090 if (argvars[1].v_type != VAR_UNKNOWN)
9091 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009092 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009093 if (base != 2 && base != 8 && base != 10 && base != 16)
9094 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009095 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009096 return;
9097 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02009098 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009099 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009100 }
9101
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01009102 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009103 isneg = (*p == '-');
9104 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009105 p = skipwhite(p + 1);
9106 switch (base)
9107 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009108 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02009109 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009110 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009111 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02009112 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
9113 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01009114 if (isneg)
9115 rettv->vval.v_number = -n;
9116 else
9117 rettv->vval.v_number = n;
9118
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009119}
9120
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009121/*
9122 * "strgetchar()" function
9123 */
9124 static void
9125f_strgetchar(typval_T *argvars, typval_T *rettv)
9126{
9127 char_u *str;
9128 int len;
9129 int error = FALSE;
9130 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01009131 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009132
9133 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009134 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009135 if (str == NULL)
9136 return;
9137 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009138 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009139 if (error)
9140 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009141
Bram Moolenaar13505972019-01-24 15:04:48 +01009142 while (charidx >= 0 && byteidx < len)
9143 {
9144 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009145 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009146 rettv->vval.v_number = mb_ptr2char(str + byteidx);
9147 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009148 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009149 --charidx;
9150 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009151 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009152}
9153
9154/*
9155 * "stridx()" function
9156 */
9157 static void
9158f_stridx(typval_T *argvars, typval_T *rettv)
9159{
9160 char_u buf[NUMBUFLEN];
9161 char_u *needle;
9162 char_u *haystack;
9163 char_u *save_haystack;
9164 char_u *pos;
9165 int start_idx;
9166
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009167 needle = tv_get_string_chk(&argvars[1]);
9168 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009169 rettv->vval.v_number = -1;
9170 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009171 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009172
9173 if (argvars[2].v_type != VAR_UNKNOWN)
9174 {
9175 int error = FALSE;
9176
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009177 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009178 if (error || start_idx >= (int)STRLEN(haystack))
9179 return;
9180 if (start_idx >= 0)
9181 haystack += start_idx;
9182 }
9183
9184 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9185 if (pos != NULL)
9186 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
9187}
9188
9189/*
9190 * "string()" function
9191 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009192 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009193f_string(typval_T *argvars, typval_T *rettv)
9194{
9195 char_u *tofree;
9196 char_u numbuf[NUMBUFLEN];
9197
9198 rettv->v_type = VAR_STRING;
9199 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
9200 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009201 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009202 if (rettv->vval.v_string != NULL && tofree == NULL)
9203 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
9204}
9205
9206/*
9207 * "strlen()" function
9208 */
9209 static void
9210f_strlen(typval_T *argvars, typval_T *rettv)
9211{
9212 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009213 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009214}
9215
9216/*
9217 * "strchars()" function
9218 */
9219 static void
9220f_strchars(typval_T *argvars, typval_T *rettv)
9221{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009222 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009223 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009224 varnumber_T len = 0;
9225 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009226
9227 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009228 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009229 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02009230 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009231 else
9232 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009233 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
9234 while (*s != NUL)
9235 {
9236 func_mb_ptr2char_adv(&s);
9237 ++len;
9238 }
9239 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009240 }
9241}
9242
9243/*
9244 * "strdisplaywidth()" function
9245 */
9246 static void
9247f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
9248{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009249 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009250 int col = 0;
9251
9252 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009253 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009254
9255 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
9256}
9257
9258/*
9259 * "strwidth()" function
9260 */
9261 static void
9262f_strwidth(typval_T *argvars, typval_T *rettv)
9263{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009264 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009265
Bram Moolenaar13505972019-01-24 15:04:48 +01009266 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009267}
9268
9269/*
9270 * "strcharpart()" function
9271 */
9272 static void
9273f_strcharpart(typval_T *argvars, typval_T *rettv)
9274{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009275 char_u *p;
9276 int nchar;
9277 int nbyte = 0;
9278 int charlen;
9279 int len = 0;
9280 int slen;
9281 int error = FALSE;
9282
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009283 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009284 slen = (int)STRLEN(p);
9285
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009286 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009287 if (!error)
9288 {
9289 if (nchar > 0)
9290 while (nchar > 0 && nbyte < slen)
9291 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009292 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009293 --nchar;
9294 }
9295 else
9296 nbyte = nchar;
9297 if (argvars[2].v_type != VAR_UNKNOWN)
9298 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009299 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009300 while (charlen > 0 && nbyte + len < slen)
9301 {
9302 int off = nbyte + len;
9303
9304 if (off < 0)
9305 len += 1;
9306 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009307 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009308 --charlen;
9309 }
9310 }
9311 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009312 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009313 }
9314
9315 /*
9316 * Only return the overlap between the specified part and the actual
9317 * string.
9318 */
9319 if (nbyte < 0)
9320 {
9321 len += nbyte;
9322 nbyte = 0;
9323 }
9324 else if (nbyte > slen)
9325 nbyte = slen;
9326 if (len < 0)
9327 len = 0;
9328 else if (nbyte + len > slen)
9329 len = slen - nbyte;
9330
9331 rettv->v_type = VAR_STRING;
9332 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009333}
9334
9335/*
9336 * "strpart()" function
9337 */
9338 static void
9339f_strpart(typval_T *argvars, typval_T *rettv)
9340{
9341 char_u *p;
9342 int n;
9343 int len;
9344 int slen;
9345 int error = FALSE;
9346
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009347 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009348 slen = (int)STRLEN(p);
9349
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009350 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009351 if (error)
9352 len = 0;
9353 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009354 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009355 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009356 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009357
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009358 // Only return the overlap between the specified part and the actual
9359 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009360 if (n < 0)
9361 {
9362 len += n;
9363 n = 0;
9364 }
9365 else if (n > slen)
9366 n = slen;
9367 if (len < 0)
9368 len = 0;
9369 else if (n + len > slen)
9370 len = slen - n;
9371
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009372 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9373 {
9374 int off;
9375
9376 // length in characters
9377 for (off = n; off < slen && len > 0; --len)
9378 off += mb_ptr2len(p + off);
9379 len = off - n;
9380 }
9381
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009382 rettv->v_type = VAR_STRING;
9383 rettv->vval.v_string = vim_strnsave(p + n, len);
9384}
9385
9386/*
9387 * "strridx()" function
9388 */
9389 static void
9390f_strridx(typval_T *argvars, typval_T *rettv)
9391{
9392 char_u buf[NUMBUFLEN];
9393 char_u *needle;
9394 char_u *haystack;
9395 char_u *rest;
9396 char_u *lastmatch = NULL;
9397 int haystack_len, end_idx;
9398
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009399 needle = tv_get_string_chk(&argvars[1]);
9400 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009401
9402 rettv->vval.v_number = -1;
9403 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009404 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009405
9406 haystack_len = (int)STRLEN(haystack);
9407 if (argvars[2].v_type != VAR_UNKNOWN)
9408 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009409 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009410 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009411 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009412 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009413 }
9414 else
9415 end_idx = haystack_len;
9416
9417 if (*needle == NUL)
9418 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009419 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009420 lastmatch = haystack + end_idx;
9421 }
9422 else
9423 {
9424 for (rest = haystack; *rest != '\0'; ++rest)
9425 {
9426 rest = (char_u *)strstr((char *)rest, (char *)needle);
9427 if (rest == NULL || rest > haystack + end_idx)
9428 break;
9429 lastmatch = rest;
9430 }
9431 }
9432
9433 if (lastmatch == NULL)
9434 rettv->vval.v_number = -1;
9435 else
9436 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9437}
9438
9439/*
9440 * "strtrans()" function
9441 */
9442 static void
9443f_strtrans(typval_T *argvars, typval_T *rettv)
9444{
9445 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009446 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009447}
9448
9449/*
9450 * "submatch()" function
9451 */
9452 static void
9453f_submatch(typval_T *argvars, typval_T *rettv)
9454{
9455 int error = FALSE;
9456 int no;
9457 int retList = 0;
9458
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009459 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009460 if (error)
9461 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009462 if (no < 0 || no >= NSUBEXP)
9463 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009464 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009465 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009466 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009467 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009468 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009469 if (error)
9470 return;
9471
9472 if (retList == 0)
9473 {
9474 rettv->v_type = VAR_STRING;
9475 rettv->vval.v_string = reg_submatch(no);
9476 }
9477 else
9478 {
9479 rettv->v_type = VAR_LIST;
9480 rettv->vval.v_list = reg_submatch_list(no);
9481 }
9482}
9483
9484/*
9485 * "substitute()" function
9486 */
9487 static void
9488f_substitute(typval_T *argvars, typval_T *rettv)
9489{
9490 char_u patbuf[NUMBUFLEN];
9491 char_u subbuf[NUMBUFLEN];
9492 char_u flagsbuf[NUMBUFLEN];
9493
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009494 char_u *str = tv_get_string_chk(&argvars[0]);
9495 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009496 char_u *sub = NULL;
9497 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009498 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009499
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009500 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9501 expr = &argvars[2];
9502 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009503 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009504
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009505 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009506 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9507 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009508 rettv->vval.v_string = NULL;
9509 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009510 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009511}
9512
9513/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009514 * "swapinfo(swap_filename)" function
9515 */
9516 static void
9517f_swapinfo(typval_T *argvars, typval_T *rettv)
9518{
9519 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009520 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009521}
9522
9523/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009524 * "swapname(expr)" function
9525 */
9526 static void
9527f_swapname(typval_T *argvars, typval_T *rettv)
9528{
9529 buf_T *buf;
9530
9531 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009532 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009533 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9534 || buf->b_ml.ml_mfp->mf_fname == NULL)
9535 rettv->vval.v_string = NULL;
9536 else
9537 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9538}
9539
9540/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009541 * "synID(lnum, col, trans)" function
9542 */
9543 static void
9544f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9545{
9546 int id = 0;
9547#ifdef FEAT_SYN_HL
9548 linenr_T lnum;
9549 colnr_T col;
9550 int trans;
9551 int transerr = FALSE;
9552
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009553 lnum = tv_get_lnum(argvars); // -1 on type error
9554 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009555 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009556
9557 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9558 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9559 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9560#endif
9561
9562 rettv->vval.v_number = id;
9563}
9564
9565/*
9566 * "synIDattr(id, what [, mode])" function
9567 */
9568 static void
9569f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9570{
9571 char_u *p = NULL;
9572#ifdef FEAT_SYN_HL
9573 int id;
9574 char_u *what;
9575 char_u *mode;
9576 char_u modebuf[NUMBUFLEN];
9577 int modec;
9578
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009579 id = (int)tv_get_number(&argvars[0]);
9580 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009581 if (argvars[2].v_type != VAR_UNKNOWN)
9582 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009583 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009584 modec = TOLOWER_ASC(mode[0]);
9585 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009586 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009587 }
9588 else
9589 {
9590#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9591 if (USE_24BIT)
9592 modec = 'g';
9593 else
9594#endif
9595 if (t_colors > 1)
9596 modec = 'c';
9597 else
9598 modec = 't';
9599 }
9600
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009601 switch (TOLOWER_ASC(what[0]))
9602 {
9603 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009604 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009605 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009606 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009607 p = highlight_has_attr(id, HL_BOLD, modec);
9608 break;
9609
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009610 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009611 p = highlight_color(id, what, modec);
9612 break;
9613
9614 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009615 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009616 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009617 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009618 p = highlight_has_attr(id, HL_ITALIC, modec);
9619 break;
9620
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009621 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009622 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009623 break;
9624
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009625 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009626 p = highlight_has_attr(id, HL_INVERSE, modec);
9627 break;
9628
9629 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009630 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009631 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009632 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009633 else if (TOLOWER_ASC(what[1]) == 't' &&
9634 TOLOWER_ASC(what[2]) == 'r')
9635 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009636 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009637 p = highlight_has_attr(id, HL_STANDOUT, modec);
9638 break;
9639
9640 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009641 if (TOLOWER_ASC(what[1]) == 'l') // ul
9642 p = highlight_color(id, what, modec);
9643 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009644 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009645 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9646 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009647 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009648 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9649 break;
9650 }
9651
9652 if (p != NULL)
9653 p = vim_strsave(p);
9654#endif
9655 rettv->v_type = VAR_STRING;
9656 rettv->vval.v_string = p;
9657}
9658
9659/*
9660 * "synIDtrans(id)" function
9661 */
9662 static void
9663f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9664{
9665 int id;
9666
9667#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009668 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009669
9670 if (id > 0)
9671 id = syn_get_final_id(id);
9672 else
9673#endif
9674 id = 0;
9675
9676 rettv->vval.v_number = id;
9677}
9678
9679/*
9680 * "synconcealed(lnum, col)" function
9681 */
9682 static void
9683f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9684{
9685#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9686 linenr_T lnum;
9687 colnr_T col;
9688 int syntax_flags = 0;
9689 int cchar;
9690 int matchid = 0;
9691 char_u str[NUMBUFLEN];
9692#endif
9693
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009694 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009695
9696#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009697 lnum = tv_get_lnum(argvars); // -1 on type error
9698 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009699
Bram Moolenaara80faa82020-04-12 19:37:17 +02009700 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009701
9702 if (rettv_list_alloc(rettv) != FAIL)
9703 {
9704 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9705 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9706 && curwin->w_p_cole > 0)
9707 {
9708 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9709 syntax_flags = get_syntax_info(&matchid);
9710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009711 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009712 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9713 {
9714 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009715 if (cchar == NUL && curwin->w_p_cole == 1)
9716 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009717 if (cchar != NUL)
9718 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009719 if (has_mbyte)
9720 (*mb_char2bytes)(cchar, str);
9721 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009722 str[0] = cchar;
9723 }
9724 }
9725 }
9726
9727 list_append_number(rettv->vval.v_list,
9728 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009729 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009730 list_append_string(rettv->vval.v_list, str, -1);
9731 list_append_number(rettv->vval.v_list, matchid);
9732 }
9733#endif
9734}
9735
9736/*
9737 * "synstack(lnum, col)" function
9738 */
9739 static void
9740f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9741{
9742#ifdef FEAT_SYN_HL
9743 linenr_T lnum;
9744 colnr_T col;
9745 int i;
9746 int id;
9747#endif
9748
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009749 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009750
9751#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009752 lnum = tv_get_lnum(argvars); // -1 on type error
9753 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009754
9755 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9756 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9757 && rettv_list_alloc(rettv) != FAIL)
9758 {
9759 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9760 for (i = 0; ; ++i)
9761 {
9762 id = syn_get_stack_item(i);
9763 if (id < 0)
9764 break;
9765 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9766 break;
9767 }
9768 }
9769#endif
9770}
9771
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009772/*
9773 * "tabpagebuflist()" function
9774 */
9775 static void
9776f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9777{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009778 tabpage_T *tp;
9779 win_T *wp = NULL;
9780
9781 if (argvars[0].v_type == VAR_UNKNOWN)
9782 wp = firstwin;
9783 else
9784 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009785 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009786 if (tp != NULL)
9787 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9788 }
9789 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9790 {
9791 for (; wp != NULL; wp = wp->w_next)
9792 if (list_append_number(rettv->vval.v_list,
9793 wp->w_buffer->b_fnum) == FAIL)
9794 break;
9795 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009796}
9797
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009798/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009799 * "tagfiles()" function
9800 */
9801 static void
9802f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9803{
9804 char_u *fname;
9805 tagname_T tn;
9806 int first;
9807
9808 if (rettv_list_alloc(rettv) == FAIL)
9809 return;
9810 fname = alloc(MAXPATHL);
9811 if (fname == NULL)
9812 return;
9813
9814 for (first = TRUE; ; first = FALSE)
9815 if (get_tagfname(&tn, first, fname) == FAIL
9816 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9817 break;
9818 tagname_free(&tn);
9819 vim_free(fname);
9820}
9821
9822/*
9823 * "taglist()" function
9824 */
9825 static void
9826f_taglist(typval_T *argvars, typval_T *rettv)
9827{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009828 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009829 char_u *tag_pattern;
9830
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009831 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009832
9833 rettv->vval.v_number = FALSE;
9834 if (*tag_pattern == NUL)
9835 return;
9836
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009837 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009838 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009839 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009840 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009841}
9842
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009843#ifdef FEAT_FLOAT
9844/*
9845 * "tan()" function
9846 */
9847 static void
9848f_tan(typval_T *argvars, typval_T *rettv)
9849{
9850 float_T f = 0.0;
9851
9852 rettv->v_type = VAR_FLOAT;
9853 if (get_float_arg(argvars, &f) == OK)
9854 rettv->vval.v_float = tan(f);
9855 else
9856 rettv->vval.v_float = 0.0;
9857}
9858
9859/*
9860 * "tanh()" function
9861 */
9862 static void
9863f_tanh(typval_T *argvars, typval_T *rettv)
9864{
9865 float_T f = 0.0;
9866
9867 rettv->v_type = VAR_FLOAT;
9868 if (get_float_arg(argvars, &f) == OK)
9869 rettv->vval.v_float = tanh(f);
9870 else
9871 rettv->vval.v_float = 0.0;
9872}
9873#endif
9874
9875/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009876 * "tolower(string)" function
9877 */
9878 static void
9879f_tolower(typval_T *argvars, typval_T *rettv)
9880{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009881 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009882 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009883}
9884
9885/*
9886 * "toupper(string)" function
9887 */
9888 static void
9889f_toupper(typval_T *argvars, typval_T *rettv)
9890{
9891 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009892 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009893}
9894
9895/*
9896 * "tr(string, fromstr, tostr)" function
9897 */
9898 static void
9899f_tr(typval_T *argvars, typval_T *rettv)
9900{
9901 char_u *in_str;
9902 char_u *fromstr;
9903 char_u *tostr;
9904 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009905 int inlen;
9906 int fromlen;
9907 int tolen;
9908 int idx;
9909 char_u *cpstr;
9910 int cplen;
9911 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009912 char_u buf[NUMBUFLEN];
9913 char_u buf2[NUMBUFLEN];
9914 garray_T ga;
9915
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009916 in_str = tv_get_string(&argvars[0]);
9917 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9918 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009919
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009920 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009921 rettv->v_type = VAR_STRING;
9922 rettv->vval.v_string = NULL;
9923 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009924 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009925 ga_init2(&ga, (int)sizeof(char), 80);
9926
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009927 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009928 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009929 if (STRLEN(fromstr) != STRLEN(tostr))
9930 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009931error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009932 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009933 ga_clear(&ga);
9934 return;
9935 }
9936
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009937 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009938 while (*in_str != NUL)
9939 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009940 if (has_mbyte)
9941 {
9942 inlen = (*mb_ptr2len)(in_str);
9943 cpstr = in_str;
9944 cplen = inlen;
9945 idx = 0;
9946 for (p = fromstr; *p != NUL; p += fromlen)
9947 {
9948 fromlen = (*mb_ptr2len)(p);
9949 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9950 {
9951 for (p = tostr; *p != NUL; p += tolen)
9952 {
9953 tolen = (*mb_ptr2len)(p);
9954 if (idx-- == 0)
9955 {
9956 cplen = tolen;
9957 cpstr = p;
9958 break;
9959 }
9960 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009961 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009962 goto error;
9963 break;
9964 }
9965 ++idx;
9966 }
9967
9968 if (first && cpstr == in_str)
9969 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009970 // Check that fromstr and tostr have the same number of
9971 // (multi-byte) characters. Done only once when a character
9972 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009973 first = FALSE;
9974 for (p = tostr; *p != NUL; p += tolen)
9975 {
9976 tolen = (*mb_ptr2len)(p);
9977 --idx;
9978 }
9979 if (idx != 0)
9980 goto error;
9981 }
9982
9983 (void)ga_grow(&ga, cplen);
9984 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
9985 ga.ga_len += cplen;
9986
9987 in_str += inlen;
9988 }
9989 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009990 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009991 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009992 p = vim_strchr(fromstr, *in_str);
9993 if (p != NULL)
9994 ga_append(&ga, tostr[p - fromstr]);
9995 else
9996 ga_append(&ga, *in_str);
9997 ++in_str;
9998 }
9999 }
10000
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010001 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010002 (void)ga_grow(&ga, 1);
10003 ga_append(&ga, NUL);
10004
10005 rettv->vval.v_string = ga.ga_data;
10006}
10007
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010008/*
10009 * "trim({expr})" function
10010 */
10011 static void
10012f_trim(typval_T *argvars, typval_T *rettv)
10013{
10014 char_u buf1[NUMBUFLEN];
10015 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010016 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010017 char_u *mask = NULL;
10018 char_u *tail;
10019 char_u *prev;
10020 char_u *p;
10021 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010022 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010023
10024 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010025 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010026 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010027 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010028
10029 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010030 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010031 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010032
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010033 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010034 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010035 int error = 0;
10036
10037 // leading or trailing characters to trim
10038 dir = (int)tv_get_number_chk(&argvars[2], &error);
10039 if (error)
10040 return;
10041 if (dir < 0 || dir > 2)
10042 {
10043 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
10044 return;
10045 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010046 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010047 }
10048
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010049 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010050 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010051 // Trim leading characters
10052 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010053 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010054 c1 = PTR2CHAR(head);
10055 if (mask == NULL)
10056 {
10057 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010058 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010059 }
10060 else
10061 {
10062 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10063 if (c1 == PTR2CHAR(p))
10064 break;
10065 if (*p == NUL)
10066 break;
10067 }
10068 MB_PTR_ADV(head);
10069 }
10070 }
10071
10072 tail = head + STRLEN(head);
10073 if (dir == 0 || dir == 2)
10074 {
10075 // Trim trailing characters
10076 for (; tail > head; tail = prev)
10077 {
10078 prev = tail;
10079 MB_PTR_BACK(head, prev);
10080 c1 = PTR2CHAR(prev);
10081 if (mask == NULL)
10082 {
10083 if (c1 > ' ' && c1 != 0xa0)
10084 break;
10085 }
10086 else
10087 {
10088 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10089 if (c1 == PTR2CHAR(p))
10090 break;
10091 if (*p == NUL)
10092 break;
10093 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010094 }
10095 }
Bram Moolenaardf44a272020-06-07 20:49:05 +020010096 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010097}
10098
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010099#ifdef FEAT_FLOAT
10100/*
10101 * "trunc({float})" function
10102 */
10103 static void
10104f_trunc(typval_T *argvars, typval_T *rettv)
10105{
10106 float_T f = 0.0;
10107
10108 rettv->v_type = VAR_FLOAT;
10109 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010110 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010111 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
10112 else
10113 rettv->vval.v_float = 0.0;
10114}
10115#endif
10116
10117/*
10118 * "type(expr)" function
10119 */
10120 static void
10121f_type(typval_T *argvars, typval_T *rettv)
10122{
10123 int n = -1;
10124
10125 switch (argvars[0].v_type)
10126 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010127 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
10128 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010129 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010130 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
10131 case VAR_LIST: n = VAR_TYPE_LIST; break;
10132 case VAR_DICT: n = VAR_TYPE_DICT; break;
10133 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
10134 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
10135 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020010136 case VAR_JOB: n = VAR_TYPE_JOB; break;
10137 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010138 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010139 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +020010140 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010141 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +010010142 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010143 n = -1;
10144 break;
10145 }
10146 rettv->vval.v_number = n;
10147}
10148
10149/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010150 * "virtcol(string)" function
10151 */
10152 static void
10153f_virtcol(typval_T *argvars, typval_T *rettv)
10154{
10155 colnr_T vcol = 0;
10156 pos_T *fp;
10157 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010158 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010159
Bram Moolenaar6f02b002021-01-10 20:22:54 +010010160 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010161 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
10162 && fnum == curbuf->b_fnum)
10163 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010164 // Limit the column to a valid value, getvvcol() doesn't check.
10165 if (fp->col < 0)
10166 fp->col = 0;
10167 else
10168 {
10169 len = (int)STRLEN(ml_get(fp->lnum));
10170 if (fp->col > len)
10171 fp->col = len;
10172 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010173 getvvcol(curwin, fp, NULL, NULL, &vcol);
10174 ++vcol;
10175 }
10176
10177 rettv->vval.v_number = vcol;
10178}
10179
10180/*
10181 * "visualmode()" function
10182 */
10183 static void
10184f_visualmode(typval_T *argvars, typval_T *rettv)
10185{
10186 char_u str[2];
10187
10188 rettv->v_type = VAR_STRING;
10189 str[0] = curbuf->b_visual_mode_eval;
10190 str[1] = NUL;
10191 rettv->vval.v_string = vim_strsave(str);
10192
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010193 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010194 if (non_zero_arg(&argvars[0]))
10195 curbuf->b_visual_mode_eval = NUL;
10196}
10197
10198/*
10199 * "wildmenumode()" function
10200 */
10201 static void
10202f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10203{
10204#ifdef FEAT_WILDMENU
10205 if (wild_menu_showing)
10206 rettv->vval.v_number = 1;
10207#endif
10208}
10209
10210/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +010010211 * "windowsversion()" function
10212 */
10213 static void
10214f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10215{
10216 rettv->v_type = VAR_STRING;
10217 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
10218}
10219
10220/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010221 * "wordcount()" function
10222 */
10223 static void
10224f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
10225{
10226 if (rettv_dict_alloc(rettv) == FAIL)
10227 return;
10228 cursor_pos_info(rettv->vval.v_dict);
10229}
10230
10231/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010232 * "xor(expr, expr)" function
10233 */
10234 static void
10235f_xor(typval_T *argvars, typval_T *rettv)
10236{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010237 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
10238 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010239}
10240
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010241#endif // FEAT_EVAL