blob: 0b4b5021adbf1cec03791ed0a15c53b45cefebff [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020023#ifdef FEAT_FLOAT
24static void f_abs(typval_T *argvars, typval_T *rettv);
25static void f_acos(typval_T *argvars, typval_T *rettv);
26#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020027static void f_and(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028#ifdef FEAT_FLOAT
29static void f_asin(typval_T *argvars, typval_T *rettv);
30static void f_atan(typval_T *argvars, typval_T *rettv);
31static void f_atan2(typval_T *argvars, typval_T *rettv);
32#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010033#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020034static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010035static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010036# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010037static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010038# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010039#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_byte2line(typval_T *argvars, typval_T *rettv);
41static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
42static void f_byteidx(typval_T *argvars, typval_T *rettv);
43static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
44static void f_call(typval_T *argvars, typval_T *rettv);
45#ifdef FEAT_FLOAT
46static void f_ceil(typval_T *argvars, typval_T *rettv);
47#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_changenr(typval_T *argvars, typval_T *rettv);
49static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010050static void f_charcol(typval_T *argvars, typval_T *rettv);
Bram Moolenaar17793ef2020-12-28 12:56:58 +010051static void f_charidx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020053static void f_confirm(typval_T *argvars, typval_T *rettv);
54static void f_copy(typval_T *argvars, typval_T *rettv);
55#ifdef FEAT_FLOAT
56static void f_cos(typval_T *argvars, typval_T *rettv);
57static void f_cosh(typval_T *argvars, typval_T *rettv);
58#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010060#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020061static void f_debugbreak(typval_T *argvars, typval_T *rettv);
62#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020063static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020064static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010065static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020066static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020067static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_escape(typval_T *argvars, typval_T *rettv);
69static void f_eval(typval_T *argvars, typval_T *rettv);
70static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_exists(typval_T *argvars, typval_T *rettv);
73#ifdef FEAT_FLOAT
74static void f_exp(typval_T *argvars, typval_T *rettv);
75#endif
76static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020077static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020078static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020079#ifdef FEAT_FLOAT
80static void f_float2nr(typval_T *argvars, typval_T *rettv);
81static void f_floor(typval_T *argvars, typval_T *rettv);
82static void f_fmod(typval_T *argvars, typval_T *rettv);
83#endif
84static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020085static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020086static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_function(typval_T *argvars, typval_T *rettv);
88static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
89static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010090static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010091static void f_getcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020092static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020093static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020094static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010095static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_getpid(typval_T *argvars, typval_T *rettv);
97static void f_getcurpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010098static void f_getcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020099static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200100static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200101static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100103static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +0200104static void f_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200105static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
106static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200107static void f_hlID(typval_T *argvars, typval_T *rettv);
108static void f_hlexists(typval_T *argvars, typval_T *rettv);
109static void f_hostname(typval_T *argvars, typval_T *rettv);
110static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200111static void f_index(typval_T *argvars, typval_T *rettv);
112static void f_input(typval_T *argvars, typval_T *rettv);
113static void f_inputdialog(typval_T *argvars, typval_T *rettv);
114static void f_inputlist(typval_T *argvars, typval_T *rettv);
115static void f_inputrestore(typval_T *argvars, typval_T *rettv);
116static void f_inputsave(typval_T *argvars, typval_T *rettv);
117static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +0100118static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200119static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120static void f_islocked(typval_T *argvars, typval_T *rettv);
121#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200122static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200123static void f_isnan(typval_T *argvars, typval_T *rettv);
124#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200125static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
126static void f_len(typval_T *argvars, typval_T *rettv);
127static void f_libcall(typval_T *argvars, typval_T *rettv);
128static void f_libcallnr(typval_T *argvars, typval_T *rettv);
129static void f_line(typval_T *argvars, typval_T *rettv);
130static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200131#ifdef FEAT_FLOAT
132static void f_log(typval_T *argvars, typval_T *rettv);
133static void f_log10(typval_T *argvars, typval_T *rettv);
134#endif
135#ifdef FEAT_LUA
136static void f_luaeval(typval_T *argvars, typval_T *rettv);
137#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200138static void f_maparg(typval_T *argvars, typval_T *rettv);
139static void f_mapcheck(typval_T *argvars, typval_T *rettv);
140static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200141static void f_matchend(typval_T *argvars, typval_T *rettv);
142static void f_matchlist(typval_T *argvars, typval_T *rettv);
143static void f_matchstr(typval_T *argvars, typval_T *rettv);
144static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
145static void f_max(typval_T *argvars, typval_T *rettv);
146static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200147#ifdef FEAT_MZSCHEME
148static void f_mzeval(typval_T *argvars, typval_T *rettv);
149#endif
150static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
151static void f_nr2char(typval_T *argvars, typval_T *rettv);
152static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200153#ifdef FEAT_PERL
154static void f_perleval(typval_T *argvars, typval_T *rettv);
155#endif
156#ifdef FEAT_FLOAT
157static void f_pow(typval_T *argvars, typval_T *rettv);
158#endif
159static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
160static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200161static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200162static void f_pumvisible(typval_T *argvars, typval_T *rettv);
163#ifdef FEAT_PYTHON3
164static void f_py3eval(typval_T *argvars, typval_T *rettv);
165#endif
166#ifdef FEAT_PYTHON
167static void f_pyeval(typval_T *argvars, typval_T *rettv);
168#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100169#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
170static void f_pyxeval(typval_T *argvars, typval_T *rettv);
171#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100172static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100173static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200175static void f_reg_executing(typval_T *argvars, typval_T *rettv);
176static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_rename(typval_T *argvars, typval_T *rettv);
178static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200179#ifdef FEAT_FLOAT
180static void f_round(typval_T *argvars, typval_T *rettv);
181#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100182#ifdef FEAT_RUBY
183static void f_rubyeval(typval_T *argvars, typval_T *rettv);
184#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200185static void f_screenattr(typval_T *argvars, typval_T *rettv);
186static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100187static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200188static void f_screencol(typval_T *argvars, typval_T *rettv);
189static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100190static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200191static void f_search(typval_T *argvars, typval_T *rettv);
192static void f_searchdecl(typval_T *argvars, typval_T *rettv);
193static void f_searchpair(typval_T *argvars, typval_T *rettv);
194static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
195static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100196static void f_setcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100198static void f_setcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200199static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100203static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200204#ifdef FEAT_CRYPT
205static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200206#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200207static void f_shellescape(typval_T *argvars, typval_T *rettv);
208static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200209#ifdef FEAT_FLOAT
210static void f_sin(typval_T *argvars, typval_T *rettv);
211static void f_sinh(typval_T *argvars, typval_T *rettv);
212#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200213static void f_soundfold(typval_T *argvars, typval_T *rettv);
214static void f_spellbadword(typval_T *argvars, typval_T *rettv);
215static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
216static void f_split(typval_T *argvars, typval_T *rettv);
217#ifdef FEAT_FLOAT
218static void f_sqrt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100219#endif
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100220static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100221#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200222static void f_str2float(typval_T *argvars, typval_T *rettv);
223#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200224static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200225static void f_str2nr(typval_T *argvars, typval_T *rettv);
226static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200227static void f_strgetchar(typval_T *argvars, typval_T *rettv);
228static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200229static void f_strlen(typval_T *argvars, typval_T *rettv);
230static void f_strcharpart(typval_T *argvars, typval_T *rettv);
231static void f_strpart(typval_T *argvars, typval_T *rettv);
232static void f_strridx(typval_T *argvars, typval_T *rettv);
233static void f_strtrans(typval_T *argvars, typval_T *rettv);
234static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
235static void f_strwidth(typval_T *argvars, typval_T *rettv);
236static void f_submatch(typval_T *argvars, typval_T *rettv);
237static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200238static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200239static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200240static void f_synID(typval_T *argvars, typval_T *rettv);
241static void f_synIDattr(typval_T *argvars, typval_T *rettv);
242static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
243static void f_synstack(typval_T *argvars, typval_T *rettv);
244static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200246static void f_taglist(typval_T *argvars, typval_T *rettv);
247static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200248#ifdef FEAT_FLOAT
249static void f_tan(typval_T *argvars, typval_T *rettv);
250static void f_tanh(typval_T *argvars, typval_T *rettv);
251#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200252static void f_tolower(typval_T *argvars, typval_T *rettv);
253static void f_toupper(typval_T *argvars, typval_T *rettv);
254static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100255static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200256#ifdef FEAT_FLOAT
257static void f_trunc(typval_T *argvars, typval_T *rettv);
258#endif
259static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200260static void f_virtcol(typval_T *argvars, typval_T *rettv);
261static void f_visualmode(typval_T *argvars, typval_T *rettv);
262static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100263static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200264static void f_wordcount(typval_T *argvars, typval_T *rettv);
265static void f_xor(typval_T *argvars, typval_T *rettv);
266
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100267
Bram Moolenaar94738d82020-10-21 14:25:07 +0200268/*
269 * Functions that check the argument type of a builtin function.
270 * Each function returns FAIL and gives an error message if the type is wrong.
271 */
272
273// Context passed to an arg_ function.
274typedef struct {
Bram Moolenaarca174532020-10-21 16:42:22 +0200275 int arg_count; // actual argument count
276 type_T **arg_types; // list of argument types
277 int arg_idx; // current argument index (first arg is zero)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100278 cctx_T *arg_cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +0200279} argcontext_T;
280
281// A function to check one argument type. The first argument is the type to
282// check. If needed, other argument types can be obtained with the context.
283// E.g. if "arg_idx" is 1, then (type - 1) is the first argument type.
284typedef int (*argcheck_T)(type_T *, argcontext_T *);
285
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100286/*
Bram Moolenaar351ead02021-01-16 16:07:01 +0100287 * Call need_type() to check an argument type.
288 */
289 static int
290check_arg_type(
291 type_T *expected,
292 type_T *actual,
293 argcontext_T *context)
294{
295 // TODO: would be useful to know if "actual" is a constant and pass it to
296 // need_type() to get a compile time error if possible.
297 return need_type(actual, expected,
298 context->arg_idx - context->arg_count, context->arg_idx + 1,
299 context->arg_cctx, FALSE, FALSE);
300}
301
302/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100303 * Check "type" is a float or a number.
304 */
Bram Moolenaar94738d82020-10-21 14:25:07 +0200305 static int
306arg_float_or_nr(type_T *type, argcontext_T *context)
307{
Bram Moolenaarca174532020-10-21 16:42:22 +0200308 if (type->tt_type == VAR_ANY
309 || type->tt_type == VAR_FLOAT || type->tt_type == VAR_NUMBER)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200310 return OK;
311 arg_type_mismatch(&t_number, type, context->arg_idx + 1);
312 return FAIL;
313}
314
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100315/*
316 * Check "type" is a number.
317 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200318 static int
319arg_number(type_T *type, argcontext_T *context)
320{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100321 return check_arg_type(&t_number, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200322}
323
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100324/*
325 * Check "type" is a string.
326 */
327 static int
328arg_string(type_T *type, argcontext_T *context)
329{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100330 return check_arg_type(&t_string, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100331}
332
333/*
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100334 * Check "type" is a bool or number 0 or 1.
335 */
336 static int
337arg_bool(type_T *type, argcontext_T *context)
338{
339 if (type->tt_type == VAR_ANY
340 || type->tt_type == VAR_NUMBER || type->tt_type == VAR_BOOL)
341 return OK;
342 return check_arg_type(&t_bool, type, context);
343}
344
345/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100346 * Check "type" is a list or a blob.
347 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200348 static int
349arg_list_or_blob(type_T *type, argcontext_T *context)
350{
351 if (type->tt_type == VAR_ANY
352 || type->tt_type == VAR_LIST || type->tt_type == VAR_BLOB)
353 return OK;
354 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
355 return FAIL;
356}
357
358/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100359 * Check "type" is a list or a dict.
360 */
361 static int
362arg_list_or_dict(type_T *type, argcontext_T *context)
363{
364 if (type->tt_type == VAR_ANY
365 || type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
366 return OK;
367 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
368 return FAIL;
369}
370
371/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100372 * Check "type" is the same type as the previous argument.
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100373 * Must not be used for the first argcheck_T entry.
374 */
375 static int
376arg_same_as_prev(type_T *type, argcontext_T *context)
377{
378 type_T *prev_type = context->arg_types[context->arg_idx - 1];
379
Bram Moolenaar351ead02021-01-16 16:07:01 +0100380 return check_arg_type(prev_type, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100381}
382
383/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100384 * Check "type" is the same basic type as the previous argument, checks list or
385 * dict vs other type, but not member type.
386 * Must not be used for the first argcheck_T entry.
387 */
388 static int
389arg_same_struct_as_prev(type_T *type, argcontext_T *context)
390{
391 type_T *prev_type = context->arg_types[context->arg_idx - 1];
392
393 if (prev_type->tt_type != context->arg_types[context->arg_idx]->tt_type)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100394 return check_arg_type(prev_type, type, context);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100395 return OK;
396}
397
398/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100399 * Check "type" is an item of the list or blob of the previous arg.
Bram Moolenaarca174532020-10-21 16:42:22 +0200400 * Must not be used for the first argcheck_T entry.
401 */
402 static int
403arg_item_of_prev(type_T *type, argcontext_T *context)
404{
405 type_T *prev_type = context->arg_types[context->arg_idx - 1];
406 type_T *expected;
407
408 if (prev_type->tt_type == VAR_LIST)
409 expected = prev_type->tt_member;
410 else if (prev_type->tt_type == VAR_BLOB)
411 expected = &t_number;
412 else
413 // probably VAR_ANY, can't check
414 return OK;
415
Bram Moolenaar351ead02021-01-16 16:07:01 +0100416 return check_arg_type(expected, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200417}
418
Bram Moolenaar94738d82020-10-21 14:25:07 +0200419/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100420 * Check "type" which is the third argument of extend().
421 */
422 static int
423arg_extend3(type_T *type, argcontext_T *context)
424{
425 type_T *first_type = context->arg_types[context->arg_idx - 2];
426
427 if (first_type->tt_type == VAR_LIST)
428 return arg_number(type, context);
429 if (first_type->tt_type == VAR_DICT)
430 return arg_string(type, context);
431 return OK;
432}
433
434
435/*
Bram Moolenaar94738d82020-10-21 14:25:07 +0200436 * Lists of functions that check the argument types of a builtin function.
437 */
Bram Moolenaar80ad3e22021-01-31 20:48:58 +0100438argcheck_T arg1_string[] = {arg_string};
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100439argcheck_T arg3_string_nr_bool[] = {arg_string, arg_number, arg_bool};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200440argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100441argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev};
442argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3};
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100443argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
Bram Moolenaarca174532020-10-21 16:42:22 +0200444argcheck_T arg3_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200445
446/*
447 * Functions that return the return type of a builtin function.
Bram Moolenaara1224cb2020-10-22 12:31:49 +0200448 * Note that "argtypes" is NULL if "argcount" is zero.
Bram Moolenaar94738d82020-10-21 14:25:07 +0200449 */
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100450 static type_T *
451ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
452{
453 return &t_void;
454}
455 static type_T *
456ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
457{
458 return &t_any;
459}
460 static type_T *
Bram Moolenaar403dc312020-10-17 19:29:51 +0200461ret_bool(int argcount UNUSED, type_T **argtypes UNUSED)
462{
463 return &t_bool;
464}
465 static type_T *
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100466ret_number_bool(int argcount UNUSED, type_T **argtypes UNUSED)
467{
468 return &t_number_bool;
469}
470 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100471ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
472{
473 return &t_number;
474}
475 static type_T *
476ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
477{
478 return &t_float;
479}
480 static type_T *
481ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
482{
483 return &t_string;
484}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200485 static type_T *
486ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100487{
488 return &t_list_any;
489}
490 static type_T *
491ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
492{
493 return &t_list_number;
494}
495 static type_T *
496ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
497{
498 return &t_list_string;
499}
500 static type_T *
501ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
502{
503 return &t_list_dict_any;
504}
505 static type_T *
506ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
507{
508 return &t_dict_any;
509}
510 static type_T *
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100511ret_job_info(int argcount, type_T **argtypes UNUSED)
512{
513 if (argcount == 0)
514 return &t_list_job;
515 return &t_dict_any;
516}
517 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100518ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
519{
520 return &t_dict_number;
521}
522 static type_T *
523ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
524{
525 return &t_dict_string;
526}
527 static type_T *
528ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
529{
530 return &t_blob;
531}
532 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200533ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100534{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200535 return &t_func_any;
536}
537 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100538ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
539{
540 return &t_channel;
541}
542 static type_T *
543ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
544{
545 return &t_job;
546}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200547 static type_T *
548ret_first_arg(int argcount, type_T **argtypes)
549{
550 if (argcount > 0)
551 return argtypes[0];
552 return &t_void;
553}
Bram Moolenaarea696852020-11-09 18:31:39 +0100554// for map(): returns first argument but item type may differ
555 static type_T *
556ret_first_cont(int argcount UNUSED, type_T **argtypes)
557{
558 if (argtypes[0]->tt_type == VAR_LIST)
559 return &t_list_any;
560 if (argtypes[0]->tt_type == VAR_DICT)
561 return &t_dict_any;
562 if (argtypes[0]->tt_type == VAR_BLOB)
563 return argtypes[0];
564 return &t_any;
565}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200566
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200567/*
568 * Used for getqflist(): returns list if there is no argument, dict if there is
569 * one.
570 */
571 static type_T *
572ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
573{
574 if (argcount > 0)
575 return &t_dict_any;
576 return &t_list_dict_any;
577}
578
579/*
580 * Used for getloclist(): returns list if there is one argument, dict if there
581 * are two.
582 */
583 static type_T *
584ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
585{
586 if (argcount > 1)
587 return &t_dict_any;
588 return &t_list_dict_any;
589}
590
Bram Moolenaar846178a2020-07-05 17:04:13 +0200591 static type_T *
592ret_argv(int argcount, type_T **argtypes UNUSED)
593{
594 // argv() returns list of strings
595 if (argcount == 0)
596 return &t_list_string;
597
598 // argv(0) returns a string, but argv(-1] returns a list
599 return &t_any;
600}
601
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200602 static type_T *
603ret_remove(int argcount UNUSED, type_T **argtypes)
604{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200605 if (argtypes != NULL)
606 {
607 if (argtypes[0]->tt_type == VAR_LIST
608 || argtypes[0]->tt_type == VAR_DICT)
609 return argtypes[0]->tt_member;
610 if (argtypes[0]->tt_type == VAR_BLOB)
611 return &t_number;
612 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200613 return &t_any;
614}
615
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200616 static type_T *
617ret_getreg(int argcount, type_T **argtypes UNUSED)
618{
619 // Assume that if the third argument is passed it's non-zero
620 if (argcount == 3)
621 return &t_list_string;
622 return &t_string;
623}
624
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200625 static type_T *
626ret_maparg(int argcount, type_T **argtypes UNUSED)
627{
628 // Assume that if the fourth argument is passed it's non-zero
629 if (argcount == 4)
630 return &t_dict_any;
631 return &t_string;
632}
633
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100634static type_T *ret_f_function(int argcount, type_T **argtypes);
635
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636/*
637 * Array with names and number of arguments of all internal functions
638 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
639 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200640typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200641{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200642 char *f_name; // function name
643 char f_min_argc; // minimal number of arguments
644 char f_max_argc; // maximal number of arguments
645 char f_argtype; // for method: FEARG_ values
Bram Moolenaar94738d82020-10-21 14:25:07 +0200646 argcheck_T *f_argcheck; // list of functions to check argument types
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100647 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
648 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200649 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200650 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200651} funcentry_T;
652
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200653// values for f_argtype; zero means it cannot be used as a method
654#define FEARG_1 1 // base is the first argument
655#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200656#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200657#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200658#define FEARG_LAST 9 // base is the last argument
659
Bram Moolenaar15c47602020-03-26 22:16:48 +0100660#ifdef FEAT_FLOAT
661# define FLOAT_FUNC(name) name
662#else
663# define FLOAT_FUNC(name) NULL
664#endif
665#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
666# define MATH_FUNC(name) name
667#else
668# define MATH_FUNC(name) NULL
669#endif
670#ifdef FEAT_TIMERS
671# define TIMER_FUNC(name) name
672#else
673# define TIMER_FUNC(name) NULL
674#endif
675#ifdef FEAT_JOB_CHANNEL
676# define JOB_FUNC(name) name
677#else
678# define JOB_FUNC(name) NULL
679#endif
680#ifdef FEAT_PROP_POPUP
681# define PROP_FUNC(name) name
682#else
683# define PROP_FUNC(name) NULL
684#endif
685#ifdef FEAT_SIGNS
686# define SIGN_FUNC(name) name
687#else
688# define SIGN_FUNC(name) NULL
689#endif
690#ifdef FEAT_SOUND
691# define SOUND_FUNC(name) name
692#else
693# define SOUND_FUNC(name) NULL
694#endif
695#ifdef FEAT_TERMINAL
696# define TERM_FUNC(name) name
697#else
698# define TERM_FUNC(name) NULL
699#endif
700
Bram Moolenaarac92e252019-08-03 21:58:38 +0200701static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200702{
Bram Moolenaar94738d82020-10-21 14:25:07 +0200703 {"abs", 1, 1, FEARG_1, arg1_float_or_nr,
704 ret_any, FLOAT_FUNC(f_abs)},
705 {"acos", 1, 1, FEARG_1, NULL,
706 ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100707 {"add", 2, 2, FEARG_1, NULL /* arg2_listblob_item */,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200708 ret_first_arg, f_add},
709 {"and", 2, 2, FEARG_1, NULL,
710 ret_number, f_and},
711 {"append", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100712 ret_number_bool, f_append},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200713 {"appendbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100714 ret_number_bool, f_appendbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200715 {"argc", 0, 1, 0, NULL,
716 ret_number, f_argc},
717 {"argidx", 0, 0, 0, NULL,
718 ret_number, f_argidx},
719 {"arglistid", 0, 2, 0, NULL,
720 ret_number, f_arglistid},
721 {"argv", 0, 2, 0, NULL,
722 ret_argv, f_argv},
723 {"asin", 1, 1, FEARG_1, NULL,
724 ret_float, FLOAT_FUNC(f_asin)},
725 {"assert_beeps", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100726 ret_number_bool, f_assert_beeps},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200727 {"assert_equal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100728 ret_number_bool, f_assert_equal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200729 {"assert_equalfile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100730 ret_number_bool, f_assert_equalfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200731 {"assert_exception", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100732 ret_number_bool, f_assert_exception},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200733 {"assert_fails", 1, 5, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100734 ret_number_bool, f_assert_fails},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200735 {"assert_false", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100736 ret_number_bool, f_assert_false},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200737 {"assert_inrange", 3, 4, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100738 ret_number_bool, f_assert_inrange},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200739 {"assert_match", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100740 ret_number_bool, f_assert_match},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200741 {"assert_notequal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100742 ret_number_bool, f_assert_notequal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200743 {"assert_notmatch", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100744 ret_number_bool, f_assert_notmatch},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200745 {"assert_report", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100746 ret_number_bool, f_assert_report},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200747 {"assert_true", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100748 ret_number_bool, f_assert_true},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200749 {"atan", 1, 1, FEARG_1, NULL,
750 ret_float, FLOAT_FUNC(f_atan)},
751 {"atan2", 2, 2, FEARG_1, NULL,
752 ret_float, FLOAT_FUNC(f_atan2)},
753 {"balloon_gettext", 0, 0, 0, NULL,
754 ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100755#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100756 f_balloon_gettext
757#else
758 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100759#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100760 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200761 {"balloon_show", 1, 1, FEARG_1, NULL,
762 ret_void,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100763#ifdef FEAT_BEVAL
764 f_balloon_show
765#else
766 NULL
767#endif
768 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200769 {"balloon_split", 1, 1, FEARG_1, NULL,
770 ret_list_string,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100771#if defined(FEAT_BEVAL_TERM)
772 f_balloon_split
773#else
774 NULL
775#endif
776 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200777 {"browse", 4, 4, 0, NULL,
778 ret_string, f_browse},
779 {"browsedir", 2, 2, 0, NULL,
780 ret_string, f_browsedir},
781 {"bufadd", 1, 1, FEARG_1, NULL,
782 ret_number, f_bufadd},
783 {"bufexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100784 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200785 {"buffer_exists", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100786 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200787 {"buffer_name", 0, 1, FEARG_1, NULL, // obsolete
788 ret_string, f_bufname},
789 {"buffer_number", 0, 1, FEARG_1, NULL, // obsolete
790 ret_number, f_bufnr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200791 {"buflisted", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100792 ret_number_bool, f_buflisted},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200793 {"bufload", 1, 1, FEARG_1, NULL,
794 ret_void, f_bufload},
795 {"bufloaded", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100796 ret_number_bool, f_bufloaded},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200797 {"bufname", 0, 1, FEARG_1, NULL,
798 ret_string, f_bufname},
799 {"bufnr", 0, 2, FEARG_1, NULL,
800 ret_number, f_bufnr},
801 {"bufwinid", 1, 1, FEARG_1, NULL,
802 ret_number, f_bufwinid},
803 {"bufwinnr", 1, 1, FEARG_1, NULL,
804 ret_number, f_bufwinnr},
805 {"byte2line", 1, 1, FEARG_1, NULL,
806 ret_number, f_byte2line},
807 {"byteidx", 2, 2, FEARG_1, NULL,
808 ret_number, f_byteidx},
809 {"byteidxcomp", 2, 2, FEARG_1, NULL,
810 ret_number, f_byteidxcomp},
811 {"call", 2, 3, FEARG_1, NULL,
812 ret_any, f_call},
813 {"ceil", 1, 1, FEARG_1, NULL,
814 ret_float, FLOAT_FUNC(f_ceil)},
815 {"ch_canread", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100816 ret_number_bool, JOB_FUNC(f_ch_canread)},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200817 {"ch_close", 1, 1, FEARG_1, NULL,
818 ret_void, JOB_FUNC(f_ch_close)},
819 {"ch_close_in", 1, 1, FEARG_1, NULL,
820 ret_void, JOB_FUNC(f_ch_close_in)},
821 {"ch_evalexpr", 2, 3, FEARG_1, NULL,
822 ret_any, JOB_FUNC(f_ch_evalexpr)},
823 {"ch_evalraw", 2, 3, FEARG_1, NULL,
824 ret_any, JOB_FUNC(f_ch_evalraw)},
825 {"ch_getbufnr", 2, 2, FEARG_1, NULL,
826 ret_number, JOB_FUNC(f_ch_getbufnr)},
827 {"ch_getjob", 1, 1, FEARG_1, NULL,
828 ret_job, JOB_FUNC(f_ch_getjob)},
829 {"ch_info", 1, 1, FEARG_1, NULL,
830 ret_dict_any, JOB_FUNC(f_ch_info)},
831 {"ch_log", 1, 2, FEARG_1, NULL,
832 ret_void, JOB_FUNC(f_ch_log)},
833 {"ch_logfile", 1, 2, FEARG_1, NULL,
834 ret_void, JOB_FUNC(f_ch_logfile)},
835 {"ch_open", 1, 2, FEARG_1, NULL,
836 ret_channel, JOB_FUNC(f_ch_open)},
837 {"ch_read", 1, 2, FEARG_1, NULL,
838 ret_string, JOB_FUNC(f_ch_read)},
839 {"ch_readblob", 1, 2, FEARG_1, NULL,
840 ret_blob, JOB_FUNC(f_ch_readblob)},
841 {"ch_readraw", 1, 2, FEARG_1, NULL,
842 ret_string, JOB_FUNC(f_ch_readraw)},
843 {"ch_sendexpr", 2, 3, FEARG_1, NULL,
844 ret_void, JOB_FUNC(f_ch_sendexpr)},
845 {"ch_sendraw", 2, 3, FEARG_1, NULL,
846 ret_void, JOB_FUNC(f_ch_sendraw)},
847 {"ch_setoptions", 2, 2, FEARG_1, NULL,
848 ret_void, JOB_FUNC(f_ch_setoptions)},
849 {"ch_status", 1, 2, FEARG_1, NULL,
850 ret_string, JOB_FUNC(f_ch_status)},
851 {"changenr", 0, 0, 0, NULL,
852 ret_number, f_changenr},
853 {"char2nr", 1, 2, FEARG_1, NULL,
854 ret_number, f_char2nr},
855 {"charclass", 1, 1, FEARG_1, NULL,
856 ret_number, f_charclass},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100857 {"charcol", 1, 1, FEARG_1, NULL,
858 ret_number, f_charcol},
Bram Moolenaar17793ef2020-12-28 12:56:58 +0100859 {"charidx", 2, 3, FEARG_1, NULL,
860 ret_number, f_charidx},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200861 {"chdir", 1, 1, FEARG_1, NULL,
862 ret_string, f_chdir},
863 {"cindent", 1, 1, FEARG_1, NULL,
864 ret_number, f_cindent},
865 {"clearmatches", 0, 1, FEARG_1, NULL,
866 ret_void, f_clearmatches},
867 {"col", 1, 1, FEARG_1, NULL,
868 ret_number, f_col},
869 {"complete", 2, 2, FEARG_2, NULL,
870 ret_void, f_complete},
871 {"complete_add", 1, 1, FEARG_1, NULL,
872 ret_number, f_complete_add},
873 {"complete_check", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100874 ret_number_bool, f_complete_check},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200875 {"complete_info", 0, 1, FEARG_1, NULL,
876 ret_dict_any, f_complete_info},
877 {"confirm", 1, 4, FEARG_1, NULL,
878 ret_number, f_confirm},
879 {"copy", 1, 1, FEARG_1, NULL,
880 ret_first_arg, f_copy},
881 {"cos", 1, 1, FEARG_1, NULL,
882 ret_float, FLOAT_FUNC(f_cos)},
883 {"cosh", 1, 1, FEARG_1, NULL,
884 ret_float, FLOAT_FUNC(f_cosh)},
885 {"count", 2, 4, FEARG_1, NULL,
886 ret_number, f_count},
887 {"cscope_connection",0,3, 0, NULL,
888 ret_number, f_cscope_connection},
889 {"cursor", 1, 3, FEARG_1, NULL,
890 ret_number, f_cursor},
891 {"debugbreak", 1, 1, FEARG_1, NULL,
892 ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100893#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100894 f_debugbreak
895#else
896 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200897#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100898 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200899 {"deepcopy", 1, 2, FEARG_1, NULL,
900 ret_first_arg, f_deepcopy},
901 {"delete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100902 ret_number_bool, f_delete},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200903 {"deletebufline", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100904 ret_number_bool, f_deletebufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200905 {"did_filetype", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100906 ret_number_bool, f_did_filetype},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200907 {"diff_filler", 1, 1, FEARG_1, NULL,
908 ret_number, f_diff_filler},
909 {"diff_hlID", 2, 2, FEARG_1, NULL,
910 ret_number, f_diff_hlID},
911 {"echoraw", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100912 ret_void, f_echoraw},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200913 {"empty", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100914 ret_number_bool, f_empty},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200915 {"environ", 0, 0, 0, NULL,
916 ret_dict_string, f_environ},
917 {"escape", 2, 2, FEARG_1, NULL,
918 ret_string, f_escape},
919 {"eval", 1, 1, FEARG_1, NULL,
920 ret_any, f_eval},
921 {"eventhandler", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100922 ret_number_bool, f_eventhandler},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200923 {"executable", 1, 1, FEARG_1, NULL,
924 ret_number, f_executable},
925 {"execute", 1, 2, FEARG_1, NULL,
926 ret_string, f_execute},
927 {"exepath", 1, 1, FEARG_1, NULL,
928 ret_string, f_exepath},
929 {"exists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100930 ret_number_bool, f_exists},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200931 {"exp", 1, 1, FEARG_1, NULL,
932 ret_float, FLOAT_FUNC(f_exp)},
933 {"expand", 1, 3, FEARG_1, NULL,
934 ret_any, f_expand},
935 {"expandcmd", 1, 1, FEARG_1, NULL,
936 ret_string, f_expandcmd},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100937 {"extend", 2, 3, FEARG_1, arg23_extend,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200938 ret_first_arg, f_extend},
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100939 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
940 ret_first_cont, f_extendnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200941 {"feedkeys", 1, 2, FEARG_1, NULL,
942 ret_void, f_feedkeys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200943 {"file_readable", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100944 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200945 {"filereadable", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100946 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200947 {"filewritable", 1, 1, FEARG_1, NULL,
948 ret_number, f_filewritable},
949 {"filter", 2, 2, FEARG_1, NULL,
950 ret_first_arg, f_filter},
951 {"finddir", 1, 3, FEARG_1, NULL,
952 ret_string, f_finddir},
953 {"findfile", 1, 3, FEARG_1, NULL,
954 ret_string, f_findfile},
955 {"flatten", 1, 2, FEARG_1, NULL,
956 ret_list_any, f_flatten},
Bram Moolenaar3b690062021-02-01 20:14:51 +0100957 {"flattennew", 1, 2, FEARG_1, NULL,
958 ret_list_any, f_flattennew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200959 {"float2nr", 1, 1, FEARG_1, NULL,
960 ret_number, FLOAT_FUNC(f_float2nr)},
961 {"floor", 1, 1, FEARG_1, NULL,
962 ret_float, FLOAT_FUNC(f_floor)},
963 {"fmod", 2, 2, FEARG_1, NULL,
964 ret_float, FLOAT_FUNC(f_fmod)},
965 {"fnameescape", 1, 1, FEARG_1, NULL,
966 ret_string, f_fnameescape},
967 {"fnamemodify", 2, 2, FEARG_1, NULL,
968 ret_string, f_fnamemodify},
969 {"foldclosed", 1, 1, FEARG_1, NULL,
970 ret_number, f_foldclosed},
971 {"foldclosedend", 1, 1, FEARG_1, NULL,
972 ret_number, f_foldclosedend},
973 {"foldlevel", 1, 1, FEARG_1, NULL,
974 ret_number, f_foldlevel},
975 {"foldtext", 0, 0, 0, NULL,
976 ret_string, f_foldtext},
977 {"foldtextresult", 1, 1, FEARG_1, NULL,
978 ret_string, f_foldtextresult},
979 {"foreground", 0, 0, 0, NULL,
980 ret_void, f_foreground},
981 {"funcref", 1, 3, FEARG_1, NULL,
982 ret_func_any, f_funcref},
983 {"function", 1, 3, FEARG_1, NULL,
984 ret_f_function, f_function},
985 {"garbagecollect", 0, 1, 0, NULL,
986 ret_void, f_garbagecollect},
987 {"get", 2, 3, FEARG_1, NULL,
988 ret_any, f_get},
989 {"getbufinfo", 0, 1, FEARG_1, NULL,
990 ret_list_dict_any, f_getbufinfo},
991 {"getbufline", 2, 3, FEARG_1, NULL,
992 ret_list_string, f_getbufline},
993 {"getbufvar", 2, 3, FEARG_1, NULL,
994 ret_any, f_getbufvar},
995 {"getchangelist", 0, 1, FEARG_1, NULL,
996 ret_list_any, f_getchangelist},
997 {"getchar", 0, 1, 0, NULL,
998 ret_number, f_getchar},
999 {"getcharmod", 0, 0, 0, NULL,
1000 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001001 {"getcharpos", 1, 1, FEARG_1, NULL,
1002 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001003 {"getcharsearch", 0, 0, 0, NULL,
1004 ret_dict_any, f_getcharsearch},
1005 {"getcmdline", 0, 0, 0, NULL,
1006 ret_string, f_getcmdline},
1007 {"getcmdpos", 0, 0, 0, NULL,
1008 ret_number, f_getcmdpos},
1009 {"getcmdtype", 0, 0, 0, NULL,
1010 ret_string, f_getcmdtype},
1011 {"getcmdwintype", 0, 0, 0, NULL,
1012 ret_string, f_getcmdwintype},
1013 {"getcompletion", 2, 3, FEARG_1, NULL,
1014 ret_list_string, f_getcompletion},
1015 {"getcurpos", 0, 1, FEARG_1, NULL,
1016 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001017 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
1018 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001019 {"getcwd", 0, 2, FEARG_1, NULL,
1020 ret_string, f_getcwd},
1021 {"getenv", 1, 1, FEARG_1, NULL,
1022 ret_string, f_getenv},
1023 {"getfontname", 0, 1, 0, NULL,
1024 ret_string, f_getfontname},
1025 {"getfperm", 1, 1, FEARG_1, NULL,
1026 ret_string, f_getfperm},
1027 {"getfsize", 1, 1, FEARG_1, NULL,
1028 ret_number, f_getfsize},
1029 {"getftime", 1, 1, FEARG_1, NULL,
1030 ret_number, f_getftime},
1031 {"getftype", 1, 1, FEARG_1, NULL,
1032 ret_string, f_getftype},
1033 {"getimstatus", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001034 ret_number_bool, f_getimstatus},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001035 {"getjumplist", 0, 2, FEARG_1, NULL,
1036 ret_list_any, f_getjumplist},
1037 {"getline", 1, 2, FEARG_1, NULL,
1038 ret_f_getline, f_getline},
1039 {"getloclist", 1, 2, 0, NULL,
1040 ret_list_or_dict_1, f_getloclist},
1041 {"getmarklist", 0, 1, FEARG_1, NULL,
1042 ret_list_dict_any, f_getmarklist},
1043 {"getmatches", 0, 1, 0, NULL,
1044 ret_list_dict_any, f_getmatches},
1045 {"getmousepos", 0, 0, 0, NULL,
1046 ret_dict_number, f_getmousepos},
1047 {"getpid", 0, 0, 0, NULL,
1048 ret_number, f_getpid},
1049 {"getpos", 1, 1, FEARG_1, NULL,
1050 ret_list_number, f_getpos},
1051 {"getqflist", 0, 1, 0, NULL,
1052 ret_list_or_dict_0, f_getqflist},
1053 {"getreg", 0, 3, FEARG_1, NULL,
1054 ret_getreg, f_getreg},
1055 {"getreginfo", 0, 1, FEARG_1, NULL,
1056 ret_dict_any, f_getreginfo},
1057 {"getregtype", 0, 1, FEARG_1, NULL,
1058 ret_string, f_getregtype},
1059 {"gettabinfo", 0, 1, FEARG_1, NULL,
1060 ret_list_dict_any, f_gettabinfo},
1061 {"gettabvar", 2, 3, FEARG_1, NULL,
1062 ret_any, f_gettabvar},
1063 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1064 ret_any, f_gettabwinvar},
1065 {"gettagstack", 0, 1, FEARG_1, NULL,
1066 ret_dict_any, f_gettagstack},
1067 {"gettext", 1, 1, FEARG_1, NULL,
1068 ret_string, f_gettext},
1069 {"getwininfo", 0, 1, FEARG_1, NULL,
1070 ret_list_dict_any, f_getwininfo},
1071 {"getwinpos", 0, 1, FEARG_1, NULL,
1072 ret_list_number, f_getwinpos},
1073 {"getwinposx", 0, 0, 0, NULL,
1074 ret_number, f_getwinposx},
1075 {"getwinposy", 0, 0, 0, NULL,
1076 ret_number, f_getwinposy},
1077 {"getwinvar", 2, 3, FEARG_1, NULL,
1078 ret_any, f_getwinvar},
1079 {"glob", 1, 4, FEARG_1, NULL,
1080 ret_any, f_glob},
1081 {"glob2regpat", 1, 1, FEARG_1, NULL,
1082 ret_string, f_glob2regpat},
1083 {"globpath", 2, 5, FEARG_2, NULL,
1084 ret_any, f_globpath},
1085 {"has", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001086 ret_number_bool, f_has},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001087 {"has_key", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001088 ret_number_bool, f_has_key},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001089 {"haslocaldir", 0, 2, FEARG_1, NULL,
1090 ret_number, f_haslocaldir},
1091 {"hasmapto", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001092 ret_number_bool, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001093 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1094 ret_number, f_hlID},
1095 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001096 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001097 {"histadd", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001098 ret_number_bool, f_histadd},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001099 {"histdel", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001100 ret_number_bool, f_histdel},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001101 {"histget", 1, 2, FEARG_1, NULL,
1102 ret_string, f_histget},
1103 {"histnr", 1, 1, FEARG_1, NULL,
1104 ret_number, f_histnr},
1105 {"hlID", 1, 1, FEARG_1, NULL,
1106 ret_number, f_hlID},
1107 {"hlexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001108 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001109 {"hostname", 0, 0, 0, NULL,
1110 ret_string, f_hostname},
1111 {"iconv", 3, 3, FEARG_1, NULL,
1112 ret_string, f_iconv},
1113 {"indent", 1, 1, FEARG_1, NULL,
1114 ret_number, f_indent},
1115 {"index", 2, 4, FEARG_1, NULL,
1116 ret_number, f_index},
1117 {"input", 1, 3, FEARG_1, NULL,
1118 ret_string, f_input},
1119 {"inputdialog", 1, 3, FEARG_1, NULL,
1120 ret_string, f_inputdialog},
1121 {"inputlist", 1, 1, FEARG_1, NULL,
1122 ret_number, f_inputlist},
1123 {"inputrestore", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001124 ret_number_bool, f_inputrestore},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001125 {"inputsave", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001126 ret_number_bool, f_inputsave},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001127 {"inputsecret", 1, 2, FEARG_1, NULL,
1128 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001129 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001130 ret_first_arg, f_insert},
1131 {"interrupt", 0, 0, 0, NULL,
1132 ret_void, f_interrupt},
1133 {"invert", 1, 1, FEARG_1, NULL,
1134 ret_number, f_invert},
1135 {"isdirectory", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001136 ret_number_bool, f_isdirectory},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001137 {"isinf", 1, 1, FEARG_1, NULL,
1138 ret_number, MATH_FUNC(f_isinf)},
1139 {"islocked", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001140 ret_number_bool, f_islocked},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001141 {"isnan", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001142 ret_number_bool, MATH_FUNC(f_isnan)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001143 {"items", 1, 1, FEARG_1, NULL,
1144 ret_list_any, f_items},
1145 {"job_getchannel", 1, 1, FEARG_1, NULL,
1146 ret_channel, JOB_FUNC(f_job_getchannel)},
1147 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001148 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001149 {"job_setoptions", 2, 2, FEARG_1, NULL,
1150 ret_void, JOB_FUNC(f_job_setoptions)},
1151 {"job_start", 1, 2, FEARG_1, NULL,
1152 ret_job, JOB_FUNC(f_job_start)},
1153 {"job_status", 1, 1, FEARG_1, NULL,
1154 ret_string, JOB_FUNC(f_job_status)},
1155 {"job_stop", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001156 ret_number_bool, JOB_FUNC(f_job_stop)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001157 {"join", 1, 2, FEARG_1, NULL,
1158 ret_string, f_join},
1159 {"js_decode", 1, 1, FEARG_1, NULL,
1160 ret_any, f_js_decode},
1161 {"js_encode", 1, 1, FEARG_1, NULL,
1162 ret_string, f_js_encode},
1163 {"json_decode", 1, 1, FEARG_1, NULL,
1164 ret_any, f_json_decode},
1165 {"json_encode", 1, 1, FEARG_1, NULL,
1166 ret_string, f_json_encode},
1167 {"keys", 1, 1, FEARG_1, NULL,
1168 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001169 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1170 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001171 {"len", 1, 1, FEARG_1, NULL,
1172 ret_number, f_len},
1173 {"libcall", 3, 3, FEARG_3, NULL,
1174 ret_string, f_libcall},
1175 {"libcallnr", 3, 3, FEARG_3, NULL,
1176 ret_number, f_libcallnr},
1177 {"line", 1, 2, FEARG_1, NULL,
1178 ret_number, f_line},
1179 {"line2byte", 1, 1, FEARG_1, NULL,
1180 ret_number, f_line2byte},
1181 {"lispindent", 1, 1, FEARG_1, NULL,
1182 ret_number, f_lispindent},
1183 {"list2str", 1, 2, FEARG_1, NULL,
1184 ret_string, f_list2str},
1185 {"listener_add", 1, 2, FEARG_2, NULL,
1186 ret_number, f_listener_add},
1187 {"listener_flush", 0, 1, FEARG_1, NULL,
1188 ret_void, f_listener_flush},
1189 {"listener_remove", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001190 ret_number_bool, f_listener_remove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001191 {"localtime", 0, 0, 0, NULL,
1192 ret_number, f_localtime},
1193 {"log", 1, 1, FEARG_1, NULL,
1194 ret_float, FLOAT_FUNC(f_log)},
1195 {"log10", 1, 1, FEARG_1, NULL,
1196 ret_float, FLOAT_FUNC(f_log10)},
1197 {"luaeval", 1, 2, FEARG_1, NULL,
1198 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001199#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001200 f_luaeval
1201#else
1202 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001203#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001204 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001205 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001206 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001207 {"maparg", 1, 4, FEARG_1, NULL,
1208 ret_maparg, f_maparg},
1209 {"mapcheck", 1, 3, FEARG_1, NULL,
1210 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001211 {"mapnew", 2, 2, FEARG_1, NULL,
1212 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001213 {"mapset", 3, 3, FEARG_1, NULL,
1214 ret_void, f_mapset},
1215 {"match", 2, 4, FEARG_1, NULL,
1216 ret_any, f_match},
1217 {"matchadd", 2, 5, FEARG_1, NULL,
1218 ret_number, f_matchadd},
1219 {"matchaddpos", 2, 5, FEARG_1, NULL,
1220 ret_number, f_matchaddpos},
1221 {"matcharg", 1, 1, FEARG_1, NULL,
1222 ret_list_string, f_matcharg},
1223 {"matchdelete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001224 ret_number_bool, f_matchdelete},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001225 {"matchend", 2, 4, FEARG_1, NULL,
1226 ret_number, f_matchend},
1227 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1228 ret_list_string, f_matchfuzzy},
1229 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1230 ret_list_any, f_matchfuzzypos},
1231 {"matchlist", 2, 4, FEARG_1, NULL,
1232 ret_list_string, f_matchlist},
1233 {"matchstr", 2, 4, FEARG_1, NULL,
1234 ret_string, f_matchstr},
1235 {"matchstrpos", 2, 4, FEARG_1, NULL,
1236 ret_list_any, f_matchstrpos},
1237 {"max", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001238 ret_number, f_max},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001239 {"menu_info", 1, 2, FEARG_1, NULL,
1240 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001241#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001242 f_menu_info
1243#else
1244 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001245#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001246 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001247 {"min", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001248 ret_number, f_min},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001249 {"mkdir", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001250 ret_number_bool, f_mkdir},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001251 {"mode", 0, 1, FEARG_1, NULL,
1252 ret_string, f_mode},
1253 {"mzeval", 1, 1, FEARG_1, NULL,
1254 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001255#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001256 f_mzeval
1257#else
1258 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001259#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001260 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001261 {"nextnonblank", 1, 1, FEARG_1, NULL,
1262 ret_number, f_nextnonblank},
1263 {"nr2char", 1, 2, FEARG_1, NULL,
1264 ret_string, f_nr2char},
1265 {"or", 2, 2, FEARG_1, NULL,
1266 ret_number, f_or},
1267 {"pathshorten", 1, 2, FEARG_1, NULL,
1268 ret_string, f_pathshorten},
1269 {"perleval", 1, 1, FEARG_1, NULL,
1270 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001271#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001272 f_perleval
1273#else
1274 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001275#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001276 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001277 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1278 ret_number, PROP_FUNC(f_popup_atcursor)},
1279 {"popup_beval", 2, 2, FEARG_1, NULL,
1280 ret_number, PROP_FUNC(f_popup_beval)},
1281 {"popup_clear", 0, 1, 0, NULL,
1282 ret_void, PROP_FUNC(f_popup_clear)},
1283 {"popup_close", 1, 2, FEARG_1, NULL,
1284 ret_void, PROP_FUNC(f_popup_close)},
1285 {"popup_create", 2, 2, FEARG_1, NULL,
1286 ret_number, PROP_FUNC(f_popup_create)},
1287 {"popup_dialog", 2, 2, FEARG_1, NULL,
1288 ret_number, PROP_FUNC(f_popup_dialog)},
1289 {"popup_filter_menu", 2, 2, 0, NULL,
1290 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1291 {"popup_filter_yesno", 2, 2, 0, NULL,
1292 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1293 {"popup_findinfo", 0, 0, 0, NULL,
1294 ret_number, PROP_FUNC(f_popup_findinfo)},
1295 {"popup_findpreview", 0, 0, 0, NULL,
1296 ret_number, PROP_FUNC(f_popup_findpreview)},
1297 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1298 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1299 {"popup_getpos", 1, 1, FEARG_1, NULL,
1300 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1301 {"popup_hide", 1, 1, FEARG_1, NULL,
1302 ret_void, PROP_FUNC(f_popup_hide)},
1303 {"popup_list", 0, 0, 0, NULL,
1304 ret_list_number, PROP_FUNC(f_popup_list)},
1305 {"popup_locate", 2, 2, 0, NULL,
1306 ret_number, PROP_FUNC(f_popup_locate)},
1307 {"popup_menu", 2, 2, FEARG_1, NULL,
1308 ret_number, PROP_FUNC(f_popup_menu)},
1309 {"popup_move", 2, 2, FEARG_1, NULL,
1310 ret_void, PROP_FUNC(f_popup_move)},
1311 {"popup_notification", 2, 2, FEARG_1, NULL,
1312 ret_number, PROP_FUNC(f_popup_notification)},
1313 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1314 ret_void, PROP_FUNC(f_popup_setoptions)},
1315 {"popup_settext", 2, 2, FEARG_1, NULL,
1316 ret_void, PROP_FUNC(f_popup_settext)},
1317 {"popup_show", 1, 1, FEARG_1, NULL,
1318 ret_void, PROP_FUNC(f_popup_show)},
1319 {"pow", 2, 2, FEARG_1, NULL,
1320 ret_float, FLOAT_FUNC(f_pow)},
1321 {"prevnonblank", 1, 1, FEARG_1, NULL,
1322 ret_number, f_prevnonblank},
1323 {"printf", 1, 19, FEARG_2, NULL,
1324 ret_string, f_printf},
1325 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1326 ret_string, JOB_FUNC(f_prompt_getprompt)},
1327 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1328 ret_void, JOB_FUNC(f_prompt_setcallback)},
1329 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1330 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1331 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1332 ret_void, JOB_FUNC(f_prompt_setprompt)},
1333 {"prop_add", 3, 3, FEARG_1, NULL,
1334 ret_void, PROP_FUNC(f_prop_add)},
1335 {"prop_clear", 1, 3, FEARG_1, NULL,
1336 ret_void, PROP_FUNC(f_prop_clear)},
1337 {"prop_find", 1, 2, FEARG_1, NULL,
1338 ret_dict_any, PROP_FUNC(f_prop_find)},
1339 {"prop_list", 1, 2, FEARG_1, NULL,
1340 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1341 {"prop_remove", 1, 3, FEARG_1, NULL,
1342 ret_number, PROP_FUNC(f_prop_remove)},
1343 {"prop_type_add", 2, 2, FEARG_1, NULL,
1344 ret_void, PROP_FUNC(f_prop_type_add)},
1345 {"prop_type_change", 2, 2, FEARG_1, NULL,
1346 ret_void, PROP_FUNC(f_prop_type_change)},
1347 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1348 ret_void, PROP_FUNC(f_prop_type_delete)},
1349 {"prop_type_get", 1, 2, FEARG_1, NULL,
1350 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1351 {"prop_type_list", 0, 1, FEARG_1, NULL,
1352 ret_list_string, PROP_FUNC(f_prop_type_list)},
1353 {"pum_getpos", 0, 0, 0, NULL,
1354 ret_dict_number, f_pum_getpos},
1355 {"pumvisible", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001356 ret_number_bool, f_pumvisible},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001357 {"py3eval", 1, 1, FEARG_1, NULL,
1358 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001359#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001360 f_py3eval
1361#else
1362 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001363#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001364 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001365 {"pyeval", 1, 1, FEARG_1, NULL,
1366 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001367#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001368 f_pyeval
1369#else
1370 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001371#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001372 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001373 {"pyxeval", 1, 1, FEARG_1, NULL,
1374 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001375#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001376 f_pyxeval
1377#else
1378 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001379#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001380 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001381 {"rand", 0, 1, FEARG_1, NULL,
1382 ret_number, f_rand},
1383 {"range", 1, 3, FEARG_1, NULL,
1384 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001385 {"readblob", 1, 1, FEARG_1, NULL,
1386 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001387 {"readdir", 1, 3, FEARG_1, NULL,
1388 ret_list_string, f_readdir},
1389 {"readdirex", 1, 3, FEARG_1, NULL,
1390 ret_list_dict_any, f_readdirex},
1391 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001392 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001393 {"reduce", 2, 3, FEARG_1, NULL,
1394 ret_any, f_reduce},
1395 {"reg_executing", 0, 0, 0, NULL,
1396 ret_string, f_reg_executing},
1397 {"reg_recording", 0, 0, 0, NULL,
1398 ret_string, f_reg_recording},
1399 {"reltime", 0, 2, FEARG_1, NULL,
1400 ret_list_any, f_reltime},
1401 {"reltimefloat", 1, 1, FEARG_1, NULL,
1402 ret_float, FLOAT_FUNC(f_reltimefloat)},
1403 {"reltimestr", 1, 1, FEARG_1, NULL,
1404 ret_string, f_reltimestr},
1405 {"remote_expr", 2, 4, FEARG_1, NULL,
1406 ret_string, f_remote_expr},
1407 {"remote_foreground", 1, 1, FEARG_1, NULL,
1408 ret_string, f_remote_foreground},
1409 {"remote_peek", 1, 2, FEARG_1, NULL,
1410 ret_number, f_remote_peek},
1411 {"remote_read", 1, 2, FEARG_1, NULL,
1412 ret_string, f_remote_read},
1413 {"remote_send", 2, 3, FEARG_1, NULL,
1414 ret_string, f_remote_send},
1415 {"remote_startserver", 1, 1, FEARG_1, NULL,
1416 ret_void, f_remote_startserver},
1417 {"remove", 2, 3, FEARG_1, NULL,
1418 ret_remove, f_remove},
1419 {"rename", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001420 ret_number_bool, f_rename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001421 {"repeat", 2, 2, FEARG_1, NULL,
1422 ret_first_arg, f_repeat},
1423 {"resolve", 1, 1, FEARG_1, NULL,
1424 ret_string, f_resolve},
1425 {"reverse", 1, 1, FEARG_1, NULL,
1426 ret_first_arg, f_reverse},
1427 {"round", 1, 1, FEARG_1, NULL,
1428 ret_float, FLOAT_FUNC(f_round)},
1429 {"rubyeval", 1, 1, FEARG_1, NULL,
1430 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001431#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001432 f_rubyeval
1433#else
1434 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001435#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001436 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001437 {"screenattr", 2, 2, FEARG_1, NULL,
1438 ret_number, f_screenattr},
1439 {"screenchar", 2, 2, FEARG_1, NULL,
1440 ret_number, f_screenchar},
1441 {"screenchars", 2, 2, FEARG_1, NULL,
1442 ret_list_number, f_screenchars},
1443 {"screencol", 0, 0, 0, NULL,
1444 ret_number, f_screencol},
1445 {"screenpos", 3, 3, FEARG_1, NULL,
1446 ret_dict_number, f_screenpos},
1447 {"screenrow", 0, 0, 0, NULL,
1448 ret_number, f_screenrow},
1449 {"screenstring", 2, 2, FEARG_1, NULL,
1450 ret_string, f_screenstring},
1451 {"search", 1, 5, FEARG_1, NULL,
1452 ret_number, f_search},
1453 {"searchcount", 0, 1, FEARG_1, NULL,
1454 ret_dict_any, f_searchcount},
1455 {"searchdecl", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001456 ret_number_bool, f_searchdecl},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001457 {"searchpair", 3, 7, 0, NULL,
1458 ret_number, f_searchpair},
1459 {"searchpairpos", 3, 7, 0, NULL,
1460 ret_list_number, f_searchpairpos},
1461 {"searchpos", 1, 5, FEARG_1, NULL,
1462 ret_list_number, f_searchpos},
1463 {"server2client", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001464 ret_number_bool, f_server2client},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001465 {"serverlist", 0, 0, 0, NULL,
1466 ret_string, f_serverlist},
1467 {"setbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001468 ret_number_bool, f_setbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001469 {"setbufvar", 3, 3, FEARG_3, NULL,
1470 ret_void, f_setbufvar},
1471 {"setcellwidths", 1, 1, FEARG_1, NULL,
1472 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001473 {"setcharpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001474 ret_number_bool, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001475 {"setcharsearch", 1, 1, FEARG_1, NULL,
1476 ret_void, f_setcharsearch},
1477 {"setcmdpos", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001478 ret_number_bool, f_setcmdpos},
1479 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1480 ret_number_bool, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001481 {"setenv", 2, 2, FEARG_2, NULL,
1482 ret_void, f_setenv},
1483 {"setfperm", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001484 ret_number_bool, f_setfperm},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001485 {"setline", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001486 ret_number_bool, f_setline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001487 {"setloclist", 2, 4, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001488 ret_number_bool, f_setloclist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001489 {"setmatches", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001490 ret_number_bool, f_setmatches},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001491 {"setpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001492 ret_number_bool, f_setpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001493 {"setqflist", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001494 ret_number_bool, f_setqflist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001495 {"setreg", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001496 ret_number_bool, f_setreg},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001497 {"settabvar", 3, 3, FEARG_3, NULL,
1498 ret_void, f_settabvar},
1499 {"settabwinvar", 4, 4, FEARG_4, NULL,
1500 ret_void, f_settabwinvar},
1501 {"settagstack", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001502 ret_number_bool, f_settagstack},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001503 {"setwinvar", 3, 3, FEARG_3, NULL,
1504 ret_void, f_setwinvar},
1505 {"sha256", 1, 1, FEARG_1, NULL,
1506 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001507#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001508 f_sha256
1509#else
1510 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001511#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001512 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001513 {"shellescape", 1, 2, FEARG_1, NULL,
1514 ret_string, f_shellescape},
1515 {"shiftwidth", 0, 1, FEARG_1, NULL,
1516 ret_number, f_shiftwidth},
1517 {"sign_define", 1, 2, FEARG_1, NULL,
1518 ret_any, SIGN_FUNC(f_sign_define)},
1519 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1520 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1521 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1522 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1523 {"sign_jump", 3, 3, FEARG_1, NULL,
1524 ret_number, SIGN_FUNC(f_sign_jump)},
1525 {"sign_place", 4, 5, FEARG_1, NULL,
1526 ret_number, SIGN_FUNC(f_sign_place)},
1527 {"sign_placelist", 1, 1, FEARG_1, NULL,
1528 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1529 {"sign_undefine", 0, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001530 ret_number_bool, SIGN_FUNC(f_sign_undefine)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001531 {"sign_unplace", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001532 ret_number_bool, SIGN_FUNC(f_sign_unplace)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001533 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1534 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1535 {"simplify", 1, 1, FEARG_1, NULL,
1536 ret_string, f_simplify},
1537 {"sin", 1, 1, FEARG_1, NULL,
1538 ret_float, FLOAT_FUNC(f_sin)},
1539 {"sinh", 1, 1, FEARG_1, NULL,
1540 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001541 {"slice", 2, 3, FEARG_1, NULL,
1542 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001543 {"sort", 1, 3, FEARG_1, NULL,
1544 ret_first_arg, f_sort},
1545 {"sound_clear", 0, 0, 0, NULL,
1546 ret_void, SOUND_FUNC(f_sound_clear)},
1547 {"sound_playevent", 1, 2, FEARG_1, NULL,
1548 ret_number, SOUND_FUNC(f_sound_playevent)},
1549 {"sound_playfile", 1, 2, FEARG_1, NULL,
1550 ret_number, SOUND_FUNC(f_sound_playfile)},
1551 {"sound_stop", 1, 1, FEARG_1, NULL,
1552 ret_void, SOUND_FUNC(f_sound_stop)},
1553 {"soundfold", 1, 1, FEARG_1, NULL,
1554 ret_string, f_soundfold},
1555 {"spellbadword", 0, 1, FEARG_1, NULL,
1556 ret_list_string, f_spellbadword},
1557 {"spellsuggest", 1, 3, FEARG_1, NULL,
1558 ret_list_string, f_spellsuggest},
1559 {"split", 1, 3, FEARG_1, NULL,
1560 ret_list_string, f_split},
1561 {"sqrt", 1, 1, FEARG_1, NULL,
1562 ret_float, FLOAT_FUNC(f_sqrt)},
1563 {"srand", 0, 1, FEARG_1, NULL,
1564 ret_list_number, f_srand},
1565 {"state", 0, 1, FEARG_1, NULL,
1566 ret_string, f_state},
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001567 {"str2float", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001568 ret_float, FLOAT_FUNC(f_str2float)},
1569 {"str2list", 1, 2, FEARG_1, NULL,
1570 ret_list_number, f_str2list},
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001571 {"str2nr", 1, 3, FEARG_1, arg3_string_nr_bool,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001572 ret_number, f_str2nr},
1573 {"strcharpart", 2, 3, FEARG_1, NULL,
1574 ret_string, f_strcharpart},
1575 {"strchars", 1, 2, FEARG_1, NULL,
1576 ret_number, f_strchars},
1577 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1578 ret_number, f_strdisplaywidth},
1579 {"strftime", 1, 2, FEARG_1, NULL,
1580 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001581#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001582 f_strftime
1583#else
1584 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001585#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001586 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001587 {"strgetchar", 2, 2, FEARG_1, NULL,
1588 ret_number, f_strgetchar},
1589 {"stridx", 2, 3, FEARG_1, NULL,
1590 ret_number, f_stridx},
1591 {"string", 1, 1, FEARG_1, NULL,
1592 ret_string, f_string},
1593 {"strlen", 1, 1, FEARG_1, NULL,
1594 ret_number, f_strlen},
1595 {"strpart", 2, 4, FEARG_1, NULL,
1596 ret_string, f_strpart},
1597 {"strptime", 2, 2, FEARG_1, NULL,
1598 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001599#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001600 f_strptime
1601#else
1602 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001603#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001604 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001605 {"strridx", 2, 3, FEARG_1, NULL,
1606 ret_number, f_strridx},
1607 {"strtrans", 1, 1, FEARG_1, NULL,
1608 ret_string, f_strtrans},
1609 {"strwidth", 1, 1, FEARG_1, NULL,
1610 ret_number, f_strwidth},
1611 {"submatch", 1, 2, FEARG_1, NULL,
1612 ret_string, f_submatch},
1613 {"substitute", 4, 4, FEARG_1, NULL,
1614 ret_string, f_substitute},
1615 {"swapinfo", 1, 1, FEARG_1, NULL,
1616 ret_dict_any, f_swapinfo},
1617 {"swapname", 1, 1, FEARG_1, NULL,
1618 ret_string, f_swapname},
1619 {"synID", 3, 3, 0, NULL,
1620 ret_number, f_synID},
1621 {"synIDattr", 2, 3, FEARG_1, NULL,
1622 ret_string, f_synIDattr},
1623 {"synIDtrans", 1, 1, FEARG_1, NULL,
1624 ret_number, f_synIDtrans},
1625 {"synconcealed", 2, 2, 0, NULL,
1626 ret_list_any, f_synconcealed},
1627 {"synstack", 2, 2, 0, NULL,
1628 ret_list_number, f_synstack},
1629 {"system", 1, 2, FEARG_1, NULL,
1630 ret_string, f_system},
1631 {"systemlist", 1, 2, FEARG_1, NULL,
1632 ret_list_string, f_systemlist},
1633 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1634 ret_list_number, f_tabpagebuflist},
1635 {"tabpagenr", 0, 1, 0, NULL,
1636 ret_number, f_tabpagenr},
1637 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1638 ret_number, f_tabpagewinnr},
1639 {"tagfiles", 0, 0, 0, NULL,
1640 ret_list_string, f_tagfiles},
1641 {"taglist", 1, 2, FEARG_1, NULL,
1642 ret_list_dict_any, f_taglist},
1643 {"tan", 1, 1, FEARG_1, NULL,
1644 ret_float, FLOAT_FUNC(f_tan)},
1645 {"tanh", 1, 1, FEARG_1, NULL,
1646 ret_float, FLOAT_FUNC(f_tanh)},
1647 {"tempname", 0, 0, 0, NULL,
1648 ret_string, f_tempname},
1649 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1650 ret_number, TERM_FUNC(f_term_dumpdiff)},
1651 {"term_dumpload", 1, 2, FEARG_1, NULL,
1652 ret_number, TERM_FUNC(f_term_dumpload)},
1653 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1654 ret_void, TERM_FUNC(f_term_dumpwrite)},
1655 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1656 ret_number, TERM_FUNC(f_term_getaltscreen)},
1657 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1658 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001659#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001660 f_term_getansicolors
1661#else
1662 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001663#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001664 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001665 {"term_getattr", 2, 2, FEARG_1, NULL,
1666 ret_number, TERM_FUNC(f_term_getattr)},
1667 {"term_getcursor", 1, 1, FEARG_1, NULL,
1668 ret_list_any, TERM_FUNC(f_term_getcursor)},
1669 {"term_getjob", 1, 1, FEARG_1, NULL,
1670 ret_job, TERM_FUNC(f_term_getjob)},
1671 {"term_getline", 2, 2, FEARG_1, NULL,
1672 ret_string, TERM_FUNC(f_term_getline)},
1673 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1674 ret_number, TERM_FUNC(f_term_getscrolled)},
1675 {"term_getsize", 1, 1, FEARG_1, NULL,
1676 ret_list_number, TERM_FUNC(f_term_getsize)},
1677 {"term_getstatus", 1, 1, FEARG_1, NULL,
1678 ret_string, TERM_FUNC(f_term_getstatus)},
1679 {"term_gettitle", 1, 1, FEARG_1, NULL,
1680 ret_string, TERM_FUNC(f_term_gettitle)},
1681 {"term_gettty", 1, 2, FEARG_1, NULL,
1682 ret_string, TERM_FUNC(f_term_gettty)},
1683 {"term_list", 0, 0, 0, NULL,
1684 ret_list_number, TERM_FUNC(f_term_list)},
1685 {"term_scrape", 2, 2, FEARG_1, NULL,
1686 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1687 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1688 ret_void, TERM_FUNC(f_term_sendkeys)},
1689 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1690 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001691#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001692 f_term_setansicolors
1693#else
1694 NULL
1695#endif
1696 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001697 {"term_setapi", 2, 2, FEARG_1, NULL,
1698 ret_void, TERM_FUNC(f_term_setapi)},
1699 {"term_setkill", 2, 2, FEARG_1, NULL,
1700 ret_void, TERM_FUNC(f_term_setkill)},
1701 {"term_setrestore", 2, 2, FEARG_1, NULL,
1702 ret_void, TERM_FUNC(f_term_setrestore)},
1703 {"term_setsize", 3, 3, FEARG_1, NULL,
1704 ret_void, TERM_FUNC(f_term_setsize)},
1705 {"term_start", 1, 2, FEARG_1, NULL,
1706 ret_number, TERM_FUNC(f_term_start)},
1707 {"term_wait", 1, 2, FEARG_1, NULL,
1708 ret_void, TERM_FUNC(f_term_wait)},
1709 {"terminalprops", 0, 0, 0, NULL,
1710 ret_dict_string, f_terminalprops},
1711 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1712 ret_void, f_test_alloc_fail},
1713 {"test_autochdir", 0, 0, 0, NULL,
1714 ret_void, f_test_autochdir},
1715 {"test_feedinput", 1, 1, FEARG_1, NULL,
1716 ret_void, f_test_feedinput},
1717 {"test_garbagecollect_now", 0, 0, 0, NULL,
1718 ret_void, f_test_garbagecollect_now},
1719 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1720 ret_void, f_test_garbagecollect_soon},
1721 {"test_getvalue", 1, 1, FEARG_1, NULL,
1722 ret_number, f_test_getvalue},
1723 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1724 ret_void, f_test_ignore_error},
1725 {"test_null_blob", 0, 0, 0, NULL,
1726 ret_blob, f_test_null_blob},
1727 {"test_null_channel", 0, 0, 0, NULL,
1728 ret_channel, JOB_FUNC(f_test_null_channel)},
1729 {"test_null_dict", 0, 0, 0, NULL,
1730 ret_dict_any, f_test_null_dict},
1731 {"test_null_function", 0, 0, 0, NULL,
1732 ret_func_any, f_test_null_function},
1733 {"test_null_job", 0, 0, 0, NULL,
1734 ret_job, JOB_FUNC(f_test_null_job)},
1735 {"test_null_list", 0, 0, 0, NULL,
1736 ret_list_any, f_test_null_list},
1737 {"test_null_partial", 0, 0, 0, NULL,
1738 ret_func_any, f_test_null_partial},
1739 {"test_null_string", 0, 0, 0, NULL,
1740 ret_string, f_test_null_string},
1741 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1742 ret_void, f_test_option_not_set},
1743 {"test_override", 2, 2, FEARG_2, NULL,
1744 ret_void, f_test_override},
1745 {"test_refcount", 1, 1, FEARG_1, NULL,
1746 ret_number, f_test_refcount},
1747 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1748 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001749#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001750 f_test_scrollbar
1751#else
1752 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001753#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001754 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001755 {"test_setmouse", 2, 2, 0, NULL,
1756 ret_void, f_test_setmouse},
1757 {"test_settime", 1, 1, FEARG_1, NULL,
1758 ret_void, f_test_settime},
1759 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1760 ret_void, f_test_srand_seed},
1761 {"test_unknown", 0, 0, 0, NULL,
1762 ret_any, f_test_unknown},
1763 {"test_void", 0, 0, 0, NULL,
1764 ret_void, f_test_void},
1765 {"timer_info", 0, 1, FEARG_1, NULL,
1766 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1767 {"timer_pause", 2, 2, FEARG_1, NULL,
1768 ret_void, TIMER_FUNC(f_timer_pause)},
1769 {"timer_start", 2, 3, FEARG_1, NULL,
1770 ret_number, TIMER_FUNC(f_timer_start)},
1771 {"timer_stop", 1, 1, FEARG_1, NULL,
1772 ret_void, TIMER_FUNC(f_timer_stop)},
1773 {"timer_stopall", 0, 0, 0, NULL,
1774 ret_void, TIMER_FUNC(f_timer_stopall)},
1775 {"tolower", 1, 1, FEARG_1, NULL,
1776 ret_string, f_tolower},
1777 {"toupper", 1, 1, FEARG_1, NULL,
1778 ret_string, f_toupper},
1779 {"tr", 3, 3, FEARG_1, NULL,
1780 ret_string, f_tr},
1781 {"trim", 1, 3, FEARG_1, NULL,
1782 ret_string, f_trim},
1783 {"trunc", 1, 1, FEARG_1, NULL,
1784 ret_float, FLOAT_FUNC(f_trunc)},
1785 {"type", 1, 1, FEARG_1, NULL,
1786 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001787 {"typename", 1, 1, FEARG_1, NULL,
1788 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001789 {"undofile", 1, 1, FEARG_1, NULL,
1790 ret_string, f_undofile},
1791 {"undotree", 0, 0, 0, NULL,
1792 ret_dict_any, f_undotree},
1793 {"uniq", 1, 3, FEARG_1, NULL,
1794 ret_list_any, f_uniq},
1795 {"values", 1, 1, FEARG_1, NULL,
1796 ret_list_any, f_values},
1797 {"virtcol", 1, 1, FEARG_1, NULL,
1798 ret_number, f_virtcol},
1799 {"visualmode", 0, 1, 0, NULL,
1800 ret_string, f_visualmode},
1801 {"wildmenumode", 0, 0, 0, NULL,
1802 ret_number, f_wildmenumode},
1803 {"win_execute", 2, 3, FEARG_2, NULL,
1804 ret_string, f_win_execute},
1805 {"win_findbuf", 1, 1, FEARG_1, NULL,
1806 ret_list_number, f_win_findbuf},
1807 {"win_getid", 0, 2, FEARG_1, NULL,
1808 ret_number, f_win_getid},
1809 {"win_gettype", 0, 1, FEARG_1, NULL,
1810 ret_string, f_win_gettype},
1811 {"win_gotoid", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001812 ret_number_bool, f_win_gotoid},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001813 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1814 ret_list_number, f_win_id2tabwin},
1815 {"win_id2win", 1, 1, FEARG_1, NULL,
1816 ret_number, f_win_id2win},
1817 {"win_screenpos", 1, 1, FEARG_1, NULL,
1818 ret_list_number, f_win_screenpos},
1819 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001820 ret_number_bool, f_win_splitmove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001821 {"winbufnr", 1, 1, FEARG_1, NULL,
1822 ret_number, f_winbufnr},
1823 {"wincol", 0, 0, 0, NULL,
1824 ret_number, f_wincol},
1825 {"windowsversion", 0, 0, 0, NULL,
1826 ret_string, f_windowsversion},
1827 {"winheight", 1, 1, FEARG_1, NULL,
1828 ret_number, f_winheight},
1829 {"winlayout", 0, 1, FEARG_1, NULL,
1830 ret_list_any, f_winlayout},
1831 {"winline", 0, 0, 0, NULL,
1832 ret_number, f_winline},
1833 {"winnr", 0, 1, FEARG_1, NULL,
1834 ret_number, f_winnr},
1835 {"winrestcmd", 0, 0, 0, NULL,
1836 ret_string, f_winrestcmd},
1837 {"winrestview", 1, 1, FEARG_1, NULL,
1838 ret_void, f_winrestview},
1839 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001840 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001841 {"winwidth", 1, 1, FEARG_1, NULL,
1842 ret_number, f_winwidth},
1843 {"wordcount", 0, 0, 0, NULL,
1844 ret_dict_number, f_wordcount},
1845 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001846 ret_number_bool, f_writefile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001847 {"xor", 2, 2, FEARG_1, NULL,
1848 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001849};
1850
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001851/*
1852 * Function given to ExpandGeneric() to obtain the list of internal
1853 * or user defined function names.
1854 */
1855 char_u *
1856get_function_name(expand_T *xp, int idx)
1857{
1858 static int intidx = -1;
1859 char_u *name;
1860
1861 if (idx == 0)
1862 intidx = -1;
1863 if (intidx < 0)
1864 {
1865 name = get_user_func_name(xp, idx);
1866 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001867 {
1868 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1869 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001870 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001871 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001872 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001873 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001874 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001875 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001876 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001877 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001878 STRCAT(IObuff, ")");
1879 return IObuff;
1880 }
1881
1882 return NULL;
1883}
1884
1885/*
1886 * Function given to ExpandGeneric() to obtain the list of internal or
1887 * user defined variable or function names.
1888 */
1889 char_u *
1890get_expr_name(expand_T *xp, int idx)
1891{
1892 static int intidx = -1;
1893 char_u *name;
1894
1895 if (idx == 0)
1896 intidx = -1;
1897 if (intidx < 0)
1898 {
1899 name = get_function_name(xp, idx);
1900 if (name != NULL)
1901 return name;
1902 }
1903 return get_user_var_name(xp, ++intidx);
1904}
1905
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001906/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001907 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001908 * Return index, or -1 if not found or "implemented" is TRUE and the function
1909 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001910 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001911 static int
1912find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001913{
1914 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001915 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001916 int cmp;
1917 int x;
1918
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001919 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001920
1921 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001922 while (first <= last)
1923 {
1924 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001925 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001926 if (cmp < 0)
1927 last = x - 1;
1928 else if (cmp > 0)
1929 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001930 else if (implemented && global_functions[x].f_func == NULL)
1931 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001932 else
1933 return x;
1934 }
1935 return -1;
1936}
1937
Bram Moolenaar15c47602020-03-26 22:16:48 +01001938/*
1939 * Find internal function "name" in table "global_functions".
1940 * Return index, or -1 if not found or the function is not implemented.
1941 */
1942 int
1943find_internal_func(char_u *name)
1944{
1945 return find_internal_func_opt(name, TRUE);
1946}
1947
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001948 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001949has_internal_func(char_u *name)
1950{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001951 return find_internal_func_opt(name, TRUE) >= 0;
1952}
1953
1954 static int
1955has_internal_func_name(char_u *name)
1956{
1957 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001958}
1959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 char *
1961internal_func_name(int idx)
1962{
1963 return global_functions[idx].f_name;
1964}
1965
Bram Moolenaar94738d82020-10-21 14:25:07 +02001966/*
1967 * Check the argument types for builting function "idx".
1968 * Uses the list of types on the type stack: "types".
1969 * Return FAIL and gives an error message when a type is wrong.
1970 */
1971 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001972internal_func_check_arg_types(
1973 type_T **types,
1974 int idx,
1975 int argcount,
1976 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001977{
1978 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1979 int i;
1980
1981 if (argchecks != NULL)
1982 {
1983 argcontext_T context;
1984
1985 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001986 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001987 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001988 for (i = 0; i < argcount; ++i)
1989 if (argchecks[i] != NULL)
1990 {
1991 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001992 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001993 return FAIL;
1994 }
1995 }
1996 return OK;
1997}
1998
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001999/*
2000 * Call the "f_retfunc" function to obtain the return type of function "idx".
2001 * "argtypes" is the list of argument types or NULL when there are no
2002 * arguments.
2003 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002005internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002007 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008}
2009
2010/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002011 * Return TRUE if "idx" is for the map() function.
2012 */
2013 int
2014internal_func_is_map(int idx)
2015{
2016 return global_functions[idx].f_func == f_map;
2017}
2018
2019/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002021 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2022 * first argument, 2 if method base is second argument, etc. 9 if method base
2023 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 */
2025 int
2026check_internal_func(int idx, int argcount)
2027{
2028 int res;
2029 char *name;
2030
2031 if (argcount < global_functions[idx].f_min_argc)
2032 res = FCERR_TOOFEW;
2033 else if (argcount > global_functions[idx].f_max_argc)
2034 res = FCERR_TOOMANY;
2035 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002036 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037
2038 name = internal_func_name(idx);
2039 if (res == FCERR_TOOMANY)
2040 semsg(_(e_toomanyarg), name);
2041 else
2042 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002043 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044}
2045
Bram Moolenaarac92e252019-08-03 21:58:38 +02002046 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002047call_internal_func(
2048 char_u *name,
2049 int argcount,
2050 typval_T *argvars,
2051 typval_T *rettv)
2052{
2053 int i;
2054
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002055 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002056 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002057 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002058 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002059 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002060 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002061 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002062 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002063 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002064 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002065}
2066
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 void
2068call_internal_func_by_idx(
2069 int idx,
2070 typval_T *argvars,
2071 typval_T *rettv)
2072{
2073 global_functions[idx].f_func(argvars, rettv);
2074}
2075
Bram Moolenaarac92e252019-08-03 21:58:38 +02002076/*
2077 * Invoke a method for base->method().
2078 */
2079 int
2080call_internal_method(
2081 char_u *name,
2082 int argcount,
2083 typval_T *argvars,
2084 typval_T *rettv,
2085 typval_T *basetv)
2086{
2087 int i;
2088 int fi;
2089 typval_T argv[MAX_FUNC_ARGS + 1];
2090
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002091 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002092 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002093 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002094 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002095 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002096 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002097 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002098 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002099 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002100
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002101 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002102 {
2103 // base value goes last
2104 for (i = 0; i < argcount; ++i)
2105 argv[i] = argvars[i];
2106 argv[argcount] = *basetv;
2107 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002108 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002109 {
2110 // base value goes second
2111 argv[0] = argvars[0];
2112 argv[1] = *basetv;
2113 for (i = 1; i < argcount; ++i)
2114 argv[i + 1] = argvars[i];
2115 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002116 else if (global_functions[fi].f_argtype == FEARG_3)
2117 {
2118 // base value goes third
2119 argv[0] = argvars[0];
2120 argv[1] = argvars[1];
2121 argv[2] = *basetv;
2122 for (i = 2; i < argcount; ++i)
2123 argv[i + 1] = argvars[i];
2124 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002125 else if (global_functions[fi].f_argtype == FEARG_4)
2126 {
2127 // base value goes fourth
2128 argv[0] = argvars[0];
2129 argv[1] = argvars[1];
2130 argv[2] = argvars[2];
2131 argv[3] = *basetv;
2132 for (i = 3; i < argcount; ++i)
2133 argv[i + 1] = argvars[i];
2134 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002135 else
2136 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002137 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002138 argv[0] = *basetv;
2139 for (i = 0; i < argcount; ++i)
2140 argv[i + 1] = argvars[i];
2141 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002142 argv[argcount + 1].v_type = VAR_UNKNOWN;
2143
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002144 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002145 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002146}
2147
2148/*
2149 * Return TRUE for a non-zero Number and a non-empty String.
2150 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002151 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002152non_zero_arg(typval_T *argvars)
2153{
2154 return ((argvars[0].v_type == VAR_NUMBER
2155 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002156 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157 && argvars[0].vval.v_number == VVAL_TRUE)
2158 || (argvars[0].v_type == VAR_STRING
2159 && argvars[0].vval.v_string != NULL
2160 && *argvars[0].vval.v_string != NUL));
2161}
2162
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002163#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002164/*
2165 * Get the float value of "argvars[0]" into "f".
2166 * Returns FAIL when the argument is not a Number or Float.
2167 */
2168 static int
2169get_float_arg(typval_T *argvars, float_T *f)
2170{
2171 if (argvars[0].v_type == VAR_FLOAT)
2172 {
2173 *f = argvars[0].vval.v_float;
2174 return OK;
2175 }
2176 if (argvars[0].v_type == VAR_NUMBER)
2177 {
2178 *f = (float_T)argvars[0].vval.v_number;
2179 return OK;
2180 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002181 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002182 return FAIL;
2183}
2184
2185/*
2186 * "abs(expr)" function
2187 */
2188 static void
2189f_abs(typval_T *argvars, typval_T *rettv)
2190{
2191 if (argvars[0].v_type == VAR_FLOAT)
2192 {
2193 rettv->v_type = VAR_FLOAT;
2194 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
2195 }
2196 else
2197 {
2198 varnumber_T n;
2199 int error = FALSE;
2200
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002201 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002202 if (error)
2203 rettv->vval.v_number = -1;
2204 else if (n > 0)
2205 rettv->vval.v_number = n;
2206 else
2207 rettv->vval.v_number = -n;
2208 }
2209}
2210
2211/*
2212 * "acos()" function
2213 */
2214 static void
2215f_acos(typval_T *argvars, typval_T *rettv)
2216{
2217 float_T f = 0.0;
2218
2219 rettv->v_type = VAR_FLOAT;
2220 if (get_float_arg(argvars, &f) == OK)
2221 rettv->vval.v_float = acos(f);
2222 else
2223 rettv->vval.v_float = 0.0;
2224}
2225#endif
2226
2227/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002228 * "and(expr, expr)" function
2229 */
2230 static void
2231f_and(typval_T *argvars, typval_T *rettv)
2232{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002233 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2234 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002235}
2236
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002237#ifdef FEAT_FLOAT
2238/*
2239 * "asin()" function
2240 */
2241 static void
2242f_asin(typval_T *argvars, typval_T *rettv)
2243{
2244 float_T f = 0.0;
2245
2246 rettv->v_type = VAR_FLOAT;
2247 if (get_float_arg(argvars, &f) == OK)
2248 rettv->vval.v_float = asin(f);
2249 else
2250 rettv->vval.v_float = 0.0;
2251}
2252
2253/*
2254 * "atan()" function
2255 */
2256 static void
2257f_atan(typval_T *argvars, typval_T *rettv)
2258{
2259 float_T f = 0.0;
2260
2261 rettv->v_type = VAR_FLOAT;
2262 if (get_float_arg(argvars, &f) == OK)
2263 rettv->vval.v_float = atan(f);
2264 else
2265 rettv->vval.v_float = 0.0;
2266}
2267
2268/*
2269 * "atan2()" function
2270 */
2271 static void
2272f_atan2(typval_T *argvars, typval_T *rettv)
2273{
2274 float_T fx = 0.0, fy = 0.0;
2275
2276 rettv->v_type = VAR_FLOAT;
2277 if (get_float_arg(argvars, &fx) == OK
2278 && get_float_arg(&argvars[1], &fy) == OK)
2279 rettv->vval.v_float = atan2(fx, fy);
2280 else
2281 rettv->vval.v_float = 0.0;
2282}
2283#endif
2284
2285/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002286 * "balloon_show()" function
2287 */
2288#ifdef FEAT_BEVAL
2289 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002290f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2291{
2292 rettv->v_type = VAR_STRING;
2293 if (balloonEval != NULL)
2294 {
2295 if (balloonEval->msg == NULL)
2296 rettv->vval.v_string = NULL;
2297 else
2298 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2299 }
2300}
2301
2302 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002303f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2304{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002305 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002306 {
2307 if (argvars[0].v_type == VAR_LIST
2308# ifdef FEAT_GUI
2309 && !gui.in_use
2310# endif
2311 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002312 {
2313 list_T *l = argvars[0].vval.v_list;
2314
2315 // empty list removes the balloon
2316 post_balloon(balloonEval, NULL,
2317 l == NULL || l->lv_len == 0 ? NULL : l);
2318 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002319 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002320 {
2321 char_u *mesg = tv_get_string_chk(&argvars[0]);
2322
2323 if (mesg != NULL)
2324 // empty string removes the balloon
2325 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2326 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002327 }
2328}
2329
Bram Moolenaar669a8282017-11-19 20:13:05 +01002330# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002331 static void
2332f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2333{
2334 if (rettv_list_alloc(rettv) == OK)
2335 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002336 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002337
2338 if (msg != NULL)
2339 {
2340 pumitem_T *array;
2341 int size = split_message(msg, &array);
2342 int i;
2343
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002344 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002345 for (i = 1; i < size - 1; ++i)
2346 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002347 while (size > 0)
2348 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002349 vim_free(array);
2350 }
2351 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002352}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002353# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002354#endif
2355
2356/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002357 * Get the buffer from "arg" and give an error and return NULL if it is not
2358 * valid.
2359 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002360 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002361get_buf_arg(typval_T *arg)
2362{
2363 buf_T *buf;
2364
2365 ++emsg_off;
2366 buf = tv_get_buf(arg, FALSE);
2367 --emsg_off;
2368 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002369 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002370 return buf;
2371}
2372
2373/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002374 * "byte2line(byte)" function
2375 */
2376 static void
2377f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2378{
2379#ifndef FEAT_BYTEOFF
2380 rettv->vval.v_number = -1;
2381#else
2382 long boff = 0;
2383
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002384 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002385 if (boff < 0)
2386 rettv->vval.v_number = -1;
2387 else
2388 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2389 (linenr_T)0, &boff);
2390#endif
2391}
2392
2393 static void
2394byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2395{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002396 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002397 char_u *str;
2398 varnumber_T idx;
2399
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002400 str = tv_get_string_chk(&argvars[0]);
2401 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002402 rettv->vval.v_number = -1;
2403 if (str == NULL || idx < 0)
2404 return;
2405
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002406 t = str;
2407 for ( ; idx > 0; idx--)
2408 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002409 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002410 return;
2411 if (enc_utf8 && comp)
2412 t += utf_ptr2len(t);
2413 else
2414 t += (*mb_ptr2len)(t);
2415 }
2416 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002417}
2418
2419/*
2420 * "byteidx()" function
2421 */
2422 static void
2423f_byteidx(typval_T *argvars, typval_T *rettv)
2424{
2425 byteidx(argvars, rettv, FALSE);
2426}
2427
2428/*
2429 * "byteidxcomp()" function
2430 */
2431 static void
2432f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2433{
2434 byteidx(argvars, rettv, TRUE);
2435}
2436
2437/*
2438 * "call(func, arglist [, dict])" function
2439 */
2440 static void
2441f_call(typval_T *argvars, typval_T *rettv)
2442{
2443 char_u *func;
2444 partial_T *partial = NULL;
2445 dict_T *selfdict = NULL;
2446
2447 if (argvars[1].v_type != VAR_LIST)
2448 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002449 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002450 return;
2451 }
2452 if (argvars[1].vval.v_list == NULL)
2453 return;
2454
2455 if (argvars[0].v_type == VAR_FUNC)
2456 func = argvars[0].vval.v_string;
2457 else if (argvars[0].v_type == VAR_PARTIAL)
2458 {
2459 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002460 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002461 }
2462 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002463 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002464 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002465 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002466
2467 if (argvars[2].v_type != VAR_UNKNOWN)
2468 {
2469 if (argvars[2].v_type != VAR_DICT)
2470 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002471 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002472 return;
2473 }
2474 selfdict = argvars[2].vval.v_dict;
2475 }
2476
2477 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2478}
2479
2480#ifdef FEAT_FLOAT
2481/*
2482 * "ceil({float})" function
2483 */
2484 static void
2485f_ceil(typval_T *argvars, typval_T *rettv)
2486{
2487 float_T f = 0.0;
2488
2489 rettv->v_type = VAR_FLOAT;
2490 if (get_float_arg(argvars, &f) == OK)
2491 rettv->vval.v_float = ceil(f);
2492 else
2493 rettv->vval.v_float = 0.0;
2494}
2495#endif
2496
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002497/*
2498 * "changenr()" function
2499 */
2500 static void
2501f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2502{
2503 rettv->vval.v_number = curbuf->b_u_seq_cur;
2504}
2505
2506/*
2507 * "char2nr(string)" function
2508 */
2509 static void
2510f_char2nr(typval_T *argvars, typval_T *rettv)
2511{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002512 if (has_mbyte)
2513 {
2514 int utf8 = 0;
2515
2516 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002517 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002518
2519 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002520 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002521 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002522 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002523 }
2524 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002525 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002526}
2527
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002528/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002529 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2530 * returns the character index of the column. Otherwise, returns the byte index
2531 * of the column.
2532 */
2533 static void
2534get_col(typval_T *argvars, typval_T *rettv, int charcol)
2535{
2536 colnr_T col = 0;
2537 pos_T *fp;
2538 int fnum = curbuf->b_fnum;
2539
2540 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2541 if (fp != NULL && fnum == curbuf->b_fnum)
2542 {
2543 if (fp->col == MAXCOL)
2544 {
2545 // '> can be MAXCOL, get the length of the line then
2546 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2547 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2548 else
2549 col = MAXCOL;
2550 }
2551 else
2552 {
2553 col = fp->col + 1;
2554 // col(".") when the cursor is on the NUL at the end of the line
2555 // because of "coladd" can be seen as an extra column.
2556 if (virtual_active() && fp == &curwin->w_cursor)
2557 {
2558 char_u *p = ml_get_cursor();
2559
2560 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2561 curwin->w_virtcol - curwin->w_cursor.coladd))
2562 {
2563 int l;
2564
2565 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2566 col += l;
2567 }
2568 }
2569 }
2570 }
2571 rettv->vval.v_number = col;
2572}
2573
2574/*
2575 * "charcol()" function
2576 */
2577 static void
2578f_charcol(typval_T *argvars, typval_T *rettv)
2579{
2580 get_col(argvars, rettv, TRUE);
2581}
2582
2583/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002584 * "charidx()" function
2585 */
2586 static void
2587f_charidx(typval_T *argvars, typval_T *rettv)
2588{
2589 char_u *str;
2590 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002591 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002592 char_u *p;
2593 int len;
2594 int (*ptr2len)(char_u *);
2595
2596 rettv->vval.v_number = -1;
2597
2598 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2599 || (argvars[2].v_type != VAR_UNKNOWN
2600 && argvars[2].v_type != VAR_NUMBER))
2601 {
2602 emsg(_(e_invarg));
2603 return;
2604 }
2605
2606 str = tv_get_string_chk(&argvars[0]);
2607 idx = tv_get_number_chk(&argvars[1], NULL);
2608 if (str == NULL || idx < 0)
2609 return;
2610
2611 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002612 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002613 if (countcc < 0 || countcc > 1)
2614 {
2615 semsg(_(e_using_number_as_bool_nr), countcc);
2616 return;
2617 }
2618
2619 if (enc_utf8 && countcc)
2620 ptr2len = utf_ptr2len;
2621 else
2622 ptr2len = mb_ptr2len;
2623
2624 for (p = str, len = 0; p <= str + idx; len++)
2625 {
2626 if (*p == NUL)
2627 return;
2628 p += ptr2len(p);
2629 }
2630
2631 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2632}
2633
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002634 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002635get_optional_window(typval_T *argvars, int idx)
2636{
2637 win_T *win = curwin;
2638
2639 if (argvars[idx].v_type != VAR_UNKNOWN)
2640 {
2641 win = find_win_by_nr_or_id(&argvars[idx]);
2642 if (win == NULL)
2643 {
2644 emsg(_(e_invalwindow));
2645 return NULL;
2646 }
2647 }
2648 return win;
2649}
2650
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002651/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002652 * "col(string)" function
2653 */
2654 static void
2655f_col(typval_T *argvars, typval_T *rettv)
2656{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002657 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002658}
2659
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002660/*
2661 * "confirm(message, buttons[, default [, type]])" function
2662 */
2663 static void
2664f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2665{
2666#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2667 char_u *message;
2668 char_u *buttons = NULL;
2669 char_u buf[NUMBUFLEN];
2670 char_u buf2[NUMBUFLEN];
2671 int def = 1;
2672 int type = VIM_GENERIC;
2673 char_u *typestr;
2674 int error = FALSE;
2675
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002676 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002677 if (message == NULL)
2678 error = TRUE;
2679 if (argvars[1].v_type != VAR_UNKNOWN)
2680 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002681 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002682 if (buttons == NULL)
2683 error = TRUE;
2684 if (argvars[2].v_type != VAR_UNKNOWN)
2685 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002686 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002687 if (argvars[3].v_type != VAR_UNKNOWN)
2688 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002689 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002690 if (typestr == NULL)
2691 error = TRUE;
2692 else
2693 {
2694 switch (TOUPPER_ASC(*typestr))
2695 {
2696 case 'E': type = VIM_ERROR; break;
2697 case 'Q': type = VIM_QUESTION; break;
2698 case 'I': type = VIM_INFO; break;
2699 case 'W': type = VIM_WARNING; break;
2700 case 'G': type = VIM_GENERIC; break;
2701 }
2702 }
2703 }
2704 }
2705 }
2706
2707 if (buttons == NULL || *buttons == NUL)
2708 buttons = (char_u *)_("&Ok");
2709
2710 if (!error)
2711 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2712 def, NULL, FALSE);
2713#endif
2714}
2715
2716/*
2717 * "copy()" function
2718 */
2719 static void
2720f_copy(typval_T *argvars, typval_T *rettv)
2721{
2722 item_copy(&argvars[0], rettv, FALSE, 0);
2723}
2724
2725#ifdef FEAT_FLOAT
2726/*
2727 * "cos()" function
2728 */
2729 static void
2730f_cos(typval_T *argvars, typval_T *rettv)
2731{
2732 float_T f = 0.0;
2733
2734 rettv->v_type = VAR_FLOAT;
2735 if (get_float_arg(argvars, &f) == OK)
2736 rettv->vval.v_float = cos(f);
2737 else
2738 rettv->vval.v_float = 0.0;
2739}
2740
2741/*
2742 * "cosh()" function
2743 */
2744 static void
2745f_cosh(typval_T *argvars, typval_T *rettv)
2746{
2747 float_T f = 0.0;
2748
2749 rettv->v_type = VAR_FLOAT;
2750 if (get_float_arg(argvars, &f) == OK)
2751 rettv->vval.v_float = cosh(f);
2752 else
2753 rettv->vval.v_float = 0.0;
2754}
2755#endif
2756
2757/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002758 * Set the cursor position.
2759 * If 'charcol' is TRUE, then use the column number as a character offet.
2760 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002761 */
2762 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002763set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002764{
2765 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002766 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002767 int set_curswant = TRUE;
2768
2769 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002770 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002771 {
2772 pos_T pos;
2773 colnr_T curswant = -1;
2774
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002775 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002776 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002777 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002778 return;
2779 }
2780 line = pos.lnum;
2781 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002783 if (curswant >= 0)
2784 {
2785 curwin->w_curswant = curswant - 1;
2786 set_curswant = FALSE;
2787 }
2788 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002789 else if ((argvars[0].v_type == VAR_NUMBER ||
2790 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002791 && (argvars[1].v_type == VAR_NUMBER ||
2792 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002793 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002794 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002795 if (line < 0)
2796 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002797 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002798 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002799 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002800 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002801 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002802 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002803 else
2804 {
2805 emsg(_(e_invarg));
2806 return;
2807 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002808 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002809 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002810 if (line > 0)
2811 curwin->w_cursor.lnum = line;
2812 if (col > 0)
2813 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002814 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002815
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002816 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002817 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002818 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002819 if (has_mbyte)
2820 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002821
2822 curwin->w_set_curswant = set_curswant;
2823 rettv->vval.v_number = 0;
2824}
2825
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002826/*
2827 * "cursor(lnum, col)" function, or
2828 * "cursor(list)"
2829 *
2830 * Moves the cursor to the specified line and column.
2831 * Returns 0 when the position could be set, -1 otherwise.
2832 */
2833 static void
2834f_cursor(typval_T *argvars, typval_T *rettv)
2835{
2836 set_cursorpos(argvars, rettv, FALSE);
2837}
2838
Bram Moolenaar4f974752019-02-17 17:44:42 +01002839#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002840/*
2841 * "debugbreak()" function
2842 */
2843 static void
2844f_debugbreak(typval_T *argvars, typval_T *rettv)
2845{
2846 int pid;
2847
2848 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002849 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002850 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002851 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002852 else
2853 {
2854 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2855
2856 if (hProcess != NULL)
2857 {
2858 DebugBreakProcess(hProcess);
2859 CloseHandle(hProcess);
2860 rettv->vval.v_number = OK;
2861 }
2862 }
2863}
2864#endif
2865
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002866/*
2867 * "deepcopy()" function
2868 */
2869 static void
2870f_deepcopy(typval_T *argvars, typval_T *rettv)
2871{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002872 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002873 int copyID;
2874
2875 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002876 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002877 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002878 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002879 else
2880 {
2881 copyID = get_copyID();
2882 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2883 }
2884}
2885
2886/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002887 * "did_filetype()" function
2888 */
2889 static void
2890f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2891{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002892 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002893}
2894
2895/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002896 * "echoraw({expr})" function
2897 */
2898 static void
2899f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2900{
2901 char_u *str = tv_get_string_chk(&argvars[0]);
2902
2903 if (str != NULL && *str != NUL)
2904 {
2905 out_str(str);
2906 out_flush();
2907 }
2908}
2909
2910/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002911 * "empty({expr})" function
2912 */
2913 static void
2914f_empty(typval_T *argvars, typval_T *rettv)
2915{
2916 int n = FALSE;
2917
2918 switch (argvars[0].v_type)
2919 {
2920 case VAR_STRING:
2921 case VAR_FUNC:
2922 n = argvars[0].vval.v_string == NULL
2923 || *argvars[0].vval.v_string == NUL;
2924 break;
2925 case VAR_PARTIAL:
2926 n = FALSE;
2927 break;
2928 case VAR_NUMBER:
2929 n = argvars[0].vval.v_number == 0;
2930 break;
2931 case VAR_FLOAT:
2932#ifdef FEAT_FLOAT
2933 n = argvars[0].vval.v_float == 0.0;
2934 break;
2935#endif
2936 case VAR_LIST:
2937 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002938 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002939 break;
2940 case VAR_DICT:
2941 n = argvars[0].vval.v_dict == NULL
2942 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2943 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002944 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002945 case VAR_SPECIAL:
2946 n = argvars[0].vval.v_number != VVAL_TRUE;
2947 break;
2948
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002949 case VAR_BLOB:
2950 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002951 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2952 break;
2953
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002954 case VAR_JOB:
2955#ifdef FEAT_JOB_CHANNEL
2956 n = argvars[0].vval.v_job == NULL
2957 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2958 break;
2959#endif
2960 case VAR_CHANNEL:
2961#ifdef FEAT_JOB_CHANNEL
2962 n = argvars[0].vval.v_channel == NULL
2963 || !channel_is_open(argvars[0].vval.v_channel);
2964 break;
2965#endif
2966 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002967 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002969 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002970 n = TRUE;
2971 break;
2972 }
2973
2974 rettv->vval.v_number = n;
2975}
2976
2977/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002978 * "environ()" function
2979 */
2980 static void
2981f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2982{
2983#if !defined(AMIGA)
2984 int i = 0;
2985 char_u *entry, *value;
2986# ifdef MSWIN
2987 extern wchar_t **_wenviron;
2988# else
2989 extern char **environ;
2990# endif
2991
2992 if (rettv_dict_alloc(rettv) != OK)
2993 return;
2994
2995# ifdef MSWIN
2996 if (*_wenviron == NULL)
2997 return;
2998# else
2999 if (*environ == NULL)
3000 return;
3001# endif
3002
3003 for (i = 0; ; ++i)
3004 {
3005# ifdef MSWIN
3006 short_u *p;
3007
3008 if ((p = (short_u *)_wenviron[i]) == NULL)
3009 return;
3010 entry = utf16_to_enc(p, NULL);
3011# else
3012 if ((entry = (char_u *)environ[i]) == NULL)
3013 return;
3014 entry = vim_strsave(entry);
3015# endif
3016 if (entry == NULL) // out of memory
3017 return;
3018 if ((value = vim_strchr(entry, '=')) == NULL)
3019 {
3020 vim_free(entry);
3021 continue;
3022 }
3023 *value++ = NUL;
3024 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3025 vim_free(entry);
3026 }
3027#endif
3028}
3029
3030/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003031 * "escape({string}, {chars})" function
3032 */
3033 static void
3034f_escape(typval_T *argvars, typval_T *rettv)
3035{
3036 char_u buf[NUMBUFLEN];
3037
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003038 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3039 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003040 rettv->v_type = VAR_STRING;
3041}
3042
3043/*
3044 * "eval()" function
3045 */
3046 static void
3047f_eval(typval_T *argvars, typval_T *rettv)
3048{
3049 char_u *s, *p;
3050
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003051 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003052 if (s != NULL)
3053 s = skipwhite(s);
3054
3055 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003056 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003057 {
3058 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003059 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003060 need_clr_eos = FALSE;
3061 rettv->v_type = VAR_NUMBER;
3062 rettv->vval.v_number = 0;
3063 }
3064 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003065 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003066}
3067
3068/*
3069 * "eventhandler()" function
3070 */
3071 static void
3072f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3073{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003074 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003075}
3076
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003077static garray_T redir_execute_ga;
3078
3079/*
3080 * Append "value[value_len]" to the execute() output.
3081 */
3082 void
3083execute_redir_str(char_u *value, int value_len)
3084{
3085 int len;
3086
3087 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003088 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003089 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003090 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003091 if (ga_grow(&redir_execute_ga, len) == OK)
3092 {
3093 mch_memmove((char *)redir_execute_ga.ga_data
3094 + redir_execute_ga.ga_len, value, len);
3095 redir_execute_ga.ga_len += len;
3096 }
3097}
3098
3099/*
3100 * Get next line from a list.
3101 * Called by do_cmdline() to get the next line.
3102 * Returns allocated string, or NULL for end of function.
3103 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003104 static char_u *
3105get_list_line(
3106 int c UNUSED,
3107 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003108 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02003109 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003110{
3111 listitem_T **p = (listitem_T **)cookie;
3112 listitem_T *item = *p;
3113 char_u buf[NUMBUFLEN];
3114 char_u *s;
3115
3116 if (item == NULL)
3117 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003118 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003119 *p = item->li_next;
3120 return s == NULL ? NULL : vim_strsave(s);
3121}
3122
3123/*
3124 * "execute()" function
3125 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02003126 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003127execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003128{
3129 char_u *cmd = NULL;
3130 list_T *list = NULL;
3131 int save_msg_silent = msg_silent;
3132 int save_emsg_silent = emsg_silent;
3133 int save_emsg_noredir = emsg_noredir;
3134 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003135 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003136 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003137 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003138 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003139
3140 rettv->vval.v_string = NULL;
3141 rettv->v_type = VAR_STRING;
3142
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003143 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003144 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003145 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003146 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003147 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003148 return;
3149 ++list->lv_refcount;
3150 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003151 else if (argvars[arg_off].v_type == VAR_JOB
3152 || argvars[arg_off].v_type == VAR_CHANNEL)
3153 {
3154 emsg(_(e_inval_string));
3155 return;
3156 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003157 else
3158 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003159 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003160 if (cmd == NULL)
3161 return;
3162 }
3163
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003164 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003165 {
3166 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003167 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003168
3169 if (s == NULL)
3170 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003171 if (*s == NUL)
3172 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003173 if (STRNCMP(s, "silent", 6) == 0)
3174 ++msg_silent;
3175 if (STRCMP(s, "silent!") == 0)
3176 {
3177 emsg_silent = TRUE;
3178 emsg_noredir = TRUE;
3179 }
3180 }
3181 else
3182 ++msg_silent;
3183
3184 if (redir_execute)
3185 save_ga = redir_execute_ga;
3186 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3187 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003188 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003189 if (!echo_output)
3190 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003191
3192 if (cmd != NULL)
3193 do_cmdline_cmd(cmd);
3194 else
3195 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003196 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003197
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003198 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003199 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003200 do_cmdline(NULL, get_list_line, (void *)&item,
3201 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3202 --list->lv_refcount;
3203 }
3204
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003205 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003206 if (ga_grow(&redir_execute_ga, 1) == OK)
3207 {
3208 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3209 rettv->vval.v_string = redir_execute_ga.ga_data;
3210 }
3211 else
3212 {
3213 ga_clear(&redir_execute_ga);
3214 rettv->vval.v_string = NULL;
3215 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003216 msg_silent = save_msg_silent;
3217 emsg_silent = save_emsg_silent;
3218 emsg_noredir = save_emsg_noredir;
3219
3220 redir_execute = save_redir_execute;
3221 if (redir_execute)
3222 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003223 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003224
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003225 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003226 if (echo_output)
3227 // When not working silently: put it in column zero. A following
3228 // "echon" will overwrite the message, unavoidably.
3229 msg_col = 0;
3230 else
3231 // When working silently: Put it back where it was, since nothing
3232 // should have been written.
3233 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003234}
3235
3236/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003237 * "execute()" function
3238 */
3239 static void
3240f_execute(typval_T *argvars, typval_T *rettv)
3241{
3242 execute_common(argvars, rettv, 0);
3243}
3244
3245/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003246 * "exists()" function
3247 */
3248 static void
3249f_exists(typval_T *argvars, typval_T *rettv)
3250{
3251 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003252 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003253
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003254 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003255 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003256 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003257 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003258 if (mch_getenv(p + 1) != NULL)
3259 n = TRUE;
3260 else
3261 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003262 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003263 p = expand_env_save(p);
3264 if (p != NULL && *p != '$')
3265 n = TRUE;
3266 vim_free(p);
3267 }
3268 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003269 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003270 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003271 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003272 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003273 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003274 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003275 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003276 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003277 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003278 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003279 else if (*p == '?') // internal function only
3280 {
3281 n = has_internal_func_name(p + 1);
3282 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003283 else if (*p == ':')
3284 {
3285 n = cmd_exists(p + 1);
3286 }
3287 else if (*p == '#')
3288 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003289 if (p[1] == '#')
3290 n = autocmd_supported(p + 2);
3291 else
3292 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003293 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003294 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003295 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003296 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003297 }
3298
3299 rettv->vval.v_number = n;
3300}
3301
3302#ifdef FEAT_FLOAT
3303/*
3304 * "exp()" function
3305 */
3306 static void
3307f_exp(typval_T *argvars, typval_T *rettv)
3308{
3309 float_T f = 0.0;
3310
3311 rettv->v_type = VAR_FLOAT;
3312 if (get_float_arg(argvars, &f) == OK)
3313 rettv->vval.v_float = exp(f);
3314 else
3315 rettv->vval.v_float = 0.0;
3316}
3317#endif
3318
3319/*
3320 * "expand()" function
3321 */
3322 static void
3323f_expand(typval_T *argvars, typval_T *rettv)
3324{
3325 char_u *s;
3326 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003327 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003328 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3329 expand_T xpc;
3330 int error = FALSE;
3331 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003332#ifdef BACKSLASH_IN_FILENAME
3333 char_u *p_csl_save = p_csl;
3334
3335 // avoid using 'completeslash' here
3336 p_csl = empty_option;
3337#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003338
3339 rettv->v_type = VAR_STRING;
3340 if (argvars[1].v_type != VAR_UNKNOWN
3341 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003342 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003343 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003344 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003345
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003346 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003347 if (*s == '%' || *s == '#' || *s == '<')
3348 {
3349 ++emsg_off;
3350 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3351 --emsg_off;
3352 if (rettv->v_type == VAR_LIST)
3353 {
3354 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3355 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003356 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003357 }
3358 else
3359 rettv->vval.v_string = result;
3360 }
3361 else
3362 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003363 // When the optional second argument is non-zero, don't remove matches
3364 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003365 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003366 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003367 options |= WILD_KEEP_ALL;
3368 if (!error)
3369 {
3370 ExpandInit(&xpc);
3371 xpc.xp_context = EXPAND_FILES;
3372 if (p_wic)
3373 options += WILD_ICASE;
3374 if (rettv->v_type == VAR_STRING)
3375 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3376 options, WILD_ALL);
3377 else if (rettv_list_alloc(rettv) != FAIL)
3378 {
3379 int i;
3380
3381 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3382 for (i = 0; i < xpc.xp_numfiles; i++)
3383 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3384 ExpandCleanup(&xpc);
3385 }
3386 }
3387 else
3388 rettv->vval.v_string = NULL;
3389 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003390#ifdef BACKSLASH_IN_FILENAME
3391 p_csl = p_csl_save;
3392#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003393}
3394
3395/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003396 * "expandcmd()" function
3397 * Expand all the special characters in a command string.
3398 */
3399 static void
3400f_expandcmd(typval_T *argvars, typval_T *rettv)
3401{
3402 exarg_T eap;
3403 char_u *cmdstr;
3404 char *errormsg = NULL;
3405
3406 rettv->v_type = VAR_STRING;
3407 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3408
3409 memset(&eap, 0, sizeof(eap));
3410 eap.cmd = cmdstr;
3411 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003412 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003413 eap.usefilter = FALSE;
3414 eap.nextcmd = NULL;
3415 eap.cmdidx = CMD_USER;
3416
3417 expand_filename(&eap, &cmdstr, &errormsg);
3418 if (errormsg != NULL && *errormsg != NUL)
3419 emsg(errormsg);
3420
3421 rettv->vval.v_string = cmdstr;
3422}
3423
3424/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003425 * "feedkeys()" function
3426 */
3427 static void
3428f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3429{
3430 int remap = TRUE;
3431 int insert = FALSE;
3432 char_u *keys, *flags;
3433 char_u nbuf[NUMBUFLEN];
3434 int typed = FALSE;
3435 int execute = FALSE;
3436 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003437 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003438 char_u *keys_esc;
3439
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003440 // This is not allowed in the sandbox. If the commands would still be
3441 // executed in the sandbox it would be OK, but it probably happens later,
3442 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003443 if (check_secure())
3444 return;
3445
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003446 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003447
3448 if (argvars[1].v_type != VAR_UNKNOWN)
3449 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003450 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003451 for ( ; *flags != NUL; ++flags)
3452 {
3453 switch (*flags)
3454 {
3455 case 'n': remap = FALSE; break;
3456 case 'm': remap = TRUE; break;
3457 case 't': typed = TRUE; break;
3458 case 'i': insert = TRUE; break;
3459 case 'x': execute = TRUE; break;
3460 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003461 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003462 }
3463 }
3464 }
3465
3466 if (*keys != NUL || execute)
3467 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003468 // Need to escape K_SPECIAL and CSI before putting the string in the
3469 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003470 keys_esc = vim_strsave_escape_csi(keys);
3471 if (keys_esc != NULL)
3472 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003473 if (lowlevel)
3474 {
3475#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003476 int idx;
3477 int len = (int)STRLEN(keys);
3478
3479 for (idx = 0; idx < len; ++idx)
3480 {
3481 // if a CTRL-C was typed, set got_int, similar to what
3482 // happens in fill_input_buf()
3483 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3484 got_int = TRUE;
3485 add_to_input_buf(keys + idx, 1);
3486 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003487#else
3488 emsg(_("E980: lowlevel input not supported"));
3489#endif
3490 }
3491 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003492 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003493 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003494 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003495 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003496#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003497 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003498#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003499 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003500 typebuf_was_filled = TRUE;
3501 }
3502 vim_free(keys_esc);
3503
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003504 if (execute)
3505 {
3506 int save_msg_scroll = msg_scroll;
3507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003508 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003509 msg_scroll = FALSE;
3510
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003511 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003512 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003513 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003514 ++in_feedkeys;
3515 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003516 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003517 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003518 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003519 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003520 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003521 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003522
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003523 msg_scroll |= save_msg_scroll;
3524 }
3525 }
3526 }
3527}
3528
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003529#ifdef FEAT_FLOAT
3530/*
3531 * "float2nr({float})" function
3532 */
3533 static void
3534f_float2nr(typval_T *argvars, typval_T *rettv)
3535{
3536 float_T f = 0.0;
3537
3538 if (get_float_arg(argvars, &f) == OK)
3539 {
Bram Moolenaar37184272020-05-23 19:30:05 +02003540 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003541 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02003542 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003543 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003544 else
3545 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003546 }
3547}
3548
3549/*
3550 * "floor({float})" function
3551 */
3552 static void
3553f_floor(typval_T *argvars, typval_T *rettv)
3554{
3555 float_T f = 0.0;
3556
3557 rettv->v_type = VAR_FLOAT;
3558 if (get_float_arg(argvars, &f) == OK)
3559 rettv->vval.v_float = floor(f);
3560 else
3561 rettv->vval.v_float = 0.0;
3562}
3563
3564/*
3565 * "fmod()" function
3566 */
3567 static void
3568f_fmod(typval_T *argvars, typval_T *rettv)
3569{
3570 float_T fx = 0.0, fy = 0.0;
3571
3572 rettv->v_type = VAR_FLOAT;
3573 if (get_float_arg(argvars, &fx) == OK
3574 && get_float_arg(&argvars[1], &fy) == OK)
3575 rettv->vval.v_float = fmod(fx, fy);
3576 else
3577 rettv->vval.v_float = 0.0;
3578}
3579#endif
3580
3581/*
3582 * "fnameescape({string})" function
3583 */
3584 static void
3585f_fnameescape(typval_T *argvars, typval_T *rettv)
3586{
3587 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003588 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003589 rettv->v_type = VAR_STRING;
3590}
3591
3592/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003593 * "foreground()" function
3594 */
3595 static void
3596f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3597{
3598#ifdef FEAT_GUI
3599 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003600 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003601 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003602 return;
3603 }
3604#endif
3605#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003606 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003607#endif
3608}
3609
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003610 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003611common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003612{
3613 char_u *s;
3614 char_u *name;
3615 int use_string = FALSE;
3616 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003617 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003618 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003619
3620 if (argvars[0].v_type == VAR_FUNC)
3621 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003622 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003623 s = argvars[0].vval.v_string;
3624 }
3625 else if (argvars[0].v_type == VAR_PARTIAL
3626 && argvars[0].vval.v_partial != NULL)
3627 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003628 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003629 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003630 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003631 }
3632 else
3633 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003634 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003635 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003636 use_string = TRUE;
3637 }
3638
Bram Moolenaar843b8842016-08-21 14:36:15 +02003639 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003640 {
3641 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003642 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003643 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3644 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003645 if (*name != NUL)
3646 s = NULL;
3647 }
3648
Bram Moolenaar843b8842016-08-21 14:36:15 +02003649 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3650 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003651 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003652 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003653 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003654 ? find_func(trans_name, is_global, NULL) == NULL
3655 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003656 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003657 else
3658 {
3659 int dict_idx = 0;
3660 int arg_idx = 0;
3661 list_T *list = NULL;
3662
3663 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3664 {
3665 char sid_buf[25];
3666 int off = *s == 's' ? 2 : 5;
3667
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003668 // Expand s: and <SID> into <SNR>nr_, so that the function can
3669 // also be called from another script. Using trans_function_name()
3670 // would also work, but some plugins depend on the name being
3671 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003672 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003673 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003674 if (name != NULL)
3675 {
3676 STRCPY(name, sid_buf);
3677 STRCAT(name, s + off);
3678 }
3679 }
3680 else
3681 name = vim_strsave(s);
3682
3683 if (argvars[1].v_type != VAR_UNKNOWN)
3684 {
3685 if (argvars[2].v_type != VAR_UNKNOWN)
3686 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003687 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003688 arg_idx = 1;
3689 dict_idx = 2;
3690 }
3691 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003692 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003693 dict_idx = 1;
3694 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003695 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003696 arg_idx = 1;
3697 if (dict_idx > 0)
3698 {
3699 if (argvars[dict_idx].v_type != VAR_DICT)
3700 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003701 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003702 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003703 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003704 }
3705 if (argvars[dict_idx].vval.v_dict == NULL)
3706 dict_idx = 0;
3707 }
3708 if (arg_idx > 0)
3709 {
3710 if (argvars[arg_idx].v_type != VAR_LIST)
3711 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003712 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003713 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003714 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003715 }
3716 list = argvars[arg_idx].vval.v_list;
3717 if (list == NULL || list->lv_len == 0)
3718 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003719 else if (list->lv_len > MAX_FUNC_ARGS)
3720 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003721 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003722 vim_free(name);
3723 goto theend;
3724 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003725 }
3726 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003727 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003728 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003729 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003730
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003731 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003732 if (pt == NULL)
3733 vim_free(name);
3734 else
3735 {
3736 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3737 {
3738 listitem_T *li;
3739 int i = 0;
3740 int arg_len = 0;
3741 int lv_len = 0;
3742
3743 if (arg_pt != NULL)
3744 arg_len = arg_pt->pt_argc;
3745 if (list != NULL)
3746 lv_len = list->lv_len;
3747 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003748 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003749 if (pt->pt_argv == NULL)
3750 {
3751 vim_free(pt);
3752 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003753 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003754 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003755 for (i = 0; i < arg_len; i++)
3756 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3757 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003758 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003759 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003760 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003761 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003762 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003763 }
3764
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003765 // For "function(dict.func, [], dict)" and "func" is a partial
3766 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003767 if (dict_idx > 0)
3768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003769 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003770 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3771 ++pt->pt_dict->dv_refcount;
3772 }
3773 else if (arg_pt != NULL)
3774 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003775 // If the dict was bound automatically the result is also
3776 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003777 pt->pt_dict = arg_pt->pt_dict;
3778 pt->pt_auto = arg_pt->pt_auto;
3779 if (pt->pt_dict != NULL)
3780 ++pt->pt_dict->dv_refcount;
3781 }
3782
3783 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003784 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3785 {
3786 pt->pt_func = arg_pt->pt_func;
3787 func_ptr_ref(pt->pt_func);
3788 vim_free(name);
3789 }
3790 else if (is_funcref)
3791 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003792 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003793 func_ptr_ref(pt->pt_func);
3794 vim_free(name);
3795 }
3796 else
3797 {
3798 pt->pt_name = name;
3799 func_ref(name);
3800 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003801 }
3802 rettv->v_type = VAR_PARTIAL;
3803 rettv->vval.v_partial = pt;
3804 }
3805 else
3806 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003807 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003808 rettv->v_type = VAR_FUNC;
3809 rettv->vval.v_string = name;
3810 func_ref(name);
3811 }
3812 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003813theend:
3814 vim_free(trans_name);
3815}
3816
3817/*
3818 * "funcref()" function
3819 */
3820 static void
3821f_funcref(typval_T *argvars, typval_T *rettv)
3822{
3823 common_function(argvars, rettv, TRUE);
3824}
3825
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003826 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003827ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003828{
3829 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3830 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003831 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003832}
3833
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003834/*
3835 * "function()" function
3836 */
3837 static void
3838f_function(typval_T *argvars, typval_T *rettv)
3839{
3840 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003841}
3842
3843/*
3844 * "garbagecollect()" function
3845 */
3846 static void
3847f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3848{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003849 // This is postponed until we are back at the toplevel, because we may be
3850 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003851 want_garbage_collect = TRUE;
3852
Bram Moolenaar2df47312020-09-05 17:30:44 +02003853 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003854 garbage_collect_at_exit = TRUE;
3855}
3856
3857/*
3858 * "get()" function
3859 */
3860 static void
3861f_get(typval_T *argvars, typval_T *rettv)
3862{
3863 listitem_T *li;
3864 list_T *l;
3865 dictitem_T *di;
3866 dict_T *d;
3867 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003868 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003869
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003870 if (argvars[0].v_type == VAR_BLOB)
3871 {
3872 int error = FALSE;
3873 int idx = tv_get_number_chk(&argvars[1], &error);
3874
3875 if (!error)
3876 {
3877 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003878 if (idx < 0)
3879 idx = blob_len(argvars[0].vval.v_blob) + idx;
3880 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3881 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003882 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003883 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003884 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003885 tv = rettv;
3886 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003887 }
3888 }
3889 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003890 {
3891 if ((l = argvars[0].vval.v_list) != NULL)
3892 {
3893 int error = FALSE;
3894
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003895 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003896 if (!error && li != NULL)
3897 tv = &li->li_tv;
3898 }
3899 }
3900 else if (argvars[0].v_type == VAR_DICT)
3901 {
3902 if ((d = argvars[0].vval.v_dict) != NULL)
3903 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003904 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003905 if (di != NULL)
3906 tv = &di->di_tv;
3907 }
3908 }
3909 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3910 {
3911 partial_T *pt;
3912 partial_T fref_pt;
3913
3914 if (argvars[0].v_type == VAR_PARTIAL)
3915 pt = argvars[0].vval.v_partial;
3916 else
3917 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003918 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003919 fref_pt.pt_name = argvars[0].vval.v_string;
3920 pt = &fref_pt;
3921 }
3922
3923 if (pt != NULL)
3924 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003925 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003926 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003927
3928 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3929 {
3930 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003931 n = partial_name(pt);
3932 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003933 rettv->vval.v_string = NULL;
3934 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003935 {
3936 rettv->vval.v_string = vim_strsave(n);
3937 if (rettv->v_type == VAR_FUNC)
3938 func_ref(rettv->vval.v_string);
3939 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003940 }
3941 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003942 {
3943 what_is_dict = TRUE;
3944 if (pt->pt_dict != NULL)
3945 rettv_dict_set(rettv, pt->pt_dict);
3946 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003947 else if (STRCMP(what, "args") == 0)
3948 {
3949 rettv->v_type = VAR_LIST;
3950 if (rettv_list_alloc(rettv) == OK)
3951 {
3952 int i;
3953
3954 for (i = 0; i < pt->pt_argc; ++i)
3955 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3956 }
3957 }
3958 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003959 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003960
3961 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3962 // third argument
3963 if (!what_is_dict)
3964 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003965 }
3966 }
3967 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003968 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003969
3970 if (tv == NULL)
3971 {
3972 if (argvars[2].v_type != VAR_UNKNOWN)
3973 copy_tv(&argvars[2], rettv);
3974 }
3975 else
3976 copy_tv(tv, rettv);
3977}
3978
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003979/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003980 * "getchangelist()" function
3981 */
3982 static void
3983f_getchangelist(typval_T *argvars, typval_T *rettv)
3984{
3985#ifdef FEAT_JUMPLIST
3986 buf_T *buf;
3987 int i;
3988 list_T *l;
3989 dict_T *d;
3990#endif
3991
3992 if (rettv_list_alloc(rettv) != OK)
3993 return;
3994
3995#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003996 if (argvars[0].v_type == VAR_UNKNOWN)
3997 buf = curbuf;
3998 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003999 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004000 if (buf == NULL)
4001 return;
4002
4003 l = list_alloc();
4004 if (l == NULL)
4005 return;
4006
4007 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4008 return;
4009 /*
4010 * The current window change list index tracks only the position in the
4011 * current buffer change list. For other buffers, use the change list
4012 * length as the current index.
4013 */
4014 list_append_number(rettv->vval.v_list,
4015 (varnumber_T)((buf == curwin->w_buffer)
4016 ? curwin->w_changelistidx : buf->b_changelistlen));
4017
4018 for (i = 0; i < buf->b_changelistlen; ++i)
4019 {
4020 if (buf->b_changelist[i].lnum == 0)
4021 continue;
4022 if ((d = dict_alloc()) == NULL)
4023 return;
4024 if (list_append_dict(l, d) == FAIL)
4025 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004026 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4027 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004028 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004029 }
4030#endif
4031}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004032
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004033 static void
4034getpos_both(
4035 typval_T *argvars,
4036 typval_T *rettv,
4037 int getcurpos,
4038 int charcol)
4039{
4040 pos_T *fp = NULL;
4041 pos_T pos;
4042 win_T *wp = curwin;
4043 list_T *l;
4044 int fnum = -1;
4045
4046 if (rettv_list_alloc(rettv) == OK)
4047 {
4048 l = rettv->vval.v_list;
4049 if (getcurpos)
4050 {
4051 if (argvars[0].v_type != VAR_UNKNOWN)
4052 {
4053 wp = find_win_by_nr_or_id(&argvars[0]);
4054 if (wp != NULL)
4055 fp = &wp->w_cursor;
4056 }
4057 else
4058 fp = &curwin->w_cursor;
4059 if (fp != NULL && charcol)
4060 {
4061 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01004062 pos.col =
4063 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004064 fp = &pos;
4065 }
4066 }
4067 else
4068 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
4069 if (fnum != -1)
4070 list_append_number(l, (varnumber_T)fnum);
4071 else
4072 list_append_number(l, (varnumber_T)0);
4073 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4074 : (varnumber_T)0);
4075 list_append_number(l, (fp != NULL)
4076 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4077 : (varnumber_T)0);
4078 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
4079 (varnumber_T)0);
4080 if (getcurpos)
4081 {
4082 int save_set_curswant = curwin->w_set_curswant;
4083 colnr_T save_curswant = curwin->w_curswant;
4084 colnr_T save_virtcol = curwin->w_virtcol;
4085
4086 if (wp == curwin)
4087 update_curswant();
4088 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
4089 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
4090
4091 // Do not change "curswant", as it is unexpected that a get
4092 // function has a side effect.
4093 if (wp == curwin && save_set_curswant)
4094 {
4095 curwin->w_set_curswant = save_set_curswant;
4096 curwin->w_curswant = save_curswant;
4097 curwin->w_virtcol = save_virtcol;
4098 curwin->w_valid &= ~VALID_VIRTCOL;
4099 }
4100 }
4101 }
4102 else
4103 rettv->vval.v_number = FALSE;
4104}
4105
4106/*
4107 * "getcharpos()" function
4108 */
4109 static void
4110f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
4111{
4112 getpos_both(argvars, rettv, FALSE, TRUE);
4113}
4114
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004115/*
4116 * "getcharsearch()" function
4117 */
4118 static void
4119f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4120{
4121 if (rettv_dict_alloc(rettv) != FAIL)
4122 {
4123 dict_T *dict = rettv->vval.v_dict;
4124
Bram Moolenaare0be1672018-07-08 16:50:37 +02004125 dict_add_string(dict, "char", last_csearch());
4126 dict_add_number(dict, "forward", last_csearch_forward());
4127 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004128 }
4129}
4130
4131/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004132 * "getenv()" function
4133 */
4134 static void
4135f_getenv(typval_T *argvars, typval_T *rettv)
4136{
4137 int mustfree = FALSE;
4138 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4139
4140 if (p == NULL)
4141 {
4142 rettv->v_type = VAR_SPECIAL;
4143 rettv->vval.v_number = VVAL_NULL;
4144 return;
4145 }
4146 if (!mustfree)
4147 p = vim_strsave(p);
4148 rettv->vval.v_string = p;
4149 rettv->v_type = VAR_STRING;
4150}
4151
4152/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004153 * "getfontname()" function
4154 */
4155 static void
4156f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4157{
4158 rettv->v_type = VAR_STRING;
4159 rettv->vval.v_string = NULL;
4160#ifdef FEAT_GUI
4161 if (gui.in_use)
4162 {
4163 GuiFont font;
4164 char_u *name = NULL;
4165
4166 if (argvars[0].v_type == VAR_UNKNOWN)
4167 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004168 // Get the "Normal" font. Either the name saved by
4169 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004170 font = gui.norm_font;
4171 name = hl_get_font_name();
4172 }
4173 else
4174 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004175 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004176 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004177 return;
4178 font = gui_mch_get_font(name, FALSE);
4179 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004180 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004181 }
4182 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4183 if (argvars[0].v_type != VAR_UNKNOWN)
4184 gui_mch_free_font(font);
4185 }
4186#endif
4187}
4188
4189/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004190 * "getjumplist()" function
4191 */
4192 static void
4193f_getjumplist(typval_T *argvars, typval_T *rettv)
4194{
4195#ifdef FEAT_JUMPLIST
4196 win_T *wp;
4197 int i;
4198 list_T *l;
4199 dict_T *d;
4200#endif
4201
4202 if (rettv_list_alloc(rettv) != OK)
4203 return;
4204
4205#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004206 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004207 if (wp == NULL)
4208 return;
4209
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004210 cleanup_jumplist(wp, TRUE);
4211
Bram Moolenaar4f505882018-02-10 21:06:32 +01004212 l = list_alloc();
4213 if (l == NULL)
4214 return;
4215
4216 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4217 return;
4218 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4219
4220 for (i = 0; i < wp->w_jumplistlen; ++i)
4221 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004222 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4223 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004224 if ((d = dict_alloc()) == NULL)
4225 return;
4226 if (list_append_dict(l, d) == FAIL)
4227 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004228 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4229 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004230 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004231 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004232 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004233 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004234 }
4235#endif
4236}
4237
4238/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004239 * "getpid()" function
4240 */
4241 static void
4242f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4243{
4244 rettv->vval.v_number = mch_get_pid();
4245}
4246
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004247/*
4248 * "getcurpos()" function
4249 */
4250 static void
4251f_getcurpos(typval_T *argvars, typval_T *rettv)
4252{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004253 getpos_both(argvars, rettv, TRUE, FALSE);
4254}
4255
4256 static void
4257f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4258{
4259 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004260}
4261
4262/*
4263 * "getpos(string)" function
4264 */
4265 static void
4266f_getpos(typval_T *argvars, typval_T *rettv)
4267{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004268 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004269}
4270
4271/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004272 * "getreg()" function
4273 */
4274 static void
4275f_getreg(typval_T *argvars, typval_T *rettv)
4276{
4277 char_u *strregname;
4278 int regname;
4279 int arg2 = FALSE;
4280 int return_list = FALSE;
4281 int error = FALSE;
4282
4283 if (argvars[0].v_type != VAR_UNKNOWN)
4284 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004285 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004286 error = strregname == NULL;
4287 if (argvars[1].v_type != VAR_UNKNOWN)
4288 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004289 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004290 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004291 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004292 }
4293 }
4294 else
4295 strregname = get_vim_var_str(VV_REG);
4296
4297 if (error)
4298 return;
4299
4300 regname = (strregname == NULL ? '"' : *strregname);
4301 if (regname == 0)
4302 regname = '"';
4303
4304 if (return_list)
4305 {
4306 rettv->v_type = VAR_LIST;
4307 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4308 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4309 if (rettv->vval.v_list == NULL)
4310 (void)rettv_list_alloc(rettv);
4311 else
4312 ++rettv->vval.v_list->lv_refcount;
4313 }
4314 else
4315 {
4316 rettv->v_type = VAR_STRING;
4317 rettv->vval.v_string = get_reg_contents(regname,
4318 arg2 ? GREG_EXPR_SRC : 0);
4319 }
4320}
4321
4322/*
4323 * "getregtype()" function
4324 */
4325 static void
4326f_getregtype(typval_T *argvars, typval_T *rettv)
4327{
4328 char_u *strregname;
4329 int regname;
4330 char_u buf[NUMBUFLEN + 2];
4331 long reglen = 0;
4332
4333 if (argvars[0].v_type != VAR_UNKNOWN)
4334 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004335 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004336 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004337 {
4338 rettv->v_type = VAR_STRING;
4339 rettv->vval.v_string = NULL;
4340 return;
4341 }
4342 }
4343 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004344 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004345 strregname = get_vim_var_str(VV_REG);
4346
4347 regname = (strregname == NULL ? '"' : *strregname);
4348 if (regname == 0)
4349 regname = '"';
4350
4351 buf[0] = NUL;
4352 buf[1] = NUL;
4353 switch (get_reg_type(regname, &reglen))
4354 {
4355 case MLINE: buf[0] = 'V'; break;
4356 case MCHAR: buf[0] = 'v'; break;
4357 case MBLOCK:
4358 buf[0] = Ctrl_V;
4359 sprintf((char *)buf + 1, "%ld", reglen + 1);
4360 break;
4361 }
4362 rettv->v_type = VAR_STRING;
4363 rettv->vval.v_string = vim_strsave(buf);
4364}
4365
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004366/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004367 * "gettagstack()" function
4368 */
4369 static void
4370f_gettagstack(typval_T *argvars, typval_T *rettv)
4371{
4372 win_T *wp = curwin; // default is current window
4373
4374 if (rettv_dict_alloc(rettv) != OK)
4375 return;
4376
4377 if (argvars[0].v_type != VAR_UNKNOWN)
4378 {
4379 wp = find_win_by_nr_or_id(&argvars[0]);
4380 if (wp == NULL)
4381 return;
4382 }
4383
4384 get_tagstack(wp, rettv->vval.v_dict);
4385}
4386
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004387/*
4388 * "gettext()" function
4389 */
4390 static void
4391f_gettext(typval_T *argvars, typval_T *rettv)
4392{
4393 if (argvars[0].v_type != VAR_STRING
4394 || argvars[0].vval.v_string == NULL
4395 || *argvars[0].vval.v_string == NUL)
4396 {
4397 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4398 }
4399 else
4400 {
4401 rettv->v_type = VAR_STRING;
4402 rettv->vval.v_string = vim_strsave(
4403 (char_u *)_(argvars[0].vval.v_string));
4404 }
4405}
4406
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004407// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004408#include "version.h"
4409
4410/*
4411 * "has()" function
4412 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004413 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004414f_has(typval_T *argvars, typval_T *rettv)
4415{
4416 int i;
4417 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004418 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004419 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004420 typedef struct {
4421 char *name;
4422 short present;
4423 } has_item_T;
4424 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004425 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004426 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004427#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004428 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004429#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004430 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004431#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004432 },
4433 {"arp",
4434#if defined(AMIGA) && defined(FEAT_ARP)
4435 1
4436#else
4437 0
4438#endif
4439 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004440 {"haiku",
4441#ifdef __HAIKU__
4442 1
4443#else
4444 0
4445#endif
4446 },
4447 {"bsd",
4448#if defined(BSD) && !defined(MACOS_X)
4449 1
4450#else
4451 0
4452#endif
4453 },
4454 {"hpux",
4455#ifdef hpux
4456 1
4457#else
4458 0
4459#endif
4460 },
4461 {"linux",
4462#ifdef __linux__
4463 1
4464#else
4465 0
4466#endif
4467 },
4468 {"mac", // Mac OS X (and, once, Mac OS Classic)
4469#ifdef MACOS_X
4470 1
4471#else
4472 0
4473#endif
4474 },
4475 {"osx", // Mac OS X
4476#ifdef MACOS_X
4477 1
4478#else
4479 0
4480#endif
4481 },
4482 {"macunix", // Mac OS X, with the darwin feature
4483#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4484 1
4485#else
4486 0
4487#endif
4488 },
4489 {"osxdarwin", // synonym for macunix
4490#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4491 1
4492#else
4493 0
4494#endif
4495 },
4496 {"qnx",
4497#ifdef __QNX__
4498 1
4499#else
4500 0
4501#endif
4502 },
4503 {"sun",
4504#ifdef SUN_SYSTEM
4505 1
4506#else
4507 0
4508#endif
4509 },
4510 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004511#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004512 1
4513#else
4514 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004515#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004516 },
4517 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004518#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004519 1
4520#else
4521 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004522#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004523 },
4524 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004525#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004526 1
4527#else
4528 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004529#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004530 },
4531 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004532#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004533 1
4534#else
4535 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004536#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004537 },
4538 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004539#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004540 1
4541#else
4542 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004543#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004544 },
4545 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004546#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004547 1
4548#else
4549 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004550#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004551 },
4552 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004553#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004554 1
4555#else
4556 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004557#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004558 },
4559 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004560#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004561 1
4562#else
4563 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004564#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004565 },
4566 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004567#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004568 1
4569#else
4570 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004571#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004572 },
4573 {"autocmd", 1},
4574 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004575#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004576 1
4577#else
4578 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004579#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004580 },
4581 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004582#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004583 1
4584#else
4585 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004586#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004587 },
4588 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004589#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004590 1
4591#else
4592 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004593#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004594 },
4595 {"balloon_multiline",
4596#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4597 // MS-Windows requires runtime check, see below
4598 1
4599#else
4600 0
4601#endif
4602 },
4603 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004604#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004605 1
4606#else
4607 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004608#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004609 },
4610 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004611#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004612 1
4613#else
4614 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004615#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004616 },
4617 {"all_builtin_terms",
4618#if defined(ALL_BUILTIN_TCAPS)
4619 1
4620#else
4621 0
4622#endif
4623 },
4624 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004625#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004626 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004627 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004628 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004629#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004630 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004631#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004632 },
4633 {"byte_offset",
4634#ifdef FEAT_BYTEOFF
4635 1
4636#else
4637 0
4638#endif
4639 },
4640 {"channel",
4641#ifdef FEAT_JOB_CHANNEL
4642 1
4643#else
4644 0
4645#endif
4646 },
4647 {"cindent",
4648#ifdef FEAT_CINDENT
4649 1
4650#else
4651 0
4652#endif
4653 },
4654 {"clientserver",
4655#ifdef FEAT_CLIENTSERVER
4656 1
4657#else
4658 0
4659#endif
4660 },
4661 {"clipboard",
4662#ifdef FEAT_CLIPBOARD
4663 1
4664#else
4665 0
4666#endif
4667 },
4668 {"cmdline_compl", 1},
4669 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004670 {"cmdwin",
4671#ifdef FEAT_CMDWIN
4672 1
4673#else
4674 0
4675#endif
4676 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004677 {"comments", 1},
4678 {"conceal",
4679#ifdef FEAT_CONCEAL
4680 1
4681#else
4682 0
4683#endif
4684 },
4685 {"cryptv",
4686#ifdef FEAT_CRYPT
4687 1
4688#else
4689 0
4690#endif
4691 },
4692 {"crypt-blowfish",
4693#ifdef FEAT_CRYPT
4694 1
4695#else
4696 0
4697#endif
4698 },
4699 {"crypt-blowfish2",
4700#ifdef FEAT_CRYPT
4701 1
4702#else
4703 0
4704#endif
4705 },
4706 {"cscope",
4707#ifdef FEAT_CSCOPE
4708 1
4709#else
4710 0
4711#endif
4712 },
4713 {"cursorbind", 1},
4714 {"cursorshape",
4715#ifdef CURSOR_SHAPE
4716 1
4717#else
4718 0
4719#endif
4720 },
4721 {"debug",
4722#ifdef DEBUG
4723 1
4724#else
4725 0
4726#endif
4727 },
4728 {"dialog_con",
4729#ifdef FEAT_CON_DIALOG
4730 1
4731#else
4732 0
4733#endif
4734 },
4735 {"dialog_gui",
4736#ifdef FEAT_GUI_DIALOG
4737 1
4738#else
4739 0
4740#endif
4741 },
4742 {"diff",
4743#ifdef FEAT_DIFF
4744 1
4745#else
4746 0
4747#endif
4748 },
4749 {"digraphs",
4750#ifdef FEAT_DIGRAPHS
4751 1
4752#else
4753 0
4754#endif
4755 },
4756 {"directx",
4757#ifdef FEAT_DIRECTX
4758 1
4759#else
4760 0
4761#endif
4762 },
4763 {"dnd",
4764#ifdef FEAT_DND
4765 1
4766#else
4767 0
4768#endif
4769 },
4770 {"emacs_tags",
4771#ifdef FEAT_EMACS_TAGS
4772 1
4773#else
4774 0
4775#endif
4776 },
4777 {"eval", 1}, // always present, of course!
4778 {"ex_extra", 1}, // graduated feature
4779 {"extra_search",
4780#ifdef FEAT_SEARCH_EXTRA
4781 1
4782#else
4783 0
4784#endif
4785 },
4786 {"file_in_path",
4787#ifdef FEAT_SEARCHPATH
4788 1
4789#else
4790 0
4791#endif
4792 },
4793 {"filterpipe",
4794#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4795 1
4796#else
4797 0
4798#endif
4799 },
4800 {"find_in_path",
4801#ifdef FEAT_FIND_ID
4802 1
4803#else
4804 0
4805#endif
4806 },
4807 {"float",
4808#ifdef FEAT_FLOAT
4809 1
4810#else
4811 0
4812#endif
4813 },
4814 {"folding",
4815#ifdef FEAT_FOLDING
4816 1
4817#else
4818 0
4819#endif
4820 },
4821 {"footer",
4822#ifdef FEAT_FOOTER
4823 1
4824#else
4825 0
4826#endif
4827 },
4828 {"fork",
4829#if !defined(USE_SYSTEM) && defined(UNIX)
4830 1
4831#else
4832 0
4833#endif
4834 },
4835 {"gettext",
4836#ifdef FEAT_GETTEXT
4837 1
4838#else
4839 0
4840#endif
4841 },
4842 {"gui",
4843#ifdef FEAT_GUI
4844 1
4845#else
4846 0
4847#endif
4848 },
4849 {"gui_neXtaw",
4850#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4851 1
4852#else
4853 0
4854#endif
4855 },
4856 {"gui_athena",
4857#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4858 1
4859#else
4860 0
4861#endif
4862 },
4863 {"gui_gtk",
4864#ifdef FEAT_GUI_GTK
4865 1
4866#else
4867 0
4868#endif
4869 },
4870 {"gui_gtk2",
4871#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4872 1
4873#else
4874 0
4875#endif
4876 },
4877 {"gui_gtk3",
4878#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4879 1
4880#else
4881 0
4882#endif
4883 },
4884 {"gui_gnome",
4885#ifdef FEAT_GUI_GNOME
4886 1
4887#else
4888 0
4889#endif
4890 },
4891 {"gui_haiku",
4892#ifdef FEAT_GUI_HAIKU
4893 1
4894#else
4895 0
4896#endif
4897 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004898 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004899 {"gui_motif",
4900#ifdef FEAT_GUI_MOTIF
4901 1
4902#else
4903 0
4904#endif
4905 },
4906 {"gui_photon",
4907#ifdef FEAT_GUI_PHOTON
4908 1
4909#else
4910 0
4911#endif
4912 },
4913 {"gui_win32",
4914#ifdef FEAT_GUI_MSWIN
4915 1
4916#else
4917 0
4918#endif
4919 },
4920 {"iconv",
4921#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4922 1
4923#else
4924 0
4925#endif
4926 },
4927 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004928 {"ipv6",
4929#ifdef FEAT_IPV6
4930 1
4931#else
4932 0
4933#endif
4934 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004935 {"job",
4936#ifdef FEAT_JOB_CHANNEL
4937 1
4938#else
4939 0
4940#endif
4941 },
4942 {"jumplist",
4943#ifdef FEAT_JUMPLIST
4944 1
4945#else
4946 0
4947#endif
4948 },
4949 {"keymap",
4950#ifdef FEAT_KEYMAP
4951 1
4952#else
4953 0
4954#endif
4955 },
4956 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4957 {"langmap",
4958#ifdef FEAT_LANGMAP
4959 1
4960#else
4961 0
4962#endif
4963 },
4964 {"libcall",
4965#ifdef FEAT_LIBCALL
4966 1
4967#else
4968 0
4969#endif
4970 },
4971 {"linebreak",
4972#ifdef FEAT_LINEBREAK
4973 1
4974#else
4975 0
4976#endif
4977 },
4978 {"lispindent",
4979#ifdef FEAT_LISP
4980 1
4981#else
4982 0
4983#endif
4984 },
4985 {"listcmds", 1},
4986 {"localmap", 1},
4987 {"lua",
4988#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4989 1
4990#else
4991 0
4992#endif
4993 },
4994 {"menu",
4995#ifdef FEAT_MENU
4996 1
4997#else
4998 0
4999#endif
5000 },
5001 {"mksession",
5002#ifdef FEAT_SESSION
5003 1
5004#else
5005 0
5006#endif
5007 },
5008 {"modify_fname", 1},
5009 {"mouse", 1},
5010 {"mouseshape",
5011#ifdef FEAT_MOUSESHAPE
5012 1
5013#else
5014 0
5015#endif
5016 },
5017 {"mouse_dec",
5018#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
5019 1
5020#else
5021 0
5022#endif
5023 },
5024 {"mouse_gpm",
5025#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
5026 1
5027#else
5028 0
5029#endif
5030 },
5031 {"mouse_jsbterm",
5032#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
5033 1
5034#else
5035 0
5036#endif
5037 },
5038 {"mouse_netterm",
5039#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
5040 1
5041#else
5042 0
5043#endif
5044 },
5045 {"mouse_pterm",
5046#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
5047 1
5048#else
5049 0
5050#endif
5051 },
5052 {"mouse_sgr",
5053#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5054 1
5055#else
5056 0
5057#endif
5058 },
5059 {"mouse_sysmouse",
5060#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
5061 1
5062#else
5063 0
5064#endif
5065 },
5066 {"mouse_urxvt",
5067#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
5068 1
5069#else
5070 0
5071#endif
5072 },
5073 {"mouse_xterm",
5074#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5075 1
5076#else
5077 0
5078#endif
5079 },
5080 {"multi_byte", 1},
5081 {"multi_byte_ime",
5082#ifdef FEAT_MBYTE_IME
5083 1
5084#else
5085 0
5086#endif
5087 },
5088 {"multi_lang",
5089#ifdef FEAT_MULTI_LANG
5090 1
5091#else
5092 0
5093#endif
5094 },
5095 {"mzscheme",
5096#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
5097 1
5098#else
5099 0
5100#endif
5101 },
5102 {"num64", 1},
5103 {"ole",
5104#ifdef FEAT_OLE
5105 1
5106#else
5107 0
5108#endif
5109 },
5110 {"packages",
5111#ifdef FEAT_EVAL
5112 1
5113#else
5114 0
5115#endif
5116 },
5117 {"path_extra",
5118#ifdef FEAT_PATH_EXTRA
5119 1
5120#else
5121 0
5122#endif
5123 },
5124 {"perl",
5125#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
5126 1
5127#else
5128 0
5129#endif
5130 },
5131 {"persistent_undo",
5132#ifdef FEAT_PERSISTENT_UNDO
5133 1
5134#else
5135 0
5136#endif
5137 },
5138 {"python_compiled",
5139#if defined(FEAT_PYTHON)
5140 1
5141#else
5142 0
5143#endif
5144 },
5145 {"python_dynamic",
5146#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
5147 1
5148#else
5149 0
5150#endif
5151 },
5152 {"python",
5153#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
5154 1
5155#else
5156 0
5157#endif
5158 },
5159 {"pythonx",
5160#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
5161 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
5162 1
5163#else
5164 0
5165#endif
5166 },
5167 {"python3_compiled",
5168#if defined(FEAT_PYTHON3)
5169 1
5170#else
5171 0
5172#endif
5173 },
5174 {"python3_dynamic",
5175#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
5176 1
5177#else
5178 0
5179#endif
5180 },
5181 {"python3",
5182#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
5183 1
5184#else
5185 0
5186#endif
5187 },
5188 {"popupwin",
5189#ifdef FEAT_PROP_POPUP
5190 1
5191#else
5192 0
5193#endif
5194 },
5195 {"postscript",
5196#ifdef FEAT_POSTSCRIPT
5197 1
5198#else
5199 0
5200#endif
5201 },
5202 {"printer",
5203#ifdef FEAT_PRINTER
5204 1
5205#else
5206 0
5207#endif
5208 },
5209 {"profile",
5210#ifdef FEAT_PROFILE
5211 1
5212#else
5213 0
5214#endif
5215 },
5216 {"reltime",
5217#ifdef FEAT_RELTIME
5218 1
5219#else
5220 0
5221#endif
5222 },
5223 {"quickfix",
5224#ifdef FEAT_QUICKFIX
5225 1
5226#else
5227 0
5228#endif
5229 },
5230 {"rightleft",
5231#ifdef FEAT_RIGHTLEFT
5232 1
5233#else
5234 0
5235#endif
5236 },
5237 {"ruby",
5238#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5239 1
5240#else
5241 0
5242#endif
5243 },
5244 {"scrollbind", 1},
5245 {"showcmd",
5246#ifdef FEAT_CMDL_INFO
5247 1
5248#else
5249 0
5250#endif
5251 },
5252 {"cmdline_info",
5253#ifdef FEAT_CMDL_INFO
5254 1
5255#else
5256 0
5257#endif
5258 },
5259 {"signs",
5260#ifdef FEAT_SIGNS
5261 1
5262#else
5263 0
5264#endif
5265 },
5266 {"smartindent",
5267#ifdef FEAT_SMARTINDENT
5268 1
5269#else
5270 0
5271#endif
5272 },
5273 {"startuptime",
5274#ifdef STARTUPTIME
5275 1
5276#else
5277 0
5278#endif
5279 },
5280 {"statusline",
5281#ifdef FEAT_STL_OPT
5282 1
5283#else
5284 0
5285#endif
5286 },
5287 {"netbeans_intg",
5288#ifdef FEAT_NETBEANS_INTG
5289 1
5290#else
5291 0
5292#endif
5293 },
5294 {"sound",
5295#ifdef FEAT_SOUND
5296 1
5297#else
5298 0
5299#endif
5300 },
5301 {"spell",
5302#ifdef FEAT_SPELL
5303 1
5304#else
5305 0
5306#endif
5307 },
5308 {"syntax",
5309#ifdef FEAT_SYN_HL
5310 1
5311#else
5312 0
5313#endif
5314 },
5315 {"system",
5316#if defined(USE_SYSTEM) || !defined(UNIX)
5317 1
5318#else
5319 0
5320#endif
5321 },
5322 {"tag_binary",
5323#ifdef FEAT_TAG_BINS
5324 1
5325#else
5326 0
5327#endif
5328 },
5329 {"tcl",
5330#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5331 1
5332#else
5333 0
5334#endif
5335 },
5336 {"termguicolors",
5337#ifdef FEAT_TERMGUICOLORS
5338 1
5339#else
5340 0
5341#endif
5342 },
5343 {"terminal",
5344#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5345 1
5346#else
5347 0
5348#endif
5349 },
5350 {"terminfo",
5351#ifdef TERMINFO
5352 1
5353#else
5354 0
5355#endif
5356 },
5357 {"termresponse",
5358#ifdef FEAT_TERMRESPONSE
5359 1
5360#else
5361 0
5362#endif
5363 },
5364 {"textobjects",
5365#ifdef FEAT_TEXTOBJ
5366 1
5367#else
5368 0
5369#endif
5370 },
5371 {"textprop",
5372#ifdef FEAT_PROP_POPUP
5373 1
5374#else
5375 0
5376#endif
5377 },
5378 {"tgetent",
5379#ifdef HAVE_TGETENT
5380 1
5381#else
5382 0
5383#endif
5384 },
5385 {"timers",
5386#ifdef FEAT_TIMERS
5387 1
5388#else
5389 0
5390#endif
5391 },
5392 {"title",
5393#ifdef FEAT_TITLE
5394 1
5395#else
5396 0
5397#endif
5398 },
5399 {"toolbar",
5400#ifdef FEAT_TOOLBAR
5401 1
5402#else
5403 0
5404#endif
5405 },
5406 {"unnamedplus",
5407#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5408 1
5409#else
5410 0
5411#endif
5412 },
5413 {"user-commands", 1}, // was accidentally included in 5.4
5414 {"user_commands", 1},
5415 {"vartabs",
5416#ifdef FEAT_VARTABS
5417 1
5418#else
5419 0
5420#endif
5421 },
5422 {"vertsplit", 1},
5423 {"viminfo",
5424#ifdef FEAT_VIMINFO
5425 1
5426#else
5427 0
5428#endif
5429 },
5430 {"vimscript-1", 1},
5431 {"vimscript-2", 1},
5432 {"vimscript-3", 1},
5433 {"vimscript-4", 1},
5434 {"virtualedit", 1},
5435 {"visual", 1},
5436 {"visualextra", 1},
5437 {"vreplace", 1},
5438 {"vtp",
5439#ifdef FEAT_VTP
5440 1
5441#else
5442 0
5443#endif
5444 },
5445 {"wildignore",
5446#ifdef FEAT_WILDIGN
5447 1
5448#else
5449 0
5450#endif
5451 },
5452 {"wildmenu",
5453#ifdef FEAT_WILDMENU
5454 1
5455#else
5456 0
5457#endif
5458 },
5459 {"windows", 1},
5460 {"winaltkeys",
5461#ifdef FEAT_WAK
5462 1
5463#else
5464 0
5465#endif
5466 },
5467 {"writebackup",
5468#ifdef FEAT_WRITEBACKUP
5469 1
5470#else
5471 0
5472#endif
5473 },
5474 {"xim",
5475#ifdef FEAT_XIM
5476 1
5477#else
5478 0
5479#endif
5480 },
5481 {"xfontset",
5482#ifdef FEAT_XFONTSET
5483 1
5484#else
5485 0
5486#endif
5487 },
5488 {"xpm",
5489#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5490 1
5491#else
5492 0
5493#endif
5494 },
5495 {"xpm_w32", // for backward compatibility
5496#ifdef FEAT_XPM_W32
5497 1
5498#else
5499 0
5500#endif
5501 },
5502 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005503#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005504 1
5505#else
5506 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005507#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005508 },
5509 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005510#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005511 1
5512#else
5513 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005514#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005515 },
5516 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005517#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005518 1
5519#else
5520 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005521#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005522 },
5523 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005524#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005525 1
5526#else
5527 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005528#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005529 },
5530 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005531#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005532 1
5533#else
5534 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005535#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005536 },
5537 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005538 };
5539
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005540 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005541 for (i = 0; has_list[i].name != NULL; ++i)
5542 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005543 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005544 x = TRUE;
5545 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005546 break;
5547 }
5548
Bram Moolenaar79296512020-03-22 16:17:14 +01005549 // features also in has_list[] but sometimes enabled at runtime
5550 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005551 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005552 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005553 {
5554 // intentionally empty
5555 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005556#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005557 else if (STRICMP(name, "balloon_multiline") == 0)
5558 n = multiline_balloon_available();
5559#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005560#ifdef VIMDLL
5561 else if (STRICMP(name, "filterpipe") == 0)
5562 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005563#endif
5564#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5565 else if (STRICMP(name, "iconv") == 0)
5566 n = iconv_enabled(FALSE);
5567#endif
5568#ifdef DYNAMIC_LUA
5569 else if (STRICMP(name, "lua") == 0)
5570 n = lua_enabled(FALSE);
5571#endif
5572#ifdef DYNAMIC_MZSCHEME
5573 else if (STRICMP(name, "mzscheme") == 0)
5574 n = mzscheme_enabled(FALSE);
5575#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005576#ifdef DYNAMIC_PERL
5577 else if (STRICMP(name, "perl") == 0)
5578 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005579#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005580#ifdef DYNAMIC_PYTHON
5581 else if (STRICMP(name, "python") == 0)
5582 n = python_enabled(FALSE);
5583#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005584#ifdef DYNAMIC_PYTHON3
5585 else if (STRICMP(name, "python3") == 0)
5586 n = python3_enabled(FALSE);
5587#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005588#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5589 else if (STRICMP(name, "pythonx") == 0)
5590 {
5591# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5592 if (p_pyx == 0)
5593 n = python3_enabled(FALSE) || python_enabled(FALSE);
5594 else if (p_pyx == 3)
5595 n = python3_enabled(FALSE);
5596 else if (p_pyx == 2)
5597 n = python_enabled(FALSE);
5598# elif defined(DYNAMIC_PYTHON)
5599 n = python_enabled(FALSE);
5600# elif defined(DYNAMIC_PYTHON3)
5601 n = python3_enabled(FALSE);
5602# endif
5603 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005604#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005605#ifdef DYNAMIC_RUBY
5606 else if (STRICMP(name, "ruby") == 0)
5607 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005608#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005609#ifdef DYNAMIC_TCL
5610 else if (STRICMP(name, "tcl") == 0)
5611 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005612#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005613#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005614 else if (STRICMP(name, "terminal") == 0)
5615 n = terminal_enabled();
5616#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005617 }
5618
Bram Moolenaar79296512020-03-22 16:17:14 +01005619 // features not in has_list[]
5620 if (x == FALSE)
5621 {
5622 if (STRNICMP(name, "patch", 5) == 0)
5623 {
5624 x = TRUE;
5625 if (name[5] == '-'
5626 && STRLEN(name) >= 11
5627 && vim_isdigit(name[6])
5628 && vim_isdigit(name[8])
5629 && vim_isdigit(name[10]))
5630 {
5631 int major = atoi((char *)name + 6);
5632 int minor = atoi((char *)name + 8);
5633
5634 // Expect "patch-9.9.01234".
5635 n = (major < VIM_VERSION_MAJOR
5636 || (major == VIM_VERSION_MAJOR
5637 && (minor < VIM_VERSION_MINOR
5638 || (minor == VIM_VERSION_MINOR
5639 && has_patch(atoi((char *)name + 10))))));
5640 }
5641 else
5642 n = has_patch(atoi((char *)name + 5));
5643 }
5644 else if (STRICMP(name, "vim_starting") == 0)
5645 {
5646 x = TRUE;
5647 n = (starting != 0);
5648 }
5649 else if (STRICMP(name, "ttyin") == 0)
5650 {
5651 x = TRUE;
5652 n = mch_input_isatty();
5653 }
5654 else if (STRICMP(name, "ttyout") == 0)
5655 {
5656 x = TRUE;
5657 n = stdout_isatty;
5658 }
5659 else if (STRICMP(name, "multi_byte_encoding") == 0)
5660 {
5661 x = TRUE;
5662 n = has_mbyte;
5663 }
5664 else if (STRICMP(name, "gui_running") == 0)
5665 {
5666 x = TRUE;
5667#ifdef FEAT_GUI
5668 n = (gui.in_use || gui.starting);
5669#endif
5670 }
5671 else if (STRICMP(name, "browse") == 0)
5672 {
5673 x = TRUE;
5674#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5675 n = gui.in_use; // gui_mch_browse() works when GUI is running
5676#endif
5677 }
5678 else if (STRICMP(name, "syntax_items") == 0)
5679 {
5680 x = TRUE;
5681#ifdef FEAT_SYN_HL
5682 n = syntax_present(curwin);
5683#endif
5684 }
5685 else if (STRICMP(name, "vcon") == 0)
5686 {
5687 x = TRUE;
5688#ifdef FEAT_VTP
5689 n = is_term_win32() && has_vtp_working();
5690#endif
5691 }
5692 else if (STRICMP(name, "netbeans_enabled") == 0)
5693 {
5694 x = TRUE;
5695#ifdef FEAT_NETBEANS_INTG
5696 n = netbeans_active();
5697#endif
5698 }
5699 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5700 {
5701 x = TRUE;
5702#ifdef FEAT_MOUSE_GPM
5703 n = gpm_enabled();
5704#endif
5705 }
5706 else if (STRICMP(name, "conpty") == 0)
5707 {
5708 x = TRUE;
5709#if defined(FEAT_TERMINAL) && defined(MSWIN)
5710 n = use_conpty();
5711#endif
5712 }
5713 else if (STRICMP(name, "clipboard_working") == 0)
5714 {
5715 x = TRUE;
5716#ifdef FEAT_CLIPBOARD
5717 n = clip_star.available;
5718#endif
5719 }
5720 }
5721
Bram Moolenaar04637e22020-09-05 18:45:29 +02005722 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005723 // return whether feature could ever be enabled
5724 rettv->vval.v_number = x;
5725 else
5726 // return whether feature is enabled
5727 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005728}
5729
5730/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005731 * Return TRUE if "feature" can change later.
5732 * Also when checking for the feature has side effects, such as loading a DLL.
5733 */
5734 int
5735dynamic_feature(char_u *feature)
5736{
5737 return (feature == NULL
5738#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5739 || STRICMP(feature, "balloon_multiline") == 0
5740#endif
5741#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5742 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5743#endif
5744#ifdef VIMDLL
5745 || STRICMP(feature, "filterpipe") == 0
5746#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005747#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005748 // this can only change on Unix where the ":gui" command could be
5749 // used.
5750 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5751#endif
5752#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5753 || STRICMP(feature, "iconv") == 0
5754#endif
5755#ifdef DYNAMIC_LUA
5756 || STRICMP(feature, "lua") == 0
5757#endif
5758#ifdef FEAT_MOUSE_GPM
5759 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5760#endif
5761#ifdef DYNAMIC_MZSCHEME
5762 || STRICMP(feature, "mzscheme") == 0
5763#endif
5764#ifdef FEAT_NETBEANS_INTG
5765 || STRICMP(feature, "netbeans_enabled") == 0
5766#endif
5767#ifdef DYNAMIC_PERL
5768 || STRICMP(feature, "perl") == 0
5769#endif
5770#ifdef DYNAMIC_PYTHON
5771 || STRICMP(feature, "python") == 0
5772#endif
5773#ifdef DYNAMIC_PYTHON3
5774 || STRICMP(feature, "python3") == 0
5775#endif
5776#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5777 || STRICMP(feature, "pythonx") == 0
5778#endif
5779#ifdef DYNAMIC_RUBY
5780 || STRICMP(feature, "ruby") == 0
5781#endif
5782#ifdef FEAT_SYN_HL
5783 || STRICMP(feature, "syntax_items") == 0
5784#endif
5785#ifdef DYNAMIC_TCL
5786 || STRICMP(feature, "tcl") == 0
5787#endif
5788 // once "starting" is zero it will stay that way
5789 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5790 || STRICMP(feature, "multi_byte_encoding") == 0
5791#if defined(FEAT_TERMINAL) && defined(MSWIN)
5792 || STRICMP(feature, "conpty") == 0
5793#endif
5794 );
5795}
5796
5797/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005798 * "haslocaldir()" function
5799 */
5800 static void
5801f_haslocaldir(typval_T *argvars, typval_T *rettv)
5802{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005803 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005804 win_T *wp = NULL;
5805
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005806 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5807
5808 // Check for window-local and tab-local directories
5809 if (wp != NULL && wp->w_localdir != NULL)
5810 rettv->vval.v_number = 1;
5811 else if (tp != NULL && tp->tp_localdir != NULL)
5812 rettv->vval.v_number = 2;
5813 else
5814 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005815}
5816
5817/*
5818 * "hasmapto()" function
5819 */
5820 static void
5821f_hasmapto(typval_T *argvars, typval_T *rettv)
5822{
5823 char_u *name;
5824 char_u *mode;
5825 char_u buf[NUMBUFLEN];
5826 int abbr = FALSE;
5827
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005828 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005829 if (argvars[1].v_type == VAR_UNKNOWN)
5830 mode = (char_u *)"nvo";
5831 else
5832 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005833 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005834 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005835 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005836 }
5837
5838 if (map_to_exists(name, mode, abbr))
5839 rettv->vval.v_number = TRUE;
5840 else
5841 rettv->vval.v_number = FALSE;
5842}
5843
5844/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005845 * "highlightID(name)" function
5846 */
5847 static void
5848f_hlID(typval_T *argvars, typval_T *rettv)
5849{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005850 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005851}
5852
5853/*
5854 * "highlight_exists()" function
5855 */
5856 static void
5857f_hlexists(typval_T *argvars, typval_T *rettv)
5858{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005859 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005860}
5861
5862/*
5863 * "hostname()" function
5864 */
5865 static void
5866f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5867{
5868 char_u hostname[256];
5869
5870 mch_get_host_name(hostname, 256);
5871 rettv->v_type = VAR_STRING;
5872 rettv->vval.v_string = vim_strsave(hostname);
5873}
5874
5875/*
5876 * iconv() function
5877 */
5878 static void
5879f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5880{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005881 char_u buf1[NUMBUFLEN];
5882 char_u buf2[NUMBUFLEN];
5883 char_u *from, *to, *str;
5884 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005885
5886 rettv->v_type = VAR_STRING;
5887 rettv->vval.v_string = NULL;
5888
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005889 str = tv_get_string(&argvars[0]);
5890 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5891 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005892 vimconv.vc_type = CONV_NONE;
5893 convert_setup(&vimconv, from, to);
5894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005895 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005896 if (vimconv.vc_type == CONV_NONE)
5897 rettv->vval.v_string = vim_strsave(str);
5898 else
5899 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5900
5901 convert_setup(&vimconv, NULL, NULL);
5902 vim_free(from);
5903 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005904}
5905
5906/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005907 * "index()" function
5908 */
5909 static void
5910f_index(typval_T *argvars, typval_T *rettv)
5911{
5912 list_T *l;
5913 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005914 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005915 long idx = 0;
5916 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005917 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005918
5919 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005920 if (argvars[0].v_type == VAR_BLOB)
5921 {
5922 typval_T tv;
5923 int start = 0;
5924
5925 if (argvars[2].v_type != VAR_UNKNOWN)
5926 {
5927 start = tv_get_number_chk(&argvars[2], &error);
5928 if (error)
5929 return;
5930 }
5931 b = argvars[0].vval.v_blob;
5932 if (b == NULL)
5933 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005934 if (start < 0)
5935 {
5936 start = blob_len(b) + start;
5937 if (start < 0)
5938 start = 0;
5939 }
5940
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005941 for (idx = start; idx < blob_len(b); ++idx)
5942 {
5943 tv.v_type = VAR_NUMBER;
5944 tv.vval.v_number = blob_get(b, idx);
5945 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5946 {
5947 rettv->vval.v_number = idx;
5948 return;
5949 }
5950 }
5951 return;
5952 }
5953 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005954 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005955 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005956 return;
5957 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005958
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005959 l = argvars[0].vval.v_list;
5960 if (l != NULL)
5961 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005962 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005963 item = l->lv_first;
5964 if (argvars[2].v_type != VAR_UNKNOWN)
5965 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005966 // Start at specified item. Use the cached index that list_find()
5967 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005968 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005969 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005970 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005971 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005972 if (error)
5973 item = NULL;
5974 }
5975
5976 for ( ; item != NULL; item = item->li_next, ++idx)
5977 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5978 {
5979 rettv->vval.v_number = idx;
5980 break;
5981 }
5982 }
5983}
5984
5985static int inputsecret_flag = 0;
5986
5987/*
5988 * "input()" function
5989 * Also handles inputsecret() when inputsecret is set.
5990 */
5991 static void
5992f_input(typval_T *argvars, typval_T *rettv)
5993{
5994 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5995}
5996
5997/*
5998 * "inputdialog()" function
5999 */
6000 static void
6001f_inputdialog(typval_T *argvars, typval_T *rettv)
6002{
6003#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006004 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006005 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6006 {
6007 char_u *message;
6008 char_u buf[NUMBUFLEN];
6009 char_u *defstr = (char_u *)"";
6010
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006011 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006012 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006013 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006014 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6015 else
6016 IObuff[0] = NUL;
6017 if (message != NULL && defstr != NULL
6018 && do_dialog(VIM_QUESTION, NULL, message,
6019 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6020 rettv->vval.v_string = vim_strsave(IObuff);
6021 else
6022 {
6023 if (message != NULL && defstr != NULL
6024 && argvars[1].v_type != VAR_UNKNOWN
6025 && argvars[2].v_type != VAR_UNKNOWN)
6026 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006027 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006028 else
6029 rettv->vval.v_string = NULL;
6030 }
6031 rettv->v_type = VAR_STRING;
6032 }
6033 else
6034#endif
6035 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6036}
6037
6038/*
6039 * "inputlist()" function
6040 */
6041 static void
6042f_inputlist(typval_T *argvars, typval_T *rettv)
6043{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006044 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006045 listitem_T *li;
6046 int selected;
6047 int mouse_used;
6048
6049#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006050 // While starting up, there is no place to enter text. When running tests
6051 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006052 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006053 return;
6054#endif
6055 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6056 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006057 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006058 return;
6059 }
6060
6061 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006062 msg_row = Rows - 1; // for when 'cmdheight' > 1
6063 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006064 msg_scroll = TRUE;
6065 msg_clr_eos();
6066
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006067 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006068 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006069 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006070 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006071 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006072 msg_putchar('\n');
6073 }
6074
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006075 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006076 selected = prompt_for_number(&mouse_used);
6077 if (mouse_used)
6078 selected -= lines_left;
6079
6080 rettv->vval.v_number = selected;
6081}
6082
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006083static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6084
6085/*
6086 * "inputrestore()" function
6087 */
6088 static void
6089f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6090{
6091 if (ga_userinput.ga_len > 0)
6092 {
6093 --ga_userinput.ga_len;
6094 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6095 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006096 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006097 }
6098 else if (p_verbose > 1)
6099 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006100 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006101 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006102 }
6103}
6104
6105/*
6106 * "inputsave()" function
6107 */
6108 static void
6109f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6110{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006111 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006112 if (ga_grow(&ga_userinput, 1) == OK)
6113 {
6114 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6115 + ga_userinput.ga_len);
6116 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006117 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006118 }
6119 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006120 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006121}
6122
6123/*
6124 * "inputsecret()" function
6125 */
6126 static void
6127f_inputsecret(typval_T *argvars, typval_T *rettv)
6128{
6129 ++cmdline_star;
6130 ++inputsecret_flag;
6131 f_input(argvars, rettv);
6132 --cmdline_star;
6133 --inputsecret_flag;
6134}
6135
6136/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01006137 * "interrupt()" function
6138 */
6139 static void
6140f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6141{
6142 got_int = TRUE;
6143}
6144
6145/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006146 * "invert(expr)" function
6147 */
6148 static void
6149f_invert(typval_T *argvars, typval_T *rettv)
6150{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006151 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006152}
6153
6154/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006155 * "islocked()" function
6156 */
6157 static void
6158f_islocked(typval_T *argvars, typval_T *rettv)
6159{
6160 lval_T lv;
6161 char_u *end;
6162 dictitem_T *di;
6163
6164 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006165 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006166 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006167 if (end != NULL && lv.ll_name != NULL)
6168 {
6169 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02006170 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006171 else
6172 {
6173 if (lv.ll_tv == NULL)
6174 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006175 di = find_var(lv.ll_name, NULL, TRUE);
6176 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006177 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006178 // Consider a variable locked when:
6179 // 1. the variable itself is locked
6180 // 2. the value of the variable is locked.
6181 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01006182 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6183 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006184 }
6185 }
6186 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006187 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006188 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006189 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006190 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006191 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006192 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6193 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006194 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006195 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6196 }
6197 }
6198
6199 clear_lval(&lv);
6200}
6201
6202#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6203/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006204 * "isinf()" function
6205 */
6206 static void
6207f_isinf(typval_T *argvars, typval_T *rettv)
6208{
6209 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6210 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6211}
6212
6213/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006214 * "isnan()" function
6215 */
6216 static void
6217f_isnan(typval_T *argvars, typval_T *rettv)
6218{
6219 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6220 && isnan(argvars[0].vval.v_float);
6221}
6222#endif
6223
6224/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006225 * "last_buffer_nr()" function.
6226 */
6227 static void
6228f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6229{
6230 int n = 0;
6231 buf_T *buf;
6232
Bram Moolenaar29323592016-07-24 22:04:11 +02006233 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006234 if (n < buf->b_fnum)
6235 n = buf->b_fnum;
6236
6237 rettv->vval.v_number = n;
6238}
6239
6240/*
6241 * "len()" function
6242 */
6243 static void
6244f_len(typval_T *argvars, typval_T *rettv)
6245{
6246 switch (argvars[0].v_type)
6247 {
6248 case VAR_STRING:
6249 case VAR_NUMBER:
6250 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006251 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006252 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006253 case VAR_BLOB:
6254 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6255 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006256 case VAR_LIST:
6257 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6258 break;
6259 case VAR_DICT:
6260 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6261 break;
6262 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006263 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006264 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006265 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006266 case VAR_SPECIAL:
6267 case VAR_FLOAT:
6268 case VAR_FUNC:
6269 case VAR_PARTIAL:
6270 case VAR_JOB:
6271 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006272 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006273 break;
6274 }
6275}
6276
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006277 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006278libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006279{
6280#ifdef FEAT_LIBCALL
6281 char_u *string_in;
6282 char_u **string_result;
6283 int nr_result;
6284#endif
6285
6286 rettv->v_type = type;
6287 if (type != VAR_NUMBER)
6288 rettv->vval.v_string = NULL;
6289
6290 if (check_restricted() || check_secure())
6291 return;
6292
6293#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006294 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006295 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6296 {
6297 string_in = NULL;
6298 if (argvars[2].v_type == VAR_STRING)
6299 string_in = argvars[2].vval.v_string;
6300 if (type == VAR_NUMBER)
6301 string_result = NULL;
6302 else
6303 string_result = &rettv->vval.v_string;
6304 if (mch_libcall(argvars[0].vval.v_string,
6305 argvars[1].vval.v_string,
6306 string_in,
6307 argvars[2].vval.v_number,
6308 string_result,
6309 &nr_result) == OK
6310 && type == VAR_NUMBER)
6311 rettv->vval.v_number = nr_result;
6312 }
6313#endif
6314}
6315
6316/*
6317 * "libcall()" function
6318 */
6319 static void
6320f_libcall(typval_T *argvars, typval_T *rettv)
6321{
6322 libcall_common(argvars, rettv, VAR_STRING);
6323}
6324
6325/*
6326 * "libcallnr()" function
6327 */
6328 static void
6329f_libcallnr(typval_T *argvars, typval_T *rettv)
6330{
6331 libcall_common(argvars, rettv, VAR_NUMBER);
6332}
6333
6334/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006335 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006336 */
6337 static void
6338f_line(typval_T *argvars, typval_T *rettv)
6339{
6340 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006341 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006342 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006343 int id;
6344 tabpage_T *tp;
6345 win_T *wp;
6346 win_T *save_curwin;
6347 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006348
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006349 if (argvars[1].v_type != VAR_UNKNOWN)
6350 {
6351 // use window specified in the second argument
6352 id = (int)tv_get_number(&argvars[1]);
6353 wp = win_id2wp_tp(id, &tp);
6354 if (wp != NULL && tp != NULL)
6355 {
6356 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6357 == OK)
6358 {
6359 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006360 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006361 }
6362 restore_win_noblock(save_curwin, save_curtab, TRUE);
6363 }
6364 }
6365 else
6366 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006367 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006368
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006369 if (fp != NULL)
6370 lnum = fp->lnum;
6371 rettv->vval.v_number = lnum;
6372}
6373
6374/*
6375 * "line2byte(lnum)" function
6376 */
6377 static void
6378f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6379{
6380#ifndef FEAT_BYTEOFF
6381 rettv->vval.v_number = -1;
6382#else
6383 linenr_T lnum;
6384
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006385 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006386 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6387 rettv->vval.v_number = -1;
6388 else
6389 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6390 if (rettv->vval.v_number >= 0)
6391 ++rettv->vval.v_number;
6392#endif
6393}
6394
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006395#ifdef FEAT_FLOAT
6396/*
6397 * "log()" function
6398 */
6399 static void
6400f_log(typval_T *argvars, typval_T *rettv)
6401{
6402 float_T f = 0.0;
6403
6404 rettv->v_type = VAR_FLOAT;
6405 if (get_float_arg(argvars, &f) == OK)
6406 rettv->vval.v_float = log(f);
6407 else
6408 rettv->vval.v_float = 0.0;
6409}
6410
6411/*
6412 * "log10()" function
6413 */
6414 static void
6415f_log10(typval_T *argvars, typval_T *rettv)
6416{
6417 float_T f = 0.0;
6418
6419 rettv->v_type = VAR_FLOAT;
6420 if (get_float_arg(argvars, &f) == OK)
6421 rettv->vval.v_float = log10(f);
6422 else
6423 rettv->vval.v_float = 0.0;
6424}
6425#endif
6426
6427#ifdef FEAT_LUA
6428/*
6429 * "luaeval()" function
6430 */
6431 static void
6432f_luaeval(typval_T *argvars, typval_T *rettv)
6433{
6434 char_u *str;
6435 char_u buf[NUMBUFLEN];
6436
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006437 if (check_restricted() || check_secure())
6438 return;
6439
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006440 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006441 do_luaeval(str, argvars + 1, rettv);
6442}
6443#endif
6444
6445/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006446 * "maparg()" function
6447 */
6448 static void
6449f_maparg(typval_T *argvars, typval_T *rettv)
6450{
6451 get_maparg(argvars, rettv, TRUE);
6452}
6453
6454/*
6455 * "mapcheck()" function
6456 */
6457 static void
6458f_mapcheck(typval_T *argvars, typval_T *rettv)
6459{
6460 get_maparg(argvars, rettv, FALSE);
6461}
6462
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006463typedef enum
6464{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006465 MATCH_END, // matchend()
6466 MATCH_MATCH, // match()
6467 MATCH_STR, // matchstr()
6468 MATCH_LIST, // matchlist()
6469 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006470} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006471
6472 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006473find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006474{
6475 char_u *str = NULL;
6476 long len = 0;
6477 char_u *expr = NULL;
6478 char_u *pat;
6479 regmatch_T regmatch;
6480 char_u patbuf[NUMBUFLEN];
6481 char_u strbuf[NUMBUFLEN];
6482 char_u *save_cpo;
6483 long start = 0;
6484 long nth = 1;
6485 colnr_T startcol = 0;
6486 int match = 0;
6487 list_T *l = NULL;
6488 listitem_T *li = NULL;
6489 long idx = 0;
6490 char_u *tofree = NULL;
6491
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006492 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006493 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006494 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006495
6496 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006497 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006498 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006499 // type MATCH_LIST: return empty list when there are no matches.
6500 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006501 if (rettv_list_alloc(rettv) == FAIL)
6502 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006503 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006504 && (list_append_string(rettv->vval.v_list,
6505 (char_u *)"", 0) == FAIL
6506 || list_append_number(rettv->vval.v_list,
6507 (varnumber_T)-1) == FAIL
6508 || list_append_number(rettv->vval.v_list,
6509 (varnumber_T)-1) == FAIL
6510 || list_append_number(rettv->vval.v_list,
6511 (varnumber_T)-1) == FAIL))
6512 {
6513 list_free(rettv->vval.v_list);
6514 rettv->vval.v_list = NULL;
6515 goto theend;
6516 }
6517 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006518 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006519 {
6520 rettv->v_type = VAR_STRING;
6521 rettv->vval.v_string = NULL;
6522 }
6523
6524 if (argvars[0].v_type == VAR_LIST)
6525 {
6526 if ((l = argvars[0].vval.v_list) == NULL)
6527 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006528 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006529 li = l->lv_first;
6530 }
6531 else
6532 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006533 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006534 len = (long)STRLEN(str);
6535 }
6536
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006537 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006538 if (pat == NULL)
6539 goto theend;
6540
6541 if (argvars[2].v_type != VAR_UNKNOWN)
6542 {
6543 int error = FALSE;
6544
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006545 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006546 if (error)
6547 goto theend;
6548 if (l != NULL)
6549 {
6550 li = list_find(l, start);
6551 if (li == NULL)
6552 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006553 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006554 }
6555 else
6556 {
6557 if (start < 0)
6558 start = 0;
6559 if (start > len)
6560 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006561 // When "count" argument is there ignore matches before "start",
6562 // otherwise skip part of the string. Differs when pattern is "^"
6563 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006564 if (argvars[3].v_type != VAR_UNKNOWN)
6565 startcol = start;
6566 else
6567 {
6568 str += start;
6569 len -= start;
6570 }
6571 }
6572
6573 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006574 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006575 if (error)
6576 goto theend;
6577 }
6578
6579 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6580 if (regmatch.regprog != NULL)
6581 {
6582 regmatch.rm_ic = p_ic;
6583
6584 for (;;)
6585 {
6586 if (l != NULL)
6587 {
6588 if (li == NULL)
6589 {
6590 match = FALSE;
6591 break;
6592 }
6593 vim_free(tofree);
6594 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6595 if (str == NULL)
6596 break;
6597 }
6598
6599 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6600
6601 if (match && --nth <= 0)
6602 break;
6603 if (l == NULL && !match)
6604 break;
6605
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006606 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006607 if (l != NULL)
6608 {
6609 li = li->li_next;
6610 ++idx;
6611 }
6612 else
6613 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006614 startcol = (colnr_T)(regmatch.startp[0]
6615 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006616 if (startcol > (colnr_T)len
6617 || str + startcol <= regmatch.startp[0])
6618 {
6619 match = FALSE;
6620 break;
6621 }
6622 }
6623 }
6624
6625 if (match)
6626 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006627 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006628 {
6629 listitem_T *li1 = rettv->vval.v_list->lv_first;
6630 listitem_T *li2 = li1->li_next;
6631 listitem_T *li3 = li2->li_next;
6632 listitem_T *li4 = li3->li_next;
6633
6634 vim_free(li1->li_tv.vval.v_string);
6635 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006636 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006637 li3->li_tv.vval.v_number =
6638 (varnumber_T)(regmatch.startp[0] - expr);
6639 li4->li_tv.vval.v_number =
6640 (varnumber_T)(regmatch.endp[0] - expr);
6641 if (l != NULL)
6642 li2->li_tv.vval.v_number = (varnumber_T)idx;
6643 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006644 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006645 {
6646 int i;
6647
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006648 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006649 for (i = 0; i < NSUBEXP; ++i)
6650 {
6651 if (regmatch.endp[i] == NULL)
6652 {
6653 if (list_append_string(rettv->vval.v_list,
6654 (char_u *)"", 0) == FAIL)
6655 break;
6656 }
6657 else if (list_append_string(rettv->vval.v_list,
6658 regmatch.startp[i],
6659 (int)(regmatch.endp[i] - regmatch.startp[i]))
6660 == FAIL)
6661 break;
6662 }
6663 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006664 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006665 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006666 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006667 if (l != NULL)
6668 copy_tv(&li->li_tv, rettv);
6669 else
6670 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006671 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006672 }
6673 else if (l != NULL)
6674 rettv->vval.v_number = idx;
6675 else
6676 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006677 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006678 rettv->vval.v_number =
6679 (varnumber_T)(regmatch.startp[0] - str);
6680 else
6681 rettv->vval.v_number =
6682 (varnumber_T)(regmatch.endp[0] - str);
6683 rettv->vval.v_number += (varnumber_T)(str - expr);
6684 }
6685 }
6686 vim_regfree(regmatch.regprog);
6687 }
6688
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006689theend:
6690 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006691 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006692 listitem_remove(rettv->vval.v_list,
6693 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694 vim_free(tofree);
6695 p_cpo = save_cpo;
6696}
6697
6698/*
6699 * "match()" function
6700 */
6701 static void
6702f_match(typval_T *argvars, typval_T *rettv)
6703{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006704 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006705}
6706
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006707/*
6708 * "matchend()" function
6709 */
6710 static void
6711f_matchend(typval_T *argvars, typval_T *rettv)
6712{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006713 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006714}
6715
6716/*
6717 * "matchlist()" function
6718 */
6719 static void
6720f_matchlist(typval_T *argvars, typval_T *rettv)
6721{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006722 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006723}
6724
6725/*
6726 * "matchstr()" function
6727 */
6728 static void
6729f_matchstr(typval_T *argvars, typval_T *rettv)
6730{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006731 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006732}
6733
6734/*
6735 * "matchstrpos()" function
6736 */
6737 static void
6738f_matchstrpos(typval_T *argvars, typval_T *rettv)
6739{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006740 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006741}
6742
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006743 static void
6744max_min(typval_T *argvars, typval_T *rettv, int domax)
6745{
6746 varnumber_T n = 0;
6747 varnumber_T i;
6748 int error = FALSE;
6749
6750 if (argvars[0].v_type == VAR_LIST)
6751 {
6752 list_T *l;
6753 listitem_T *li;
6754
6755 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006756 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006757 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006758 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006759 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006760 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6761 n = l->lv_u.nonmat.lv_start;
6762 else
6763 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6764 * l->lv_u.nonmat.lv_stride;
6765 }
6766 else
6767 {
6768 li = l->lv_first;
6769 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006770 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006771 n = tv_get_number_chk(&li->li_tv, &error);
6772 for (;;)
6773 {
6774 li = li->li_next;
6775 if (li == NULL)
6776 break;
6777 i = tv_get_number_chk(&li->li_tv, &error);
6778 if (domax ? i > n : i < n)
6779 n = i;
6780 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006781 }
6782 }
6783 }
6784 }
6785 else if (argvars[0].v_type == VAR_DICT)
6786 {
6787 dict_T *d;
6788 int first = TRUE;
6789 hashitem_T *hi;
6790 int todo;
6791
6792 d = argvars[0].vval.v_dict;
6793 if (d != NULL)
6794 {
6795 todo = (int)d->dv_hashtab.ht_used;
6796 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6797 {
6798 if (!HASHITEM_EMPTY(hi))
6799 {
6800 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006801 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006802 if (first)
6803 {
6804 n = i;
6805 first = FALSE;
6806 }
6807 else if (domax ? i > n : i < n)
6808 n = i;
6809 }
6810 }
6811 }
6812 }
6813 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006814 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006815 rettv->vval.v_number = error ? 0 : n;
6816}
6817
6818/*
6819 * "max()" function
6820 */
6821 static void
6822f_max(typval_T *argvars, typval_T *rettv)
6823{
6824 max_min(argvars, rettv, TRUE);
6825}
6826
6827/*
6828 * "min()" function
6829 */
6830 static void
6831f_min(typval_T *argvars, typval_T *rettv)
6832{
6833 max_min(argvars, rettv, FALSE);
6834}
6835
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006836#if defined(FEAT_MZSCHEME) || defined(PROTO)
6837/*
6838 * "mzeval()" function
6839 */
6840 static void
6841f_mzeval(typval_T *argvars, typval_T *rettv)
6842{
6843 char_u *str;
6844 char_u buf[NUMBUFLEN];
6845
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006846 if (check_restricted() || check_secure())
6847 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006848 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006849 do_mzeval(str, rettv);
6850}
6851
6852 void
6853mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6854{
6855 typval_T argvars[3];
6856
6857 argvars[0].v_type = VAR_STRING;
6858 argvars[0].vval.v_string = name;
6859 copy_tv(args, &argvars[1]);
6860 argvars[2].v_type = VAR_UNKNOWN;
6861 f_call(argvars, rettv);
6862 clear_tv(&argvars[1]);
6863}
6864#endif
6865
6866/*
6867 * "nextnonblank()" function
6868 */
6869 static void
6870f_nextnonblank(typval_T *argvars, typval_T *rettv)
6871{
6872 linenr_T lnum;
6873
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006874 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006875 {
6876 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6877 {
6878 lnum = 0;
6879 break;
6880 }
6881 if (*skipwhite(ml_get(lnum)) != NUL)
6882 break;
6883 }
6884 rettv->vval.v_number = lnum;
6885}
6886
6887/*
6888 * "nr2char()" function
6889 */
6890 static void
6891f_nr2char(typval_T *argvars, typval_T *rettv)
6892{
6893 char_u buf[NUMBUFLEN];
6894
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006895 if (has_mbyte)
6896 {
6897 int utf8 = 0;
6898
6899 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006900 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006902 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006903 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006904 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006905 }
6906 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006907 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006908 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006909 buf[1] = NUL;
6910 }
6911 rettv->v_type = VAR_STRING;
6912 rettv->vval.v_string = vim_strsave(buf);
6913}
6914
6915/*
6916 * "or(expr, expr)" function
6917 */
6918 static void
6919f_or(typval_T *argvars, typval_T *rettv)
6920{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006921 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6922 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923}
6924
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006925#ifdef FEAT_PERL
6926/*
6927 * "perleval()" function
6928 */
6929 static void
6930f_perleval(typval_T *argvars, typval_T *rettv)
6931{
6932 char_u *str;
6933 char_u buf[NUMBUFLEN];
6934
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006935 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006936 do_perleval(str, rettv);
6937}
6938#endif
6939
6940#ifdef FEAT_FLOAT
6941/*
6942 * "pow()" function
6943 */
6944 static void
6945f_pow(typval_T *argvars, typval_T *rettv)
6946{
6947 float_T fx = 0.0, fy = 0.0;
6948
6949 rettv->v_type = VAR_FLOAT;
6950 if (get_float_arg(argvars, &fx) == OK
6951 && get_float_arg(&argvars[1], &fy) == OK)
6952 rettv->vval.v_float = pow(fx, fy);
6953 else
6954 rettv->vval.v_float = 0.0;
6955}
6956#endif
6957
6958/*
6959 * "prevnonblank()" function
6960 */
6961 static void
6962f_prevnonblank(typval_T *argvars, typval_T *rettv)
6963{
6964 linenr_T lnum;
6965
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006966 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006967 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6968 lnum = 0;
6969 else
6970 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6971 --lnum;
6972 rettv->vval.v_number = lnum;
6973}
6974
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006975// This dummy va_list is here because:
6976// - passing a NULL pointer doesn't work when va_list isn't a pointer
6977// - locally in the function results in a "used before set" warning
6978// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006979static va_list ap;
6980
6981/*
6982 * "printf()" function
6983 */
6984 static void
6985f_printf(typval_T *argvars, typval_T *rettv)
6986{
6987 char_u buf[NUMBUFLEN];
6988 int len;
6989 char_u *s;
6990 int saved_did_emsg = did_emsg;
6991 char *fmt;
6992
6993 rettv->v_type = VAR_STRING;
6994 rettv->vval.v_string = NULL;
6995
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006996 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006997 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006998 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006999 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007000 if (!did_emsg)
7001 {
7002 s = alloc(len + 1);
7003 if (s != NULL)
7004 {
7005 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007006 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
7007 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007008 }
7009 }
7010 did_emsg |= saved_did_emsg;
7011}
7012
7013/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007014 * "pum_getpos()" function
7015 */
7016 static void
7017f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7018{
7019 if (rettv_dict_alloc(rettv) != OK)
7020 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007021 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007022}
7023
7024/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007025 * "pumvisible()" function
7026 */
7027 static void
7028f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7029{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007030 if (pum_visible())
7031 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007032}
7033
7034#ifdef FEAT_PYTHON3
7035/*
7036 * "py3eval()" function
7037 */
7038 static void
7039f_py3eval(typval_T *argvars, typval_T *rettv)
7040{
7041 char_u *str;
7042 char_u buf[NUMBUFLEN];
7043
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007044 if (check_restricted() || check_secure())
7045 return;
7046
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007047 if (p_pyx == 0)
7048 p_pyx = 3;
7049
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007050 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007051 do_py3eval(str, rettv);
7052}
7053#endif
7054
7055#ifdef FEAT_PYTHON
7056/*
7057 * "pyeval()" function
7058 */
7059 static void
7060f_pyeval(typval_T *argvars, typval_T *rettv)
7061{
7062 char_u *str;
7063 char_u buf[NUMBUFLEN];
7064
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007065 if (check_restricted() || check_secure())
7066 return;
7067
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007068 if (p_pyx == 0)
7069 p_pyx = 2;
7070
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007071 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007072 do_pyeval(str, rettv);
7073}
7074#endif
7075
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007076#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
7077/*
7078 * "pyxeval()" function
7079 */
7080 static void
7081f_pyxeval(typval_T *argvars, typval_T *rettv)
7082{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007083 if (check_restricted() || check_secure())
7084 return;
7085
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007086# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
7087 init_pyxversion();
7088 if (p_pyx == 2)
7089 f_pyeval(argvars, rettv);
7090 else
7091 f_py3eval(argvars, rettv);
7092# elif defined(FEAT_PYTHON)
7093 f_pyeval(argvars, rettv);
7094# elif defined(FEAT_PYTHON3)
7095 f_py3eval(argvars, rettv);
7096# endif
7097}
7098#endif
7099
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007100static UINT32_T srand_seed_for_testing = 0;
7101static int srand_seed_for_testing_is_used = FALSE;
7102
7103 static void
7104f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
7105{
7106 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007107 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007108 else
7109 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007110 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
7111 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007112 }
7113}
7114
7115 static void
7116init_srand(UINT32_T *x)
7117{
7118#ifndef MSWIN
7119 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
7120#endif
7121
7122 if (srand_seed_for_testing_is_used)
7123 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007124 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007125 return;
7126 }
7127#ifndef MSWIN
7128 if (dev_urandom_state != FAIL)
7129 {
7130 int fd = open("/dev/urandom", O_RDONLY);
7131 struct {
7132 union {
7133 UINT32_T number;
7134 char bytes[sizeof(UINT32_T)];
7135 } contents;
7136 } buf;
7137
7138 // Attempt reading /dev/urandom.
7139 if (fd == -1)
7140 dev_urandom_state = FAIL;
7141 else
7142 {
7143 buf.contents.number = 0;
7144 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
7145 != sizeof(UINT32_T))
7146 dev_urandom_state = FAIL;
7147 else
7148 {
7149 dev_urandom_state = OK;
7150 *x = buf.contents.number;
7151 }
7152 close(fd);
7153 }
7154 }
7155 if (dev_urandom_state != OK)
7156 // Reading /dev/urandom doesn't work, fall back to time().
7157#endif
7158 *x = vim_time();
7159}
7160
7161#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
7162#define SPLITMIX32(x, z) ( \
7163 z = (x += 0x9e3779b9), \
7164 z = (z ^ (z >> 16)) * 0x85ebca6b, \
7165 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
7166 z ^ (z >> 16) \
7167 )
7168#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
7169 result = ROTL(y * 5, 7) * 9; \
7170 t = y << 9; \
7171 z ^= x; \
7172 w ^= y; \
7173 y ^= z, x ^= w; \
7174 z ^= t; \
7175 w = ROTL(w, 11);
7176
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007177/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007178 * "rand()" function
7179 */
7180 static void
7181f_rand(typval_T *argvars, typval_T *rettv)
7182{
7183 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007184 static UINT32_T gx, gy, gz, gw;
7185 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007186 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01007187 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007188
7189 if (argvars[0].v_type == VAR_UNKNOWN)
7190 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007191 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007192 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007193 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007194 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007195 init_srand(&x);
7196
7197 gx = SPLITMIX32(x, z);
7198 gy = SPLITMIX32(x, z);
7199 gz = SPLITMIX32(x, z);
7200 gw = SPLITMIX32(x, z);
7201 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007202 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007203
7204 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007205 }
7206 else if (argvars[0].v_type == VAR_LIST)
7207 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007208 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007209 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007210 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007211
7212 lx = list_find(l, 0L);
7213 ly = list_find(l, 1L);
7214 lz = list_find(l, 2L);
7215 lw = list_find(l, 3L);
7216 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
7217 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
7218 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
7219 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
7220 x = (UINT32_T)lx->li_tv.vval.v_number;
7221 y = (UINT32_T)ly->li_tv.vval.v_number;
7222 z = (UINT32_T)lz->li_tv.vval.v_number;
7223 w = (UINT32_T)lw->li_tv.vval.v_number;
7224
7225 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
7226
7227 lx->li_tv.vval.v_number = (varnumber_T)x;
7228 ly->li_tv.vval.v_number = (varnumber_T)y;
7229 lz->li_tv.vval.v_number = (varnumber_T)z;
7230 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007231 }
7232 else
7233 goto theend;
7234
7235 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007236 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007237 return;
7238
7239theend:
7240 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007241 rettv->v_type = VAR_NUMBER;
7242 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007243}
7244
7245/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007246 * "srand()" function
7247 */
7248 static void
7249f_srand(typval_T *argvars, typval_T *rettv)
7250{
7251 UINT32_T x = 0, z;
7252
7253 if (rettv_list_alloc(rettv) == FAIL)
7254 return;
7255 if (argvars[0].v_type == VAR_UNKNOWN)
7256 {
7257 init_srand(&x);
7258 }
7259 else
7260 {
7261 int error = FALSE;
7262
7263 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7264 if (error)
7265 return;
7266 }
7267
7268 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7269 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7270 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7271 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7272}
7273
7274#undef ROTL
7275#undef SPLITMIX32
7276#undef SHUFFLE_XOSHIRO128STARSTAR
7277
7278/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007279 * "range()" function
7280 */
7281 static void
7282f_range(typval_T *argvars, typval_T *rettv)
7283{
7284 varnumber_T start;
7285 varnumber_T end;
7286 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007287 int error = FALSE;
7288
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007289 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007290 if (argvars[1].v_type == VAR_UNKNOWN)
7291 {
7292 end = start - 1;
7293 start = 0;
7294 }
7295 else
7296 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007297 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007298 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007299 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007300 }
7301
7302 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007303 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007304 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007305 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007306 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007307 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007308 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007309 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310 list_T *list = rettv->vval.v_list;
7311
7312 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007313 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314 // be called.
7315 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007316 list->lv_u.nonmat.lv_start = start;
7317 list->lv_u.nonmat.lv_end = end;
7318 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007319 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007320 }
7321}
7322
7323/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007324 * Materialize "list".
7325 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007326 */
7327 void
7328range_list_materialize(list_T *list)
7329{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007330 varnumber_T start = list->lv_u.nonmat.lv_start;
7331 varnumber_T end = list->lv_u.nonmat.lv_end;
7332 int stride = list->lv_u.nonmat.lv_stride;
7333 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007334
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007335 list->lv_first = NULL;
7336 list->lv_u.mat.lv_last = NULL;
7337 list->lv_len = 0;
7338 list->lv_u.mat.lv_idx_item = NULL;
7339 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7340 if (list_append_number(list, (varnumber_T)i) == FAIL)
7341 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007342}
7343
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007344/*
7345 * "getreginfo()" function
7346 */
7347 static void
7348f_getreginfo(typval_T *argvars, typval_T *rettv)
7349{
7350 char_u *strregname;
7351 int regname;
7352 char_u buf[NUMBUFLEN + 2];
7353 long reglen = 0;
7354 dict_T *dict;
7355 list_T *list;
7356
7357 if (argvars[0].v_type != VAR_UNKNOWN)
7358 {
7359 strregname = tv_get_string_chk(&argvars[0]);
7360 if (strregname == NULL)
7361 return;
7362 }
7363 else
7364 strregname = get_vim_var_str(VV_REG);
7365
7366 regname = (strregname == NULL ? '"' : *strregname);
7367 if (regname == 0 || regname == '@')
7368 regname = '"';
7369
7370 if (rettv_dict_alloc(rettv) == FAIL)
7371 return;
7372 dict = rettv->vval.v_dict;
7373
7374 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7375 if (list == NULL)
7376 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007377 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007378
7379 buf[0] = NUL;
7380 buf[1] = NUL;
7381 switch (get_reg_type(regname, &reglen))
7382 {
7383 case MLINE: buf[0] = 'V'; break;
7384 case MCHAR: buf[0] = 'v'; break;
7385 case MBLOCK:
7386 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7387 reglen + 1);
7388 break;
7389 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007390 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007391
7392 buf[0] = get_register_name(get_unname_register());
7393 buf[1] = NUL;
7394 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007395 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007396 else
7397 {
7398 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7399
7400 if (item != NULL)
7401 {
7402 item->di_tv.v_type = VAR_SPECIAL;
7403 item->di_tv.vval.v_number = regname == buf[0]
7404 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007405 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007406 }
7407 }
7408}
7409
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007410 static void
7411return_register(int regname, typval_T *rettv)
7412{
7413 char_u buf[2] = {0, 0};
7414
7415 buf[0] = (char_u)regname;
7416 rettv->v_type = VAR_STRING;
7417 rettv->vval.v_string = vim_strsave(buf);
7418}
7419
7420/*
7421 * "reg_executing()" function
7422 */
7423 static void
7424f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7425{
7426 return_register(reg_executing, rettv);
7427}
7428
7429/*
7430 * "reg_recording()" function
7431 */
7432 static void
7433f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7434{
7435 return_register(reg_recording, rettv);
7436}
7437
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007438/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007439 * "rename({from}, {to})" function
7440 */
7441 static void
7442f_rename(typval_T *argvars, typval_T *rettv)
7443{
7444 char_u buf[NUMBUFLEN];
7445
7446 if (check_restricted() || check_secure())
7447 rettv->vval.v_number = -1;
7448 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007449 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7450 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007451}
7452
7453/*
7454 * "repeat()" function
7455 */
7456 static void
7457f_repeat(typval_T *argvars, typval_T *rettv)
7458{
7459 char_u *p;
7460 int n;
7461 int slen;
7462 int len;
7463 char_u *r;
7464 int i;
7465
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007466 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007467 if (argvars[0].v_type == VAR_LIST)
7468 {
7469 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7470 while (n-- > 0)
7471 if (list_extend(rettv->vval.v_list,
7472 argvars[0].vval.v_list, NULL) == FAIL)
7473 break;
7474 }
7475 else
7476 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007477 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007478 rettv->v_type = VAR_STRING;
7479 rettv->vval.v_string = NULL;
7480
7481 slen = (int)STRLEN(p);
7482 len = slen * n;
7483 if (len <= 0)
7484 return;
7485
7486 r = alloc(len + 1);
7487 if (r != NULL)
7488 {
7489 for (i = 0; i < n; i++)
7490 mch_memmove(r + i * slen, p, (size_t)slen);
7491 r[len] = NUL;
7492 }
7493
7494 rettv->vval.v_string = r;
7495 }
7496}
7497
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007498#define SP_NOMOVE 0x01 // don't move cursor
7499#define SP_REPEAT 0x02 // repeat to find outer pair
7500#define SP_RETCOUNT 0x04 // return matchcount
7501#define SP_SETPCMARK 0x08 // set previous context mark
7502#define SP_START 0x10 // accept match at start position
7503#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7504#define SP_END 0x40 // leave cursor at end of match
7505#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007506
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007507/*
7508 * Get flags for a search function.
7509 * Possibly sets "p_ws".
7510 * Returns BACKWARD, FORWARD or zero (for an error).
7511 */
7512 static int
7513get_search_arg(typval_T *varp, int *flagsp)
7514{
7515 int dir = FORWARD;
7516 char_u *flags;
7517 char_u nbuf[NUMBUFLEN];
7518 int mask;
7519
7520 if (varp->v_type != VAR_UNKNOWN)
7521 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007522 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007523 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007524 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007525 while (*flags != NUL)
7526 {
7527 switch (*flags)
7528 {
7529 case 'b': dir = BACKWARD; break;
7530 case 'w': p_ws = TRUE; break;
7531 case 'W': p_ws = FALSE; break;
7532 default: mask = 0;
7533 if (flagsp != NULL)
7534 switch (*flags)
7535 {
7536 case 'c': mask = SP_START; break;
7537 case 'e': mask = SP_END; break;
7538 case 'm': mask = SP_RETCOUNT; break;
7539 case 'n': mask = SP_NOMOVE; break;
7540 case 'p': mask = SP_SUBPAT; break;
7541 case 'r': mask = SP_REPEAT; break;
7542 case 's': mask = SP_SETPCMARK; break;
7543 case 'z': mask = SP_COLUMN; break;
7544 }
7545 if (mask == 0)
7546 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007547 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007548 dir = 0;
7549 }
7550 else
7551 *flagsp |= mask;
7552 }
7553 if (dir == 0)
7554 break;
7555 ++flags;
7556 }
7557 }
7558 return dir;
7559}
7560
7561/*
7562 * Shared by search() and searchpos() functions.
7563 */
7564 static int
7565search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7566{
7567 int flags;
7568 char_u *pat;
7569 pos_T pos;
7570 pos_T save_cursor;
7571 int save_p_ws = p_ws;
7572 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007573 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007574 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007575#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007576 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007577 long time_limit = 0;
7578#endif
7579 int options = SEARCH_KEEP;
7580 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007581 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007582 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007583 pos_T firstpos;
7584
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007585 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007586 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007587 if (dir == 0)
7588 goto theend;
7589 flags = *flagsp;
7590 if (flags & SP_START)
7591 options |= SEARCH_START;
7592 if (flags & SP_END)
7593 options |= SEARCH_END;
7594 if (flags & SP_COLUMN)
7595 options |= SEARCH_COL;
7596
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007597 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7599 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007600 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007601 if (lnum_stop < 0)
7602 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603 if (argvars[3].v_type != VAR_UNKNOWN)
7604 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007605#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007606 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007607 if (time_limit < 0)
7608 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007609#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007610 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007611 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007612 }
7613
7614#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007615 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007616 profile_setlimit(time_limit, &tm);
7617#endif
7618
7619 /*
7620 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7621 * Check to make sure only those flags are set.
7622 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7623 * flags cannot be set. Check for that condition also.
7624 */
7625 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7626 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007628 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007629 goto theend;
7630 }
7631
7632 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007633 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007634 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007635 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7636#ifdef FEAT_RELTIME
7637 sia.sa_tm = &tm;
7638#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007639
7640 // Repeat until {skip} returns FALSE.
7641 for (;;)
7642 {
7643 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007644 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007645 // finding the first match again means there is no match where {skip}
7646 // evaluates to zero.
7647 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7648 subpatnum = FAIL;
7649
Bram Moolenaara9c01042020-06-07 14:50:50 +02007650 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007651 // didn't find it or no skip argument
7652 break;
7653 firstpos = pos;
7654
Bram Moolenaara9c01042020-06-07 14:50:50 +02007655 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007656 {
7657 int do_skip;
7658 int err;
7659 pos_T save_pos = curwin->w_cursor;
7660
7661 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007662 err = FALSE;
7663 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007664 curwin->w_cursor = save_pos;
7665 if (err)
7666 {
7667 // Evaluating {skip} caused an error, break here.
7668 subpatnum = FAIL;
7669 break;
7670 }
7671 if (!do_skip)
7672 break;
7673 }
7674 }
7675
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007676 if (subpatnum != FAIL)
7677 {
7678 if (flags & SP_SUBPAT)
7679 retval = subpatnum;
7680 else
7681 retval = pos.lnum;
7682 if (flags & SP_SETPCMARK)
7683 setpcmark();
7684 curwin->w_cursor = pos;
7685 if (match_pos != NULL)
7686 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007687 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007688 match_pos->lnum = pos.lnum;
7689 match_pos->col = pos.col + 1;
7690 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007691 // "/$" will put the cursor after the end of the line, may need to
7692 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007693 check_cursor();
7694 }
7695
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007696 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007697 if (flags & SP_NOMOVE)
7698 curwin->w_cursor = save_cursor;
7699 else
7700 curwin->w_set_curswant = TRUE;
7701theend:
7702 p_ws = save_p_ws;
7703
7704 return retval;
7705}
7706
7707#ifdef FEAT_FLOAT
7708
7709/*
7710 * round() is not in C90, use ceil() or floor() instead.
7711 */
7712 float_T
7713vim_round(float_T f)
7714{
7715 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
7716}
7717
7718/*
7719 * "round({float})" function
7720 */
7721 static void
7722f_round(typval_T *argvars, typval_T *rettv)
7723{
7724 float_T f = 0.0;
7725
7726 rettv->v_type = VAR_FLOAT;
7727 if (get_float_arg(argvars, &f) == OK)
7728 rettv->vval.v_float = vim_round(f);
7729 else
7730 rettv->vval.v_float = 0.0;
7731}
7732#endif
7733
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007734#ifdef FEAT_RUBY
7735/*
7736 * "rubyeval()" function
7737 */
7738 static void
7739f_rubyeval(typval_T *argvars, typval_T *rettv)
7740{
7741 char_u *str;
7742 char_u buf[NUMBUFLEN];
7743
7744 str = tv_get_string_buf(&argvars[0], buf);
7745 do_rubyeval(str, rettv);
7746}
7747#endif
7748
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007749/*
7750 * "screenattr()" function
7751 */
7752 static void
7753f_screenattr(typval_T *argvars, typval_T *rettv)
7754{
7755 int row;
7756 int col;
7757 int c;
7758
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007759 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7760 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007761 if (row < 0 || row >= screen_Rows
7762 || col < 0 || col >= screen_Columns)
7763 c = -1;
7764 else
7765 c = ScreenAttrs[LineOffset[row] + col];
7766 rettv->vval.v_number = c;
7767}
7768
7769/*
7770 * "screenchar()" function
7771 */
7772 static void
7773f_screenchar(typval_T *argvars, typval_T *rettv)
7774{
7775 int row;
7776 int col;
7777 int off;
7778 int c;
7779
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007780 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7781 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007782 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007783 c = -1;
7784 else
7785 {
7786 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007787 if (enc_utf8 && ScreenLinesUC[off] != 0)
7788 c = ScreenLinesUC[off];
7789 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007790 c = ScreenLines[off];
7791 }
7792 rettv->vval.v_number = c;
7793}
7794
7795/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007796 * "screenchars()" function
7797 */
7798 static void
7799f_screenchars(typval_T *argvars, typval_T *rettv)
7800{
7801 int row;
7802 int col;
7803 int off;
7804 int c;
7805 int i;
7806
7807 if (rettv_list_alloc(rettv) == FAIL)
7808 return;
7809 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7810 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7811 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7812 return;
7813
7814 off = LineOffset[row] + col;
7815 if (enc_utf8 && ScreenLinesUC[off] != 0)
7816 c = ScreenLinesUC[off];
7817 else
7818 c = ScreenLines[off];
7819 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7820
7821 if (enc_utf8)
7822
7823 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7824 list_append_number(rettv->vval.v_list,
7825 (varnumber_T)ScreenLinesC[i][off]);
7826}
7827
7828/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007829 * "screencol()" function
7830 *
7831 * First column is 1 to be consistent with virtcol().
7832 */
7833 static void
7834f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7835{
7836 rettv->vval.v_number = screen_screencol() + 1;
7837}
7838
7839/*
7840 * "screenrow()" function
7841 */
7842 static void
7843f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7844{
7845 rettv->vval.v_number = screen_screenrow() + 1;
7846}
7847
7848/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007849 * "screenstring()" function
7850 */
7851 static void
7852f_screenstring(typval_T *argvars, typval_T *rettv)
7853{
7854 int row;
7855 int col;
7856 int off;
7857 int c;
7858 int i;
7859 char_u buf[MB_MAXBYTES + 1];
7860 int buflen = 0;
7861
7862 rettv->vval.v_string = NULL;
7863 rettv->v_type = VAR_STRING;
7864
7865 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7866 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7867 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7868 return;
7869
7870 off = LineOffset[row] + col;
7871 if (enc_utf8 && ScreenLinesUC[off] != 0)
7872 c = ScreenLinesUC[off];
7873 else
7874 c = ScreenLines[off];
7875 buflen += mb_char2bytes(c, buf);
7876
7877 if (enc_utf8)
7878 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7879 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7880
7881 buf[buflen] = NUL;
7882 rettv->vval.v_string = vim_strsave(buf);
7883}
7884
7885/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007886 * "search()" function
7887 */
7888 static void
7889f_search(typval_T *argvars, typval_T *rettv)
7890{
7891 int flags = 0;
7892
7893 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7894}
7895
7896/*
7897 * "searchdecl()" function
7898 */
7899 static void
7900f_searchdecl(typval_T *argvars, typval_T *rettv)
7901{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007902 int locally = TRUE;
7903 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007904 int error = FALSE;
7905 char_u *name;
7906
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007907 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007908
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007909 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007910 if (argvars[1].v_type != VAR_UNKNOWN)
7911 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007912 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007913 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007914 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007915 }
7916 if (!error && name != NULL)
7917 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7918 locally, thisblock, SEARCH_KEEP) == FAIL;
7919}
7920
7921/*
7922 * Used by searchpair() and searchpairpos()
7923 */
7924 static int
7925searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7926{
7927 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007928 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007929 int save_p_ws = p_ws;
7930 int dir;
7931 int flags = 0;
7932 char_u nbuf1[NUMBUFLEN];
7933 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007934 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007935 long lnum_stop = 0;
7936 long time_limit = 0;
7937
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007938 // Get the three pattern arguments: start, middle, end. Will result in an
7939 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007940 spat = tv_get_string_chk(&argvars[0]);
7941 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7942 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007943 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007944 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007945
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007946 // Handle the optional fourth argument: flags
7947 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007948 if (dir == 0)
7949 goto theend;
7950
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007951 // Don't accept SP_END or SP_SUBPAT.
7952 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007953 if ((flags & (SP_END | SP_SUBPAT)) != 0
7954 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7955 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007956 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007957 goto theend;
7958 }
7959
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007960 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007961 if (flags & SP_REPEAT)
7962 p_ws = FALSE;
7963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007964 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007965 if (argvars[3].v_type == VAR_UNKNOWN
7966 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007967 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007968 else
7969 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007970 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007971 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007972
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007973 if (argvars[5].v_type != VAR_UNKNOWN)
7974 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007975 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007976 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007977 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007978 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007979 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007980 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007981#ifdef FEAT_RELTIME
7982 if (argvars[6].v_type != VAR_UNKNOWN)
7983 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007984 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007985 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007986 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007987 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
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 }
7991#endif
7992 }
7993 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007994
7995 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
7996 match_pos, lnum_stop, time_limit);
7997
7998theend:
7999 p_ws = save_p_ws;
8000
8001 return retval;
8002}
8003
8004/*
8005 * "searchpair()" function
8006 */
8007 static void
8008f_searchpair(typval_T *argvars, typval_T *rettv)
8009{
8010 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
8011}
8012
8013/*
8014 * "searchpairpos()" function
8015 */
8016 static void
8017f_searchpairpos(typval_T *argvars, typval_T *rettv)
8018{
8019 pos_T match_pos;
8020 int lnum = 0;
8021 int col = 0;
8022
8023 if (rettv_list_alloc(rettv) == FAIL)
8024 return;
8025
8026 if (searchpair_cmn(argvars, &match_pos) > 0)
8027 {
8028 lnum = match_pos.lnum;
8029 col = match_pos.col;
8030 }
8031
8032 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8033 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8034}
8035
8036/*
8037 * Search for a start/middle/end thing.
8038 * Used by searchpair(), see its documentation for the details.
8039 * Returns 0 or -1 for no match,
8040 */
8041 long
8042do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008043 char_u *spat, // start pattern
8044 char_u *mpat, // middle pattern
8045 char_u *epat, // end pattern
8046 int dir, // BACKWARD or FORWARD
8047 typval_T *skip, // skip expression
8048 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008049 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008050 linenr_T lnum_stop, // stop at this line if not zero
8051 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008052{
8053 char_u *save_cpo;
8054 char_u *pat, *pat2 = NULL, *pat3 = NULL;
8055 long retval = 0;
8056 pos_T pos;
8057 pos_T firstpos;
8058 pos_T foundpos;
8059 pos_T save_cursor;
8060 pos_T save_pos;
8061 int n;
8062 int r;
8063 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01008064 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008065 int err;
8066 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008067#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008069#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008070
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008071 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008072 save_cpo = p_cpo;
8073 p_cpo = empty_option;
8074
8075#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008076 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008077 profile_setlimit(time_limit, &tm);
8078#endif
8079
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008080 // Make two search patterns: start/end (pat2, for in nested pairs) and
8081 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02008082 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
8083 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008084 if (pat2 == NULL || pat3 == NULL)
8085 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008086 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008087 if (*mpat == NUL)
8088 STRCPY(pat3, pat2);
8089 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008090 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008091 spat, epat, mpat);
8092 if (flags & SP_START)
8093 options |= SEARCH_START;
8094
Bram Moolenaar48570482017-10-30 21:48:41 +01008095 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02008096 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01008097
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008098 save_cursor = curwin->w_cursor;
8099 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008100 CLEAR_POS(&firstpos);
8101 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008102 pat = pat3;
8103 for (;;)
8104 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008105 searchit_arg_T sia;
8106
Bram Moolenaara80faa82020-04-12 19:37:17 +02008107 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008108 sia.sa_stop_lnum = lnum_stop;
8109#ifdef FEAT_RELTIME
8110 sia.sa_tm = &tm;
8111#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01008112 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008113 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008114 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008115 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008116 break;
8117
8118 if (firstpos.lnum == 0)
8119 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008120 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008121 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008122 // Found the same position again. Can happen with a pattern that
8123 // has "\zs" at the end and searching backwards. Advance one
8124 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008125 if (dir == BACKWARD)
8126 decl(&pos);
8127 else
8128 incl(&pos);
8129 }
8130 foundpos = pos;
8131
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008132 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008133 options &= ~SEARCH_START;
8134
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008135 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01008136 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008137 {
8138 save_pos = curwin->w_cursor;
8139 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01008140 err = FALSE;
8141 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008142 curwin->w_cursor = save_pos;
8143 if (err)
8144 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008145 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008146 curwin->w_cursor = save_cursor;
8147 retval = -1;
8148 break;
8149 }
8150 if (r)
8151 continue;
8152 }
8153
8154 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8155 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008156 // Found end when searching backwards or start when searching
8157 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008158 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008159 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008160 }
8161 else
8162 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008163 // Found end when searching forward or start when searching
8164 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008166 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008167 }
8168
8169 if (nest == 0)
8170 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008171 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008172 if (flags & SP_RETCOUNT)
8173 ++retval;
8174 else
8175 retval = pos.lnum;
8176 if (flags & SP_SETPCMARK)
8177 setpcmark();
8178 curwin->w_cursor = pos;
8179 if (!(flags & SP_REPEAT))
8180 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008181 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008182 }
8183 }
8184
8185 if (match_pos != NULL)
8186 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008187 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008188 match_pos->lnum = curwin->w_cursor.lnum;
8189 match_pos->col = curwin->w_cursor.col + 1;
8190 }
8191
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008192 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008193 if ((flags & SP_NOMOVE) || retval == 0)
8194 curwin->w_cursor = save_cursor;
8195
8196theend:
8197 vim_free(pat2);
8198 vim_free(pat3);
8199 if (p_cpo == empty_option)
8200 p_cpo = save_cpo;
8201 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008202 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008203 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008204 // If it's still empty it was changed and restored, need to restore in
8205 // the complicated way.
8206 if (*p_cpo == NUL)
8207 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008208 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008209 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008210
8211 return retval;
8212}
8213
8214/*
8215 * "searchpos()" function
8216 */
8217 static void
8218f_searchpos(typval_T *argvars, typval_T *rettv)
8219{
8220 pos_T match_pos;
8221 int lnum = 0;
8222 int col = 0;
8223 int n;
8224 int flags = 0;
8225
8226 if (rettv_list_alloc(rettv) == FAIL)
8227 return;
8228
8229 n = search_cmn(argvars, &match_pos, &flags);
8230 if (n > 0)
8231 {
8232 lnum = match_pos.lnum;
8233 col = match_pos.col;
8234 }
8235
8236 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8237 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8238 if (flags & SP_SUBPAT)
8239 list_append_number(rettv->vval.v_list, (varnumber_T)n);
8240}
8241
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008242/*
8243 * Set the cursor or mark position.
8244 * If 'charpos' is TRUE, then use the column number as a character offet.
8245 * Otherwise use the column number as a byte offset.
8246 */
8247 static void
8248set_position(typval_T *argvars, typval_T *rettv, int charpos)
8249{
8250 pos_T pos;
8251 int fnum;
8252 char_u *name;
8253 colnr_T curswant = -1;
8254
8255 rettv->vval.v_number = -1;
8256
8257 name = tv_get_string_chk(argvars);
8258 if (name != NULL)
8259 {
8260 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
8261 {
8262 if (pos.col != MAXCOL && --pos.col < 0)
8263 pos.col = 0;
8264 if ((name[0] == '.' && name[1] == NUL))
8265 {
8266 // set cursor; "fnum" is ignored
8267 curwin->w_cursor = pos;
8268 if (curswant >= 0)
8269 {
8270 curwin->w_curswant = curswant - 1;
8271 curwin->w_set_curswant = FALSE;
8272 }
8273 check_cursor();
8274 rettv->vval.v_number = 0;
8275 }
8276 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8277 {
8278 // set mark
8279 if (setmark_pos(name[1], &pos, fnum) == OK)
8280 rettv->vval.v_number = 0;
8281 }
8282 else
8283 emsg(_(e_invarg));
8284 }
8285 }
8286}
8287/*
8288 * "setcharpos()" function
8289 */
8290 static void
8291f_setcharpos(typval_T *argvars, typval_T *rettv)
8292{
8293 set_position(argvars, rettv, TRUE);
8294}
8295
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008296 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008297f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8298{
8299 dict_T *d;
8300 dictitem_T *di;
8301 char_u *csearch;
8302
8303 if (argvars[0].v_type != VAR_DICT)
8304 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008305 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008306 return;
8307 }
8308
8309 if ((d = argvars[0].vval.v_dict) != NULL)
8310 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008311 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008312 if (csearch != NULL)
8313 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008314 if (enc_utf8)
8315 {
8316 int pcc[MAX_MCO];
8317 int c = utfc_ptr2char(csearch, pcc);
8318
8319 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8320 }
8321 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008322 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008323 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008324 }
8325
8326 di = dict_find(d, (char_u *)"forward", -1);
8327 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008328 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008329 ? FORWARD : BACKWARD);
8330
8331 di = dict_find(d, (char_u *)"until", -1);
8332 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008333 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008334 }
8335}
8336
8337/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008338 * "setcursorcharpos" function
8339 */
8340 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008341f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008342{
8343 set_cursorpos(argvars, rettv, TRUE);
8344}
8345
8346/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008347 * "setenv()" function
8348 */
8349 static void
8350f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8351{
8352 char_u namebuf[NUMBUFLEN];
8353 char_u valbuf[NUMBUFLEN];
8354 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8355
8356 if (argvars[1].v_type == VAR_SPECIAL
8357 && argvars[1].vval.v_number == VVAL_NULL)
8358 vim_unsetenv(name);
8359 else
8360 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8361}
8362
8363/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008364 * "setfperm({fname}, {mode})" function
8365 */
8366 static void
8367f_setfperm(typval_T *argvars, typval_T *rettv)
8368{
8369 char_u *fname;
8370 char_u modebuf[NUMBUFLEN];
8371 char_u *mode_str;
8372 int i;
8373 int mask;
8374 int mode = 0;
8375
8376 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008377 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008378 if (fname == NULL)
8379 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008380 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008381 if (mode_str == NULL)
8382 return;
8383 if (STRLEN(mode_str) != 9)
8384 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008385 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008386 return;
8387 }
8388
8389 mask = 1;
8390 for (i = 8; i >= 0; --i)
8391 {
8392 if (mode_str[i] != '-')
8393 mode |= mask;
8394 mask = mask << 1;
8395 }
8396 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8397}
8398
8399/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008400 * "setpos()" function
8401 */
8402 static void
8403f_setpos(typval_T *argvars, typval_T *rettv)
8404{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008405 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008406}
8407
8408/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008409 * Translate a register type string to the yank type and block length
8410 */
8411 static int
8412get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8413{
8414 char_u *stropt = *pp;
8415 switch (*stropt)
8416 {
8417 case 'v': case 'c': // character-wise selection
8418 *yank_type = MCHAR;
8419 break;
8420 case 'V': case 'l': // line-wise selection
8421 *yank_type = MLINE;
8422 break;
8423 case 'b': case Ctrl_V: // block-wise selection
8424 *yank_type = MBLOCK;
8425 if (VIM_ISDIGIT(stropt[1]))
8426 {
8427 ++stropt;
8428 *block_len = getdigits(&stropt) - 1;
8429 --stropt;
8430 }
8431 break;
8432 default:
8433 return FAIL;
8434 }
8435 *pp = stropt;
8436 return OK;
8437}
8438
8439/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008440 * "setreg()" function
8441 */
8442 static void
8443f_setreg(typval_T *argvars, typval_T *rettv)
8444{
8445 int regname;
8446 char_u *strregname;
8447 char_u *stropt;
8448 char_u *strval;
8449 int append;
8450 char_u yank_type;
8451 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008452 typval_T *regcontents;
8453 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008454
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008455 pointreg = 0;
8456 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008457 block_len = -1;
8458 yank_type = MAUTO;
8459 append = FALSE;
8460
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008461 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008462 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008463
8464 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008465 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008466 regname = *strregname;
8467 if (regname == 0 || regname == '@')
8468 regname = '"';
8469
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008470 if (argvars[1].v_type == VAR_DICT)
8471 {
8472 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008473 dictitem_T *di;
8474
8475 if (d == NULL || d->dv_hashtab.ht_used == 0)
8476 {
8477 // Empty dict, clear the register (like setreg(0, []))
8478 char_u *lstval[2] = {NULL, NULL};
8479 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8480 return;
8481 }
8482
8483 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008484 if (di != NULL)
8485 regcontents = &di->di_tv;
8486
8487 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8488 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008489 {
8490 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8491
8492 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008493 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008494 semsg(_(e_invargval), "value");
8495 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008496 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008497 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008498
8499 if (regname == '"')
8500 {
8501 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8502 if (stropt != NULL)
8503 {
8504 pointreg = *stropt;
8505 regname = pointreg;
8506 }
8507 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008508 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008509 pointreg = regname;
8510 }
8511 else
8512 regcontents = &argvars[1];
8513
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008514 if (argvars[2].v_type != VAR_UNKNOWN)
8515 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008516 if (yank_type != MAUTO)
8517 {
8518 semsg(_(e_toomanyarg), "setreg");
8519 return;
8520 }
8521
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008522 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008523 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008524 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008525 for (; *stropt != NUL; ++stropt)
8526 switch (*stropt)
8527 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008528 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008529 append = TRUE;
8530 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008531 default:
8532 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008533 }
8534 }
8535
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008536 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008537 {
8538 char_u **lstval;
8539 char_u **allocval;
8540 char_u buf[NUMBUFLEN];
8541 char_u **curval;
8542 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008543 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008544 listitem_T *li;
8545 int len;
8546
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008547 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008548 len = ll == NULL ? 0 : ll->lv_len;
8549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008550 // First half: use for pointers to result lines; second half: use for
8551 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008552 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008553 if (lstval == NULL)
8554 return;
8555 curval = lstval;
8556 allocval = lstval + len + 2;
8557 curallocval = allocval;
8558
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008559 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008560 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008561 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008562 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008563 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008564 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008565 if (strval == NULL)
8566 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008567 if (strval == buf)
8568 {
8569 // Need to make a copy, next tv_get_string_buf_chk() will
8570 // overwrite the string.
8571 strval = vim_strsave(buf);
8572 if (strval == NULL)
8573 goto free_lstval;
8574 *curallocval++ = strval;
8575 }
8576 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008577 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008578 }
8579 *curval++ = NULL;
8580
8581 write_reg_contents_lst(regname, lstval, -1,
8582 append, yank_type, block_len);
8583free_lstval:
8584 while (curallocval > allocval)
8585 vim_free(*--curallocval);
8586 vim_free(lstval);
8587 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008588 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008589 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008590 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008591 if (strval == NULL)
8592 return;
8593 write_reg_contents_ex(regname, strval, -1,
8594 append, yank_type, block_len);
8595 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008596 if (pointreg != 0)
8597 get_yank_register(pointreg, TRUE);
8598
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008599 rettv->vval.v_number = 0;
8600}
8601
8602/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008603 * "settagstack()" function
8604 */
8605 static void
8606f_settagstack(typval_T *argvars, typval_T *rettv)
8607{
8608 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8609 win_T *wp;
8610 dict_T *d;
8611 int action = 'r';
8612
8613 rettv->vval.v_number = -1;
8614
8615 // first argument: window number or id
8616 wp = find_win_by_nr_or_id(&argvars[0]);
8617 if (wp == NULL)
8618 return;
8619
8620 // second argument: dict with items to set in the tag stack
8621 if (argvars[1].v_type != VAR_DICT)
8622 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008623 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008624 return;
8625 }
8626 d = argvars[1].vval.v_dict;
8627 if (d == NULL)
8628 return;
8629
8630 // third argument: action - 'a' for append and 'r' for replace.
8631 // default is to replace the stack.
8632 if (argvars[2].v_type == VAR_UNKNOWN)
8633 action = 'r';
8634 else if (argvars[2].v_type == VAR_STRING)
8635 {
8636 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008637 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008638 if (actstr == NULL)
8639 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008640 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8641 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008642 action = *actstr;
8643 else
8644 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008645 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008646 return;
8647 }
8648 }
8649 else
8650 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008651 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008652 return;
8653 }
8654
8655 if (set_tagstack(wp, d, action) == OK)
8656 rettv->vval.v_number = 0;
8657}
8658
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008659#ifdef FEAT_CRYPT
8660/*
8661 * "sha256({string})" function
8662 */
8663 static void
8664f_sha256(typval_T *argvars, typval_T *rettv)
8665{
8666 char_u *p;
8667
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008668 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008669 rettv->vval.v_string = vim_strsave(
8670 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8671 rettv->v_type = VAR_STRING;
8672}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008673#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008674
8675/*
8676 * "shellescape({string})" function
8677 */
8678 static void
8679f_shellescape(typval_T *argvars, typval_T *rettv)
8680{
Bram Moolenaar20615522017-06-05 18:46:26 +02008681 int do_special = non_zero_arg(&argvars[1]);
8682
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008683 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008684 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008685 rettv->v_type = VAR_STRING;
8686}
8687
8688/*
8689 * shiftwidth() function
8690 */
8691 static void
8692f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8693{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008694 rettv->vval.v_number = 0;
8695
8696 if (argvars[0].v_type != VAR_UNKNOWN)
8697 {
8698 long col;
8699
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008700 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008701 if (col < 0)
8702 return; // type error; errmsg already given
8703#ifdef FEAT_VARTABS
8704 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8705 return;
8706#endif
8707 }
8708
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709 rettv->vval.v_number = get_sw_value(curbuf);
8710}
8711
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008712#ifdef FEAT_FLOAT
8713/*
8714 * "sin()" function
8715 */
8716 static void
8717f_sin(typval_T *argvars, typval_T *rettv)
8718{
8719 float_T f = 0.0;
8720
8721 rettv->v_type = VAR_FLOAT;
8722 if (get_float_arg(argvars, &f) == OK)
8723 rettv->vval.v_float = sin(f);
8724 else
8725 rettv->vval.v_float = 0.0;
8726}
8727
8728/*
8729 * "sinh()" function
8730 */
8731 static void
8732f_sinh(typval_T *argvars, typval_T *rettv)
8733{
8734 float_T f = 0.0;
8735
8736 rettv->v_type = VAR_FLOAT;
8737 if (get_float_arg(argvars, &f) == OK)
8738 rettv->vval.v_float = sinh(f);
8739 else
8740 rettv->vval.v_float = 0.0;
8741}
8742#endif
8743
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008744/*
8745 * "soundfold({word})" function
8746 */
8747 static void
8748f_soundfold(typval_T *argvars, typval_T *rettv)
8749{
8750 char_u *s;
8751
8752 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008753 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008754#ifdef FEAT_SPELL
8755 rettv->vval.v_string = eval_soundfold(s);
8756#else
8757 rettv->vval.v_string = vim_strsave(s);
8758#endif
8759}
8760
8761/*
8762 * "spellbadword()" function
8763 */
8764 static void
8765f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8766{
8767 char_u *word = (char_u *)"";
8768 hlf_T attr = HLF_COUNT;
8769 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008770#ifdef FEAT_SPELL
8771 int wo_spell_save = curwin->w_p_spell;
8772
8773 if (!curwin->w_p_spell)
8774 {
8775 did_set_spelllang(curwin);
8776 curwin->w_p_spell = TRUE;
8777 }
8778
8779 if (*curwin->w_s->b_p_spl == NUL)
8780 {
8781 emsg(_(e_no_spell));
8782 curwin->w_p_spell = wo_spell_save;
8783 return;
8784 }
8785#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008786
8787 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008788 {
8789#ifdef FEAT_SPELL
8790 curwin->w_p_spell = wo_spell_save;
8791#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008792 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008793 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794
8795#ifdef FEAT_SPELL
8796 if (argvars[0].v_type == VAR_UNKNOWN)
8797 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008798 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008799 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8800 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008801 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008802 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008803 curwin->w_set_curswant = TRUE;
8804 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008805 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008806 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008807 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008808 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008809 int capcol = -1;
8810
8811 if (str != NULL)
8812 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008813 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008814 while (*str != NUL)
8815 {
8816 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8817 if (attr != HLF_COUNT)
8818 {
8819 word = str;
8820 break;
8821 }
8822 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008823 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008824 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008825 }
8826 }
8827 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008828 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008829#endif
8830
8831 list_append_string(rettv->vval.v_list, word, len);
8832 list_append_string(rettv->vval.v_list, (char_u *)(
8833 attr == HLF_SPB ? "bad" :
8834 attr == HLF_SPR ? "rare" :
8835 attr == HLF_SPL ? "local" :
8836 attr == HLF_SPC ? "caps" :
8837 ""), -1);
8838}
8839
8840/*
8841 * "spellsuggest()" function
8842 */
8843 static void
8844f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8845{
8846#ifdef FEAT_SPELL
8847 char_u *str;
8848 int typeerr = FALSE;
8849 int maxcount;
8850 garray_T ga;
8851 int i;
8852 listitem_T *li;
8853 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008854 int wo_spell_save = curwin->w_p_spell;
8855
8856 if (!curwin->w_p_spell)
8857 {
8858 did_set_spelllang(curwin);
8859 curwin->w_p_spell = TRUE;
8860 }
8861
8862 if (*curwin->w_s->b_p_spl == NUL)
8863 {
8864 emsg(_(e_no_spell));
8865 curwin->w_p_spell = wo_spell_save;
8866 return;
8867 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008868#endif
8869
8870 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008871 {
8872#ifdef FEAT_SPELL
8873 curwin->w_p_spell = wo_spell_save;
8874#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008876 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008877
8878#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008879 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008880 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008881 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008882 if (argvars[1].v_type != VAR_UNKNOWN)
8883 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008884 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008885 if (maxcount <= 0)
8886 return;
8887 if (argvars[2].v_type != VAR_UNKNOWN)
8888 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008889 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008890 if (typeerr)
8891 return;
8892 }
8893 }
8894 else
8895 maxcount = 25;
8896
8897 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8898
8899 for (i = 0; i < ga.ga_len; ++i)
8900 {
8901 str = ((char_u **)ga.ga_data)[i];
8902
8903 li = listitem_alloc();
8904 if (li == NULL)
8905 vim_free(str);
8906 else
8907 {
8908 li->li_tv.v_type = VAR_STRING;
8909 li->li_tv.v_lock = 0;
8910 li->li_tv.vval.v_string = str;
8911 list_append(rettv->vval.v_list, li);
8912 }
8913 }
8914 ga_clear(&ga);
8915 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008916 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008917#endif
8918}
8919
8920 static void
8921f_split(typval_T *argvars, typval_T *rettv)
8922{
8923 char_u *str;
8924 char_u *end;
8925 char_u *pat = NULL;
8926 regmatch_T regmatch;
8927 char_u patbuf[NUMBUFLEN];
8928 char_u *save_cpo;
8929 int match;
8930 colnr_T col = 0;
8931 int keepempty = FALSE;
8932 int typeerr = FALSE;
8933
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008934 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008935 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008936 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008937
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008938 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008939 if (argvars[1].v_type != VAR_UNKNOWN)
8940 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008941 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008942 if (pat == NULL)
8943 typeerr = TRUE;
8944 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008945 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008946 }
8947 if (pat == NULL || *pat == NUL)
8948 pat = (char_u *)"[\\x01- ]\\+";
8949
8950 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008951 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008952 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008953 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008954
8955 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8956 if (regmatch.regprog != NULL)
8957 {
8958 regmatch.rm_ic = FALSE;
8959 while (*str != NUL || keepempty)
8960 {
8961 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008962 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008963 else
8964 match = vim_regexec_nl(&regmatch, str, col);
8965 if (match)
8966 end = regmatch.startp[0];
8967 else
8968 end = str + STRLEN(str);
8969 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
8970 && *str != NUL && match && end < regmatch.endp[0]))
8971 {
8972 if (list_append_string(rettv->vval.v_list, str,
8973 (int)(end - str)) == FAIL)
8974 break;
8975 }
8976 if (!match)
8977 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01008978 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008979 if (regmatch.endp[0] > str)
8980 col = 0;
8981 else
Bram Moolenaar13505972019-01-24 15:04:48 +01008982 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008983 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008984 str = regmatch.endp[0];
8985 }
8986
8987 vim_regfree(regmatch.regprog);
8988 }
8989
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008990theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008991 p_cpo = save_cpo;
8992}
8993
8994#ifdef FEAT_FLOAT
8995/*
8996 * "sqrt()" function
8997 */
8998 static void
8999f_sqrt(typval_T *argvars, typval_T *rettv)
9000{
9001 float_T f = 0.0;
9002
9003 rettv->v_type = VAR_FLOAT;
9004 if (get_float_arg(argvars, &f) == OK)
9005 rettv->vval.v_float = sqrt(f);
9006 else
9007 rettv->vval.v_float = 0.0;
9008}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009009#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009010
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009011#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01009012/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009013 * "str2float()" function
9014 */
9015 static void
9016f_str2float(typval_T *argvars, typval_T *rettv)
9017{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009018 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009019 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009020
Bram Moolenaar08243d22017-01-10 16:12:29 +01009021 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009022 p = skipwhite(p + 1);
9023 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01009024 if (isneg)
9025 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026 rettv->v_type = VAR_FLOAT;
9027}
9028#endif
9029
9030/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02009031 * "str2list()" function
9032 */
9033 static void
9034f_str2list(typval_T *argvars, typval_T *rettv)
9035{
9036 char_u *p;
9037 int utf8 = FALSE;
9038
9039 if (rettv_list_alloc(rettv) == FAIL)
9040 return;
9041
9042 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02009043 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02009044
9045 p = tv_get_string(&argvars[0]);
9046
9047 if (has_mbyte || utf8)
9048 {
9049 int (*ptr2len)(char_u *);
9050 int (*ptr2char)(char_u *);
9051
9052 if (utf8 || enc_utf8)
9053 {
9054 ptr2len = utf_ptr2len;
9055 ptr2char = utf_ptr2char;
9056 }
9057 else
9058 {
9059 ptr2len = mb_ptr2len;
9060 ptr2char = mb_ptr2char;
9061 }
9062
9063 for ( ; *p != NUL; p += (*ptr2len)(p))
9064 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
9065 }
9066 else
9067 for ( ; *p != NUL; ++p)
9068 list_append_number(rettv->vval.v_list, *p);
9069}
9070
9071/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009072 * "str2nr()" function
9073 */
9074 static void
9075f_str2nr(typval_T *argvars, typval_T *rettv)
9076{
9077 int base = 10;
9078 char_u *p;
9079 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009080 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01009081 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009082
9083 if (argvars[1].v_type != VAR_UNKNOWN)
9084 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009085 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009086 if (base != 2 && base != 8 && base != 10 && base != 16)
9087 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009088 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009089 return;
9090 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02009091 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009092 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009093 }
9094
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01009095 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009096 isneg = (*p == '-');
9097 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009098 p = skipwhite(p + 1);
9099 switch (base)
9100 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009101 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02009102 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009103 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009104 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02009105 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
9106 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01009107 if (isneg)
9108 rettv->vval.v_number = -n;
9109 else
9110 rettv->vval.v_number = n;
9111
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112}
9113
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009114/*
9115 * "strgetchar()" function
9116 */
9117 static void
9118f_strgetchar(typval_T *argvars, typval_T *rettv)
9119{
9120 char_u *str;
9121 int len;
9122 int error = FALSE;
9123 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01009124 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009125
9126 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009127 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009128 if (str == NULL)
9129 return;
9130 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009131 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009132 if (error)
9133 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009134
Bram Moolenaar13505972019-01-24 15:04:48 +01009135 while (charidx >= 0 && byteidx < len)
9136 {
9137 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009138 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009139 rettv->vval.v_number = mb_ptr2char(str + byteidx);
9140 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009141 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009142 --charidx;
9143 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009144 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009145}
9146
9147/*
9148 * "stridx()" function
9149 */
9150 static void
9151f_stridx(typval_T *argvars, typval_T *rettv)
9152{
9153 char_u buf[NUMBUFLEN];
9154 char_u *needle;
9155 char_u *haystack;
9156 char_u *save_haystack;
9157 char_u *pos;
9158 int start_idx;
9159
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009160 needle = tv_get_string_chk(&argvars[1]);
9161 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009162 rettv->vval.v_number = -1;
9163 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009164 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009165
9166 if (argvars[2].v_type != VAR_UNKNOWN)
9167 {
9168 int error = FALSE;
9169
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009170 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009171 if (error || start_idx >= (int)STRLEN(haystack))
9172 return;
9173 if (start_idx >= 0)
9174 haystack += start_idx;
9175 }
9176
9177 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9178 if (pos != NULL)
9179 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
9180}
9181
9182/*
9183 * "string()" function
9184 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009185 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009186f_string(typval_T *argvars, typval_T *rettv)
9187{
9188 char_u *tofree;
9189 char_u numbuf[NUMBUFLEN];
9190
9191 rettv->v_type = VAR_STRING;
9192 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
9193 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009194 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009195 if (rettv->vval.v_string != NULL && tofree == NULL)
9196 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
9197}
9198
9199/*
9200 * "strlen()" function
9201 */
9202 static void
9203f_strlen(typval_T *argvars, typval_T *rettv)
9204{
9205 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009206 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009207}
9208
9209/*
9210 * "strchars()" function
9211 */
9212 static void
9213f_strchars(typval_T *argvars, typval_T *rettv)
9214{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009215 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009216 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009217 varnumber_T len = 0;
9218 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009219
9220 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009221 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009222 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02009223 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009224 else
9225 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009226 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
9227 while (*s != NUL)
9228 {
9229 func_mb_ptr2char_adv(&s);
9230 ++len;
9231 }
9232 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009233 }
9234}
9235
9236/*
9237 * "strdisplaywidth()" function
9238 */
9239 static void
9240f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
9241{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009242 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009243 int col = 0;
9244
9245 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009246 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009247
9248 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
9249}
9250
9251/*
9252 * "strwidth()" function
9253 */
9254 static void
9255f_strwidth(typval_T *argvars, typval_T *rettv)
9256{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009257 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009258
Bram Moolenaar13505972019-01-24 15:04:48 +01009259 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009260}
9261
9262/*
9263 * "strcharpart()" function
9264 */
9265 static void
9266f_strcharpart(typval_T *argvars, typval_T *rettv)
9267{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009268 char_u *p;
9269 int nchar;
9270 int nbyte = 0;
9271 int charlen;
9272 int len = 0;
9273 int slen;
9274 int error = FALSE;
9275
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009276 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009277 slen = (int)STRLEN(p);
9278
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009279 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009280 if (!error)
9281 {
9282 if (nchar > 0)
9283 while (nchar > 0 && nbyte < slen)
9284 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009285 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009286 --nchar;
9287 }
9288 else
9289 nbyte = nchar;
9290 if (argvars[2].v_type != VAR_UNKNOWN)
9291 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009292 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009293 while (charlen > 0 && nbyte + len < slen)
9294 {
9295 int off = nbyte + len;
9296
9297 if (off < 0)
9298 len += 1;
9299 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009300 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009301 --charlen;
9302 }
9303 }
9304 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009305 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009306 }
9307
9308 /*
9309 * Only return the overlap between the specified part and the actual
9310 * string.
9311 */
9312 if (nbyte < 0)
9313 {
9314 len += nbyte;
9315 nbyte = 0;
9316 }
9317 else if (nbyte > slen)
9318 nbyte = slen;
9319 if (len < 0)
9320 len = 0;
9321 else if (nbyte + len > slen)
9322 len = slen - nbyte;
9323
9324 rettv->v_type = VAR_STRING;
9325 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009326}
9327
9328/*
9329 * "strpart()" function
9330 */
9331 static void
9332f_strpart(typval_T *argvars, typval_T *rettv)
9333{
9334 char_u *p;
9335 int n;
9336 int len;
9337 int slen;
9338 int error = FALSE;
9339
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009340 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009341 slen = (int)STRLEN(p);
9342
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009343 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009344 if (error)
9345 len = 0;
9346 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009347 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009348 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009349 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009350
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009351 // Only return the overlap between the specified part and the actual
9352 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009353 if (n < 0)
9354 {
9355 len += n;
9356 n = 0;
9357 }
9358 else if (n > slen)
9359 n = slen;
9360 if (len < 0)
9361 len = 0;
9362 else if (n + len > slen)
9363 len = slen - n;
9364
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009365 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9366 {
9367 int off;
9368
9369 // length in characters
9370 for (off = n; off < slen && len > 0; --len)
9371 off += mb_ptr2len(p + off);
9372 len = off - n;
9373 }
9374
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009375 rettv->v_type = VAR_STRING;
9376 rettv->vval.v_string = vim_strnsave(p + n, len);
9377}
9378
9379/*
9380 * "strridx()" function
9381 */
9382 static void
9383f_strridx(typval_T *argvars, typval_T *rettv)
9384{
9385 char_u buf[NUMBUFLEN];
9386 char_u *needle;
9387 char_u *haystack;
9388 char_u *rest;
9389 char_u *lastmatch = NULL;
9390 int haystack_len, end_idx;
9391
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009392 needle = tv_get_string_chk(&argvars[1]);
9393 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009394
9395 rettv->vval.v_number = -1;
9396 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009397 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009398
9399 haystack_len = (int)STRLEN(haystack);
9400 if (argvars[2].v_type != VAR_UNKNOWN)
9401 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009402 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009403 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009404 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009405 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009406 }
9407 else
9408 end_idx = haystack_len;
9409
9410 if (*needle == NUL)
9411 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009412 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009413 lastmatch = haystack + end_idx;
9414 }
9415 else
9416 {
9417 for (rest = haystack; *rest != '\0'; ++rest)
9418 {
9419 rest = (char_u *)strstr((char *)rest, (char *)needle);
9420 if (rest == NULL || rest > haystack + end_idx)
9421 break;
9422 lastmatch = rest;
9423 }
9424 }
9425
9426 if (lastmatch == NULL)
9427 rettv->vval.v_number = -1;
9428 else
9429 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9430}
9431
9432/*
9433 * "strtrans()" function
9434 */
9435 static void
9436f_strtrans(typval_T *argvars, typval_T *rettv)
9437{
9438 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009439 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009440}
9441
9442/*
9443 * "submatch()" function
9444 */
9445 static void
9446f_submatch(typval_T *argvars, typval_T *rettv)
9447{
9448 int error = FALSE;
9449 int no;
9450 int retList = 0;
9451
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009452 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009453 if (error)
9454 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009455 if (no < 0 || no >= NSUBEXP)
9456 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009457 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009458 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009459 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009460 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009461 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009462 if (error)
9463 return;
9464
9465 if (retList == 0)
9466 {
9467 rettv->v_type = VAR_STRING;
9468 rettv->vval.v_string = reg_submatch(no);
9469 }
9470 else
9471 {
9472 rettv->v_type = VAR_LIST;
9473 rettv->vval.v_list = reg_submatch_list(no);
9474 }
9475}
9476
9477/*
9478 * "substitute()" function
9479 */
9480 static void
9481f_substitute(typval_T *argvars, typval_T *rettv)
9482{
9483 char_u patbuf[NUMBUFLEN];
9484 char_u subbuf[NUMBUFLEN];
9485 char_u flagsbuf[NUMBUFLEN];
9486
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009487 char_u *str = tv_get_string_chk(&argvars[0]);
9488 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009489 char_u *sub = NULL;
9490 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009491 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009492
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009493 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9494 expr = &argvars[2];
9495 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009496 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009497
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009498 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009499 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9500 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009501 rettv->vval.v_string = NULL;
9502 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009503 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009504}
9505
9506/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009507 * "swapinfo(swap_filename)" function
9508 */
9509 static void
9510f_swapinfo(typval_T *argvars, typval_T *rettv)
9511{
9512 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009513 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009514}
9515
9516/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009517 * "swapname(expr)" function
9518 */
9519 static void
9520f_swapname(typval_T *argvars, typval_T *rettv)
9521{
9522 buf_T *buf;
9523
9524 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009525 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009526 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9527 || buf->b_ml.ml_mfp->mf_fname == NULL)
9528 rettv->vval.v_string = NULL;
9529 else
9530 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9531}
9532
9533/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009534 * "synID(lnum, col, trans)" function
9535 */
9536 static void
9537f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9538{
9539 int id = 0;
9540#ifdef FEAT_SYN_HL
9541 linenr_T lnum;
9542 colnr_T col;
9543 int trans;
9544 int transerr = FALSE;
9545
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009546 lnum = tv_get_lnum(argvars); // -1 on type error
9547 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009548 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009549
9550 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9551 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9552 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9553#endif
9554
9555 rettv->vval.v_number = id;
9556}
9557
9558/*
9559 * "synIDattr(id, what [, mode])" function
9560 */
9561 static void
9562f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9563{
9564 char_u *p = NULL;
9565#ifdef FEAT_SYN_HL
9566 int id;
9567 char_u *what;
9568 char_u *mode;
9569 char_u modebuf[NUMBUFLEN];
9570 int modec;
9571
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009572 id = (int)tv_get_number(&argvars[0]);
9573 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009574 if (argvars[2].v_type != VAR_UNKNOWN)
9575 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009576 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009577 modec = TOLOWER_ASC(mode[0]);
9578 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009579 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009580 }
9581 else
9582 {
9583#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9584 if (USE_24BIT)
9585 modec = 'g';
9586 else
9587#endif
9588 if (t_colors > 1)
9589 modec = 'c';
9590 else
9591 modec = 't';
9592 }
9593
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009594 switch (TOLOWER_ASC(what[0]))
9595 {
9596 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009597 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009598 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009599 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009600 p = highlight_has_attr(id, HL_BOLD, modec);
9601 break;
9602
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009603 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009604 p = highlight_color(id, what, modec);
9605 break;
9606
9607 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009608 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009609 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009610 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009611 p = highlight_has_attr(id, HL_ITALIC, modec);
9612 break;
9613
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009614 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009615 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009616 break;
9617
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009618 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009619 p = highlight_has_attr(id, HL_INVERSE, modec);
9620 break;
9621
9622 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009623 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009624 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009625 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009626 else if (TOLOWER_ASC(what[1]) == 't' &&
9627 TOLOWER_ASC(what[2]) == 'r')
9628 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009629 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009630 p = highlight_has_attr(id, HL_STANDOUT, modec);
9631 break;
9632
9633 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009634 if (TOLOWER_ASC(what[1]) == 'l') // ul
9635 p = highlight_color(id, what, modec);
9636 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009637 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009638 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9639 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009640 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009641 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9642 break;
9643 }
9644
9645 if (p != NULL)
9646 p = vim_strsave(p);
9647#endif
9648 rettv->v_type = VAR_STRING;
9649 rettv->vval.v_string = p;
9650}
9651
9652/*
9653 * "synIDtrans(id)" function
9654 */
9655 static void
9656f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9657{
9658 int id;
9659
9660#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009661 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009662
9663 if (id > 0)
9664 id = syn_get_final_id(id);
9665 else
9666#endif
9667 id = 0;
9668
9669 rettv->vval.v_number = id;
9670}
9671
9672/*
9673 * "synconcealed(lnum, col)" function
9674 */
9675 static void
9676f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9677{
9678#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9679 linenr_T lnum;
9680 colnr_T col;
9681 int syntax_flags = 0;
9682 int cchar;
9683 int matchid = 0;
9684 char_u str[NUMBUFLEN];
9685#endif
9686
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009687 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009688
9689#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009690 lnum = tv_get_lnum(argvars); // -1 on type error
9691 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009692
Bram Moolenaara80faa82020-04-12 19:37:17 +02009693 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009694
9695 if (rettv_list_alloc(rettv) != FAIL)
9696 {
9697 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9698 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9699 && curwin->w_p_cole > 0)
9700 {
9701 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9702 syntax_flags = get_syntax_info(&matchid);
9703
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009704 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009705 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9706 {
9707 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009708 if (cchar == NUL && curwin->w_p_cole == 1)
9709 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009710 if (cchar != NUL)
9711 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009712 if (has_mbyte)
9713 (*mb_char2bytes)(cchar, str);
9714 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009715 str[0] = cchar;
9716 }
9717 }
9718 }
9719
9720 list_append_number(rettv->vval.v_list,
9721 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009722 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009723 list_append_string(rettv->vval.v_list, str, -1);
9724 list_append_number(rettv->vval.v_list, matchid);
9725 }
9726#endif
9727}
9728
9729/*
9730 * "synstack(lnum, col)" function
9731 */
9732 static void
9733f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9734{
9735#ifdef FEAT_SYN_HL
9736 linenr_T lnum;
9737 colnr_T col;
9738 int i;
9739 int id;
9740#endif
9741
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009742 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009743
9744#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009745 lnum = tv_get_lnum(argvars); // -1 on type error
9746 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009747
9748 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9749 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9750 && rettv_list_alloc(rettv) != FAIL)
9751 {
9752 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9753 for (i = 0; ; ++i)
9754 {
9755 id = syn_get_stack_item(i);
9756 if (id < 0)
9757 break;
9758 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9759 break;
9760 }
9761 }
9762#endif
9763}
9764
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009765/*
9766 * "tabpagebuflist()" function
9767 */
9768 static void
9769f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9770{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009771 tabpage_T *tp;
9772 win_T *wp = NULL;
9773
9774 if (argvars[0].v_type == VAR_UNKNOWN)
9775 wp = firstwin;
9776 else
9777 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009778 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009779 if (tp != NULL)
9780 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9781 }
9782 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9783 {
9784 for (; wp != NULL; wp = wp->w_next)
9785 if (list_append_number(rettv->vval.v_list,
9786 wp->w_buffer->b_fnum) == FAIL)
9787 break;
9788 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009789}
9790
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009791/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009792 * "tagfiles()" function
9793 */
9794 static void
9795f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9796{
9797 char_u *fname;
9798 tagname_T tn;
9799 int first;
9800
9801 if (rettv_list_alloc(rettv) == FAIL)
9802 return;
9803 fname = alloc(MAXPATHL);
9804 if (fname == NULL)
9805 return;
9806
9807 for (first = TRUE; ; first = FALSE)
9808 if (get_tagfname(&tn, first, fname) == FAIL
9809 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9810 break;
9811 tagname_free(&tn);
9812 vim_free(fname);
9813}
9814
9815/*
9816 * "taglist()" function
9817 */
9818 static void
9819f_taglist(typval_T *argvars, typval_T *rettv)
9820{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009821 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009822 char_u *tag_pattern;
9823
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009824 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009825
9826 rettv->vval.v_number = FALSE;
9827 if (*tag_pattern == NUL)
9828 return;
9829
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009830 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009831 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009832 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009833 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009834}
9835
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009836#ifdef FEAT_FLOAT
9837/*
9838 * "tan()" function
9839 */
9840 static void
9841f_tan(typval_T *argvars, typval_T *rettv)
9842{
9843 float_T f = 0.0;
9844
9845 rettv->v_type = VAR_FLOAT;
9846 if (get_float_arg(argvars, &f) == OK)
9847 rettv->vval.v_float = tan(f);
9848 else
9849 rettv->vval.v_float = 0.0;
9850}
9851
9852/*
9853 * "tanh()" function
9854 */
9855 static void
9856f_tanh(typval_T *argvars, typval_T *rettv)
9857{
9858 float_T f = 0.0;
9859
9860 rettv->v_type = VAR_FLOAT;
9861 if (get_float_arg(argvars, &f) == OK)
9862 rettv->vval.v_float = tanh(f);
9863 else
9864 rettv->vval.v_float = 0.0;
9865}
9866#endif
9867
9868/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009869 * "tolower(string)" function
9870 */
9871 static void
9872f_tolower(typval_T *argvars, typval_T *rettv)
9873{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009874 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009875 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009876}
9877
9878/*
9879 * "toupper(string)" function
9880 */
9881 static void
9882f_toupper(typval_T *argvars, typval_T *rettv)
9883{
9884 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009885 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009886}
9887
9888/*
9889 * "tr(string, fromstr, tostr)" function
9890 */
9891 static void
9892f_tr(typval_T *argvars, typval_T *rettv)
9893{
9894 char_u *in_str;
9895 char_u *fromstr;
9896 char_u *tostr;
9897 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009898 int inlen;
9899 int fromlen;
9900 int tolen;
9901 int idx;
9902 char_u *cpstr;
9903 int cplen;
9904 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009905 char_u buf[NUMBUFLEN];
9906 char_u buf2[NUMBUFLEN];
9907 garray_T ga;
9908
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009909 in_str = tv_get_string(&argvars[0]);
9910 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9911 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009912
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009913 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009914 rettv->v_type = VAR_STRING;
9915 rettv->vval.v_string = NULL;
9916 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009917 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009918 ga_init2(&ga, (int)sizeof(char), 80);
9919
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009920 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009921 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009922 if (STRLEN(fromstr) != STRLEN(tostr))
9923 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009924error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009925 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009926 ga_clear(&ga);
9927 return;
9928 }
9929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009930 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009931 while (*in_str != NUL)
9932 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009933 if (has_mbyte)
9934 {
9935 inlen = (*mb_ptr2len)(in_str);
9936 cpstr = in_str;
9937 cplen = inlen;
9938 idx = 0;
9939 for (p = fromstr; *p != NUL; p += fromlen)
9940 {
9941 fromlen = (*mb_ptr2len)(p);
9942 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9943 {
9944 for (p = tostr; *p != NUL; p += tolen)
9945 {
9946 tolen = (*mb_ptr2len)(p);
9947 if (idx-- == 0)
9948 {
9949 cplen = tolen;
9950 cpstr = p;
9951 break;
9952 }
9953 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009954 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009955 goto error;
9956 break;
9957 }
9958 ++idx;
9959 }
9960
9961 if (first && cpstr == in_str)
9962 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009963 // Check that fromstr and tostr have the same number of
9964 // (multi-byte) characters. Done only once when a character
9965 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009966 first = FALSE;
9967 for (p = tostr; *p != NUL; p += tolen)
9968 {
9969 tolen = (*mb_ptr2len)(p);
9970 --idx;
9971 }
9972 if (idx != 0)
9973 goto error;
9974 }
9975
9976 (void)ga_grow(&ga, cplen);
9977 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
9978 ga.ga_len += cplen;
9979
9980 in_str += inlen;
9981 }
9982 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009983 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009984 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009985 p = vim_strchr(fromstr, *in_str);
9986 if (p != NULL)
9987 ga_append(&ga, tostr[p - fromstr]);
9988 else
9989 ga_append(&ga, *in_str);
9990 ++in_str;
9991 }
9992 }
9993
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009994 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009995 (void)ga_grow(&ga, 1);
9996 ga_append(&ga, NUL);
9997
9998 rettv->vval.v_string = ga.ga_data;
9999}
10000
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010001/*
10002 * "trim({expr})" function
10003 */
10004 static void
10005f_trim(typval_T *argvars, typval_T *rettv)
10006{
10007 char_u buf1[NUMBUFLEN];
10008 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010009 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010010 char_u *mask = NULL;
10011 char_u *tail;
10012 char_u *prev;
10013 char_u *p;
10014 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010015 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010016
10017 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010018 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010019 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010020 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010021
10022 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010023 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010024 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010025
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010026 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010027 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010028 int error = 0;
10029
10030 // leading or trailing characters to trim
10031 dir = (int)tv_get_number_chk(&argvars[2], &error);
10032 if (error)
10033 return;
10034 if (dir < 0 || dir > 2)
10035 {
10036 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
10037 return;
10038 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010039 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010040 }
10041
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010042 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010043 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010044 // Trim leading characters
10045 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010046 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010047 c1 = PTR2CHAR(head);
10048 if (mask == NULL)
10049 {
10050 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010051 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010052 }
10053 else
10054 {
10055 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10056 if (c1 == PTR2CHAR(p))
10057 break;
10058 if (*p == NUL)
10059 break;
10060 }
10061 MB_PTR_ADV(head);
10062 }
10063 }
10064
10065 tail = head + STRLEN(head);
10066 if (dir == 0 || dir == 2)
10067 {
10068 // Trim trailing characters
10069 for (; tail > head; tail = prev)
10070 {
10071 prev = tail;
10072 MB_PTR_BACK(head, prev);
10073 c1 = PTR2CHAR(prev);
10074 if (mask == NULL)
10075 {
10076 if (c1 > ' ' && c1 != 0xa0)
10077 break;
10078 }
10079 else
10080 {
10081 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10082 if (c1 == PTR2CHAR(p))
10083 break;
10084 if (*p == NUL)
10085 break;
10086 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010087 }
10088 }
Bram Moolenaardf44a272020-06-07 20:49:05 +020010089 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010090}
10091
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010092#ifdef FEAT_FLOAT
10093/*
10094 * "trunc({float})" function
10095 */
10096 static void
10097f_trunc(typval_T *argvars, typval_T *rettv)
10098{
10099 float_T f = 0.0;
10100
10101 rettv->v_type = VAR_FLOAT;
10102 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010103 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010104 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
10105 else
10106 rettv->vval.v_float = 0.0;
10107}
10108#endif
10109
10110/*
10111 * "type(expr)" function
10112 */
10113 static void
10114f_type(typval_T *argvars, typval_T *rettv)
10115{
10116 int n = -1;
10117
10118 switch (argvars[0].v_type)
10119 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010120 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
10121 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010122 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010123 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
10124 case VAR_LIST: n = VAR_TYPE_LIST; break;
10125 case VAR_DICT: n = VAR_TYPE_DICT; break;
10126 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
10127 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
10128 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020010129 case VAR_JOB: n = VAR_TYPE_JOB; break;
10130 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010131 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010132 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +020010133 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010134 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +010010135 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010136 n = -1;
10137 break;
10138 }
10139 rettv->vval.v_number = n;
10140}
10141
10142/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010143 * "virtcol(string)" function
10144 */
10145 static void
10146f_virtcol(typval_T *argvars, typval_T *rettv)
10147{
10148 colnr_T vcol = 0;
10149 pos_T *fp;
10150 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010151 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010152
Bram Moolenaar6f02b002021-01-10 20:22:54 +010010153 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010154 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
10155 && fnum == curbuf->b_fnum)
10156 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010157 // Limit the column to a valid value, getvvcol() doesn't check.
10158 if (fp->col < 0)
10159 fp->col = 0;
10160 else
10161 {
10162 len = (int)STRLEN(ml_get(fp->lnum));
10163 if (fp->col > len)
10164 fp->col = len;
10165 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010166 getvvcol(curwin, fp, NULL, NULL, &vcol);
10167 ++vcol;
10168 }
10169
10170 rettv->vval.v_number = vcol;
10171}
10172
10173/*
10174 * "visualmode()" function
10175 */
10176 static void
10177f_visualmode(typval_T *argvars, typval_T *rettv)
10178{
10179 char_u str[2];
10180
10181 rettv->v_type = VAR_STRING;
10182 str[0] = curbuf->b_visual_mode_eval;
10183 str[1] = NUL;
10184 rettv->vval.v_string = vim_strsave(str);
10185
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010186 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010187 if (non_zero_arg(&argvars[0]))
10188 curbuf->b_visual_mode_eval = NUL;
10189}
10190
10191/*
10192 * "wildmenumode()" function
10193 */
10194 static void
10195f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10196{
10197#ifdef FEAT_WILDMENU
10198 if (wild_menu_showing)
10199 rettv->vval.v_number = 1;
10200#endif
10201}
10202
10203/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +010010204 * "windowsversion()" function
10205 */
10206 static void
10207f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10208{
10209 rettv->v_type = VAR_STRING;
10210 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
10211}
10212
10213/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010214 * "wordcount()" function
10215 */
10216 static void
10217f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
10218{
10219 if (rettv_dict_alloc(rettv) == FAIL)
10220 return;
10221 cursor_pos_info(rettv->vval.v_dict);
10222}
10223
10224/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010225 * "xor(expr, expr)" function
10226 */
10227 static void
10228f_xor(typval_T *argvars, typval_T *rettv)
10229{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010230 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
10231 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010232}
10233
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010234#endif // FEAT_EVAL