blob: 16447aba25b636ab992c961ecdf3ce9908ee2f97 [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/*
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02001974 * Check the argument types for builtin function "idx".
Bram Moolenaar94738d82020-10-21 14:25:07 +02001975 * 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]);
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002478 if (func == NULL || *func == NUL)
2479 return; // type error, empty name or null function
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.
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002782 * If 'charcol' is TRUE, then use the column number as a character offset.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002783 * 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 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02003178 semsg(_(e_using_invalid_value_as_string_str),
3179 vartype_name(argvars[arg_off].v_type));
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003180 return;
3181 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003182 else
3183 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003184 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003185 if (cmd == NULL)
3186 return;
3187 }
3188
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003189 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003190 {
3191 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003192 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003193
3194 if (s == NULL)
3195 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003196 if (*s == NUL)
3197 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003198 if (STRNCMP(s, "silent", 6) == 0)
3199 ++msg_silent;
3200 if (STRCMP(s, "silent!") == 0)
3201 {
3202 emsg_silent = TRUE;
3203 emsg_noredir = TRUE;
3204 }
3205 }
3206 else
3207 ++msg_silent;
3208
3209 if (redir_execute)
3210 save_ga = redir_execute_ga;
3211 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3212 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003213 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003214 if (!echo_output)
3215 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003216
3217 if (cmd != NULL)
3218 do_cmdline_cmd(cmd);
3219 else
3220 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003221 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003222
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003223 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003224 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003225 do_cmdline(NULL, get_list_line, (void *)&item,
3226 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3227 --list->lv_refcount;
3228 }
3229
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003230 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003231 if (ga_grow(&redir_execute_ga, 1) == OK)
3232 {
3233 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3234 rettv->vval.v_string = redir_execute_ga.ga_data;
3235 }
3236 else
3237 {
3238 ga_clear(&redir_execute_ga);
3239 rettv->vval.v_string = NULL;
3240 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003241 msg_silent = save_msg_silent;
3242 emsg_silent = save_emsg_silent;
3243 emsg_noredir = save_emsg_noredir;
3244
3245 redir_execute = save_redir_execute;
3246 if (redir_execute)
3247 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003248 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003249
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003250 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003251 if (echo_output)
3252 // When not working silently: put it in column zero. A following
3253 // "echon" will overwrite the message, unavoidably.
3254 msg_col = 0;
3255 else
3256 // When working silently: Put it back where it was, since nothing
3257 // should have been written.
3258 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003259}
3260
3261/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003262 * "execute()" function
3263 */
3264 static void
3265f_execute(typval_T *argvars, typval_T *rettv)
3266{
3267 execute_common(argvars, rettv, 0);
3268}
3269
3270/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003271 * "exists()" function
3272 */
3273 static void
3274f_exists(typval_T *argvars, typval_T *rettv)
3275{
3276 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003277 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003278
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003279 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003280 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003281 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003282 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003283 if (mch_getenv(p + 1) != NULL)
3284 n = TRUE;
3285 else
3286 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003287 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003288 p = expand_env_save(p);
3289 if (p != NULL && *p != '$')
3290 n = TRUE;
3291 vim_free(p);
3292 }
3293 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003294 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003295 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003296 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003297 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003298 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003300 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003301 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003302 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003303 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003304 else if (*p == '?') // internal function only
3305 {
3306 n = has_internal_func_name(p + 1);
3307 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003308 else if (*p == ':')
3309 {
3310 n = cmd_exists(p + 1);
3311 }
3312 else if (*p == '#')
3313 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003314 if (p[1] == '#')
3315 n = autocmd_supported(p + 2);
3316 else
3317 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003318 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003319 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003320 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003321 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003322 }
3323
3324 rettv->vval.v_number = n;
3325}
3326
3327#ifdef FEAT_FLOAT
3328/*
3329 * "exp()" function
3330 */
3331 static void
3332f_exp(typval_T *argvars, typval_T *rettv)
3333{
3334 float_T f = 0.0;
3335
3336 rettv->v_type = VAR_FLOAT;
3337 if (get_float_arg(argvars, &f) == OK)
3338 rettv->vval.v_float = exp(f);
3339 else
3340 rettv->vval.v_float = 0.0;
3341}
3342#endif
3343
3344/*
3345 * "expand()" function
3346 */
3347 static void
3348f_expand(typval_T *argvars, typval_T *rettv)
3349{
3350 char_u *s;
3351 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003352 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003353 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3354 expand_T xpc;
3355 int error = FALSE;
3356 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003357#ifdef BACKSLASH_IN_FILENAME
3358 char_u *p_csl_save = p_csl;
3359
3360 // avoid using 'completeslash' here
3361 p_csl = empty_option;
3362#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003363
3364 rettv->v_type = VAR_STRING;
3365 if (argvars[1].v_type != VAR_UNKNOWN
3366 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003367 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003368 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003369 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003370
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003371 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003372 if (*s == '%' || *s == '#' || *s == '<')
3373 {
3374 ++emsg_off;
3375 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3376 --emsg_off;
3377 if (rettv->v_type == VAR_LIST)
3378 {
3379 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3380 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003381 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003382 }
3383 else
3384 rettv->vval.v_string = result;
3385 }
3386 else
3387 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003388 // When the optional second argument is non-zero, don't remove matches
3389 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003390 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003391 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003392 options |= WILD_KEEP_ALL;
3393 if (!error)
3394 {
3395 ExpandInit(&xpc);
3396 xpc.xp_context = EXPAND_FILES;
3397 if (p_wic)
3398 options += WILD_ICASE;
3399 if (rettv->v_type == VAR_STRING)
3400 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3401 options, WILD_ALL);
3402 else if (rettv_list_alloc(rettv) != FAIL)
3403 {
3404 int i;
3405
3406 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3407 for (i = 0; i < xpc.xp_numfiles; i++)
3408 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3409 ExpandCleanup(&xpc);
3410 }
3411 }
3412 else
3413 rettv->vval.v_string = NULL;
3414 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003415#ifdef BACKSLASH_IN_FILENAME
3416 p_csl = p_csl_save;
3417#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003418}
3419
3420/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003421 * "expandcmd()" function
3422 * Expand all the special characters in a command string.
3423 */
3424 static void
3425f_expandcmd(typval_T *argvars, typval_T *rettv)
3426{
3427 exarg_T eap;
3428 char_u *cmdstr;
3429 char *errormsg = NULL;
3430
3431 rettv->v_type = VAR_STRING;
3432 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3433
3434 memset(&eap, 0, sizeof(eap));
3435 eap.cmd = cmdstr;
3436 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003437 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003438 eap.usefilter = FALSE;
3439 eap.nextcmd = NULL;
3440 eap.cmdidx = CMD_USER;
3441
3442 expand_filename(&eap, &cmdstr, &errormsg);
3443 if (errormsg != NULL && *errormsg != NUL)
3444 emsg(errormsg);
3445
3446 rettv->vval.v_string = cmdstr;
3447}
3448
3449/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003450 * "feedkeys()" function
3451 */
3452 static void
3453f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3454{
3455 int remap = TRUE;
3456 int insert = FALSE;
3457 char_u *keys, *flags;
3458 char_u nbuf[NUMBUFLEN];
3459 int typed = FALSE;
3460 int execute = FALSE;
3461 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003462 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003463 char_u *keys_esc;
3464
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003465 // This is not allowed in the sandbox. If the commands would still be
3466 // executed in the sandbox it would be OK, but it probably happens later,
3467 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003468 if (check_secure())
3469 return;
3470
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003471 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003472
3473 if (argvars[1].v_type != VAR_UNKNOWN)
3474 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003475 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003476 for ( ; *flags != NUL; ++flags)
3477 {
3478 switch (*flags)
3479 {
3480 case 'n': remap = FALSE; break;
3481 case 'm': remap = TRUE; break;
3482 case 't': typed = TRUE; break;
3483 case 'i': insert = TRUE; break;
3484 case 'x': execute = TRUE; break;
3485 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003486 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003487 }
3488 }
3489 }
3490
3491 if (*keys != NUL || execute)
3492 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003493 // Need to escape K_SPECIAL and CSI before putting the string in the
3494 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003495 keys_esc = vim_strsave_escape_csi(keys);
3496 if (keys_esc != NULL)
3497 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003498 if (lowlevel)
3499 {
3500#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003501 int idx;
3502 int len = (int)STRLEN(keys);
3503
3504 for (idx = 0; idx < len; ++idx)
3505 {
3506 // if a CTRL-C was typed, set got_int, similar to what
3507 // happens in fill_input_buf()
3508 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3509 got_int = TRUE;
3510 add_to_input_buf(keys + idx, 1);
3511 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003512#else
3513 emsg(_("E980: lowlevel input not supported"));
3514#endif
3515 }
3516 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003517 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003518 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003519 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003520 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003521#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003522 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003523#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003524 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003525 typebuf_was_filled = TRUE;
3526 }
3527 vim_free(keys_esc);
3528
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003529 if (execute)
3530 {
3531 int save_msg_scroll = msg_scroll;
3532
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003533 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003534 msg_scroll = FALSE;
3535
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003536 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003537 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003538 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003539 ++in_feedkeys;
3540 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003541 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003542 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003543 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003544 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003545 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003546 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003547
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003548 msg_scroll |= save_msg_scroll;
3549 }
3550 }
3551 }
3552}
3553
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003554#ifdef FEAT_FLOAT
3555/*
3556 * "float2nr({float})" function
3557 */
3558 static void
3559f_float2nr(typval_T *argvars, typval_T *rettv)
3560{
3561 float_T f = 0.0;
3562
3563 if (get_float_arg(argvars, &f) == OK)
3564 {
Bram Moolenaar37184272020-05-23 19:30:05 +02003565 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003566 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02003567 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003568 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003569 else
3570 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003571 }
3572}
3573
3574/*
3575 * "floor({float})" function
3576 */
3577 static void
3578f_floor(typval_T *argvars, typval_T *rettv)
3579{
3580 float_T f = 0.0;
3581
3582 rettv->v_type = VAR_FLOAT;
3583 if (get_float_arg(argvars, &f) == OK)
3584 rettv->vval.v_float = floor(f);
3585 else
3586 rettv->vval.v_float = 0.0;
3587}
3588
3589/*
3590 * "fmod()" function
3591 */
3592 static void
3593f_fmod(typval_T *argvars, typval_T *rettv)
3594{
3595 float_T fx = 0.0, fy = 0.0;
3596
3597 rettv->v_type = VAR_FLOAT;
3598 if (get_float_arg(argvars, &fx) == OK
3599 && get_float_arg(&argvars[1], &fy) == OK)
3600 rettv->vval.v_float = fmod(fx, fy);
3601 else
3602 rettv->vval.v_float = 0.0;
3603}
3604#endif
3605
3606/*
3607 * "fnameescape({string})" function
3608 */
3609 static void
3610f_fnameescape(typval_T *argvars, typval_T *rettv)
3611{
3612 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003613 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003614 rettv->v_type = VAR_STRING;
3615}
3616
3617/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003618 * "foreground()" function
3619 */
3620 static void
3621f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3622{
3623#ifdef FEAT_GUI
3624 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003625 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003626 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003627 return;
3628 }
3629#endif
3630#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003631 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003632#endif
3633}
3634
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003635 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003636common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003637{
3638 char_u *s;
3639 char_u *name;
3640 int use_string = FALSE;
3641 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003642 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003643 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003644
3645 if (argvars[0].v_type == VAR_FUNC)
3646 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003647 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003648 s = argvars[0].vval.v_string;
3649 }
3650 else if (argvars[0].v_type == VAR_PARTIAL
3651 && argvars[0].vval.v_partial != NULL)
3652 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003653 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003654 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003655 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003656 }
3657 else
3658 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003659 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003660 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003661 use_string = TRUE;
3662 }
3663
Bram Moolenaar843b8842016-08-21 14:36:15 +02003664 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003665 {
3666 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003667 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003668 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3669 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003670 if (*name != NUL)
3671 s = NULL;
3672 }
3673
Bram Moolenaar843b8842016-08-21 14:36:15 +02003674 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3675 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003676 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003677 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003678 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003679 ? find_func(trans_name, is_global, NULL) == NULL
3680 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003681 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003682 else
3683 {
3684 int dict_idx = 0;
3685 int arg_idx = 0;
3686 list_T *list = NULL;
3687
3688 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3689 {
3690 char sid_buf[25];
3691 int off = *s == 's' ? 2 : 5;
3692
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003693 // Expand s: and <SID> into <SNR>nr_, so that the function can
3694 // also be called from another script. Using trans_function_name()
3695 // would also work, but some plugins depend on the name being
3696 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003697 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003698 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003699 if (name != NULL)
3700 {
3701 STRCPY(name, sid_buf);
3702 STRCAT(name, s + off);
3703 }
3704 }
3705 else
3706 name = vim_strsave(s);
3707
3708 if (argvars[1].v_type != VAR_UNKNOWN)
3709 {
3710 if (argvars[2].v_type != VAR_UNKNOWN)
3711 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003712 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003713 arg_idx = 1;
3714 dict_idx = 2;
3715 }
3716 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003717 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003718 dict_idx = 1;
3719 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003720 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003721 arg_idx = 1;
3722 if (dict_idx > 0)
3723 {
3724 if (argvars[dict_idx].v_type != VAR_DICT)
3725 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003726 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003727 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003728 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003729 }
3730 if (argvars[dict_idx].vval.v_dict == NULL)
3731 dict_idx = 0;
3732 }
3733 if (arg_idx > 0)
3734 {
3735 if (argvars[arg_idx].v_type != VAR_LIST)
3736 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003737 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003738 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003739 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003740 }
3741 list = argvars[arg_idx].vval.v_list;
3742 if (list == NULL || list->lv_len == 0)
3743 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003744 else if (list->lv_len > MAX_FUNC_ARGS)
3745 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003746 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003747 vim_free(name);
3748 goto theend;
3749 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003750 }
3751 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003752 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003753 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003754 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003755
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003756 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003757 if (pt == NULL)
3758 vim_free(name);
3759 else
3760 {
3761 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3762 {
3763 listitem_T *li;
3764 int i = 0;
3765 int arg_len = 0;
3766 int lv_len = 0;
3767
3768 if (arg_pt != NULL)
3769 arg_len = arg_pt->pt_argc;
3770 if (list != NULL)
3771 lv_len = list->lv_len;
3772 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003773 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003774 if (pt->pt_argv == NULL)
3775 {
3776 vim_free(pt);
3777 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003778 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003779 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003780 for (i = 0; i < arg_len; i++)
3781 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3782 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003783 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003784 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003785 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003786 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003787 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003788 }
3789
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003790 // For "function(dict.func, [], dict)" and "func" is a partial
3791 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003792 if (dict_idx > 0)
3793 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003794 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003795 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3796 ++pt->pt_dict->dv_refcount;
3797 }
3798 else if (arg_pt != NULL)
3799 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003800 // If the dict was bound automatically the result is also
3801 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003802 pt->pt_dict = arg_pt->pt_dict;
3803 pt->pt_auto = arg_pt->pt_auto;
3804 if (pt->pt_dict != NULL)
3805 ++pt->pt_dict->dv_refcount;
3806 }
3807
3808 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003809 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3810 {
3811 pt->pt_func = arg_pt->pt_func;
3812 func_ptr_ref(pt->pt_func);
3813 vim_free(name);
3814 }
3815 else if (is_funcref)
3816 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003817 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003818 func_ptr_ref(pt->pt_func);
3819 vim_free(name);
3820 }
3821 else
3822 {
3823 pt->pt_name = name;
3824 func_ref(name);
3825 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003826 }
3827 rettv->v_type = VAR_PARTIAL;
3828 rettv->vval.v_partial = pt;
3829 }
3830 else
3831 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003832 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003833 rettv->v_type = VAR_FUNC;
3834 rettv->vval.v_string = name;
3835 func_ref(name);
3836 }
3837 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003838theend:
3839 vim_free(trans_name);
3840}
3841
3842/*
3843 * "funcref()" function
3844 */
3845 static void
3846f_funcref(typval_T *argvars, typval_T *rettv)
3847{
3848 common_function(argvars, rettv, TRUE);
3849}
3850
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003851 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003852ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003853{
3854 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3855 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003856 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003857}
3858
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003859/*
3860 * "function()" function
3861 */
3862 static void
3863f_function(typval_T *argvars, typval_T *rettv)
3864{
3865 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003866}
3867
3868/*
3869 * "garbagecollect()" function
3870 */
3871 static void
3872f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3873{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003874 // This is postponed until we are back at the toplevel, because we may be
3875 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003876 want_garbage_collect = TRUE;
3877
Bram Moolenaar2df47312020-09-05 17:30:44 +02003878 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003879 garbage_collect_at_exit = TRUE;
3880}
3881
3882/*
3883 * "get()" function
3884 */
3885 static void
3886f_get(typval_T *argvars, typval_T *rettv)
3887{
3888 listitem_T *li;
3889 list_T *l;
3890 dictitem_T *di;
3891 dict_T *d;
3892 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003893 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003894
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003895 if (argvars[0].v_type == VAR_BLOB)
3896 {
3897 int error = FALSE;
3898 int idx = tv_get_number_chk(&argvars[1], &error);
3899
3900 if (!error)
3901 {
3902 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003903 if (idx < 0)
3904 idx = blob_len(argvars[0].vval.v_blob) + idx;
3905 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3906 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003907 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003908 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003909 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003910 tv = rettv;
3911 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003912 }
3913 }
3914 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003915 {
3916 if ((l = argvars[0].vval.v_list) != NULL)
3917 {
3918 int error = FALSE;
3919
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003920 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003921 if (!error && li != NULL)
3922 tv = &li->li_tv;
3923 }
3924 }
3925 else if (argvars[0].v_type == VAR_DICT)
3926 {
3927 if ((d = argvars[0].vval.v_dict) != NULL)
3928 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003929 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003930 if (di != NULL)
3931 tv = &di->di_tv;
3932 }
3933 }
3934 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3935 {
3936 partial_T *pt;
3937 partial_T fref_pt;
3938
3939 if (argvars[0].v_type == VAR_PARTIAL)
3940 pt = argvars[0].vval.v_partial;
3941 else
3942 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003943 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003944 fref_pt.pt_name = argvars[0].vval.v_string;
3945 pt = &fref_pt;
3946 }
3947
3948 if (pt != NULL)
3949 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003950 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003951 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003952
3953 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3954 {
3955 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003956 n = partial_name(pt);
3957 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003958 rettv->vval.v_string = NULL;
3959 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003960 {
3961 rettv->vval.v_string = vim_strsave(n);
3962 if (rettv->v_type == VAR_FUNC)
3963 func_ref(rettv->vval.v_string);
3964 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003965 }
3966 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003967 {
3968 what_is_dict = TRUE;
3969 if (pt->pt_dict != NULL)
3970 rettv_dict_set(rettv, pt->pt_dict);
3971 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003972 else if (STRCMP(what, "args") == 0)
3973 {
3974 rettv->v_type = VAR_LIST;
3975 if (rettv_list_alloc(rettv) == OK)
3976 {
3977 int i;
3978
3979 for (i = 0; i < pt->pt_argc; ++i)
3980 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3981 }
3982 }
3983 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003984 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003985
3986 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3987 // third argument
3988 if (!what_is_dict)
3989 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003990 }
3991 }
3992 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003993 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003994
3995 if (tv == NULL)
3996 {
3997 if (argvars[2].v_type != VAR_UNKNOWN)
3998 copy_tv(&argvars[2], rettv);
3999 }
4000 else
4001 copy_tv(tv, rettv);
4002}
4003
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004004/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004005 * "getchangelist()" function
4006 */
4007 static void
4008f_getchangelist(typval_T *argvars, typval_T *rettv)
4009{
4010#ifdef FEAT_JUMPLIST
4011 buf_T *buf;
4012 int i;
4013 list_T *l;
4014 dict_T *d;
4015#endif
4016
4017 if (rettv_list_alloc(rettv) != OK)
4018 return;
4019
4020#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02004021 if (argvars[0].v_type == VAR_UNKNOWN)
4022 buf = curbuf;
4023 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02004024 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004025 if (buf == NULL)
4026 return;
4027
4028 l = list_alloc();
4029 if (l == NULL)
4030 return;
4031
4032 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4033 return;
4034 /*
4035 * The current window change list index tracks only the position in the
4036 * current buffer change list. For other buffers, use the change list
4037 * length as the current index.
4038 */
4039 list_append_number(rettv->vval.v_list,
4040 (varnumber_T)((buf == curwin->w_buffer)
4041 ? curwin->w_changelistidx : buf->b_changelistlen));
4042
4043 for (i = 0; i < buf->b_changelistlen; ++i)
4044 {
4045 if (buf->b_changelist[i].lnum == 0)
4046 continue;
4047 if ((d = dict_alloc()) == NULL)
4048 return;
4049 if (list_append_dict(l, d) == FAIL)
4050 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004051 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4052 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004053 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004054 }
4055#endif
4056}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004057
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004058 static void
4059getpos_both(
4060 typval_T *argvars,
4061 typval_T *rettv,
4062 int getcurpos,
4063 int charcol)
4064{
4065 pos_T *fp = NULL;
4066 pos_T pos;
4067 win_T *wp = curwin;
4068 list_T *l;
4069 int fnum = -1;
4070
4071 if (rettv_list_alloc(rettv) == OK)
4072 {
4073 l = rettv->vval.v_list;
4074 if (getcurpos)
4075 {
4076 if (argvars[0].v_type != VAR_UNKNOWN)
4077 {
4078 wp = find_win_by_nr_or_id(&argvars[0]);
4079 if (wp != NULL)
4080 fp = &wp->w_cursor;
4081 }
4082 else
4083 fp = &curwin->w_cursor;
4084 if (fp != NULL && charcol)
4085 {
4086 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01004087 pos.col =
4088 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004089 fp = &pos;
4090 }
4091 }
4092 else
4093 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
4094 if (fnum != -1)
4095 list_append_number(l, (varnumber_T)fnum);
4096 else
4097 list_append_number(l, (varnumber_T)0);
4098 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4099 : (varnumber_T)0);
4100 list_append_number(l, (fp != NULL)
4101 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4102 : (varnumber_T)0);
4103 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
4104 (varnumber_T)0);
4105 if (getcurpos)
4106 {
4107 int save_set_curswant = curwin->w_set_curswant;
4108 colnr_T save_curswant = curwin->w_curswant;
4109 colnr_T save_virtcol = curwin->w_virtcol;
4110
4111 if (wp == curwin)
4112 update_curswant();
4113 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
4114 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
4115
4116 // Do not change "curswant", as it is unexpected that a get
4117 // function has a side effect.
4118 if (wp == curwin && save_set_curswant)
4119 {
4120 curwin->w_set_curswant = save_set_curswant;
4121 curwin->w_curswant = save_curswant;
4122 curwin->w_virtcol = save_virtcol;
4123 curwin->w_valid &= ~VALID_VIRTCOL;
4124 }
4125 }
4126 }
4127 else
4128 rettv->vval.v_number = FALSE;
4129}
4130
4131/*
4132 * "getcharpos()" function
4133 */
4134 static void
4135f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
4136{
4137 getpos_both(argvars, rettv, FALSE, TRUE);
4138}
4139
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004140/*
4141 * "getcharsearch()" function
4142 */
4143 static void
4144f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4145{
4146 if (rettv_dict_alloc(rettv) != FAIL)
4147 {
4148 dict_T *dict = rettv->vval.v_dict;
4149
Bram Moolenaare0be1672018-07-08 16:50:37 +02004150 dict_add_string(dict, "char", last_csearch());
4151 dict_add_number(dict, "forward", last_csearch_forward());
4152 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004153 }
4154}
4155
4156/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004157 * "getenv()" function
4158 */
4159 static void
4160f_getenv(typval_T *argvars, typval_T *rettv)
4161{
4162 int mustfree = FALSE;
4163 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4164
4165 if (p == NULL)
4166 {
4167 rettv->v_type = VAR_SPECIAL;
4168 rettv->vval.v_number = VVAL_NULL;
4169 return;
4170 }
4171 if (!mustfree)
4172 p = vim_strsave(p);
4173 rettv->vval.v_string = p;
4174 rettv->v_type = VAR_STRING;
4175}
4176
4177/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004178 * "getfontname()" function
4179 */
4180 static void
4181f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4182{
4183 rettv->v_type = VAR_STRING;
4184 rettv->vval.v_string = NULL;
4185#ifdef FEAT_GUI
4186 if (gui.in_use)
4187 {
4188 GuiFont font;
4189 char_u *name = NULL;
4190
4191 if (argvars[0].v_type == VAR_UNKNOWN)
4192 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004193 // Get the "Normal" font. Either the name saved by
4194 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004195 font = gui.norm_font;
4196 name = hl_get_font_name();
4197 }
4198 else
4199 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004200 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004201 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004202 return;
4203 font = gui_mch_get_font(name, FALSE);
4204 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004205 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004206 }
4207 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4208 if (argvars[0].v_type != VAR_UNKNOWN)
4209 gui_mch_free_font(font);
4210 }
4211#endif
4212}
4213
4214/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004215 * "getjumplist()" function
4216 */
4217 static void
4218f_getjumplist(typval_T *argvars, typval_T *rettv)
4219{
4220#ifdef FEAT_JUMPLIST
4221 win_T *wp;
4222 int i;
4223 list_T *l;
4224 dict_T *d;
4225#endif
4226
4227 if (rettv_list_alloc(rettv) != OK)
4228 return;
4229
4230#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004231 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004232 if (wp == NULL)
4233 return;
4234
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004235 cleanup_jumplist(wp, TRUE);
4236
Bram Moolenaar4f505882018-02-10 21:06:32 +01004237 l = list_alloc();
4238 if (l == NULL)
4239 return;
4240
4241 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4242 return;
4243 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4244
4245 for (i = 0; i < wp->w_jumplistlen; ++i)
4246 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004247 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4248 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004249 if ((d = dict_alloc()) == NULL)
4250 return;
4251 if (list_append_dict(l, d) == FAIL)
4252 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004253 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4254 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004255 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004256 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004257 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004258 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004259 }
4260#endif
4261}
4262
4263/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004264 * "getpid()" function
4265 */
4266 static void
4267f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4268{
4269 rettv->vval.v_number = mch_get_pid();
4270}
4271
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004272/*
4273 * "getcurpos()" function
4274 */
4275 static void
4276f_getcurpos(typval_T *argvars, typval_T *rettv)
4277{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004278 getpos_both(argvars, rettv, TRUE, FALSE);
4279}
4280
4281 static void
4282f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4283{
4284 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004285}
4286
4287/*
4288 * "getpos(string)" function
4289 */
4290 static void
4291f_getpos(typval_T *argvars, typval_T *rettv)
4292{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004293 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004294}
4295
4296/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004297 * "getreg()" function
4298 */
4299 static void
4300f_getreg(typval_T *argvars, typval_T *rettv)
4301{
4302 char_u *strregname;
4303 int regname;
4304 int arg2 = FALSE;
4305 int return_list = FALSE;
4306 int error = FALSE;
4307
4308 if (argvars[0].v_type != VAR_UNKNOWN)
4309 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004310 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004311 if (strregname == NULL)
4312 error = TRUE;
4313 else if (in_vim9script() && STRLEN(strregname) > 1)
4314 {
4315 semsg(_(e_register_name_must_be_one_char_str), strregname);
4316 error = TRUE;
4317 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004318 if (argvars[1].v_type != VAR_UNKNOWN)
4319 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004320 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004321 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004322 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004323 }
4324 }
4325 else
4326 strregname = get_vim_var_str(VV_REG);
4327
4328 if (error)
4329 return;
4330
4331 regname = (strregname == NULL ? '"' : *strregname);
4332 if (regname == 0)
4333 regname = '"';
4334
4335 if (return_list)
4336 {
4337 rettv->v_type = VAR_LIST;
4338 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4339 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4340 if (rettv->vval.v_list == NULL)
4341 (void)rettv_list_alloc(rettv);
4342 else
4343 ++rettv->vval.v_list->lv_refcount;
4344 }
4345 else
4346 {
4347 rettv->v_type = VAR_STRING;
4348 rettv->vval.v_string = get_reg_contents(regname,
4349 arg2 ? GREG_EXPR_SRC : 0);
4350 }
4351}
4352
4353/*
4354 * "getregtype()" function
4355 */
4356 static void
4357f_getregtype(typval_T *argvars, typval_T *rettv)
4358{
4359 char_u *strregname;
4360 int regname;
4361 char_u buf[NUMBUFLEN + 2];
4362 long reglen = 0;
4363
4364 if (argvars[0].v_type != VAR_UNKNOWN)
4365 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004366 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004367 if (strregname != NULL && in_vim9script() && STRLEN(strregname) > 1)
4368 {
4369 semsg(_(e_register_name_must_be_one_char_str), strregname);
4370 strregname = NULL;
4371 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004372 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004373 {
4374 rettv->v_type = VAR_STRING;
4375 rettv->vval.v_string = NULL;
4376 return;
4377 }
4378 }
4379 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004380 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004381 strregname = get_vim_var_str(VV_REG);
4382
4383 regname = (strregname == NULL ? '"' : *strregname);
4384 if (regname == 0)
4385 regname = '"';
4386
4387 buf[0] = NUL;
4388 buf[1] = NUL;
4389 switch (get_reg_type(regname, &reglen))
4390 {
4391 case MLINE: buf[0] = 'V'; break;
4392 case MCHAR: buf[0] = 'v'; break;
4393 case MBLOCK:
4394 buf[0] = Ctrl_V;
4395 sprintf((char *)buf + 1, "%ld", reglen + 1);
4396 break;
4397 }
4398 rettv->v_type = VAR_STRING;
4399 rettv->vval.v_string = vim_strsave(buf);
4400}
4401
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004402/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004403 * "gettagstack()" function
4404 */
4405 static void
4406f_gettagstack(typval_T *argvars, typval_T *rettv)
4407{
4408 win_T *wp = curwin; // default is current window
4409
4410 if (rettv_dict_alloc(rettv) != OK)
4411 return;
4412
4413 if (argvars[0].v_type != VAR_UNKNOWN)
4414 {
4415 wp = find_win_by_nr_or_id(&argvars[0]);
4416 if (wp == NULL)
4417 return;
4418 }
4419
4420 get_tagstack(wp, rettv->vval.v_dict);
4421}
4422
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004423/*
4424 * "gettext()" function
4425 */
4426 static void
4427f_gettext(typval_T *argvars, typval_T *rettv)
4428{
4429 if (argvars[0].v_type != VAR_STRING
4430 || argvars[0].vval.v_string == NULL
4431 || *argvars[0].vval.v_string == NUL)
4432 {
4433 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4434 }
4435 else
4436 {
4437 rettv->v_type = VAR_STRING;
4438 rettv->vval.v_string = vim_strsave(
4439 (char_u *)_(argvars[0].vval.v_string));
4440 }
4441}
4442
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004443// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004444#include "version.h"
4445
4446/*
4447 * "has()" function
4448 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004449 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004450f_has(typval_T *argvars, typval_T *rettv)
4451{
4452 int i;
4453 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004454 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004455 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004456 typedef struct {
4457 char *name;
4458 short present;
4459 } has_item_T;
4460 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004461 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004462 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004463#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004464 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004465#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004466 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004467#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004468 },
4469 {"arp",
4470#if defined(AMIGA) && defined(FEAT_ARP)
4471 1
4472#else
4473 0
4474#endif
4475 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004476 {"haiku",
4477#ifdef __HAIKU__
4478 1
4479#else
4480 0
4481#endif
4482 },
4483 {"bsd",
4484#if defined(BSD) && !defined(MACOS_X)
4485 1
4486#else
4487 0
4488#endif
4489 },
4490 {"hpux",
4491#ifdef hpux
4492 1
4493#else
4494 0
4495#endif
4496 },
4497 {"linux",
4498#ifdef __linux__
4499 1
4500#else
4501 0
4502#endif
4503 },
4504 {"mac", // Mac OS X (and, once, Mac OS Classic)
4505#ifdef MACOS_X
4506 1
4507#else
4508 0
4509#endif
4510 },
4511 {"osx", // Mac OS X
4512#ifdef MACOS_X
4513 1
4514#else
4515 0
4516#endif
4517 },
4518 {"macunix", // Mac OS X, with the darwin feature
4519#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4520 1
4521#else
4522 0
4523#endif
4524 },
4525 {"osxdarwin", // synonym for macunix
4526#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4527 1
4528#else
4529 0
4530#endif
4531 },
4532 {"qnx",
4533#ifdef __QNX__
4534 1
4535#else
4536 0
4537#endif
4538 },
4539 {"sun",
4540#ifdef SUN_SYSTEM
4541 1
4542#else
4543 0
4544#endif
4545 },
4546 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004547#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004548 1
4549#else
4550 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004551#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004552 },
4553 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004554#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004555 1
4556#else
4557 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004558#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004559 },
4560 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004561#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004562 1
4563#else
4564 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004565#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004566 },
4567 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004568#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004569 1
4570#else
4571 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004572#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004573 },
4574 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004575#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004576 1
4577#else
4578 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004579#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004580 },
4581 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004582#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004583 1
4584#else
4585 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004586#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004587 },
4588 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004589#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004590 1
4591#else
4592 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004593#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004594 },
4595 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004596#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004597 1
4598#else
4599 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004600#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004601 },
4602 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004603#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004604 1
4605#else
4606 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004607#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004608 },
4609 {"autocmd", 1},
4610 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004611#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004612 1
4613#else
4614 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004615#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004616 },
4617 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004618#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004619 1
4620#else
4621 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004622#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004623 },
4624 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004625#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004626 1
4627#else
4628 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004629#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004630 },
4631 {"balloon_multiline",
4632#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4633 // MS-Windows requires runtime check, see below
4634 1
4635#else
4636 0
4637#endif
4638 },
4639 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004640#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004641 1
4642#else
4643 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004644#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004645 },
4646 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004647#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004648 1
4649#else
4650 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004651#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004652 },
4653 {"all_builtin_terms",
4654#if defined(ALL_BUILTIN_TCAPS)
4655 1
4656#else
4657 0
4658#endif
4659 },
4660 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004661#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004662 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004663 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004664 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004665#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004666 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004667#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004668 },
4669 {"byte_offset",
4670#ifdef FEAT_BYTEOFF
4671 1
4672#else
4673 0
4674#endif
4675 },
4676 {"channel",
4677#ifdef FEAT_JOB_CHANNEL
4678 1
4679#else
4680 0
4681#endif
4682 },
4683 {"cindent",
4684#ifdef FEAT_CINDENT
4685 1
4686#else
4687 0
4688#endif
4689 },
4690 {"clientserver",
4691#ifdef FEAT_CLIENTSERVER
4692 1
4693#else
4694 0
4695#endif
4696 },
4697 {"clipboard",
4698#ifdef FEAT_CLIPBOARD
4699 1
4700#else
4701 0
4702#endif
4703 },
4704 {"cmdline_compl", 1},
4705 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004706 {"cmdwin",
4707#ifdef FEAT_CMDWIN
4708 1
4709#else
4710 0
4711#endif
4712 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004713 {"comments", 1},
4714 {"conceal",
4715#ifdef FEAT_CONCEAL
4716 1
4717#else
4718 0
4719#endif
4720 },
4721 {"cryptv",
4722#ifdef FEAT_CRYPT
4723 1
4724#else
4725 0
4726#endif
4727 },
4728 {"crypt-blowfish",
4729#ifdef FEAT_CRYPT
4730 1
4731#else
4732 0
4733#endif
4734 },
4735 {"crypt-blowfish2",
4736#ifdef FEAT_CRYPT
4737 1
4738#else
4739 0
4740#endif
4741 },
4742 {"cscope",
4743#ifdef FEAT_CSCOPE
4744 1
4745#else
4746 0
4747#endif
4748 },
4749 {"cursorbind", 1},
4750 {"cursorshape",
4751#ifdef CURSOR_SHAPE
4752 1
4753#else
4754 0
4755#endif
4756 },
4757 {"debug",
4758#ifdef DEBUG
4759 1
4760#else
4761 0
4762#endif
4763 },
4764 {"dialog_con",
4765#ifdef FEAT_CON_DIALOG
4766 1
4767#else
4768 0
4769#endif
4770 },
4771 {"dialog_gui",
4772#ifdef FEAT_GUI_DIALOG
4773 1
4774#else
4775 0
4776#endif
4777 },
4778 {"diff",
4779#ifdef FEAT_DIFF
4780 1
4781#else
4782 0
4783#endif
4784 },
4785 {"digraphs",
4786#ifdef FEAT_DIGRAPHS
4787 1
4788#else
4789 0
4790#endif
4791 },
4792 {"directx",
4793#ifdef FEAT_DIRECTX
4794 1
4795#else
4796 0
4797#endif
4798 },
4799 {"dnd",
4800#ifdef FEAT_DND
4801 1
4802#else
4803 0
4804#endif
4805 },
4806 {"emacs_tags",
4807#ifdef FEAT_EMACS_TAGS
4808 1
4809#else
4810 0
4811#endif
4812 },
4813 {"eval", 1}, // always present, of course!
4814 {"ex_extra", 1}, // graduated feature
4815 {"extra_search",
4816#ifdef FEAT_SEARCH_EXTRA
4817 1
4818#else
4819 0
4820#endif
4821 },
4822 {"file_in_path",
4823#ifdef FEAT_SEARCHPATH
4824 1
4825#else
4826 0
4827#endif
4828 },
4829 {"filterpipe",
4830#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4831 1
4832#else
4833 0
4834#endif
4835 },
4836 {"find_in_path",
4837#ifdef FEAT_FIND_ID
4838 1
4839#else
4840 0
4841#endif
4842 },
4843 {"float",
4844#ifdef FEAT_FLOAT
4845 1
4846#else
4847 0
4848#endif
4849 },
4850 {"folding",
4851#ifdef FEAT_FOLDING
4852 1
4853#else
4854 0
4855#endif
4856 },
4857 {"footer",
4858#ifdef FEAT_FOOTER
4859 1
4860#else
4861 0
4862#endif
4863 },
4864 {"fork",
4865#if !defined(USE_SYSTEM) && defined(UNIX)
4866 1
4867#else
4868 0
4869#endif
4870 },
4871 {"gettext",
4872#ifdef FEAT_GETTEXT
4873 1
4874#else
4875 0
4876#endif
4877 },
4878 {"gui",
4879#ifdef FEAT_GUI
4880 1
4881#else
4882 0
4883#endif
4884 },
4885 {"gui_neXtaw",
4886#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4887 1
4888#else
4889 0
4890#endif
4891 },
4892 {"gui_athena",
4893#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4894 1
4895#else
4896 0
4897#endif
4898 },
4899 {"gui_gtk",
4900#ifdef FEAT_GUI_GTK
4901 1
4902#else
4903 0
4904#endif
4905 },
4906 {"gui_gtk2",
4907#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4908 1
4909#else
4910 0
4911#endif
4912 },
4913 {"gui_gtk3",
4914#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4915 1
4916#else
4917 0
4918#endif
4919 },
4920 {"gui_gnome",
4921#ifdef FEAT_GUI_GNOME
4922 1
4923#else
4924 0
4925#endif
4926 },
4927 {"gui_haiku",
4928#ifdef FEAT_GUI_HAIKU
4929 1
4930#else
4931 0
4932#endif
4933 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004934 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004935 {"gui_motif",
4936#ifdef FEAT_GUI_MOTIF
4937 1
4938#else
4939 0
4940#endif
4941 },
4942 {"gui_photon",
4943#ifdef FEAT_GUI_PHOTON
4944 1
4945#else
4946 0
4947#endif
4948 },
4949 {"gui_win32",
4950#ifdef FEAT_GUI_MSWIN
4951 1
4952#else
4953 0
4954#endif
4955 },
4956 {"iconv",
4957#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4958 1
4959#else
4960 0
4961#endif
4962 },
4963 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004964 {"ipv6",
4965#ifdef FEAT_IPV6
4966 1
4967#else
4968 0
4969#endif
4970 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004971 {"job",
4972#ifdef FEAT_JOB_CHANNEL
4973 1
4974#else
4975 0
4976#endif
4977 },
4978 {"jumplist",
4979#ifdef FEAT_JUMPLIST
4980 1
4981#else
4982 0
4983#endif
4984 },
4985 {"keymap",
4986#ifdef FEAT_KEYMAP
4987 1
4988#else
4989 0
4990#endif
4991 },
4992 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4993 {"langmap",
4994#ifdef FEAT_LANGMAP
4995 1
4996#else
4997 0
4998#endif
4999 },
5000 {"libcall",
5001#ifdef FEAT_LIBCALL
5002 1
5003#else
5004 0
5005#endif
5006 },
5007 {"linebreak",
5008#ifdef FEAT_LINEBREAK
5009 1
5010#else
5011 0
5012#endif
5013 },
5014 {"lispindent",
5015#ifdef FEAT_LISP
5016 1
5017#else
5018 0
5019#endif
5020 },
5021 {"listcmds", 1},
5022 {"localmap", 1},
5023 {"lua",
5024#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
5025 1
5026#else
5027 0
5028#endif
5029 },
5030 {"menu",
5031#ifdef FEAT_MENU
5032 1
5033#else
5034 0
5035#endif
5036 },
5037 {"mksession",
5038#ifdef FEAT_SESSION
5039 1
5040#else
5041 0
5042#endif
5043 },
5044 {"modify_fname", 1},
5045 {"mouse", 1},
5046 {"mouseshape",
5047#ifdef FEAT_MOUSESHAPE
5048 1
5049#else
5050 0
5051#endif
5052 },
5053 {"mouse_dec",
5054#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
5055 1
5056#else
5057 0
5058#endif
5059 },
5060 {"mouse_gpm",
5061#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
5062 1
5063#else
5064 0
5065#endif
5066 },
5067 {"mouse_jsbterm",
5068#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
5069 1
5070#else
5071 0
5072#endif
5073 },
5074 {"mouse_netterm",
5075#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
5076 1
5077#else
5078 0
5079#endif
5080 },
5081 {"mouse_pterm",
5082#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
5083 1
5084#else
5085 0
5086#endif
5087 },
5088 {"mouse_sgr",
5089#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5090 1
5091#else
5092 0
5093#endif
5094 },
5095 {"mouse_sysmouse",
5096#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
5097 1
5098#else
5099 0
5100#endif
5101 },
5102 {"mouse_urxvt",
5103#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
5104 1
5105#else
5106 0
5107#endif
5108 },
5109 {"mouse_xterm",
5110#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5111 1
5112#else
5113 0
5114#endif
5115 },
5116 {"multi_byte", 1},
5117 {"multi_byte_ime",
5118#ifdef FEAT_MBYTE_IME
5119 1
5120#else
5121 0
5122#endif
5123 },
5124 {"multi_lang",
5125#ifdef FEAT_MULTI_LANG
5126 1
5127#else
5128 0
5129#endif
5130 },
5131 {"mzscheme",
5132#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
5133 1
5134#else
5135 0
5136#endif
5137 },
5138 {"num64", 1},
5139 {"ole",
5140#ifdef FEAT_OLE
5141 1
5142#else
5143 0
5144#endif
5145 },
5146 {"packages",
5147#ifdef FEAT_EVAL
5148 1
5149#else
5150 0
5151#endif
5152 },
5153 {"path_extra",
5154#ifdef FEAT_PATH_EXTRA
5155 1
5156#else
5157 0
5158#endif
5159 },
5160 {"perl",
5161#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
5162 1
5163#else
5164 0
5165#endif
5166 },
5167 {"persistent_undo",
5168#ifdef FEAT_PERSISTENT_UNDO
5169 1
5170#else
5171 0
5172#endif
5173 },
5174 {"python_compiled",
5175#if defined(FEAT_PYTHON)
5176 1
5177#else
5178 0
5179#endif
5180 },
5181 {"python_dynamic",
5182#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
5183 1
5184#else
5185 0
5186#endif
5187 },
5188 {"python",
5189#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
5190 1
5191#else
5192 0
5193#endif
5194 },
5195 {"pythonx",
5196#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
5197 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
5198 1
5199#else
5200 0
5201#endif
5202 },
5203 {"python3_compiled",
5204#if defined(FEAT_PYTHON3)
5205 1
5206#else
5207 0
5208#endif
5209 },
5210 {"python3_dynamic",
5211#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
5212 1
5213#else
5214 0
5215#endif
5216 },
5217 {"python3",
5218#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
5219 1
5220#else
5221 0
5222#endif
5223 },
5224 {"popupwin",
5225#ifdef FEAT_PROP_POPUP
5226 1
5227#else
5228 0
5229#endif
5230 },
5231 {"postscript",
5232#ifdef FEAT_POSTSCRIPT
5233 1
5234#else
5235 0
5236#endif
5237 },
5238 {"printer",
5239#ifdef FEAT_PRINTER
5240 1
5241#else
5242 0
5243#endif
5244 },
5245 {"profile",
5246#ifdef FEAT_PROFILE
5247 1
5248#else
5249 0
5250#endif
5251 },
5252 {"reltime",
5253#ifdef FEAT_RELTIME
5254 1
5255#else
5256 0
5257#endif
5258 },
5259 {"quickfix",
5260#ifdef FEAT_QUICKFIX
5261 1
5262#else
5263 0
5264#endif
5265 },
5266 {"rightleft",
5267#ifdef FEAT_RIGHTLEFT
5268 1
5269#else
5270 0
5271#endif
5272 },
5273 {"ruby",
5274#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5275 1
5276#else
5277 0
5278#endif
5279 },
5280 {"scrollbind", 1},
5281 {"showcmd",
5282#ifdef FEAT_CMDL_INFO
5283 1
5284#else
5285 0
5286#endif
5287 },
5288 {"cmdline_info",
5289#ifdef FEAT_CMDL_INFO
5290 1
5291#else
5292 0
5293#endif
5294 },
5295 {"signs",
5296#ifdef FEAT_SIGNS
5297 1
5298#else
5299 0
5300#endif
5301 },
5302 {"smartindent",
5303#ifdef FEAT_SMARTINDENT
5304 1
5305#else
5306 0
5307#endif
5308 },
5309 {"startuptime",
5310#ifdef STARTUPTIME
5311 1
5312#else
5313 0
5314#endif
5315 },
5316 {"statusline",
5317#ifdef FEAT_STL_OPT
5318 1
5319#else
5320 0
5321#endif
5322 },
5323 {"netbeans_intg",
5324#ifdef FEAT_NETBEANS_INTG
5325 1
5326#else
5327 0
5328#endif
5329 },
5330 {"sound",
5331#ifdef FEAT_SOUND
5332 1
5333#else
5334 0
5335#endif
5336 },
5337 {"spell",
5338#ifdef FEAT_SPELL
5339 1
5340#else
5341 0
5342#endif
5343 },
5344 {"syntax",
5345#ifdef FEAT_SYN_HL
5346 1
5347#else
5348 0
5349#endif
5350 },
5351 {"system",
5352#if defined(USE_SYSTEM) || !defined(UNIX)
5353 1
5354#else
5355 0
5356#endif
5357 },
5358 {"tag_binary",
5359#ifdef FEAT_TAG_BINS
5360 1
5361#else
5362 0
5363#endif
5364 },
5365 {"tcl",
5366#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5367 1
5368#else
5369 0
5370#endif
5371 },
5372 {"termguicolors",
5373#ifdef FEAT_TERMGUICOLORS
5374 1
5375#else
5376 0
5377#endif
5378 },
5379 {"terminal",
5380#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5381 1
5382#else
5383 0
5384#endif
5385 },
5386 {"terminfo",
5387#ifdef TERMINFO
5388 1
5389#else
5390 0
5391#endif
5392 },
5393 {"termresponse",
5394#ifdef FEAT_TERMRESPONSE
5395 1
5396#else
5397 0
5398#endif
5399 },
5400 {"textobjects",
5401#ifdef FEAT_TEXTOBJ
5402 1
5403#else
5404 0
5405#endif
5406 },
5407 {"textprop",
5408#ifdef FEAT_PROP_POPUP
5409 1
5410#else
5411 0
5412#endif
5413 },
5414 {"tgetent",
5415#ifdef HAVE_TGETENT
5416 1
5417#else
5418 0
5419#endif
5420 },
5421 {"timers",
5422#ifdef FEAT_TIMERS
5423 1
5424#else
5425 0
5426#endif
5427 },
5428 {"title",
5429#ifdef FEAT_TITLE
5430 1
5431#else
5432 0
5433#endif
5434 },
5435 {"toolbar",
5436#ifdef FEAT_TOOLBAR
5437 1
5438#else
5439 0
5440#endif
5441 },
5442 {"unnamedplus",
5443#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5444 1
5445#else
5446 0
5447#endif
5448 },
5449 {"user-commands", 1}, // was accidentally included in 5.4
5450 {"user_commands", 1},
5451 {"vartabs",
5452#ifdef FEAT_VARTABS
5453 1
5454#else
5455 0
5456#endif
5457 },
5458 {"vertsplit", 1},
5459 {"viminfo",
5460#ifdef FEAT_VIMINFO
5461 1
5462#else
5463 0
5464#endif
5465 },
5466 {"vimscript-1", 1},
5467 {"vimscript-2", 1},
5468 {"vimscript-3", 1},
5469 {"vimscript-4", 1},
5470 {"virtualedit", 1},
5471 {"visual", 1},
5472 {"visualextra", 1},
5473 {"vreplace", 1},
5474 {"vtp",
5475#ifdef FEAT_VTP
5476 1
5477#else
5478 0
5479#endif
5480 },
5481 {"wildignore",
5482#ifdef FEAT_WILDIGN
5483 1
5484#else
5485 0
5486#endif
5487 },
5488 {"wildmenu",
5489#ifdef FEAT_WILDMENU
5490 1
5491#else
5492 0
5493#endif
5494 },
5495 {"windows", 1},
5496 {"winaltkeys",
5497#ifdef FEAT_WAK
5498 1
5499#else
5500 0
5501#endif
5502 },
5503 {"writebackup",
5504#ifdef FEAT_WRITEBACKUP
5505 1
5506#else
5507 0
5508#endif
5509 },
5510 {"xim",
5511#ifdef FEAT_XIM
5512 1
5513#else
5514 0
5515#endif
5516 },
5517 {"xfontset",
5518#ifdef FEAT_XFONTSET
5519 1
5520#else
5521 0
5522#endif
5523 },
5524 {"xpm",
5525#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5526 1
5527#else
5528 0
5529#endif
5530 },
5531 {"xpm_w32", // for backward compatibility
5532#ifdef FEAT_XPM_W32
5533 1
5534#else
5535 0
5536#endif
5537 },
5538 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005539#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005540 1
5541#else
5542 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005543#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005544 },
5545 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005546#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005547 1
5548#else
5549 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005550#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005551 },
5552 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005553#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005554 1
5555#else
5556 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005557#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005558 },
5559 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005560#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005561 1
5562#else
5563 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005564#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005565 },
5566 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005567#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005568 1
5569#else
5570 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005571#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005572 },
5573 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005574 };
5575
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005576 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005577 for (i = 0; has_list[i].name != NULL; ++i)
5578 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005579 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005580 x = TRUE;
5581 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005582 break;
5583 }
5584
Bram Moolenaar79296512020-03-22 16:17:14 +01005585 // features also in has_list[] but sometimes enabled at runtime
5586 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005587 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005588 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005589 {
5590 // intentionally empty
5591 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005592#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005593 else if (STRICMP(name, "balloon_multiline") == 0)
5594 n = multiline_balloon_available();
5595#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005596#ifdef VIMDLL
5597 else if (STRICMP(name, "filterpipe") == 0)
5598 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005599#endif
5600#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5601 else if (STRICMP(name, "iconv") == 0)
5602 n = iconv_enabled(FALSE);
5603#endif
5604#ifdef DYNAMIC_LUA
5605 else if (STRICMP(name, "lua") == 0)
5606 n = lua_enabled(FALSE);
5607#endif
5608#ifdef DYNAMIC_MZSCHEME
5609 else if (STRICMP(name, "mzscheme") == 0)
5610 n = mzscheme_enabled(FALSE);
5611#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005612#ifdef DYNAMIC_PERL
5613 else if (STRICMP(name, "perl") == 0)
5614 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005615#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005616#ifdef DYNAMIC_PYTHON
5617 else if (STRICMP(name, "python") == 0)
5618 n = python_enabled(FALSE);
5619#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005620#ifdef DYNAMIC_PYTHON3
5621 else if (STRICMP(name, "python3") == 0)
5622 n = python3_enabled(FALSE);
5623#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005624#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5625 else if (STRICMP(name, "pythonx") == 0)
5626 {
5627# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5628 if (p_pyx == 0)
5629 n = python3_enabled(FALSE) || python_enabled(FALSE);
5630 else if (p_pyx == 3)
5631 n = python3_enabled(FALSE);
5632 else if (p_pyx == 2)
5633 n = python_enabled(FALSE);
5634# elif defined(DYNAMIC_PYTHON)
5635 n = python_enabled(FALSE);
5636# elif defined(DYNAMIC_PYTHON3)
5637 n = python3_enabled(FALSE);
5638# endif
5639 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005640#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005641#ifdef DYNAMIC_RUBY
5642 else if (STRICMP(name, "ruby") == 0)
5643 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005644#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005645#ifdef DYNAMIC_TCL
5646 else if (STRICMP(name, "tcl") == 0)
5647 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005648#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005649#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005650 else if (STRICMP(name, "terminal") == 0)
5651 n = terminal_enabled();
5652#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005653 }
5654
Bram Moolenaar79296512020-03-22 16:17:14 +01005655 // features not in has_list[]
5656 if (x == FALSE)
5657 {
5658 if (STRNICMP(name, "patch", 5) == 0)
5659 {
5660 x = TRUE;
5661 if (name[5] == '-'
5662 && STRLEN(name) >= 11
5663 && vim_isdigit(name[6])
5664 && vim_isdigit(name[8])
5665 && vim_isdigit(name[10]))
5666 {
5667 int major = atoi((char *)name + 6);
5668 int minor = atoi((char *)name + 8);
5669
5670 // Expect "patch-9.9.01234".
5671 n = (major < VIM_VERSION_MAJOR
5672 || (major == VIM_VERSION_MAJOR
5673 && (minor < VIM_VERSION_MINOR
5674 || (minor == VIM_VERSION_MINOR
5675 && has_patch(atoi((char *)name + 10))))));
5676 }
5677 else
5678 n = has_patch(atoi((char *)name + 5));
5679 }
5680 else if (STRICMP(name, "vim_starting") == 0)
5681 {
5682 x = TRUE;
5683 n = (starting != 0);
5684 }
5685 else if (STRICMP(name, "ttyin") == 0)
5686 {
5687 x = TRUE;
5688 n = mch_input_isatty();
5689 }
5690 else if (STRICMP(name, "ttyout") == 0)
5691 {
5692 x = TRUE;
5693 n = stdout_isatty;
5694 }
5695 else if (STRICMP(name, "multi_byte_encoding") == 0)
5696 {
5697 x = TRUE;
5698 n = has_mbyte;
5699 }
5700 else if (STRICMP(name, "gui_running") == 0)
5701 {
5702 x = TRUE;
5703#ifdef FEAT_GUI
5704 n = (gui.in_use || gui.starting);
5705#endif
5706 }
5707 else if (STRICMP(name, "browse") == 0)
5708 {
5709 x = TRUE;
5710#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5711 n = gui.in_use; // gui_mch_browse() works when GUI is running
5712#endif
5713 }
5714 else if (STRICMP(name, "syntax_items") == 0)
5715 {
5716 x = TRUE;
5717#ifdef FEAT_SYN_HL
5718 n = syntax_present(curwin);
5719#endif
5720 }
5721 else if (STRICMP(name, "vcon") == 0)
5722 {
5723 x = TRUE;
5724#ifdef FEAT_VTP
5725 n = is_term_win32() && has_vtp_working();
5726#endif
5727 }
5728 else if (STRICMP(name, "netbeans_enabled") == 0)
5729 {
5730 x = TRUE;
5731#ifdef FEAT_NETBEANS_INTG
5732 n = netbeans_active();
5733#endif
5734 }
5735 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5736 {
5737 x = TRUE;
5738#ifdef FEAT_MOUSE_GPM
5739 n = gpm_enabled();
5740#endif
5741 }
5742 else if (STRICMP(name, "conpty") == 0)
5743 {
5744 x = TRUE;
5745#if defined(FEAT_TERMINAL) && defined(MSWIN)
5746 n = use_conpty();
5747#endif
5748 }
5749 else if (STRICMP(name, "clipboard_working") == 0)
5750 {
5751 x = TRUE;
5752#ifdef FEAT_CLIPBOARD
5753 n = clip_star.available;
5754#endif
5755 }
5756 }
5757
Bram Moolenaar04637e22020-09-05 18:45:29 +02005758 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005759 // return whether feature could ever be enabled
5760 rettv->vval.v_number = x;
5761 else
5762 // return whether feature is enabled
5763 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005764}
5765
5766/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005767 * Return TRUE if "feature" can change later.
5768 * Also when checking for the feature has side effects, such as loading a DLL.
5769 */
5770 int
5771dynamic_feature(char_u *feature)
5772{
5773 return (feature == NULL
5774#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5775 || STRICMP(feature, "balloon_multiline") == 0
5776#endif
5777#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5778 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5779#endif
5780#ifdef VIMDLL
5781 || STRICMP(feature, "filterpipe") == 0
5782#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005783#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005784 // this can only change on Unix where the ":gui" command could be
5785 // used.
5786 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5787#endif
5788#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5789 || STRICMP(feature, "iconv") == 0
5790#endif
5791#ifdef DYNAMIC_LUA
5792 || STRICMP(feature, "lua") == 0
5793#endif
5794#ifdef FEAT_MOUSE_GPM
5795 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5796#endif
5797#ifdef DYNAMIC_MZSCHEME
5798 || STRICMP(feature, "mzscheme") == 0
5799#endif
5800#ifdef FEAT_NETBEANS_INTG
5801 || STRICMP(feature, "netbeans_enabled") == 0
5802#endif
5803#ifdef DYNAMIC_PERL
5804 || STRICMP(feature, "perl") == 0
5805#endif
5806#ifdef DYNAMIC_PYTHON
5807 || STRICMP(feature, "python") == 0
5808#endif
5809#ifdef DYNAMIC_PYTHON3
5810 || STRICMP(feature, "python3") == 0
5811#endif
5812#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5813 || STRICMP(feature, "pythonx") == 0
5814#endif
5815#ifdef DYNAMIC_RUBY
5816 || STRICMP(feature, "ruby") == 0
5817#endif
5818#ifdef FEAT_SYN_HL
5819 || STRICMP(feature, "syntax_items") == 0
5820#endif
5821#ifdef DYNAMIC_TCL
5822 || STRICMP(feature, "tcl") == 0
5823#endif
5824 // once "starting" is zero it will stay that way
5825 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5826 || STRICMP(feature, "multi_byte_encoding") == 0
5827#if defined(FEAT_TERMINAL) && defined(MSWIN)
5828 || STRICMP(feature, "conpty") == 0
5829#endif
5830 );
5831}
5832
5833/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005834 * "haslocaldir()" function
5835 */
5836 static void
5837f_haslocaldir(typval_T *argvars, typval_T *rettv)
5838{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005839 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005840 win_T *wp = NULL;
5841
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005842 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5843
5844 // Check for window-local and tab-local directories
5845 if (wp != NULL && wp->w_localdir != NULL)
5846 rettv->vval.v_number = 1;
5847 else if (tp != NULL && tp->tp_localdir != NULL)
5848 rettv->vval.v_number = 2;
5849 else
5850 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005851}
5852
5853/*
5854 * "hasmapto()" function
5855 */
5856 static void
5857f_hasmapto(typval_T *argvars, typval_T *rettv)
5858{
5859 char_u *name;
5860 char_u *mode;
5861 char_u buf[NUMBUFLEN];
5862 int abbr = FALSE;
5863
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005864 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005865 if (argvars[1].v_type == VAR_UNKNOWN)
5866 mode = (char_u *)"nvo";
5867 else
5868 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005869 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005870 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005871 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005872 }
5873
5874 if (map_to_exists(name, mode, abbr))
5875 rettv->vval.v_number = TRUE;
5876 else
5877 rettv->vval.v_number = FALSE;
5878}
5879
5880/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005881 * "highlightID(name)" function
5882 */
5883 static void
5884f_hlID(typval_T *argvars, typval_T *rettv)
5885{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005886 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005887}
5888
5889/*
5890 * "highlight_exists()" function
5891 */
5892 static void
5893f_hlexists(typval_T *argvars, typval_T *rettv)
5894{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005895 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005896}
5897
5898/*
5899 * "hostname()" function
5900 */
5901 static void
5902f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5903{
5904 char_u hostname[256];
5905
5906 mch_get_host_name(hostname, 256);
5907 rettv->v_type = VAR_STRING;
5908 rettv->vval.v_string = vim_strsave(hostname);
5909}
5910
5911/*
5912 * iconv() function
5913 */
5914 static void
5915f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5916{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005917 char_u buf1[NUMBUFLEN];
5918 char_u buf2[NUMBUFLEN];
5919 char_u *from, *to, *str;
5920 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005921
5922 rettv->v_type = VAR_STRING;
5923 rettv->vval.v_string = NULL;
5924
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005925 str = tv_get_string(&argvars[0]);
5926 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5927 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005928 vimconv.vc_type = CONV_NONE;
5929 convert_setup(&vimconv, from, to);
5930
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005931 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005932 if (vimconv.vc_type == CONV_NONE)
5933 rettv->vval.v_string = vim_strsave(str);
5934 else
5935 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5936
5937 convert_setup(&vimconv, NULL, NULL);
5938 vim_free(from);
5939 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005940}
5941
5942/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005943 * "index()" function
5944 */
5945 static void
5946f_index(typval_T *argvars, typval_T *rettv)
5947{
5948 list_T *l;
5949 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005950 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005951 long idx = 0;
5952 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005953 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005954
5955 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005956 if (argvars[0].v_type == VAR_BLOB)
5957 {
5958 typval_T tv;
5959 int start = 0;
5960
5961 if (argvars[2].v_type != VAR_UNKNOWN)
5962 {
5963 start = tv_get_number_chk(&argvars[2], &error);
5964 if (error)
5965 return;
5966 }
5967 b = argvars[0].vval.v_blob;
5968 if (b == NULL)
5969 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005970 if (start < 0)
5971 {
5972 start = blob_len(b) + start;
5973 if (start < 0)
5974 start = 0;
5975 }
5976
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005977 for (idx = start; idx < blob_len(b); ++idx)
5978 {
5979 tv.v_type = VAR_NUMBER;
5980 tv.vval.v_number = blob_get(b, idx);
5981 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5982 {
5983 rettv->vval.v_number = idx;
5984 return;
5985 }
5986 }
5987 return;
5988 }
5989 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005990 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005991 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005992 return;
5993 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005994
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005995 l = argvars[0].vval.v_list;
5996 if (l != NULL)
5997 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005998 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005999 item = l->lv_first;
6000 if (argvars[2].v_type != VAR_UNKNOWN)
6001 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006002 // Start at specified item. Use the cached index that list_find()
6003 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006004 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006005 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006006 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02006007 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006008 if (error)
6009 item = NULL;
6010 }
6011
6012 for ( ; item != NULL; item = item->li_next, ++idx)
6013 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6014 {
6015 rettv->vval.v_number = idx;
6016 break;
6017 }
6018 }
6019}
6020
6021static int inputsecret_flag = 0;
6022
6023/*
6024 * "input()" function
6025 * Also handles inputsecret() when inputsecret is set.
6026 */
6027 static void
6028f_input(typval_T *argvars, typval_T *rettv)
6029{
6030 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6031}
6032
6033/*
6034 * "inputdialog()" function
6035 */
6036 static void
6037f_inputdialog(typval_T *argvars, typval_T *rettv)
6038{
6039#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006040 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006041 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6042 {
6043 char_u *message;
6044 char_u buf[NUMBUFLEN];
6045 char_u *defstr = (char_u *)"";
6046
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006047 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006048 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006049 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006050 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6051 else
6052 IObuff[0] = NUL;
6053 if (message != NULL && defstr != NULL
6054 && do_dialog(VIM_QUESTION, NULL, message,
6055 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6056 rettv->vval.v_string = vim_strsave(IObuff);
6057 else
6058 {
6059 if (message != NULL && defstr != NULL
6060 && argvars[1].v_type != VAR_UNKNOWN
6061 && argvars[2].v_type != VAR_UNKNOWN)
6062 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006063 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006064 else
6065 rettv->vval.v_string = NULL;
6066 }
6067 rettv->v_type = VAR_STRING;
6068 }
6069 else
6070#endif
6071 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6072}
6073
6074/*
6075 * "inputlist()" function
6076 */
6077 static void
6078f_inputlist(typval_T *argvars, typval_T *rettv)
6079{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006080 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006081 listitem_T *li;
6082 int selected;
6083 int mouse_used;
6084
6085#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006086 // While starting up, there is no place to enter text. When running tests
6087 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006088 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006089 return;
6090#endif
6091 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6092 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006093 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006094 return;
6095 }
6096
6097 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006098 msg_row = Rows - 1; // for when 'cmdheight' > 1
6099 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006100 msg_scroll = TRUE;
6101 msg_clr_eos();
6102
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006103 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006104 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006105 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006106 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006107 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006108 msg_putchar('\n');
6109 }
6110
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006111 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006112 selected = prompt_for_number(&mouse_used);
6113 if (mouse_used)
6114 selected -= lines_left;
6115
6116 rettv->vval.v_number = selected;
6117}
6118
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006119static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6120
6121/*
6122 * "inputrestore()" function
6123 */
6124 static void
6125f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6126{
6127 if (ga_userinput.ga_len > 0)
6128 {
6129 --ga_userinput.ga_len;
6130 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6131 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006132 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006133 }
6134 else if (p_verbose > 1)
6135 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006136 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006137 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006138 }
6139}
6140
6141/*
6142 * "inputsave()" function
6143 */
6144 static void
6145f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6146{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006147 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006148 if (ga_grow(&ga_userinput, 1) == OK)
6149 {
6150 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6151 + ga_userinput.ga_len);
6152 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006153 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006154 }
6155 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006156 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006157}
6158
6159/*
6160 * "inputsecret()" function
6161 */
6162 static void
6163f_inputsecret(typval_T *argvars, typval_T *rettv)
6164{
6165 ++cmdline_star;
6166 ++inputsecret_flag;
6167 f_input(argvars, rettv);
6168 --cmdline_star;
6169 --inputsecret_flag;
6170}
6171
6172/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01006173 * "interrupt()" function
6174 */
6175 static void
6176f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6177{
6178 got_int = TRUE;
6179}
6180
6181/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006182 * "invert(expr)" function
6183 */
6184 static void
6185f_invert(typval_T *argvars, typval_T *rettv)
6186{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006187 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006188}
6189
6190/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006191 * "islocked()" function
6192 */
6193 static void
6194f_islocked(typval_T *argvars, typval_T *rettv)
6195{
6196 lval_T lv;
6197 char_u *end;
6198 dictitem_T *di;
6199
6200 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006201 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006202 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006203 if (end != NULL && lv.ll_name != NULL)
6204 {
6205 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02006206 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006207 else
6208 {
6209 if (lv.ll_tv == NULL)
6210 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006211 di = find_var(lv.ll_name, NULL, TRUE);
6212 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006213 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006214 // Consider a variable locked when:
6215 // 1. the variable itself is locked
6216 // 2. the value of the variable is locked.
6217 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01006218 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6219 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006220 }
6221 }
6222 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006223 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006224 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006225 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006226 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006227 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006228 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6229 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006230 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006231 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6232 }
6233 }
6234
6235 clear_lval(&lv);
6236}
6237
6238#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6239/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006240 * "isinf()" function
6241 */
6242 static void
6243f_isinf(typval_T *argvars, typval_T *rettv)
6244{
6245 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6246 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6247}
6248
6249/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006250 * "isnan()" function
6251 */
6252 static void
6253f_isnan(typval_T *argvars, typval_T *rettv)
6254{
6255 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6256 && isnan(argvars[0].vval.v_float);
6257}
6258#endif
6259
6260/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006261 * "last_buffer_nr()" function.
6262 */
6263 static void
6264f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6265{
6266 int n = 0;
6267 buf_T *buf;
6268
Bram Moolenaar29323592016-07-24 22:04:11 +02006269 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006270 if (n < buf->b_fnum)
6271 n = buf->b_fnum;
6272
6273 rettv->vval.v_number = n;
6274}
6275
6276/*
6277 * "len()" function
6278 */
6279 static void
6280f_len(typval_T *argvars, typval_T *rettv)
6281{
6282 switch (argvars[0].v_type)
6283 {
6284 case VAR_STRING:
6285 case VAR_NUMBER:
6286 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006287 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006288 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006289 case VAR_BLOB:
6290 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6291 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006292 case VAR_LIST:
6293 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6294 break;
6295 case VAR_DICT:
6296 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6297 break;
6298 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006299 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006300 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006301 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006302 case VAR_SPECIAL:
6303 case VAR_FLOAT:
6304 case VAR_FUNC:
6305 case VAR_PARTIAL:
6306 case VAR_JOB:
6307 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006308 case VAR_INSTR:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006309 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006310 break;
6311 }
6312}
6313
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006314 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006315libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006316{
6317#ifdef FEAT_LIBCALL
6318 char_u *string_in;
6319 char_u **string_result;
6320 int nr_result;
6321#endif
6322
6323 rettv->v_type = type;
6324 if (type != VAR_NUMBER)
6325 rettv->vval.v_string = NULL;
6326
6327 if (check_restricted() || check_secure())
6328 return;
6329
6330#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006331 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006332 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6333 {
6334 string_in = NULL;
6335 if (argvars[2].v_type == VAR_STRING)
6336 string_in = argvars[2].vval.v_string;
6337 if (type == VAR_NUMBER)
6338 string_result = NULL;
6339 else
6340 string_result = &rettv->vval.v_string;
6341 if (mch_libcall(argvars[0].vval.v_string,
6342 argvars[1].vval.v_string,
6343 string_in,
6344 argvars[2].vval.v_number,
6345 string_result,
6346 &nr_result) == OK
6347 && type == VAR_NUMBER)
6348 rettv->vval.v_number = nr_result;
6349 }
6350#endif
6351}
6352
6353/*
6354 * "libcall()" function
6355 */
6356 static void
6357f_libcall(typval_T *argvars, typval_T *rettv)
6358{
6359 libcall_common(argvars, rettv, VAR_STRING);
6360}
6361
6362/*
6363 * "libcallnr()" function
6364 */
6365 static void
6366f_libcallnr(typval_T *argvars, typval_T *rettv)
6367{
6368 libcall_common(argvars, rettv, VAR_NUMBER);
6369}
6370
6371/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006372 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006373 */
6374 static void
6375f_line(typval_T *argvars, typval_T *rettv)
6376{
6377 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006378 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006379 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006380 int id;
6381 tabpage_T *tp;
6382 win_T *wp;
6383 win_T *save_curwin;
6384 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006385
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006386 if (argvars[1].v_type != VAR_UNKNOWN)
6387 {
6388 // use window specified in the second argument
6389 id = (int)tv_get_number(&argvars[1]);
6390 wp = win_id2wp_tp(id, &tp);
6391 if (wp != NULL && tp != NULL)
6392 {
6393 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6394 == OK)
6395 {
6396 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006397 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006398 }
6399 restore_win_noblock(save_curwin, save_curtab, TRUE);
6400 }
6401 }
6402 else
6403 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006404 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006405
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006406 if (fp != NULL)
6407 lnum = fp->lnum;
6408 rettv->vval.v_number = lnum;
6409}
6410
6411/*
6412 * "line2byte(lnum)" function
6413 */
6414 static void
6415f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6416{
6417#ifndef FEAT_BYTEOFF
6418 rettv->vval.v_number = -1;
6419#else
6420 linenr_T lnum;
6421
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006422 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006423 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6424 rettv->vval.v_number = -1;
6425 else
6426 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6427 if (rettv->vval.v_number >= 0)
6428 ++rettv->vval.v_number;
6429#endif
6430}
6431
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006432#ifdef FEAT_FLOAT
6433/*
6434 * "log()" function
6435 */
6436 static void
6437f_log(typval_T *argvars, typval_T *rettv)
6438{
6439 float_T f = 0.0;
6440
6441 rettv->v_type = VAR_FLOAT;
6442 if (get_float_arg(argvars, &f) == OK)
6443 rettv->vval.v_float = log(f);
6444 else
6445 rettv->vval.v_float = 0.0;
6446}
6447
6448/*
6449 * "log10()" function
6450 */
6451 static void
6452f_log10(typval_T *argvars, typval_T *rettv)
6453{
6454 float_T f = 0.0;
6455
6456 rettv->v_type = VAR_FLOAT;
6457 if (get_float_arg(argvars, &f) == OK)
6458 rettv->vval.v_float = log10(f);
6459 else
6460 rettv->vval.v_float = 0.0;
6461}
6462#endif
6463
6464#ifdef FEAT_LUA
6465/*
6466 * "luaeval()" function
6467 */
6468 static void
6469f_luaeval(typval_T *argvars, typval_T *rettv)
6470{
6471 char_u *str;
6472 char_u buf[NUMBUFLEN];
6473
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006474 if (check_restricted() || check_secure())
6475 return;
6476
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006477 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006478 do_luaeval(str, argvars + 1, rettv);
6479}
6480#endif
6481
6482/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006483 * "maparg()" function
6484 */
6485 static void
6486f_maparg(typval_T *argvars, typval_T *rettv)
6487{
6488 get_maparg(argvars, rettv, TRUE);
6489}
6490
6491/*
6492 * "mapcheck()" function
6493 */
6494 static void
6495f_mapcheck(typval_T *argvars, typval_T *rettv)
6496{
6497 get_maparg(argvars, rettv, FALSE);
6498}
6499
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006500typedef enum
6501{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006502 MATCH_END, // matchend()
6503 MATCH_MATCH, // match()
6504 MATCH_STR, // matchstr()
6505 MATCH_LIST, // matchlist()
6506 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006507} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006508
6509 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006510find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006511{
6512 char_u *str = NULL;
6513 long len = 0;
6514 char_u *expr = NULL;
6515 char_u *pat;
6516 regmatch_T regmatch;
6517 char_u patbuf[NUMBUFLEN];
6518 char_u strbuf[NUMBUFLEN];
6519 char_u *save_cpo;
6520 long start = 0;
6521 long nth = 1;
6522 colnr_T startcol = 0;
6523 int match = 0;
6524 list_T *l = NULL;
6525 listitem_T *li = NULL;
6526 long idx = 0;
6527 char_u *tofree = NULL;
6528
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006529 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006530 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006531 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006532
6533 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006534 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006535 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006536 // type MATCH_LIST: return empty list when there are no matches.
6537 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006538 if (rettv_list_alloc(rettv) == FAIL)
6539 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006540 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006541 && (list_append_string(rettv->vval.v_list,
6542 (char_u *)"", 0) == FAIL
6543 || list_append_number(rettv->vval.v_list,
6544 (varnumber_T)-1) == FAIL
6545 || list_append_number(rettv->vval.v_list,
6546 (varnumber_T)-1) == FAIL
6547 || list_append_number(rettv->vval.v_list,
6548 (varnumber_T)-1) == FAIL))
6549 {
6550 list_free(rettv->vval.v_list);
6551 rettv->vval.v_list = NULL;
6552 goto theend;
6553 }
6554 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006555 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006556 {
6557 rettv->v_type = VAR_STRING;
6558 rettv->vval.v_string = NULL;
6559 }
6560
6561 if (argvars[0].v_type == VAR_LIST)
6562 {
6563 if ((l = argvars[0].vval.v_list) == NULL)
6564 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006565 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006566 li = l->lv_first;
6567 }
6568 else
6569 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006570 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571 len = (long)STRLEN(str);
6572 }
6573
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006574 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006575 if (pat == NULL)
6576 goto theend;
6577
6578 if (argvars[2].v_type != VAR_UNKNOWN)
6579 {
6580 int error = FALSE;
6581
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006582 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006583 if (error)
6584 goto theend;
6585 if (l != NULL)
6586 {
6587 li = list_find(l, start);
6588 if (li == NULL)
6589 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006590 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006591 }
6592 else
6593 {
6594 if (start < 0)
6595 start = 0;
6596 if (start > len)
6597 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006598 // When "count" argument is there ignore matches before "start",
6599 // otherwise skip part of the string. Differs when pattern is "^"
6600 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006601 if (argvars[3].v_type != VAR_UNKNOWN)
6602 startcol = start;
6603 else
6604 {
6605 str += start;
6606 len -= start;
6607 }
6608 }
6609
6610 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006611 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006612 if (error)
6613 goto theend;
6614 }
6615
6616 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6617 if (regmatch.regprog != NULL)
6618 {
6619 regmatch.rm_ic = p_ic;
6620
6621 for (;;)
6622 {
6623 if (l != NULL)
6624 {
6625 if (li == NULL)
6626 {
6627 match = FALSE;
6628 break;
6629 }
6630 vim_free(tofree);
6631 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6632 if (str == NULL)
6633 break;
6634 }
6635
6636 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6637
6638 if (match && --nth <= 0)
6639 break;
6640 if (l == NULL && !match)
6641 break;
6642
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006643 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006644 if (l != NULL)
6645 {
6646 li = li->li_next;
6647 ++idx;
6648 }
6649 else
6650 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006651 startcol = (colnr_T)(regmatch.startp[0]
6652 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006653 if (startcol > (colnr_T)len
6654 || str + startcol <= regmatch.startp[0])
6655 {
6656 match = FALSE;
6657 break;
6658 }
6659 }
6660 }
6661
6662 if (match)
6663 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006664 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006665 {
6666 listitem_T *li1 = rettv->vval.v_list->lv_first;
6667 listitem_T *li2 = li1->li_next;
6668 listitem_T *li3 = li2->li_next;
6669 listitem_T *li4 = li3->li_next;
6670
6671 vim_free(li1->li_tv.vval.v_string);
6672 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006673 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006674 li3->li_tv.vval.v_number =
6675 (varnumber_T)(regmatch.startp[0] - expr);
6676 li4->li_tv.vval.v_number =
6677 (varnumber_T)(regmatch.endp[0] - expr);
6678 if (l != NULL)
6679 li2->li_tv.vval.v_number = (varnumber_T)idx;
6680 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006681 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006682 {
6683 int i;
6684
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006685 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006686 for (i = 0; i < NSUBEXP; ++i)
6687 {
6688 if (regmatch.endp[i] == NULL)
6689 {
6690 if (list_append_string(rettv->vval.v_list,
6691 (char_u *)"", 0) == FAIL)
6692 break;
6693 }
6694 else if (list_append_string(rettv->vval.v_list,
6695 regmatch.startp[i],
6696 (int)(regmatch.endp[i] - regmatch.startp[i]))
6697 == FAIL)
6698 break;
6699 }
6700 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006701 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006702 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006703 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006704 if (l != NULL)
6705 copy_tv(&li->li_tv, rettv);
6706 else
6707 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006708 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006709 }
6710 else if (l != NULL)
6711 rettv->vval.v_number = idx;
6712 else
6713 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006714 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006715 rettv->vval.v_number =
6716 (varnumber_T)(regmatch.startp[0] - str);
6717 else
6718 rettv->vval.v_number =
6719 (varnumber_T)(regmatch.endp[0] - str);
6720 rettv->vval.v_number += (varnumber_T)(str - expr);
6721 }
6722 }
6723 vim_regfree(regmatch.regprog);
6724 }
6725
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006726theend:
6727 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006728 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006729 listitem_remove(rettv->vval.v_list,
6730 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006731 vim_free(tofree);
6732 p_cpo = save_cpo;
6733}
6734
6735/*
6736 * "match()" function
6737 */
6738 static void
6739f_match(typval_T *argvars, typval_T *rettv)
6740{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006741 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006742}
6743
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006744/*
6745 * "matchend()" function
6746 */
6747 static void
6748f_matchend(typval_T *argvars, typval_T *rettv)
6749{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006750 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006751}
6752
6753/*
6754 * "matchlist()" function
6755 */
6756 static void
6757f_matchlist(typval_T *argvars, typval_T *rettv)
6758{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006759 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006760}
6761
6762/*
6763 * "matchstr()" function
6764 */
6765 static void
6766f_matchstr(typval_T *argvars, typval_T *rettv)
6767{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006768 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006769}
6770
6771/*
6772 * "matchstrpos()" function
6773 */
6774 static void
6775f_matchstrpos(typval_T *argvars, typval_T *rettv)
6776{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006777 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006778}
6779
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006780 static void
6781max_min(typval_T *argvars, typval_T *rettv, int domax)
6782{
6783 varnumber_T n = 0;
6784 varnumber_T i;
6785 int error = FALSE;
6786
6787 if (argvars[0].v_type == VAR_LIST)
6788 {
6789 list_T *l;
6790 listitem_T *li;
6791
6792 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006793 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006794 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006795 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006796 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006797 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6798 n = l->lv_u.nonmat.lv_start;
6799 else
6800 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6801 * l->lv_u.nonmat.lv_stride;
6802 }
6803 else
6804 {
6805 li = l->lv_first;
6806 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006807 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006808 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006809 if (error)
6810 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006811 for (;;)
6812 {
6813 li = li->li_next;
6814 if (li == NULL)
6815 break;
6816 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006817 if (error)
6818 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006819 if (domax ? i > n : i < n)
6820 n = i;
6821 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006822 }
6823 }
6824 }
6825 }
6826 else if (argvars[0].v_type == VAR_DICT)
6827 {
6828 dict_T *d;
6829 int first = TRUE;
6830 hashitem_T *hi;
6831 int todo;
6832
6833 d = argvars[0].vval.v_dict;
6834 if (d != NULL)
6835 {
6836 todo = (int)d->dv_hashtab.ht_used;
6837 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6838 {
6839 if (!HASHITEM_EMPTY(hi))
6840 {
6841 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006842 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006843 if (error)
6844 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006845 if (first)
6846 {
6847 n = i;
6848 first = FALSE;
6849 }
6850 else if (domax ? i > n : i < n)
6851 n = i;
6852 }
6853 }
6854 }
6855 }
6856 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006857 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006858
6859 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006860}
6861
6862/*
6863 * "max()" function
6864 */
6865 static void
6866f_max(typval_T *argvars, typval_T *rettv)
6867{
6868 max_min(argvars, rettv, TRUE);
6869}
6870
6871/*
6872 * "min()" function
6873 */
6874 static void
6875f_min(typval_T *argvars, typval_T *rettv)
6876{
6877 max_min(argvars, rettv, FALSE);
6878}
6879
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006880#if defined(FEAT_MZSCHEME) || defined(PROTO)
6881/*
6882 * "mzeval()" function
6883 */
6884 static void
6885f_mzeval(typval_T *argvars, typval_T *rettv)
6886{
6887 char_u *str;
6888 char_u buf[NUMBUFLEN];
6889
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006890 if (check_restricted() || check_secure())
6891 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006892 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006893 do_mzeval(str, rettv);
6894}
6895
6896 void
6897mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6898{
6899 typval_T argvars[3];
6900
6901 argvars[0].v_type = VAR_STRING;
6902 argvars[0].vval.v_string = name;
6903 copy_tv(args, &argvars[1]);
6904 argvars[2].v_type = VAR_UNKNOWN;
6905 f_call(argvars, rettv);
6906 clear_tv(&argvars[1]);
6907}
6908#endif
6909
6910/*
6911 * "nextnonblank()" function
6912 */
6913 static void
6914f_nextnonblank(typval_T *argvars, typval_T *rettv)
6915{
6916 linenr_T lnum;
6917
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006918 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006919 {
6920 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6921 {
6922 lnum = 0;
6923 break;
6924 }
6925 if (*skipwhite(ml_get(lnum)) != NUL)
6926 break;
6927 }
6928 rettv->vval.v_number = lnum;
6929}
6930
6931/*
6932 * "nr2char()" function
6933 */
6934 static void
6935f_nr2char(typval_T *argvars, typval_T *rettv)
6936{
6937 char_u buf[NUMBUFLEN];
6938
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006939 if (has_mbyte)
6940 {
6941 int utf8 = 0;
6942
6943 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006944 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006945 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006946 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006947 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006948 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006949 }
6950 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006951 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006952 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006953 buf[1] = NUL;
6954 }
6955 rettv->v_type = VAR_STRING;
6956 rettv->vval.v_string = vim_strsave(buf);
6957}
6958
6959/*
6960 * "or(expr, expr)" function
6961 */
6962 static void
6963f_or(typval_T *argvars, typval_T *rettv)
6964{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006965 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6966 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006967}
6968
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006969#ifdef FEAT_PERL
6970/*
6971 * "perleval()" function
6972 */
6973 static void
6974f_perleval(typval_T *argvars, typval_T *rettv)
6975{
6976 char_u *str;
6977 char_u buf[NUMBUFLEN];
6978
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006979 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006980 do_perleval(str, rettv);
6981}
6982#endif
6983
6984#ifdef FEAT_FLOAT
6985/*
6986 * "pow()" function
6987 */
6988 static void
6989f_pow(typval_T *argvars, typval_T *rettv)
6990{
6991 float_T fx = 0.0, fy = 0.0;
6992
6993 rettv->v_type = VAR_FLOAT;
6994 if (get_float_arg(argvars, &fx) == OK
6995 && get_float_arg(&argvars[1], &fy) == OK)
6996 rettv->vval.v_float = pow(fx, fy);
6997 else
6998 rettv->vval.v_float = 0.0;
6999}
7000#endif
7001
7002/*
7003 * "prevnonblank()" function
7004 */
7005 static void
7006f_prevnonblank(typval_T *argvars, typval_T *rettv)
7007{
7008 linenr_T lnum;
7009
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007010 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007011 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7012 lnum = 0;
7013 else
7014 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
7015 --lnum;
7016 rettv->vval.v_number = lnum;
7017}
7018
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007019// This dummy va_list is here because:
7020// - passing a NULL pointer doesn't work when va_list isn't a pointer
7021// - locally in the function results in a "used before set" warning
7022// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007023static va_list ap;
7024
7025/*
7026 * "printf()" function
7027 */
7028 static void
7029f_printf(typval_T *argvars, typval_T *rettv)
7030{
7031 char_u buf[NUMBUFLEN];
7032 int len;
7033 char_u *s;
7034 int saved_did_emsg = did_emsg;
7035 char *fmt;
7036
7037 rettv->v_type = VAR_STRING;
7038 rettv->vval.v_string = NULL;
7039
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007040 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007041 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007042 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007043 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007044 if (!did_emsg)
7045 {
7046 s = alloc(len + 1);
7047 if (s != NULL)
7048 {
7049 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007050 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
7051 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007052 }
7053 }
7054 did_emsg |= saved_did_emsg;
7055}
7056
7057/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007058 * "pum_getpos()" function
7059 */
7060 static void
7061f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7062{
7063 if (rettv_dict_alloc(rettv) != OK)
7064 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007065 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007066}
7067
7068/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007069 * "pumvisible()" function
7070 */
7071 static void
7072f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7073{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007074 if (pum_visible())
7075 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007076}
7077
7078#ifdef FEAT_PYTHON3
7079/*
7080 * "py3eval()" function
7081 */
7082 static void
7083f_py3eval(typval_T *argvars, typval_T *rettv)
7084{
7085 char_u *str;
7086 char_u buf[NUMBUFLEN];
7087
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007088 if (check_restricted() || check_secure())
7089 return;
7090
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007091 if (p_pyx == 0)
7092 p_pyx = 3;
7093
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007094 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007095 do_py3eval(str, rettv);
7096}
7097#endif
7098
7099#ifdef FEAT_PYTHON
7100/*
7101 * "pyeval()" function
7102 */
7103 static void
7104f_pyeval(typval_T *argvars, typval_T *rettv)
7105{
7106 char_u *str;
7107 char_u buf[NUMBUFLEN];
7108
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007109 if (check_restricted() || check_secure())
7110 return;
7111
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007112 if (p_pyx == 0)
7113 p_pyx = 2;
7114
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007115 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007116 do_pyeval(str, rettv);
7117}
7118#endif
7119
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007120#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
7121/*
7122 * "pyxeval()" function
7123 */
7124 static void
7125f_pyxeval(typval_T *argvars, typval_T *rettv)
7126{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007127 if (check_restricted() || check_secure())
7128 return;
7129
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007130# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
7131 init_pyxversion();
7132 if (p_pyx == 2)
7133 f_pyeval(argvars, rettv);
7134 else
7135 f_py3eval(argvars, rettv);
7136# elif defined(FEAT_PYTHON)
7137 f_pyeval(argvars, rettv);
7138# elif defined(FEAT_PYTHON3)
7139 f_py3eval(argvars, rettv);
7140# endif
7141}
7142#endif
7143
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007144static UINT32_T srand_seed_for_testing = 0;
7145static int srand_seed_for_testing_is_used = FALSE;
7146
7147 static void
7148f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
7149{
7150 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007151 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007152 else
7153 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007154 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
7155 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007156 }
7157}
7158
7159 static void
7160init_srand(UINT32_T *x)
7161{
7162#ifndef MSWIN
7163 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
7164#endif
7165
7166 if (srand_seed_for_testing_is_used)
7167 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007168 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007169 return;
7170 }
7171#ifndef MSWIN
7172 if (dev_urandom_state != FAIL)
7173 {
7174 int fd = open("/dev/urandom", O_RDONLY);
7175 struct {
7176 union {
7177 UINT32_T number;
7178 char bytes[sizeof(UINT32_T)];
7179 } contents;
7180 } buf;
7181
7182 // Attempt reading /dev/urandom.
7183 if (fd == -1)
7184 dev_urandom_state = FAIL;
7185 else
7186 {
7187 buf.contents.number = 0;
7188 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
7189 != sizeof(UINT32_T))
7190 dev_urandom_state = FAIL;
7191 else
7192 {
7193 dev_urandom_state = OK;
7194 *x = buf.contents.number;
7195 }
7196 close(fd);
7197 }
7198 }
7199 if (dev_urandom_state != OK)
7200 // Reading /dev/urandom doesn't work, fall back to time().
7201#endif
7202 *x = vim_time();
7203}
7204
7205#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
7206#define SPLITMIX32(x, z) ( \
7207 z = (x += 0x9e3779b9), \
7208 z = (z ^ (z >> 16)) * 0x85ebca6b, \
7209 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
7210 z ^ (z >> 16) \
7211 )
7212#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
7213 result = ROTL(y * 5, 7) * 9; \
7214 t = y << 9; \
7215 z ^= x; \
7216 w ^= y; \
7217 y ^= z, x ^= w; \
7218 z ^= t; \
7219 w = ROTL(w, 11);
7220
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007221/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007222 * "rand()" function
7223 */
7224 static void
7225f_rand(typval_T *argvars, typval_T *rettv)
7226{
7227 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007228 static UINT32_T gx, gy, gz, gw;
7229 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007230 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01007231 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007232
7233 if (argvars[0].v_type == VAR_UNKNOWN)
7234 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007235 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007236 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007237 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007238 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007239 init_srand(&x);
7240
7241 gx = SPLITMIX32(x, z);
7242 gy = SPLITMIX32(x, z);
7243 gz = SPLITMIX32(x, z);
7244 gw = SPLITMIX32(x, z);
7245 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007246 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007247
7248 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007249 }
7250 else if (argvars[0].v_type == VAR_LIST)
7251 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007252 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007253 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007254 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007255
7256 lx = list_find(l, 0L);
7257 ly = list_find(l, 1L);
7258 lz = list_find(l, 2L);
7259 lw = list_find(l, 3L);
7260 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
7261 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
7262 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
7263 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
7264 x = (UINT32_T)lx->li_tv.vval.v_number;
7265 y = (UINT32_T)ly->li_tv.vval.v_number;
7266 z = (UINT32_T)lz->li_tv.vval.v_number;
7267 w = (UINT32_T)lw->li_tv.vval.v_number;
7268
7269 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
7270
7271 lx->li_tv.vval.v_number = (varnumber_T)x;
7272 ly->li_tv.vval.v_number = (varnumber_T)y;
7273 lz->li_tv.vval.v_number = (varnumber_T)z;
7274 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007275 }
7276 else
7277 goto theend;
7278
7279 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007280 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007281 return;
7282
7283theend:
7284 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007285 rettv->v_type = VAR_NUMBER;
7286 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007287}
7288
7289/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007290 * "srand()" function
7291 */
7292 static void
7293f_srand(typval_T *argvars, typval_T *rettv)
7294{
7295 UINT32_T x = 0, z;
7296
7297 if (rettv_list_alloc(rettv) == FAIL)
7298 return;
7299 if (argvars[0].v_type == VAR_UNKNOWN)
7300 {
7301 init_srand(&x);
7302 }
7303 else
7304 {
7305 int error = FALSE;
7306
7307 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7308 if (error)
7309 return;
7310 }
7311
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 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7316}
7317
7318#undef ROTL
7319#undef SPLITMIX32
7320#undef SHUFFLE_XOSHIRO128STARSTAR
7321
7322/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007323 * "range()" function
7324 */
7325 static void
7326f_range(typval_T *argvars, typval_T *rettv)
7327{
7328 varnumber_T start;
7329 varnumber_T end;
7330 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007331 int error = FALSE;
7332
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007333 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007334 if (argvars[1].v_type == VAR_UNKNOWN)
7335 {
7336 end = start - 1;
7337 start = 0;
7338 }
7339 else
7340 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007341 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007342 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007343 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007344 }
7345
7346 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007347 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007348 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007349 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007350 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007351 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007352 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007353 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007354 list_T *list = rettv->vval.v_list;
7355
7356 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007357 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007358 // be called.
7359 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007360 list->lv_u.nonmat.lv_start = start;
7361 list->lv_u.nonmat.lv_end = end;
7362 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007363 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007364 }
7365}
7366
7367/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007368 * Materialize "list".
7369 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007370 */
7371 void
7372range_list_materialize(list_T *list)
7373{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007374 varnumber_T start = list->lv_u.nonmat.lv_start;
7375 varnumber_T end = list->lv_u.nonmat.lv_end;
7376 int stride = list->lv_u.nonmat.lv_stride;
7377 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007379 list->lv_first = NULL;
7380 list->lv_u.mat.lv_last = NULL;
7381 list->lv_len = 0;
7382 list->lv_u.mat.lv_idx_item = NULL;
7383 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7384 if (list_append_number(list, (varnumber_T)i) == FAIL)
7385 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007386}
7387
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007388/*
7389 * "getreginfo()" function
7390 */
7391 static void
7392f_getreginfo(typval_T *argvars, typval_T *rettv)
7393{
7394 char_u *strregname;
7395 int regname;
7396 char_u buf[NUMBUFLEN + 2];
7397 long reglen = 0;
7398 dict_T *dict;
7399 list_T *list;
7400
7401 if (argvars[0].v_type != VAR_UNKNOWN)
7402 {
7403 strregname = tv_get_string_chk(&argvars[0]);
7404 if (strregname == NULL)
7405 return;
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007406 if (in_vim9script() && STRLEN(strregname) > 1)
7407 {
7408 semsg(_(e_register_name_must_be_one_char_str), strregname);
7409 return;
7410 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007411 }
7412 else
7413 strregname = get_vim_var_str(VV_REG);
7414
7415 regname = (strregname == NULL ? '"' : *strregname);
7416 if (regname == 0 || regname == '@')
7417 regname = '"';
7418
7419 if (rettv_dict_alloc(rettv) == FAIL)
7420 return;
7421 dict = rettv->vval.v_dict;
7422
7423 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7424 if (list == NULL)
7425 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007426 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007427
7428 buf[0] = NUL;
7429 buf[1] = NUL;
7430 switch (get_reg_type(regname, &reglen))
7431 {
7432 case MLINE: buf[0] = 'V'; break;
7433 case MCHAR: buf[0] = 'v'; break;
7434 case MBLOCK:
7435 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7436 reglen + 1);
7437 break;
7438 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007439 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007440
7441 buf[0] = get_register_name(get_unname_register());
7442 buf[1] = NUL;
7443 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007444 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007445 else
7446 {
7447 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7448
7449 if (item != NULL)
7450 {
7451 item->di_tv.v_type = VAR_SPECIAL;
7452 item->di_tv.vval.v_number = regname == buf[0]
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007453 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007454 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007455 }
7456 }
7457}
7458
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007459 static void
7460return_register(int regname, typval_T *rettv)
7461{
7462 char_u buf[2] = {0, 0};
7463
7464 buf[0] = (char_u)regname;
7465 rettv->v_type = VAR_STRING;
7466 rettv->vval.v_string = vim_strsave(buf);
7467}
7468
7469/*
7470 * "reg_executing()" function
7471 */
7472 static void
7473f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7474{
7475 return_register(reg_executing, rettv);
7476}
7477
7478/*
7479 * "reg_recording()" function
7480 */
7481 static void
7482f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7483{
7484 return_register(reg_recording, rettv);
7485}
7486
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007487/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007488 * "rename({from}, {to})" function
7489 */
7490 static void
7491f_rename(typval_T *argvars, typval_T *rettv)
7492{
7493 char_u buf[NUMBUFLEN];
7494
7495 if (check_restricted() || check_secure())
7496 rettv->vval.v_number = -1;
7497 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007498 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7499 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007500}
7501
7502/*
7503 * "repeat()" function
7504 */
7505 static void
7506f_repeat(typval_T *argvars, typval_T *rettv)
7507{
7508 char_u *p;
7509 int n;
7510 int slen;
7511 int len;
7512 char_u *r;
7513 int i;
7514
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007515 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007516 if (argvars[0].v_type == VAR_LIST)
7517 {
7518 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7519 while (n-- > 0)
7520 if (list_extend(rettv->vval.v_list,
7521 argvars[0].vval.v_list, NULL) == FAIL)
7522 break;
7523 }
7524 else
7525 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007526 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007527 rettv->v_type = VAR_STRING;
7528 rettv->vval.v_string = NULL;
7529
7530 slen = (int)STRLEN(p);
7531 len = slen * n;
7532 if (len <= 0)
7533 return;
7534
7535 r = alloc(len + 1);
7536 if (r != NULL)
7537 {
7538 for (i = 0; i < n; i++)
7539 mch_memmove(r + i * slen, p, (size_t)slen);
7540 r[len] = NUL;
7541 }
7542
7543 rettv->vval.v_string = r;
7544 }
7545}
7546
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007547#define SP_NOMOVE 0x01 // don't move cursor
7548#define SP_REPEAT 0x02 // repeat to find outer pair
7549#define SP_RETCOUNT 0x04 // return matchcount
7550#define SP_SETPCMARK 0x08 // set previous context mark
7551#define SP_START 0x10 // accept match at start position
7552#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7553#define SP_END 0x40 // leave cursor at end of match
7554#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007555
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007556/*
7557 * Get flags for a search function.
7558 * Possibly sets "p_ws".
7559 * Returns BACKWARD, FORWARD or zero (for an error).
7560 */
7561 static int
7562get_search_arg(typval_T *varp, int *flagsp)
7563{
7564 int dir = FORWARD;
7565 char_u *flags;
7566 char_u nbuf[NUMBUFLEN];
7567 int mask;
7568
7569 if (varp->v_type != VAR_UNKNOWN)
7570 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007571 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007572 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007573 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007574 while (*flags != NUL)
7575 {
7576 switch (*flags)
7577 {
7578 case 'b': dir = BACKWARD; break;
7579 case 'w': p_ws = TRUE; break;
7580 case 'W': p_ws = FALSE; break;
7581 default: mask = 0;
7582 if (flagsp != NULL)
7583 switch (*flags)
7584 {
7585 case 'c': mask = SP_START; break;
7586 case 'e': mask = SP_END; break;
7587 case 'm': mask = SP_RETCOUNT; break;
7588 case 'n': mask = SP_NOMOVE; break;
7589 case 'p': mask = SP_SUBPAT; break;
7590 case 'r': mask = SP_REPEAT; break;
7591 case 's': mask = SP_SETPCMARK; break;
7592 case 'z': mask = SP_COLUMN; break;
7593 }
7594 if (mask == 0)
7595 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007596 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007597 dir = 0;
7598 }
7599 else
7600 *flagsp |= mask;
7601 }
7602 if (dir == 0)
7603 break;
7604 ++flags;
7605 }
7606 }
7607 return dir;
7608}
7609
7610/*
7611 * Shared by search() and searchpos() functions.
7612 */
7613 static int
7614search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7615{
7616 int flags;
7617 char_u *pat;
7618 pos_T pos;
7619 pos_T save_cursor;
7620 int save_p_ws = p_ws;
7621 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007622 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007623 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007624#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007625 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007626 long time_limit = 0;
7627#endif
7628 int options = SEARCH_KEEP;
7629 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007630 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007631 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007632 pos_T firstpos;
7633
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007634 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007635 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007636 if (dir == 0)
7637 goto theend;
7638 flags = *flagsp;
7639 if (flags & SP_START)
7640 options |= SEARCH_START;
7641 if (flags & SP_END)
7642 options |= SEARCH_END;
7643 if (flags & SP_COLUMN)
7644 options |= SEARCH_COL;
7645
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007646 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007647 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7648 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007649 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007650 if (lnum_stop < 0)
7651 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007652 if (argvars[3].v_type != VAR_UNKNOWN)
7653 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007654#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007655 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007656 if (time_limit < 0)
7657 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007658#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007659 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007660 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007661 }
7662
7663#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007664 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007665 profile_setlimit(time_limit, &tm);
7666#endif
7667
7668 /*
7669 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7670 * Check to make sure only those flags are set.
7671 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7672 * flags cannot be set. Check for that condition also.
7673 */
7674 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7675 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7676 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007677 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007678 goto theend;
7679 }
7680
7681 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007682 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007683 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007684 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7685#ifdef FEAT_RELTIME
7686 sia.sa_tm = &tm;
7687#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007688
7689 // Repeat until {skip} returns FALSE.
7690 for (;;)
7691 {
7692 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007693 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007694 // finding the first match again means there is no match where {skip}
7695 // evaluates to zero.
7696 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7697 subpatnum = FAIL;
7698
Bram Moolenaara9c01042020-06-07 14:50:50 +02007699 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007700 // didn't find it or no skip argument
7701 break;
7702 firstpos = pos;
7703
Bram Moolenaara9c01042020-06-07 14:50:50 +02007704 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007705 {
7706 int do_skip;
7707 int err;
7708 pos_T save_pos = curwin->w_cursor;
7709
7710 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007711 err = FALSE;
7712 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007713 curwin->w_cursor = save_pos;
7714 if (err)
7715 {
7716 // Evaluating {skip} caused an error, break here.
7717 subpatnum = FAIL;
7718 break;
7719 }
7720 if (!do_skip)
7721 break;
7722 }
7723 }
7724
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007725 if (subpatnum != FAIL)
7726 {
7727 if (flags & SP_SUBPAT)
7728 retval = subpatnum;
7729 else
7730 retval = pos.lnum;
7731 if (flags & SP_SETPCMARK)
7732 setpcmark();
7733 curwin->w_cursor = pos;
7734 if (match_pos != NULL)
7735 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007736 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007737 match_pos->lnum = pos.lnum;
7738 match_pos->col = pos.col + 1;
7739 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007740 // "/$" will put the cursor after the end of the line, may need to
7741 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007742 check_cursor();
7743 }
7744
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007745 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007746 if (flags & SP_NOMOVE)
7747 curwin->w_cursor = save_cursor;
7748 else
7749 curwin->w_set_curswant = TRUE;
7750theend:
7751 p_ws = save_p_ws;
7752
7753 return retval;
7754}
7755
7756#ifdef FEAT_FLOAT
7757
7758/*
7759 * round() is not in C90, use ceil() or floor() instead.
7760 */
7761 float_T
7762vim_round(float_T f)
7763{
7764 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
7765}
7766
7767/*
7768 * "round({float})" function
7769 */
7770 static void
7771f_round(typval_T *argvars, typval_T *rettv)
7772{
7773 float_T f = 0.0;
7774
7775 rettv->v_type = VAR_FLOAT;
7776 if (get_float_arg(argvars, &f) == OK)
7777 rettv->vval.v_float = vim_round(f);
7778 else
7779 rettv->vval.v_float = 0.0;
7780}
7781#endif
7782
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007783#ifdef FEAT_RUBY
7784/*
7785 * "rubyeval()" function
7786 */
7787 static void
7788f_rubyeval(typval_T *argvars, typval_T *rettv)
7789{
7790 char_u *str;
7791 char_u buf[NUMBUFLEN];
7792
7793 str = tv_get_string_buf(&argvars[0], buf);
7794 do_rubyeval(str, rettv);
7795}
7796#endif
7797
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007798/*
7799 * "screenattr()" function
7800 */
7801 static void
7802f_screenattr(typval_T *argvars, typval_T *rettv)
7803{
7804 int row;
7805 int col;
7806 int c;
7807
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007808 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7809 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007810 if (row < 0 || row >= screen_Rows
7811 || col < 0 || col >= screen_Columns)
7812 c = -1;
7813 else
7814 c = ScreenAttrs[LineOffset[row] + col];
7815 rettv->vval.v_number = c;
7816}
7817
7818/*
7819 * "screenchar()" function
7820 */
7821 static void
7822f_screenchar(typval_T *argvars, typval_T *rettv)
7823{
7824 int row;
7825 int col;
7826 int off;
7827 int c;
7828
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007829 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7830 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007831 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007832 c = -1;
7833 else
7834 {
7835 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007836 if (enc_utf8 && ScreenLinesUC[off] != 0)
7837 c = ScreenLinesUC[off];
7838 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007839 c = ScreenLines[off];
7840 }
7841 rettv->vval.v_number = c;
7842}
7843
7844/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007845 * "screenchars()" function
7846 */
7847 static void
7848f_screenchars(typval_T *argvars, typval_T *rettv)
7849{
7850 int row;
7851 int col;
7852 int off;
7853 int c;
7854 int i;
7855
7856 if (rettv_list_alloc(rettv) == FAIL)
7857 return;
7858 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7859 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7860 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7861 return;
7862
7863 off = LineOffset[row] + col;
7864 if (enc_utf8 && ScreenLinesUC[off] != 0)
7865 c = ScreenLinesUC[off];
7866 else
7867 c = ScreenLines[off];
7868 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7869
7870 if (enc_utf8)
7871
7872 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7873 list_append_number(rettv->vval.v_list,
7874 (varnumber_T)ScreenLinesC[i][off]);
7875}
7876
7877/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007878 * "screencol()" function
7879 *
7880 * First column is 1 to be consistent with virtcol().
7881 */
7882 static void
7883f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7884{
7885 rettv->vval.v_number = screen_screencol() + 1;
7886}
7887
7888/*
7889 * "screenrow()" function
7890 */
7891 static void
7892f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7893{
7894 rettv->vval.v_number = screen_screenrow() + 1;
7895}
7896
7897/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007898 * "screenstring()" function
7899 */
7900 static void
7901f_screenstring(typval_T *argvars, typval_T *rettv)
7902{
7903 int row;
7904 int col;
7905 int off;
7906 int c;
7907 int i;
7908 char_u buf[MB_MAXBYTES + 1];
7909 int buflen = 0;
7910
7911 rettv->vval.v_string = NULL;
7912 rettv->v_type = VAR_STRING;
7913
7914 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7915 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7916 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7917 return;
7918
7919 off = LineOffset[row] + col;
7920 if (enc_utf8 && ScreenLinesUC[off] != 0)
7921 c = ScreenLinesUC[off];
7922 else
7923 c = ScreenLines[off];
7924 buflen += mb_char2bytes(c, buf);
7925
Bram Moolenaarf1387282021-03-22 17:11:15 +01007926 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007927 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7928 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7929
7930 buf[buflen] = NUL;
7931 rettv->vval.v_string = vim_strsave(buf);
7932}
7933
7934/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007935 * "search()" function
7936 */
7937 static void
7938f_search(typval_T *argvars, typval_T *rettv)
7939{
7940 int flags = 0;
7941
7942 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7943}
7944
7945/*
7946 * "searchdecl()" function
7947 */
7948 static void
7949f_searchdecl(typval_T *argvars, typval_T *rettv)
7950{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007951 int locally = TRUE;
7952 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007953 int error = FALSE;
7954 char_u *name;
7955
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007956 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007957
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007958 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007959 if (argvars[1].v_type != VAR_UNKNOWN)
7960 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007961 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007962 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007963 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007964 }
7965 if (!error && name != NULL)
7966 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7967 locally, thisblock, SEARCH_KEEP) == FAIL;
7968}
7969
7970/*
7971 * Used by searchpair() and searchpairpos()
7972 */
7973 static int
7974searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7975{
7976 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007977 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007978 int save_p_ws = p_ws;
7979 int dir;
7980 int flags = 0;
7981 char_u nbuf1[NUMBUFLEN];
7982 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007983 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007984 long lnum_stop = 0;
7985 long time_limit = 0;
7986
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007987 // Get the three pattern arguments: start, middle, end. Will result in an
7988 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007989 spat = tv_get_string_chk(&argvars[0]);
7990 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7991 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007992 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007993 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007994
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007995 // Handle the optional fourth argument: flags
7996 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007997 if (dir == 0)
7998 goto theend;
7999
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008000 // Don't accept SP_END or SP_SUBPAT.
8001 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008002 if ((flags & (SP_END | SP_SUBPAT)) != 0
8003 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
8004 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008005 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008006 goto theend;
8007 }
8008
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008009 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008010 if (flags & SP_REPEAT)
8011 p_ws = FALSE;
8012
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008013 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008014 if (argvars[3].v_type == VAR_UNKNOWN
8015 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01008016 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008017 else
8018 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02008019 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01008020 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02008021
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008022 if (argvars[5].v_type != VAR_UNKNOWN)
8023 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008024 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008025 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008026 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008027 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008028 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008029 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008030#ifdef FEAT_RELTIME
8031 if (argvars[6].v_type != VAR_UNKNOWN)
8032 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008033 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008034 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008035 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008036 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008037 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008038 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008039 }
8040#endif
8041 }
8042 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008043
8044 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
8045 match_pos, lnum_stop, time_limit);
8046
8047theend:
8048 p_ws = save_p_ws;
8049
8050 return retval;
8051}
8052
8053/*
8054 * "searchpair()" function
8055 */
8056 static void
8057f_searchpair(typval_T *argvars, typval_T *rettv)
8058{
8059 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
8060}
8061
8062/*
8063 * "searchpairpos()" function
8064 */
8065 static void
8066f_searchpairpos(typval_T *argvars, typval_T *rettv)
8067{
8068 pos_T match_pos;
8069 int lnum = 0;
8070 int col = 0;
8071
8072 if (rettv_list_alloc(rettv) == FAIL)
8073 return;
8074
8075 if (searchpair_cmn(argvars, &match_pos) > 0)
8076 {
8077 lnum = match_pos.lnum;
8078 col = match_pos.col;
8079 }
8080
8081 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8082 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8083}
8084
8085/*
8086 * Search for a start/middle/end thing.
8087 * Used by searchpair(), see its documentation for the details.
8088 * Returns 0 or -1 for no match,
8089 */
8090 long
8091do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008092 char_u *spat, // start pattern
8093 char_u *mpat, // middle pattern
8094 char_u *epat, // end pattern
8095 int dir, // BACKWARD or FORWARD
8096 typval_T *skip, // skip expression
8097 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008098 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008099 linenr_T lnum_stop, // stop at this line if not zero
8100 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008101{
8102 char_u *save_cpo;
8103 char_u *pat, *pat2 = NULL, *pat3 = NULL;
8104 long retval = 0;
8105 pos_T pos;
8106 pos_T firstpos;
8107 pos_T foundpos;
8108 pos_T save_cursor;
8109 pos_T save_pos;
8110 int n;
8111 int r;
8112 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01008113 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008114 int err;
8115 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008116#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008117 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008118#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008119
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008120 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008121 save_cpo = p_cpo;
8122 p_cpo = empty_option;
8123
8124#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008125 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008126 profile_setlimit(time_limit, &tm);
8127#endif
8128
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008129 // Make two search patterns: start/end (pat2, for in nested pairs) and
8130 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02008131 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
8132 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008133 if (pat2 == NULL || pat3 == NULL)
8134 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008135 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008136 if (*mpat == NUL)
8137 STRCPY(pat3, pat2);
8138 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008139 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008140 spat, epat, mpat);
8141 if (flags & SP_START)
8142 options |= SEARCH_START;
8143
Bram Moolenaar48570482017-10-30 21:48:41 +01008144 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02008145 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01008146
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008147 save_cursor = curwin->w_cursor;
8148 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008149 CLEAR_POS(&firstpos);
8150 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008151 pat = pat3;
8152 for (;;)
8153 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008154 searchit_arg_T sia;
8155
Bram Moolenaara80faa82020-04-12 19:37:17 +02008156 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008157 sia.sa_stop_lnum = lnum_stop;
8158#ifdef FEAT_RELTIME
8159 sia.sa_tm = &tm;
8160#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01008161 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008162 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008163 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008164 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165 break;
8166
8167 if (firstpos.lnum == 0)
8168 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008169 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008170 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008171 // Found the same position again. Can happen with a pattern that
8172 // has "\zs" at the end and searching backwards. Advance one
8173 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008174 if (dir == BACKWARD)
8175 decl(&pos);
8176 else
8177 incl(&pos);
8178 }
8179 foundpos = pos;
8180
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008181 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008182 options &= ~SEARCH_START;
8183
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008184 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01008185 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008186 {
8187 save_pos = curwin->w_cursor;
8188 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01008189 err = FALSE;
8190 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008191 curwin->w_cursor = save_pos;
8192 if (err)
8193 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008194 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008195 curwin->w_cursor = save_cursor;
8196 retval = -1;
8197 break;
8198 }
8199 if (r)
8200 continue;
8201 }
8202
8203 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8204 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008205 // Found end when searching backwards or start when searching
8206 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008207 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008208 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008209 }
8210 else
8211 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008212 // Found end when searching forward or start when searching
8213 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008214 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008215 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008216 }
8217
8218 if (nest == 0)
8219 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008220 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008221 if (flags & SP_RETCOUNT)
8222 ++retval;
8223 else
8224 retval = pos.lnum;
8225 if (flags & SP_SETPCMARK)
8226 setpcmark();
8227 curwin->w_cursor = pos;
8228 if (!(flags & SP_REPEAT))
8229 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008230 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008231 }
8232 }
8233
8234 if (match_pos != NULL)
8235 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008236 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008237 match_pos->lnum = curwin->w_cursor.lnum;
8238 match_pos->col = curwin->w_cursor.col + 1;
8239 }
8240
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008241 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008242 if ((flags & SP_NOMOVE) || retval == 0)
8243 curwin->w_cursor = save_cursor;
8244
8245theend:
8246 vim_free(pat2);
8247 vim_free(pat3);
8248 if (p_cpo == empty_option)
8249 p_cpo = save_cpo;
8250 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008251 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008252 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008253 // If it's still empty it was changed and restored, need to restore in
8254 // the complicated way.
8255 if (*p_cpo == NUL)
8256 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008257 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008258 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008259
8260 return retval;
8261}
8262
8263/*
8264 * "searchpos()" function
8265 */
8266 static void
8267f_searchpos(typval_T *argvars, typval_T *rettv)
8268{
8269 pos_T match_pos;
8270 int lnum = 0;
8271 int col = 0;
8272 int n;
8273 int flags = 0;
8274
8275 if (rettv_list_alloc(rettv) == FAIL)
8276 return;
8277
8278 n = search_cmn(argvars, &match_pos, &flags);
8279 if (n > 0)
8280 {
8281 lnum = match_pos.lnum;
8282 col = match_pos.col;
8283 }
8284
8285 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8286 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8287 if (flags & SP_SUBPAT)
8288 list_append_number(rettv->vval.v_list, (varnumber_T)n);
8289}
8290
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008291/*
8292 * Set the cursor or mark position.
8293 * If 'charpos' is TRUE, then use the column number as a character offet.
8294 * Otherwise use the column number as a byte offset.
8295 */
8296 static void
8297set_position(typval_T *argvars, typval_T *rettv, int charpos)
8298{
8299 pos_T pos;
8300 int fnum;
8301 char_u *name;
8302 colnr_T curswant = -1;
8303
8304 rettv->vval.v_number = -1;
8305
8306 name = tv_get_string_chk(argvars);
8307 if (name != NULL)
8308 {
8309 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
8310 {
8311 if (pos.col != MAXCOL && --pos.col < 0)
8312 pos.col = 0;
8313 if ((name[0] == '.' && name[1] == NUL))
8314 {
8315 // set cursor; "fnum" is ignored
8316 curwin->w_cursor = pos;
8317 if (curswant >= 0)
8318 {
8319 curwin->w_curswant = curswant - 1;
8320 curwin->w_set_curswant = FALSE;
8321 }
8322 check_cursor();
8323 rettv->vval.v_number = 0;
8324 }
8325 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8326 {
8327 // set mark
8328 if (setmark_pos(name[1], &pos, fnum) == OK)
8329 rettv->vval.v_number = 0;
8330 }
8331 else
8332 emsg(_(e_invarg));
8333 }
8334 }
8335}
8336/*
8337 * "setcharpos()" function
8338 */
8339 static void
8340f_setcharpos(typval_T *argvars, typval_T *rettv)
8341{
8342 set_position(argvars, rettv, TRUE);
8343}
8344
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008345 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008346f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8347{
8348 dict_T *d;
8349 dictitem_T *di;
8350 char_u *csearch;
8351
8352 if (argvars[0].v_type != VAR_DICT)
8353 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008354 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008355 return;
8356 }
8357
8358 if ((d = argvars[0].vval.v_dict) != NULL)
8359 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008360 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008361 if (csearch != NULL)
8362 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008363 if (enc_utf8)
8364 {
8365 int pcc[MAX_MCO];
8366 int c = utfc_ptr2char(csearch, pcc);
8367
8368 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8369 }
8370 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008371 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008372 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008373 }
8374
8375 di = dict_find(d, (char_u *)"forward", -1);
8376 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008377 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008378 ? FORWARD : BACKWARD);
8379
8380 di = dict_find(d, (char_u *)"until", -1);
8381 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008382 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008383 }
8384}
8385
8386/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008387 * "setcursorcharpos" function
8388 */
8389 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008390f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008391{
8392 set_cursorpos(argvars, rettv, TRUE);
8393}
8394
8395/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008396 * "setenv()" function
8397 */
8398 static void
8399f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8400{
8401 char_u namebuf[NUMBUFLEN];
8402 char_u valbuf[NUMBUFLEN];
8403 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8404
8405 if (argvars[1].v_type == VAR_SPECIAL
8406 && argvars[1].vval.v_number == VVAL_NULL)
8407 vim_unsetenv(name);
8408 else
8409 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8410}
8411
8412/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413 * "setfperm({fname}, {mode})" function
8414 */
8415 static void
8416f_setfperm(typval_T *argvars, typval_T *rettv)
8417{
8418 char_u *fname;
8419 char_u modebuf[NUMBUFLEN];
8420 char_u *mode_str;
8421 int i;
8422 int mask;
8423 int mode = 0;
8424
8425 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008426 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008427 if (fname == NULL)
8428 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008429 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008430 if (mode_str == NULL)
8431 return;
8432 if (STRLEN(mode_str) != 9)
8433 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008434 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008435 return;
8436 }
8437
8438 mask = 1;
8439 for (i = 8; i >= 0; --i)
8440 {
8441 if (mode_str[i] != '-')
8442 mode |= mask;
8443 mask = mask << 1;
8444 }
8445 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8446}
8447
8448/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008449 * "setpos()" function
8450 */
8451 static void
8452f_setpos(typval_T *argvars, typval_T *rettv)
8453{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008454 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008455}
8456
8457/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008458 * Translate a register type string to the yank type and block length
8459 */
8460 static int
8461get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8462{
8463 char_u *stropt = *pp;
8464 switch (*stropt)
8465 {
8466 case 'v': case 'c': // character-wise selection
8467 *yank_type = MCHAR;
8468 break;
8469 case 'V': case 'l': // line-wise selection
8470 *yank_type = MLINE;
8471 break;
8472 case 'b': case Ctrl_V: // block-wise selection
8473 *yank_type = MBLOCK;
8474 if (VIM_ISDIGIT(stropt[1]))
8475 {
8476 ++stropt;
8477 *block_len = getdigits(&stropt) - 1;
8478 --stropt;
8479 }
8480 break;
8481 default:
8482 return FAIL;
8483 }
8484 *pp = stropt;
8485 return OK;
8486}
8487
8488/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008489 * "setreg()" function
8490 */
8491 static void
8492f_setreg(typval_T *argvars, typval_T *rettv)
8493{
8494 int regname;
8495 char_u *strregname;
8496 char_u *stropt;
8497 char_u *strval;
8498 int append;
8499 char_u yank_type;
8500 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008501 typval_T *regcontents;
8502 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008503
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008504 pointreg = 0;
8505 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008506 block_len = -1;
8507 yank_type = MAUTO;
8508 append = FALSE;
8509
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008510 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008511 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008512
8513 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008514 return; // type error; errmsg already given
Bram Moolenaar418a29f2021-02-10 22:23:41 +01008515 if (in_vim9script() && STRLEN(strregname) > 1)
8516 {
8517 semsg(_(e_register_name_must_be_one_char_str), strregname);
8518 return;
8519 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008520 regname = *strregname;
8521 if (regname == 0 || regname == '@')
8522 regname = '"';
8523
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008524 if (argvars[1].v_type == VAR_DICT)
8525 {
8526 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008527 dictitem_T *di;
8528
8529 if (d == NULL || d->dv_hashtab.ht_used == 0)
8530 {
8531 // Empty dict, clear the register (like setreg(0, []))
8532 char_u *lstval[2] = {NULL, NULL};
8533 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8534 return;
8535 }
8536
8537 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008538 if (di != NULL)
8539 regcontents = &di->di_tv;
8540
8541 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8542 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008543 {
8544 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8545
8546 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008547 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008548 semsg(_(e_invargval), "value");
8549 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008550 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008551 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008552
8553 if (regname == '"')
8554 {
8555 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8556 if (stropt != NULL)
8557 {
8558 pointreg = *stropt;
8559 regname = pointreg;
8560 }
8561 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008562 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008563 pointreg = regname;
8564 }
8565 else
8566 regcontents = &argvars[1];
8567
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008568 if (argvars[2].v_type != VAR_UNKNOWN)
8569 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008570 if (yank_type != MAUTO)
8571 {
8572 semsg(_(e_toomanyarg), "setreg");
8573 return;
8574 }
8575
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008576 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008577 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008578 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008579 for (; *stropt != NUL; ++stropt)
8580 switch (*stropt)
8581 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008582 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583 append = TRUE;
8584 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008585 default:
8586 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008587 }
8588 }
8589
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008590 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008591 {
8592 char_u **lstval;
8593 char_u **allocval;
8594 char_u buf[NUMBUFLEN];
8595 char_u **curval;
8596 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008597 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008598 listitem_T *li;
8599 int len;
8600
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008601 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602 len = ll == NULL ? 0 : ll->lv_len;
8603
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008604 // First half: use for pointers to result lines; second half: use for
8605 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008606 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008607 if (lstval == NULL)
8608 return;
8609 curval = lstval;
8610 allocval = lstval + len + 2;
8611 curallocval = allocval;
8612
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008613 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008614 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008615 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008616 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008617 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008618 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008619 if (strval == NULL)
8620 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008621 if (strval == buf)
8622 {
8623 // Need to make a copy, next tv_get_string_buf_chk() will
8624 // overwrite the string.
8625 strval = vim_strsave(buf);
8626 if (strval == NULL)
8627 goto free_lstval;
8628 *curallocval++ = strval;
8629 }
8630 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008631 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008632 }
8633 *curval++ = NULL;
8634
8635 write_reg_contents_lst(regname, lstval, -1,
8636 append, yank_type, block_len);
8637free_lstval:
8638 while (curallocval > allocval)
8639 vim_free(*--curallocval);
8640 vim_free(lstval);
8641 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008642 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008643 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008644 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645 if (strval == NULL)
8646 return;
8647 write_reg_contents_ex(regname, strval, -1,
8648 append, yank_type, block_len);
8649 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008650 if (pointreg != 0)
8651 get_yank_register(pointreg, TRUE);
8652
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008653 rettv->vval.v_number = 0;
8654}
8655
8656/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008657 * "settagstack()" function
8658 */
8659 static void
8660f_settagstack(typval_T *argvars, typval_T *rettv)
8661{
8662 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8663 win_T *wp;
8664 dict_T *d;
8665 int action = 'r';
8666
8667 rettv->vval.v_number = -1;
8668
8669 // first argument: window number or id
8670 wp = find_win_by_nr_or_id(&argvars[0]);
8671 if (wp == NULL)
8672 return;
8673
8674 // second argument: dict with items to set in the tag stack
8675 if (argvars[1].v_type != VAR_DICT)
8676 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008677 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008678 return;
8679 }
8680 d = argvars[1].vval.v_dict;
8681 if (d == NULL)
8682 return;
8683
8684 // third argument: action - 'a' for append and 'r' for replace.
8685 // default is to replace the stack.
8686 if (argvars[2].v_type == VAR_UNKNOWN)
8687 action = 'r';
8688 else if (argvars[2].v_type == VAR_STRING)
8689 {
8690 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008691 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008692 if (actstr == NULL)
8693 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008694 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8695 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008696 action = *actstr;
8697 else
8698 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008699 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008700 return;
8701 }
8702 }
8703 else
8704 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008705 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008706 return;
8707 }
8708
8709 if (set_tagstack(wp, d, action) == OK)
8710 rettv->vval.v_number = 0;
8711}
8712
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008713#ifdef FEAT_CRYPT
8714/*
8715 * "sha256({string})" function
8716 */
8717 static void
8718f_sha256(typval_T *argvars, typval_T *rettv)
8719{
8720 char_u *p;
8721
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008722 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723 rettv->vval.v_string = vim_strsave(
8724 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8725 rettv->v_type = VAR_STRING;
8726}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008727#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008728
8729/*
8730 * "shellescape({string})" function
8731 */
8732 static void
8733f_shellescape(typval_T *argvars, typval_T *rettv)
8734{
Bram Moolenaar20615522017-06-05 18:46:26 +02008735 int do_special = non_zero_arg(&argvars[1]);
8736
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008737 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008738 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008739 rettv->v_type = VAR_STRING;
8740}
8741
8742/*
8743 * shiftwidth() function
8744 */
8745 static void
8746f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8747{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008748 rettv->vval.v_number = 0;
8749
8750 if (argvars[0].v_type != VAR_UNKNOWN)
8751 {
8752 long col;
8753
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008754 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008755 if (col < 0)
8756 return; // type error; errmsg already given
8757#ifdef FEAT_VARTABS
8758 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8759 return;
8760#endif
8761 }
8762
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008763 rettv->vval.v_number = get_sw_value(curbuf);
8764}
8765
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008766#ifdef FEAT_FLOAT
8767/*
8768 * "sin()" function
8769 */
8770 static void
8771f_sin(typval_T *argvars, typval_T *rettv)
8772{
8773 float_T f = 0.0;
8774
8775 rettv->v_type = VAR_FLOAT;
8776 if (get_float_arg(argvars, &f) == OK)
8777 rettv->vval.v_float = sin(f);
8778 else
8779 rettv->vval.v_float = 0.0;
8780}
8781
8782/*
8783 * "sinh()" function
8784 */
8785 static void
8786f_sinh(typval_T *argvars, typval_T *rettv)
8787{
8788 float_T f = 0.0;
8789
8790 rettv->v_type = VAR_FLOAT;
8791 if (get_float_arg(argvars, &f) == OK)
8792 rettv->vval.v_float = sinh(f);
8793 else
8794 rettv->vval.v_float = 0.0;
8795}
8796#endif
8797
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008798/*
8799 * "soundfold({word})" function
8800 */
8801 static void
8802f_soundfold(typval_T *argvars, typval_T *rettv)
8803{
8804 char_u *s;
8805
8806 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008807 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008808#ifdef FEAT_SPELL
8809 rettv->vval.v_string = eval_soundfold(s);
8810#else
8811 rettv->vval.v_string = vim_strsave(s);
8812#endif
8813}
8814
8815/*
8816 * "spellbadword()" function
8817 */
8818 static void
8819f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8820{
8821 char_u *word = (char_u *)"";
8822 hlf_T attr = HLF_COUNT;
8823 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008824#ifdef FEAT_SPELL
8825 int wo_spell_save = curwin->w_p_spell;
8826
8827 if (!curwin->w_p_spell)
8828 {
8829 did_set_spelllang(curwin);
8830 curwin->w_p_spell = TRUE;
8831 }
8832
8833 if (*curwin->w_s->b_p_spl == NUL)
8834 {
8835 emsg(_(e_no_spell));
8836 curwin->w_p_spell = wo_spell_save;
8837 return;
8838 }
8839#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840
8841 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008842 {
8843#ifdef FEAT_SPELL
8844 curwin->w_p_spell = wo_spell_save;
8845#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008846 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008847 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008848
8849#ifdef FEAT_SPELL
8850 if (argvars[0].v_type == VAR_UNKNOWN)
8851 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008852 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008853 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8854 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008855 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008856 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008857 curwin->w_set_curswant = TRUE;
8858 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008859 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008860 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008861 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008862 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008863 int capcol = -1;
8864
8865 if (str != NULL)
8866 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008867 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008868 while (*str != NUL)
8869 {
8870 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8871 if (attr != HLF_COUNT)
8872 {
8873 word = str;
8874 break;
8875 }
8876 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008877 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008878 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008879 }
8880 }
8881 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008882 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008883#endif
8884
8885 list_append_string(rettv->vval.v_list, word, len);
8886 list_append_string(rettv->vval.v_list, (char_u *)(
8887 attr == HLF_SPB ? "bad" :
8888 attr == HLF_SPR ? "rare" :
8889 attr == HLF_SPL ? "local" :
8890 attr == HLF_SPC ? "caps" :
8891 ""), -1);
8892}
8893
8894/*
8895 * "spellsuggest()" function
8896 */
8897 static void
8898f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8899{
8900#ifdef FEAT_SPELL
8901 char_u *str;
8902 int typeerr = FALSE;
8903 int maxcount;
8904 garray_T ga;
8905 int i;
8906 listitem_T *li;
8907 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008908 int wo_spell_save = curwin->w_p_spell;
8909
8910 if (!curwin->w_p_spell)
8911 {
8912 did_set_spelllang(curwin);
8913 curwin->w_p_spell = TRUE;
8914 }
8915
8916 if (*curwin->w_s->b_p_spl == NUL)
8917 {
8918 emsg(_(e_no_spell));
8919 curwin->w_p_spell = wo_spell_save;
8920 return;
8921 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008922#endif
8923
8924 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008925 {
8926#ifdef FEAT_SPELL
8927 curwin->w_p_spell = wo_spell_save;
8928#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008929 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008930 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008931
8932#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008933 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008934 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008935 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008936 if (argvars[1].v_type != VAR_UNKNOWN)
8937 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008938 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008939 if (maxcount <= 0)
8940 return;
8941 if (argvars[2].v_type != VAR_UNKNOWN)
8942 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008943 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008944 if (typeerr)
8945 return;
8946 }
8947 }
8948 else
8949 maxcount = 25;
8950
8951 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8952
8953 for (i = 0; i < ga.ga_len; ++i)
8954 {
8955 str = ((char_u **)ga.ga_data)[i];
8956
8957 li = listitem_alloc();
8958 if (li == NULL)
8959 vim_free(str);
8960 else
8961 {
8962 li->li_tv.v_type = VAR_STRING;
8963 li->li_tv.v_lock = 0;
8964 li->li_tv.vval.v_string = str;
8965 list_append(rettv->vval.v_list, li);
8966 }
8967 }
8968 ga_clear(&ga);
8969 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008970 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008971#endif
8972}
8973
8974 static void
8975f_split(typval_T *argvars, typval_T *rettv)
8976{
8977 char_u *str;
8978 char_u *end;
8979 char_u *pat = NULL;
8980 regmatch_T regmatch;
8981 char_u patbuf[NUMBUFLEN];
8982 char_u *save_cpo;
8983 int match;
8984 colnr_T col = 0;
8985 int keepempty = FALSE;
8986 int typeerr = FALSE;
8987
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008988 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008989 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008990 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008991
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008992 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008993 if (argvars[1].v_type != VAR_UNKNOWN)
8994 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008995 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008996 if (pat == NULL)
8997 typeerr = TRUE;
8998 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008999 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009000 }
9001 if (pat == NULL || *pat == NUL)
9002 pat = (char_u *)"[\\x01- ]\\+";
9003
9004 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02009005 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009006 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02009007 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009008
9009 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9010 if (regmatch.regprog != NULL)
9011 {
9012 regmatch.rm_ic = FALSE;
9013 while (*str != NUL || keepempty)
9014 {
9015 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009016 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009017 else
9018 match = vim_regexec_nl(&regmatch, str, col);
9019 if (match)
9020 end = regmatch.startp[0];
9021 else
9022 end = str + STRLEN(str);
9023 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
9024 && *str != NUL && match && end < regmatch.endp[0]))
9025 {
9026 if (list_append_string(rettv->vval.v_list, str,
9027 (int)(end - str)) == FAIL)
9028 break;
9029 }
9030 if (!match)
9031 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01009032 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033 if (regmatch.endp[0] > str)
9034 col = 0;
9035 else
Bram Moolenaar13505972019-01-24 15:04:48 +01009036 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009037 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009038 str = regmatch.endp[0];
9039 }
9040
9041 vim_regfree(regmatch.regprog);
9042 }
9043
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02009044theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009045 p_cpo = save_cpo;
9046}
9047
9048#ifdef FEAT_FLOAT
9049/*
9050 * "sqrt()" function
9051 */
9052 static void
9053f_sqrt(typval_T *argvars, typval_T *rettv)
9054{
9055 float_T f = 0.0;
9056
9057 rettv->v_type = VAR_FLOAT;
9058 if (get_float_arg(argvars, &f) == OK)
9059 rettv->vval.v_float = sqrt(f);
9060 else
9061 rettv->vval.v_float = 0.0;
9062}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009063#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009064
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009065#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01009066/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009067 * "str2float()" function
9068 */
9069 static void
9070f_str2float(typval_T *argvars, typval_T *rettv)
9071{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009072 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009073 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009074
Bram Moolenaar08243d22017-01-10 16:12:29 +01009075 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009076 p = skipwhite(p + 1);
9077 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01009078 if (isneg)
9079 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009080 rettv->v_type = VAR_FLOAT;
9081}
9082#endif
9083
9084/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02009085 * "str2list()" function
9086 */
9087 static void
9088f_str2list(typval_T *argvars, typval_T *rettv)
9089{
9090 char_u *p;
9091 int utf8 = FALSE;
9092
9093 if (rettv_list_alloc(rettv) == FAIL)
9094 return;
9095
9096 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02009097 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02009098
9099 p = tv_get_string(&argvars[0]);
9100
9101 if (has_mbyte || utf8)
9102 {
9103 int (*ptr2len)(char_u *);
9104 int (*ptr2char)(char_u *);
9105
9106 if (utf8 || enc_utf8)
9107 {
9108 ptr2len = utf_ptr2len;
9109 ptr2char = utf_ptr2char;
9110 }
9111 else
9112 {
9113 ptr2len = mb_ptr2len;
9114 ptr2char = mb_ptr2char;
9115 }
9116
9117 for ( ; *p != NUL; p += (*ptr2len)(p))
9118 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
9119 }
9120 else
9121 for ( ; *p != NUL; ++p)
9122 list_append_number(rettv->vval.v_list, *p);
9123}
9124
9125/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009126 * "str2nr()" function
9127 */
9128 static void
9129f_str2nr(typval_T *argvars, typval_T *rettv)
9130{
9131 int base = 10;
9132 char_u *p;
9133 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009134 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01009135 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009136
9137 if (argvars[1].v_type != VAR_UNKNOWN)
9138 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009139 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009140 if (base != 2 && base != 8 && base != 10 && base != 16)
9141 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009142 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009143 return;
9144 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02009145 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009146 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009147 }
9148
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01009149 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009150 isneg = (*p == '-');
9151 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009152 p = skipwhite(p + 1);
9153 switch (base)
9154 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009155 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02009156 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009157 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009158 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02009159 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
9160 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01009161 if (isneg)
9162 rettv->vval.v_number = -n;
9163 else
9164 rettv->vval.v_number = n;
9165
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009166}
9167
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009168/*
9169 * "strgetchar()" function
9170 */
9171 static void
9172f_strgetchar(typval_T *argvars, typval_T *rettv)
9173{
9174 char_u *str;
9175 int len;
9176 int error = FALSE;
9177 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01009178 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009179
9180 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009181 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009182 if (str == NULL)
9183 return;
9184 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009185 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009186 if (error)
9187 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009188
Bram Moolenaar13505972019-01-24 15:04:48 +01009189 while (charidx >= 0 && byteidx < len)
9190 {
9191 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009192 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009193 rettv->vval.v_number = mb_ptr2char(str + byteidx);
9194 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009195 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009196 --charidx;
9197 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009198 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009199}
9200
9201/*
9202 * "stridx()" function
9203 */
9204 static void
9205f_stridx(typval_T *argvars, typval_T *rettv)
9206{
9207 char_u buf[NUMBUFLEN];
9208 char_u *needle;
9209 char_u *haystack;
9210 char_u *save_haystack;
9211 char_u *pos;
9212 int start_idx;
9213
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009214 needle = tv_get_string_chk(&argvars[1]);
9215 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009216 rettv->vval.v_number = -1;
9217 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009218 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009219
9220 if (argvars[2].v_type != VAR_UNKNOWN)
9221 {
9222 int error = FALSE;
9223
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009224 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009225 if (error || start_idx >= (int)STRLEN(haystack))
9226 return;
9227 if (start_idx >= 0)
9228 haystack += start_idx;
9229 }
9230
9231 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9232 if (pos != NULL)
9233 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
9234}
9235
9236/*
9237 * "string()" function
9238 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009239 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009240f_string(typval_T *argvars, typval_T *rettv)
9241{
9242 char_u *tofree;
9243 char_u numbuf[NUMBUFLEN];
9244
9245 rettv->v_type = VAR_STRING;
9246 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
9247 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009248 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009249 if (rettv->vval.v_string != NULL && tofree == NULL)
9250 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
9251}
9252
9253/*
9254 * "strlen()" function
9255 */
9256 static void
9257f_strlen(typval_T *argvars, typval_T *rettv)
9258{
9259 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009260 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009261}
9262
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01009263 static void
9264strchar_common(typval_T *argvars, typval_T *rettv, int skipcc)
9265{
9266 char_u *s = tv_get_string(&argvars[0]);
9267 varnumber_T len = 0;
9268 int (*func_mb_ptr2char_adv)(char_u **pp);
9269
9270 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
9271 while (*s != NUL)
9272 {
9273 func_mb_ptr2char_adv(&s);
9274 ++len;
9275 }
9276 rettv->vval.v_number = len;
9277}
9278
9279/*
9280 * "strcharlen()" function
9281 */
9282 static void
9283f_strcharlen(typval_T *argvars, typval_T *rettv)
9284{
9285 strchar_common(argvars, rettv, TRUE);
9286}
9287
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009288/*
9289 * "strchars()" function
9290 */
9291 static void
9292f_strchars(typval_T *argvars, typval_T *rettv)
9293{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009294 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009295
9296 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009297 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009298 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02009299 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009300 else
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01009301 strchar_common(argvars, rettv, skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009302}
9303
9304/*
9305 * "strdisplaywidth()" function
9306 */
9307 static void
9308f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
9309{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009310 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009311 int col = 0;
9312
9313 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009314 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009315
9316 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
9317}
9318
9319/*
9320 * "strwidth()" function
9321 */
9322 static void
9323f_strwidth(typval_T *argvars, typval_T *rettv)
9324{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009325 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009326
Bram Moolenaar13505972019-01-24 15:04:48 +01009327 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009328}
9329
9330/*
9331 * "strcharpart()" function
9332 */
9333 static void
9334f_strcharpart(typval_T *argvars, typval_T *rettv)
9335{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009336 char_u *p;
9337 int nchar;
9338 int nbyte = 0;
9339 int charlen;
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009340 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009341 int len = 0;
9342 int slen;
9343 int error = FALSE;
9344
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009345 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009346 slen = (int)STRLEN(p);
9347
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009348 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009349 if (!error)
9350 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009351 if (argvars[2].v_type != VAR_UNKNOWN
9352 && argvars[3].v_type != VAR_UNKNOWN)
9353 {
9354 skipcc = tv_get_bool(&argvars[3]);
9355 if (skipcc < 0 || skipcc > 1)
9356 {
9357 semsg(_(e_using_number_as_bool_nr), skipcc);
9358 return;
9359 }
9360 }
9361
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009362 if (nchar > 0)
9363 while (nchar > 0 && nbyte < slen)
9364 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009365 if (skipcc)
9366 nbyte += mb_ptr2len(p + nbyte);
9367 else
9368 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009369 --nchar;
9370 }
9371 else
9372 nbyte = nchar;
9373 if (argvars[2].v_type != VAR_UNKNOWN)
9374 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009375 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009376 while (charlen > 0 && nbyte + len < slen)
9377 {
9378 int off = nbyte + len;
9379
9380 if (off < 0)
9381 len += 1;
9382 else
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009383 {
9384 if (skipcc)
9385 len += mb_ptr2len(p + off);
9386 else
9387 len += MB_CPTR2LEN(p + off);
9388 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009389 --charlen;
9390 }
9391 }
9392 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009393 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009394 }
9395
9396 /*
9397 * Only return the overlap between the specified part and the actual
9398 * string.
9399 */
9400 if (nbyte < 0)
9401 {
9402 len += nbyte;
9403 nbyte = 0;
9404 }
9405 else if (nbyte > slen)
9406 nbyte = slen;
9407 if (len < 0)
9408 len = 0;
9409 else if (nbyte + len > slen)
9410 len = slen - nbyte;
9411
9412 rettv->v_type = VAR_STRING;
9413 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009414}
9415
9416/*
9417 * "strpart()" function
9418 */
9419 static void
9420f_strpart(typval_T *argvars, typval_T *rettv)
9421{
9422 char_u *p;
9423 int n;
9424 int len;
9425 int slen;
9426 int error = FALSE;
9427
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009428 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009429 slen = (int)STRLEN(p);
9430
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009431 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009432 if (error)
9433 len = 0;
9434 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009435 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009436 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009437 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009438
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009439 // Only return the overlap between the specified part and the actual
9440 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009441 if (n < 0)
9442 {
9443 len += n;
9444 n = 0;
9445 }
9446 else if (n > slen)
9447 n = slen;
9448 if (len < 0)
9449 len = 0;
9450 else if (n + len > slen)
9451 len = slen - n;
9452
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009453 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9454 {
9455 int off;
9456
9457 // length in characters
9458 for (off = n; off < slen && len > 0; --len)
9459 off += mb_ptr2len(p + off);
9460 len = off - n;
9461 }
9462
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009463 rettv->v_type = VAR_STRING;
9464 rettv->vval.v_string = vim_strnsave(p + n, len);
9465}
9466
9467/*
9468 * "strridx()" function
9469 */
9470 static void
9471f_strridx(typval_T *argvars, typval_T *rettv)
9472{
9473 char_u buf[NUMBUFLEN];
9474 char_u *needle;
9475 char_u *haystack;
9476 char_u *rest;
9477 char_u *lastmatch = NULL;
9478 int haystack_len, end_idx;
9479
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009480 needle = tv_get_string_chk(&argvars[1]);
9481 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009482
9483 rettv->vval.v_number = -1;
9484 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009485 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009486
9487 haystack_len = (int)STRLEN(haystack);
9488 if (argvars[2].v_type != VAR_UNKNOWN)
9489 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009490 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009491 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009492 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009493 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009494 }
9495 else
9496 end_idx = haystack_len;
9497
9498 if (*needle == NUL)
9499 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009500 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009501 lastmatch = haystack + end_idx;
9502 }
9503 else
9504 {
9505 for (rest = haystack; *rest != '\0'; ++rest)
9506 {
9507 rest = (char_u *)strstr((char *)rest, (char *)needle);
9508 if (rest == NULL || rest > haystack + end_idx)
9509 break;
9510 lastmatch = rest;
9511 }
9512 }
9513
9514 if (lastmatch == NULL)
9515 rettv->vval.v_number = -1;
9516 else
9517 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9518}
9519
9520/*
9521 * "strtrans()" function
9522 */
9523 static void
9524f_strtrans(typval_T *argvars, typval_T *rettv)
9525{
9526 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009527 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009528}
9529
9530/*
9531 * "submatch()" function
9532 */
9533 static void
9534f_submatch(typval_T *argvars, typval_T *rettv)
9535{
9536 int error = FALSE;
9537 int no;
9538 int retList = 0;
9539
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009540 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009541 if (error)
9542 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009543 if (no < 0 || no >= NSUBEXP)
9544 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009545 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009546 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009547 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009548 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009549 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009550 if (error)
9551 return;
9552
9553 if (retList == 0)
9554 {
9555 rettv->v_type = VAR_STRING;
9556 rettv->vval.v_string = reg_submatch(no);
9557 }
9558 else
9559 {
9560 rettv->v_type = VAR_LIST;
9561 rettv->vval.v_list = reg_submatch_list(no);
9562 }
9563}
9564
9565/*
9566 * "substitute()" function
9567 */
9568 static void
9569f_substitute(typval_T *argvars, typval_T *rettv)
9570{
9571 char_u patbuf[NUMBUFLEN];
9572 char_u subbuf[NUMBUFLEN];
9573 char_u flagsbuf[NUMBUFLEN];
9574
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009575 char_u *str = tv_get_string_chk(&argvars[0]);
9576 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009577 char_u *sub = NULL;
9578 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009579 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009580
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009581 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9582 expr = &argvars[2];
9583 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009584 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009585
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009586 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009587 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9588 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009589 rettv->vval.v_string = NULL;
9590 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009591 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009592}
9593
9594/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009595 * "swapinfo(swap_filename)" function
9596 */
9597 static void
9598f_swapinfo(typval_T *argvars, typval_T *rettv)
9599{
9600 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009601 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009602}
9603
9604/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009605 * "swapname(expr)" function
9606 */
9607 static void
9608f_swapname(typval_T *argvars, typval_T *rettv)
9609{
9610 buf_T *buf;
9611
9612 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009613 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009614 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9615 || buf->b_ml.ml_mfp->mf_fname == NULL)
9616 rettv->vval.v_string = NULL;
9617 else
9618 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9619}
9620
9621/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009622 * "synID(lnum, col, trans)" function
9623 */
9624 static void
9625f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9626{
9627 int id = 0;
9628#ifdef FEAT_SYN_HL
9629 linenr_T lnum;
9630 colnr_T col;
9631 int trans;
9632 int transerr = FALSE;
9633
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009634 lnum = tv_get_lnum(argvars); // -1 on type error
9635 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009636 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009637
9638 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9639 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9640 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9641#endif
9642
9643 rettv->vval.v_number = id;
9644}
9645
9646/*
9647 * "synIDattr(id, what [, mode])" function
9648 */
9649 static void
9650f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9651{
9652 char_u *p = NULL;
9653#ifdef FEAT_SYN_HL
9654 int id;
9655 char_u *what;
9656 char_u *mode;
9657 char_u modebuf[NUMBUFLEN];
9658 int modec;
9659
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009660 id = (int)tv_get_number(&argvars[0]);
9661 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009662 if (argvars[2].v_type != VAR_UNKNOWN)
9663 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009664 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009665 modec = TOLOWER_ASC(mode[0]);
9666 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009667 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009668 }
9669 else
9670 {
9671#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9672 if (USE_24BIT)
9673 modec = 'g';
9674 else
9675#endif
9676 if (t_colors > 1)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02009677 modec = 'c';
9678 else
9679 modec = 't';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009680 }
9681
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009682 switch (TOLOWER_ASC(what[0]))
9683 {
9684 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009685 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009686 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009687 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009688 p = highlight_has_attr(id, HL_BOLD, modec);
9689 break;
9690
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009691 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009692 p = highlight_color(id, what, modec);
9693 break;
9694
9695 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009696 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009697 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009698 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009699 p = highlight_has_attr(id, HL_ITALIC, modec);
9700 break;
9701
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009702 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009703 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009704 break;
9705
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009706 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009707 p = highlight_has_attr(id, HL_INVERSE, modec);
9708 break;
9709
9710 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009711 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009712 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009713 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009714 else if (TOLOWER_ASC(what[1]) == 't' &&
9715 TOLOWER_ASC(what[2]) == 'r')
9716 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009717 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009718 p = highlight_has_attr(id, HL_STANDOUT, modec);
9719 break;
9720
9721 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009722 if (TOLOWER_ASC(what[1]) == 'l') // ul
9723 p = highlight_color(id, what, modec);
9724 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009725 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009726 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9727 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009728 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009729 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9730 break;
9731 }
9732
9733 if (p != NULL)
9734 p = vim_strsave(p);
9735#endif
9736 rettv->v_type = VAR_STRING;
9737 rettv->vval.v_string = p;
9738}
9739
9740/*
9741 * "synIDtrans(id)" function
9742 */
9743 static void
9744f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9745{
9746 int id;
9747
9748#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009749 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009750
9751 if (id > 0)
9752 id = syn_get_final_id(id);
9753 else
9754#endif
9755 id = 0;
9756
9757 rettv->vval.v_number = id;
9758}
9759
9760/*
9761 * "synconcealed(lnum, col)" function
9762 */
9763 static void
9764f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9765{
9766#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9767 linenr_T lnum;
9768 colnr_T col;
9769 int syntax_flags = 0;
9770 int cchar;
9771 int matchid = 0;
9772 char_u str[NUMBUFLEN];
9773#endif
9774
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009775 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009776
9777#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009778 lnum = tv_get_lnum(argvars); // -1 on type error
9779 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009780
Bram Moolenaara80faa82020-04-12 19:37:17 +02009781 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009782
9783 if (rettv_list_alloc(rettv) != FAIL)
9784 {
9785 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9786 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9787 && curwin->w_p_cole > 0)
9788 {
9789 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9790 syntax_flags = get_syntax_info(&matchid);
9791
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009792 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009793 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9794 {
9795 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009796 if (cchar == NUL && curwin->w_p_cole == 1)
Bram Moolenaareed9d462021-02-15 20:38:25 +01009797 cchar = (curwin->w_lcs_chars.conceal == NUL) ? ' '
9798 : curwin->w_lcs_chars.conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009799 if (cchar != NUL)
9800 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009801 if (has_mbyte)
9802 (*mb_char2bytes)(cchar, str);
9803 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009804 str[0] = cchar;
9805 }
9806 }
9807 }
9808
9809 list_append_number(rettv->vval.v_list,
9810 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009811 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009812 list_append_string(rettv->vval.v_list, str, -1);
9813 list_append_number(rettv->vval.v_list, matchid);
9814 }
9815#endif
9816}
9817
9818/*
9819 * "synstack(lnum, col)" function
9820 */
9821 static void
9822f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9823{
9824#ifdef FEAT_SYN_HL
9825 linenr_T lnum;
9826 colnr_T col;
9827 int i;
9828 int id;
9829#endif
9830
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009831 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009832
9833#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009834 lnum = tv_get_lnum(argvars); // -1 on type error
9835 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009836
9837 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9838 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9839 && rettv_list_alloc(rettv) != FAIL)
9840 {
9841 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9842 for (i = 0; ; ++i)
9843 {
9844 id = syn_get_stack_item(i);
9845 if (id < 0)
9846 break;
9847 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9848 break;
9849 }
9850 }
9851#endif
9852}
9853
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009854/*
9855 * "tabpagebuflist()" function
9856 */
9857 static void
9858f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9859{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009860 tabpage_T *tp;
9861 win_T *wp = NULL;
9862
9863 if (argvars[0].v_type == VAR_UNKNOWN)
9864 wp = firstwin;
9865 else
9866 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009867 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009868 if (tp != NULL)
9869 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9870 }
9871 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9872 {
9873 for (; wp != NULL; wp = wp->w_next)
9874 if (list_append_number(rettv->vval.v_list,
9875 wp->w_buffer->b_fnum) == FAIL)
9876 break;
9877 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009878}
9879
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009880/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009881 * "tagfiles()" function
9882 */
9883 static void
9884f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9885{
9886 char_u *fname;
9887 tagname_T tn;
9888 int first;
9889
9890 if (rettv_list_alloc(rettv) == FAIL)
9891 return;
9892 fname = alloc(MAXPATHL);
9893 if (fname == NULL)
9894 return;
9895
9896 for (first = TRUE; ; first = FALSE)
9897 if (get_tagfname(&tn, first, fname) == FAIL
9898 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9899 break;
9900 tagname_free(&tn);
9901 vim_free(fname);
9902}
9903
9904/*
9905 * "taglist()" function
9906 */
9907 static void
9908f_taglist(typval_T *argvars, typval_T *rettv)
9909{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009910 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009911 char_u *tag_pattern;
9912
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009913 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009914
9915 rettv->vval.v_number = FALSE;
9916 if (*tag_pattern == NUL)
9917 return;
9918
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009919 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009920 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009921 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009922 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009923}
9924
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009925#ifdef FEAT_FLOAT
9926/*
9927 * "tan()" function
9928 */
9929 static void
9930f_tan(typval_T *argvars, typval_T *rettv)
9931{
9932 float_T f = 0.0;
9933
9934 rettv->v_type = VAR_FLOAT;
9935 if (get_float_arg(argvars, &f) == OK)
9936 rettv->vval.v_float = tan(f);
9937 else
9938 rettv->vval.v_float = 0.0;
9939}
9940
9941/*
9942 * "tanh()" function
9943 */
9944 static void
9945f_tanh(typval_T *argvars, typval_T *rettv)
9946{
9947 float_T f = 0.0;
9948
9949 rettv->v_type = VAR_FLOAT;
9950 if (get_float_arg(argvars, &f) == OK)
9951 rettv->vval.v_float = tanh(f);
9952 else
9953 rettv->vval.v_float = 0.0;
9954}
9955#endif
9956
9957/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009958 * "tolower(string)" function
9959 */
9960 static void
9961f_tolower(typval_T *argvars, typval_T *rettv)
9962{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009963 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009964 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009965}
9966
9967/*
9968 * "toupper(string)" function
9969 */
9970 static void
9971f_toupper(typval_T *argvars, typval_T *rettv)
9972{
9973 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009974 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009975}
9976
9977/*
9978 * "tr(string, fromstr, tostr)" function
9979 */
9980 static void
9981f_tr(typval_T *argvars, typval_T *rettv)
9982{
9983 char_u *in_str;
9984 char_u *fromstr;
9985 char_u *tostr;
9986 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009987 int inlen;
9988 int fromlen;
9989 int tolen;
9990 int idx;
9991 char_u *cpstr;
9992 int cplen;
9993 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009994 char_u buf[NUMBUFLEN];
9995 char_u buf2[NUMBUFLEN];
9996 garray_T ga;
9997
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009998 in_str = tv_get_string(&argvars[0]);
9999 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
10000 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010002 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010003 rettv->v_type = VAR_STRING;
10004 rettv->vval.v_string = NULL;
10005 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010006 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010007 ga_init2(&ga, (int)sizeof(char), 80);
10008
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010009 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010010 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010011 if (STRLEN(fromstr) != STRLEN(tostr))
10012 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010013error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010014 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010015 ga_clear(&ga);
10016 return;
10017 }
10018
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010019 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010020 while (*in_str != NUL)
10021 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010022 if (has_mbyte)
10023 {
10024 inlen = (*mb_ptr2len)(in_str);
10025 cpstr = in_str;
10026 cplen = inlen;
10027 idx = 0;
10028 for (p = fromstr; *p != NUL; p += fromlen)
10029 {
10030 fromlen = (*mb_ptr2len)(p);
10031 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
10032 {
10033 for (p = tostr; *p != NUL; p += tolen)
10034 {
10035 tolen = (*mb_ptr2len)(p);
10036 if (idx-- == 0)
10037 {
10038 cplen = tolen;
10039 cpstr = p;
10040 break;
10041 }
10042 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010043 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010044 goto error;
10045 break;
10046 }
10047 ++idx;
10048 }
10049
10050 if (first && cpstr == in_str)
10051 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010052 // Check that fromstr and tostr have the same number of
10053 // (multi-byte) characters. Done only once when a character
10054 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010055 first = FALSE;
10056 for (p = tostr; *p != NUL; p += tolen)
10057 {
10058 tolen = (*mb_ptr2len)(p);
10059 --idx;
10060 }
10061 if (idx != 0)
10062 goto error;
10063 }
10064
10065 (void)ga_grow(&ga, cplen);
10066 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
10067 ga.ga_len += cplen;
10068
10069 in_str += inlen;
10070 }
10071 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010072 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010073 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010074 p = vim_strchr(fromstr, *in_str);
10075 if (p != NULL)
10076 ga_append(&ga, tostr[p - fromstr]);
10077 else
10078 ga_append(&ga, *in_str);
10079 ++in_str;
10080 }
10081 }
10082
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010083 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010084 (void)ga_grow(&ga, 1);
10085 ga_append(&ga, NUL);
10086
10087 rettv->vval.v_string = ga.ga_data;
10088}
10089
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010090/*
10091 * "trim({expr})" function
10092 */
10093 static void
10094f_trim(typval_T *argvars, typval_T *rettv)
10095{
10096 char_u buf1[NUMBUFLEN];
10097 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010098 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010099 char_u *mask = NULL;
10100 char_u *tail;
10101 char_u *prev;
10102 char_u *p;
10103 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010104 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010105
10106 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010107 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010108 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010109 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010110
10111 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010112 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010113 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010114
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010115 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010116 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010117 int error = 0;
10118
10119 // leading or trailing characters to trim
10120 dir = (int)tv_get_number_chk(&argvars[2], &error);
10121 if (error)
10122 return;
10123 if (dir < 0 || dir > 2)
10124 {
10125 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
10126 return;
10127 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010128 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010129 }
10130
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010131 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010132 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010133 // Trim leading characters
10134 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010135 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010136 c1 = PTR2CHAR(head);
10137 if (mask == NULL)
10138 {
10139 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010140 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010141 }
10142 else
10143 {
10144 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10145 if (c1 == PTR2CHAR(p))
10146 break;
10147 if (*p == NUL)
10148 break;
10149 }
10150 MB_PTR_ADV(head);
10151 }
10152 }
10153
10154 tail = head + STRLEN(head);
10155 if (dir == 0 || dir == 2)
10156 {
10157 // Trim trailing characters
10158 for (; tail > head; tail = prev)
10159 {
10160 prev = tail;
10161 MB_PTR_BACK(head, prev);
10162 c1 = PTR2CHAR(prev);
10163 if (mask == NULL)
10164 {
10165 if (c1 > ' ' && c1 != 0xa0)
10166 break;
10167 }
10168 else
10169 {
10170 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10171 if (c1 == PTR2CHAR(p))
10172 break;
10173 if (*p == NUL)
10174 break;
10175 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010176 }
10177 }
Bram Moolenaardf44a272020-06-07 20:49:05 +020010178 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010179}
10180
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010181#ifdef FEAT_FLOAT
10182/*
10183 * "trunc({float})" function
10184 */
10185 static void
10186f_trunc(typval_T *argvars, typval_T *rettv)
10187{
10188 float_T f = 0.0;
10189
10190 rettv->v_type = VAR_FLOAT;
10191 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010192 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010193 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
10194 else
10195 rettv->vval.v_float = 0.0;
10196}
10197#endif
10198
10199/*
10200 * "type(expr)" function
10201 */
10202 static void
10203f_type(typval_T *argvars, typval_T *rettv)
10204{
10205 int n = -1;
10206
10207 switch (argvars[0].v_type)
10208 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010209 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
10210 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010211 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010212 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
10213 case VAR_LIST: n = VAR_TYPE_LIST; break;
10214 case VAR_DICT: n = VAR_TYPE_DICT; break;
10215 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
10216 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
10217 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020010218 case VAR_JOB: n = VAR_TYPE_JOB; break;
10219 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010220 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010221 case VAR_INSTR: n = VAR_TYPE_INSTR; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010222 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +020010223 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010224 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +010010225 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010226 n = -1;
10227 break;
10228 }
10229 rettv->vval.v_number = n;
10230}
10231
10232/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010233 * "virtcol(string)" function
10234 */
10235 static void
10236f_virtcol(typval_T *argvars, typval_T *rettv)
10237{
10238 colnr_T vcol = 0;
10239 pos_T *fp;
10240 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010241 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010242
Bram Moolenaar6f02b002021-01-10 20:22:54 +010010243 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010244 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
10245 && fnum == curbuf->b_fnum)
10246 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010247 // Limit the column to a valid value, getvvcol() doesn't check.
10248 if (fp->col < 0)
10249 fp->col = 0;
10250 else
10251 {
10252 len = (int)STRLEN(ml_get(fp->lnum));
10253 if (fp->col > len)
10254 fp->col = len;
10255 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010256 getvvcol(curwin, fp, NULL, NULL, &vcol);
10257 ++vcol;
10258 }
10259
10260 rettv->vval.v_number = vcol;
10261}
10262
10263/*
10264 * "visualmode()" function
10265 */
10266 static void
10267f_visualmode(typval_T *argvars, typval_T *rettv)
10268{
10269 char_u str[2];
10270
10271 rettv->v_type = VAR_STRING;
10272 str[0] = curbuf->b_visual_mode_eval;
10273 str[1] = NUL;
10274 rettv->vval.v_string = vim_strsave(str);
10275
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010276 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010277 if (non_zero_arg(&argvars[0]))
10278 curbuf->b_visual_mode_eval = NUL;
10279}
10280
10281/*
10282 * "wildmenumode()" function
10283 */
10284 static void
10285f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10286{
10287#ifdef FEAT_WILDMENU
10288 if (wild_menu_showing)
10289 rettv->vval.v_number = 1;
10290#endif
10291}
10292
10293/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +010010294 * "windowsversion()" function
10295 */
10296 static void
10297f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10298{
10299 rettv->v_type = VAR_STRING;
10300 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
10301}
10302
10303/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010304 * "wordcount()" function
10305 */
10306 static void
10307f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
10308{
10309 if (rettv_dict_alloc(rettv) == FAIL)
10310 return;
10311 cursor_pos_info(rettv->vval.v_dict);
10312}
10313
10314/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010315 * "xor(expr, expr)" function
10316 */
10317 static void
10318f_xor(typval_T *argvars, typval_T *rettv)
10319{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010320 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
10321 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010322}
10323
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010324#endif // FEAT_EVAL