blob: 01994b047563dcac39eb2b9bb460516e2b87500f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020023#ifdef FEAT_FLOAT
24static void f_abs(typval_T *argvars, typval_T *rettv);
25static void f_acos(typval_T *argvars, typval_T *rettv);
26#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020027static void f_and(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028#ifdef FEAT_FLOAT
29static void f_asin(typval_T *argvars, typval_T *rettv);
30static void f_atan(typval_T *argvars, typval_T *rettv);
31static void f_atan2(typval_T *argvars, typval_T *rettv);
32#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010033#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020034static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010035static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010036# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010037static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010038# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010039#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_byte2line(typval_T *argvars, typval_T *rettv);
41static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
42static void f_byteidx(typval_T *argvars, typval_T *rettv);
43static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
44static void f_call(typval_T *argvars, typval_T *rettv);
45#ifdef FEAT_FLOAT
46static void f_ceil(typval_T *argvars, typval_T *rettv);
47#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_changenr(typval_T *argvars, typval_T *rettv);
49static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010050static void f_charcol(typval_T *argvars, typval_T *rettv);
Bram Moolenaar17793ef2020-12-28 12:56:58 +010051static void f_charidx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020053static void f_confirm(typval_T *argvars, typval_T *rettv);
54static void f_copy(typval_T *argvars, typval_T *rettv);
55#ifdef FEAT_FLOAT
56static void f_cos(typval_T *argvars, typval_T *rettv);
57static void f_cosh(typval_T *argvars, typval_T *rettv);
58#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010060#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020061static void f_debugbreak(typval_T *argvars, typval_T *rettv);
62#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020063static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020064static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010065static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020066static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020067static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_escape(typval_T *argvars, typval_T *rettv);
69static void f_eval(typval_T *argvars, typval_T *rettv);
70static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_exists(typval_T *argvars, typval_T *rettv);
73#ifdef FEAT_FLOAT
74static void f_exp(typval_T *argvars, typval_T *rettv);
75#endif
76static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020077static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020078static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020079#ifdef FEAT_FLOAT
80static void f_float2nr(typval_T *argvars, typval_T *rettv);
81static void f_floor(typval_T *argvars, typval_T *rettv);
82static void f_fmod(typval_T *argvars, typval_T *rettv);
83#endif
84static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020085static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020086static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_function(typval_T *argvars, typval_T *rettv);
88static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
89static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010090static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010091static void f_getcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020092static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020093static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020094static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010095static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_getpid(typval_T *argvars, typval_T *rettv);
97static void f_getcurpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010098static void f_getcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020099static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200100static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200101static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100103static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +0200104static void f_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200105static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
106static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200107static void f_hlID(typval_T *argvars, typval_T *rettv);
108static void f_hlexists(typval_T *argvars, typval_T *rettv);
109static void f_hostname(typval_T *argvars, typval_T *rettv);
110static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200111static void f_index(typval_T *argvars, typval_T *rettv);
112static void f_input(typval_T *argvars, typval_T *rettv);
113static void f_inputdialog(typval_T *argvars, typval_T *rettv);
114static void f_inputlist(typval_T *argvars, typval_T *rettv);
115static void f_inputrestore(typval_T *argvars, typval_T *rettv);
116static void f_inputsave(typval_T *argvars, typval_T *rettv);
117static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +0100118static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200119static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120static void f_islocked(typval_T *argvars, typval_T *rettv);
121#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200122static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200123static void f_isnan(typval_T *argvars, typval_T *rettv);
124#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200125static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
126static void f_len(typval_T *argvars, typval_T *rettv);
127static void f_libcall(typval_T *argvars, typval_T *rettv);
128static void f_libcallnr(typval_T *argvars, typval_T *rettv);
129static void f_line(typval_T *argvars, typval_T *rettv);
130static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200131#ifdef FEAT_FLOAT
132static void f_log(typval_T *argvars, typval_T *rettv);
133static void f_log10(typval_T *argvars, typval_T *rettv);
134#endif
135#ifdef FEAT_LUA
136static void f_luaeval(typval_T *argvars, typval_T *rettv);
137#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200138static void f_maparg(typval_T *argvars, typval_T *rettv);
139static void f_mapcheck(typval_T *argvars, typval_T *rettv);
140static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200141static void f_matchend(typval_T *argvars, typval_T *rettv);
142static void f_matchlist(typval_T *argvars, typval_T *rettv);
143static void f_matchstr(typval_T *argvars, typval_T *rettv);
144static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
145static void f_max(typval_T *argvars, typval_T *rettv);
146static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200147#ifdef FEAT_MZSCHEME
148static void f_mzeval(typval_T *argvars, typval_T *rettv);
149#endif
150static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
151static void f_nr2char(typval_T *argvars, typval_T *rettv);
152static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200153#ifdef FEAT_PERL
154static void f_perleval(typval_T *argvars, typval_T *rettv);
155#endif
156#ifdef FEAT_FLOAT
157static void f_pow(typval_T *argvars, typval_T *rettv);
158#endif
159static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
160static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200161static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200162static void f_pumvisible(typval_T *argvars, typval_T *rettv);
163#ifdef FEAT_PYTHON3
164static void f_py3eval(typval_T *argvars, typval_T *rettv);
165#endif
166#ifdef FEAT_PYTHON
167static void f_pyeval(typval_T *argvars, typval_T *rettv);
168#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100169#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
170static void f_pyxeval(typval_T *argvars, typval_T *rettv);
171#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100172static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100173static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200175static void f_reg_executing(typval_T *argvars, typval_T *rettv);
176static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_rename(typval_T *argvars, typval_T *rettv);
178static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200179#ifdef FEAT_FLOAT
180static void f_round(typval_T *argvars, typval_T *rettv);
181#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100182#ifdef FEAT_RUBY
183static void f_rubyeval(typval_T *argvars, typval_T *rettv);
184#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200185static void f_screenattr(typval_T *argvars, typval_T *rettv);
186static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100187static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200188static void f_screencol(typval_T *argvars, typval_T *rettv);
189static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100190static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200191static void f_search(typval_T *argvars, typval_T *rettv);
192static void f_searchdecl(typval_T *argvars, typval_T *rettv);
193static void f_searchpair(typval_T *argvars, typval_T *rettv);
194static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
195static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100196static void f_setcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100198static void f_setcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200199static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100203static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200204#ifdef FEAT_CRYPT
205static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200206#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200207static void f_shellescape(typval_T *argvars, typval_T *rettv);
208static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200209#ifdef FEAT_FLOAT
210static void f_sin(typval_T *argvars, typval_T *rettv);
211static void f_sinh(typval_T *argvars, typval_T *rettv);
212#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200213static void f_soundfold(typval_T *argvars, typval_T *rettv);
214static void f_spellbadword(typval_T *argvars, typval_T *rettv);
215static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
216static void f_split(typval_T *argvars, typval_T *rettv);
217#ifdef FEAT_FLOAT
218static void f_sqrt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100219#endif
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100220static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100221#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200222static void f_str2float(typval_T *argvars, typval_T *rettv);
223#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200224static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200225static void f_str2nr(typval_T *argvars, typval_T *rettv);
226static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200227static void f_strgetchar(typval_T *argvars, typval_T *rettv);
228static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200229static void f_strlen(typval_T *argvars, typval_T *rettv);
230static void f_strcharpart(typval_T *argvars, typval_T *rettv);
231static void f_strpart(typval_T *argvars, typval_T *rettv);
232static void f_strridx(typval_T *argvars, typval_T *rettv);
233static void f_strtrans(typval_T *argvars, typval_T *rettv);
234static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
235static void f_strwidth(typval_T *argvars, typval_T *rettv);
236static void f_submatch(typval_T *argvars, typval_T *rettv);
237static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200238static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200239static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200240static void f_synID(typval_T *argvars, typval_T *rettv);
241static void f_synIDattr(typval_T *argvars, typval_T *rettv);
242static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
243static void f_synstack(typval_T *argvars, typval_T *rettv);
244static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200246static void f_taglist(typval_T *argvars, typval_T *rettv);
247static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200248#ifdef FEAT_FLOAT
249static void f_tan(typval_T *argvars, typval_T *rettv);
250static void f_tanh(typval_T *argvars, typval_T *rettv);
251#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200252static void f_tolower(typval_T *argvars, typval_T *rettv);
253static void f_toupper(typval_T *argvars, typval_T *rettv);
254static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100255static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200256#ifdef FEAT_FLOAT
257static void f_trunc(typval_T *argvars, typval_T *rettv);
258#endif
259static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200260static void f_virtcol(typval_T *argvars, typval_T *rettv);
261static void f_visualmode(typval_T *argvars, typval_T *rettv);
262static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100263static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200264static void f_wordcount(typval_T *argvars, typval_T *rettv);
265static void f_xor(typval_T *argvars, typval_T *rettv);
266
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100267
Bram Moolenaar94738d82020-10-21 14:25:07 +0200268/*
269 * Functions that check the argument type of a builtin function.
270 * Each function returns FAIL and gives an error message if the type is wrong.
271 */
272
273// Context passed to an arg_ function.
274typedef struct {
Bram Moolenaarca174532020-10-21 16:42:22 +0200275 int arg_count; // actual argument count
276 type_T **arg_types; // list of argument types
277 int arg_idx; // current argument index (first arg is zero)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100278 cctx_T *arg_cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +0200279} argcontext_T;
280
281// A function to check one argument type. The first argument is the type to
282// check. If needed, other argument types can be obtained with the context.
283// E.g. if "arg_idx" is 1, then (type - 1) is the first argument type.
284typedef int (*argcheck_T)(type_T *, argcontext_T *);
285
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100286/*
Bram Moolenaar351ead02021-01-16 16:07:01 +0100287 * Call need_type() to check an argument type.
288 */
289 static int
290check_arg_type(
291 type_T *expected,
292 type_T *actual,
293 argcontext_T *context)
294{
295 // TODO: would be useful to know if "actual" is a constant and pass it to
296 // need_type() to get a compile time error if possible.
297 return need_type(actual, expected,
298 context->arg_idx - context->arg_count, context->arg_idx + 1,
299 context->arg_cctx, FALSE, FALSE);
300}
301
302/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100303 * Check "type" is a float or a number.
304 */
Bram Moolenaar94738d82020-10-21 14:25:07 +0200305 static int
306arg_float_or_nr(type_T *type, argcontext_T *context)
307{
Bram Moolenaarca174532020-10-21 16:42:22 +0200308 if (type->tt_type == VAR_ANY
309 || type->tt_type == VAR_FLOAT || type->tt_type == VAR_NUMBER)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200310 return OK;
311 arg_type_mismatch(&t_number, type, context->arg_idx + 1);
312 return FAIL;
313}
314
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100315/*
316 * Check "type" is a number.
317 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200318 static int
319arg_number(type_T *type, argcontext_T *context)
320{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100321 return check_arg_type(&t_number, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200322}
323
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100324/*
325 * Check "type" is a string.
326 */
327 static int
328arg_string(type_T *type, argcontext_T *context)
329{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100330 return check_arg_type(&t_string, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100331}
332
333/*
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100334 * Check "type" is a bool or number 0 or 1.
335 */
336 static int
337arg_bool(type_T *type, argcontext_T *context)
338{
339 if (type->tt_type == VAR_ANY
340 || type->tt_type == VAR_NUMBER || type->tt_type == VAR_BOOL)
341 return OK;
342 return check_arg_type(&t_bool, type, context);
343}
344
345/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100346 * Check "type" is a list or a blob.
347 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200348 static int
349arg_list_or_blob(type_T *type, argcontext_T *context)
350{
351 if (type->tt_type == VAR_ANY
352 || type->tt_type == VAR_LIST || type->tt_type == VAR_BLOB)
353 return OK;
354 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
355 return FAIL;
356}
357
358/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100359 * Check "type" is a list or a dict.
360 */
361 static int
362arg_list_or_dict(type_T *type, argcontext_T *context)
363{
364 if (type->tt_type == VAR_ANY
365 || type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
366 return OK;
367 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
368 return FAIL;
369}
370
371/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100372 * Check "type" is the same type as the previous argument.
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100373 * Must not be used for the first argcheck_T entry.
374 */
375 static int
376arg_same_as_prev(type_T *type, argcontext_T *context)
377{
378 type_T *prev_type = context->arg_types[context->arg_idx - 1];
379
Bram Moolenaar351ead02021-01-16 16:07:01 +0100380 return check_arg_type(prev_type, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100381}
382
383/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100384 * Check "type" is the same basic type as the previous argument, checks list or
385 * dict vs other type, but not member type.
386 * Must not be used for the first argcheck_T entry.
387 */
388 static int
389arg_same_struct_as_prev(type_T *type, argcontext_T *context)
390{
391 type_T *prev_type = context->arg_types[context->arg_idx - 1];
392
393 if (prev_type->tt_type != context->arg_types[context->arg_idx]->tt_type)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100394 return check_arg_type(prev_type, type, context);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100395 return OK;
396}
397
398/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100399 * Check "type" is an item of the list or blob of the previous arg.
Bram Moolenaarca174532020-10-21 16:42:22 +0200400 * Must not be used for the first argcheck_T entry.
401 */
402 static int
403arg_item_of_prev(type_T *type, argcontext_T *context)
404{
405 type_T *prev_type = context->arg_types[context->arg_idx - 1];
406 type_T *expected;
407
408 if (prev_type->tt_type == VAR_LIST)
409 expected = prev_type->tt_member;
410 else if (prev_type->tt_type == VAR_BLOB)
411 expected = &t_number;
412 else
413 // probably VAR_ANY, can't check
414 return OK;
415
Bram Moolenaar351ead02021-01-16 16:07:01 +0100416 return check_arg_type(expected, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200417}
418
Bram Moolenaar94738d82020-10-21 14:25:07 +0200419/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100420 * Check "type" which is the third argument of extend().
421 */
422 static int
423arg_extend3(type_T *type, argcontext_T *context)
424{
425 type_T *first_type = context->arg_types[context->arg_idx - 2];
426
427 if (first_type->tt_type == VAR_LIST)
428 return arg_number(type, context);
429 if (first_type->tt_type == VAR_DICT)
430 return arg_string(type, context);
431 return OK;
432}
433
434
435/*
Bram Moolenaar94738d82020-10-21 14:25:07 +0200436 * Lists of functions that check the argument types of a builtin function.
437 */
Bram Moolenaar80ad3e22021-01-31 20:48:58 +0100438argcheck_T arg1_string[] = {arg_string};
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100439argcheck_T arg3_string_nr_bool[] = {arg_string, arg_number, arg_bool};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200440argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100441argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev};
442argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3};
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100443argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
Bram Moolenaarca174532020-10-21 16:42:22 +0200444argcheck_T arg3_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200445
446/*
447 * Functions that return the return type of a builtin function.
Bram Moolenaara1224cb2020-10-22 12:31:49 +0200448 * Note that "argtypes" is NULL if "argcount" is zero.
Bram Moolenaar94738d82020-10-21 14:25:07 +0200449 */
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100450 static type_T *
451ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
452{
453 return &t_void;
454}
455 static type_T *
456ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
457{
458 return &t_any;
459}
460 static type_T *
Bram Moolenaar403dc312020-10-17 19:29:51 +0200461ret_bool(int argcount UNUSED, type_T **argtypes UNUSED)
462{
463 return &t_bool;
464}
465 static type_T *
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100466ret_number_bool(int argcount UNUSED, type_T **argtypes UNUSED)
467{
468 return &t_number_bool;
469}
470 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100471ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
472{
473 return &t_number;
474}
475 static type_T *
476ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
477{
478 return &t_float;
479}
480 static type_T *
481ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
482{
483 return &t_string;
484}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200485 static type_T *
486ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100487{
488 return &t_list_any;
489}
490 static type_T *
491ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
492{
493 return &t_list_number;
494}
495 static type_T *
496ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
497{
498 return &t_list_string;
499}
500 static type_T *
501ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
502{
503 return &t_list_dict_any;
504}
505 static type_T *
506ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
507{
508 return &t_dict_any;
509}
510 static type_T *
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100511ret_job_info(int argcount, type_T **argtypes UNUSED)
512{
513 if (argcount == 0)
514 return &t_list_job;
515 return &t_dict_any;
516}
517 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100518ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
519{
520 return &t_dict_number;
521}
522 static type_T *
523ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
524{
525 return &t_dict_string;
526}
527 static type_T *
528ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
529{
530 return &t_blob;
531}
532 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200533ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100534{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200535 return &t_func_any;
536}
537 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100538ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
539{
540 return &t_channel;
541}
542 static type_T *
543ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
544{
545 return &t_job;
546}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200547 static type_T *
548ret_first_arg(int argcount, type_T **argtypes)
549{
550 if (argcount > 0)
551 return argtypes[0];
552 return &t_void;
553}
Bram Moolenaarea696852020-11-09 18:31:39 +0100554// for map(): returns first argument but item type may differ
555 static type_T *
556ret_first_cont(int argcount UNUSED, type_T **argtypes)
557{
558 if (argtypes[0]->tt_type == VAR_LIST)
559 return &t_list_any;
560 if (argtypes[0]->tt_type == VAR_DICT)
561 return &t_dict_any;
562 if (argtypes[0]->tt_type == VAR_BLOB)
563 return argtypes[0];
564 return &t_any;
565}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200566
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200567/*
568 * Used for getqflist(): returns list if there is no argument, dict if there is
569 * one.
570 */
571 static type_T *
572ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
573{
574 if (argcount > 0)
575 return &t_dict_any;
576 return &t_list_dict_any;
577}
578
579/*
580 * Used for getloclist(): returns list if there is one argument, dict if there
581 * are two.
582 */
583 static type_T *
584ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
585{
586 if (argcount > 1)
587 return &t_dict_any;
588 return &t_list_dict_any;
589}
590
Bram Moolenaar846178a2020-07-05 17:04:13 +0200591 static type_T *
592ret_argv(int argcount, type_T **argtypes UNUSED)
593{
594 // argv() returns list of strings
595 if (argcount == 0)
596 return &t_list_string;
597
598 // argv(0) returns a string, but argv(-1] returns a list
599 return &t_any;
600}
601
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200602 static type_T *
603ret_remove(int argcount UNUSED, type_T **argtypes)
604{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200605 if (argtypes != NULL)
606 {
607 if (argtypes[0]->tt_type == VAR_LIST
608 || argtypes[0]->tt_type == VAR_DICT)
609 return argtypes[0]->tt_member;
610 if (argtypes[0]->tt_type == VAR_BLOB)
611 return &t_number;
612 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200613 return &t_any;
614}
615
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200616 static type_T *
617ret_getreg(int argcount, type_T **argtypes UNUSED)
618{
619 // Assume that if the third argument is passed it's non-zero
620 if (argcount == 3)
621 return &t_list_string;
622 return &t_string;
623}
624
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200625 static type_T *
626ret_maparg(int argcount, type_T **argtypes UNUSED)
627{
628 // Assume that if the fourth argument is passed it's non-zero
629 if (argcount == 4)
630 return &t_dict_any;
631 return &t_string;
632}
633
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100634static type_T *ret_f_function(int argcount, type_T **argtypes);
635
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636/*
637 * Array with names and number of arguments of all internal functions
638 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
639 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200640typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200641{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200642 char *f_name; // function name
643 char f_min_argc; // minimal number of arguments
644 char f_max_argc; // maximal number of arguments
645 char f_argtype; // for method: FEARG_ values
Bram Moolenaar94738d82020-10-21 14:25:07 +0200646 argcheck_T *f_argcheck; // list of functions to check argument types
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100647 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
648 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200649 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200650 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200651} funcentry_T;
652
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200653// values for f_argtype; zero means it cannot be used as a method
654#define FEARG_1 1 // base is the first argument
655#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200656#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200657#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200658#define FEARG_LAST 9 // base is the last argument
659
Bram Moolenaar15c47602020-03-26 22:16:48 +0100660#ifdef FEAT_FLOAT
661# define FLOAT_FUNC(name) name
662#else
663# define FLOAT_FUNC(name) NULL
664#endif
665#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
666# define MATH_FUNC(name) name
667#else
668# define MATH_FUNC(name) NULL
669#endif
670#ifdef FEAT_TIMERS
671# define TIMER_FUNC(name) name
672#else
673# define TIMER_FUNC(name) NULL
674#endif
675#ifdef FEAT_JOB_CHANNEL
676# define JOB_FUNC(name) name
677#else
678# define JOB_FUNC(name) NULL
679#endif
680#ifdef FEAT_PROP_POPUP
681# define PROP_FUNC(name) name
682#else
683# define PROP_FUNC(name) NULL
684#endif
685#ifdef FEAT_SIGNS
686# define SIGN_FUNC(name) name
687#else
688# define SIGN_FUNC(name) NULL
689#endif
690#ifdef FEAT_SOUND
691# define SOUND_FUNC(name) name
692#else
693# define SOUND_FUNC(name) NULL
694#endif
695#ifdef FEAT_TERMINAL
696# define TERM_FUNC(name) name
697#else
698# define TERM_FUNC(name) NULL
699#endif
700
Bram Moolenaarac92e252019-08-03 21:58:38 +0200701static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200702{
Bram Moolenaar94738d82020-10-21 14:25:07 +0200703 {"abs", 1, 1, FEARG_1, arg1_float_or_nr,
704 ret_any, FLOAT_FUNC(f_abs)},
705 {"acos", 1, 1, FEARG_1, NULL,
706 ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100707 {"add", 2, 2, FEARG_1, NULL /* arg2_listblob_item */,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200708 ret_first_arg, f_add},
709 {"and", 2, 2, FEARG_1, NULL,
710 ret_number, f_and},
711 {"append", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100712 ret_number_bool, f_append},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200713 {"appendbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100714 ret_number_bool, f_appendbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200715 {"argc", 0, 1, 0, NULL,
716 ret_number, f_argc},
717 {"argidx", 0, 0, 0, NULL,
718 ret_number, f_argidx},
719 {"arglistid", 0, 2, 0, NULL,
720 ret_number, f_arglistid},
721 {"argv", 0, 2, 0, NULL,
722 ret_argv, f_argv},
723 {"asin", 1, 1, FEARG_1, NULL,
724 ret_float, FLOAT_FUNC(f_asin)},
725 {"assert_beeps", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100726 ret_number_bool, f_assert_beeps},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200727 {"assert_equal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100728 ret_number_bool, f_assert_equal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200729 {"assert_equalfile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100730 ret_number_bool, f_assert_equalfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200731 {"assert_exception", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100732 ret_number_bool, f_assert_exception},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200733 {"assert_fails", 1, 5, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100734 ret_number_bool, f_assert_fails},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200735 {"assert_false", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100736 ret_number_bool, f_assert_false},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200737 {"assert_inrange", 3, 4, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100738 ret_number_bool, f_assert_inrange},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200739 {"assert_match", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100740 ret_number_bool, f_assert_match},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200741 {"assert_notequal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100742 ret_number_bool, f_assert_notequal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200743 {"assert_notmatch", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100744 ret_number_bool, f_assert_notmatch},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200745 {"assert_report", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100746 ret_number_bool, f_assert_report},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200747 {"assert_true", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100748 ret_number_bool, f_assert_true},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200749 {"atan", 1, 1, FEARG_1, NULL,
750 ret_float, FLOAT_FUNC(f_atan)},
751 {"atan2", 2, 2, FEARG_1, NULL,
752 ret_float, FLOAT_FUNC(f_atan2)},
753 {"balloon_gettext", 0, 0, 0, NULL,
754 ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100755#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100756 f_balloon_gettext
757#else
758 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100759#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100760 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200761 {"balloon_show", 1, 1, FEARG_1, NULL,
762 ret_void,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100763#ifdef FEAT_BEVAL
764 f_balloon_show
765#else
766 NULL
767#endif
768 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200769 {"balloon_split", 1, 1, FEARG_1, NULL,
770 ret_list_string,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100771#if defined(FEAT_BEVAL_TERM)
772 f_balloon_split
773#else
774 NULL
775#endif
776 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200777 {"browse", 4, 4, 0, NULL,
778 ret_string, f_browse},
779 {"browsedir", 2, 2, 0, NULL,
780 ret_string, f_browsedir},
781 {"bufadd", 1, 1, FEARG_1, NULL,
782 ret_number, f_bufadd},
783 {"bufexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100784 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200785 {"buffer_exists", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100786 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200787 {"buffer_name", 0, 1, FEARG_1, NULL, // obsolete
788 ret_string, f_bufname},
789 {"buffer_number", 0, 1, FEARG_1, NULL, // obsolete
790 ret_number, f_bufnr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200791 {"buflisted", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100792 ret_number_bool, f_buflisted},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200793 {"bufload", 1, 1, FEARG_1, NULL,
794 ret_void, f_bufload},
795 {"bufloaded", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100796 ret_number_bool, f_bufloaded},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200797 {"bufname", 0, 1, FEARG_1, NULL,
798 ret_string, f_bufname},
799 {"bufnr", 0, 2, FEARG_1, NULL,
800 ret_number, f_bufnr},
801 {"bufwinid", 1, 1, FEARG_1, NULL,
802 ret_number, f_bufwinid},
803 {"bufwinnr", 1, 1, FEARG_1, NULL,
804 ret_number, f_bufwinnr},
805 {"byte2line", 1, 1, FEARG_1, NULL,
806 ret_number, f_byte2line},
807 {"byteidx", 2, 2, FEARG_1, NULL,
808 ret_number, f_byteidx},
809 {"byteidxcomp", 2, 2, FEARG_1, NULL,
810 ret_number, f_byteidxcomp},
811 {"call", 2, 3, FEARG_1, NULL,
812 ret_any, f_call},
813 {"ceil", 1, 1, FEARG_1, NULL,
814 ret_float, FLOAT_FUNC(f_ceil)},
815 {"ch_canread", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100816 ret_number_bool, JOB_FUNC(f_ch_canread)},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200817 {"ch_close", 1, 1, FEARG_1, NULL,
818 ret_void, JOB_FUNC(f_ch_close)},
819 {"ch_close_in", 1, 1, FEARG_1, NULL,
820 ret_void, JOB_FUNC(f_ch_close_in)},
821 {"ch_evalexpr", 2, 3, FEARG_1, NULL,
822 ret_any, JOB_FUNC(f_ch_evalexpr)},
823 {"ch_evalraw", 2, 3, FEARG_1, NULL,
824 ret_any, JOB_FUNC(f_ch_evalraw)},
825 {"ch_getbufnr", 2, 2, FEARG_1, NULL,
826 ret_number, JOB_FUNC(f_ch_getbufnr)},
827 {"ch_getjob", 1, 1, FEARG_1, NULL,
828 ret_job, JOB_FUNC(f_ch_getjob)},
829 {"ch_info", 1, 1, FEARG_1, NULL,
830 ret_dict_any, JOB_FUNC(f_ch_info)},
831 {"ch_log", 1, 2, FEARG_1, NULL,
832 ret_void, JOB_FUNC(f_ch_log)},
833 {"ch_logfile", 1, 2, FEARG_1, NULL,
834 ret_void, JOB_FUNC(f_ch_logfile)},
835 {"ch_open", 1, 2, FEARG_1, NULL,
836 ret_channel, JOB_FUNC(f_ch_open)},
837 {"ch_read", 1, 2, FEARG_1, NULL,
838 ret_string, JOB_FUNC(f_ch_read)},
839 {"ch_readblob", 1, 2, FEARG_1, NULL,
840 ret_blob, JOB_FUNC(f_ch_readblob)},
841 {"ch_readraw", 1, 2, FEARG_1, NULL,
842 ret_string, JOB_FUNC(f_ch_readraw)},
843 {"ch_sendexpr", 2, 3, FEARG_1, NULL,
844 ret_void, JOB_FUNC(f_ch_sendexpr)},
845 {"ch_sendraw", 2, 3, FEARG_1, NULL,
846 ret_void, JOB_FUNC(f_ch_sendraw)},
847 {"ch_setoptions", 2, 2, FEARG_1, NULL,
848 ret_void, JOB_FUNC(f_ch_setoptions)},
849 {"ch_status", 1, 2, FEARG_1, NULL,
850 ret_string, JOB_FUNC(f_ch_status)},
851 {"changenr", 0, 0, 0, NULL,
852 ret_number, f_changenr},
853 {"char2nr", 1, 2, FEARG_1, NULL,
854 ret_number, f_char2nr},
855 {"charclass", 1, 1, FEARG_1, NULL,
856 ret_number, f_charclass},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100857 {"charcol", 1, 1, FEARG_1, NULL,
858 ret_number, f_charcol},
Bram Moolenaar17793ef2020-12-28 12:56:58 +0100859 {"charidx", 2, 3, FEARG_1, NULL,
860 ret_number, f_charidx},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200861 {"chdir", 1, 1, FEARG_1, NULL,
862 ret_string, f_chdir},
863 {"cindent", 1, 1, FEARG_1, NULL,
864 ret_number, f_cindent},
865 {"clearmatches", 0, 1, FEARG_1, NULL,
866 ret_void, f_clearmatches},
867 {"col", 1, 1, FEARG_1, NULL,
868 ret_number, f_col},
869 {"complete", 2, 2, FEARG_2, NULL,
870 ret_void, f_complete},
871 {"complete_add", 1, 1, FEARG_1, NULL,
872 ret_number, f_complete_add},
873 {"complete_check", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100874 ret_number_bool, f_complete_check},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200875 {"complete_info", 0, 1, FEARG_1, NULL,
876 ret_dict_any, f_complete_info},
877 {"confirm", 1, 4, FEARG_1, NULL,
878 ret_number, f_confirm},
879 {"copy", 1, 1, FEARG_1, NULL,
880 ret_first_arg, f_copy},
881 {"cos", 1, 1, FEARG_1, NULL,
882 ret_float, FLOAT_FUNC(f_cos)},
883 {"cosh", 1, 1, FEARG_1, NULL,
884 ret_float, FLOAT_FUNC(f_cosh)},
885 {"count", 2, 4, FEARG_1, NULL,
886 ret_number, f_count},
887 {"cscope_connection",0,3, 0, NULL,
888 ret_number, f_cscope_connection},
889 {"cursor", 1, 3, FEARG_1, NULL,
890 ret_number, f_cursor},
891 {"debugbreak", 1, 1, FEARG_1, NULL,
892 ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100893#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100894 f_debugbreak
895#else
896 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200897#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100898 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200899 {"deepcopy", 1, 2, FEARG_1, NULL,
900 ret_first_arg, f_deepcopy},
901 {"delete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100902 ret_number_bool, f_delete},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200903 {"deletebufline", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100904 ret_number_bool, f_deletebufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200905 {"did_filetype", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100906 ret_number_bool, f_did_filetype},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200907 {"diff_filler", 1, 1, FEARG_1, NULL,
908 ret_number, f_diff_filler},
909 {"diff_hlID", 2, 2, FEARG_1, NULL,
910 ret_number, f_diff_hlID},
911 {"echoraw", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100912 ret_void, f_echoraw},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200913 {"empty", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100914 ret_number_bool, f_empty},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200915 {"environ", 0, 0, 0, NULL,
916 ret_dict_string, f_environ},
917 {"escape", 2, 2, FEARG_1, NULL,
918 ret_string, f_escape},
919 {"eval", 1, 1, FEARG_1, NULL,
920 ret_any, f_eval},
921 {"eventhandler", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100922 ret_number_bool, f_eventhandler},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200923 {"executable", 1, 1, FEARG_1, NULL,
924 ret_number, f_executable},
925 {"execute", 1, 2, FEARG_1, NULL,
926 ret_string, f_execute},
927 {"exepath", 1, 1, FEARG_1, NULL,
928 ret_string, f_exepath},
929 {"exists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100930 ret_number_bool, f_exists},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200931 {"exp", 1, 1, FEARG_1, NULL,
932 ret_float, FLOAT_FUNC(f_exp)},
933 {"expand", 1, 3, FEARG_1, NULL,
934 ret_any, f_expand},
935 {"expandcmd", 1, 1, FEARG_1, NULL,
936 ret_string, f_expandcmd},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100937 {"extend", 2, 3, FEARG_1, arg23_extend,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200938 ret_first_arg, f_extend},
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100939 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
940 ret_first_cont, f_extendnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200941 {"feedkeys", 1, 2, FEARG_1, NULL,
942 ret_void, f_feedkeys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200943 {"file_readable", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100944 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200945 {"filereadable", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100946 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200947 {"filewritable", 1, 1, FEARG_1, NULL,
948 ret_number, f_filewritable},
949 {"filter", 2, 2, FEARG_1, NULL,
950 ret_first_arg, f_filter},
951 {"finddir", 1, 3, FEARG_1, NULL,
952 ret_string, f_finddir},
953 {"findfile", 1, 3, FEARG_1, NULL,
954 ret_string, f_findfile},
955 {"flatten", 1, 2, FEARG_1, NULL,
956 ret_list_any, f_flatten},
Bram Moolenaar3b690062021-02-01 20:14:51 +0100957 {"flattennew", 1, 2, FEARG_1, NULL,
958 ret_list_any, f_flattennew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200959 {"float2nr", 1, 1, FEARG_1, NULL,
960 ret_number, FLOAT_FUNC(f_float2nr)},
961 {"floor", 1, 1, FEARG_1, NULL,
962 ret_float, FLOAT_FUNC(f_floor)},
963 {"fmod", 2, 2, FEARG_1, NULL,
964 ret_float, FLOAT_FUNC(f_fmod)},
965 {"fnameescape", 1, 1, FEARG_1, NULL,
966 ret_string, f_fnameescape},
967 {"fnamemodify", 2, 2, FEARG_1, NULL,
968 ret_string, f_fnamemodify},
969 {"foldclosed", 1, 1, FEARG_1, NULL,
970 ret_number, f_foldclosed},
971 {"foldclosedend", 1, 1, FEARG_1, NULL,
972 ret_number, f_foldclosedend},
973 {"foldlevel", 1, 1, FEARG_1, NULL,
974 ret_number, f_foldlevel},
975 {"foldtext", 0, 0, 0, NULL,
976 ret_string, f_foldtext},
977 {"foldtextresult", 1, 1, FEARG_1, NULL,
978 ret_string, f_foldtextresult},
979 {"foreground", 0, 0, 0, NULL,
980 ret_void, f_foreground},
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100981 {"fullcommand", 1, 1, FEARG_1, arg1_string,
982 ret_string, f_fullcommand},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200983 {"funcref", 1, 3, FEARG_1, NULL,
984 ret_func_any, f_funcref},
985 {"function", 1, 3, FEARG_1, NULL,
986 ret_f_function, f_function},
987 {"garbagecollect", 0, 1, 0, NULL,
988 ret_void, f_garbagecollect},
989 {"get", 2, 3, FEARG_1, NULL,
990 ret_any, f_get},
991 {"getbufinfo", 0, 1, FEARG_1, NULL,
992 ret_list_dict_any, f_getbufinfo},
993 {"getbufline", 2, 3, FEARG_1, NULL,
994 ret_list_string, f_getbufline},
995 {"getbufvar", 2, 3, FEARG_1, NULL,
996 ret_any, f_getbufvar},
997 {"getchangelist", 0, 1, FEARG_1, NULL,
998 ret_list_any, f_getchangelist},
999 {"getchar", 0, 1, 0, NULL,
1000 ret_number, f_getchar},
1001 {"getcharmod", 0, 0, 0, NULL,
1002 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001003 {"getcharpos", 1, 1, FEARG_1, NULL,
1004 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001005 {"getcharsearch", 0, 0, 0, NULL,
1006 ret_dict_any, f_getcharsearch},
1007 {"getcmdline", 0, 0, 0, NULL,
1008 ret_string, f_getcmdline},
1009 {"getcmdpos", 0, 0, 0, NULL,
1010 ret_number, f_getcmdpos},
1011 {"getcmdtype", 0, 0, 0, NULL,
1012 ret_string, f_getcmdtype},
1013 {"getcmdwintype", 0, 0, 0, NULL,
1014 ret_string, f_getcmdwintype},
1015 {"getcompletion", 2, 3, FEARG_1, NULL,
1016 ret_list_string, f_getcompletion},
1017 {"getcurpos", 0, 1, FEARG_1, NULL,
1018 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001019 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
1020 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001021 {"getcwd", 0, 2, FEARG_1, NULL,
1022 ret_string, f_getcwd},
1023 {"getenv", 1, 1, FEARG_1, NULL,
1024 ret_string, f_getenv},
1025 {"getfontname", 0, 1, 0, NULL,
1026 ret_string, f_getfontname},
1027 {"getfperm", 1, 1, FEARG_1, NULL,
1028 ret_string, f_getfperm},
1029 {"getfsize", 1, 1, FEARG_1, NULL,
1030 ret_number, f_getfsize},
1031 {"getftime", 1, 1, FEARG_1, NULL,
1032 ret_number, f_getftime},
1033 {"getftype", 1, 1, FEARG_1, NULL,
1034 ret_string, f_getftype},
1035 {"getimstatus", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001036 ret_number_bool, f_getimstatus},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001037 {"getjumplist", 0, 2, FEARG_1, NULL,
1038 ret_list_any, f_getjumplist},
1039 {"getline", 1, 2, FEARG_1, NULL,
1040 ret_f_getline, f_getline},
1041 {"getloclist", 1, 2, 0, NULL,
1042 ret_list_or_dict_1, f_getloclist},
1043 {"getmarklist", 0, 1, FEARG_1, NULL,
1044 ret_list_dict_any, f_getmarklist},
1045 {"getmatches", 0, 1, 0, NULL,
1046 ret_list_dict_any, f_getmatches},
1047 {"getmousepos", 0, 0, 0, NULL,
1048 ret_dict_number, f_getmousepos},
1049 {"getpid", 0, 0, 0, NULL,
1050 ret_number, f_getpid},
1051 {"getpos", 1, 1, FEARG_1, NULL,
1052 ret_list_number, f_getpos},
1053 {"getqflist", 0, 1, 0, NULL,
1054 ret_list_or_dict_0, f_getqflist},
1055 {"getreg", 0, 3, FEARG_1, NULL,
1056 ret_getreg, f_getreg},
1057 {"getreginfo", 0, 1, FEARG_1, NULL,
1058 ret_dict_any, f_getreginfo},
1059 {"getregtype", 0, 1, FEARG_1, NULL,
1060 ret_string, f_getregtype},
1061 {"gettabinfo", 0, 1, FEARG_1, NULL,
1062 ret_list_dict_any, f_gettabinfo},
1063 {"gettabvar", 2, 3, FEARG_1, NULL,
1064 ret_any, f_gettabvar},
1065 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1066 ret_any, f_gettabwinvar},
1067 {"gettagstack", 0, 1, FEARG_1, NULL,
1068 ret_dict_any, f_gettagstack},
1069 {"gettext", 1, 1, FEARG_1, NULL,
1070 ret_string, f_gettext},
1071 {"getwininfo", 0, 1, FEARG_1, NULL,
1072 ret_list_dict_any, f_getwininfo},
1073 {"getwinpos", 0, 1, FEARG_1, NULL,
1074 ret_list_number, f_getwinpos},
1075 {"getwinposx", 0, 0, 0, NULL,
1076 ret_number, f_getwinposx},
1077 {"getwinposy", 0, 0, 0, NULL,
1078 ret_number, f_getwinposy},
1079 {"getwinvar", 2, 3, FEARG_1, NULL,
1080 ret_any, f_getwinvar},
1081 {"glob", 1, 4, FEARG_1, NULL,
1082 ret_any, f_glob},
1083 {"glob2regpat", 1, 1, FEARG_1, NULL,
1084 ret_string, f_glob2regpat},
1085 {"globpath", 2, 5, FEARG_2, NULL,
1086 ret_any, f_globpath},
1087 {"has", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001088 ret_number_bool, f_has},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001089 {"has_key", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001090 ret_number_bool, f_has_key},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001091 {"haslocaldir", 0, 2, FEARG_1, NULL,
1092 ret_number, f_haslocaldir},
1093 {"hasmapto", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001094 ret_number_bool, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001095 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1096 ret_number, f_hlID},
1097 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001098 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001099 {"histadd", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001100 ret_number_bool, f_histadd},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001101 {"histdel", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001102 ret_number_bool, f_histdel},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001103 {"histget", 1, 2, FEARG_1, NULL,
1104 ret_string, f_histget},
1105 {"histnr", 1, 1, FEARG_1, NULL,
1106 ret_number, f_histnr},
1107 {"hlID", 1, 1, FEARG_1, NULL,
1108 ret_number, f_hlID},
1109 {"hlexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001110 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001111 {"hostname", 0, 0, 0, NULL,
1112 ret_string, f_hostname},
1113 {"iconv", 3, 3, FEARG_1, NULL,
1114 ret_string, f_iconv},
1115 {"indent", 1, 1, FEARG_1, NULL,
1116 ret_number, f_indent},
1117 {"index", 2, 4, FEARG_1, NULL,
1118 ret_number, f_index},
1119 {"input", 1, 3, FEARG_1, NULL,
1120 ret_string, f_input},
1121 {"inputdialog", 1, 3, FEARG_1, NULL,
1122 ret_string, f_inputdialog},
1123 {"inputlist", 1, 1, FEARG_1, NULL,
1124 ret_number, f_inputlist},
1125 {"inputrestore", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001126 ret_number_bool, f_inputrestore},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001127 {"inputsave", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001128 ret_number_bool, f_inputsave},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001129 {"inputsecret", 1, 2, FEARG_1, NULL,
1130 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001131 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001132 ret_first_arg, f_insert},
1133 {"interrupt", 0, 0, 0, NULL,
1134 ret_void, f_interrupt},
1135 {"invert", 1, 1, FEARG_1, NULL,
1136 ret_number, f_invert},
1137 {"isdirectory", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001138 ret_number_bool, f_isdirectory},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001139 {"isinf", 1, 1, FEARG_1, NULL,
1140 ret_number, MATH_FUNC(f_isinf)},
1141 {"islocked", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001142 ret_number_bool, f_islocked},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001143 {"isnan", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001144 ret_number_bool, MATH_FUNC(f_isnan)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001145 {"items", 1, 1, FEARG_1, NULL,
1146 ret_list_any, f_items},
1147 {"job_getchannel", 1, 1, FEARG_1, NULL,
1148 ret_channel, JOB_FUNC(f_job_getchannel)},
1149 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001150 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001151 {"job_setoptions", 2, 2, FEARG_1, NULL,
1152 ret_void, JOB_FUNC(f_job_setoptions)},
1153 {"job_start", 1, 2, FEARG_1, NULL,
1154 ret_job, JOB_FUNC(f_job_start)},
1155 {"job_status", 1, 1, FEARG_1, NULL,
1156 ret_string, JOB_FUNC(f_job_status)},
1157 {"job_stop", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001158 ret_number_bool, JOB_FUNC(f_job_stop)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001159 {"join", 1, 2, FEARG_1, NULL,
1160 ret_string, f_join},
1161 {"js_decode", 1, 1, FEARG_1, NULL,
1162 ret_any, f_js_decode},
1163 {"js_encode", 1, 1, FEARG_1, NULL,
1164 ret_string, f_js_encode},
1165 {"json_decode", 1, 1, FEARG_1, NULL,
1166 ret_any, f_json_decode},
1167 {"json_encode", 1, 1, FEARG_1, NULL,
1168 ret_string, f_json_encode},
1169 {"keys", 1, 1, FEARG_1, NULL,
1170 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001171 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1172 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001173 {"len", 1, 1, FEARG_1, NULL,
1174 ret_number, f_len},
1175 {"libcall", 3, 3, FEARG_3, NULL,
1176 ret_string, f_libcall},
1177 {"libcallnr", 3, 3, FEARG_3, NULL,
1178 ret_number, f_libcallnr},
1179 {"line", 1, 2, FEARG_1, NULL,
1180 ret_number, f_line},
1181 {"line2byte", 1, 1, FEARG_1, NULL,
1182 ret_number, f_line2byte},
1183 {"lispindent", 1, 1, FEARG_1, NULL,
1184 ret_number, f_lispindent},
1185 {"list2str", 1, 2, FEARG_1, NULL,
1186 ret_string, f_list2str},
1187 {"listener_add", 1, 2, FEARG_2, NULL,
1188 ret_number, f_listener_add},
1189 {"listener_flush", 0, 1, FEARG_1, NULL,
1190 ret_void, f_listener_flush},
1191 {"listener_remove", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001192 ret_number_bool, f_listener_remove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001193 {"localtime", 0, 0, 0, NULL,
1194 ret_number, f_localtime},
1195 {"log", 1, 1, FEARG_1, NULL,
1196 ret_float, FLOAT_FUNC(f_log)},
1197 {"log10", 1, 1, FEARG_1, NULL,
1198 ret_float, FLOAT_FUNC(f_log10)},
1199 {"luaeval", 1, 2, FEARG_1, NULL,
1200 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001201#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001202 f_luaeval
1203#else
1204 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001205#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001206 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001207 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001208 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001209 {"maparg", 1, 4, FEARG_1, NULL,
1210 ret_maparg, f_maparg},
1211 {"mapcheck", 1, 3, FEARG_1, NULL,
1212 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001213 {"mapnew", 2, 2, FEARG_1, NULL,
1214 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001215 {"mapset", 3, 3, FEARG_1, NULL,
1216 ret_void, f_mapset},
1217 {"match", 2, 4, FEARG_1, NULL,
1218 ret_any, f_match},
1219 {"matchadd", 2, 5, FEARG_1, NULL,
1220 ret_number, f_matchadd},
1221 {"matchaddpos", 2, 5, FEARG_1, NULL,
1222 ret_number, f_matchaddpos},
1223 {"matcharg", 1, 1, FEARG_1, NULL,
1224 ret_list_string, f_matcharg},
1225 {"matchdelete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001226 ret_number_bool, f_matchdelete},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001227 {"matchend", 2, 4, FEARG_1, NULL,
1228 ret_number, f_matchend},
1229 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1230 ret_list_string, f_matchfuzzy},
1231 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1232 ret_list_any, f_matchfuzzypos},
1233 {"matchlist", 2, 4, FEARG_1, NULL,
1234 ret_list_string, f_matchlist},
1235 {"matchstr", 2, 4, FEARG_1, NULL,
1236 ret_string, f_matchstr},
1237 {"matchstrpos", 2, 4, FEARG_1, NULL,
1238 ret_list_any, f_matchstrpos},
1239 {"max", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001240 ret_number, f_max},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001241 {"menu_info", 1, 2, FEARG_1, NULL,
1242 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001243#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001244 f_menu_info
1245#else
1246 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001247#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001248 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001249 {"min", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001250 ret_number, f_min},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001251 {"mkdir", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001252 ret_number_bool, f_mkdir},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001253 {"mode", 0, 1, FEARG_1, NULL,
1254 ret_string, f_mode},
1255 {"mzeval", 1, 1, FEARG_1, NULL,
1256 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001257#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001258 f_mzeval
1259#else
1260 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001261#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001262 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001263 {"nextnonblank", 1, 1, FEARG_1, NULL,
1264 ret_number, f_nextnonblank},
1265 {"nr2char", 1, 2, FEARG_1, NULL,
1266 ret_string, f_nr2char},
1267 {"or", 2, 2, FEARG_1, NULL,
1268 ret_number, f_or},
1269 {"pathshorten", 1, 2, FEARG_1, NULL,
1270 ret_string, f_pathshorten},
1271 {"perleval", 1, 1, FEARG_1, NULL,
1272 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001273#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001274 f_perleval
1275#else
1276 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001277#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001278 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001279 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1280 ret_number, PROP_FUNC(f_popup_atcursor)},
1281 {"popup_beval", 2, 2, FEARG_1, NULL,
1282 ret_number, PROP_FUNC(f_popup_beval)},
1283 {"popup_clear", 0, 1, 0, NULL,
1284 ret_void, PROP_FUNC(f_popup_clear)},
1285 {"popup_close", 1, 2, FEARG_1, NULL,
1286 ret_void, PROP_FUNC(f_popup_close)},
1287 {"popup_create", 2, 2, FEARG_1, NULL,
1288 ret_number, PROP_FUNC(f_popup_create)},
1289 {"popup_dialog", 2, 2, FEARG_1, NULL,
1290 ret_number, PROP_FUNC(f_popup_dialog)},
1291 {"popup_filter_menu", 2, 2, 0, NULL,
1292 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1293 {"popup_filter_yesno", 2, 2, 0, NULL,
1294 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1295 {"popup_findinfo", 0, 0, 0, NULL,
1296 ret_number, PROP_FUNC(f_popup_findinfo)},
1297 {"popup_findpreview", 0, 0, 0, NULL,
1298 ret_number, PROP_FUNC(f_popup_findpreview)},
1299 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1300 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1301 {"popup_getpos", 1, 1, FEARG_1, NULL,
1302 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1303 {"popup_hide", 1, 1, FEARG_1, NULL,
1304 ret_void, PROP_FUNC(f_popup_hide)},
1305 {"popup_list", 0, 0, 0, NULL,
1306 ret_list_number, PROP_FUNC(f_popup_list)},
1307 {"popup_locate", 2, 2, 0, NULL,
1308 ret_number, PROP_FUNC(f_popup_locate)},
1309 {"popup_menu", 2, 2, FEARG_1, NULL,
1310 ret_number, PROP_FUNC(f_popup_menu)},
1311 {"popup_move", 2, 2, FEARG_1, NULL,
1312 ret_void, PROP_FUNC(f_popup_move)},
1313 {"popup_notification", 2, 2, FEARG_1, NULL,
1314 ret_number, PROP_FUNC(f_popup_notification)},
1315 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1316 ret_void, PROP_FUNC(f_popup_setoptions)},
1317 {"popup_settext", 2, 2, FEARG_1, NULL,
1318 ret_void, PROP_FUNC(f_popup_settext)},
1319 {"popup_show", 1, 1, FEARG_1, NULL,
1320 ret_void, PROP_FUNC(f_popup_show)},
1321 {"pow", 2, 2, FEARG_1, NULL,
1322 ret_float, FLOAT_FUNC(f_pow)},
1323 {"prevnonblank", 1, 1, FEARG_1, NULL,
1324 ret_number, f_prevnonblank},
1325 {"printf", 1, 19, FEARG_2, NULL,
1326 ret_string, f_printf},
1327 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1328 ret_string, JOB_FUNC(f_prompt_getprompt)},
1329 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1330 ret_void, JOB_FUNC(f_prompt_setcallback)},
1331 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1332 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1333 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1334 ret_void, JOB_FUNC(f_prompt_setprompt)},
1335 {"prop_add", 3, 3, FEARG_1, NULL,
1336 ret_void, PROP_FUNC(f_prop_add)},
1337 {"prop_clear", 1, 3, FEARG_1, NULL,
1338 ret_void, PROP_FUNC(f_prop_clear)},
1339 {"prop_find", 1, 2, FEARG_1, NULL,
1340 ret_dict_any, PROP_FUNC(f_prop_find)},
1341 {"prop_list", 1, 2, FEARG_1, NULL,
1342 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1343 {"prop_remove", 1, 3, FEARG_1, NULL,
1344 ret_number, PROP_FUNC(f_prop_remove)},
1345 {"prop_type_add", 2, 2, FEARG_1, NULL,
1346 ret_void, PROP_FUNC(f_prop_type_add)},
1347 {"prop_type_change", 2, 2, FEARG_1, NULL,
1348 ret_void, PROP_FUNC(f_prop_type_change)},
1349 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1350 ret_void, PROP_FUNC(f_prop_type_delete)},
1351 {"prop_type_get", 1, 2, FEARG_1, NULL,
1352 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1353 {"prop_type_list", 0, 1, FEARG_1, NULL,
1354 ret_list_string, PROP_FUNC(f_prop_type_list)},
1355 {"pum_getpos", 0, 0, 0, NULL,
1356 ret_dict_number, f_pum_getpos},
1357 {"pumvisible", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001358 ret_number_bool, f_pumvisible},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001359 {"py3eval", 1, 1, FEARG_1, NULL,
1360 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001361#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001362 f_py3eval
1363#else
1364 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001365#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001366 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001367 {"pyeval", 1, 1, FEARG_1, NULL,
1368 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001369#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001370 f_pyeval
1371#else
1372 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001373#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001374 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001375 {"pyxeval", 1, 1, FEARG_1, NULL,
1376 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001377#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001378 f_pyxeval
1379#else
1380 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001381#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001382 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001383 {"rand", 0, 1, FEARG_1, NULL,
1384 ret_number, f_rand},
1385 {"range", 1, 3, FEARG_1, NULL,
1386 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001387 {"readblob", 1, 1, FEARG_1, NULL,
1388 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001389 {"readdir", 1, 3, FEARG_1, NULL,
1390 ret_list_string, f_readdir},
1391 {"readdirex", 1, 3, FEARG_1, NULL,
1392 ret_list_dict_any, f_readdirex},
1393 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001394 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001395 {"reduce", 2, 3, FEARG_1, NULL,
1396 ret_any, f_reduce},
1397 {"reg_executing", 0, 0, 0, NULL,
1398 ret_string, f_reg_executing},
1399 {"reg_recording", 0, 0, 0, NULL,
1400 ret_string, f_reg_recording},
1401 {"reltime", 0, 2, FEARG_1, NULL,
1402 ret_list_any, f_reltime},
1403 {"reltimefloat", 1, 1, FEARG_1, NULL,
1404 ret_float, FLOAT_FUNC(f_reltimefloat)},
1405 {"reltimestr", 1, 1, FEARG_1, NULL,
1406 ret_string, f_reltimestr},
1407 {"remote_expr", 2, 4, FEARG_1, NULL,
1408 ret_string, f_remote_expr},
1409 {"remote_foreground", 1, 1, FEARG_1, NULL,
1410 ret_string, f_remote_foreground},
1411 {"remote_peek", 1, 2, FEARG_1, NULL,
1412 ret_number, f_remote_peek},
1413 {"remote_read", 1, 2, FEARG_1, NULL,
1414 ret_string, f_remote_read},
1415 {"remote_send", 2, 3, FEARG_1, NULL,
1416 ret_string, f_remote_send},
1417 {"remote_startserver", 1, 1, FEARG_1, NULL,
1418 ret_void, f_remote_startserver},
1419 {"remove", 2, 3, FEARG_1, NULL,
1420 ret_remove, f_remove},
1421 {"rename", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001422 ret_number_bool, f_rename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001423 {"repeat", 2, 2, FEARG_1, NULL,
1424 ret_first_arg, f_repeat},
1425 {"resolve", 1, 1, FEARG_1, NULL,
1426 ret_string, f_resolve},
1427 {"reverse", 1, 1, FEARG_1, NULL,
1428 ret_first_arg, f_reverse},
1429 {"round", 1, 1, FEARG_1, NULL,
1430 ret_float, FLOAT_FUNC(f_round)},
1431 {"rubyeval", 1, 1, FEARG_1, NULL,
1432 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001433#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001434 f_rubyeval
1435#else
1436 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001437#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001438 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001439 {"screenattr", 2, 2, FEARG_1, NULL,
1440 ret_number, f_screenattr},
1441 {"screenchar", 2, 2, FEARG_1, NULL,
1442 ret_number, f_screenchar},
1443 {"screenchars", 2, 2, FEARG_1, NULL,
1444 ret_list_number, f_screenchars},
1445 {"screencol", 0, 0, 0, NULL,
1446 ret_number, f_screencol},
1447 {"screenpos", 3, 3, FEARG_1, NULL,
1448 ret_dict_number, f_screenpos},
1449 {"screenrow", 0, 0, 0, NULL,
1450 ret_number, f_screenrow},
1451 {"screenstring", 2, 2, FEARG_1, NULL,
1452 ret_string, f_screenstring},
1453 {"search", 1, 5, FEARG_1, NULL,
1454 ret_number, f_search},
1455 {"searchcount", 0, 1, FEARG_1, NULL,
1456 ret_dict_any, f_searchcount},
1457 {"searchdecl", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001458 ret_number_bool, f_searchdecl},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001459 {"searchpair", 3, 7, 0, NULL,
1460 ret_number, f_searchpair},
1461 {"searchpairpos", 3, 7, 0, NULL,
1462 ret_list_number, f_searchpairpos},
1463 {"searchpos", 1, 5, FEARG_1, NULL,
1464 ret_list_number, f_searchpos},
1465 {"server2client", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001466 ret_number_bool, f_server2client},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001467 {"serverlist", 0, 0, 0, NULL,
1468 ret_string, f_serverlist},
1469 {"setbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001470 ret_number_bool, f_setbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001471 {"setbufvar", 3, 3, FEARG_3, NULL,
1472 ret_void, f_setbufvar},
1473 {"setcellwidths", 1, 1, FEARG_1, NULL,
1474 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001475 {"setcharpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001476 ret_number_bool, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001477 {"setcharsearch", 1, 1, FEARG_1, NULL,
1478 ret_void, f_setcharsearch},
1479 {"setcmdpos", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001480 ret_number_bool, f_setcmdpos},
1481 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1482 ret_number_bool, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001483 {"setenv", 2, 2, FEARG_2, NULL,
1484 ret_void, f_setenv},
1485 {"setfperm", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001486 ret_number_bool, f_setfperm},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001487 {"setline", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001488 ret_number_bool, f_setline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001489 {"setloclist", 2, 4, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001490 ret_number_bool, f_setloclist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001491 {"setmatches", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001492 ret_number_bool, f_setmatches},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001493 {"setpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001494 ret_number_bool, f_setpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001495 {"setqflist", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001496 ret_number_bool, f_setqflist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001497 {"setreg", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001498 ret_number_bool, f_setreg},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001499 {"settabvar", 3, 3, FEARG_3, NULL,
1500 ret_void, f_settabvar},
1501 {"settabwinvar", 4, 4, FEARG_4, NULL,
1502 ret_void, f_settabwinvar},
1503 {"settagstack", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001504 ret_number_bool, f_settagstack},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001505 {"setwinvar", 3, 3, FEARG_3, NULL,
1506 ret_void, f_setwinvar},
1507 {"sha256", 1, 1, FEARG_1, NULL,
1508 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001509#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001510 f_sha256
1511#else
1512 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001513#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001514 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001515 {"shellescape", 1, 2, FEARG_1, NULL,
1516 ret_string, f_shellescape},
1517 {"shiftwidth", 0, 1, FEARG_1, NULL,
1518 ret_number, f_shiftwidth},
1519 {"sign_define", 1, 2, FEARG_1, NULL,
1520 ret_any, SIGN_FUNC(f_sign_define)},
1521 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1522 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1523 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1524 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1525 {"sign_jump", 3, 3, FEARG_1, NULL,
1526 ret_number, SIGN_FUNC(f_sign_jump)},
1527 {"sign_place", 4, 5, FEARG_1, NULL,
1528 ret_number, SIGN_FUNC(f_sign_place)},
1529 {"sign_placelist", 1, 1, FEARG_1, NULL,
1530 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1531 {"sign_undefine", 0, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001532 ret_number_bool, SIGN_FUNC(f_sign_undefine)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001533 {"sign_unplace", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001534 ret_number_bool, SIGN_FUNC(f_sign_unplace)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001535 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1536 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1537 {"simplify", 1, 1, FEARG_1, NULL,
1538 ret_string, f_simplify},
1539 {"sin", 1, 1, FEARG_1, NULL,
1540 ret_float, FLOAT_FUNC(f_sin)},
1541 {"sinh", 1, 1, FEARG_1, NULL,
1542 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001543 {"slice", 2, 3, FEARG_1, NULL,
1544 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001545 {"sort", 1, 3, FEARG_1, NULL,
1546 ret_first_arg, f_sort},
1547 {"sound_clear", 0, 0, 0, NULL,
1548 ret_void, SOUND_FUNC(f_sound_clear)},
1549 {"sound_playevent", 1, 2, FEARG_1, NULL,
1550 ret_number, SOUND_FUNC(f_sound_playevent)},
1551 {"sound_playfile", 1, 2, FEARG_1, NULL,
1552 ret_number, SOUND_FUNC(f_sound_playfile)},
1553 {"sound_stop", 1, 1, FEARG_1, NULL,
1554 ret_void, SOUND_FUNC(f_sound_stop)},
1555 {"soundfold", 1, 1, FEARG_1, NULL,
1556 ret_string, f_soundfold},
1557 {"spellbadword", 0, 1, FEARG_1, NULL,
1558 ret_list_string, f_spellbadword},
1559 {"spellsuggest", 1, 3, FEARG_1, NULL,
1560 ret_list_string, f_spellsuggest},
1561 {"split", 1, 3, FEARG_1, NULL,
1562 ret_list_string, f_split},
1563 {"sqrt", 1, 1, FEARG_1, NULL,
1564 ret_float, FLOAT_FUNC(f_sqrt)},
1565 {"srand", 0, 1, FEARG_1, NULL,
1566 ret_list_number, f_srand},
1567 {"state", 0, 1, FEARG_1, NULL,
1568 ret_string, f_state},
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001569 {"str2float", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001570 ret_float, FLOAT_FUNC(f_str2float)},
1571 {"str2list", 1, 2, FEARG_1, NULL,
1572 ret_list_number, f_str2list},
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001573 {"str2nr", 1, 3, FEARG_1, arg3_string_nr_bool,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001574 ret_number, f_str2nr},
1575 {"strcharpart", 2, 3, FEARG_1, NULL,
1576 ret_string, f_strcharpart},
1577 {"strchars", 1, 2, FEARG_1, NULL,
1578 ret_number, f_strchars},
1579 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1580 ret_number, f_strdisplaywidth},
1581 {"strftime", 1, 2, FEARG_1, NULL,
1582 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001583#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001584 f_strftime
1585#else
1586 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001587#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001588 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001589 {"strgetchar", 2, 2, FEARG_1, NULL,
1590 ret_number, f_strgetchar},
1591 {"stridx", 2, 3, FEARG_1, NULL,
1592 ret_number, f_stridx},
1593 {"string", 1, 1, FEARG_1, NULL,
1594 ret_string, f_string},
1595 {"strlen", 1, 1, FEARG_1, NULL,
1596 ret_number, f_strlen},
1597 {"strpart", 2, 4, FEARG_1, NULL,
1598 ret_string, f_strpart},
1599 {"strptime", 2, 2, FEARG_1, NULL,
1600 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001601#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001602 f_strptime
1603#else
1604 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001605#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001606 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001607 {"strridx", 2, 3, FEARG_1, NULL,
1608 ret_number, f_strridx},
1609 {"strtrans", 1, 1, FEARG_1, NULL,
1610 ret_string, f_strtrans},
1611 {"strwidth", 1, 1, FEARG_1, NULL,
1612 ret_number, f_strwidth},
1613 {"submatch", 1, 2, FEARG_1, NULL,
1614 ret_string, f_submatch},
1615 {"substitute", 4, 4, FEARG_1, NULL,
1616 ret_string, f_substitute},
1617 {"swapinfo", 1, 1, FEARG_1, NULL,
1618 ret_dict_any, f_swapinfo},
1619 {"swapname", 1, 1, FEARG_1, NULL,
1620 ret_string, f_swapname},
1621 {"synID", 3, 3, 0, NULL,
1622 ret_number, f_synID},
1623 {"synIDattr", 2, 3, FEARG_1, NULL,
1624 ret_string, f_synIDattr},
1625 {"synIDtrans", 1, 1, FEARG_1, NULL,
1626 ret_number, f_synIDtrans},
1627 {"synconcealed", 2, 2, 0, NULL,
1628 ret_list_any, f_synconcealed},
1629 {"synstack", 2, 2, 0, NULL,
1630 ret_list_number, f_synstack},
1631 {"system", 1, 2, FEARG_1, NULL,
1632 ret_string, f_system},
1633 {"systemlist", 1, 2, FEARG_1, NULL,
1634 ret_list_string, f_systemlist},
1635 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1636 ret_list_number, f_tabpagebuflist},
1637 {"tabpagenr", 0, 1, 0, NULL,
1638 ret_number, f_tabpagenr},
1639 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1640 ret_number, f_tabpagewinnr},
1641 {"tagfiles", 0, 0, 0, NULL,
1642 ret_list_string, f_tagfiles},
1643 {"taglist", 1, 2, FEARG_1, NULL,
1644 ret_list_dict_any, f_taglist},
1645 {"tan", 1, 1, FEARG_1, NULL,
1646 ret_float, FLOAT_FUNC(f_tan)},
1647 {"tanh", 1, 1, FEARG_1, NULL,
1648 ret_float, FLOAT_FUNC(f_tanh)},
1649 {"tempname", 0, 0, 0, NULL,
1650 ret_string, f_tempname},
1651 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1652 ret_number, TERM_FUNC(f_term_dumpdiff)},
1653 {"term_dumpload", 1, 2, FEARG_1, NULL,
1654 ret_number, TERM_FUNC(f_term_dumpload)},
1655 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1656 ret_void, TERM_FUNC(f_term_dumpwrite)},
1657 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1658 ret_number, TERM_FUNC(f_term_getaltscreen)},
1659 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1660 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001661#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001662 f_term_getansicolors
1663#else
1664 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001665#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001666 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001667 {"term_getattr", 2, 2, FEARG_1, NULL,
1668 ret_number, TERM_FUNC(f_term_getattr)},
1669 {"term_getcursor", 1, 1, FEARG_1, NULL,
1670 ret_list_any, TERM_FUNC(f_term_getcursor)},
1671 {"term_getjob", 1, 1, FEARG_1, NULL,
1672 ret_job, TERM_FUNC(f_term_getjob)},
1673 {"term_getline", 2, 2, FEARG_1, NULL,
1674 ret_string, TERM_FUNC(f_term_getline)},
1675 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1676 ret_number, TERM_FUNC(f_term_getscrolled)},
1677 {"term_getsize", 1, 1, FEARG_1, NULL,
1678 ret_list_number, TERM_FUNC(f_term_getsize)},
1679 {"term_getstatus", 1, 1, FEARG_1, NULL,
1680 ret_string, TERM_FUNC(f_term_getstatus)},
1681 {"term_gettitle", 1, 1, FEARG_1, NULL,
1682 ret_string, TERM_FUNC(f_term_gettitle)},
1683 {"term_gettty", 1, 2, FEARG_1, NULL,
1684 ret_string, TERM_FUNC(f_term_gettty)},
1685 {"term_list", 0, 0, 0, NULL,
1686 ret_list_number, TERM_FUNC(f_term_list)},
1687 {"term_scrape", 2, 2, FEARG_1, NULL,
1688 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1689 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1690 ret_void, TERM_FUNC(f_term_sendkeys)},
1691 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1692 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001693#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001694 f_term_setansicolors
1695#else
1696 NULL
1697#endif
1698 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001699 {"term_setapi", 2, 2, FEARG_1, NULL,
1700 ret_void, TERM_FUNC(f_term_setapi)},
1701 {"term_setkill", 2, 2, FEARG_1, NULL,
1702 ret_void, TERM_FUNC(f_term_setkill)},
1703 {"term_setrestore", 2, 2, FEARG_1, NULL,
1704 ret_void, TERM_FUNC(f_term_setrestore)},
1705 {"term_setsize", 3, 3, FEARG_1, NULL,
1706 ret_void, TERM_FUNC(f_term_setsize)},
1707 {"term_start", 1, 2, FEARG_1, NULL,
1708 ret_number, TERM_FUNC(f_term_start)},
1709 {"term_wait", 1, 2, FEARG_1, NULL,
1710 ret_void, TERM_FUNC(f_term_wait)},
1711 {"terminalprops", 0, 0, 0, NULL,
1712 ret_dict_string, f_terminalprops},
1713 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1714 ret_void, f_test_alloc_fail},
1715 {"test_autochdir", 0, 0, 0, NULL,
1716 ret_void, f_test_autochdir},
1717 {"test_feedinput", 1, 1, FEARG_1, NULL,
1718 ret_void, f_test_feedinput},
1719 {"test_garbagecollect_now", 0, 0, 0, NULL,
1720 ret_void, f_test_garbagecollect_now},
1721 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1722 ret_void, f_test_garbagecollect_soon},
1723 {"test_getvalue", 1, 1, FEARG_1, NULL,
1724 ret_number, f_test_getvalue},
1725 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1726 ret_void, f_test_ignore_error},
1727 {"test_null_blob", 0, 0, 0, NULL,
1728 ret_blob, f_test_null_blob},
1729 {"test_null_channel", 0, 0, 0, NULL,
1730 ret_channel, JOB_FUNC(f_test_null_channel)},
1731 {"test_null_dict", 0, 0, 0, NULL,
1732 ret_dict_any, f_test_null_dict},
1733 {"test_null_function", 0, 0, 0, NULL,
1734 ret_func_any, f_test_null_function},
1735 {"test_null_job", 0, 0, 0, NULL,
1736 ret_job, JOB_FUNC(f_test_null_job)},
1737 {"test_null_list", 0, 0, 0, NULL,
1738 ret_list_any, f_test_null_list},
1739 {"test_null_partial", 0, 0, 0, NULL,
1740 ret_func_any, f_test_null_partial},
1741 {"test_null_string", 0, 0, 0, NULL,
1742 ret_string, f_test_null_string},
1743 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1744 ret_void, f_test_option_not_set},
1745 {"test_override", 2, 2, FEARG_2, NULL,
1746 ret_void, f_test_override},
1747 {"test_refcount", 1, 1, FEARG_1, NULL,
1748 ret_number, f_test_refcount},
1749 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1750 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001751#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001752 f_test_scrollbar
1753#else
1754 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001755#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001756 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001757 {"test_setmouse", 2, 2, 0, NULL,
1758 ret_void, f_test_setmouse},
1759 {"test_settime", 1, 1, FEARG_1, NULL,
1760 ret_void, f_test_settime},
1761 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1762 ret_void, f_test_srand_seed},
1763 {"test_unknown", 0, 0, 0, NULL,
1764 ret_any, f_test_unknown},
1765 {"test_void", 0, 0, 0, NULL,
1766 ret_void, f_test_void},
1767 {"timer_info", 0, 1, FEARG_1, NULL,
1768 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1769 {"timer_pause", 2, 2, FEARG_1, NULL,
1770 ret_void, TIMER_FUNC(f_timer_pause)},
1771 {"timer_start", 2, 3, FEARG_1, NULL,
1772 ret_number, TIMER_FUNC(f_timer_start)},
1773 {"timer_stop", 1, 1, FEARG_1, NULL,
1774 ret_void, TIMER_FUNC(f_timer_stop)},
1775 {"timer_stopall", 0, 0, 0, NULL,
1776 ret_void, TIMER_FUNC(f_timer_stopall)},
1777 {"tolower", 1, 1, FEARG_1, NULL,
1778 ret_string, f_tolower},
1779 {"toupper", 1, 1, FEARG_1, NULL,
1780 ret_string, f_toupper},
1781 {"tr", 3, 3, FEARG_1, NULL,
1782 ret_string, f_tr},
1783 {"trim", 1, 3, FEARG_1, NULL,
1784 ret_string, f_trim},
1785 {"trunc", 1, 1, FEARG_1, NULL,
1786 ret_float, FLOAT_FUNC(f_trunc)},
1787 {"type", 1, 1, FEARG_1, NULL,
1788 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001789 {"typename", 1, 1, FEARG_1, NULL,
1790 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001791 {"undofile", 1, 1, FEARG_1, NULL,
1792 ret_string, f_undofile},
1793 {"undotree", 0, 0, 0, NULL,
1794 ret_dict_any, f_undotree},
1795 {"uniq", 1, 3, FEARG_1, NULL,
1796 ret_list_any, f_uniq},
1797 {"values", 1, 1, FEARG_1, NULL,
1798 ret_list_any, f_values},
1799 {"virtcol", 1, 1, FEARG_1, NULL,
1800 ret_number, f_virtcol},
1801 {"visualmode", 0, 1, 0, NULL,
1802 ret_string, f_visualmode},
1803 {"wildmenumode", 0, 0, 0, NULL,
1804 ret_number, f_wildmenumode},
1805 {"win_execute", 2, 3, FEARG_2, NULL,
1806 ret_string, f_win_execute},
1807 {"win_findbuf", 1, 1, FEARG_1, NULL,
1808 ret_list_number, f_win_findbuf},
1809 {"win_getid", 0, 2, FEARG_1, NULL,
1810 ret_number, f_win_getid},
1811 {"win_gettype", 0, 1, FEARG_1, NULL,
1812 ret_string, f_win_gettype},
1813 {"win_gotoid", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001814 ret_number_bool, f_win_gotoid},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001815 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1816 ret_list_number, f_win_id2tabwin},
1817 {"win_id2win", 1, 1, FEARG_1, NULL,
1818 ret_number, f_win_id2win},
1819 {"win_screenpos", 1, 1, FEARG_1, NULL,
1820 ret_list_number, f_win_screenpos},
1821 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001822 ret_number_bool, f_win_splitmove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001823 {"winbufnr", 1, 1, FEARG_1, NULL,
1824 ret_number, f_winbufnr},
1825 {"wincol", 0, 0, 0, NULL,
1826 ret_number, f_wincol},
1827 {"windowsversion", 0, 0, 0, NULL,
1828 ret_string, f_windowsversion},
1829 {"winheight", 1, 1, FEARG_1, NULL,
1830 ret_number, f_winheight},
1831 {"winlayout", 0, 1, FEARG_1, NULL,
1832 ret_list_any, f_winlayout},
1833 {"winline", 0, 0, 0, NULL,
1834 ret_number, f_winline},
1835 {"winnr", 0, 1, FEARG_1, NULL,
1836 ret_number, f_winnr},
1837 {"winrestcmd", 0, 0, 0, NULL,
1838 ret_string, f_winrestcmd},
1839 {"winrestview", 1, 1, FEARG_1, NULL,
1840 ret_void, f_winrestview},
1841 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001842 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001843 {"winwidth", 1, 1, FEARG_1, NULL,
1844 ret_number, f_winwidth},
1845 {"wordcount", 0, 0, 0, NULL,
1846 ret_dict_number, f_wordcount},
1847 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001848 ret_number_bool, f_writefile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001849 {"xor", 2, 2, FEARG_1, NULL,
1850 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001851};
1852
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001853/*
1854 * Function given to ExpandGeneric() to obtain the list of internal
1855 * or user defined function names.
1856 */
1857 char_u *
1858get_function_name(expand_T *xp, int idx)
1859{
1860 static int intidx = -1;
1861 char_u *name;
1862
1863 if (idx == 0)
1864 intidx = -1;
1865 if (intidx < 0)
1866 {
1867 name = get_user_func_name(xp, idx);
1868 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001869 {
1870 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1871 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001872 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001873 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001874 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001875 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001876 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001877 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001878 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001879 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001880 STRCAT(IObuff, ")");
1881 return IObuff;
1882 }
1883
1884 return NULL;
1885}
1886
1887/*
1888 * Function given to ExpandGeneric() to obtain the list of internal or
1889 * user defined variable or function names.
1890 */
1891 char_u *
1892get_expr_name(expand_T *xp, int idx)
1893{
1894 static int intidx = -1;
1895 char_u *name;
1896
1897 if (idx == 0)
1898 intidx = -1;
1899 if (intidx < 0)
1900 {
1901 name = get_function_name(xp, idx);
1902 if (name != NULL)
1903 return name;
1904 }
1905 return get_user_var_name(xp, ++intidx);
1906}
1907
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001908/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001909 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001910 * Return index, or -1 if not found or "implemented" is TRUE and the function
1911 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001912 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001913 static int
1914find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001915{
1916 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001917 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001918 int cmp;
1919 int x;
1920
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001921 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001922
1923 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001924 while (first <= last)
1925 {
1926 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001927 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001928 if (cmp < 0)
1929 last = x - 1;
1930 else if (cmp > 0)
1931 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001932 else if (implemented && global_functions[x].f_func == NULL)
1933 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001934 else
1935 return x;
1936 }
1937 return -1;
1938}
1939
Bram Moolenaar15c47602020-03-26 22:16:48 +01001940/*
1941 * Find internal function "name" in table "global_functions".
1942 * Return index, or -1 if not found or the function is not implemented.
1943 */
1944 int
1945find_internal_func(char_u *name)
1946{
1947 return find_internal_func_opt(name, TRUE);
1948}
1949
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001950 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001951has_internal_func(char_u *name)
1952{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001953 return find_internal_func_opt(name, TRUE) >= 0;
1954}
1955
1956 static int
1957has_internal_func_name(char_u *name)
1958{
1959 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001960}
1961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962 char *
1963internal_func_name(int idx)
1964{
1965 return global_functions[idx].f_name;
1966}
1967
Bram Moolenaar94738d82020-10-21 14:25:07 +02001968/*
1969 * Check the argument types for builting function "idx".
1970 * Uses the list of types on the type stack: "types".
1971 * Return FAIL and gives an error message when a type is wrong.
1972 */
1973 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001974internal_func_check_arg_types(
1975 type_T **types,
1976 int idx,
1977 int argcount,
1978 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001979{
1980 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1981 int i;
1982
1983 if (argchecks != NULL)
1984 {
1985 argcontext_T context;
1986
1987 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001988 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001989 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001990 for (i = 0; i < argcount; ++i)
1991 if (argchecks[i] != NULL)
1992 {
1993 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001994 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001995 return FAIL;
1996 }
1997 }
1998 return OK;
1999}
2000
Bram Moolenaara1224cb2020-10-22 12:31:49 +02002001/*
2002 * Call the "f_retfunc" function to obtain the return type of function "idx".
2003 * "argtypes" is the list of argument types or NULL when there are no
2004 * arguments.
2005 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002007internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002009 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010}
2011
2012/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002013 * Return TRUE if "idx" is for the map() function.
2014 */
2015 int
2016internal_func_is_map(int idx)
2017{
2018 return global_functions[idx].f_func == f_map;
2019}
2020
2021/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002023 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2024 * first argument, 2 if method base is second argument, etc. 9 if method base
2025 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 */
2027 int
2028check_internal_func(int idx, int argcount)
2029{
2030 int res;
2031 char *name;
2032
2033 if (argcount < global_functions[idx].f_min_argc)
2034 res = FCERR_TOOFEW;
2035 else if (argcount > global_functions[idx].f_max_argc)
2036 res = FCERR_TOOMANY;
2037 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002038 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039
2040 name = internal_func_name(idx);
2041 if (res == FCERR_TOOMANY)
2042 semsg(_(e_toomanyarg), name);
2043 else
2044 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002045 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046}
2047
Bram Moolenaarac92e252019-08-03 21:58:38 +02002048 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002049call_internal_func(
2050 char_u *name,
2051 int argcount,
2052 typval_T *argvars,
2053 typval_T *rettv)
2054{
2055 int i;
2056
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002057 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002058 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002059 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002060 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002061 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002062 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002063 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002064 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002065 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002066 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002067}
2068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069 void
2070call_internal_func_by_idx(
2071 int idx,
2072 typval_T *argvars,
2073 typval_T *rettv)
2074{
2075 global_functions[idx].f_func(argvars, rettv);
2076}
2077
Bram Moolenaarac92e252019-08-03 21:58:38 +02002078/*
2079 * Invoke a method for base->method().
2080 */
2081 int
2082call_internal_method(
2083 char_u *name,
2084 int argcount,
2085 typval_T *argvars,
2086 typval_T *rettv,
2087 typval_T *basetv)
2088{
2089 int i;
2090 int fi;
2091 typval_T argv[MAX_FUNC_ARGS + 1];
2092
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002093 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002094 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002095 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002096 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002097 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002098 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002099 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002100 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002101 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002102
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002103 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002104 {
2105 // base value goes last
2106 for (i = 0; i < argcount; ++i)
2107 argv[i] = argvars[i];
2108 argv[argcount] = *basetv;
2109 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002110 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002111 {
2112 // base value goes second
2113 argv[0] = argvars[0];
2114 argv[1] = *basetv;
2115 for (i = 1; i < argcount; ++i)
2116 argv[i + 1] = argvars[i];
2117 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002118 else if (global_functions[fi].f_argtype == FEARG_3)
2119 {
2120 // base value goes third
2121 argv[0] = argvars[0];
2122 argv[1] = argvars[1];
2123 argv[2] = *basetv;
2124 for (i = 2; i < argcount; ++i)
2125 argv[i + 1] = argvars[i];
2126 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002127 else if (global_functions[fi].f_argtype == FEARG_4)
2128 {
2129 // base value goes fourth
2130 argv[0] = argvars[0];
2131 argv[1] = argvars[1];
2132 argv[2] = argvars[2];
2133 argv[3] = *basetv;
2134 for (i = 3; i < argcount; ++i)
2135 argv[i + 1] = argvars[i];
2136 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002137 else
2138 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002139 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002140 argv[0] = *basetv;
2141 for (i = 0; i < argcount; ++i)
2142 argv[i + 1] = argvars[i];
2143 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002144 argv[argcount + 1].v_type = VAR_UNKNOWN;
2145
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002146 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002147 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002148}
2149
2150/*
2151 * Return TRUE for a non-zero Number and a non-empty String.
2152 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002153 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154non_zero_arg(typval_T *argvars)
2155{
2156 return ((argvars[0].v_type == VAR_NUMBER
2157 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002158 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002159 && argvars[0].vval.v_number == VVAL_TRUE)
2160 || (argvars[0].v_type == VAR_STRING
2161 && argvars[0].vval.v_string != NULL
2162 && *argvars[0].vval.v_string != NUL));
2163}
2164
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002165#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002166/*
2167 * Get the float value of "argvars[0]" into "f".
2168 * Returns FAIL when the argument is not a Number or Float.
2169 */
2170 static int
2171get_float_arg(typval_T *argvars, float_T *f)
2172{
2173 if (argvars[0].v_type == VAR_FLOAT)
2174 {
2175 *f = argvars[0].vval.v_float;
2176 return OK;
2177 }
2178 if (argvars[0].v_type == VAR_NUMBER)
2179 {
2180 *f = (float_T)argvars[0].vval.v_number;
2181 return OK;
2182 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002183 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002184 return FAIL;
2185}
2186
2187/*
2188 * "abs(expr)" function
2189 */
2190 static void
2191f_abs(typval_T *argvars, typval_T *rettv)
2192{
2193 if (argvars[0].v_type == VAR_FLOAT)
2194 {
2195 rettv->v_type = VAR_FLOAT;
2196 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
2197 }
2198 else
2199 {
2200 varnumber_T n;
2201 int error = FALSE;
2202
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002203 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002204 if (error)
2205 rettv->vval.v_number = -1;
2206 else if (n > 0)
2207 rettv->vval.v_number = n;
2208 else
2209 rettv->vval.v_number = -n;
2210 }
2211}
2212
2213/*
2214 * "acos()" function
2215 */
2216 static void
2217f_acos(typval_T *argvars, typval_T *rettv)
2218{
2219 float_T f = 0.0;
2220
2221 rettv->v_type = VAR_FLOAT;
2222 if (get_float_arg(argvars, &f) == OK)
2223 rettv->vval.v_float = acos(f);
2224 else
2225 rettv->vval.v_float = 0.0;
2226}
2227#endif
2228
2229/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002230 * "and(expr, expr)" function
2231 */
2232 static void
2233f_and(typval_T *argvars, typval_T *rettv)
2234{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002235 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2236 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002237}
2238
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002239#ifdef FEAT_FLOAT
2240/*
2241 * "asin()" function
2242 */
2243 static void
2244f_asin(typval_T *argvars, typval_T *rettv)
2245{
2246 float_T f = 0.0;
2247
2248 rettv->v_type = VAR_FLOAT;
2249 if (get_float_arg(argvars, &f) == OK)
2250 rettv->vval.v_float = asin(f);
2251 else
2252 rettv->vval.v_float = 0.0;
2253}
2254
2255/*
2256 * "atan()" function
2257 */
2258 static void
2259f_atan(typval_T *argvars, typval_T *rettv)
2260{
2261 float_T f = 0.0;
2262
2263 rettv->v_type = VAR_FLOAT;
2264 if (get_float_arg(argvars, &f) == OK)
2265 rettv->vval.v_float = atan(f);
2266 else
2267 rettv->vval.v_float = 0.0;
2268}
2269
2270/*
2271 * "atan2()" function
2272 */
2273 static void
2274f_atan2(typval_T *argvars, typval_T *rettv)
2275{
2276 float_T fx = 0.0, fy = 0.0;
2277
2278 rettv->v_type = VAR_FLOAT;
2279 if (get_float_arg(argvars, &fx) == OK
2280 && get_float_arg(&argvars[1], &fy) == OK)
2281 rettv->vval.v_float = atan2(fx, fy);
2282 else
2283 rettv->vval.v_float = 0.0;
2284}
2285#endif
2286
2287/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002288 * "balloon_show()" function
2289 */
2290#ifdef FEAT_BEVAL
2291 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002292f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2293{
2294 rettv->v_type = VAR_STRING;
2295 if (balloonEval != NULL)
2296 {
2297 if (balloonEval->msg == NULL)
2298 rettv->vval.v_string = NULL;
2299 else
2300 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2301 }
2302}
2303
2304 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002305f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2306{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002307 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002308 {
2309 if (argvars[0].v_type == VAR_LIST
2310# ifdef FEAT_GUI
2311 && !gui.in_use
2312# endif
2313 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002314 {
2315 list_T *l = argvars[0].vval.v_list;
2316
2317 // empty list removes the balloon
2318 post_balloon(balloonEval, NULL,
2319 l == NULL || l->lv_len == 0 ? NULL : l);
2320 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002321 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002322 {
2323 char_u *mesg = tv_get_string_chk(&argvars[0]);
2324
2325 if (mesg != NULL)
2326 // empty string removes the balloon
2327 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2328 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002329 }
2330}
2331
Bram Moolenaar669a8282017-11-19 20:13:05 +01002332# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002333 static void
2334f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2335{
2336 if (rettv_list_alloc(rettv) == OK)
2337 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002338 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002339
2340 if (msg != NULL)
2341 {
2342 pumitem_T *array;
2343 int size = split_message(msg, &array);
2344 int i;
2345
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002346 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002347 for (i = 1; i < size - 1; ++i)
2348 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002349 while (size > 0)
2350 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002351 vim_free(array);
2352 }
2353 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002354}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002355# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002356#endif
2357
2358/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002359 * Get the buffer from "arg" and give an error and return NULL if it is not
2360 * valid.
2361 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002362 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002363get_buf_arg(typval_T *arg)
2364{
2365 buf_T *buf;
2366
2367 ++emsg_off;
2368 buf = tv_get_buf(arg, FALSE);
2369 --emsg_off;
2370 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002371 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002372 return buf;
2373}
2374
2375/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002376 * "byte2line(byte)" function
2377 */
2378 static void
2379f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2380{
2381#ifndef FEAT_BYTEOFF
2382 rettv->vval.v_number = -1;
2383#else
2384 long boff = 0;
2385
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002386 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002387 if (boff < 0)
2388 rettv->vval.v_number = -1;
2389 else
2390 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2391 (linenr_T)0, &boff);
2392#endif
2393}
2394
2395 static void
2396byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2397{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002398 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002399 char_u *str;
2400 varnumber_T idx;
2401
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002402 str = tv_get_string_chk(&argvars[0]);
2403 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002404 rettv->vval.v_number = -1;
2405 if (str == NULL || idx < 0)
2406 return;
2407
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002408 t = str;
2409 for ( ; idx > 0; idx--)
2410 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002411 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002412 return;
2413 if (enc_utf8 && comp)
2414 t += utf_ptr2len(t);
2415 else
2416 t += (*mb_ptr2len)(t);
2417 }
2418 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002419}
2420
2421/*
2422 * "byteidx()" function
2423 */
2424 static void
2425f_byteidx(typval_T *argvars, typval_T *rettv)
2426{
2427 byteidx(argvars, rettv, FALSE);
2428}
2429
2430/*
2431 * "byteidxcomp()" function
2432 */
2433 static void
2434f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2435{
2436 byteidx(argvars, rettv, TRUE);
2437}
2438
2439/*
2440 * "call(func, arglist [, dict])" function
2441 */
2442 static void
2443f_call(typval_T *argvars, typval_T *rettv)
2444{
2445 char_u *func;
2446 partial_T *partial = NULL;
2447 dict_T *selfdict = NULL;
2448
2449 if (argvars[1].v_type != VAR_LIST)
2450 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002451 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002452 return;
2453 }
2454 if (argvars[1].vval.v_list == NULL)
2455 return;
2456
2457 if (argvars[0].v_type == VAR_FUNC)
2458 func = argvars[0].vval.v_string;
2459 else if (argvars[0].v_type == VAR_PARTIAL)
2460 {
2461 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002462 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002463 }
2464 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002465 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002466 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002467 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002468
2469 if (argvars[2].v_type != VAR_UNKNOWN)
2470 {
2471 if (argvars[2].v_type != VAR_DICT)
2472 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002473 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002474 return;
2475 }
2476 selfdict = argvars[2].vval.v_dict;
2477 }
2478
2479 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2480}
2481
2482#ifdef FEAT_FLOAT
2483/*
2484 * "ceil({float})" function
2485 */
2486 static void
2487f_ceil(typval_T *argvars, typval_T *rettv)
2488{
2489 float_T f = 0.0;
2490
2491 rettv->v_type = VAR_FLOAT;
2492 if (get_float_arg(argvars, &f) == OK)
2493 rettv->vval.v_float = ceil(f);
2494 else
2495 rettv->vval.v_float = 0.0;
2496}
2497#endif
2498
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002499/*
2500 * "changenr()" function
2501 */
2502 static void
2503f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2504{
2505 rettv->vval.v_number = curbuf->b_u_seq_cur;
2506}
2507
2508/*
2509 * "char2nr(string)" function
2510 */
2511 static void
2512f_char2nr(typval_T *argvars, typval_T *rettv)
2513{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002514 if (has_mbyte)
2515 {
2516 int utf8 = 0;
2517
2518 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002519 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002520
2521 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002522 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002523 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002524 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002525 }
2526 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002527 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002528}
2529
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002530/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002531 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2532 * returns the character index of the column. Otherwise, returns the byte index
2533 * of the column.
2534 */
2535 static void
2536get_col(typval_T *argvars, typval_T *rettv, int charcol)
2537{
2538 colnr_T col = 0;
2539 pos_T *fp;
2540 int fnum = curbuf->b_fnum;
2541
2542 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2543 if (fp != NULL && fnum == curbuf->b_fnum)
2544 {
2545 if (fp->col == MAXCOL)
2546 {
2547 // '> can be MAXCOL, get the length of the line then
2548 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2549 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2550 else
2551 col = MAXCOL;
2552 }
2553 else
2554 {
2555 col = fp->col + 1;
2556 // col(".") when the cursor is on the NUL at the end of the line
2557 // because of "coladd" can be seen as an extra column.
2558 if (virtual_active() && fp == &curwin->w_cursor)
2559 {
2560 char_u *p = ml_get_cursor();
2561
2562 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2563 curwin->w_virtcol - curwin->w_cursor.coladd))
2564 {
2565 int l;
2566
2567 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2568 col += l;
2569 }
2570 }
2571 }
2572 }
2573 rettv->vval.v_number = col;
2574}
2575
2576/*
2577 * "charcol()" function
2578 */
2579 static void
2580f_charcol(typval_T *argvars, typval_T *rettv)
2581{
2582 get_col(argvars, rettv, TRUE);
2583}
2584
2585/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002586 * "charidx()" function
2587 */
2588 static void
2589f_charidx(typval_T *argvars, typval_T *rettv)
2590{
2591 char_u *str;
2592 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002593 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002594 char_u *p;
2595 int len;
2596 int (*ptr2len)(char_u *);
2597
2598 rettv->vval.v_number = -1;
2599
2600 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2601 || (argvars[2].v_type != VAR_UNKNOWN
2602 && argvars[2].v_type != VAR_NUMBER))
2603 {
2604 emsg(_(e_invarg));
2605 return;
2606 }
2607
2608 str = tv_get_string_chk(&argvars[0]);
2609 idx = tv_get_number_chk(&argvars[1], NULL);
2610 if (str == NULL || idx < 0)
2611 return;
2612
2613 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002614 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002615 if (countcc < 0 || countcc > 1)
2616 {
2617 semsg(_(e_using_number_as_bool_nr), countcc);
2618 return;
2619 }
2620
2621 if (enc_utf8 && countcc)
2622 ptr2len = utf_ptr2len;
2623 else
2624 ptr2len = mb_ptr2len;
2625
2626 for (p = str, len = 0; p <= str + idx; len++)
2627 {
2628 if (*p == NUL)
2629 return;
2630 p += ptr2len(p);
2631 }
2632
2633 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2634}
2635
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002636 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002637get_optional_window(typval_T *argvars, int idx)
2638{
2639 win_T *win = curwin;
2640
2641 if (argvars[idx].v_type != VAR_UNKNOWN)
2642 {
2643 win = find_win_by_nr_or_id(&argvars[idx]);
2644 if (win == NULL)
2645 {
2646 emsg(_(e_invalwindow));
2647 return NULL;
2648 }
2649 }
2650 return win;
2651}
2652
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002653/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002654 * "col(string)" function
2655 */
2656 static void
2657f_col(typval_T *argvars, typval_T *rettv)
2658{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002659 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002660}
2661
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002662/*
2663 * "confirm(message, buttons[, default [, type]])" function
2664 */
2665 static void
2666f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2667{
2668#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2669 char_u *message;
2670 char_u *buttons = NULL;
2671 char_u buf[NUMBUFLEN];
2672 char_u buf2[NUMBUFLEN];
2673 int def = 1;
2674 int type = VIM_GENERIC;
2675 char_u *typestr;
2676 int error = FALSE;
2677
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002678 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002679 if (message == NULL)
2680 error = TRUE;
2681 if (argvars[1].v_type != VAR_UNKNOWN)
2682 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002683 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002684 if (buttons == NULL)
2685 error = TRUE;
2686 if (argvars[2].v_type != VAR_UNKNOWN)
2687 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002688 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002689 if (argvars[3].v_type != VAR_UNKNOWN)
2690 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002691 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002692 if (typestr == NULL)
2693 error = TRUE;
2694 else
2695 {
2696 switch (TOUPPER_ASC(*typestr))
2697 {
2698 case 'E': type = VIM_ERROR; break;
2699 case 'Q': type = VIM_QUESTION; break;
2700 case 'I': type = VIM_INFO; break;
2701 case 'W': type = VIM_WARNING; break;
2702 case 'G': type = VIM_GENERIC; break;
2703 }
2704 }
2705 }
2706 }
2707 }
2708
2709 if (buttons == NULL || *buttons == NUL)
2710 buttons = (char_u *)_("&Ok");
2711
2712 if (!error)
2713 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2714 def, NULL, FALSE);
2715#endif
2716}
2717
2718/*
2719 * "copy()" function
2720 */
2721 static void
2722f_copy(typval_T *argvars, typval_T *rettv)
2723{
2724 item_copy(&argvars[0], rettv, FALSE, 0);
2725}
2726
2727#ifdef FEAT_FLOAT
2728/*
2729 * "cos()" function
2730 */
2731 static void
2732f_cos(typval_T *argvars, typval_T *rettv)
2733{
2734 float_T f = 0.0;
2735
2736 rettv->v_type = VAR_FLOAT;
2737 if (get_float_arg(argvars, &f) == OK)
2738 rettv->vval.v_float = cos(f);
2739 else
2740 rettv->vval.v_float = 0.0;
2741}
2742
2743/*
2744 * "cosh()" function
2745 */
2746 static void
2747f_cosh(typval_T *argvars, typval_T *rettv)
2748{
2749 float_T f = 0.0;
2750
2751 rettv->v_type = VAR_FLOAT;
2752 if (get_float_arg(argvars, &f) == OK)
2753 rettv->vval.v_float = cosh(f);
2754 else
2755 rettv->vval.v_float = 0.0;
2756}
2757#endif
2758
2759/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002760 * Set the cursor position.
2761 * If 'charcol' is TRUE, then use the column number as a character offet.
2762 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002763 */
2764 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002765set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002766{
2767 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002768 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002769 int set_curswant = TRUE;
2770
2771 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002772 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002773 {
2774 pos_T pos;
2775 colnr_T curswant = -1;
2776
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002777 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002778 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002779 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002780 return;
2781 }
2782 line = pos.lnum;
2783 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002784 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002785 if (curswant >= 0)
2786 {
2787 curwin->w_curswant = curswant - 1;
2788 set_curswant = FALSE;
2789 }
2790 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002791 else if ((argvars[0].v_type == VAR_NUMBER ||
2792 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002793 && (argvars[1].v_type == VAR_NUMBER ||
2794 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002795 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002796 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002797 if (line < 0)
2798 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002799 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002800 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002801 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002802 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002803 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002804 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002805 else
2806 {
2807 emsg(_(e_invarg));
2808 return;
2809 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002810 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002811 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002812 if (line > 0)
2813 curwin->w_cursor.lnum = line;
2814 if (col > 0)
2815 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002816 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002817
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002818 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002819 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002820 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002821 if (has_mbyte)
2822 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002823
2824 curwin->w_set_curswant = set_curswant;
2825 rettv->vval.v_number = 0;
2826}
2827
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002828/*
2829 * "cursor(lnum, col)" function, or
2830 * "cursor(list)"
2831 *
2832 * Moves the cursor to the specified line and column.
2833 * Returns 0 when the position could be set, -1 otherwise.
2834 */
2835 static void
2836f_cursor(typval_T *argvars, typval_T *rettv)
2837{
2838 set_cursorpos(argvars, rettv, FALSE);
2839}
2840
Bram Moolenaar4f974752019-02-17 17:44:42 +01002841#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002842/*
2843 * "debugbreak()" function
2844 */
2845 static void
2846f_debugbreak(typval_T *argvars, typval_T *rettv)
2847{
2848 int pid;
2849
2850 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002851 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002852 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002853 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002854 else
2855 {
2856 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2857
2858 if (hProcess != NULL)
2859 {
2860 DebugBreakProcess(hProcess);
2861 CloseHandle(hProcess);
2862 rettv->vval.v_number = OK;
2863 }
2864 }
2865}
2866#endif
2867
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002868/*
2869 * "deepcopy()" function
2870 */
2871 static void
2872f_deepcopy(typval_T *argvars, typval_T *rettv)
2873{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002874 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002875 int copyID;
2876
2877 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002878 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002879 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002880 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002881 else
2882 {
2883 copyID = get_copyID();
2884 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2885 }
2886}
2887
2888/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002889 * "did_filetype()" function
2890 */
2891 static void
2892f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2893{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002894 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002895}
2896
2897/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002898 * "echoraw({expr})" function
2899 */
2900 static void
2901f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2902{
2903 char_u *str = tv_get_string_chk(&argvars[0]);
2904
2905 if (str != NULL && *str != NUL)
2906 {
2907 out_str(str);
2908 out_flush();
2909 }
2910}
2911
2912/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002913 * "empty({expr})" function
2914 */
2915 static void
2916f_empty(typval_T *argvars, typval_T *rettv)
2917{
2918 int n = FALSE;
2919
2920 switch (argvars[0].v_type)
2921 {
2922 case VAR_STRING:
2923 case VAR_FUNC:
2924 n = argvars[0].vval.v_string == NULL
2925 || *argvars[0].vval.v_string == NUL;
2926 break;
2927 case VAR_PARTIAL:
2928 n = FALSE;
2929 break;
2930 case VAR_NUMBER:
2931 n = argvars[0].vval.v_number == 0;
2932 break;
2933 case VAR_FLOAT:
2934#ifdef FEAT_FLOAT
2935 n = argvars[0].vval.v_float == 0.0;
2936 break;
2937#endif
2938 case VAR_LIST:
2939 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002940 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002941 break;
2942 case VAR_DICT:
2943 n = argvars[0].vval.v_dict == NULL
2944 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2945 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002946 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002947 case VAR_SPECIAL:
2948 n = argvars[0].vval.v_number != VVAL_TRUE;
2949 break;
2950
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002951 case VAR_BLOB:
2952 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002953 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2954 break;
2955
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002956 case VAR_JOB:
2957#ifdef FEAT_JOB_CHANNEL
2958 n = argvars[0].vval.v_job == NULL
2959 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2960 break;
2961#endif
2962 case VAR_CHANNEL:
2963#ifdef FEAT_JOB_CHANNEL
2964 n = argvars[0].vval.v_channel == NULL
2965 || !channel_is_open(argvars[0].vval.v_channel);
2966 break;
2967#endif
2968 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002969 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002971 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002972 n = TRUE;
2973 break;
2974 }
2975
2976 rettv->vval.v_number = n;
2977}
2978
2979/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002980 * "environ()" function
2981 */
2982 static void
2983f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2984{
2985#if !defined(AMIGA)
2986 int i = 0;
2987 char_u *entry, *value;
2988# ifdef MSWIN
2989 extern wchar_t **_wenviron;
2990# else
2991 extern char **environ;
2992# endif
2993
2994 if (rettv_dict_alloc(rettv) != OK)
2995 return;
2996
2997# ifdef MSWIN
2998 if (*_wenviron == NULL)
2999 return;
3000# else
3001 if (*environ == NULL)
3002 return;
3003# endif
3004
3005 for (i = 0; ; ++i)
3006 {
3007# ifdef MSWIN
3008 short_u *p;
3009
3010 if ((p = (short_u *)_wenviron[i]) == NULL)
3011 return;
3012 entry = utf16_to_enc(p, NULL);
3013# else
3014 if ((entry = (char_u *)environ[i]) == NULL)
3015 return;
3016 entry = vim_strsave(entry);
3017# endif
3018 if (entry == NULL) // out of memory
3019 return;
3020 if ((value = vim_strchr(entry, '=')) == NULL)
3021 {
3022 vim_free(entry);
3023 continue;
3024 }
3025 *value++ = NUL;
3026 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3027 vim_free(entry);
3028 }
3029#endif
3030}
3031
3032/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033 * "escape({string}, {chars})" function
3034 */
3035 static void
3036f_escape(typval_T *argvars, typval_T *rettv)
3037{
3038 char_u buf[NUMBUFLEN];
3039
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003040 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3041 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003042 rettv->v_type = VAR_STRING;
3043}
3044
3045/*
3046 * "eval()" function
3047 */
3048 static void
3049f_eval(typval_T *argvars, typval_T *rettv)
3050{
3051 char_u *s, *p;
3052
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003053 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003054 if (s != NULL)
3055 s = skipwhite(s);
3056
3057 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003058 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003059 {
3060 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003061 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003062 need_clr_eos = FALSE;
3063 rettv->v_type = VAR_NUMBER;
3064 rettv->vval.v_number = 0;
3065 }
3066 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003067 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003068}
3069
3070/*
3071 * "eventhandler()" function
3072 */
3073 static void
3074f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3075{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003076 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003077}
3078
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003079static garray_T redir_execute_ga;
3080
3081/*
3082 * Append "value[value_len]" to the execute() output.
3083 */
3084 void
3085execute_redir_str(char_u *value, int value_len)
3086{
3087 int len;
3088
3089 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003090 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003091 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003092 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003093 if (ga_grow(&redir_execute_ga, len) == OK)
3094 {
3095 mch_memmove((char *)redir_execute_ga.ga_data
3096 + redir_execute_ga.ga_len, value, len);
3097 redir_execute_ga.ga_len += len;
3098 }
3099}
3100
3101/*
3102 * Get next line from a list.
3103 * Called by do_cmdline() to get the next line.
3104 * Returns allocated string, or NULL for end of function.
3105 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003106 static char_u *
3107get_list_line(
3108 int c UNUSED,
3109 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003110 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02003111 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003112{
3113 listitem_T **p = (listitem_T **)cookie;
3114 listitem_T *item = *p;
3115 char_u buf[NUMBUFLEN];
3116 char_u *s;
3117
3118 if (item == NULL)
3119 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003120 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003121 *p = item->li_next;
3122 return s == NULL ? NULL : vim_strsave(s);
3123}
3124
3125/*
3126 * "execute()" function
3127 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02003128 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003129execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003130{
3131 char_u *cmd = NULL;
3132 list_T *list = NULL;
3133 int save_msg_silent = msg_silent;
3134 int save_emsg_silent = emsg_silent;
3135 int save_emsg_noredir = emsg_noredir;
3136 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003137 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003138 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003139 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003140 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003141
3142 rettv->vval.v_string = NULL;
3143 rettv->v_type = VAR_STRING;
3144
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003145 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003146 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003147 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003148 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003149 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003150 return;
3151 ++list->lv_refcount;
3152 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003153 else if (argvars[arg_off].v_type == VAR_JOB
3154 || argvars[arg_off].v_type == VAR_CHANNEL)
3155 {
3156 emsg(_(e_inval_string));
3157 return;
3158 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003159 else
3160 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003161 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003162 if (cmd == NULL)
3163 return;
3164 }
3165
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003166 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003167 {
3168 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003169 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003170
3171 if (s == NULL)
3172 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003173 if (*s == NUL)
3174 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003175 if (STRNCMP(s, "silent", 6) == 0)
3176 ++msg_silent;
3177 if (STRCMP(s, "silent!") == 0)
3178 {
3179 emsg_silent = TRUE;
3180 emsg_noredir = TRUE;
3181 }
3182 }
3183 else
3184 ++msg_silent;
3185
3186 if (redir_execute)
3187 save_ga = redir_execute_ga;
3188 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3189 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003190 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003191 if (!echo_output)
3192 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003193
3194 if (cmd != NULL)
3195 do_cmdline_cmd(cmd);
3196 else
3197 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003198 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003199
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003200 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003201 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003202 do_cmdline(NULL, get_list_line, (void *)&item,
3203 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3204 --list->lv_refcount;
3205 }
3206
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003207 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003208 if (ga_grow(&redir_execute_ga, 1) == OK)
3209 {
3210 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3211 rettv->vval.v_string = redir_execute_ga.ga_data;
3212 }
3213 else
3214 {
3215 ga_clear(&redir_execute_ga);
3216 rettv->vval.v_string = NULL;
3217 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003218 msg_silent = save_msg_silent;
3219 emsg_silent = save_emsg_silent;
3220 emsg_noredir = save_emsg_noredir;
3221
3222 redir_execute = save_redir_execute;
3223 if (redir_execute)
3224 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003225 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003226
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003227 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003228 if (echo_output)
3229 // When not working silently: put it in column zero. A following
3230 // "echon" will overwrite the message, unavoidably.
3231 msg_col = 0;
3232 else
3233 // When working silently: Put it back where it was, since nothing
3234 // should have been written.
3235 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003236}
3237
3238/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003239 * "execute()" function
3240 */
3241 static void
3242f_execute(typval_T *argvars, typval_T *rettv)
3243{
3244 execute_common(argvars, rettv, 0);
3245}
3246
3247/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003248 * "exists()" function
3249 */
3250 static void
3251f_exists(typval_T *argvars, typval_T *rettv)
3252{
3253 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003254 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003255
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003256 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003257 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003258 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003259 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003260 if (mch_getenv(p + 1) != NULL)
3261 n = TRUE;
3262 else
3263 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003264 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003265 p = expand_env_save(p);
3266 if (p != NULL && *p != '$')
3267 n = TRUE;
3268 vim_free(p);
3269 }
3270 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003271 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003272 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003273 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003274 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003275 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003276 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003277 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003278 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003279 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003280 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003281 else if (*p == '?') // internal function only
3282 {
3283 n = has_internal_func_name(p + 1);
3284 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003285 else if (*p == ':')
3286 {
3287 n = cmd_exists(p + 1);
3288 }
3289 else if (*p == '#')
3290 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003291 if (p[1] == '#')
3292 n = autocmd_supported(p + 2);
3293 else
3294 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003295 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003296 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003297 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003298 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299 }
3300
3301 rettv->vval.v_number = n;
3302}
3303
3304#ifdef FEAT_FLOAT
3305/*
3306 * "exp()" function
3307 */
3308 static void
3309f_exp(typval_T *argvars, typval_T *rettv)
3310{
3311 float_T f = 0.0;
3312
3313 rettv->v_type = VAR_FLOAT;
3314 if (get_float_arg(argvars, &f) == OK)
3315 rettv->vval.v_float = exp(f);
3316 else
3317 rettv->vval.v_float = 0.0;
3318}
3319#endif
3320
3321/*
3322 * "expand()" function
3323 */
3324 static void
3325f_expand(typval_T *argvars, typval_T *rettv)
3326{
3327 char_u *s;
3328 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003329 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003330 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3331 expand_T xpc;
3332 int error = FALSE;
3333 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003334#ifdef BACKSLASH_IN_FILENAME
3335 char_u *p_csl_save = p_csl;
3336
3337 // avoid using 'completeslash' here
3338 p_csl = empty_option;
3339#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003340
3341 rettv->v_type = VAR_STRING;
3342 if (argvars[1].v_type != VAR_UNKNOWN
3343 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003344 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003345 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003346 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003347
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003348 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003349 if (*s == '%' || *s == '#' || *s == '<')
3350 {
3351 ++emsg_off;
3352 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3353 --emsg_off;
3354 if (rettv->v_type == VAR_LIST)
3355 {
3356 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3357 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003358 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003359 }
3360 else
3361 rettv->vval.v_string = result;
3362 }
3363 else
3364 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003365 // When the optional second argument is non-zero, don't remove matches
3366 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003367 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003368 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003369 options |= WILD_KEEP_ALL;
3370 if (!error)
3371 {
3372 ExpandInit(&xpc);
3373 xpc.xp_context = EXPAND_FILES;
3374 if (p_wic)
3375 options += WILD_ICASE;
3376 if (rettv->v_type == VAR_STRING)
3377 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3378 options, WILD_ALL);
3379 else if (rettv_list_alloc(rettv) != FAIL)
3380 {
3381 int i;
3382
3383 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3384 for (i = 0; i < xpc.xp_numfiles; i++)
3385 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3386 ExpandCleanup(&xpc);
3387 }
3388 }
3389 else
3390 rettv->vval.v_string = NULL;
3391 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003392#ifdef BACKSLASH_IN_FILENAME
3393 p_csl = p_csl_save;
3394#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003395}
3396
3397/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003398 * "expandcmd()" function
3399 * Expand all the special characters in a command string.
3400 */
3401 static void
3402f_expandcmd(typval_T *argvars, typval_T *rettv)
3403{
3404 exarg_T eap;
3405 char_u *cmdstr;
3406 char *errormsg = NULL;
3407
3408 rettv->v_type = VAR_STRING;
3409 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3410
3411 memset(&eap, 0, sizeof(eap));
3412 eap.cmd = cmdstr;
3413 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003414 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003415 eap.usefilter = FALSE;
3416 eap.nextcmd = NULL;
3417 eap.cmdidx = CMD_USER;
3418
3419 expand_filename(&eap, &cmdstr, &errormsg);
3420 if (errormsg != NULL && *errormsg != NUL)
3421 emsg(errormsg);
3422
3423 rettv->vval.v_string = cmdstr;
3424}
3425
3426/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003427 * "feedkeys()" function
3428 */
3429 static void
3430f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3431{
3432 int remap = TRUE;
3433 int insert = FALSE;
3434 char_u *keys, *flags;
3435 char_u nbuf[NUMBUFLEN];
3436 int typed = FALSE;
3437 int execute = FALSE;
3438 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003439 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003440 char_u *keys_esc;
3441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003442 // This is not allowed in the sandbox. If the commands would still be
3443 // executed in the sandbox it would be OK, but it probably happens later,
3444 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003445 if (check_secure())
3446 return;
3447
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003448 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003449
3450 if (argvars[1].v_type != VAR_UNKNOWN)
3451 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003452 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003453 for ( ; *flags != NUL; ++flags)
3454 {
3455 switch (*flags)
3456 {
3457 case 'n': remap = FALSE; break;
3458 case 'm': remap = TRUE; break;
3459 case 't': typed = TRUE; break;
3460 case 'i': insert = TRUE; break;
3461 case 'x': execute = TRUE; break;
3462 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003463 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003464 }
3465 }
3466 }
3467
3468 if (*keys != NUL || execute)
3469 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003470 // Need to escape K_SPECIAL and CSI before putting the string in the
3471 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003472 keys_esc = vim_strsave_escape_csi(keys);
3473 if (keys_esc != NULL)
3474 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003475 if (lowlevel)
3476 {
3477#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003478 int idx;
3479 int len = (int)STRLEN(keys);
3480
3481 for (idx = 0; idx < len; ++idx)
3482 {
3483 // if a CTRL-C was typed, set got_int, similar to what
3484 // happens in fill_input_buf()
3485 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3486 got_int = TRUE;
3487 add_to_input_buf(keys + idx, 1);
3488 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003489#else
3490 emsg(_("E980: lowlevel input not supported"));
3491#endif
3492 }
3493 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003494 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003495 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003496 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003497 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003498#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003499 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003500#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003501 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003502 typebuf_was_filled = TRUE;
3503 }
3504 vim_free(keys_esc);
3505
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003506 if (execute)
3507 {
3508 int save_msg_scroll = msg_scroll;
3509
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003510 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003511 msg_scroll = FALSE;
3512
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003513 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003514 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003515 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003516 ++in_feedkeys;
3517 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003518 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003519 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003520 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003521 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003522 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003523 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003524
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003525 msg_scroll |= save_msg_scroll;
3526 }
3527 }
3528 }
3529}
3530
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003531#ifdef FEAT_FLOAT
3532/*
3533 * "float2nr({float})" function
3534 */
3535 static void
3536f_float2nr(typval_T *argvars, typval_T *rettv)
3537{
3538 float_T f = 0.0;
3539
3540 if (get_float_arg(argvars, &f) == OK)
3541 {
Bram Moolenaar37184272020-05-23 19:30:05 +02003542 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003543 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02003544 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003545 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003546 else
3547 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003548 }
3549}
3550
3551/*
3552 * "floor({float})" function
3553 */
3554 static void
3555f_floor(typval_T *argvars, typval_T *rettv)
3556{
3557 float_T f = 0.0;
3558
3559 rettv->v_type = VAR_FLOAT;
3560 if (get_float_arg(argvars, &f) == OK)
3561 rettv->vval.v_float = floor(f);
3562 else
3563 rettv->vval.v_float = 0.0;
3564}
3565
3566/*
3567 * "fmod()" function
3568 */
3569 static void
3570f_fmod(typval_T *argvars, typval_T *rettv)
3571{
3572 float_T fx = 0.0, fy = 0.0;
3573
3574 rettv->v_type = VAR_FLOAT;
3575 if (get_float_arg(argvars, &fx) == OK
3576 && get_float_arg(&argvars[1], &fy) == OK)
3577 rettv->vval.v_float = fmod(fx, fy);
3578 else
3579 rettv->vval.v_float = 0.0;
3580}
3581#endif
3582
3583/*
3584 * "fnameescape({string})" function
3585 */
3586 static void
3587f_fnameescape(typval_T *argvars, typval_T *rettv)
3588{
3589 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003590 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003591 rettv->v_type = VAR_STRING;
3592}
3593
3594/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003595 * "foreground()" function
3596 */
3597 static void
3598f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3599{
3600#ifdef FEAT_GUI
3601 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003602 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003603 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003604 return;
3605 }
3606#endif
3607#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003608 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003609#endif
3610}
3611
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003612 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003613common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003614{
3615 char_u *s;
3616 char_u *name;
3617 int use_string = FALSE;
3618 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003619 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003620 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003621
3622 if (argvars[0].v_type == VAR_FUNC)
3623 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003624 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003625 s = argvars[0].vval.v_string;
3626 }
3627 else if (argvars[0].v_type == VAR_PARTIAL
3628 && argvars[0].vval.v_partial != NULL)
3629 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003630 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003631 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003632 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003633 }
3634 else
3635 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003636 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003637 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003638 use_string = TRUE;
3639 }
3640
Bram Moolenaar843b8842016-08-21 14:36:15 +02003641 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003642 {
3643 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003644 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003645 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3646 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003647 if (*name != NUL)
3648 s = NULL;
3649 }
3650
Bram Moolenaar843b8842016-08-21 14:36:15 +02003651 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3652 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003653 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003654 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003655 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003656 ? find_func(trans_name, is_global, NULL) == NULL
3657 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003658 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003659 else
3660 {
3661 int dict_idx = 0;
3662 int arg_idx = 0;
3663 list_T *list = NULL;
3664
3665 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3666 {
3667 char sid_buf[25];
3668 int off = *s == 's' ? 2 : 5;
3669
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003670 // Expand s: and <SID> into <SNR>nr_, so that the function can
3671 // also be called from another script. Using trans_function_name()
3672 // would also work, but some plugins depend on the name being
3673 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003674 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003675 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003676 if (name != NULL)
3677 {
3678 STRCPY(name, sid_buf);
3679 STRCAT(name, s + off);
3680 }
3681 }
3682 else
3683 name = vim_strsave(s);
3684
3685 if (argvars[1].v_type != VAR_UNKNOWN)
3686 {
3687 if (argvars[2].v_type != VAR_UNKNOWN)
3688 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003689 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003690 arg_idx = 1;
3691 dict_idx = 2;
3692 }
3693 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003694 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003695 dict_idx = 1;
3696 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003697 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003698 arg_idx = 1;
3699 if (dict_idx > 0)
3700 {
3701 if (argvars[dict_idx].v_type != VAR_DICT)
3702 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003703 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003704 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003705 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003706 }
3707 if (argvars[dict_idx].vval.v_dict == NULL)
3708 dict_idx = 0;
3709 }
3710 if (arg_idx > 0)
3711 {
3712 if (argvars[arg_idx].v_type != VAR_LIST)
3713 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003714 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003715 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003716 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003717 }
3718 list = argvars[arg_idx].vval.v_list;
3719 if (list == NULL || list->lv_len == 0)
3720 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003721 else if (list->lv_len > MAX_FUNC_ARGS)
3722 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003723 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003724 vim_free(name);
3725 goto theend;
3726 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003727 }
3728 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003729 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003730 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003731 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003732
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003733 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003734 if (pt == NULL)
3735 vim_free(name);
3736 else
3737 {
3738 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3739 {
3740 listitem_T *li;
3741 int i = 0;
3742 int arg_len = 0;
3743 int lv_len = 0;
3744
3745 if (arg_pt != NULL)
3746 arg_len = arg_pt->pt_argc;
3747 if (list != NULL)
3748 lv_len = list->lv_len;
3749 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003750 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003751 if (pt->pt_argv == NULL)
3752 {
3753 vim_free(pt);
3754 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003755 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003756 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003757 for (i = 0; i < arg_len; i++)
3758 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3759 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003760 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003761 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003762 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003763 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003764 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003765 }
3766
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003767 // For "function(dict.func, [], dict)" and "func" is a partial
3768 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003769 if (dict_idx > 0)
3770 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003771 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003772 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3773 ++pt->pt_dict->dv_refcount;
3774 }
3775 else if (arg_pt != NULL)
3776 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003777 // If the dict was bound automatically the result is also
3778 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003779 pt->pt_dict = arg_pt->pt_dict;
3780 pt->pt_auto = arg_pt->pt_auto;
3781 if (pt->pt_dict != NULL)
3782 ++pt->pt_dict->dv_refcount;
3783 }
3784
3785 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003786 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3787 {
3788 pt->pt_func = arg_pt->pt_func;
3789 func_ptr_ref(pt->pt_func);
3790 vim_free(name);
3791 }
3792 else if (is_funcref)
3793 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003794 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003795 func_ptr_ref(pt->pt_func);
3796 vim_free(name);
3797 }
3798 else
3799 {
3800 pt->pt_name = name;
3801 func_ref(name);
3802 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003803 }
3804 rettv->v_type = VAR_PARTIAL;
3805 rettv->vval.v_partial = pt;
3806 }
3807 else
3808 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003809 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003810 rettv->v_type = VAR_FUNC;
3811 rettv->vval.v_string = name;
3812 func_ref(name);
3813 }
3814 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003815theend:
3816 vim_free(trans_name);
3817}
3818
3819/*
3820 * "funcref()" function
3821 */
3822 static void
3823f_funcref(typval_T *argvars, typval_T *rettv)
3824{
3825 common_function(argvars, rettv, TRUE);
3826}
3827
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003828 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003829ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003830{
3831 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3832 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003833 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003834}
3835
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003836/*
3837 * "function()" function
3838 */
3839 static void
3840f_function(typval_T *argvars, typval_T *rettv)
3841{
3842 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003843}
3844
3845/*
3846 * "garbagecollect()" function
3847 */
3848 static void
3849f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3850{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003851 // This is postponed until we are back at the toplevel, because we may be
3852 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003853 want_garbage_collect = TRUE;
3854
Bram Moolenaar2df47312020-09-05 17:30:44 +02003855 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003856 garbage_collect_at_exit = TRUE;
3857}
3858
3859/*
3860 * "get()" function
3861 */
3862 static void
3863f_get(typval_T *argvars, typval_T *rettv)
3864{
3865 listitem_T *li;
3866 list_T *l;
3867 dictitem_T *di;
3868 dict_T *d;
3869 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003870 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003871
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003872 if (argvars[0].v_type == VAR_BLOB)
3873 {
3874 int error = FALSE;
3875 int idx = tv_get_number_chk(&argvars[1], &error);
3876
3877 if (!error)
3878 {
3879 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003880 if (idx < 0)
3881 idx = blob_len(argvars[0].vval.v_blob) + idx;
3882 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3883 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003884 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003885 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003886 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003887 tv = rettv;
3888 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003889 }
3890 }
3891 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003892 {
3893 if ((l = argvars[0].vval.v_list) != NULL)
3894 {
3895 int error = FALSE;
3896
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003897 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003898 if (!error && li != NULL)
3899 tv = &li->li_tv;
3900 }
3901 }
3902 else if (argvars[0].v_type == VAR_DICT)
3903 {
3904 if ((d = argvars[0].vval.v_dict) != NULL)
3905 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003906 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003907 if (di != NULL)
3908 tv = &di->di_tv;
3909 }
3910 }
3911 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3912 {
3913 partial_T *pt;
3914 partial_T fref_pt;
3915
3916 if (argvars[0].v_type == VAR_PARTIAL)
3917 pt = argvars[0].vval.v_partial;
3918 else
3919 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003920 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003921 fref_pt.pt_name = argvars[0].vval.v_string;
3922 pt = &fref_pt;
3923 }
3924
3925 if (pt != NULL)
3926 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003927 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003928 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003929
3930 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3931 {
3932 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003933 n = partial_name(pt);
3934 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003935 rettv->vval.v_string = NULL;
3936 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003937 {
3938 rettv->vval.v_string = vim_strsave(n);
3939 if (rettv->v_type == VAR_FUNC)
3940 func_ref(rettv->vval.v_string);
3941 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003942 }
3943 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003944 {
3945 what_is_dict = TRUE;
3946 if (pt->pt_dict != NULL)
3947 rettv_dict_set(rettv, pt->pt_dict);
3948 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003949 else if (STRCMP(what, "args") == 0)
3950 {
3951 rettv->v_type = VAR_LIST;
3952 if (rettv_list_alloc(rettv) == OK)
3953 {
3954 int i;
3955
3956 for (i = 0; i < pt->pt_argc; ++i)
3957 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3958 }
3959 }
3960 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003961 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003962
3963 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3964 // third argument
3965 if (!what_is_dict)
3966 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003967 }
3968 }
3969 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003970 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003971
3972 if (tv == NULL)
3973 {
3974 if (argvars[2].v_type != VAR_UNKNOWN)
3975 copy_tv(&argvars[2], rettv);
3976 }
3977 else
3978 copy_tv(tv, rettv);
3979}
3980
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003981/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003982 * "getchangelist()" function
3983 */
3984 static void
3985f_getchangelist(typval_T *argvars, typval_T *rettv)
3986{
3987#ifdef FEAT_JUMPLIST
3988 buf_T *buf;
3989 int i;
3990 list_T *l;
3991 dict_T *d;
3992#endif
3993
3994 if (rettv_list_alloc(rettv) != OK)
3995 return;
3996
3997#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003998 if (argvars[0].v_type == VAR_UNKNOWN)
3999 buf = curbuf;
4000 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02004001 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004002 if (buf == NULL)
4003 return;
4004
4005 l = list_alloc();
4006 if (l == NULL)
4007 return;
4008
4009 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4010 return;
4011 /*
4012 * The current window change list index tracks only the position in the
4013 * current buffer change list. For other buffers, use the change list
4014 * length as the current index.
4015 */
4016 list_append_number(rettv->vval.v_list,
4017 (varnumber_T)((buf == curwin->w_buffer)
4018 ? curwin->w_changelistidx : buf->b_changelistlen));
4019
4020 for (i = 0; i < buf->b_changelistlen; ++i)
4021 {
4022 if (buf->b_changelist[i].lnum == 0)
4023 continue;
4024 if ((d = dict_alloc()) == NULL)
4025 return;
4026 if (list_append_dict(l, d) == FAIL)
4027 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004028 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4029 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004030 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004031 }
4032#endif
4033}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004034
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004035 static void
4036getpos_both(
4037 typval_T *argvars,
4038 typval_T *rettv,
4039 int getcurpos,
4040 int charcol)
4041{
4042 pos_T *fp = NULL;
4043 pos_T pos;
4044 win_T *wp = curwin;
4045 list_T *l;
4046 int fnum = -1;
4047
4048 if (rettv_list_alloc(rettv) == OK)
4049 {
4050 l = rettv->vval.v_list;
4051 if (getcurpos)
4052 {
4053 if (argvars[0].v_type != VAR_UNKNOWN)
4054 {
4055 wp = find_win_by_nr_or_id(&argvars[0]);
4056 if (wp != NULL)
4057 fp = &wp->w_cursor;
4058 }
4059 else
4060 fp = &curwin->w_cursor;
4061 if (fp != NULL && charcol)
4062 {
4063 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01004064 pos.col =
4065 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004066 fp = &pos;
4067 }
4068 }
4069 else
4070 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
4071 if (fnum != -1)
4072 list_append_number(l, (varnumber_T)fnum);
4073 else
4074 list_append_number(l, (varnumber_T)0);
4075 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4076 : (varnumber_T)0);
4077 list_append_number(l, (fp != NULL)
4078 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4079 : (varnumber_T)0);
4080 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
4081 (varnumber_T)0);
4082 if (getcurpos)
4083 {
4084 int save_set_curswant = curwin->w_set_curswant;
4085 colnr_T save_curswant = curwin->w_curswant;
4086 colnr_T save_virtcol = curwin->w_virtcol;
4087
4088 if (wp == curwin)
4089 update_curswant();
4090 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
4091 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
4092
4093 // Do not change "curswant", as it is unexpected that a get
4094 // function has a side effect.
4095 if (wp == curwin && save_set_curswant)
4096 {
4097 curwin->w_set_curswant = save_set_curswant;
4098 curwin->w_curswant = save_curswant;
4099 curwin->w_virtcol = save_virtcol;
4100 curwin->w_valid &= ~VALID_VIRTCOL;
4101 }
4102 }
4103 }
4104 else
4105 rettv->vval.v_number = FALSE;
4106}
4107
4108/*
4109 * "getcharpos()" function
4110 */
4111 static void
4112f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
4113{
4114 getpos_both(argvars, rettv, FALSE, TRUE);
4115}
4116
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004117/*
4118 * "getcharsearch()" function
4119 */
4120 static void
4121f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4122{
4123 if (rettv_dict_alloc(rettv) != FAIL)
4124 {
4125 dict_T *dict = rettv->vval.v_dict;
4126
Bram Moolenaare0be1672018-07-08 16:50:37 +02004127 dict_add_string(dict, "char", last_csearch());
4128 dict_add_number(dict, "forward", last_csearch_forward());
4129 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004130 }
4131}
4132
4133/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004134 * "getenv()" function
4135 */
4136 static void
4137f_getenv(typval_T *argvars, typval_T *rettv)
4138{
4139 int mustfree = FALSE;
4140 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4141
4142 if (p == NULL)
4143 {
4144 rettv->v_type = VAR_SPECIAL;
4145 rettv->vval.v_number = VVAL_NULL;
4146 return;
4147 }
4148 if (!mustfree)
4149 p = vim_strsave(p);
4150 rettv->vval.v_string = p;
4151 rettv->v_type = VAR_STRING;
4152}
4153
4154/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004155 * "getfontname()" function
4156 */
4157 static void
4158f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4159{
4160 rettv->v_type = VAR_STRING;
4161 rettv->vval.v_string = NULL;
4162#ifdef FEAT_GUI
4163 if (gui.in_use)
4164 {
4165 GuiFont font;
4166 char_u *name = NULL;
4167
4168 if (argvars[0].v_type == VAR_UNKNOWN)
4169 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004170 // Get the "Normal" font. Either the name saved by
4171 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004172 font = gui.norm_font;
4173 name = hl_get_font_name();
4174 }
4175 else
4176 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004177 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004178 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004179 return;
4180 font = gui_mch_get_font(name, FALSE);
4181 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004182 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004183 }
4184 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4185 if (argvars[0].v_type != VAR_UNKNOWN)
4186 gui_mch_free_font(font);
4187 }
4188#endif
4189}
4190
4191/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004192 * "getjumplist()" function
4193 */
4194 static void
4195f_getjumplist(typval_T *argvars, typval_T *rettv)
4196{
4197#ifdef FEAT_JUMPLIST
4198 win_T *wp;
4199 int i;
4200 list_T *l;
4201 dict_T *d;
4202#endif
4203
4204 if (rettv_list_alloc(rettv) != OK)
4205 return;
4206
4207#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004208 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004209 if (wp == NULL)
4210 return;
4211
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004212 cleanup_jumplist(wp, TRUE);
4213
Bram Moolenaar4f505882018-02-10 21:06:32 +01004214 l = list_alloc();
4215 if (l == NULL)
4216 return;
4217
4218 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4219 return;
4220 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4221
4222 for (i = 0; i < wp->w_jumplistlen; ++i)
4223 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004224 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4225 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004226 if ((d = dict_alloc()) == NULL)
4227 return;
4228 if (list_append_dict(l, d) == FAIL)
4229 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004230 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4231 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004232 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004233 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004234 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004235 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004236 }
4237#endif
4238}
4239
4240/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004241 * "getpid()" function
4242 */
4243 static void
4244f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4245{
4246 rettv->vval.v_number = mch_get_pid();
4247}
4248
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004249/*
4250 * "getcurpos()" function
4251 */
4252 static void
4253f_getcurpos(typval_T *argvars, typval_T *rettv)
4254{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004255 getpos_both(argvars, rettv, TRUE, FALSE);
4256}
4257
4258 static void
4259f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4260{
4261 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004262}
4263
4264/*
4265 * "getpos(string)" function
4266 */
4267 static void
4268f_getpos(typval_T *argvars, typval_T *rettv)
4269{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004270 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004271}
4272
4273/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004274 * "getreg()" function
4275 */
4276 static void
4277f_getreg(typval_T *argvars, typval_T *rettv)
4278{
4279 char_u *strregname;
4280 int regname;
4281 int arg2 = FALSE;
4282 int return_list = FALSE;
4283 int error = FALSE;
4284
4285 if (argvars[0].v_type != VAR_UNKNOWN)
4286 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004287 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004288 error = strregname == NULL;
4289 if (argvars[1].v_type != VAR_UNKNOWN)
4290 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004291 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004292 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004293 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004294 }
4295 }
4296 else
4297 strregname = get_vim_var_str(VV_REG);
4298
4299 if (error)
4300 return;
4301
4302 regname = (strregname == NULL ? '"' : *strregname);
4303 if (regname == 0)
4304 regname = '"';
4305
4306 if (return_list)
4307 {
4308 rettv->v_type = VAR_LIST;
4309 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4310 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4311 if (rettv->vval.v_list == NULL)
4312 (void)rettv_list_alloc(rettv);
4313 else
4314 ++rettv->vval.v_list->lv_refcount;
4315 }
4316 else
4317 {
4318 rettv->v_type = VAR_STRING;
4319 rettv->vval.v_string = get_reg_contents(regname,
4320 arg2 ? GREG_EXPR_SRC : 0);
4321 }
4322}
4323
4324/*
4325 * "getregtype()" function
4326 */
4327 static void
4328f_getregtype(typval_T *argvars, typval_T *rettv)
4329{
4330 char_u *strregname;
4331 int regname;
4332 char_u buf[NUMBUFLEN + 2];
4333 long reglen = 0;
4334
4335 if (argvars[0].v_type != VAR_UNKNOWN)
4336 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004337 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004338 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004339 {
4340 rettv->v_type = VAR_STRING;
4341 rettv->vval.v_string = NULL;
4342 return;
4343 }
4344 }
4345 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004346 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004347 strregname = get_vim_var_str(VV_REG);
4348
4349 regname = (strregname == NULL ? '"' : *strregname);
4350 if (regname == 0)
4351 regname = '"';
4352
4353 buf[0] = NUL;
4354 buf[1] = NUL;
4355 switch (get_reg_type(regname, &reglen))
4356 {
4357 case MLINE: buf[0] = 'V'; break;
4358 case MCHAR: buf[0] = 'v'; break;
4359 case MBLOCK:
4360 buf[0] = Ctrl_V;
4361 sprintf((char *)buf + 1, "%ld", reglen + 1);
4362 break;
4363 }
4364 rettv->v_type = VAR_STRING;
4365 rettv->vval.v_string = vim_strsave(buf);
4366}
4367
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004368/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004369 * "gettagstack()" function
4370 */
4371 static void
4372f_gettagstack(typval_T *argvars, typval_T *rettv)
4373{
4374 win_T *wp = curwin; // default is current window
4375
4376 if (rettv_dict_alloc(rettv) != OK)
4377 return;
4378
4379 if (argvars[0].v_type != VAR_UNKNOWN)
4380 {
4381 wp = find_win_by_nr_or_id(&argvars[0]);
4382 if (wp == NULL)
4383 return;
4384 }
4385
4386 get_tagstack(wp, rettv->vval.v_dict);
4387}
4388
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004389/*
4390 * "gettext()" function
4391 */
4392 static void
4393f_gettext(typval_T *argvars, typval_T *rettv)
4394{
4395 if (argvars[0].v_type != VAR_STRING
4396 || argvars[0].vval.v_string == NULL
4397 || *argvars[0].vval.v_string == NUL)
4398 {
4399 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4400 }
4401 else
4402 {
4403 rettv->v_type = VAR_STRING;
4404 rettv->vval.v_string = vim_strsave(
4405 (char_u *)_(argvars[0].vval.v_string));
4406 }
4407}
4408
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004409// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004410#include "version.h"
4411
4412/*
4413 * "has()" function
4414 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004415 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004416f_has(typval_T *argvars, typval_T *rettv)
4417{
4418 int i;
4419 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004420 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004421 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004422 typedef struct {
4423 char *name;
4424 short present;
4425 } has_item_T;
4426 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004427 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004428 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004429#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004430 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004431#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004432 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004433#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004434 },
4435 {"arp",
4436#if defined(AMIGA) && defined(FEAT_ARP)
4437 1
4438#else
4439 0
4440#endif
4441 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004442 {"haiku",
4443#ifdef __HAIKU__
4444 1
4445#else
4446 0
4447#endif
4448 },
4449 {"bsd",
4450#if defined(BSD) && !defined(MACOS_X)
4451 1
4452#else
4453 0
4454#endif
4455 },
4456 {"hpux",
4457#ifdef hpux
4458 1
4459#else
4460 0
4461#endif
4462 },
4463 {"linux",
4464#ifdef __linux__
4465 1
4466#else
4467 0
4468#endif
4469 },
4470 {"mac", // Mac OS X (and, once, Mac OS Classic)
4471#ifdef MACOS_X
4472 1
4473#else
4474 0
4475#endif
4476 },
4477 {"osx", // Mac OS X
4478#ifdef MACOS_X
4479 1
4480#else
4481 0
4482#endif
4483 },
4484 {"macunix", // Mac OS X, with the darwin feature
4485#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4486 1
4487#else
4488 0
4489#endif
4490 },
4491 {"osxdarwin", // synonym for macunix
4492#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4493 1
4494#else
4495 0
4496#endif
4497 },
4498 {"qnx",
4499#ifdef __QNX__
4500 1
4501#else
4502 0
4503#endif
4504 },
4505 {"sun",
4506#ifdef SUN_SYSTEM
4507 1
4508#else
4509 0
4510#endif
4511 },
4512 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004513#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004514 1
4515#else
4516 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004517#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004518 },
4519 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004520#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004521 1
4522#else
4523 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004524#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004525 },
4526 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004527#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004528 1
4529#else
4530 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004531#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004532 },
4533 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004534#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004535 1
4536#else
4537 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004538#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004539 },
4540 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004541#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004542 1
4543#else
4544 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004545#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004546 },
4547 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004548#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004549 1
4550#else
4551 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004552#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004553 },
4554 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004555#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004556 1
4557#else
4558 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004559#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004560 },
4561 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004562#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004563 1
4564#else
4565 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004566#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004567 },
4568 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004569#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004570 1
4571#else
4572 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004573#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004574 },
4575 {"autocmd", 1},
4576 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004577#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004578 1
4579#else
4580 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004581#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004582 },
4583 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004584#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004585 1
4586#else
4587 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004588#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004589 },
4590 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004591#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004592 1
4593#else
4594 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004595#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004596 },
4597 {"balloon_multiline",
4598#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4599 // MS-Windows requires runtime check, see below
4600 1
4601#else
4602 0
4603#endif
4604 },
4605 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004606#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004607 1
4608#else
4609 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004610#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004611 },
4612 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004613#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004614 1
4615#else
4616 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004617#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004618 },
4619 {"all_builtin_terms",
4620#if defined(ALL_BUILTIN_TCAPS)
4621 1
4622#else
4623 0
4624#endif
4625 },
4626 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004627#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004628 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004629 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004630 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004631#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004632 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004633#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004634 },
4635 {"byte_offset",
4636#ifdef FEAT_BYTEOFF
4637 1
4638#else
4639 0
4640#endif
4641 },
4642 {"channel",
4643#ifdef FEAT_JOB_CHANNEL
4644 1
4645#else
4646 0
4647#endif
4648 },
4649 {"cindent",
4650#ifdef FEAT_CINDENT
4651 1
4652#else
4653 0
4654#endif
4655 },
4656 {"clientserver",
4657#ifdef FEAT_CLIENTSERVER
4658 1
4659#else
4660 0
4661#endif
4662 },
4663 {"clipboard",
4664#ifdef FEAT_CLIPBOARD
4665 1
4666#else
4667 0
4668#endif
4669 },
4670 {"cmdline_compl", 1},
4671 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004672 {"cmdwin",
4673#ifdef FEAT_CMDWIN
4674 1
4675#else
4676 0
4677#endif
4678 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004679 {"comments", 1},
4680 {"conceal",
4681#ifdef FEAT_CONCEAL
4682 1
4683#else
4684 0
4685#endif
4686 },
4687 {"cryptv",
4688#ifdef FEAT_CRYPT
4689 1
4690#else
4691 0
4692#endif
4693 },
4694 {"crypt-blowfish",
4695#ifdef FEAT_CRYPT
4696 1
4697#else
4698 0
4699#endif
4700 },
4701 {"crypt-blowfish2",
4702#ifdef FEAT_CRYPT
4703 1
4704#else
4705 0
4706#endif
4707 },
4708 {"cscope",
4709#ifdef FEAT_CSCOPE
4710 1
4711#else
4712 0
4713#endif
4714 },
4715 {"cursorbind", 1},
4716 {"cursorshape",
4717#ifdef CURSOR_SHAPE
4718 1
4719#else
4720 0
4721#endif
4722 },
4723 {"debug",
4724#ifdef DEBUG
4725 1
4726#else
4727 0
4728#endif
4729 },
4730 {"dialog_con",
4731#ifdef FEAT_CON_DIALOG
4732 1
4733#else
4734 0
4735#endif
4736 },
4737 {"dialog_gui",
4738#ifdef FEAT_GUI_DIALOG
4739 1
4740#else
4741 0
4742#endif
4743 },
4744 {"diff",
4745#ifdef FEAT_DIFF
4746 1
4747#else
4748 0
4749#endif
4750 },
4751 {"digraphs",
4752#ifdef FEAT_DIGRAPHS
4753 1
4754#else
4755 0
4756#endif
4757 },
4758 {"directx",
4759#ifdef FEAT_DIRECTX
4760 1
4761#else
4762 0
4763#endif
4764 },
4765 {"dnd",
4766#ifdef FEAT_DND
4767 1
4768#else
4769 0
4770#endif
4771 },
4772 {"emacs_tags",
4773#ifdef FEAT_EMACS_TAGS
4774 1
4775#else
4776 0
4777#endif
4778 },
4779 {"eval", 1}, // always present, of course!
4780 {"ex_extra", 1}, // graduated feature
4781 {"extra_search",
4782#ifdef FEAT_SEARCH_EXTRA
4783 1
4784#else
4785 0
4786#endif
4787 },
4788 {"file_in_path",
4789#ifdef FEAT_SEARCHPATH
4790 1
4791#else
4792 0
4793#endif
4794 },
4795 {"filterpipe",
4796#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4797 1
4798#else
4799 0
4800#endif
4801 },
4802 {"find_in_path",
4803#ifdef FEAT_FIND_ID
4804 1
4805#else
4806 0
4807#endif
4808 },
4809 {"float",
4810#ifdef FEAT_FLOAT
4811 1
4812#else
4813 0
4814#endif
4815 },
4816 {"folding",
4817#ifdef FEAT_FOLDING
4818 1
4819#else
4820 0
4821#endif
4822 },
4823 {"footer",
4824#ifdef FEAT_FOOTER
4825 1
4826#else
4827 0
4828#endif
4829 },
4830 {"fork",
4831#if !defined(USE_SYSTEM) && defined(UNIX)
4832 1
4833#else
4834 0
4835#endif
4836 },
4837 {"gettext",
4838#ifdef FEAT_GETTEXT
4839 1
4840#else
4841 0
4842#endif
4843 },
4844 {"gui",
4845#ifdef FEAT_GUI
4846 1
4847#else
4848 0
4849#endif
4850 },
4851 {"gui_neXtaw",
4852#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4853 1
4854#else
4855 0
4856#endif
4857 },
4858 {"gui_athena",
4859#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4860 1
4861#else
4862 0
4863#endif
4864 },
4865 {"gui_gtk",
4866#ifdef FEAT_GUI_GTK
4867 1
4868#else
4869 0
4870#endif
4871 },
4872 {"gui_gtk2",
4873#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4874 1
4875#else
4876 0
4877#endif
4878 },
4879 {"gui_gtk3",
4880#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4881 1
4882#else
4883 0
4884#endif
4885 },
4886 {"gui_gnome",
4887#ifdef FEAT_GUI_GNOME
4888 1
4889#else
4890 0
4891#endif
4892 },
4893 {"gui_haiku",
4894#ifdef FEAT_GUI_HAIKU
4895 1
4896#else
4897 0
4898#endif
4899 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004900 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004901 {"gui_motif",
4902#ifdef FEAT_GUI_MOTIF
4903 1
4904#else
4905 0
4906#endif
4907 },
4908 {"gui_photon",
4909#ifdef FEAT_GUI_PHOTON
4910 1
4911#else
4912 0
4913#endif
4914 },
4915 {"gui_win32",
4916#ifdef FEAT_GUI_MSWIN
4917 1
4918#else
4919 0
4920#endif
4921 },
4922 {"iconv",
4923#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4924 1
4925#else
4926 0
4927#endif
4928 },
4929 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004930 {"ipv6",
4931#ifdef FEAT_IPV6
4932 1
4933#else
4934 0
4935#endif
4936 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004937 {"job",
4938#ifdef FEAT_JOB_CHANNEL
4939 1
4940#else
4941 0
4942#endif
4943 },
4944 {"jumplist",
4945#ifdef FEAT_JUMPLIST
4946 1
4947#else
4948 0
4949#endif
4950 },
4951 {"keymap",
4952#ifdef FEAT_KEYMAP
4953 1
4954#else
4955 0
4956#endif
4957 },
4958 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4959 {"langmap",
4960#ifdef FEAT_LANGMAP
4961 1
4962#else
4963 0
4964#endif
4965 },
4966 {"libcall",
4967#ifdef FEAT_LIBCALL
4968 1
4969#else
4970 0
4971#endif
4972 },
4973 {"linebreak",
4974#ifdef FEAT_LINEBREAK
4975 1
4976#else
4977 0
4978#endif
4979 },
4980 {"lispindent",
4981#ifdef FEAT_LISP
4982 1
4983#else
4984 0
4985#endif
4986 },
4987 {"listcmds", 1},
4988 {"localmap", 1},
4989 {"lua",
4990#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4991 1
4992#else
4993 0
4994#endif
4995 },
4996 {"menu",
4997#ifdef FEAT_MENU
4998 1
4999#else
5000 0
5001#endif
5002 },
5003 {"mksession",
5004#ifdef FEAT_SESSION
5005 1
5006#else
5007 0
5008#endif
5009 },
5010 {"modify_fname", 1},
5011 {"mouse", 1},
5012 {"mouseshape",
5013#ifdef FEAT_MOUSESHAPE
5014 1
5015#else
5016 0
5017#endif
5018 },
5019 {"mouse_dec",
5020#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
5021 1
5022#else
5023 0
5024#endif
5025 },
5026 {"mouse_gpm",
5027#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
5028 1
5029#else
5030 0
5031#endif
5032 },
5033 {"mouse_jsbterm",
5034#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
5035 1
5036#else
5037 0
5038#endif
5039 },
5040 {"mouse_netterm",
5041#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
5042 1
5043#else
5044 0
5045#endif
5046 },
5047 {"mouse_pterm",
5048#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
5049 1
5050#else
5051 0
5052#endif
5053 },
5054 {"mouse_sgr",
5055#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5056 1
5057#else
5058 0
5059#endif
5060 },
5061 {"mouse_sysmouse",
5062#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
5063 1
5064#else
5065 0
5066#endif
5067 },
5068 {"mouse_urxvt",
5069#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
5070 1
5071#else
5072 0
5073#endif
5074 },
5075 {"mouse_xterm",
5076#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5077 1
5078#else
5079 0
5080#endif
5081 },
5082 {"multi_byte", 1},
5083 {"multi_byte_ime",
5084#ifdef FEAT_MBYTE_IME
5085 1
5086#else
5087 0
5088#endif
5089 },
5090 {"multi_lang",
5091#ifdef FEAT_MULTI_LANG
5092 1
5093#else
5094 0
5095#endif
5096 },
5097 {"mzscheme",
5098#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
5099 1
5100#else
5101 0
5102#endif
5103 },
5104 {"num64", 1},
5105 {"ole",
5106#ifdef FEAT_OLE
5107 1
5108#else
5109 0
5110#endif
5111 },
5112 {"packages",
5113#ifdef FEAT_EVAL
5114 1
5115#else
5116 0
5117#endif
5118 },
5119 {"path_extra",
5120#ifdef FEAT_PATH_EXTRA
5121 1
5122#else
5123 0
5124#endif
5125 },
5126 {"perl",
5127#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
5128 1
5129#else
5130 0
5131#endif
5132 },
5133 {"persistent_undo",
5134#ifdef FEAT_PERSISTENT_UNDO
5135 1
5136#else
5137 0
5138#endif
5139 },
5140 {"python_compiled",
5141#if defined(FEAT_PYTHON)
5142 1
5143#else
5144 0
5145#endif
5146 },
5147 {"python_dynamic",
5148#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
5149 1
5150#else
5151 0
5152#endif
5153 },
5154 {"python",
5155#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
5156 1
5157#else
5158 0
5159#endif
5160 },
5161 {"pythonx",
5162#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
5163 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
5164 1
5165#else
5166 0
5167#endif
5168 },
5169 {"python3_compiled",
5170#if defined(FEAT_PYTHON3)
5171 1
5172#else
5173 0
5174#endif
5175 },
5176 {"python3_dynamic",
5177#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
5178 1
5179#else
5180 0
5181#endif
5182 },
5183 {"python3",
5184#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
5185 1
5186#else
5187 0
5188#endif
5189 },
5190 {"popupwin",
5191#ifdef FEAT_PROP_POPUP
5192 1
5193#else
5194 0
5195#endif
5196 },
5197 {"postscript",
5198#ifdef FEAT_POSTSCRIPT
5199 1
5200#else
5201 0
5202#endif
5203 },
5204 {"printer",
5205#ifdef FEAT_PRINTER
5206 1
5207#else
5208 0
5209#endif
5210 },
5211 {"profile",
5212#ifdef FEAT_PROFILE
5213 1
5214#else
5215 0
5216#endif
5217 },
5218 {"reltime",
5219#ifdef FEAT_RELTIME
5220 1
5221#else
5222 0
5223#endif
5224 },
5225 {"quickfix",
5226#ifdef FEAT_QUICKFIX
5227 1
5228#else
5229 0
5230#endif
5231 },
5232 {"rightleft",
5233#ifdef FEAT_RIGHTLEFT
5234 1
5235#else
5236 0
5237#endif
5238 },
5239 {"ruby",
5240#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5241 1
5242#else
5243 0
5244#endif
5245 },
5246 {"scrollbind", 1},
5247 {"showcmd",
5248#ifdef FEAT_CMDL_INFO
5249 1
5250#else
5251 0
5252#endif
5253 },
5254 {"cmdline_info",
5255#ifdef FEAT_CMDL_INFO
5256 1
5257#else
5258 0
5259#endif
5260 },
5261 {"signs",
5262#ifdef FEAT_SIGNS
5263 1
5264#else
5265 0
5266#endif
5267 },
5268 {"smartindent",
5269#ifdef FEAT_SMARTINDENT
5270 1
5271#else
5272 0
5273#endif
5274 },
5275 {"startuptime",
5276#ifdef STARTUPTIME
5277 1
5278#else
5279 0
5280#endif
5281 },
5282 {"statusline",
5283#ifdef FEAT_STL_OPT
5284 1
5285#else
5286 0
5287#endif
5288 },
5289 {"netbeans_intg",
5290#ifdef FEAT_NETBEANS_INTG
5291 1
5292#else
5293 0
5294#endif
5295 },
5296 {"sound",
5297#ifdef FEAT_SOUND
5298 1
5299#else
5300 0
5301#endif
5302 },
5303 {"spell",
5304#ifdef FEAT_SPELL
5305 1
5306#else
5307 0
5308#endif
5309 },
5310 {"syntax",
5311#ifdef FEAT_SYN_HL
5312 1
5313#else
5314 0
5315#endif
5316 },
5317 {"system",
5318#if defined(USE_SYSTEM) || !defined(UNIX)
5319 1
5320#else
5321 0
5322#endif
5323 },
5324 {"tag_binary",
5325#ifdef FEAT_TAG_BINS
5326 1
5327#else
5328 0
5329#endif
5330 },
5331 {"tcl",
5332#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5333 1
5334#else
5335 0
5336#endif
5337 },
5338 {"termguicolors",
5339#ifdef FEAT_TERMGUICOLORS
5340 1
5341#else
5342 0
5343#endif
5344 },
5345 {"terminal",
5346#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5347 1
5348#else
5349 0
5350#endif
5351 },
5352 {"terminfo",
5353#ifdef TERMINFO
5354 1
5355#else
5356 0
5357#endif
5358 },
5359 {"termresponse",
5360#ifdef FEAT_TERMRESPONSE
5361 1
5362#else
5363 0
5364#endif
5365 },
5366 {"textobjects",
5367#ifdef FEAT_TEXTOBJ
5368 1
5369#else
5370 0
5371#endif
5372 },
5373 {"textprop",
5374#ifdef FEAT_PROP_POPUP
5375 1
5376#else
5377 0
5378#endif
5379 },
5380 {"tgetent",
5381#ifdef HAVE_TGETENT
5382 1
5383#else
5384 0
5385#endif
5386 },
5387 {"timers",
5388#ifdef FEAT_TIMERS
5389 1
5390#else
5391 0
5392#endif
5393 },
5394 {"title",
5395#ifdef FEAT_TITLE
5396 1
5397#else
5398 0
5399#endif
5400 },
5401 {"toolbar",
5402#ifdef FEAT_TOOLBAR
5403 1
5404#else
5405 0
5406#endif
5407 },
5408 {"unnamedplus",
5409#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5410 1
5411#else
5412 0
5413#endif
5414 },
5415 {"user-commands", 1}, // was accidentally included in 5.4
5416 {"user_commands", 1},
5417 {"vartabs",
5418#ifdef FEAT_VARTABS
5419 1
5420#else
5421 0
5422#endif
5423 },
5424 {"vertsplit", 1},
5425 {"viminfo",
5426#ifdef FEAT_VIMINFO
5427 1
5428#else
5429 0
5430#endif
5431 },
5432 {"vimscript-1", 1},
5433 {"vimscript-2", 1},
5434 {"vimscript-3", 1},
5435 {"vimscript-4", 1},
5436 {"virtualedit", 1},
5437 {"visual", 1},
5438 {"visualextra", 1},
5439 {"vreplace", 1},
5440 {"vtp",
5441#ifdef FEAT_VTP
5442 1
5443#else
5444 0
5445#endif
5446 },
5447 {"wildignore",
5448#ifdef FEAT_WILDIGN
5449 1
5450#else
5451 0
5452#endif
5453 },
5454 {"wildmenu",
5455#ifdef FEAT_WILDMENU
5456 1
5457#else
5458 0
5459#endif
5460 },
5461 {"windows", 1},
5462 {"winaltkeys",
5463#ifdef FEAT_WAK
5464 1
5465#else
5466 0
5467#endif
5468 },
5469 {"writebackup",
5470#ifdef FEAT_WRITEBACKUP
5471 1
5472#else
5473 0
5474#endif
5475 },
5476 {"xim",
5477#ifdef FEAT_XIM
5478 1
5479#else
5480 0
5481#endif
5482 },
5483 {"xfontset",
5484#ifdef FEAT_XFONTSET
5485 1
5486#else
5487 0
5488#endif
5489 },
5490 {"xpm",
5491#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5492 1
5493#else
5494 0
5495#endif
5496 },
5497 {"xpm_w32", // for backward compatibility
5498#ifdef FEAT_XPM_W32
5499 1
5500#else
5501 0
5502#endif
5503 },
5504 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005505#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005506 1
5507#else
5508 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005509#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005510 },
5511 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005512#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005513 1
5514#else
5515 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005516#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005517 },
5518 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005519#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005520 1
5521#else
5522 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005523#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005524 },
5525 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005526#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005527 1
5528#else
5529 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005530#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005531 },
5532 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005533#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005534 1
5535#else
5536 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005537#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005538 },
5539 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005540 };
5541
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005542 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005543 for (i = 0; has_list[i].name != NULL; ++i)
5544 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005545 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005546 x = TRUE;
5547 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005548 break;
5549 }
5550
Bram Moolenaar79296512020-03-22 16:17:14 +01005551 // features also in has_list[] but sometimes enabled at runtime
5552 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005553 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005554 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005555 {
5556 // intentionally empty
5557 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005558#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005559 else if (STRICMP(name, "balloon_multiline") == 0)
5560 n = multiline_balloon_available();
5561#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005562#ifdef VIMDLL
5563 else if (STRICMP(name, "filterpipe") == 0)
5564 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005565#endif
5566#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5567 else if (STRICMP(name, "iconv") == 0)
5568 n = iconv_enabled(FALSE);
5569#endif
5570#ifdef DYNAMIC_LUA
5571 else if (STRICMP(name, "lua") == 0)
5572 n = lua_enabled(FALSE);
5573#endif
5574#ifdef DYNAMIC_MZSCHEME
5575 else if (STRICMP(name, "mzscheme") == 0)
5576 n = mzscheme_enabled(FALSE);
5577#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005578#ifdef DYNAMIC_PERL
5579 else if (STRICMP(name, "perl") == 0)
5580 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005581#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005582#ifdef DYNAMIC_PYTHON
5583 else if (STRICMP(name, "python") == 0)
5584 n = python_enabled(FALSE);
5585#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005586#ifdef DYNAMIC_PYTHON3
5587 else if (STRICMP(name, "python3") == 0)
5588 n = python3_enabled(FALSE);
5589#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005590#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5591 else if (STRICMP(name, "pythonx") == 0)
5592 {
5593# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5594 if (p_pyx == 0)
5595 n = python3_enabled(FALSE) || python_enabled(FALSE);
5596 else if (p_pyx == 3)
5597 n = python3_enabled(FALSE);
5598 else if (p_pyx == 2)
5599 n = python_enabled(FALSE);
5600# elif defined(DYNAMIC_PYTHON)
5601 n = python_enabled(FALSE);
5602# elif defined(DYNAMIC_PYTHON3)
5603 n = python3_enabled(FALSE);
5604# endif
5605 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005606#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005607#ifdef DYNAMIC_RUBY
5608 else if (STRICMP(name, "ruby") == 0)
5609 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005610#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005611#ifdef DYNAMIC_TCL
5612 else if (STRICMP(name, "tcl") == 0)
5613 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005614#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005615#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005616 else if (STRICMP(name, "terminal") == 0)
5617 n = terminal_enabled();
5618#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005619 }
5620
Bram Moolenaar79296512020-03-22 16:17:14 +01005621 // features not in has_list[]
5622 if (x == FALSE)
5623 {
5624 if (STRNICMP(name, "patch", 5) == 0)
5625 {
5626 x = TRUE;
5627 if (name[5] == '-'
5628 && STRLEN(name) >= 11
5629 && vim_isdigit(name[6])
5630 && vim_isdigit(name[8])
5631 && vim_isdigit(name[10]))
5632 {
5633 int major = atoi((char *)name + 6);
5634 int minor = atoi((char *)name + 8);
5635
5636 // Expect "patch-9.9.01234".
5637 n = (major < VIM_VERSION_MAJOR
5638 || (major == VIM_VERSION_MAJOR
5639 && (minor < VIM_VERSION_MINOR
5640 || (minor == VIM_VERSION_MINOR
5641 && has_patch(atoi((char *)name + 10))))));
5642 }
5643 else
5644 n = has_patch(atoi((char *)name + 5));
5645 }
5646 else if (STRICMP(name, "vim_starting") == 0)
5647 {
5648 x = TRUE;
5649 n = (starting != 0);
5650 }
5651 else if (STRICMP(name, "ttyin") == 0)
5652 {
5653 x = TRUE;
5654 n = mch_input_isatty();
5655 }
5656 else if (STRICMP(name, "ttyout") == 0)
5657 {
5658 x = TRUE;
5659 n = stdout_isatty;
5660 }
5661 else if (STRICMP(name, "multi_byte_encoding") == 0)
5662 {
5663 x = TRUE;
5664 n = has_mbyte;
5665 }
5666 else if (STRICMP(name, "gui_running") == 0)
5667 {
5668 x = TRUE;
5669#ifdef FEAT_GUI
5670 n = (gui.in_use || gui.starting);
5671#endif
5672 }
5673 else if (STRICMP(name, "browse") == 0)
5674 {
5675 x = TRUE;
5676#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5677 n = gui.in_use; // gui_mch_browse() works when GUI is running
5678#endif
5679 }
5680 else if (STRICMP(name, "syntax_items") == 0)
5681 {
5682 x = TRUE;
5683#ifdef FEAT_SYN_HL
5684 n = syntax_present(curwin);
5685#endif
5686 }
5687 else if (STRICMP(name, "vcon") == 0)
5688 {
5689 x = TRUE;
5690#ifdef FEAT_VTP
5691 n = is_term_win32() && has_vtp_working();
5692#endif
5693 }
5694 else if (STRICMP(name, "netbeans_enabled") == 0)
5695 {
5696 x = TRUE;
5697#ifdef FEAT_NETBEANS_INTG
5698 n = netbeans_active();
5699#endif
5700 }
5701 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5702 {
5703 x = TRUE;
5704#ifdef FEAT_MOUSE_GPM
5705 n = gpm_enabled();
5706#endif
5707 }
5708 else if (STRICMP(name, "conpty") == 0)
5709 {
5710 x = TRUE;
5711#if defined(FEAT_TERMINAL) && defined(MSWIN)
5712 n = use_conpty();
5713#endif
5714 }
5715 else if (STRICMP(name, "clipboard_working") == 0)
5716 {
5717 x = TRUE;
5718#ifdef FEAT_CLIPBOARD
5719 n = clip_star.available;
5720#endif
5721 }
5722 }
5723
Bram Moolenaar04637e22020-09-05 18:45:29 +02005724 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005725 // return whether feature could ever be enabled
5726 rettv->vval.v_number = x;
5727 else
5728 // return whether feature is enabled
5729 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005730}
5731
5732/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005733 * Return TRUE if "feature" can change later.
5734 * Also when checking for the feature has side effects, such as loading a DLL.
5735 */
5736 int
5737dynamic_feature(char_u *feature)
5738{
5739 return (feature == NULL
5740#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5741 || STRICMP(feature, "balloon_multiline") == 0
5742#endif
5743#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5744 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5745#endif
5746#ifdef VIMDLL
5747 || STRICMP(feature, "filterpipe") == 0
5748#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005749#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005750 // this can only change on Unix where the ":gui" command could be
5751 // used.
5752 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5753#endif
5754#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5755 || STRICMP(feature, "iconv") == 0
5756#endif
5757#ifdef DYNAMIC_LUA
5758 || STRICMP(feature, "lua") == 0
5759#endif
5760#ifdef FEAT_MOUSE_GPM
5761 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5762#endif
5763#ifdef DYNAMIC_MZSCHEME
5764 || STRICMP(feature, "mzscheme") == 0
5765#endif
5766#ifdef FEAT_NETBEANS_INTG
5767 || STRICMP(feature, "netbeans_enabled") == 0
5768#endif
5769#ifdef DYNAMIC_PERL
5770 || STRICMP(feature, "perl") == 0
5771#endif
5772#ifdef DYNAMIC_PYTHON
5773 || STRICMP(feature, "python") == 0
5774#endif
5775#ifdef DYNAMIC_PYTHON3
5776 || STRICMP(feature, "python3") == 0
5777#endif
5778#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5779 || STRICMP(feature, "pythonx") == 0
5780#endif
5781#ifdef DYNAMIC_RUBY
5782 || STRICMP(feature, "ruby") == 0
5783#endif
5784#ifdef FEAT_SYN_HL
5785 || STRICMP(feature, "syntax_items") == 0
5786#endif
5787#ifdef DYNAMIC_TCL
5788 || STRICMP(feature, "tcl") == 0
5789#endif
5790 // once "starting" is zero it will stay that way
5791 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5792 || STRICMP(feature, "multi_byte_encoding") == 0
5793#if defined(FEAT_TERMINAL) && defined(MSWIN)
5794 || STRICMP(feature, "conpty") == 0
5795#endif
5796 );
5797}
5798
5799/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005800 * "haslocaldir()" function
5801 */
5802 static void
5803f_haslocaldir(typval_T *argvars, typval_T *rettv)
5804{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005805 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005806 win_T *wp = NULL;
5807
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005808 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5809
5810 // Check for window-local and tab-local directories
5811 if (wp != NULL && wp->w_localdir != NULL)
5812 rettv->vval.v_number = 1;
5813 else if (tp != NULL && tp->tp_localdir != NULL)
5814 rettv->vval.v_number = 2;
5815 else
5816 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005817}
5818
5819/*
5820 * "hasmapto()" function
5821 */
5822 static void
5823f_hasmapto(typval_T *argvars, typval_T *rettv)
5824{
5825 char_u *name;
5826 char_u *mode;
5827 char_u buf[NUMBUFLEN];
5828 int abbr = FALSE;
5829
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005830 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005831 if (argvars[1].v_type == VAR_UNKNOWN)
5832 mode = (char_u *)"nvo";
5833 else
5834 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005835 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005836 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005837 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005838 }
5839
5840 if (map_to_exists(name, mode, abbr))
5841 rettv->vval.v_number = TRUE;
5842 else
5843 rettv->vval.v_number = FALSE;
5844}
5845
5846/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005847 * "highlightID(name)" function
5848 */
5849 static void
5850f_hlID(typval_T *argvars, typval_T *rettv)
5851{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005852 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005853}
5854
5855/*
5856 * "highlight_exists()" function
5857 */
5858 static void
5859f_hlexists(typval_T *argvars, typval_T *rettv)
5860{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005861 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005862}
5863
5864/*
5865 * "hostname()" function
5866 */
5867 static void
5868f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5869{
5870 char_u hostname[256];
5871
5872 mch_get_host_name(hostname, 256);
5873 rettv->v_type = VAR_STRING;
5874 rettv->vval.v_string = vim_strsave(hostname);
5875}
5876
5877/*
5878 * iconv() function
5879 */
5880 static void
5881f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5882{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005883 char_u buf1[NUMBUFLEN];
5884 char_u buf2[NUMBUFLEN];
5885 char_u *from, *to, *str;
5886 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005887
5888 rettv->v_type = VAR_STRING;
5889 rettv->vval.v_string = NULL;
5890
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005891 str = tv_get_string(&argvars[0]);
5892 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5893 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005894 vimconv.vc_type = CONV_NONE;
5895 convert_setup(&vimconv, from, to);
5896
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005897 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005898 if (vimconv.vc_type == CONV_NONE)
5899 rettv->vval.v_string = vim_strsave(str);
5900 else
5901 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5902
5903 convert_setup(&vimconv, NULL, NULL);
5904 vim_free(from);
5905 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005906}
5907
5908/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005909 * "index()" function
5910 */
5911 static void
5912f_index(typval_T *argvars, typval_T *rettv)
5913{
5914 list_T *l;
5915 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005916 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005917 long idx = 0;
5918 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005919 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005920
5921 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005922 if (argvars[0].v_type == VAR_BLOB)
5923 {
5924 typval_T tv;
5925 int start = 0;
5926
5927 if (argvars[2].v_type != VAR_UNKNOWN)
5928 {
5929 start = tv_get_number_chk(&argvars[2], &error);
5930 if (error)
5931 return;
5932 }
5933 b = argvars[0].vval.v_blob;
5934 if (b == NULL)
5935 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005936 if (start < 0)
5937 {
5938 start = blob_len(b) + start;
5939 if (start < 0)
5940 start = 0;
5941 }
5942
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005943 for (idx = start; idx < blob_len(b); ++idx)
5944 {
5945 tv.v_type = VAR_NUMBER;
5946 tv.vval.v_number = blob_get(b, idx);
5947 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5948 {
5949 rettv->vval.v_number = idx;
5950 return;
5951 }
5952 }
5953 return;
5954 }
5955 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005956 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005957 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005958 return;
5959 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005960
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005961 l = argvars[0].vval.v_list;
5962 if (l != NULL)
5963 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005964 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005965 item = l->lv_first;
5966 if (argvars[2].v_type != VAR_UNKNOWN)
5967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005968 // Start at specified item. Use the cached index that list_find()
5969 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005970 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005971 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005972 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005973 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005974 if (error)
5975 item = NULL;
5976 }
5977
5978 for ( ; item != NULL; item = item->li_next, ++idx)
5979 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5980 {
5981 rettv->vval.v_number = idx;
5982 break;
5983 }
5984 }
5985}
5986
5987static int inputsecret_flag = 0;
5988
5989/*
5990 * "input()" function
5991 * Also handles inputsecret() when inputsecret is set.
5992 */
5993 static void
5994f_input(typval_T *argvars, typval_T *rettv)
5995{
5996 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5997}
5998
5999/*
6000 * "inputdialog()" function
6001 */
6002 static void
6003f_inputdialog(typval_T *argvars, typval_T *rettv)
6004{
6005#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006006 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006007 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6008 {
6009 char_u *message;
6010 char_u buf[NUMBUFLEN];
6011 char_u *defstr = (char_u *)"";
6012
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006013 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006014 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006015 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006016 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6017 else
6018 IObuff[0] = NUL;
6019 if (message != NULL && defstr != NULL
6020 && do_dialog(VIM_QUESTION, NULL, message,
6021 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6022 rettv->vval.v_string = vim_strsave(IObuff);
6023 else
6024 {
6025 if (message != NULL && defstr != NULL
6026 && argvars[1].v_type != VAR_UNKNOWN
6027 && argvars[2].v_type != VAR_UNKNOWN)
6028 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006029 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006030 else
6031 rettv->vval.v_string = NULL;
6032 }
6033 rettv->v_type = VAR_STRING;
6034 }
6035 else
6036#endif
6037 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6038}
6039
6040/*
6041 * "inputlist()" function
6042 */
6043 static void
6044f_inputlist(typval_T *argvars, typval_T *rettv)
6045{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006046 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006047 listitem_T *li;
6048 int selected;
6049 int mouse_used;
6050
6051#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006052 // While starting up, there is no place to enter text. When running tests
6053 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006054 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006055 return;
6056#endif
6057 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6058 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006059 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006060 return;
6061 }
6062
6063 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006064 msg_row = Rows - 1; // for when 'cmdheight' > 1
6065 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006066 msg_scroll = TRUE;
6067 msg_clr_eos();
6068
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006069 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006070 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006071 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006072 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006073 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006074 msg_putchar('\n');
6075 }
6076
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006077 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006078 selected = prompt_for_number(&mouse_used);
6079 if (mouse_used)
6080 selected -= lines_left;
6081
6082 rettv->vval.v_number = selected;
6083}
6084
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006085static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6086
6087/*
6088 * "inputrestore()" function
6089 */
6090 static void
6091f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6092{
6093 if (ga_userinput.ga_len > 0)
6094 {
6095 --ga_userinput.ga_len;
6096 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6097 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006098 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006099 }
6100 else if (p_verbose > 1)
6101 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006102 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006103 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006104 }
6105}
6106
6107/*
6108 * "inputsave()" function
6109 */
6110 static void
6111f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6112{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006113 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006114 if (ga_grow(&ga_userinput, 1) == OK)
6115 {
6116 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6117 + ga_userinput.ga_len);
6118 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006119 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006120 }
6121 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006122 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006123}
6124
6125/*
6126 * "inputsecret()" function
6127 */
6128 static void
6129f_inputsecret(typval_T *argvars, typval_T *rettv)
6130{
6131 ++cmdline_star;
6132 ++inputsecret_flag;
6133 f_input(argvars, rettv);
6134 --cmdline_star;
6135 --inputsecret_flag;
6136}
6137
6138/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01006139 * "interrupt()" function
6140 */
6141 static void
6142f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6143{
6144 got_int = TRUE;
6145}
6146
6147/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006148 * "invert(expr)" function
6149 */
6150 static void
6151f_invert(typval_T *argvars, typval_T *rettv)
6152{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006153 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006154}
6155
6156/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006157 * "islocked()" function
6158 */
6159 static void
6160f_islocked(typval_T *argvars, typval_T *rettv)
6161{
6162 lval_T lv;
6163 char_u *end;
6164 dictitem_T *di;
6165
6166 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006167 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006168 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006169 if (end != NULL && lv.ll_name != NULL)
6170 {
6171 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02006172 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006173 else
6174 {
6175 if (lv.ll_tv == NULL)
6176 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006177 di = find_var(lv.ll_name, NULL, TRUE);
6178 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006179 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006180 // Consider a variable locked when:
6181 // 1. the variable itself is locked
6182 // 2. the value of the variable is locked.
6183 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01006184 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6185 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006186 }
6187 }
6188 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006189 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006190 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006191 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006192 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006193 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006194 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6195 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006196 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006197 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6198 }
6199 }
6200
6201 clear_lval(&lv);
6202}
6203
6204#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6205/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006206 * "isinf()" function
6207 */
6208 static void
6209f_isinf(typval_T *argvars, typval_T *rettv)
6210{
6211 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6212 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6213}
6214
6215/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006216 * "isnan()" function
6217 */
6218 static void
6219f_isnan(typval_T *argvars, typval_T *rettv)
6220{
6221 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6222 && isnan(argvars[0].vval.v_float);
6223}
6224#endif
6225
6226/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006227 * "last_buffer_nr()" function.
6228 */
6229 static void
6230f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6231{
6232 int n = 0;
6233 buf_T *buf;
6234
Bram Moolenaar29323592016-07-24 22:04:11 +02006235 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006236 if (n < buf->b_fnum)
6237 n = buf->b_fnum;
6238
6239 rettv->vval.v_number = n;
6240}
6241
6242/*
6243 * "len()" function
6244 */
6245 static void
6246f_len(typval_T *argvars, typval_T *rettv)
6247{
6248 switch (argvars[0].v_type)
6249 {
6250 case VAR_STRING:
6251 case VAR_NUMBER:
6252 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006253 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006254 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006255 case VAR_BLOB:
6256 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6257 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006258 case VAR_LIST:
6259 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6260 break;
6261 case VAR_DICT:
6262 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6263 break;
6264 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006265 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006266 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006267 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006268 case VAR_SPECIAL:
6269 case VAR_FLOAT:
6270 case VAR_FUNC:
6271 case VAR_PARTIAL:
6272 case VAR_JOB:
6273 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006274 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006275 break;
6276 }
6277}
6278
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006279 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006280libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006281{
6282#ifdef FEAT_LIBCALL
6283 char_u *string_in;
6284 char_u **string_result;
6285 int nr_result;
6286#endif
6287
6288 rettv->v_type = type;
6289 if (type != VAR_NUMBER)
6290 rettv->vval.v_string = NULL;
6291
6292 if (check_restricted() || check_secure())
6293 return;
6294
6295#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006296 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006297 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6298 {
6299 string_in = NULL;
6300 if (argvars[2].v_type == VAR_STRING)
6301 string_in = argvars[2].vval.v_string;
6302 if (type == VAR_NUMBER)
6303 string_result = NULL;
6304 else
6305 string_result = &rettv->vval.v_string;
6306 if (mch_libcall(argvars[0].vval.v_string,
6307 argvars[1].vval.v_string,
6308 string_in,
6309 argvars[2].vval.v_number,
6310 string_result,
6311 &nr_result) == OK
6312 && type == VAR_NUMBER)
6313 rettv->vval.v_number = nr_result;
6314 }
6315#endif
6316}
6317
6318/*
6319 * "libcall()" function
6320 */
6321 static void
6322f_libcall(typval_T *argvars, typval_T *rettv)
6323{
6324 libcall_common(argvars, rettv, VAR_STRING);
6325}
6326
6327/*
6328 * "libcallnr()" function
6329 */
6330 static void
6331f_libcallnr(typval_T *argvars, typval_T *rettv)
6332{
6333 libcall_common(argvars, rettv, VAR_NUMBER);
6334}
6335
6336/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006337 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006338 */
6339 static void
6340f_line(typval_T *argvars, typval_T *rettv)
6341{
6342 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006343 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006344 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006345 int id;
6346 tabpage_T *tp;
6347 win_T *wp;
6348 win_T *save_curwin;
6349 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006350
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006351 if (argvars[1].v_type != VAR_UNKNOWN)
6352 {
6353 // use window specified in the second argument
6354 id = (int)tv_get_number(&argvars[1]);
6355 wp = win_id2wp_tp(id, &tp);
6356 if (wp != NULL && tp != NULL)
6357 {
6358 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6359 == OK)
6360 {
6361 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006362 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006363 }
6364 restore_win_noblock(save_curwin, save_curtab, TRUE);
6365 }
6366 }
6367 else
6368 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006369 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006370
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006371 if (fp != NULL)
6372 lnum = fp->lnum;
6373 rettv->vval.v_number = lnum;
6374}
6375
6376/*
6377 * "line2byte(lnum)" function
6378 */
6379 static void
6380f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6381{
6382#ifndef FEAT_BYTEOFF
6383 rettv->vval.v_number = -1;
6384#else
6385 linenr_T lnum;
6386
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006387 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006388 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6389 rettv->vval.v_number = -1;
6390 else
6391 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6392 if (rettv->vval.v_number >= 0)
6393 ++rettv->vval.v_number;
6394#endif
6395}
6396
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006397#ifdef FEAT_FLOAT
6398/*
6399 * "log()" function
6400 */
6401 static void
6402f_log(typval_T *argvars, typval_T *rettv)
6403{
6404 float_T f = 0.0;
6405
6406 rettv->v_type = VAR_FLOAT;
6407 if (get_float_arg(argvars, &f) == OK)
6408 rettv->vval.v_float = log(f);
6409 else
6410 rettv->vval.v_float = 0.0;
6411}
6412
6413/*
6414 * "log10()" function
6415 */
6416 static void
6417f_log10(typval_T *argvars, typval_T *rettv)
6418{
6419 float_T f = 0.0;
6420
6421 rettv->v_type = VAR_FLOAT;
6422 if (get_float_arg(argvars, &f) == OK)
6423 rettv->vval.v_float = log10(f);
6424 else
6425 rettv->vval.v_float = 0.0;
6426}
6427#endif
6428
6429#ifdef FEAT_LUA
6430/*
6431 * "luaeval()" function
6432 */
6433 static void
6434f_luaeval(typval_T *argvars, typval_T *rettv)
6435{
6436 char_u *str;
6437 char_u buf[NUMBUFLEN];
6438
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006439 if (check_restricted() || check_secure())
6440 return;
6441
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006442 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006443 do_luaeval(str, argvars + 1, rettv);
6444}
6445#endif
6446
6447/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006448 * "maparg()" function
6449 */
6450 static void
6451f_maparg(typval_T *argvars, typval_T *rettv)
6452{
6453 get_maparg(argvars, rettv, TRUE);
6454}
6455
6456/*
6457 * "mapcheck()" function
6458 */
6459 static void
6460f_mapcheck(typval_T *argvars, typval_T *rettv)
6461{
6462 get_maparg(argvars, rettv, FALSE);
6463}
6464
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006465typedef enum
6466{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006467 MATCH_END, // matchend()
6468 MATCH_MATCH, // match()
6469 MATCH_STR, // matchstr()
6470 MATCH_LIST, // matchlist()
6471 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006472} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006473
6474 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006475find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006476{
6477 char_u *str = NULL;
6478 long len = 0;
6479 char_u *expr = NULL;
6480 char_u *pat;
6481 regmatch_T regmatch;
6482 char_u patbuf[NUMBUFLEN];
6483 char_u strbuf[NUMBUFLEN];
6484 char_u *save_cpo;
6485 long start = 0;
6486 long nth = 1;
6487 colnr_T startcol = 0;
6488 int match = 0;
6489 list_T *l = NULL;
6490 listitem_T *li = NULL;
6491 long idx = 0;
6492 char_u *tofree = NULL;
6493
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006494 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006495 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006496 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006497
6498 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006499 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006500 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006501 // type MATCH_LIST: return empty list when there are no matches.
6502 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006503 if (rettv_list_alloc(rettv) == FAIL)
6504 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006505 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506 && (list_append_string(rettv->vval.v_list,
6507 (char_u *)"", 0) == FAIL
6508 || list_append_number(rettv->vval.v_list,
6509 (varnumber_T)-1) == FAIL
6510 || list_append_number(rettv->vval.v_list,
6511 (varnumber_T)-1) == FAIL
6512 || list_append_number(rettv->vval.v_list,
6513 (varnumber_T)-1) == FAIL))
6514 {
6515 list_free(rettv->vval.v_list);
6516 rettv->vval.v_list = NULL;
6517 goto theend;
6518 }
6519 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006520 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006521 {
6522 rettv->v_type = VAR_STRING;
6523 rettv->vval.v_string = NULL;
6524 }
6525
6526 if (argvars[0].v_type == VAR_LIST)
6527 {
6528 if ((l = argvars[0].vval.v_list) == NULL)
6529 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006530 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006531 li = l->lv_first;
6532 }
6533 else
6534 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006535 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006536 len = (long)STRLEN(str);
6537 }
6538
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006539 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006540 if (pat == NULL)
6541 goto theend;
6542
6543 if (argvars[2].v_type != VAR_UNKNOWN)
6544 {
6545 int error = FALSE;
6546
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006547 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006548 if (error)
6549 goto theend;
6550 if (l != NULL)
6551 {
6552 li = list_find(l, start);
6553 if (li == NULL)
6554 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006555 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006556 }
6557 else
6558 {
6559 if (start < 0)
6560 start = 0;
6561 if (start > len)
6562 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006563 // When "count" argument is there ignore matches before "start",
6564 // otherwise skip part of the string. Differs when pattern is "^"
6565 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006566 if (argvars[3].v_type != VAR_UNKNOWN)
6567 startcol = start;
6568 else
6569 {
6570 str += start;
6571 len -= start;
6572 }
6573 }
6574
6575 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006576 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006577 if (error)
6578 goto theend;
6579 }
6580
6581 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6582 if (regmatch.regprog != NULL)
6583 {
6584 regmatch.rm_ic = p_ic;
6585
6586 for (;;)
6587 {
6588 if (l != NULL)
6589 {
6590 if (li == NULL)
6591 {
6592 match = FALSE;
6593 break;
6594 }
6595 vim_free(tofree);
6596 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6597 if (str == NULL)
6598 break;
6599 }
6600
6601 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6602
6603 if (match && --nth <= 0)
6604 break;
6605 if (l == NULL && !match)
6606 break;
6607
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006608 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006609 if (l != NULL)
6610 {
6611 li = li->li_next;
6612 ++idx;
6613 }
6614 else
6615 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006616 startcol = (colnr_T)(regmatch.startp[0]
6617 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006618 if (startcol > (colnr_T)len
6619 || str + startcol <= regmatch.startp[0])
6620 {
6621 match = FALSE;
6622 break;
6623 }
6624 }
6625 }
6626
6627 if (match)
6628 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006629 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006630 {
6631 listitem_T *li1 = rettv->vval.v_list->lv_first;
6632 listitem_T *li2 = li1->li_next;
6633 listitem_T *li3 = li2->li_next;
6634 listitem_T *li4 = li3->li_next;
6635
6636 vim_free(li1->li_tv.vval.v_string);
6637 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006638 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006639 li3->li_tv.vval.v_number =
6640 (varnumber_T)(regmatch.startp[0] - expr);
6641 li4->li_tv.vval.v_number =
6642 (varnumber_T)(regmatch.endp[0] - expr);
6643 if (l != NULL)
6644 li2->li_tv.vval.v_number = (varnumber_T)idx;
6645 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006646 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006647 {
6648 int i;
6649
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006650 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006651 for (i = 0; i < NSUBEXP; ++i)
6652 {
6653 if (regmatch.endp[i] == NULL)
6654 {
6655 if (list_append_string(rettv->vval.v_list,
6656 (char_u *)"", 0) == FAIL)
6657 break;
6658 }
6659 else if (list_append_string(rettv->vval.v_list,
6660 regmatch.startp[i],
6661 (int)(regmatch.endp[i] - regmatch.startp[i]))
6662 == FAIL)
6663 break;
6664 }
6665 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006666 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006667 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006668 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006669 if (l != NULL)
6670 copy_tv(&li->li_tv, rettv);
6671 else
6672 rettv->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 }
6675 else if (l != NULL)
6676 rettv->vval.v_number = idx;
6677 else
6678 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006679 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006680 rettv->vval.v_number =
6681 (varnumber_T)(regmatch.startp[0] - str);
6682 else
6683 rettv->vval.v_number =
6684 (varnumber_T)(regmatch.endp[0] - str);
6685 rettv->vval.v_number += (varnumber_T)(str - expr);
6686 }
6687 }
6688 vim_regfree(regmatch.regprog);
6689 }
6690
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006691theend:
6692 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006693 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694 listitem_remove(rettv->vval.v_list,
6695 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006696 vim_free(tofree);
6697 p_cpo = save_cpo;
6698}
6699
6700/*
6701 * "match()" function
6702 */
6703 static void
6704f_match(typval_T *argvars, typval_T *rettv)
6705{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006706 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006707}
6708
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006709/*
6710 * "matchend()" function
6711 */
6712 static void
6713f_matchend(typval_T *argvars, typval_T *rettv)
6714{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006715 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006716}
6717
6718/*
6719 * "matchlist()" function
6720 */
6721 static void
6722f_matchlist(typval_T *argvars, typval_T *rettv)
6723{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006724 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006725}
6726
6727/*
6728 * "matchstr()" function
6729 */
6730 static void
6731f_matchstr(typval_T *argvars, typval_T *rettv)
6732{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006733 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006734}
6735
6736/*
6737 * "matchstrpos()" function
6738 */
6739 static void
6740f_matchstrpos(typval_T *argvars, typval_T *rettv)
6741{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006742 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006743}
6744
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006745 static void
6746max_min(typval_T *argvars, typval_T *rettv, int domax)
6747{
6748 varnumber_T n = 0;
6749 varnumber_T i;
6750 int error = FALSE;
6751
6752 if (argvars[0].v_type == VAR_LIST)
6753 {
6754 list_T *l;
6755 listitem_T *li;
6756
6757 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006758 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006759 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006760 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006761 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006762 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6763 n = l->lv_u.nonmat.lv_start;
6764 else
6765 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6766 * l->lv_u.nonmat.lv_stride;
6767 }
6768 else
6769 {
6770 li = l->lv_first;
6771 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006772 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006773 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006774 if (error)
6775 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006776 for (;;)
6777 {
6778 li = li->li_next;
6779 if (li == NULL)
6780 break;
6781 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006782 if (error)
6783 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006784 if (domax ? i > n : i < n)
6785 n = i;
6786 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006787 }
6788 }
6789 }
6790 }
6791 else if (argvars[0].v_type == VAR_DICT)
6792 {
6793 dict_T *d;
6794 int first = TRUE;
6795 hashitem_T *hi;
6796 int todo;
6797
6798 d = argvars[0].vval.v_dict;
6799 if (d != NULL)
6800 {
6801 todo = (int)d->dv_hashtab.ht_used;
6802 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6803 {
6804 if (!HASHITEM_EMPTY(hi))
6805 {
6806 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006807 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006808 if (error)
6809 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006810 if (first)
6811 {
6812 n = i;
6813 first = FALSE;
6814 }
6815 else if (domax ? i > n : i < n)
6816 n = i;
6817 }
6818 }
6819 }
6820 }
6821 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006822 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006823
6824 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006825}
6826
6827/*
6828 * "max()" function
6829 */
6830 static void
6831f_max(typval_T *argvars, typval_T *rettv)
6832{
6833 max_min(argvars, rettv, TRUE);
6834}
6835
6836/*
6837 * "min()" function
6838 */
6839 static void
6840f_min(typval_T *argvars, typval_T *rettv)
6841{
6842 max_min(argvars, rettv, FALSE);
6843}
6844
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006845#if defined(FEAT_MZSCHEME) || defined(PROTO)
6846/*
6847 * "mzeval()" function
6848 */
6849 static void
6850f_mzeval(typval_T *argvars, typval_T *rettv)
6851{
6852 char_u *str;
6853 char_u buf[NUMBUFLEN];
6854
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006855 if (check_restricted() || check_secure())
6856 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006857 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006858 do_mzeval(str, rettv);
6859}
6860
6861 void
6862mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6863{
6864 typval_T argvars[3];
6865
6866 argvars[0].v_type = VAR_STRING;
6867 argvars[0].vval.v_string = name;
6868 copy_tv(args, &argvars[1]);
6869 argvars[2].v_type = VAR_UNKNOWN;
6870 f_call(argvars, rettv);
6871 clear_tv(&argvars[1]);
6872}
6873#endif
6874
6875/*
6876 * "nextnonblank()" function
6877 */
6878 static void
6879f_nextnonblank(typval_T *argvars, typval_T *rettv)
6880{
6881 linenr_T lnum;
6882
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006883 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006884 {
6885 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6886 {
6887 lnum = 0;
6888 break;
6889 }
6890 if (*skipwhite(ml_get(lnum)) != NUL)
6891 break;
6892 }
6893 rettv->vval.v_number = lnum;
6894}
6895
6896/*
6897 * "nr2char()" function
6898 */
6899 static void
6900f_nr2char(typval_T *argvars, typval_T *rettv)
6901{
6902 char_u buf[NUMBUFLEN];
6903
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006904 if (has_mbyte)
6905 {
6906 int utf8 = 0;
6907
6908 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006909 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006910 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006911 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006912 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006913 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006914 }
6915 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006916 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006917 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006918 buf[1] = NUL;
6919 }
6920 rettv->v_type = VAR_STRING;
6921 rettv->vval.v_string = vim_strsave(buf);
6922}
6923
6924/*
6925 * "or(expr, expr)" function
6926 */
6927 static void
6928f_or(typval_T *argvars, typval_T *rettv)
6929{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006930 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6931 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006932}
6933
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006934#ifdef FEAT_PERL
6935/*
6936 * "perleval()" function
6937 */
6938 static void
6939f_perleval(typval_T *argvars, typval_T *rettv)
6940{
6941 char_u *str;
6942 char_u buf[NUMBUFLEN];
6943
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006944 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006945 do_perleval(str, rettv);
6946}
6947#endif
6948
6949#ifdef FEAT_FLOAT
6950/*
6951 * "pow()" function
6952 */
6953 static void
6954f_pow(typval_T *argvars, typval_T *rettv)
6955{
6956 float_T fx = 0.0, fy = 0.0;
6957
6958 rettv->v_type = VAR_FLOAT;
6959 if (get_float_arg(argvars, &fx) == OK
6960 && get_float_arg(&argvars[1], &fy) == OK)
6961 rettv->vval.v_float = pow(fx, fy);
6962 else
6963 rettv->vval.v_float = 0.0;
6964}
6965#endif
6966
6967/*
6968 * "prevnonblank()" function
6969 */
6970 static void
6971f_prevnonblank(typval_T *argvars, typval_T *rettv)
6972{
6973 linenr_T lnum;
6974
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006975 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006976 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6977 lnum = 0;
6978 else
6979 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6980 --lnum;
6981 rettv->vval.v_number = lnum;
6982}
6983
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006984// This dummy va_list is here because:
6985// - passing a NULL pointer doesn't work when va_list isn't a pointer
6986// - locally in the function results in a "used before set" warning
6987// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006988static va_list ap;
6989
6990/*
6991 * "printf()" function
6992 */
6993 static void
6994f_printf(typval_T *argvars, typval_T *rettv)
6995{
6996 char_u buf[NUMBUFLEN];
6997 int len;
6998 char_u *s;
6999 int saved_did_emsg = did_emsg;
7000 char *fmt;
7001
7002 rettv->v_type = VAR_STRING;
7003 rettv->vval.v_string = NULL;
7004
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007005 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007006 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007007 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007008 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007009 if (!did_emsg)
7010 {
7011 s = alloc(len + 1);
7012 if (s != NULL)
7013 {
7014 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007015 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
7016 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007017 }
7018 }
7019 did_emsg |= saved_did_emsg;
7020}
7021
7022/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007023 * "pum_getpos()" function
7024 */
7025 static void
7026f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7027{
7028 if (rettv_dict_alloc(rettv) != OK)
7029 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007030 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007031}
7032
7033/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007034 * "pumvisible()" function
7035 */
7036 static void
7037f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7038{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007039 if (pum_visible())
7040 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007041}
7042
7043#ifdef FEAT_PYTHON3
7044/*
7045 * "py3eval()" function
7046 */
7047 static void
7048f_py3eval(typval_T *argvars, typval_T *rettv)
7049{
7050 char_u *str;
7051 char_u buf[NUMBUFLEN];
7052
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007053 if (check_restricted() || check_secure())
7054 return;
7055
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007056 if (p_pyx == 0)
7057 p_pyx = 3;
7058
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007059 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007060 do_py3eval(str, rettv);
7061}
7062#endif
7063
7064#ifdef FEAT_PYTHON
7065/*
7066 * "pyeval()" function
7067 */
7068 static void
7069f_pyeval(typval_T *argvars, typval_T *rettv)
7070{
7071 char_u *str;
7072 char_u buf[NUMBUFLEN];
7073
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007074 if (check_restricted() || check_secure())
7075 return;
7076
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007077 if (p_pyx == 0)
7078 p_pyx = 2;
7079
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007080 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007081 do_pyeval(str, rettv);
7082}
7083#endif
7084
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007085#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
7086/*
7087 * "pyxeval()" function
7088 */
7089 static void
7090f_pyxeval(typval_T *argvars, typval_T *rettv)
7091{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007092 if (check_restricted() || check_secure())
7093 return;
7094
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007095# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
7096 init_pyxversion();
7097 if (p_pyx == 2)
7098 f_pyeval(argvars, rettv);
7099 else
7100 f_py3eval(argvars, rettv);
7101# elif defined(FEAT_PYTHON)
7102 f_pyeval(argvars, rettv);
7103# elif defined(FEAT_PYTHON3)
7104 f_py3eval(argvars, rettv);
7105# endif
7106}
7107#endif
7108
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007109static UINT32_T srand_seed_for_testing = 0;
7110static int srand_seed_for_testing_is_used = FALSE;
7111
7112 static void
7113f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
7114{
7115 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007116 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007117 else
7118 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007119 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
7120 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007121 }
7122}
7123
7124 static void
7125init_srand(UINT32_T *x)
7126{
7127#ifndef MSWIN
7128 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
7129#endif
7130
7131 if (srand_seed_for_testing_is_used)
7132 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007133 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007134 return;
7135 }
7136#ifndef MSWIN
7137 if (dev_urandom_state != FAIL)
7138 {
7139 int fd = open("/dev/urandom", O_RDONLY);
7140 struct {
7141 union {
7142 UINT32_T number;
7143 char bytes[sizeof(UINT32_T)];
7144 } contents;
7145 } buf;
7146
7147 // Attempt reading /dev/urandom.
7148 if (fd == -1)
7149 dev_urandom_state = FAIL;
7150 else
7151 {
7152 buf.contents.number = 0;
7153 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
7154 != sizeof(UINT32_T))
7155 dev_urandom_state = FAIL;
7156 else
7157 {
7158 dev_urandom_state = OK;
7159 *x = buf.contents.number;
7160 }
7161 close(fd);
7162 }
7163 }
7164 if (dev_urandom_state != OK)
7165 // Reading /dev/urandom doesn't work, fall back to time().
7166#endif
7167 *x = vim_time();
7168}
7169
7170#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
7171#define SPLITMIX32(x, z) ( \
7172 z = (x += 0x9e3779b9), \
7173 z = (z ^ (z >> 16)) * 0x85ebca6b, \
7174 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
7175 z ^ (z >> 16) \
7176 )
7177#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
7178 result = ROTL(y * 5, 7) * 9; \
7179 t = y << 9; \
7180 z ^= x; \
7181 w ^= y; \
7182 y ^= z, x ^= w; \
7183 z ^= t; \
7184 w = ROTL(w, 11);
7185
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007186/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007187 * "rand()" function
7188 */
7189 static void
7190f_rand(typval_T *argvars, typval_T *rettv)
7191{
7192 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007193 static UINT32_T gx, gy, gz, gw;
7194 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007195 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01007196 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007197
7198 if (argvars[0].v_type == VAR_UNKNOWN)
7199 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007200 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007201 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007202 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007203 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007204 init_srand(&x);
7205
7206 gx = SPLITMIX32(x, z);
7207 gy = SPLITMIX32(x, z);
7208 gz = SPLITMIX32(x, z);
7209 gw = SPLITMIX32(x, z);
7210 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007211 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007212
7213 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007214 }
7215 else if (argvars[0].v_type == VAR_LIST)
7216 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007217 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007218 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007219 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007220
7221 lx = list_find(l, 0L);
7222 ly = list_find(l, 1L);
7223 lz = list_find(l, 2L);
7224 lw = list_find(l, 3L);
7225 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
7226 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
7227 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
7228 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
7229 x = (UINT32_T)lx->li_tv.vval.v_number;
7230 y = (UINT32_T)ly->li_tv.vval.v_number;
7231 z = (UINT32_T)lz->li_tv.vval.v_number;
7232 w = (UINT32_T)lw->li_tv.vval.v_number;
7233
7234 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
7235
7236 lx->li_tv.vval.v_number = (varnumber_T)x;
7237 ly->li_tv.vval.v_number = (varnumber_T)y;
7238 lz->li_tv.vval.v_number = (varnumber_T)z;
7239 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007240 }
7241 else
7242 goto theend;
7243
7244 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007245 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007246 return;
7247
7248theend:
7249 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007250 rettv->v_type = VAR_NUMBER;
7251 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007252}
7253
7254/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007255 * "srand()" function
7256 */
7257 static void
7258f_srand(typval_T *argvars, typval_T *rettv)
7259{
7260 UINT32_T x = 0, z;
7261
7262 if (rettv_list_alloc(rettv) == FAIL)
7263 return;
7264 if (argvars[0].v_type == VAR_UNKNOWN)
7265 {
7266 init_srand(&x);
7267 }
7268 else
7269 {
7270 int error = FALSE;
7271
7272 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7273 if (error)
7274 return;
7275 }
7276
7277 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7278 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7279 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7280 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7281}
7282
7283#undef ROTL
7284#undef SPLITMIX32
7285#undef SHUFFLE_XOSHIRO128STARSTAR
7286
7287/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007288 * "range()" function
7289 */
7290 static void
7291f_range(typval_T *argvars, typval_T *rettv)
7292{
7293 varnumber_T start;
7294 varnumber_T end;
7295 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007296 int error = FALSE;
7297
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007298 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007299 if (argvars[1].v_type == VAR_UNKNOWN)
7300 {
7301 end = start - 1;
7302 start = 0;
7303 }
7304 else
7305 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007306 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007307 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007308 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007309 }
7310
7311 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007312 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007313 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007314 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007315 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007316 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007317 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007318 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007319 list_T *list = rettv->vval.v_list;
7320
7321 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007322 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007323 // be called.
7324 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007325 list->lv_u.nonmat.lv_start = start;
7326 list->lv_u.nonmat.lv_end = end;
7327 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007328 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007329 }
7330}
7331
7332/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007333 * Materialize "list".
7334 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007335 */
7336 void
7337range_list_materialize(list_T *list)
7338{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007339 varnumber_T start = list->lv_u.nonmat.lv_start;
7340 varnumber_T end = list->lv_u.nonmat.lv_end;
7341 int stride = list->lv_u.nonmat.lv_stride;
7342 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007343
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007344 list->lv_first = NULL;
7345 list->lv_u.mat.lv_last = NULL;
7346 list->lv_len = 0;
7347 list->lv_u.mat.lv_idx_item = NULL;
7348 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7349 if (list_append_number(list, (varnumber_T)i) == FAIL)
7350 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007351}
7352
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007353/*
7354 * "getreginfo()" function
7355 */
7356 static void
7357f_getreginfo(typval_T *argvars, typval_T *rettv)
7358{
7359 char_u *strregname;
7360 int regname;
7361 char_u buf[NUMBUFLEN + 2];
7362 long reglen = 0;
7363 dict_T *dict;
7364 list_T *list;
7365
7366 if (argvars[0].v_type != VAR_UNKNOWN)
7367 {
7368 strregname = tv_get_string_chk(&argvars[0]);
7369 if (strregname == NULL)
7370 return;
7371 }
7372 else
7373 strregname = get_vim_var_str(VV_REG);
7374
7375 regname = (strregname == NULL ? '"' : *strregname);
7376 if (regname == 0 || regname == '@')
7377 regname = '"';
7378
7379 if (rettv_dict_alloc(rettv) == FAIL)
7380 return;
7381 dict = rettv->vval.v_dict;
7382
7383 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7384 if (list == NULL)
7385 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007386 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007387
7388 buf[0] = NUL;
7389 buf[1] = NUL;
7390 switch (get_reg_type(regname, &reglen))
7391 {
7392 case MLINE: buf[0] = 'V'; break;
7393 case MCHAR: buf[0] = 'v'; break;
7394 case MBLOCK:
7395 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7396 reglen + 1);
7397 break;
7398 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007399 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007400
7401 buf[0] = get_register_name(get_unname_register());
7402 buf[1] = NUL;
7403 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007404 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007405 else
7406 {
7407 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7408
7409 if (item != NULL)
7410 {
7411 item->di_tv.v_type = VAR_SPECIAL;
7412 item->di_tv.vval.v_number = regname == buf[0]
7413 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007414 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007415 }
7416 }
7417}
7418
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007419 static void
7420return_register(int regname, typval_T *rettv)
7421{
7422 char_u buf[2] = {0, 0};
7423
7424 buf[0] = (char_u)regname;
7425 rettv->v_type = VAR_STRING;
7426 rettv->vval.v_string = vim_strsave(buf);
7427}
7428
7429/*
7430 * "reg_executing()" function
7431 */
7432 static void
7433f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7434{
7435 return_register(reg_executing, rettv);
7436}
7437
7438/*
7439 * "reg_recording()" function
7440 */
7441 static void
7442f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7443{
7444 return_register(reg_recording, rettv);
7445}
7446
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007447/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007448 * "rename({from}, {to})" function
7449 */
7450 static void
7451f_rename(typval_T *argvars, typval_T *rettv)
7452{
7453 char_u buf[NUMBUFLEN];
7454
7455 if (check_restricted() || check_secure())
7456 rettv->vval.v_number = -1;
7457 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007458 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7459 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007460}
7461
7462/*
7463 * "repeat()" function
7464 */
7465 static void
7466f_repeat(typval_T *argvars, typval_T *rettv)
7467{
7468 char_u *p;
7469 int n;
7470 int slen;
7471 int len;
7472 char_u *r;
7473 int i;
7474
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007475 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007476 if (argvars[0].v_type == VAR_LIST)
7477 {
7478 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7479 while (n-- > 0)
7480 if (list_extend(rettv->vval.v_list,
7481 argvars[0].vval.v_list, NULL) == FAIL)
7482 break;
7483 }
7484 else
7485 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007486 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007487 rettv->v_type = VAR_STRING;
7488 rettv->vval.v_string = NULL;
7489
7490 slen = (int)STRLEN(p);
7491 len = slen * n;
7492 if (len <= 0)
7493 return;
7494
7495 r = alloc(len + 1);
7496 if (r != NULL)
7497 {
7498 for (i = 0; i < n; i++)
7499 mch_memmove(r + i * slen, p, (size_t)slen);
7500 r[len] = NUL;
7501 }
7502
7503 rettv->vval.v_string = r;
7504 }
7505}
7506
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007507#define SP_NOMOVE 0x01 // don't move cursor
7508#define SP_REPEAT 0x02 // repeat to find outer pair
7509#define SP_RETCOUNT 0x04 // return matchcount
7510#define SP_SETPCMARK 0x08 // set previous context mark
7511#define SP_START 0x10 // accept match at start position
7512#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7513#define SP_END 0x40 // leave cursor at end of match
7514#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007515
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007516/*
7517 * Get flags for a search function.
7518 * Possibly sets "p_ws".
7519 * Returns BACKWARD, FORWARD or zero (for an error).
7520 */
7521 static int
7522get_search_arg(typval_T *varp, int *flagsp)
7523{
7524 int dir = FORWARD;
7525 char_u *flags;
7526 char_u nbuf[NUMBUFLEN];
7527 int mask;
7528
7529 if (varp->v_type != VAR_UNKNOWN)
7530 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007531 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007532 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007533 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007534 while (*flags != NUL)
7535 {
7536 switch (*flags)
7537 {
7538 case 'b': dir = BACKWARD; break;
7539 case 'w': p_ws = TRUE; break;
7540 case 'W': p_ws = FALSE; break;
7541 default: mask = 0;
7542 if (flagsp != NULL)
7543 switch (*flags)
7544 {
7545 case 'c': mask = SP_START; break;
7546 case 'e': mask = SP_END; break;
7547 case 'm': mask = SP_RETCOUNT; break;
7548 case 'n': mask = SP_NOMOVE; break;
7549 case 'p': mask = SP_SUBPAT; break;
7550 case 'r': mask = SP_REPEAT; break;
7551 case 's': mask = SP_SETPCMARK; break;
7552 case 'z': mask = SP_COLUMN; break;
7553 }
7554 if (mask == 0)
7555 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007556 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007557 dir = 0;
7558 }
7559 else
7560 *flagsp |= mask;
7561 }
7562 if (dir == 0)
7563 break;
7564 ++flags;
7565 }
7566 }
7567 return dir;
7568}
7569
7570/*
7571 * Shared by search() and searchpos() functions.
7572 */
7573 static int
7574search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7575{
7576 int flags;
7577 char_u *pat;
7578 pos_T pos;
7579 pos_T save_cursor;
7580 int save_p_ws = p_ws;
7581 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007582 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007583 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007584#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007585 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007586 long time_limit = 0;
7587#endif
7588 int options = SEARCH_KEEP;
7589 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007590 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007591 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007592 pos_T firstpos;
7593
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007594 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007595 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007596 if (dir == 0)
7597 goto theend;
7598 flags = *flagsp;
7599 if (flags & SP_START)
7600 options |= SEARCH_START;
7601 if (flags & SP_END)
7602 options |= SEARCH_END;
7603 if (flags & SP_COLUMN)
7604 options |= SEARCH_COL;
7605
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007606 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007607 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7608 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007609 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007610 if (lnum_stop < 0)
7611 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007612 if (argvars[3].v_type != VAR_UNKNOWN)
7613 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007614#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007615 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007616 if (time_limit < 0)
7617 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007618#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007619 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007620 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007621 }
7622
7623#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007624 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007625 profile_setlimit(time_limit, &tm);
7626#endif
7627
7628 /*
7629 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7630 * Check to make sure only those flags are set.
7631 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7632 * flags cannot be set. Check for that condition also.
7633 */
7634 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7635 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7636 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007637 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007638 goto theend;
7639 }
7640
7641 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007642 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007643 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007644 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7645#ifdef FEAT_RELTIME
7646 sia.sa_tm = &tm;
7647#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007648
7649 // Repeat until {skip} returns FALSE.
7650 for (;;)
7651 {
7652 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007653 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007654 // finding the first match again means there is no match where {skip}
7655 // evaluates to zero.
7656 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7657 subpatnum = FAIL;
7658
Bram Moolenaara9c01042020-06-07 14:50:50 +02007659 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007660 // didn't find it or no skip argument
7661 break;
7662 firstpos = pos;
7663
Bram Moolenaara9c01042020-06-07 14:50:50 +02007664 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007665 {
7666 int do_skip;
7667 int err;
7668 pos_T save_pos = curwin->w_cursor;
7669
7670 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007671 err = FALSE;
7672 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007673 curwin->w_cursor = save_pos;
7674 if (err)
7675 {
7676 // Evaluating {skip} caused an error, break here.
7677 subpatnum = FAIL;
7678 break;
7679 }
7680 if (!do_skip)
7681 break;
7682 }
7683 }
7684
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007685 if (subpatnum != FAIL)
7686 {
7687 if (flags & SP_SUBPAT)
7688 retval = subpatnum;
7689 else
7690 retval = pos.lnum;
7691 if (flags & SP_SETPCMARK)
7692 setpcmark();
7693 curwin->w_cursor = pos;
7694 if (match_pos != NULL)
7695 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007696 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007697 match_pos->lnum = pos.lnum;
7698 match_pos->col = pos.col + 1;
7699 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007700 // "/$" will put the cursor after the end of the line, may need to
7701 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007702 check_cursor();
7703 }
7704
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007705 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007706 if (flags & SP_NOMOVE)
7707 curwin->w_cursor = save_cursor;
7708 else
7709 curwin->w_set_curswant = TRUE;
7710theend:
7711 p_ws = save_p_ws;
7712
7713 return retval;
7714}
7715
7716#ifdef FEAT_FLOAT
7717
7718/*
7719 * round() is not in C90, use ceil() or floor() instead.
7720 */
7721 float_T
7722vim_round(float_T f)
7723{
7724 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
7725}
7726
7727/*
7728 * "round({float})" function
7729 */
7730 static void
7731f_round(typval_T *argvars, typval_T *rettv)
7732{
7733 float_T f = 0.0;
7734
7735 rettv->v_type = VAR_FLOAT;
7736 if (get_float_arg(argvars, &f) == OK)
7737 rettv->vval.v_float = vim_round(f);
7738 else
7739 rettv->vval.v_float = 0.0;
7740}
7741#endif
7742
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007743#ifdef FEAT_RUBY
7744/*
7745 * "rubyeval()" function
7746 */
7747 static void
7748f_rubyeval(typval_T *argvars, typval_T *rettv)
7749{
7750 char_u *str;
7751 char_u buf[NUMBUFLEN];
7752
7753 str = tv_get_string_buf(&argvars[0], buf);
7754 do_rubyeval(str, rettv);
7755}
7756#endif
7757
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007758/*
7759 * "screenattr()" function
7760 */
7761 static void
7762f_screenattr(typval_T *argvars, typval_T *rettv)
7763{
7764 int row;
7765 int col;
7766 int c;
7767
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007768 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7769 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007770 if (row < 0 || row >= screen_Rows
7771 || col < 0 || col >= screen_Columns)
7772 c = -1;
7773 else
7774 c = ScreenAttrs[LineOffset[row] + col];
7775 rettv->vval.v_number = c;
7776}
7777
7778/*
7779 * "screenchar()" function
7780 */
7781 static void
7782f_screenchar(typval_T *argvars, typval_T *rettv)
7783{
7784 int row;
7785 int col;
7786 int off;
7787 int c;
7788
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007789 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7790 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007791 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007792 c = -1;
7793 else
7794 {
7795 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007796 if (enc_utf8 && ScreenLinesUC[off] != 0)
7797 c = ScreenLinesUC[off];
7798 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007799 c = ScreenLines[off];
7800 }
7801 rettv->vval.v_number = c;
7802}
7803
7804/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007805 * "screenchars()" function
7806 */
7807 static void
7808f_screenchars(typval_T *argvars, typval_T *rettv)
7809{
7810 int row;
7811 int col;
7812 int off;
7813 int c;
7814 int i;
7815
7816 if (rettv_list_alloc(rettv) == FAIL)
7817 return;
7818 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7819 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7820 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7821 return;
7822
7823 off = LineOffset[row] + col;
7824 if (enc_utf8 && ScreenLinesUC[off] != 0)
7825 c = ScreenLinesUC[off];
7826 else
7827 c = ScreenLines[off];
7828 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7829
7830 if (enc_utf8)
7831
7832 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7833 list_append_number(rettv->vval.v_list,
7834 (varnumber_T)ScreenLinesC[i][off]);
7835}
7836
7837/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007838 * "screencol()" function
7839 *
7840 * First column is 1 to be consistent with virtcol().
7841 */
7842 static void
7843f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7844{
7845 rettv->vval.v_number = screen_screencol() + 1;
7846}
7847
7848/*
7849 * "screenrow()" function
7850 */
7851 static void
7852f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7853{
7854 rettv->vval.v_number = screen_screenrow() + 1;
7855}
7856
7857/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007858 * "screenstring()" function
7859 */
7860 static void
7861f_screenstring(typval_T *argvars, typval_T *rettv)
7862{
7863 int row;
7864 int col;
7865 int off;
7866 int c;
7867 int i;
7868 char_u buf[MB_MAXBYTES + 1];
7869 int buflen = 0;
7870
7871 rettv->vval.v_string = NULL;
7872 rettv->v_type = VAR_STRING;
7873
7874 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7875 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7876 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7877 return;
7878
7879 off = LineOffset[row] + col;
7880 if (enc_utf8 && ScreenLinesUC[off] != 0)
7881 c = ScreenLinesUC[off];
7882 else
7883 c = ScreenLines[off];
7884 buflen += mb_char2bytes(c, buf);
7885
7886 if (enc_utf8)
7887 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7888 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7889
7890 buf[buflen] = NUL;
7891 rettv->vval.v_string = vim_strsave(buf);
7892}
7893
7894/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007895 * "search()" function
7896 */
7897 static void
7898f_search(typval_T *argvars, typval_T *rettv)
7899{
7900 int flags = 0;
7901
7902 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7903}
7904
7905/*
7906 * "searchdecl()" function
7907 */
7908 static void
7909f_searchdecl(typval_T *argvars, typval_T *rettv)
7910{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007911 int locally = TRUE;
7912 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007913 int error = FALSE;
7914 char_u *name;
7915
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007916 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007917
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007918 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007919 if (argvars[1].v_type != VAR_UNKNOWN)
7920 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007921 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007922 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007923 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007924 }
7925 if (!error && name != NULL)
7926 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7927 locally, thisblock, SEARCH_KEEP) == FAIL;
7928}
7929
7930/*
7931 * Used by searchpair() and searchpairpos()
7932 */
7933 static int
7934searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7935{
7936 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007937 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007938 int save_p_ws = p_ws;
7939 int dir;
7940 int flags = 0;
7941 char_u nbuf1[NUMBUFLEN];
7942 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007943 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007944 long lnum_stop = 0;
7945 long time_limit = 0;
7946
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007947 // Get the three pattern arguments: start, middle, end. Will result in an
7948 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007949 spat = tv_get_string_chk(&argvars[0]);
7950 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7951 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007952 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007953 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007954
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007955 // Handle the optional fourth argument: flags
7956 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007957 if (dir == 0)
7958 goto theend;
7959
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007960 // Don't accept SP_END or SP_SUBPAT.
7961 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007962 if ((flags & (SP_END | SP_SUBPAT)) != 0
7963 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7964 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007965 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007966 goto theend;
7967 }
7968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007969 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007970 if (flags & SP_REPEAT)
7971 p_ws = FALSE;
7972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007973 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007974 if (argvars[3].v_type == VAR_UNKNOWN
7975 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007976 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007977 else
7978 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007979 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007980 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007981
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007982 if (argvars[5].v_type != VAR_UNKNOWN)
7983 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007984 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007985 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007986 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007987 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007988 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007989 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007990#ifdef FEAT_RELTIME
7991 if (argvars[6].v_type != VAR_UNKNOWN)
7992 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007993 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007994 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007995 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007996 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007997 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007998 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007999 }
8000#endif
8001 }
8002 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008003
8004 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
8005 match_pos, lnum_stop, time_limit);
8006
8007theend:
8008 p_ws = save_p_ws;
8009
8010 return retval;
8011}
8012
8013/*
8014 * "searchpair()" function
8015 */
8016 static void
8017f_searchpair(typval_T *argvars, typval_T *rettv)
8018{
8019 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
8020}
8021
8022/*
8023 * "searchpairpos()" function
8024 */
8025 static void
8026f_searchpairpos(typval_T *argvars, typval_T *rettv)
8027{
8028 pos_T match_pos;
8029 int lnum = 0;
8030 int col = 0;
8031
8032 if (rettv_list_alloc(rettv) == FAIL)
8033 return;
8034
8035 if (searchpair_cmn(argvars, &match_pos) > 0)
8036 {
8037 lnum = match_pos.lnum;
8038 col = match_pos.col;
8039 }
8040
8041 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8042 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8043}
8044
8045/*
8046 * Search for a start/middle/end thing.
8047 * Used by searchpair(), see its documentation for the details.
8048 * Returns 0 or -1 for no match,
8049 */
8050 long
8051do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008052 char_u *spat, // start pattern
8053 char_u *mpat, // middle pattern
8054 char_u *epat, // end pattern
8055 int dir, // BACKWARD or FORWARD
8056 typval_T *skip, // skip expression
8057 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008058 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008059 linenr_T lnum_stop, // stop at this line if not zero
8060 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008061{
8062 char_u *save_cpo;
8063 char_u *pat, *pat2 = NULL, *pat3 = NULL;
8064 long retval = 0;
8065 pos_T pos;
8066 pos_T firstpos;
8067 pos_T foundpos;
8068 pos_T save_cursor;
8069 pos_T save_pos;
8070 int n;
8071 int r;
8072 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01008073 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008074 int err;
8075 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008076#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008077 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008078#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008079
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008080 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008081 save_cpo = p_cpo;
8082 p_cpo = empty_option;
8083
8084#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008085 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008086 profile_setlimit(time_limit, &tm);
8087#endif
8088
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008089 // Make two search patterns: start/end (pat2, for in nested pairs) and
8090 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02008091 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
8092 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008093 if (pat2 == NULL || pat3 == NULL)
8094 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008095 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008096 if (*mpat == NUL)
8097 STRCPY(pat3, pat2);
8098 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008099 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008100 spat, epat, mpat);
8101 if (flags & SP_START)
8102 options |= SEARCH_START;
8103
Bram Moolenaar48570482017-10-30 21:48:41 +01008104 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02008105 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01008106
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008107 save_cursor = curwin->w_cursor;
8108 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008109 CLEAR_POS(&firstpos);
8110 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008111 pat = pat3;
8112 for (;;)
8113 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008114 searchit_arg_T sia;
8115
Bram Moolenaara80faa82020-04-12 19:37:17 +02008116 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008117 sia.sa_stop_lnum = lnum_stop;
8118#ifdef FEAT_RELTIME
8119 sia.sa_tm = &tm;
8120#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01008121 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008122 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008123 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008124 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008125 break;
8126
8127 if (firstpos.lnum == 0)
8128 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008129 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008130 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008131 // Found the same position again. Can happen with a pattern that
8132 // has "\zs" at the end and searching backwards. Advance one
8133 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008134 if (dir == BACKWARD)
8135 decl(&pos);
8136 else
8137 incl(&pos);
8138 }
8139 foundpos = pos;
8140
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008141 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008142 options &= ~SEARCH_START;
8143
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008144 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01008145 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008146 {
8147 save_pos = curwin->w_cursor;
8148 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01008149 err = FALSE;
8150 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008151 curwin->w_cursor = save_pos;
8152 if (err)
8153 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008154 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008155 curwin->w_cursor = save_cursor;
8156 retval = -1;
8157 break;
8158 }
8159 if (r)
8160 continue;
8161 }
8162
8163 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8164 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008165 // Found end when searching backwards or start when searching
8166 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008167 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008168 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008169 }
8170 else
8171 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008172 // Found end when searching forward or start when searching
8173 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008174 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008175 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008176 }
8177
8178 if (nest == 0)
8179 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008180 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008181 if (flags & SP_RETCOUNT)
8182 ++retval;
8183 else
8184 retval = pos.lnum;
8185 if (flags & SP_SETPCMARK)
8186 setpcmark();
8187 curwin->w_cursor = pos;
8188 if (!(flags & SP_REPEAT))
8189 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008190 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008191 }
8192 }
8193
8194 if (match_pos != NULL)
8195 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008196 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008197 match_pos->lnum = curwin->w_cursor.lnum;
8198 match_pos->col = curwin->w_cursor.col + 1;
8199 }
8200
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008201 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008202 if ((flags & SP_NOMOVE) || retval == 0)
8203 curwin->w_cursor = save_cursor;
8204
8205theend:
8206 vim_free(pat2);
8207 vim_free(pat3);
8208 if (p_cpo == empty_option)
8209 p_cpo = save_cpo;
8210 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008211 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008212 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008213 // If it's still empty it was changed and restored, need to restore in
8214 // the complicated way.
8215 if (*p_cpo == NUL)
8216 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008217 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008218 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008219
8220 return retval;
8221}
8222
8223/*
8224 * "searchpos()" function
8225 */
8226 static void
8227f_searchpos(typval_T *argvars, typval_T *rettv)
8228{
8229 pos_T match_pos;
8230 int lnum = 0;
8231 int col = 0;
8232 int n;
8233 int flags = 0;
8234
8235 if (rettv_list_alloc(rettv) == FAIL)
8236 return;
8237
8238 n = search_cmn(argvars, &match_pos, &flags);
8239 if (n > 0)
8240 {
8241 lnum = match_pos.lnum;
8242 col = match_pos.col;
8243 }
8244
8245 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8246 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8247 if (flags & SP_SUBPAT)
8248 list_append_number(rettv->vval.v_list, (varnumber_T)n);
8249}
8250
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008251/*
8252 * Set the cursor or mark position.
8253 * If 'charpos' is TRUE, then use the column number as a character offet.
8254 * Otherwise use the column number as a byte offset.
8255 */
8256 static void
8257set_position(typval_T *argvars, typval_T *rettv, int charpos)
8258{
8259 pos_T pos;
8260 int fnum;
8261 char_u *name;
8262 colnr_T curswant = -1;
8263
8264 rettv->vval.v_number = -1;
8265
8266 name = tv_get_string_chk(argvars);
8267 if (name != NULL)
8268 {
8269 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
8270 {
8271 if (pos.col != MAXCOL && --pos.col < 0)
8272 pos.col = 0;
8273 if ((name[0] == '.' && name[1] == NUL))
8274 {
8275 // set cursor; "fnum" is ignored
8276 curwin->w_cursor = pos;
8277 if (curswant >= 0)
8278 {
8279 curwin->w_curswant = curswant - 1;
8280 curwin->w_set_curswant = FALSE;
8281 }
8282 check_cursor();
8283 rettv->vval.v_number = 0;
8284 }
8285 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8286 {
8287 // set mark
8288 if (setmark_pos(name[1], &pos, fnum) == OK)
8289 rettv->vval.v_number = 0;
8290 }
8291 else
8292 emsg(_(e_invarg));
8293 }
8294 }
8295}
8296/*
8297 * "setcharpos()" function
8298 */
8299 static void
8300f_setcharpos(typval_T *argvars, typval_T *rettv)
8301{
8302 set_position(argvars, rettv, TRUE);
8303}
8304
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008305 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008306f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8307{
8308 dict_T *d;
8309 dictitem_T *di;
8310 char_u *csearch;
8311
8312 if (argvars[0].v_type != VAR_DICT)
8313 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008314 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008315 return;
8316 }
8317
8318 if ((d = argvars[0].vval.v_dict) != NULL)
8319 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008320 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008321 if (csearch != NULL)
8322 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008323 if (enc_utf8)
8324 {
8325 int pcc[MAX_MCO];
8326 int c = utfc_ptr2char(csearch, pcc);
8327
8328 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8329 }
8330 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008331 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008332 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008333 }
8334
8335 di = dict_find(d, (char_u *)"forward", -1);
8336 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008337 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008338 ? FORWARD : BACKWARD);
8339
8340 di = dict_find(d, (char_u *)"until", -1);
8341 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008342 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008343 }
8344}
8345
8346/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008347 * "setcursorcharpos" function
8348 */
8349 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008350f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008351{
8352 set_cursorpos(argvars, rettv, TRUE);
8353}
8354
8355/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008356 * "setenv()" function
8357 */
8358 static void
8359f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8360{
8361 char_u namebuf[NUMBUFLEN];
8362 char_u valbuf[NUMBUFLEN];
8363 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8364
8365 if (argvars[1].v_type == VAR_SPECIAL
8366 && argvars[1].vval.v_number == VVAL_NULL)
8367 vim_unsetenv(name);
8368 else
8369 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8370}
8371
8372/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008373 * "setfperm({fname}, {mode})" function
8374 */
8375 static void
8376f_setfperm(typval_T *argvars, typval_T *rettv)
8377{
8378 char_u *fname;
8379 char_u modebuf[NUMBUFLEN];
8380 char_u *mode_str;
8381 int i;
8382 int mask;
8383 int mode = 0;
8384
8385 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008386 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008387 if (fname == NULL)
8388 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008389 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008390 if (mode_str == NULL)
8391 return;
8392 if (STRLEN(mode_str) != 9)
8393 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008394 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008395 return;
8396 }
8397
8398 mask = 1;
8399 for (i = 8; i >= 0; --i)
8400 {
8401 if (mode_str[i] != '-')
8402 mode |= mask;
8403 mask = mask << 1;
8404 }
8405 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8406}
8407
8408/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008409 * "setpos()" function
8410 */
8411 static void
8412f_setpos(typval_T *argvars, typval_T *rettv)
8413{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008414 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008415}
8416
8417/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008418 * Translate a register type string to the yank type and block length
8419 */
8420 static int
8421get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8422{
8423 char_u *stropt = *pp;
8424 switch (*stropt)
8425 {
8426 case 'v': case 'c': // character-wise selection
8427 *yank_type = MCHAR;
8428 break;
8429 case 'V': case 'l': // line-wise selection
8430 *yank_type = MLINE;
8431 break;
8432 case 'b': case Ctrl_V: // block-wise selection
8433 *yank_type = MBLOCK;
8434 if (VIM_ISDIGIT(stropt[1]))
8435 {
8436 ++stropt;
8437 *block_len = getdigits(&stropt) - 1;
8438 --stropt;
8439 }
8440 break;
8441 default:
8442 return FAIL;
8443 }
8444 *pp = stropt;
8445 return OK;
8446}
8447
8448/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008449 * "setreg()" function
8450 */
8451 static void
8452f_setreg(typval_T *argvars, typval_T *rettv)
8453{
8454 int regname;
8455 char_u *strregname;
8456 char_u *stropt;
8457 char_u *strval;
8458 int append;
8459 char_u yank_type;
8460 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008461 typval_T *regcontents;
8462 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008463
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008464 pointreg = 0;
8465 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008466 block_len = -1;
8467 yank_type = MAUTO;
8468 append = FALSE;
8469
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008470 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008471 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008472
8473 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008474 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008475 regname = *strregname;
8476 if (regname == 0 || regname == '@')
8477 regname = '"';
8478
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008479 if (argvars[1].v_type == VAR_DICT)
8480 {
8481 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008482 dictitem_T *di;
8483
8484 if (d == NULL || d->dv_hashtab.ht_used == 0)
8485 {
8486 // Empty dict, clear the register (like setreg(0, []))
8487 char_u *lstval[2] = {NULL, NULL};
8488 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8489 return;
8490 }
8491
8492 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008493 if (di != NULL)
8494 regcontents = &di->di_tv;
8495
8496 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8497 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008498 {
8499 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8500
8501 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008502 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008503 semsg(_(e_invargval), "value");
8504 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008505 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008506 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008507
8508 if (regname == '"')
8509 {
8510 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8511 if (stropt != NULL)
8512 {
8513 pointreg = *stropt;
8514 regname = pointreg;
8515 }
8516 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008517 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008518 pointreg = regname;
8519 }
8520 else
8521 regcontents = &argvars[1];
8522
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008523 if (argvars[2].v_type != VAR_UNKNOWN)
8524 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008525 if (yank_type != MAUTO)
8526 {
8527 semsg(_(e_toomanyarg), "setreg");
8528 return;
8529 }
8530
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008531 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008533 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008534 for (; *stropt != NUL; ++stropt)
8535 switch (*stropt)
8536 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008537 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008538 append = TRUE;
8539 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008540 default:
8541 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008542 }
8543 }
8544
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008545 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008546 {
8547 char_u **lstval;
8548 char_u **allocval;
8549 char_u buf[NUMBUFLEN];
8550 char_u **curval;
8551 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008552 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008553 listitem_T *li;
8554 int len;
8555
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008556 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008557 len = ll == NULL ? 0 : ll->lv_len;
8558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008559 // First half: use for pointers to result lines; second half: use for
8560 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008561 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562 if (lstval == NULL)
8563 return;
8564 curval = lstval;
8565 allocval = lstval + len + 2;
8566 curallocval = allocval;
8567
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008568 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008569 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008570 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008571 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008572 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008573 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008574 if (strval == NULL)
8575 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008576 if (strval == buf)
8577 {
8578 // Need to make a copy, next tv_get_string_buf_chk() will
8579 // overwrite the string.
8580 strval = vim_strsave(buf);
8581 if (strval == NULL)
8582 goto free_lstval;
8583 *curallocval++ = strval;
8584 }
8585 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008586 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008587 }
8588 *curval++ = NULL;
8589
8590 write_reg_contents_lst(regname, lstval, -1,
8591 append, yank_type, block_len);
8592free_lstval:
8593 while (curallocval > allocval)
8594 vim_free(*--curallocval);
8595 vim_free(lstval);
8596 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008597 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008598 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008599 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008600 if (strval == NULL)
8601 return;
8602 write_reg_contents_ex(regname, strval, -1,
8603 append, yank_type, block_len);
8604 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008605 if (pointreg != 0)
8606 get_yank_register(pointreg, TRUE);
8607
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008608 rettv->vval.v_number = 0;
8609}
8610
8611/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008612 * "settagstack()" function
8613 */
8614 static void
8615f_settagstack(typval_T *argvars, typval_T *rettv)
8616{
8617 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8618 win_T *wp;
8619 dict_T *d;
8620 int action = 'r';
8621
8622 rettv->vval.v_number = -1;
8623
8624 // first argument: window number or id
8625 wp = find_win_by_nr_or_id(&argvars[0]);
8626 if (wp == NULL)
8627 return;
8628
8629 // second argument: dict with items to set in the tag stack
8630 if (argvars[1].v_type != VAR_DICT)
8631 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008632 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008633 return;
8634 }
8635 d = argvars[1].vval.v_dict;
8636 if (d == NULL)
8637 return;
8638
8639 // third argument: action - 'a' for append and 'r' for replace.
8640 // default is to replace the stack.
8641 if (argvars[2].v_type == VAR_UNKNOWN)
8642 action = 'r';
8643 else if (argvars[2].v_type == VAR_STRING)
8644 {
8645 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008646 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008647 if (actstr == NULL)
8648 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008649 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8650 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008651 action = *actstr;
8652 else
8653 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008654 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008655 return;
8656 }
8657 }
8658 else
8659 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008660 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008661 return;
8662 }
8663
8664 if (set_tagstack(wp, d, action) == OK)
8665 rettv->vval.v_number = 0;
8666}
8667
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008668#ifdef FEAT_CRYPT
8669/*
8670 * "sha256({string})" function
8671 */
8672 static void
8673f_sha256(typval_T *argvars, typval_T *rettv)
8674{
8675 char_u *p;
8676
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008677 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008678 rettv->vval.v_string = vim_strsave(
8679 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8680 rettv->v_type = VAR_STRING;
8681}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008682#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008683
8684/*
8685 * "shellescape({string})" function
8686 */
8687 static void
8688f_shellescape(typval_T *argvars, typval_T *rettv)
8689{
Bram Moolenaar20615522017-06-05 18:46:26 +02008690 int do_special = non_zero_arg(&argvars[1]);
8691
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008692 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008693 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008694 rettv->v_type = VAR_STRING;
8695}
8696
8697/*
8698 * shiftwidth() function
8699 */
8700 static void
8701f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8702{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008703 rettv->vval.v_number = 0;
8704
8705 if (argvars[0].v_type != VAR_UNKNOWN)
8706 {
8707 long col;
8708
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008709 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008710 if (col < 0)
8711 return; // type error; errmsg already given
8712#ifdef FEAT_VARTABS
8713 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8714 return;
8715#endif
8716 }
8717
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008718 rettv->vval.v_number = get_sw_value(curbuf);
8719}
8720
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008721#ifdef FEAT_FLOAT
8722/*
8723 * "sin()" function
8724 */
8725 static void
8726f_sin(typval_T *argvars, typval_T *rettv)
8727{
8728 float_T f = 0.0;
8729
8730 rettv->v_type = VAR_FLOAT;
8731 if (get_float_arg(argvars, &f) == OK)
8732 rettv->vval.v_float = sin(f);
8733 else
8734 rettv->vval.v_float = 0.0;
8735}
8736
8737/*
8738 * "sinh()" function
8739 */
8740 static void
8741f_sinh(typval_T *argvars, typval_T *rettv)
8742{
8743 float_T f = 0.0;
8744
8745 rettv->v_type = VAR_FLOAT;
8746 if (get_float_arg(argvars, &f) == OK)
8747 rettv->vval.v_float = sinh(f);
8748 else
8749 rettv->vval.v_float = 0.0;
8750}
8751#endif
8752
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008753/*
8754 * "soundfold({word})" function
8755 */
8756 static void
8757f_soundfold(typval_T *argvars, typval_T *rettv)
8758{
8759 char_u *s;
8760
8761 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008762 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008763#ifdef FEAT_SPELL
8764 rettv->vval.v_string = eval_soundfold(s);
8765#else
8766 rettv->vval.v_string = vim_strsave(s);
8767#endif
8768}
8769
8770/*
8771 * "spellbadword()" function
8772 */
8773 static void
8774f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8775{
8776 char_u *word = (char_u *)"";
8777 hlf_T attr = HLF_COUNT;
8778 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008779#ifdef FEAT_SPELL
8780 int wo_spell_save = curwin->w_p_spell;
8781
8782 if (!curwin->w_p_spell)
8783 {
8784 did_set_spelllang(curwin);
8785 curwin->w_p_spell = TRUE;
8786 }
8787
8788 if (*curwin->w_s->b_p_spl == NUL)
8789 {
8790 emsg(_(e_no_spell));
8791 curwin->w_p_spell = wo_spell_save;
8792 return;
8793 }
8794#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008795
8796 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008797 {
8798#ifdef FEAT_SPELL
8799 curwin->w_p_spell = wo_spell_save;
8800#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008801 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008802 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008803
8804#ifdef FEAT_SPELL
8805 if (argvars[0].v_type == VAR_UNKNOWN)
8806 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008807 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008808 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8809 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008810 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008811 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008812 curwin->w_set_curswant = TRUE;
8813 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008814 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008815 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008816 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008817 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008818 int capcol = -1;
8819
8820 if (str != NULL)
8821 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008822 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008823 while (*str != NUL)
8824 {
8825 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8826 if (attr != HLF_COUNT)
8827 {
8828 word = str;
8829 break;
8830 }
8831 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008832 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008833 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008834 }
8835 }
8836 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008837 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008838#endif
8839
8840 list_append_string(rettv->vval.v_list, word, len);
8841 list_append_string(rettv->vval.v_list, (char_u *)(
8842 attr == HLF_SPB ? "bad" :
8843 attr == HLF_SPR ? "rare" :
8844 attr == HLF_SPL ? "local" :
8845 attr == HLF_SPC ? "caps" :
8846 ""), -1);
8847}
8848
8849/*
8850 * "spellsuggest()" function
8851 */
8852 static void
8853f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8854{
8855#ifdef FEAT_SPELL
8856 char_u *str;
8857 int typeerr = FALSE;
8858 int maxcount;
8859 garray_T ga;
8860 int i;
8861 listitem_T *li;
8862 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008863 int wo_spell_save = curwin->w_p_spell;
8864
8865 if (!curwin->w_p_spell)
8866 {
8867 did_set_spelllang(curwin);
8868 curwin->w_p_spell = TRUE;
8869 }
8870
8871 if (*curwin->w_s->b_p_spl == NUL)
8872 {
8873 emsg(_(e_no_spell));
8874 curwin->w_p_spell = wo_spell_save;
8875 return;
8876 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008877#endif
8878
8879 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008880 {
8881#ifdef FEAT_SPELL
8882 curwin->w_p_spell = wo_spell_save;
8883#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008884 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008885 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008886
8887#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008888 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008889 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008890 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008891 if (argvars[1].v_type != VAR_UNKNOWN)
8892 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008893 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008894 if (maxcount <= 0)
8895 return;
8896 if (argvars[2].v_type != VAR_UNKNOWN)
8897 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008898 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008899 if (typeerr)
8900 return;
8901 }
8902 }
8903 else
8904 maxcount = 25;
8905
8906 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8907
8908 for (i = 0; i < ga.ga_len; ++i)
8909 {
8910 str = ((char_u **)ga.ga_data)[i];
8911
8912 li = listitem_alloc();
8913 if (li == NULL)
8914 vim_free(str);
8915 else
8916 {
8917 li->li_tv.v_type = VAR_STRING;
8918 li->li_tv.v_lock = 0;
8919 li->li_tv.vval.v_string = str;
8920 list_append(rettv->vval.v_list, li);
8921 }
8922 }
8923 ga_clear(&ga);
8924 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008925 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008926#endif
8927}
8928
8929 static void
8930f_split(typval_T *argvars, typval_T *rettv)
8931{
8932 char_u *str;
8933 char_u *end;
8934 char_u *pat = NULL;
8935 regmatch_T regmatch;
8936 char_u patbuf[NUMBUFLEN];
8937 char_u *save_cpo;
8938 int match;
8939 colnr_T col = 0;
8940 int keepempty = FALSE;
8941 int typeerr = FALSE;
8942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008943 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008944 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008945 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008946
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008947 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008948 if (argvars[1].v_type != VAR_UNKNOWN)
8949 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008950 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008951 if (pat == NULL)
8952 typeerr = TRUE;
8953 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008954 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008955 }
8956 if (pat == NULL || *pat == NUL)
8957 pat = (char_u *)"[\\x01- ]\\+";
8958
8959 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008960 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008961 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008962 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008963
8964 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8965 if (regmatch.regprog != NULL)
8966 {
8967 regmatch.rm_ic = FALSE;
8968 while (*str != NUL || keepempty)
8969 {
8970 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008971 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008972 else
8973 match = vim_regexec_nl(&regmatch, str, col);
8974 if (match)
8975 end = regmatch.startp[0];
8976 else
8977 end = str + STRLEN(str);
8978 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
8979 && *str != NUL && match && end < regmatch.endp[0]))
8980 {
8981 if (list_append_string(rettv->vval.v_list, str,
8982 (int)(end - str)) == FAIL)
8983 break;
8984 }
8985 if (!match)
8986 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01008987 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008988 if (regmatch.endp[0] > str)
8989 col = 0;
8990 else
Bram Moolenaar13505972019-01-24 15:04:48 +01008991 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008992 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008993 str = regmatch.endp[0];
8994 }
8995
8996 vim_regfree(regmatch.regprog);
8997 }
8998
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008999theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009000 p_cpo = save_cpo;
9001}
9002
9003#ifdef FEAT_FLOAT
9004/*
9005 * "sqrt()" function
9006 */
9007 static void
9008f_sqrt(typval_T *argvars, typval_T *rettv)
9009{
9010 float_T f = 0.0;
9011
9012 rettv->v_type = VAR_FLOAT;
9013 if (get_float_arg(argvars, &f) == OK)
9014 rettv->vval.v_float = sqrt(f);
9015 else
9016 rettv->vval.v_float = 0.0;
9017}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009018#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009019
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009020#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01009021/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009022 * "str2float()" function
9023 */
9024 static void
9025f_str2float(typval_T *argvars, typval_T *rettv)
9026{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009027 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009028 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009029
Bram Moolenaar08243d22017-01-10 16:12:29 +01009030 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009031 p = skipwhite(p + 1);
9032 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01009033 if (isneg)
9034 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009035 rettv->v_type = VAR_FLOAT;
9036}
9037#endif
9038
9039/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02009040 * "str2list()" function
9041 */
9042 static void
9043f_str2list(typval_T *argvars, typval_T *rettv)
9044{
9045 char_u *p;
9046 int utf8 = FALSE;
9047
9048 if (rettv_list_alloc(rettv) == FAIL)
9049 return;
9050
9051 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02009052 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02009053
9054 p = tv_get_string(&argvars[0]);
9055
9056 if (has_mbyte || utf8)
9057 {
9058 int (*ptr2len)(char_u *);
9059 int (*ptr2char)(char_u *);
9060
9061 if (utf8 || enc_utf8)
9062 {
9063 ptr2len = utf_ptr2len;
9064 ptr2char = utf_ptr2char;
9065 }
9066 else
9067 {
9068 ptr2len = mb_ptr2len;
9069 ptr2char = mb_ptr2char;
9070 }
9071
9072 for ( ; *p != NUL; p += (*ptr2len)(p))
9073 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
9074 }
9075 else
9076 for ( ; *p != NUL; ++p)
9077 list_append_number(rettv->vval.v_list, *p);
9078}
9079
9080/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009081 * "str2nr()" function
9082 */
9083 static void
9084f_str2nr(typval_T *argvars, typval_T *rettv)
9085{
9086 int base = 10;
9087 char_u *p;
9088 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009089 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01009090 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009091
9092 if (argvars[1].v_type != VAR_UNKNOWN)
9093 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009094 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009095 if (base != 2 && base != 8 && base != 10 && base != 16)
9096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009097 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009098 return;
9099 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02009100 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009101 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009102 }
9103
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01009104 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009105 isneg = (*p == '-');
9106 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009107 p = skipwhite(p + 1);
9108 switch (base)
9109 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009110 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02009111 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009112 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009113 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02009114 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
9115 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01009116 if (isneg)
9117 rettv->vval.v_number = -n;
9118 else
9119 rettv->vval.v_number = n;
9120
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009121}
9122
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009123/*
9124 * "strgetchar()" function
9125 */
9126 static void
9127f_strgetchar(typval_T *argvars, typval_T *rettv)
9128{
9129 char_u *str;
9130 int len;
9131 int error = FALSE;
9132 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01009133 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009134
9135 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009136 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009137 if (str == NULL)
9138 return;
9139 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009140 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009141 if (error)
9142 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009143
Bram Moolenaar13505972019-01-24 15:04:48 +01009144 while (charidx >= 0 && byteidx < len)
9145 {
9146 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009147 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009148 rettv->vval.v_number = mb_ptr2char(str + byteidx);
9149 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009150 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009151 --charidx;
9152 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009153 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009154}
9155
9156/*
9157 * "stridx()" function
9158 */
9159 static void
9160f_stridx(typval_T *argvars, typval_T *rettv)
9161{
9162 char_u buf[NUMBUFLEN];
9163 char_u *needle;
9164 char_u *haystack;
9165 char_u *save_haystack;
9166 char_u *pos;
9167 int start_idx;
9168
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009169 needle = tv_get_string_chk(&argvars[1]);
9170 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009171 rettv->vval.v_number = -1;
9172 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009173 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009174
9175 if (argvars[2].v_type != VAR_UNKNOWN)
9176 {
9177 int error = FALSE;
9178
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009179 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009180 if (error || start_idx >= (int)STRLEN(haystack))
9181 return;
9182 if (start_idx >= 0)
9183 haystack += start_idx;
9184 }
9185
9186 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9187 if (pos != NULL)
9188 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
9189}
9190
9191/*
9192 * "string()" function
9193 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009194 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009195f_string(typval_T *argvars, typval_T *rettv)
9196{
9197 char_u *tofree;
9198 char_u numbuf[NUMBUFLEN];
9199
9200 rettv->v_type = VAR_STRING;
9201 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
9202 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009203 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009204 if (rettv->vval.v_string != NULL && tofree == NULL)
9205 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
9206}
9207
9208/*
9209 * "strlen()" function
9210 */
9211 static void
9212f_strlen(typval_T *argvars, typval_T *rettv)
9213{
9214 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009215 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009216}
9217
9218/*
9219 * "strchars()" function
9220 */
9221 static void
9222f_strchars(typval_T *argvars, typval_T *rettv)
9223{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009224 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009225 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009226 varnumber_T len = 0;
9227 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009228
9229 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009230 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009231 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02009232 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009233 else
9234 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009235 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
9236 while (*s != NUL)
9237 {
9238 func_mb_ptr2char_adv(&s);
9239 ++len;
9240 }
9241 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009242 }
9243}
9244
9245/*
9246 * "strdisplaywidth()" function
9247 */
9248 static void
9249f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
9250{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009251 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009252 int col = 0;
9253
9254 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009255 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009256
9257 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
9258}
9259
9260/*
9261 * "strwidth()" function
9262 */
9263 static void
9264f_strwidth(typval_T *argvars, typval_T *rettv)
9265{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009266 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009267
Bram Moolenaar13505972019-01-24 15:04:48 +01009268 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009269}
9270
9271/*
9272 * "strcharpart()" function
9273 */
9274 static void
9275f_strcharpart(typval_T *argvars, typval_T *rettv)
9276{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009277 char_u *p;
9278 int nchar;
9279 int nbyte = 0;
9280 int charlen;
9281 int len = 0;
9282 int slen;
9283 int error = FALSE;
9284
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009285 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009286 slen = (int)STRLEN(p);
9287
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009288 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009289 if (!error)
9290 {
9291 if (nchar > 0)
9292 while (nchar > 0 && nbyte < slen)
9293 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009294 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009295 --nchar;
9296 }
9297 else
9298 nbyte = nchar;
9299 if (argvars[2].v_type != VAR_UNKNOWN)
9300 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009301 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009302 while (charlen > 0 && nbyte + len < slen)
9303 {
9304 int off = nbyte + len;
9305
9306 if (off < 0)
9307 len += 1;
9308 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009309 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009310 --charlen;
9311 }
9312 }
9313 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009314 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009315 }
9316
9317 /*
9318 * Only return the overlap between the specified part and the actual
9319 * string.
9320 */
9321 if (nbyte < 0)
9322 {
9323 len += nbyte;
9324 nbyte = 0;
9325 }
9326 else if (nbyte > slen)
9327 nbyte = slen;
9328 if (len < 0)
9329 len = 0;
9330 else if (nbyte + len > slen)
9331 len = slen - nbyte;
9332
9333 rettv->v_type = VAR_STRING;
9334 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009335}
9336
9337/*
9338 * "strpart()" function
9339 */
9340 static void
9341f_strpart(typval_T *argvars, typval_T *rettv)
9342{
9343 char_u *p;
9344 int n;
9345 int len;
9346 int slen;
9347 int error = FALSE;
9348
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009349 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009350 slen = (int)STRLEN(p);
9351
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009352 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009353 if (error)
9354 len = 0;
9355 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009356 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009357 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009358 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009359
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009360 // Only return the overlap between the specified part and the actual
9361 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009362 if (n < 0)
9363 {
9364 len += n;
9365 n = 0;
9366 }
9367 else if (n > slen)
9368 n = slen;
9369 if (len < 0)
9370 len = 0;
9371 else if (n + len > slen)
9372 len = slen - n;
9373
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009374 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9375 {
9376 int off;
9377
9378 // length in characters
9379 for (off = n; off < slen && len > 0; --len)
9380 off += mb_ptr2len(p + off);
9381 len = off - n;
9382 }
9383
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009384 rettv->v_type = VAR_STRING;
9385 rettv->vval.v_string = vim_strnsave(p + n, len);
9386}
9387
9388/*
9389 * "strridx()" function
9390 */
9391 static void
9392f_strridx(typval_T *argvars, typval_T *rettv)
9393{
9394 char_u buf[NUMBUFLEN];
9395 char_u *needle;
9396 char_u *haystack;
9397 char_u *rest;
9398 char_u *lastmatch = NULL;
9399 int haystack_len, end_idx;
9400
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009401 needle = tv_get_string_chk(&argvars[1]);
9402 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009403
9404 rettv->vval.v_number = -1;
9405 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009406 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009407
9408 haystack_len = (int)STRLEN(haystack);
9409 if (argvars[2].v_type != VAR_UNKNOWN)
9410 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009411 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009412 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009413 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009414 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009415 }
9416 else
9417 end_idx = haystack_len;
9418
9419 if (*needle == NUL)
9420 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009421 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009422 lastmatch = haystack + end_idx;
9423 }
9424 else
9425 {
9426 for (rest = haystack; *rest != '\0'; ++rest)
9427 {
9428 rest = (char_u *)strstr((char *)rest, (char *)needle);
9429 if (rest == NULL || rest > haystack + end_idx)
9430 break;
9431 lastmatch = rest;
9432 }
9433 }
9434
9435 if (lastmatch == NULL)
9436 rettv->vval.v_number = -1;
9437 else
9438 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9439}
9440
9441/*
9442 * "strtrans()" function
9443 */
9444 static void
9445f_strtrans(typval_T *argvars, typval_T *rettv)
9446{
9447 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009448 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009449}
9450
9451/*
9452 * "submatch()" function
9453 */
9454 static void
9455f_submatch(typval_T *argvars, typval_T *rettv)
9456{
9457 int error = FALSE;
9458 int no;
9459 int retList = 0;
9460
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009461 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009462 if (error)
9463 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009464 if (no < 0 || no >= NSUBEXP)
9465 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009466 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009467 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009468 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009469 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009470 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009471 if (error)
9472 return;
9473
9474 if (retList == 0)
9475 {
9476 rettv->v_type = VAR_STRING;
9477 rettv->vval.v_string = reg_submatch(no);
9478 }
9479 else
9480 {
9481 rettv->v_type = VAR_LIST;
9482 rettv->vval.v_list = reg_submatch_list(no);
9483 }
9484}
9485
9486/*
9487 * "substitute()" function
9488 */
9489 static void
9490f_substitute(typval_T *argvars, typval_T *rettv)
9491{
9492 char_u patbuf[NUMBUFLEN];
9493 char_u subbuf[NUMBUFLEN];
9494 char_u flagsbuf[NUMBUFLEN];
9495
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009496 char_u *str = tv_get_string_chk(&argvars[0]);
9497 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009498 char_u *sub = NULL;
9499 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009500 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009501
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009502 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9503 expr = &argvars[2];
9504 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009505 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009506
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009507 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009508 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9509 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009510 rettv->vval.v_string = NULL;
9511 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009512 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009513}
9514
9515/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009516 * "swapinfo(swap_filename)" function
9517 */
9518 static void
9519f_swapinfo(typval_T *argvars, typval_T *rettv)
9520{
9521 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009522 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009523}
9524
9525/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009526 * "swapname(expr)" function
9527 */
9528 static void
9529f_swapname(typval_T *argvars, typval_T *rettv)
9530{
9531 buf_T *buf;
9532
9533 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009534 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009535 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9536 || buf->b_ml.ml_mfp->mf_fname == NULL)
9537 rettv->vval.v_string = NULL;
9538 else
9539 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9540}
9541
9542/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009543 * "synID(lnum, col, trans)" function
9544 */
9545 static void
9546f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9547{
9548 int id = 0;
9549#ifdef FEAT_SYN_HL
9550 linenr_T lnum;
9551 colnr_T col;
9552 int trans;
9553 int transerr = FALSE;
9554
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009555 lnum = tv_get_lnum(argvars); // -1 on type error
9556 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009557 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009558
9559 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9560 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9561 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9562#endif
9563
9564 rettv->vval.v_number = id;
9565}
9566
9567/*
9568 * "synIDattr(id, what [, mode])" function
9569 */
9570 static void
9571f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9572{
9573 char_u *p = NULL;
9574#ifdef FEAT_SYN_HL
9575 int id;
9576 char_u *what;
9577 char_u *mode;
9578 char_u modebuf[NUMBUFLEN];
9579 int modec;
9580
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009581 id = (int)tv_get_number(&argvars[0]);
9582 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009583 if (argvars[2].v_type != VAR_UNKNOWN)
9584 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009585 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009586 modec = TOLOWER_ASC(mode[0]);
9587 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009588 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009589 }
9590 else
9591 {
9592#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9593 if (USE_24BIT)
9594 modec = 'g';
9595 else
9596#endif
9597 if (t_colors > 1)
9598 modec = 'c';
9599 else
9600 modec = 't';
9601 }
9602
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009603 switch (TOLOWER_ASC(what[0]))
9604 {
9605 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009606 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009607 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009608 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009609 p = highlight_has_attr(id, HL_BOLD, modec);
9610 break;
9611
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009612 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009613 p = highlight_color(id, what, modec);
9614 break;
9615
9616 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009617 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009618 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009619 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009620 p = highlight_has_attr(id, HL_ITALIC, modec);
9621 break;
9622
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009623 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009624 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009625 break;
9626
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009627 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009628 p = highlight_has_attr(id, HL_INVERSE, modec);
9629 break;
9630
9631 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009632 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009633 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009634 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009635 else if (TOLOWER_ASC(what[1]) == 't' &&
9636 TOLOWER_ASC(what[2]) == 'r')
9637 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009638 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009639 p = highlight_has_attr(id, HL_STANDOUT, modec);
9640 break;
9641
9642 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009643 if (TOLOWER_ASC(what[1]) == 'l') // ul
9644 p = highlight_color(id, what, modec);
9645 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009646 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009647 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9648 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009649 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009650 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9651 break;
9652 }
9653
9654 if (p != NULL)
9655 p = vim_strsave(p);
9656#endif
9657 rettv->v_type = VAR_STRING;
9658 rettv->vval.v_string = p;
9659}
9660
9661/*
9662 * "synIDtrans(id)" function
9663 */
9664 static void
9665f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9666{
9667 int id;
9668
9669#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009670 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009671
9672 if (id > 0)
9673 id = syn_get_final_id(id);
9674 else
9675#endif
9676 id = 0;
9677
9678 rettv->vval.v_number = id;
9679}
9680
9681/*
9682 * "synconcealed(lnum, col)" function
9683 */
9684 static void
9685f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9686{
9687#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9688 linenr_T lnum;
9689 colnr_T col;
9690 int syntax_flags = 0;
9691 int cchar;
9692 int matchid = 0;
9693 char_u str[NUMBUFLEN];
9694#endif
9695
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009696 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009697
9698#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009699 lnum = tv_get_lnum(argvars); // -1 on type error
9700 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009701
Bram Moolenaara80faa82020-04-12 19:37:17 +02009702 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009703
9704 if (rettv_list_alloc(rettv) != FAIL)
9705 {
9706 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9707 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9708 && curwin->w_p_cole > 0)
9709 {
9710 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9711 syntax_flags = get_syntax_info(&matchid);
9712
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009713 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009714 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9715 {
9716 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009717 if (cchar == NUL && curwin->w_p_cole == 1)
9718 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009719 if (cchar != NUL)
9720 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009721 if (has_mbyte)
9722 (*mb_char2bytes)(cchar, str);
9723 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009724 str[0] = cchar;
9725 }
9726 }
9727 }
9728
9729 list_append_number(rettv->vval.v_list,
9730 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009731 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009732 list_append_string(rettv->vval.v_list, str, -1);
9733 list_append_number(rettv->vval.v_list, matchid);
9734 }
9735#endif
9736}
9737
9738/*
9739 * "synstack(lnum, col)" function
9740 */
9741 static void
9742f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9743{
9744#ifdef FEAT_SYN_HL
9745 linenr_T lnum;
9746 colnr_T col;
9747 int i;
9748 int id;
9749#endif
9750
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009751 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009752
9753#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009754 lnum = tv_get_lnum(argvars); // -1 on type error
9755 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009756
9757 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9758 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9759 && rettv_list_alloc(rettv) != FAIL)
9760 {
9761 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9762 for (i = 0; ; ++i)
9763 {
9764 id = syn_get_stack_item(i);
9765 if (id < 0)
9766 break;
9767 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9768 break;
9769 }
9770 }
9771#endif
9772}
9773
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009774/*
9775 * "tabpagebuflist()" function
9776 */
9777 static void
9778f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9779{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009780 tabpage_T *tp;
9781 win_T *wp = NULL;
9782
9783 if (argvars[0].v_type == VAR_UNKNOWN)
9784 wp = firstwin;
9785 else
9786 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009787 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009788 if (tp != NULL)
9789 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9790 }
9791 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9792 {
9793 for (; wp != NULL; wp = wp->w_next)
9794 if (list_append_number(rettv->vval.v_list,
9795 wp->w_buffer->b_fnum) == FAIL)
9796 break;
9797 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009798}
9799
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009800/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009801 * "tagfiles()" function
9802 */
9803 static void
9804f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9805{
9806 char_u *fname;
9807 tagname_T tn;
9808 int first;
9809
9810 if (rettv_list_alloc(rettv) == FAIL)
9811 return;
9812 fname = alloc(MAXPATHL);
9813 if (fname == NULL)
9814 return;
9815
9816 for (first = TRUE; ; first = FALSE)
9817 if (get_tagfname(&tn, first, fname) == FAIL
9818 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9819 break;
9820 tagname_free(&tn);
9821 vim_free(fname);
9822}
9823
9824/*
9825 * "taglist()" function
9826 */
9827 static void
9828f_taglist(typval_T *argvars, typval_T *rettv)
9829{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009830 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009831 char_u *tag_pattern;
9832
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009833 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009834
9835 rettv->vval.v_number = FALSE;
9836 if (*tag_pattern == NUL)
9837 return;
9838
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009839 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009840 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009841 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009842 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009843}
9844
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009845#ifdef FEAT_FLOAT
9846/*
9847 * "tan()" function
9848 */
9849 static void
9850f_tan(typval_T *argvars, typval_T *rettv)
9851{
9852 float_T f = 0.0;
9853
9854 rettv->v_type = VAR_FLOAT;
9855 if (get_float_arg(argvars, &f) == OK)
9856 rettv->vval.v_float = tan(f);
9857 else
9858 rettv->vval.v_float = 0.0;
9859}
9860
9861/*
9862 * "tanh()" function
9863 */
9864 static void
9865f_tanh(typval_T *argvars, typval_T *rettv)
9866{
9867 float_T f = 0.0;
9868
9869 rettv->v_type = VAR_FLOAT;
9870 if (get_float_arg(argvars, &f) == OK)
9871 rettv->vval.v_float = tanh(f);
9872 else
9873 rettv->vval.v_float = 0.0;
9874}
9875#endif
9876
9877/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009878 * "tolower(string)" function
9879 */
9880 static void
9881f_tolower(typval_T *argvars, typval_T *rettv)
9882{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009883 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009884 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009885}
9886
9887/*
9888 * "toupper(string)" function
9889 */
9890 static void
9891f_toupper(typval_T *argvars, typval_T *rettv)
9892{
9893 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009894 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009895}
9896
9897/*
9898 * "tr(string, fromstr, tostr)" function
9899 */
9900 static void
9901f_tr(typval_T *argvars, typval_T *rettv)
9902{
9903 char_u *in_str;
9904 char_u *fromstr;
9905 char_u *tostr;
9906 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009907 int inlen;
9908 int fromlen;
9909 int tolen;
9910 int idx;
9911 char_u *cpstr;
9912 int cplen;
9913 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009914 char_u buf[NUMBUFLEN];
9915 char_u buf2[NUMBUFLEN];
9916 garray_T ga;
9917
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009918 in_str = tv_get_string(&argvars[0]);
9919 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9920 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009922 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009923 rettv->v_type = VAR_STRING;
9924 rettv->vval.v_string = NULL;
9925 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009926 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009927 ga_init2(&ga, (int)sizeof(char), 80);
9928
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009929 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009930 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009931 if (STRLEN(fromstr) != STRLEN(tostr))
9932 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009933error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009934 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009935 ga_clear(&ga);
9936 return;
9937 }
9938
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009939 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009940 while (*in_str != NUL)
9941 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009942 if (has_mbyte)
9943 {
9944 inlen = (*mb_ptr2len)(in_str);
9945 cpstr = in_str;
9946 cplen = inlen;
9947 idx = 0;
9948 for (p = fromstr; *p != NUL; p += fromlen)
9949 {
9950 fromlen = (*mb_ptr2len)(p);
9951 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9952 {
9953 for (p = tostr; *p != NUL; p += tolen)
9954 {
9955 tolen = (*mb_ptr2len)(p);
9956 if (idx-- == 0)
9957 {
9958 cplen = tolen;
9959 cpstr = p;
9960 break;
9961 }
9962 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009963 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009964 goto error;
9965 break;
9966 }
9967 ++idx;
9968 }
9969
9970 if (first && cpstr == in_str)
9971 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009972 // Check that fromstr and tostr have the same number of
9973 // (multi-byte) characters. Done only once when a character
9974 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009975 first = FALSE;
9976 for (p = tostr; *p != NUL; p += tolen)
9977 {
9978 tolen = (*mb_ptr2len)(p);
9979 --idx;
9980 }
9981 if (idx != 0)
9982 goto error;
9983 }
9984
9985 (void)ga_grow(&ga, cplen);
9986 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
9987 ga.ga_len += cplen;
9988
9989 in_str += inlen;
9990 }
9991 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009992 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009993 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009994 p = vim_strchr(fromstr, *in_str);
9995 if (p != NULL)
9996 ga_append(&ga, tostr[p - fromstr]);
9997 else
9998 ga_append(&ga, *in_str);
9999 ++in_str;
10000 }
10001 }
10002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010003 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010004 (void)ga_grow(&ga, 1);
10005 ga_append(&ga, NUL);
10006
10007 rettv->vval.v_string = ga.ga_data;
10008}
10009
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010010/*
10011 * "trim({expr})" function
10012 */
10013 static void
10014f_trim(typval_T *argvars, typval_T *rettv)
10015{
10016 char_u buf1[NUMBUFLEN];
10017 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010018 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010019 char_u *mask = NULL;
10020 char_u *tail;
10021 char_u *prev;
10022 char_u *p;
10023 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010024 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010025
10026 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010027 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010028 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010029 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010030
10031 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010032 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010033 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010034
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010035 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010036 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010037 int error = 0;
10038
10039 // leading or trailing characters to trim
10040 dir = (int)tv_get_number_chk(&argvars[2], &error);
10041 if (error)
10042 return;
10043 if (dir < 0 || dir > 2)
10044 {
10045 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
10046 return;
10047 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010048 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010049 }
10050
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010051 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010052 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010053 // Trim leading characters
10054 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010055 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010056 c1 = PTR2CHAR(head);
10057 if (mask == NULL)
10058 {
10059 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010060 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010061 }
10062 else
10063 {
10064 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10065 if (c1 == PTR2CHAR(p))
10066 break;
10067 if (*p == NUL)
10068 break;
10069 }
10070 MB_PTR_ADV(head);
10071 }
10072 }
10073
10074 tail = head + STRLEN(head);
10075 if (dir == 0 || dir == 2)
10076 {
10077 // Trim trailing characters
10078 for (; tail > head; tail = prev)
10079 {
10080 prev = tail;
10081 MB_PTR_BACK(head, prev);
10082 c1 = PTR2CHAR(prev);
10083 if (mask == NULL)
10084 {
10085 if (c1 > ' ' && c1 != 0xa0)
10086 break;
10087 }
10088 else
10089 {
10090 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10091 if (c1 == PTR2CHAR(p))
10092 break;
10093 if (*p == NUL)
10094 break;
10095 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010096 }
10097 }
Bram Moolenaardf44a272020-06-07 20:49:05 +020010098 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010099}
10100
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010101#ifdef FEAT_FLOAT
10102/*
10103 * "trunc({float})" function
10104 */
10105 static void
10106f_trunc(typval_T *argvars, typval_T *rettv)
10107{
10108 float_T f = 0.0;
10109
10110 rettv->v_type = VAR_FLOAT;
10111 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010112 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010113 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
10114 else
10115 rettv->vval.v_float = 0.0;
10116}
10117#endif
10118
10119/*
10120 * "type(expr)" function
10121 */
10122 static void
10123f_type(typval_T *argvars, typval_T *rettv)
10124{
10125 int n = -1;
10126
10127 switch (argvars[0].v_type)
10128 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010129 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
10130 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010131 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010132 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
10133 case VAR_LIST: n = VAR_TYPE_LIST; break;
10134 case VAR_DICT: n = VAR_TYPE_DICT; break;
10135 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
10136 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
10137 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020010138 case VAR_JOB: n = VAR_TYPE_JOB; break;
10139 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010140 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010141 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +020010142 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010143 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +010010144 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010145 n = -1;
10146 break;
10147 }
10148 rettv->vval.v_number = n;
10149}
10150
10151/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010152 * "virtcol(string)" function
10153 */
10154 static void
10155f_virtcol(typval_T *argvars, typval_T *rettv)
10156{
10157 colnr_T vcol = 0;
10158 pos_T *fp;
10159 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010160 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010161
Bram Moolenaar6f02b002021-01-10 20:22:54 +010010162 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010163 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
10164 && fnum == curbuf->b_fnum)
10165 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010166 // Limit the column to a valid value, getvvcol() doesn't check.
10167 if (fp->col < 0)
10168 fp->col = 0;
10169 else
10170 {
10171 len = (int)STRLEN(ml_get(fp->lnum));
10172 if (fp->col > len)
10173 fp->col = len;
10174 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010175 getvvcol(curwin, fp, NULL, NULL, &vcol);
10176 ++vcol;
10177 }
10178
10179 rettv->vval.v_number = vcol;
10180}
10181
10182/*
10183 * "visualmode()" function
10184 */
10185 static void
10186f_visualmode(typval_T *argvars, typval_T *rettv)
10187{
10188 char_u str[2];
10189
10190 rettv->v_type = VAR_STRING;
10191 str[0] = curbuf->b_visual_mode_eval;
10192 str[1] = NUL;
10193 rettv->vval.v_string = vim_strsave(str);
10194
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010195 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010196 if (non_zero_arg(&argvars[0]))
10197 curbuf->b_visual_mode_eval = NUL;
10198}
10199
10200/*
10201 * "wildmenumode()" function
10202 */
10203 static void
10204f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10205{
10206#ifdef FEAT_WILDMENU
10207 if (wild_menu_showing)
10208 rettv->vval.v_number = 1;
10209#endif
10210}
10211
10212/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +010010213 * "windowsversion()" function
10214 */
10215 static void
10216f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10217{
10218 rettv->v_type = VAR_STRING;
10219 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
10220}
10221
10222/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010223 * "wordcount()" function
10224 */
10225 static void
10226f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
10227{
10228 if (rettv_dict_alloc(rettv) == FAIL)
10229 return;
10230 cursor_pos_info(rettv->vval.v_dict);
10231}
10232
10233/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010234 * "xor(expr, expr)" function
10235 */
10236 static void
10237f_xor(typval_T *argvars, typval_T *rettv)
10238{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010239 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
10240 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010241}
10242
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010243#endif // FEAT_EVAL