blob: a0caf3dce8bf738289eb68339d307efeaeb93de5 [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);
Bram Moolenaar70ce8a12021-03-14 19:02:09 +0100226static void f_strcharlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200227static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200228static void f_strgetchar(typval_T *argvars, typval_T *rettv);
229static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200230static void f_strlen(typval_T *argvars, typval_T *rettv);
231static void f_strcharpart(typval_T *argvars, typval_T *rettv);
232static void f_strpart(typval_T *argvars, typval_T *rettv);
233static void f_strridx(typval_T *argvars, typval_T *rettv);
234static void f_strtrans(typval_T *argvars, typval_T *rettv);
235static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
236static void f_strwidth(typval_T *argvars, typval_T *rettv);
237static void f_submatch(typval_T *argvars, typval_T *rettv);
238static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200239static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200240static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200241static void f_synID(typval_T *argvars, typval_T *rettv);
242static void f_synIDattr(typval_T *argvars, typval_T *rettv);
243static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
244static void f_synstack(typval_T *argvars, typval_T *rettv);
245static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200246static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200247static void f_taglist(typval_T *argvars, typval_T *rettv);
248static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200249#ifdef FEAT_FLOAT
250static void f_tan(typval_T *argvars, typval_T *rettv);
251static void f_tanh(typval_T *argvars, typval_T *rettv);
252#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200253static void f_tolower(typval_T *argvars, typval_T *rettv);
254static void f_toupper(typval_T *argvars, typval_T *rettv);
255static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100256static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200257#ifdef FEAT_FLOAT
258static void f_trunc(typval_T *argvars, typval_T *rettv);
259#endif
260static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200261static void f_virtcol(typval_T *argvars, typval_T *rettv);
262static void f_visualmode(typval_T *argvars, typval_T *rettv);
263static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100264static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200265static void f_wordcount(typval_T *argvars, typval_T *rettv);
266static void f_xor(typval_T *argvars, typval_T *rettv);
267
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100268
Bram Moolenaar94738d82020-10-21 14:25:07 +0200269/*
270 * Functions that check the argument type of a builtin function.
271 * Each function returns FAIL and gives an error message if the type is wrong.
272 */
273
274// Context passed to an arg_ function.
275typedef struct {
Bram Moolenaarca174532020-10-21 16:42:22 +0200276 int arg_count; // actual argument count
277 type_T **arg_types; // list of argument types
278 int arg_idx; // current argument index (first arg is zero)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100279 cctx_T *arg_cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +0200280} argcontext_T;
281
282// A function to check one argument type. The first argument is the type to
283// check. If needed, other argument types can be obtained with the context.
284// E.g. if "arg_idx" is 1, then (type - 1) is the first argument type.
285typedef int (*argcheck_T)(type_T *, argcontext_T *);
286
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100287/*
Bram Moolenaar351ead02021-01-16 16:07:01 +0100288 * Call need_type() to check an argument type.
289 */
290 static int
291check_arg_type(
292 type_T *expected,
293 type_T *actual,
294 argcontext_T *context)
295{
296 // TODO: would be useful to know if "actual" is a constant and pass it to
297 // need_type() to get a compile time error if possible.
298 return need_type(actual, expected,
299 context->arg_idx - context->arg_count, context->arg_idx + 1,
300 context->arg_cctx, FALSE, FALSE);
301}
302
303/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100304 * Check "type" is a float or a number.
305 */
Bram Moolenaar94738d82020-10-21 14:25:07 +0200306 static int
307arg_float_or_nr(type_T *type, argcontext_T *context)
308{
Bram Moolenaarca174532020-10-21 16:42:22 +0200309 if (type->tt_type == VAR_ANY
310 || type->tt_type == VAR_FLOAT || type->tt_type == VAR_NUMBER)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200311 return OK;
312 arg_type_mismatch(&t_number, type, context->arg_idx + 1);
313 return FAIL;
314}
315
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100316/*
317 * Check "type" is a number.
318 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200319 static int
320arg_number(type_T *type, argcontext_T *context)
321{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100322 return check_arg_type(&t_number, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200323}
324
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100325/*
326 * Check "type" is a string.
327 */
328 static int
329arg_string(type_T *type, argcontext_T *context)
330{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100331 return check_arg_type(&t_string, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100332}
333
334/*
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100335 * Check "type" is a bool or number 0 or 1.
336 */
337 static int
338arg_bool(type_T *type, argcontext_T *context)
339{
340 if (type->tt_type == VAR_ANY
341 || type->tt_type == VAR_NUMBER || type->tt_type == VAR_BOOL)
342 return OK;
343 return check_arg_type(&t_bool, type, context);
344}
345
346/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100347 * Check "type" is a list or a blob.
348 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200349 static int
350arg_list_or_blob(type_T *type, argcontext_T *context)
351{
352 if (type->tt_type == VAR_ANY
353 || type->tt_type == VAR_LIST || type->tt_type == VAR_BLOB)
354 return OK;
355 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
356 return FAIL;
357}
358
359/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100360 * Check "type" is a list or a dict.
361 */
362 static int
363arg_list_or_dict(type_T *type, argcontext_T *context)
364{
365 if (type->tt_type == VAR_ANY
366 || type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
367 return OK;
368 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
369 return FAIL;
370}
371
372/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100373 * Check "type" is the same type as the previous argument.
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100374 * Must not be used for the first argcheck_T entry.
375 */
376 static int
377arg_same_as_prev(type_T *type, argcontext_T *context)
378{
379 type_T *prev_type = context->arg_types[context->arg_idx - 1];
380
Bram Moolenaar351ead02021-01-16 16:07:01 +0100381 return check_arg_type(prev_type, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100382}
383
384/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100385 * Check "type" is the same basic type as the previous argument, checks list or
386 * dict vs other type, but not member type.
387 * Must not be used for the first argcheck_T entry.
388 */
389 static int
390arg_same_struct_as_prev(type_T *type, argcontext_T *context)
391{
392 type_T *prev_type = context->arg_types[context->arg_idx - 1];
393
394 if (prev_type->tt_type != context->arg_types[context->arg_idx]->tt_type)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100395 return check_arg_type(prev_type, type, context);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100396 return OK;
397}
398
399/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100400 * Check "type" is an item of the list or blob of the previous arg.
Bram Moolenaarca174532020-10-21 16:42:22 +0200401 * Must not be used for the first argcheck_T entry.
402 */
403 static int
404arg_item_of_prev(type_T *type, argcontext_T *context)
405{
406 type_T *prev_type = context->arg_types[context->arg_idx - 1];
407 type_T *expected;
408
409 if (prev_type->tt_type == VAR_LIST)
410 expected = prev_type->tt_member;
411 else if (prev_type->tt_type == VAR_BLOB)
412 expected = &t_number;
413 else
414 // probably VAR_ANY, can't check
415 return OK;
416
Bram Moolenaar351ead02021-01-16 16:07:01 +0100417 return check_arg_type(expected, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200418}
419
Bram Moolenaar94738d82020-10-21 14:25:07 +0200420/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100421 * Check "type" which is the third argument of extend().
422 */
423 static int
424arg_extend3(type_T *type, argcontext_T *context)
425{
426 type_T *first_type = context->arg_types[context->arg_idx - 2];
427
428 if (first_type->tt_type == VAR_LIST)
429 return arg_number(type, context);
430 if (first_type->tt_type == VAR_DICT)
431 return arg_string(type, context);
432 return OK;
433}
434
435
436/*
Bram Moolenaar94738d82020-10-21 14:25:07 +0200437 * Lists of functions that check the argument types of a builtin function.
438 */
Bram Moolenaar80ad3e22021-01-31 20:48:58 +0100439argcheck_T arg1_string[] = {arg_string};
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100440argcheck_T arg3_string_nr_bool[] = {arg_string, arg_number, arg_bool};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200441argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100442argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev};
443argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3};
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100444argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
Bram Moolenaarca174532020-10-21 16:42:22 +0200445argcheck_T arg3_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200446
447/*
448 * Functions that return the return type of a builtin function.
Bram Moolenaara1224cb2020-10-22 12:31:49 +0200449 * Note that "argtypes" is NULL if "argcount" is zero.
Bram Moolenaar94738d82020-10-21 14:25:07 +0200450 */
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100451 static type_T *
452ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
453{
454 return &t_void;
455}
456 static type_T *
457ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
458{
459 return &t_any;
460}
461 static type_T *
Bram Moolenaar403dc312020-10-17 19:29:51 +0200462ret_bool(int argcount UNUSED, type_T **argtypes UNUSED)
463{
464 return &t_bool;
465}
466 static type_T *
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100467ret_number_bool(int argcount UNUSED, type_T **argtypes UNUSED)
468{
469 return &t_number_bool;
470}
471 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100472ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
473{
474 return &t_number;
475}
476 static type_T *
477ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
478{
479 return &t_float;
480}
481 static type_T *
482ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
483{
484 return &t_string;
485}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200486 static type_T *
487ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100488{
489 return &t_list_any;
490}
491 static type_T *
492ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
493{
494 return &t_list_number;
495}
496 static type_T *
497ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
498{
499 return &t_list_string;
500}
501 static type_T *
502ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
503{
504 return &t_list_dict_any;
505}
506 static type_T *
507ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
508{
509 return &t_dict_any;
510}
511 static type_T *
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100512ret_job_info(int argcount, type_T **argtypes UNUSED)
513{
514 if (argcount == 0)
515 return &t_list_job;
516 return &t_dict_any;
517}
518 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100519ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
520{
521 return &t_dict_number;
522}
523 static type_T *
524ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
525{
526 return &t_dict_string;
527}
528 static type_T *
529ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
530{
531 return &t_blob;
532}
533 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200534ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100535{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200536 return &t_func_any;
537}
538 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100539ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
540{
541 return &t_channel;
542}
543 static type_T *
544ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
545{
546 return &t_job;
547}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200548 static type_T *
549ret_first_arg(int argcount, type_T **argtypes)
550{
551 if (argcount > 0)
552 return argtypes[0];
553 return &t_void;
554}
Bram Moolenaarea696852020-11-09 18:31:39 +0100555// for map(): returns first argument but item type may differ
556 static type_T *
557ret_first_cont(int argcount UNUSED, type_T **argtypes)
558{
559 if (argtypes[0]->tt_type == VAR_LIST)
560 return &t_list_any;
561 if (argtypes[0]->tt_type == VAR_DICT)
562 return &t_dict_any;
563 if (argtypes[0]->tt_type == VAR_BLOB)
564 return argtypes[0];
565 return &t_any;
566}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200567
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200568/*
569 * Used for getqflist(): returns list if there is no argument, dict if there is
570 * one.
571 */
572 static type_T *
573ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
574{
575 if (argcount > 0)
576 return &t_dict_any;
577 return &t_list_dict_any;
578}
579
580/*
581 * Used for getloclist(): returns list if there is one argument, dict if there
582 * are two.
583 */
584 static type_T *
585ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
586{
587 if (argcount > 1)
588 return &t_dict_any;
589 return &t_list_dict_any;
590}
591
Bram Moolenaar846178a2020-07-05 17:04:13 +0200592 static type_T *
593ret_argv(int argcount, type_T **argtypes UNUSED)
594{
595 // argv() returns list of strings
596 if (argcount == 0)
597 return &t_list_string;
598
599 // argv(0) returns a string, but argv(-1] returns a list
600 return &t_any;
601}
602
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200603 static type_T *
604ret_remove(int argcount UNUSED, type_T **argtypes)
605{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200606 if (argtypes != NULL)
607 {
608 if (argtypes[0]->tt_type == VAR_LIST
609 || argtypes[0]->tt_type == VAR_DICT)
610 return argtypes[0]->tt_member;
611 if (argtypes[0]->tt_type == VAR_BLOB)
612 return &t_number;
613 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200614 return &t_any;
615}
616
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200617 static type_T *
618ret_getreg(int argcount, type_T **argtypes UNUSED)
619{
620 // Assume that if the third argument is passed it's non-zero
621 if (argcount == 3)
622 return &t_list_string;
623 return &t_string;
624}
625
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200626 static type_T *
627ret_maparg(int argcount, type_T **argtypes UNUSED)
628{
629 // Assume that if the fourth argument is passed it's non-zero
630 if (argcount == 4)
631 return &t_dict_any;
632 return &t_string;
633}
634
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100635static type_T *ret_f_function(int argcount, type_T **argtypes);
636
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200637/*
638 * Array with names and number of arguments of all internal functions
639 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
640 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200641typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200642{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200643 char *f_name; // function name
644 char f_min_argc; // minimal number of arguments
645 char f_max_argc; // maximal number of arguments
646 char f_argtype; // for method: FEARG_ values
Bram Moolenaar94738d82020-10-21 14:25:07 +0200647 argcheck_T *f_argcheck; // list of functions to check argument types
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100648 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
649 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200650 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200651 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200652} funcentry_T;
653
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200654// values for f_argtype; zero means it cannot be used as a method
655#define FEARG_1 1 // base is the first argument
656#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200657#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200658#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200659#define FEARG_LAST 9 // base is the last argument
660
Bram Moolenaar15c47602020-03-26 22:16:48 +0100661#ifdef FEAT_FLOAT
662# define FLOAT_FUNC(name) name
663#else
664# define FLOAT_FUNC(name) NULL
665#endif
666#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
667# define MATH_FUNC(name) name
668#else
669# define MATH_FUNC(name) NULL
670#endif
671#ifdef FEAT_TIMERS
672# define TIMER_FUNC(name) name
673#else
674# define TIMER_FUNC(name) NULL
675#endif
676#ifdef FEAT_JOB_CHANNEL
677# define JOB_FUNC(name) name
678#else
679# define JOB_FUNC(name) NULL
680#endif
681#ifdef FEAT_PROP_POPUP
682# define PROP_FUNC(name) name
683#else
684# define PROP_FUNC(name) NULL
685#endif
686#ifdef FEAT_SIGNS
687# define SIGN_FUNC(name) name
688#else
689# define SIGN_FUNC(name) NULL
690#endif
691#ifdef FEAT_SOUND
692# define SOUND_FUNC(name) name
693#else
694# define SOUND_FUNC(name) NULL
695#endif
696#ifdef FEAT_TERMINAL
697# define TERM_FUNC(name) name
698#else
699# define TERM_FUNC(name) NULL
700#endif
701
Bram Moolenaarac92e252019-08-03 21:58:38 +0200702static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200703{
Bram Moolenaar94738d82020-10-21 14:25:07 +0200704 {"abs", 1, 1, FEARG_1, arg1_float_or_nr,
705 ret_any, FLOAT_FUNC(f_abs)},
706 {"acos", 1, 1, FEARG_1, NULL,
707 ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100708 {"add", 2, 2, FEARG_1, NULL /* arg2_listblob_item */,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200709 ret_first_arg, f_add},
710 {"and", 2, 2, FEARG_1, NULL,
711 ret_number, f_and},
712 {"append", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100713 ret_number_bool, f_append},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200714 {"appendbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100715 ret_number_bool, f_appendbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200716 {"argc", 0, 1, 0, NULL,
717 ret_number, f_argc},
718 {"argidx", 0, 0, 0, NULL,
719 ret_number, f_argidx},
720 {"arglistid", 0, 2, 0, NULL,
721 ret_number, f_arglistid},
722 {"argv", 0, 2, 0, NULL,
723 ret_argv, f_argv},
724 {"asin", 1, 1, FEARG_1, NULL,
725 ret_float, FLOAT_FUNC(f_asin)},
726 {"assert_beeps", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100727 ret_number_bool, f_assert_beeps},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200728 {"assert_equal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100729 ret_number_bool, f_assert_equal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200730 {"assert_equalfile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100731 ret_number_bool, f_assert_equalfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200732 {"assert_exception", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100733 ret_number_bool, f_assert_exception},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200734 {"assert_fails", 1, 5, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100735 ret_number_bool, f_assert_fails},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200736 {"assert_false", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100737 ret_number_bool, f_assert_false},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200738 {"assert_inrange", 3, 4, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100739 ret_number_bool, f_assert_inrange},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200740 {"assert_match", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100741 ret_number_bool, f_assert_match},
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +0200742 {"assert_nobeep", 1, 2, FEARG_1, NULL,
743 ret_number_bool, f_assert_nobeep},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200744 {"assert_notequal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100745 ret_number_bool, f_assert_notequal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200746 {"assert_notmatch", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100747 ret_number_bool, f_assert_notmatch},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200748 {"assert_report", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100749 ret_number_bool, f_assert_report},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200750 {"assert_true", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100751 ret_number_bool, f_assert_true},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200752 {"atan", 1, 1, FEARG_1, NULL,
753 ret_float, FLOAT_FUNC(f_atan)},
754 {"atan2", 2, 2, FEARG_1, NULL,
755 ret_float, FLOAT_FUNC(f_atan2)},
756 {"balloon_gettext", 0, 0, 0, NULL,
757 ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100758#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100759 f_balloon_gettext
760#else
761 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100762#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100763 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200764 {"balloon_show", 1, 1, FEARG_1, NULL,
765 ret_void,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100766#ifdef FEAT_BEVAL
767 f_balloon_show
768#else
769 NULL
770#endif
771 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200772 {"balloon_split", 1, 1, FEARG_1, NULL,
773 ret_list_string,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100774#if defined(FEAT_BEVAL_TERM)
775 f_balloon_split
776#else
777 NULL
778#endif
779 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200780 {"browse", 4, 4, 0, NULL,
781 ret_string, f_browse},
782 {"browsedir", 2, 2, 0, NULL,
783 ret_string, f_browsedir},
784 {"bufadd", 1, 1, FEARG_1, NULL,
785 ret_number, f_bufadd},
786 {"bufexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100787 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200788 {"buffer_exists", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100789 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200790 {"buffer_name", 0, 1, FEARG_1, NULL, // obsolete
791 ret_string, f_bufname},
792 {"buffer_number", 0, 1, FEARG_1, NULL, // obsolete
793 ret_number, f_bufnr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200794 {"buflisted", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100795 ret_number_bool, f_buflisted},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200796 {"bufload", 1, 1, FEARG_1, NULL,
797 ret_void, f_bufload},
798 {"bufloaded", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100799 ret_number_bool, f_bufloaded},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200800 {"bufname", 0, 1, FEARG_1, NULL,
801 ret_string, f_bufname},
802 {"bufnr", 0, 2, FEARG_1, NULL,
803 ret_number, f_bufnr},
804 {"bufwinid", 1, 1, FEARG_1, NULL,
805 ret_number, f_bufwinid},
806 {"bufwinnr", 1, 1, FEARG_1, NULL,
807 ret_number, f_bufwinnr},
808 {"byte2line", 1, 1, FEARG_1, NULL,
809 ret_number, f_byte2line},
810 {"byteidx", 2, 2, FEARG_1, NULL,
811 ret_number, f_byteidx},
812 {"byteidxcomp", 2, 2, FEARG_1, NULL,
813 ret_number, f_byteidxcomp},
814 {"call", 2, 3, FEARG_1, NULL,
815 ret_any, f_call},
816 {"ceil", 1, 1, FEARG_1, NULL,
817 ret_float, FLOAT_FUNC(f_ceil)},
818 {"ch_canread", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100819 ret_number_bool, JOB_FUNC(f_ch_canread)},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200820 {"ch_close", 1, 1, FEARG_1, NULL,
821 ret_void, JOB_FUNC(f_ch_close)},
822 {"ch_close_in", 1, 1, FEARG_1, NULL,
823 ret_void, JOB_FUNC(f_ch_close_in)},
824 {"ch_evalexpr", 2, 3, FEARG_1, NULL,
825 ret_any, JOB_FUNC(f_ch_evalexpr)},
826 {"ch_evalraw", 2, 3, FEARG_1, NULL,
827 ret_any, JOB_FUNC(f_ch_evalraw)},
828 {"ch_getbufnr", 2, 2, FEARG_1, NULL,
829 ret_number, JOB_FUNC(f_ch_getbufnr)},
830 {"ch_getjob", 1, 1, FEARG_1, NULL,
831 ret_job, JOB_FUNC(f_ch_getjob)},
832 {"ch_info", 1, 1, FEARG_1, NULL,
833 ret_dict_any, JOB_FUNC(f_ch_info)},
834 {"ch_log", 1, 2, FEARG_1, NULL,
835 ret_void, JOB_FUNC(f_ch_log)},
836 {"ch_logfile", 1, 2, FEARG_1, NULL,
837 ret_void, JOB_FUNC(f_ch_logfile)},
838 {"ch_open", 1, 2, FEARG_1, NULL,
839 ret_channel, JOB_FUNC(f_ch_open)},
840 {"ch_read", 1, 2, FEARG_1, NULL,
841 ret_string, JOB_FUNC(f_ch_read)},
842 {"ch_readblob", 1, 2, FEARG_1, NULL,
843 ret_blob, JOB_FUNC(f_ch_readblob)},
844 {"ch_readraw", 1, 2, FEARG_1, NULL,
845 ret_string, JOB_FUNC(f_ch_readraw)},
846 {"ch_sendexpr", 2, 3, FEARG_1, NULL,
847 ret_void, JOB_FUNC(f_ch_sendexpr)},
848 {"ch_sendraw", 2, 3, FEARG_1, NULL,
849 ret_void, JOB_FUNC(f_ch_sendraw)},
850 {"ch_setoptions", 2, 2, FEARG_1, NULL,
851 ret_void, JOB_FUNC(f_ch_setoptions)},
852 {"ch_status", 1, 2, FEARG_1, NULL,
853 ret_string, JOB_FUNC(f_ch_status)},
854 {"changenr", 0, 0, 0, NULL,
855 ret_number, f_changenr},
856 {"char2nr", 1, 2, FEARG_1, NULL,
857 ret_number, f_char2nr},
858 {"charclass", 1, 1, FEARG_1, NULL,
859 ret_number, f_charclass},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100860 {"charcol", 1, 1, FEARG_1, NULL,
861 ret_number, f_charcol},
Bram Moolenaar17793ef2020-12-28 12:56:58 +0100862 {"charidx", 2, 3, FEARG_1, NULL,
863 ret_number, f_charidx},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200864 {"chdir", 1, 1, FEARG_1, NULL,
865 ret_string, f_chdir},
866 {"cindent", 1, 1, FEARG_1, NULL,
867 ret_number, f_cindent},
868 {"clearmatches", 0, 1, FEARG_1, NULL,
869 ret_void, f_clearmatches},
870 {"col", 1, 1, FEARG_1, NULL,
871 ret_number, f_col},
872 {"complete", 2, 2, FEARG_2, NULL,
873 ret_void, f_complete},
874 {"complete_add", 1, 1, FEARG_1, NULL,
875 ret_number, f_complete_add},
876 {"complete_check", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100877 ret_number_bool, f_complete_check},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200878 {"complete_info", 0, 1, FEARG_1, NULL,
879 ret_dict_any, f_complete_info},
880 {"confirm", 1, 4, FEARG_1, NULL,
881 ret_number, f_confirm},
882 {"copy", 1, 1, FEARG_1, NULL,
883 ret_first_arg, f_copy},
884 {"cos", 1, 1, FEARG_1, NULL,
885 ret_float, FLOAT_FUNC(f_cos)},
886 {"cosh", 1, 1, FEARG_1, NULL,
887 ret_float, FLOAT_FUNC(f_cosh)},
888 {"count", 2, 4, FEARG_1, NULL,
889 ret_number, f_count},
890 {"cscope_connection",0,3, 0, NULL,
891 ret_number, f_cscope_connection},
892 {"cursor", 1, 3, FEARG_1, NULL,
893 ret_number, f_cursor},
894 {"debugbreak", 1, 1, FEARG_1, NULL,
895 ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100896#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100897 f_debugbreak
898#else
899 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200900#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100901 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200902 {"deepcopy", 1, 2, FEARG_1, NULL,
903 ret_first_arg, f_deepcopy},
904 {"delete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100905 ret_number_bool, f_delete},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200906 {"deletebufline", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100907 ret_number_bool, f_deletebufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200908 {"did_filetype", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100909 ret_number_bool, f_did_filetype},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200910 {"diff_filler", 1, 1, FEARG_1, NULL,
911 ret_number, f_diff_filler},
912 {"diff_hlID", 2, 2, FEARG_1, NULL,
913 ret_number, f_diff_hlID},
914 {"echoraw", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100915 ret_void, f_echoraw},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200916 {"empty", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100917 ret_number_bool, f_empty},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200918 {"environ", 0, 0, 0, NULL,
919 ret_dict_string, f_environ},
920 {"escape", 2, 2, FEARG_1, NULL,
921 ret_string, f_escape},
922 {"eval", 1, 1, FEARG_1, NULL,
923 ret_any, f_eval},
924 {"eventhandler", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100925 ret_number_bool, f_eventhandler},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200926 {"executable", 1, 1, FEARG_1, NULL,
927 ret_number, f_executable},
928 {"execute", 1, 2, FEARG_1, NULL,
929 ret_string, f_execute},
930 {"exepath", 1, 1, FEARG_1, NULL,
931 ret_string, f_exepath},
932 {"exists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100933 ret_number_bool, f_exists},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200934 {"exp", 1, 1, FEARG_1, NULL,
935 ret_float, FLOAT_FUNC(f_exp)},
936 {"expand", 1, 3, FEARG_1, NULL,
937 ret_any, f_expand},
938 {"expandcmd", 1, 1, FEARG_1, NULL,
939 ret_string, f_expandcmd},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100940 {"extend", 2, 3, FEARG_1, arg23_extend,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200941 ret_first_arg, f_extend},
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100942 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
943 ret_first_cont, f_extendnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200944 {"feedkeys", 1, 2, FEARG_1, NULL,
945 ret_void, f_feedkeys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200946 {"file_readable", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100947 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200948 {"filereadable", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100949 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200950 {"filewritable", 1, 1, FEARG_1, NULL,
951 ret_number, f_filewritable},
952 {"filter", 2, 2, FEARG_1, NULL,
953 ret_first_arg, f_filter},
954 {"finddir", 1, 3, FEARG_1, NULL,
955 ret_string, f_finddir},
956 {"findfile", 1, 3, FEARG_1, NULL,
957 ret_string, f_findfile},
958 {"flatten", 1, 2, FEARG_1, NULL,
959 ret_list_any, f_flatten},
Bram Moolenaar3b690062021-02-01 20:14:51 +0100960 {"flattennew", 1, 2, FEARG_1, NULL,
961 ret_list_any, f_flattennew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200962 {"float2nr", 1, 1, FEARG_1, NULL,
963 ret_number, FLOAT_FUNC(f_float2nr)},
964 {"floor", 1, 1, FEARG_1, NULL,
965 ret_float, FLOAT_FUNC(f_floor)},
966 {"fmod", 2, 2, FEARG_1, NULL,
967 ret_float, FLOAT_FUNC(f_fmod)},
968 {"fnameescape", 1, 1, FEARG_1, NULL,
969 ret_string, f_fnameescape},
970 {"fnamemodify", 2, 2, FEARG_1, NULL,
971 ret_string, f_fnamemodify},
972 {"foldclosed", 1, 1, FEARG_1, NULL,
973 ret_number, f_foldclosed},
974 {"foldclosedend", 1, 1, FEARG_1, NULL,
975 ret_number, f_foldclosedend},
976 {"foldlevel", 1, 1, FEARG_1, NULL,
977 ret_number, f_foldlevel},
978 {"foldtext", 0, 0, 0, NULL,
979 ret_string, f_foldtext},
980 {"foldtextresult", 1, 1, FEARG_1, NULL,
981 ret_string, f_foldtextresult},
982 {"foreground", 0, 0, 0, NULL,
983 ret_void, f_foreground},
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100984 {"fullcommand", 1, 1, FEARG_1, arg1_string,
985 ret_string, f_fullcommand},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200986 {"funcref", 1, 3, FEARG_1, NULL,
987 ret_func_any, f_funcref},
988 {"function", 1, 3, FEARG_1, NULL,
989 ret_f_function, f_function},
990 {"garbagecollect", 0, 1, 0, NULL,
991 ret_void, f_garbagecollect},
992 {"get", 2, 3, FEARG_1, NULL,
993 ret_any, f_get},
994 {"getbufinfo", 0, 1, FEARG_1, NULL,
995 ret_list_dict_any, f_getbufinfo},
996 {"getbufline", 2, 3, FEARG_1, NULL,
997 ret_list_string, f_getbufline},
998 {"getbufvar", 2, 3, FEARG_1, NULL,
999 ret_any, f_getbufvar},
1000 {"getchangelist", 0, 1, FEARG_1, NULL,
1001 ret_list_any, f_getchangelist},
1002 {"getchar", 0, 1, 0, NULL,
1003 ret_number, f_getchar},
1004 {"getcharmod", 0, 0, 0, NULL,
1005 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001006 {"getcharpos", 1, 1, FEARG_1, NULL,
1007 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001008 {"getcharsearch", 0, 0, 0, NULL,
1009 ret_dict_any, f_getcharsearch},
1010 {"getcmdline", 0, 0, 0, NULL,
1011 ret_string, f_getcmdline},
1012 {"getcmdpos", 0, 0, 0, NULL,
1013 ret_number, f_getcmdpos},
1014 {"getcmdtype", 0, 0, 0, NULL,
1015 ret_string, f_getcmdtype},
1016 {"getcmdwintype", 0, 0, 0, NULL,
1017 ret_string, f_getcmdwintype},
1018 {"getcompletion", 2, 3, FEARG_1, NULL,
1019 ret_list_string, f_getcompletion},
1020 {"getcurpos", 0, 1, FEARG_1, NULL,
1021 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001022 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
1023 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001024 {"getcwd", 0, 2, FEARG_1, NULL,
1025 ret_string, f_getcwd},
1026 {"getenv", 1, 1, FEARG_1, NULL,
Bram Moolenaar7ad67d12021-03-10 16:08:26 +01001027 ret_any, f_getenv},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001028 {"getfontname", 0, 1, 0, NULL,
1029 ret_string, f_getfontname},
1030 {"getfperm", 1, 1, FEARG_1, NULL,
1031 ret_string, f_getfperm},
1032 {"getfsize", 1, 1, FEARG_1, NULL,
1033 ret_number, f_getfsize},
1034 {"getftime", 1, 1, FEARG_1, NULL,
1035 ret_number, f_getftime},
1036 {"getftype", 1, 1, FEARG_1, NULL,
1037 ret_string, f_getftype},
1038 {"getimstatus", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001039 ret_number_bool, f_getimstatus},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001040 {"getjumplist", 0, 2, FEARG_1, NULL,
1041 ret_list_any, f_getjumplist},
1042 {"getline", 1, 2, FEARG_1, NULL,
1043 ret_f_getline, f_getline},
1044 {"getloclist", 1, 2, 0, NULL,
1045 ret_list_or_dict_1, f_getloclist},
1046 {"getmarklist", 0, 1, FEARG_1, NULL,
1047 ret_list_dict_any, f_getmarklist},
1048 {"getmatches", 0, 1, 0, NULL,
1049 ret_list_dict_any, f_getmatches},
1050 {"getmousepos", 0, 0, 0, NULL,
1051 ret_dict_number, f_getmousepos},
1052 {"getpid", 0, 0, 0, NULL,
1053 ret_number, f_getpid},
1054 {"getpos", 1, 1, FEARG_1, NULL,
1055 ret_list_number, f_getpos},
1056 {"getqflist", 0, 1, 0, NULL,
1057 ret_list_or_dict_0, f_getqflist},
1058 {"getreg", 0, 3, FEARG_1, NULL,
1059 ret_getreg, f_getreg},
1060 {"getreginfo", 0, 1, FEARG_1, NULL,
1061 ret_dict_any, f_getreginfo},
1062 {"getregtype", 0, 1, FEARG_1, NULL,
1063 ret_string, f_getregtype},
1064 {"gettabinfo", 0, 1, FEARG_1, NULL,
1065 ret_list_dict_any, f_gettabinfo},
1066 {"gettabvar", 2, 3, FEARG_1, NULL,
1067 ret_any, f_gettabvar},
1068 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1069 ret_any, f_gettabwinvar},
1070 {"gettagstack", 0, 1, FEARG_1, NULL,
1071 ret_dict_any, f_gettagstack},
1072 {"gettext", 1, 1, FEARG_1, NULL,
1073 ret_string, f_gettext},
1074 {"getwininfo", 0, 1, FEARG_1, NULL,
1075 ret_list_dict_any, f_getwininfo},
1076 {"getwinpos", 0, 1, FEARG_1, NULL,
1077 ret_list_number, f_getwinpos},
1078 {"getwinposx", 0, 0, 0, NULL,
1079 ret_number, f_getwinposx},
1080 {"getwinposy", 0, 0, 0, NULL,
1081 ret_number, f_getwinposy},
1082 {"getwinvar", 2, 3, FEARG_1, NULL,
1083 ret_any, f_getwinvar},
1084 {"glob", 1, 4, FEARG_1, NULL,
1085 ret_any, f_glob},
1086 {"glob2regpat", 1, 1, FEARG_1, NULL,
1087 ret_string, f_glob2regpat},
1088 {"globpath", 2, 5, FEARG_2, NULL,
1089 ret_any, f_globpath},
1090 {"has", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001091 ret_number_bool, f_has},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001092 {"has_key", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001093 ret_number_bool, f_has_key},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001094 {"haslocaldir", 0, 2, FEARG_1, NULL,
1095 ret_number, f_haslocaldir},
1096 {"hasmapto", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001097 ret_number_bool, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001098 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1099 ret_number, f_hlID},
1100 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001101 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001102 {"histadd", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001103 ret_number_bool, f_histadd},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001104 {"histdel", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001105 ret_number_bool, f_histdel},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001106 {"histget", 1, 2, FEARG_1, NULL,
1107 ret_string, f_histget},
1108 {"histnr", 1, 1, FEARG_1, NULL,
1109 ret_number, f_histnr},
1110 {"hlID", 1, 1, FEARG_1, NULL,
1111 ret_number, f_hlID},
1112 {"hlexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001113 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001114 {"hostname", 0, 0, 0, NULL,
1115 ret_string, f_hostname},
1116 {"iconv", 3, 3, FEARG_1, NULL,
1117 ret_string, f_iconv},
1118 {"indent", 1, 1, FEARG_1, NULL,
1119 ret_number, f_indent},
1120 {"index", 2, 4, FEARG_1, NULL,
1121 ret_number, f_index},
1122 {"input", 1, 3, FEARG_1, NULL,
1123 ret_string, f_input},
1124 {"inputdialog", 1, 3, FEARG_1, NULL,
1125 ret_string, f_inputdialog},
1126 {"inputlist", 1, 1, FEARG_1, NULL,
1127 ret_number, f_inputlist},
1128 {"inputrestore", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001129 ret_number_bool, f_inputrestore},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001130 {"inputsave", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001131 ret_number_bool, f_inputsave},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001132 {"inputsecret", 1, 2, FEARG_1, NULL,
1133 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001134 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001135 ret_first_arg, f_insert},
1136 {"interrupt", 0, 0, 0, NULL,
1137 ret_void, f_interrupt},
1138 {"invert", 1, 1, FEARG_1, NULL,
1139 ret_number, f_invert},
1140 {"isdirectory", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001141 ret_number_bool, f_isdirectory},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001142 {"isinf", 1, 1, FEARG_1, NULL,
1143 ret_number, MATH_FUNC(f_isinf)},
1144 {"islocked", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001145 ret_number_bool, f_islocked},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001146 {"isnan", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001147 ret_number_bool, MATH_FUNC(f_isnan)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001148 {"items", 1, 1, FEARG_1, NULL,
1149 ret_list_any, f_items},
1150 {"job_getchannel", 1, 1, FEARG_1, NULL,
1151 ret_channel, JOB_FUNC(f_job_getchannel)},
1152 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001153 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001154 {"job_setoptions", 2, 2, FEARG_1, NULL,
1155 ret_void, JOB_FUNC(f_job_setoptions)},
1156 {"job_start", 1, 2, FEARG_1, NULL,
1157 ret_job, JOB_FUNC(f_job_start)},
1158 {"job_status", 1, 1, FEARG_1, NULL,
1159 ret_string, JOB_FUNC(f_job_status)},
1160 {"job_stop", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001161 ret_number_bool, JOB_FUNC(f_job_stop)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001162 {"join", 1, 2, FEARG_1, NULL,
1163 ret_string, f_join},
1164 {"js_decode", 1, 1, FEARG_1, NULL,
1165 ret_any, f_js_decode},
1166 {"js_encode", 1, 1, FEARG_1, NULL,
1167 ret_string, f_js_encode},
1168 {"json_decode", 1, 1, FEARG_1, NULL,
1169 ret_any, f_json_decode},
1170 {"json_encode", 1, 1, FEARG_1, NULL,
1171 ret_string, f_json_encode},
1172 {"keys", 1, 1, FEARG_1, NULL,
1173 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001174 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1175 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001176 {"len", 1, 1, FEARG_1, NULL,
1177 ret_number, f_len},
1178 {"libcall", 3, 3, FEARG_3, NULL,
1179 ret_string, f_libcall},
1180 {"libcallnr", 3, 3, FEARG_3, NULL,
1181 ret_number, f_libcallnr},
1182 {"line", 1, 2, FEARG_1, NULL,
1183 ret_number, f_line},
1184 {"line2byte", 1, 1, FEARG_1, NULL,
1185 ret_number, f_line2byte},
1186 {"lispindent", 1, 1, FEARG_1, NULL,
1187 ret_number, f_lispindent},
1188 {"list2str", 1, 2, FEARG_1, NULL,
1189 ret_string, f_list2str},
1190 {"listener_add", 1, 2, FEARG_2, NULL,
1191 ret_number, f_listener_add},
1192 {"listener_flush", 0, 1, FEARG_1, NULL,
1193 ret_void, f_listener_flush},
1194 {"listener_remove", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001195 ret_number_bool, f_listener_remove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001196 {"localtime", 0, 0, 0, NULL,
1197 ret_number, f_localtime},
1198 {"log", 1, 1, FEARG_1, NULL,
1199 ret_float, FLOAT_FUNC(f_log)},
1200 {"log10", 1, 1, FEARG_1, NULL,
1201 ret_float, FLOAT_FUNC(f_log10)},
1202 {"luaeval", 1, 2, FEARG_1, NULL,
1203 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001204#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001205 f_luaeval
1206#else
1207 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001208#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001209 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001210 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001211 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001212 {"maparg", 1, 4, FEARG_1, NULL,
1213 ret_maparg, f_maparg},
1214 {"mapcheck", 1, 3, FEARG_1, NULL,
1215 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001216 {"mapnew", 2, 2, FEARG_1, NULL,
1217 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001218 {"mapset", 3, 3, FEARG_1, NULL,
1219 ret_void, f_mapset},
1220 {"match", 2, 4, FEARG_1, NULL,
1221 ret_any, f_match},
1222 {"matchadd", 2, 5, FEARG_1, NULL,
1223 ret_number, f_matchadd},
1224 {"matchaddpos", 2, 5, FEARG_1, NULL,
1225 ret_number, f_matchaddpos},
1226 {"matcharg", 1, 1, FEARG_1, NULL,
1227 ret_list_string, f_matcharg},
1228 {"matchdelete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001229 ret_number_bool, f_matchdelete},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001230 {"matchend", 2, 4, FEARG_1, NULL,
1231 ret_number, f_matchend},
1232 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1233 ret_list_string, f_matchfuzzy},
1234 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1235 ret_list_any, f_matchfuzzypos},
1236 {"matchlist", 2, 4, FEARG_1, NULL,
1237 ret_list_string, f_matchlist},
1238 {"matchstr", 2, 4, FEARG_1, NULL,
1239 ret_string, f_matchstr},
1240 {"matchstrpos", 2, 4, FEARG_1, NULL,
1241 ret_list_any, f_matchstrpos},
1242 {"max", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001243 ret_number, f_max},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001244 {"menu_info", 1, 2, FEARG_1, NULL,
1245 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001246#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001247 f_menu_info
1248#else
1249 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001250#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001251 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001252 {"min", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001253 ret_number, f_min},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001254 {"mkdir", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001255 ret_number_bool, f_mkdir},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001256 {"mode", 0, 1, FEARG_1, NULL,
1257 ret_string, f_mode},
1258 {"mzeval", 1, 1, FEARG_1, NULL,
1259 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001260#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001261 f_mzeval
1262#else
1263 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001264#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001265 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001266 {"nextnonblank", 1, 1, FEARG_1, NULL,
1267 ret_number, f_nextnonblank},
1268 {"nr2char", 1, 2, FEARG_1, NULL,
1269 ret_string, f_nr2char},
1270 {"or", 2, 2, FEARG_1, NULL,
1271 ret_number, f_or},
1272 {"pathshorten", 1, 2, FEARG_1, NULL,
1273 ret_string, f_pathshorten},
1274 {"perleval", 1, 1, FEARG_1, NULL,
1275 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001276#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001277 f_perleval
1278#else
1279 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001280#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001281 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001282 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1283 ret_number, PROP_FUNC(f_popup_atcursor)},
1284 {"popup_beval", 2, 2, FEARG_1, NULL,
1285 ret_number, PROP_FUNC(f_popup_beval)},
1286 {"popup_clear", 0, 1, 0, NULL,
1287 ret_void, PROP_FUNC(f_popup_clear)},
1288 {"popup_close", 1, 2, FEARG_1, NULL,
1289 ret_void, PROP_FUNC(f_popup_close)},
1290 {"popup_create", 2, 2, FEARG_1, NULL,
1291 ret_number, PROP_FUNC(f_popup_create)},
1292 {"popup_dialog", 2, 2, FEARG_1, NULL,
1293 ret_number, PROP_FUNC(f_popup_dialog)},
1294 {"popup_filter_menu", 2, 2, 0, NULL,
1295 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1296 {"popup_filter_yesno", 2, 2, 0, NULL,
1297 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1298 {"popup_findinfo", 0, 0, 0, NULL,
1299 ret_number, PROP_FUNC(f_popup_findinfo)},
1300 {"popup_findpreview", 0, 0, 0, NULL,
1301 ret_number, PROP_FUNC(f_popup_findpreview)},
1302 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1303 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1304 {"popup_getpos", 1, 1, FEARG_1, NULL,
1305 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1306 {"popup_hide", 1, 1, FEARG_1, NULL,
1307 ret_void, PROP_FUNC(f_popup_hide)},
1308 {"popup_list", 0, 0, 0, NULL,
1309 ret_list_number, PROP_FUNC(f_popup_list)},
1310 {"popup_locate", 2, 2, 0, NULL,
1311 ret_number, PROP_FUNC(f_popup_locate)},
1312 {"popup_menu", 2, 2, FEARG_1, NULL,
1313 ret_number, PROP_FUNC(f_popup_menu)},
1314 {"popup_move", 2, 2, FEARG_1, NULL,
1315 ret_void, PROP_FUNC(f_popup_move)},
1316 {"popup_notification", 2, 2, FEARG_1, NULL,
1317 ret_number, PROP_FUNC(f_popup_notification)},
1318 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1319 ret_void, PROP_FUNC(f_popup_setoptions)},
1320 {"popup_settext", 2, 2, FEARG_1, NULL,
1321 ret_void, PROP_FUNC(f_popup_settext)},
1322 {"popup_show", 1, 1, FEARG_1, NULL,
1323 ret_void, PROP_FUNC(f_popup_show)},
1324 {"pow", 2, 2, FEARG_1, NULL,
1325 ret_float, FLOAT_FUNC(f_pow)},
1326 {"prevnonblank", 1, 1, FEARG_1, NULL,
1327 ret_number, f_prevnonblank},
1328 {"printf", 1, 19, FEARG_2, NULL,
1329 ret_string, f_printf},
1330 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1331 ret_string, JOB_FUNC(f_prompt_getprompt)},
1332 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1333 ret_void, JOB_FUNC(f_prompt_setcallback)},
1334 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1335 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1336 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1337 ret_void, JOB_FUNC(f_prompt_setprompt)},
1338 {"prop_add", 3, 3, FEARG_1, NULL,
1339 ret_void, PROP_FUNC(f_prop_add)},
1340 {"prop_clear", 1, 3, FEARG_1, NULL,
1341 ret_void, PROP_FUNC(f_prop_clear)},
1342 {"prop_find", 1, 2, FEARG_1, NULL,
1343 ret_dict_any, PROP_FUNC(f_prop_find)},
1344 {"prop_list", 1, 2, FEARG_1, NULL,
1345 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1346 {"prop_remove", 1, 3, FEARG_1, NULL,
1347 ret_number, PROP_FUNC(f_prop_remove)},
1348 {"prop_type_add", 2, 2, FEARG_1, NULL,
1349 ret_void, PROP_FUNC(f_prop_type_add)},
1350 {"prop_type_change", 2, 2, FEARG_1, NULL,
1351 ret_void, PROP_FUNC(f_prop_type_change)},
1352 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1353 ret_void, PROP_FUNC(f_prop_type_delete)},
1354 {"prop_type_get", 1, 2, FEARG_1, NULL,
1355 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1356 {"prop_type_list", 0, 1, FEARG_1, NULL,
1357 ret_list_string, PROP_FUNC(f_prop_type_list)},
1358 {"pum_getpos", 0, 0, 0, NULL,
1359 ret_dict_number, f_pum_getpos},
1360 {"pumvisible", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001361 ret_number_bool, f_pumvisible},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001362 {"py3eval", 1, 1, FEARG_1, NULL,
1363 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001364#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001365 f_py3eval
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 {"pyeval", 1, 1, FEARG_1, NULL,
1371 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001372#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001373 f_pyeval
1374#else
1375 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001376#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001377 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001378 {"pyxeval", 1, 1, FEARG_1, NULL,
1379 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001380#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001381 f_pyxeval
1382#else
1383 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001384#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001385 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001386 {"rand", 0, 1, FEARG_1, NULL,
1387 ret_number, f_rand},
1388 {"range", 1, 3, FEARG_1, NULL,
1389 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001390 {"readblob", 1, 1, FEARG_1, NULL,
1391 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001392 {"readdir", 1, 3, FEARG_1, NULL,
1393 ret_list_string, f_readdir},
1394 {"readdirex", 1, 3, FEARG_1, NULL,
1395 ret_list_dict_any, f_readdirex},
1396 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001397 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001398 {"reduce", 2, 3, FEARG_1, NULL,
1399 ret_any, f_reduce},
1400 {"reg_executing", 0, 0, 0, NULL,
1401 ret_string, f_reg_executing},
1402 {"reg_recording", 0, 0, 0, NULL,
1403 ret_string, f_reg_recording},
1404 {"reltime", 0, 2, FEARG_1, NULL,
1405 ret_list_any, f_reltime},
1406 {"reltimefloat", 1, 1, FEARG_1, NULL,
1407 ret_float, FLOAT_FUNC(f_reltimefloat)},
1408 {"reltimestr", 1, 1, FEARG_1, NULL,
1409 ret_string, f_reltimestr},
1410 {"remote_expr", 2, 4, FEARG_1, NULL,
1411 ret_string, f_remote_expr},
1412 {"remote_foreground", 1, 1, FEARG_1, NULL,
1413 ret_string, f_remote_foreground},
1414 {"remote_peek", 1, 2, FEARG_1, NULL,
1415 ret_number, f_remote_peek},
1416 {"remote_read", 1, 2, FEARG_1, NULL,
1417 ret_string, f_remote_read},
1418 {"remote_send", 2, 3, FEARG_1, NULL,
1419 ret_string, f_remote_send},
1420 {"remote_startserver", 1, 1, FEARG_1, NULL,
1421 ret_void, f_remote_startserver},
1422 {"remove", 2, 3, FEARG_1, NULL,
1423 ret_remove, f_remove},
1424 {"rename", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001425 ret_number_bool, f_rename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001426 {"repeat", 2, 2, FEARG_1, NULL,
1427 ret_first_arg, f_repeat},
1428 {"resolve", 1, 1, FEARG_1, NULL,
1429 ret_string, f_resolve},
1430 {"reverse", 1, 1, FEARG_1, NULL,
1431 ret_first_arg, f_reverse},
1432 {"round", 1, 1, FEARG_1, NULL,
1433 ret_float, FLOAT_FUNC(f_round)},
1434 {"rubyeval", 1, 1, FEARG_1, NULL,
1435 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001436#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001437 f_rubyeval
1438#else
1439 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001440#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001441 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001442 {"screenattr", 2, 2, FEARG_1, NULL,
1443 ret_number, f_screenattr},
1444 {"screenchar", 2, 2, FEARG_1, NULL,
1445 ret_number, f_screenchar},
1446 {"screenchars", 2, 2, FEARG_1, NULL,
1447 ret_list_number, f_screenchars},
1448 {"screencol", 0, 0, 0, NULL,
1449 ret_number, f_screencol},
1450 {"screenpos", 3, 3, FEARG_1, NULL,
1451 ret_dict_number, f_screenpos},
1452 {"screenrow", 0, 0, 0, NULL,
1453 ret_number, f_screenrow},
1454 {"screenstring", 2, 2, FEARG_1, NULL,
1455 ret_string, f_screenstring},
1456 {"search", 1, 5, FEARG_1, NULL,
1457 ret_number, f_search},
1458 {"searchcount", 0, 1, FEARG_1, NULL,
1459 ret_dict_any, f_searchcount},
1460 {"searchdecl", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001461 ret_number_bool, f_searchdecl},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001462 {"searchpair", 3, 7, 0, NULL,
1463 ret_number, f_searchpair},
1464 {"searchpairpos", 3, 7, 0, NULL,
1465 ret_list_number, f_searchpairpos},
1466 {"searchpos", 1, 5, FEARG_1, NULL,
1467 ret_list_number, f_searchpos},
1468 {"server2client", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001469 ret_number_bool, f_server2client},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001470 {"serverlist", 0, 0, 0, NULL,
1471 ret_string, f_serverlist},
1472 {"setbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001473 ret_number_bool, f_setbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001474 {"setbufvar", 3, 3, FEARG_3, NULL,
1475 ret_void, f_setbufvar},
1476 {"setcellwidths", 1, 1, FEARG_1, NULL,
1477 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001478 {"setcharpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001479 ret_number_bool, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001480 {"setcharsearch", 1, 1, FEARG_1, NULL,
1481 ret_void, f_setcharsearch},
1482 {"setcmdpos", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001483 ret_number_bool, f_setcmdpos},
1484 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1485 ret_number_bool, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001486 {"setenv", 2, 2, FEARG_2, NULL,
1487 ret_void, f_setenv},
1488 {"setfperm", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001489 ret_number_bool, f_setfperm},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001490 {"setline", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001491 ret_number_bool, f_setline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001492 {"setloclist", 2, 4, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001493 ret_number_bool, f_setloclist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001494 {"setmatches", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001495 ret_number_bool, f_setmatches},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001496 {"setpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001497 ret_number_bool, f_setpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001498 {"setqflist", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001499 ret_number_bool, f_setqflist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001500 {"setreg", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001501 ret_number_bool, f_setreg},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001502 {"settabvar", 3, 3, FEARG_3, NULL,
1503 ret_void, f_settabvar},
1504 {"settabwinvar", 4, 4, FEARG_4, NULL,
1505 ret_void, f_settabwinvar},
1506 {"settagstack", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001507 ret_number_bool, f_settagstack},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001508 {"setwinvar", 3, 3, FEARG_3, NULL,
1509 ret_void, f_setwinvar},
1510 {"sha256", 1, 1, FEARG_1, NULL,
1511 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001512#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001513 f_sha256
1514#else
1515 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001516#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001517 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001518 {"shellescape", 1, 2, FEARG_1, NULL,
1519 ret_string, f_shellescape},
1520 {"shiftwidth", 0, 1, FEARG_1, NULL,
1521 ret_number, f_shiftwidth},
1522 {"sign_define", 1, 2, FEARG_1, NULL,
1523 ret_any, SIGN_FUNC(f_sign_define)},
1524 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1525 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1526 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1527 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1528 {"sign_jump", 3, 3, FEARG_1, NULL,
1529 ret_number, SIGN_FUNC(f_sign_jump)},
1530 {"sign_place", 4, 5, FEARG_1, NULL,
1531 ret_number, SIGN_FUNC(f_sign_place)},
1532 {"sign_placelist", 1, 1, FEARG_1, NULL,
1533 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1534 {"sign_undefine", 0, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001535 ret_number_bool, SIGN_FUNC(f_sign_undefine)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001536 {"sign_unplace", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001537 ret_number_bool, SIGN_FUNC(f_sign_unplace)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001538 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1539 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1540 {"simplify", 1, 1, FEARG_1, NULL,
1541 ret_string, f_simplify},
1542 {"sin", 1, 1, FEARG_1, NULL,
1543 ret_float, FLOAT_FUNC(f_sin)},
1544 {"sinh", 1, 1, FEARG_1, NULL,
1545 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001546 {"slice", 2, 3, FEARG_1, NULL,
1547 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001548 {"sort", 1, 3, FEARG_1, NULL,
1549 ret_first_arg, f_sort},
1550 {"sound_clear", 0, 0, 0, NULL,
1551 ret_void, SOUND_FUNC(f_sound_clear)},
1552 {"sound_playevent", 1, 2, FEARG_1, NULL,
1553 ret_number, SOUND_FUNC(f_sound_playevent)},
1554 {"sound_playfile", 1, 2, FEARG_1, NULL,
1555 ret_number, SOUND_FUNC(f_sound_playfile)},
1556 {"sound_stop", 1, 1, FEARG_1, NULL,
1557 ret_void, SOUND_FUNC(f_sound_stop)},
1558 {"soundfold", 1, 1, FEARG_1, NULL,
1559 ret_string, f_soundfold},
1560 {"spellbadword", 0, 1, FEARG_1, NULL,
1561 ret_list_string, f_spellbadword},
1562 {"spellsuggest", 1, 3, FEARG_1, NULL,
1563 ret_list_string, f_spellsuggest},
1564 {"split", 1, 3, FEARG_1, NULL,
1565 ret_list_string, f_split},
1566 {"sqrt", 1, 1, FEARG_1, NULL,
1567 ret_float, FLOAT_FUNC(f_sqrt)},
1568 {"srand", 0, 1, FEARG_1, NULL,
1569 ret_list_number, f_srand},
1570 {"state", 0, 1, FEARG_1, NULL,
1571 ret_string, f_state},
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001572 {"str2float", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001573 ret_float, FLOAT_FUNC(f_str2float)},
1574 {"str2list", 1, 2, FEARG_1, NULL,
1575 ret_list_number, f_str2list},
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001576 {"str2nr", 1, 3, FEARG_1, arg3_string_nr_bool,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001577 ret_number, f_str2nr},
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01001578 {"strcharlen", 1, 1, FEARG_1, NULL,
1579 ret_number, f_strcharlen},
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01001580 {"strcharpart", 2, 4, FEARG_1, NULL,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001581 ret_string, f_strcharpart},
1582 {"strchars", 1, 2, FEARG_1, NULL,
1583 ret_number, f_strchars},
1584 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1585 ret_number, f_strdisplaywidth},
1586 {"strftime", 1, 2, FEARG_1, NULL,
1587 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001588#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001589 f_strftime
1590#else
1591 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001592#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001593 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001594 {"strgetchar", 2, 2, FEARG_1, NULL,
1595 ret_number, f_strgetchar},
1596 {"stridx", 2, 3, FEARG_1, NULL,
1597 ret_number, f_stridx},
1598 {"string", 1, 1, FEARG_1, NULL,
1599 ret_string, f_string},
1600 {"strlen", 1, 1, FEARG_1, NULL,
1601 ret_number, f_strlen},
1602 {"strpart", 2, 4, FEARG_1, NULL,
1603 ret_string, f_strpart},
1604 {"strptime", 2, 2, FEARG_1, NULL,
1605 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001606#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001607 f_strptime
1608#else
1609 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001610#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001611 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001612 {"strridx", 2, 3, FEARG_1, NULL,
1613 ret_number, f_strridx},
1614 {"strtrans", 1, 1, FEARG_1, NULL,
1615 ret_string, f_strtrans},
1616 {"strwidth", 1, 1, FEARG_1, NULL,
1617 ret_number, f_strwidth},
1618 {"submatch", 1, 2, FEARG_1, NULL,
1619 ret_string, f_submatch},
1620 {"substitute", 4, 4, FEARG_1, NULL,
1621 ret_string, f_substitute},
1622 {"swapinfo", 1, 1, FEARG_1, NULL,
1623 ret_dict_any, f_swapinfo},
1624 {"swapname", 1, 1, FEARG_1, NULL,
1625 ret_string, f_swapname},
1626 {"synID", 3, 3, 0, NULL,
1627 ret_number, f_synID},
1628 {"synIDattr", 2, 3, FEARG_1, NULL,
1629 ret_string, f_synIDattr},
1630 {"synIDtrans", 1, 1, FEARG_1, NULL,
1631 ret_number, f_synIDtrans},
1632 {"synconcealed", 2, 2, 0, NULL,
1633 ret_list_any, f_synconcealed},
1634 {"synstack", 2, 2, 0, NULL,
1635 ret_list_number, f_synstack},
1636 {"system", 1, 2, FEARG_1, NULL,
1637 ret_string, f_system},
1638 {"systemlist", 1, 2, FEARG_1, NULL,
1639 ret_list_string, f_systemlist},
1640 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1641 ret_list_number, f_tabpagebuflist},
1642 {"tabpagenr", 0, 1, 0, NULL,
1643 ret_number, f_tabpagenr},
1644 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1645 ret_number, f_tabpagewinnr},
1646 {"tagfiles", 0, 0, 0, NULL,
1647 ret_list_string, f_tagfiles},
1648 {"taglist", 1, 2, FEARG_1, NULL,
1649 ret_list_dict_any, f_taglist},
1650 {"tan", 1, 1, FEARG_1, NULL,
1651 ret_float, FLOAT_FUNC(f_tan)},
1652 {"tanh", 1, 1, FEARG_1, NULL,
1653 ret_float, FLOAT_FUNC(f_tanh)},
1654 {"tempname", 0, 0, 0, NULL,
1655 ret_string, f_tempname},
1656 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1657 ret_number, TERM_FUNC(f_term_dumpdiff)},
1658 {"term_dumpload", 1, 2, FEARG_1, NULL,
1659 ret_number, TERM_FUNC(f_term_dumpload)},
1660 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1661 ret_void, TERM_FUNC(f_term_dumpwrite)},
1662 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1663 ret_number, TERM_FUNC(f_term_getaltscreen)},
1664 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1665 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001666#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001667 f_term_getansicolors
1668#else
1669 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001670#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001671 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001672 {"term_getattr", 2, 2, FEARG_1, NULL,
1673 ret_number, TERM_FUNC(f_term_getattr)},
1674 {"term_getcursor", 1, 1, FEARG_1, NULL,
1675 ret_list_any, TERM_FUNC(f_term_getcursor)},
1676 {"term_getjob", 1, 1, FEARG_1, NULL,
1677 ret_job, TERM_FUNC(f_term_getjob)},
1678 {"term_getline", 2, 2, FEARG_1, NULL,
1679 ret_string, TERM_FUNC(f_term_getline)},
1680 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1681 ret_number, TERM_FUNC(f_term_getscrolled)},
1682 {"term_getsize", 1, 1, FEARG_1, NULL,
1683 ret_list_number, TERM_FUNC(f_term_getsize)},
1684 {"term_getstatus", 1, 1, FEARG_1, NULL,
1685 ret_string, TERM_FUNC(f_term_getstatus)},
1686 {"term_gettitle", 1, 1, FEARG_1, NULL,
1687 ret_string, TERM_FUNC(f_term_gettitle)},
1688 {"term_gettty", 1, 2, FEARG_1, NULL,
1689 ret_string, TERM_FUNC(f_term_gettty)},
1690 {"term_list", 0, 0, 0, NULL,
1691 ret_list_number, TERM_FUNC(f_term_list)},
1692 {"term_scrape", 2, 2, FEARG_1, NULL,
1693 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1694 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1695 ret_void, TERM_FUNC(f_term_sendkeys)},
1696 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1697 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001698#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001699 f_term_setansicolors
1700#else
1701 NULL
1702#endif
1703 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001704 {"term_setapi", 2, 2, FEARG_1, NULL,
1705 ret_void, TERM_FUNC(f_term_setapi)},
1706 {"term_setkill", 2, 2, FEARG_1, NULL,
1707 ret_void, TERM_FUNC(f_term_setkill)},
1708 {"term_setrestore", 2, 2, FEARG_1, NULL,
1709 ret_void, TERM_FUNC(f_term_setrestore)},
1710 {"term_setsize", 3, 3, FEARG_1, NULL,
1711 ret_void, TERM_FUNC(f_term_setsize)},
1712 {"term_start", 1, 2, FEARG_1, NULL,
1713 ret_number, TERM_FUNC(f_term_start)},
1714 {"term_wait", 1, 2, FEARG_1, NULL,
1715 ret_void, TERM_FUNC(f_term_wait)},
1716 {"terminalprops", 0, 0, 0, NULL,
1717 ret_dict_string, f_terminalprops},
1718 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1719 ret_void, f_test_alloc_fail},
1720 {"test_autochdir", 0, 0, 0, NULL,
1721 ret_void, f_test_autochdir},
1722 {"test_feedinput", 1, 1, FEARG_1, NULL,
1723 ret_void, f_test_feedinput},
1724 {"test_garbagecollect_now", 0, 0, 0, NULL,
1725 ret_void, f_test_garbagecollect_now},
1726 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1727 ret_void, f_test_garbagecollect_soon},
1728 {"test_getvalue", 1, 1, FEARG_1, NULL,
1729 ret_number, f_test_getvalue},
1730 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1731 ret_void, f_test_ignore_error},
1732 {"test_null_blob", 0, 0, 0, NULL,
1733 ret_blob, f_test_null_blob},
1734 {"test_null_channel", 0, 0, 0, NULL,
1735 ret_channel, JOB_FUNC(f_test_null_channel)},
1736 {"test_null_dict", 0, 0, 0, NULL,
1737 ret_dict_any, f_test_null_dict},
1738 {"test_null_function", 0, 0, 0, NULL,
1739 ret_func_any, f_test_null_function},
1740 {"test_null_job", 0, 0, 0, NULL,
1741 ret_job, JOB_FUNC(f_test_null_job)},
1742 {"test_null_list", 0, 0, 0, NULL,
1743 ret_list_any, f_test_null_list},
1744 {"test_null_partial", 0, 0, 0, NULL,
1745 ret_func_any, f_test_null_partial},
1746 {"test_null_string", 0, 0, 0, NULL,
1747 ret_string, f_test_null_string},
1748 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1749 ret_void, f_test_option_not_set},
1750 {"test_override", 2, 2, FEARG_2, NULL,
1751 ret_void, f_test_override},
1752 {"test_refcount", 1, 1, FEARG_1, NULL,
1753 ret_number, f_test_refcount},
1754 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1755 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001756#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001757 f_test_scrollbar
1758#else
1759 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001760#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001761 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001762 {"test_setmouse", 2, 2, 0, NULL,
1763 ret_void, f_test_setmouse},
1764 {"test_settime", 1, 1, FEARG_1, NULL,
1765 ret_void, f_test_settime},
1766 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1767 ret_void, f_test_srand_seed},
1768 {"test_unknown", 0, 0, 0, NULL,
1769 ret_any, f_test_unknown},
1770 {"test_void", 0, 0, 0, NULL,
1771 ret_void, f_test_void},
1772 {"timer_info", 0, 1, FEARG_1, NULL,
1773 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1774 {"timer_pause", 2, 2, FEARG_1, NULL,
1775 ret_void, TIMER_FUNC(f_timer_pause)},
1776 {"timer_start", 2, 3, FEARG_1, NULL,
1777 ret_number, TIMER_FUNC(f_timer_start)},
1778 {"timer_stop", 1, 1, FEARG_1, NULL,
1779 ret_void, TIMER_FUNC(f_timer_stop)},
1780 {"timer_stopall", 0, 0, 0, NULL,
1781 ret_void, TIMER_FUNC(f_timer_stopall)},
1782 {"tolower", 1, 1, FEARG_1, NULL,
1783 ret_string, f_tolower},
1784 {"toupper", 1, 1, FEARG_1, NULL,
1785 ret_string, f_toupper},
1786 {"tr", 3, 3, FEARG_1, NULL,
1787 ret_string, f_tr},
1788 {"trim", 1, 3, FEARG_1, NULL,
1789 ret_string, f_trim},
1790 {"trunc", 1, 1, FEARG_1, NULL,
1791 ret_float, FLOAT_FUNC(f_trunc)},
1792 {"type", 1, 1, FEARG_1, NULL,
1793 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001794 {"typename", 1, 1, FEARG_1, NULL,
1795 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001796 {"undofile", 1, 1, FEARG_1, NULL,
1797 ret_string, f_undofile},
1798 {"undotree", 0, 0, 0, NULL,
1799 ret_dict_any, f_undotree},
1800 {"uniq", 1, 3, FEARG_1, NULL,
1801 ret_list_any, f_uniq},
1802 {"values", 1, 1, FEARG_1, NULL,
1803 ret_list_any, f_values},
1804 {"virtcol", 1, 1, FEARG_1, NULL,
1805 ret_number, f_virtcol},
1806 {"visualmode", 0, 1, 0, NULL,
1807 ret_string, f_visualmode},
1808 {"wildmenumode", 0, 0, 0, NULL,
1809 ret_number, f_wildmenumode},
1810 {"win_execute", 2, 3, FEARG_2, NULL,
1811 ret_string, f_win_execute},
1812 {"win_findbuf", 1, 1, FEARG_1, NULL,
1813 ret_list_number, f_win_findbuf},
1814 {"win_getid", 0, 2, FEARG_1, NULL,
1815 ret_number, f_win_getid},
1816 {"win_gettype", 0, 1, FEARG_1, NULL,
1817 ret_string, f_win_gettype},
1818 {"win_gotoid", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001819 ret_number_bool, f_win_gotoid},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001820 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1821 ret_list_number, f_win_id2tabwin},
1822 {"win_id2win", 1, 1, FEARG_1, NULL,
1823 ret_number, f_win_id2win},
1824 {"win_screenpos", 1, 1, FEARG_1, NULL,
1825 ret_list_number, f_win_screenpos},
1826 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001827 ret_number_bool, f_win_splitmove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001828 {"winbufnr", 1, 1, FEARG_1, NULL,
1829 ret_number, f_winbufnr},
1830 {"wincol", 0, 0, 0, NULL,
1831 ret_number, f_wincol},
1832 {"windowsversion", 0, 0, 0, NULL,
1833 ret_string, f_windowsversion},
1834 {"winheight", 1, 1, FEARG_1, NULL,
1835 ret_number, f_winheight},
1836 {"winlayout", 0, 1, FEARG_1, NULL,
1837 ret_list_any, f_winlayout},
1838 {"winline", 0, 0, 0, NULL,
1839 ret_number, f_winline},
1840 {"winnr", 0, 1, FEARG_1, NULL,
1841 ret_number, f_winnr},
1842 {"winrestcmd", 0, 0, 0, NULL,
1843 ret_string, f_winrestcmd},
1844 {"winrestview", 1, 1, FEARG_1, NULL,
1845 ret_void, f_winrestview},
1846 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001847 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001848 {"winwidth", 1, 1, FEARG_1, NULL,
1849 ret_number, f_winwidth},
1850 {"wordcount", 0, 0, 0, NULL,
1851 ret_dict_number, f_wordcount},
1852 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001853 ret_number_bool, f_writefile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001854 {"xor", 2, 2, FEARG_1, NULL,
1855 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001856};
1857
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001858/*
1859 * Function given to ExpandGeneric() to obtain the list of internal
1860 * or user defined function names.
1861 */
1862 char_u *
1863get_function_name(expand_T *xp, int idx)
1864{
1865 static int intidx = -1;
1866 char_u *name;
1867
1868 if (idx == 0)
1869 intidx = -1;
1870 if (intidx < 0)
1871 {
1872 name = get_user_func_name(xp, idx);
1873 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001874 {
1875 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1876 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001877 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001878 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001879 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001880 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001881 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001882 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001883 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001884 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001885 STRCAT(IObuff, ")");
1886 return IObuff;
1887 }
1888
1889 return NULL;
1890}
1891
1892/*
1893 * Function given to ExpandGeneric() to obtain the list of internal or
1894 * user defined variable or function names.
1895 */
1896 char_u *
1897get_expr_name(expand_T *xp, int idx)
1898{
1899 static int intidx = -1;
1900 char_u *name;
1901
1902 if (idx == 0)
1903 intidx = -1;
1904 if (intidx < 0)
1905 {
1906 name = get_function_name(xp, idx);
1907 if (name != NULL)
1908 return name;
1909 }
1910 return get_user_var_name(xp, ++intidx);
1911}
1912
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001913/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001914 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001915 * Return index, or -1 if not found or "implemented" is TRUE and the function
1916 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001917 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001918 static int
1919find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001920{
1921 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001922 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001923 int cmp;
1924 int x;
1925
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001926 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001927
1928 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001929 while (first <= last)
1930 {
1931 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001932 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001933 if (cmp < 0)
1934 last = x - 1;
1935 else if (cmp > 0)
1936 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001937 else if (implemented && global_functions[x].f_func == NULL)
1938 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001939 else
1940 return x;
1941 }
1942 return -1;
1943}
1944
Bram Moolenaar15c47602020-03-26 22:16:48 +01001945/*
1946 * Find internal function "name" in table "global_functions".
1947 * Return index, or -1 if not found or the function is not implemented.
1948 */
1949 int
1950find_internal_func(char_u *name)
1951{
1952 return find_internal_func_opt(name, TRUE);
1953}
1954
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001955 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001956has_internal_func(char_u *name)
1957{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001958 return find_internal_func_opt(name, TRUE) >= 0;
1959}
1960
1961 static int
1962has_internal_func_name(char_u *name)
1963{
1964 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001965}
1966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 char *
1968internal_func_name(int idx)
1969{
1970 return global_functions[idx].f_name;
1971}
1972
Bram Moolenaar94738d82020-10-21 14:25:07 +02001973/*
1974 * Check the argument types for builting function "idx".
1975 * Uses the list of types on the type stack: "types".
1976 * Return FAIL and gives an error message when a type is wrong.
1977 */
1978 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001979internal_func_check_arg_types(
1980 type_T **types,
1981 int idx,
1982 int argcount,
1983 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001984{
1985 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1986 int i;
1987
1988 if (argchecks != NULL)
1989 {
1990 argcontext_T context;
1991
1992 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001993 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001994 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001995 for (i = 0; i < argcount; ++i)
1996 if (argchecks[i] != NULL)
1997 {
1998 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001999 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02002000 return FAIL;
2001 }
2002 }
2003 return OK;
2004}
2005
Bram Moolenaara1224cb2020-10-22 12:31:49 +02002006/*
2007 * Call the "f_retfunc" function to obtain the return type of function "idx".
2008 * "argtypes" is the list of argument types or NULL when there are no
2009 * arguments.
2010 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002012internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002014 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015}
2016
2017/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002018 * Return TRUE if "idx" is for the map() function.
2019 */
2020 int
2021internal_func_is_map(int idx)
2022{
2023 return global_functions[idx].f_func == f_map;
2024}
2025
2026/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002028 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2029 * first argument, 2 if method base is second argument, etc. 9 if method base
2030 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 */
2032 int
2033check_internal_func(int idx, int argcount)
2034{
2035 int res;
2036 char *name;
2037
2038 if (argcount < global_functions[idx].f_min_argc)
2039 res = FCERR_TOOFEW;
2040 else if (argcount > global_functions[idx].f_max_argc)
2041 res = FCERR_TOOMANY;
2042 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002043 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044
2045 name = internal_func_name(idx);
2046 if (res == FCERR_TOOMANY)
2047 semsg(_(e_toomanyarg), name);
2048 else
2049 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002050 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051}
2052
Bram Moolenaarac92e252019-08-03 21:58:38 +02002053 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002054call_internal_func(
2055 char_u *name,
2056 int argcount,
2057 typval_T *argvars,
2058 typval_T *rettv)
2059{
2060 int i;
2061
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002062 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002063 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002064 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002065 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002066 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002067 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002068 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002069 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002070 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002071 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002072}
2073
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074 void
2075call_internal_func_by_idx(
2076 int idx,
2077 typval_T *argvars,
2078 typval_T *rettv)
2079{
2080 global_functions[idx].f_func(argvars, rettv);
2081}
2082
Bram Moolenaarac92e252019-08-03 21:58:38 +02002083/*
2084 * Invoke a method for base->method().
2085 */
2086 int
2087call_internal_method(
2088 char_u *name,
2089 int argcount,
2090 typval_T *argvars,
2091 typval_T *rettv,
2092 typval_T *basetv)
2093{
2094 int i;
2095 int fi;
2096 typval_T argv[MAX_FUNC_ARGS + 1];
2097
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002098 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002099 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002100 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002101 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002102 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002103 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002104 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002105 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002106 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002107
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002108 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002109 {
2110 // base value goes last
2111 for (i = 0; i < argcount; ++i)
2112 argv[i] = argvars[i];
2113 argv[argcount] = *basetv;
2114 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002115 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002116 {
2117 // base value goes second
2118 argv[0] = argvars[0];
2119 argv[1] = *basetv;
2120 for (i = 1; i < argcount; ++i)
2121 argv[i + 1] = argvars[i];
2122 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002123 else if (global_functions[fi].f_argtype == FEARG_3)
2124 {
2125 // base value goes third
2126 argv[0] = argvars[0];
2127 argv[1] = argvars[1];
2128 argv[2] = *basetv;
2129 for (i = 2; i < argcount; ++i)
2130 argv[i + 1] = argvars[i];
2131 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002132 else if (global_functions[fi].f_argtype == FEARG_4)
2133 {
2134 // base value goes fourth
2135 argv[0] = argvars[0];
2136 argv[1] = argvars[1];
2137 argv[2] = argvars[2];
2138 argv[3] = *basetv;
2139 for (i = 3; i < argcount; ++i)
2140 argv[i + 1] = argvars[i];
2141 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002142 else
2143 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002144 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002145 argv[0] = *basetv;
2146 for (i = 0; i < argcount; ++i)
2147 argv[i + 1] = argvars[i];
2148 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002149 argv[argcount + 1].v_type = VAR_UNKNOWN;
2150
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002151 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002152 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002153}
2154
2155/*
2156 * Return TRUE for a non-zero Number and a non-empty String.
2157 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002158 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002159non_zero_arg(typval_T *argvars)
2160{
2161 return ((argvars[0].v_type == VAR_NUMBER
2162 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002163 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002164 && argvars[0].vval.v_number == VVAL_TRUE)
2165 || (argvars[0].v_type == VAR_STRING
2166 && argvars[0].vval.v_string != NULL
2167 && *argvars[0].vval.v_string != NUL));
2168}
2169
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002170#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002171/*
2172 * Get the float value of "argvars[0]" into "f".
2173 * Returns FAIL when the argument is not a Number or Float.
2174 */
2175 static int
2176get_float_arg(typval_T *argvars, float_T *f)
2177{
2178 if (argvars[0].v_type == VAR_FLOAT)
2179 {
2180 *f = argvars[0].vval.v_float;
2181 return OK;
2182 }
2183 if (argvars[0].v_type == VAR_NUMBER)
2184 {
2185 *f = (float_T)argvars[0].vval.v_number;
2186 return OK;
2187 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002188 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002189 return FAIL;
2190}
2191
2192/*
2193 * "abs(expr)" function
2194 */
2195 static void
2196f_abs(typval_T *argvars, typval_T *rettv)
2197{
2198 if (argvars[0].v_type == VAR_FLOAT)
2199 {
2200 rettv->v_type = VAR_FLOAT;
2201 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
2202 }
2203 else
2204 {
2205 varnumber_T n;
2206 int error = FALSE;
2207
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002208 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002209 if (error)
2210 rettv->vval.v_number = -1;
2211 else if (n > 0)
2212 rettv->vval.v_number = n;
2213 else
2214 rettv->vval.v_number = -n;
2215 }
2216}
2217
2218/*
2219 * "acos()" function
2220 */
2221 static void
2222f_acos(typval_T *argvars, typval_T *rettv)
2223{
2224 float_T f = 0.0;
2225
2226 rettv->v_type = VAR_FLOAT;
2227 if (get_float_arg(argvars, &f) == OK)
2228 rettv->vval.v_float = acos(f);
2229 else
2230 rettv->vval.v_float = 0.0;
2231}
2232#endif
2233
2234/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002235 * "and(expr, expr)" function
2236 */
2237 static void
2238f_and(typval_T *argvars, typval_T *rettv)
2239{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002240 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2241 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002242}
2243
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002244#ifdef FEAT_FLOAT
2245/*
2246 * "asin()" function
2247 */
2248 static void
2249f_asin(typval_T *argvars, typval_T *rettv)
2250{
2251 float_T f = 0.0;
2252
2253 rettv->v_type = VAR_FLOAT;
2254 if (get_float_arg(argvars, &f) == OK)
2255 rettv->vval.v_float = asin(f);
2256 else
2257 rettv->vval.v_float = 0.0;
2258}
2259
2260/*
2261 * "atan()" function
2262 */
2263 static void
2264f_atan(typval_T *argvars, typval_T *rettv)
2265{
2266 float_T f = 0.0;
2267
2268 rettv->v_type = VAR_FLOAT;
2269 if (get_float_arg(argvars, &f) == OK)
2270 rettv->vval.v_float = atan(f);
2271 else
2272 rettv->vval.v_float = 0.0;
2273}
2274
2275/*
2276 * "atan2()" function
2277 */
2278 static void
2279f_atan2(typval_T *argvars, typval_T *rettv)
2280{
2281 float_T fx = 0.0, fy = 0.0;
2282
2283 rettv->v_type = VAR_FLOAT;
2284 if (get_float_arg(argvars, &fx) == OK
2285 && get_float_arg(&argvars[1], &fy) == OK)
2286 rettv->vval.v_float = atan2(fx, fy);
2287 else
2288 rettv->vval.v_float = 0.0;
2289}
2290#endif
2291
2292/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002293 * "balloon_show()" function
2294 */
2295#ifdef FEAT_BEVAL
2296 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002297f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2298{
2299 rettv->v_type = VAR_STRING;
2300 if (balloonEval != NULL)
2301 {
2302 if (balloonEval->msg == NULL)
2303 rettv->vval.v_string = NULL;
2304 else
2305 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2306 }
2307}
2308
2309 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002310f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2311{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002312 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002313 {
2314 if (argvars[0].v_type == VAR_LIST
2315# ifdef FEAT_GUI
2316 && !gui.in_use
2317# endif
2318 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002319 {
2320 list_T *l = argvars[0].vval.v_list;
2321
2322 // empty list removes the balloon
2323 post_balloon(balloonEval, NULL,
2324 l == NULL || l->lv_len == 0 ? NULL : l);
2325 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002326 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002327 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002328 char_u *mesg;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002329
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002330 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2331 return;
2332
2333 mesg = tv_get_string_chk(&argvars[0]);
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002334 if (mesg != NULL)
2335 // empty string removes the balloon
2336 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2337 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002338 }
2339}
2340
Bram Moolenaar669a8282017-11-19 20:13:05 +01002341# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002342 static void
2343f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2344{
2345 if (rettv_list_alloc(rettv) == OK)
2346 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002347 char_u *msg;
Bram Moolenaar246fe032017-11-19 19:56:27 +01002348
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002349 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2350 return;
2351 msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002352 if (msg != NULL)
2353 {
2354 pumitem_T *array;
2355 int size = split_message(msg, &array);
2356 int i;
2357
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002358 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002359 for (i = 1; i < size - 1; ++i)
2360 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002361 while (size > 0)
2362 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002363 vim_free(array);
2364 }
2365 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002366}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002367# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002368#endif
2369
2370/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002371 * Get the buffer from "arg" and give an error and return NULL if it is not
2372 * valid.
2373 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002374 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002375get_buf_arg(typval_T *arg)
2376{
2377 buf_T *buf;
2378
2379 ++emsg_off;
2380 buf = tv_get_buf(arg, FALSE);
2381 --emsg_off;
2382 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002383 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002384 return buf;
2385}
2386
2387/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002388 * "byte2line(byte)" function
2389 */
2390 static void
2391f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2392{
2393#ifndef FEAT_BYTEOFF
2394 rettv->vval.v_number = -1;
2395#else
2396 long boff = 0;
2397
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002398 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002399 if (boff < 0)
2400 rettv->vval.v_number = -1;
2401 else
2402 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2403 (linenr_T)0, &boff);
2404#endif
2405}
2406
2407 static void
2408byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2409{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002410 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002411 char_u *str;
2412 varnumber_T idx;
2413
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002414 str = tv_get_string_chk(&argvars[0]);
2415 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002416 rettv->vval.v_number = -1;
2417 if (str == NULL || idx < 0)
2418 return;
2419
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002420 t = str;
2421 for ( ; idx > 0; idx--)
2422 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002423 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002424 return;
2425 if (enc_utf8 && comp)
2426 t += utf_ptr2len(t);
2427 else
2428 t += (*mb_ptr2len)(t);
2429 }
2430 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002431}
2432
2433/*
2434 * "byteidx()" function
2435 */
2436 static void
2437f_byteidx(typval_T *argvars, typval_T *rettv)
2438{
2439 byteidx(argvars, rettv, FALSE);
2440}
2441
2442/*
2443 * "byteidxcomp()" function
2444 */
2445 static void
2446f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2447{
2448 byteidx(argvars, rettv, TRUE);
2449}
2450
2451/*
2452 * "call(func, arglist [, dict])" function
2453 */
2454 static void
2455f_call(typval_T *argvars, typval_T *rettv)
2456{
2457 char_u *func;
2458 partial_T *partial = NULL;
2459 dict_T *selfdict = NULL;
2460
2461 if (argvars[1].v_type != VAR_LIST)
2462 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002463 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002464 return;
2465 }
2466 if (argvars[1].vval.v_list == NULL)
2467 return;
2468
2469 if (argvars[0].v_type == VAR_FUNC)
2470 func = argvars[0].vval.v_string;
2471 else if (argvars[0].v_type == VAR_PARTIAL)
2472 {
2473 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002474 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002475 }
2476 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002477 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002478 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002479 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002480
2481 if (argvars[2].v_type != VAR_UNKNOWN)
2482 {
2483 if (argvars[2].v_type != VAR_DICT)
2484 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002485 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002486 return;
2487 }
2488 selfdict = argvars[2].vval.v_dict;
2489 }
2490
2491 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2492}
2493
2494#ifdef FEAT_FLOAT
2495/*
2496 * "ceil({float})" function
2497 */
2498 static void
2499f_ceil(typval_T *argvars, typval_T *rettv)
2500{
2501 float_T f = 0.0;
2502
2503 rettv->v_type = VAR_FLOAT;
2504 if (get_float_arg(argvars, &f) == OK)
2505 rettv->vval.v_float = ceil(f);
2506 else
2507 rettv->vval.v_float = 0.0;
2508}
2509#endif
2510
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002511/*
2512 * "changenr()" function
2513 */
2514 static void
2515f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2516{
2517 rettv->vval.v_number = curbuf->b_u_seq_cur;
2518}
2519
2520/*
2521 * "char2nr(string)" function
2522 */
2523 static void
2524f_char2nr(typval_T *argvars, typval_T *rettv)
2525{
Bram Moolenaarc5809432021-03-27 21:23:30 +01002526 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2527 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002528 if (has_mbyte)
2529 {
2530 int utf8 = 0;
2531
2532 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002533 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002534
2535 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002536 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002537 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002538 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002539 }
2540 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002541 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002542}
2543
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002544/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002545 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2546 * returns the character index of the column. Otherwise, returns the byte index
2547 * of the column.
2548 */
2549 static void
2550get_col(typval_T *argvars, typval_T *rettv, int charcol)
2551{
2552 colnr_T col = 0;
2553 pos_T *fp;
2554 int fnum = curbuf->b_fnum;
2555
2556 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2557 if (fp != NULL && fnum == curbuf->b_fnum)
2558 {
2559 if (fp->col == MAXCOL)
2560 {
2561 // '> can be MAXCOL, get the length of the line then
2562 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2563 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2564 else
2565 col = MAXCOL;
2566 }
2567 else
2568 {
2569 col = fp->col + 1;
2570 // col(".") when the cursor is on the NUL at the end of the line
2571 // because of "coladd" can be seen as an extra column.
2572 if (virtual_active() && fp == &curwin->w_cursor)
2573 {
2574 char_u *p = ml_get_cursor();
2575
2576 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2577 curwin->w_virtcol - curwin->w_cursor.coladd))
2578 {
2579 int l;
2580
2581 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2582 col += l;
2583 }
2584 }
2585 }
2586 }
2587 rettv->vval.v_number = col;
2588}
2589
2590/*
2591 * "charcol()" function
2592 */
2593 static void
2594f_charcol(typval_T *argvars, typval_T *rettv)
2595{
2596 get_col(argvars, rettv, TRUE);
2597}
2598
2599/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002600 * "charidx()" function
2601 */
2602 static void
2603f_charidx(typval_T *argvars, typval_T *rettv)
2604{
2605 char_u *str;
2606 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002607 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002608 char_u *p;
2609 int len;
2610 int (*ptr2len)(char_u *);
2611
2612 rettv->vval.v_number = -1;
2613
2614 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2615 || (argvars[2].v_type != VAR_UNKNOWN
2616 && argvars[2].v_type != VAR_NUMBER))
2617 {
2618 emsg(_(e_invarg));
2619 return;
2620 }
2621
2622 str = tv_get_string_chk(&argvars[0]);
2623 idx = tv_get_number_chk(&argvars[1], NULL);
2624 if (str == NULL || idx < 0)
2625 return;
2626
2627 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002628 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002629 if (countcc < 0 || countcc > 1)
2630 {
2631 semsg(_(e_using_number_as_bool_nr), countcc);
2632 return;
2633 }
2634
2635 if (enc_utf8 && countcc)
2636 ptr2len = utf_ptr2len;
2637 else
2638 ptr2len = mb_ptr2len;
2639
2640 for (p = str, len = 0; p <= str + idx; len++)
2641 {
2642 if (*p == NUL)
2643 return;
2644 p += ptr2len(p);
2645 }
2646
2647 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2648}
2649
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002650 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002651get_optional_window(typval_T *argvars, int idx)
2652{
2653 win_T *win = curwin;
2654
2655 if (argvars[idx].v_type != VAR_UNKNOWN)
2656 {
2657 win = find_win_by_nr_or_id(&argvars[idx]);
2658 if (win == NULL)
2659 {
2660 emsg(_(e_invalwindow));
2661 return NULL;
2662 }
2663 }
2664 return win;
2665}
2666
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002667/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002668 * "col(string)" function
2669 */
2670 static void
2671f_col(typval_T *argvars, typval_T *rettv)
2672{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002673 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002674}
2675
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002676/*
2677 * "confirm(message, buttons[, default [, type]])" function
2678 */
2679 static void
2680f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2681{
2682#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2683 char_u *message;
2684 char_u *buttons = NULL;
2685 char_u buf[NUMBUFLEN];
2686 char_u buf2[NUMBUFLEN];
2687 int def = 1;
2688 int type = VIM_GENERIC;
2689 char_u *typestr;
2690 int error = FALSE;
2691
Bram Moolenaarc5809432021-03-27 21:23:30 +01002692 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2693 return;
2694
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002695 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002696 if (message == NULL)
2697 error = TRUE;
2698 if (argvars[1].v_type != VAR_UNKNOWN)
2699 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002700 if (in_vim9script() && check_for_string_arg(argvars, 1) == FAIL)
2701 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002702 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002703 if (buttons == NULL)
2704 error = TRUE;
2705 if (argvars[2].v_type != VAR_UNKNOWN)
2706 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002707 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002708 if (argvars[3].v_type != VAR_UNKNOWN)
2709 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002710 if (in_vim9script() && check_for_string_arg(argvars, 3) == FAIL)
2711 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002712 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002713 if (typestr == NULL)
2714 error = TRUE;
2715 else
2716 {
2717 switch (TOUPPER_ASC(*typestr))
2718 {
2719 case 'E': type = VIM_ERROR; break;
2720 case 'Q': type = VIM_QUESTION; break;
2721 case 'I': type = VIM_INFO; break;
2722 case 'W': type = VIM_WARNING; break;
2723 case 'G': type = VIM_GENERIC; break;
2724 }
2725 }
2726 }
2727 }
2728 }
2729
2730 if (buttons == NULL || *buttons == NUL)
2731 buttons = (char_u *)_("&Ok");
2732
2733 if (!error)
2734 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2735 def, NULL, FALSE);
2736#endif
2737}
2738
2739/*
2740 * "copy()" function
2741 */
2742 static void
2743f_copy(typval_T *argvars, typval_T *rettv)
2744{
2745 item_copy(&argvars[0], rettv, FALSE, 0);
2746}
2747
2748#ifdef FEAT_FLOAT
2749/*
2750 * "cos()" function
2751 */
2752 static void
2753f_cos(typval_T *argvars, typval_T *rettv)
2754{
2755 float_T f = 0.0;
2756
2757 rettv->v_type = VAR_FLOAT;
2758 if (get_float_arg(argvars, &f) == OK)
2759 rettv->vval.v_float = cos(f);
2760 else
2761 rettv->vval.v_float = 0.0;
2762}
2763
2764/*
2765 * "cosh()" function
2766 */
2767 static void
2768f_cosh(typval_T *argvars, typval_T *rettv)
2769{
2770 float_T f = 0.0;
2771
2772 rettv->v_type = VAR_FLOAT;
2773 if (get_float_arg(argvars, &f) == OK)
2774 rettv->vval.v_float = cosh(f);
2775 else
2776 rettv->vval.v_float = 0.0;
2777}
2778#endif
2779
2780/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002781 * Set the cursor position.
2782 * If 'charcol' is TRUE, then use the column number as a character offet.
2783 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002784 */
2785 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002786set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002787{
2788 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002789 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002790 int set_curswant = TRUE;
2791
2792 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002793 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002794 {
2795 pos_T pos;
2796 colnr_T curswant = -1;
2797
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002798 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002799 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002800 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002801 return;
2802 }
2803 line = pos.lnum;
2804 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002805 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002806 if (curswant >= 0)
2807 {
2808 curwin->w_curswant = curswant - 1;
2809 set_curswant = FALSE;
2810 }
2811 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002812 else if ((argvars[0].v_type == VAR_NUMBER ||
2813 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002814 && (argvars[1].v_type == VAR_NUMBER ||
2815 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002816 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002817 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002818 if (line < 0)
2819 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002820 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002821 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002822 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002823 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002824 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002825 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002826 else
2827 {
2828 emsg(_(e_invarg));
2829 return;
2830 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002831 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002832 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002833 if (line > 0)
2834 curwin->w_cursor.lnum = line;
2835 if (col > 0)
2836 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002837 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002838
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002839 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002840 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002841 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002842 if (has_mbyte)
2843 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002844
2845 curwin->w_set_curswant = set_curswant;
2846 rettv->vval.v_number = 0;
2847}
2848
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002849/*
2850 * "cursor(lnum, col)" function, or
2851 * "cursor(list)"
2852 *
2853 * Moves the cursor to the specified line and column.
2854 * Returns 0 when the position could be set, -1 otherwise.
2855 */
2856 static void
2857f_cursor(typval_T *argvars, typval_T *rettv)
2858{
2859 set_cursorpos(argvars, rettv, FALSE);
2860}
2861
Bram Moolenaar4f974752019-02-17 17:44:42 +01002862#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002863/*
2864 * "debugbreak()" function
2865 */
2866 static void
2867f_debugbreak(typval_T *argvars, typval_T *rettv)
2868{
2869 int pid;
2870
2871 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002872 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002873 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002874 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002875 else
2876 {
2877 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2878
2879 if (hProcess != NULL)
2880 {
2881 DebugBreakProcess(hProcess);
2882 CloseHandle(hProcess);
2883 rettv->vval.v_number = OK;
2884 }
2885 }
2886}
2887#endif
2888
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002889/*
2890 * "deepcopy()" function
2891 */
2892 static void
2893f_deepcopy(typval_T *argvars, typval_T *rettv)
2894{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002895 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002896 int copyID;
2897
2898 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002899 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002900 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002901 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002902 else
2903 {
2904 copyID = get_copyID();
2905 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2906 }
2907}
2908
2909/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002910 * "did_filetype()" function
2911 */
2912 static void
2913f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2914{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002915 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002916}
2917
2918/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002919 * "echoraw({expr})" function
2920 */
2921 static void
2922f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2923{
2924 char_u *str = tv_get_string_chk(&argvars[0]);
2925
2926 if (str != NULL && *str != NUL)
2927 {
2928 out_str(str);
2929 out_flush();
2930 }
2931}
2932
2933/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002934 * "empty({expr})" function
2935 */
2936 static void
2937f_empty(typval_T *argvars, typval_T *rettv)
2938{
2939 int n = FALSE;
2940
2941 switch (argvars[0].v_type)
2942 {
2943 case VAR_STRING:
2944 case VAR_FUNC:
2945 n = argvars[0].vval.v_string == NULL
2946 || *argvars[0].vval.v_string == NUL;
2947 break;
2948 case VAR_PARTIAL:
2949 n = FALSE;
2950 break;
2951 case VAR_NUMBER:
2952 n = argvars[0].vval.v_number == 0;
2953 break;
2954 case VAR_FLOAT:
2955#ifdef FEAT_FLOAT
2956 n = argvars[0].vval.v_float == 0.0;
2957 break;
2958#endif
2959 case VAR_LIST:
2960 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002961 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002962 break;
2963 case VAR_DICT:
2964 n = argvars[0].vval.v_dict == NULL
2965 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2966 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002967 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002968 case VAR_SPECIAL:
2969 n = argvars[0].vval.v_number != VVAL_TRUE;
2970 break;
2971
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002972 case VAR_BLOB:
2973 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002974 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2975 break;
2976
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002977 case VAR_JOB:
2978#ifdef FEAT_JOB_CHANNEL
2979 n = argvars[0].vval.v_job == NULL
2980 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2981 break;
2982#endif
2983 case VAR_CHANNEL:
2984#ifdef FEAT_JOB_CHANNEL
2985 n = argvars[0].vval.v_channel == NULL
2986 || !channel_is_open(argvars[0].vval.v_channel);
2987 break;
2988#endif
2989 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002990 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002992 case VAR_INSTR:
Bram Moolenaardd589232020-02-29 17:38:12 +01002993 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002994 n = TRUE;
2995 break;
2996 }
2997
2998 rettv->vval.v_number = n;
2999}
3000
3001/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003002 * "environ()" function
3003 */
3004 static void
3005f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3006{
3007#if !defined(AMIGA)
3008 int i = 0;
3009 char_u *entry, *value;
3010# ifdef MSWIN
3011 extern wchar_t **_wenviron;
3012# else
3013 extern char **environ;
3014# endif
3015
3016 if (rettv_dict_alloc(rettv) != OK)
3017 return;
3018
3019# ifdef MSWIN
3020 if (*_wenviron == NULL)
3021 return;
3022# else
3023 if (*environ == NULL)
3024 return;
3025# endif
3026
3027 for (i = 0; ; ++i)
3028 {
3029# ifdef MSWIN
3030 short_u *p;
3031
3032 if ((p = (short_u *)_wenviron[i]) == NULL)
3033 return;
3034 entry = utf16_to_enc(p, NULL);
3035# else
3036 if ((entry = (char_u *)environ[i]) == NULL)
3037 return;
3038 entry = vim_strsave(entry);
3039# endif
3040 if (entry == NULL) // out of memory
3041 return;
3042 if ((value = vim_strchr(entry, '=')) == NULL)
3043 {
3044 vim_free(entry);
3045 continue;
3046 }
3047 *value++ = NUL;
3048 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3049 vim_free(entry);
3050 }
3051#endif
3052}
3053
3054/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003055 * "escape({string}, {chars})" function
3056 */
3057 static void
3058f_escape(typval_T *argvars, typval_T *rettv)
3059{
3060 char_u buf[NUMBUFLEN];
3061
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003062 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3063 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003064 rettv->v_type = VAR_STRING;
3065}
3066
3067/*
3068 * "eval()" function
3069 */
3070 static void
3071f_eval(typval_T *argvars, typval_T *rettv)
3072{
3073 char_u *s, *p;
3074
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003075 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003076 if (s != NULL)
3077 s = skipwhite(s);
3078
3079 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003080 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003081 {
3082 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003083 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003084 need_clr_eos = FALSE;
3085 rettv->v_type = VAR_NUMBER;
3086 rettv->vval.v_number = 0;
3087 }
3088 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003089 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003090}
3091
3092/*
3093 * "eventhandler()" function
3094 */
3095 static void
3096f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3097{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003098 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003099}
3100
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003101static garray_T redir_execute_ga;
3102
3103/*
3104 * Append "value[value_len]" to the execute() output.
3105 */
3106 void
3107execute_redir_str(char_u *value, int value_len)
3108{
3109 int len;
3110
3111 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003112 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003113 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003114 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003115 if (ga_grow(&redir_execute_ga, len) == OK)
3116 {
3117 mch_memmove((char *)redir_execute_ga.ga_data
3118 + redir_execute_ga.ga_len, value, len);
3119 redir_execute_ga.ga_len += len;
3120 }
3121}
3122
3123/*
3124 * Get next line from a list.
3125 * Called by do_cmdline() to get the next line.
3126 * Returns allocated string, or NULL for end of function.
3127 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003128 static char_u *
3129get_list_line(
3130 int c UNUSED,
3131 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003132 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02003133 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003134{
3135 listitem_T **p = (listitem_T **)cookie;
3136 listitem_T *item = *p;
3137 char_u buf[NUMBUFLEN];
3138 char_u *s;
3139
3140 if (item == NULL)
3141 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003142 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003143 *p = item->li_next;
3144 return s == NULL ? NULL : vim_strsave(s);
3145}
3146
3147/*
3148 * "execute()" function
3149 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02003150 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003151execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003152{
3153 char_u *cmd = NULL;
3154 list_T *list = NULL;
3155 int save_msg_silent = msg_silent;
3156 int save_emsg_silent = emsg_silent;
3157 int save_emsg_noredir = emsg_noredir;
3158 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003159 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003160 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003161 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003162 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003163
3164 rettv->vval.v_string = NULL;
3165 rettv->v_type = VAR_STRING;
3166
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003167 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003168 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003169 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003170 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003171 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003172 return;
3173 ++list->lv_refcount;
3174 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003175 else if (argvars[arg_off].v_type == VAR_JOB
3176 || argvars[arg_off].v_type == VAR_CHANNEL)
3177 {
3178 emsg(_(e_inval_string));
3179 return;
3180 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003181 else
3182 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003183 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003184 if (cmd == NULL)
3185 return;
3186 }
3187
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003188 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003189 {
3190 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003191 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003192
3193 if (s == NULL)
3194 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003195 if (*s == NUL)
3196 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003197 if (STRNCMP(s, "silent", 6) == 0)
3198 ++msg_silent;
3199 if (STRCMP(s, "silent!") == 0)
3200 {
3201 emsg_silent = TRUE;
3202 emsg_noredir = TRUE;
3203 }
3204 }
3205 else
3206 ++msg_silent;
3207
3208 if (redir_execute)
3209 save_ga = redir_execute_ga;
3210 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3211 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003212 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003213 if (!echo_output)
3214 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003215
3216 if (cmd != NULL)
3217 do_cmdline_cmd(cmd);
3218 else
3219 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003220 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003221
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003222 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003223 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003224 do_cmdline(NULL, get_list_line, (void *)&item,
3225 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3226 --list->lv_refcount;
3227 }
3228
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003229 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003230 if (ga_grow(&redir_execute_ga, 1) == OK)
3231 {
3232 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3233 rettv->vval.v_string = redir_execute_ga.ga_data;
3234 }
3235 else
3236 {
3237 ga_clear(&redir_execute_ga);
3238 rettv->vval.v_string = NULL;
3239 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003240 msg_silent = save_msg_silent;
3241 emsg_silent = save_emsg_silent;
3242 emsg_noredir = save_emsg_noredir;
3243
3244 redir_execute = save_redir_execute;
3245 if (redir_execute)
3246 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003247 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003248
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003249 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003250 if (echo_output)
3251 // When not working silently: put it in column zero. A following
3252 // "echon" will overwrite the message, unavoidably.
3253 msg_col = 0;
3254 else
3255 // When working silently: Put it back where it was, since nothing
3256 // should have been written.
3257 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003258}
3259
3260/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003261 * "execute()" function
3262 */
3263 static void
3264f_execute(typval_T *argvars, typval_T *rettv)
3265{
3266 execute_common(argvars, rettv, 0);
3267}
3268
3269/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003270 * "exists()" function
3271 */
3272 static void
3273f_exists(typval_T *argvars, typval_T *rettv)
3274{
3275 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003276 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003277
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003278 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003279 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003280 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003281 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003282 if (mch_getenv(p + 1) != NULL)
3283 n = TRUE;
3284 else
3285 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003286 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003287 p = expand_env_save(p);
3288 if (p != NULL && *p != '$')
3289 n = TRUE;
3290 vim_free(p);
3291 }
3292 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003293 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003294 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003295 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003296 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003297 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003298 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003299 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003300 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003301 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003302 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003303 else if (*p == '?') // internal function only
3304 {
3305 n = has_internal_func_name(p + 1);
3306 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003307 else if (*p == ':')
3308 {
3309 n = cmd_exists(p + 1);
3310 }
3311 else if (*p == '#')
3312 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003313 if (p[1] == '#')
3314 n = autocmd_supported(p + 2);
3315 else
3316 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003317 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003318 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003319 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003320 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003321 }
3322
3323 rettv->vval.v_number = n;
3324}
3325
3326#ifdef FEAT_FLOAT
3327/*
3328 * "exp()" function
3329 */
3330 static void
3331f_exp(typval_T *argvars, typval_T *rettv)
3332{
3333 float_T f = 0.0;
3334
3335 rettv->v_type = VAR_FLOAT;
3336 if (get_float_arg(argvars, &f) == OK)
3337 rettv->vval.v_float = exp(f);
3338 else
3339 rettv->vval.v_float = 0.0;
3340}
3341#endif
3342
3343/*
3344 * "expand()" function
3345 */
3346 static void
3347f_expand(typval_T *argvars, typval_T *rettv)
3348{
3349 char_u *s;
3350 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003351 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003352 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3353 expand_T xpc;
3354 int error = FALSE;
3355 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003356#ifdef BACKSLASH_IN_FILENAME
3357 char_u *p_csl_save = p_csl;
3358
3359 // avoid using 'completeslash' here
3360 p_csl = empty_option;
3361#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003362
3363 rettv->v_type = VAR_STRING;
3364 if (argvars[1].v_type != VAR_UNKNOWN
3365 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003366 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003367 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003368 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003369
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003370 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003371 if (*s == '%' || *s == '#' || *s == '<')
3372 {
3373 ++emsg_off;
3374 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3375 --emsg_off;
3376 if (rettv->v_type == VAR_LIST)
3377 {
3378 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3379 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003380 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003381 }
3382 else
3383 rettv->vval.v_string = result;
3384 }
3385 else
3386 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003387 // When the optional second argument is non-zero, don't remove matches
3388 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003389 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003390 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003391 options |= WILD_KEEP_ALL;
3392 if (!error)
3393 {
3394 ExpandInit(&xpc);
3395 xpc.xp_context = EXPAND_FILES;
3396 if (p_wic)
3397 options += WILD_ICASE;
3398 if (rettv->v_type == VAR_STRING)
3399 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3400 options, WILD_ALL);
3401 else if (rettv_list_alloc(rettv) != FAIL)
3402 {
3403 int i;
3404
3405 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3406 for (i = 0; i < xpc.xp_numfiles; i++)
3407 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3408 ExpandCleanup(&xpc);
3409 }
3410 }
3411 else
3412 rettv->vval.v_string = NULL;
3413 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003414#ifdef BACKSLASH_IN_FILENAME
3415 p_csl = p_csl_save;
3416#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003417}
3418
3419/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003420 * "expandcmd()" function
3421 * Expand all the special characters in a command string.
3422 */
3423 static void
3424f_expandcmd(typval_T *argvars, typval_T *rettv)
3425{
3426 exarg_T eap;
3427 char_u *cmdstr;
3428 char *errormsg = NULL;
3429
3430 rettv->v_type = VAR_STRING;
3431 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3432
3433 memset(&eap, 0, sizeof(eap));
3434 eap.cmd = cmdstr;
3435 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003436 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003437 eap.usefilter = FALSE;
3438 eap.nextcmd = NULL;
3439 eap.cmdidx = CMD_USER;
3440
3441 expand_filename(&eap, &cmdstr, &errormsg);
3442 if (errormsg != NULL && *errormsg != NUL)
3443 emsg(errormsg);
3444
3445 rettv->vval.v_string = cmdstr;
3446}
3447
3448/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003449 * "feedkeys()" function
3450 */
3451 static void
3452f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3453{
3454 int remap = TRUE;
3455 int insert = FALSE;
3456 char_u *keys, *flags;
3457 char_u nbuf[NUMBUFLEN];
3458 int typed = FALSE;
3459 int execute = FALSE;
3460 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003461 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003462 char_u *keys_esc;
3463
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003464 // This is not allowed in the sandbox. If the commands would still be
3465 // executed in the sandbox it would be OK, but it probably happens later,
3466 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003467 if (check_secure())
3468 return;
3469
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003470 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003471
3472 if (argvars[1].v_type != VAR_UNKNOWN)
3473 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003474 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003475 for ( ; *flags != NUL; ++flags)
3476 {
3477 switch (*flags)
3478 {
3479 case 'n': remap = FALSE; break;
3480 case 'm': remap = TRUE; break;
3481 case 't': typed = TRUE; break;
3482 case 'i': insert = TRUE; break;
3483 case 'x': execute = TRUE; break;
3484 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003485 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003486 }
3487 }
3488 }
3489
3490 if (*keys != NUL || execute)
3491 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003492 // Need to escape K_SPECIAL and CSI before putting the string in the
3493 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003494 keys_esc = vim_strsave_escape_csi(keys);
3495 if (keys_esc != NULL)
3496 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003497 if (lowlevel)
3498 {
3499#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003500 int idx;
3501 int len = (int)STRLEN(keys);
3502
3503 for (idx = 0; idx < len; ++idx)
3504 {
3505 // if a CTRL-C was typed, set got_int, similar to what
3506 // happens in fill_input_buf()
3507 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3508 got_int = TRUE;
3509 add_to_input_buf(keys + idx, 1);
3510 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003511#else
3512 emsg(_("E980: lowlevel input not supported"));
3513#endif
3514 }
3515 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003516 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003517 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003518 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003519 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003520#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003521 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003522#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003523 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003524 typebuf_was_filled = TRUE;
3525 }
3526 vim_free(keys_esc);
3527
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003528 if (execute)
3529 {
3530 int save_msg_scroll = msg_scroll;
3531
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003532 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003533 msg_scroll = FALSE;
3534
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003535 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003536 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003537 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003538 ++in_feedkeys;
3539 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003540 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003541 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003542 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003543 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003544 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003545 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003546
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003547 msg_scroll |= save_msg_scroll;
3548 }
3549 }
3550 }
3551}
3552
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003553#ifdef FEAT_FLOAT
3554/*
3555 * "float2nr({float})" function
3556 */
3557 static void
3558f_float2nr(typval_T *argvars, typval_T *rettv)
3559{
3560 float_T f = 0.0;
3561
3562 if (get_float_arg(argvars, &f) == OK)
3563 {
Bram Moolenaar37184272020-05-23 19:30:05 +02003564 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003565 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02003566 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003567 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003568 else
3569 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003570 }
3571}
3572
3573/*
3574 * "floor({float})" function
3575 */
3576 static void
3577f_floor(typval_T *argvars, typval_T *rettv)
3578{
3579 float_T f = 0.0;
3580
3581 rettv->v_type = VAR_FLOAT;
3582 if (get_float_arg(argvars, &f) == OK)
3583 rettv->vval.v_float = floor(f);
3584 else
3585 rettv->vval.v_float = 0.0;
3586}
3587
3588/*
3589 * "fmod()" function
3590 */
3591 static void
3592f_fmod(typval_T *argvars, typval_T *rettv)
3593{
3594 float_T fx = 0.0, fy = 0.0;
3595
3596 rettv->v_type = VAR_FLOAT;
3597 if (get_float_arg(argvars, &fx) == OK
3598 && get_float_arg(&argvars[1], &fy) == OK)
3599 rettv->vval.v_float = fmod(fx, fy);
3600 else
3601 rettv->vval.v_float = 0.0;
3602}
3603#endif
3604
3605/*
3606 * "fnameescape({string})" function
3607 */
3608 static void
3609f_fnameescape(typval_T *argvars, typval_T *rettv)
3610{
3611 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003612 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003613 rettv->v_type = VAR_STRING;
3614}
3615
3616/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003617 * "foreground()" function
3618 */
3619 static void
3620f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3621{
3622#ifdef FEAT_GUI
3623 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003624 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003625 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003626 return;
3627 }
3628#endif
3629#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003630 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003631#endif
3632}
3633
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003634 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003635common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003636{
3637 char_u *s;
3638 char_u *name;
3639 int use_string = FALSE;
3640 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003641 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003642 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003643
3644 if (argvars[0].v_type == VAR_FUNC)
3645 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003646 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003647 s = argvars[0].vval.v_string;
3648 }
3649 else if (argvars[0].v_type == VAR_PARTIAL
3650 && argvars[0].vval.v_partial != NULL)
3651 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003652 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003653 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003654 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003655 }
3656 else
3657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003658 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003659 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003660 use_string = TRUE;
3661 }
3662
Bram Moolenaar843b8842016-08-21 14:36:15 +02003663 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003664 {
3665 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003666 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003667 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3668 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003669 if (*name != NUL)
3670 s = NULL;
3671 }
3672
Bram Moolenaar843b8842016-08-21 14:36:15 +02003673 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3674 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003675 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003676 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003677 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003678 ? find_func(trans_name, is_global, NULL) == NULL
3679 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003680 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003681 else
3682 {
3683 int dict_idx = 0;
3684 int arg_idx = 0;
3685 list_T *list = NULL;
3686
3687 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3688 {
3689 char sid_buf[25];
3690 int off = *s == 's' ? 2 : 5;
3691
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003692 // Expand s: and <SID> into <SNR>nr_, so that the function can
3693 // also be called from another script. Using trans_function_name()
3694 // would also work, but some plugins depend on the name being
3695 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003696 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003697 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003698 if (name != NULL)
3699 {
3700 STRCPY(name, sid_buf);
3701 STRCAT(name, s + off);
3702 }
3703 }
3704 else
3705 name = vim_strsave(s);
3706
3707 if (argvars[1].v_type != VAR_UNKNOWN)
3708 {
3709 if (argvars[2].v_type != VAR_UNKNOWN)
3710 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003711 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003712 arg_idx = 1;
3713 dict_idx = 2;
3714 }
3715 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003716 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003717 dict_idx = 1;
3718 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003719 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003720 arg_idx = 1;
3721 if (dict_idx > 0)
3722 {
3723 if (argvars[dict_idx].v_type != VAR_DICT)
3724 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003725 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003726 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003727 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003728 }
3729 if (argvars[dict_idx].vval.v_dict == NULL)
3730 dict_idx = 0;
3731 }
3732 if (arg_idx > 0)
3733 {
3734 if (argvars[arg_idx].v_type != VAR_LIST)
3735 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003736 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003737 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003738 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003739 }
3740 list = argvars[arg_idx].vval.v_list;
3741 if (list == NULL || list->lv_len == 0)
3742 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003743 else if (list->lv_len > MAX_FUNC_ARGS)
3744 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003745 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003746 vim_free(name);
3747 goto theend;
3748 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003749 }
3750 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003751 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003752 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003753 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003754
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003755 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003756 if (pt == NULL)
3757 vim_free(name);
3758 else
3759 {
3760 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3761 {
3762 listitem_T *li;
3763 int i = 0;
3764 int arg_len = 0;
3765 int lv_len = 0;
3766
3767 if (arg_pt != NULL)
3768 arg_len = arg_pt->pt_argc;
3769 if (list != NULL)
3770 lv_len = list->lv_len;
3771 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003772 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003773 if (pt->pt_argv == NULL)
3774 {
3775 vim_free(pt);
3776 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003777 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003778 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003779 for (i = 0; i < arg_len; i++)
3780 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3781 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003782 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003783 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003784 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003785 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003786 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003787 }
3788
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003789 // For "function(dict.func, [], dict)" and "func" is a partial
3790 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003791 if (dict_idx > 0)
3792 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003793 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003794 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3795 ++pt->pt_dict->dv_refcount;
3796 }
3797 else if (arg_pt != NULL)
3798 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003799 // If the dict was bound automatically the result is also
3800 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003801 pt->pt_dict = arg_pt->pt_dict;
3802 pt->pt_auto = arg_pt->pt_auto;
3803 if (pt->pt_dict != NULL)
3804 ++pt->pt_dict->dv_refcount;
3805 }
3806
3807 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003808 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3809 {
3810 pt->pt_func = arg_pt->pt_func;
3811 func_ptr_ref(pt->pt_func);
3812 vim_free(name);
3813 }
3814 else if (is_funcref)
3815 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003816 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003817 func_ptr_ref(pt->pt_func);
3818 vim_free(name);
3819 }
3820 else
3821 {
3822 pt->pt_name = name;
3823 func_ref(name);
3824 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003825 }
3826 rettv->v_type = VAR_PARTIAL;
3827 rettv->vval.v_partial = pt;
3828 }
3829 else
3830 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003831 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003832 rettv->v_type = VAR_FUNC;
3833 rettv->vval.v_string = name;
3834 func_ref(name);
3835 }
3836 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003837theend:
3838 vim_free(trans_name);
3839}
3840
3841/*
3842 * "funcref()" function
3843 */
3844 static void
3845f_funcref(typval_T *argvars, typval_T *rettv)
3846{
3847 common_function(argvars, rettv, TRUE);
3848}
3849
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003850 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003851ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003852{
3853 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3854 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003855 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003856}
3857
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003858/*
3859 * "function()" function
3860 */
3861 static void
3862f_function(typval_T *argvars, typval_T *rettv)
3863{
3864 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003865}
3866
3867/*
3868 * "garbagecollect()" function
3869 */
3870 static void
3871f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3872{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003873 // This is postponed until we are back at the toplevel, because we may be
3874 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003875 want_garbage_collect = TRUE;
3876
Bram Moolenaar2df47312020-09-05 17:30:44 +02003877 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003878 garbage_collect_at_exit = TRUE;
3879}
3880
3881/*
3882 * "get()" function
3883 */
3884 static void
3885f_get(typval_T *argvars, typval_T *rettv)
3886{
3887 listitem_T *li;
3888 list_T *l;
3889 dictitem_T *di;
3890 dict_T *d;
3891 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003892 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003893
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003894 if (argvars[0].v_type == VAR_BLOB)
3895 {
3896 int error = FALSE;
3897 int idx = tv_get_number_chk(&argvars[1], &error);
3898
3899 if (!error)
3900 {
3901 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003902 if (idx < 0)
3903 idx = blob_len(argvars[0].vval.v_blob) + idx;
3904 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3905 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003906 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003907 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003908 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003909 tv = rettv;
3910 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003911 }
3912 }
3913 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003914 {
3915 if ((l = argvars[0].vval.v_list) != NULL)
3916 {
3917 int error = FALSE;
3918
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003919 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003920 if (!error && li != NULL)
3921 tv = &li->li_tv;
3922 }
3923 }
3924 else if (argvars[0].v_type == VAR_DICT)
3925 {
3926 if ((d = argvars[0].vval.v_dict) != NULL)
3927 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003928 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003929 if (di != NULL)
3930 tv = &di->di_tv;
3931 }
3932 }
3933 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3934 {
3935 partial_T *pt;
3936 partial_T fref_pt;
3937
3938 if (argvars[0].v_type == VAR_PARTIAL)
3939 pt = argvars[0].vval.v_partial;
3940 else
3941 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003942 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003943 fref_pt.pt_name = argvars[0].vval.v_string;
3944 pt = &fref_pt;
3945 }
3946
3947 if (pt != NULL)
3948 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003949 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003950 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003951
3952 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3953 {
3954 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003955 n = partial_name(pt);
3956 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003957 rettv->vval.v_string = NULL;
3958 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003959 {
3960 rettv->vval.v_string = vim_strsave(n);
3961 if (rettv->v_type == VAR_FUNC)
3962 func_ref(rettv->vval.v_string);
3963 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003964 }
3965 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003966 {
3967 what_is_dict = TRUE;
3968 if (pt->pt_dict != NULL)
3969 rettv_dict_set(rettv, pt->pt_dict);
3970 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003971 else if (STRCMP(what, "args") == 0)
3972 {
3973 rettv->v_type = VAR_LIST;
3974 if (rettv_list_alloc(rettv) == OK)
3975 {
3976 int i;
3977
3978 for (i = 0; i < pt->pt_argc; ++i)
3979 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3980 }
3981 }
3982 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003983 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003984
3985 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3986 // third argument
3987 if (!what_is_dict)
3988 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003989 }
3990 }
3991 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003992 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003993
3994 if (tv == NULL)
3995 {
3996 if (argvars[2].v_type != VAR_UNKNOWN)
3997 copy_tv(&argvars[2], rettv);
3998 }
3999 else
4000 copy_tv(tv, rettv);
4001}
4002
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004003/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004004 * "getchangelist()" function
4005 */
4006 static void
4007f_getchangelist(typval_T *argvars, typval_T *rettv)
4008{
4009#ifdef FEAT_JUMPLIST
4010 buf_T *buf;
4011 int i;
4012 list_T *l;
4013 dict_T *d;
4014#endif
4015
4016 if (rettv_list_alloc(rettv) != OK)
4017 return;
4018
4019#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02004020 if (argvars[0].v_type == VAR_UNKNOWN)
4021 buf = curbuf;
4022 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02004023 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004024 if (buf == NULL)
4025 return;
4026
4027 l = list_alloc();
4028 if (l == NULL)
4029 return;
4030
4031 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4032 return;
4033 /*
4034 * The current window change list index tracks only the position in the
4035 * current buffer change list. For other buffers, use the change list
4036 * length as the current index.
4037 */
4038 list_append_number(rettv->vval.v_list,
4039 (varnumber_T)((buf == curwin->w_buffer)
4040 ? curwin->w_changelistidx : buf->b_changelistlen));
4041
4042 for (i = 0; i < buf->b_changelistlen; ++i)
4043 {
4044 if (buf->b_changelist[i].lnum == 0)
4045 continue;
4046 if ((d = dict_alloc()) == NULL)
4047 return;
4048 if (list_append_dict(l, d) == FAIL)
4049 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004050 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4051 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004052 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004053 }
4054#endif
4055}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004056
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004057 static void
4058getpos_both(
4059 typval_T *argvars,
4060 typval_T *rettv,
4061 int getcurpos,
4062 int charcol)
4063{
4064 pos_T *fp = NULL;
4065 pos_T pos;
4066 win_T *wp = curwin;
4067 list_T *l;
4068 int fnum = -1;
4069
4070 if (rettv_list_alloc(rettv) == OK)
4071 {
4072 l = rettv->vval.v_list;
4073 if (getcurpos)
4074 {
4075 if (argvars[0].v_type != VAR_UNKNOWN)
4076 {
4077 wp = find_win_by_nr_or_id(&argvars[0]);
4078 if (wp != NULL)
4079 fp = &wp->w_cursor;
4080 }
4081 else
4082 fp = &curwin->w_cursor;
4083 if (fp != NULL && charcol)
4084 {
4085 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01004086 pos.col =
4087 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004088 fp = &pos;
4089 }
4090 }
4091 else
4092 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
4093 if (fnum != -1)
4094 list_append_number(l, (varnumber_T)fnum);
4095 else
4096 list_append_number(l, (varnumber_T)0);
4097 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4098 : (varnumber_T)0);
4099 list_append_number(l, (fp != NULL)
4100 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4101 : (varnumber_T)0);
4102 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
4103 (varnumber_T)0);
4104 if (getcurpos)
4105 {
4106 int save_set_curswant = curwin->w_set_curswant;
4107 colnr_T save_curswant = curwin->w_curswant;
4108 colnr_T save_virtcol = curwin->w_virtcol;
4109
4110 if (wp == curwin)
4111 update_curswant();
4112 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
4113 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
4114
4115 // Do not change "curswant", as it is unexpected that a get
4116 // function has a side effect.
4117 if (wp == curwin && save_set_curswant)
4118 {
4119 curwin->w_set_curswant = save_set_curswant;
4120 curwin->w_curswant = save_curswant;
4121 curwin->w_virtcol = save_virtcol;
4122 curwin->w_valid &= ~VALID_VIRTCOL;
4123 }
4124 }
4125 }
4126 else
4127 rettv->vval.v_number = FALSE;
4128}
4129
4130/*
4131 * "getcharpos()" function
4132 */
4133 static void
4134f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
4135{
4136 getpos_both(argvars, rettv, FALSE, TRUE);
4137}
4138
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004139/*
4140 * "getcharsearch()" function
4141 */
4142 static void
4143f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4144{
4145 if (rettv_dict_alloc(rettv) != FAIL)
4146 {
4147 dict_T *dict = rettv->vval.v_dict;
4148
Bram Moolenaare0be1672018-07-08 16:50:37 +02004149 dict_add_string(dict, "char", last_csearch());
4150 dict_add_number(dict, "forward", last_csearch_forward());
4151 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004152 }
4153}
4154
4155/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004156 * "getenv()" function
4157 */
4158 static void
4159f_getenv(typval_T *argvars, typval_T *rettv)
4160{
4161 int mustfree = FALSE;
4162 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4163
4164 if (p == NULL)
4165 {
4166 rettv->v_type = VAR_SPECIAL;
4167 rettv->vval.v_number = VVAL_NULL;
4168 return;
4169 }
4170 if (!mustfree)
4171 p = vim_strsave(p);
4172 rettv->vval.v_string = p;
4173 rettv->v_type = VAR_STRING;
4174}
4175
4176/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004177 * "getfontname()" function
4178 */
4179 static void
4180f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4181{
4182 rettv->v_type = VAR_STRING;
4183 rettv->vval.v_string = NULL;
4184#ifdef FEAT_GUI
4185 if (gui.in_use)
4186 {
4187 GuiFont font;
4188 char_u *name = NULL;
4189
4190 if (argvars[0].v_type == VAR_UNKNOWN)
4191 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004192 // Get the "Normal" font. Either the name saved by
4193 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004194 font = gui.norm_font;
4195 name = hl_get_font_name();
4196 }
4197 else
4198 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004199 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004200 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004201 return;
4202 font = gui_mch_get_font(name, FALSE);
4203 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004204 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004205 }
4206 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4207 if (argvars[0].v_type != VAR_UNKNOWN)
4208 gui_mch_free_font(font);
4209 }
4210#endif
4211}
4212
4213/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004214 * "getjumplist()" function
4215 */
4216 static void
4217f_getjumplist(typval_T *argvars, typval_T *rettv)
4218{
4219#ifdef FEAT_JUMPLIST
4220 win_T *wp;
4221 int i;
4222 list_T *l;
4223 dict_T *d;
4224#endif
4225
4226 if (rettv_list_alloc(rettv) != OK)
4227 return;
4228
4229#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004230 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004231 if (wp == NULL)
4232 return;
4233
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004234 cleanup_jumplist(wp, TRUE);
4235
Bram Moolenaar4f505882018-02-10 21:06:32 +01004236 l = list_alloc();
4237 if (l == NULL)
4238 return;
4239
4240 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4241 return;
4242 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4243
4244 for (i = 0; i < wp->w_jumplistlen; ++i)
4245 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004246 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4247 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004248 if ((d = dict_alloc()) == NULL)
4249 return;
4250 if (list_append_dict(l, d) == FAIL)
4251 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004252 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4253 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004254 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004255 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004256 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004257 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004258 }
4259#endif
4260}
4261
4262/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004263 * "getpid()" function
4264 */
4265 static void
4266f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4267{
4268 rettv->vval.v_number = mch_get_pid();
4269}
4270
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004271/*
4272 * "getcurpos()" function
4273 */
4274 static void
4275f_getcurpos(typval_T *argvars, typval_T *rettv)
4276{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004277 getpos_both(argvars, rettv, TRUE, FALSE);
4278}
4279
4280 static void
4281f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4282{
4283 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004284}
4285
4286/*
4287 * "getpos(string)" function
4288 */
4289 static void
4290f_getpos(typval_T *argvars, typval_T *rettv)
4291{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004292 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004293}
4294
4295/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004296 * "getreg()" function
4297 */
4298 static void
4299f_getreg(typval_T *argvars, typval_T *rettv)
4300{
4301 char_u *strregname;
4302 int regname;
4303 int arg2 = FALSE;
4304 int return_list = FALSE;
4305 int error = FALSE;
4306
4307 if (argvars[0].v_type != VAR_UNKNOWN)
4308 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004309 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004310 if (strregname == NULL)
4311 error = TRUE;
4312 else if (in_vim9script() && STRLEN(strregname) > 1)
4313 {
4314 semsg(_(e_register_name_must_be_one_char_str), strregname);
4315 error = TRUE;
4316 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004317 if (argvars[1].v_type != VAR_UNKNOWN)
4318 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004319 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004320 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004321 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004322 }
4323 }
4324 else
4325 strregname = get_vim_var_str(VV_REG);
4326
4327 if (error)
4328 return;
4329
4330 regname = (strregname == NULL ? '"' : *strregname);
4331 if (regname == 0)
4332 regname = '"';
4333
4334 if (return_list)
4335 {
4336 rettv->v_type = VAR_LIST;
4337 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4338 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4339 if (rettv->vval.v_list == NULL)
4340 (void)rettv_list_alloc(rettv);
4341 else
4342 ++rettv->vval.v_list->lv_refcount;
4343 }
4344 else
4345 {
4346 rettv->v_type = VAR_STRING;
4347 rettv->vval.v_string = get_reg_contents(regname,
4348 arg2 ? GREG_EXPR_SRC : 0);
4349 }
4350}
4351
4352/*
4353 * "getregtype()" function
4354 */
4355 static void
4356f_getregtype(typval_T *argvars, typval_T *rettv)
4357{
4358 char_u *strregname;
4359 int regname;
4360 char_u buf[NUMBUFLEN + 2];
4361 long reglen = 0;
4362
4363 if (argvars[0].v_type != VAR_UNKNOWN)
4364 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004365 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004366 if (strregname != NULL && in_vim9script() && STRLEN(strregname) > 1)
4367 {
4368 semsg(_(e_register_name_must_be_one_char_str), strregname);
4369 strregname = NULL;
4370 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004371 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004372 {
4373 rettv->v_type = VAR_STRING;
4374 rettv->vval.v_string = NULL;
4375 return;
4376 }
4377 }
4378 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004379 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004380 strregname = get_vim_var_str(VV_REG);
4381
4382 regname = (strregname == NULL ? '"' : *strregname);
4383 if (regname == 0)
4384 regname = '"';
4385
4386 buf[0] = NUL;
4387 buf[1] = NUL;
4388 switch (get_reg_type(regname, &reglen))
4389 {
4390 case MLINE: buf[0] = 'V'; break;
4391 case MCHAR: buf[0] = 'v'; break;
4392 case MBLOCK:
4393 buf[0] = Ctrl_V;
4394 sprintf((char *)buf + 1, "%ld", reglen + 1);
4395 break;
4396 }
4397 rettv->v_type = VAR_STRING;
4398 rettv->vval.v_string = vim_strsave(buf);
4399}
4400
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004401/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004402 * "gettagstack()" function
4403 */
4404 static void
4405f_gettagstack(typval_T *argvars, typval_T *rettv)
4406{
4407 win_T *wp = curwin; // default is current window
4408
4409 if (rettv_dict_alloc(rettv) != OK)
4410 return;
4411
4412 if (argvars[0].v_type != VAR_UNKNOWN)
4413 {
4414 wp = find_win_by_nr_or_id(&argvars[0]);
4415 if (wp == NULL)
4416 return;
4417 }
4418
4419 get_tagstack(wp, rettv->vval.v_dict);
4420}
4421
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004422/*
4423 * "gettext()" function
4424 */
4425 static void
4426f_gettext(typval_T *argvars, typval_T *rettv)
4427{
4428 if (argvars[0].v_type != VAR_STRING
4429 || argvars[0].vval.v_string == NULL
4430 || *argvars[0].vval.v_string == NUL)
4431 {
4432 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4433 }
4434 else
4435 {
4436 rettv->v_type = VAR_STRING;
4437 rettv->vval.v_string = vim_strsave(
4438 (char_u *)_(argvars[0].vval.v_string));
4439 }
4440}
4441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004442// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004443#include "version.h"
4444
4445/*
4446 * "has()" function
4447 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004448 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004449f_has(typval_T *argvars, typval_T *rettv)
4450{
4451 int i;
4452 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004453 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004454 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004455 typedef struct {
4456 char *name;
4457 short present;
4458 } has_item_T;
4459 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004460 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004461 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004462#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004463 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004464#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004465 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004466#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004467 },
4468 {"arp",
4469#if defined(AMIGA) && defined(FEAT_ARP)
4470 1
4471#else
4472 0
4473#endif
4474 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004475 {"haiku",
4476#ifdef __HAIKU__
4477 1
4478#else
4479 0
4480#endif
4481 },
4482 {"bsd",
4483#if defined(BSD) && !defined(MACOS_X)
4484 1
4485#else
4486 0
4487#endif
4488 },
4489 {"hpux",
4490#ifdef hpux
4491 1
4492#else
4493 0
4494#endif
4495 },
4496 {"linux",
4497#ifdef __linux__
4498 1
4499#else
4500 0
4501#endif
4502 },
4503 {"mac", // Mac OS X (and, once, Mac OS Classic)
4504#ifdef MACOS_X
4505 1
4506#else
4507 0
4508#endif
4509 },
4510 {"osx", // Mac OS X
4511#ifdef MACOS_X
4512 1
4513#else
4514 0
4515#endif
4516 },
4517 {"macunix", // Mac OS X, with the darwin feature
4518#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4519 1
4520#else
4521 0
4522#endif
4523 },
4524 {"osxdarwin", // synonym for macunix
4525#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4526 1
4527#else
4528 0
4529#endif
4530 },
4531 {"qnx",
4532#ifdef __QNX__
4533 1
4534#else
4535 0
4536#endif
4537 },
4538 {"sun",
4539#ifdef SUN_SYSTEM
4540 1
4541#else
4542 0
4543#endif
4544 },
4545 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004546#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004547 1
4548#else
4549 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004550#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004551 },
4552 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004553#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004554 1
4555#else
4556 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004557#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004558 },
4559 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004560#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004561 1
4562#else
4563 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004564#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004565 },
4566 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004567#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004568 1
4569#else
4570 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004571#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004572 },
4573 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004574#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004575 1
4576#else
4577 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004578#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004579 },
4580 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004581#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004582 1
4583#else
4584 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004585#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004586 },
4587 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004588#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004589 1
4590#else
4591 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004592#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004593 },
4594 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004595#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004596 1
4597#else
4598 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004599#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004600 },
4601 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004602#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004603 1
4604#else
4605 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004606#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004607 },
4608 {"autocmd", 1},
4609 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004610#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004611 1
4612#else
4613 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004614#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004615 },
4616 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004617#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004618 1
4619#else
4620 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004621#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004622 },
4623 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004624#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004625 1
4626#else
4627 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004628#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004629 },
4630 {"balloon_multiline",
4631#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4632 // MS-Windows requires runtime check, see below
4633 1
4634#else
4635 0
4636#endif
4637 },
4638 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004639#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004640 1
4641#else
4642 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004643#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004644 },
4645 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004646#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004647 1
4648#else
4649 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004650#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004651 },
4652 {"all_builtin_terms",
4653#if defined(ALL_BUILTIN_TCAPS)
4654 1
4655#else
4656 0
4657#endif
4658 },
4659 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004660#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004661 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004662 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004663 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004664#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004665 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004666#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004667 },
4668 {"byte_offset",
4669#ifdef FEAT_BYTEOFF
4670 1
4671#else
4672 0
4673#endif
4674 },
4675 {"channel",
4676#ifdef FEAT_JOB_CHANNEL
4677 1
4678#else
4679 0
4680#endif
4681 },
4682 {"cindent",
4683#ifdef FEAT_CINDENT
4684 1
4685#else
4686 0
4687#endif
4688 },
4689 {"clientserver",
4690#ifdef FEAT_CLIENTSERVER
4691 1
4692#else
4693 0
4694#endif
4695 },
4696 {"clipboard",
4697#ifdef FEAT_CLIPBOARD
4698 1
4699#else
4700 0
4701#endif
4702 },
4703 {"cmdline_compl", 1},
4704 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004705 {"cmdwin",
4706#ifdef FEAT_CMDWIN
4707 1
4708#else
4709 0
4710#endif
4711 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004712 {"comments", 1},
4713 {"conceal",
4714#ifdef FEAT_CONCEAL
4715 1
4716#else
4717 0
4718#endif
4719 },
4720 {"cryptv",
4721#ifdef FEAT_CRYPT
4722 1
4723#else
4724 0
4725#endif
4726 },
4727 {"crypt-blowfish",
4728#ifdef FEAT_CRYPT
4729 1
4730#else
4731 0
4732#endif
4733 },
4734 {"crypt-blowfish2",
4735#ifdef FEAT_CRYPT
4736 1
4737#else
4738 0
4739#endif
4740 },
4741 {"cscope",
4742#ifdef FEAT_CSCOPE
4743 1
4744#else
4745 0
4746#endif
4747 },
4748 {"cursorbind", 1},
4749 {"cursorshape",
4750#ifdef CURSOR_SHAPE
4751 1
4752#else
4753 0
4754#endif
4755 },
4756 {"debug",
4757#ifdef DEBUG
4758 1
4759#else
4760 0
4761#endif
4762 },
4763 {"dialog_con",
4764#ifdef FEAT_CON_DIALOG
4765 1
4766#else
4767 0
4768#endif
4769 },
4770 {"dialog_gui",
4771#ifdef FEAT_GUI_DIALOG
4772 1
4773#else
4774 0
4775#endif
4776 },
4777 {"diff",
4778#ifdef FEAT_DIFF
4779 1
4780#else
4781 0
4782#endif
4783 },
4784 {"digraphs",
4785#ifdef FEAT_DIGRAPHS
4786 1
4787#else
4788 0
4789#endif
4790 },
4791 {"directx",
4792#ifdef FEAT_DIRECTX
4793 1
4794#else
4795 0
4796#endif
4797 },
4798 {"dnd",
4799#ifdef FEAT_DND
4800 1
4801#else
4802 0
4803#endif
4804 },
4805 {"emacs_tags",
4806#ifdef FEAT_EMACS_TAGS
4807 1
4808#else
4809 0
4810#endif
4811 },
4812 {"eval", 1}, // always present, of course!
4813 {"ex_extra", 1}, // graduated feature
4814 {"extra_search",
4815#ifdef FEAT_SEARCH_EXTRA
4816 1
4817#else
4818 0
4819#endif
4820 },
4821 {"file_in_path",
4822#ifdef FEAT_SEARCHPATH
4823 1
4824#else
4825 0
4826#endif
4827 },
4828 {"filterpipe",
4829#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4830 1
4831#else
4832 0
4833#endif
4834 },
4835 {"find_in_path",
4836#ifdef FEAT_FIND_ID
4837 1
4838#else
4839 0
4840#endif
4841 },
4842 {"float",
4843#ifdef FEAT_FLOAT
4844 1
4845#else
4846 0
4847#endif
4848 },
4849 {"folding",
4850#ifdef FEAT_FOLDING
4851 1
4852#else
4853 0
4854#endif
4855 },
4856 {"footer",
4857#ifdef FEAT_FOOTER
4858 1
4859#else
4860 0
4861#endif
4862 },
4863 {"fork",
4864#if !defined(USE_SYSTEM) && defined(UNIX)
4865 1
4866#else
4867 0
4868#endif
4869 },
4870 {"gettext",
4871#ifdef FEAT_GETTEXT
4872 1
4873#else
4874 0
4875#endif
4876 },
4877 {"gui",
4878#ifdef FEAT_GUI
4879 1
4880#else
4881 0
4882#endif
4883 },
4884 {"gui_neXtaw",
4885#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4886 1
4887#else
4888 0
4889#endif
4890 },
4891 {"gui_athena",
4892#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4893 1
4894#else
4895 0
4896#endif
4897 },
4898 {"gui_gtk",
4899#ifdef FEAT_GUI_GTK
4900 1
4901#else
4902 0
4903#endif
4904 },
4905 {"gui_gtk2",
4906#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4907 1
4908#else
4909 0
4910#endif
4911 },
4912 {"gui_gtk3",
4913#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4914 1
4915#else
4916 0
4917#endif
4918 },
4919 {"gui_gnome",
4920#ifdef FEAT_GUI_GNOME
4921 1
4922#else
4923 0
4924#endif
4925 },
4926 {"gui_haiku",
4927#ifdef FEAT_GUI_HAIKU
4928 1
4929#else
4930 0
4931#endif
4932 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004933 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004934 {"gui_motif",
4935#ifdef FEAT_GUI_MOTIF
4936 1
4937#else
4938 0
4939#endif
4940 },
4941 {"gui_photon",
4942#ifdef FEAT_GUI_PHOTON
4943 1
4944#else
4945 0
4946#endif
4947 },
4948 {"gui_win32",
4949#ifdef FEAT_GUI_MSWIN
4950 1
4951#else
4952 0
4953#endif
4954 },
4955 {"iconv",
4956#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4957 1
4958#else
4959 0
4960#endif
4961 },
4962 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004963 {"ipv6",
4964#ifdef FEAT_IPV6
4965 1
4966#else
4967 0
4968#endif
4969 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004970 {"job",
4971#ifdef FEAT_JOB_CHANNEL
4972 1
4973#else
4974 0
4975#endif
4976 },
4977 {"jumplist",
4978#ifdef FEAT_JUMPLIST
4979 1
4980#else
4981 0
4982#endif
4983 },
4984 {"keymap",
4985#ifdef FEAT_KEYMAP
4986 1
4987#else
4988 0
4989#endif
4990 },
4991 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4992 {"langmap",
4993#ifdef FEAT_LANGMAP
4994 1
4995#else
4996 0
4997#endif
4998 },
4999 {"libcall",
5000#ifdef FEAT_LIBCALL
5001 1
5002#else
5003 0
5004#endif
5005 },
5006 {"linebreak",
5007#ifdef FEAT_LINEBREAK
5008 1
5009#else
5010 0
5011#endif
5012 },
5013 {"lispindent",
5014#ifdef FEAT_LISP
5015 1
5016#else
5017 0
5018#endif
5019 },
5020 {"listcmds", 1},
5021 {"localmap", 1},
5022 {"lua",
5023#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
5024 1
5025#else
5026 0
5027#endif
5028 },
5029 {"menu",
5030#ifdef FEAT_MENU
5031 1
5032#else
5033 0
5034#endif
5035 },
5036 {"mksession",
5037#ifdef FEAT_SESSION
5038 1
5039#else
5040 0
5041#endif
5042 },
5043 {"modify_fname", 1},
5044 {"mouse", 1},
5045 {"mouseshape",
5046#ifdef FEAT_MOUSESHAPE
5047 1
5048#else
5049 0
5050#endif
5051 },
5052 {"mouse_dec",
5053#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
5054 1
5055#else
5056 0
5057#endif
5058 },
5059 {"mouse_gpm",
5060#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
5061 1
5062#else
5063 0
5064#endif
5065 },
5066 {"mouse_jsbterm",
5067#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
5068 1
5069#else
5070 0
5071#endif
5072 },
5073 {"mouse_netterm",
5074#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
5075 1
5076#else
5077 0
5078#endif
5079 },
5080 {"mouse_pterm",
5081#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
5082 1
5083#else
5084 0
5085#endif
5086 },
5087 {"mouse_sgr",
5088#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5089 1
5090#else
5091 0
5092#endif
5093 },
5094 {"mouse_sysmouse",
5095#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
5096 1
5097#else
5098 0
5099#endif
5100 },
5101 {"mouse_urxvt",
5102#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
5103 1
5104#else
5105 0
5106#endif
5107 },
5108 {"mouse_xterm",
5109#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5110 1
5111#else
5112 0
5113#endif
5114 },
5115 {"multi_byte", 1},
5116 {"multi_byte_ime",
5117#ifdef FEAT_MBYTE_IME
5118 1
5119#else
5120 0
5121#endif
5122 },
5123 {"multi_lang",
5124#ifdef FEAT_MULTI_LANG
5125 1
5126#else
5127 0
5128#endif
5129 },
5130 {"mzscheme",
5131#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
5132 1
5133#else
5134 0
5135#endif
5136 },
5137 {"num64", 1},
5138 {"ole",
5139#ifdef FEAT_OLE
5140 1
5141#else
5142 0
5143#endif
5144 },
5145 {"packages",
5146#ifdef FEAT_EVAL
5147 1
5148#else
5149 0
5150#endif
5151 },
5152 {"path_extra",
5153#ifdef FEAT_PATH_EXTRA
5154 1
5155#else
5156 0
5157#endif
5158 },
5159 {"perl",
5160#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
5161 1
5162#else
5163 0
5164#endif
5165 },
5166 {"persistent_undo",
5167#ifdef FEAT_PERSISTENT_UNDO
5168 1
5169#else
5170 0
5171#endif
5172 },
5173 {"python_compiled",
5174#if defined(FEAT_PYTHON)
5175 1
5176#else
5177 0
5178#endif
5179 },
5180 {"python_dynamic",
5181#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
5182 1
5183#else
5184 0
5185#endif
5186 },
5187 {"python",
5188#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
5189 1
5190#else
5191 0
5192#endif
5193 },
5194 {"pythonx",
5195#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
5196 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
5197 1
5198#else
5199 0
5200#endif
5201 },
5202 {"python3_compiled",
5203#if defined(FEAT_PYTHON3)
5204 1
5205#else
5206 0
5207#endif
5208 },
5209 {"python3_dynamic",
5210#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
5211 1
5212#else
5213 0
5214#endif
5215 },
5216 {"python3",
5217#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
5218 1
5219#else
5220 0
5221#endif
5222 },
5223 {"popupwin",
5224#ifdef FEAT_PROP_POPUP
5225 1
5226#else
5227 0
5228#endif
5229 },
5230 {"postscript",
5231#ifdef FEAT_POSTSCRIPT
5232 1
5233#else
5234 0
5235#endif
5236 },
5237 {"printer",
5238#ifdef FEAT_PRINTER
5239 1
5240#else
5241 0
5242#endif
5243 },
5244 {"profile",
5245#ifdef FEAT_PROFILE
5246 1
5247#else
5248 0
5249#endif
5250 },
5251 {"reltime",
5252#ifdef FEAT_RELTIME
5253 1
5254#else
5255 0
5256#endif
5257 },
5258 {"quickfix",
5259#ifdef FEAT_QUICKFIX
5260 1
5261#else
5262 0
5263#endif
5264 },
5265 {"rightleft",
5266#ifdef FEAT_RIGHTLEFT
5267 1
5268#else
5269 0
5270#endif
5271 },
5272 {"ruby",
5273#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5274 1
5275#else
5276 0
5277#endif
5278 },
5279 {"scrollbind", 1},
5280 {"showcmd",
5281#ifdef FEAT_CMDL_INFO
5282 1
5283#else
5284 0
5285#endif
5286 },
5287 {"cmdline_info",
5288#ifdef FEAT_CMDL_INFO
5289 1
5290#else
5291 0
5292#endif
5293 },
5294 {"signs",
5295#ifdef FEAT_SIGNS
5296 1
5297#else
5298 0
5299#endif
5300 },
5301 {"smartindent",
5302#ifdef FEAT_SMARTINDENT
5303 1
5304#else
5305 0
5306#endif
5307 },
5308 {"startuptime",
5309#ifdef STARTUPTIME
5310 1
5311#else
5312 0
5313#endif
5314 },
5315 {"statusline",
5316#ifdef FEAT_STL_OPT
5317 1
5318#else
5319 0
5320#endif
5321 },
5322 {"netbeans_intg",
5323#ifdef FEAT_NETBEANS_INTG
5324 1
5325#else
5326 0
5327#endif
5328 },
5329 {"sound",
5330#ifdef FEAT_SOUND
5331 1
5332#else
5333 0
5334#endif
5335 },
5336 {"spell",
5337#ifdef FEAT_SPELL
5338 1
5339#else
5340 0
5341#endif
5342 },
5343 {"syntax",
5344#ifdef FEAT_SYN_HL
5345 1
5346#else
5347 0
5348#endif
5349 },
5350 {"system",
5351#if defined(USE_SYSTEM) || !defined(UNIX)
5352 1
5353#else
5354 0
5355#endif
5356 },
5357 {"tag_binary",
5358#ifdef FEAT_TAG_BINS
5359 1
5360#else
5361 0
5362#endif
5363 },
5364 {"tcl",
5365#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5366 1
5367#else
5368 0
5369#endif
5370 },
5371 {"termguicolors",
5372#ifdef FEAT_TERMGUICOLORS
5373 1
5374#else
5375 0
5376#endif
5377 },
5378 {"terminal",
5379#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5380 1
5381#else
5382 0
5383#endif
5384 },
5385 {"terminfo",
5386#ifdef TERMINFO
5387 1
5388#else
5389 0
5390#endif
5391 },
5392 {"termresponse",
5393#ifdef FEAT_TERMRESPONSE
5394 1
5395#else
5396 0
5397#endif
5398 },
5399 {"textobjects",
5400#ifdef FEAT_TEXTOBJ
5401 1
5402#else
5403 0
5404#endif
5405 },
5406 {"textprop",
5407#ifdef FEAT_PROP_POPUP
5408 1
5409#else
5410 0
5411#endif
5412 },
5413 {"tgetent",
5414#ifdef HAVE_TGETENT
5415 1
5416#else
5417 0
5418#endif
5419 },
5420 {"timers",
5421#ifdef FEAT_TIMERS
5422 1
5423#else
5424 0
5425#endif
5426 },
5427 {"title",
5428#ifdef FEAT_TITLE
5429 1
5430#else
5431 0
5432#endif
5433 },
5434 {"toolbar",
5435#ifdef FEAT_TOOLBAR
5436 1
5437#else
5438 0
5439#endif
5440 },
5441 {"unnamedplus",
5442#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5443 1
5444#else
5445 0
5446#endif
5447 },
5448 {"user-commands", 1}, // was accidentally included in 5.4
5449 {"user_commands", 1},
5450 {"vartabs",
5451#ifdef FEAT_VARTABS
5452 1
5453#else
5454 0
5455#endif
5456 },
5457 {"vertsplit", 1},
5458 {"viminfo",
5459#ifdef FEAT_VIMINFO
5460 1
5461#else
5462 0
5463#endif
5464 },
5465 {"vimscript-1", 1},
5466 {"vimscript-2", 1},
5467 {"vimscript-3", 1},
5468 {"vimscript-4", 1},
5469 {"virtualedit", 1},
5470 {"visual", 1},
5471 {"visualextra", 1},
5472 {"vreplace", 1},
5473 {"vtp",
5474#ifdef FEAT_VTP
5475 1
5476#else
5477 0
5478#endif
5479 },
5480 {"wildignore",
5481#ifdef FEAT_WILDIGN
5482 1
5483#else
5484 0
5485#endif
5486 },
5487 {"wildmenu",
5488#ifdef FEAT_WILDMENU
5489 1
5490#else
5491 0
5492#endif
5493 },
5494 {"windows", 1},
5495 {"winaltkeys",
5496#ifdef FEAT_WAK
5497 1
5498#else
5499 0
5500#endif
5501 },
5502 {"writebackup",
5503#ifdef FEAT_WRITEBACKUP
5504 1
5505#else
5506 0
5507#endif
5508 },
5509 {"xim",
5510#ifdef FEAT_XIM
5511 1
5512#else
5513 0
5514#endif
5515 },
5516 {"xfontset",
5517#ifdef FEAT_XFONTSET
5518 1
5519#else
5520 0
5521#endif
5522 },
5523 {"xpm",
5524#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5525 1
5526#else
5527 0
5528#endif
5529 },
5530 {"xpm_w32", // for backward compatibility
5531#ifdef FEAT_XPM_W32
5532 1
5533#else
5534 0
5535#endif
5536 },
5537 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005538#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005539 1
5540#else
5541 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005542#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005543 },
5544 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005545#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005546 1
5547#else
5548 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005549#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005550 },
5551 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005552#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005553 1
5554#else
5555 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005556#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005557 },
5558 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005559#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005560 1
5561#else
5562 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005563#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005564 },
5565 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005566#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005567 1
5568#else
5569 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005570#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005571 },
5572 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005573 };
5574
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005575 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005576 for (i = 0; has_list[i].name != NULL; ++i)
5577 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005578 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005579 x = TRUE;
5580 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005581 break;
5582 }
5583
Bram Moolenaar79296512020-03-22 16:17:14 +01005584 // features also in has_list[] but sometimes enabled at runtime
5585 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005586 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005587 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005588 {
5589 // intentionally empty
5590 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005591#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005592 else if (STRICMP(name, "balloon_multiline") == 0)
5593 n = multiline_balloon_available();
5594#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005595#ifdef VIMDLL
5596 else if (STRICMP(name, "filterpipe") == 0)
5597 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005598#endif
5599#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5600 else if (STRICMP(name, "iconv") == 0)
5601 n = iconv_enabled(FALSE);
5602#endif
5603#ifdef DYNAMIC_LUA
5604 else if (STRICMP(name, "lua") == 0)
5605 n = lua_enabled(FALSE);
5606#endif
5607#ifdef DYNAMIC_MZSCHEME
5608 else if (STRICMP(name, "mzscheme") == 0)
5609 n = mzscheme_enabled(FALSE);
5610#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005611#ifdef DYNAMIC_PERL
5612 else if (STRICMP(name, "perl") == 0)
5613 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005614#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005615#ifdef DYNAMIC_PYTHON
5616 else if (STRICMP(name, "python") == 0)
5617 n = python_enabled(FALSE);
5618#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005619#ifdef DYNAMIC_PYTHON3
5620 else if (STRICMP(name, "python3") == 0)
5621 n = python3_enabled(FALSE);
5622#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005623#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5624 else if (STRICMP(name, "pythonx") == 0)
5625 {
5626# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5627 if (p_pyx == 0)
5628 n = python3_enabled(FALSE) || python_enabled(FALSE);
5629 else if (p_pyx == 3)
5630 n = python3_enabled(FALSE);
5631 else if (p_pyx == 2)
5632 n = python_enabled(FALSE);
5633# elif defined(DYNAMIC_PYTHON)
5634 n = python_enabled(FALSE);
5635# elif defined(DYNAMIC_PYTHON3)
5636 n = python3_enabled(FALSE);
5637# endif
5638 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005639#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005640#ifdef DYNAMIC_RUBY
5641 else if (STRICMP(name, "ruby") == 0)
5642 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005643#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005644#ifdef DYNAMIC_TCL
5645 else if (STRICMP(name, "tcl") == 0)
5646 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005647#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005648#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005649 else if (STRICMP(name, "terminal") == 0)
5650 n = terminal_enabled();
5651#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005652 }
5653
Bram Moolenaar79296512020-03-22 16:17:14 +01005654 // features not in has_list[]
5655 if (x == FALSE)
5656 {
5657 if (STRNICMP(name, "patch", 5) == 0)
5658 {
5659 x = TRUE;
5660 if (name[5] == '-'
5661 && STRLEN(name) >= 11
5662 && vim_isdigit(name[6])
5663 && vim_isdigit(name[8])
5664 && vim_isdigit(name[10]))
5665 {
5666 int major = atoi((char *)name + 6);
5667 int minor = atoi((char *)name + 8);
5668
5669 // Expect "patch-9.9.01234".
5670 n = (major < VIM_VERSION_MAJOR
5671 || (major == VIM_VERSION_MAJOR
5672 && (minor < VIM_VERSION_MINOR
5673 || (minor == VIM_VERSION_MINOR
5674 && has_patch(atoi((char *)name + 10))))));
5675 }
5676 else
5677 n = has_patch(atoi((char *)name + 5));
5678 }
5679 else if (STRICMP(name, "vim_starting") == 0)
5680 {
5681 x = TRUE;
5682 n = (starting != 0);
5683 }
5684 else if (STRICMP(name, "ttyin") == 0)
5685 {
5686 x = TRUE;
5687 n = mch_input_isatty();
5688 }
5689 else if (STRICMP(name, "ttyout") == 0)
5690 {
5691 x = TRUE;
5692 n = stdout_isatty;
5693 }
5694 else if (STRICMP(name, "multi_byte_encoding") == 0)
5695 {
5696 x = TRUE;
5697 n = has_mbyte;
5698 }
5699 else if (STRICMP(name, "gui_running") == 0)
5700 {
5701 x = TRUE;
5702#ifdef FEAT_GUI
5703 n = (gui.in_use || gui.starting);
5704#endif
5705 }
5706 else if (STRICMP(name, "browse") == 0)
5707 {
5708 x = TRUE;
5709#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5710 n = gui.in_use; // gui_mch_browse() works when GUI is running
5711#endif
5712 }
5713 else if (STRICMP(name, "syntax_items") == 0)
5714 {
5715 x = TRUE;
5716#ifdef FEAT_SYN_HL
5717 n = syntax_present(curwin);
5718#endif
5719 }
5720 else if (STRICMP(name, "vcon") == 0)
5721 {
5722 x = TRUE;
5723#ifdef FEAT_VTP
5724 n = is_term_win32() && has_vtp_working();
5725#endif
5726 }
5727 else if (STRICMP(name, "netbeans_enabled") == 0)
5728 {
5729 x = TRUE;
5730#ifdef FEAT_NETBEANS_INTG
5731 n = netbeans_active();
5732#endif
5733 }
5734 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5735 {
5736 x = TRUE;
5737#ifdef FEAT_MOUSE_GPM
5738 n = gpm_enabled();
5739#endif
5740 }
5741 else if (STRICMP(name, "conpty") == 0)
5742 {
5743 x = TRUE;
5744#if defined(FEAT_TERMINAL) && defined(MSWIN)
5745 n = use_conpty();
5746#endif
5747 }
5748 else if (STRICMP(name, "clipboard_working") == 0)
5749 {
5750 x = TRUE;
5751#ifdef FEAT_CLIPBOARD
5752 n = clip_star.available;
5753#endif
5754 }
5755 }
5756
Bram Moolenaar04637e22020-09-05 18:45:29 +02005757 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005758 // return whether feature could ever be enabled
5759 rettv->vval.v_number = x;
5760 else
5761 // return whether feature is enabled
5762 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005763}
5764
5765/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005766 * Return TRUE if "feature" can change later.
5767 * Also when checking for the feature has side effects, such as loading a DLL.
5768 */
5769 int
5770dynamic_feature(char_u *feature)
5771{
5772 return (feature == NULL
5773#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5774 || STRICMP(feature, "balloon_multiline") == 0
5775#endif
5776#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5777 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5778#endif
5779#ifdef VIMDLL
5780 || STRICMP(feature, "filterpipe") == 0
5781#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005782#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005783 // this can only change on Unix where the ":gui" command could be
5784 // used.
5785 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5786#endif
5787#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5788 || STRICMP(feature, "iconv") == 0
5789#endif
5790#ifdef DYNAMIC_LUA
5791 || STRICMP(feature, "lua") == 0
5792#endif
5793#ifdef FEAT_MOUSE_GPM
5794 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5795#endif
5796#ifdef DYNAMIC_MZSCHEME
5797 || STRICMP(feature, "mzscheme") == 0
5798#endif
5799#ifdef FEAT_NETBEANS_INTG
5800 || STRICMP(feature, "netbeans_enabled") == 0
5801#endif
5802#ifdef DYNAMIC_PERL
5803 || STRICMP(feature, "perl") == 0
5804#endif
5805#ifdef DYNAMIC_PYTHON
5806 || STRICMP(feature, "python") == 0
5807#endif
5808#ifdef DYNAMIC_PYTHON3
5809 || STRICMP(feature, "python3") == 0
5810#endif
5811#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5812 || STRICMP(feature, "pythonx") == 0
5813#endif
5814#ifdef DYNAMIC_RUBY
5815 || STRICMP(feature, "ruby") == 0
5816#endif
5817#ifdef FEAT_SYN_HL
5818 || STRICMP(feature, "syntax_items") == 0
5819#endif
5820#ifdef DYNAMIC_TCL
5821 || STRICMP(feature, "tcl") == 0
5822#endif
5823 // once "starting" is zero it will stay that way
5824 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5825 || STRICMP(feature, "multi_byte_encoding") == 0
5826#if defined(FEAT_TERMINAL) && defined(MSWIN)
5827 || STRICMP(feature, "conpty") == 0
5828#endif
5829 );
5830}
5831
5832/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005833 * "haslocaldir()" function
5834 */
5835 static void
5836f_haslocaldir(typval_T *argvars, typval_T *rettv)
5837{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005838 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005839 win_T *wp = NULL;
5840
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005841 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5842
5843 // Check for window-local and tab-local directories
5844 if (wp != NULL && wp->w_localdir != NULL)
5845 rettv->vval.v_number = 1;
5846 else if (tp != NULL && tp->tp_localdir != NULL)
5847 rettv->vval.v_number = 2;
5848 else
5849 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005850}
5851
5852/*
5853 * "hasmapto()" function
5854 */
5855 static void
5856f_hasmapto(typval_T *argvars, typval_T *rettv)
5857{
5858 char_u *name;
5859 char_u *mode;
5860 char_u buf[NUMBUFLEN];
5861 int abbr = FALSE;
5862
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005863 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005864 if (argvars[1].v_type == VAR_UNKNOWN)
5865 mode = (char_u *)"nvo";
5866 else
5867 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005868 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005869 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005870 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005871 }
5872
5873 if (map_to_exists(name, mode, abbr))
5874 rettv->vval.v_number = TRUE;
5875 else
5876 rettv->vval.v_number = FALSE;
5877}
5878
5879/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005880 * "highlightID(name)" function
5881 */
5882 static void
5883f_hlID(typval_T *argvars, typval_T *rettv)
5884{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005885 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005886}
5887
5888/*
5889 * "highlight_exists()" function
5890 */
5891 static void
5892f_hlexists(typval_T *argvars, typval_T *rettv)
5893{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005894 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005895}
5896
5897/*
5898 * "hostname()" function
5899 */
5900 static void
5901f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5902{
5903 char_u hostname[256];
5904
5905 mch_get_host_name(hostname, 256);
5906 rettv->v_type = VAR_STRING;
5907 rettv->vval.v_string = vim_strsave(hostname);
5908}
5909
5910/*
5911 * iconv() function
5912 */
5913 static void
5914f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5915{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005916 char_u buf1[NUMBUFLEN];
5917 char_u buf2[NUMBUFLEN];
5918 char_u *from, *to, *str;
5919 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005920
5921 rettv->v_type = VAR_STRING;
5922 rettv->vval.v_string = NULL;
5923
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005924 str = tv_get_string(&argvars[0]);
5925 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5926 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005927 vimconv.vc_type = CONV_NONE;
5928 convert_setup(&vimconv, from, to);
5929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005930 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005931 if (vimconv.vc_type == CONV_NONE)
5932 rettv->vval.v_string = vim_strsave(str);
5933 else
5934 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5935
5936 convert_setup(&vimconv, NULL, NULL);
5937 vim_free(from);
5938 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005939}
5940
5941/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005942 * "index()" function
5943 */
5944 static void
5945f_index(typval_T *argvars, typval_T *rettv)
5946{
5947 list_T *l;
5948 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005949 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005950 long idx = 0;
5951 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005952 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005953
5954 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005955 if (argvars[0].v_type == VAR_BLOB)
5956 {
5957 typval_T tv;
5958 int start = 0;
5959
5960 if (argvars[2].v_type != VAR_UNKNOWN)
5961 {
5962 start = tv_get_number_chk(&argvars[2], &error);
5963 if (error)
5964 return;
5965 }
5966 b = argvars[0].vval.v_blob;
5967 if (b == NULL)
5968 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005969 if (start < 0)
5970 {
5971 start = blob_len(b) + start;
5972 if (start < 0)
5973 start = 0;
5974 }
5975
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005976 for (idx = start; idx < blob_len(b); ++idx)
5977 {
5978 tv.v_type = VAR_NUMBER;
5979 tv.vval.v_number = blob_get(b, idx);
5980 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5981 {
5982 rettv->vval.v_number = idx;
5983 return;
5984 }
5985 }
5986 return;
5987 }
5988 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005989 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005990 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005991 return;
5992 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005993
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005994 l = argvars[0].vval.v_list;
5995 if (l != NULL)
5996 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005997 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005998 item = l->lv_first;
5999 if (argvars[2].v_type != VAR_UNKNOWN)
6000 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006001 // Start at specified item. Use the cached index that list_find()
6002 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006003 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006004 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006005 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02006006 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006007 if (error)
6008 item = NULL;
6009 }
6010
6011 for ( ; item != NULL; item = item->li_next, ++idx)
6012 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6013 {
6014 rettv->vval.v_number = idx;
6015 break;
6016 }
6017 }
6018}
6019
6020static int inputsecret_flag = 0;
6021
6022/*
6023 * "input()" function
6024 * Also handles inputsecret() when inputsecret is set.
6025 */
6026 static void
6027f_input(typval_T *argvars, typval_T *rettv)
6028{
6029 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6030}
6031
6032/*
6033 * "inputdialog()" function
6034 */
6035 static void
6036f_inputdialog(typval_T *argvars, typval_T *rettv)
6037{
6038#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006039 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006040 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6041 {
6042 char_u *message;
6043 char_u buf[NUMBUFLEN];
6044 char_u *defstr = (char_u *)"";
6045
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006046 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006047 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006048 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006049 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6050 else
6051 IObuff[0] = NUL;
6052 if (message != NULL && defstr != NULL
6053 && do_dialog(VIM_QUESTION, NULL, message,
6054 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6055 rettv->vval.v_string = vim_strsave(IObuff);
6056 else
6057 {
6058 if (message != NULL && defstr != NULL
6059 && argvars[1].v_type != VAR_UNKNOWN
6060 && argvars[2].v_type != VAR_UNKNOWN)
6061 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006062 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006063 else
6064 rettv->vval.v_string = NULL;
6065 }
6066 rettv->v_type = VAR_STRING;
6067 }
6068 else
6069#endif
6070 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6071}
6072
6073/*
6074 * "inputlist()" function
6075 */
6076 static void
6077f_inputlist(typval_T *argvars, typval_T *rettv)
6078{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006079 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006080 listitem_T *li;
6081 int selected;
6082 int mouse_used;
6083
6084#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006085 // While starting up, there is no place to enter text. When running tests
6086 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006087 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006088 return;
6089#endif
6090 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6091 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006092 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006093 return;
6094 }
6095
6096 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006097 msg_row = Rows - 1; // for when 'cmdheight' > 1
6098 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006099 msg_scroll = TRUE;
6100 msg_clr_eos();
6101
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006102 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006103 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006104 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006105 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006106 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006107 msg_putchar('\n');
6108 }
6109
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006110 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006111 selected = prompt_for_number(&mouse_used);
6112 if (mouse_used)
6113 selected -= lines_left;
6114
6115 rettv->vval.v_number = selected;
6116}
6117
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006118static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6119
6120/*
6121 * "inputrestore()" function
6122 */
6123 static void
6124f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6125{
6126 if (ga_userinput.ga_len > 0)
6127 {
6128 --ga_userinput.ga_len;
6129 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6130 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006131 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006132 }
6133 else if (p_verbose > 1)
6134 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006135 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006136 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006137 }
6138}
6139
6140/*
6141 * "inputsave()" function
6142 */
6143 static void
6144f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6145{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006146 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006147 if (ga_grow(&ga_userinput, 1) == OK)
6148 {
6149 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6150 + ga_userinput.ga_len);
6151 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006152 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006153 }
6154 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006155 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006156}
6157
6158/*
6159 * "inputsecret()" function
6160 */
6161 static void
6162f_inputsecret(typval_T *argvars, typval_T *rettv)
6163{
6164 ++cmdline_star;
6165 ++inputsecret_flag;
6166 f_input(argvars, rettv);
6167 --cmdline_star;
6168 --inputsecret_flag;
6169}
6170
6171/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01006172 * "interrupt()" function
6173 */
6174 static void
6175f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6176{
6177 got_int = TRUE;
6178}
6179
6180/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006181 * "invert(expr)" function
6182 */
6183 static void
6184f_invert(typval_T *argvars, typval_T *rettv)
6185{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006186 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006187}
6188
6189/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006190 * "islocked()" function
6191 */
6192 static void
6193f_islocked(typval_T *argvars, typval_T *rettv)
6194{
6195 lval_T lv;
6196 char_u *end;
6197 dictitem_T *di;
6198
6199 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006200 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006201 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006202 if (end != NULL && lv.ll_name != NULL)
6203 {
6204 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02006205 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006206 else
6207 {
6208 if (lv.ll_tv == NULL)
6209 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006210 di = find_var(lv.ll_name, NULL, TRUE);
6211 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006212 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006213 // Consider a variable locked when:
6214 // 1. the variable itself is locked
6215 // 2. the value of the variable is locked.
6216 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01006217 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6218 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006219 }
6220 }
6221 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006222 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006223 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006224 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006225 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006226 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006227 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6228 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006229 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006230 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6231 }
6232 }
6233
6234 clear_lval(&lv);
6235}
6236
6237#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6238/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006239 * "isinf()" function
6240 */
6241 static void
6242f_isinf(typval_T *argvars, typval_T *rettv)
6243{
6244 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6245 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6246}
6247
6248/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006249 * "isnan()" function
6250 */
6251 static void
6252f_isnan(typval_T *argvars, typval_T *rettv)
6253{
6254 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6255 && isnan(argvars[0].vval.v_float);
6256}
6257#endif
6258
6259/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006260 * "last_buffer_nr()" function.
6261 */
6262 static void
6263f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6264{
6265 int n = 0;
6266 buf_T *buf;
6267
Bram Moolenaar29323592016-07-24 22:04:11 +02006268 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006269 if (n < buf->b_fnum)
6270 n = buf->b_fnum;
6271
6272 rettv->vval.v_number = n;
6273}
6274
6275/*
6276 * "len()" function
6277 */
6278 static void
6279f_len(typval_T *argvars, typval_T *rettv)
6280{
6281 switch (argvars[0].v_type)
6282 {
6283 case VAR_STRING:
6284 case VAR_NUMBER:
6285 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006286 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006287 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006288 case VAR_BLOB:
6289 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6290 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006291 case VAR_LIST:
6292 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6293 break;
6294 case VAR_DICT:
6295 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6296 break;
6297 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006298 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006299 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006300 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006301 case VAR_SPECIAL:
6302 case VAR_FLOAT:
6303 case VAR_FUNC:
6304 case VAR_PARTIAL:
6305 case VAR_JOB:
6306 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006307 case VAR_INSTR:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006308 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006309 break;
6310 }
6311}
6312
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006313 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006314libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006315{
6316#ifdef FEAT_LIBCALL
6317 char_u *string_in;
6318 char_u **string_result;
6319 int nr_result;
6320#endif
6321
6322 rettv->v_type = type;
6323 if (type != VAR_NUMBER)
6324 rettv->vval.v_string = NULL;
6325
6326 if (check_restricted() || check_secure())
6327 return;
6328
6329#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006330 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006331 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6332 {
6333 string_in = NULL;
6334 if (argvars[2].v_type == VAR_STRING)
6335 string_in = argvars[2].vval.v_string;
6336 if (type == VAR_NUMBER)
6337 string_result = NULL;
6338 else
6339 string_result = &rettv->vval.v_string;
6340 if (mch_libcall(argvars[0].vval.v_string,
6341 argvars[1].vval.v_string,
6342 string_in,
6343 argvars[2].vval.v_number,
6344 string_result,
6345 &nr_result) == OK
6346 && type == VAR_NUMBER)
6347 rettv->vval.v_number = nr_result;
6348 }
6349#endif
6350}
6351
6352/*
6353 * "libcall()" function
6354 */
6355 static void
6356f_libcall(typval_T *argvars, typval_T *rettv)
6357{
6358 libcall_common(argvars, rettv, VAR_STRING);
6359}
6360
6361/*
6362 * "libcallnr()" function
6363 */
6364 static void
6365f_libcallnr(typval_T *argvars, typval_T *rettv)
6366{
6367 libcall_common(argvars, rettv, VAR_NUMBER);
6368}
6369
6370/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006371 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006372 */
6373 static void
6374f_line(typval_T *argvars, typval_T *rettv)
6375{
6376 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006377 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006378 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006379 int id;
6380 tabpage_T *tp;
6381 win_T *wp;
6382 win_T *save_curwin;
6383 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006384
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006385 if (argvars[1].v_type != VAR_UNKNOWN)
6386 {
6387 // use window specified in the second argument
6388 id = (int)tv_get_number(&argvars[1]);
6389 wp = win_id2wp_tp(id, &tp);
6390 if (wp != NULL && tp != NULL)
6391 {
6392 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6393 == OK)
6394 {
6395 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006396 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006397 }
6398 restore_win_noblock(save_curwin, save_curtab, TRUE);
6399 }
6400 }
6401 else
6402 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006403 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006404
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006405 if (fp != NULL)
6406 lnum = fp->lnum;
6407 rettv->vval.v_number = lnum;
6408}
6409
6410/*
6411 * "line2byte(lnum)" function
6412 */
6413 static void
6414f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6415{
6416#ifndef FEAT_BYTEOFF
6417 rettv->vval.v_number = -1;
6418#else
6419 linenr_T lnum;
6420
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006421 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006422 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6423 rettv->vval.v_number = -1;
6424 else
6425 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6426 if (rettv->vval.v_number >= 0)
6427 ++rettv->vval.v_number;
6428#endif
6429}
6430
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006431#ifdef FEAT_FLOAT
6432/*
6433 * "log()" function
6434 */
6435 static void
6436f_log(typval_T *argvars, typval_T *rettv)
6437{
6438 float_T f = 0.0;
6439
6440 rettv->v_type = VAR_FLOAT;
6441 if (get_float_arg(argvars, &f) == OK)
6442 rettv->vval.v_float = log(f);
6443 else
6444 rettv->vval.v_float = 0.0;
6445}
6446
6447/*
6448 * "log10()" function
6449 */
6450 static void
6451f_log10(typval_T *argvars, typval_T *rettv)
6452{
6453 float_T f = 0.0;
6454
6455 rettv->v_type = VAR_FLOAT;
6456 if (get_float_arg(argvars, &f) == OK)
6457 rettv->vval.v_float = log10(f);
6458 else
6459 rettv->vval.v_float = 0.0;
6460}
6461#endif
6462
6463#ifdef FEAT_LUA
6464/*
6465 * "luaeval()" function
6466 */
6467 static void
6468f_luaeval(typval_T *argvars, typval_T *rettv)
6469{
6470 char_u *str;
6471 char_u buf[NUMBUFLEN];
6472
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006473 if (check_restricted() || check_secure())
6474 return;
6475
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006476 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006477 do_luaeval(str, argvars + 1, rettv);
6478}
6479#endif
6480
6481/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006482 * "maparg()" function
6483 */
6484 static void
6485f_maparg(typval_T *argvars, typval_T *rettv)
6486{
6487 get_maparg(argvars, rettv, TRUE);
6488}
6489
6490/*
6491 * "mapcheck()" function
6492 */
6493 static void
6494f_mapcheck(typval_T *argvars, typval_T *rettv)
6495{
6496 get_maparg(argvars, rettv, FALSE);
6497}
6498
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006499typedef enum
6500{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006501 MATCH_END, // matchend()
6502 MATCH_MATCH, // match()
6503 MATCH_STR, // matchstr()
6504 MATCH_LIST, // matchlist()
6505 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006506} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006507
6508 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006509find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006510{
6511 char_u *str = NULL;
6512 long len = 0;
6513 char_u *expr = NULL;
6514 char_u *pat;
6515 regmatch_T regmatch;
6516 char_u patbuf[NUMBUFLEN];
6517 char_u strbuf[NUMBUFLEN];
6518 char_u *save_cpo;
6519 long start = 0;
6520 long nth = 1;
6521 colnr_T startcol = 0;
6522 int match = 0;
6523 list_T *l = NULL;
6524 listitem_T *li = NULL;
6525 long idx = 0;
6526 char_u *tofree = NULL;
6527
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006528 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006529 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006530 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006531
6532 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006533 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006534 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006535 // type MATCH_LIST: return empty list when there are no matches.
6536 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006537 if (rettv_list_alloc(rettv) == FAIL)
6538 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006539 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006540 && (list_append_string(rettv->vval.v_list,
6541 (char_u *)"", 0) == FAIL
6542 || list_append_number(rettv->vval.v_list,
6543 (varnumber_T)-1) == FAIL
6544 || list_append_number(rettv->vval.v_list,
6545 (varnumber_T)-1) == FAIL
6546 || list_append_number(rettv->vval.v_list,
6547 (varnumber_T)-1) == FAIL))
6548 {
6549 list_free(rettv->vval.v_list);
6550 rettv->vval.v_list = NULL;
6551 goto theend;
6552 }
6553 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006554 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006555 {
6556 rettv->v_type = VAR_STRING;
6557 rettv->vval.v_string = NULL;
6558 }
6559
6560 if (argvars[0].v_type == VAR_LIST)
6561 {
6562 if ((l = argvars[0].vval.v_list) == NULL)
6563 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006564 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006565 li = l->lv_first;
6566 }
6567 else
6568 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006569 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006570 len = (long)STRLEN(str);
6571 }
6572
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006573 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006574 if (pat == NULL)
6575 goto theend;
6576
6577 if (argvars[2].v_type != VAR_UNKNOWN)
6578 {
6579 int error = FALSE;
6580
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006581 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006582 if (error)
6583 goto theend;
6584 if (l != NULL)
6585 {
6586 li = list_find(l, start);
6587 if (li == NULL)
6588 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006589 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006590 }
6591 else
6592 {
6593 if (start < 0)
6594 start = 0;
6595 if (start > len)
6596 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006597 // When "count" argument is there ignore matches before "start",
6598 // otherwise skip part of the string. Differs when pattern is "^"
6599 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006600 if (argvars[3].v_type != VAR_UNKNOWN)
6601 startcol = start;
6602 else
6603 {
6604 str += start;
6605 len -= start;
6606 }
6607 }
6608
6609 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006610 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006611 if (error)
6612 goto theend;
6613 }
6614
6615 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6616 if (regmatch.regprog != NULL)
6617 {
6618 regmatch.rm_ic = p_ic;
6619
6620 for (;;)
6621 {
6622 if (l != NULL)
6623 {
6624 if (li == NULL)
6625 {
6626 match = FALSE;
6627 break;
6628 }
6629 vim_free(tofree);
6630 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6631 if (str == NULL)
6632 break;
6633 }
6634
6635 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6636
6637 if (match && --nth <= 0)
6638 break;
6639 if (l == NULL && !match)
6640 break;
6641
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006642 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006643 if (l != NULL)
6644 {
6645 li = li->li_next;
6646 ++idx;
6647 }
6648 else
6649 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006650 startcol = (colnr_T)(regmatch.startp[0]
6651 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006652 if (startcol > (colnr_T)len
6653 || str + startcol <= regmatch.startp[0])
6654 {
6655 match = FALSE;
6656 break;
6657 }
6658 }
6659 }
6660
6661 if (match)
6662 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006663 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006664 {
6665 listitem_T *li1 = rettv->vval.v_list->lv_first;
6666 listitem_T *li2 = li1->li_next;
6667 listitem_T *li3 = li2->li_next;
6668 listitem_T *li4 = li3->li_next;
6669
6670 vim_free(li1->li_tv.vval.v_string);
6671 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006672 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006673 li3->li_tv.vval.v_number =
6674 (varnumber_T)(regmatch.startp[0] - expr);
6675 li4->li_tv.vval.v_number =
6676 (varnumber_T)(regmatch.endp[0] - expr);
6677 if (l != NULL)
6678 li2->li_tv.vval.v_number = (varnumber_T)idx;
6679 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006680 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006681 {
6682 int i;
6683
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006684 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006685 for (i = 0; i < NSUBEXP; ++i)
6686 {
6687 if (regmatch.endp[i] == NULL)
6688 {
6689 if (list_append_string(rettv->vval.v_list,
6690 (char_u *)"", 0) == FAIL)
6691 break;
6692 }
6693 else if (list_append_string(rettv->vval.v_list,
6694 regmatch.startp[i],
6695 (int)(regmatch.endp[i] - regmatch.startp[i]))
6696 == FAIL)
6697 break;
6698 }
6699 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006700 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006701 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006702 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006703 if (l != NULL)
6704 copy_tv(&li->li_tv, rettv);
6705 else
6706 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006707 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006708 }
6709 else if (l != NULL)
6710 rettv->vval.v_number = idx;
6711 else
6712 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006713 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006714 rettv->vval.v_number =
6715 (varnumber_T)(regmatch.startp[0] - str);
6716 else
6717 rettv->vval.v_number =
6718 (varnumber_T)(regmatch.endp[0] - str);
6719 rettv->vval.v_number += (varnumber_T)(str - expr);
6720 }
6721 }
6722 vim_regfree(regmatch.regprog);
6723 }
6724
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006725theend:
6726 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006727 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006728 listitem_remove(rettv->vval.v_list,
6729 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006730 vim_free(tofree);
6731 p_cpo = save_cpo;
6732}
6733
6734/*
6735 * "match()" function
6736 */
6737 static void
6738f_match(typval_T *argvars, typval_T *rettv)
6739{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006740 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006741}
6742
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006743/*
6744 * "matchend()" function
6745 */
6746 static void
6747f_matchend(typval_T *argvars, typval_T *rettv)
6748{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006749 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006750}
6751
6752/*
6753 * "matchlist()" function
6754 */
6755 static void
6756f_matchlist(typval_T *argvars, typval_T *rettv)
6757{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006758 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006759}
6760
6761/*
6762 * "matchstr()" function
6763 */
6764 static void
6765f_matchstr(typval_T *argvars, typval_T *rettv)
6766{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006767 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006768}
6769
6770/*
6771 * "matchstrpos()" function
6772 */
6773 static void
6774f_matchstrpos(typval_T *argvars, typval_T *rettv)
6775{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006776 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006777}
6778
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006779 static void
6780max_min(typval_T *argvars, typval_T *rettv, int domax)
6781{
6782 varnumber_T n = 0;
6783 varnumber_T i;
6784 int error = FALSE;
6785
6786 if (argvars[0].v_type == VAR_LIST)
6787 {
6788 list_T *l;
6789 listitem_T *li;
6790
6791 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006792 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006793 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006794 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006795 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006796 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6797 n = l->lv_u.nonmat.lv_start;
6798 else
6799 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6800 * l->lv_u.nonmat.lv_stride;
6801 }
6802 else
6803 {
6804 li = l->lv_first;
6805 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006806 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006807 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006808 if (error)
6809 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006810 for (;;)
6811 {
6812 li = li->li_next;
6813 if (li == NULL)
6814 break;
6815 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006816 if (error)
6817 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006818 if (domax ? i > n : i < n)
6819 n = i;
6820 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006821 }
6822 }
6823 }
6824 }
6825 else if (argvars[0].v_type == VAR_DICT)
6826 {
6827 dict_T *d;
6828 int first = TRUE;
6829 hashitem_T *hi;
6830 int todo;
6831
6832 d = argvars[0].vval.v_dict;
6833 if (d != NULL)
6834 {
6835 todo = (int)d->dv_hashtab.ht_used;
6836 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6837 {
6838 if (!HASHITEM_EMPTY(hi))
6839 {
6840 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006841 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006842 if (error)
6843 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006844 if (first)
6845 {
6846 n = i;
6847 first = FALSE;
6848 }
6849 else if (domax ? i > n : i < n)
6850 n = i;
6851 }
6852 }
6853 }
6854 }
6855 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006856 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006857
6858 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006859}
6860
6861/*
6862 * "max()" function
6863 */
6864 static void
6865f_max(typval_T *argvars, typval_T *rettv)
6866{
6867 max_min(argvars, rettv, TRUE);
6868}
6869
6870/*
6871 * "min()" function
6872 */
6873 static void
6874f_min(typval_T *argvars, typval_T *rettv)
6875{
6876 max_min(argvars, rettv, FALSE);
6877}
6878
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006879#if defined(FEAT_MZSCHEME) || defined(PROTO)
6880/*
6881 * "mzeval()" function
6882 */
6883 static void
6884f_mzeval(typval_T *argvars, typval_T *rettv)
6885{
6886 char_u *str;
6887 char_u buf[NUMBUFLEN];
6888
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006889 if (check_restricted() || check_secure())
6890 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006891 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006892 do_mzeval(str, rettv);
6893}
6894
6895 void
6896mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6897{
6898 typval_T argvars[3];
6899
6900 argvars[0].v_type = VAR_STRING;
6901 argvars[0].vval.v_string = name;
6902 copy_tv(args, &argvars[1]);
6903 argvars[2].v_type = VAR_UNKNOWN;
6904 f_call(argvars, rettv);
6905 clear_tv(&argvars[1]);
6906}
6907#endif
6908
6909/*
6910 * "nextnonblank()" function
6911 */
6912 static void
6913f_nextnonblank(typval_T *argvars, typval_T *rettv)
6914{
6915 linenr_T lnum;
6916
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006917 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006918 {
6919 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6920 {
6921 lnum = 0;
6922 break;
6923 }
6924 if (*skipwhite(ml_get(lnum)) != NUL)
6925 break;
6926 }
6927 rettv->vval.v_number = lnum;
6928}
6929
6930/*
6931 * "nr2char()" function
6932 */
6933 static void
6934f_nr2char(typval_T *argvars, typval_T *rettv)
6935{
6936 char_u buf[NUMBUFLEN];
6937
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006938 if (has_mbyte)
6939 {
6940 int utf8 = 0;
6941
6942 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006943 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006944 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006945 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006946 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006947 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006948 }
6949 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006950 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006951 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006952 buf[1] = NUL;
6953 }
6954 rettv->v_type = VAR_STRING;
6955 rettv->vval.v_string = vim_strsave(buf);
6956}
6957
6958/*
6959 * "or(expr, expr)" function
6960 */
6961 static void
6962f_or(typval_T *argvars, typval_T *rettv)
6963{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006964 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6965 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006966}
6967
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006968#ifdef FEAT_PERL
6969/*
6970 * "perleval()" function
6971 */
6972 static void
6973f_perleval(typval_T *argvars, typval_T *rettv)
6974{
6975 char_u *str;
6976 char_u buf[NUMBUFLEN];
6977
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006978 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006979 do_perleval(str, rettv);
6980}
6981#endif
6982
6983#ifdef FEAT_FLOAT
6984/*
6985 * "pow()" function
6986 */
6987 static void
6988f_pow(typval_T *argvars, typval_T *rettv)
6989{
6990 float_T fx = 0.0, fy = 0.0;
6991
6992 rettv->v_type = VAR_FLOAT;
6993 if (get_float_arg(argvars, &fx) == OK
6994 && get_float_arg(&argvars[1], &fy) == OK)
6995 rettv->vval.v_float = pow(fx, fy);
6996 else
6997 rettv->vval.v_float = 0.0;
6998}
6999#endif
7000
7001/*
7002 * "prevnonblank()" function
7003 */
7004 static void
7005f_prevnonblank(typval_T *argvars, typval_T *rettv)
7006{
7007 linenr_T lnum;
7008
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007009 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007010 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7011 lnum = 0;
7012 else
7013 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
7014 --lnum;
7015 rettv->vval.v_number = lnum;
7016}
7017
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007018// This dummy va_list is here because:
7019// - passing a NULL pointer doesn't work when va_list isn't a pointer
7020// - locally in the function results in a "used before set" warning
7021// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007022static va_list ap;
7023
7024/*
7025 * "printf()" function
7026 */
7027 static void
7028f_printf(typval_T *argvars, typval_T *rettv)
7029{
7030 char_u buf[NUMBUFLEN];
7031 int len;
7032 char_u *s;
7033 int saved_did_emsg = did_emsg;
7034 char *fmt;
7035
7036 rettv->v_type = VAR_STRING;
7037 rettv->vval.v_string = NULL;
7038
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007039 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007040 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007041 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007042 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007043 if (!did_emsg)
7044 {
7045 s = alloc(len + 1);
7046 if (s != NULL)
7047 {
7048 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007049 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
7050 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007051 }
7052 }
7053 did_emsg |= saved_did_emsg;
7054}
7055
7056/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007057 * "pum_getpos()" function
7058 */
7059 static void
7060f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7061{
7062 if (rettv_dict_alloc(rettv) != OK)
7063 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007064 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007065}
7066
7067/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007068 * "pumvisible()" function
7069 */
7070 static void
7071f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7072{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007073 if (pum_visible())
7074 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007075}
7076
7077#ifdef FEAT_PYTHON3
7078/*
7079 * "py3eval()" function
7080 */
7081 static void
7082f_py3eval(typval_T *argvars, typval_T *rettv)
7083{
7084 char_u *str;
7085 char_u buf[NUMBUFLEN];
7086
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007087 if (check_restricted() || check_secure())
7088 return;
7089
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007090 if (p_pyx == 0)
7091 p_pyx = 3;
7092
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007093 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007094 do_py3eval(str, rettv);
7095}
7096#endif
7097
7098#ifdef FEAT_PYTHON
7099/*
7100 * "pyeval()" function
7101 */
7102 static void
7103f_pyeval(typval_T *argvars, typval_T *rettv)
7104{
7105 char_u *str;
7106 char_u buf[NUMBUFLEN];
7107
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007108 if (check_restricted() || check_secure())
7109 return;
7110
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007111 if (p_pyx == 0)
7112 p_pyx = 2;
7113
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007114 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007115 do_pyeval(str, rettv);
7116}
7117#endif
7118
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007119#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
7120/*
7121 * "pyxeval()" function
7122 */
7123 static void
7124f_pyxeval(typval_T *argvars, typval_T *rettv)
7125{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007126 if (check_restricted() || check_secure())
7127 return;
7128
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007129# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
7130 init_pyxversion();
7131 if (p_pyx == 2)
7132 f_pyeval(argvars, rettv);
7133 else
7134 f_py3eval(argvars, rettv);
7135# elif defined(FEAT_PYTHON)
7136 f_pyeval(argvars, rettv);
7137# elif defined(FEAT_PYTHON3)
7138 f_py3eval(argvars, rettv);
7139# endif
7140}
7141#endif
7142
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007143static UINT32_T srand_seed_for_testing = 0;
7144static int srand_seed_for_testing_is_used = FALSE;
7145
7146 static void
7147f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
7148{
7149 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007150 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007151 else
7152 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007153 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
7154 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007155 }
7156}
7157
7158 static void
7159init_srand(UINT32_T *x)
7160{
7161#ifndef MSWIN
7162 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
7163#endif
7164
7165 if (srand_seed_for_testing_is_used)
7166 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007167 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007168 return;
7169 }
7170#ifndef MSWIN
7171 if (dev_urandom_state != FAIL)
7172 {
7173 int fd = open("/dev/urandom", O_RDONLY);
7174 struct {
7175 union {
7176 UINT32_T number;
7177 char bytes[sizeof(UINT32_T)];
7178 } contents;
7179 } buf;
7180
7181 // Attempt reading /dev/urandom.
7182 if (fd == -1)
7183 dev_urandom_state = FAIL;
7184 else
7185 {
7186 buf.contents.number = 0;
7187 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
7188 != sizeof(UINT32_T))
7189 dev_urandom_state = FAIL;
7190 else
7191 {
7192 dev_urandom_state = OK;
7193 *x = buf.contents.number;
7194 }
7195 close(fd);
7196 }
7197 }
7198 if (dev_urandom_state != OK)
7199 // Reading /dev/urandom doesn't work, fall back to time().
7200#endif
7201 *x = vim_time();
7202}
7203
7204#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
7205#define SPLITMIX32(x, z) ( \
7206 z = (x += 0x9e3779b9), \
7207 z = (z ^ (z >> 16)) * 0x85ebca6b, \
7208 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
7209 z ^ (z >> 16) \
7210 )
7211#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
7212 result = ROTL(y * 5, 7) * 9; \
7213 t = y << 9; \
7214 z ^= x; \
7215 w ^= y; \
7216 y ^= z, x ^= w; \
7217 z ^= t; \
7218 w = ROTL(w, 11);
7219
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007220/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007221 * "rand()" function
7222 */
7223 static void
7224f_rand(typval_T *argvars, typval_T *rettv)
7225{
7226 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007227 static UINT32_T gx, gy, gz, gw;
7228 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007229 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01007230 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007231
7232 if (argvars[0].v_type == VAR_UNKNOWN)
7233 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007234 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007235 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007236 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007237 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007238 init_srand(&x);
7239
7240 gx = SPLITMIX32(x, z);
7241 gy = SPLITMIX32(x, z);
7242 gz = SPLITMIX32(x, z);
7243 gw = SPLITMIX32(x, z);
7244 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007245 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007246
7247 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007248 }
7249 else if (argvars[0].v_type == VAR_LIST)
7250 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007251 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007252 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007253 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007254
7255 lx = list_find(l, 0L);
7256 ly = list_find(l, 1L);
7257 lz = list_find(l, 2L);
7258 lw = list_find(l, 3L);
7259 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
7260 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
7261 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
7262 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
7263 x = (UINT32_T)lx->li_tv.vval.v_number;
7264 y = (UINT32_T)ly->li_tv.vval.v_number;
7265 z = (UINT32_T)lz->li_tv.vval.v_number;
7266 w = (UINT32_T)lw->li_tv.vval.v_number;
7267
7268 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
7269
7270 lx->li_tv.vval.v_number = (varnumber_T)x;
7271 ly->li_tv.vval.v_number = (varnumber_T)y;
7272 lz->li_tv.vval.v_number = (varnumber_T)z;
7273 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007274 }
7275 else
7276 goto theend;
7277
7278 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007279 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007280 return;
7281
7282theend:
7283 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007284 rettv->v_type = VAR_NUMBER;
7285 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007286}
7287
7288/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007289 * "srand()" function
7290 */
7291 static void
7292f_srand(typval_T *argvars, typval_T *rettv)
7293{
7294 UINT32_T x = 0, z;
7295
7296 if (rettv_list_alloc(rettv) == FAIL)
7297 return;
7298 if (argvars[0].v_type == VAR_UNKNOWN)
7299 {
7300 init_srand(&x);
7301 }
7302 else
7303 {
7304 int error = FALSE;
7305
7306 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7307 if (error)
7308 return;
7309 }
7310
7311 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7312 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7313 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7314 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7315}
7316
7317#undef ROTL
7318#undef SPLITMIX32
7319#undef SHUFFLE_XOSHIRO128STARSTAR
7320
7321/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007322 * "range()" function
7323 */
7324 static void
7325f_range(typval_T *argvars, typval_T *rettv)
7326{
7327 varnumber_T start;
7328 varnumber_T end;
7329 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007330 int error = FALSE;
7331
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007332 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007333 if (argvars[1].v_type == VAR_UNKNOWN)
7334 {
7335 end = start - 1;
7336 start = 0;
7337 }
7338 else
7339 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007340 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007341 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007342 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007343 }
7344
7345 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007346 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007347 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007348 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007349 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007350 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007352 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353 list_T *list = rettv->vval.v_list;
7354
7355 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007356 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357 // be called.
7358 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007359 list->lv_u.nonmat.lv_start = start;
7360 list->lv_u.nonmat.lv_end = end;
7361 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007362 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007363 }
7364}
7365
7366/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007367 * Materialize "list".
7368 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369 */
7370 void
7371range_list_materialize(list_T *list)
7372{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007373 varnumber_T start = list->lv_u.nonmat.lv_start;
7374 varnumber_T end = list->lv_u.nonmat.lv_end;
7375 int stride = list->lv_u.nonmat.lv_stride;
7376 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007378 list->lv_first = NULL;
7379 list->lv_u.mat.lv_last = NULL;
7380 list->lv_len = 0;
7381 list->lv_u.mat.lv_idx_item = NULL;
7382 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7383 if (list_append_number(list, (varnumber_T)i) == FAIL)
7384 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007385}
7386
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007387/*
7388 * "getreginfo()" function
7389 */
7390 static void
7391f_getreginfo(typval_T *argvars, typval_T *rettv)
7392{
7393 char_u *strregname;
7394 int regname;
7395 char_u buf[NUMBUFLEN + 2];
7396 long reglen = 0;
7397 dict_T *dict;
7398 list_T *list;
7399
7400 if (argvars[0].v_type != VAR_UNKNOWN)
7401 {
7402 strregname = tv_get_string_chk(&argvars[0]);
7403 if (strregname == NULL)
7404 return;
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007405 if (in_vim9script() && STRLEN(strregname) > 1)
7406 {
7407 semsg(_(e_register_name_must_be_one_char_str), strregname);
7408 return;
7409 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007410 }
7411 else
7412 strregname = get_vim_var_str(VV_REG);
7413
7414 regname = (strregname == NULL ? '"' : *strregname);
7415 if (regname == 0 || regname == '@')
7416 regname = '"';
7417
7418 if (rettv_dict_alloc(rettv) == FAIL)
7419 return;
7420 dict = rettv->vval.v_dict;
7421
7422 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7423 if (list == NULL)
7424 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007425 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007426
7427 buf[0] = NUL;
7428 buf[1] = NUL;
7429 switch (get_reg_type(regname, &reglen))
7430 {
7431 case MLINE: buf[0] = 'V'; break;
7432 case MCHAR: buf[0] = 'v'; break;
7433 case MBLOCK:
7434 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7435 reglen + 1);
7436 break;
7437 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007438 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007439
7440 buf[0] = get_register_name(get_unname_register());
7441 buf[1] = NUL;
7442 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007443 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007444 else
7445 {
7446 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7447
7448 if (item != NULL)
7449 {
7450 item->di_tv.v_type = VAR_SPECIAL;
7451 item->di_tv.vval.v_number = regname == buf[0]
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007452 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007453 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007454 }
7455 }
7456}
7457
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007458 static void
7459return_register(int regname, typval_T *rettv)
7460{
7461 char_u buf[2] = {0, 0};
7462
7463 buf[0] = (char_u)regname;
7464 rettv->v_type = VAR_STRING;
7465 rettv->vval.v_string = vim_strsave(buf);
7466}
7467
7468/*
7469 * "reg_executing()" function
7470 */
7471 static void
7472f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7473{
7474 return_register(reg_executing, rettv);
7475}
7476
7477/*
7478 * "reg_recording()" function
7479 */
7480 static void
7481f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7482{
7483 return_register(reg_recording, rettv);
7484}
7485
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007486/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007487 * "rename({from}, {to})" function
7488 */
7489 static void
7490f_rename(typval_T *argvars, typval_T *rettv)
7491{
7492 char_u buf[NUMBUFLEN];
7493
7494 if (check_restricted() || check_secure())
7495 rettv->vval.v_number = -1;
7496 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007497 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7498 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007499}
7500
7501/*
7502 * "repeat()" function
7503 */
7504 static void
7505f_repeat(typval_T *argvars, typval_T *rettv)
7506{
7507 char_u *p;
7508 int n;
7509 int slen;
7510 int len;
7511 char_u *r;
7512 int i;
7513
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007514 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007515 if (argvars[0].v_type == VAR_LIST)
7516 {
7517 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7518 while (n-- > 0)
7519 if (list_extend(rettv->vval.v_list,
7520 argvars[0].vval.v_list, NULL) == FAIL)
7521 break;
7522 }
7523 else
7524 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007525 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007526 rettv->v_type = VAR_STRING;
7527 rettv->vval.v_string = NULL;
7528
7529 slen = (int)STRLEN(p);
7530 len = slen * n;
7531 if (len <= 0)
7532 return;
7533
7534 r = alloc(len + 1);
7535 if (r != NULL)
7536 {
7537 for (i = 0; i < n; i++)
7538 mch_memmove(r + i * slen, p, (size_t)slen);
7539 r[len] = NUL;
7540 }
7541
7542 rettv->vval.v_string = r;
7543 }
7544}
7545
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007546#define SP_NOMOVE 0x01 // don't move cursor
7547#define SP_REPEAT 0x02 // repeat to find outer pair
7548#define SP_RETCOUNT 0x04 // return matchcount
7549#define SP_SETPCMARK 0x08 // set previous context mark
7550#define SP_START 0x10 // accept match at start position
7551#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7552#define SP_END 0x40 // leave cursor at end of match
7553#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007554
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007555/*
7556 * Get flags for a search function.
7557 * Possibly sets "p_ws".
7558 * Returns BACKWARD, FORWARD or zero (for an error).
7559 */
7560 static int
7561get_search_arg(typval_T *varp, int *flagsp)
7562{
7563 int dir = FORWARD;
7564 char_u *flags;
7565 char_u nbuf[NUMBUFLEN];
7566 int mask;
7567
7568 if (varp->v_type != VAR_UNKNOWN)
7569 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007570 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007571 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007572 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007573 while (*flags != NUL)
7574 {
7575 switch (*flags)
7576 {
7577 case 'b': dir = BACKWARD; break;
7578 case 'w': p_ws = TRUE; break;
7579 case 'W': p_ws = FALSE; break;
7580 default: mask = 0;
7581 if (flagsp != NULL)
7582 switch (*flags)
7583 {
7584 case 'c': mask = SP_START; break;
7585 case 'e': mask = SP_END; break;
7586 case 'm': mask = SP_RETCOUNT; break;
7587 case 'n': mask = SP_NOMOVE; break;
7588 case 'p': mask = SP_SUBPAT; break;
7589 case 'r': mask = SP_REPEAT; break;
7590 case 's': mask = SP_SETPCMARK; break;
7591 case 'z': mask = SP_COLUMN; break;
7592 }
7593 if (mask == 0)
7594 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007595 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007596 dir = 0;
7597 }
7598 else
7599 *flagsp |= mask;
7600 }
7601 if (dir == 0)
7602 break;
7603 ++flags;
7604 }
7605 }
7606 return dir;
7607}
7608
7609/*
7610 * Shared by search() and searchpos() functions.
7611 */
7612 static int
7613search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7614{
7615 int flags;
7616 char_u *pat;
7617 pos_T pos;
7618 pos_T save_cursor;
7619 int save_p_ws = p_ws;
7620 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007621 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007622 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007623#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007624 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007625 long time_limit = 0;
7626#endif
7627 int options = SEARCH_KEEP;
7628 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007629 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007630 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007631 pos_T firstpos;
7632
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007633 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007634 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007635 if (dir == 0)
7636 goto theend;
7637 flags = *flagsp;
7638 if (flags & SP_START)
7639 options |= SEARCH_START;
7640 if (flags & SP_END)
7641 options |= SEARCH_END;
7642 if (flags & SP_COLUMN)
7643 options |= SEARCH_COL;
7644
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007645 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007646 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7647 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007648 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007649 if (lnum_stop < 0)
7650 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007651 if (argvars[3].v_type != VAR_UNKNOWN)
7652 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007653#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007654 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007655 if (time_limit < 0)
7656 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007657#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007658 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007659 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007660 }
7661
7662#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007663 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007664 profile_setlimit(time_limit, &tm);
7665#endif
7666
7667 /*
7668 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7669 * Check to make sure only those flags are set.
7670 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7671 * flags cannot be set. Check for that condition also.
7672 */
7673 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7674 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7675 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007676 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007677 goto theend;
7678 }
7679
7680 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007681 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007682 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007683 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7684#ifdef FEAT_RELTIME
7685 sia.sa_tm = &tm;
7686#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007687
7688 // Repeat until {skip} returns FALSE.
7689 for (;;)
7690 {
7691 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007692 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007693 // finding the first match again means there is no match where {skip}
7694 // evaluates to zero.
7695 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7696 subpatnum = FAIL;
7697
Bram Moolenaara9c01042020-06-07 14:50:50 +02007698 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007699 // didn't find it or no skip argument
7700 break;
7701 firstpos = pos;
7702
Bram Moolenaara9c01042020-06-07 14:50:50 +02007703 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007704 {
7705 int do_skip;
7706 int err;
7707 pos_T save_pos = curwin->w_cursor;
7708
7709 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007710 err = FALSE;
7711 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007712 curwin->w_cursor = save_pos;
7713 if (err)
7714 {
7715 // Evaluating {skip} caused an error, break here.
7716 subpatnum = FAIL;
7717 break;
7718 }
7719 if (!do_skip)
7720 break;
7721 }
7722 }
7723
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007724 if (subpatnum != FAIL)
7725 {
7726 if (flags & SP_SUBPAT)
7727 retval = subpatnum;
7728 else
7729 retval = pos.lnum;
7730 if (flags & SP_SETPCMARK)
7731 setpcmark();
7732 curwin->w_cursor = pos;
7733 if (match_pos != NULL)
7734 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007735 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007736 match_pos->lnum = pos.lnum;
7737 match_pos->col = pos.col + 1;
7738 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007739 // "/$" will put the cursor after the end of the line, may need to
7740 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007741 check_cursor();
7742 }
7743
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007744 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007745 if (flags & SP_NOMOVE)
7746 curwin->w_cursor = save_cursor;
7747 else
7748 curwin->w_set_curswant = TRUE;
7749theend:
7750 p_ws = save_p_ws;
7751
7752 return retval;
7753}
7754
7755#ifdef FEAT_FLOAT
7756
7757/*
7758 * round() is not in C90, use ceil() or floor() instead.
7759 */
7760 float_T
7761vim_round(float_T f)
7762{
7763 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
7764}
7765
7766/*
7767 * "round({float})" function
7768 */
7769 static void
7770f_round(typval_T *argvars, typval_T *rettv)
7771{
7772 float_T f = 0.0;
7773
7774 rettv->v_type = VAR_FLOAT;
7775 if (get_float_arg(argvars, &f) == OK)
7776 rettv->vval.v_float = vim_round(f);
7777 else
7778 rettv->vval.v_float = 0.0;
7779}
7780#endif
7781
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007782#ifdef FEAT_RUBY
7783/*
7784 * "rubyeval()" function
7785 */
7786 static void
7787f_rubyeval(typval_T *argvars, typval_T *rettv)
7788{
7789 char_u *str;
7790 char_u buf[NUMBUFLEN];
7791
7792 str = tv_get_string_buf(&argvars[0], buf);
7793 do_rubyeval(str, rettv);
7794}
7795#endif
7796
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007797/*
7798 * "screenattr()" function
7799 */
7800 static void
7801f_screenattr(typval_T *argvars, typval_T *rettv)
7802{
7803 int row;
7804 int col;
7805 int c;
7806
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007807 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7808 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007809 if (row < 0 || row >= screen_Rows
7810 || col < 0 || col >= screen_Columns)
7811 c = -1;
7812 else
7813 c = ScreenAttrs[LineOffset[row] + col];
7814 rettv->vval.v_number = c;
7815}
7816
7817/*
7818 * "screenchar()" function
7819 */
7820 static void
7821f_screenchar(typval_T *argvars, typval_T *rettv)
7822{
7823 int row;
7824 int col;
7825 int off;
7826 int c;
7827
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007828 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7829 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007830 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007831 c = -1;
7832 else
7833 {
7834 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007835 if (enc_utf8 && ScreenLinesUC[off] != 0)
7836 c = ScreenLinesUC[off];
7837 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007838 c = ScreenLines[off];
7839 }
7840 rettv->vval.v_number = c;
7841}
7842
7843/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007844 * "screenchars()" function
7845 */
7846 static void
7847f_screenchars(typval_T *argvars, typval_T *rettv)
7848{
7849 int row;
7850 int col;
7851 int off;
7852 int c;
7853 int i;
7854
7855 if (rettv_list_alloc(rettv) == FAIL)
7856 return;
7857 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7858 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7859 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7860 return;
7861
7862 off = LineOffset[row] + col;
7863 if (enc_utf8 && ScreenLinesUC[off] != 0)
7864 c = ScreenLinesUC[off];
7865 else
7866 c = ScreenLines[off];
7867 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7868
7869 if (enc_utf8)
7870
7871 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7872 list_append_number(rettv->vval.v_list,
7873 (varnumber_T)ScreenLinesC[i][off]);
7874}
7875
7876/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007877 * "screencol()" function
7878 *
7879 * First column is 1 to be consistent with virtcol().
7880 */
7881 static void
7882f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7883{
7884 rettv->vval.v_number = screen_screencol() + 1;
7885}
7886
7887/*
7888 * "screenrow()" function
7889 */
7890 static void
7891f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7892{
7893 rettv->vval.v_number = screen_screenrow() + 1;
7894}
7895
7896/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007897 * "screenstring()" function
7898 */
7899 static void
7900f_screenstring(typval_T *argvars, typval_T *rettv)
7901{
7902 int row;
7903 int col;
7904 int off;
7905 int c;
7906 int i;
7907 char_u buf[MB_MAXBYTES + 1];
7908 int buflen = 0;
7909
7910 rettv->vval.v_string = NULL;
7911 rettv->v_type = VAR_STRING;
7912
7913 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7914 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7915 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7916 return;
7917
7918 off = LineOffset[row] + col;
7919 if (enc_utf8 && ScreenLinesUC[off] != 0)
7920 c = ScreenLinesUC[off];
7921 else
7922 c = ScreenLines[off];
7923 buflen += mb_char2bytes(c, buf);
7924
Bram Moolenaarf1387282021-03-22 17:11:15 +01007925 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007926 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7927 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7928
7929 buf[buflen] = NUL;
7930 rettv->vval.v_string = vim_strsave(buf);
7931}
7932
7933/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007934 * "search()" function
7935 */
7936 static void
7937f_search(typval_T *argvars, typval_T *rettv)
7938{
7939 int flags = 0;
7940
7941 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7942}
7943
7944/*
7945 * "searchdecl()" function
7946 */
7947 static void
7948f_searchdecl(typval_T *argvars, typval_T *rettv)
7949{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007950 int locally = TRUE;
7951 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007952 int error = FALSE;
7953 char_u *name;
7954
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007955 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007956
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007957 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007958 if (argvars[1].v_type != VAR_UNKNOWN)
7959 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007960 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007961 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007962 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007963 }
7964 if (!error && name != NULL)
7965 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7966 locally, thisblock, SEARCH_KEEP) == FAIL;
7967}
7968
7969/*
7970 * Used by searchpair() and searchpairpos()
7971 */
7972 static int
7973searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7974{
7975 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007976 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007977 int save_p_ws = p_ws;
7978 int dir;
7979 int flags = 0;
7980 char_u nbuf1[NUMBUFLEN];
7981 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007982 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007983 long lnum_stop = 0;
7984 long time_limit = 0;
7985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007986 // Get the three pattern arguments: start, middle, end. Will result in an
7987 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007988 spat = tv_get_string_chk(&argvars[0]);
7989 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7990 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007991 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007992 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007993
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007994 // Handle the optional fourth argument: flags
7995 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007996 if (dir == 0)
7997 goto theend;
7998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007999 // Don't accept SP_END or SP_SUBPAT.
8000 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008001 if ((flags & (SP_END | SP_SUBPAT)) != 0
8002 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
8003 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008004 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008005 goto theend;
8006 }
8007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008008 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008009 if (flags & SP_REPEAT)
8010 p_ws = FALSE;
8011
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008012 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008013 if (argvars[3].v_type == VAR_UNKNOWN
8014 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01008015 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008016 else
8017 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02008018 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01008019 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02008020
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008021 if (argvars[5].v_type != VAR_UNKNOWN)
8022 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008023 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008024 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008025 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008026 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008027 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008028 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008029#ifdef FEAT_RELTIME
8030 if (argvars[6].v_type != VAR_UNKNOWN)
8031 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008032 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008033 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008034 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008035 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008037 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008038 }
8039#endif
8040 }
8041 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008042
8043 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
8044 match_pos, lnum_stop, time_limit);
8045
8046theend:
8047 p_ws = save_p_ws;
8048
8049 return retval;
8050}
8051
8052/*
8053 * "searchpair()" function
8054 */
8055 static void
8056f_searchpair(typval_T *argvars, typval_T *rettv)
8057{
8058 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
8059}
8060
8061/*
8062 * "searchpairpos()" function
8063 */
8064 static void
8065f_searchpairpos(typval_T *argvars, typval_T *rettv)
8066{
8067 pos_T match_pos;
8068 int lnum = 0;
8069 int col = 0;
8070
8071 if (rettv_list_alloc(rettv) == FAIL)
8072 return;
8073
8074 if (searchpair_cmn(argvars, &match_pos) > 0)
8075 {
8076 lnum = match_pos.lnum;
8077 col = match_pos.col;
8078 }
8079
8080 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8081 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8082}
8083
8084/*
8085 * Search for a start/middle/end thing.
8086 * Used by searchpair(), see its documentation for the details.
8087 * Returns 0 or -1 for no match,
8088 */
8089 long
8090do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008091 char_u *spat, // start pattern
8092 char_u *mpat, // middle pattern
8093 char_u *epat, // end pattern
8094 int dir, // BACKWARD or FORWARD
8095 typval_T *skip, // skip expression
8096 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008097 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008098 linenr_T lnum_stop, // stop at this line if not zero
8099 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008100{
8101 char_u *save_cpo;
8102 char_u *pat, *pat2 = NULL, *pat3 = NULL;
8103 long retval = 0;
8104 pos_T pos;
8105 pos_T firstpos;
8106 pos_T foundpos;
8107 pos_T save_cursor;
8108 pos_T save_pos;
8109 int n;
8110 int r;
8111 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01008112 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008113 int err;
8114 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008115#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008116 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008117#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008119 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008120 save_cpo = p_cpo;
8121 p_cpo = empty_option;
8122
8123#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008124 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008125 profile_setlimit(time_limit, &tm);
8126#endif
8127
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008128 // Make two search patterns: start/end (pat2, for in nested pairs) and
8129 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02008130 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
8131 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008132 if (pat2 == NULL || pat3 == NULL)
8133 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008134 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008135 if (*mpat == NUL)
8136 STRCPY(pat3, pat2);
8137 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008138 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008139 spat, epat, mpat);
8140 if (flags & SP_START)
8141 options |= SEARCH_START;
8142
Bram Moolenaar48570482017-10-30 21:48:41 +01008143 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02008144 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01008145
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008146 save_cursor = curwin->w_cursor;
8147 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008148 CLEAR_POS(&firstpos);
8149 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008150 pat = pat3;
8151 for (;;)
8152 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008153 searchit_arg_T sia;
8154
Bram Moolenaara80faa82020-04-12 19:37:17 +02008155 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008156 sia.sa_stop_lnum = lnum_stop;
8157#ifdef FEAT_RELTIME
8158 sia.sa_tm = &tm;
8159#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01008160 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008161 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008162 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008163 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008164 break;
8165
8166 if (firstpos.lnum == 0)
8167 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008168 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008169 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008170 // Found the same position again. Can happen with a pattern that
8171 // has "\zs" at the end and searching backwards. Advance one
8172 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008173 if (dir == BACKWARD)
8174 decl(&pos);
8175 else
8176 incl(&pos);
8177 }
8178 foundpos = pos;
8179
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008180 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008181 options &= ~SEARCH_START;
8182
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008183 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01008184 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008185 {
8186 save_pos = curwin->w_cursor;
8187 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01008188 err = FALSE;
8189 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008190 curwin->w_cursor = save_pos;
8191 if (err)
8192 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008193 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008194 curwin->w_cursor = save_cursor;
8195 retval = -1;
8196 break;
8197 }
8198 if (r)
8199 continue;
8200 }
8201
8202 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8203 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008204 // Found end when searching backwards or start when searching
8205 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008206 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008207 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008208 }
8209 else
8210 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008211 // Found end when searching forward or start when searching
8212 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008213 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008214 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008215 }
8216
8217 if (nest == 0)
8218 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008219 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008220 if (flags & SP_RETCOUNT)
8221 ++retval;
8222 else
8223 retval = pos.lnum;
8224 if (flags & SP_SETPCMARK)
8225 setpcmark();
8226 curwin->w_cursor = pos;
8227 if (!(flags & SP_REPEAT))
8228 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008229 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008230 }
8231 }
8232
8233 if (match_pos != NULL)
8234 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008235 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008236 match_pos->lnum = curwin->w_cursor.lnum;
8237 match_pos->col = curwin->w_cursor.col + 1;
8238 }
8239
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008240 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008241 if ((flags & SP_NOMOVE) || retval == 0)
8242 curwin->w_cursor = save_cursor;
8243
8244theend:
8245 vim_free(pat2);
8246 vim_free(pat3);
8247 if (p_cpo == empty_option)
8248 p_cpo = save_cpo;
8249 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008250 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008251 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008252 // If it's still empty it was changed and restored, need to restore in
8253 // the complicated way.
8254 if (*p_cpo == NUL)
8255 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008256 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008257 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008258
8259 return retval;
8260}
8261
8262/*
8263 * "searchpos()" function
8264 */
8265 static void
8266f_searchpos(typval_T *argvars, typval_T *rettv)
8267{
8268 pos_T match_pos;
8269 int lnum = 0;
8270 int col = 0;
8271 int n;
8272 int flags = 0;
8273
8274 if (rettv_list_alloc(rettv) == FAIL)
8275 return;
8276
8277 n = search_cmn(argvars, &match_pos, &flags);
8278 if (n > 0)
8279 {
8280 lnum = match_pos.lnum;
8281 col = match_pos.col;
8282 }
8283
8284 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8285 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8286 if (flags & SP_SUBPAT)
8287 list_append_number(rettv->vval.v_list, (varnumber_T)n);
8288}
8289
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008290/*
8291 * Set the cursor or mark position.
8292 * If 'charpos' is TRUE, then use the column number as a character offet.
8293 * Otherwise use the column number as a byte offset.
8294 */
8295 static void
8296set_position(typval_T *argvars, typval_T *rettv, int charpos)
8297{
8298 pos_T pos;
8299 int fnum;
8300 char_u *name;
8301 colnr_T curswant = -1;
8302
8303 rettv->vval.v_number = -1;
8304
8305 name = tv_get_string_chk(argvars);
8306 if (name != NULL)
8307 {
8308 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
8309 {
8310 if (pos.col != MAXCOL && --pos.col < 0)
8311 pos.col = 0;
8312 if ((name[0] == '.' && name[1] == NUL))
8313 {
8314 // set cursor; "fnum" is ignored
8315 curwin->w_cursor = pos;
8316 if (curswant >= 0)
8317 {
8318 curwin->w_curswant = curswant - 1;
8319 curwin->w_set_curswant = FALSE;
8320 }
8321 check_cursor();
8322 rettv->vval.v_number = 0;
8323 }
8324 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8325 {
8326 // set mark
8327 if (setmark_pos(name[1], &pos, fnum) == OK)
8328 rettv->vval.v_number = 0;
8329 }
8330 else
8331 emsg(_(e_invarg));
8332 }
8333 }
8334}
8335/*
8336 * "setcharpos()" function
8337 */
8338 static void
8339f_setcharpos(typval_T *argvars, typval_T *rettv)
8340{
8341 set_position(argvars, rettv, TRUE);
8342}
8343
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008344 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008345f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8346{
8347 dict_T *d;
8348 dictitem_T *di;
8349 char_u *csearch;
8350
8351 if (argvars[0].v_type != VAR_DICT)
8352 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008353 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008354 return;
8355 }
8356
8357 if ((d = argvars[0].vval.v_dict) != NULL)
8358 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008359 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008360 if (csearch != NULL)
8361 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362 if (enc_utf8)
8363 {
8364 int pcc[MAX_MCO];
8365 int c = utfc_ptr2char(csearch, pcc);
8366
8367 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8368 }
8369 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008370 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008371 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008372 }
8373
8374 di = dict_find(d, (char_u *)"forward", -1);
8375 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008376 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008377 ? FORWARD : BACKWARD);
8378
8379 di = dict_find(d, (char_u *)"until", -1);
8380 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008381 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008382 }
8383}
8384
8385/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008386 * "setcursorcharpos" function
8387 */
8388 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008389f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008390{
8391 set_cursorpos(argvars, rettv, TRUE);
8392}
8393
8394/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008395 * "setenv()" function
8396 */
8397 static void
8398f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8399{
8400 char_u namebuf[NUMBUFLEN];
8401 char_u valbuf[NUMBUFLEN];
8402 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8403
8404 if (argvars[1].v_type == VAR_SPECIAL
8405 && argvars[1].vval.v_number == VVAL_NULL)
8406 vim_unsetenv(name);
8407 else
8408 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8409}
8410
8411/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008412 * "setfperm({fname}, {mode})" function
8413 */
8414 static void
8415f_setfperm(typval_T *argvars, typval_T *rettv)
8416{
8417 char_u *fname;
8418 char_u modebuf[NUMBUFLEN];
8419 char_u *mode_str;
8420 int i;
8421 int mask;
8422 int mode = 0;
8423
8424 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008425 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426 if (fname == NULL)
8427 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008428 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008429 if (mode_str == NULL)
8430 return;
8431 if (STRLEN(mode_str) != 9)
8432 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008433 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008434 return;
8435 }
8436
8437 mask = 1;
8438 for (i = 8; i >= 0; --i)
8439 {
8440 if (mode_str[i] != '-')
8441 mode |= mask;
8442 mask = mask << 1;
8443 }
8444 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8445}
8446
8447/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008448 * "setpos()" function
8449 */
8450 static void
8451f_setpos(typval_T *argvars, typval_T *rettv)
8452{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008453 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008454}
8455
8456/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008457 * Translate a register type string to the yank type and block length
8458 */
8459 static int
8460get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8461{
8462 char_u *stropt = *pp;
8463 switch (*stropt)
8464 {
8465 case 'v': case 'c': // character-wise selection
8466 *yank_type = MCHAR;
8467 break;
8468 case 'V': case 'l': // line-wise selection
8469 *yank_type = MLINE;
8470 break;
8471 case 'b': case Ctrl_V: // block-wise selection
8472 *yank_type = MBLOCK;
8473 if (VIM_ISDIGIT(stropt[1]))
8474 {
8475 ++stropt;
8476 *block_len = getdigits(&stropt) - 1;
8477 --stropt;
8478 }
8479 break;
8480 default:
8481 return FAIL;
8482 }
8483 *pp = stropt;
8484 return OK;
8485}
8486
8487/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008488 * "setreg()" function
8489 */
8490 static void
8491f_setreg(typval_T *argvars, typval_T *rettv)
8492{
8493 int regname;
8494 char_u *strregname;
8495 char_u *stropt;
8496 char_u *strval;
8497 int append;
8498 char_u yank_type;
8499 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008500 typval_T *regcontents;
8501 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008502
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008503 pointreg = 0;
8504 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008505 block_len = -1;
8506 yank_type = MAUTO;
8507 append = FALSE;
8508
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008509 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008510 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008511
8512 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008513 return; // type error; errmsg already given
Bram Moolenaar418a29f2021-02-10 22:23:41 +01008514 if (in_vim9script() && STRLEN(strregname) > 1)
8515 {
8516 semsg(_(e_register_name_must_be_one_char_str), strregname);
8517 return;
8518 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008519 regname = *strregname;
8520 if (regname == 0 || regname == '@')
8521 regname = '"';
8522
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008523 if (argvars[1].v_type == VAR_DICT)
8524 {
8525 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008526 dictitem_T *di;
8527
8528 if (d == NULL || d->dv_hashtab.ht_used == 0)
8529 {
8530 // Empty dict, clear the register (like setreg(0, []))
8531 char_u *lstval[2] = {NULL, NULL};
8532 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8533 return;
8534 }
8535
8536 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008537 if (di != NULL)
8538 regcontents = &di->di_tv;
8539
8540 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8541 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008542 {
8543 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8544
8545 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008546 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008547 semsg(_(e_invargval), "value");
8548 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008549 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008550 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008551
8552 if (regname == '"')
8553 {
8554 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8555 if (stropt != NULL)
8556 {
8557 pointreg = *stropt;
8558 regname = pointreg;
8559 }
8560 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008561 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008562 pointreg = regname;
8563 }
8564 else
8565 regcontents = &argvars[1];
8566
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008567 if (argvars[2].v_type != VAR_UNKNOWN)
8568 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008569 if (yank_type != MAUTO)
8570 {
8571 semsg(_(e_toomanyarg), "setreg");
8572 return;
8573 }
8574
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008575 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008576 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008577 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008578 for (; *stropt != NUL; ++stropt)
8579 switch (*stropt)
8580 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008581 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008582 append = TRUE;
8583 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008584 default:
8585 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008586 }
8587 }
8588
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008589 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008590 {
8591 char_u **lstval;
8592 char_u **allocval;
8593 char_u buf[NUMBUFLEN];
8594 char_u **curval;
8595 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008596 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008597 listitem_T *li;
8598 int len;
8599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008600 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008601 len = ll == NULL ? 0 : ll->lv_len;
8602
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008603 // First half: use for pointers to result lines; second half: use for
8604 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008605 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008606 if (lstval == NULL)
8607 return;
8608 curval = lstval;
8609 allocval = lstval + len + 2;
8610 curallocval = allocval;
8611
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008612 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008613 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008614 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008615 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008616 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008617 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008618 if (strval == NULL)
8619 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008620 if (strval == buf)
8621 {
8622 // Need to make a copy, next tv_get_string_buf_chk() will
8623 // overwrite the string.
8624 strval = vim_strsave(buf);
8625 if (strval == NULL)
8626 goto free_lstval;
8627 *curallocval++ = strval;
8628 }
8629 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008630 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008631 }
8632 *curval++ = NULL;
8633
8634 write_reg_contents_lst(regname, lstval, -1,
8635 append, yank_type, block_len);
8636free_lstval:
8637 while (curallocval > allocval)
8638 vim_free(*--curallocval);
8639 vim_free(lstval);
8640 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008641 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008642 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008643 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008644 if (strval == NULL)
8645 return;
8646 write_reg_contents_ex(regname, strval, -1,
8647 append, yank_type, block_len);
8648 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008649 if (pointreg != 0)
8650 get_yank_register(pointreg, TRUE);
8651
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008652 rettv->vval.v_number = 0;
8653}
8654
8655/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008656 * "settagstack()" function
8657 */
8658 static void
8659f_settagstack(typval_T *argvars, typval_T *rettv)
8660{
8661 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8662 win_T *wp;
8663 dict_T *d;
8664 int action = 'r';
8665
8666 rettv->vval.v_number = -1;
8667
8668 // first argument: window number or id
8669 wp = find_win_by_nr_or_id(&argvars[0]);
8670 if (wp == NULL)
8671 return;
8672
8673 // second argument: dict with items to set in the tag stack
8674 if (argvars[1].v_type != VAR_DICT)
8675 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008676 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008677 return;
8678 }
8679 d = argvars[1].vval.v_dict;
8680 if (d == NULL)
8681 return;
8682
8683 // third argument: action - 'a' for append and 'r' for replace.
8684 // default is to replace the stack.
8685 if (argvars[2].v_type == VAR_UNKNOWN)
8686 action = 'r';
8687 else if (argvars[2].v_type == VAR_STRING)
8688 {
8689 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008690 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008691 if (actstr == NULL)
8692 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008693 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8694 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008695 action = *actstr;
8696 else
8697 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008698 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008699 return;
8700 }
8701 }
8702 else
8703 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008704 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008705 return;
8706 }
8707
8708 if (set_tagstack(wp, d, action) == OK)
8709 rettv->vval.v_number = 0;
8710}
8711
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008712#ifdef FEAT_CRYPT
8713/*
8714 * "sha256({string})" function
8715 */
8716 static void
8717f_sha256(typval_T *argvars, typval_T *rettv)
8718{
8719 char_u *p;
8720
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008721 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008722 rettv->vval.v_string = vim_strsave(
8723 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8724 rettv->v_type = VAR_STRING;
8725}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008726#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008727
8728/*
8729 * "shellescape({string})" function
8730 */
8731 static void
8732f_shellescape(typval_T *argvars, typval_T *rettv)
8733{
Bram Moolenaar20615522017-06-05 18:46:26 +02008734 int do_special = non_zero_arg(&argvars[1]);
8735
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008736 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008737 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008738 rettv->v_type = VAR_STRING;
8739}
8740
8741/*
8742 * shiftwidth() function
8743 */
8744 static void
8745f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8746{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008747 rettv->vval.v_number = 0;
8748
8749 if (argvars[0].v_type != VAR_UNKNOWN)
8750 {
8751 long col;
8752
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008753 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008754 if (col < 0)
8755 return; // type error; errmsg already given
8756#ifdef FEAT_VARTABS
8757 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8758 return;
8759#endif
8760 }
8761
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008762 rettv->vval.v_number = get_sw_value(curbuf);
8763}
8764
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008765#ifdef FEAT_FLOAT
8766/*
8767 * "sin()" function
8768 */
8769 static void
8770f_sin(typval_T *argvars, typval_T *rettv)
8771{
8772 float_T f = 0.0;
8773
8774 rettv->v_type = VAR_FLOAT;
8775 if (get_float_arg(argvars, &f) == OK)
8776 rettv->vval.v_float = sin(f);
8777 else
8778 rettv->vval.v_float = 0.0;
8779}
8780
8781/*
8782 * "sinh()" function
8783 */
8784 static void
8785f_sinh(typval_T *argvars, typval_T *rettv)
8786{
8787 float_T f = 0.0;
8788
8789 rettv->v_type = VAR_FLOAT;
8790 if (get_float_arg(argvars, &f) == OK)
8791 rettv->vval.v_float = sinh(f);
8792 else
8793 rettv->vval.v_float = 0.0;
8794}
8795#endif
8796
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008797/*
8798 * "soundfold({word})" function
8799 */
8800 static void
8801f_soundfold(typval_T *argvars, typval_T *rettv)
8802{
8803 char_u *s;
8804
8805 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008806 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008807#ifdef FEAT_SPELL
8808 rettv->vval.v_string = eval_soundfold(s);
8809#else
8810 rettv->vval.v_string = vim_strsave(s);
8811#endif
8812}
8813
8814/*
8815 * "spellbadword()" function
8816 */
8817 static void
8818f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8819{
8820 char_u *word = (char_u *)"";
8821 hlf_T attr = HLF_COUNT;
8822 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008823#ifdef FEAT_SPELL
8824 int wo_spell_save = curwin->w_p_spell;
8825
8826 if (!curwin->w_p_spell)
8827 {
8828 did_set_spelllang(curwin);
8829 curwin->w_p_spell = TRUE;
8830 }
8831
8832 if (*curwin->w_s->b_p_spl == NUL)
8833 {
8834 emsg(_(e_no_spell));
8835 curwin->w_p_spell = wo_spell_save;
8836 return;
8837 }
8838#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008839
8840 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008841 {
8842#ifdef FEAT_SPELL
8843 curwin->w_p_spell = wo_spell_save;
8844#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008845 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008846 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008847
8848#ifdef FEAT_SPELL
8849 if (argvars[0].v_type == VAR_UNKNOWN)
8850 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008851 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008852 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8853 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008854 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008855 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008856 curwin->w_set_curswant = TRUE;
8857 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008858 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008859 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008860 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008861 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008862 int capcol = -1;
8863
8864 if (str != NULL)
8865 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008866 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008867 while (*str != NUL)
8868 {
8869 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8870 if (attr != HLF_COUNT)
8871 {
8872 word = str;
8873 break;
8874 }
8875 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008876 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008877 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008878 }
8879 }
8880 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008881 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008882#endif
8883
8884 list_append_string(rettv->vval.v_list, word, len);
8885 list_append_string(rettv->vval.v_list, (char_u *)(
8886 attr == HLF_SPB ? "bad" :
8887 attr == HLF_SPR ? "rare" :
8888 attr == HLF_SPL ? "local" :
8889 attr == HLF_SPC ? "caps" :
8890 ""), -1);
8891}
8892
8893/*
8894 * "spellsuggest()" function
8895 */
8896 static void
8897f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8898{
8899#ifdef FEAT_SPELL
8900 char_u *str;
8901 int typeerr = FALSE;
8902 int maxcount;
8903 garray_T ga;
8904 int i;
8905 listitem_T *li;
8906 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008907 int wo_spell_save = curwin->w_p_spell;
8908
8909 if (!curwin->w_p_spell)
8910 {
8911 did_set_spelllang(curwin);
8912 curwin->w_p_spell = TRUE;
8913 }
8914
8915 if (*curwin->w_s->b_p_spl == NUL)
8916 {
8917 emsg(_(e_no_spell));
8918 curwin->w_p_spell = wo_spell_save;
8919 return;
8920 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008921#endif
8922
8923 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008924 {
8925#ifdef FEAT_SPELL
8926 curwin->w_p_spell = wo_spell_save;
8927#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008928 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008929 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008930
8931#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008932 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008933 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008934 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008935 if (argvars[1].v_type != VAR_UNKNOWN)
8936 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008937 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008938 if (maxcount <= 0)
8939 return;
8940 if (argvars[2].v_type != VAR_UNKNOWN)
8941 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008942 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008943 if (typeerr)
8944 return;
8945 }
8946 }
8947 else
8948 maxcount = 25;
8949
8950 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8951
8952 for (i = 0; i < ga.ga_len; ++i)
8953 {
8954 str = ((char_u **)ga.ga_data)[i];
8955
8956 li = listitem_alloc();
8957 if (li == NULL)
8958 vim_free(str);
8959 else
8960 {
8961 li->li_tv.v_type = VAR_STRING;
8962 li->li_tv.v_lock = 0;
8963 li->li_tv.vval.v_string = str;
8964 list_append(rettv->vval.v_list, li);
8965 }
8966 }
8967 ga_clear(&ga);
8968 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008969 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008970#endif
8971}
8972
8973 static void
8974f_split(typval_T *argvars, typval_T *rettv)
8975{
8976 char_u *str;
8977 char_u *end;
8978 char_u *pat = NULL;
8979 regmatch_T regmatch;
8980 char_u patbuf[NUMBUFLEN];
8981 char_u *save_cpo;
8982 int match;
8983 colnr_T col = 0;
8984 int keepempty = FALSE;
8985 int typeerr = FALSE;
8986
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008987 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008988 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008989 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008990
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008991 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008992 if (argvars[1].v_type != VAR_UNKNOWN)
8993 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008994 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008995 if (pat == NULL)
8996 typeerr = TRUE;
8997 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008998 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008999 }
9000 if (pat == NULL || *pat == NUL)
9001 pat = (char_u *)"[\\x01- ]\\+";
9002
9003 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02009004 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009005 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02009006 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009007
9008 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9009 if (regmatch.regprog != NULL)
9010 {
9011 regmatch.rm_ic = FALSE;
9012 while (*str != NUL || keepempty)
9013 {
9014 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009015 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009016 else
9017 match = vim_regexec_nl(&regmatch, str, col);
9018 if (match)
9019 end = regmatch.startp[0];
9020 else
9021 end = str + STRLEN(str);
9022 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
9023 && *str != NUL && match && end < regmatch.endp[0]))
9024 {
9025 if (list_append_string(rettv->vval.v_list, str,
9026 (int)(end - str)) == FAIL)
9027 break;
9028 }
9029 if (!match)
9030 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01009031 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009032 if (regmatch.endp[0] > str)
9033 col = 0;
9034 else
Bram Moolenaar13505972019-01-24 15:04:48 +01009035 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009036 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009037 str = regmatch.endp[0];
9038 }
9039
9040 vim_regfree(regmatch.regprog);
9041 }
9042
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02009043theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009044 p_cpo = save_cpo;
9045}
9046
9047#ifdef FEAT_FLOAT
9048/*
9049 * "sqrt()" function
9050 */
9051 static void
9052f_sqrt(typval_T *argvars, typval_T *rettv)
9053{
9054 float_T f = 0.0;
9055
9056 rettv->v_type = VAR_FLOAT;
9057 if (get_float_arg(argvars, &f) == OK)
9058 rettv->vval.v_float = sqrt(f);
9059 else
9060 rettv->vval.v_float = 0.0;
9061}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009062#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009063
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009064#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01009065/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009066 * "str2float()" function
9067 */
9068 static void
9069f_str2float(typval_T *argvars, typval_T *rettv)
9070{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009071 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009072 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009073
Bram Moolenaar08243d22017-01-10 16:12:29 +01009074 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009075 p = skipwhite(p + 1);
9076 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01009077 if (isneg)
9078 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009079 rettv->v_type = VAR_FLOAT;
9080}
9081#endif
9082
9083/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02009084 * "str2list()" function
9085 */
9086 static void
9087f_str2list(typval_T *argvars, typval_T *rettv)
9088{
9089 char_u *p;
9090 int utf8 = FALSE;
9091
9092 if (rettv_list_alloc(rettv) == FAIL)
9093 return;
9094
9095 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02009096 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02009097
9098 p = tv_get_string(&argvars[0]);
9099
9100 if (has_mbyte || utf8)
9101 {
9102 int (*ptr2len)(char_u *);
9103 int (*ptr2char)(char_u *);
9104
9105 if (utf8 || enc_utf8)
9106 {
9107 ptr2len = utf_ptr2len;
9108 ptr2char = utf_ptr2char;
9109 }
9110 else
9111 {
9112 ptr2len = mb_ptr2len;
9113 ptr2char = mb_ptr2char;
9114 }
9115
9116 for ( ; *p != NUL; p += (*ptr2len)(p))
9117 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
9118 }
9119 else
9120 for ( ; *p != NUL; ++p)
9121 list_append_number(rettv->vval.v_list, *p);
9122}
9123
9124/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009125 * "str2nr()" function
9126 */
9127 static void
9128f_str2nr(typval_T *argvars, typval_T *rettv)
9129{
9130 int base = 10;
9131 char_u *p;
9132 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009133 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01009134 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009135
9136 if (argvars[1].v_type != VAR_UNKNOWN)
9137 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009138 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009139 if (base != 2 && base != 8 && base != 10 && base != 16)
9140 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009141 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009142 return;
9143 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02009144 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009145 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009146 }
9147
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01009148 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009149 isneg = (*p == '-');
9150 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009151 p = skipwhite(p + 1);
9152 switch (base)
9153 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009154 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02009155 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009156 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009157 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02009158 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
9159 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01009160 if (isneg)
9161 rettv->vval.v_number = -n;
9162 else
9163 rettv->vval.v_number = n;
9164
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009165}
9166
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009167/*
9168 * "strgetchar()" function
9169 */
9170 static void
9171f_strgetchar(typval_T *argvars, typval_T *rettv)
9172{
9173 char_u *str;
9174 int len;
9175 int error = FALSE;
9176 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01009177 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009178
9179 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009180 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009181 if (str == NULL)
9182 return;
9183 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009184 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009185 if (error)
9186 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009187
Bram Moolenaar13505972019-01-24 15:04:48 +01009188 while (charidx >= 0 && byteidx < len)
9189 {
9190 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009191 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009192 rettv->vval.v_number = mb_ptr2char(str + byteidx);
9193 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009194 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009195 --charidx;
9196 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009197 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009198}
9199
9200/*
9201 * "stridx()" function
9202 */
9203 static void
9204f_stridx(typval_T *argvars, typval_T *rettv)
9205{
9206 char_u buf[NUMBUFLEN];
9207 char_u *needle;
9208 char_u *haystack;
9209 char_u *save_haystack;
9210 char_u *pos;
9211 int start_idx;
9212
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009213 needle = tv_get_string_chk(&argvars[1]);
9214 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009215 rettv->vval.v_number = -1;
9216 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009217 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009218
9219 if (argvars[2].v_type != VAR_UNKNOWN)
9220 {
9221 int error = FALSE;
9222
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009223 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009224 if (error || start_idx >= (int)STRLEN(haystack))
9225 return;
9226 if (start_idx >= 0)
9227 haystack += start_idx;
9228 }
9229
9230 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9231 if (pos != NULL)
9232 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
9233}
9234
9235/*
9236 * "string()" function
9237 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009238 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009239f_string(typval_T *argvars, typval_T *rettv)
9240{
9241 char_u *tofree;
9242 char_u numbuf[NUMBUFLEN];
9243
9244 rettv->v_type = VAR_STRING;
9245 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
9246 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009247 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009248 if (rettv->vval.v_string != NULL && tofree == NULL)
9249 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
9250}
9251
9252/*
9253 * "strlen()" function
9254 */
9255 static void
9256f_strlen(typval_T *argvars, typval_T *rettv)
9257{
9258 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009259 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009260}
9261
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01009262 static void
9263strchar_common(typval_T *argvars, typval_T *rettv, int skipcc)
9264{
9265 char_u *s = tv_get_string(&argvars[0]);
9266 varnumber_T len = 0;
9267 int (*func_mb_ptr2char_adv)(char_u **pp);
9268
9269 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
9270 while (*s != NUL)
9271 {
9272 func_mb_ptr2char_adv(&s);
9273 ++len;
9274 }
9275 rettv->vval.v_number = len;
9276}
9277
9278/*
9279 * "strcharlen()" function
9280 */
9281 static void
9282f_strcharlen(typval_T *argvars, typval_T *rettv)
9283{
9284 strchar_common(argvars, rettv, TRUE);
9285}
9286
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009287/*
9288 * "strchars()" function
9289 */
9290 static void
9291f_strchars(typval_T *argvars, typval_T *rettv)
9292{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009293 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009294
9295 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009296 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009297 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02009298 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009299 else
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01009300 strchar_common(argvars, rettv, skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009301}
9302
9303/*
9304 * "strdisplaywidth()" function
9305 */
9306 static void
9307f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
9308{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009309 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009310 int col = 0;
9311
9312 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009313 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009314
9315 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
9316}
9317
9318/*
9319 * "strwidth()" function
9320 */
9321 static void
9322f_strwidth(typval_T *argvars, typval_T *rettv)
9323{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009324 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009325
Bram Moolenaar13505972019-01-24 15:04:48 +01009326 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009327}
9328
9329/*
9330 * "strcharpart()" function
9331 */
9332 static void
9333f_strcharpart(typval_T *argvars, typval_T *rettv)
9334{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009335 char_u *p;
9336 int nchar;
9337 int nbyte = 0;
9338 int charlen;
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009339 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009340 int len = 0;
9341 int slen;
9342 int error = FALSE;
9343
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009344 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009345 slen = (int)STRLEN(p);
9346
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009347 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009348 if (!error)
9349 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009350 if (argvars[2].v_type != VAR_UNKNOWN
9351 && argvars[3].v_type != VAR_UNKNOWN)
9352 {
9353 skipcc = tv_get_bool(&argvars[3]);
9354 if (skipcc < 0 || skipcc > 1)
9355 {
9356 semsg(_(e_using_number_as_bool_nr), skipcc);
9357 return;
9358 }
9359 }
9360
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009361 if (nchar > 0)
9362 while (nchar > 0 && nbyte < slen)
9363 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009364 if (skipcc)
9365 nbyte += mb_ptr2len(p + nbyte);
9366 else
9367 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009368 --nchar;
9369 }
9370 else
9371 nbyte = nchar;
9372 if (argvars[2].v_type != VAR_UNKNOWN)
9373 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009374 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009375 while (charlen > 0 && nbyte + len < slen)
9376 {
9377 int off = nbyte + len;
9378
9379 if (off < 0)
9380 len += 1;
9381 else
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009382 {
9383 if (skipcc)
9384 len += mb_ptr2len(p + off);
9385 else
9386 len += MB_CPTR2LEN(p + off);
9387 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009388 --charlen;
9389 }
9390 }
9391 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009392 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009393 }
9394
9395 /*
9396 * Only return the overlap between the specified part and the actual
9397 * string.
9398 */
9399 if (nbyte < 0)
9400 {
9401 len += nbyte;
9402 nbyte = 0;
9403 }
9404 else if (nbyte > slen)
9405 nbyte = slen;
9406 if (len < 0)
9407 len = 0;
9408 else if (nbyte + len > slen)
9409 len = slen - nbyte;
9410
9411 rettv->v_type = VAR_STRING;
9412 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009413}
9414
9415/*
9416 * "strpart()" function
9417 */
9418 static void
9419f_strpart(typval_T *argvars, typval_T *rettv)
9420{
9421 char_u *p;
9422 int n;
9423 int len;
9424 int slen;
9425 int error = FALSE;
9426
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009427 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009428 slen = (int)STRLEN(p);
9429
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009430 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009431 if (error)
9432 len = 0;
9433 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009434 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009435 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009436 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009437
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009438 // Only return the overlap between the specified part and the actual
9439 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009440 if (n < 0)
9441 {
9442 len += n;
9443 n = 0;
9444 }
9445 else if (n > slen)
9446 n = slen;
9447 if (len < 0)
9448 len = 0;
9449 else if (n + len > slen)
9450 len = slen - n;
9451
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009452 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9453 {
9454 int off;
9455
9456 // length in characters
9457 for (off = n; off < slen && len > 0; --len)
9458 off += mb_ptr2len(p + off);
9459 len = off - n;
9460 }
9461
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009462 rettv->v_type = VAR_STRING;
9463 rettv->vval.v_string = vim_strnsave(p + n, len);
9464}
9465
9466/*
9467 * "strridx()" function
9468 */
9469 static void
9470f_strridx(typval_T *argvars, typval_T *rettv)
9471{
9472 char_u buf[NUMBUFLEN];
9473 char_u *needle;
9474 char_u *haystack;
9475 char_u *rest;
9476 char_u *lastmatch = NULL;
9477 int haystack_len, end_idx;
9478
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009479 needle = tv_get_string_chk(&argvars[1]);
9480 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009481
9482 rettv->vval.v_number = -1;
9483 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009484 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009485
9486 haystack_len = (int)STRLEN(haystack);
9487 if (argvars[2].v_type != VAR_UNKNOWN)
9488 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009489 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009490 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009491 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009492 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009493 }
9494 else
9495 end_idx = haystack_len;
9496
9497 if (*needle == NUL)
9498 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009499 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009500 lastmatch = haystack + end_idx;
9501 }
9502 else
9503 {
9504 for (rest = haystack; *rest != '\0'; ++rest)
9505 {
9506 rest = (char_u *)strstr((char *)rest, (char *)needle);
9507 if (rest == NULL || rest > haystack + end_idx)
9508 break;
9509 lastmatch = rest;
9510 }
9511 }
9512
9513 if (lastmatch == NULL)
9514 rettv->vval.v_number = -1;
9515 else
9516 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9517}
9518
9519/*
9520 * "strtrans()" function
9521 */
9522 static void
9523f_strtrans(typval_T *argvars, typval_T *rettv)
9524{
9525 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009526 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009527}
9528
9529/*
9530 * "submatch()" function
9531 */
9532 static void
9533f_submatch(typval_T *argvars, typval_T *rettv)
9534{
9535 int error = FALSE;
9536 int no;
9537 int retList = 0;
9538
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009539 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009540 if (error)
9541 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009542 if (no < 0 || no >= NSUBEXP)
9543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009544 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009545 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009546 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009547 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009548 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009549 if (error)
9550 return;
9551
9552 if (retList == 0)
9553 {
9554 rettv->v_type = VAR_STRING;
9555 rettv->vval.v_string = reg_submatch(no);
9556 }
9557 else
9558 {
9559 rettv->v_type = VAR_LIST;
9560 rettv->vval.v_list = reg_submatch_list(no);
9561 }
9562}
9563
9564/*
9565 * "substitute()" function
9566 */
9567 static void
9568f_substitute(typval_T *argvars, typval_T *rettv)
9569{
9570 char_u patbuf[NUMBUFLEN];
9571 char_u subbuf[NUMBUFLEN];
9572 char_u flagsbuf[NUMBUFLEN];
9573
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009574 char_u *str = tv_get_string_chk(&argvars[0]);
9575 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009576 char_u *sub = NULL;
9577 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009578 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009579
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009580 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9581 expr = &argvars[2];
9582 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009583 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009584
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009585 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009586 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9587 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009588 rettv->vval.v_string = NULL;
9589 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009590 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009591}
9592
9593/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009594 * "swapinfo(swap_filename)" function
9595 */
9596 static void
9597f_swapinfo(typval_T *argvars, typval_T *rettv)
9598{
9599 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009600 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009601}
9602
9603/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009604 * "swapname(expr)" function
9605 */
9606 static void
9607f_swapname(typval_T *argvars, typval_T *rettv)
9608{
9609 buf_T *buf;
9610
9611 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009612 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009613 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9614 || buf->b_ml.ml_mfp->mf_fname == NULL)
9615 rettv->vval.v_string = NULL;
9616 else
9617 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9618}
9619
9620/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009621 * "synID(lnum, col, trans)" function
9622 */
9623 static void
9624f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9625{
9626 int id = 0;
9627#ifdef FEAT_SYN_HL
9628 linenr_T lnum;
9629 colnr_T col;
9630 int trans;
9631 int transerr = FALSE;
9632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009633 lnum = tv_get_lnum(argvars); // -1 on type error
9634 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009635 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009636
9637 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9638 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9639 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9640#endif
9641
9642 rettv->vval.v_number = id;
9643}
9644
9645/*
9646 * "synIDattr(id, what [, mode])" function
9647 */
9648 static void
9649f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9650{
9651 char_u *p = NULL;
9652#ifdef FEAT_SYN_HL
9653 int id;
9654 char_u *what;
9655 char_u *mode;
9656 char_u modebuf[NUMBUFLEN];
9657 int modec;
9658
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009659 id = (int)tv_get_number(&argvars[0]);
9660 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009661 if (argvars[2].v_type != VAR_UNKNOWN)
9662 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009663 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009664 modec = TOLOWER_ASC(mode[0]);
9665 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009666 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009667 }
9668 else
9669 {
9670#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9671 if (USE_24BIT)
9672 modec = 'g';
9673 else
9674#endif
9675 if (t_colors > 1)
9676 modec = 'c';
9677 else
9678 modec = 't';
9679 }
9680
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009681 switch (TOLOWER_ASC(what[0]))
9682 {
9683 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009684 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009685 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009686 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009687 p = highlight_has_attr(id, HL_BOLD, modec);
9688 break;
9689
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009690 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009691 p = highlight_color(id, what, modec);
9692 break;
9693
9694 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009695 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009696 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009697 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009698 p = highlight_has_attr(id, HL_ITALIC, modec);
9699 break;
9700
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009701 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009702 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009703 break;
9704
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009705 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009706 p = highlight_has_attr(id, HL_INVERSE, modec);
9707 break;
9708
9709 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009710 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009711 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009712 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009713 else if (TOLOWER_ASC(what[1]) == 't' &&
9714 TOLOWER_ASC(what[2]) == 'r')
9715 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009716 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009717 p = highlight_has_attr(id, HL_STANDOUT, modec);
9718 break;
9719
9720 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009721 if (TOLOWER_ASC(what[1]) == 'l') // ul
9722 p = highlight_color(id, what, modec);
9723 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009724 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009725 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9726 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009727 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009728 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9729 break;
9730 }
9731
9732 if (p != NULL)
9733 p = vim_strsave(p);
9734#endif
9735 rettv->v_type = VAR_STRING;
9736 rettv->vval.v_string = p;
9737}
9738
9739/*
9740 * "synIDtrans(id)" function
9741 */
9742 static void
9743f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9744{
9745 int id;
9746
9747#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009748 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009749
9750 if (id > 0)
9751 id = syn_get_final_id(id);
9752 else
9753#endif
9754 id = 0;
9755
9756 rettv->vval.v_number = id;
9757}
9758
9759/*
9760 * "synconcealed(lnum, col)" function
9761 */
9762 static void
9763f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9764{
9765#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9766 linenr_T lnum;
9767 colnr_T col;
9768 int syntax_flags = 0;
9769 int cchar;
9770 int matchid = 0;
9771 char_u str[NUMBUFLEN];
9772#endif
9773
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009774 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009775
9776#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009777 lnum = tv_get_lnum(argvars); // -1 on type error
9778 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009779
Bram Moolenaara80faa82020-04-12 19:37:17 +02009780 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009781
9782 if (rettv_list_alloc(rettv) != FAIL)
9783 {
9784 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9785 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9786 && curwin->w_p_cole > 0)
9787 {
9788 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9789 syntax_flags = get_syntax_info(&matchid);
9790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009791 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009792 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9793 {
9794 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009795 if (cchar == NUL && curwin->w_p_cole == 1)
Bram Moolenaareed9d462021-02-15 20:38:25 +01009796 cchar = (curwin->w_lcs_chars.conceal == NUL) ? ' '
9797 : curwin->w_lcs_chars.conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009798 if (cchar != NUL)
9799 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009800 if (has_mbyte)
9801 (*mb_char2bytes)(cchar, str);
9802 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009803 str[0] = cchar;
9804 }
9805 }
9806 }
9807
9808 list_append_number(rettv->vval.v_list,
9809 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009810 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009811 list_append_string(rettv->vval.v_list, str, -1);
9812 list_append_number(rettv->vval.v_list, matchid);
9813 }
9814#endif
9815}
9816
9817/*
9818 * "synstack(lnum, col)" function
9819 */
9820 static void
9821f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9822{
9823#ifdef FEAT_SYN_HL
9824 linenr_T lnum;
9825 colnr_T col;
9826 int i;
9827 int id;
9828#endif
9829
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009830 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009831
9832#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009833 lnum = tv_get_lnum(argvars); // -1 on type error
9834 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009835
9836 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9837 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9838 && rettv_list_alloc(rettv) != FAIL)
9839 {
9840 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9841 for (i = 0; ; ++i)
9842 {
9843 id = syn_get_stack_item(i);
9844 if (id < 0)
9845 break;
9846 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9847 break;
9848 }
9849 }
9850#endif
9851}
9852
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009853/*
9854 * "tabpagebuflist()" function
9855 */
9856 static void
9857f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9858{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009859 tabpage_T *tp;
9860 win_T *wp = NULL;
9861
9862 if (argvars[0].v_type == VAR_UNKNOWN)
9863 wp = firstwin;
9864 else
9865 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009866 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009867 if (tp != NULL)
9868 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9869 }
9870 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9871 {
9872 for (; wp != NULL; wp = wp->w_next)
9873 if (list_append_number(rettv->vval.v_list,
9874 wp->w_buffer->b_fnum) == FAIL)
9875 break;
9876 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009877}
9878
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009879/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009880 * "tagfiles()" function
9881 */
9882 static void
9883f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9884{
9885 char_u *fname;
9886 tagname_T tn;
9887 int first;
9888
9889 if (rettv_list_alloc(rettv) == FAIL)
9890 return;
9891 fname = alloc(MAXPATHL);
9892 if (fname == NULL)
9893 return;
9894
9895 for (first = TRUE; ; first = FALSE)
9896 if (get_tagfname(&tn, first, fname) == FAIL
9897 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9898 break;
9899 tagname_free(&tn);
9900 vim_free(fname);
9901}
9902
9903/*
9904 * "taglist()" function
9905 */
9906 static void
9907f_taglist(typval_T *argvars, typval_T *rettv)
9908{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009909 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009910 char_u *tag_pattern;
9911
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009912 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009913
9914 rettv->vval.v_number = FALSE;
9915 if (*tag_pattern == NUL)
9916 return;
9917
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009918 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009919 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009920 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009921 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009922}
9923
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009924#ifdef FEAT_FLOAT
9925/*
9926 * "tan()" function
9927 */
9928 static void
9929f_tan(typval_T *argvars, typval_T *rettv)
9930{
9931 float_T f = 0.0;
9932
9933 rettv->v_type = VAR_FLOAT;
9934 if (get_float_arg(argvars, &f) == OK)
9935 rettv->vval.v_float = tan(f);
9936 else
9937 rettv->vval.v_float = 0.0;
9938}
9939
9940/*
9941 * "tanh()" function
9942 */
9943 static void
9944f_tanh(typval_T *argvars, typval_T *rettv)
9945{
9946 float_T f = 0.0;
9947
9948 rettv->v_type = VAR_FLOAT;
9949 if (get_float_arg(argvars, &f) == OK)
9950 rettv->vval.v_float = tanh(f);
9951 else
9952 rettv->vval.v_float = 0.0;
9953}
9954#endif
9955
9956/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009957 * "tolower(string)" function
9958 */
9959 static void
9960f_tolower(typval_T *argvars, typval_T *rettv)
9961{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009962 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009963 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009964}
9965
9966/*
9967 * "toupper(string)" function
9968 */
9969 static void
9970f_toupper(typval_T *argvars, typval_T *rettv)
9971{
9972 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009973 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009974}
9975
9976/*
9977 * "tr(string, fromstr, tostr)" function
9978 */
9979 static void
9980f_tr(typval_T *argvars, typval_T *rettv)
9981{
9982 char_u *in_str;
9983 char_u *fromstr;
9984 char_u *tostr;
9985 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009986 int inlen;
9987 int fromlen;
9988 int tolen;
9989 int idx;
9990 char_u *cpstr;
9991 int cplen;
9992 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009993 char_u buf[NUMBUFLEN];
9994 char_u buf2[NUMBUFLEN];
9995 garray_T ga;
9996
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009997 in_str = tv_get_string(&argvars[0]);
9998 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9999 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010000
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010001 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010002 rettv->v_type = VAR_STRING;
10003 rettv->vval.v_string = NULL;
10004 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010005 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010006 ga_init2(&ga, (int)sizeof(char), 80);
10007
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010008 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010009 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010010 if (STRLEN(fromstr) != STRLEN(tostr))
10011 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010012error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010013 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010014 ga_clear(&ga);
10015 return;
10016 }
10017
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010018 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010019 while (*in_str != NUL)
10020 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010021 if (has_mbyte)
10022 {
10023 inlen = (*mb_ptr2len)(in_str);
10024 cpstr = in_str;
10025 cplen = inlen;
10026 idx = 0;
10027 for (p = fromstr; *p != NUL; p += fromlen)
10028 {
10029 fromlen = (*mb_ptr2len)(p);
10030 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
10031 {
10032 for (p = tostr; *p != NUL; p += tolen)
10033 {
10034 tolen = (*mb_ptr2len)(p);
10035 if (idx-- == 0)
10036 {
10037 cplen = tolen;
10038 cpstr = p;
10039 break;
10040 }
10041 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010042 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010043 goto error;
10044 break;
10045 }
10046 ++idx;
10047 }
10048
10049 if (first && cpstr == in_str)
10050 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010051 // Check that fromstr and tostr have the same number of
10052 // (multi-byte) characters. Done only once when a character
10053 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010054 first = FALSE;
10055 for (p = tostr; *p != NUL; p += tolen)
10056 {
10057 tolen = (*mb_ptr2len)(p);
10058 --idx;
10059 }
10060 if (idx != 0)
10061 goto error;
10062 }
10063
10064 (void)ga_grow(&ga, cplen);
10065 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
10066 ga.ga_len += cplen;
10067
10068 in_str += inlen;
10069 }
10070 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010071 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010072 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010073 p = vim_strchr(fromstr, *in_str);
10074 if (p != NULL)
10075 ga_append(&ga, tostr[p - fromstr]);
10076 else
10077 ga_append(&ga, *in_str);
10078 ++in_str;
10079 }
10080 }
10081
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010082 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010083 (void)ga_grow(&ga, 1);
10084 ga_append(&ga, NUL);
10085
10086 rettv->vval.v_string = ga.ga_data;
10087}
10088
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010089/*
10090 * "trim({expr})" function
10091 */
10092 static void
10093f_trim(typval_T *argvars, typval_T *rettv)
10094{
10095 char_u buf1[NUMBUFLEN];
10096 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010097 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010098 char_u *mask = NULL;
10099 char_u *tail;
10100 char_u *prev;
10101 char_u *p;
10102 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010103 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010104
10105 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010106 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010107 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010108 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010109
10110 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010111 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010112 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010113
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010114 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010115 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010116 int error = 0;
10117
10118 // leading or trailing characters to trim
10119 dir = (int)tv_get_number_chk(&argvars[2], &error);
10120 if (error)
10121 return;
10122 if (dir < 0 || dir > 2)
10123 {
10124 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
10125 return;
10126 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010127 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010128 }
10129
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010130 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010131 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010132 // Trim leading characters
10133 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010134 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010135 c1 = PTR2CHAR(head);
10136 if (mask == NULL)
10137 {
10138 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010139 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010140 }
10141 else
10142 {
10143 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10144 if (c1 == PTR2CHAR(p))
10145 break;
10146 if (*p == NUL)
10147 break;
10148 }
10149 MB_PTR_ADV(head);
10150 }
10151 }
10152
10153 tail = head + STRLEN(head);
10154 if (dir == 0 || dir == 2)
10155 {
10156 // Trim trailing characters
10157 for (; tail > head; tail = prev)
10158 {
10159 prev = tail;
10160 MB_PTR_BACK(head, prev);
10161 c1 = PTR2CHAR(prev);
10162 if (mask == NULL)
10163 {
10164 if (c1 > ' ' && c1 != 0xa0)
10165 break;
10166 }
10167 else
10168 {
10169 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10170 if (c1 == PTR2CHAR(p))
10171 break;
10172 if (*p == NUL)
10173 break;
10174 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010175 }
10176 }
Bram Moolenaardf44a272020-06-07 20:49:05 +020010177 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010178}
10179
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010180#ifdef FEAT_FLOAT
10181/*
10182 * "trunc({float})" function
10183 */
10184 static void
10185f_trunc(typval_T *argvars, typval_T *rettv)
10186{
10187 float_T f = 0.0;
10188
10189 rettv->v_type = VAR_FLOAT;
10190 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010191 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010192 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
10193 else
10194 rettv->vval.v_float = 0.0;
10195}
10196#endif
10197
10198/*
10199 * "type(expr)" function
10200 */
10201 static void
10202f_type(typval_T *argvars, typval_T *rettv)
10203{
10204 int n = -1;
10205
10206 switch (argvars[0].v_type)
10207 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010208 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
10209 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010210 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010211 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
10212 case VAR_LIST: n = VAR_TYPE_LIST; break;
10213 case VAR_DICT: n = VAR_TYPE_DICT; break;
10214 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
10215 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
10216 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020010217 case VAR_JOB: n = VAR_TYPE_JOB; break;
10218 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010219 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010220 case VAR_INSTR: n = VAR_TYPE_INSTR; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010221 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +020010222 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010223 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +010010224 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010225 n = -1;
10226 break;
10227 }
10228 rettv->vval.v_number = n;
10229}
10230
10231/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010232 * "virtcol(string)" function
10233 */
10234 static void
10235f_virtcol(typval_T *argvars, typval_T *rettv)
10236{
10237 colnr_T vcol = 0;
10238 pos_T *fp;
10239 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010240 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010241
Bram Moolenaar6f02b002021-01-10 20:22:54 +010010242 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010243 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
10244 && fnum == curbuf->b_fnum)
10245 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010246 // Limit the column to a valid value, getvvcol() doesn't check.
10247 if (fp->col < 0)
10248 fp->col = 0;
10249 else
10250 {
10251 len = (int)STRLEN(ml_get(fp->lnum));
10252 if (fp->col > len)
10253 fp->col = len;
10254 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010255 getvvcol(curwin, fp, NULL, NULL, &vcol);
10256 ++vcol;
10257 }
10258
10259 rettv->vval.v_number = vcol;
10260}
10261
10262/*
10263 * "visualmode()" function
10264 */
10265 static void
10266f_visualmode(typval_T *argvars, typval_T *rettv)
10267{
10268 char_u str[2];
10269
10270 rettv->v_type = VAR_STRING;
10271 str[0] = curbuf->b_visual_mode_eval;
10272 str[1] = NUL;
10273 rettv->vval.v_string = vim_strsave(str);
10274
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010275 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010276 if (non_zero_arg(&argvars[0]))
10277 curbuf->b_visual_mode_eval = NUL;
10278}
10279
10280/*
10281 * "wildmenumode()" function
10282 */
10283 static void
10284f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10285{
10286#ifdef FEAT_WILDMENU
10287 if (wild_menu_showing)
10288 rettv->vval.v_number = 1;
10289#endif
10290}
10291
10292/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +010010293 * "windowsversion()" function
10294 */
10295 static void
10296f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10297{
10298 rettv->v_type = VAR_STRING;
10299 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
10300}
10301
10302/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010303 * "wordcount()" function
10304 */
10305 static void
10306f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
10307{
10308 if (rettv_dict_alloc(rettv) == FAIL)
10309 return;
10310 cursor_pos_info(rettv->vval.v_dict);
10311}
10312
10313/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010314 * "xor(expr, expr)" function
10315 */
10316 static void
10317f_xor(typval_T *argvars, typval_T *rettv)
10318{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010319 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
10320 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010321}
10322
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010323#endif // FEAT_EVAL