blob: 1fad082ac021834dd8b644cf9b8bf104da3307a7 [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 Moolenaarf2b26bc2021-01-30 23:05:11 +0100438argcheck_T arg3_string_nr_bool[] = {arg_string, arg_number, arg_bool};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200439argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100440argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev};
441argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3};
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100442argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
Bram Moolenaarca174532020-10-21 16:42:22 +0200443argcheck_T arg3_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200444
445/*
446 * Functions that return the return type of a builtin function.
Bram Moolenaara1224cb2020-10-22 12:31:49 +0200447 * Note that "argtypes" is NULL if "argcount" is zero.
Bram Moolenaar94738d82020-10-21 14:25:07 +0200448 */
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100449 static type_T *
450ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
451{
452 return &t_void;
453}
454 static type_T *
455ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
456{
457 return &t_any;
458}
459 static type_T *
Bram Moolenaar403dc312020-10-17 19:29:51 +0200460ret_bool(int argcount UNUSED, type_T **argtypes UNUSED)
461{
462 return &t_bool;
463}
464 static type_T *
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100465ret_number_bool(int argcount UNUSED, type_T **argtypes UNUSED)
466{
467 return &t_number_bool;
468}
469 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100470ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
471{
472 return &t_number;
473}
474 static type_T *
475ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
476{
477 return &t_float;
478}
479 static type_T *
480ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
481{
482 return &t_string;
483}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200484 static type_T *
485ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100486{
487 return &t_list_any;
488}
489 static type_T *
490ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
491{
492 return &t_list_number;
493}
494 static type_T *
495ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
496{
497 return &t_list_string;
498}
499 static type_T *
500ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
501{
502 return &t_list_dict_any;
503}
504 static type_T *
505ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
506{
507 return &t_dict_any;
508}
509 static type_T *
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100510ret_job_info(int argcount, type_T **argtypes UNUSED)
511{
512 if (argcount == 0)
513 return &t_list_job;
514 return &t_dict_any;
515}
516 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100517ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
518{
519 return &t_dict_number;
520}
521 static type_T *
522ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
523{
524 return &t_dict_string;
525}
526 static type_T *
527ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
528{
529 return &t_blob;
530}
531 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200532ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100533{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200534 return &t_func_any;
535}
536 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100537ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
538{
539 return &t_channel;
540}
541 static type_T *
542ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
543{
544 return &t_job;
545}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200546 static type_T *
547ret_first_arg(int argcount, type_T **argtypes)
548{
549 if (argcount > 0)
550 return argtypes[0];
551 return &t_void;
552}
Bram Moolenaarea696852020-11-09 18:31:39 +0100553// for map(): returns first argument but item type may differ
554 static type_T *
555ret_first_cont(int argcount UNUSED, type_T **argtypes)
556{
557 if (argtypes[0]->tt_type == VAR_LIST)
558 return &t_list_any;
559 if (argtypes[0]->tt_type == VAR_DICT)
560 return &t_dict_any;
561 if (argtypes[0]->tt_type == VAR_BLOB)
562 return argtypes[0];
563 return &t_any;
564}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200565
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200566/*
567 * Used for getqflist(): returns list if there is no argument, dict if there is
568 * one.
569 */
570 static type_T *
571ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
572{
573 if (argcount > 0)
574 return &t_dict_any;
575 return &t_list_dict_any;
576}
577
578/*
579 * Used for getloclist(): returns list if there is one argument, dict if there
580 * are two.
581 */
582 static type_T *
583ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
584{
585 if (argcount > 1)
586 return &t_dict_any;
587 return &t_list_dict_any;
588}
589
Bram Moolenaar846178a2020-07-05 17:04:13 +0200590 static type_T *
591ret_argv(int argcount, type_T **argtypes UNUSED)
592{
593 // argv() returns list of strings
594 if (argcount == 0)
595 return &t_list_string;
596
597 // argv(0) returns a string, but argv(-1] returns a list
598 return &t_any;
599}
600
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200601 static type_T *
602ret_remove(int argcount UNUSED, type_T **argtypes)
603{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200604 if (argtypes != NULL)
605 {
606 if (argtypes[0]->tt_type == VAR_LIST
607 || argtypes[0]->tt_type == VAR_DICT)
608 return argtypes[0]->tt_member;
609 if (argtypes[0]->tt_type == VAR_BLOB)
610 return &t_number;
611 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200612 return &t_any;
613}
614
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200615 static type_T *
616ret_getreg(int argcount, type_T **argtypes UNUSED)
617{
618 // Assume that if the third argument is passed it's non-zero
619 if (argcount == 3)
620 return &t_list_string;
621 return &t_string;
622}
623
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200624 static type_T *
625ret_maparg(int argcount, type_T **argtypes UNUSED)
626{
627 // Assume that if the fourth argument is passed it's non-zero
628 if (argcount == 4)
629 return &t_dict_any;
630 return &t_string;
631}
632
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100633static type_T *ret_f_function(int argcount, type_T **argtypes);
634
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200635/*
636 * Array with names and number of arguments of all internal functions
637 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
638 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200639typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200640{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200641 char *f_name; // function name
642 char f_min_argc; // minimal number of arguments
643 char f_max_argc; // maximal number of arguments
644 char f_argtype; // for method: FEARG_ values
Bram Moolenaar94738d82020-10-21 14:25:07 +0200645 argcheck_T *f_argcheck; // list of functions to check argument types
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100646 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
647 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200648 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200649 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200650} funcentry_T;
651
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200652// values for f_argtype; zero means it cannot be used as a method
653#define FEARG_1 1 // base is the first argument
654#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200655#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200656#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200657#define FEARG_LAST 9 // base is the last argument
658
Bram Moolenaar15c47602020-03-26 22:16:48 +0100659#ifdef FEAT_FLOAT
660# define FLOAT_FUNC(name) name
661#else
662# define FLOAT_FUNC(name) NULL
663#endif
664#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
665# define MATH_FUNC(name) name
666#else
667# define MATH_FUNC(name) NULL
668#endif
669#ifdef FEAT_TIMERS
670# define TIMER_FUNC(name) name
671#else
672# define TIMER_FUNC(name) NULL
673#endif
674#ifdef FEAT_JOB_CHANNEL
675# define JOB_FUNC(name) name
676#else
677# define JOB_FUNC(name) NULL
678#endif
679#ifdef FEAT_PROP_POPUP
680# define PROP_FUNC(name) name
681#else
682# define PROP_FUNC(name) NULL
683#endif
684#ifdef FEAT_SIGNS
685# define SIGN_FUNC(name) name
686#else
687# define SIGN_FUNC(name) NULL
688#endif
689#ifdef FEAT_SOUND
690# define SOUND_FUNC(name) name
691#else
692# define SOUND_FUNC(name) NULL
693#endif
694#ifdef FEAT_TERMINAL
695# define TERM_FUNC(name) name
696#else
697# define TERM_FUNC(name) NULL
698#endif
699
Bram Moolenaarac92e252019-08-03 21:58:38 +0200700static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200701{
Bram Moolenaar94738d82020-10-21 14:25:07 +0200702 {"abs", 1, 1, FEARG_1, arg1_float_or_nr,
703 ret_any, FLOAT_FUNC(f_abs)},
704 {"acos", 1, 1, FEARG_1, NULL,
705 ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100706 {"add", 2, 2, FEARG_1, NULL /* arg2_listblob_item */,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200707 ret_first_arg, f_add},
708 {"and", 2, 2, FEARG_1, NULL,
709 ret_number, f_and},
710 {"append", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100711 ret_number_bool, f_append},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200712 {"appendbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100713 ret_number_bool, f_appendbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200714 {"argc", 0, 1, 0, NULL,
715 ret_number, f_argc},
716 {"argidx", 0, 0, 0, NULL,
717 ret_number, f_argidx},
718 {"arglistid", 0, 2, 0, NULL,
719 ret_number, f_arglistid},
720 {"argv", 0, 2, 0, NULL,
721 ret_argv, f_argv},
722 {"asin", 1, 1, FEARG_1, NULL,
723 ret_float, FLOAT_FUNC(f_asin)},
724 {"assert_beeps", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100725 ret_number_bool, f_assert_beeps},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200726 {"assert_equal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100727 ret_number_bool, f_assert_equal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200728 {"assert_equalfile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100729 ret_number_bool, f_assert_equalfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200730 {"assert_exception", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100731 ret_number_bool, f_assert_exception},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200732 {"assert_fails", 1, 5, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100733 ret_number_bool, f_assert_fails},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200734 {"assert_false", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100735 ret_number_bool, f_assert_false},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200736 {"assert_inrange", 3, 4, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100737 ret_number_bool, f_assert_inrange},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200738 {"assert_match", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100739 ret_number_bool, f_assert_match},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200740 {"assert_notequal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100741 ret_number_bool, f_assert_notequal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200742 {"assert_notmatch", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100743 ret_number_bool, f_assert_notmatch},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200744 {"assert_report", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100745 ret_number_bool, f_assert_report},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200746 {"assert_true", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100747 ret_number_bool, f_assert_true},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200748 {"atan", 1, 1, FEARG_1, NULL,
749 ret_float, FLOAT_FUNC(f_atan)},
750 {"atan2", 2, 2, FEARG_1, NULL,
751 ret_float, FLOAT_FUNC(f_atan2)},
752 {"balloon_gettext", 0, 0, 0, NULL,
753 ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100754#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100755 f_balloon_gettext
756#else
757 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100758#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100759 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200760 {"balloon_show", 1, 1, FEARG_1, NULL,
761 ret_void,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100762#ifdef FEAT_BEVAL
763 f_balloon_show
764#else
765 NULL
766#endif
767 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200768 {"balloon_split", 1, 1, FEARG_1, NULL,
769 ret_list_string,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100770#if defined(FEAT_BEVAL_TERM)
771 f_balloon_split
772#else
773 NULL
774#endif
775 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200776 {"browse", 4, 4, 0, NULL,
777 ret_string, f_browse},
778 {"browsedir", 2, 2, 0, NULL,
779 ret_string, f_browsedir},
780 {"bufadd", 1, 1, FEARG_1, NULL,
781 ret_number, f_bufadd},
782 {"bufexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100783 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200784 {"buffer_exists", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100785 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200786 {"buffer_name", 0, 1, FEARG_1, NULL, // obsolete
787 ret_string, f_bufname},
788 {"buffer_number", 0, 1, FEARG_1, NULL, // obsolete
789 ret_number, f_bufnr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200790 {"buflisted", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100791 ret_number_bool, f_buflisted},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200792 {"bufload", 1, 1, FEARG_1, NULL,
793 ret_void, f_bufload},
794 {"bufloaded", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100795 ret_number_bool, f_bufloaded},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200796 {"bufname", 0, 1, FEARG_1, NULL,
797 ret_string, f_bufname},
798 {"bufnr", 0, 2, FEARG_1, NULL,
799 ret_number, f_bufnr},
800 {"bufwinid", 1, 1, FEARG_1, NULL,
801 ret_number, f_bufwinid},
802 {"bufwinnr", 1, 1, FEARG_1, NULL,
803 ret_number, f_bufwinnr},
804 {"byte2line", 1, 1, FEARG_1, NULL,
805 ret_number, f_byte2line},
806 {"byteidx", 2, 2, FEARG_1, NULL,
807 ret_number, f_byteidx},
808 {"byteidxcomp", 2, 2, FEARG_1, NULL,
809 ret_number, f_byteidxcomp},
810 {"call", 2, 3, FEARG_1, NULL,
811 ret_any, f_call},
812 {"ceil", 1, 1, FEARG_1, NULL,
813 ret_float, FLOAT_FUNC(f_ceil)},
814 {"ch_canread", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100815 ret_number_bool, JOB_FUNC(f_ch_canread)},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200816 {"ch_close", 1, 1, FEARG_1, NULL,
817 ret_void, JOB_FUNC(f_ch_close)},
818 {"ch_close_in", 1, 1, FEARG_1, NULL,
819 ret_void, JOB_FUNC(f_ch_close_in)},
820 {"ch_evalexpr", 2, 3, FEARG_1, NULL,
821 ret_any, JOB_FUNC(f_ch_evalexpr)},
822 {"ch_evalraw", 2, 3, FEARG_1, NULL,
823 ret_any, JOB_FUNC(f_ch_evalraw)},
824 {"ch_getbufnr", 2, 2, FEARG_1, NULL,
825 ret_number, JOB_FUNC(f_ch_getbufnr)},
826 {"ch_getjob", 1, 1, FEARG_1, NULL,
827 ret_job, JOB_FUNC(f_ch_getjob)},
828 {"ch_info", 1, 1, FEARG_1, NULL,
829 ret_dict_any, JOB_FUNC(f_ch_info)},
830 {"ch_log", 1, 2, FEARG_1, NULL,
831 ret_void, JOB_FUNC(f_ch_log)},
832 {"ch_logfile", 1, 2, FEARG_1, NULL,
833 ret_void, JOB_FUNC(f_ch_logfile)},
834 {"ch_open", 1, 2, FEARG_1, NULL,
835 ret_channel, JOB_FUNC(f_ch_open)},
836 {"ch_read", 1, 2, FEARG_1, NULL,
837 ret_string, JOB_FUNC(f_ch_read)},
838 {"ch_readblob", 1, 2, FEARG_1, NULL,
839 ret_blob, JOB_FUNC(f_ch_readblob)},
840 {"ch_readraw", 1, 2, FEARG_1, NULL,
841 ret_string, JOB_FUNC(f_ch_readraw)},
842 {"ch_sendexpr", 2, 3, FEARG_1, NULL,
843 ret_void, JOB_FUNC(f_ch_sendexpr)},
844 {"ch_sendraw", 2, 3, FEARG_1, NULL,
845 ret_void, JOB_FUNC(f_ch_sendraw)},
846 {"ch_setoptions", 2, 2, FEARG_1, NULL,
847 ret_void, JOB_FUNC(f_ch_setoptions)},
848 {"ch_status", 1, 2, FEARG_1, NULL,
849 ret_string, JOB_FUNC(f_ch_status)},
850 {"changenr", 0, 0, 0, NULL,
851 ret_number, f_changenr},
852 {"char2nr", 1, 2, FEARG_1, NULL,
853 ret_number, f_char2nr},
854 {"charclass", 1, 1, FEARG_1, NULL,
855 ret_number, f_charclass},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100856 {"charcol", 1, 1, FEARG_1, NULL,
857 ret_number, f_charcol},
Bram Moolenaar17793ef2020-12-28 12:56:58 +0100858 {"charidx", 2, 3, FEARG_1, NULL,
859 ret_number, f_charidx},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200860 {"chdir", 1, 1, FEARG_1, NULL,
861 ret_string, f_chdir},
862 {"cindent", 1, 1, FEARG_1, NULL,
863 ret_number, f_cindent},
864 {"clearmatches", 0, 1, FEARG_1, NULL,
865 ret_void, f_clearmatches},
866 {"col", 1, 1, FEARG_1, NULL,
867 ret_number, f_col},
868 {"complete", 2, 2, FEARG_2, NULL,
869 ret_void, f_complete},
870 {"complete_add", 1, 1, FEARG_1, NULL,
871 ret_number, f_complete_add},
872 {"complete_check", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100873 ret_number_bool, f_complete_check},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200874 {"complete_info", 0, 1, FEARG_1, NULL,
875 ret_dict_any, f_complete_info},
876 {"confirm", 1, 4, FEARG_1, NULL,
877 ret_number, f_confirm},
878 {"copy", 1, 1, FEARG_1, NULL,
879 ret_first_arg, f_copy},
880 {"cos", 1, 1, FEARG_1, NULL,
881 ret_float, FLOAT_FUNC(f_cos)},
882 {"cosh", 1, 1, FEARG_1, NULL,
883 ret_float, FLOAT_FUNC(f_cosh)},
884 {"count", 2, 4, FEARG_1, NULL,
885 ret_number, f_count},
886 {"cscope_connection",0,3, 0, NULL,
887 ret_number, f_cscope_connection},
888 {"cursor", 1, 3, FEARG_1, NULL,
889 ret_number, f_cursor},
890 {"debugbreak", 1, 1, FEARG_1, NULL,
891 ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100892#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100893 f_debugbreak
894#else
895 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200896#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100897 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200898 {"deepcopy", 1, 2, FEARG_1, NULL,
899 ret_first_arg, f_deepcopy},
900 {"delete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100901 ret_number_bool, f_delete},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200902 {"deletebufline", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100903 ret_number_bool, f_deletebufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200904 {"did_filetype", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100905 ret_number_bool, f_did_filetype},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200906 {"diff_filler", 1, 1, FEARG_1, NULL,
907 ret_number, f_diff_filler},
908 {"diff_hlID", 2, 2, FEARG_1, NULL,
909 ret_number, f_diff_hlID},
910 {"echoraw", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100911 ret_void, f_echoraw},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200912 {"empty", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100913 ret_number_bool, f_empty},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200914 {"environ", 0, 0, 0, NULL,
915 ret_dict_string, f_environ},
916 {"escape", 2, 2, FEARG_1, NULL,
917 ret_string, f_escape},
918 {"eval", 1, 1, FEARG_1, NULL,
919 ret_any, f_eval},
920 {"eventhandler", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100921 ret_number_bool, f_eventhandler},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200922 {"executable", 1, 1, FEARG_1, NULL,
923 ret_number, f_executable},
924 {"execute", 1, 2, FEARG_1, NULL,
925 ret_string, f_execute},
926 {"exepath", 1, 1, FEARG_1, NULL,
927 ret_string, f_exepath},
928 {"exists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100929 ret_number_bool, f_exists},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200930 {"exp", 1, 1, FEARG_1, NULL,
931 ret_float, FLOAT_FUNC(f_exp)},
932 {"expand", 1, 3, FEARG_1, NULL,
933 ret_any, f_expand},
934 {"expandcmd", 1, 1, FEARG_1, NULL,
935 ret_string, f_expandcmd},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100936 {"extend", 2, 3, FEARG_1, arg23_extend,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200937 ret_first_arg, f_extend},
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100938 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
939 ret_first_cont, f_extendnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200940 {"feedkeys", 1, 2, FEARG_1, NULL,
941 ret_void, f_feedkeys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200942 {"file_readable", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100943 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200944 {"filereadable", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100945 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200946 {"filewritable", 1, 1, FEARG_1, NULL,
947 ret_number, f_filewritable},
948 {"filter", 2, 2, FEARG_1, NULL,
949 ret_first_arg, f_filter},
950 {"finddir", 1, 3, FEARG_1, NULL,
951 ret_string, f_finddir},
952 {"findfile", 1, 3, FEARG_1, NULL,
953 ret_string, f_findfile},
954 {"flatten", 1, 2, FEARG_1, NULL,
955 ret_list_any, f_flatten},
956 {"float2nr", 1, 1, FEARG_1, NULL,
957 ret_number, FLOAT_FUNC(f_float2nr)},
958 {"floor", 1, 1, FEARG_1, NULL,
959 ret_float, FLOAT_FUNC(f_floor)},
960 {"fmod", 2, 2, FEARG_1, NULL,
961 ret_float, FLOAT_FUNC(f_fmod)},
962 {"fnameescape", 1, 1, FEARG_1, NULL,
963 ret_string, f_fnameescape},
964 {"fnamemodify", 2, 2, FEARG_1, NULL,
965 ret_string, f_fnamemodify},
966 {"foldclosed", 1, 1, FEARG_1, NULL,
967 ret_number, f_foldclosed},
968 {"foldclosedend", 1, 1, FEARG_1, NULL,
969 ret_number, f_foldclosedend},
970 {"foldlevel", 1, 1, FEARG_1, NULL,
971 ret_number, f_foldlevel},
972 {"foldtext", 0, 0, 0, NULL,
973 ret_string, f_foldtext},
974 {"foldtextresult", 1, 1, FEARG_1, NULL,
975 ret_string, f_foldtextresult},
976 {"foreground", 0, 0, 0, NULL,
977 ret_void, f_foreground},
978 {"funcref", 1, 3, FEARG_1, NULL,
979 ret_func_any, f_funcref},
980 {"function", 1, 3, FEARG_1, NULL,
981 ret_f_function, f_function},
982 {"garbagecollect", 0, 1, 0, NULL,
983 ret_void, f_garbagecollect},
984 {"get", 2, 3, FEARG_1, NULL,
985 ret_any, f_get},
986 {"getbufinfo", 0, 1, FEARG_1, NULL,
987 ret_list_dict_any, f_getbufinfo},
988 {"getbufline", 2, 3, FEARG_1, NULL,
989 ret_list_string, f_getbufline},
990 {"getbufvar", 2, 3, FEARG_1, NULL,
991 ret_any, f_getbufvar},
992 {"getchangelist", 0, 1, FEARG_1, NULL,
993 ret_list_any, f_getchangelist},
994 {"getchar", 0, 1, 0, NULL,
995 ret_number, f_getchar},
996 {"getcharmod", 0, 0, 0, NULL,
997 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100998 {"getcharpos", 1, 1, FEARG_1, NULL,
999 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001000 {"getcharsearch", 0, 0, 0, NULL,
1001 ret_dict_any, f_getcharsearch},
1002 {"getcmdline", 0, 0, 0, NULL,
1003 ret_string, f_getcmdline},
1004 {"getcmdpos", 0, 0, 0, NULL,
1005 ret_number, f_getcmdpos},
1006 {"getcmdtype", 0, 0, 0, NULL,
1007 ret_string, f_getcmdtype},
1008 {"getcmdwintype", 0, 0, 0, NULL,
1009 ret_string, f_getcmdwintype},
1010 {"getcompletion", 2, 3, FEARG_1, NULL,
1011 ret_list_string, f_getcompletion},
1012 {"getcurpos", 0, 1, FEARG_1, NULL,
1013 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001014 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
1015 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001016 {"getcwd", 0, 2, FEARG_1, NULL,
1017 ret_string, f_getcwd},
1018 {"getenv", 1, 1, FEARG_1, NULL,
1019 ret_string, f_getenv},
1020 {"getfontname", 0, 1, 0, NULL,
1021 ret_string, f_getfontname},
1022 {"getfperm", 1, 1, FEARG_1, NULL,
1023 ret_string, f_getfperm},
1024 {"getfsize", 1, 1, FEARG_1, NULL,
1025 ret_number, f_getfsize},
1026 {"getftime", 1, 1, FEARG_1, NULL,
1027 ret_number, f_getftime},
1028 {"getftype", 1, 1, FEARG_1, NULL,
1029 ret_string, f_getftype},
1030 {"getimstatus", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001031 ret_number_bool, f_getimstatus},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001032 {"getjumplist", 0, 2, FEARG_1, NULL,
1033 ret_list_any, f_getjumplist},
1034 {"getline", 1, 2, FEARG_1, NULL,
1035 ret_f_getline, f_getline},
1036 {"getloclist", 1, 2, 0, NULL,
1037 ret_list_or_dict_1, f_getloclist},
1038 {"getmarklist", 0, 1, FEARG_1, NULL,
1039 ret_list_dict_any, f_getmarklist},
1040 {"getmatches", 0, 1, 0, NULL,
1041 ret_list_dict_any, f_getmatches},
1042 {"getmousepos", 0, 0, 0, NULL,
1043 ret_dict_number, f_getmousepos},
1044 {"getpid", 0, 0, 0, NULL,
1045 ret_number, f_getpid},
1046 {"getpos", 1, 1, FEARG_1, NULL,
1047 ret_list_number, f_getpos},
1048 {"getqflist", 0, 1, 0, NULL,
1049 ret_list_or_dict_0, f_getqflist},
1050 {"getreg", 0, 3, FEARG_1, NULL,
1051 ret_getreg, f_getreg},
1052 {"getreginfo", 0, 1, FEARG_1, NULL,
1053 ret_dict_any, f_getreginfo},
1054 {"getregtype", 0, 1, FEARG_1, NULL,
1055 ret_string, f_getregtype},
1056 {"gettabinfo", 0, 1, FEARG_1, NULL,
1057 ret_list_dict_any, f_gettabinfo},
1058 {"gettabvar", 2, 3, FEARG_1, NULL,
1059 ret_any, f_gettabvar},
1060 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1061 ret_any, f_gettabwinvar},
1062 {"gettagstack", 0, 1, FEARG_1, NULL,
1063 ret_dict_any, f_gettagstack},
1064 {"gettext", 1, 1, FEARG_1, NULL,
1065 ret_string, f_gettext},
1066 {"getwininfo", 0, 1, FEARG_1, NULL,
1067 ret_list_dict_any, f_getwininfo},
1068 {"getwinpos", 0, 1, FEARG_1, NULL,
1069 ret_list_number, f_getwinpos},
1070 {"getwinposx", 0, 0, 0, NULL,
1071 ret_number, f_getwinposx},
1072 {"getwinposy", 0, 0, 0, NULL,
1073 ret_number, f_getwinposy},
1074 {"getwinvar", 2, 3, FEARG_1, NULL,
1075 ret_any, f_getwinvar},
1076 {"glob", 1, 4, FEARG_1, NULL,
1077 ret_any, f_glob},
1078 {"glob2regpat", 1, 1, FEARG_1, NULL,
1079 ret_string, f_glob2regpat},
1080 {"globpath", 2, 5, FEARG_2, NULL,
1081 ret_any, f_globpath},
1082 {"has", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001083 ret_number_bool, f_has},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001084 {"has_key", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001085 ret_number_bool, f_has_key},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001086 {"haslocaldir", 0, 2, FEARG_1, NULL,
1087 ret_number, f_haslocaldir},
1088 {"hasmapto", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001089 ret_number_bool, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001090 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1091 ret_number, f_hlID},
1092 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001093 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001094 {"histadd", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001095 ret_number_bool, f_histadd},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001096 {"histdel", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001097 ret_number_bool, f_histdel},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001098 {"histget", 1, 2, FEARG_1, NULL,
1099 ret_string, f_histget},
1100 {"histnr", 1, 1, FEARG_1, NULL,
1101 ret_number, f_histnr},
1102 {"hlID", 1, 1, FEARG_1, NULL,
1103 ret_number, f_hlID},
1104 {"hlexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001105 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001106 {"hostname", 0, 0, 0, NULL,
1107 ret_string, f_hostname},
1108 {"iconv", 3, 3, FEARG_1, NULL,
1109 ret_string, f_iconv},
1110 {"indent", 1, 1, FEARG_1, NULL,
1111 ret_number, f_indent},
1112 {"index", 2, 4, FEARG_1, NULL,
1113 ret_number, f_index},
1114 {"input", 1, 3, FEARG_1, NULL,
1115 ret_string, f_input},
1116 {"inputdialog", 1, 3, FEARG_1, NULL,
1117 ret_string, f_inputdialog},
1118 {"inputlist", 1, 1, FEARG_1, NULL,
1119 ret_number, f_inputlist},
1120 {"inputrestore", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001121 ret_number_bool, f_inputrestore},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001122 {"inputsave", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001123 ret_number_bool, f_inputsave},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001124 {"inputsecret", 1, 2, FEARG_1, NULL,
1125 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001126 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001127 ret_first_arg, f_insert},
1128 {"interrupt", 0, 0, 0, NULL,
1129 ret_void, f_interrupt},
1130 {"invert", 1, 1, FEARG_1, NULL,
1131 ret_number, f_invert},
1132 {"isdirectory", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001133 ret_number_bool, f_isdirectory},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001134 {"isinf", 1, 1, FEARG_1, NULL,
1135 ret_number, MATH_FUNC(f_isinf)},
1136 {"islocked", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001137 ret_number_bool, f_islocked},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001138 {"isnan", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001139 ret_number_bool, MATH_FUNC(f_isnan)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001140 {"items", 1, 1, FEARG_1, NULL,
1141 ret_list_any, f_items},
1142 {"job_getchannel", 1, 1, FEARG_1, NULL,
1143 ret_channel, JOB_FUNC(f_job_getchannel)},
1144 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001145 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001146 {"job_setoptions", 2, 2, FEARG_1, NULL,
1147 ret_void, JOB_FUNC(f_job_setoptions)},
1148 {"job_start", 1, 2, FEARG_1, NULL,
1149 ret_job, JOB_FUNC(f_job_start)},
1150 {"job_status", 1, 1, FEARG_1, NULL,
1151 ret_string, JOB_FUNC(f_job_status)},
1152 {"job_stop", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001153 ret_number_bool, JOB_FUNC(f_job_stop)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001154 {"join", 1, 2, FEARG_1, NULL,
1155 ret_string, f_join},
1156 {"js_decode", 1, 1, FEARG_1, NULL,
1157 ret_any, f_js_decode},
1158 {"js_encode", 1, 1, FEARG_1, NULL,
1159 ret_string, f_js_encode},
1160 {"json_decode", 1, 1, FEARG_1, NULL,
1161 ret_any, f_json_decode},
1162 {"json_encode", 1, 1, FEARG_1, NULL,
1163 ret_string, f_json_encode},
1164 {"keys", 1, 1, FEARG_1, NULL,
1165 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001166 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1167 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001168 {"len", 1, 1, FEARG_1, NULL,
1169 ret_number, f_len},
1170 {"libcall", 3, 3, FEARG_3, NULL,
1171 ret_string, f_libcall},
1172 {"libcallnr", 3, 3, FEARG_3, NULL,
1173 ret_number, f_libcallnr},
1174 {"line", 1, 2, FEARG_1, NULL,
1175 ret_number, f_line},
1176 {"line2byte", 1, 1, FEARG_1, NULL,
1177 ret_number, f_line2byte},
1178 {"lispindent", 1, 1, FEARG_1, NULL,
1179 ret_number, f_lispindent},
1180 {"list2str", 1, 2, FEARG_1, NULL,
1181 ret_string, f_list2str},
1182 {"listener_add", 1, 2, FEARG_2, NULL,
1183 ret_number, f_listener_add},
1184 {"listener_flush", 0, 1, FEARG_1, NULL,
1185 ret_void, f_listener_flush},
1186 {"listener_remove", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001187 ret_number_bool, f_listener_remove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001188 {"localtime", 0, 0, 0, NULL,
1189 ret_number, f_localtime},
1190 {"log", 1, 1, FEARG_1, NULL,
1191 ret_float, FLOAT_FUNC(f_log)},
1192 {"log10", 1, 1, FEARG_1, NULL,
1193 ret_float, FLOAT_FUNC(f_log10)},
1194 {"luaeval", 1, 2, FEARG_1, NULL,
1195 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001196#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001197 f_luaeval
1198#else
1199 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001200#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001201 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001202 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001203 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001204 {"maparg", 1, 4, FEARG_1, NULL,
1205 ret_maparg, f_maparg},
1206 {"mapcheck", 1, 3, FEARG_1, NULL,
1207 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001208 {"mapnew", 2, 2, FEARG_1, NULL,
1209 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001210 {"mapset", 3, 3, FEARG_1, NULL,
1211 ret_void, f_mapset},
1212 {"match", 2, 4, FEARG_1, NULL,
1213 ret_any, f_match},
1214 {"matchadd", 2, 5, FEARG_1, NULL,
1215 ret_number, f_matchadd},
1216 {"matchaddpos", 2, 5, FEARG_1, NULL,
1217 ret_number, f_matchaddpos},
1218 {"matcharg", 1, 1, FEARG_1, NULL,
1219 ret_list_string, f_matcharg},
1220 {"matchdelete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001221 ret_number_bool, f_matchdelete},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001222 {"matchend", 2, 4, FEARG_1, NULL,
1223 ret_number, f_matchend},
1224 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1225 ret_list_string, f_matchfuzzy},
1226 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1227 ret_list_any, f_matchfuzzypos},
1228 {"matchlist", 2, 4, FEARG_1, NULL,
1229 ret_list_string, f_matchlist},
1230 {"matchstr", 2, 4, FEARG_1, NULL,
1231 ret_string, f_matchstr},
1232 {"matchstrpos", 2, 4, FEARG_1, NULL,
1233 ret_list_any, f_matchstrpos},
1234 {"max", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001235 ret_number, f_max},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001236 {"menu_info", 1, 2, FEARG_1, NULL,
1237 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001238#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001239 f_menu_info
1240#else
1241 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001242#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001243 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001244 {"min", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001245 ret_number, f_min},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001246 {"mkdir", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001247 ret_number_bool, f_mkdir},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001248 {"mode", 0, 1, FEARG_1, NULL,
1249 ret_string, f_mode},
1250 {"mzeval", 1, 1, FEARG_1, NULL,
1251 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001252#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001253 f_mzeval
1254#else
1255 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001256#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001257 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001258 {"nextnonblank", 1, 1, FEARG_1, NULL,
1259 ret_number, f_nextnonblank},
1260 {"nr2char", 1, 2, FEARG_1, NULL,
1261 ret_string, f_nr2char},
1262 {"or", 2, 2, FEARG_1, NULL,
1263 ret_number, f_or},
1264 {"pathshorten", 1, 2, FEARG_1, NULL,
1265 ret_string, f_pathshorten},
1266 {"perleval", 1, 1, FEARG_1, NULL,
1267 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001268#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001269 f_perleval
1270#else
1271 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001272#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001273 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001274 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1275 ret_number, PROP_FUNC(f_popup_atcursor)},
1276 {"popup_beval", 2, 2, FEARG_1, NULL,
1277 ret_number, PROP_FUNC(f_popup_beval)},
1278 {"popup_clear", 0, 1, 0, NULL,
1279 ret_void, PROP_FUNC(f_popup_clear)},
1280 {"popup_close", 1, 2, FEARG_1, NULL,
1281 ret_void, PROP_FUNC(f_popup_close)},
1282 {"popup_create", 2, 2, FEARG_1, NULL,
1283 ret_number, PROP_FUNC(f_popup_create)},
1284 {"popup_dialog", 2, 2, FEARG_1, NULL,
1285 ret_number, PROP_FUNC(f_popup_dialog)},
1286 {"popup_filter_menu", 2, 2, 0, NULL,
1287 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1288 {"popup_filter_yesno", 2, 2, 0, NULL,
1289 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1290 {"popup_findinfo", 0, 0, 0, NULL,
1291 ret_number, PROP_FUNC(f_popup_findinfo)},
1292 {"popup_findpreview", 0, 0, 0, NULL,
1293 ret_number, PROP_FUNC(f_popup_findpreview)},
1294 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1295 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1296 {"popup_getpos", 1, 1, FEARG_1, NULL,
1297 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1298 {"popup_hide", 1, 1, FEARG_1, NULL,
1299 ret_void, PROP_FUNC(f_popup_hide)},
1300 {"popup_list", 0, 0, 0, NULL,
1301 ret_list_number, PROP_FUNC(f_popup_list)},
1302 {"popup_locate", 2, 2, 0, NULL,
1303 ret_number, PROP_FUNC(f_popup_locate)},
1304 {"popup_menu", 2, 2, FEARG_1, NULL,
1305 ret_number, PROP_FUNC(f_popup_menu)},
1306 {"popup_move", 2, 2, FEARG_1, NULL,
1307 ret_void, PROP_FUNC(f_popup_move)},
1308 {"popup_notification", 2, 2, FEARG_1, NULL,
1309 ret_number, PROP_FUNC(f_popup_notification)},
1310 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1311 ret_void, PROP_FUNC(f_popup_setoptions)},
1312 {"popup_settext", 2, 2, FEARG_1, NULL,
1313 ret_void, PROP_FUNC(f_popup_settext)},
1314 {"popup_show", 1, 1, FEARG_1, NULL,
1315 ret_void, PROP_FUNC(f_popup_show)},
1316 {"pow", 2, 2, FEARG_1, NULL,
1317 ret_float, FLOAT_FUNC(f_pow)},
1318 {"prevnonblank", 1, 1, FEARG_1, NULL,
1319 ret_number, f_prevnonblank},
1320 {"printf", 1, 19, FEARG_2, NULL,
1321 ret_string, f_printf},
1322 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1323 ret_string, JOB_FUNC(f_prompt_getprompt)},
1324 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1325 ret_void, JOB_FUNC(f_prompt_setcallback)},
1326 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1327 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1328 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1329 ret_void, JOB_FUNC(f_prompt_setprompt)},
1330 {"prop_add", 3, 3, FEARG_1, NULL,
1331 ret_void, PROP_FUNC(f_prop_add)},
1332 {"prop_clear", 1, 3, FEARG_1, NULL,
1333 ret_void, PROP_FUNC(f_prop_clear)},
1334 {"prop_find", 1, 2, FEARG_1, NULL,
1335 ret_dict_any, PROP_FUNC(f_prop_find)},
1336 {"prop_list", 1, 2, FEARG_1, NULL,
1337 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1338 {"prop_remove", 1, 3, FEARG_1, NULL,
1339 ret_number, PROP_FUNC(f_prop_remove)},
1340 {"prop_type_add", 2, 2, FEARG_1, NULL,
1341 ret_void, PROP_FUNC(f_prop_type_add)},
1342 {"prop_type_change", 2, 2, FEARG_1, NULL,
1343 ret_void, PROP_FUNC(f_prop_type_change)},
1344 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1345 ret_void, PROP_FUNC(f_prop_type_delete)},
1346 {"prop_type_get", 1, 2, FEARG_1, NULL,
1347 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1348 {"prop_type_list", 0, 1, FEARG_1, NULL,
1349 ret_list_string, PROP_FUNC(f_prop_type_list)},
1350 {"pum_getpos", 0, 0, 0, NULL,
1351 ret_dict_number, f_pum_getpos},
1352 {"pumvisible", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001353 ret_number_bool, f_pumvisible},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001354 {"py3eval", 1, 1, FEARG_1, NULL,
1355 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001356#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001357 f_py3eval
1358#else
1359 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001360#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001361 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001362 {"pyeval", 1, 1, FEARG_1, NULL,
1363 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001364#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001365 f_pyeval
1366#else
1367 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001368#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001369 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001370 {"pyxeval", 1, 1, FEARG_1, NULL,
1371 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001372#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001373 f_pyxeval
1374#else
1375 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001376#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001377 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001378 {"rand", 0, 1, FEARG_1, NULL,
1379 ret_number, f_rand},
1380 {"range", 1, 3, FEARG_1, NULL,
1381 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001382 {"readblob", 1, 1, FEARG_1, NULL,
1383 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001384 {"readdir", 1, 3, FEARG_1, NULL,
1385 ret_list_string, f_readdir},
1386 {"readdirex", 1, 3, FEARG_1, NULL,
1387 ret_list_dict_any, f_readdirex},
1388 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001389 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001390 {"reduce", 2, 3, FEARG_1, NULL,
1391 ret_any, f_reduce},
1392 {"reg_executing", 0, 0, 0, NULL,
1393 ret_string, f_reg_executing},
1394 {"reg_recording", 0, 0, 0, NULL,
1395 ret_string, f_reg_recording},
1396 {"reltime", 0, 2, FEARG_1, NULL,
1397 ret_list_any, f_reltime},
1398 {"reltimefloat", 1, 1, FEARG_1, NULL,
1399 ret_float, FLOAT_FUNC(f_reltimefloat)},
1400 {"reltimestr", 1, 1, FEARG_1, NULL,
1401 ret_string, f_reltimestr},
1402 {"remote_expr", 2, 4, FEARG_1, NULL,
1403 ret_string, f_remote_expr},
1404 {"remote_foreground", 1, 1, FEARG_1, NULL,
1405 ret_string, f_remote_foreground},
1406 {"remote_peek", 1, 2, FEARG_1, NULL,
1407 ret_number, f_remote_peek},
1408 {"remote_read", 1, 2, FEARG_1, NULL,
1409 ret_string, f_remote_read},
1410 {"remote_send", 2, 3, FEARG_1, NULL,
1411 ret_string, f_remote_send},
1412 {"remote_startserver", 1, 1, FEARG_1, NULL,
1413 ret_void, f_remote_startserver},
1414 {"remove", 2, 3, FEARG_1, NULL,
1415 ret_remove, f_remove},
1416 {"rename", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001417 ret_number_bool, f_rename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001418 {"repeat", 2, 2, FEARG_1, NULL,
1419 ret_first_arg, f_repeat},
1420 {"resolve", 1, 1, FEARG_1, NULL,
1421 ret_string, f_resolve},
1422 {"reverse", 1, 1, FEARG_1, NULL,
1423 ret_first_arg, f_reverse},
1424 {"round", 1, 1, FEARG_1, NULL,
1425 ret_float, FLOAT_FUNC(f_round)},
1426 {"rubyeval", 1, 1, FEARG_1, NULL,
1427 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001428#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001429 f_rubyeval
1430#else
1431 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001432#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001433 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001434 {"screenattr", 2, 2, FEARG_1, NULL,
1435 ret_number, f_screenattr},
1436 {"screenchar", 2, 2, FEARG_1, NULL,
1437 ret_number, f_screenchar},
1438 {"screenchars", 2, 2, FEARG_1, NULL,
1439 ret_list_number, f_screenchars},
1440 {"screencol", 0, 0, 0, NULL,
1441 ret_number, f_screencol},
1442 {"screenpos", 3, 3, FEARG_1, NULL,
1443 ret_dict_number, f_screenpos},
1444 {"screenrow", 0, 0, 0, NULL,
1445 ret_number, f_screenrow},
1446 {"screenstring", 2, 2, FEARG_1, NULL,
1447 ret_string, f_screenstring},
1448 {"search", 1, 5, FEARG_1, NULL,
1449 ret_number, f_search},
1450 {"searchcount", 0, 1, FEARG_1, NULL,
1451 ret_dict_any, f_searchcount},
1452 {"searchdecl", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001453 ret_number_bool, f_searchdecl},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001454 {"searchpair", 3, 7, 0, NULL,
1455 ret_number, f_searchpair},
1456 {"searchpairpos", 3, 7, 0, NULL,
1457 ret_list_number, f_searchpairpos},
1458 {"searchpos", 1, 5, FEARG_1, NULL,
1459 ret_list_number, f_searchpos},
1460 {"server2client", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001461 ret_number_bool, f_server2client},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001462 {"serverlist", 0, 0, 0, NULL,
1463 ret_string, f_serverlist},
1464 {"setbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001465 ret_number_bool, f_setbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001466 {"setbufvar", 3, 3, FEARG_3, NULL,
1467 ret_void, f_setbufvar},
1468 {"setcellwidths", 1, 1, FEARG_1, NULL,
1469 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001470 {"setcharpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001471 ret_number_bool, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001472 {"setcharsearch", 1, 1, FEARG_1, NULL,
1473 ret_void, f_setcharsearch},
1474 {"setcmdpos", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001475 ret_number_bool, f_setcmdpos},
1476 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1477 ret_number_bool, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001478 {"setenv", 2, 2, FEARG_2, NULL,
1479 ret_void, f_setenv},
1480 {"setfperm", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001481 ret_number_bool, f_setfperm},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001482 {"setline", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001483 ret_number_bool, f_setline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001484 {"setloclist", 2, 4, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001485 ret_number_bool, f_setloclist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001486 {"setmatches", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001487 ret_number_bool, f_setmatches},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001488 {"setpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001489 ret_number_bool, f_setpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001490 {"setqflist", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001491 ret_number_bool, f_setqflist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001492 {"setreg", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001493 ret_number_bool, f_setreg},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001494 {"settabvar", 3, 3, FEARG_3, NULL,
1495 ret_void, f_settabvar},
1496 {"settabwinvar", 4, 4, FEARG_4, NULL,
1497 ret_void, f_settabwinvar},
1498 {"settagstack", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001499 ret_number_bool, f_settagstack},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001500 {"setwinvar", 3, 3, FEARG_3, NULL,
1501 ret_void, f_setwinvar},
1502 {"sha256", 1, 1, FEARG_1, NULL,
1503 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001504#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001505 f_sha256
1506#else
1507 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001508#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001509 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001510 {"shellescape", 1, 2, FEARG_1, NULL,
1511 ret_string, f_shellescape},
1512 {"shiftwidth", 0, 1, FEARG_1, NULL,
1513 ret_number, f_shiftwidth},
1514 {"sign_define", 1, 2, FEARG_1, NULL,
1515 ret_any, SIGN_FUNC(f_sign_define)},
1516 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1517 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1518 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1519 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1520 {"sign_jump", 3, 3, FEARG_1, NULL,
1521 ret_number, SIGN_FUNC(f_sign_jump)},
1522 {"sign_place", 4, 5, FEARG_1, NULL,
1523 ret_number, SIGN_FUNC(f_sign_place)},
1524 {"sign_placelist", 1, 1, FEARG_1, NULL,
1525 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1526 {"sign_undefine", 0, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001527 ret_number_bool, SIGN_FUNC(f_sign_undefine)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001528 {"sign_unplace", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001529 ret_number_bool, SIGN_FUNC(f_sign_unplace)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001530 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1531 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1532 {"simplify", 1, 1, FEARG_1, NULL,
1533 ret_string, f_simplify},
1534 {"sin", 1, 1, FEARG_1, NULL,
1535 ret_float, FLOAT_FUNC(f_sin)},
1536 {"sinh", 1, 1, FEARG_1, NULL,
1537 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001538 {"slice", 2, 3, FEARG_1, NULL,
1539 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001540 {"sort", 1, 3, FEARG_1, NULL,
1541 ret_first_arg, f_sort},
1542 {"sound_clear", 0, 0, 0, NULL,
1543 ret_void, SOUND_FUNC(f_sound_clear)},
1544 {"sound_playevent", 1, 2, FEARG_1, NULL,
1545 ret_number, SOUND_FUNC(f_sound_playevent)},
1546 {"sound_playfile", 1, 2, FEARG_1, NULL,
1547 ret_number, SOUND_FUNC(f_sound_playfile)},
1548 {"sound_stop", 1, 1, FEARG_1, NULL,
1549 ret_void, SOUND_FUNC(f_sound_stop)},
1550 {"soundfold", 1, 1, FEARG_1, NULL,
1551 ret_string, f_soundfold},
1552 {"spellbadword", 0, 1, FEARG_1, NULL,
1553 ret_list_string, f_spellbadword},
1554 {"spellsuggest", 1, 3, FEARG_1, NULL,
1555 ret_list_string, f_spellsuggest},
1556 {"split", 1, 3, FEARG_1, NULL,
1557 ret_list_string, f_split},
1558 {"sqrt", 1, 1, FEARG_1, NULL,
1559 ret_float, FLOAT_FUNC(f_sqrt)},
1560 {"srand", 0, 1, FEARG_1, NULL,
1561 ret_list_number, f_srand},
1562 {"state", 0, 1, FEARG_1, NULL,
1563 ret_string, f_state},
1564 {"str2float", 1, 1, FEARG_1, NULL,
1565 ret_float, FLOAT_FUNC(f_str2float)},
1566 {"str2list", 1, 2, FEARG_1, NULL,
1567 ret_list_number, f_str2list},
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001568 {"str2nr", 1, 3, FEARG_1, arg3_string_nr_bool,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001569 ret_number, f_str2nr},
1570 {"strcharpart", 2, 3, FEARG_1, NULL,
1571 ret_string, f_strcharpart},
1572 {"strchars", 1, 2, FEARG_1, NULL,
1573 ret_number, f_strchars},
1574 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1575 ret_number, f_strdisplaywidth},
1576 {"strftime", 1, 2, FEARG_1, NULL,
1577 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001578#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001579 f_strftime
1580#else
1581 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001582#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001583 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001584 {"strgetchar", 2, 2, FEARG_1, NULL,
1585 ret_number, f_strgetchar},
1586 {"stridx", 2, 3, FEARG_1, NULL,
1587 ret_number, f_stridx},
1588 {"string", 1, 1, FEARG_1, NULL,
1589 ret_string, f_string},
1590 {"strlen", 1, 1, FEARG_1, NULL,
1591 ret_number, f_strlen},
1592 {"strpart", 2, 4, FEARG_1, NULL,
1593 ret_string, f_strpart},
1594 {"strptime", 2, 2, FEARG_1, NULL,
1595 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001596#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001597 f_strptime
1598#else
1599 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001600#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001601 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001602 {"strridx", 2, 3, FEARG_1, NULL,
1603 ret_number, f_strridx},
1604 {"strtrans", 1, 1, FEARG_1, NULL,
1605 ret_string, f_strtrans},
1606 {"strwidth", 1, 1, FEARG_1, NULL,
1607 ret_number, f_strwidth},
1608 {"submatch", 1, 2, FEARG_1, NULL,
1609 ret_string, f_submatch},
1610 {"substitute", 4, 4, FEARG_1, NULL,
1611 ret_string, f_substitute},
1612 {"swapinfo", 1, 1, FEARG_1, NULL,
1613 ret_dict_any, f_swapinfo},
1614 {"swapname", 1, 1, FEARG_1, NULL,
1615 ret_string, f_swapname},
1616 {"synID", 3, 3, 0, NULL,
1617 ret_number, f_synID},
1618 {"synIDattr", 2, 3, FEARG_1, NULL,
1619 ret_string, f_synIDattr},
1620 {"synIDtrans", 1, 1, FEARG_1, NULL,
1621 ret_number, f_synIDtrans},
1622 {"synconcealed", 2, 2, 0, NULL,
1623 ret_list_any, f_synconcealed},
1624 {"synstack", 2, 2, 0, NULL,
1625 ret_list_number, f_synstack},
1626 {"system", 1, 2, FEARG_1, NULL,
1627 ret_string, f_system},
1628 {"systemlist", 1, 2, FEARG_1, NULL,
1629 ret_list_string, f_systemlist},
1630 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1631 ret_list_number, f_tabpagebuflist},
1632 {"tabpagenr", 0, 1, 0, NULL,
1633 ret_number, f_tabpagenr},
1634 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1635 ret_number, f_tabpagewinnr},
1636 {"tagfiles", 0, 0, 0, NULL,
1637 ret_list_string, f_tagfiles},
1638 {"taglist", 1, 2, FEARG_1, NULL,
1639 ret_list_dict_any, f_taglist},
1640 {"tan", 1, 1, FEARG_1, NULL,
1641 ret_float, FLOAT_FUNC(f_tan)},
1642 {"tanh", 1, 1, FEARG_1, NULL,
1643 ret_float, FLOAT_FUNC(f_tanh)},
1644 {"tempname", 0, 0, 0, NULL,
1645 ret_string, f_tempname},
1646 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1647 ret_number, TERM_FUNC(f_term_dumpdiff)},
1648 {"term_dumpload", 1, 2, FEARG_1, NULL,
1649 ret_number, TERM_FUNC(f_term_dumpload)},
1650 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1651 ret_void, TERM_FUNC(f_term_dumpwrite)},
1652 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1653 ret_number, TERM_FUNC(f_term_getaltscreen)},
1654 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1655 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001656#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001657 f_term_getansicolors
1658#else
1659 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001660#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001661 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001662 {"term_getattr", 2, 2, FEARG_1, NULL,
1663 ret_number, TERM_FUNC(f_term_getattr)},
1664 {"term_getcursor", 1, 1, FEARG_1, NULL,
1665 ret_list_any, TERM_FUNC(f_term_getcursor)},
1666 {"term_getjob", 1, 1, FEARG_1, NULL,
1667 ret_job, TERM_FUNC(f_term_getjob)},
1668 {"term_getline", 2, 2, FEARG_1, NULL,
1669 ret_string, TERM_FUNC(f_term_getline)},
1670 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1671 ret_number, TERM_FUNC(f_term_getscrolled)},
1672 {"term_getsize", 1, 1, FEARG_1, NULL,
1673 ret_list_number, TERM_FUNC(f_term_getsize)},
1674 {"term_getstatus", 1, 1, FEARG_1, NULL,
1675 ret_string, TERM_FUNC(f_term_getstatus)},
1676 {"term_gettitle", 1, 1, FEARG_1, NULL,
1677 ret_string, TERM_FUNC(f_term_gettitle)},
1678 {"term_gettty", 1, 2, FEARG_1, NULL,
1679 ret_string, TERM_FUNC(f_term_gettty)},
1680 {"term_list", 0, 0, 0, NULL,
1681 ret_list_number, TERM_FUNC(f_term_list)},
1682 {"term_scrape", 2, 2, FEARG_1, NULL,
1683 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1684 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1685 ret_void, TERM_FUNC(f_term_sendkeys)},
1686 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1687 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001688#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001689 f_term_setansicolors
1690#else
1691 NULL
1692#endif
1693 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001694 {"term_setapi", 2, 2, FEARG_1, NULL,
1695 ret_void, TERM_FUNC(f_term_setapi)},
1696 {"term_setkill", 2, 2, FEARG_1, NULL,
1697 ret_void, TERM_FUNC(f_term_setkill)},
1698 {"term_setrestore", 2, 2, FEARG_1, NULL,
1699 ret_void, TERM_FUNC(f_term_setrestore)},
1700 {"term_setsize", 3, 3, FEARG_1, NULL,
1701 ret_void, TERM_FUNC(f_term_setsize)},
1702 {"term_start", 1, 2, FEARG_1, NULL,
1703 ret_number, TERM_FUNC(f_term_start)},
1704 {"term_wait", 1, 2, FEARG_1, NULL,
1705 ret_void, TERM_FUNC(f_term_wait)},
1706 {"terminalprops", 0, 0, 0, NULL,
1707 ret_dict_string, f_terminalprops},
1708 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1709 ret_void, f_test_alloc_fail},
1710 {"test_autochdir", 0, 0, 0, NULL,
1711 ret_void, f_test_autochdir},
1712 {"test_feedinput", 1, 1, FEARG_1, NULL,
1713 ret_void, f_test_feedinput},
1714 {"test_garbagecollect_now", 0, 0, 0, NULL,
1715 ret_void, f_test_garbagecollect_now},
1716 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1717 ret_void, f_test_garbagecollect_soon},
1718 {"test_getvalue", 1, 1, FEARG_1, NULL,
1719 ret_number, f_test_getvalue},
1720 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1721 ret_void, f_test_ignore_error},
1722 {"test_null_blob", 0, 0, 0, NULL,
1723 ret_blob, f_test_null_blob},
1724 {"test_null_channel", 0, 0, 0, NULL,
1725 ret_channel, JOB_FUNC(f_test_null_channel)},
1726 {"test_null_dict", 0, 0, 0, NULL,
1727 ret_dict_any, f_test_null_dict},
1728 {"test_null_function", 0, 0, 0, NULL,
1729 ret_func_any, f_test_null_function},
1730 {"test_null_job", 0, 0, 0, NULL,
1731 ret_job, JOB_FUNC(f_test_null_job)},
1732 {"test_null_list", 0, 0, 0, NULL,
1733 ret_list_any, f_test_null_list},
1734 {"test_null_partial", 0, 0, 0, NULL,
1735 ret_func_any, f_test_null_partial},
1736 {"test_null_string", 0, 0, 0, NULL,
1737 ret_string, f_test_null_string},
1738 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1739 ret_void, f_test_option_not_set},
1740 {"test_override", 2, 2, FEARG_2, NULL,
1741 ret_void, f_test_override},
1742 {"test_refcount", 1, 1, FEARG_1, NULL,
1743 ret_number, f_test_refcount},
1744 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1745 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001746#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001747 f_test_scrollbar
1748#else
1749 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001750#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001751 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001752 {"test_setmouse", 2, 2, 0, NULL,
1753 ret_void, f_test_setmouse},
1754 {"test_settime", 1, 1, FEARG_1, NULL,
1755 ret_void, f_test_settime},
1756 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1757 ret_void, f_test_srand_seed},
1758 {"test_unknown", 0, 0, 0, NULL,
1759 ret_any, f_test_unknown},
1760 {"test_void", 0, 0, 0, NULL,
1761 ret_void, f_test_void},
1762 {"timer_info", 0, 1, FEARG_1, NULL,
1763 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1764 {"timer_pause", 2, 2, FEARG_1, NULL,
1765 ret_void, TIMER_FUNC(f_timer_pause)},
1766 {"timer_start", 2, 3, FEARG_1, NULL,
1767 ret_number, TIMER_FUNC(f_timer_start)},
1768 {"timer_stop", 1, 1, FEARG_1, NULL,
1769 ret_void, TIMER_FUNC(f_timer_stop)},
1770 {"timer_stopall", 0, 0, 0, NULL,
1771 ret_void, TIMER_FUNC(f_timer_stopall)},
1772 {"tolower", 1, 1, FEARG_1, NULL,
1773 ret_string, f_tolower},
1774 {"toupper", 1, 1, FEARG_1, NULL,
1775 ret_string, f_toupper},
1776 {"tr", 3, 3, FEARG_1, NULL,
1777 ret_string, f_tr},
1778 {"trim", 1, 3, FEARG_1, NULL,
1779 ret_string, f_trim},
1780 {"trunc", 1, 1, FEARG_1, NULL,
1781 ret_float, FLOAT_FUNC(f_trunc)},
1782 {"type", 1, 1, FEARG_1, NULL,
1783 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001784 {"typename", 1, 1, FEARG_1, NULL,
1785 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001786 {"undofile", 1, 1, FEARG_1, NULL,
1787 ret_string, f_undofile},
1788 {"undotree", 0, 0, 0, NULL,
1789 ret_dict_any, f_undotree},
1790 {"uniq", 1, 3, FEARG_1, NULL,
1791 ret_list_any, f_uniq},
1792 {"values", 1, 1, FEARG_1, NULL,
1793 ret_list_any, f_values},
1794 {"virtcol", 1, 1, FEARG_1, NULL,
1795 ret_number, f_virtcol},
1796 {"visualmode", 0, 1, 0, NULL,
1797 ret_string, f_visualmode},
1798 {"wildmenumode", 0, 0, 0, NULL,
1799 ret_number, f_wildmenumode},
1800 {"win_execute", 2, 3, FEARG_2, NULL,
1801 ret_string, f_win_execute},
1802 {"win_findbuf", 1, 1, FEARG_1, NULL,
1803 ret_list_number, f_win_findbuf},
1804 {"win_getid", 0, 2, FEARG_1, NULL,
1805 ret_number, f_win_getid},
1806 {"win_gettype", 0, 1, FEARG_1, NULL,
1807 ret_string, f_win_gettype},
1808 {"win_gotoid", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001809 ret_number_bool, f_win_gotoid},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001810 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1811 ret_list_number, f_win_id2tabwin},
1812 {"win_id2win", 1, 1, FEARG_1, NULL,
1813 ret_number, f_win_id2win},
1814 {"win_screenpos", 1, 1, FEARG_1, NULL,
1815 ret_list_number, f_win_screenpos},
1816 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001817 ret_number_bool, f_win_splitmove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001818 {"winbufnr", 1, 1, FEARG_1, NULL,
1819 ret_number, f_winbufnr},
1820 {"wincol", 0, 0, 0, NULL,
1821 ret_number, f_wincol},
1822 {"windowsversion", 0, 0, 0, NULL,
1823 ret_string, f_windowsversion},
1824 {"winheight", 1, 1, FEARG_1, NULL,
1825 ret_number, f_winheight},
1826 {"winlayout", 0, 1, FEARG_1, NULL,
1827 ret_list_any, f_winlayout},
1828 {"winline", 0, 0, 0, NULL,
1829 ret_number, f_winline},
1830 {"winnr", 0, 1, FEARG_1, NULL,
1831 ret_number, f_winnr},
1832 {"winrestcmd", 0, 0, 0, NULL,
1833 ret_string, f_winrestcmd},
1834 {"winrestview", 1, 1, FEARG_1, NULL,
1835 ret_void, f_winrestview},
1836 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001837 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001838 {"winwidth", 1, 1, FEARG_1, NULL,
1839 ret_number, f_winwidth},
1840 {"wordcount", 0, 0, 0, NULL,
1841 ret_dict_number, f_wordcount},
1842 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001843 ret_number_bool, f_writefile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001844 {"xor", 2, 2, FEARG_1, NULL,
1845 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001846};
1847
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001848/*
1849 * Function given to ExpandGeneric() to obtain the list of internal
1850 * or user defined function names.
1851 */
1852 char_u *
1853get_function_name(expand_T *xp, int idx)
1854{
1855 static int intidx = -1;
1856 char_u *name;
1857
1858 if (idx == 0)
1859 intidx = -1;
1860 if (intidx < 0)
1861 {
1862 name = get_user_func_name(xp, idx);
1863 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001864 {
1865 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1866 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001867 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001868 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001869 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001870 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001871 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001872 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001873 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001874 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001875 STRCAT(IObuff, ")");
1876 return IObuff;
1877 }
1878
1879 return NULL;
1880}
1881
1882/*
1883 * Function given to ExpandGeneric() to obtain the list of internal or
1884 * user defined variable or function names.
1885 */
1886 char_u *
1887get_expr_name(expand_T *xp, int idx)
1888{
1889 static int intidx = -1;
1890 char_u *name;
1891
1892 if (idx == 0)
1893 intidx = -1;
1894 if (intidx < 0)
1895 {
1896 name = get_function_name(xp, idx);
1897 if (name != NULL)
1898 return name;
1899 }
1900 return get_user_var_name(xp, ++intidx);
1901}
1902
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001903/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001904 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001905 * Return index, or -1 if not found or "implemented" is TRUE and the function
1906 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001907 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001908 static int
1909find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001910{
1911 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001912 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001913 int cmp;
1914 int x;
1915
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001916 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001917
1918 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001919 while (first <= last)
1920 {
1921 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001922 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001923 if (cmp < 0)
1924 last = x - 1;
1925 else if (cmp > 0)
1926 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001927 else if (implemented && global_functions[x].f_func == NULL)
1928 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001929 else
1930 return x;
1931 }
1932 return -1;
1933}
1934
Bram Moolenaar15c47602020-03-26 22:16:48 +01001935/*
1936 * Find internal function "name" in table "global_functions".
1937 * Return index, or -1 if not found or the function is not implemented.
1938 */
1939 int
1940find_internal_func(char_u *name)
1941{
1942 return find_internal_func_opt(name, TRUE);
1943}
1944
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001945 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001946has_internal_func(char_u *name)
1947{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001948 return find_internal_func_opt(name, TRUE) >= 0;
1949}
1950
1951 static int
1952has_internal_func_name(char_u *name)
1953{
1954 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001955}
1956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 char *
1958internal_func_name(int idx)
1959{
1960 return global_functions[idx].f_name;
1961}
1962
Bram Moolenaar94738d82020-10-21 14:25:07 +02001963/*
1964 * Check the argument types for builting function "idx".
1965 * Uses the list of types on the type stack: "types".
1966 * Return FAIL and gives an error message when a type is wrong.
1967 */
1968 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001969internal_func_check_arg_types(
1970 type_T **types,
1971 int idx,
1972 int argcount,
1973 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001974{
1975 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1976 int i;
1977
1978 if (argchecks != NULL)
1979 {
1980 argcontext_T context;
1981
1982 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001983 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001984 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001985 for (i = 0; i < argcount; ++i)
1986 if (argchecks[i] != NULL)
1987 {
1988 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001989 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001990 return FAIL;
1991 }
1992 }
1993 return OK;
1994}
1995
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001996/*
1997 * Call the "f_retfunc" function to obtain the return type of function "idx".
1998 * "argtypes" is the list of argument types or NULL when there are no
1999 * arguments.
2000 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002002internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002004 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005}
2006
2007/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002008 * Return TRUE if "idx" is for the map() function.
2009 */
2010 int
2011internal_func_is_map(int idx)
2012{
2013 return global_functions[idx].f_func == f_map;
2014}
2015
2016/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002018 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2019 * first argument, 2 if method base is second argument, etc. 9 if method base
2020 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 */
2022 int
2023check_internal_func(int idx, int argcount)
2024{
2025 int res;
2026 char *name;
2027
2028 if (argcount < global_functions[idx].f_min_argc)
2029 res = FCERR_TOOFEW;
2030 else if (argcount > global_functions[idx].f_max_argc)
2031 res = FCERR_TOOMANY;
2032 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002033 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034
2035 name = internal_func_name(idx);
2036 if (res == FCERR_TOOMANY)
2037 semsg(_(e_toomanyarg), name);
2038 else
2039 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002040 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002041}
2042
Bram Moolenaarac92e252019-08-03 21:58:38 +02002043 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002044call_internal_func(
2045 char_u *name,
2046 int argcount,
2047 typval_T *argvars,
2048 typval_T *rettv)
2049{
2050 int i;
2051
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002052 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002053 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002054 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002055 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002056 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002057 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002058 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002059 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002060 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002061 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002062}
2063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 void
2065call_internal_func_by_idx(
2066 int idx,
2067 typval_T *argvars,
2068 typval_T *rettv)
2069{
2070 global_functions[idx].f_func(argvars, rettv);
2071}
2072
Bram Moolenaarac92e252019-08-03 21:58:38 +02002073/*
2074 * Invoke a method for base->method().
2075 */
2076 int
2077call_internal_method(
2078 char_u *name,
2079 int argcount,
2080 typval_T *argvars,
2081 typval_T *rettv,
2082 typval_T *basetv)
2083{
2084 int i;
2085 int fi;
2086 typval_T argv[MAX_FUNC_ARGS + 1];
2087
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002088 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002089 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002090 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002091 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002092 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002093 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002094 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002095 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002096 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002097
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002098 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002099 {
2100 // base value goes last
2101 for (i = 0; i < argcount; ++i)
2102 argv[i] = argvars[i];
2103 argv[argcount] = *basetv;
2104 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002105 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002106 {
2107 // base value goes second
2108 argv[0] = argvars[0];
2109 argv[1] = *basetv;
2110 for (i = 1; i < argcount; ++i)
2111 argv[i + 1] = argvars[i];
2112 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002113 else if (global_functions[fi].f_argtype == FEARG_3)
2114 {
2115 // base value goes third
2116 argv[0] = argvars[0];
2117 argv[1] = argvars[1];
2118 argv[2] = *basetv;
2119 for (i = 2; i < argcount; ++i)
2120 argv[i + 1] = argvars[i];
2121 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002122 else if (global_functions[fi].f_argtype == FEARG_4)
2123 {
2124 // base value goes fourth
2125 argv[0] = argvars[0];
2126 argv[1] = argvars[1];
2127 argv[2] = argvars[2];
2128 argv[3] = *basetv;
2129 for (i = 3; i < argcount; ++i)
2130 argv[i + 1] = argvars[i];
2131 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002132 else
2133 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002134 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002135 argv[0] = *basetv;
2136 for (i = 0; i < argcount; ++i)
2137 argv[i + 1] = argvars[i];
2138 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002139 argv[argcount + 1].v_type = VAR_UNKNOWN;
2140
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002141 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002142 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002143}
2144
2145/*
2146 * Return TRUE for a non-zero Number and a non-empty String.
2147 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002148 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002149non_zero_arg(typval_T *argvars)
2150{
2151 return ((argvars[0].v_type == VAR_NUMBER
2152 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002153 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154 && argvars[0].vval.v_number == VVAL_TRUE)
2155 || (argvars[0].v_type == VAR_STRING
2156 && argvars[0].vval.v_string != NULL
2157 && *argvars[0].vval.v_string != NUL));
2158}
2159
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002160#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002161/*
2162 * Get the float value of "argvars[0]" into "f".
2163 * Returns FAIL when the argument is not a Number or Float.
2164 */
2165 static int
2166get_float_arg(typval_T *argvars, float_T *f)
2167{
2168 if (argvars[0].v_type == VAR_FLOAT)
2169 {
2170 *f = argvars[0].vval.v_float;
2171 return OK;
2172 }
2173 if (argvars[0].v_type == VAR_NUMBER)
2174 {
2175 *f = (float_T)argvars[0].vval.v_number;
2176 return OK;
2177 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002178 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002179 return FAIL;
2180}
2181
2182/*
2183 * "abs(expr)" function
2184 */
2185 static void
2186f_abs(typval_T *argvars, typval_T *rettv)
2187{
2188 if (argvars[0].v_type == VAR_FLOAT)
2189 {
2190 rettv->v_type = VAR_FLOAT;
2191 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
2192 }
2193 else
2194 {
2195 varnumber_T n;
2196 int error = FALSE;
2197
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002198 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002199 if (error)
2200 rettv->vval.v_number = -1;
2201 else if (n > 0)
2202 rettv->vval.v_number = n;
2203 else
2204 rettv->vval.v_number = -n;
2205 }
2206}
2207
2208/*
2209 * "acos()" function
2210 */
2211 static void
2212f_acos(typval_T *argvars, typval_T *rettv)
2213{
2214 float_T f = 0.0;
2215
2216 rettv->v_type = VAR_FLOAT;
2217 if (get_float_arg(argvars, &f) == OK)
2218 rettv->vval.v_float = acos(f);
2219 else
2220 rettv->vval.v_float = 0.0;
2221}
2222#endif
2223
2224/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002225 * "and(expr, expr)" function
2226 */
2227 static void
2228f_and(typval_T *argvars, typval_T *rettv)
2229{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002230 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2231 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002232}
2233
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002234#ifdef FEAT_FLOAT
2235/*
2236 * "asin()" function
2237 */
2238 static void
2239f_asin(typval_T *argvars, typval_T *rettv)
2240{
2241 float_T f = 0.0;
2242
2243 rettv->v_type = VAR_FLOAT;
2244 if (get_float_arg(argvars, &f) == OK)
2245 rettv->vval.v_float = asin(f);
2246 else
2247 rettv->vval.v_float = 0.0;
2248}
2249
2250/*
2251 * "atan()" function
2252 */
2253 static void
2254f_atan(typval_T *argvars, typval_T *rettv)
2255{
2256 float_T f = 0.0;
2257
2258 rettv->v_type = VAR_FLOAT;
2259 if (get_float_arg(argvars, &f) == OK)
2260 rettv->vval.v_float = atan(f);
2261 else
2262 rettv->vval.v_float = 0.0;
2263}
2264
2265/*
2266 * "atan2()" function
2267 */
2268 static void
2269f_atan2(typval_T *argvars, typval_T *rettv)
2270{
2271 float_T fx = 0.0, fy = 0.0;
2272
2273 rettv->v_type = VAR_FLOAT;
2274 if (get_float_arg(argvars, &fx) == OK
2275 && get_float_arg(&argvars[1], &fy) == OK)
2276 rettv->vval.v_float = atan2(fx, fy);
2277 else
2278 rettv->vval.v_float = 0.0;
2279}
2280#endif
2281
2282/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002283 * "balloon_show()" function
2284 */
2285#ifdef FEAT_BEVAL
2286 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002287f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2288{
2289 rettv->v_type = VAR_STRING;
2290 if (balloonEval != NULL)
2291 {
2292 if (balloonEval->msg == NULL)
2293 rettv->vval.v_string = NULL;
2294 else
2295 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2296 }
2297}
2298
2299 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002300f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2301{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002302 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002303 {
2304 if (argvars[0].v_type == VAR_LIST
2305# ifdef FEAT_GUI
2306 && !gui.in_use
2307# endif
2308 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002309 {
2310 list_T *l = argvars[0].vval.v_list;
2311
2312 // empty list removes the balloon
2313 post_balloon(balloonEval, NULL,
2314 l == NULL || l->lv_len == 0 ? NULL : l);
2315 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002316 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002317 {
2318 char_u *mesg = tv_get_string_chk(&argvars[0]);
2319
2320 if (mesg != NULL)
2321 // empty string removes the balloon
2322 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2323 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002324 }
2325}
2326
Bram Moolenaar669a8282017-11-19 20:13:05 +01002327# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002328 static void
2329f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2330{
2331 if (rettv_list_alloc(rettv) == OK)
2332 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002333 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002334
2335 if (msg != NULL)
2336 {
2337 pumitem_T *array;
2338 int size = split_message(msg, &array);
2339 int i;
2340
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002341 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002342 for (i = 1; i < size - 1; ++i)
2343 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002344 while (size > 0)
2345 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002346 vim_free(array);
2347 }
2348 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002349}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002350# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002351#endif
2352
2353/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002354 * Get the buffer from "arg" and give an error and return NULL if it is not
2355 * valid.
2356 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002357 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002358get_buf_arg(typval_T *arg)
2359{
2360 buf_T *buf;
2361
2362 ++emsg_off;
2363 buf = tv_get_buf(arg, FALSE);
2364 --emsg_off;
2365 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002366 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002367 return buf;
2368}
2369
2370/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002371 * "byte2line(byte)" function
2372 */
2373 static void
2374f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2375{
2376#ifndef FEAT_BYTEOFF
2377 rettv->vval.v_number = -1;
2378#else
2379 long boff = 0;
2380
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002381 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002382 if (boff < 0)
2383 rettv->vval.v_number = -1;
2384 else
2385 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2386 (linenr_T)0, &boff);
2387#endif
2388}
2389
2390 static void
2391byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2392{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002393 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002394 char_u *str;
2395 varnumber_T idx;
2396
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002397 str = tv_get_string_chk(&argvars[0]);
2398 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002399 rettv->vval.v_number = -1;
2400 if (str == NULL || idx < 0)
2401 return;
2402
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002403 t = str;
2404 for ( ; idx > 0; idx--)
2405 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002406 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002407 return;
2408 if (enc_utf8 && comp)
2409 t += utf_ptr2len(t);
2410 else
2411 t += (*mb_ptr2len)(t);
2412 }
2413 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002414}
2415
2416/*
2417 * "byteidx()" function
2418 */
2419 static void
2420f_byteidx(typval_T *argvars, typval_T *rettv)
2421{
2422 byteidx(argvars, rettv, FALSE);
2423}
2424
2425/*
2426 * "byteidxcomp()" function
2427 */
2428 static void
2429f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2430{
2431 byteidx(argvars, rettv, TRUE);
2432}
2433
2434/*
2435 * "call(func, arglist [, dict])" function
2436 */
2437 static void
2438f_call(typval_T *argvars, typval_T *rettv)
2439{
2440 char_u *func;
2441 partial_T *partial = NULL;
2442 dict_T *selfdict = NULL;
2443
2444 if (argvars[1].v_type != VAR_LIST)
2445 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002446 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002447 return;
2448 }
2449 if (argvars[1].vval.v_list == NULL)
2450 return;
2451
2452 if (argvars[0].v_type == VAR_FUNC)
2453 func = argvars[0].vval.v_string;
2454 else if (argvars[0].v_type == VAR_PARTIAL)
2455 {
2456 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002457 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002458 }
2459 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002460 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002461 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002462 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002463
2464 if (argvars[2].v_type != VAR_UNKNOWN)
2465 {
2466 if (argvars[2].v_type != VAR_DICT)
2467 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002468 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002469 return;
2470 }
2471 selfdict = argvars[2].vval.v_dict;
2472 }
2473
2474 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2475}
2476
2477#ifdef FEAT_FLOAT
2478/*
2479 * "ceil({float})" function
2480 */
2481 static void
2482f_ceil(typval_T *argvars, typval_T *rettv)
2483{
2484 float_T f = 0.0;
2485
2486 rettv->v_type = VAR_FLOAT;
2487 if (get_float_arg(argvars, &f) == OK)
2488 rettv->vval.v_float = ceil(f);
2489 else
2490 rettv->vval.v_float = 0.0;
2491}
2492#endif
2493
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002494/*
2495 * "changenr()" function
2496 */
2497 static void
2498f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2499{
2500 rettv->vval.v_number = curbuf->b_u_seq_cur;
2501}
2502
2503/*
2504 * "char2nr(string)" function
2505 */
2506 static void
2507f_char2nr(typval_T *argvars, typval_T *rettv)
2508{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002509 if (has_mbyte)
2510 {
2511 int utf8 = 0;
2512
2513 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002514 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002515
2516 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002517 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002518 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002519 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002520 }
2521 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002522 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002523}
2524
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002525/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002526 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2527 * returns the character index of the column. Otherwise, returns the byte index
2528 * of the column.
2529 */
2530 static void
2531get_col(typval_T *argvars, typval_T *rettv, int charcol)
2532{
2533 colnr_T col = 0;
2534 pos_T *fp;
2535 int fnum = curbuf->b_fnum;
2536
2537 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2538 if (fp != NULL && fnum == curbuf->b_fnum)
2539 {
2540 if (fp->col == MAXCOL)
2541 {
2542 // '> can be MAXCOL, get the length of the line then
2543 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2544 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2545 else
2546 col = MAXCOL;
2547 }
2548 else
2549 {
2550 col = fp->col + 1;
2551 // col(".") when the cursor is on the NUL at the end of the line
2552 // because of "coladd" can be seen as an extra column.
2553 if (virtual_active() && fp == &curwin->w_cursor)
2554 {
2555 char_u *p = ml_get_cursor();
2556
2557 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2558 curwin->w_virtcol - curwin->w_cursor.coladd))
2559 {
2560 int l;
2561
2562 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2563 col += l;
2564 }
2565 }
2566 }
2567 }
2568 rettv->vval.v_number = col;
2569}
2570
2571/*
2572 * "charcol()" function
2573 */
2574 static void
2575f_charcol(typval_T *argvars, typval_T *rettv)
2576{
2577 get_col(argvars, rettv, TRUE);
2578}
2579
2580/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002581 * "charidx()" function
2582 */
2583 static void
2584f_charidx(typval_T *argvars, typval_T *rettv)
2585{
2586 char_u *str;
2587 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002588 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002589 char_u *p;
2590 int len;
2591 int (*ptr2len)(char_u *);
2592
2593 rettv->vval.v_number = -1;
2594
2595 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2596 || (argvars[2].v_type != VAR_UNKNOWN
2597 && argvars[2].v_type != VAR_NUMBER))
2598 {
2599 emsg(_(e_invarg));
2600 return;
2601 }
2602
2603 str = tv_get_string_chk(&argvars[0]);
2604 idx = tv_get_number_chk(&argvars[1], NULL);
2605 if (str == NULL || idx < 0)
2606 return;
2607
2608 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002609 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002610 if (countcc < 0 || countcc > 1)
2611 {
2612 semsg(_(e_using_number_as_bool_nr), countcc);
2613 return;
2614 }
2615
2616 if (enc_utf8 && countcc)
2617 ptr2len = utf_ptr2len;
2618 else
2619 ptr2len = mb_ptr2len;
2620
2621 for (p = str, len = 0; p <= str + idx; len++)
2622 {
2623 if (*p == NUL)
2624 return;
2625 p += ptr2len(p);
2626 }
2627
2628 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2629}
2630
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002631 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002632get_optional_window(typval_T *argvars, int idx)
2633{
2634 win_T *win = curwin;
2635
2636 if (argvars[idx].v_type != VAR_UNKNOWN)
2637 {
2638 win = find_win_by_nr_or_id(&argvars[idx]);
2639 if (win == NULL)
2640 {
2641 emsg(_(e_invalwindow));
2642 return NULL;
2643 }
2644 }
2645 return win;
2646}
2647
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002648/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002649 * "col(string)" function
2650 */
2651 static void
2652f_col(typval_T *argvars, typval_T *rettv)
2653{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002654 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002655}
2656
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002657/*
2658 * "confirm(message, buttons[, default [, type]])" function
2659 */
2660 static void
2661f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2662{
2663#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2664 char_u *message;
2665 char_u *buttons = NULL;
2666 char_u buf[NUMBUFLEN];
2667 char_u buf2[NUMBUFLEN];
2668 int def = 1;
2669 int type = VIM_GENERIC;
2670 char_u *typestr;
2671 int error = FALSE;
2672
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002673 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002674 if (message == NULL)
2675 error = TRUE;
2676 if (argvars[1].v_type != VAR_UNKNOWN)
2677 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002678 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002679 if (buttons == NULL)
2680 error = TRUE;
2681 if (argvars[2].v_type != VAR_UNKNOWN)
2682 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002683 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002684 if (argvars[3].v_type != VAR_UNKNOWN)
2685 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002686 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002687 if (typestr == NULL)
2688 error = TRUE;
2689 else
2690 {
2691 switch (TOUPPER_ASC(*typestr))
2692 {
2693 case 'E': type = VIM_ERROR; break;
2694 case 'Q': type = VIM_QUESTION; break;
2695 case 'I': type = VIM_INFO; break;
2696 case 'W': type = VIM_WARNING; break;
2697 case 'G': type = VIM_GENERIC; break;
2698 }
2699 }
2700 }
2701 }
2702 }
2703
2704 if (buttons == NULL || *buttons == NUL)
2705 buttons = (char_u *)_("&Ok");
2706
2707 if (!error)
2708 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2709 def, NULL, FALSE);
2710#endif
2711}
2712
2713/*
2714 * "copy()" function
2715 */
2716 static void
2717f_copy(typval_T *argvars, typval_T *rettv)
2718{
2719 item_copy(&argvars[0], rettv, FALSE, 0);
2720}
2721
2722#ifdef FEAT_FLOAT
2723/*
2724 * "cos()" function
2725 */
2726 static void
2727f_cos(typval_T *argvars, typval_T *rettv)
2728{
2729 float_T f = 0.0;
2730
2731 rettv->v_type = VAR_FLOAT;
2732 if (get_float_arg(argvars, &f) == OK)
2733 rettv->vval.v_float = cos(f);
2734 else
2735 rettv->vval.v_float = 0.0;
2736}
2737
2738/*
2739 * "cosh()" function
2740 */
2741 static void
2742f_cosh(typval_T *argvars, typval_T *rettv)
2743{
2744 float_T f = 0.0;
2745
2746 rettv->v_type = VAR_FLOAT;
2747 if (get_float_arg(argvars, &f) == OK)
2748 rettv->vval.v_float = cosh(f);
2749 else
2750 rettv->vval.v_float = 0.0;
2751}
2752#endif
2753
2754/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002755 * Set the cursor position.
2756 * If 'charcol' is TRUE, then use the column number as a character offet.
2757 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002758 */
2759 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002760set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002761{
2762 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002763 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002764 int set_curswant = TRUE;
2765
2766 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002767 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002768 {
2769 pos_T pos;
2770 colnr_T curswant = -1;
2771
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002772 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002773 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002774 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002775 return;
2776 }
2777 line = pos.lnum;
2778 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002779 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002780 if (curswant >= 0)
2781 {
2782 curwin->w_curswant = curswant - 1;
2783 set_curswant = FALSE;
2784 }
2785 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002786 else if ((argvars[0].v_type == VAR_NUMBER ||
2787 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002788 && (argvars[1].v_type == VAR_NUMBER ||
2789 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002790 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002791 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002792 if (line < 0)
2793 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002794 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002795 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002796 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002797 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002798 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002799 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002800 else
2801 {
2802 emsg(_(e_invarg));
2803 return;
2804 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002805 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002806 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002807 if (line > 0)
2808 curwin->w_cursor.lnum = line;
2809 if (col > 0)
2810 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002811 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002812
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002813 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002814 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002815 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002816 if (has_mbyte)
2817 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002818
2819 curwin->w_set_curswant = set_curswant;
2820 rettv->vval.v_number = 0;
2821}
2822
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002823/*
2824 * "cursor(lnum, col)" function, or
2825 * "cursor(list)"
2826 *
2827 * Moves the cursor to the specified line and column.
2828 * Returns 0 when the position could be set, -1 otherwise.
2829 */
2830 static void
2831f_cursor(typval_T *argvars, typval_T *rettv)
2832{
2833 set_cursorpos(argvars, rettv, FALSE);
2834}
2835
Bram Moolenaar4f974752019-02-17 17:44:42 +01002836#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002837/*
2838 * "debugbreak()" function
2839 */
2840 static void
2841f_debugbreak(typval_T *argvars, typval_T *rettv)
2842{
2843 int pid;
2844
2845 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002846 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002847 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002848 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002849 else
2850 {
2851 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2852
2853 if (hProcess != NULL)
2854 {
2855 DebugBreakProcess(hProcess);
2856 CloseHandle(hProcess);
2857 rettv->vval.v_number = OK;
2858 }
2859 }
2860}
2861#endif
2862
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002863/*
2864 * "deepcopy()" function
2865 */
2866 static void
2867f_deepcopy(typval_T *argvars, typval_T *rettv)
2868{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002869 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002870 int copyID;
2871
2872 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002873 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002874 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002875 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002876 else
2877 {
2878 copyID = get_copyID();
2879 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2880 }
2881}
2882
2883/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002884 * "did_filetype()" function
2885 */
2886 static void
2887f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2888{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002889 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002890}
2891
2892/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002893 * "echoraw({expr})" function
2894 */
2895 static void
2896f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2897{
2898 char_u *str = tv_get_string_chk(&argvars[0]);
2899
2900 if (str != NULL && *str != NUL)
2901 {
2902 out_str(str);
2903 out_flush();
2904 }
2905}
2906
2907/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002908 * "empty({expr})" function
2909 */
2910 static void
2911f_empty(typval_T *argvars, typval_T *rettv)
2912{
2913 int n = FALSE;
2914
2915 switch (argvars[0].v_type)
2916 {
2917 case VAR_STRING:
2918 case VAR_FUNC:
2919 n = argvars[0].vval.v_string == NULL
2920 || *argvars[0].vval.v_string == NUL;
2921 break;
2922 case VAR_PARTIAL:
2923 n = FALSE;
2924 break;
2925 case VAR_NUMBER:
2926 n = argvars[0].vval.v_number == 0;
2927 break;
2928 case VAR_FLOAT:
2929#ifdef FEAT_FLOAT
2930 n = argvars[0].vval.v_float == 0.0;
2931 break;
2932#endif
2933 case VAR_LIST:
2934 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002935 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002936 break;
2937 case VAR_DICT:
2938 n = argvars[0].vval.v_dict == NULL
2939 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2940 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002941 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002942 case VAR_SPECIAL:
2943 n = argvars[0].vval.v_number != VVAL_TRUE;
2944 break;
2945
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002946 case VAR_BLOB:
2947 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002948 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2949 break;
2950
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002951 case VAR_JOB:
2952#ifdef FEAT_JOB_CHANNEL
2953 n = argvars[0].vval.v_job == NULL
2954 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2955 break;
2956#endif
2957 case VAR_CHANNEL:
2958#ifdef FEAT_JOB_CHANNEL
2959 n = argvars[0].vval.v_channel == NULL
2960 || !channel_is_open(argvars[0].vval.v_channel);
2961 break;
2962#endif
2963 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002964 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002966 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002967 n = TRUE;
2968 break;
2969 }
2970
2971 rettv->vval.v_number = n;
2972}
2973
2974/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002975 * "environ()" function
2976 */
2977 static void
2978f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2979{
2980#if !defined(AMIGA)
2981 int i = 0;
2982 char_u *entry, *value;
2983# ifdef MSWIN
2984 extern wchar_t **_wenviron;
2985# else
2986 extern char **environ;
2987# endif
2988
2989 if (rettv_dict_alloc(rettv) != OK)
2990 return;
2991
2992# ifdef MSWIN
2993 if (*_wenviron == NULL)
2994 return;
2995# else
2996 if (*environ == NULL)
2997 return;
2998# endif
2999
3000 for (i = 0; ; ++i)
3001 {
3002# ifdef MSWIN
3003 short_u *p;
3004
3005 if ((p = (short_u *)_wenviron[i]) == NULL)
3006 return;
3007 entry = utf16_to_enc(p, NULL);
3008# else
3009 if ((entry = (char_u *)environ[i]) == NULL)
3010 return;
3011 entry = vim_strsave(entry);
3012# endif
3013 if (entry == NULL) // out of memory
3014 return;
3015 if ((value = vim_strchr(entry, '=')) == NULL)
3016 {
3017 vim_free(entry);
3018 continue;
3019 }
3020 *value++ = NUL;
3021 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3022 vim_free(entry);
3023 }
3024#endif
3025}
3026
3027/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003028 * "escape({string}, {chars})" function
3029 */
3030 static void
3031f_escape(typval_T *argvars, typval_T *rettv)
3032{
3033 char_u buf[NUMBUFLEN];
3034
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003035 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3036 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003037 rettv->v_type = VAR_STRING;
3038}
3039
3040/*
3041 * "eval()" function
3042 */
3043 static void
3044f_eval(typval_T *argvars, typval_T *rettv)
3045{
3046 char_u *s, *p;
3047
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003048 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003049 if (s != NULL)
3050 s = skipwhite(s);
3051
3052 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003053 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003054 {
3055 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003056 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003057 need_clr_eos = FALSE;
3058 rettv->v_type = VAR_NUMBER;
3059 rettv->vval.v_number = 0;
3060 }
3061 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003062 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003063}
3064
3065/*
3066 * "eventhandler()" function
3067 */
3068 static void
3069f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3070{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003071 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003072}
3073
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003074static garray_T redir_execute_ga;
3075
3076/*
3077 * Append "value[value_len]" to the execute() output.
3078 */
3079 void
3080execute_redir_str(char_u *value, int value_len)
3081{
3082 int len;
3083
3084 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003085 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003086 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003087 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003088 if (ga_grow(&redir_execute_ga, len) == OK)
3089 {
3090 mch_memmove((char *)redir_execute_ga.ga_data
3091 + redir_execute_ga.ga_len, value, len);
3092 redir_execute_ga.ga_len += len;
3093 }
3094}
3095
3096/*
3097 * Get next line from a list.
3098 * Called by do_cmdline() to get the next line.
3099 * Returns allocated string, or NULL for end of function.
3100 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003101 static char_u *
3102get_list_line(
3103 int c UNUSED,
3104 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003105 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02003106 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003107{
3108 listitem_T **p = (listitem_T **)cookie;
3109 listitem_T *item = *p;
3110 char_u buf[NUMBUFLEN];
3111 char_u *s;
3112
3113 if (item == NULL)
3114 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003115 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003116 *p = item->li_next;
3117 return s == NULL ? NULL : vim_strsave(s);
3118}
3119
3120/*
3121 * "execute()" function
3122 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02003123 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003124execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003125{
3126 char_u *cmd = NULL;
3127 list_T *list = NULL;
3128 int save_msg_silent = msg_silent;
3129 int save_emsg_silent = emsg_silent;
3130 int save_emsg_noredir = emsg_noredir;
3131 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003132 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003133 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003134 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003135 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003136
3137 rettv->vval.v_string = NULL;
3138 rettv->v_type = VAR_STRING;
3139
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003140 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003141 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003142 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003143 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003144 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003145 return;
3146 ++list->lv_refcount;
3147 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003148 else if (argvars[arg_off].v_type == VAR_JOB
3149 || argvars[arg_off].v_type == VAR_CHANNEL)
3150 {
3151 emsg(_(e_inval_string));
3152 return;
3153 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003154 else
3155 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003156 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003157 if (cmd == NULL)
3158 return;
3159 }
3160
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003161 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003162 {
3163 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003164 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003165
3166 if (s == NULL)
3167 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003168 if (*s == NUL)
3169 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003170 if (STRNCMP(s, "silent", 6) == 0)
3171 ++msg_silent;
3172 if (STRCMP(s, "silent!") == 0)
3173 {
3174 emsg_silent = TRUE;
3175 emsg_noredir = TRUE;
3176 }
3177 }
3178 else
3179 ++msg_silent;
3180
3181 if (redir_execute)
3182 save_ga = redir_execute_ga;
3183 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3184 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003185 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003186 if (!echo_output)
3187 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003188
3189 if (cmd != NULL)
3190 do_cmdline_cmd(cmd);
3191 else
3192 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003193 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003194
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003195 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003196 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003197 do_cmdline(NULL, get_list_line, (void *)&item,
3198 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3199 --list->lv_refcount;
3200 }
3201
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003202 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003203 if (ga_grow(&redir_execute_ga, 1) == OK)
3204 {
3205 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3206 rettv->vval.v_string = redir_execute_ga.ga_data;
3207 }
3208 else
3209 {
3210 ga_clear(&redir_execute_ga);
3211 rettv->vval.v_string = NULL;
3212 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003213 msg_silent = save_msg_silent;
3214 emsg_silent = save_emsg_silent;
3215 emsg_noredir = save_emsg_noredir;
3216
3217 redir_execute = save_redir_execute;
3218 if (redir_execute)
3219 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003220 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003221
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003222 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003223 if (echo_output)
3224 // When not working silently: put it in column zero. A following
3225 // "echon" will overwrite the message, unavoidably.
3226 msg_col = 0;
3227 else
3228 // When working silently: Put it back where it was, since nothing
3229 // should have been written.
3230 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003231}
3232
3233/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003234 * "execute()" function
3235 */
3236 static void
3237f_execute(typval_T *argvars, typval_T *rettv)
3238{
3239 execute_common(argvars, rettv, 0);
3240}
3241
3242/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003243 * "exists()" function
3244 */
3245 static void
3246f_exists(typval_T *argvars, typval_T *rettv)
3247{
3248 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003249 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003250
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003251 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003252 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003253 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003254 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003255 if (mch_getenv(p + 1) != NULL)
3256 n = TRUE;
3257 else
3258 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003259 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003260 p = expand_env_save(p);
3261 if (p != NULL && *p != '$')
3262 n = TRUE;
3263 vim_free(p);
3264 }
3265 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003266 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003267 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003268 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003269 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003270 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003271 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003272 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003273 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003274 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003275 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003276 else if (*p == '?') // internal function only
3277 {
3278 n = has_internal_func_name(p + 1);
3279 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003280 else if (*p == ':')
3281 {
3282 n = cmd_exists(p + 1);
3283 }
3284 else if (*p == '#')
3285 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003286 if (p[1] == '#')
3287 n = autocmd_supported(p + 2);
3288 else
3289 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003290 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003291 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003292 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003293 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003294 }
3295
3296 rettv->vval.v_number = n;
3297}
3298
3299#ifdef FEAT_FLOAT
3300/*
3301 * "exp()" function
3302 */
3303 static void
3304f_exp(typval_T *argvars, typval_T *rettv)
3305{
3306 float_T f = 0.0;
3307
3308 rettv->v_type = VAR_FLOAT;
3309 if (get_float_arg(argvars, &f) == OK)
3310 rettv->vval.v_float = exp(f);
3311 else
3312 rettv->vval.v_float = 0.0;
3313}
3314#endif
3315
3316/*
3317 * "expand()" function
3318 */
3319 static void
3320f_expand(typval_T *argvars, typval_T *rettv)
3321{
3322 char_u *s;
3323 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003324 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003325 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3326 expand_T xpc;
3327 int error = FALSE;
3328 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003329#ifdef BACKSLASH_IN_FILENAME
3330 char_u *p_csl_save = p_csl;
3331
3332 // avoid using 'completeslash' here
3333 p_csl = empty_option;
3334#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003335
3336 rettv->v_type = VAR_STRING;
3337 if (argvars[1].v_type != VAR_UNKNOWN
3338 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003339 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003340 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003341 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003342
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003343 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003344 if (*s == '%' || *s == '#' || *s == '<')
3345 {
3346 ++emsg_off;
3347 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3348 --emsg_off;
3349 if (rettv->v_type == VAR_LIST)
3350 {
3351 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3352 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003353 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003354 }
3355 else
3356 rettv->vval.v_string = result;
3357 }
3358 else
3359 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003360 // When the optional second argument is non-zero, don't remove matches
3361 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003362 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003363 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003364 options |= WILD_KEEP_ALL;
3365 if (!error)
3366 {
3367 ExpandInit(&xpc);
3368 xpc.xp_context = EXPAND_FILES;
3369 if (p_wic)
3370 options += WILD_ICASE;
3371 if (rettv->v_type == VAR_STRING)
3372 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3373 options, WILD_ALL);
3374 else if (rettv_list_alloc(rettv) != FAIL)
3375 {
3376 int i;
3377
3378 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3379 for (i = 0; i < xpc.xp_numfiles; i++)
3380 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3381 ExpandCleanup(&xpc);
3382 }
3383 }
3384 else
3385 rettv->vval.v_string = NULL;
3386 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003387#ifdef BACKSLASH_IN_FILENAME
3388 p_csl = p_csl_save;
3389#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003390}
3391
3392/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003393 * "expandcmd()" function
3394 * Expand all the special characters in a command string.
3395 */
3396 static void
3397f_expandcmd(typval_T *argvars, typval_T *rettv)
3398{
3399 exarg_T eap;
3400 char_u *cmdstr;
3401 char *errormsg = NULL;
3402
3403 rettv->v_type = VAR_STRING;
3404 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3405
3406 memset(&eap, 0, sizeof(eap));
3407 eap.cmd = cmdstr;
3408 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003409 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003410 eap.usefilter = FALSE;
3411 eap.nextcmd = NULL;
3412 eap.cmdidx = CMD_USER;
3413
3414 expand_filename(&eap, &cmdstr, &errormsg);
3415 if (errormsg != NULL && *errormsg != NUL)
3416 emsg(errormsg);
3417
3418 rettv->vval.v_string = cmdstr;
3419}
3420
3421/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003422 * "feedkeys()" function
3423 */
3424 static void
3425f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3426{
3427 int remap = TRUE;
3428 int insert = FALSE;
3429 char_u *keys, *flags;
3430 char_u nbuf[NUMBUFLEN];
3431 int typed = FALSE;
3432 int execute = FALSE;
3433 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003434 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003435 char_u *keys_esc;
3436
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003437 // This is not allowed in the sandbox. If the commands would still be
3438 // executed in the sandbox it would be OK, but it probably happens later,
3439 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003440 if (check_secure())
3441 return;
3442
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003443 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003444
3445 if (argvars[1].v_type != VAR_UNKNOWN)
3446 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003447 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003448 for ( ; *flags != NUL; ++flags)
3449 {
3450 switch (*flags)
3451 {
3452 case 'n': remap = FALSE; break;
3453 case 'm': remap = TRUE; break;
3454 case 't': typed = TRUE; break;
3455 case 'i': insert = TRUE; break;
3456 case 'x': execute = TRUE; break;
3457 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003458 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003459 }
3460 }
3461 }
3462
3463 if (*keys != NUL || execute)
3464 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003465 // Need to escape K_SPECIAL and CSI before putting the string in the
3466 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003467 keys_esc = vim_strsave_escape_csi(keys);
3468 if (keys_esc != NULL)
3469 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003470 if (lowlevel)
3471 {
3472#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003473 int idx;
3474 int len = (int)STRLEN(keys);
3475
3476 for (idx = 0; idx < len; ++idx)
3477 {
3478 // if a CTRL-C was typed, set got_int, similar to what
3479 // happens in fill_input_buf()
3480 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3481 got_int = TRUE;
3482 add_to_input_buf(keys + idx, 1);
3483 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003484#else
3485 emsg(_("E980: lowlevel input not supported"));
3486#endif
3487 }
3488 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003489 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003490 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003491 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003492 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003493#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003494 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003495#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003496 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003497 typebuf_was_filled = TRUE;
3498 }
3499 vim_free(keys_esc);
3500
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003501 if (execute)
3502 {
3503 int save_msg_scroll = msg_scroll;
3504
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003505 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003506 msg_scroll = FALSE;
3507
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003508 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003509 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003510 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003511 ++in_feedkeys;
3512 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003513 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003514 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003515 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003516 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003517 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003518 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003519
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003520 msg_scroll |= save_msg_scroll;
3521 }
3522 }
3523 }
3524}
3525
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003526#ifdef FEAT_FLOAT
3527/*
3528 * "float2nr({float})" function
3529 */
3530 static void
3531f_float2nr(typval_T *argvars, typval_T *rettv)
3532{
3533 float_T f = 0.0;
3534
3535 if (get_float_arg(argvars, &f) == OK)
3536 {
Bram Moolenaar37184272020-05-23 19:30:05 +02003537 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003538 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02003539 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003540 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003541 else
3542 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003543 }
3544}
3545
3546/*
3547 * "floor({float})" function
3548 */
3549 static void
3550f_floor(typval_T *argvars, typval_T *rettv)
3551{
3552 float_T f = 0.0;
3553
3554 rettv->v_type = VAR_FLOAT;
3555 if (get_float_arg(argvars, &f) == OK)
3556 rettv->vval.v_float = floor(f);
3557 else
3558 rettv->vval.v_float = 0.0;
3559}
3560
3561/*
3562 * "fmod()" function
3563 */
3564 static void
3565f_fmod(typval_T *argvars, typval_T *rettv)
3566{
3567 float_T fx = 0.0, fy = 0.0;
3568
3569 rettv->v_type = VAR_FLOAT;
3570 if (get_float_arg(argvars, &fx) == OK
3571 && get_float_arg(&argvars[1], &fy) == OK)
3572 rettv->vval.v_float = fmod(fx, fy);
3573 else
3574 rettv->vval.v_float = 0.0;
3575}
3576#endif
3577
3578/*
3579 * "fnameescape({string})" function
3580 */
3581 static void
3582f_fnameescape(typval_T *argvars, typval_T *rettv)
3583{
3584 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003585 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003586 rettv->v_type = VAR_STRING;
3587}
3588
3589/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003590 * "foreground()" function
3591 */
3592 static void
3593f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3594{
3595#ifdef FEAT_GUI
3596 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003597 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003598 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003599 return;
3600 }
3601#endif
3602#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003603 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003604#endif
3605}
3606
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003607 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003608common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003609{
3610 char_u *s;
3611 char_u *name;
3612 int use_string = FALSE;
3613 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003614 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003615 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003616
3617 if (argvars[0].v_type == VAR_FUNC)
3618 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003619 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003620 s = argvars[0].vval.v_string;
3621 }
3622 else if (argvars[0].v_type == VAR_PARTIAL
3623 && argvars[0].vval.v_partial != NULL)
3624 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003625 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003626 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003627 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003628 }
3629 else
3630 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003631 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003632 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003633 use_string = TRUE;
3634 }
3635
Bram Moolenaar843b8842016-08-21 14:36:15 +02003636 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003637 {
3638 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003639 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003640 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3641 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003642 if (*name != NUL)
3643 s = NULL;
3644 }
3645
Bram Moolenaar843b8842016-08-21 14:36:15 +02003646 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3647 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003648 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003649 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003650 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003651 ? find_func(trans_name, is_global, NULL) == NULL
3652 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003653 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003654 else
3655 {
3656 int dict_idx = 0;
3657 int arg_idx = 0;
3658 list_T *list = NULL;
3659
3660 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3661 {
3662 char sid_buf[25];
3663 int off = *s == 's' ? 2 : 5;
3664
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003665 // Expand s: and <SID> into <SNR>nr_, so that the function can
3666 // also be called from another script. Using trans_function_name()
3667 // would also work, but some plugins depend on the name being
3668 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003669 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003670 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003671 if (name != NULL)
3672 {
3673 STRCPY(name, sid_buf);
3674 STRCAT(name, s + off);
3675 }
3676 }
3677 else
3678 name = vim_strsave(s);
3679
3680 if (argvars[1].v_type != VAR_UNKNOWN)
3681 {
3682 if (argvars[2].v_type != VAR_UNKNOWN)
3683 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003684 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003685 arg_idx = 1;
3686 dict_idx = 2;
3687 }
3688 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003689 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003690 dict_idx = 1;
3691 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003692 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003693 arg_idx = 1;
3694 if (dict_idx > 0)
3695 {
3696 if (argvars[dict_idx].v_type != VAR_DICT)
3697 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003698 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003699 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003700 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003701 }
3702 if (argvars[dict_idx].vval.v_dict == NULL)
3703 dict_idx = 0;
3704 }
3705 if (arg_idx > 0)
3706 {
3707 if (argvars[arg_idx].v_type != VAR_LIST)
3708 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003709 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003710 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003711 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003712 }
3713 list = argvars[arg_idx].vval.v_list;
3714 if (list == NULL || list->lv_len == 0)
3715 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003716 else if (list->lv_len > MAX_FUNC_ARGS)
3717 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003718 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003719 vim_free(name);
3720 goto theend;
3721 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003722 }
3723 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003724 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003725 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003726 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003727
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003728 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003729 if (pt == NULL)
3730 vim_free(name);
3731 else
3732 {
3733 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3734 {
3735 listitem_T *li;
3736 int i = 0;
3737 int arg_len = 0;
3738 int lv_len = 0;
3739
3740 if (arg_pt != NULL)
3741 arg_len = arg_pt->pt_argc;
3742 if (list != NULL)
3743 lv_len = list->lv_len;
3744 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003745 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003746 if (pt->pt_argv == NULL)
3747 {
3748 vim_free(pt);
3749 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003750 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003751 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003752 for (i = 0; i < arg_len; i++)
3753 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3754 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003755 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003756 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003757 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003758 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003759 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003760 }
3761
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003762 // For "function(dict.func, [], dict)" and "func" is a partial
3763 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003764 if (dict_idx > 0)
3765 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003766 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003767 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3768 ++pt->pt_dict->dv_refcount;
3769 }
3770 else if (arg_pt != NULL)
3771 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003772 // If the dict was bound automatically the result is also
3773 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003774 pt->pt_dict = arg_pt->pt_dict;
3775 pt->pt_auto = arg_pt->pt_auto;
3776 if (pt->pt_dict != NULL)
3777 ++pt->pt_dict->dv_refcount;
3778 }
3779
3780 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003781 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3782 {
3783 pt->pt_func = arg_pt->pt_func;
3784 func_ptr_ref(pt->pt_func);
3785 vim_free(name);
3786 }
3787 else if (is_funcref)
3788 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003789 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003790 func_ptr_ref(pt->pt_func);
3791 vim_free(name);
3792 }
3793 else
3794 {
3795 pt->pt_name = name;
3796 func_ref(name);
3797 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003798 }
3799 rettv->v_type = VAR_PARTIAL;
3800 rettv->vval.v_partial = pt;
3801 }
3802 else
3803 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003804 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003805 rettv->v_type = VAR_FUNC;
3806 rettv->vval.v_string = name;
3807 func_ref(name);
3808 }
3809 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003810theend:
3811 vim_free(trans_name);
3812}
3813
3814/*
3815 * "funcref()" function
3816 */
3817 static void
3818f_funcref(typval_T *argvars, typval_T *rettv)
3819{
3820 common_function(argvars, rettv, TRUE);
3821}
3822
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003823 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003824ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003825{
3826 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3827 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003828 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003829}
3830
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003831/*
3832 * "function()" function
3833 */
3834 static void
3835f_function(typval_T *argvars, typval_T *rettv)
3836{
3837 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003838}
3839
3840/*
3841 * "garbagecollect()" function
3842 */
3843 static void
3844f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3845{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003846 // This is postponed until we are back at the toplevel, because we may be
3847 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003848 want_garbage_collect = TRUE;
3849
Bram Moolenaar2df47312020-09-05 17:30:44 +02003850 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003851 garbage_collect_at_exit = TRUE;
3852}
3853
3854/*
3855 * "get()" function
3856 */
3857 static void
3858f_get(typval_T *argvars, typval_T *rettv)
3859{
3860 listitem_T *li;
3861 list_T *l;
3862 dictitem_T *di;
3863 dict_T *d;
3864 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003865 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003866
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003867 if (argvars[0].v_type == VAR_BLOB)
3868 {
3869 int error = FALSE;
3870 int idx = tv_get_number_chk(&argvars[1], &error);
3871
3872 if (!error)
3873 {
3874 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003875 if (idx < 0)
3876 idx = blob_len(argvars[0].vval.v_blob) + idx;
3877 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3878 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003879 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003880 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003881 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003882 tv = rettv;
3883 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003884 }
3885 }
3886 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003887 {
3888 if ((l = argvars[0].vval.v_list) != NULL)
3889 {
3890 int error = FALSE;
3891
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003892 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003893 if (!error && li != NULL)
3894 tv = &li->li_tv;
3895 }
3896 }
3897 else if (argvars[0].v_type == VAR_DICT)
3898 {
3899 if ((d = argvars[0].vval.v_dict) != NULL)
3900 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003901 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003902 if (di != NULL)
3903 tv = &di->di_tv;
3904 }
3905 }
3906 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3907 {
3908 partial_T *pt;
3909 partial_T fref_pt;
3910
3911 if (argvars[0].v_type == VAR_PARTIAL)
3912 pt = argvars[0].vval.v_partial;
3913 else
3914 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003915 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003916 fref_pt.pt_name = argvars[0].vval.v_string;
3917 pt = &fref_pt;
3918 }
3919
3920 if (pt != NULL)
3921 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003922 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003923 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003924
3925 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3926 {
3927 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003928 n = partial_name(pt);
3929 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003930 rettv->vval.v_string = NULL;
3931 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003932 {
3933 rettv->vval.v_string = vim_strsave(n);
3934 if (rettv->v_type == VAR_FUNC)
3935 func_ref(rettv->vval.v_string);
3936 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003937 }
3938 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003939 {
3940 what_is_dict = TRUE;
3941 if (pt->pt_dict != NULL)
3942 rettv_dict_set(rettv, pt->pt_dict);
3943 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003944 else if (STRCMP(what, "args") == 0)
3945 {
3946 rettv->v_type = VAR_LIST;
3947 if (rettv_list_alloc(rettv) == OK)
3948 {
3949 int i;
3950
3951 for (i = 0; i < pt->pt_argc; ++i)
3952 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3953 }
3954 }
3955 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003956 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003957
3958 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3959 // third argument
3960 if (!what_is_dict)
3961 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003962 }
3963 }
3964 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003965 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003966
3967 if (tv == NULL)
3968 {
3969 if (argvars[2].v_type != VAR_UNKNOWN)
3970 copy_tv(&argvars[2], rettv);
3971 }
3972 else
3973 copy_tv(tv, rettv);
3974}
3975
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003976/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003977 * "getchangelist()" function
3978 */
3979 static void
3980f_getchangelist(typval_T *argvars, typval_T *rettv)
3981{
3982#ifdef FEAT_JUMPLIST
3983 buf_T *buf;
3984 int i;
3985 list_T *l;
3986 dict_T *d;
3987#endif
3988
3989 if (rettv_list_alloc(rettv) != OK)
3990 return;
3991
3992#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003993 if (argvars[0].v_type == VAR_UNKNOWN)
3994 buf = curbuf;
3995 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003996 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003997 if (buf == NULL)
3998 return;
3999
4000 l = list_alloc();
4001 if (l == NULL)
4002 return;
4003
4004 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4005 return;
4006 /*
4007 * The current window change list index tracks only the position in the
4008 * current buffer change list. For other buffers, use the change list
4009 * length as the current index.
4010 */
4011 list_append_number(rettv->vval.v_list,
4012 (varnumber_T)((buf == curwin->w_buffer)
4013 ? curwin->w_changelistidx : buf->b_changelistlen));
4014
4015 for (i = 0; i < buf->b_changelistlen; ++i)
4016 {
4017 if (buf->b_changelist[i].lnum == 0)
4018 continue;
4019 if ((d = dict_alloc()) == NULL)
4020 return;
4021 if (list_append_dict(l, d) == FAIL)
4022 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004023 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4024 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004025 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004026 }
4027#endif
4028}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004029
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004030 static void
4031getpos_both(
4032 typval_T *argvars,
4033 typval_T *rettv,
4034 int getcurpos,
4035 int charcol)
4036{
4037 pos_T *fp = NULL;
4038 pos_T pos;
4039 win_T *wp = curwin;
4040 list_T *l;
4041 int fnum = -1;
4042
4043 if (rettv_list_alloc(rettv) == OK)
4044 {
4045 l = rettv->vval.v_list;
4046 if (getcurpos)
4047 {
4048 if (argvars[0].v_type != VAR_UNKNOWN)
4049 {
4050 wp = find_win_by_nr_or_id(&argvars[0]);
4051 if (wp != NULL)
4052 fp = &wp->w_cursor;
4053 }
4054 else
4055 fp = &curwin->w_cursor;
4056 if (fp != NULL && charcol)
4057 {
4058 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01004059 pos.col =
4060 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004061 fp = &pos;
4062 }
4063 }
4064 else
4065 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
4066 if (fnum != -1)
4067 list_append_number(l, (varnumber_T)fnum);
4068 else
4069 list_append_number(l, (varnumber_T)0);
4070 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4071 : (varnumber_T)0);
4072 list_append_number(l, (fp != NULL)
4073 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4074 : (varnumber_T)0);
4075 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
4076 (varnumber_T)0);
4077 if (getcurpos)
4078 {
4079 int save_set_curswant = curwin->w_set_curswant;
4080 colnr_T save_curswant = curwin->w_curswant;
4081 colnr_T save_virtcol = curwin->w_virtcol;
4082
4083 if (wp == curwin)
4084 update_curswant();
4085 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
4086 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
4087
4088 // Do not change "curswant", as it is unexpected that a get
4089 // function has a side effect.
4090 if (wp == curwin && save_set_curswant)
4091 {
4092 curwin->w_set_curswant = save_set_curswant;
4093 curwin->w_curswant = save_curswant;
4094 curwin->w_virtcol = save_virtcol;
4095 curwin->w_valid &= ~VALID_VIRTCOL;
4096 }
4097 }
4098 }
4099 else
4100 rettv->vval.v_number = FALSE;
4101}
4102
4103/*
4104 * "getcharpos()" function
4105 */
4106 static void
4107f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
4108{
4109 getpos_both(argvars, rettv, FALSE, TRUE);
4110}
4111
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004112/*
4113 * "getcharsearch()" function
4114 */
4115 static void
4116f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4117{
4118 if (rettv_dict_alloc(rettv) != FAIL)
4119 {
4120 dict_T *dict = rettv->vval.v_dict;
4121
Bram Moolenaare0be1672018-07-08 16:50:37 +02004122 dict_add_string(dict, "char", last_csearch());
4123 dict_add_number(dict, "forward", last_csearch_forward());
4124 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004125 }
4126}
4127
4128/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004129 * "getenv()" function
4130 */
4131 static void
4132f_getenv(typval_T *argvars, typval_T *rettv)
4133{
4134 int mustfree = FALSE;
4135 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4136
4137 if (p == NULL)
4138 {
4139 rettv->v_type = VAR_SPECIAL;
4140 rettv->vval.v_number = VVAL_NULL;
4141 return;
4142 }
4143 if (!mustfree)
4144 p = vim_strsave(p);
4145 rettv->vval.v_string = p;
4146 rettv->v_type = VAR_STRING;
4147}
4148
4149/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004150 * "getfontname()" function
4151 */
4152 static void
4153f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4154{
4155 rettv->v_type = VAR_STRING;
4156 rettv->vval.v_string = NULL;
4157#ifdef FEAT_GUI
4158 if (gui.in_use)
4159 {
4160 GuiFont font;
4161 char_u *name = NULL;
4162
4163 if (argvars[0].v_type == VAR_UNKNOWN)
4164 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004165 // Get the "Normal" font. Either the name saved by
4166 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004167 font = gui.norm_font;
4168 name = hl_get_font_name();
4169 }
4170 else
4171 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004172 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004173 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004174 return;
4175 font = gui_mch_get_font(name, FALSE);
4176 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004177 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004178 }
4179 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4180 if (argvars[0].v_type != VAR_UNKNOWN)
4181 gui_mch_free_font(font);
4182 }
4183#endif
4184}
4185
4186/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004187 * "getjumplist()" function
4188 */
4189 static void
4190f_getjumplist(typval_T *argvars, typval_T *rettv)
4191{
4192#ifdef FEAT_JUMPLIST
4193 win_T *wp;
4194 int i;
4195 list_T *l;
4196 dict_T *d;
4197#endif
4198
4199 if (rettv_list_alloc(rettv) != OK)
4200 return;
4201
4202#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004203 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004204 if (wp == NULL)
4205 return;
4206
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004207 cleanup_jumplist(wp, TRUE);
4208
Bram Moolenaar4f505882018-02-10 21:06:32 +01004209 l = list_alloc();
4210 if (l == NULL)
4211 return;
4212
4213 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4214 return;
4215 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4216
4217 for (i = 0; i < wp->w_jumplistlen; ++i)
4218 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004219 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4220 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004221 if ((d = dict_alloc()) == NULL)
4222 return;
4223 if (list_append_dict(l, d) == FAIL)
4224 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004225 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4226 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004227 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004228 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004229 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004230 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004231 }
4232#endif
4233}
4234
4235/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004236 * "getpid()" function
4237 */
4238 static void
4239f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4240{
4241 rettv->vval.v_number = mch_get_pid();
4242}
4243
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004244/*
4245 * "getcurpos()" function
4246 */
4247 static void
4248f_getcurpos(typval_T *argvars, typval_T *rettv)
4249{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004250 getpos_both(argvars, rettv, TRUE, FALSE);
4251}
4252
4253 static void
4254f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4255{
4256 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004257}
4258
4259/*
4260 * "getpos(string)" function
4261 */
4262 static void
4263f_getpos(typval_T *argvars, typval_T *rettv)
4264{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004265 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004266}
4267
4268/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004269 * "getreg()" function
4270 */
4271 static void
4272f_getreg(typval_T *argvars, typval_T *rettv)
4273{
4274 char_u *strregname;
4275 int regname;
4276 int arg2 = FALSE;
4277 int return_list = FALSE;
4278 int error = FALSE;
4279
4280 if (argvars[0].v_type != VAR_UNKNOWN)
4281 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004282 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004283 error = strregname == NULL;
4284 if (argvars[1].v_type != VAR_UNKNOWN)
4285 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004286 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004287 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004288 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004289 }
4290 }
4291 else
4292 strregname = get_vim_var_str(VV_REG);
4293
4294 if (error)
4295 return;
4296
4297 regname = (strregname == NULL ? '"' : *strregname);
4298 if (regname == 0)
4299 regname = '"';
4300
4301 if (return_list)
4302 {
4303 rettv->v_type = VAR_LIST;
4304 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4305 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4306 if (rettv->vval.v_list == NULL)
4307 (void)rettv_list_alloc(rettv);
4308 else
4309 ++rettv->vval.v_list->lv_refcount;
4310 }
4311 else
4312 {
4313 rettv->v_type = VAR_STRING;
4314 rettv->vval.v_string = get_reg_contents(regname,
4315 arg2 ? GREG_EXPR_SRC : 0);
4316 }
4317}
4318
4319/*
4320 * "getregtype()" function
4321 */
4322 static void
4323f_getregtype(typval_T *argvars, typval_T *rettv)
4324{
4325 char_u *strregname;
4326 int regname;
4327 char_u buf[NUMBUFLEN + 2];
4328 long reglen = 0;
4329
4330 if (argvars[0].v_type != VAR_UNKNOWN)
4331 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004332 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004333 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004334 {
4335 rettv->v_type = VAR_STRING;
4336 rettv->vval.v_string = NULL;
4337 return;
4338 }
4339 }
4340 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004341 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004342 strregname = get_vim_var_str(VV_REG);
4343
4344 regname = (strregname == NULL ? '"' : *strregname);
4345 if (regname == 0)
4346 regname = '"';
4347
4348 buf[0] = NUL;
4349 buf[1] = NUL;
4350 switch (get_reg_type(regname, &reglen))
4351 {
4352 case MLINE: buf[0] = 'V'; break;
4353 case MCHAR: buf[0] = 'v'; break;
4354 case MBLOCK:
4355 buf[0] = Ctrl_V;
4356 sprintf((char *)buf + 1, "%ld", reglen + 1);
4357 break;
4358 }
4359 rettv->v_type = VAR_STRING;
4360 rettv->vval.v_string = vim_strsave(buf);
4361}
4362
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004363/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004364 * "gettagstack()" function
4365 */
4366 static void
4367f_gettagstack(typval_T *argvars, typval_T *rettv)
4368{
4369 win_T *wp = curwin; // default is current window
4370
4371 if (rettv_dict_alloc(rettv) != OK)
4372 return;
4373
4374 if (argvars[0].v_type != VAR_UNKNOWN)
4375 {
4376 wp = find_win_by_nr_or_id(&argvars[0]);
4377 if (wp == NULL)
4378 return;
4379 }
4380
4381 get_tagstack(wp, rettv->vval.v_dict);
4382}
4383
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004384/*
4385 * "gettext()" function
4386 */
4387 static void
4388f_gettext(typval_T *argvars, typval_T *rettv)
4389{
4390 if (argvars[0].v_type != VAR_STRING
4391 || argvars[0].vval.v_string == NULL
4392 || *argvars[0].vval.v_string == NUL)
4393 {
4394 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4395 }
4396 else
4397 {
4398 rettv->v_type = VAR_STRING;
4399 rettv->vval.v_string = vim_strsave(
4400 (char_u *)_(argvars[0].vval.v_string));
4401 }
4402}
4403
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004404// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004405#include "version.h"
4406
4407/*
4408 * "has()" function
4409 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004410 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004411f_has(typval_T *argvars, typval_T *rettv)
4412{
4413 int i;
4414 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004415 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004416 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004417 typedef struct {
4418 char *name;
4419 short present;
4420 } has_item_T;
4421 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004422 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004423 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004424#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004425 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004426#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004427 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004428#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004429 },
4430 {"arp",
4431#if defined(AMIGA) && defined(FEAT_ARP)
4432 1
4433#else
4434 0
4435#endif
4436 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004437 {"haiku",
4438#ifdef __HAIKU__
4439 1
4440#else
4441 0
4442#endif
4443 },
4444 {"bsd",
4445#if defined(BSD) && !defined(MACOS_X)
4446 1
4447#else
4448 0
4449#endif
4450 },
4451 {"hpux",
4452#ifdef hpux
4453 1
4454#else
4455 0
4456#endif
4457 },
4458 {"linux",
4459#ifdef __linux__
4460 1
4461#else
4462 0
4463#endif
4464 },
4465 {"mac", // Mac OS X (and, once, Mac OS Classic)
4466#ifdef MACOS_X
4467 1
4468#else
4469 0
4470#endif
4471 },
4472 {"osx", // Mac OS X
4473#ifdef MACOS_X
4474 1
4475#else
4476 0
4477#endif
4478 },
4479 {"macunix", // Mac OS X, with the darwin feature
4480#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4481 1
4482#else
4483 0
4484#endif
4485 },
4486 {"osxdarwin", // synonym for macunix
4487#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4488 1
4489#else
4490 0
4491#endif
4492 },
4493 {"qnx",
4494#ifdef __QNX__
4495 1
4496#else
4497 0
4498#endif
4499 },
4500 {"sun",
4501#ifdef SUN_SYSTEM
4502 1
4503#else
4504 0
4505#endif
4506 },
4507 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004508#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004509 1
4510#else
4511 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004512#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004513 },
4514 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004515#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004516 1
4517#else
4518 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004519#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004520 },
4521 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004522#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004523 1
4524#else
4525 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004526#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004527 },
4528 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004529#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004530 1
4531#else
4532 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004533#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004534 },
4535 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004536#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004537 1
4538#else
4539 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004540#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004541 },
4542 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004543#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004544 1
4545#else
4546 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004547#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004548 },
4549 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004550#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004551 1
4552#else
4553 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004554#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004555 },
4556 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004557#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004558 1
4559#else
4560 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004561#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004562 },
4563 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004564#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004565 1
4566#else
4567 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004568#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004569 },
4570 {"autocmd", 1},
4571 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004572#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004573 1
4574#else
4575 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004576#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004577 },
4578 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004579#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004580 1
4581#else
4582 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004583#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004584 },
4585 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004586#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004587 1
4588#else
4589 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004590#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004591 },
4592 {"balloon_multiline",
4593#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4594 // MS-Windows requires runtime check, see below
4595 1
4596#else
4597 0
4598#endif
4599 },
4600 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004601#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004602 1
4603#else
4604 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004605#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004606 },
4607 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004608#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004609 1
4610#else
4611 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004612#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004613 },
4614 {"all_builtin_terms",
4615#if defined(ALL_BUILTIN_TCAPS)
4616 1
4617#else
4618 0
4619#endif
4620 },
4621 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004622#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004623 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004624 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004625 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004626#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004627 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004628#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004629 },
4630 {"byte_offset",
4631#ifdef FEAT_BYTEOFF
4632 1
4633#else
4634 0
4635#endif
4636 },
4637 {"channel",
4638#ifdef FEAT_JOB_CHANNEL
4639 1
4640#else
4641 0
4642#endif
4643 },
4644 {"cindent",
4645#ifdef FEAT_CINDENT
4646 1
4647#else
4648 0
4649#endif
4650 },
4651 {"clientserver",
4652#ifdef FEAT_CLIENTSERVER
4653 1
4654#else
4655 0
4656#endif
4657 },
4658 {"clipboard",
4659#ifdef FEAT_CLIPBOARD
4660 1
4661#else
4662 0
4663#endif
4664 },
4665 {"cmdline_compl", 1},
4666 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004667 {"cmdwin",
4668#ifdef FEAT_CMDWIN
4669 1
4670#else
4671 0
4672#endif
4673 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004674 {"comments", 1},
4675 {"conceal",
4676#ifdef FEAT_CONCEAL
4677 1
4678#else
4679 0
4680#endif
4681 },
4682 {"cryptv",
4683#ifdef FEAT_CRYPT
4684 1
4685#else
4686 0
4687#endif
4688 },
4689 {"crypt-blowfish",
4690#ifdef FEAT_CRYPT
4691 1
4692#else
4693 0
4694#endif
4695 },
4696 {"crypt-blowfish2",
4697#ifdef FEAT_CRYPT
4698 1
4699#else
4700 0
4701#endif
4702 },
4703 {"cscope",
4704#ifdef FEAT_CSCOPE
4705 1
4706#else
4707 0
4708#endif
4709 },
4710 {"cursorbind", 1},
4711 {"cursorshape",
4712#ifdef CURSOR_SHAPE
4713 1
4714#else
4715 0
4716#endif
4717 },
4718 {"debug",
4719#ifdef DEBUG
4720 1
4721#else
4722 0
4723#endif
4724 },
4725 {"dialog_con",
4726#ifdef FEAT_CON_DIALOG
4727 1
4728#else
4729 0
4730#endif
4731 },
4732 {"dialog_gui",
4733#ifdef FEAT_GUI_DIALOG
4734 1
4735#else
4736 0
4737#endif
4738 },
4739 {"diff",
4740#ifdef FEAT_DIFF
4741 1
4742#else
4743 0
4744#endif
4745 },
4746 {"digraphs",
4747#ifdef FEAT_DIGRAPHS
4748 1
4749#else
4750 0
4751#endif
4752 },
4753 {"directx",
4754#ifdef FEAT_DIRECTX
4755 1
4756#else
4757 0
4758#endif
4759 },
4760 {"dnd",
4761#ifdef FEAT_DND
4762 1
4763#else
4764 0
4765#endif
4766 },
4767 {"emacs_tags",
4768#ifdef FEAT_EMACS_TAGS
4769 1
4770#else
4771 0
4772#endif
4773 },
4774 {"eval", 1}, // always present, of course!
4775 {"ex_extra", 1}, // graduated feature
4776 {"extra_search",
4777#ifdef FEAT_SEARCH_EXTRA
4778 1
4779#else
4780 0
4781#endif
4782 },
4783 {"file_in_path",
4784#ifdef FEAT_SEARCHPATH
4785 1
4786#else
4787 0
4788#endif
4789 },
4790 {"filterpipe",
4791#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4792 1
4793#else
4794 0
4795#endif
4796 },
4797 {"find_in_path",
4798#ifdef FEAT_FIND_ID
4799 1
4800#else
4801 0
4802#endif
4803 },
4804 {"float",
4805#ifdef FEAT_FLOAT
4806 1
4807#else
4808 0
4809#endif
4810 },
4811 {"folding",
4812#ifdef FEAT_FOLDING
4813 1
4814#else
4815 0
4816#endif
4817 },
4818 {"footer",
4819#ifdef FEAT_FOOTER
4820 1
4821#else
4822 0
4823#endif
4824 },
4825 {"fork",
4826#if !defined(USE_SYSTEM) && defined(UNIX)
4827 1
4828#else
4829 0
4830#endif
4831 },
4832 {"gettext",
4833#ifdef FEAT_GETTEXT
4834 1
4835#else
4836 0
4837#endif
4838 },
4839 {"gui",
4840#ifdef FEAT_GUI
4841 1
4842#else
4843 0
4844#endif
4845 },
4846 {"gui_neXtaw",
4847#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4848 1
4849#else
4850 0
4851#endif
4852 },
4853 {"gui_athena",
4854#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4855 1
4856#else
4857 0
4858#endif
4859 },
4860 {"gui_gtk",
4861#ifdef FEAT_GUI_GTK
4862 1
4863#else
4864 0
4865#endif
4866 },
4867 {"gui_gtk2",
4868#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4869 1
4870#else
4871 0
4872#endif
4873 },
4874 {"gui_gtk3",
4875#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4876 1
4877#else
4878 0
4879#endif
4880 },
4881 {"gui_gnome",
4882#ifdef FEAT_GUI_GNOME
4883 1
4884#else
4885 0
4886#endif
4887 },
4888 {"gui_haiku",
4889#ifdef FEAT_GUI_HAIKU
4890 1
4891#else
4892 0
4893#endif
4894 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004895 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004896 {"gui_motif",
4897#ifdef FEAT_GUI_MOTIF
4898 1
4899#else
4900 0
4901#endif
4902 },
4903 {"gui_photon",
4904#ifdef FEAT_GUI_PHOTON
4905 1
4906#else
4907 0
4908#endif
4909 },
4910 {"gui_win32",
4911#ifdef FEAT_GUI_MSWIN
4912 1
4913#else
4914 0
4915#endif
4916 },
4917 {"iconv",
4918#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4919 1
4920#else
4921 0
4922#endif
4923 },
4924 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004925 {"ipv6",
4926#ifdef FEAT_IPV6
4927 1
4928#else
4929 0
4930#endif
4931 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004932 {"job",
4933#ifdef FEAT_JOB_CHANNEL
4934 1
4935#else
4936 0
4937#endif
4938 },
4939 {"jumplist",
4940#ifdef FEAT_JUMPLIST
4941 1
4942#else
4943 0
4944#endif
4945 },
4946 {"keymap",
4947#ifdef FEAT_KEYMAP
4948 1
4949#else
4950 0
4951#endif
4952 },
4953 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4954 {"langmap",
4955#ifdef FEAT_LANGMAP
4956 1
4957#else
4958 0
4959#endif
4960 },
4961 {"libcall",
4962#ifdef FEAT_LIBCALL
4963 1
4964#else
4965 0
4966#endif
4967 },
4968 {"linebreak",
4969#ifdef FEAT_LINEBREAK
4970 1
4971#else
4972 0
4973#endif
4974 },
4975 {"lispindent",
4976#ifdef FEAT_LISP
4977 1
4978#else
4979 0
4980#endif
4981 },
4982 {"listcmds", 1},
4983 {"localmap", 1},
4984 {"lua",
4985#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4986 1
4987#else
4988 0
4989#endif
4990 },
4991 {"menu",
4992#ifdef FEAT_MENU
4993 1
4994#else
4995 0
4996#endif
4997 },
4998 {"mksession",
4999#ifdef FEAT_SESSION
5000 1
5001#else
5002 0
5003#endif
5004 },
5005 {"modify_fname", 1},
5006 {"mouse", 1},
5007 {"mouseshape",
5008#ifdef FEAT_MOUSESHAPE
5009 1
5010#else
5011 0
5012#endif
5013 },
5014 {"mouse_dec",
5015#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
5016 1
5017#else
5018 0
5019#endif
5020 },
5021 {"mouse_gpm",
5022#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
5023 1
5024#else
5025 0
5026#endif
5027 },
5028 {"mouse_jsbterm",
5029#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
5030 1
5031#else
5032 0
5033#endif
5034 },
5035 {"mouse_netterm",
5036#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
5037 1
5038#else
5039 0
5040#endif
5041 },
5042 {"mouse_pterm",
5043#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
5044 1
5045#else
5046 0
5047#endif
5048 },
5049 {"mouse_sgr",
5050#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5051 1
5052#else
5053 0
5054#endif
5055 },
5056 {"mouse_sysmouse",
5057#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
5058 1
5059#else
5060 0
5061#endif
5062 },
5063 {"mouse_urxvt",
5064#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
5065 1
5066#else
5067 0
5068#endif
5069 },
5070 {"mouse_xterm",
5071#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5072 1
5073#else
5074 0
5075#endif
5076 },
5077 {"multi_byte", 1},
5078 {"multi_byte_ime",
5079#ifdef FEAT_MBYTE_IME
5080 1
5081#else
5082 0
5083#endif
5084 },
5085 {"multi_lang",
5086#ifdef FEAT_MULTI_LANG
5087 1
5088#else
5089 0
5090#endif
5091 },
5092 {"mzscheme",
5093#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
5094 1
5095#else
5096 0
5097#endif
5098 },
5099 {"num64", 1},
5100 {"ole",
5101#ifdef FEAT_OLE
5102 1
5103#else
5104 0
5105#endif
5106 },
5107 {"packages",
5108#ifdef FEAT_EVAL
5109 1
5110#else
5111 0
5112#endif
5113 },
5114 {"path_extra",
5115#ifdef FEAT_PATH_EXTRA
5116 1
5117#else
5118 0
5119#endif
5120 },
5121 {"perl",
5122#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
5123 1
5124#else
5125 0
5126#endif
5127 },
5128 {"persistent_undo",
5129#ifdef FEAT_PERSISTENT_UNDO
5130 1
5131#else
5132 0
5133#endif
5134 },
5135 {"python_compiled",
5136#if defined(FEAT_PYTHON)
5137 1
5138#else
5139 0
5140#endif
5141 },
5142 {"python_dynamic",
5143#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
5144 1
5145#else
5146 0
5147#endif
5148 },
5149 {"python",
5150#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
5151 1
5152#else
5153 0
5154#endif
5155 },
5156 {"pythonx",
5157#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
5158 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
5159 1
5160#else
5161 0
5162#endif
5163 },
5164 {"python3_compiled",
5165#if defined(FEAT_PYTHON3)
5166 1
5167#else
5168 0
5169#endif
5170 },
5171 {"python3_dynamic",
5172#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
5173 1
5174#else
5175 0
5176#endif
5177 },
5178 {"python3",
5179#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
5180 1
5181#else
5182 0
5183#endif
5184 },
5185 {"popupwin",
5186#ifdef FEAT_PROP_POPUP
5187 1
5188#else
5189 0
5190#endif
5191 },
5192 {"postscript",
5193#ifdef FEAT_POSTSCRIPT
5194 1
5195#else
5196 0
5197#endif
5198 },
5199 {"printer",
5200#ifdef FEAT_PRINTER
5201 1
5202#else
5203 0
5204#endif
5205 },
5206 {"profile",
5207#ifdef FEAT_PROFILE
5208 1
5209#else
5210 0
5211#endif
5212 },
5213 {"reltime",
5214#ifdef FEAT_RELTIME
5215 1
5216#else
5217 0
5218#endif
5219 },
5220 {"quickfix",
5221#ifdef FEAT_QUICKFIX
5222 1
5223#else
5224 0
5225#endif
5226 },
5227 {"rightleft",
5228#ifdef FEAT_RIGHTLEFT
5229 1
5230#else
5231 0
5232#endif
5233 },
5234 {"ruby",
5235#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5236 1
5237#else
5238 0
5239#endif
5240 },
5241 {"scrollbind", 1},
5242 {"showcmd",
5243#ifdef FEAT_CMDL_INFO
5244 1
5245#else
5246 0
5247#endif
5248 },
5249 {"cmdline_info",
5250#ifdef FEAT_CMDL_INFO
5251 1
5252#else
5253 0
5254#endif
5255 },
5256 {"signs",
5257#ifdef FEAT_SIGNS
5258 1
5259#else
5260 0
5261#endif
5262 },
5263 {"smartindent",
5264#ifdef FEAT_SMARTINDENT
5265 1
5266#else
5267 0
5268#endif
5269 },
5270 {"startuptime",
5271#ifdef STARTUPTIME
5272 1
5273#else
5274 0
5275#endif
5276 },
5277 {"statusline",
5278#ifdef FEAT_STL_OPT
5279 1
5280#else
5281 0
5282#endif
5283 },
5284 {"netbeans_intg",
5285#ifdef FEAT_NETBEANS_INTG
5286 1
5287#else
5288 0
5289#endif
5290 },
5291 {"sound",
5292#ifdef FEAT_SOUND
5293 1
5294#else
5295 0
5296#endif
5297 },
5298 {"spell",
5299#ifdef FEAT_SPELL
5300 1
5301#else
5302 0
5303#endif
5304 },
5305 {"syntax",
5306#ifdef FEAT_SYN_HL
5307 1
5308#else
5309 0
5310#endif
5311 },
5312 {"system",
5313#if defined(USE_SYSTEM) || !defined(UNIX)
5314 1
5315#else
5316 0
5317#endif
5318 },
5319 {"tag_binary",
5320#ifdef FEAT_TAG_BINS
5321 1
5322#else
5323 0
5324#endif
5325 },
5326 {"tcl",
5327#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5328 1
5329#else
5330 0
5331#endif
5332 },
5333 {"termguicolors",
5334#ifdef FEAT_TERMGUICOLORS
5335 1
5336#else
5337 0
5338#endif
5339 },
5340 {"terminal",
5341#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5342 1
5343#else
5344 0
5345#endif
5346 },
5347 {"terminfo",
5348#ifdef TERMINFO
5349 1
5350#else
5351 0
5352#endif
5353 },
5354 {"termresponse",
5355#ifdef FEAT_TERMRESPONSE
5356 1
5357#else
5358 0
5359#endif
5360 },
5361 {"textobjects",
5362#ifdef FEAT_TEXTOBJ
5363 1
5364#else
5365 0
5366#endif
5367 },
5368 {"textprop",
5369#ifdef FEAT_PROP_POPUP
5370 1
5371#else
5372 0
5373#endif
5374 },
5375 {"tgetent",
5376#ifdef HAVE_TGETENT
5377 1
5378#else
5379 0
5380#endif
5381 },
5382 {"timers",
5383#ifdef FEAT_TIMERS
5384 1
5385#else
5386 0
5387#endif
5388 },
5389 {"title",
5390#ifdef FEAT_TITLE
5391 1
5392#else
5393 0
5394#endif
5395 },
5396 {"toolbar",
5397#ifdef FEAT_TOOLBAR
5398 1
5399#else
5400 0
5401#endif
5402 },
5403 {"unnamedplus",
5404#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5405 1
5406#else
5407 0
5408#endif
5409 },
5410 {"user-commands", 1}, // was accidentally included in 5.4
5411 {"user_commands", 1},
5412 {"vartabs",
5413#ifdef FEAT_VARTABS
5414 1
5415#else
5416 0
5417#endif
5418 },
5419 {"vertsplit", 1},
5420 {"viminfo",
5421#ifdef FEAT_VIMINFO
5422 1
5423#else
5424 0
5425#endif
5426 },
5427 {"vimscript-1", 1},
5428 {"vimscript-2", 1},
5429 {"vimscript-3", 1},
5430 {"vimscript-4", 1},
5431 {"virtualedit", 1},
5432 {"visual", 1},
5433 {"visualextra", 1},
5434 {"vreplace", 1},
5435 {"vtp",
5436#ifdef FEAT_VTP
5437 1
5438#else
5439 0
5440#endif
5441 },
5442 {"wildignore",
5443#ifdef FEAT_WILDIGN
5444 1
5445#else
5446 0
5447#endif
5448 },
5449 {"wildmenu",
5450#ifdef FEAT_WILDMENU
5451 1
5452#else
5453 0
5454#endif
5455 },
5456 {"windows", 1},
5457 {"winaltkeys",
5458#ifdef FEAT_WAK
5459 1
5460#else
5461 0
5462#endif
5463 },
5464 {"writebackup",
5465#ifdef FEAT_WRITEBACKUP
5466 1
5467#else
5468 0
5469#endif
5470 },
5471 {"xim",
5472#ifdef FEAT_XIM
5473 1
5474#else
5475 0
5476#endif
5477 },
5478 {"xfontset",
5479#ifdef FEAT_XFONTSET
5480 1
5481#else
5482 0
5483#endif
5484 },
5485 {"xpm",
5486#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5487 1
5488#else
5489 0
5490#endif
5491 },
5492 {"xpm_w32", // for backward compatibility
5493#ifdef FEAT_XPM_W32
5494 1
5495#else
5496 0
5497#endif
5498 },
5499 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005500#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005501 1
5502#else
5503 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005504#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005505 },
5506 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005507#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005508 1
5509#else
5510 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005511#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005512 },
5513 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005514#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005515 1
5516#else
5517 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005518#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005519 },
5520 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005521#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005522 1
5523#else
5524 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005525#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005526 },
5527 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005528#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005529 1
5530#else
5531 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005532#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005533 },
5534 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005535 };
5536
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005537 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005538 for (i = 0; has_list[i].name != NULL; ++i)
5539 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005540 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005541 x = TRUE;
5542 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005543 break;
5544 }
5545
Bram Moolenaar79296512020-03-22 16:17:14 +01005546 // features also in has_list[] but sometimes enabled at runtime
5547 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005548 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005549 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005550 {
5551 // intentionally empty
5552 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005553#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005554 else if (STRICMP(name, "balloon_multiline") == 0)
5555 n = multiline_balloon_available();
5556#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005557#ifdef VIMDLL
5558 else if (STRICMP(name, "filterpipe") == 0)
5559 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005560#endif
5561#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5562 else if (STRICMP(name, "iconv") == 0)
5563 n = iconv_enabled(FALSE);
5564#endif
5565#ifdef DYNAMIC_LUA
5566 else if (STRICMP(name, "lua") == 0)
5567 n = lua_enabled(FALSE);
5568#endif
5569#ifdef DYNAMIC_MZSCHEME
5570 else if (STRICMP(name, "mzscheme") == 0)
5571 n = mzscheme_enabled(FALSE);
5572#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005573#ifdef DYNAMIC_PERL
5574 else if (STRICMP(name, "perl") == 0)
5575 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005576#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005577#ifdef DYNAMIC_PYTHON
5578 else if (STRICMP(name, "python") == 0)
5579 n = python_enabled(FALSE);
5580#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005581#ifdef DYNAMIC_PYTHON3
5582 else if (STRICMP(name, "python3") == 0)
5583 n = python3_enabled(FALSE);
5584#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005585#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5586 else if (STRICMP(name, "pythonx") == 0)
5587 {
5588# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5589 if (p_pyx == 0)
5590 n = python3_enabled(FALSE) || python_enabled(FALSE);
5591 else if (p_pyx == 3)
5592 n = python3_enabled(FALSE);
5593 else if (p_pyx == 2)
5594 n = python_enabled(FALSE);
5595# elif defined(DYNAMIC_PYTHON)
5596 n = python_enabled(FALSE);
5597# elif defined(DYNAMIC_PYTHON3)
5598 n = python3_enabled(FALSE);
5599# endif
5600 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005601#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005602#ifdef DYNAMIC_RUBY
5603 else if (STRICMP(name, "ruby") == 0)
5604 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005605#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005606#ifdef DYNAMIC_TCL
5607 else if (STRICMP(name, "tcl") == 0)
5608 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005609#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005610#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005611 else if (STRICMP(name, "terminal") == 0)
5612 n = terminal_enabled();
5613#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005614 }
5615
Bram Moolenaar79296512020-03-22 16:17:14 +01005616 // features not in has_list[]
5617 if (x == FALSE)
5618 {
5619 if (STRNICMP(name, "patch", 5) == 0)
5620 {
5621 x = TRUE;
5622 if (name[5] == '-'
5623 && STRLEN(name) >= 11
5624 && vim_isdigit(name[6])
5625 && vim_isdigit(name[8])
5626 && vim_isdigit(name[10]))
5627 {
5628 int major = atoi((char *)name + 6);
5629 int minor = atoi((char *)name + 8);
5630
5631 // Expect "patch-9.9.01234".
5632 n = (major < VIM_VERSION_MAJOR
5633 || (major == VIM_VERSION_MAJOR
5634 && (minor < VIM_VERSION_MINOR
5635 || (minor == VIM_VERSION_MINOR
5636 && has_patch(atoi((char *)name + 10))))));
5637 }
5638 else
5639 n = has_patch(atoi((char *)name + 5));
5640 }
5641 else if (STRICMP(name, "vim_starting") == 0)
5642 {
5643 x = TRUE;
5644 n = (starting != 0);
5645 }
5646 else if (STRICMP(name, "ttyin") == 0)
5647 {
5648 x = TRUE;
5649 n = mch_input_isatty();
5650 }
5651 else if (STRICMP(name, "ttyout") == 0)
5652 {
5653 x = TRUE;
5654 n = stdout_isatty;
5655 }
5656 else if (STRICMP(name, "multi_byte_encoding") == 0)
5657 {
5658 x = TRUE;
5659 n = has_mbyte;
5660 }
5661 else if (STRICMP(name, "gui_running") == 0)
5662 {
5663 x = TRUE;
5664#ifdef FEAT_GUI
5665 n = (gui.in_use || gui.starting);
5666#endif
5667 }
5668 else if (STRICMP(name, "browse") == 0)
5669 {
5670 x = TRUE;
5671#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5672 n = gui.in_use; // gui_mch_browse() works when GUI is running
5673#endif
5674 }
5675 else if (STRICMP(name, "syntax_items") == 0)
5676 {
5677 x = TRUE;
5678#ifdef FEAT_SYN_HL
5679 n = syntax_present(curwin);
5680#endif
5681 }
5682 else if (STRICMP(name, "vcon") == 0)
5683 {
5684 x = TRUE;
5685#ifdef FEAT_VTP
5686 n = is_term_win32() && has_vtp_working();
5687#endif
5688 }
5689 else if (STRICMP(name, "netbeans_enabled") == 0)
5690 {
5691 x = TRUE;
5692#ifdef FEAT_NETBEANS_INTG
5693 n = netbeans_active();
5694#endif
5695 }
5696 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5697 {
5698 x = TRUE;
5699#ifdef FEAT_MOUSE_GPM
5700 n = gpm_enabled();
5701#endif
5702 }
5703 else if (STRICMP(name, "conpty") == 0)
5704 {
5705 x = TRUE;
5706#if defined(FEAT_TERMINAL) && defined(MSWIN)
5707 n = use_conpty();
5708#endif
5709 }
5710 else if (STRICMP(name, "clipboard_working") == 0)
5711 {
5712 x = TRUE;
5713#ifdef FEAT_CLIPBOARD
5714 n = clip_star.available;
5715#endif
5716 }
5717 }
5718
Bram Moolenaar04637e22020-09-05 18:45:29 +02005719 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005720 // return whether feature could ever be enabled
5721 rettv->vval.v_number = x;
5722 else
5723 // return whether feature is enabled
5724 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005725}
5726
5727/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005728 * Return TRUE if "feature" can change later.
5729 * Also when checking for the feature has side effects, such as loading a DLL.
5730 */
5731 int
5732dynamic_feature(char_u *feature)
5733{
5734 return (feature == NULL
5735#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5736 || STRICMP(feature, "balloon_multiline") == 0
5737#endif
5738#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5739 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5740#endif
5741#ifdef VIMDLL
5742 || STRICMP(feature, "filterpipe") == 0
5743#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005744#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005745 // this can only change on Unix where the ":gui" command could be
5746 // used.
5747 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5748#endif
5749#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5750 || STRICMP(feature, "iconv") == 0
5751#endif
5752#ifdef DYNAMIC_LUA
5753 || STRICMP(feature, "lua") == 0
5754#endif
5755#ifdef FEAT_MOUSE_GPM
5756 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5757#endif
5758#ifdef DYNAMIC_MZSCHEME
5759 || STRICMP(feature, "mzscheme") == 0
5760#endif
5761#ifdef FEAT_NETBEANS_INTG
5762 || STRICMP(feature, "netbeans_enabled") == 0
5763#endif
5764#ifdef DYNAMIC_PERL
5765 || STRICMP(feature, "perl") == 0
5766#endif
5767#ifdef DYNAMIC_PYTHON
5768 || STRICMP(feature, "python") == 0
5769#endif
5770#ifdef DYNAMIC_PYTHON3
5771 || STRICMP(feature, "python3") == 0
5772#endif
5773#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5774 || STRICMP(feature, "pythonx") == 0
5775#endif
5776#ifdef DYNAMIC_RUBY
5777 || STRICMP(feature, "ruby") == 0
5778#endif
5779#ifdef FEAT_SYN_HL
5780 || STRICMP(feature, "syntax_items") == 0
5781#endif
5782#ifdef DYNAMIC_TCL
5783 || STRICMP(feature, "tcl") == 0
5784#endif
5785 // once "starting" is zero it will stay that way
5786 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5787 || STRICMP(feature, "multi_byte_encoding") == 0
5788#if defined(FEAT_TERMINAL) && defined(MSWIN)
5789 || STRICMP(feature, "conpty") == 0
5790#endif
5791 );
5792}
5793
5794/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005795 * "haslocaldir()" function
5796 */
5797 static void
5798f_haslocaldir(typval_T *argvars, typval_T *rettv)
5799{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005800 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005801 win_T *wp = NULL;
5802
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005803 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5804
5805 // Check for window-local and tab-local directories
5806 if (wp != NULL && wp->w_localdir != NULL)
5807 rettv->vval.v_number = 1;
5808 else if (tp != NULL && tp->tp_localdir != NULL)
5809 rettv->vval.v_number = 2;
5810 else
5811 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005812}
5813
5814/*
5815 * "hasmapto()" function
5816 */
5817 static void
5818f_hasmapto(typval_T *argvars, typval_T *rettv)
5819{
5820 char_u *name;
5821 char_u *mode;
5822 char_u buf[NUMBUFLEN];
5823 int abbr = FALSE;
5824
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005825 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005826 if (argvars[1].v_type == VAR_UNKNOWN)
5827 mode = (char_u *)"nvo";
5828 else
5829 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005830 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005831 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005832 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005833 }
5834
5835 if (map_to_exists(name, mode, abbr))
5836 rettv->vval.v_number = TRUE;
5837 else
5838 rettv->vval.v_number = FALSE;
5839}
5840
5841/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005842 * "highlightID(name)" function
5843 */
5844 static void
5845f_hlID(typval_T *argvars, typval_T *rettv)
5846{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005847 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005848}
5849
5850/*
5851 * "highlight_exists()" function
5852 */
5853 static void
5854f_hlexists(typval_T *argvars, typval_T *rettv)
5855{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005856 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005857}
5858
5859/*
5860 * "hostname()" function
5861 */
5862 static void
5863f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5864{
5865 char_u hostname[256];
5866
5867 mch_get_host_name(hostname, 256);
5868 rettv->v_type = VAR_STRING;
5869 rettv->vval.v_string = vim_strsave(hostname);
5870}
5871
5872/*
5873 * iconv() function
5874 */
5875 static void
5876f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5877{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005878 char_u buf1[NUMBUFLEN];
5879 char_u buf2[NUMBUFLEN];
5880 char_u *from, *to, *str;
5881 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005882
5883 rettv->v_type = VAR_STRING;
5884 rettv->vval.v_string = NULL;
5885
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005886 str = tv_get_string(&argvars[0]);
5887 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5888 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005889 vimconv.vc_type = CONV_NONE;
5890 convert_setup(&vimconv, from, to);
5891
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005892 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005893 if (vimconv.vc_type == CONV_NONE)
5894 rettv->vval.v_string = vim_strsave(str);
5895 else
5896 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5897
5898 convert_setup(&vimconv, NULL, NULL);
5899 vim_free(from);
5900 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005901}
5902
5903/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005904 * "index()" function
5905 */
5906 static void
5907f_index(typval_T *argvars, typval_T *rettv)
5908{
5909 list_T *l;
5910 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005911 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005912 long idx = 0;
5913 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005914 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005915
5916 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005917 if (argvars[0].v_type == VAR_BLOB)
5918 {
5919 typval_T tv;
5920 int start = 0;
5921
5922 if (argvars[2].v_type != VAR_UNKNOWN)
5923 {
5924 start = tv_get_number_chk(&argvars[2], &error);
5925 if (error)
5926 return;
5927 }
5928 b = argvars[0].vval.v_blob;
5929 if (b == NULL)
5930 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005931 if (start < 0)
5932 {
5933 start = blob_len(b) + start;
5934 if (start < 0)
5935 start = 0;
5936 }
5937
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005938 for (idx = start; idx < blob_len(b); ++idx)
5939 {
5940 tv.v_type = VAR_NUMBER;
5941 tv.vval.v_number = blob_get(b, idx);
5942 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5943 {
5944 rettv->vval.v_number = idx;
5945 return;
5946 }
5947 }
5948 return;
5949 }
5950 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005951 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005952 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005953 return;
5954 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005955
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005956 l = argvars[0].vval.v_list;
5957 if (l != NULL)
5958 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005959 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005960 item = l->lv_first;
5961 if (argvars[2].v_type != VAR_UNKNOWN)
5962 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005963 // Start at specified item. Use the cached index that list_find()
5964 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005965 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005966 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005967 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005968 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005969 if (error)
5970 item = NULL;
5971 }
5972
5973 for ( ; item != NULL; item = item->li_next, ++idx)
5974 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5975 {
5976 rettv->vval.v_number = idx;
5977 break;
5978 }
5979 }
5980}
5981
5982static int inputsecret_flag = 0;
5983
5984/*
5985 * "input()" function
5986 * Also handles inputsecret() when inputsecret is set.
5987 */
5988 static void
5989f_input(typval_T *argvars, typval_T *rettv)
5990{
5991 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5992}
5993
5994/*
5995 * "inputdialog()" function
5996 */
5997 static void
5998f_inputdialog(typval_T *argvars, typval_T *rettv)
5999{
6000#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006001 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006002 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6003 {
6004 char_u *message;
6005 char_u buf[NUMBUFLEN];
6006 char_u *defstr = (char_u *)"";
6007
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006008 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006009 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006010 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006011 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6012 else
6013 IObuff[0] = NUL;
6014 if (message != NULL && defstr != NULL
6015 && do_dialog(VIM_QUESTION, NULL, message,
6016 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6017 rettv->vval.v_string = vim_strsave(IObuff);
6018 else
6019 {
6020 if (message != NULL && defstr != NULL
6021 && argvars[1].v_type != VAR_UNKNOWN
6022 && argvars[2].v_type != VAR_UNKNOWN)
6023 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006024 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006025 else
6026 rettv->vval.v_string = NULL;
6027 }
6028 rettv->v_type = VAR_STRING;
6029 }
6030 else
6031#endif
6032 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6033}
6034
6035/*
6036 * "inputlist()" function
6037 */
6038 static void
6039f_inputlist(typval_T *argvars, typval_T *rettv)
6040{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006041 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006042 listitem_T *li;
6043 int selected;
6044 int mouse_used;
6045
6046#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006047 // While starting up, there is no place to enter text. When running tests
6048 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006049 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006050 return;
6051#endif
6052 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6053 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006054 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006055 return;
6056 }
6057
6058 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006059 msg_row = Rows - 1; // for when 'cmdheight' > 1
6060 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006061 msg_scroll = TRUE;
6062 msg_clr_eos();
6063
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006064 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006065 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006066 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006067 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006068 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006069 msg_putchar('\n');
6070 }
6071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006072 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006073 selected = prompt_for_number(&mouse_used);
6074 if (mouse_used)
6075 selected -= lines_left;
6076
6077 rettv->vval.v_number = selected;
6078}
6079
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006080static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6081
6082/*
6083 * "inputrestore()" function
6084 */
6085 static void
6086f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6087{
6088 if (ga_userinput.ga_len > 0)
6089 {
6090 --ga_userinput.ga_len;
6091 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6092 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006093 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006094 }
6095 else if (p_verbose > 1)
6096 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006097 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006098 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006099 }
6100}
6101
6102/*
6103 * "inputsave()" function
6104 */
6105 static void
6106f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6107{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006108 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006109 if (ga_grow(&ga_userinput, 1) == OK)
6110 {
6111 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6112 + ga_userinput.ga_len);
6113 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006114 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006115 }
6116 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006117 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006118}
6119
6120/*
6121 * "inputsecret()" function
6122 */
6123 static void
6124f_inputsecret(typval_T *argvars, typval_T *rettv)
6125{
6126 ++cmdline_star;
6127 ++inputsecret_flag;
6128 f_input(argvars, rettv);
6129 --cmdline_star;
6130 --inputsecret_flag;
6131}
6132
6133/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01006134 * "interrupt()" function
6135 */
6136 static void
6137f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6138{
6139 got_int = TRUE;
6140}
6141
6142/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006143 * "invert(expr)" function
6144 */
6145 static void
6146f_invert(typval_T *argvars, typval_T *rettv)
6147{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006148 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006149}
6150
6151/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006152 * "islocked()" function
6153 */
6154 static void
6155f_islocked(typval_T *argvars, typval_T *rettv)
6156{
6157 lval_T lv;
6158 char_u *end;
6159 dictitem_T *di;
6160
6161 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006162 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006163 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006164 if (end != NULL && lv.ll_name != NULL)
6165 {
6166 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02006167 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006168 else
6169 {
6170 if (lv.ll_tv == NULL)
6171 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006172 di = find_var(lv.ll_name, NULL, TRUE);
6173 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006174 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006175 // Consider a variable locked when:
6176 // 1. the variable itself is locked
6177 // 2. the value of the variable is locked.
6178 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01006179 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6180 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006181 }
6182 }
6183 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006184 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006185 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006186 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006187 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006188 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006189 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6190 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006191 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006192 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6193 }
6194 }
6195
6196 clear_lval(&lv);
6197}
6198
6199#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6200/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006201 * "isinf()" function
6202 */
6203 static void
6204f_isinf(typval_T *argvars, typval_T *rettv)
6205{
6206 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6207 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6208}
6209
6210/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006211 * "isnan()" function
6212 */
6213 static void
6214f_isnan(typval_T *argvars, typval_T *rettv)
6215{
6216 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6217 && isnan(argvars[0].vval.v_float);
6218}
6219#endif
6220
6221/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006222 * "last_buffer_nr()" function.
6223 */
6224 static void
6225f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6226{
6227 int n = 0;
6228 buf_T *buf;
6229
Bram Moolenaar29323592016-07-24 22:04:11 +02006230 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006231 if (n < buf->b_fnum)
6232 n = buf->b_fnum;
6233
6234 rettv->vval.v_number = n;
6235}
6236
6237/*
6238 * "len()" function
6239 */
6240 static void
6241f_len(typval_T *argvars, typval_T *rettv)
6242{
6243 switch (argvars[0].v_type)
6244 {
6245 case VAR_STRING:
6246 case VAR_NUMBER:
6247 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006248 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006249 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006250 case VAR_BLOB:
6251 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6252 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006253 case VAR_LIST:
6254 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6255 break;
6256 case VAR_DICT:
6257 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6258 break;
6259 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006260 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006261 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006262 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006263 case VAR_SPECIAL:
6264 case VAR_FLOAT:
6265 case VAR_FUNC:
6266 case VAR_PARTIAL:
6267 case VAR_JOB:
6268 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006269 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006270 break;
6271 }
6272}
6273
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006274 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006275libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006276{
6277#ifdef FEAT_LIBCALL
6278 char_u *string_in;
6279 char_u **string_result;
6280 int nr_result;
6281#endif
6282
6283 rettv->v_type = type;
6284 if (type != VAR_NUMBER)
6285 rettv->vval.v_string = NULL;
6286
6287 if (check_restricted() || check_secure())
6288 return;
6289
6290#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006291 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006292 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6293 {
6294 string_in = NULL;
6295 if (argvars[2].v_type == VAR_STRING)
6296 string_in = argvars[2].vval.v_string;
6297 if (type == VAR_NUMBER)
6298 string_result = NULL;
6299 else
6300 string_result = &rettv->vval.v_string;
6301 if (mch_libcall(argvars[0].vval.v_string,
6302 argvars[1].vval.v_string,
6303 string_in,
6304 argvars[2].vval.v_number,
6305 string_result,
6306 &nr_result) == OK
6307 && type == VAR_NUMBER)
6308 rettv->vval.v_number = nr_result;
6309 }
6310#endif
6311}
6312
6313/*
6314 * "libcall()" function
6315 */
6316 static void
6317f_libcall(typval_T *argvars, typval_T *rettv)
6318{
6319 libcall_common(argvars, rettv, VAR_STRING);
6320}
6321
6322/*
6323 * "libcallnr()" function
6324 */
6325 static void
6326f_libcallnr(typval_T *argvars, typval_T *rettv)
6327{
6328 libcall_common(argvars, rettv, VAR_NUMBER);
6329}
6330
6331/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006332 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006333 */
6334 static void
6335f_line(typval_T *argvars, typval_T *rettv)
6336{
6337 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006338 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006339 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006340 int id;
6341 tabpage_T *tp;
6342 win_T *wp;
6343 win_T *save_curwin;
6344 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006345
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006346 if (argvars[1].v_type != VAR_UNKNOWN)
6347 {
6348 // use window specified in the second argument
6349 id = (int)tv_get_number(&argvars[1]);
6350 wp = win_id2wp_tp(id, &tp);
6351 if (wp != NULL && tp != NULL)
6352 {
6353 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6354 == OK)
6355 {
6356 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006357 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006358 }
6359 restore_win_noblock(save_curwin, save_curtab, TRUE);
6360 }
6361 }
6362 else
6363 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006364 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006365
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006366 if (fp != NULL)
6367 lnum = fp->lnum;
6368 rettv->vval.v_number = lnum;
6369}
6370
6371/*
6372 * "line2byte(lnum)" function
6373 */
6374 static void
6375f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6376{
6377#ifndef FEAT_BYTEOFF
6378 rettv->vval.v_number = -1;
6379#else
6380 linenr_T lnum;
6381
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006382 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006383 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6384 rettv->vval.v_number = -1;
6385 else
6386 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6387 if (rettv->vval.v_number >= 0)
6388 ++rettv->vval.v_number;
6389#endif
6390}
6391
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006392#ifdef FEAT_FLOAT
6393/*
6394 * "log()" function
6395 */
6396 static void
6397f_log(typval_T *argvars, typval_T *rettv)
6398{
6399 float_T f = 0.0;
6400
6401 rettv->v_type = VAR_FLOAT;
6402 if (get_float_arg(argvars, &f) == OK)
6403 rettv->vval.v_float = log(f);
6404 else
6405 rettv->vval.v_float = 0.0;
6406}
6407
6408/*
6409 * "log10()" function
6410 */
6411 static void
6412f_log10(typval_T *argvars, typval_T *rettv)
6413{
6414 float_T f = 0.0;
6415
6416 rettv->v_type = VAR_FLOAT;
6417 if (get_float_arg(argvars, &f) == OK)
6418 rettv->vval.v_float = log10(f);
6419 else
6420 rettv->vval.v_float = 0.0;
6421}
6422#endif
6423
6424#ifdef FEAT_LUA
6425/*
6426 * "luaeval()" function
6427 */
6428 static void
6429f_luaeval(typval_T *argvars, typval_T *rettv)
6430{
6431 char_u *str;
6432 char_u buf[NUMBUFLEN];
6433
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006434 if (check_restricted() || check_secure())
6435 return;
6436
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006437 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006438 do_luaeval(str, argvars + 1, rettv);
6439}
6440#endif
6441
6442/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006443 * "maparg()" function
6444 */
6445 static void
6446f_maparg(typval_T *argvars, typval_T *rettv)
6447{
6448 get_maparg(argvars, rettv, TRUE);
6449}
6450
6451/*
6452 * "mapcheck()" function
6453 */
6454 static void
6455f_mapcheck(typval_T *argvars, typval_T *rettv)
6456{
6457 get_maparg(argvars, rettv, FALSE);
6458}
6459
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006460typedef enum
6461{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006462 MATCH_END, // matchend()
6463 MATCH_MATCH, // match()
6464 MATCH_STR, // matchstr()
6465 MATCH_LIST, // matchlist()
6466 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006467} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006468
6469 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006470find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006471{
6472 char_u *str = NULL;
6473 long len = 0;
6474 char_u *expr = NULL;
6475 char_u *pat;
6476 regmatch_T regmatch;
6477 char_u patbuf[NUMBUFLEN];
6478 char_u strbuf[NUMBUFLEN];
6479 char_u *save_cpo;
6480 long start = 0;
6481 long nth = 1;
6482 colnr_T startcol = 0;
6483 int match = 0;
6484 list_T *l = NULL;
6485 listitem_T *li = NULL;
6486 long idx = 0;
6487 char_u *tofree = NULL;
6488
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006489 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006490 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006491 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006492
6493 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006494 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006495 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006496 // type MATCH_LIST: return empty list when there are no matches.
6497 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006498 if (rettv_list_alloc(rettv) == FAIL)
6499 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006500 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006501 && (list_append_string(rettv->vval.v_list,
6502 (char_u *)"", 0) == FAIL
6503 || list_append_number(rettv->vval.v_list,
6504 (varnumber_T)-1) == FAIL
6505 || list_append_number(rettv->vval.v_list,
6506 (varnumber_T)-1) == FAIL
6507 || list_append_number(rettv->vval.v_list,
6508 (varnumber_T)-1) == FAIL))
6509 {
6510 list_free(rettv->vval.v_list);
6511 rettv->vval.v_list = NULL;
6512 goto theend;
6513 }
6514 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006515 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006516 {
6517 rettv->v_type = VAR_STRING;
6518 rettv->vval.v_string = NULL;
6519 }
6520
6521 if (argvars[0].v_type == VAR_LIST)
6522 {
6523 if ((l = argvars[0].vval.v_list) == NULL)
6524 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006525 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006526 li = l->lv_first;
6527 }
6528 else
6529 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006530 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006531 len = (long)STRLEN(str);
6532 }
6533
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006534 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006535 if (pat == NULL)
6536 goto theend;
6537
6538 if (argvars[2].v_type != VAR_UNKNOWN)
6539 {
6540 int error = FALSE;
6541
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006542 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006543 if (error)
6544 goto theend;
6545 if (l != NULL)
6546 {
6547 li = list_find(l, start);
6548 if (li == NULL)
6549 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006550 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006551 }
6552 else
6553 {
6554 if (start < 0)
6555 start = 0;
6556 if (start > len)
6557 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006558 // When "count" argument is there ignore matches before "start",
6559 // otherwise skip part of the string. Differs when pattern is "^"
6560 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006561 if (argvars[3].v_type != VAR_UNKNOWN)
6562 startcol = start;
6563 else
6564 {
6565 str += start;
6566 len -= start;
6567 }
6568 }
6569
6570 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006571 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006572 if (error)
6573 goto theend;
6574 }
6575
6576 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6577 if (regmatch.regprog != NULL)
6578 {
6579 regmatch.rm_ic = p_ic;
6580
6581 for (;;)
6582 {
6583 if (l != NULL)
6584 {
6585 if (li == NULL)
6586 {
6587 match = FALSE;
6588 break;
6589 }
6590 vim_free(tofree);
6591 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6592 if (str == NULL)
6593 break;
6594 }
6595
6596 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6597
6598 if (match && --nth <= 0)
6599 break;
6600 if (l == NULL && !match)
6601 break;
6602
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006603 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006604 if (l != NULL)
6605 {
6606 li = li->li_next;
6607 ++idx;
6608 }
6609 else
6610 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006611 startcol = (colnr_T)(regmatch.startp[0]
6612 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006613 if (startcol > (colnr_T)len
6614 || str + startcol <= regmatch.startp[0])
6615 {
6616 match = FALSE;
6617 break;
6618 }
6619 }
6620 }
6621
6622 if (match)
6623 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006624 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006625 {
6626 listitem_T *li1 = rettv->vval.v_list->lv_first;
6627 listitem_T *li2 = li1->li_next;
6628 listitem_T *li3 = li2->li_next;
6629 listitem_T *li4 = li3->li_next;
6630
6631 vim_free(li1->li_tv.vval.v_string);
6632 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006633 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006634 li3->li_tv.vval.v_number =
6635 (varnumber_T)(regmatch.startp[0] - expr);
6636 li4->li_tv.vval.v_number =
6637 (varnumber_T)(regmatch.endp[0] - expr);
6638 if (l != NULL)
6639 li2->li_tv.vval.v_number = (varnumber_T)idx;
6640 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006641 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006642 {
6643 int i;
6644
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006645 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006646 for (i = 0; i < NSUBEXP; ++i)
6647 {
6648 if (regmatch.endp[i] == NULL)
6649 {
6650 if (list_append_string(rettv->vval.v_list,
6651 (char_u *)"", 0) == FAIL)
6652 break;
6653 }
6654 else if (list_append_string(rettv->vval.v_list,
6655 regmatch.startp[i],
6656 (int)(regmatch.endp[i] - regmatch.startp[i]))
6657 == FAIL)
6658 break;
6659 }
6660 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006661 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006662 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006663 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006664 if (l != NULL)
6665 copy_tv(&li->li_tv, rettv);
6666 else
6667 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006668 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006669 }
6670 else if (l != NULL)
6671 rettv->vval.v_number = idx;
6672 else
6673 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006674 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006675 rettv->vval.v_number =
6676 (varnumber_T)(regmatch.startp[0] - str);
6677 else
6678 rettv->vval.v_number =
6679 (varnumber_T)(regmatch.endp[0] - str);
6680 rettv->vval.v_number += (varnumber_T)(str - expr);
6681 }
6682 }
6683 vim_regfree(regmatch.regprog);
6684 }
6685
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006686theend:
6687 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006688 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006689 listitem_remove(rettv->vval.v_list,
6690 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006691 vim_free(tofree);
6692 p_cpo = save_cpo;
6693}
6694
6695/*
6696 * "match()" function
6697 */
6698 static void
6699f_match(typval_T *argvars, typval_T *rettv)
6700{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006701 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006702}
6703
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006704/*
6705 * "matchend()" function
6706 */
6707 static void
6708f_matchend(typval_T *argvars, typval_T *rettv)
6709{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006710 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006711}
6712
6713/*
6714 * "matchlist()" function
6715 */
6716 static void
6717f_matchlist(typval_T *argvars, typval_T *rettv)
6718{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006719 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006720}
6721
6722/*
6723 * "matchstr()" function
6724 */
6725 static void
6726f_matchstr(typval_T *argvars, typval_T *rettv)
6727{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006728 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006729}
6730
6731/*
6732 * "matchstrpos()" function
6733 */
6734 static void
6735f_matchstrpos(typval_T *argvars, typval_T *rettv)
6736{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006737 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006738}
6739
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006740 static void
6741max_min(typval_T *argvars, typval_T *rettv, int domax)
6742{
6743 varnumber_T n = 0;
6744 varnumber_T i;
6745 int error = FALSE;
6746
6747 if (argvars[0].v_type == VAR_LIST)
6748 {
6749 list_T *l;
6750 listitem_T *li;
6751
6752 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006753 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006754 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006755 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006756 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006757 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6758 n = l->lv_u.nonmat.lv_start;
6759 else
6760 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6761 * l->lv_u.nonmat.lv_stride;
6762 }
6763 else
6764 {
6765 li = l->lv_first;
6766 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006767 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006768 n = tv_get_number_chk(&li->li_tv, &error);
6769 for (;;)
6770 {
6771 li = li->li_next;
6772 if (li == NULL)
6773 break;
6774 i = tv_get_number_chk(&li->li_tv, &error);
6775 if (domax ? i > n : i < n)
6776 n = i;
6777 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006778 }
6779 }
6780 }
6781 }
6782 else if (argvars[0].v_type == VAR_DICT)
6783 {
6784 dict_T *d;
6785 int first = TRUE;
6786 hashitem_T *hi;
6787 int todo;
6788
6789 d = argvars[0].vval.v_dict;
6790 if (d != NULL)
6791 {
6792 todo = (int)d->dv_hashtab.ht_used;
6793 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6794 {
6795 if (!HASHITEM_EMPTY(hi))
6796 {
6797 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006798 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006799 if (first)
6800 {
6801 n = i;
6802 first = FALSE;
6803 }
6804 else if (domax ? i > n : i < n)
6805 n = i;
6806 }
6807 }
6808 }
6809 }
6810 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006811 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006812 rettv->vval.v_number = error ? 0 : n;
6813}
6814
6815/*
6816 * "max()" function
6817 */
6818 static void
6819f_max(typval_T *argvars, typval_T *rettv)
6820{
6821 max_min(argvars, rettv, TRUE);
6822}
6823
6824/*
6825 * "min()" function
6826 */
6827 static void
6828f_min(typval_T *argvars, typval_T *rettv)
6829{
6830 max_min(argvars, rettv, FALSE);
6831}
6832
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006833#if defined(FEAT_MZSCHEME) || defined(PROTO)
6834/*
6835 * "mzeval()" function
6836 */
6837 static void
6838f_mzeval(typval_T *argvars, typval_T *rettv)
6839{
6840 char_u *str;
6841 char_u buf[NUMBUFLEN];
6842
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006843 if (check_restricted() || check_secure())
6844 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006845 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006846 do_mzeval(str, rettv);
6847}
6848
6849 void
6850mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6851{
6852 typval_T argvars[3];
6853
6854 argvars[0].v_type = VAR_STRING;
6855 argvars[0].vval.v_string = name;
6856 copy_tv(args, &argvars[1]);
6857 argvars[2].v_type = VAR_UNKNOWN;
6858 f_call(argvars, rettv);
6859 clear_tv(&argvars[1]);
6860}
6861#endif
6862
6863/*
6864 * "nextnonblank()" function
6865 */
6866 static void
6867f_nextnonblank(typval_T *argvars, typval_T *rettv)
6868{
6869 linenr_T lnum;
6870
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006871 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006872 {
6873 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6874 {
6875 lnum = 0;
6876 break;
6877 }
6878 if (*skipwhite(ml_get(lnum)) != NUL)
6879 break;
6880 }
6881 rettv->vval.v_number = lnum;
6882}
6883
6884/*
6885 * "nr2char()" function
6886 */
6887 static void
6888f_nr2char(typval_T *argvars, typval_T *rettv)
6889{
6890 char_u buf[NUMBUFLEN];
6891
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006892 if (has_mbyte)
6893 {
6894 int utf8 = 0;
6895
6896 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006897 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006898 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006899 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006901 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006902 }
6903 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006904 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006905 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006906 buf[1] = NUL;
6907 }
6908 rettv->v_type = VAR_STRING;
6909 rettv->vval.v_string = vim_strsave(buf);
6910}
6911
6912/*
6913 * "or(expr, expr)" function
6914 */
6915 static void
6916f_or(typval_T *argvars, typval_T *rettv)
6917{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006918 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6919 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006920}
6921
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006922#ifdef FEAT_PERL
6923/*
6924 * "perleval()" function
6925 */
6926 static void
6927f_perleval(typval_T *argvars, typval_T *rettv)
6928{
6929 char_u *str;
6930 char_u buf[NUMBUFLEN];
6931
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006932 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006933 do_perleval(str, rettv);
6934}
6935#endif
6936
6937#ifdef FEAT_FLOAT
6938/*
6939 * "pow()" function
6940 */
6941 static void
6942f_pow(typval_T *argvars, typval_T *rettv)
6943{
6944 float_T fx = 0.0, fy = 0.0;
6945
6946 rettv->v_type = VAR_FLOAT;
6947 if (get_float_arg(argvars, &fx) == OK
6948 && get_float_arg(&argvars[1], &fy) == OK)
6949 rettv->vval.v_float = pow(fx, fy);
6950 else
6951 rettv->vval.v_float = 0.0;
6952}
6953#endif
6954
6955/*
6956 * "prevnonblank()" function
6957 */
6958 static void
6959f_prevnonblank(typval_T *argvars, typval_T *rettv)
6960{
6961 linenr_T lnum;
6962
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006963 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006964 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6965 lnum = 0;
6966 else
6967 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6968 --lnum;
6969 rettv->vval.v_number = lnum;
6970}
6971
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006972// This dummy va_list is here because:
6973// - passing a NULL pointer doesn't work when va_list isn't a pointer
6974// - locally in the function results in a "used before set" warning
6975// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006976static va_list ap;
6977
6978/*
6979 * "printf()" function
6980 */
6981 static void
6982f_printf(typval_T *argvars, typval_T *rettv)
6983{
6984 char_u buf[NUMBUFLEN];
6985 int len;
6986 char_u *s;
6987 int saved_did_emsg = did_emsg;
6988 char *fmt;
6989
6990 rettv->v_type = VAR_STRING;
6991 rettv->vval.v_string = NULL;
6992
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006993 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006994 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006995 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006996 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006997 if (!did_emsg)
6998 {
6999 s = alloc(len + 1);
7000 if (s != NULL)
7001 {
7002 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007003 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
7004 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007005 }
7006 }
7007 did_emsg |= saved_did_emsg;
7008}
7009
7010/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007011 * "pum_getpos()" function
7012 */
7013 static void
7014f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7015{
7016 if (rettv_dict_alloc(rettv) != OK)
7017 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007018 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007019}
7020
7021/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007022 * "pumvisible()" function
7023 */
7024 static void
7025f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7026{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007027 if (pum_visible())
7028 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007029}
7030
7031#ifdef FEAT_PYTHON3
7032/*
7033 * "py3eval()" function
7034 */
7035 static void
7036f_py3eval(typval_T *argvars, typval_T *rettv)
7037{
7038 char_u *str;
7039 char_u buf[NUMBUFLEN];
7040
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007041 if (check_restricted() || check_secure())
7042 return;
7043
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007044 if (p_pyx == 0)
7045 p_pyx = 3;
7046
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007047 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007048 do_py3eval(str, rettv);
7049}
7050#endif
7051
7052#ifdef FEAT_PYTHON
7053/*
7054 * "pyeval()" function
7055 */
7056 static void
7057f_pyeval(typval_T *argvars, typval_T *rettv)
7058{
7059 char_u *str;
7060 char_u buf[NUMBUFLEN];
7061
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007062 if (check_restricted() || check_secure())
7063 return;
7064
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007065 if (p_pyx == 0)
7066 p_pyx = 2;
7067
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007068 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007069 do_pyeval(str, rettv);
7070}
7071#endif
7072
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007073#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
7074/*
7075 * "pyxeval()" function
7076 */
7077 static void
7078f_pyxeval(typval_T *argvars, typval_T *rettv)
7079{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007080 if (check_restricted() || check_secure())
7081 return;
7082
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007083# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
7084 init_pyxversion();
7085 if (p_pyx == 2)
7086 f_pyeval(argvars, rettv);
7087 else
7088 f_py3eval(argvars, rettv);
7089# elif defined(FEAT_PYTHON)
7090 f_pyeval(argvars, rettv);
7091# elif defined(FEAT_PYTHON3)
7092 f_py3eval(argvars, rettv);
7093# endif
7094}
7095#endif
7096
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007097static UINT32_T srand_seed_for_testing = 0;
7098static int srand_seed_for_testing_is_used = FALSE;
7099
7100 static void
7101f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
7102{
7103 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007104 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007105 else
7106 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007107 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
7108 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007109 }
7110}
7111
7112 static void
7113init_srand(UINT32_T *x)
7114{
7115#ifndef MSWIN
7116 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
7117#endif
7118
7119 if (srand_seed_for_testing_is_used)
7120 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007121 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007122 return;
7123 }
7124#ifndef MSWIN
7125 if (dev_urandom_state != FAIL)
7126 {
7127 int fd = open("/dev/urandom", O_RDONLY);
7128 struct {
7129 union {
7130 UINT32_T number;
7131 char bytes[sizeof(UINT32_T)];
7132 } contents;
7133 } buf;
7134
7135 // Attempt reading /dev/urandom.
7136 if (fd == -1)
7137 dev_urandom_state = FAIL;
7138 else
7139 {
7140 buf.contents.number = 0;
7141 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
7142 != sizeof(UINT32_T))
7143 dev_urandom_state = FAIL;
7144 else
7145 {
7146 dev_urandom_state = OK;
7147 *x = buf.contents.number;
7148 }
7149 close(fd);
7150 }
7151 }
7152 if (dev_urandom_state != OK)
7153 // Reading /dev/urandom doesn't work, fall back to time().
7154#endif
7155 *x = vim_time();
7156}
7157
7158#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
7159#define SPLITMIX32(x, z) ( \
7160 z = (x += 0x9e3779b9), \
7161 z = (z ^ (z >> 16)) * 0x85ebca6b, \
7162 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
7163 z ^ (z >> 16) \
7164 )
7165#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
7166 result = ROTL(y * 5, 7) * 9; \
7167 t = y << 9; \
7168 z ^= x; \
7169 w ^= y; \
7170 y ^= z, x ^= w; \
7171 z ^= t; \
7172 w = ROTL(w, 11);
7173
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007174/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007175 * "rand()" function
7176 */
7177 static void
7178f_rand(typval_T *argvars, typval_T *rettv)
7179{
7180 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007181 static UINT32_T gx, gy, gz, gw;
7182 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007183 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01007184 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007185
7186 if (argvars[0].v_type == VAR_UNKNOWN)
7187 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007188 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007189 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007190 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007191 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007192 init_srand(&x);
7193
7194 gx = SPLITMIX32(x, z);
7195 gy = SPLITMIX32(x, z);
7196 gz = SPLITMIX32(x, z);
7197 gw = SPLITMIX32(x, z);
7198 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007199 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007200
7201 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007202 }
7203 else if (argvars[0].v_type == VAR_LIST)
7204 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007205 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007206 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007207 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007208
7209 lx = list_find(l, 0L);
7210 ly = list_find(l, 1L);
7211 lz = list_find(l, 2L);
7212 lw = list_find(l, 3L);
7213 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
7214 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
7215 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
7216 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
7217 x = (UINT32_T)lx->li_tv.vval.v_number;
7218 y = (UINT32_T)ly->li_tv.vval.v_number;
7219 z = (UINT32_T)lz->li_tv.vval.v_number;
7220 w = (UINT32_T)lw->li_tv.vval.v_number;
7221
7222 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
7223
7224 lx->li_tv.vval.v_number = (varnumber_T)x;
7225 ly->li_tv.vval.v_number = (varnumber_T)y;
7226 lz->li_tv.vval.v_number = (varnumber_T)z;
7227 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007228 }
7229 else
7230 goto theend;
7231
7232 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007233 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007234 return;
7235
7236theend:
7237 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007238 rettv->v_type = VAR_NUMBER;
7239 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007240}
7241
7242/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007243 * "srand()" function
7244 */
7245 static void
7246f_srand(typval_T *argvars, typval_T *rettv)
7247{
7248 UINT32_T x = 0, z;
7249
7250 if (rettv_list_alloc(rettv) == FAIL)
7251 return;
7252 if (argvars[0].v_type == VAR_UNKNOWN)
7253 {
7254 init_srand(&x);
7255 }
7256 else
7257 {
7258 int error = FALSE;
7259
7260 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7261 if (error)
7262 return;
7263 }
7264
7265 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7266 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7267 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7268 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7269}
7270
7271#undef ROTL
7272#undef SPLITMIX32
7273#undef SHUFFLE_XOSHIRO128STARSTAR
7274
7275/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007276 * "range()" function
7277 */
7278 static void
7279f_range(typval_T *argvars, typval_T *rettv)
7280{
7281 varnumber_T start;
7282 varnumber_T end;
7283 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007284 int error = FALSE;
7285
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007286 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007287 if (argvars[1].v_type == VAR_UNKNOWN)
7288 {
7289 end = start - 1;
7290 start = 0;
7291 }
7292 else
7293 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007294 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007295 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007296 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007297 }
7298
7299 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007300 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007301 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007302 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007303 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007304 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007305 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007306 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007307 list_T *list = rettv->vval.v_list;
7308
7309 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007310 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007311 // be called.
7312 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007313 list->lv_u.nonmat.lv_start = start;
7314 list->lv_u.nonmat.lv_end = end;
7315 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007316 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007317 }
7318}
7319
7320/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007321 * Materialize "list".
7322 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007323 */
7324 void
7325range_list_materialize(list_T *list)
7326{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007327 varnumber_T start = list->lv_u.nonmat.lv_start;
7328 varnumber_T end = list->lv_u.nonmat.lv_end;
7329 int stride = list->lv_u.nonmat.lv_stride;
7330 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007331
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007332 list->lv_first = NULL;
7333 list->lv_u.mat.lv_last = NULL;
7334 list->lv_len = 0;
7335 list->lv_u.mat.lv_idx_item = NULL;
7336 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7337 if (list_append_number(list, (varnumber_T)i) == FAIL)
7338 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007339}
7340
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007341/*
7342 * "getreginfo()" function
7343 */
7344 static void
7345f_getreginfo(typval_T *argvars, typval_T *rettv)
7346{
7347 char_u *strregname;
7348 int regname;
7349 char_u buf[NUMBUFLEN + 2];
7350 long reglen = 0;
7351 dict_T *dict;
7352 list_T *list;
7353
7354 if (argvars[0].v_type != VAR_UNKNOWN)
7355 {
7356 strregname = tv_get_string_chk(&argvars[0]);
7357 if (strregname == NULL)
7358 return;
7359 }
7360 else
7361 strregname = get_vim_var_str(VV_REG);
7362
7363 regname = (strregname == NULL ? '"' : *strregname);
7364 if (regname == 0 || regname == '@')
7365 regname = '"';
7366
7367 if (rettv_dict_alloc(rettv) == FAIL)
7368 return;
7369 dict = rettv->vval.v_dict;
7370
7371 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7372 if (list == NULL)
7373 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007374 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007375
7376 buf[0] = NUL;
7377 buf[1] = NUL;
7378 switch (get_reg_type(regname, &reglen))
7379 {
7380 case MLINE: buf[0] = 'V'; break;
7381 case MCHAR: buf[0] = 'v'; break;
7382 case MBLOCK:
7383 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7384 reglen + 1);
7385 break;
7386 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007387 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007388
7389 buf[0] = get_register_name(get_unname_register());
7390 buf[1] = NUL;
7391 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007392 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007393 else
7394 {
7395 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7396
7397 if (item != NULL)
7398 {
7399 item->di_tv.v_type = VAR_SPECIAL;
7400 item->di_tv.vval.v_number = regname == buf[0]
7401 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007402 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007403 }
7404 }
7405}
7406
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007407 static void
7408return_register(int regname, typval_T *rettv)
7409{
7410 char_u buf[2] = {0, 0};
7411
7412 buf[0] = (char_u)regname;
7413 rettv->v_type = VAR_STRING;
7414 rettv->vval.v_string = vim_strsave(buf);
7415}
7416
7417/*
7418 * "reg_executing()" function
7419 */
7420 static void
7421f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7422{
7423 return_register(reg_executing, rettv);
7424}
7425
7426/*
7427 * "reg_recording()" function
7428 */
7429 static void
7430f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7431{
7432 return_register(reg_recording, rettv);
7433}
7434
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007435/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007436 * "rename({from}, {to})" function
7437 */
7438 static void
7439f_rename(typval_T *argvars, typval_T *rettv)
7440{
7441 char_u buf[NUMBUFLEN];
7442
7443 if (check_restricted() || check_secure())
7444 rettv->vval.v_number = -1;
7445 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007446 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7447 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007448}
7449
7450/*
7451 * "repeat()" function
7452 */
7453 static void
7454f_repeat(typval_T *argvars, typval_T *rettv)
7455{
7456 char_u *p;
7457 int n;
7458 int slen;
7459 int len;
7460 char_u *r;
7461 int i;
7462
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007463 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007464 if (argvars[0].v_type == VAR_LIST)
7465 {
7466 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7467 while (n-- > 0)
7468 if (list_extend(rettv->vval.v_list,
7469 argvars[0].vval.v_list, NULL) == FAIL)
7470 break;
7471 }
7472 else
7473 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007474 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007475 rettv->v_type = VAR_STRING;
7476 rettv->vval.v_string = NULL;
7477
7478 slen = (int)STRLEN(p);
7479 len = slen * n;
7480 if (len <= 0)
7481 return;
7482
7483 r = alloc(len + 1);
7484 if (r != NULL)
7485 {
7486 for (i = 0; i < n; i++)
7487 mch_memmove(r + i * slen, p, (size_t)slen);
7488 r[len] = NUL;
7489 }
7490
7491 rettv->vval.v_string = r;
7492 }
7493}
7494
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007495#define SP_NOMOVE 0x01 // don't move cursor
7496#define SP_REPEAT 0x02 // repeat to find outer pair
7497#define SP_RETCOUNT 0x04 // return matchcount
7498#define SP_SETPCMARK 0x08 // set previous context mark
7499#define SP_START 0x10 // accept match at start position
7500#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7501#define SP_END 0x40 // leave cursor at end of match
7502#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007503
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007504/*
7505 * Get flags for a search function.
7506 * Possibly sets "p_ws".
7507 * Returns BACKWARD, FORWARD or zero (for an error).
7508 */
7509 static int
7510get_search_arg(typval_T *varp, int *flagsp)
7511{
7512 int dir = FORWARD;
7513 char_u *flags;
7514 char_u nbuf[NUMBUFLEN];
7515 int mask;
7516
7517 if (varp->v_type != VAR_UNKNOWN)
7518 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007519 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007520 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007521 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007522 while (*flags != NUL)
7523 {
7524 switch (*flags)
7525 {
7526 case 'b': dir = BACKWARD; break;
7527 case 'w': p_ws = TRUE; break;
7528 case 'W': p_ws = FALSE; break;
7529 default: mask = 0;
7530 if (flagsp != NULL)
7531 switch (*flags)
7532 {
7533 case 'c': mask = SP_START; break;
7534 case 'e': mask = SP_END; break;
7535 case 'm': mask = SP_RETCOUNT; break;
7536 case 'n': mask = SP_NOMOVE; break;
7537 case 'p': mask = SP_SUBPAT; break;
7538 case 'r': mask = SP_REPEAT; break;
7539 case 's': mask = SP_SETPCMARK; break;
7540 case 'z': mask = SP_COLUMN; break;
7541 }
7542 if (mask == 0)
7543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007544 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007545 dir = 0;
7546 }
7547 else
7548 *flagsp |= mask;
7549 }
7550 if (dir == 0)
7551 break;
7552 ++flags;
7553 }
7554 }
7555 return dir;
7556}
7557
7558/*
7559 * Shared by search() and searchpos() functions.
7560 */
7561 static int
7562search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7563{
7564 int flags;
7565 char_u *pat;
7566 pos_T pos;
7567 pos_T save_cursor;
7568 int save_p_ws = p_ws;
7569 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007570 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007571 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007572#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007573 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007574 long time_limit = 0;
7575#endif
7576 int options = SEARCH_KEEP;
7577 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007578 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007579 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007580 pos_T firstpos;
7581
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007582 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007583 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007584 if (dir == 0)
7585 goto theend;
7586 flags = *flagsp;
7587 if (flags & SP_START)
7588 options |= SEARCH_START;
7589 if (flags & SP_END)
7590 options |= SEARCH_END;
7591 if (flags & SP_COLUMN)
7592 options |= SEARCH_COL;
7593
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007594 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007595 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7596 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007597 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598 if (lnum_stop < 0)
7599 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007600 if (argvars[3].v_type != VAR_UNKNOWN)
7601 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007602#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007603 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007604 if (time_limit < 0)
7605 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007606#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007607 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007608 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007609 }
7610
7611#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007612 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007613 profile_setlimit(time_limit, &tm);
7614#endif
7615
7616 /*
7617 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7618 * Check to make sure only those flags are set.
7619 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7620 * flags cannot be set. Check for that condition also.
7621 */
7622 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7623 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7624 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007625 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007626 goto theend;
7627 }
7628
7629 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007630 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007631 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007632 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7633#ifdef FEAT_RELTIME
7634 sia.sa_tm = &tm;
7635#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007636
7637 // Repeat until {skip} returns FALSE.
7638 for (;;)
7639 {
7640 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007641 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007642 // finding the first match again means there is no match where {skip}
7643 // evaluates to zero.
7644 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7645 subpatnum = FAIL;
7646
Bram Moolenaara9c01042020-06-07 14:50:50 +02007647 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007648 // didn't find it or no skip argument
7649 break;
7650 firstpos = pos;
7651
Bram Moolenaara9c01042020-06-07 14:50:50 +02007652 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007653 {
7654 int do_skip;
7655 int err;
7656 pos_T save_pos = curwin->w_cursor;
7657
7658 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007659 err = FALSE;
7660 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007661 curwin->w_cursor = save_pos;
7662 if (err)
7663 {
7664 // Evaluating {skip} caused an error, break here.
7665 subpatnum = FAIL;
7666 break;
7667 }
7668 if (!do_skip)
7669 break;
7670 }
7671 }
7672
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007673 if (subpatnum != FAIL)
7674 {
7675 if (flags & SP_SUBPAT)
7676 retval = subpatnum;
7677 else
7678 retval = pos.lnum;
7679 if (flags & SP_SETPCMARK)
7680 setpcmark();
7681 curwin->w_cursor = pos;
7682 if (match_pos != NULL)
7683 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007684 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007685 match_pos->lnum = pos.lnum;
7686 match_pos->col = pos.col + 1;
7687 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007688 // "/$" will put the cursor after the end of the line, may need to
7689 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007690 check_cursor();
7691 }
7692
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007693 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007694 if (flags & SP_NOMOVE)
7695 curwin->w_cursor = save_cursor;
7696 else
7697 curwin->w_set_curswant = TRUE;
7698theend:
7699 p_ws = save_p_ws;
7700
7701 return retval;
7702}
7703
7704#ifdef FEAT_FLOAT
7705
7706/*
7707 * round() is not in C90, use ceil() or floor() instead.
7708 */
7709 float_T
7710vim_round(float_T f)
7711{
7712 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
7713}
7714
7715/*
7716 * "round({float})" function
7717 */
7718 static void
7719f_round(typval_T *argvars, typval_T *rettv)
7720{
7721 float_T f = 0.0;
7722
7723 rettv->v_type = VAR_FLOAT;
7724 if (get_float_arg(argvars, &f) == OK)
7725 rettv->vval.v_float = vim_round(f);
7726 else
7727 rettv->vval.v_float = 0.0;
7728}
7729#endif
7730
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007731#ifdef FEAT_RUBY
7732/*
7733 * "rubyeval()" function
7734 */
7735 static void
7736f_rubyeval(typval_T *argvars, typval_T *rettv)
7737{
7738 char_u *str;
7739 char_u buf[NUMBUFLEN];
7740
7741 str = tv_get_string_buf(&argvars[0], buf);
7742 do_rubyeval(str, rettv);
7743}
7744#endif
7745
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007746/*
7747 * "screenattr()" function
7748 */
7749 static void
7750f_screenattr(typval_T *argvars, typval_T *rettv)
7751{
7752 int row;
7753 int col;
7754 int c;
7755
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007756 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7757 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007758 if (row < 0 || row >= screen_Rows
7759 || col < 0 || col >= screen_Columns)
7760 c = -1;
7761 else
7762 c = ScreenAttrs[LineOffset[row] + col];
7763 rettv->vval.v_number = c;
7764}
7765
7766/*
7767 * "screenchar()" function
7768 */
7769 static void
7770f_screenchar(typval_T *argvars, typval_T *rettv)
7771{
7772 int row;
7773 int col;
7774 int off;
7775 int c;
7776
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007777 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7778 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007779 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007780 c = -1;
7781 else
7782 {
7783 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007784 if (enc_utf8 && ScreenLinesUC[off] != 0)
7785 c = ScreenLinesUC[off];
7786 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007787 c = ScreenLines[off];
7788 }
7789 rettv->vval.v_number = c;
7790}
7791
7792/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007793 * "screenchars()" function
7794 */
7795 static void
7796f_screenchars(typval_T *argvars, typval_T *rettv)
7797{
7798 int row;
7799 int col;
7800 int off;
7801 int c;
7802 int i;
7803
7804 if (rettv_list_alloc(rettv) == FAIL)
7805 return;
7806 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7807 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7808 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7809 return;
7810
7811 off = LineOffset[row] + col;
7812 if (enc_utf8 && ScreenLinesUC[off] != 0)
7813 c = ScreenLinesUC[off];
7814 else
7815 c = ScreenLines[off];
7816 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7817
7818 if (enc_utf8)
7819
7820 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7821 list_append_number(rettv->vval.v_list,
7822 (varnumber_T)ScreenLinesC[i][off]);
7823}
7824
7825/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007826 * "screencol()" function
7827 *
7828 * First column is 1 to be consistent with virtcol().
7829 */
7830 static void
7831f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7832{
7833 rettv->vval.v_number = screen_screencol() + 1;
7834}
7835
7836/*
7837 * "screenrow()" function
7838 */
7839 static void
7840f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7841{
7842 rettv->vval.v_number = screen_screenrow() + 1;
7843}
7844
7845/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007846 * "screenstring()" function
7847 */
7848 static void
7849f_screenstring(typval_T *argvars, typval_T *rettv)
7850{
7851 int row;
7852 int col;
7853 int off;
7854 int c;
7855 int i;
7856 char_u buf[MB_MAXBYTES + 1];
7857 int buflen = 0;
7858
7859 rettv->vval.v_string = NULL;
7860 rettv->v_type = VAR_STRING;
7861
7862 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7863 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7864 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7865 return;
7866
7867 off = LineOffset[row] + col;
7868 if (enc_utf8 && ScreenLinesUC[off] != 0)
7869 c = ScreenLinesUC[off];
7870 else
7871 c = ScreenLines[off];
7872 buflen += mb_char2bytes(c, buf);
7873
7874 if (enc_utf8)
7875 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7876 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7877
7878 buf[buflen] = NUL;
7879 rettv->vval.v_string = vim_strsave(buf);
7880}
7881
7882/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007883 * "search()" function
7884 */
7885 static void
7886f_search(typval_T *argvars, typval_T *rettv)
7887{
7888 int flags = 0;
7889
7890 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7891}
7892
7893/*
7894 * "searchdecl()" function
7895 */
7896 static void
7897f_searchdecl(typval_T *argvars, typval_T *rettv)
7898{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007899 int locally = TRUE;
7900 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007901 int error = FALSE;
7902 char_u *name;
7903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007904 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007905
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007906 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007907 if (argvars[1].v_type != VAR_UNKNOWN)
7908 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007909 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007910 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007911 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007912 }
7913 if (!error && name != NULL)
7914 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7915 locally, thisblock, SEARCH_KEEP) == FAIL;
7916}
7917
7918/*
7919 * Used by searchpair() and searchpairpos()
7920 */
7921 static int
7922searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7923{
7924 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007925 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007926 int save_p_ws = p_ws;
7927 int dir;
7928 int flags = 0;
7929 char_u nbuf1[NUMBUFLEN];
7930 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007931 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007932 long lnum_stop = 0;
7933 long time_limit = 0;
7934
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007935 // Get the three pattern arguments: start, middle, end. Will result in an
7936 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007937 spat = tv_get_string_chk(&argvars[0]);
7938 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7939 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007940 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007941 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007943 // Handle the optional fourth argument: flags
7944 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007945 if (dir == 0)
7946 goto theend;
7947
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007948 // Don't accept SP_END or SP_SUBPAT.
7949 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007950 if ((flags & (SP_END | SP_SUBPAT)) != 0
7951 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7952 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007953 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007954 goto theend;
7955 }
7956
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007957 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007958 if (flags & SP_REPEAT)
7959 p_ws = FALSE;
7960
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007961 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007962 if (argvars[3].v_type == VAR_UNKNOWN
7963 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007964 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007965 else
7966 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007967 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007968 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007969
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007970 if (argvars[5].v_type != VAR_UNKNOWN)
7971 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007972 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007973 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007974 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007975 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007976 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007977 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007978#ifdef FEAT_RELTIME
7979 if (argvars[6].v_type != VAR_UNKNOWN)
7980 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007981 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007982 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007983 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007984 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007985 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007986 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007987 }
7988#endif
7989 }
7990 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007991
7992 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
7993 match_pos, lnum_stop, time_limit);
7994
7995theend:
7996 p_ws = save_p_ws;
7997
7998 return retval;
7999}
8000
8001/*
8002 * "searchpair()" function
8003 */
8004 static void
8005f_searchpair(typval_T *argvars, typval_T *rettv)
8006{
8007 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
8008}
8009
8010/*
8011 * "searchpairpos()" function
8012 */
8013 static void
8014f_searchpairpos(typval_T *argvars, typval_T *rettv)
8015{
8016 pos_T match_pos;
8017 int lnum = 0;
8018 int col = 0;
8019
8020 if (rettv_list_alloc(rettv) == FAIL)
8021 return;
8022
8023 if (searchpair_cmn(argvars, &match_pos) > 0)
8024 {
8025 lnum = match_pos.lnum;
8026 col = match_pos.col;
8027 }
8028
8029 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8030 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8031}
8032
8033/*
8034 * Search for a start/middle/end thing.
8035 * Used by searchpair(), see its documentation for the details.
8036 * Returns 0 or -1 for no match,
8037 */
8038 long
8039do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008040 char_u *spat, // start pattern
8041 char_u *mpat, // middle pattern
8042 char_u *epat, // end pattern
8043 int dir, // BACKWARD or FORWARD
8044 typval_T *skip, // skip expression
8045 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008046 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008047 linenr_T lnum_stop, // stop at this line if not zero
8048 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008049{
8050 char_u *save_cpo;
8051 char_u *pat, *pat2 = NULL, *pat3 = NULL;
8052 long retval = 0;
8053 pos_T pos;
8054 pos_T firstpos;
8055 pos_T foundpos;
8056 pos_T save_cursor;
8057 pos_T save_pos;
8058 int n;
8059 int r;
8060 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01008061 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008062 int err;
8063 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008064#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008065 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008066#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008067
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008068 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008069 save_cpo = p_cpo;
8070 p_cpo = empty_option;
8071
8072#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008073 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008074 profile_setlimit(time_limit, &tm);
8075#endif
8076
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008077 // Make two search patterns: start/end (pat2, for in nested pairs) and
8078 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02008079 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
8080 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008081 if (pat2 == NULL || pat3 == NULL)
8082 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008083 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008084 if (*mpat == NUL)
8085 STRCPY(pat3, pat2);
8086 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008087 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008088 spat, epat, mpat);
8089 if (flags & SP_START)
8090 options |= SEARCH_START;
8091
Bram Moolenaar48570482017-10-30 21:48:41 +01008092 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02008093 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01008094
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008095 save_cursor = curwin->w_cursor;
8096 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008097 CLEAR_POS(&firstpos);
8098 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008099 pat = pat3;
8100 for (;;)
8101 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008102 searchit_arg_T sia;
8103
Bram Moolenaara80faa82020-04-12 19:37:17 +02008104 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008105 sia.sa_stop_lnum = lnum_stop;
8106#ifdef FEAT_RELTIME
8107 sia.sa_tm = &tm;
8108#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01008109 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008110 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008111 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008112 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008113 break;
8114
8115 if (firstpos.lnum == 0)
8116 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008117 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008119 // Found the same position again. Can happen with a pattern that
8120 // has "\zs" at the end and searching backwards. Advance one
8121 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008122 if (dir == BACKWARD)
8123 decl(&pos);
8124 else
8125 incl(&pos);
8126 }
8127 foundpos = pos;
8128
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008129 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008130 options &= ~SEARCH_START;
8131
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008132 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01008133 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008134 {
8135 save_pos = curwin->w_cursor;
8136 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01008137 err = FALSE;
8138 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008139 curwin->w_cursor = save_pos;
8140 if (err)
8141 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008142 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008143 curwin->w_cursor = save_cursor;
8144 retval = -1;
8145 break;
8146 }
8147 if (r)
8148 continue;
8149 }
8150
8151 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8152 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008153 // Found end when searching backwards or start when searching
8154 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008155 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008156 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008157 }
8158 else
8159 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008160 // Found end when searching forward or start when searching
8161 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008162 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008163 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008164 }
8165
8166 if (nest == 0)
8167 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008168 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008169 if (flags & SP_RETCOUNT)
8170 ++retval;
8171 else
8172 retval = pos.lnum;
8173 if (flags & SP_SETPCMARK)
8174 setpcmark();
8175 curwin->w_cursor = pos;
8176 if (!(flags & SP_REPEAT))
8177 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008178 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008179 }
8180 }
8181
8182 if (match_pos != NULL)
8183 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008184 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008185 match_pos->lnum = curwin->w_cursor.lnum;
8186 match_pos->col = curwin->w_cursor.col + 1;
8187 }
8188
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008189 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008190 if ((flags & SP_NOMOVE) || retval == 0)
8191 curwin->w_cursor = save_cursor;
8192
8193theend:
8194 vim_free(pat2);
8195 vim_free(pat3);
8196 if (p_cpo == empty_option)
8197 p_cpo = save_cpo;
8198 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008199 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008200 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008201 // If it's still empty it was changed and restored, need to restore in
8202 // the complicated way.
8203 if (*p_cpo == NUL)
8204 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008205 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008206 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008207
8208 return retval;
8209}
8210
8211/*
8212 * "searchpos()" function
8213 */
8214 static void
8215f_searchpos(typval_T *argvars, typval_T *rettv)
8216{
8217 pos_T match_pos;
8218 int lnum = 0;
8219 int col = 0;
8220 int n;
8221 int flags = 0;
8222
8223 if (rettv_list_alloc(rettv) == FAIL)
8224 return;
8225
8226 n = search_cmn(argvars, &match_pos, &flags);
8227 if (n > 0)
8228 {
8229 lnum = match_pos.lnum;
8230 col = match_pos.col;
8231 }
8232
8233 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8234 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8235 if (flags & SP_SUBPAT)
8236 list_append_number(rettv->vval.v_list, (varnumber_T)n);
8237}
8238
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008239/*
8240 * Set the cursor or mark position.
8241 * If 'charpos' is TRUE, then use the column number as a character offet.
8242 * Otherwise use the column number as a byte offset.
8243 */
8244 static void
8245set_position(typval_T *argvars, typval_T *rettv, int charpos)
8246{
8247 pos_T pos;
8248 int fnum;
8249 char_u *name;
8250 colnr_T curswant = -1;
8251
8252 rettv->vval.v_number = -1;
8253
8254 name = tv_get_string_chk(argvars);
8255 if (name != NULL)
8256 {
8257 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
8258 {
8259 if (pos.col != MAXCOL && --pos.col < 0)
8260 pos.col = 0;
8261 if ((name[0] == '.' && name[1] == NUL))
8262 {
8263 // set cursor; "fnum" is ignored
8264 curwin->w_cursor = pos;
8265 if (curswant >= 0)
8266 {
8267 curwin->w_curswant = curswant - 1;
8268 curwin->w_set_curswant = FALSE;
8269 }
8270 check_cursor();
8271 rettv->vval.v_number = 0;
8272 }
8273 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8274 {
8275 // set mark
8276 if (setmark_pos(name[1], &pos, fnum) == OK)
8277 rettv->vval.v_number = 0;
8278 }
8279 else
8280 emsg(_(e_invarg));
8281 }
8282 }
8283}
8284/*
8285 * "setcharpos()" function
8286 */
8287 static void
8288f_setcharpos(typval_T *argvars, typval_T *rettv)
8289{
8290 set_position(argvars, rettv, TRUE);
8291}
8292
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008293 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008294f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8295{
8296 dict_T *d;
8297 dictitem_T *di;
8298 char_u *csearch;
8299
8300 if (argvars[0].v_type != VAR_DICT)
8301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008302 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008303 return;
8304 }
8305
8306 if ((d = argvars[0].vval.v_dict) != NULL)
8307 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008308 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008309 if (csearch != NULL)
8310 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008311 if (enc_utf8)
8312 {
8313 int pcc[MAX_MCO];
8314 int c = utfc_ptr2char(csearch, pcc);
8315
8316 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8317 }
8318 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008319 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008320 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008321 }
8322
8323 di = dict_find(d, (char_u *)"forward", -1);
8324 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008325 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008326 ? FORWARD : BACKWARD);
8327
8328 di = dict_find(d, (char_u *)"until", -1);
8329 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008330 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008331 }
8332}
8333
8334/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008335 * "setcursorcharpos" function
8336 */
8337 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008338f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008339{
8340 set_cursorpos(argvars, rettv, TRUE);
8341}
8342
8343/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008344 * "setenv()" function
8345 */
8346 static void
8347f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8348{
8349 char_u namebuf[NUMBUFLEN];
8350 char_u valbuf[NUMBUFLEN];
8351 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8352
8353 if (argvars[1].v_type == VAR_SPECIAL
8354 && argvars[1].vval.v_number == VVAL_NULL)
8355 vim_unsetenv(name);
8356 else
8357 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8358}
8359
8360/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008361 * "setfperm({fname}, {mode})" function
8362 */
8363 static void
8364f_setfperm(typval_T *argvars, typval_T *rettv)
8365{
8366 char_u *fname;
8367 char_u modebuf[NUMBUFLEN];
8368 char_u *mode_str;
8369 int i;
8370 int mask;
8371 int mode = 0;
8372
8373 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008374 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008375 if (fname == NULL)
8376 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008377 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008378 if (mode_str == NULL)
8379 return;
8380 if (STRLEN(mode_str) != 9)
8381 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008382 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008383 return;
8384 }
8385
8386 mask = 1;
8387 for (i = 8; i >= 0; --i)
8388 {
8389 if (mode_str[i] != '-')
8390 mode |= mask;
8391 mask = mask << 1;
8392 }
8393 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8394}
8395
8396/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008397 * "setpos()" function
8398 */
8399 static void
8400f_setpos(typval_T *argvars, typval_T *rettv)
8401{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008402 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008403}
8404
8405/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008406 * Translate a register type string to the yank type and block length
8407 */
8408 static int
8409get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8410{
8411 char_u *stropt = *pp;
8412 switch (*stropt)
8413 {
8414 case 'v': case 'c': // character-wise selection
8415 *yank_type = MCHAR;
8416 break;
8417 case 'V': case 'l': // line-wise selection
8418 *yank_type = MLINE;
8419 break;
8420 case 'b': case Ctrl_V: // block-wise selection
8421 *yank_type = MBLOCK;
8422 if (VIM_ISDIGIT(stropt[1]))
8423 {
8424 ++stropt;
8425 *block_len = getdigits(&stropt) - 1;
8426 --stropt;
8427 }
8428 break;
8429 default:
8430 return FAIL;
8431 }
8432 *pp = stropt;
8433 return OK;
8434}
8435
8436/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008437 * "setreg()" function
8438 */
8439 static void
8440f_setreg(typval_T *argvars, typval_T *rettv)
8441{
8442 int regname;
8443 char_u *strregname;
8444 char_u *stropt;
8445 char_u *strval;
8446 int append;
8447 char_u yank_type;
8448 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008449 typval_T *regcontents;
8450 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008451
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008452 pointreg = 0;
8453 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008454 block_len = -1;
8455 yank_type = MAUTO;
8456 append = FALSE;
8457
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008458 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008459 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008460
8461 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008462 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008463 regname = *strregname;
8464 if (regname == 0 || regname == '@')
8465 regname = '"';
8466
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008467 if (argvars[1].v_type == VAR_DICT)
8468 {
8469 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008470 dictitem_T *di;
8471
8472 if (d == NULL || d->dv_hashtab.ht_used == 0)
8473 {
8474 // Empty dict, clear the register (like setreg(0, []))
8475 char_u *lstval[2] = {NULL, NULL};
8476 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8477 return;
8478 }
8479
8480 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008481 if (di != NULL)
8482 regcontents = &di->di_tv;
8483
8484 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8485 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008486 {
8487 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8488
8489 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008490 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008491 semsg(_(e_invargval), "value");
8492 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008493 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008494 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008495
8496 if (regname == '"')
8497 {
8498 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8499 if (stropt != NULL)
8500 {
8501 pointreg = *stropt;
8502 regname = pointreg;
8503 }
8504 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008505 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008506 pointreg = regname;
8507 }
8508 else
8509 regcontents = &argvars[1];
8510
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008511 if (argvars[2].v_type != VAR_UNKNOWN)
8512 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008513 if (yank_type != MAUTO)
8514 {
8515 semsg(_(e_toomanyarg), "setreg");
8516 return;
8517 }
8518
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008519 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008520 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008521 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008522 for (; *stropt != NUL; ++stropt)
8523 switch (*stropt)
8524 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008525 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008526 append = TRUE;
8527 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008528 default:
8529 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008530 }
8531 }
8532
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008533 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008534 {
8535 char_u **lstval;
8536 char_u **allocval;
8537 char_u buf[NUMBUFLEN];
8538 char_u **curval;
8539 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008540 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008541 listitem_T *li;
8542 int len;
8543
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008544 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008545 len = ll == NULL ? 0 : ll->lv_len;
8546
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008547 // First half: use for pointers to result lines; second half: use for
8548 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008549 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008550 if (lstval == NULL)
8551 return;
8552 curval = lstval;
8553 allocval = lstval + len + 2;
8554 curallocval = allocval;
8555
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008556 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008557 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008558 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008559 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008560 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008561 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562 if (strval == NULL)
8563 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008564 if (strval == buf)
8565 {
8566 // Need to make a copy, next tv_get_string_buf_chk() will
8567 // overwrite the string.
8568 strval = vim_strsave(buf);
8569 if (strval == NULL)
8570 goto free_lstval;
8571 *curallocval++ = strval;
8572 }
8573 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008574 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008575 }
8576 *curval++ = NULL;
8577
8578 write_reg_contents_lst(regname, lstval, -1,
8579 append, yank_type, block_len);
8580free_lstval:
8581 while (curallocval > allocval)
8582 vim_free(*--curallocval);
8583 vim_free(lstval);
8584 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008585 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008586 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008587 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008588 if (strval == NULL)
8589 return;
8590 write_reg_contents_ex(regname, strval, -1,
8591 append, yank_type, block_len);
8592 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008593 if (pointreg != 0)
8594 get_yank_register(pointreg, TRUE);
8595
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008596 rettv->vval.v_number = 0;
8597}
8598
8599/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008600 * "settagstack()" function
8601 */
8602 static void
8603f_settagstack(typval_T *argvars, typval_T *rettv)
8604{
8605 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8606 win_T *wp;
8607 dict_T *d;
8608 int action = 'r';
8609
8610 rettv->vval.v_number = -1;
8611
8612 // first argument: window number or id
8613 wp = find_win_by_nr_or_id(&argvars[0]);
8614 if (wp == NULL)
8615 return;
8616
8617 // second argument: dict with items to set in the tag stack
8618 if (argvars[1].v_type != VAR_DICT)
8619 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008620 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008621 return;
8622 }
8623 d = argvars[1].vval.v_dict;
8624 if (d == NULL)
8625 return;
8626
8627 // third argument: action - 'a' for append and 'r' for replace.
8628 // default is to replace the stack.
8629 if (argvars[2].v_type == VAR_UNKNOWN)
8630 action = 'r';
8631 else if (argvars[2].v_type == VAR_STRING)
8632 {
8633 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008634 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008635 if (actstr == NULL)
8636 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008637 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8638 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008639 action = *actstr;
8640 else
8641 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008642 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008643 return;
8644 }
8645 }
8646 else
8647 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008648 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008649 return;
8650 }
8651
8652 if (set_tagstack(wp, d, action) == OK)
8653 rettv->vval.v_number = 0;
8654}
8655
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008656#ifdef FEAT_CRYPT
8657/*
8658 * "sha256({string})" function
8659 */
8660 static void
8661f_sha256(typval_T *argvars, typval_T *rettv)
8662{
8663 char_u *p;
8664
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008665 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008666 rettv->vval.v_string = vim_strsave(
8667 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8668 rettv->v_type = VAR_STRING;
8669}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008670#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008671
8672/*
8673 * "shellescape({string})" function
8674 */
8675 static void
8676f_shellescape(typval_T *argvars, typval_T *rettv)
8677{
Bram Moolenaar20615522017-06-05 18:46:26 +02008678 int do_special = non_zero_arg(&argvars[1]);
8679
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008680 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008681 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008682 rettv->v_type = VAR_STRING;
8683}
8684
8685/*
8686 * shiftwidth() function
8687 */
8688 static void
8689f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8690{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008691 rettv->vval.v_number = 0;
8692
8693 if (argvars[0].v_type != VAR_UNKNOWN)
8694 {
8695 long col;
8696
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008697 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008698 if (col < 0)
8699 return; // type error; errmsg already given
8700#ifdef FEAT_VARTABS
8701 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8702 return;
8703#endif
8704 }
8705
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008706 rettv->vval.v_number = get_sw_value(curbuf);
8707}
8708
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709#ifdef FEAT_FLOAT
8710/*
8711 * "sin()" function
8712 */
8713 static void
8714f_sin(typval_T *argvars, typval_T *rettv)
8715{
8716 float_T f = 0.0;
8717
8718 rettv->v_type = VAR_FLOAT;
8719 if (get_float_arg(argvars, &f) == OK)
8720 rettv->vval.v_float = sin(f);
8721 else
8722 rettv->vval.v_float = 0.0;
8723}
8724
8725/*
8726 * "sinh()" function
8727 */
8728 static void
8729f_sinh(typval_T *argvars, typval_T *rettv)
8730{
8731 float_T f = 0.0;
8732
8733 rettv->v_type = VAR_FLOAT;
8734 if (get_float_arg(argvars, &f) == OK)
8735 rettv->vval.v_float = sinh(f);
8736 else
8737 rettv->vval.v_float = 0.0;
8738}
8739#endif
8740
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008741/*
8742 * "soundfold({word})" function
8743 */
8744 static void
8745f_soundfold(typval_T *argvars, typval_T *rettv)
8746{
8747 char_u *s;
8748
8749 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008750 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008751#ifdef FEAT_SPELL
8752 rettv->vval.v_string = eval_soundfold(s);
8753#else
8754 rettv->vval.v_string = vim_strsave(s);
8755#endif
8756}
8757
8758/*
8759 * "spellbadword()" function
8760 */
8761 static void
8762f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8763{
8764 char_u *word = (char_u *)"";
8765 hlf_T attr = HLF_COUNT;
8766 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008767#ifdef FEAT_SPELL
8768 int wo_spell_save = curwin->w_p_spell;
8769
8770 if (!curwin->w_p_spell)
8771 {
8772 did_set_spelllang(curwin);
8773 curwin->w_p_spell = TRUE;
8774 }
8775
8776 if (*curwin->w_s->b_p_spl == NUL)
8777 {
8778 emsg(_(e_no_spell));
8779 curwin->w_p_spell = wo_spell_save;
8780 return;
8781 }
8782#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008783
8784 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008785 {
8786#ifdef FEAT_SPELL
8787 curwin->w_p_spell = wo_spell_save;
8788#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008789 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008790 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008791
8792#ifdef FEAT_SPELL
8793 if (argvars[0].v_type == VAR_UNKNOWN)
8794 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008795 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8797 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008798 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008799 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008800 curwin->w_set_curswant = TRUE;
8801 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008802 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008803 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008804 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008805 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008806 int capcol = -1;
8807
8808 if (str != NULL)
8809 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008810 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008811 while (*str != NUL)
8812 {
8813 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8814 if (attr != HLF_COUNT)
8815 {
8816 word = str;
8817 break;
8818 }
8819 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008820 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008821 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008822 }
8823 }
8824 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008825 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008826#endif
8827
8828 list_append_string(rettv->vval.v_list, word, len);
8829 list_append_string(rettv->vval.v_list, (char_u *)(
8830 attr == HLF_SPB ? "bad" :
8831 attr == HLF_SPR ? "rare" :
8832 attr == HLF_SPL ? "local" :
8833 attr == HLF_SPC ? "caps" :
8834 ""), -1);
8835}
8836
8837/*
8838 * "spellsuggest()" function
8839 */
8840 static void
8841f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8842{
8843#ifdef FEAT_SPELL
8844 char_u *str;
8845 int typeerr = FALSE;
8846 int maxcount;
8847 garray_T ga;
8848 int i;
8849 listitem_T *li;
8850 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008851 int wo_spell_save = curwin->w_p_spell;
8852
8853 if (!curwin->w_p_spell)
8854 {
8855 did_set_spelllang(curwin);
8856 curwin->w_p_spell = TRUE;
8857 }
8858
8859 if (*curwin->w_s->b_p_spl == NUL)
8860 {
8861 emsg(_(e_no_spell));
8862 curwin->w_p_spell = wo_spell_save;
8863 return;
8864 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008865#endif
8866
8867 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008868 {
8869#ifdef FEAT_SPELL
8870 curwin->w_p_spell = wo_spell_save;
8871#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008872 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008873 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008874
8875#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008876 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008877 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008878 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008879 if (argvars[1].v_type != VAR_UNKNOWN)
8880 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008881 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008882 if (maxcount <= 0)
8883 return;
8884 if (argvars[2].v_type != VAR_UNKNOWN)
8885 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008886 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008887 if (typeerr)
8888 return;
8889 }
8890 }
8891 else
8892 maxcount = 25;
8893
8894 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8895
8896 for (i = 0; i < ga.ga_len; ++i)
8897 {
8898 str = ((char_u **)ga.ga_data)[i];
8899
8900 li = listitem_alloc();
8901 if (li == NULL)
8902 vim_free(str);
8903 else
8904 {
8905 li->li_tv.v_type = VAR_STRING;
8906 li->li_tv.v_lock = 0;
8907 li->li_tv.vval.v_string = str;
8908 list_append(rettv->vval.v_list, li);
8909 }
8910 }
8911 ga_clear(&ga);
8912 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008913 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008914#endif
8915}
8916
8917 static void
8918f_split(typval_T *argvars, typval_T *rettv)
8919{
8920 char_u *str;
8921 char_u *end;
8922 char_u *pat = NULL;
8923 regmatch_T regmatch;
8924 char_u patbuf[NUMBUFLEN];
8925 char_u *save_cpo;
8926 int match;
8927 colnr_T col = 0;
8928 int keepempty = FALSE;
8929 int typeerr = FALSE;
8930
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008931 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008932 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008933 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008934
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008935 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008936 if (argvars[1].v_type != VAR_UNKNOWN)
8937 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008938 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008939 if (pat == NULL)
8940 typeerr = TRUE;
8941 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008942 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008943 }
8944 if (pat == NULL || *pat == NUL)
8945 pat = (char_u *)"[\\x01- ]\\+";
8946
8947 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008948 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008949 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008950 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008951
8952 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8953 if (regmatch.regprog != NULL)
8954 {
8955 regmatch.rm_ic = FALSE;
8956 while (*str != NUL || keepempty)
8957 {
8958 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008959 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008960 else
8961 match = vim_regexec_nl(&regmatch, str, col);
8962 if (match)
8963 end = regmatch.startp[0];
8964 else
8965 end = str + STRLEN(str);
8966 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
8967 && *str != NUL && match && end < regmatch.endp[0]))
8968 {
8969 if (list_append_string(rettv->vval.v_list, str,
8970 (int)(end - str)) == FAIL)
8971 break;
8972 }
8973 if (!match)
8974 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01008975 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008976 if (regmatch.endp[0] > str)
8977 col = 0;
8978 else
Bram Moolenaar13505972019-01-24 15:04:48 +01008979 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008980 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008981 str = regmatch.endp[0];
8982 }
8983
8984 vim_regfree(regmatch.regprog);
8985 }
8986
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008987theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008988 p_cpo = save_cpo;
8989}
8990
8991#ifdef FEAT_FLOAT
8992/*
8993 * "sqrt()" function
8994 */
8995 static void
8996f_sqrt(typval_T *argvars, typval_T *rettv)
8997{
8998 float_T f = 0.0;
8999
9000 rettv->v_type = VAR_FLOAT;
9001 if (get_float_arg(argvars, &f) == OK)
9002 rettv->vval.v_float = sqrt(f);
9003 else
9004 rettv->vval.v_float = 0.0;
9005}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009006#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009007
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009008#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01009009/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009010 * "str2float()" function
9011 */
9012 static void
9013f_str2float(typval_T *argvars, typval_T *rettv)
9014{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009015 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009016 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009017
Bram Moolenaar08243d22017-01-10 16:12:29 +01009018 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009019 p = skipwhite(p + 1);
9020 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01009021 if (isneg)
9022 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009023 rettv->v_type = VAR_FLOAT;
9024}
9025#endif
9026
9027/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02009028 * "str2list()" function
9029 */
9030 static void
9031f_str2list(typval_T *argvars, typval_T *rettv)
9032{
9033 char_u *p;
9034 int utf8 = FALSE;
9035
9036 if (rettv_list_alloc(rettv) == FAIL)
9037 return;
9038
9039 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02009040 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02009041
9042 p = tv_get_string(&argvars[0]);
9043
9044 if (has_mbyte || utf8)
9045 {
9046 int (*ptr2len)(char_u *);
9047 int (*ptr2char)(char_u *);
9048
9049 if (utf8 || enc_utf8)
9050 {
9051 ptr2len = utf_ptr2len;
9052 ptr2char = utf_ptr2char;
9053 }
9054 else
9055 {
9056 ptr2len = mb_ptr2len;
9057 ptr2char = mb_ptr2char;
9058 }
9059
9060 for ( ; *p != NUL; p += (*ptr2len)(p))
9061 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
9062 }
9063 else
9064 for ( ; *p != NUL; ++p)
9065 list_append_number(rettv->vval.v_list, *p);
9066}
9067
9068/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009069 * "str2nr()" function
9070 */
9071 static void
9072f_str2nr(typval_T *argvars, typval_T *rettv)
9073{
9074 int base = 10;
9075 char_u *p;
9076 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009077 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01009078 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009079
9080 if (argvars[1].v_type != VAR_UNKNOWN)
9081 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009082 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009083 if (base != 2 && base != 8 && base != 10 && base != 16)
9084 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009085 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009086 return;
9087 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02009088 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009089 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009090 }
9091
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01009092 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009093 isneg = (*p == '-');
9094 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009095 p = skipwhite(p + 1);
9096 switch (base)
9097 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009098 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02009099 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009100 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009101 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02009102 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
9103 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01009104 if (isneg)
9105 rettv->vval.v_number = -n;
9106 else
9107 rettv->vval.v_number = n;
9108
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009109}
9110
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009111/*
9112 * "strgetchar()" function
9113 */
9114 static void
9115f_strgetchar(typval_T *argvars, typval_T *rettv)
9116{
9117 char_u *str;
9118 int len;
9119 int error = FALSE;
9120 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01009121 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009122
9123 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009124 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009125 if (str == NULL)
9126 return;
9127 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009128 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009129 if (error)
9130 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009131
Bram Moolenaar13505972019-01-24 15:04:48 +01009132 while (charidx >= 0 && byteidx < len)
9133 {
9134 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009135 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009136 rettv->vval.v_number = mb_ptr2char(str + byteidx);
9137 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009138 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009139 --charidx;
9140 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009141 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009142}
9143
9144/*
9145 * "stridx()" function
9146 */
9147 static void
9148f_stridx(typval_T *argvars, typval_T *rettv)
9149{
9150 char_u buf[NUMBUFLEN];
9151 char_u *needle;
9152 char_u *haystack;
9153 char_u *save_haystack;
9154 char_u *pos;
9155 int start_idx;
9156
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009157 needle = tv_get_string_chk(&argvars[1]);
9158 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009159 rettv->vval.v_number = -1;
9160 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009161 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009162
9163 if (argvars[2].v_type != VAR_UNKNOWN)
9164 {
9165 int error = FALSE;
9166
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009167 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009168 if (error || start_idx >= (int)STRLEN(haystack))
9169 return;
9170 if (start_idx >= 0)
9171 haystack += start_idx;
9172 }
9173
9174 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9175 if (pos != NULL)
9176 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
9177}
9178
9179/*
9180 * "string()" function
9181 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009182 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009183f_string(typval_T *argvars, typval_T *rettv)
9184{
9185 char_u *tofree;
9186 char_u numbuf[NUMBUFLEN];
9187
9188 rettv->v_type = VAR_STRING;
9189 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
9190 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009191 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009192 if (rettv->vval.v_string != NULL && tofree == NULL)
9193 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
9194}
9195
9196/*
9197 * "strlen()" function
9198 */
9199 static void
9200f_strlen(typval_T *argvars, typval_T *rettv)
9201{
9202 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009203 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009204}
9205
9206/*
9207 * "strchars()" function
9208 */
9209 static void
9210f_strchars(typval_T *argvars, typval_T *rettv)
9211{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009212 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009213 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009214 varnumber_T len = 0;
9215 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009216
9217 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009218 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009219 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02009220 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009221 else
9222 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009223 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
9224 while (*s != NUL)
9225 {
9226 func_mb_ptr2char_adv(&s);
9227 ++len;
9228 }
9229 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009230 }
9231}
9232
9233/*
9234 * "strdisplaywidth()" function
9235 */
9236 static void
9237f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
9238{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009239 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009240 int col = 0;
9241
9242 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009243 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009244
9245 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
9246}
9247
9248/*
9249 * "strwidth()" function
9250 */
9251 static void
9252f_strwidth(typval_T *argvars, typval_T *rettv)
9253{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009254 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009255
Bram Moolenaar13505972019-01-24 15:04:48 +01009256 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009257}
9258
9259/*
9260 * "strcharpart()" function
9261 */
9262 static void
9263f_strcharpart(typval_T *argvars, typval_T *rettv)
9264{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009265 char_u *p;
9266 int nchar;
9267 int nbyte = 0;
9268 int charlen;
9269 int len = 0;
9270 int slen;
9271 int error = FALSE;
9272
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009273 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009274 slen = (int)STRLEN(p);
9275
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009276 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009277 if (!error)
9278 {
9279 if (nchar > 0)
9280 while (nchar > 0 && nbyte < slen)
9281 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009282 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009283 --nchar;
9284 }
9285 else
9286 nbyte = nchar;
9287 if (argvars[2].v_type != VAR_UNKNOWN)
9288 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009289 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009290 while (charlen > 0 && nbyte + len < slen)
9291 {
9292 int off = nbyte + len;
9293
9294 if (off < 0)
9295 len += 1;
9296 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009297 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009298 --charlen;
9299 }
9300 }
9301 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009302 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009303 }
9304
9305 /*
9306 * Only return the overlap between the specified part and the actual
9307 * string.
9308 */
9309 if (nbyte < 0)
9310 {
9311 len += nbyte;
9312 nbyte = 0;
9313 }
9314 else if (nbyte > slen)
9315 nbyte = slen;
9316 if (len < 0)
9317 len = 0;
9318 else if (nbyte + len > slen)
9319 len = slen - nbyte;
9320
9321 rettv->v_type = VAR_STRING;
9322 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009323}
9324
9325/*
9326 * "strpart()" function
9327 */
9328 static void
9329f_strpart(typval_T *argvars, typval_T *rettv)
9330{
9331 char_u *p;
9332 int n;
9333 int len;
9334 int slen;
9335 int error = FALSE;
9336
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009337 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009338 slen = (int)STRLEN(p);
9339
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009340 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009341 if (error)
9342 len = 0;
9343 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009344 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009345 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009346 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009347
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009348 // Only return the overlap between the specified part and the actual
9349 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009350 if (n < 0)
9351 {
9352 len += n;
9353 n = 0;
9354 }
9355 else if (n > slen)
9356 n = slen;
9357 if (len < 0)
9358 len = 0;
9359 else if (n + len > slen)
9360 len = slen - n;
9361
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009362 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9363 {
9364 int off;
9365
9366 // length in characters
9367 for (off = n; off < slen && len > 0; --len)
9368 off += mb_ptr2len(p + off);
9369 len = off - n;
9370 }
9371
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009372 rettv->v_type = VAR_STRING;
9373 rettv->vval.v_string = vim_strnsave(p + n, len);
9374}
9375
9376/*
9377 * "strridx()" function
9378 */
9379 static void
9380f_strridx(typval_T *argvars, typval_T *rettv)
9381{
9382 char_u buf[NUMBUFLEN];
9383 char_u *needle;
9384 char_u *haystack;
9385 char_u *rest;
9386 char_u *lastmatch = NULL;
9387 int haystack_len, end_idx;
9388
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009389 needle = tv_get_string_chk(&argvars[1]);
9390 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009391
9392 rettv->vval.v_number = -1;
9393 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009394 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009395
9396 haystack_len = (int)STRLEN(haystack);
9397 if (argvars[2].v_type != VAR_UNKNOWN)
9398 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009399 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009400 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009401 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009402 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009403 }
9404 else
9405 end_idx = haystack_len;
9406
9407 if (*needle == NUL)
9408 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009409 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009410 lastmatch = haystack + end_idx;
9411 }
9412 else
9413 {
9414 for (rest = haystack; *rest != '\0'; ++rest)
9415 {
9416 rest = (char_u *)strstr((char *)rest, (char *)needle);
9417 if (rest == NULL || rest > haystack + end_idx)
9418 break;
9419 lastmatch = rest;
9420 }
9421 }
9422
9423 if (lastmatch == NULL)
9424 rettv->vval.v_number = -1;
9425 else
9426 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9427}
9428
9429/*
9430 * "strtrans()" function
9431 */
9432 static void
9433f_strtrans(typval_T *argvars, typval_T *rettv)
9434{
9435 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009436 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009437}
9438
9439/*
9440 * "submatch()" function
9441 */
9442 static void
9443f_submatch(typval_T *argvars, typval_T *rettv)
9444{
9445 int error = FALSE;
9446 int no;
9447 int retList = 0;
9448
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009449 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009450 if (error)
9451 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009452 if (no < 0 || no >= NSUBEXP)
9453 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009454 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009455 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009456 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009457 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009458 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009459 if (error)
9460 return;
9461
9462 if (retList == 0)
9463 {
9464 rettv->v_type = VAR_STRING;
9465 rettv->vval.v_string = reg_submatch(no);
9466 }
9467 else
9468 {
9469 rettv->v_type = VAR_LIST;
9470 rettv->vval.v_list = reg_submatch_list(no);
9471 }
9472}
9473
9474/*
9475 * "substitute()" function
9476 */
9477 static void
9478f_substitute(typval_T *argvars, typval_T *rettv)
9479{
9480 char_u patbuf[NUMBUFLEN];
9481 char_u subbuf[NUMBUFLEN];
9482 char_u flagsbuf[NUMBUFLEN];
9483
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009484 char_u *str = tv_get_string_chk(&argvars[0]);
9485 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009486 char_u *sub = NULL;
9487 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009488 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009489
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009490 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9491 expr = &argvars[2];
9492 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009493 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009494
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009495 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009496 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9497 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009498 rettv->vval.v_string = NULL;
9499 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009500 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009501}
9502
9503/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009504 * "swapinfo(swap_filename)" function
9505 */
9506 static void
9507f_swapinfo(typval_T *argvars, typval_T *rettv)
9508{
9509 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009510 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009511}
9512
9513/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009514 * "swapname(expr)" function
9515 */
9516 static void
9517f_swapname(typval_T *argvars, typval_T *rettv)
9518{
9519 buf_T *buf;
9520
9521 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009522 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009523 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9524 || buf->b_ml.ml_mfp->mf_fname == NULL)
9525 rettv->vval.v_string = NULL;
9526 else
9527 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9528}
9529
9530/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009531 * "synID(lnum, col, trans)" function
9532 */
9533 static void
9534f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9535{
9536 int id = 0;
9537#ifdef FEAT_SYN_HL
9538 linenr_T lnum;
9539 colnr_T col;
9540 int trans;
9541 int transerr = FALSE;
9542
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009543 lnum = tv_get_lnum(argvars); // -1 on type error
9544 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009545 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009546
9547 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9548 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9549 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9550#endif
9551
9552 rettv->vval.v_number = id;
9553}
9554
9555/*
9556 * "synIDattr(id, what [, mode])" function
9557 */
9558 static void
9559f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9560{
9561 char_u *p = NULL;
9562#ifdef FEAT_SYN_HL
9563 int id;
9564 char_u *what;
9565 char_u *mode;
9566 char_u modebuf[NUMBUFLEN];
9567 int modec;
9568
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009569 id = (int)tv_get_number(&argvars[0]);
9570 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009571 if (argvars[2].v_type != VAR_UNKNOWN)
9572 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009573 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009574 modec = TOLOWER_ASC(mode[0]);
9575 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009576 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009577 }
9578 else
9579 {
9580#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9581 if (USE_24BIT)
9582 modec = 'g';
9583 else
9584#endif
9585 if (t_colors > 1)
9586 modec = 'c';
9587 else
9588 modec = 't';
9589 }
9590
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009591 switch (TOLOWER_ASC(what[0]))
9592 {
9593 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009594 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009595 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009596 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009597 p = highlight_has_attr(id, HL_BOLD, modec);
9598 break;
9599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009600 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009601 p = highlight_color(id, what, modec);
9602 break;
9603
9604 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009605 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009606 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009607 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009608 p = highlight_has_attr(id, HL_ITALIC, modec);
9609 break;
9610
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009611 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009612 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009613 break;
9614
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009615 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009616 p = highlight_has_attr(id, HL_INVERSE, modec);
9617 break;
9618
9619 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009620 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009621 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009622 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009623 else if (TOLOWER_ASC(what[1]) == 't' &&
9624 TOLOWER_ASC(what[2]) == 'r')
9625 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009626 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009627 p = highlight_has_attr(id, HL_STANDOUT, modec);
9628 break;
9629
9630 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009631 if (TOLOWER_ASC(what[1]) == 'l') // ul
9632 p = highlight_color(id, what, modec);
9633 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009634 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009635 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9636 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009637 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009638 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9639 break;
9640 }
9641
9642 if (p != NULL)
9643 p = vim_strsave(p);
9644#endif
9645 rettv->v_type = VAR_STRING;
9646 rettv->vval.v_string = p;
9647}
9648
9649/*
9650 * "synIDtrans(id)" function
9651 */
9652 static void
9653f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9654{
9655 int id;
9656
9657#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009658 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009659
9660 if (id > 0)
9661 id = syn_get_final_id(id);
9662 else
9663#endif
9664 id = 0;
9665
9666 rettv->vval.v_number = id;
9667}
9668
9669/*
9670 * "synconcealed(lnum, col)" function
9671 */
9672 static void
9673f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9674{
9675#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9676 linenr_T lnum;
9677 colnr_T col;
9678 int syntax_flags = 0;
9679 int cchar;
9680 int matchid = 0;
9681 char_u str[NUMBUFLEN];
9682#endif
9683
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009684 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009685
9686#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009687 lnum = tv_get_lnum(argvars); // -1 on type error
9688 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009689
Bram Moolenaara80faa82020-04-12 19:37:17 +02009690 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009691
9692 if (rettv_list_alloc(rettv) != FAIL)
9693 {
9694 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9695 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9696 && curwin->w_p_cole > 0)
9697 {
9698 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9699 syntax_flags = get_syntax_info(&matchid);
9700
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009701 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009702 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9703 {
9704 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009705 if (cchar == NUL && curwin->w_p_cole == 1)
9706 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009707 if (cchar != NUL)
9708 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009709 if (has_mbyte)
9710 (*mb_char2bytes)(cchar, str);
9711 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009712 str[0] = cchar;
9713 }
9714 }
9715 }
9716
9717 list_append_number(rettv->vval.v_list,
9718 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009719 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009720 list_append_string(rettv->vval.v_list, str, -1);
9721 list_append_number(rettv->vval.v_list, matchid);
9722 }
9723#endif
9724}
9725
9726/*
9727 * "synstack(lnum, col)" function
9728 */
9729 static void
9730f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9731{
9732#ifdef FEAT_SYN_HL
9733 linenr_T lnum;
9734 colnr_T col;
9735 int i;
9736 int id;
9737#endif
9738
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009739 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009740
9741#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009742 lnum = tv_get_lnum(argvars); // -1 on type error
9743 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009744
9745 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9746 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9747 && rettv_list_alloc(rettv) != FAIL)
9748 {
9749 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9750 for (i = 0; ; ++i)
9751 {
9752 id = syn_get_stack_item(i);
9753 if (id < 0)
9754 break;
9755 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9756 break;
9757 }
9758 }
9759#endif
9760}
9761
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009762/*
9763 * "tabpagebuflist()" function
9764 */
9765 static void
9766f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9767{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009768 tabpage_T *tp;
9769 win_T *wp = NULL;
9770
9771 if (argvars[0].v_type == VAR_UNKNOWN)
9772 wp = firstwin;
9773 else
9774 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009775 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009776 if (tp != NULL)
9777 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9778 }
9779 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9780 {
9781 for (; wp != NULL; wp = wp->w_next)
9782 if (list_append_number(rettv->vval.v_list,
9783 wp->w_buffer->b_fnum) == FAIL)
9784 break;
9785 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009786}
9787
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009788/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009789 * "tagfiles()" function
9790 */
9791 static void
9792f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9793{
9794 char_u *fname;
9795 tagname_T tn;
9796 int first;
9797
9798 if (rettv_list_alloc(rettv) == FAIL)
9799 return;
9800 fname = alloc(MAXPATHL);
9801 if (fname == NULL)
9802 return;
9803
9804 for (first = TRUE; ; first = FALSE)
9805 if (get_tagfname(&tn, first, fname) == FAIL
9806 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9807 break;
9808 tagname_free(&tn);
9809 vim_free(fname);
9810}
9811
9812/*
9813 * "taglist()" function
9814 */
9815 static void
9816f_taglist(typval_T *argvars, typval_T *rettv)
9817{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009818 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009819 char_u *tag_pattern;
9820
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009821 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009822
9823 rettv->vval.v_number = FALSE;
9824 if (*tag_pattern == NUL)
9825 return;
9826
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009827 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009828 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009829 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009830 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009831}
9832
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009833#ifdef FEAT_FLOAT
9834/*
9835 * "tan()" function
9836 */
9837 static void
9838f_tan(typval_T *argvars, typval_T *rettv)
9839{
9840 float_T f = 0.0;
9841
9842 rettv->v_type = VAR_FLOAT;
9843 if (get_float_arg(argvars, &f) == OK)
9844 rettv->vval.v_float = tan(f);
9845 else
9846 rettv->vval.v_float = 0.0;
9847}
9848
9849/*
9850 * "tanh()" function
9851 */
9852 static void
9853f_tanh(typval_T *argvars, typval_T *rettv)
9854{
9855 float_T f = 0.0;
9856
9857 rettv->v_type = VAR_FLOAT;
9858 if (get_float_arg(argvars, &f) == OK)
9859 rettv->vval.v_float = tanh(f);
9860 else
9861 rettv->vval.v_float = 0.0;
9862}
9863#endif
9864
9865/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009866 * "tolower(string)" function
9867 */
9868 static void
9869f_tolower(typval_T *argvars, typval_T *rettv)
9870{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009871 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009872 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009873}
9874
9875/*
9876 * "toupper(string)" function
9877 */
9878 static void
9879f_toupper(typval_T *argvars, typval_T *rettv)
9880{
9881 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009882 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009883}
9884
9885/*
9886 * "tr(string, fromstr, tostr)" function
9887 */
9888 static void
9889f_tr(typval_T *argvars, typval_T *rettv)
9890{
9891 char_u *in_str;
9892 char_u *fromstr;
9893 char_u *tostr;
9894 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009895 int inlen;
9896 int fromlen;
9897 int tolen;
9898 int idx;
9899 char_u *cpstr;
9900 int cplen;
9901 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009902 char_u buf[NUMBUFLEN];
9903 char_u buf2[NUMBUFLEN];
9904 garray_T ga;
9905
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009906 in_str = tv_get_string(&argvars[0]);
9907 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9908 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009910 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009911 rettv->v_type = VAR_STRING;
9912 rettv->vval.v_string = NULL;
9913 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009914 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009915 ga_init2(&ga, (int)sizeof(char), 80);
9916
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009917 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009918 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009919 if (STRLEN(fromstr) != STRLEN(tostr))
9920 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009921error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009922 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009923 ga_clear(&ga);
9924 return;
9925 }
9926
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009927 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009928 while (*in_str != NUL)
9929 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009930 if (has_mbyte)
9931 {
9932 inlen = (*mb_ptr2len)(in_str);
9933 cpstr = in_str;
9934 cplen = inlen;
9935 idx = 0;
9936 for (p = fromstr; *p != NUL; p += fromlen)
9937 {
9938 fromlen = (*mb_ptr2len)(p);
9939 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9940 {
9941 for (p = tostr; *p != NUL; p += tolen)
9942 {
9943 tolen = (*mb_ptr2len)(p);
9944 if (idx-- == 0)
9945 {
9946 cplen = tolen;
9947 cpstr = p;
9948 break;
9949 }
9950 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009951 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009952 goto error;
9953 break;
9954 }
9955 ++idx;
9956 }
9957
9958 if (first && cpstr == in_str)
9959 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009960 // Check that fromstr and tostr have the same number of
9961 // (multi-byte) characters. Done only once when a character
9962 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009963 first = FALSE;
9964 for (p = tostr; *p != NUL; p += tolen)
9965 {
9966 tolen = (*mb_ptr2len)(p);
9967 --idx;
9968 }
9969 if (idx != 0)
9970 goto error;
9971 }
9972
9973 (void)ga_grow(&ga, cplen);
9974 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
9975 ga.ga_len += cplen;
9976
9977 in_str += inlen;
9978 }
9979 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009980 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009981 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009982 p = vim_strchr(fromstr, *in_str);
9983 if (p != NULL)
9984 ga_append(&ga, tostr[p - fromstr]);
9985 else
9986 ga_append(&ga, *in_str);
9987 ++in_str;
9988 }
9989 }
9990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009991 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009992 (void)ga_grow(&ga, 1);
9993 ga_append(&ga, NUL);
9994
9995 rettv->vval.v_string = ga.ga_data;
9996}
9997
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009998/*
9999 * "trim({expr})" function
10000 */
10001 static void
10002f_trim(typval_T *argvars, typval_T *rettv)
10003{
10004 char_u buf1[NUMBUFLEN];
10005 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010006 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010007 char_u *mask = NULL;
10008 char_u *tail;
10009 char_u *prev;
10010 char_u *p;
10011 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010012 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010013
10014 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010015 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010016 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010017 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010018
10019 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010020 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010021 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010022
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010023 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010024 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010025 int error = 0;
10026
10027 // leading or trailing characters to trim
10028 dir = (int)tv_get_number_chk(&argvars[2], &error);
10029 if (error)
10030 return;
10031 if (dir < 0 || dir > 2)
10032 {
10033 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
10034 return;
10035 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010036 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010037 }
10038
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010039 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010040 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010041 // Trim leading characters
10042 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010043 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010044 c1 = PTR2CHAR(head);
10045 if (mask == NULL)
10046 {
10047 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010048 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010049 }
10050 else
10051 {
10052 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10053 if (c1 == PTR2CHAR(p))
10054 break;
10055 if (*p == NUL)
10056 break;
10057 }
10058 MB_PTR_ADV(head);
10059 }
10060 }
10061
10062 tail = head + STRLEN(head);
10063 if (dir == 0 || dir == 2)
10064 {
10065 // Trim trailing characters
10066 for (; tail > head; tail = prev)
10067 {
10068 prev = tail;
10069 MB_PTR_BACK(head, prev);
10070 c1 = PTR2CHAR(prev);
10071 if (mask == NULL)
10072 {
10073 if (c1 > ' ' && c1 != 0xa0)
10074 break;
10075 }
10076 else
10077 {
10078 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10079 if (c1 == PTR2CHAR(p))
10080 break;
10081 if (*p == NUL)
10082 break;
10083 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010084 }
10085 }
Bram Moolenaardf44a272020-06-07 20:49:05 +020010086 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010087}
10088
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010089#ifdef FEAT_FLOAT
10090/*
10091 * "trunc({float})" function
10092 */
10093 static void
10094f_trunc(typval_T *argvars, typval_T *rettv)
10095{
10096 float_T f = 0.0;
10097
10098 rettv->v_type = VAR_FLOAT;
10099 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010100 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010101 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
10102 else
10103 rettv->vval.v_float = 0.0;
10104}
10105#endif
10106
10107/*
10108 * "type(expr)" function
10109 */
10110 static void
10111f_type(typval_T *argvars, typval_T *rettv)
10112{
10113 int n = -1;
10114
10115 switch (argvars[0].v_type)
10116 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010117 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
10118 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010119 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010120 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
10121 case VAR_LIST: n = VAR_TYPE_LIST; break;
10122 case VAR_DICT: n = VAR_TYPE_DICT; break;
10123 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
10124 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
10125 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020010126 case VAR_JOB: n = VAR_TYPE_JOB; break;
10127 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010128 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010129 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +020010130 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010131 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +010010132 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010133 n = -1;
10134 break;
10135 }
10136 rettv->vval.v_number = n;
10137}
10138
10139/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010140 * "virtcol(string)" function
10141 */
10142 static void
10143f_virtcol(typval_T *argvars, typval_T *rettv)
10144{
10145 colnr_T vcol = 0;
10146 pos_T *fp;
10147 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010148 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010149
Bram Moolenaar6f02b002021-01-10 20:22:54 +010010150 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010151 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
10152 && fnum == curbuf->b_fnum)
10153 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010154 // Limit the column to a valid value, getvvcol() doesn't check.
10155 if (fp->col < 0)
10156 fp->col = 0;
10157 else
10158 {
10159 len = (int)STRLEN(ml_get(fp->lnum));
10160 if (fp->col > len)
10161 fp->col = len;
10162 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010163 getvvcol(curwin, fp, NULL, NULL, &vcol);
10164 ++vcol;
10165 }
10166
10167 rettv->vval.v_number = vcol;
10168}
10169
10170/*
10171 * "visualmode()" function
10172 */
10173 static void
10174f_visualmode(typval_T *argvars, typval_T *rettv)
10175{
10176 char_u str[2];
10177
10178 rettv->v_type = VAR_STRING;
10179 str[0] = curbuf->b_visual_mode_eval;
10180 str[1] = NUL;
10181 rettv->vval.v_string = vim_strsave(str);
10182
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010183 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010184 if (non_zero_arg(&argvars[0]))
10185 curbuf->b_visual_mode_eval = NUL;
10186}
10187
10188/*
10189 * "wildmenumode()" function
10190 */
10191 static void
10192f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10193{
10194#ifdef FEAT_WILDMENU
10195 if (wild_menu_showing)
10196 rettv->vval.v_number = 1;
10197#endif
10198}
10199
10200/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +010010201 * "windowsversion()" function
10202 */
10203 static void
10204f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10205{
10206 rettv->v_type = VAR_STRING;
10207 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
10208}
10209
10210/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010211 * "wordcount()" function
10212 */
10213 static void
10214f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
10215{
10216 if (rettv_dict_alloc(rettv) == FAIL)
10217 return;
10218 cursor_pos_info(rettv->vval.v_dict);
10219}
10220
10221/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010222 * "xor(expr, expr)" function
10223 */
10224 static void
10225f_xor(typval_T *argvars, typval_T *rettv)
10226{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010227 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
10228 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010229}
10230
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010231#endif // FEAT_EVAL