blob: 29bbe8ab879afab04b9b275e1f9fb160ad9026f7 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020023#ifdef FEAT_FLOAT
24static void f_abs(typval_T *argvars, typval_T *rettv);
25static void f_acos(typval_T *argvars, typval_T *rettv);
26#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020027static void f_and(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028#ifdef FEAT_FLOAT
29static void f_asin(typval_T *argvars, typval_T *rettv);
30static void f_atan(typval_T *argvars, typval_T *rettv);
31static void f_atan2(typval_T *argvars, typval_T *rettv);
32#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010033#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020034static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010035static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010036# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010037static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010038# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010039#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_byte2line(typval_T *argvars, typval_T *rettv);
41static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
42static void f_byteidx(typval_T *argvars, typval_T *rettv);
43static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
44static void f_call(typval_T *argvars, typval_T *rettv);
45#ifdef FEAT_FLOAT
46static void f_ceil(typval_T *argvars, typval_T *rettv);
47#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_changenr(typval_T *argvars, typval_T *rettv);
49static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010050static void f_charcol(typval_T *argvars, typval_T *rettv);
Bram Moolenaar17793ef2020-12-28 12:56:58 +010051static void f_charidx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020053static void f_confirm(typval_T *argvars, typval_T *rettv);
54static void f_copy(typval_T *argvars, typval_T *rettv);
55#ifdef FEAT_FLOAT
56static void f_cos(typval_T *argvars, typval_T *rettv);
57static void f_cosh(typval_T *argvars, typval_T *rettv);
58#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010060#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020061static void f_debugbreak(typval_T *argvars, typval_T *rettv);
62#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020063static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020064static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010065static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020066static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020067static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_escape(typval_T *argvars, typval_T *rettv);
69static void f_eval(typval_T *argvars, typval_T *rettv);
70static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_exists(typval_T *argvars, typval_T *rettv);
73#ifdef FEAT_FLOAT
74static void f_exp(typval_T *argvars, typval_T *rettv);
75#endif
76static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020077static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020078static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020079#ifdef FEAT_FLOAT
80static void f_float2nr(typval_T *argvars, typval_T *rettv);
81static void f_floor(typval_T *argvars, typval_T *rettv);
82static void f_fmod(typval_T *argvars, typval_T *rettv);
83#endif
84static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020085static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020086static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_function(typval_T *argvars, typval_T *rettv);
88static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
89static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010090static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010091static void f_getcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020092static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020093static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020094static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010095static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_getpid(typval_T *argvars, typval_T *rettv);
97static void f_getcurpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010098static void f_getcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020099static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200100static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200101static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100103static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +0200104static void f_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200105static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
106static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200107static void f_hlID(typval_T *argvars, typval_T *rettv);
108static void f_hlexists(typval_T *argvars, typval_T *rettv);
109static void f_hostname(typval_T *argvars, typval_T *rettv);
110static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200111static void f_index(typval_T *argvars, typval_T *rettv);
112static void f_input(typval_T *argvars, typval_T *rettv);
113static void f_inputdialog(typval_T *argvars, typval_T *rettv);
114static void f_inputlist(typval_T *argvars, typval_T *rettv);
115static void f_inputrestore(typval_T *argvars, typval_T *rettv);
116static void f_inputsave(typval_T *argvars, typval_T *rettv);
117static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +0100118static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200119static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120static void f_islocked(typval_T *argvars, typval_T *rettv);
121#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200122static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200123static void f_isnan(typval_T *argvars, typval_T *rettv);
124#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200125static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
126static void f_len(typval_T *argvars, typval_T *rettv);
127static void f_libcall(typval_T *argvars, typval_T *rettv);
128static void f_libcallnr(typval_T *argvars, typval_T *rettv);
129static void f_line(typval_T *argvars, typval_T *rettv);
130static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200131#ifdef FEAT_FLOAT
132static void f_log(typval_T *argvars, typval_T *rettv);
133static void f_log10(typval_T *argvars, typval_T *rettv);
134#endif
135#ifdef FEAT_LUA
136static void f_luaeval(typval_T *argvars, typval_T *rettv);
137#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200138static void f_maparg(typval_T *argvars, typval_T *rettv);
139static void f_mapcheck(typval_T *argvars, typval_T *rettv);
140static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200141static void f_matchend(typval_T *argvars, typval_T *rettv);
142static void f_matchlist(typval_T *argvars, typval_T *rettv);
143static void f_matchstr(typval_T *argvars, typval_T *rettv);
144static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
145static void f_max(typval_T *argvars, typval_T *rettv);
146static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200147#ifdef FEAT_MZSCHEME
148static void f_mzeval(typval_T *argvars, typval_T *rettv);
149#endif
150static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
151static void f_nr2char(typval_T *argvars, typval_T *rettv);
152static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200153#ifdef FEAT_PERL
154static void f_perleval(typval_T *argvars, typval_T *rettv);
155#endif
156#ifdef FEAT_FLOAT
157static void f_pow(typval_T *argvars, typval_T *rettv);
158#endif
159static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
160static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200161static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200162static void f_pumvisible(typval_T *argvars, typval_T *rettv);
163#ifdef FEAT_PYTHON3
164static void f_py3eval(typval_T *argvars, typval_T *rettv);
165#endif
166#ifdef FEAT_PYTHON
167static void f_pyeval(typval_T *argvars, typval_T *rettv);
168#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100169#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
170static void f_pyxeval(typval_T *argvars, typval_T *rettv);
171#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100172static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100173static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200175static void f_reg_executing(typval_T *argvars, typval_T *rettv);
176static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_rename(typval_T *argvars, typval_T *rettv);
178static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200179#ifdef FEAT_FLOAT
180static void f_round(typval_T *argvars, typval_T *rettv);
181#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100182#ifdef FEAT_RUBY
183static void f_rubyeval(typval_T *argvars, typval_T *rettv);
184#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200185static void f_screenattr(typval_T *argvars, typval_T *rettv);
186static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100187static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200188static void f_screencol(typval_T *argvars, typval_T *rettv);
189static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100190static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200191static void f_search(typval_T *argvars, typval_T *rettv);
192static void f_searchdecl(typval_T *argvars, typval_T *rettv);
193static void f_searchpair(typval_T *argvars, typval_T *rettv);
194static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
195static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100196static void f_setcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100198static void f_setcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200199static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100203static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200204#ifdef FEAT_CRYPT
205static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200206#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200207static void f_shellescape(typval_T *argvars, typval_T *rettv);
208static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200209#ifdef FEAT_FLOAT
210static void f_sin(typval_T *argvars, typval_T *rettv);
211static void f_sinh(typval_T *argvars, typval_T *rettv);
212#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200213static void f_soundfold(typval_T *argvars, typval_T *rettv);
214static void f_spellbadword(typval_T *argvars, typval_T *rettv);
215static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
216static void f_split(typval_T *argvars, typval_T *rettv);
217#ifdef FEAT_FLOAT
218static void f_sqrt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100219#endif
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100220static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100221#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200222static void f_str2float(typval_T *argvars, typval_T *rettv);
223#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200224static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200225static void f_str2nr(typval_T *argvars, typval_T *rettv);
226static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200227static void f_strgetchar(typval_T *argvars, typval_T *rettv);
228static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200229static void f_strlen(typval_T *argvars, typval_T *rettv);
230static void f_strcharpart(typval_T *argvars, typval_T *rettv);
231static void f_strpart(typval_T *argvars, typval_T *rettv);
232static void f_strridx(typval_T *argvars, typval_T *rettv);
233static void f_strtrans(typval_T *argvars, typval_T *rettv);
234static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
235static void f_strwidth(typval_T *argvars, typval_T *rettv);
236static void f_submatch(typval_T *argvars, typval_T *rettv);
237static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200238static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200239static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200240static void f_synID(typval_T *argvars, typval_T *rettv);
241static void f_synIDattr(typval_T *argvars, typval_T *rettv);
242static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
243static void f_synstack(typval_T *argvars, typval_T *rettv);
244static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200246static void f_taglist(typval_T *argvars, typval_T *rettv);
247static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200248#ifdef FEAT_FLOAT
249static void f_tan(typval_T *argvars, typval_T *rettv);
250static void f_tanh(typval_T *argvars, typval_T *rettv);
251#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200252static void f_tolower(typval_T *argvars, typval_T *rettv);
253static void f_toupper(typval_T *argvars, typval_T *rettv);
254static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100255static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200256#ifdef FEAT_FLOAT
257static void f_trunc(typval_T *argvars, typval_T *rettv);
258#endif
259static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200260static void f_virtcol(typval_T *argvars, typval_T *rettv);
261static void f_visualmode(typval_T *argvars, typval_T *rettv);
262static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100263static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200264static void f_wordcount(typval_T *argvars, typval_T *rettv);
265static void f_xor(typval_T *argvars, typval_T *rettv);
266
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100267
Bram Moolenaar94738d82020-10-21 14:25:07 +0200268/*
269 * Functions that check the argument type of a builtin function.
270 * Each function returns FAIL and gives an error message if the type is wrong.
271 */
272
273// Context passed to an arg_ function.
274typedef struct {
Bram Moolenaarca174532020-10-21 16:42:22 +0200275 int arg_count; // actual argument count
276 type_T **arg_types; // list of argument types
277 int arg_idx; // current argument index (first arg is zero)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100278 cctx_T *arg_cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +0200279} argcontext_T;
280
281// A function to check one argument type. The first argument is the type to
282// check. If needed, other argument types can be obtained with the context.
283// E.g. if "arg_idx" is 1, then (type - 1) is the first argument type.
284typedef int (*argcheck_T)(type_T *, argcontext_T *);
285
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100286/*
Bram Moolenaar351ead02021-01-16 16:07:01 +0100287 * Call need_type() to check an argument type.
288 */
289 static int
290check_arg_type(
291 type_T *expected,
292 type_T *actual,
293 argcontext_T *context)
294{
295 // TODO: would be useful to know if "actual" is a constant and pass it to
296 // need_type() to get a compile time error if possible.
297 return need_type(actual, expected,
298 context->arg_idx - context->arg_count, context->arg_idx + 1,
299 context->arg_cctx, FALSE, FALSE);
300}
301
302/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100303 * Check "type" is a float or a number.
304 */
Bram Moolenaar94738d82020-10-21 14:25:07 +0200305 static int
306arg_float_or_nr(type_T *type, argcontext_T *context)
307{
Bram Moolenaarca174532020-10-21 16:42:22 +0200308 if (type->tt_type == VAR_ANY
309 || type->tt_type == VAR_FLOAT || type->tt_type == VAR_NUMBER)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200310 return OK;
311 arg_type_mismatch(&t_number, type, context->arg_idx + 1);
312 return FAIL;
313}
314
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100315/*
316 * Check "type" is a number.
317 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200318 static int
319arg_number(type_T *type, argcontext_T *context)
320{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100321 return check_arg_type(&t_number, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200322}
323
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100324/*
325 * Check "type" is a string.
326 */
327 static int
328arg_string(type_T *type, argcontext_T *context)
329{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100330 return check_arg_type(&t_string, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100331}
332
333/*
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100334 * Check "type" is a bool or number 0 or 1.
335 */
336 static int
337arg_bool(type_T *type, argcontext_T *context)
338{
339 if (type->tt_type == VAR_ANY
340 || type->tt_type == VAR_NUMBER || type->tt_type == VAR_BOOL)
341 return OK;
342 return check_arg_type(&t_bool, type, context);
343}
344
345/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100346 * Check "type" is a list or a blob.
347 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200348 static int
349arg_list_or_blob(type_T *type, argcontext_T *context)
350{
351 if (type->tt_type == VAR_ANY
352 || type->tt_type == VAR_LIST || type->tt_type == VAR_BLOB)
353 return OK;
354 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
355 return FAIL;
356}
357
358/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100359 * Check "type" is a list or a dict.
360 */
361 static int
362arg_list_or_dict(type_T *type, argcontext_T *context)
363{
364 if (type->tt_type == VAR_ANY
365 || type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
366 return OK;
367 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
368 return FAIL;
369}
370
371/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100372 * Check "type" is the same type as the previous argument.
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100373 * Must not be used for the first argcheck_T entry.
374 */
375 static int
376arg_same_as_prev(type_T *type, argcontext_T *context)
377{
378 type_T *prev_type = context->arg_types[context->arg_idx - 1];
379
Bram Moolenaar351ead02021-01-16 16:07:01 +0100380 return check_arg_type(prev_type, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100381}
382
383/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100384 * Check "type" is the same basic type as the previous argument, checks list or
385 * dict vs other type, but not member type.
386 * Must not be used for the first argcheck_T entry.
387 */
388 static int
389arg_same_struct_as_prev(type_T *type, argcontext_T *context)
390{
391 type_T *prev_type = context->arg_types[context->arg_idx - 1];
392
393 if (prev_type->tt_type != context->arg_types[context->arg_idx]->tt_type)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100394 return check_arg_type(prev_type, type, context);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100395 return OK;
396}
397
398/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100399 * Check "type" is an item of the list or blob of the previous arg.
Bram Moolenaarca174532020-10-21 16:42:22 +0200400 * Must not be used for the first argcheck_T entry.
401 */
402 static int
403arg_item_of_prev(type_T *type, argcontext_T *context)
404{
405 type_T *prev_type = context->arg_types[context->arg_idx - 1];
406 type_T *expected;
407
408 if (prev_type->tt_type == VAR_LIST)
409 expected = prev_type->tt_member;
410 else if (prev_type->tt_type == VAR_BLOB)
411 expected = &t_number;
412 else
413 // probably VAR_ANY, can't check
414 return OK;
415
Bram Moolenaar351ead02021-01-16 16:07:01 +0100416 return check_arg_type(expected, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200417}
418
Bram Moolenaar94738d82020-10-21 14:25:07 +0200419/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100420 * Check "type" which is the third argument of extend().
421 */
422 static int
423arg_extend3(type_T *type, argcontext_T *context)
424{
425 type_T *first_type = context->arg_types[context->arg_idx - 2];
426
427 if (first_type->tt_type == VAR_LIST)
428 return arg_number(type, context);
429 if (first_type->tt_type == VAR_DICT)
430 return arg_string(type, context);
431 return OK;
432}
433
434
435/*
Bram Moolenaar94738d82020-10-21 14:25:07 +0200436 * Lists of functions that check the argument types of a builtin function.
437 */
Bram Moolenaar80ad3e22021-01-31 20:48:58 +0100438argcheck_T arg1_string[] = {arg_string};
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100439argcheck_T arg3_string_nr_bool[] = {arg_string, arg_number, arg_bool};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200440argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100441argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev};
442argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3};
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100443argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
Bram Moolenaarca174532020-10-21 16:42:22 +0200444argcheck_T arg3_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200445
446/*
447 * Functions that return the return type of a builtin function.
Bram Moolenaara1224cb2020-10-22 12:31:49 +0200448 * Note that "argtypes" is NULL if "argcount" is zero.
Bram Moolenaar94738d82020-10-21 14:25:07 +0200449 */
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100450 static type_T *
451ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
452{
453 return &t_void;
454}
455 static type_T *
456ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
457{
458 return &t_any;
459}
460 static type_T *
Bram Moolenaar403dc312020-10-17 19:29:51 +0200461ret_bool(int argcount UNUSED, type_T **argtypes UNUSED)
462{
463 return &t_bool;
464}
465 static type_T *
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100466ret_number_bool(int argcount UNUSED, type_T **argtypes UNUSED)
467{
468 return &t_number_bool;
469}
470 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100471ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
472{
473 return &t_number;
474}
475 static type_T *
476ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
477{
478 return &t_float;
479}
480 static type_T *
481ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
482{
483 return &t_string;
484}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200485 static type_T *
486ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100487{
488 return &t_list_any;
489}
490 static type_T *
491ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
492{
493 return &t_list_number;
494}
495 static type_T *
496ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
497{
498 return &t_list_string;
499}
500 static type_T *
501ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
502{
503 return &t_list_dict_any;
504}
505 static type_T *
506ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
507{
508 return &t_dict_any;
509}
510 static type_T *
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100511ret_job_info(int argcount, type_T **argtypes UNUSED)
512{
513 if (argcount == 0)
514 return &t_list_job;
515 return &t_dict_any;
516}
517 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100518ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
519{
520 return &t_dict_number;
521}
522 static type_T *
523ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
524{
525 return &t_dict_string;
526}
527 static type_T *
528ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
529{
530 return &t_blob;
531}
532 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200533ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100534{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200535 return &t_func_any;
536}
537 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100538ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
539{
540 return &t_channel;
541}
542 static type_T *
543ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
544{
545 return &t_job;
546}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200547 static type_T *
548ret_first_arg(int argcount, type_T **argtypes)
549{
550 if (argcount > 0)
551 return argtypes[0];
552 return &t_void;
553}
Bram Moolenaarea696852020-11-09 18:31:39 +0100554// for map(): returns first argument but item type may differ
555 static type_T *
556ret_first_cont(int argcount UNUSED, type_T **argtypes)
557{
558 if (argtypes[0]->tt_type == VAR_LIST)
559 return &t_list_any;
560 if (argtypes[0]->tt_type == VAR_DICT)
561 return &t_dict_any;
562 if (argtypes[0]->tt_type == VAR_BLOB)
563 return argtypes[0];
564 return &t_any;
565}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200566
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200567/*
568 * Used for getqflist(): returns list if there is no argument, dict if there is
569 * one.
570 */
571 static type_T *
572ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
573{
574 if (argcount > 0)
575 return &t_dict_any;
576 return &t_list_dict_any;
577}
578
579/*
580 * Used for getloclist(): returns list if there is one argument, dict if there
581 * are two.
582 */
583 static type_T *
584ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
585{
586 if (argcount > 1)
587 return &t_dict_any;
588 return &t_list_dict_any;
589}
590
Bram Moolenaar846178a2020-07-05 17:04:13 +0200591 static type_T *
592ret_argv(int argcount, type_T **argtypes UNUSED)
593{
594 // argv() returns list of strings
595 if (argcount == 0)
596 return &t_list_string;
597
598 // argv(0) returns a string, but argv(-1] returns a list
599 return &t_any;
600}
601
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200602 static type_T *
603ret_remove(int argcount UNUSED, type_T **argtypes)
604{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200605 if (argtypes != NULL)
606 {
607 if (argtypes[0]->tt_type == VAR_LIST
608 || argtypes[0]->tt_type == VAR_DICT)
609 return argtypes[0]->tt_member;
610 if (argtypes[0]->tt_type == VAR_BLOB)
611 return &t_number;
612 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200613 return &t_any;
614}
615
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200616 static type_T *
617ret_getreg(int argcount, type_T **argtypes UNUSED)
618{
619 // Assume that if the third argument is passed it's non-zero
620 if (argcount == 3)
621 return &t_list_string;
622 return &t_string;
623}
624
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200625 static type_T *
626ret_maparg(int argcount, type_T **argtypes UNUSED)
627{
628 // Assume that if the fourth argument is passed it's non-zero
629 if (argcount == 4)
630 return &t_dict_any;
631 return &t_string;
632}
633
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100634static type_T *ret_f_function(int argcount, type_T **argtypes);
635
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636/*
637 * Array with names and number of arguments of all internal functions
638 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
639 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200640typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200641{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200642 char *f_name; // function name
643 char f_min_argc; // minimal number of arguments
644 char f_max_argc; // maximal number of arguments
645 char f_argtype; // for method: FEARG_ values
Bram Moolenaar94738d82020-10-21 14:25:07 +0200646 argcheck_T *f_argcheck; // list of functions to check argument types
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100647 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
648 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200649 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200650 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200651} funcentry_T;
652
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200653// values for f_argtype; zero means it cannot be used as a method
654#define FEARG_1 1 // base is the first argument
655#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200656#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200657#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200658#define FEARG_LAST 9 // base is the last argument
659
Bram Moolenaar15c47602020-03-26 22:16:48 +0100660#ifdef FEAT_FLOAT
661# define FLOAT_FUNC(name) name
662#else
663# define FLOAT_FUNC(name) NULL
664#endif
665#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
666# define MATH_FUNC(name) name
667#else
668# define MATH_FUNC(name) NULL
669#endif
670#ifdef FEAT_TIMERS
671# define TIMER_FUNC(name) name
672#else
673# define TIMER_FUNC(name) NULL
674#endif
675#ifdef FEAT_JOB_CHANNEL
676# define JOB_FUNC(name) name
677#else
678# define JOB_FUNC(name) NULL
679#endif
680#ifdef FEAT_PROP_POPUP
681# define PROP_FUNC(name) name
682#else
683# define PROP_FUNC(name) NULL
684#endif
685#ifdef FEAT_SIGNS
686# define SIGN_FUNC(name) name
687#else
688# define SIGN_FUNC(name) NULL
689#endif
690#ifdef FEAT_SOUND
691# define SOUND_FUNC(name) name
692#else
693# define SOUND_FUNC(name) NULL
694#endif
695#ifdef FEAT_TERMINAL
696# define TERM_FUNC(name) name
697#else
698# define TERM_FUNC(name) NULL
699#endif
700
Bram Moolenaarac92e252019-08-03 21:58:38 +0200701static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200702{
Bram Moolenaar94738d82020-10-21 14:25:07 +0200703 {"abs", 1, 1, FEARG_1, arg1_float_or_nr,
704 ret_any, FLOAT_FUNC(f_abs)},
705 {"acos", 1, 1, FEARG_1, NULL,
706 ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100707 {"add", 2, 2, FEARG_1, NULL /* arg2_listblob_item */,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200708 ret_first_arg, f_add},
709 {"and", 2, 2, FEARG_1, NULL,
710 ret_number, f_and},
711 {"append", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100712 ret_number_bool, f_append},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200713 {"appendbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100714 ret_number_bool, f_appendbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200715 {"argc", 0, 1, 0, NULL,
716 ret_number, f_argc},
717 {"argidx", 0, 0, 0, NULL,
718 ret_number, f_argidx},
719 {"arglistid", 0, 2, 0, NULL,
720 ret_number, f_arglistid},
721 {"argv", 0, 2, 0, NULL,
722 ret_argv, f_argv},
723 {"asin", 1, 1, FEARG_1, NULL,
724 ret_float, FLOAT_FUNC(f_asin)},
725 {"assert_beeps", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100726 ret_number_bool, f_assert_beeps},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200727 {"assert_equal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100728 ret_number_bool, f_assert_equal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200729 {"assert_equalfile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100730 ret_number_bool, f_assert_equalfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200731 {"assert_exception", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100732 ret_number_bool, f_assert_exception},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200733 {"assert_fails", 1, 5, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100734 ret_number_bool, f_assert_fails},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200735 {"assert_false", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100736 ret_number_bool, f_assert_false},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200737 {"assert_inrange", 3, 4, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100738 ret_number_bool, f_assert_inrange},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200739 {"assert_match", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100740 ret_number_bool, f_assert_match},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200741 {"assert_notequal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100742 ret_number_bool, f_assert_notequal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200743 {"assert_notmatch", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100744 ret_number_bool, f_assert_notmatch},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200745 {"assert_report", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100746 ret_number_bool, f_assert_report},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200747 {"assert_true", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100748 ret_number_bool, f_assert_true},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200749 {"atan", 1, 1, FEARG_1, NULL,
750 ret_float, FLOAT_FUNC(f_atan)},
751 {"atan2", 2, 2, FEARG_1, NULL,
752 ret_float, FLOAT_FUNC(f_atan2)},
753 {"balloon_gettext", 0, 0, 0, NULL,
754 ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100755#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100756 f_balloon_gettext
757#else
758 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100759#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100760 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200761 {"balloon_show", 1, 1, FEARG_1, NULL,
762 ret_void,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100763#ifdef FEAT_BEVAL
764 f_balloon_show
765#else
766 NULL
767#endif
768 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200769 {"balloon_split", 1, 1, FEARG_1, NULL,
770 ret_list_string,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100771#if defined(FEAT_BEVAL_TERM)
772 f_balloon_split
773#else
774 NULL
775#endif
776 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200777 {"browse", 4, 4, 0, NULL,
778 ret_string, f_browse},
779 {"browsedir", 2, 2, 0, NULL,
780 ret_string, f_browsedir},
781 {"bufadd", 1, 1, FEARG_1, NULL,
782 ret_number, f_bufadd},
783 {"bufexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100784 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200785 {"buffer_exists", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100786 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200787 {"buffer_name", 0, 1, FEARG_1, NULL, // obsolete
788 ret_string, f_bufname},
789 {"buffer_number", 0, 1, FEARG_1, NULL, // obsolete
790 ret_number, f_bufnr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200791 {"buflisted", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100792 ret_number_bool, f_buflisted},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200793 {"bufload", 1, 1, FEARG_1, NULL,
794 ret_void, f_bufload},
795 {"bufloaded", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100796 ret_number_bool, f_bufloaded},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200797 {"bufname", 0, 1, FEARG_1, NULL,
798 ret_string, f_bufname},
799 {"bufnr", 0, 2, FEARG_1, NULL,
800 ret_number, f_bufnr},
801 {"bufwinid", 1, 1, FEARG_1, NULL,
802 ret_number, f_bufwinid},
803 {"bufwinnr", 1, 1, FEARG_1, NULL,
804 ret_number, f_bufwinnr},
805 {"byte2line", 1, 1, FEARG_1, NULL,
806 ret_number, f_byte2line},
807 {"byteidx", 2, 2, FEARG_1, NULL,
808 ret_number, f_byteidx},
809 {"byteidxcomp", 2, 2, FEARG_1, NULL,
810 ret_number, f_byteidxcomp},
811 {"call", 2, 3, FEARG_1, NULL,
812 ret_any, f_call},
813 {"ceil", 1, 1, FEARG_1, NULL,
814 ret_float, FLOAT_FUNC(f_ceil)},
815 {"ch_canread", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100816 ret_number_bool, JOB_FUNC(f_ch_canread)},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200817 {"ch_close", 1, 1, FEARG_1, NULL,
818 ret_void, JOB_FUNC(f_ch_close)},
819 {"ch_close_in", 1, 1, FEARG_1, NULL,
820 ret_void, JOB_FUNC(f_ch_close_in)},
821 {"ch_evalexpr", 2, 3, FEARG_1, NULL,
822 ret_any, JOB_FUNC(f_ch_evalexpr)},
823 {"ch_evalraw", 2, 3, FEARG_1, NULL,
824 ret_any, JOB_FUNC(f_ch_evalraw)},
825 {"ch_getbufnr", 2, 2, FEARG_1, NULL,
826 ret_number, JOB_FUNC(f_ch_getbufnr)},
827 {"ch_getjob", 1, 1, FEARG_1, NULL,
828 ret_job, JOB_FUNC(f_ch_getjob)},
829 {"ch_info", 1, 1, FEARG_1, NULL,
830 ret_dict_any, JOB_FUNC(f_ch_info)},
831 {"ch_log", 1, 2, FEARG_1, NULL,
832 ret_void, JOB_FUNC(f_ch_log)},
833 {"ch_logfile", 1, 2, FEARG_1, NULL,
834 ret_void, JOB_FUNC(f_ch_logfile)},
835 {"ch_open", 1, 2, FEARG_1, NULL,
836 ret_channel, JOB_FUNC(f_ch_open)},
837 {"ch_read", 1, 2, FEARG_1, NULL,
838 ret_string, JOB_FUNC(f_ch_read)},
839 {"ch_readblob", 1, 2, FEARG_1, NULL,
840 ret_blob, JOB_FUNC(f_ch_readblob)},
841 {"ch_readraw", 1, 2, FEARG_1, NULL,
842 ret_string, JOB_FUNC(f_ch_readraw)},
843 {"ch_sendexpr", 2, 3, FEARG_1, NULL,
844 ret_void, JOB_FUNC(f_ch_sendexpr)},
845 {"ch_sendraw", 2, 3, FEARG_1, NULL,
846 ret_void, JOB_FUNC(f_ch_sendraw)},
847 {"ch_setoptions", 2, 2, FEARG_1, NULL,
848 ret_void, JOB_FUNC(f_ch_setoptions)},
849 {"ch_status", 1, 2, FEARG_1, NULL,
850 ret_string, JOB_FUNC(f_ch_status)},
851 {"changenr", 0, 0, 0, NULL,
852 ret_number, f_changenr},
853 {"char2nr", 1, 2, FEARG_1, NULL,
854 ret_number, f_char2nr},
855 {"charclass", 1, 1, FEARG_1, NULL,
856 ret_number, f_charclass},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100857 {"charcol", 1, 1, FEARG_1, NULL,
858 ret_number, f_charcol},
Bram Moolenaar17793ef2020-12-28 12:56:58 +0100859 {"charidx", 2, 3, FEARG_1, NULL,
860 ret_number, f_charidx},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200861 {"chdir", 1, 1, FEARG_1, NULL,
862 ret_string, f_chdir},
863 {"cindent", 1, 1, FEARG_1, NULL,
864 ret_number, f_cindent},
865 {"clearmatches", 0, 1, FEARG_1, NULL,
866 ret_void, f_clearmatches},
867 {"col", 1, 1, FEARG_1, NULL,
868 ret_number, f_col},
869 {"complete", 2, 2, FEARG_2, NULL,
870 ret_void, f_complete},
871 {"complete_add", 1, 1, FEARG_1, NULL,
872 ret_number, f_complete_add},
873 {"complete_check", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100874 ret_number_bool, f_complete_check},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200875 {"complete_info", 0, 1, FEARG_1, NULL,
876 ret_dict_any, f_complete_info},
877 {"confirm", 1, 4, FEARG_1, NULL,
878 ret_number, f_confirm},
879 {"copy", 1, 1, FEARG_1, NULL,
880 ret_first_arg, f_copy},
881 {"cos", 1, 1, FEARG_1, NULL,
882 ret_float, FLOAT_FUNC(f_cos)},
883 {"cosh", 1, 1, FEARG_1, NULL,
884 ret_float, FLOAT_FUNC(f_cosh)},
885 {"count", 2, 4, FEARG_1, NULL,
886 ret_number, f_count},
887 {"cscope_connection",0,3, 0, NULL,
888 ret_number, f_cscope_connection},
889 {"cursor", 1, 3, FEARG_1, NULL,
890 ret_number, f_cursor},
891 {"debugbreak", 1, 1, FEARG_1, NULL,
892 ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100893#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100894 f_debugbreak
895#else
896 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200897#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100898 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200899 {"deepcopy", 1, 2, FEARG_1, NULL,
900 ret_first_arg, f_deepcopy},
901 {"delete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100902 ret_number_bool, f_delete},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200903 {"deletebufline", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100904 ret_number_bool, f_deletebufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200905 {"did_filetype", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100906 ret_number_bool, f_did_filetype},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200907 {"diff_filler", 1, 1, FEARG_1, NULL,
908 ret_number, f_diff_filler},
909 {"diff_hlID", 2, 2, FEARG_1, NULL,
910 ret_number, f_diff_hlID},
911 {"echoraw", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100912 ret_void, f_echoraw},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200913 {"empty", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100914 ret_number_bool, f_empty},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200915 {"environ", 0, 0, 0, NULL,
916 ret_dict_string, f_environ},
917 {"escape", 2, 2, FEARG_1, NULL,
918 ret_string, f_escape},
919 {"eval", 1, 1, FEARG_1, NULL,
920 ret_any, f_eval},
921 {"eventhandler", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100922 ret_number_bool, f_eventhandler},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200923 {"executable", 1, 1, FEARG_1, NULL,
924 ret_number, f_executable},
925 {"execute", 1, 2, FEARG_1, NULL,
926 ret_string, f_execute},
927 {"exepath", 1, 1, FEARG_1, NULL,
928 ret_string, f_exepath},
929 {"exists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100930 ret_number_bool, f_exists},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200931 {"exp", 1, 1, FEARG_1, NULL,
932 ret_float, FLOAT_FUNC(f_exp)},
933 {"expand", 1, 3, FEARG_1, NULL,
934 ret_any, f_expand},
935 {"expandcmd", 1, 1, FEARG_1, NULL,
936 ret_string, f_expandcmd},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100937 {"extend", 2, 3, FEARG_1, arg23_extend,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200938 ret_first_arg, f_extend},
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100939 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
940 ret_first_cont, f_extendnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200941 {"feedkeys", 1, 2, FEARG_1, NULL,
942 ret_void, f_feedkeys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200943 {"file_readable", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100944 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200945 {"filereadable", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100946 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200947 {"filewritable", 1, 1, FEARG_1, NULL,
948 ret_number, f_filewritable},
949 {"filter", 2, 2, FEARG_1, NULL,
950 ret_first_arg, f_filter},
951 {"finddir", 1, 3, FEARG_1, NULL,
952 ret_string, f_finddir},
953 {"findfile", 1, 3, FEARG_1, NULL,
954 ret_string, f_findfile},
955 {"flatten", 1, 2, FEARG_1, NULL,
956 ret_list_any, f_flatten},
Bram Moolenaar3b690062021-02-01 20:14:51 +0100957 {"flattennew", 1, 2, FEARG_1, NULL,
958 ret_list_any, f_flattennew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200959 {"float2nr", 1, 1, FEARG_1, NULL,
960 ret_number, FLOAT_FUNC(f_float2nr)},
961 {"floor", 1, 1, FEARG_1, NULL,
962 ret_float, FLOAT_FUNC(f_floor)},
963 {"fmod", 2, 2, FEARG_1, NULL,
964 ret_float, FLOAT_FUNC(f_fmod)},
965 {"fnameescape", 1, 1, FEARG_1, NULL,
966 ret_string, f_fnameescape},
967 {"fnamemodify", 2, 2, FEARG_1, NULL,
968 ret_string, f_fnamemodify},
969 {"foldclosed", 1, 1, FEARG_1, NULL,
970 ret_number, f_foldclosed},
971 {"foldclosedend", 1, 1, FEARG_1, NULL,
972 ret_number, f_foldclosedend},
973 {"foldlevel", 1, 1, FEARG_1, NULL,
974 ret_number, f_foldlevel},
975 {"foldtext", 0, 0, 0, NULL,
976 ret_string, f_foldtext},
977 {"foldtextresult", 1, 1, FEARG_1, NULL,
978 ret_string, f_foldtextresult},
979 {"foreground", 0, 0, 0, NULL,
980 ret_void, f_foreground},
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100981 {"fullcommand", 1, 1, FEARG_1, arg1_string,
982 ret_string, f_fullcommand},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200983 {"funcref", 1, 3, FEARG_1, NULL,
984 ret_func_any, f_funcref},
985 {"function", 1, 3, FEARG_1, NULL,
986 ret_f_function, f_function},
987 {"garbagecollect", 0, 1, 0, NULL,
988 ret_void, f_garbagecollect},
989 {"get", 2, 3, FEARG_1, NULL,
990 ret_any, f_get},
991 {"getbufinfo", 0, 1, FEARG_1, NULL,
992 ret_list_dict_any, f_getbufinfo},
993 {"getbufline", 2, 3, FEARG_1, NULL,
994 ret_list_string, f_getbufline},
995 {"getbufvar", 2, 3, FEARG_1, NULL,
996 ret_any, f_getbufvar},
997 {"getchangelist", 0, 1, FEARG_1, NULL,
998 ret_list_any, f_getchangelist},
999 {"getchar", 0, 1, 0, NULL,
1000 ret_number, f_getchar},
1001 {"getcharmod", 0, 0, 0, NULL,
1002 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001003 {"getcharpos", 1, 1, FEARG_1, NULL,
1004 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001005 {"getcharsearch", 0, 0, 0, NULL,
1006 ret_dict_any, f_getcharsearch},
1007 {"getcmdline", 0, 0, 0, NULL,
1008 ret_string, f_getcmdline},
1009 {"getcmdpos", 0, 0, 0, NULL,
1010 ret_number, f_getcmdpos},
1011 {"getcmdtype", 0, 0, 0, NULL,
1012 ret_string, f_getcmdtype},
1013 {"getcmdwintype", 0, 0, 0, NULL,
1014 ret_string, f_getcmdwintype},
1015 {"getcompletion", 2, 3, FEARG_1, NULL,
1016 ret_list_string, f_getcompletion},
1017 {"getcurpos", 0, 1, FEARG_1, NULL,
1018 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001019 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
1020 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001021 {"getcwd", 0, 2, FEARG_1, NULL,
1022 ret_string, f_getcwd},
1023 {"getenv", 1, 1, FEARG_1, NULL,
Bram Moolenaar7ad67d12021-03-10 16:08:26 +01001024 ret_any, f_getenv},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001025 {"getfontname", 0, 1, 0, NULL,
1026 ret_string, f_getfontname},
1027 {"getfperm", 1, 1, FEARG_1, NULL,
1028 ret_string, f_getfperm},
1029 {"getfsize", 1, 1, FEARG_1, NULL,
1030 ret_number, f_getfsize},
1031 {"getftime", 1, 1, FEARG_1, NULL,
1032 ret_number, f_getftime},
1033 {"getftype", 1, 1, FEARG_1, NULL,
1034 ret_string, f_getftype},
1035 {"getimstatus", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001036 ret_number_bool, f_getimstatus},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001037 {"getjumplist", 0, 2, FEARG_1, NULL,
1038 ret_list_any, f_getjumplist},
1039 {"getline", 1, 2, FEARG_1, NULL,
1040 ret_f_getline, f_getline},
1041 {"getloclist", 1, 2, 0, NULL,
1042 ret_list_or_dict_1, f_getloclist},
1043 {"getmarklist", 0, 1, FEARG_1, NULL,
1044 ret_list_dict_any, f_getmarklist},
1045 {"getmatches", 0, 1, 0, NULL,
1046 ret_list_dict_any, f_getmatches},
1047 {"getmousepos", 0, 0, 0, NULL,
1048 ret_dict_number, f_getmousepos},
1049 {"getpid", 0, 0, 0, NULL,
1050 ret_number, f_getpid},
1051 {"getpos", 1, 1, FEARG_1, NULL,
1052 ret_list_number, f_getpos},
1053 {"getqflist", 0, 1, 0, NULL,
1054 ret_list_or_dict_0, f_getqflist},
1055 {"getreg", 0, 3, FEARG_1, NULL,
1056 ret_getreg, f_getreg},
1057 {"getreginfo", 0, 1, FEARG_1, NULL,
1058 ret_dict_any, f_getreginfo},
1059 {"getregtype", 0, 1, FEARG_1, NULL,
1060 ret_string, f_getregtype},
1061 {"gettabinfo", 0, 1, FEARG_1, NULL,
1062 ret_list_dict_any, f_gettabinfo},
1063 {"gettabvar", 2, 3, FEARG_1, NULL,
1064 ret_any, f_gettabvar},
1065 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1066 ret_any, f_gettabwinvar},
1067 {"gettagstack", 0, 1, FEARG_1, NULL,
1068 ret_dict_any, f_gettagstack},
1069 {"gettext", 1, 1, FEARG_1, NULL,
1070 ret_string, f_gettext},
1071 {"getwininfo", 0, 1, FEARG_1, NULL,
1072 ret_list_dict_any, f_getwininfo},
1073 {"getwinpos", 0, 1, FEARG_1, NULL,
1074 ret_list_number, f_getwinpos},
1075 {"getwinposx", 0, 0, 0, NULL,
1076 ret_number, f_getwinposx},
1077 {"getwinposy", 0, 0, 0, NULL,
1078 ret_number, f_getwinposy},
1079 {"getwinvar", 2, 3, FEARG_1, NULL,
1080 ret_any, f_getwinvar},
1081 {"glob", 1, 4, FEARG_1, NULL,
1082 ret_any, f_glob},
1083 {"glob2regpat", 1, 1, FEARG_1, NULL,
1084 ret_string, f_glob2regpat},
1085 {"globpath", 2, 5, FEARG_2, NULL,
1086 ret_any, f_globpath},
1087 {"has", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001088 ret_number_bool, f_has},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001089 {"has_key", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001090 ret_number_bool, f_has_key},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001091 {"haslocaldir", 0, 2, FEARG_1, NULL,
1092 ret_number, f_haslocaldir},
1093 {"hasmapto", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001094 ret_number_bool, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001095 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1096 ret_number, f_hlID},
1097 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001098 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001099 {"histadd", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001100 ret_number_bool, f_histadd},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001101 {"histdel", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001102 ret_number_bool, f_histdel},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001103 {"histget", 1, 2, FEARG_1, NULL,
1104 ret_string, f_histget},
1105 {"histnr", 1, 1, FEARG_1, NULL,
1106 ret_number, f_histnr},
1107 {"hlID", 1, 1, FEARG_1, NULL,
1108 ret_number, f_hlID},
1109 {"hlexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001110 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001111 {"hostname", 0, 0, 0, NULL,
1112 ret_string, f_hostname},
1113 {"iconv", 3, 3, FEARG_1, NULL,
1114 ret_string, f_iconv},
1115 {"indent", 1, 1, FEARG_1, NULL,
1116 ret_number, f_indent},
1117 {"index", 2, 4, FEARG_1, NULL,
1118 ret_number, f_index},
1119 {"input", 1, 3, FEARG_1, NULL,
1120 ret_string, f_input},
1121 {"inputdialog", 1, 3, FEARG_1, NULL,
1122 ret_string, f_inputdialog},
1123 {"inputlist", 1, 1, FEARG_1, NULL,
1124 ret_number, f_inputlist},
1125 {"inputrestore", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001126 ret_number_bool, f_inputrestore},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001127 {"inputsave", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001128 ret_number_bool, f_inputsave},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001129 {"inputsecret", 1, 2, FEARG_1, NULL,
1130 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001131 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001132 ret_first_arg, f_insert},
1133 {"interrupt", 0, 0, 0, NULL,
1134 ret_void, f_interrupt},
1135 {"invert", 1, 1, FEARG_1, NULL,
1136 ret_number, f_invert},
1137 {"isdirectory", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001138 ret_number_bool, f_isdirectory},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001139 {"isinf", 1, 1, FEARG_1, NULL,
1140 ret_number, MATH_FUNC(f_isinf)},
1141 {"islocked", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001142 ret_number_bool, f_islocked},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001143 {"isnan", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001144 ret_number_bool, MATH_FUNC(f_isnan)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001145 {"items", 1, 1, FEARG_1, NULL,
1146 ret_list_any, f_items},
1147 {"job_getchannel", 1, 1, FEARG_1, NULL,
1148 ret_channel, JOB_FUNC(f_job_getchannel)},
1149 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001150 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001151 {"job_setoptions", 2, 2, FEARG_1, NULL,
1152 ret_void, JOB_FUNC(f_job_setoptions)},
1153 {"job_start", 1, 2, FEARG_1, NULL,
1154 ret_job, JOB_FUNC(f_job_start)},
1155 {"job_status", 1, 1, FEARG_1, NULL,
1156 ret_string, JOB_FUNC(f_job_status)},
1157 {"job_stop", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001158 ret_number_bool, JOB_FUNC(f_job_stop)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001159 {"join", 1, 2, FEARG_1, NULL,
1160 ret_string, f_join},
1161 {"js_decode", 1, 1, FEARG_1, NULL,
1162 ret_any, f_js_decode},
1163 {"js_encode", 1, 1, FEARG_1, NULL,
1164 ret_string, f_js_encode},
1165 {"json_decode", 1, 1, FEARG_1, NULL,
1166 ret_any, f_json_decode},
1167 {"json_encode", 1, 1, FEARG_1, NULL,
1168 ret_string, f_json_encode},
1169 {"keys", 1, 1, FEARG_1, NULL,
1170 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001171 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1172 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001173 {"len", 1, 1, FEARG_1, NULL,
1174 ret_number, f_len},
1175 {"libcall", 3, 3, FEARG_3, NULL,
1176 ret_string, f_libcall},
1177 {"libcallnr", 3, 3, FEARG_3, NULL,
1178 ret_number, f_libcallnr},
1179 {"line", 1, 2, FEARG_1, NULL,
1180 ret_number, f_line},
1181 {"line2byte", 1, 1, FEARG_1, NULL,
1182 ret_number, f_line2byte},
1183 {"lispindent", 1, 1, FEARG_1, NULL,
1184 ret_number, f_lispindent},
1185 {"list2str", 1, 2, FEARG_1, NULL,
1186 ret_string, f_list2str},
1187 {"listener_add", 1, 2, FEARG_2, NULL,
1188 ret_number, f_listener_add},
1189 {"listener_flush", 0, 1, FEARG_1, NULL,
1190 ret_void, f_listener_flush},
1191 {"listener_remove", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001192 ret_number_bool, f_listener_remove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001193 {"localtime", 0, 0, 0, NULL,
1194 ret_number, f_localtime},
1195 {"log", 1, 1, FEARG_1, NULL,
1196 ret_float, FLOAT_FUNC(f_log)},
1197 {"log10", 1, 1, FEARG_1, NULL,
1198 ret_float, FLOAT_FUNC(f_log10)},
1199 {"luaeval", 1, 2, FEARG_1, NULL,
1200 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001201#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001202 f_luaeval
1203#else
1204 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001205#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001206 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001207 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001208 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001209 {"maparg", 1, 4, FEARG_1, NULL,
1210 ret_maparg, f_maparg},
1211 {"mapcheck", 1, 3, FEARG_1, NULL,
1212 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001213 {"mapnew", 2, 2, FEARG_1, NULL,
1214 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001215 {"mapset", 3, 3, FEARG_1, NULL,
1216 ret_void, f_mapset},
1217 {"match", 2, 4, FEARG_1, NULL,
1218 ret_any, f_match},
1219 {"matchadd", 2, 5, FEARG_1, NULL,
1220 ret_number, f_matchadd},
1221 {"matchaddpos", 2, 5, FEARG_1, NULL,
1222 ret_number, f_matchaddpos},
1223 {"matcharg", 1, 1, FEARG_1, NULL,
1224 ret_list_string, f_matcharg},
1225 {"matchdelete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001226 ret_number_bool, f_matchdelete},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001227 {"matchend", 2, 4, FEARG_1, NULL,
1228 ret_number, f_matchend},
1229 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1230 ret_list_string, f_matchfuzzy},
1231 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1232 ret_list_any, f_matchfuzzypos},
1233 {"matchlist", 2, 4, FEARG_1, NULL,
1234 ret_list_string, f_matchlist},
1235 {"matchstr", 2, 4, FEARG_1, NULL,
1236 ret_string, f_matchstr},
1237 {"matchstrpos", 2, 4, FEARG_1, NULL,
1238 ret_list_any, f_matchstrpos},
1239 {"max", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001240 ret_number, f_max},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001241 {"menu_info", 1, 2, FEARG_1, NULL,
1242 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001243#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001244 f_menu_info
1245#else
1246 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001247#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001248 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001249 {"min", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001250 ret_number, f_min},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001251 {"mkdir", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001252 ret_number_bool, f_mkdir},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001253 {"mode", 0, 1, FEARG_1, NULL,
1254 ret_string, f_mode},
1255 {"mzeval", 1, 1, FEARG_1, NULL,
1256 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001257#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001258 f_mzeval
1259#else
1260 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001261#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001262 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001263 {"nextnonblank", 1, 1, FEARG_1, NULL,
1264 ret_number, f_nextnonblank},
1265 {"nr2char", 1, 2, FEARG_1, NULL,
1266 ret_string, f_nr2char},
1267 {"or", 2, 2, FEARG_1, NULL,
1268 ret_number, f_or},
1269 {"pathshorten", 1, 2, FEARG_1, NULL,
1270 ret_string, f_pathshorten},
1271 {"perleval", 1, 1, FEARG_1, NULL,
1272 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001273#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001274 f_perleval
1275#else
1276 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001277#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001278 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001279 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1280 ret_number, PROP_FUNC(f_popup_atcursor)},
1281 {"popup_beval", 2, 2, FEARG_1, NULL,
1282 ret_number, PROP_FUNC(f_popup_beval)},
1283 {"popup_clear", 0, 1, 0, NULL,
1284 ret_void, PROP_FUNC(f_popup_clear)},
1285 {"popup_close", 1, 2, FEARG_1, NULL,
1286 ret_void, PROP_FUNC(f_popup_close)},
1287 {"popup_create", 2, 2, FEARG_1, NULL,
1288 ret_number, PROP_FUNC(f_popup_create)},
1289 {"popup_dialog", 2, 2, FEARG_1, NULL,
1290 ret_number, PROP_FUNC(f_popup_dialog)},
1291 {"popup_filter_menu", 2, 2, 0, NULL,
1292 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1293 {"popup_filter_yesno", 2, 2, 0, NULL,
1294 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1295 {"popup_findinfo", 0, 0, 0, NULL,
1296 ret_number, PROP_FUNC(f_popup_findinfo)},
1297 {"popup_findpreview", 0, 0, 0, NULL,
1298 ret_number, PROP_FUNC(f_popup_findpreview)},
1299 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1300 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1301 {"popup_getpos", 1, 1, FEARG_1, NULL,
1302 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1303 {"popup_hide", 1, 1, FEARG_1, NULL,
1304 ret_void, PROP_FUNC(f_popup_hide)},
1305 {"popup_list", 0, 0, 0, NULL,
1306 ret_list_number, PROP_FUNC(f_popup_list)},
1307 {"popup_locate", 2, 2, 0, NULL,
1308 ret_number, PROP_FUNC(f_popup_locate)},
1309 {"popup_menu", 2, 2, FEARG_1, NULL,
1310 ret_number, PROP_FUNC(f_popup_menu)},
1311 {"popup_move", 2, 2, FEARG_1, NULL,
1312 ret_void, PROP_FUNC(f_popup_move)},
1313 {"popup_notification", 2, 2, FEARG_1, NULL,
1314 ret_number, PROP_FUNC(f_popup_notification)},
1315 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1316 ret_void, PROP_FUNC(f_popup_setoptions)},
1317 {"popup_settext", 2, 2, FEARG_1, NULL,
1318 ret_void, PROP_FUNC(f_popup_settext)},
1319 {"popup_show", 1, 1, FEARG_1, NULL,
1320 ret_void, PROP_FUNC(f_popup_show)},
1321 {"pow", 2, 2, FEARG_1, NULL,
1322 ret_float, FLOAT_FUNC(f_pow)},
1323 {"prevnonblank", 1, 1, FEARG_1, NULL,
1324 ret_number, f_prevnonblank},
1325 {"printf", 1, 19, FEARG_2, NULL,
1326 ret_string, f_printf},
1327 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1328 ret_string, JOB_FUNC(f_prompt_getprompt)},
1329 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1330 ret_void, JOB_FUNC(f_prompt_setcallback)},
1331 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1332 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1333 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1334 ret_void, JOB_FUNC(f_prompt_setprompt)},
1335 {"prop_add", 3, 3, FEARG_1, NULL,
1336 ret_void, PROP_FUNC(f_prop_add)},
1337 {"prop_clear", 1, 3, FEARG_1, NULL,
1338 ret_void, PROP_FUNC(f_prop_clear)},
1339 {"prop_find", 1, 2, FEARG_1, NULL,
1340 ret_dict_any, PROP_FUNC(f_prop_find)},
1341 {"prop_list", 1, 2, FEARG_1, NULL,
1342 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1343 {"prop_remove", 1, 3, FEARG_1, NULL,
1344 ret_number, PROP_FUNC(f_prop_remove)},
1345 {"prop_type_add", 2, 2, FEARG_1, NULL,
1346 ret_void, PROP_FUNC(f_prop_type_add)},
1347 {"prop_type_change", 2, 2, FEARG_1, NULL,
1348 ret_void, PROP_FUNC(f_prop_type_change)},
1349 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1350 ret_void, PROP_FUNC(f_prop_type_delete)},
1351 {"prop_type_get", 1, 2, FEARG_1, NULL,
1352 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1353 {"prop_type_list", 0, 1, FEARG_1, NULL,
1354 ret_list_string, PROP_FUNC(f_prop_type_list)},
1355 {"pum_getpos", 0, 0, 0, NULL,
1356 ret_dict_number, f_pum_getpos},
1357 {"pumvisible", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001358 ret_number_bool, f_pumvisible},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001359 {"py3eval", 1, 1, FEARG_1, NULL,
1360 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001361#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001362 f_py3eval
1363#else
1364 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001365#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001366 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001367 {"pyeval", 1, 1, FEARG_1, NULL,
1368 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001369#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001370 f_pyeval
1371#else
1372 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001373#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001374 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001375 {"pyxeval", 1, 1, FEARG_1, NULL,
1376 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001377#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001378 f_pyxeval
1379#else
1380 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001381#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001382 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001383 {"rand", 0, 1, FEARG_1, NULL,
1384 ret_number, f_rand},
1385 {"range", 1, 3, FEARG_1, NULL,
1386 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001387 {"readblob", 1, 1, FEARG_1, NULL,
1388 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001389 {"readdir", 1, 3, FEARG_1, NULL,
1390 ret_list_string, f_readdir},
1391 {"readdirex", 1, 3, FEARG_1, NULL,
1392 ret_list_dict_any, f_readdirex},
1393 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001394 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001395 {"reduce", 2, 3, FEARG_1, NULL,
1396 ret_any, f_reduce},
1397 {"reg_executing", 0, 0, 0, NULL,
1398 ret_string, f_reg_executing},
1399 {"reg_recording", 0, 0, 0, NULL,
1400 ret_string, f_reg_recording},
1401 {"reltime", 0, 2, FEARG_1, NULL,
1402 ret_list_any, f_reltime},
1403 {"reltimefloat", 1, 1, FEARG_1, NULL,
1404 ret_float, FLOAT_FUNC(f_reltimefloat)},
1405 {"reltimestr", 1, 1, FEARG_1, NULL,
1406 ret_string, f_reltimestr},
1407 {"remote_expr", 2, 4, FEARG_1, NULL,
1408 ret_string, f_remote_expr},
1409 {"remote_foreground", 1, 1, FEARG_1, NULL,
1410 ret_string, f_remote_foreground},
1411 {"remote_peek", 1, 2, FEARG_1, NULL,
1412 ret_number, f_remote_peek},
1413 {"remote_read", 1, 2, FEARG_1, NULL,
1414 ret_string, f_remote_read},
1415 {"remote_send", 2, 3, FEARG_1, NULL,
1416 ret_string, f_remote_send},
1417 {"remote_startserver", 1, 1, FEARG_1, NULL,
1418 ret_void, f_remote_startserver},
1419 {"remove", 2, 3, FEARG_1, NULL,
1420 ret_remove, f_remove},
1421 {"rename", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001422 ret_number_bool, f_rename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001423 {"repeat", 2, 2, FEARG_1, NULL,
1424 ret_first_arg, f_repeat},
1425 {"resolve", 1, 1, FEARG_1, NULL,
1426 ret_string, f_resolve},
1427 {"reverse", 1, 1, FEARG_1, NULL,
1428 ret_first_arg, f_reverse},
1429 {"round", 1, 1, FEARG_1, NULL,
1430 ret_float, FLOAT_FUNC(f_round)},
1431 {"rubyeval", 1, 1, FEARG_1, NULL,
1432 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001433#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001434 f_rubyeval
1435#else
1436 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001437#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001438 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001439 {"screenattr", 2, 2, FEARG_1, NULL,
1440 ret_number, f_screenattr},
1441 {"screenchar", 2, 2, FEARG_1, NULL,
1442 ret_number, f_screenchar},
1443 {"screenchars", 2, 2, FEARG_1, NULL,
1444 ret_list_number, f_screenchars},
1445 {"screencol", 0, 0, 0, NULL,
1446 ret_number, f_screencol},
1447 {"screenpos", 3, 3, FEARG_1, NULL,
1448 ret_dict_number, f_screenpos},
1449 {"screenrow", 0, 0, 0, NULL,
1450 ret_number, f_screenrow},
1451 {"screenstring", 2, 2, FEARG_1, NULL,
1452 ret_string, f_screenstring},
1453 {"search", 1, 5, FEARG_1, NULL,
1454 ret_number, f_search},
1455 {"searchcount", 0, 1, FEARG_1, NULL,
1456 ret_dict_any, f_searchcount},
1457 {"searchdecl", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001458 ret_number_bool, f_searchdecl},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001459 {"searchpair", 3, 7, 0, NULL,
1460 ret_number, f_searchpair},
1461 {"searchpairpos", 3, 7, 0, NULL,
1462 ret_list_number, f_searchpairpos},
1463 {"searchpos", 1, 5, FEARG_1, NULL,
1464 ret_list_number, f_searchpos},
1465 {"server2client", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001466 ret_number_bool, f_server2client},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001467 {"serverlist", 0, 0, 0, NULL,
1468 ret_string, f_serverlist},
1469 {"setbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001470 ret_number_bool, f_setbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001471 {"setbufvar", 3, 3, FEARG_3, NULL,
1472 ret_void, f_setbufvar},
1473 {"setcellwidths", 1, 1, FEARG_1, NULL,
1474 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001475 {"setcharpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001476 ret_number_bool, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001477 {"setcharsearch", 1, 1, FEARG_1, NULL,
1478 ret_void, f_setcharsearch},
1479 {"setcmdpos", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001480 ret_number_bool, f_setcmdpos},
1481 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1482 ret_number_bool, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001483 {"setenv", 2, 2, FEARG_2, NULL,
1484 ret_void, f_setenv},
1485 {"setfperm", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001486 ret_number_bool, f_setfperm},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001487 {"setline", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001488 ret_number_bool, f_setline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001489 {"setloclist", 2, 4, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001490 ret_number_bool, f_setloclist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001491 {"setmatches", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001492 ret_number_bool, f_setmatches},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001493 {"setpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001494 ret_number_bool, f_setpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001495 {"setqflist", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001496 ret_number_bool, f_setqflist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001497 {"setreg", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001498 ret_number_bool, f_setreg},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001499 {"settabvar", 3, 3, FEARG_3, NULL,
1500 ret_void, f_settabvar},
1501 {"settabwinvar", 4, 4, FEARG_4, NULL,
1502 ret_void, f_settabwinvar},
1503 {"settagstack", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001504 ret_number_bool, f_settagstack},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001505 {"setwinvar", 3, 3, FEARG_3, NULL,
1506 ret_void, f_setwinvar},
1507 {"sha256", 1, 1, FEARG_1, NULL,
1508 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001509#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001510 f_sha256
1511#else
1512 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001513#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001514 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001515 {"shellescape", 1, 2, FEARG_1, NULL,
1516 ret_string, f_shellescape},
1517 {"shiftwidth", 0, 1, FEARG_1, NULL,
1518 ret_number, f_shiftwidth},
1519 {"sign_define", 1, 2, FEARG_1, NULL,
1520 ret_any, SIGN_FUNC(f_sign_define)},
1521 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1522 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1523 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1524 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1525 {"sign_jump", 3, 3, FEARG_1, NULL,
1526 ret_number, SIGN_FUNC(f_sign_jump)},
1527 {"sign_place", 4, 5, FEARG_1, NULL,
1528 ret_number, SIGN_FUNC(f_sign_place)},
1529 {"sign_placelist", 1, 1, FEARG_1, NULL,
1530 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1531 {"sign_undefine", 0, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001532 ret_number_bool, SIGN_FUNC(f_sign_undefine)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001533 {"sign_unplace", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001534 ret_number_bool, SIGN_FUNC(f_sign_unplace)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001535 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1536 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1537 {"simplify", 1, 1, FEARG_1, NULL,
1538 ret_string, f_simplify},
1539 {"sin", 1, 1, FEARG_1, NULL,
1540 ret_float, FLOAT_FUNC(f_sin)},
1541 {"sinh", 1, 1, FEARG_1, NULL,
1542 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001543 {"slice", 2, 3, FEARG_1, NULL,
1544 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001545 {"sort", 1, 3, FEARG_1, NULL,
1546 ret_first_arg, f_sort},
1547 {"sound_clear", 0, 0, 0, NULL,
1548 ret_void, SOUND_FUNC(f_sound_clear)},
1549 {"sound_playevent", 1, 2, FEARG_1, NULL,
1550 ret_number, SOUND_FUNC(f_sound_playevent)},
1551 {"sound_playfile", 1, 2, FEARG_1, NULL,
1552 ret_number, SOUND_FUNC(f_sound_playfile)},
1553 {"sound_stop", 1, 1, FEARG_1, NULL,
1554 ret_void, SOUND_FUNC(f_sound_stop)},
1555 {"soundfold", 1, 1, FEARG_1, NULL,
1556 ret_string, f_soundfold},
1557 {"spellbadword", 0, 1, FEARG_1, NULL,
1558 ret_list_string, f_spellbadword},
1559 {"spellsuggest", 1, 3, FEARG_1, NULL,
1560 ret_list_string, f_spellsuggest},
1561 {"split", 1, 3, FEARG_1, NULL,
1562 ret_list_string, f_split},
1563 {"sqrt", 1, 1, FEARG_1, NULL,
1564 ret_float, FLOAT_FUNC(f_sqrt)},
1565 {"srand", 0, 1, FEARG_1, NULL,
1566 ret_list_number, f_srand},
1567 {"state", 0, 1, FEARG_1, NULL,
1568 ret_string, f_state},
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001569 {"str2float", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001570 ret_float, FLOAT_FUNC(f_str2float)},
1571 {"str2list", 1, 2, FEARG_1, NULL,
1572 ret_list_number, f_str2list},
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001573 {"str2nr", 1, 3, FEARG_1, arg3_string_nr_bool,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001574 ret_number, f_str2nr},
1575 {"strcharpart", 2, 3, FEARG_1, NULL,
1576 ret_string, f_strcharpart},
1577 {"strchars", 1, 2, FEARG_1, NULL,
1578 ret_number, f_strchars},
1579 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1580 ret_number, f_strdisplaywidth},
1581 {"strftime", 1, 2, FEARG_1, NULL,
1582 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001583#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001584 f_strftime
1585#else
1586 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001587#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001588 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001589 {"strgetchar", 2, 2, FEARG_1, NULL,
1590 ret_number, f_strgetchar},
1591 {"stridx", 2, 3, FEARG_1, NULL,
1592 ret_number, f_stridx},
1593 {"string", 1, 1, FEARG_1, NULL,
1594 ret_string, f_string},
1595 {"strlen", 1, 1, FEARG_1, NULL,
1596 ret_number, f_strlen},
1597 {"strpart", 2, 4, FEARG_1, NULL,
1598 ret_string, f_strpart},
1599 {"strptime", 2, 2, FEARG_1, NULL,
1600 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001601#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001602 f_strptime
1603#else
1604 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001605#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001606 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001607 {"strridx", 2, 3, FEARG_1, NULL,
1608 ret_number, f_strridx},
1609 {"strtrans", 1, 1, FEARG_1, NULL,
1610 ret_string, f_strtrans},
1611 {"strwidth", 1, 1, FEARG_1, NULL,
1612 ret_number, f_strwidth},
1613 {"submatch", 1, 2, FEARG_1, NULL,
1614 ret_string, f_submatch},
1615 {"substitute", 4, 4, FEARG_1, NULL,
1616 ret_string, f_substitute},
1617 {"swapinfo", 1, 1, FEARG_1, NULL,
1618 ret_dict_any, f_swapinfo},
1619 {"swapname", 1, 1, FEARG_1, NULL,
1620 ret_string, f_swapname},
1621 {"synID", 3, 3, 0, NULL,
1622 ret_number, f_synID},
1623 {"synIDattr", 2, 3, FEARG_1, NULL,
1624 ret_string, f_synIDattr},
1625 {"synIDtrans", 1, 1, FEARG_1, NULL,
1626 ret_number, f_synIDtrans},
1627 {"synconcealed", 2, 2, 0, NULL,
1628 ret_list_any, f_synconcealed},
1629 {"synstack", 2, 2, 0, NULL,
1630 ret_list_number, f_synstack},
1631 {"system", 1, 2, FEARG_1, NULL,
1632 ret_string, f_system},
1633 {"systemlist", 1, 2, FEARG_1, NULL,
1634 ret_list_string, f_systemlist},
1635 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1636 ret_list_number, f_tabpagebuflist},
1637 {"tabpagenr", 0, 1, 0, NULL,
1638 ret_number, f_tabpagenr},
1639 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1640 ret_number, f_tabpagewinnr},
1641 {"tagfiles", 0, 0, 0, NULL,
1642 ret_list_string, f_tagfiles},
1643 {"taglist", 1, 2, FEARG_1, NULL,
1644 ret_list_dict_any, f_taglist},
1645 {"tan", 1, 1, FEARG_1, NULL,
1646 ret_float, FLOAT_FUNC(f_tan)},
1647 {"tanh", 1, 1, FEARG_1, NULL,
1648 ret_float, FLOAT_FUNC(f_tanh)},
1649 {"tempname", 0, 0, 0, NULL,
1650 ret_string, f_tempname},
1651 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1652 ret_number, TERM_FUNC(f_term_dumpdiff)},
1653 {"term_dumpload", 1, 2, FEARG_1, NULL,
1654 ret_number, TERM_FUNC(f_term_dumpload)},
1655 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1656 ret_void, TERM_FUNC(f_term_dumpwrite)},
1657 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1658 ret_number, TERM_FUNC(f_term_getaltscreen)},
1659 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1660 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001661#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001662 f_term_getansicolors
1663#else
1664 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001665#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001666 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001667 {"term_getattr", 2, 2, FEARG_1, NULL,
1668 ret_number, TERM_FUNC(f_term_getattr)},
1669 {"term_getcursor", 1, 1, FEARG_1, NULL,
1670 ret_list_any, TERM_FUNC(f_term_getcursor)},
1671 {"term_getjob", 1, 1, FEARG_1, NULL,
1672 ret_job, TERM_FUNC(f_term_getjob)},
1673 {"term_getline", 2, 2, FEARG_1, NULL,
1674 ret_string, TERM_FUNC(f_term_getline)},
1675 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1676 ret_number, TERM_FUNC(f_term_getscrolled)},
1677 {"term_getsize", 1, 1, FEARG_1, NULL,
1678 ret_list_number, TERM_FUNC(f_term_getsize)},
1679 {"term_getstatus", 1, 1, FEARG_1, NULL,
1680 ret_string, TERM_FUNC(f_term_getstatus)},
1681 {"term_gettitle", 1, 1, FEARG_1, NULL,
1682 ret_string, TERM_FUNC(f_term_gettitle)},
1683 {"term_gettty", 1, 2, FEARG_1, NULL,
1684 ret_string, TERM_FUNC(f_term_gettty)},
1685 {"term_list", 0, 0, 0, NULL,
1686 ret_list_number, TERM_FUNC(f_term_list)},
1687 {"term_scrape", 2, 2, FEARG_1, NULL,
1688 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1689 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1690 ret_void, TERM_FUNC(f_term_sendkeys)},
1691 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1692 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001693#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001694 f_term_setansicolors
1695#else
1696 NULL
1697#endif
1698 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001699 {"term_setapi", 2, 2, FEARG_1, NULL,
1700 ret_void, TERM_FUNC(f_term_setapi)},
1701 {"term_setkill", 2, 2, FEARG_1, NULL,
1702 ret_void, TERM_FUNC(f_term_setkill)},
1703 {"term_setrestore", 2, 2, FEARG_1, NULL,
1704 ret_void, TERM_FUNC(f_term_setrestore)},
1705 {"term_setsize", 3, 3, FEARG_1, NULL,
1706 ret_void, TERM_FUNC(f_term_setsize)},
1707 {"term_start", 1, 2, FEARG_1, NULL,
1708 ret_number, TERM_FUNC(f_term_start)},
1709 {"term_wait", 1, 2, FEARG_1, NULL,
1710 ret_void, TERM_FUNC(f_term_wait)},
1711 {"terminalprops", 0, 0, 0, NULL,
1712 ret_dict_string, f_terminalprops},
1713 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1714 ret_void, f_test_alloc_fail},
1715 {"test_autochdir", 0, 0, 0, NULL,
1716 ret_void, f_test_autochdir},
1717 {"test_feedinput", 1, 1, FEARG_1, NULL,
1718 ret_void, f_test_feedinput},
1719 {"test_garbagecollect_now", 0, 0, 0, NULL,
1720 ret_void, f_test_garbagecollect_now},
1721 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1722 ret_void, f_test_garbagecollect_soon},
1723 {"test_getvalue", 1, 1, FEARG_1, NULL,
1724 ret_number, f_test_getvalue},
1725 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1726 ret_void, f_test_ignore_error},
1727 {"test_null_blob", 0, 0, 0, NULL,
1728 ret_blob, f_test_null_blob},
1729 {"test_null_channel", 0, 0, 0, NULL,
1730 ret_channel, JOB_FUNC(f_test_null_channel)},
1731 {"test_null_dict", 0, 0, 0, NULL,
1732 ret_dict_any, f_test_null_dict},
1733 {"test_null_function", 0, 0, 0, NULL,
1734 ret_func_any, f_test_null_function},
1735 {"test_null_job", 0, 0, 0, NULL,
1736 ret_job, JOB_FUNC(f_test_null_job)},
1737 {"test_null_list", 0, 0, 0, NULL,
1738 ret_list_any, f_test_null_list},
1739 {"test_null_partial", 0, 0, 0, NULL,
1740 ret_func_any, f_test_null_partial},
1741 {"test_null_string", 0, 0, 0, NULL,
1742 ret_string, f_test_null_string},
1743 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1744 ret_void, f_test_option_not_set},
1745 {"test_override", 2, 2, FEARG_2, NULL,
1746 ret_void, f_test_override},
1747 {"test_refcount", 1, 1, FEARG_1, NULL,
1748 ret_number, f_test_refcount},
1749 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1750 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001751#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001752 f_test_scrollbar
1753#else
1754 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001755#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001756 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001757 {"test_setmouse", 2, 2, 0, NULL,
1758 ret_void, f_test_setmouse},
1759 {"test_settime", 1, 1, FEARG_1, NULL,
1760 ret_void, f_test_settime},
1761 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1762 ret_void, f_test_srand_seed},
1763 {"test_unknown", 0, 0, 0, NULL,
1764 ret_any, f_test_unknown},
1765 {"test_void", 0, 0, 0, NULL,
1766 ret_void, f_test_void},
1767 {"timer_info", 0, 1, FEARG_1, NULL,
1768 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1769 {"timer_pause", 2, 2, FEARG_1, NULL,
1770 ret_void, TIMER_FUNC(f_timer_pause)},
1771 {"timer_start", 2, 3, FEARG_1, NULL,
1772 ret_number, TIMER_FUNC(f_timer_start)},
1773 {"timer_stop", 1, 1, FEARG_1, NULL,
1774 ret_void, TIMER_FUNC(f_timer_stop)},
1775 {"timer_stopall", 0, 0, 0, NULL,
1776 ret_void, TIMER_FUNC(f_timer_stopall)},
1777 {"tolower", 1, 1, FEARG_1, NULL,
1778 ret_string, f_tolower},
1779 {"toupper", 1, 1, FEARG_1, NULL,
1780 ret_string, f_toupper},
1781 {"tr", 3, 3, FEARG_1, NULL,
1782 ret_string, f_tr},
1783 {"trim", 1, 3, FEARG_1, NULL,
1784 ret_string, f_trim},
1785 {"trunc", 1, 1, FEARG_1, NULL,
1786 ret_float, FLOAT_FUNC(f_trunc)},
1787 {"type", 1, 1, FEARG_1, NULL,
1788 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001789 {"typename", 1, 1, FEARG_1, NULL,
1790 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001791 {"undofile", 1, 1, FEARG_1, NULL,
1792 ret_string, f_undofile},
1793 {"undotree", 0, 0, 0, NULL,
1794 ret_dict_any, f_undotree},
1795 {"uniq", 1, 3, FEARG_1, NULL,
1796 ret_list_any, f_uniq},
1797 {"values", 1, 1, FEARG_1, NULL,
1798 ret_list_any, f_values},
1799 {"virtcol", 1, 1, FEARG_1, NULL,
1800 ret_number, f_virtcol},
1801 {"visualmode", 0, 1, 0, NULL,
1802 ret_string, f_visualmode},
1803 {"wildmenumode", 0, 0, 0, NULL,
1804 ret_number, f_wildmenumode},
1805 {"win_execute", 2, 3, FEARG_2, NULL,
1806 ret_string, f_win_execute},
1807 {"win_findbuf", 1, 1, FEARG_1, NULL,
1808 ret_list_number, f_win_findbuf},
1809 {"win_getid", 0, 2, FEARG_1, NULL,
1810 ret_number, f_win_getid},
1811 {"win_gettype", 0, 1, FEARG_1, NULL,
1812 ret_string, f_win_gettype},
1813 {"win_gotoid", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001814 ret_number_bool, f_win_gotoid},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001815 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1816 ret_list_number, f_win_id2tabwin},
1817 {"win_id2win", 1, 1, FEARG_1, NULL,
1818 ret_number, f_win_id2win},
1819 {"win_screenpos", 1, 1, FEARG_1, NULL,
1820 ret_list_number, f_win_screenpos},
1821 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001822 ret_number_bool, f_win_splitmove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001823 {"winbufnr", 1, 1, FEARG_1, NULL,
1824 ret_number, f_winbufnr},
1825 {"wincol", 0, 0, 0, NULL,
1826 ret_number, f_wincol},
1827 {"windowsversion", 0, 0, 0, NULL,
1828 ret_string, f_windowsversion},
1829 {"winheight", 1, 1, FEARG_1, NULL,
1830 ret_number, f_winheight},
1831 {"winlayout", 0, 1, FEARG_1, NULL,
1832 ret_list_any, f_winlayout},
1833 {"winline", 0, 0, 0, NULL,
1834 ret_number, f_winline},
1835 {"winnr", 0, 1, FEARG_1, NULL,
1836 ret_number, f_winnr},
1837 {"winrestcmd", 0, 0, 0, NULL,
1838 ret_string, f_winrestcmd},
1839 {"winrestview", 1, 1, FEARG_1, NULL,
1840 ret_void, f_winrestview},
1841 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001842 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001843 {"winwidth", 1, 1, FEARG_1, NULL,
1844 ret_number, f_winwidth},
1845 {"wordcount", 0, 0, 0, NULL,
1846 ret_dict_number, f_wordcount},
1847 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001848 ret_number_bool, f_writefile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001849 {"xor", 2, 2, FEARG_1, NULL,
1850 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001851};
1852
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001853/*
1854 * Function given to ExpandGeneric() to obtain the list of internal
1855 * or user defined function names.
1856 */
1857 char_u *
1858get_function_name(expand_T *xp, int idx)
1859{
1860 static int intidx = -1;
1861 char_u *name;
1862
1863 if (idx == 0)
1864 intidx = -1;
1865 if (intidx < 0)
1866 {
1867 name = get_user_func_name(xp, idx);
1868 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001869 {
1870 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1871 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001872 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001873 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001874 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001875 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001876 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001877 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001878 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001879 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001880 STRCAT(IObuff, ")");
1881 return IObuff;
1882 }
1883
1884 return NULL;
1885}
1886
1887/*
1888 * Function given to ExpandGeneric() to obtain the list of internal or
1889 * user defined variable or function names.
1890 */
1891 char_u *
1892get_expr_name(expand_T *xp, int idx)
1893{
1894 static int intidx = -1;
1895 char_u *name;
1896
1897 if (idx == 0)
1898 intidx = -1;
1899 if (intidx < 0)
1900 {
1901 name = get_function_name(xp, idx);
1902 if (name != NULL)
1903 return name;
1904 }
1905 return get_user_var_name(xp, ++intidx);
1906}
1907
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001908/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001909 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001910 * Return index, or -1 if not found or "implemented" is TRUE and the function
1911 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001912 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001913 static int
1914find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001915{
1916 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001917 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001918 int cmp;
1919 int x;
1920
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001921 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001922
1923 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001924 while (first <= last)
1925 {
1926 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001927 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001928 if (cmp < 0)
1929 last = x - 1;
1930 else if (cmp > 0)
1931 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001932 else if (implemented && global_functions[x].f_func == NULL)
1933 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001934 else
1935 return x;
1936 }
1937 return -1;
1938}
1939
Bram Moolenaar15c47602020-03-26 22:16:48 +01001940/*
1941 * Find internal function "name" in table "global_functions".
1942 * Return index, or -1 if not found or the function is not implemented.
1943 */
1944 int
1945find_internal_func(char_u *name)
1946{
1947 return find_internal_func_opt(name, TRUE);
1948}
1949
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001950 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001951has_internal_func(char_u *name)
1952{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001953 return find_internal_func_opt(name, TRUE) >= 0;
1954}
1955
1956 static int
1957has_internal_func_name(char_u *name)
1958{
1959 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001960}
1961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962 char *
1963internal_func_name(int idx)
1964{
1965 return global_functions[idx].f_name;
1966}
1967
Bram Moolenaar94738d82020-10-21 14:25:07 +02001968/*
1969 * Check the argument types for builting function "idx".
1970 * Uses the list of types on the type stack: "types".
1971 * Return FAIL and gives an error message when a type is wrong.
1972 */
1973 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001974internal_func_check_arg_types(
1975 type_T **types,
1976 int idx,
1977 int argcount,
1978 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001979{
1980 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1981 int i;
1982
1983 if (argchecks != NULL)
1984 {
1985 argcontext_T context;
1986
1987 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001988 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001989 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001990 for (i = 0; i < argcount; ++i)
1991 if (argchecks[i] != NULL)
1992 {
1993 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001994 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001995 return FAIL;
1996 }
1997 }
1998 return OK;
1999}
2000
Bram Moolenaara1224cb2020-10-22 12:31:49 +02002001/*
2002 * Call the "f_retfunc" function to obtain the return type of function "idx".
2003 * "argtypes" is the list of argument types or NULL when there are no
2004 * arguments.
2005 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002007internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002009 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010}
2011
2012/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002013 * Return TRUE if "idx" is for the map() function.
2014 */
2015 int
2016internal_func_is_map(int idx)
2017{
2018 return global_functions[idx].f_func == f_map;
2019}
2020
2021/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002023 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2024 * first argument, 2 if method base is second argument, etc. 9 if method base
2025 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 */
2027 int
2028check_internal_func(int idx, int argcount)
2029{
2030 int res;
2031 char *name;
2032
2033 if (argcount < global_functions[idx].f_min_argc)
2034 res = FCERR_TOOFEW;
2035 else if (argcount > global_functions[idx].f_max_argc)
2036 res = FCERR_TOOMANY;
2037 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002038 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039
2040 name = internal_func_name(idx);
2041 if (res == FCERR_TOOMANY)
2042 semsg(_(e_toomanyarg), name);
2043 else
2044 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002045 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046}
2047
Bram Moolenaarac92e252019-08-03 21:58:38 +02002048 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002049call_internal_func(
2050 char_u *name,
2051 int argcount,
2052 typval_T *argvars,
2053 typval_T *rettv)
2054{
2055 int i;
2056
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002057 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002058 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002059 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002060 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002061 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002062 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002063 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002064 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002065 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002066 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002067}
2068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069 void
2070call_internal_func_by_idx(
2071 int idx,
2072 typval_T *argvars,
2073 typval_T *rettv)
2074{
2075 global_functions[idx].f_func(argvars, rettv);
2076}
2077
Bram Moolenaarac92e252019-08-03 21:58:38 +02002078/*
2079 * Invoke a method for base->method().
2080 */
2081 int
2082call_internal_method(
2083 char_u *name,
2084 int argcount,
2085 typval_T *argvars,
2086 typval_T *rettv,
2087 typval_T *basetv)
2088{
2089 int i;
2090 int fi;
2091 typval_T argv[MAX_FUNC_ARGS + 1];
2092
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002093 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002094 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002095 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002096 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002097 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002098 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002099 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002100 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002101 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002102
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002103 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002104 {
2105 // base value goes last
2106 for (i = 0; i < argcount; ++i)
2107 argv[i] = argvars[i];
2108 argv[argcount] = *basetv;
2109 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002110 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002111 {
2112 // base value goes second
2113 argv[0] = argvars[0];
2114 argv[1] = *basetv;
2115 for (i = 1; i < argcount; ++i)
2116 argv[i + 1] = argvars[i];
2117 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002118 else if (global_functions[fi].f_argtype == FEARG_3)
2119 {
2120 // base value goes third
2121 argv[0] = argvars[0];
2122 argv[1] = argvars[1];
2123 argv[2] = *basetv;
2124 for (i = 2; i < argcount; ++i)
2125 argv[i + 1] = argvars[i];
2126 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002127 else if (global_functions[fi].f_argtype == FEARG_4)
2128 {
2129 // base value goes fourth
2130 argv[0] = argvars[0];
2131 argv[1] = argvars[1];
2132 argv[2] = argvars[2];
2133 argv[3] = *basetv;
2134 for (i = 3; i < argcount; ++i)
2135 argv[i + 1] = argvars[i];
2136 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002137 else
2138 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002139 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002140 argv[0] = *basetv;
2141 for (i = 0; i < argcount; ++i)
2142 argv[i + 1] = argvars[i];
2143 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002144 argv[argcount + 1].v_type = VAR_UNKNOWN;
2145
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002146 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002147 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002148}
2149
2150/*
2151 * Return TRUE for a non-zero Number and a non-empty String.
2152 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002153 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154non_zero_arg(typval_T *argvars)
2155{
2156 return ((argvars[0].v_type == VAR_NUMBER
2157 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002158 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002159 && argvars[0].vval.v_number == VVAL_TRUE)
2160 || (argvars[0].v_type == VAR_STRING
2161 && argvars[0].vval.v_string != NULL
2162 && *argvars[0].vval.v_string != NUL));
2163}
2164
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002165#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002166/*
2167 * Get the float value of "argvars[0]" into "f".
2168 * Returns FAIL when the argument is not a Number or Float.
2169 */
2170 static int
2171get_float_arg(typval_T *argvars, float_T *f)
2172{
2173 if (argvars[0].v_type == VAR_FLOAT)
2174 {
2175 *f = argvars[0].vval.v_float;
2176 return OK;
2177 }
2178 if (argvars[0].v_type == VAR_NUMBER)
2179 {
2180 *f = (float_T)argvars[0].vval.v_number;
2181 return OK;
2182 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002183 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002184 return FAIL;
2185}
2186
2187/*
2188 * "abs(expr)" function
2189 */
2190 static void
2191f_abs(typval_T *argvars, typval_T *rettv)
2192{
2193 if (argvars[0].v_type == VAR_FLOAT)
2194 {
2195 rettv->v_type = VAR_FLOAT;
2196 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
2197 }
2198 else
2199 {
2200 varnumber_T n;
2201 int error = FALSE;
2202
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002203 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002204 if (error)
2205 rettv->vval.v_number = -1;
2206 else if (n > 0)
2207 rettv->vval.v_number = n;
2208 else
2209 rettv->vval.v_number = -n;
2210 }
2211}
2212
2213/*
2214 * "acos()" function
2215 */
2216 static void
2217f_acos(typval_T *argvars, typval_T *rettv)
2218{
2219 float_T f = 0.0;
2220
2221 rettv->v_type = VAR_FLOAT;
2222 if (get_float_arg(argvars, &f) == OK)
2223 rettv->vval.v_float = acos(f);
2224 else
2225 rettv->vval.v_float = 0.0;
2226}
2227#endif
2228
2229/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002230 * "and(expr, expr)" function
2231 */
2232 static void
2233f_and(typval_T *argvars, typval_T *rettv)
2234{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002235 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2236 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002237}
2238
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002239#ifdef FEAT_FLOAT
2240/*
2241 * "asin()" function
2242 */
2243 static void
2244f_asin(typval_T *argvars, typval_T *rettv)
2245{
2246 float_T f = 0.0;
2247
2248 rettv->v_type = VAR_FLOAT;
2249 if (get_float_arg(argvars, &f) == OK)
2250 rettv->vval.v_float = asin(f);
2251 else
2252 rettv->vval.v_float = 0.0;
2253}
2254
2255/*
2256 * "atan()" function
2257 */
2258 static void
2259f_atan(typval_T *argvars, typval_T *rettv)
2260{
2261 float_T f = 0.0;
2262
2263 rettv->v_type = VAR_FLOAT;
2264 if (get_float_arg(argvars, &f) == OK)
2265 rettv->vval.v_float = atan(f);
2266 else
2267 rettv->vval.v_float = 0.0;
2268}
2269
2270/*
2271 * "atan2()" function
2272 */
2273 static void
2274f_atan2(typval_T *argvars, typval_T *rettv)
2275{
2276 float_T fx = 0.0, fy = 0.0;
2277
2278 rettv->v_type = VAR_FLOAT;
2279 if (get_float_arg(argvars, &fx) == OK
2280 && get_float_arg(&argvars[1], &fy) == OK)
2281 rettv->vval.v_float = atan2(fx, fy);
2282 else
2283 rettv->vval.v_float = 0.0;
2284}
2285#endif
2286
2287/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002288 * "balloon_show()" function
2289 */
2290#ifdef FEAT_BEVAL
2291 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002292f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2293{
2294 rettv->v_type = VAR_STRING;
2295 if (balloonEval != NULL)
2296 {
2297 if (balloonEval->msg == NULL)
2298 rettv->vval.v_string = NULL;
2299 else
2300 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2301 }
2302}
2303
2304 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002305f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2306{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002307 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002308 {
2309 if (argvars[0].v_type == VAR_LIST
2310# ifdef FEAT_GUI
2311 && !gui.in_use
2312# endif
2313 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002314 {
2315 list_T *l = argvars[0].vval.v_list;
2316
2317 // empty list removes the balloon
2318 post_balloon(balloonEval, NULL,
2319 l == NULL || l->lv_len == 0 ? NULL : l);
2320 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002321 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002322 {
2323 char_u *mesg = tv_get_string_chk(&argvars[0]);
2324
2325 if (mesg != NULL)
2326 // empty string removes the balloon
2327 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2328 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002329 }
2330}
2331
Bram Moolenaar669a8282017-11-19 20:13:05 +01002332# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002333 static void
2334f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2335{
2336 if (rettv_list_alloc(rettv) == OK)
2337 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002338 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002339
2340 if (msg != NULL)
2341 {
2342 pumitem_T *array;
2343 int size = split_message(msg, &array);
2344 int i;
2345
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002346 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002347 for (i = 1; i < size - 1; ++i)
2348 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002349 while (size > 0)
2350 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002351 vim_free(array);
2352 }
2353 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002354}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002355# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002356#endif
2357
2358/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002359 * Get the buffer from "arg" and give an error and return NULL if it is not
2360 * valid.
2361 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002362 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002363get_buf_arg(typval_T *arg)
2364{
2365 buf_T *buf;
2366
2367 ++emsg_off;
2368 buf = tv_get_buf(arg, FALSE);
2369 --emsg_off;
2370 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002371 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002372 return buf;
2373}
2374
2375/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002376 * "byte2line(byte)" function
2377 */
2378 static void
2379f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2380{
2381#ifndef FEAT_BYTEOFF
2382 rettv->vval.v_number = -1;
2383#else
2384 long boff = 0;
2385
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002386 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002387 if (boff < 0)
2388 rettv->vval.v_number = -1;
2389 else
2390 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2391 (linenr_T)0, &boff);
2392#endif
2393}
2394
2395 static void
2396byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2397{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002398 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002399 char_u *str;
2400 varnumber_T idx;
2401
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002402 str = tv_get_string_chk(&argvars[0]);
2403 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002404 rettv->vval.v_number = -1;
2405 if (str == NULL || idx < 0)
2406 return;
2407
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002408 t = str;
2409 for ( ; idx > 0; idx--)
2410 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002411 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002412 return;
2413 if (enc_utf8 && comp)
2414 t += utf_ptr2len(t);
2415 else
2416 t += (*mb_ptr2len)(t);
2417 }
2418 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002419}
2420
2421/*
2422 * "byteidx()" function
2423 */
2424 static void
2425f_byteidx(typval_T *argvars, typval_T *rettv)
2426{
2427 byteidx(argvars, rettv, FALSE);
2428}
2429
2430/*
2431 * "byteidxcomp()" function
2432 */
2433 static void
2434f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2435{
2436 byteidx(argvars, rettv, TRUE);
2437}
2438
2439/*
2440 * "call(func, arglist [, dict])" function
2441 */
2442 static void
2443f_call(typval_T *argvars, typval_T *rettv)
2444{
2445 char_u *func;
2446 partial_T *partial = NULL;
2447 dict_T *selfdict = NULL;
2448
2449 if (argvars[1].v_type != VAR_LIST)
2450 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002451 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002452 return;
2453 }
2454 if (argvars[1].vval.v_list == NULL)
2455 return;
2456
2457 if (argvars[0].v_type == VAR_FUNC)
2458 func = argvars[0].vval.v_string;
2459 else if (argvars[0].v_type == VAR_PARTIAL)
2460 {
2461 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002462 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002463 }
2464 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002465 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002466 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002467 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002468
2469 if (argvars[2].v_type != VAR_UNKNOWN)
2470 {
2471 if (argvars[2].v_type != VAR_DICT)
2472 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002473 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002474 return;
2475 }
2476 selfdict = argvars[2].vval.v_dict;
2477 }
2478
2479 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2480}
2481
2482#ifdef FEAT_FLOAT
2483/*
2484 * "ceil({float})" function
2485 */
2486 static void
2487f_ceil(typval_T *argvars, typval_T *rettv)
2488{
2489 float_T f = 0.0;
2490
2491 rettv->v_type = VAR_FLOAT;
2492 if (get_float_arg(argvars, &f) == OK)
2493 rettv->vval.v_float = ceil(f);
2494 else
2495 rettv->vval.v_float = 0.0;
2496}
2497#endif
2498
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002499/*
2500 * "changenr()" function
2501 */
2502 static void
2503f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2504{
2505 rettv->vval.v_number = curbuf->b_u_seq_cur;
2506}
2507
2508/*
2509 * "char2nr(string)" function
2510 */
2511 static void
2512f_char2nr(typval_T *argvars, typval_T *rettv)
2513{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002514 if (has_mbyte)
2515 {
2516 int utf8 = 0;
2517
2518 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002519 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002520
2521 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002522 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002523 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002524 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002525 }
2526 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002527 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002528}
2529
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002530/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002531 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2532 * returns the character index of the column. Otherwise, returns the byte index
2533 * of the column.
2534 */
2535 static void
2536get_col(typval_T *argvars, typval_T *rettv, int charcol)
2537{
2538 colnr_T col = 0;
2539 pos_T *fp;
2540 int fnum = curbuf->b_fnum;
2541
2542 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2543 if (fp != NULL && fnum == curbuf->b_fnum)
2544 {
2545 if (fp->col == MAXCOL)
2546 {
2547 // '> can be MAXCOL, get the length of the line then
2548 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2549 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2550 else
2551 col = MAXCOL;
2552 }
2553 else
2554 {
2555 col = fp->col + 1;
2556 // col(".") when the cursor is on the NUL at the end of the line
2557 // because of "coladd" can be seen as an extra column.
2558 if (virtual_active() && fp == &curwin->w_cursor)
2559 {
2560 char_u *p = ml_get_cursor();
2561
2562 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2563 curwin->w_virtcol - curwin->w_cursor.coladd))
2564 {
2565 int l;
2566
2567 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2568 col += l;
2569 }
2570 }
2571 }
2572 }
2573 rettv->vval.v_number = col;
2574}
2575
2576/*
2577 * "charcol()" function
2578 */
2579 static void
2580f_charcol(typval_T *argvars, typval_T *rettv)
2581{
2582 get_col(argvars, rettv, TRUE);
2583}
2584
2585/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002586 * "charidx()" function
2587 */
2588 static void
2589f_charidx(typval_T *argvars, typval_T *rettv)
2590{
2591 char_u *str;
2592 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002593 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002594 char_u *p;
2595 int len;
2596 int (*ptr2len)(char_u *);
2597
2598 rettv->vval.v_number = -1;
2599
2600 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2601 || (argvars[2].v_type != VAR_UNKNOWN
2602 && argvars[2].v_type != VAR_NUMBER))
2603 {
2604 emsg(_(e_invarg));
2605 return;
2606 }
2607
2608 str = tv_get_string_chk(&argvars[0]);
2609 idx = tv_get_number_chk(&argvars[1], NULL);
2610 if (str == NULL || idx < 0)
2611 return;
2612
2613 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002614 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002615 if (countcc < 0 || countcc > 1)
2616 {
2617 semsg(_(e_using_number_as_bool_nr), countcc);
2618 return;
2619 }
2620
2621 if (enc_utf8 && countcc)
2622 ptr2len = utf_ptr2len;
2623 else
2624 ptr2len = mb_ptr2len;
2625
2626 for (p = str, len = 0; p <= str + idx; len++)
2627 {
2628 if (*p == NUL)
2629 return;
2630 p += ptr2len(p);
2631 }
2632
2633 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2634}
2635
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002636 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002637get_optional_window(typval_T *argvars, int idx)
2638{
2639 win_T *win = curwin;
2640
2641 if (argvars[idx].v_type != VAR_UNKNOWN)
2642 {
2643 win = find_win_by_nr_or_id(&argvars[idx]);
2644 if (win == NULL)
2645 {
2646 emsg(_(e_invalwindow));
2647 return NULL;
2648 }
2649 }
2650 return win;
2651}
2652
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002653/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002654 * "col(string)" function
2655 */
2656 static void
2657f_col(typval_T *argvars, typval_T *rettv)
2658{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002659 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002660}
2661
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002662/*
2663 * "confirm(message, buttons[, default [, type]])" function
2664 */
2665 static void
2666f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2667{
2668#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2669 char_u *message;
2670 char_u *buttons = NULL;
2671 char_u buf[NUMBUFLEN];
2672 char_u buf2[NUMBUFLEN];
2673 int def = 1;
2674 int type = VIM_GENERIC;
2675 char_u *typestr;
2676 int error = FALSE;
2677
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002678 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002679 if (message == NULL)
2680 error = TRUE;
2681 if (argvars[1].v_type != VAR_UNKNOWN)
2682 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002683 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002684 if (buttons == NULL)
2685 error = TRUE;
2686 if (argvars[2].v_type != VAR_UNKNOWN)
2687 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002688 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002689 if (argvars[3].v_type != VAR_UNKNOWN)
2690 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002691 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002692 if (typestr == NULL)
2693 error = TRUE;
2694 else
2695 {
2696 switch (TOUPPER_ASC(*typestr))
2697 {
2698 case 'E': type = VIM_ERROR; break;
2699 case 'Q': type = VIM_QUESTION; break;
2700 case 'I': type = VIM_INFO; break;
2701 case 'W': type = VIM_WARNING; break;
2702 case 'G': type = VIM_GENERIC; break;
2703 }
2704 }
2705 }
2706 }
2707 }
2708
2709 if (buttons == NULL || *buttons == NUL)
2710 buttons = (char_u *)_("&Ok");
2711
2712 if (!error)
2713 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2714 def, NULL, FALSE);
2715#endif
2716}
2717
2718/*
2719 * "copy()" function
2720 */
2721 static void
2722f_copy(typval_T *argvars, typval_T *rettv)
2723{
2724 item_copy(&argvars[0], rettv, FALSE, 0);
2725}
2726
2727#ifdef FEAT_FLOAT
2728/*
2729 * "cos()" function
2730 */
2731 static void
2732f_cos(typval_T *argvars, typval_T *rettv)
2733{
2734 float_T f = 0.0;
2735
2736 rettv->v_type = VAR_FLOAT;
2737 if (get_float_arg(argvars, &f) == OK)
2738 rettv->vval.v_float = cos(f);
2739 else
2740 rettv->vval.v_float = 0.0;
2741}
2742
2743/*
2744 * "cosh()" function
2745 */
2746 static void
2747f_cosh(typval_T *argvars, typval_T *rettv)
2748{
2749 float_T f = 0.0;
2750
2751 rettv->v_type = VAR_FLOAT;
2752 if (get_float_arg(argvars, &f) == OK)
2753 rettv->vval.v_float = cosh(f);
2754 else
2755 rettv->vval.v_float = 0.0;
2756}
2757#endif
2758
2759/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002760 * Set the cursor position.
2761 * If 'charcol' is TRUE, then use the column number as a character offet.
2762 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002763 */
2764 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002765set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002766{
2767 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002768 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002769 int set_curswant = TRUE;
2770
2771 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002772 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002773 {
2774 pos_T pos;
2775 colnr_T curswant = -1;
2776
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002777 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002778 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002779 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002780 return;
2781 }
2782 line = pos.lnum;
2783 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002784 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002785 if (curswant >= 0)
2786 {
2787 curwin->w_curswant = curswant - 1;
2788 set_curswant = FALSE;
2789 }
2790 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002791 else if ((argvars[0].v_type == VAR_NUMBER ||
2792 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002793 && (argvars[1].v_type == VAR_NUMBER ||
2794 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002795 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002796 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002797 if (line < 0)
2798 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002799 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002800 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002801 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002802 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002803 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002804 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002805 else
2806 {
2807 emsg(_(e_invarg));
2808 return;
2809 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002810 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002811 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002812 if (line > 0)
2813 curwin->w_cursor.lnum = line;
2814 if (col > 0)
2815 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002816 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002817
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002818 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002819 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002820 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002821 if (has_mbyte)
2822 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002823
2824 curwin->w_set_curswant = set_curswant;
2825 rettv->vval.v_number = 0;
2826}
2827
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002828/*
2829 * "cursor(lnum, col)" function, or
2830 * "cursor(list)"
2831 *
2832 * Moves the cursor to the specified line and column.
2833 * Returns 0 when the position could be set, -1 otherwise.
2834 */
2835 static void
2836f_cursor(typval_T *argvars, typval_T *rettv)
2837{
2838 set_cursorpos(argvars, rettv, FALSE);
2839}
2840
Bram Moolenaar4f974752019-02-17 17:44:42 +01002841#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002842/*
2843 * "debugbreak()" function
2844 */
2845 static void
2846f_debugbreak(typval_T *argvars, typval_T *rettv)
2847{
2848 int pid;
2849
2850 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002851 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002852 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002853 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002854 else
2855 {
2856 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2857
2858 if (hProcess != NULL)
2859 {
2860 DebugBreakProcess(hProcess);
2861 CloseHandle(hProcess);
2862 rettv->vval.v_number = OK;
2863 }
2864 }
2865}
2866#endif
2867
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002868/*
2869 * "deepcopy()" function
2870 */
2871 static void
2872f_deepcopy(typval_T *argvars, typval_T *rettv)
2873{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002874 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002875 int copyID;
2876
2877 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002878 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002879 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002880 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002881 else
2882 {
2883 copyID = get_copyID();
2884 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2885 }
2886}
2887
2888/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002889 * "did_filetype()" function
2890 */
2891 static void
2892f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2893{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002894 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002895}
2896
2897/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002898 * "echoraw({expr})" function
2899 */
2900 static void
2901f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2902{
2903 char_u *str = tv_get_string_chk(&argvars[0]);
2904
2905 if (str != NULL && *str != NUL)
2906 {
2907 out_str(str);
2908 out_flush();
2909 }
2910}
2911
2912/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002913 * "empty({expr})" function
2914 */
2915 static void
2916f_empty(typval_T *argvars, typval_T *rettv)
2917{
2918 int n = FALSE;
2919
2920 switch (argvars[0].v_type)
2921 {
2922 case VAR_STRING:
2923 case VAR_FUNC:
2924 n = argvars[0].vval.v_string == NULL
2925 || *argvars[0].vval.v_string == NUL;
2926 break;
2927 case VAR_PARTIAL:
2928 n = FALSE;
2929 break;
2930 case VAR_NUMBER:
2931 n = argvars[0].vval.v_number == 0;
2932 break;
2933 case VAR_FLOAT:
2934#ifdef FEAT_FLOAT
2935 n = argvars[0].vval.v_float == 0.0;
2936 break;
2937#endif
2938 case VAR_LIST:
2939 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002940 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002941 break;
2942 case VAR_DICT:
2943 n = argvars[0].vval.v_dict == NULL
2944 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2945 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002946 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002947 case VAR_SPECIAL:
2948 n = argvars[0].vval.v_number != VVAL_TRUE;
2949 break;
2950
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002951 case VAR_BLOB:
2952 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002953 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2954 break;
2955
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002956 case VAR_JOB:
2957#ifdef FEAT_JOB_CHANNEL
2958 n = argvars[0].vval.v_job == NULL
2959 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2960 break;
2961#endif
2962 case VAR_CHANNEL:
2963#ifdef FEAT_JOB_CHANNEL
2964 n = argvars[0].vval.v_channel == NULL
2965 || !channel_is_open(argvars[0].vval.v_channel);
2966 break;
2967#endif
2968 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002969 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002971 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002972 n = TRUE;
2973 break;
2974 }
2975
2976 rettv->vval.v_number = n;
2977}
2978
2979/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002980 * "environ()" function
2981 */
2982 static void
2983f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2984{
2985#if !defined(AMIGA)
2986 int i = 0;
2987 char_u *entry, *value;
2988# ifdef MSWIN
2989 extern wchar_t **_wenviron;
2990# else
2991 extern char **environ;
2992# endif
2993
2994 if (rettv_dict_alloc(rettv) != OK)
2995 return;
2996
2997# ifdef MSWIN
2998 if (*_wenviron == NULL)
2999 return;
3000# else
3001 if (*environ == NULL)
3002 return;
3003# endif
3004
3005 for (i = 0; ; ++i)
3006 {
3007# ifdef MSWIN
3008 short_u *p;
3009
3010 if ((p = (short_u *)_wenviron[i]) == NULL)
3011 return;
3012 entry = utf16_to_enc(p, NULL);
3013# else
3014 if ((entry = (char_u *)environ[i]) == NULL)
3015 return;
3016 entry = vim_strsave(entry);
3017# endif
3018 if (entry == NULL) // out of memory
3019 return;
3020 if ((value = vim_strchr(entry, '=')) == NULL)
3021 {
3022 vim_free(entry);
3023 continue;
3024 }
3025 *value++ = NUL;
3026 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3027 vim_free(entry);
3028 }
3029#endif
3030}
3031
3032/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033 * "escape({string}, {chars})" function
3034 */
3035 static void
3036f_escape(typval_T *argvars, typval_T *rettv)
3037{
3038 char_u buf[NUMBUFLEN];
3039
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003040 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3041 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003042 rettv->v_type = VAR_STRING;
3043}
3044
3045/*
3046 * "eval()" function
3047 */
3048 static void
3049f_eval(typval_T *argvars, typval_T *rettv)
3050{
3051 char_u *s, *p;
3052
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003053 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003054 if (s != NULL)
3055 s = skipwhite(s);
3056
3057 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003058 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003059 {
3060 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003061 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003062 need_clr_eos = FALSE;
3063 rettv->v_type = VAR_NUMBER;
3064 rettv->vval.v_number = 0;
3065 }
3066 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003067 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003068}
3069
3070/*
3071 * "eventhandler()" function
3072 */
3073 static void
3074f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3075{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003076 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003077}
3078
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003079static garray_T redir_execute_ga;
3080
3081/*
3082 * Append "value[value_len]" to the execute() output.
3083 */
3084 void
3085execute_redir_str(char_u *value, int value_len)
3086{
3087 int len;
3088
3089 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003090 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003091 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003092 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003093 if (ga_grow(&redir_execute_ga, len) == OK)
3094 {
3095 mch_memmove((char *)redir_execute_ga.ga_data
3096 + redir_execute_ga.ga_len, value, len);
3097 redir_execute_ga.ga_len += len;
3098 }
3099}
3100
3101/*
3102 * Get next line from a list.
3103 * Called by do_cmdline() to get the next line.
3104 * Returns allocated string, or NULL for end of function.
3105 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003106 static char_u *
3107get_list_line(
3108 int c UNUSED,
3109 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003110 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02003111 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003112{
3113 listitem_T **p = (listitem_T **)cookie;
3114 listitem_T *item = *p;
3115 char_u buf[NUMBUFLEN];
3116 char_u *s;
3117
3118 if (item == NULL)
3119 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003120 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003121 *p = item->li_next;
3122 return s == NULL ? NULL : vim_strsave(s);
3123}
3124
3125/*
3126 * "execute()" function
3127 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02003128 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003129execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003130{
3131 char_u *cmd = NULL;
3132 list_T *list = NULL;
3133 int save_msg_silent = msg_silent;
3134 int save_emsg_silent = emsg_silent;
3135 int save_emsg_noredir = emsg_noredir;
3136 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003137 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003138 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003139 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003140 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003141
3142 rettv->vval.v_string = NULL;
3143 rettv->v_type = VAR_STRING;
3144
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003145 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003146 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003147 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003148 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003149 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003150 return;
3151 ++list->lv_refcount;
3152 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003153 else if (argvars[arg_off].v_type == VAR_JOB
3154 || argvars[arg_off].v_type == VAR_CHANNEL)
3155 {
3156 emsg(_(e_inval_string));
3157 return;
3158 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003159 else
3160 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003161 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003162 if (cmd == NULL)
3163 return;
3164 }
3165
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003166 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003167 {
3168 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003169 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003170
3171 if (s == NULL)
3172 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003173 if (*s == NUL)
3174 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003175 if (STRNCMP(s, "silent", 6) == 0)
3176 ++msg_silent;
3177 if (STRCMP(s, "silent!") == 0)
3178 {
3179 emsg_silent = TRUE;
3180 emsg_noredir = TRUE;
3181 }
3182 }
3183 else
3184 ++msg_silent;
3185
3186 if (redir_execute)
3187 save_ga = redir_execute_ga;
3188 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3189 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003190 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003191 if (!echo_output)
3192 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003193
3194 if (cmd != NULL)
3195 do_cmdline_cmd(cmd);
3196 else
3197 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003198 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003199
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003200 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003201 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003202 do_cmdline(NULL, get_list_line, (void *)&item,
3203 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3204 --list->lv_refcount;
3205 }
3206
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003207 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003208 if (ga_grow(&redir_execute_ga, 1) == OK)
3209 {
3210 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3211 rettv->vval.v_string = redir_execute_ga.ga_data;
3212 }
3213 else
3214 {
3215 ga_clear(&redir_execute_ga);
3216 rettv->vval.v_string = NULL;
3217 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003218 msg_silent = save_msg_silent;
3219 emsg_silent = save_emsg_silent;
3220 emsg_noredir = save_emsg_noredir;
3221
3222 redir_execute = save_redir_execute;
3223 if (redir_execute)
3224 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003225 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003226
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003227 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003228 if (echo_output)
3229 // When not working silently: put it in column zero. A following
3230 // "echon" will overwrite the message, unavoidably.
3231 msg_col = 0;
3232 else
3233 // When working silently: Put it back where it was, since nothing
3234 // should have been written.
3235 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003236}
3237
3238/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003239 * "execute()" function
3240 */
3241 static void
3242f_execute(typval_T *argvars, typval_T *rettv)
3243{
3244 execute_common(argvars, rettv, 0);
3245}
3246
3247/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003248 * "exists()" function
3249 */
3250 static void
3251f_exists(typval_T *argvars, typval_T *rettv)
3252{
3253 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003254 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003255
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003256 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003257 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003258 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003259 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003260 if (mch_getenv(p + 1) != NULL)
3261 n = TRUE;
3262 else
3263 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003264 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003265 p = expand_env_save(p);
3266 if (p != NULL && *p != '$')
3267 n = TRUE;
3268 vim_free(p);
3269 }
3270 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003271 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003272 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003273 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003274 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003275 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003276 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003277 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003278 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003279 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003280 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003281 else if (*p == '?') // internal function only
3282 {
3283 n = has_internal_func_name(p + 1);
3284 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003285 else if (*p == ':')
3286 {
3287 n = cmd_exists(p + 1);
3288 }
3289 else if (*p == '#')
3290 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003291 if (p[1] == '#')
3292 n = autocmd_supported(p + 2);
3293 else
3294 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003295 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003296 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003297 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003298 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299 }
3300
3301 rettv->vval.v_number = n;
3302}
3303
3304#ifdef FEAT_FLOAT
3305/*
3306 * "exp()" function
3307 */
3308 static void
3309f_exp(typval_T *argvars, typval_T *rettv)
3310{
3311 float_T f = 0.0;
3312
3313 rettv->v_type = VAR_FLOAT;
3314 if (get_float_arg(argvars, &f) == OK)
3315 rettv->vval.v_float = exp(f);
3316 else
3317 rettv->vval.v_float = 0.0;
3318}
3319#endif
3320
3321/*
3322 * "expand()" function
3323 */
3324 static void
3325f_expand(typval_T *argvars, typval_T *rettv)
3326{
3327 char_u *s;
3328 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003329 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003330 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3331 expand_T xpc;
3332 int error = FALSE;
3333 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003334#ifdef BACKSLASH_IN_FILENAME
3335 char_u *p_csl_save = p_csl;
3336
3337 // avoid using 'completeslash' here
3338 p_csl = empty_option;
3339#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003340
3341 rettv->v_type = VAR_STRING;
3342 if (argvars[1].v_type != VAR_UNKNOWN
3343 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003344 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003345 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003346 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003347
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003348 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003349 if (*s == '%' || *s == '#' || *s == '<')
3350 {
3351 ++emsg_off;
3352 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3353 --emsg_off;
3354 if (rettv->v_type == VAR_LIST)
3355 {
3356 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3357 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003358 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003359 }
3360 else
3361 rettv->vval.v_string = result;
3362 }
3363 else
3364 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003365 // When the optional second argument is non-zero, don't remove matches
3366 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003367 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003368 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003369 options |= WILD_KEEP_ALL;
3370 if (!error)
3371 {
3372 ExpandInit(&xpc);
3373 xpc.xp_context = EXPAND_FILES;
3374 if (p_wic)
3375 options += WILD_ICASE;
3376 if (rettv->v_type == VAR_STRING)
3377 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3378 options, WILD_ALL);
3379 else if (rettv_list_alloc(rettv) != FAIL)
3380 {
3381 int i;
3382
3383 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3384 for (i = 0; i < xpc.xp_numfiles; i++)
3385 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3386 ExpandCleanup(&xpc);
3387 }
3388 }
3389 else
3390 rettv->vval.v_string = NULL;
3391 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003392#ifdef BACKSLASH_IN_FILENAME
3393 p_csl = p_csl_save;
3394#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003395}
3396
3397/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003398 * "expandcmd()" function
3399 * Expand all the special characters in a command string.
3400 */
3401 static void
3402f_expandcmd(typval_T *argvars, typval_T *rettv)
3403{
3404 exarg_T eap;
3405 char_u *cmdstr;
3406 char *errormsg = NULL;
3407
3408 rettv->v_type = VAR_STRING;
3409 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3410
3411 memset(&eap, 0, sizeof(eap));
3412 eap.cmd = cmdstr;
3413 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003414 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003415 eap.usefilter = FALSE;
3416 eap.nextcmd = NULL;
3417 eap.cmdidx = CMD_USER;
3418
3419 expand_filename(&eap, &cmdstr, &errormsg);
3420 if (errormsg != NULL && *errormsg != NUL)
3421 emsg(errormsg);
3422
3423 rettv->vval.v_string = cmdstr;
3424}
3425
3426/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003427 * "feedkeys()" function
3428 */
3429 static void
3430f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3431{
3432 int remap = TRUE;
3433 int insert = FALSE;
3434 char_u *keys, *flags;
3435 char_u nbuf[NUMBUFLEN];
3436 int typed = FALSE;
3437 int execute = FALSE;
3438 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003439 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003440 char_u *keys_esc;
3441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003442 // This is not allowed in the sandbox. If the commands would still be
3443 // executed in the sandbox it would be OK, but it probably happens later,
3444 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003445 if (check_secure())
3446 return;
3447
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003448 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003449
3450 if (argvars[1].v_type != VAR_UNKNOWN)
3451 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003452 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003453 for ( ; *flags != NUL; ++flags)
3454 {
3455 switch (*flags)
3456 {
3457 case 'n': remap = FALSE; break;
3458 case 'm': remap = TRUE; break;
3459 case 't': typed = TRUE; break;
3460 case 'i': insert = TRUE; break;
3461 case 'x': execute = TRUE; break;
3462 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003463 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003464 }
3465 }
3466 }
3467
3468 if (*keys != NUL || execute)
3469 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003470 // Need to escape K_SPECIAL and CSI before putting the string in the
3471 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003472 keys_esc = vim_strsave_escape_csi(keys);
3473 if (keys_esc != NULL)
3474 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003475 if (lowlevel)
3476 {
3477#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003478 int idx;
3479 int len = (int)STRLEN(keys);
3480
3481 for (idx = 0; idx < len; ++idx)
3482 {
3483 // if a CTRL-C was typed, set got_int, similar to what
3484 // happens in fill_input_buf()
3485 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3486 got_int = TRUE;
3487 add_to_input_buf(keys + idx, 1);
3488 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003489#else
3490 emsg(_("E980: lowlevel input not supported"));
3491#endif
3492 }
3493 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003494 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003495 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003496 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003497 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003498#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003499 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003500#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003501 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003502 typebuf_was_filled = TRUE;
3503 }
3504 vim_free(keys_esc);
3505
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003506 if (execute)
3507 {
3508 int save_msg_scroll = msg_scroll;
3509
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003510 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003511 msg_scroll = FALSE;
3512
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003513 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003514 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003515 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003516 ++in_feedkeys;
3517 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003518 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003519 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003520 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003521 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003522 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003523 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003524
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003525 msg_scroll |= save_msg_scroll;
3526 }
3527 }
3528 }
3529}
3530
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003531#ifdef FEAT_FLOAT
3532/*
3533 * "float2nr({float})" function
3534 */
3535 static void
3536f_float2nr(typval_T *argvars, typval_T *rettv)
3537{
3538 float_T f = 0.0;
3539
3540 if (get_float_arg(argvars, &f) == OK)
3541 {
Bram Moolenaar37184272020-05-23 19:30:05 +02003542 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003543 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02003544 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003545 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003546 else
3547 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003548 }
3549}
3550
3551/*
3552 * "floor({float})" function
3553 */
3554 static void
3555f_floor(typval_T *argvars, typval_T *rettv)
3556{
3557 float_T f = 0.0;
3558
3559 rettv->v_type = VAR_FLOAT;
3560 if (get_float_arg(argvars, &f) == OK)
3561 rettv->vval.v_float = floor(f);
3562 else
3563 rettv->vval.v_float = 0.0;
3564}
3565
3566/*
3567 * "fmod()" function
3568 */
3569 static void
3570f_fmod(typval_T *argvars, typval_T *rettv)
3571{
3572 float_T fx = 0.0, fy = 0.0;
3573
3574 rettv->v_type = VAR_FLOAT;
3575 if (get_float_arg(argvars, &fx) == OK
3576 && get_float_arg(&argvars[1], &fy) == OK)
3577 rettv->vval.v_float = fmod(fx, fy);
3578 else
3579 rettv->vval.v_float = 0.0;
3580}
3581#endif
3582
3583/*
3584 * "fnameescape({string})" function
3585 */
3586 static void
3587f_fnameescape(typval_T *argvars, typval_T *rettv)
3588{
3589 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003590 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003591 rettv->v_type = VAR_STRING;
3592}
3593
3594/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003595 * "foreground()" function
3596 */
3597 static void
3598f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3599{
3600#ifdef FEAT_GUI
3601 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003602 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003603 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003604 return;
3605 }
3606#endif
3607#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003608 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003609#endif
3610}
3611
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003612 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003613common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003614{
3615 char_u *s;
3616 char_u *name;
3617 int use_string = FALSE;
3618 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003619 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003620 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003621
3622 if (argvars[0].v_type == VAR_FUNC)
3623 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003624 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003625 s = argvars[0].vval.v_string;
3626 }
3627 else if (argvars[0].v_type == VAR_PARTIAL
3628 && argvars[0].vval.v_partial != NULL)
3629 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003630 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003631 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003632 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003633 }
3634 else
3635 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003636 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003637 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003638 use_string = TRUE;
3639 }
3640
Bram Moolenaar843b8842016-08-21 14:36:15 +02003641 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003642 {
3643 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003644 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003645 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3646 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003647 if (*name != NUL)
3648 s = NULL;
3649 }
3650
Bram Moolenaar843b8842016-08-21 14:36:15 +02003651 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3652 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003653 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003654 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003655 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003656 ? find_func(trans_name, is_global, NULL) == NULL
3657 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003658 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003659 else
3660 {
3661 int dict_idx = 0;
3662 int arg_idx = 0;
3663 list_T *list = NULL;
3664
3665 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3666 {
3667 char sid_buf[25];
3668 int off = *s == 's' ? 2 : 5;
3669
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003670 // Expand s: and <SID> into <SNR>nr_, so that the function can
3671 // also be called from another script. Using trans_function_name()
3672 // would also work, but some plugins depend on the name being
3673 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003674 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003675 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003676 if (name != NULL)
3677 {
3678 STRCPY(name, sid_buf);
3679 STRCAT(name, s + off);
3680 }
3681 }
3682 else
3683 name = vim_strsave(s);
3684
3685 if (argvars[1].v_type != VAR_UNKNOWN)
3686 {
3687 if (argvars[2].v_type != VAR_UNKNOWN)
3688 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003689 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003690 arg_idx = 1;
3691 dict_idx = 2;
3692 }
3693 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003694 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003695 dict_idx = 1;
3696 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003697 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003698 arg_idx = 1;
3699 if (dict_idx > 0)
3700 {
3701 if (argvars[dict_idx].v_type != VAR_DICT)
3702 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003703 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003704 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003705 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003706 }
3707 if (argvars[dict_idx].vval.v_dict == NULL)
3708 dict_idx = 0;
3709 }
3710 if (arg_idx > 0)
3711 {
3712 if (argvars[arg_idx].v_type != VAR_LIST)
3713 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003714 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003715 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003716 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003717 }
3718 list = argvars[arg_idx].vval.v_list;
3719 if (list == NULL || list->lv_len == 0)
3720 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003721 else if (list->lv_len > MAX_FUNC_ARGS)
3722 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003723 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003724 vim_free(name);
3725 goto theend;
3726 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003727 }
3728 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003729 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003730 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003731 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003732
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003733 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003734 if (pt == NULL)
3735 vim_free(name);
3736 else
3737 {
3738 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3739 {
3740 listitem_T *li;
3741 int i = 0;
3742 int arg_len = 0;
3743 int lv_len = 0;
3744
3745 if (arg_pt != NULL)
3746 arg_len = arg_pt->pt_argc;
3747 if (list != NULL)
3748 lv_len = list->lv_len;
3749 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003750 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003751 if (pt->pt_argv == NULL)
3752 {
3753 vim_free(pt);
3754 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003755 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003756 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003757 for (i = 0; i < arg_len; i++)
3758 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3759 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003760 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003761 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003762 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003763 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003764 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003765 }
3766
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003767 // For "function(dict.func, [], dict)" and "func" is a partial
3768 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003769 if (dict_idx > 0)
3770 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003771 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003772 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3773 ++pt->pt_dict->dv_refcount;
3774 }
3775 else if (arg_pt != NULL)
3776 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003777 // If the dict was bound automatically the result is also
3778 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003779 pt->pt_dict = arg_pt->pt_dict;
3780 pt->pt_auto = arg_pt->pt_auto;
3781 if (pt->pt_dict != NULL)
3782 ++pt->pt_dict->dv_refcount;
3783 }
3784
3785 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003786 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3787 {
3788 pt->pt_func = arg_pt->pt_func;
3789 func_ptr_ref(pt->pt_func);
3790 vim_free(name);
3791 }
3792 else if (is_funcref)
3793 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003794 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003795 func_ptr_ref(pt->pt_func);
3796 vim_free(name);
3797 }
3798 else
3799 {
3800 pt->pt_name = name;
3801 func_ref(name);
3802 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003803 }
3804 rettv->v_type = VAR_PARTIAL;
3805 rettv->vval.v_partial = pt;
3806 }
3807 else
3808 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003809 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003810 rettv->v_type = VAR_FUNC;
3811 rettv->vval.v_string = name;
3812 func_ref(name);
3813 }
3814 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003815theend:
3816 vim_free(trans_name);
3817}
3818
3819/*
3820 * "funcref()" function
3821 */
3822 static void
3823f_funcref(typval_T *argvars, typval_T *rettv)
3824{
3825 common_function(argvars, rettv, TRUE);
3826}
3827
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003828 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003829ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003830{
3831 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3832 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003833 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003834}
3835
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003836/*
3837 * "function()" function
3838 */
3839 static void
3840f_function(typval_T *argvars, typval_T *rettv)
3841{
3842 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003843}
3844
3845/*
3846 * "garbagecollect()" function
3847 */
3848 static void
3849f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3850{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003851 // This is postponed until we are back at the toplevel, because we may be
3852 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003853 want_garbage_collect = TRUE;
3854
Bram Moolenaar2df47312020-09-05 17:30:44 +02003855 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003856 garbage_collect_at_exit = TRUE;
3857}
3858
3859/*
3860 * "get()" function
3861 */
3862 static void
3863f_get(typval_T *argvars, typval_T *rettv)
3864{
3865 listitem_T *li;
3866 list_T *l;
3867 dictitem_T *di;
3868 dict_T *d;
3869 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003870 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003871
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003872 if (argvars[0].v_type == VAR_BLOB)
3873 {
3874 int error = FALSE;
3875 int idx = tv_get_number_chk(&argvars[1], &error);
3876
3877 if (!error)
3878 {
3879 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003880 if (idx < 0)
3881 idx = blob_len(argvars[0].vval.v_blob) + idx;
3882 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3883 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003884 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003885 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003886 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003887 tv = rettv;
3888 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003889 }
3890 }
3891 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003892 {
3893 if ((l = argvars[0].vval.v_list) != NULL)
3894 {
3895 int error = FALSE;
3896
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003897 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003898 if (!error && li != NULL)
3899 tv = &li->li_tv;
3900 }
3901 }
3902 else if (argvars[0].v_type == VAR_DICT)
3903 {
3904 if ((d = argvars[0].vval.v_dict) != NULL)
3905 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003906 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003907 if (di != NULL)
3908 tv = &di->di_tv;
3909 }
3910 }
3911 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3912 {
3913 partial_T *pt;
3914 partial_T fref_pt;
3915
3916 if (argvars[0].v_type == VAR_PARTIAL)
3917 pt = argvars[0].vval.v_partial;
3918 else
3919 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003920 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003921 fref_pt.pt_name = argvars[0].vval.v_string;
3922 pt = &fref_pt;
3923 }
3924
3925 if (pt != NULL)
3926 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003927 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003928 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003929
3930 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3931 {
3932 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003933 n = partial_name(pt);
3934 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003935 rettv->vval.v_string = NULL;
3936 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003937 {
3938 rettv->vval.v_string = vim_strsave(n);
3939 if (rettv->v_type == VAR_FUNC)
3940 func_ref(rettv->vval.v_string);
3941 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003942 }
3943 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003944 {
3945 what_is_dict = TRUE;
3946 if (pt->pt_dict != NULL)
3947 rettv_dict_set(rettv, pt->pt_dict);
3948 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003949 else if (STRCMP(what, "args") == 0)
3950 {
3951 rettv->v_type = VAR_LIST;
3952 if (rettv_list_alloc(rettv) == OK)
3953 {
3954 int i;
3955
3956 for (i = 0; i < pt->pt_argc; ++i)
3957 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3958 }
3959 }
3960 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003961 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003962
3963 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3964 // third argument
3965 if (!what_is_dict)
3966 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003967 }
3968 }
3969 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003970 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003971
3972 if (tv == NULL)
3973 {
3974 if (argvars[2].v_type != VAR_UNKNOWN)
3975 copy_tv(&argvars[2], rettv);
3976 }
3977 else
3978 copy_tv(tv, rettv);
3979}
3980
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003981/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003982 * "getchangelist()" function
3983 */
3984 static void
3985f_getchangelist(typval_T *argvars, typval_T *rettv)
3986{
3987#ifdef FEAT_JUMPLIST
3988 buf_T *buf;
3989 int i;
3990 list_T *l;
3991 dict_T *d;
3992#endif
3993
3994 if (rettv_list_alloc(rettv) != OK)
3995 return;
3996
3997#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003998 if (argvars[0].v_type == VAR_UNKNOWN)
3999 buf = curbuf;
4000 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02004001 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004002 if (buf == NULL)
4003 return;
4004
4005 l = list_alloc();
4006 if (l == NULL)
4007 return;
4008
4009 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4010 return;
4011 /*
4012 * The current window change list index tracks only the position in the
4013 * current buffer change list. For other buffers, use the change list
4014 * length as the current index.
4015 */
4016 list_append_number(rettv->vval.v_list,
4017 (varnumber_T)((buf == curwin->w_buffer)
4018 ? curwin->w_changelistidx : buf->b_changelistlen));
4019
4020 for (i = 0; i < buf->b_changelistlen; ++i)
4021 {
4022 if (buf->b_changelist[i].lnum == 0)
4023 continue;
4024 if ((d = dict_alloc()) == NULL)
4025 return;
4026 if (list_append_dict(l, d) == FAIL)
4027 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004028 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4029 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004030 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004031 }
4032#endif
4033}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004034
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004035 static void
4036getpos_both(
4037 typval_T *argvars,
4038 typval_T *rettv,
4039 int getcurpos,
4040 int charcol)
4041{
4042 pos_T *fp = NULL;
4043 pos_T pos;
4044 win_T *wp = curwin;
4045 list_T *l;
4046 int fnum = -1;
4047
4048 if (rettv_list_alloc(rettv) == OK)
4049 {
4050 l = rettv->vval.v_list;
4051 if (getcurpos)
4052 {
4053 if (argvars[0].v_type != VAR_UNKNOWN)
4054 {
4055 wp = find_win_by_nr_or_id(&argvars[0]);
4056 if (wp != NULL)
4057 fp = &wp->w_cursor;
4058 }
4059 else
4060 fp = &curwin->w_cursor;
4061 if (fp != NULL && charcol)
4062 {
4063 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01004064 pos.col =
4065 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004066 fp = &pos;
4067 }
4068 }
4069 else
4070 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
4071 if (fnum != -1)
4072 list_append_number(l, (varnumber_T)fnum);
4073 else
4074 list_append_number(l, (varnumber_T)0);
4075 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4076 : (varnumber_T)0);
4077 list_append_number(l, (fp != NULL)
4078 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4079 : (varnumber_T)0);
4080 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
4081 (varnumber_T)0);
4082 if (getcurpos)
4083 {
4084 int save_set_curswant = curwin->w_set_curswant;
4085 colnr_T save_curswant = curwin->w_curswant;
4086 colnr_T save_virtcol = curwin->w_virtcol;
4087
4088 if (wp == curwin)
4089 update_curswant();
4090 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
4091 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
4092
4093 // Do not change "curswant", as it is unexpected that a get
4094 // function has a side effect.
4095 if (wp == curwin && save_set_curswant)
4096 {
4097 curwin->w_set_curswant = save_set_curswant;
4098 curwin->w_curswant = save_curswant;
4099 curwin->w_virtcol = save_virtcol;
4100 curwin->w_valid &= ~VALID_VIRTCOL;
4101 }
4102 }
4103 }
4104 else
4105 rettv->vval.v_number = FALSE;
4106}
4107
4108/*
4109 * "getcharpos()" function
4110 */
4111 static void
4112f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
4113{
4114 getpos_both(argvars, rettv, FALSE, TRUE);
4115}
4116
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004117/*
4118 * "getcharsearch()" function
4119 */
4120 static void
4121f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4122{
4123 if (rettv_dict_alloc(rettv) != FAIL)
4124 {
4125 dict_T *dict = rettv->vval.v_dict;
4126
Bram Moolenaare0be1672018-07-08 16:50:37 +02004127 dict_add_string(dict, "char", last_csearch());
4128 dict_add_number(dict, "forward", last_csearch_forward());
4129 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004130 }
4131}
4132
4133/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004134 * "getenv()" function
4135 */
4136 static void
4137f_getenv(typval_T *argvars, typval_T *rettv)
4138{
4139 int mustfree = FALSE;
4140 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4141
4142 if (p == NULL)
4143 {
4144 rettv->v_type = VAR_SPECIAL;
4145 rettv->vval.v_number = VVAL_NULL;
4146 return;
4147 }
4148 if (!mustfree)
4149 p = vim_strsave(p);
4150 rettv->vval.v_string = p;
4151 rettv->v_type = VAR_STRING;
4152}
4153
4154/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004155 * "getfontname()" function
4156 */
4157 static void
4158f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4159{
4160 rettv->v_type = VAR_STRING;
4161 rettv->vval.v_string = NULL;
4162#ifdef FEAT_GUI
4163 if (gui.in_use)
4164 {
4165 GuiFont font;
4166 char_u *name = NULL;
4167
4168 if (argvars[0].v_type == VAR_UNKNOWN)
4169 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004170 // Get the "Normal" font. Either the name saved by
4171 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004172 font = gui.norm_font;
4173 name = hl_get_font_name();
4174 }
4175 else
4176 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004177 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004178 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004179 return;
4180 font = gui_mch_get_font(name, FALSE);
4181 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004182 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004183 }
4184 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4185 if (argvars[0].v_type != VAR_UNKNOWN)
4186 gui_mch_free_font(font);
4187 }
4188#endif
4189}
4190
4191/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004192 * "getjumplist()" function
4193 */
4194 static void
4195f_getjumplist(typval_T *argvars, typval_T *rettv)
4196{
4197#ifdef FEAT_JUMPLIST
4198 win_T *wp;
4199 int i;
4200 list_T *l;
4201 dict_T *d;
4202#endif
4203
4204 if (rettv_list_alloc(rettv) != OK)
4205 return;
4206
4207#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004208 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004209 if (wp == NULL)
4210 return;
4211
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004212 cleanup_jumplist(wp, TRUE);
4213
Bram Moolenaar4f505882018-02-10 21:06:32 +01004214 l = list_alloc();
4215 if (l == NULL)
4216 return;
4217
4218 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4219 return;
4220 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4221
4222 for (i = 0; i < wp->w_jumplistlen; ++i)
4223 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004224 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4225 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004226 if ((d = dict_alloc()) == NULL)
4227 return;
4228 if (list_append_dict(l, d) == FAIL)
4229 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004230 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4231 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004232 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004233 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004234 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004235 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004236 }
4237#endif
4238}
4239
4240/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004241 * "getpid()" function
4242 */
4243 static void
4244f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4245{
4246 rettv->vval.v_number = mch_get_pid();
4247}
4248
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004249/*
4250 * "getcurpos()" function
4251 */
4252 static void
4253f_getcurpos(typval_T *argvars, typval_T *rettv)
4254{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004255 getpos_both(argvars, rettv, TRUE, FALSE);
4256}
4257
4258 static void
4259f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4260{
4261 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004262}
4263
4264/*
4265 * "getpos(string)" function
4266 */
4267 static void
4268f_getpos(typval_T *argvars, typval_T *rettv)
4269{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004270 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004271}
4272
4273/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004274 * "getreg()" function
4275 */
4276 static void
4277f_getreg(typval_T *argvars, typval_T *rettv)
4278{
4279 char_u *strregname;
4280 int regname;
4281 int arg2 = FALSE;
4282 int return_list = FALSE;
4283 int error = FALSE;
4284
4285 if (argvars[0].v_type != VAR_UNKNOWN)
4286 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004287 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004288 if (strregname == NULL)
4289 error = TRUE;
4290 else if (in_vim9script() && STRLEN(strregname) > 1)
4291 {
4292 semsg(_(e_register_name_must_be_one_char_str), strregname);
4293 error = TRUE;
4294 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004295 if (argvars[1].v_type != VAR_UNKNOWN)
4296 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004297 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004298 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004299 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004300 }
4301 }
4302 else
4303 strregname = get_vim_var_str(VV_REG);
4304
4305 if (error)
4306 return;
4307
4308 regname = (strregname == NULL ? '"' : *strregname);
4309 if (regname == 0)
4310 regname = '"';
4311
4312 if (return_list)
4313 {
4314 rettv->v_type = VAR_LIST;
4315 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4316 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4317 if (rettv->vval.v_list == NULL)
4318 (void)rettv_list_alloc(rettv);
4319 else
4320 ++rettv->vval.v_list->lv_refcount;
4321 }
4322 else
4323 {
4324 rettv->v_type = VAR_STRING;
4325 rettv->vval.v_string = get_reg_contents(regname,
4326 arg2 ? GREG_EXPR_SRC : 0);
4327 }
4328}
4329
4330/*
4331 * "getregtype()" function
4332 */
4333 static void
4334f_getregtype(typval_T *argvars, typval_T *rettv)
4335{
4336 char_u *strregname;
4337 int regname;
4338 char_u buf[NUMBUFLEN + 2];
4339 long reglen = 0;
4340
4341 if (argvars[0].v_type != VAR_UNKNOWN)
4342 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004343 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004344 if (strregname != NULL && in_vim9script() && STRLEN(strregname) > 1)
4345 {
4346 semsg(_(e_register_name_must_be_one_char_str), strregname);
4347 strregname = NULL;
4348 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004349 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004350 {
4351 rettv->v_type = VAR_STRING;
4352 rettv->vval.v_string = NULL;
4353 return;
4354 }
4355 }
4356 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004357 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004358 strregname = get_vim_var_str(VV_REG);
4359
4360 regname = (strregname == NULL ? '"' : *strregname);
4361 if (regname == 0)
4362 regname = '"';
4363
4364 buf[0] = NUL;
4365 buf[1] = NUL;
4366 switch (get_reg_type(regname, &reglen))
4367 {
4368 case MLINE: buf[0] = 'V'; break;
4369 case MCHAR: buf[0] = 'v'; break;
4370 case MBLOCK:
4371 buf[0] = Ctrl_V;
4372 sprintf((char *)buf + 1, "%ld", reglen + 1);
4373 break;
4374 }
4375 rettv->v_type = VAR_STRING;
4376 rettv->vval.v_string = vim_strsave(buf);
4377}
4378
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004379/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004380 * "gettagstack()" function
4381 */
4382 static void
4383f_gettagstack(typval_T *argvars, typval_T *rettv)
4384{
4385 win_T *wp = curwin; // default is current window
4386
4387 if (rettv_dict_alloc(rettv) != OK)
4388 return;
4389
4390 if (argvars[0].v_type != VAR_UNKNOWN)
4391 {
4392 wp = find_win_by_nr_or_id(&argvars[0]);
4393 if (wp == NULL)
4394 return;
4395 }
4396
4397 get_tagstack(wp, rettv->vval.v_dict);
4398}
4399
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004400/*
4401 * "gettext()" function
4402 */
4403 static void
4404f_gettext(typval_T *argvars, typval_T *rettv)
4405{
4406 if (argvars[0].v_type != VAR_STRING
4407 || argvars[0].vval.v_string == NULL
4408 || *argvars[0].vval.v_string == NUL)
4409 {
4410 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4411 }
4412 else
4413 {
4414 rettv->v_type = VAR_STRING;
4415 rettv->vval.v_string = vim_strsave(
4416 (char_u *)_(argvars[0].vval.v_string));
4417 }
4418}
4419
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004420// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004421#include "version.h"
4422
4423/*
4424 * "has()" function
4425 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004426 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004427f_has(typval_T *argvars, typval_T *rettv)
4428{
4429 int i;
4430 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004431 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004432 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004433 typedef struct {
4434 char *name;
4435 short present;
4436 } has_item_T;
4437 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004438 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004439 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004440#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004441 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004442#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004443 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004444#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004445 },
4446 {"arp",
4447#if defined(AMIGA) && defined(FEAT_ARP)
4448 1
4449#else
4450 0
4451#endif
4452 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004453 {"haiku",
4454#ifdef __HAIKU__
4455 1
4456#else
4457 0
4458#endif
4459 },
4460 {"bsd",
4461#if defined(BSD) && !defined(MACOS_X)
4462 1
4463#else
4464 0
4465#endif
4466 },
4467 {"hpux",
4468#ifdef hpux
4469 1
4470#else
4471 0
4472#endif
4473 },
4474 {"linux",
4475#ifdef __linux__
4476 1
4477#else
4478 0
4479#endif
4480 },
4481 {"mac", // Mac OS X (and, once, Mac OS Classic)
4482#ifdef MACOS_X
4483 1
4484#else
4485 0
4486#endif
4487 },
4488 {"osx", // Mac OS X
4489#ifdef MACOS_X
4490 1
4491#else
4492 0
4493#endif
4494 },
4495 {"macunix", // Mac OS X, with the darwin feature
4496#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4497 1
4498#else
4499 0
4500#endif
4501 },
4502 {"osxdarwin", // synonym for macunix
4503#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4504 1
4505#else
4506 0
4507#endif
4508 },
4509 {"qnx",
4510#ifdef __QNX__
4511 1
4512#else
4513 0
4514#endif
4515 },
4516 {"sun",
4517#ifdef SUN_SYSTEM
4518 1
4519#else
4520 0
4521#endif
4522 },
4523 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004524#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004525 1
4526#else
4527 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004528#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004529 },
4530 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004531#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004532 1
4533#else
4534 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004535#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004536 },
4537 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004538#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004539 1
4540#else
4541 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004542#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004543 },
4544 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004545#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004546 1
4547#else
4548 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004549#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004550 },
4551 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004552#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004553 1
4554#else
4555 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004556#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004557 },
4558 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004559#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004560 1
4561#else
4562 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004563#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004564 },
4565 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004566#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004567 1
4568#else
4569 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004570#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004571 },
4572 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004573#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004574 1
4575#else
4576 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004577#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004578 },
4579 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004580#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004581 1
4582#else
4583 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004584#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004585 },
4586 {"autocmd", 1},
4587 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004588#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004589 1
4590#else
4591 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004592#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004593 },
4594 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004595#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004596 1
4597#else
4598 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004599#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004600 },
4601 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004602#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004603 1
4604#else
4605 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004606#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004607 },
4608 {"balloon_multiline",
4609#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4610 // MS-Windows requires runtime check, see below
4611 1
4612#else
4613 0
4614#endif
4615 },
4616 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004617#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004618 1
4619#else
4620 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004621#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004622 },
4623 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004624#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004625 1
4626#else
4627 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004628#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004629 },
4630 {"all_builtin_terms",
4631#if defined(ALL_BUILTIN_TCAPS)
4632 1
4633#else
4634 0
4635#endif
4636 },
4637 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004638#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004639 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004640 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004641 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004642#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004643 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004644#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004645 },
4646 {"byte_offset",
4647#ifdef FEAT_BYTEOFF
4648 1
4649#else
4650 0
4651#endif
4652 },
4653 {"channel",
4654#ifdef FEAT_JOB_CHANNEL
4655 1
4656#else
4657 0
4658#endif
4659 },
4660 {"cindent",
4661#ifdef FEAT_CINDENT
4662 1
4663#else
4664 0
4665#endif
4666 },
4667 {"clientserver",
4668#ifdef FEAT_CLIENTSERVER
4669 1
4670#else
4671 0
4672#endif
4673 },
4674 {"clipboard",
4675#ifdef FEAT_CLIPBOARD
4676 1
4677#else
4678 0
4679#endif
4680 },
4681 {"cmdline_compl", 1},
4682 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004683 {"cmdwin",
4684#ifdef FEAT_CMDWIN
4685 1
4686#else
4687 0
4688#endif
4689 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004690 {"comments", 1},
4691 {"conceal",
4692#ifdef FEAT_CONCEAL
4693 1
4694#else
4695 0
4696#endif
4697 },
4698 {"cryptv",
4699#ifdef FEAT_CRYPT
4700 1
4701#else
4702 0
4703#endif
4704 },
4705 {"crypt-blowfish",
4706#ifdef FEAT_CRYPT
4707 1
4708#else
4709 0
4710#endif
4711 },
4712 {"crypt-blowfish2",
4713#ifdef FEAT_CRYPT
4714 1
4715#else
4716 0
4717#endif
4718 },
4719 {"cscope",
4720#ifdef FEAT_CSCOPE
4721 1
4722#else
4723 0
4724#endif
4725 },
4726 {"cursorbind", 1},
4727 {"cursorshape",
4728#ifdef CURSOR_SHAPE
4729 1
4730#else
4731 0
4732#endif
4733 },
4734 {"debug",
4735#ifdef DEBUG
4736 1
4737#else
4738 0
4739#endif
4740 },
4741 {"dialog_con",
4742#ifdef FEAT_CON_DIALOG
4743 1
4744#else
4745 0
4746#endif
4747 },
4748 {"dialog_gui",
4749#ifdef FEAT_GUI_DIALOG
4750 1
4751#else
4752 0
4753#endif
4754 },
4755 {"diff",
4756#ifdef FEAT_DIFF
4757 1
4758#else
4759 0
4760#endif
4761 },
4762 {"digraphs",
4763#ifdef FEAT_DIGRAPHS
4764 1
4765#else
4766 0
4767#endif
4768 },
4769 {"directx",
4770#ifdef FEAT_DIRECTX
4771 1
4772#else
4773 0
4774#endif
4775 },
4776 {"dnd",
4777#ifdef FEAT_DND
4778 1
4779#else
4780 0
4781#endif
4782 },
4783 {"emacs_tags",
4784#ifdef FEAT_EMACS_TAGS
4785 1
4786#else
4787 0
4788#endif
4789 },
4790 {"eval", 1}, // always present, of course!
4791 {"ex_extra", 1}, // graduated feature
4792 {"extra_search",
4793#ifdef FEAT_SEARCH_EXTRA
4794 1
4795#else
4796 0
4797#endif
4798 },
4799 {"file_in_path",
4800#ifdef FEAT_SEARCHPATH
4801 1
4802#else
4803 0
4804#endif
4805 },
4806 {"filterpipe",
4807#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4808 1
4809#else
4810 0
4811#endif
4812 },
4813 {"find_in_path",
4814#ifdef FEAT_FIND_ID
4815 1
4816#else
4817 0
4818#endif
4819 },
4820 {"float",
4821#ifdef FEAT_FLOAT
4822 1
4823#else
4824 0
4825#endif
4826 },
4827 {"folding",
4828#ifdef FEAT_FOLDING
4829 1
4830#else
4831 0
4832#endif
4833 },
4834 {"footer",
4835#ifdef FEAT_FOOTER
4836 1
4837#else
4838 0
4839#endif
4840 },
4841 {"fork",
4842#if !defined(USE_SYSTEM) && defined(UNIX)
4843 1
4844#else
4845 0
4846#endif
4847 },
4848 {"gettext",
4849#ifdef FEAT_GETTEXT
4850 1
4851#else
4852 0
4853#endif
4854 },
4855 {"gui",
4856#ifdef FEAT_GUI
4857 1
4858#else
4859 0
4860#endif
4861 },
4862 {"gui_neXtaw",
4863#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4864 1
4865#else
4866 0
4867#endif
4868 },
4869 {"gui_athena",
4870#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4871 1
4872#else
4873 0
4874#endif
4875 },
4876 {"gui_gtk",
4877#ifdef FEAT_GUI_GTK
4878 1
4879#else
4880 0
4881#endif
4882 },
4883 {"gui_gtk2",
4884#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4885 1
4886#else
4887 0
4888#endif
4889 },
4890 {"gui_gtk3",
4891#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4892 1
4893#else
4894 0
4895#endif
4896 },
4897 {"gui_gnome",
4898#ifdef FEAT_GUI_GNOME
4899 1
4900#else
4901 0
4902#endif
4903 },
4904 {"gui_haiku",
4905#ifdef FEAT_GUI_HAIKU
4906 1
4907#else
4908 0
4909#endif
4910 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004911 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004912 {"gui_motif",
4913#ifdef FEAT_GUI_MOTIF
4914 1
4915#else
4916 0
4917#endif
4918 },
4919 {"gui_photon",
4920#ifdef FEAT_GUI_PHOTON
4921 1
4922#else
4923 0
4924#endif
4925 },
4926 {"gui_win32",
4927#ifdef FEAT_GUI_MSWIN
4928 1
4929#else
4930 0
4931#endif
4932 },
4933 {"iconv",
4934#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4935 1
4936#else
4937 0
4938#endif
4939 },
4940 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004941 {"ipv6",
4942#ifdef FEAT_IPV6
4943 1
4944#else
4945 0
4946#endif
4947 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004948 {"job",
4949#ifdef FEAT_JOB_CHANNEL
4950 1
4951#else
4952 0
4953#endif
4954 },
4955 {"jumplist",
4956#ifdef FEAT_JUMPLIST
4957 1
4958#else
4959 0
4960#endif
4961 },
4962 {"keymap",
4963#ifdef FEAT_KEYMAP
4964 1
4965#else
4966 0
4967#endif
4968 },
4969 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4970 {"langmap",
4971#ifdef FEAT_LANGMAP
4972 1
4973#else
4974 0
4975#endif
4976 },
4977 {"libcall",
4978#ifdef FEAT_LIBCALL
4979 1
4980#else
4981 0
4982#endif
4983 },
4984 {"linebreak",
4985#ifdef FEAT_LINEBREAK
4986 1
4987#else
4988 0
4989#endif
4990 },
4991 {"lispindent",
4992#ifdef FEAT_LISP
4993 1
4994#else
4995 0
4996#endif
4997 },
4998 {"listcmds", 1},
4999 {"localmap", 1},
5000 {"lua",
5001#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
5002 1
5003#else
5004 0
5005#endif
5006 },
5007 {"menu",
5008#ifdef FEAT_MENU
5009 1
5010#else
5011 0
5012#endif
5013 },
5014 {"mksession",
5015#ifdef FEAT_SESSION
5016 1
5017#else
5018 0
5019#endif
5020 },
5021 {"modify_fname", 1},
5022 {"mouse", 1},
5023 {"mouseshape",
5024#ifdef FEAT_MOUSESHAPE
5025 1
5026#else
5027 0
5028#endif
5029 },
5030 {"mouse_dec",
5031#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
5032 1
5033#else
5034 0
5035#endif
5036 },
5037 {"mouse_gpm",
5038#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
5039 1
5040#else
5041 0
5042#endif
5043 },
5044 {"mouse_jsbterm",
5045#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
5046 1
5047#else
5048 0
5049#endif
5050 },
5051 {"mouse_netterm",
5052#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
5053 1
5054#else
5055 0
5056#endif
5057 },
5058 {"mouse_pterm",
5059#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
5060 1
5061#else
5062 0
5063#endif
5064 },
5065 {"mouse_sgr",
5066#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5067 1
5068#else
5069 0
5070#endif
5071 },
5072 {"mouse_sysmouse",
5073#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
5074 1
5075#else
5076 0
5077#endif
5078 },
5079 {"mouse_urxvt",
5080#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
5081 1
5082#else
5083 0
5084#endif
5085 },
5086 {"mouse_xterm",
5087#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5088 1
5089#else
5090 0
5091#endif
5092 },
5093 {"multi_byte", 1},
5094 {"multi_byte_ime",
5095#ifdef FEAT_MBYTE_IME
5096 1
5097#else
5098 0
5099#endif
5100 },
5101 {"multi_lang",
5102#ifdef FEAT_MULTI_LANG
5103 1
5104#else
5105 0
5106#endif
5107 },
5108 {"mzscheme",
5109#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
5110 1
5111#else
5112 0
5113#endif
5114 },
5115 {"num64", 1},
5116 {"ole",
5117#ifdef FEAT_OLE
5118 1
5119#else
5120 0
5121#endif
5122 },
5123 {"packages",
5124#ifdef FEAT_EVAL
5125 1
5126#else
5127 0
5128#endif
5129 },
5130 {"path_extra",
5131#ifdef FEAT_PATH_EXTRA
5132 1
5133#else
5134 0
5135#endif
5136 },
5137 {"perl",
5138#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
5139 1
5140#else
5141 0
5142#endif
5143 },
5144 {"persistent_undo",
5145#ifdef FEAT_PERSISTENT_UNDO
5146 1
5147#else
5148 0
5149#endif
5150 },
5151 {"python_compiled",
5152#if defined(FEAT_PYTHON)
5153 1
5154#else
5155 0
5156#endif
5157 },
5158 {"python_dynamic",
5159#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
5160 1
5161#else
5162 0
5163#endif
5164 },
5165 {"python",
5166#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
5167 1
5168#else
5169 0
5170#endif
5171 },
5172 {"pythonx",
5173#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
5174 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
5175 1
5176#else
5177 0
5178#endif
5179 },
5180 {"python3_compiled",
5181#if defined(FEAT_PYTHON3)
5182 1
5183#else
5184 0
5185#endif
5186 },
5187 {"python3_dynamic",
5188#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
5189 1
5190#else
5191 0
5192#endif
5193 },
5194 {"python3",
5195#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
5196 1
5197#else
5198 0
5199#endif
5200 },
5201 {"popupwin",
5202#ifdef FEAT_PROP_POPUP
5203 1
5204#else
5205 0
5206#endif
5207 },
5208 {"postscript",
5209#ifdef FEAT_POSTSCRIPT
5210 1
5211#else
5212 0
5213#endif
5214 },
5215 {"printer",
5216#ifdef FEAT_PRINTER
5217 1
5218#else
5219 0
5220#endif
5221 },
5222 {"profile",
5223#ifdef FEAT_PROFILE
5224 1
5225#else
5226 0
5227#endif
5228 },
5229 {"reltime",
5230#ifdef FEAT_RELTIME
5231 1
5232#else
5233 0
5234#endif
5235 },
5236 {"quickfix",
5237#ifdef FEAT_QUICKFIX
5238 1
5239#else
5240 0
5241#endif
5242 },
5243 {"rightleft",
5244#ifdef FEAT_RIGHTLEFT
5245 1
5246#else
5247 0
5248#endif
5249 },
5250 {"ruby",
5251#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5252 1
5253#else
5254 0
5255#endif
5256 },
5257 {"scrollbind", 1},
5258 {"showcmd",
5259#ifdef FEAT_CMDL_INFO
5260 1
5261#else
5262 0
5263#endif
5264 },
5265 {"cmdline_info",
5266#ifdef FEAT_CMDL_INFO
5267 1
5268#else
5269 0
5270#endif
5271 },
5272 {"signs",
5273#ifdef FEAT_SIGNS
5274 1
5275#else
5276 0
5277#endif
5278 },
5279 {"smartindent",
5280#ifdef FEAT_SMARTINDENT
5281 1
5282#else
5283 0
5284#endif
5285 },
5286 {"startuptime",
5287#ifdef STARTUPTIME
5288 1
5289#else
5290 0
5291#endif
5292 },
5293 {"statusline",
5294#ifdef FEAT_STL_OPT
5295 1
5296#else
5297 0
5298#endif
5299 },
5300 {"netbeans_intg",
5301#ifdef FEAT_NETBEANS_INTG
5302 1
5303#else
5304 0
5305#endif
5306 },
5307 {"sound",
5308#ifdef FEAT_SOUND
5309 1
5310#else
5311 0
5312#endif
5313 },
5314 {"spell",
5315#ifdef FEAT_SPELL
5316 1
5317#else
5318 0
5319#endif
5320 },
5321 {"syntax",
5322#ifdef FEAT_SYN_HL
5323 1
5324#else
5325 0
5326#endif
5327 },
5328 {"system",
5329#if defined(USE_SYSTEM) || !defined(UNIX)
5330 1
5331#else
5332 0
5333#endif
5334 },
5335 {"tag_binary",
5336#ifdef FEAT_TAG_BINS
5337 1
5338#else
5339 0
5340#endif
5341 },
5342 {"tcl",
5343#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5344 1
5345#else
5346 0
5347#endif
5348 },
5349 {"termguicolors",
5350#ifdef FEAT_TERMGUICOLORS
5351 1
5352#else
5353 0
5354#endif
5355 },
5356 {"terminal",
5357#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5358 1
5359#else
5360 0
5361#endif
5362 },
5363 {"terminfo",
5364#ifdef TERMINFO
5365 1
5366#else
5367 0
5368#endif
5369 },
5370 {"termresponse",
5371#ifdef FEAT_TERMRESPONSE
5372 1
5373#else
5374 0
5375#endif
5376 },
5377 {"textobjects",
5378#ifdef FEAT_TEXTOBJ
5379 1
5380#else
5381 0
5382#endif
5383 },
5384 {"textprop",
5385#ifdef FEAT_PROP_POPUP
5386 1
5387#else
5388 0
5389#endif
5390 },
5391 {"tgetent",
5392#ifdef HAVE_TGETENT
5393 1
5394#else
5395 0
5396#endif
5397 },
5398 {"timers",
5399#ifdef FEAT_TIMERS
5400 1
5401#else
5402 0
5403#endif
5404 },
5405 {"title",
5406#ifdef FEAT_TITLE
5407 1
5408#else
5409 0
5410#endif
5411 },
5412 {"toolbar",
5413#ifdef FEAT_TOOLBAR
5414 1
5415#else
5416 0
5417#endif
5418 },
5419 {"unnamedplus",
5420#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5421 1
5422#else
5423 0
5424#endif
5425 },
5426 {"user-commands", 1}, // was accidentally included in 5.4
5427 {"user_commands", 1},
5428 {"vartabs",
5429#ifdef FEAT_VARTABS
5430 1
5431#else
5432 0
5433#endif
5434 },
5435 {"vertsplit", 1},
5436 {"viminfo",
5437#ifdef FEAT_VIMINFO
5438 1
5439#else
5440 0
5441#endif
5442 },
5443 {"vimscript-1", 1},
5444 {"vimscript-2", 1},
5445 {"vimscript-3", 1},
5446 {"vimscript-4", 1},
5447 {"virtualedit", 1},
5448 {"visual", 1},
5449 {"visualextra", 1},
5450 {"vreplace", 1},
5451 {"vtp",
5452#ifdef FEAT_VTP
5453 1
5454#else
5455 0
5456#endif
5457 },
5458 {"wildignore",
5459#ifdef FEAT_WILDIGN
5460 1
5461#else
5462 0
5463#endif
5464 },
5465 {"wildmenu",
5466#ifdef FEAT_WILDMENU
5467 1
5468#else
5469 0
5470#endif
5471 },
5472 {"windows", 1},
5473 {"winaltkeys",
5474#ifdef FEAT_WAK
5475 1
5476#else
5477 0
5478#endif
5479 },
5480 {"writebackup",
5481#ifdef FEAT_WRITEBACKUP
5482 1
5483#else
5484 0
5485#endif
5486 },
5487 {"xim",
5488#ifdef FEAT_XIM
5489 1
5490#else
5491 0
5492#endif
5493 },
5494 {"xfontset",
5495#ifdef FEAT_XFONTSET
5496 1
5497#else
5498 0
5499#endif
5500 },
5501 {"xpm",
5502#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5503 1
5504#else
5505 0
5506#endif
5507 },
5508 {"xpm_w32", // for backward compatibility
5509#ifdef FEAT_XPM_W32
5510 1
5511#else
5512 0
5513#endif
5514 },
5515 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005516#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005517 1
5518#else
5519 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005520#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005521 },
5522 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005523#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005524 1
5525#else
5526 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005527#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005528 },
5529 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005530#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005531 1
5532#else
5533 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005534#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005535 },
5536 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005537#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005538 1
5539#else
5540 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005541#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005542 },
5543 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005544#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005545 1
5546#else
5547 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005548#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005549 },
5550 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005551 };
5552
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005553 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005554 for (i = 0; has_list[i].name != NULL; ++i)
5555 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005556 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005557 x = TRUE;
5558 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005559 break;
5560 }
5561
Bram Moolenaar79296512020-03-22 16:17:14 +01005562 // features also in has_list[] but sometimes enabled at runtime
5563 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005564 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005565 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005566 {
5567 // intentionally empty
5568 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005569#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005570 else if (STRICMP(name, "balloon_multiline") == 0)
5571 n = multiline_balloon_available();
5572#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005573#ifdef VIMDLL
5574 else if (STRICMP(name, "filterpipe") == 0)
5575 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005576#endif
5577#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5578 else if (STRICMP(name, "iconv") == 0)
5579 n = iconv_enabled(FALSE);
5580#endif
5581#ifdef DYNAMIC_LUA
5582 else if (STRICMP(name, "lua") == 0)
5583 n = lua_enabled(FALSE);
5584#endif
5585#ifdef DYNAMIC_MZSCHEME
5586 else if (STRICMP(name, "mzscheme") == 0)
5587 n = mzscheme_enabled(FALSE);
5588#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005589#ifdef DYNAMIC_PERL
5590 else if (STRICMP(name, "perl") == 0)
5591 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005592#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005593#ifdef DYNAMIC_PYTHON
5594 else if (STRICMP(name, "python") == 0)
5595 n = python_enabled(FALSE);
5596#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005597#ifdef DYNAMIC_PYTHON3
5598 else if (STRICMP(name, "python3") == 0)
5599 n = python3_enabled(FALSE);
5600#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005601#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5602 else if (STRICMP(name, "pythonx") == 0)
5603 {
5604# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5605 if (p_pyx == 0)
5606 n = python3_enabled(FALSE) || python_enabled(FALSE);
5607 else if (p_pyx == 3)
5608 n = python3_enabled(FALSE);
5609 else if (p_pyx == 2)
5610 n = python_enabled(FALSE);
5611# elif defined(DYNAMIC_PYTHON)
5612 n = python_enabled(FALSE);
5613# elif defined(DYNAMIC_PYTHON3)
5614 n = python3_enabled(FALSE);
5615# endif
5616 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005617#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005618#ifdef DYNAMIC_RUBY
5619 else if (STRICMP(name, "ruby") == 0)
5620 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005621#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005622#ifdef DYNAMIC_TCL
5623 else if (STRICMP(name, "tcl") == 0)
5624 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005625#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005626#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005627 else if (STRICMP(name, "terminal") == 0)
5628 n = terminal_enabled();
5629#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005630 }
5631
Bram Moolenaar79296512020-03-22 16:17:14 +01005632 // features not in has_list[]
5633 if (x == FALSE)
5634 {
5635 if (STRNICMP(name, "patch", 5) == 0)
5636 {
5637 x = TRUE;
5638 if (name[5] == '-'
5639 && STRLEN(name) >= 11
5640 && vim_isdigit(name[6])
5641 && vim_isdigit(name[8])
5642 && vim_isdigit(name[10]))
5643 {
5644 int major = atoi((char *)name + 6);
5645 int minor = atoi((char *)name + 8);
5646
5647 // Expect "patch-9.9.01234".
5648 n = (major < VIM_VERSION_MAJOR
5649 || (major == VIM_VERSION_MAJOR
5650 && (minor < VIM_VERSION_MINOR
5651 || (minor == VIM_VERSION_MINOR
5652 && has_patch(atoi((char *)name + 10))))));
5653 }
5654 else
5655 n = has_patch(atoi((char *)name + 5));
5656 }
5657 else if (STRICMP(name, "vim_starting") == 0)
5658 {
5659 x = TRUE;
5660 n = (starting != 0);
5661 }
5662 else if (STRICMP(name, "ttyin") == 0)
5663 {
5664 x = TRUE;
5665 n = mch_input_isatty();
5666 }
5667 else if (STRICMP(name, "ttyout") == 0)
5668 {
5669 x = TRUE;
5670 n = stdout_isatty;
5671 }
5672 else if (STRICMP(name, "multi_byte_encoding") == 0)
5673 {
5674 x = TRUE;
5675 n = has_mbyte;
5676 }
5677 else if (STRICMP(name, "gui_running") == 0)
5678 {
5679 x = TRUE;
5680#ifdef FEAT_GUI
5681 n = (gui.in_use || gui.starting);
5682#endif
5683 }
5684 else if (STRICMP(name, "browse") == 0)
5685 {
5686 x = TRUE;
5687#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5688 n = gui.in_use; // gui_mch_browse() works when GUI is running
5689#endif
5690 }
5691 else if (STRICMP(name, "syntax_items") == 0)
5692 {
5693 x = TRUE;
5694#ifdef FEAT_SYN_HL
5695 n = syntax_present(curwin);
5696#endif
5697 }
5698 else if (STRICMP(name, "vcon") == 0)
5699 {
5700 x = TRUE;
5701#ifdef FEAT_VTP
5702 n = is_term_win32() && has_vtp_working();
5703#endif
5704 }
5705 else if (STRICMP(name, "netbeans_enabled") == 0)
5706 {
5707 x = TRUE;
5708#ifdef FEAT_NETBEANS_INTG
5709 n = netbeans_active();
5710#endif
5711 }
5712 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5713 {
5714 x = TRUE;
5715#ifdef FEAT_MOUSE_GPM
5716 n = gpm_enabled();
5717#endif
5718 }
5719 else if (STRICMP(name, "conpty") == 0)
5720 {
5721 x = TRUE;
5722#if defined(FEAT_TERMINAL) && defined(MSWIN)
5723 n = use_conpty();
5724#endif
5725 }
5726 else if (STRICMP(name, "clipboard_working") == 0)
5727 {
5728 x = TRUE;
5729#ifdef FEAT_CLIPBOARD
5730 n = clip_star.available;
5731#endif
5732 }
5733 }
5734
Bram Moolenaar04637e22020-09-05 18:45:29 +02005735 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005736 // return whether feature could ever be enabled
5737 rettv->vval.v_number = x;
5738 else
5739 // return whether feature is enabled
5740 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005741}
5742
5743/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005744 * Return TRUE if "feature" can change later.
5745 * Also when checking for the feature has side effects, such as loading a DLL.
5746 */
5747 int
5748dynamic_feature(char_u *feature)
5749{
5750 return (feature == NULL
5751#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5752 || STRICMP(feature, "balloon_multiline") == 0
5753#endif
5754#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5755 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5756#endif
5757#ifdef VIMDLL
5758 || STRICMP(feature, "filterpipe") == 0
5759#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005760#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005761 // this can only change on Unix where the ":gui" command could be
5762 // used.
5763 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5764#endif
5765#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5766 || STRICMP(feature, "iconv") == 0
5767#endif
5768#ifdef DYNAMIC_LUA
5769 || STRICMP(feature, "lua") == 0
5770#endif
5771#ifdef FEAT_MOUSE_GPM
5772 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5773#endif
5774#ifdef DYNAMIC_MZSCHEME
5775 || STRICMP(feature, "mzscheme") == 0
5776#endif
5777#ifdef FEAT_NETBEANS_INTG
5778 || STRICMP(feature, "netbeans_enabled") == 0
5779#endif
5780#ifdef DYNAMIC_PERL
5781 || STRICMP(feature, "perl") == 0
5782#endif
5783#ifdef DYNAMIC_PYTHON
5784 || STRICMP(feature, "python") == 0
5785#endif
5786#ifdef DYNAMIC_PYTHON3
5787 || STRICMP(feature, "python3") == 0
5788#endif
5789#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5790 || STRICMP(feature, "pythonx") == 0
5791#endif
5792#ifdef DYNAMIC_RUBY
5793 || STRICMP(feature, "ruby") == 0
5794#endif
5795#ifdef FEAT_SYN_HL
5796 || STRICMP(feature, "syntax_items") == 0
5797#endif
5798#ifdef DYNAMIC_TCL
5799 || STRICMP(feature, "tcl") == 0
5800#endif
5801 // once "starting" is zero it will stay that way
5802 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5803 || STRICMP(feature, "multi_byte_encoding") == 0
5804#if defined(FEAT_TERMINAL) && defined(MSWIN)
5805 || STRICMP(feature, "conpty") == 0
5806#endif
5807 );
5808}
5809
5810/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005811 * "haslocaldir()" function
5812 */
5813 static void
5814f_haslocaldir(typval_T *argvars, typval_T *rettv)
5815{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005816 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005817 win_T *wp = NULL;
5818
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005819 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5820
5821 // Check for window-local and tab-local directories
5822 if (wp != NULL && wp->w_localdir != NULL)
5823 rettv->vval.v_number = 1;
5824 else if (tp != NULL && tp->tp_localdir != NULL)
5825 rettv->vval.v_number = 2;
5826 else
5827 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005828}
5829
5830/*
5831 * "hasmapto()" function
5832 */
5833 static void
5834f_hasmapto(typval_T *argvars, typval_T *rettv)
5835{
5836 char_u *name;
5837 char_u *mode;
5838 char_u buf[NUMBUFLEN];
5839 int abbr = FALSE;
5840
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005841 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005842 if (argvars[1].v_type == VAR_UNKNOWN)
5843 mode = (char_u *)"nvo";
5844 else
5845 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005846 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005847 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005848 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005849 }
5850
5851 if (map_to_exists(name, mode, abbr))
5852 rettv->vval.v_number = TRUE;
5853 else
5854 rettv->vval.v_number = FALSE;
5855}
5856
5857/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005858 * "highlightID(name)" function
5859 */
5860 static void
5861f_hlID(typval_T *argvars, typval_T *rettv)
5862{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005863 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005864}
5865
5866/*
5867 * "highlight_exists()" function
5868 */
5869 static void
5870f_hlexists(typval_T *argvars, typval_T *rettv)
5871{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005872 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005873}
5874
5875/*
5876 * "hostname()" function
5877 */
5878 static void
5879f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5880{
5881 char_u hostname[256];
5882
5883 mch_get_host_name(hostname, 256);
5884 rettv->v_type = VAR_STRING;
5885 rettv->vval.v_string = vim_strsave(hostname);
5886}
5887
5888/*
5889 * iconv() function
5890 */
5891 static void
5892f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5893{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005894 char_u buf1[NUMBUFLEN];
5895 char_u buf2[NUMBUFLEN];
5896 char_u *from, *to, *str;
5897 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005898
5899 rettv->v_type = VAR_STRING;
5900 rettv->vval.v_string = NULL;
5901
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005902 str = tv_get_string(&argvars[0]);
5903 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5904 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005905 vimconv.vc_type = CONV_NONE;
5906 convert_setup(&vimconv, from, to);
5907
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005908 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005909 if (vimconv.vc_type == CONV_NONE)
5910 rettv->vval.v_string = vim_strsave(str);
5911 else
5912 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5913
5914 convert_setup(&vimconv, NULL, NULL);
5915 vim_free(from);
5916 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005917}
5918
5919/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005920 * "index()" function
5921 */
5922 static void
5923f_index(typval_T *argvars, typval_T *rettv)
5924{
5925 list_T *l;
5926 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005927 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005928 long idx = 0;
5929 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005930 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005931
5932 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005933 if (argvars[0].v_type == VAR_BLOB)
5934 {
5935 typval_T tv;
5936 int start = 0;
5937
5938 if (argvars[2].v_type != VAR_UNKNOWN)
5939 {
5940 start = tv_get_number_chk(&argvars[2], &error);
5941 if (error)
5942 return;
5943 }
5944 b = argvars[0].vval.v_blob;
5945 if (b == NULL)
5946 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005947 if (start < 0)
5948 {
5949 start = blob_len(b) + start;
5950 if (start < 0)
5951 start = 0;
5952 }
5953
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005954 for (idx = start; idx < blob_len(b); ++idx)
5955 {
5956 tv.v_type = VAR_NUMBER;
5957 tv.vval.v_number = blob_get(b, idx);
5958 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5959 {
5960 rettv->vval.v_number = idx;
5961 return;
5962 }
5963 }
5964 return;
5965 }
5966 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005967 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005968 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005969 return;
5970 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005971
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005972 l = argvars[0].vval.v_list;
5973 if (l != NULL)
5974 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005975 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005976 item = l->lv_first;
5977 if (argvars[2].v_type != VAR_UNKNOWN)
5978 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005979 // Start at specified item. Use the cached index that list_find()
5980 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005981 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005982 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005983 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005984 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005985 if (error)
5986 item = NULL;
5987 }
5988
5989 for ( ; item != NULL; item = item->li_next, ++idx)
5990 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5991 {
5992 rettv->vval.v_number = idx;
5993 break;
5994 }
5995 }
5996}
5997
5998static int inputsecret_flag = 0;
5999
6000/*
6001 * "input()" function
6002 * Also handles inputsecret() when inputsecret is set.
6003 */
6004 static void
6005f_input(typval_T *argvars, typval_T *rettv)
6006{
6007 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6008}
6009
6010/*
6011 * "inputdialog()" function
6012 */
6013 static void
6014f_inputdialog(typval_T *argvars, typval_T *rettv)
6015{
6016#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006017 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006018 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6019 {
6020 char_u *message;
6021 char_u buf[NUMBUFLEN];
6022 char_u *defstr = (char_u *)"";
6023
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006024 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006025 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006026 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006027 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6028 else
6029 IObuff[0] = NUL;
6030 if (message != NULL && defstr != NULL
6031 && do_dialog(VIM_QUESTION, NULL, message,
6032 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6033 rettv->vval.v_string = vim_strsave(IObuff);
6034 else
6035 {
6036 if (message != NULL && defstr != NULL
6037 && argvars[1].v_type != VAR_UNKNOWN
6038 && argvars[2].v_type != VAR_UNKNOWN)
6039 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006040 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006041 else
6042 rettv->vval.v_string = NULL;
6043 }
6044 rettv->v_type = VAR_STRING;
6045 }
6046 else
6047#endif
6048 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6049}
6050
6051/*
6052 * "inputlist()" function
6053 */
6054 static void
6055f_inputlist(typval_T *argvars, typval_T *rettv)
6056{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006057 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006058 listitem_T *li;
6059 int selected;
6060 int mouse_used;
6061
6062#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006063 // While starting up, there is no place to enter text. When running tests
6064 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006065 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006066 return;
6067#endif
6068 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6069 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006070 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006071 return;
6072 }
6073
6074 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006075 msg_row = Rows - 1; // for when 'cmdheight' > 1
6076 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006077 msg_scroll = TRUE;
6078 msg_clr_eos();
6079
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006080 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006081 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006082 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006083 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006084 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006085 msg_putchar('\n');
6086 }
6087
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006088 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006089 selected = prompt_for_number(&mouse_used);
6090 if (mouse_used)
6091 selected -= lines_left;
6092
6093 rettv->vval.v_number = selected;
6094}
6095
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006096static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6097
6098/*
6099 * "inputrestore()" function
6100 */
6101 static void
6102f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6103{
6104 if (ga_userinput.ga_len > 0)
6105 {
6106 --ga_userinput.ga_len;
6107 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6108 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006109 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006110 }
6111 else if (p_verbose > 1)
6112 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006113 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006114 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006115 }
6116}
6117
6118/*
6119 * "inputsave()" function
6120 */
6121 static void
6122f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6123{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006124 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006125 if (ga_grow(&ga_userinput, 1) == OK)
6126 {
6127 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6128 + ga_userinput.ga_len);
6129 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006130 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006131 }
6132 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006133 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006134}
6135
6136/*
6137 * "inputsecret()" function
6138 */
6139 static void
6140f_inputsecret(typval_T *argvars, typval_T *rettv)
6141{
6142 ++cmdline_star;
6143 ++inputsecret_flag;
6144 f_input(argvars, rettv);
6145 --cmdline_star;
6146 --inputsecret_flag;
6147}
6148
6149/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01006150 * "interrupt()" function
6151 */
6152 static void
6153f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6154{
6155 got_int = TRUE;
6156}
6157
6158/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006159 * "invert(expr)" function
6160 */
6161 static void
6162f_invert(typval_T *argvars, typval_T *rettv)
6163{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006164 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006165}
6166
6167/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006168 * "islocked()" function
6169 */
6170 static void
6171f_islocked(typval_T *argvars, typval_T *rettv)
6172{
6173 lval_T lv;
6174 char_u *end;
6175 dictitem_T *di;
6176
6177 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006178 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006179 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006180 if (end != NULL && lv.ll_name != NULL)
6181 {
6182 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02006183 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006184 else
6185 {
6186 if (lv.ll_tv == NULL)
6187 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006188 di = find_var(lv.ll_name, NULL, TRUE);
6189 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006190 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006191 // Consider a variable locked when:
6192 // 1. the variable itself is locked
6193 // 2. the value of the variable is locked.
6194 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01006195 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6196 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006197 }
6198 }
6199 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006200 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006201 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006202 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006203 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006204 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006205 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6206 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006207 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006208 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6209 }
6210 }
6211
6212 clear_lval(&lv);
6213}
6214
6215#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6216/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006217 * "isinf()" function
6218 */
6219 static void
6220f_isinf(typval_T *argvars, typval_T *rettv)
6221{
6222 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6223 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6224}
6225
6226/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006227 * "isnan()" function
6228 */
6229 static void
6230f_isnan(typval_T *argvars, typval_T *rettv)
6231{
6232 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6233 && isnan(argvars[0].vval.v_float);
6234}
6235#endif
6236
6237/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006238 * "last_buffer_nr()" function.
6239 */
6240 static void
6241f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6242{
6243 int n = 0;
6244 buf_T *buf;
6245
Bram Moolenaar29323592016-07-24 22:04:11 +02006246 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247 if (n < buf->b_fnum)
6248 n = buf->b_fnum;
6249
6250 rettv->vval.v_number = n;
6251}
6252
6253/*
6254 * "len()" function
6255 */
6256 static void
6257f_len(typval_T *argvars, typval_T *rettv)
6258{
6259 switch (argvars[0].v_type)
6260 {
6261 case VAR_STRING:
6262 case VAR_NUMBER:
6263 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006264 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006265 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006266 case VAR_BLOB:
6267 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6268 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006269 case VAR_LIST:
6270 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6271 break;
6272 case VAR_DICT:
6273 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6274 break;
6275 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006276 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006278 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006279 case VAR_SPECIAL:
6280 case VAR_FLOAT:
6281 case VAR_FUNC:
6282 case VAR_PARTIAL:
6283 case VAR_JOB:
6284 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006285 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006286 break;
6287 }
6288}
6289
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006290 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006291libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006292{
6293#ifdef FEAT_LIBCALL
6294 char_u *string_in;
6295 char_u **string_result;
6296 int nr_result;
6297#endif
6298
6299 rettv->v_type = type;
6300 if (type != VAR_NUMBER)
6301 rettv->vval.v_string = NULL;
6302
6303 if (check_restricted() || check_secure())
6304 return;
6305
6306#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006307 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006308 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6309 {
6310 string_in = NULL;
6311 if (argvars[2].v_type == VAR_STRING)
6312 string_in = argvars[2].vval.v_string;
6313 if (type == VAR_NUMBER)
6314 string_result = NULL;
6315 else
6316 string_result = &rettv->vval.v_string;
6317 if (mch_libcall(argvars[0].vval.v_string,
6318 argvars[1].vval.v_string,
6319 string_in,
6320 argvars[2].vval.v_number,
6321 string_result,
6322 &nr_result) == OK
6323 && type == VAR_NUMBER)
6324 rettv->vval.v_number = nr_result;
6325 }
6326#endif
6327}
6328
6329/*
6330 * "libcall()" function
6331 */
6332 static void
6333f_libcall(typval_T *argvars, typval_T *rettv)
6334{
6335 libcall_common(argvars, rettv, VAR_STRING);
6336}
6337
6338/*
6339 * "libcallnr()" function
6340 */
6341 static void
6342f_libcallnr(typval_T *argvars, typval_T *rettv)
6343{
6344 libcall_common(argvars, rettv, VAR_NUMBER);
6345}
6346
6347/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006348 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006349 */
6350 static void
6351f_line(typval_T *argvars, typval_T *rettv)
6352{
6353 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006354 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006355 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006356 int id;
6357 tabpage_T *tp;
6358 win_T *wp;
6359 win_T *save_curwin;
6360 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006361
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006362 if (argvars[1].v_type != VAR_UNKNOWN)
6363 {
6364 // use window specified in the second argument
6365 id = (int)tv_get_number(&argvars[1]);
6366 wp = win_id2wp_tp(id, &tp);
6367 if (wp != NULL && tp != NULL)
6368 {
6369 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6370 == OK)
6371 {
6372 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006373 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006374 }
6375 restore_win_noblock(save_curwin, save_curtab, TRUE);
6376 }
6377 }
6378 else
6379 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006380 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006381
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006382 if (fp != NULL)
6383 lnum = fp->lnum;
6384 rettv->vval.v_number = lnum;
6385}
6386
6387/*
6388 * "line2byte(lnum)" function
6389 */
6390 static void
6391f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6392{
6393#ifndef FEAT_BYTEOFF
6394 rettv->vval.v_number = -1;
6395#else
6396 linenr_T lnum;
6397
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006398 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006399 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6400 rettv->vval.v_number = -1;
6401 else
6402 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6403 if (rettv->vval.v_number >= 0)
6404 ++rettv->vval.v_number;
6405#endif
6406}
6407
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408#ifdef FEAT_FLOAT
6409/*
6410 * "log()" function
6411 */
6412 static void
6413f_log(typval_T *argvars, typval_T *rettv)
6414{
6415 float_T f = 0.0;
6416
6417 rettv->v_type = VAR_FLOAT;
6418 if (get_float_arg(argvars, &f) == OK)
6419 rettv->vval.v_float = log(f);
6420 else
6421 rettv->vval.v_float = 0.0;
6422}
6423
6424/*
6425 * "log10()" function
6426 */
6427 static void
6428f_log10(typval_T *argvars, typval_T *rettv)
6429{
6430 float_T f = 0.0;
6431
6432 rettv->v_type = VAR_FLOAT;
6433 if (get_float_arg(argvars, &f) == OK)
6434 rettv->vval.v_float = log10(f);
6435 else
6436 rettv->vval.v_float = 0.0;
6437}
6438#endif
6439
6440#ifdef FEAT_LUA
6441/*
6442 * "luaeval()" function
6443 */
6444 static void
6445f_luaeval(typval_T *argvars, typval_T *rettv)
6446{
6447 char_u *str;
6448 char_u buf[NUMBUFLEN];
6449
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006450 if (check_restricted() || check_secure())
6451 return;
6452
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006453 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006454 do_luaeval(str, argvars + 1, rettv);
6455}
6456#endif
6457
6458/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006459 * "maparg()" function
6460 */
6461 static void
6462f_maparg(typval_T *argvars, typval_T *rettv)
6463{
6464 get_maparg(argvars, rettv, TRUE);
6465}
6466
6467/*
6468 * "mapcheck()" function
6469 */
6470 static void
6471f_mapcheck(typval_T *argvars, typval_T *rettv)
6472{
6473 get_maparg(argvars, rettv, FALSE);
6474}
6475
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006476typedef enum
6477{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006478 MATCH_END, // matchend()
6479 MATCH_MATCH, // match()
6480 MATCH_STR, // matchstr()
6481 MATCH_LIST, // matchlist()
6482 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006483} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006484
6485 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006486find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006487{
6488 char_u *str = NULL;
6489 long len = 0;
6490 char_u *expr = NULL;
6491 char_u *pat;
6492 regmatch_T regmatch;
6493 char_u patbuf[NUMBUFLEN];
6494 char_u strbuf[NUMBUFLEN];
6495 char_u *save_cpo;
6496 long start = 0;
6497 long nth = 1;
6498 colnr_T startcol = 0;
6499 int match = 0;
6500 list_T *l = NULL;
6501 listitem_T *li = NULL;
6502 long idx = 0;
6503 char_u *tofree = NULL;
6504
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006505 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006507 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006508
6509 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006510 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006511 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006512 // type MATCH_LIST: return empty list when there are no matches.
6513 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006514 if (rettv_list_alloc(rettv) == FAIL)
6515 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006516 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006517 && (list_append_string(rettv->vval.v_list,
6518 (char_u *)"", 0) == FAIL
6519 || list_append_number(rettv->vval.v_list,
6520 (varnumber_T)-1) == FAIL
6521 || list_append_number(rettv->vval.v_list,
6522 (varnumber_T)-1) == FAIL
6523 || list_append_number(rettv->vval.v_list,
6524 (varnumber_T)-1) == FAIL))
6525 {
6526 list_free(rettv->vval.v_list);
6527 rettv->vval.v_list = NULL;
6528 goto theend;
6529 }
6530 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006531 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006532 {
6533 rettv->v_type = VAR_STRING;
6534 rettv->vval.v_string = NULL;
6535 }
6536
6537 if (argvars[0].v_type == VAR_LIST)
6538 {
6539 if ((l = argvars[0].vval.v_list) == NULL)
6540 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006541 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006542 li = l->lv_first;
6543 }
6544 else
6545 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006546 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006547 len = (long)STRLEN(str);
6548 }
6549
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006550 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006551 if (pat == NULL)
6552 goto theend;
6553
6554 if (argvars[2].v_type != VAR_UNKNOWN)
6555 {
6556 int error = FALSE;
6557
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006558 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006559 if (error)
6560 goto theend;
6561 if (l != NULL)
6562 {
6563 li = list_find(l, start);
6564 if (li == NULL)
6565 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006566 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006567 }
6568 else
6569 {
6570 if (start < 0)
6571 start = 0;
6572 if (start > len)
6573 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006574 // When "count" argument is there ignore matches before "start",
6575 // otherwise skip part of the string. Differs when pattern is "^"
6576 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006577 if (argvars[3].v_type != VAR_UNKNOWN)
6578 startcol = start;
6579 else
6580 {
6581 str += start;
6582 len -= start;
6583 }
6584 }
6585
6586 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006587 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006588 if (error)
6589 goto theend;
6590 }
6591
6592 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6593 if (regmatch.regprog != NULL)
6594 {
6595 regmatch.rm_ic = p_ic;
6596
6597 for (;;)
6598 {
6599 if (l != NULL)
6600 {
6601 if (li == NULL)
6602 {
6603 match = FALSE;
6604 break;
6605 }
6606 vim_free(tofree);
6607 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6608 if (str == NULL)
6609 break;
6610 }
6611
6612 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6613
6614 if (match && --nth <= 0)
6615 break;
6616 if (l == NULL && !match)
6617 break;
6618
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006619 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006620 if (l != NULL)
6621 {
6622 li = li->li_next;
6623 ++idx;
6624 }
6625 else
6626 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006627 startcol = (colnr_T)(regmatch.startp[0]
6628 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006629 if (startcol > (colnr_T)len
6630 || str + startcol <= regmatch.startp[0])
6631 {
6632 match = FALSE;
6633 break;
6634 }
6635 }
6636 }
6637
6638 if (match)
6639 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006640 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006641 {
6642 listitem_T *li1 = rettv->vval.v_list->lv_first;
6643 listitem_T *li2 = li1->li_next;
6644 listitem_T *li3 = li2->li_next;
6645 listitem_T *li4 = li3->li_next;
6646
6647 vim_free(li1->li_tv.vval.v_string);
6648 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006649 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006650 li3->li_tv.vval.v_number =
6651 (varnumber_T)(regmatch.startp[0] - expr);
6652 li4->li_tv.vval.v_number =
6653 (varnumber_T)(regmatch.endp[0] - expr);
6654 if (l != NULL)
6655 li2->li_tv.vval.v_number = (varnumber_T)idx;
6656 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006657 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006658 {
6659 int i;
6660
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006661 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006662 for (i = 0; i < NSUBEXP; ++i)
6663 {
6664 if (regmatch.endp[i] == NULL)
6665 {
6666 if (list_append_string(rettv->vval.v_list,
6667 (char_u *)"", 0) == FAIL)
6668 break;
6669 }
6670 else if (list_append_string(rettv->vval.v_list,
6671 regmatch.startp[i],
6672 (int)(regmatch.endp[i] - regmatch.startp[i]))
6673 == FAIL)
6674 break;
6675 }
6676 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006677 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006678 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006679 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006680 if (l != NULL)
6681 copy_tv(&li->li_tv, rettv);
6682 else
6683 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006684 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006685 }
6686 else if (l != NULL)
6687 rettv->vval.v_number = idx;
6688 else
6689 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006690 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006691 rettv->vval.v_number =
6692 (varnumber_T)(regmatch.startp[0] - str);
6693 else
6694 rettv->vval.v_number =
6695 (varnumber_T)(regmatch.endp[0] - str);
6696 rettv->vval.v_number += (varnumber_T)(str - expr);
6697 }
6698 }
6699 vim_regfree(regmatch.regprog);
6700 }
6701
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006702theend:
6703 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006704 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006705 listitem_remove(rettv->vval.v_list,
6706 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006707 vim_free(tofree);
6708 p_cpo = save_cpo;
6709}
6710
6711/*
6712 * "match()" function
6713 */
6714 static void
6715f_match(typval_T *argvars, typval_T *rettv)
6716{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006717 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006718}
6719
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006720/*
6721 * "matchend()" function
6722 */
6723 static void
6724f_matchend(typval_T *argvars, typval_T *rettv)
6725{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006726 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006727}
6728
6729/*
6730 * "matchlist()" function
6731 */
6732 static void
6733f_matchlist(typval_T *argvars, typval_T *rettv)
6734{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006735 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006736}
6737
6738/*
6739 * "matchstr()" function
6740 */
6741 static void
6742f_matchstr(typval_T *argvars, typval_T *rettv)
6743{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006744 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006745}
6746
6747/*
6748 * "matchstrpos()" function
6749 */
6750 static void
6751f_matchstrpos(typval_T *argvars, typval_T *rettv)
6752{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006753 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006754}
6755
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006756 static void
6757max_min(typval_T *argvars, typval_T *rettv, int domax)
6758{
6759 varnumber_T n = 0;
6760 varnumber_T i;
6761 int error = FALSE;
6762
6763 if (argvars[0].v_type == VAR_LIST)
6764 {
6765 list_T *l;
6766 listitem_T *li;
6767
6768 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006769 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006770 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006771 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006772 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006773 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6774 n = l->lv_u.nonmat.lv_start;
6775 else
6776 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6777 * l->lv_u.nonmat.lv_stride;
6778 }
6779 else
6780 {
6781 li = l->lv_first;
6782 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006783 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006784 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006785 if (error)
6786 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006787 for (;;)
6788 {
6789 li = li->li_next;
6790 if (li == NULL)
6791 break;
6792 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006793 if (error)
6794 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006795 if (domax ? i > n : i < n)
6796 n = i;
6797 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006798 }
6799 }
6800 }
6801 }
6802 else if (argvars[0].v_type == VAR_DICT)
6803 {
6804 dict_T *d;
6805 int first = TRUE;
6806 hashitem_T *hi;
6807 int todo;
6808
6809 d = argvars[0].vval.v_dict;
6810 if (d != NULL)
6811 {
6812 todo = (int)d->dv_hashtab.ht_used;
6813 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6814 {
6815 if (!HASHITEM_EMPTY(hi))
6816 {
6817 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006818 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006819 if (error)
6820 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006821 if (first)
6822 {
6823 n = i;
6824 first = FALSE;
6825 }
6826 else if (domax ? i > n : i < n)
6827 n = i;
6828 }
6829 }
6830 }
6831 }
6832 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006833 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006834
6835 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006836}
6837
6838/*
6839 * "max()" function
6840 */
6841 static void
6842f_max(typval_T *argvars, typval_T *rettv)
6843{
6844 max_min(argvars, rettv, TRUE);
6845}
6846
6847/*
6848 * "min()" function
6849 */
6850 static void
6851f_min(typval_T *argvars, typval_T *rettv)
6852{
6853 max_min(argvars, rettv, FALSE);
6854}
6855
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006856#if defined(FEAT_MZSCHEME) || defined(PROTO)
6857/*
6858 * "mzeval()" function
6859 */
6860 static void
6861f_mzeval(typval_T *argvars, typval_T *rettv)
6862{
6863 char_u *str;
6864 char_u buf[NUMBUFLEN];
6865
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006866 if (check_restricted() || check_secure())
6867 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006868 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006869 do_mzeval(str, rettv);
6870}
6871
6872 void
6873mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6874{
6875 typval_T argvars[3];
6876
6877 argvars[0].v_type = VAR_STRING;
6878 argvars[0].vval.v_string = name;
6879 copy_tv(args, &argvars[1]);
6880 argvars[2].v_type = VAR_UNKNOWN;
6881 f_call(argvars, rettv);
6882 clear_tv(&argvars[1]);
6883}
6884#endif
6885
6886/*
6887 * "nextnonblank()" function
6888 */
6889 static void
6890f_nextnonblank(typval_T *argvars, typval_T *rettv)
6891{
6892 linenr_T lnum;
6893
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006894 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006895 {
6896 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6897 {
6898 lnum = 0;
6899 break;
6900 }
6901 if (*skipwhite(ml_get(lnum)) != NUL)
6902 break;
6903 }
6904 rettv->vval.v_number = lnum;
6905}
6906
6907/*
6908 * "nr2char()" function
6909 */
6910 static void
6911f_nr2char(typval_T *argvars, typval_T *rettv)
6912{
6913 char_u buf[NUMBUFLEN];
6914
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006915 if (has_mbyte)
6916 {
6917 int utf8 = 0;
6918
6919 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006920 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006921 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006922 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006924 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006925 }
6926 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006927 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006928 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006929 buf[1] = NUL;
6930 }
6931 rettv->v_type = VAR_STRING;
6932 rettv->vval.v_string = vim_strsave(buf);
6933}
6934
6935/*
6936 * "or(expr, expr)" function
6937 */
6938 static void
6939f_or(typval_T *argvars, typval_T *rettv)
6940{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006941 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6942 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006943}
6944
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006945#ifdef FEAT_PERL
6946/*
6947 * "perleval()" function
6948 */
6949 static void
6950f_perleval(typval_T *argvars, typval_T *rettv)
6951{
6952 char_u *str;
6953 char_u buf[NUMBUFLEN];
6954
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006955 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006956 do_perleval(str, rettv);
6957}
6958#endif
6959
6960#ifdef FEAT_FLOAT
6961/*
6962 * "pow()" function
6963 */
6964 static void
6965f_pow(typval_T *argvars, typval_T *rettv)
6966{
6967 float_T fx = 0.0, fy = 0.0;
6968
6969 rettv->v_type = VAR_FLOAT;
6970 if (get_float_arg(argvars, &fx) == OK
6971 && get_float_arg(&argvars[1], &fy) == OK)
6972 rettv->vval.v_float = pow(fx, fy);
6973 else
6974 rettv->vval.v_float = 0.0;
6975}
6976#endif
6977
6978/*
6979 * "prevnonblank()" function
6980 */
6981 static void
6982f_prevnonblank(typval_T *argvars, typval_T *rettv)
6983{
6984 linenr_T lnum;
6985
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006986 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006987 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6988 lnum = 0;
6989 else
6990 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6991 --lnum;
6992 rettv->vval.v_number = lnum;
6993}
6994
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006995// This dummy va_list is here because:
6996// - passing a NULL pointer doesn't work when va_list isn't a pointer
6997// - locally in the function results in a "used before set" warning
6998// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006999static va_list ap;
7000
7001/*
7002 * "printf()" function
7003 */
7004 static void
7005f_printf(typval_T *argvars, typval_T *rettv)
7006{
7007 char_u buf[NUMBUFLEN];
7008 int len;
7009 char_u *s;
7010 int saved_did_emsg = did_emsg;
7011 char *fmt;
7012
7013 rettv->v_type = VAR_STRING;
7014 rettv->vval.v_string = NULL;
7015
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007016 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007017 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007018 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007019 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007020 if (!did_emsg)
7021 {
7022 s = alloc(len + 1);
7023 if (s != NULL)
7024 {
7025 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007026 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
7027 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028 }
7029 }
7030 did_emsg |= saved_did_emsg;
7031}
7032
7033/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007034 * "pum_getpos()" function
7035 */
7036 static void
7037f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7038{
7039 if (rettv_dict_alloc(rettv) != OK)
7040 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007041 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007042}
7043
7044/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007045 * "pumvisible()" function
7046 */
7047 static void
7048f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7049{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007050 if (pum_visible())
7051 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007052}
7053
7054#ifdef FEAT_PYTHON3
7055/*
7056 * "py3eval()" function
7057 */
7058 static void
7059f_py3eval(typval_T *argvars, typval_T *rettv)
7060{
7061 char_u *str;
7062 char_u buf[NUMBUFLEN];
7063
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007064 if (check_restricted() || check_secure())
7065 return;
7066
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007067 if (p_pyx == 0)
7068 p_pyx = 3;
7069
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007070 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007071 do_py3eval(str, rettv);
7072}
7073#endif
7074
7075#ifdef FEAT_PYTHON
7076/*
7077 * "pyeval()" function
7078 */
7079 static void
7080f_pyeval(typval_T *argvars, typval_T *rettv)
7081{
7082 char_u *str;
7083 char_u buf[NUMBUFLEN];
7084
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007085 if (check_restricted() || check_secure())
7086 return;
7087
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007088 if (p_pyx == 0)
7089 p_pyx = 2;
7090
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007091 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007092 do_pyeval(str, rettv);
7093}
7094#endif
7095
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007096#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
7097/*
7098 * "pyxeval()" function
7099 */
7100 static void
7101f_pyxeval(typval_T *argvars, typval_T *rettv)
7102{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007103 if (check_restricted() || check_secure())
7104 return;
7105
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007106# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
7107 init_pyxversion();
7108 if (p_pyx == 2)
7109 f_pyeval(argvars, rettv);
7110 else
7111 f_py3eval(argvars, rettv);
7112# elif defined(FEAT_PYTHON)
7113 f_pyeval(argvars, rettv);
7114# elif defined(FEAT_PYTHON3)
7115 f_py3eval(argvars, rettv);
7116# endif
7117}
7118#endif
7119
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007120static UINT32_T srand_seed_for_testing = 0;
7121static int srand_seed_for_testing_is_used = FALSE;
7122
7123 static void
7124f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
7125{
7126 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007127 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007128 else
7129 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007130 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
7131 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007132 }
7133}
7134
7135 static void
7136init_srand(UINT32_T *x)
7137{
7138#ifndef MSWIN
7139 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
7140#endif
7141
7142 if (srand_seed_for_testing_is_used)
7143 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007144 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007145 return;
7146 }
7147#ifndef MSWIN
7148 if (dev_urandom_state != FAIL)
7149 {
7150 int fd = open("/dev/urandom", O_RDONLY);
7151 struct {
7152 union {
7153 UINT32_T number;
7154 char bytes[sizeof(UINT32_T)];
7155 } contents;
7156 } buf;
7157
7158 // Attempt reading /dev/urandom.
7159 if (fd == -1)
7160 dev_urandom_state = FAIL;
7161 else
7162 {
7163 buf.contents.number = 0;
7164 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
7165 != sizeof(UINT32_T))
7166 dev_urandom_state = FAIL;
7167 else
7168 {
7169 dev_urandom_state = OK;
7170 *x = buf.contents.number;
7171 }
7172 close(fd);
7173 }
7174 }
7175 if (dev_urandom_state != OK)
7176 // Reading /dev/urandom doesn't work, fall back to time().
7177#endif
7178 *x = vim_time();
7179}
7180
7181#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
7182#define SPLITMIX32(x, z) ( \
7183 z = (x += 0x9e3779b9), \
7184 z = (z ^ (z >> 16)) * 0x85ebca6b, \
7185 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
7186 z ^ (z >> 16) \
7187 )
7188#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
7189 result = ROTL(y * 5, 7) * 9; \
7190 t = y << 9; \
7191 z ^= x; \
7192 w ^= y; \
7193 y ^= z, x ^= w; \
7194 z ^= t; \
7195 w = ROTL(w, 11);
7196
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007197/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007198 * "rand()" function
7199 */
7200 static void
7201f_rand(typval_T *argvars, typval_T *rettv)
7202{
7203 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007204 static UINT32_T gx, gy, gz, gw;
7205 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007206 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01007207 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007208
7209 if (argvars[0].v_type == VAR_UNKNOWN)
7210 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007211 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007212 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007213 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007214 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007215 init_srand(&x);
7216
7217 gx = SPLITMIX32(x, z);
7218 gy = SPLITMIX32(x, z);
7219 gz = SPLITMIX32(x, z);
7220 gw = SPLITMIX32(x, z);
7221 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007222 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007223
7224 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007225 }
7226 else if (argvars[0].v_type == VAR_LIST)
7227 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007228 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007229 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007230 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007231
7232 lx = list_find(l, 0L);
7233 ly = list_find(l, 1L);
7234 lz = list_find(l, 2L);
7235 lw = list_find(l, 3L);
7236 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
7237 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
7238 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
7239 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
7240 x = (UINT32_T)lx->li_tv.vval.v_number;
7241 y = (UINT32_T)ly->li_tv.vval.v_number;
7242 z = (UINT32_T)lz->li_tv.vval.v_number;
7243 w = (UINT32_T)lw->li_tv.vval.v_number;
7244
7245 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
7246
7247 lx->li_tv.vval.v_number = (varnumber_T)x;
7248 ly->li_tv.vval.v_number = (varnumber_T)y;
7249 lz->li_tv.vval.v_number = (varnumber_T)z;
7250 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007251 }
7252 else
7253 goto theend;
7254
7255 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007256 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007257 return;
7258
7259theend:
7260 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007261 rettv->v_type = VAR_NUMBER;
7262 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007263}
7264
7265/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007266 * "srand()" function
7267 */
7268 static void
7269f_srand(typval_T *argvars, typval_T *rettv)
7270{
7271 UINT32_T x = 0, z;
7272
7273 if (rettv_list_alloc(rettv) == FAIL)
7274 return;
7275 if (argvars[0].v_type == VAR_UNKNOWN)
7276 {
7277 init_srand(&x);
7278 }
7279 else
7280 {
7281 int error = FALSE;
7282
7283 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7284 if (error)
7285 return;
7286 }
7287
7288 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7289 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7290 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7291 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7292}
7293
7294#undef ROTL
7295#undef SPLITMIX32
7296#undef SHUFFLE_XOSHIRO128STARSTAR
7297
7298/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007299 * "range()" function
7300 */
7301 static void
7302f_range(typval_T *argvars, typval_T *rettv)
7303{
7304 varnumber_T start;
7305 varnumber_T end;
7306 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007307 int error = FALSE;
7308
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007309 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007310 if (argvars[1].v_type == VAR_UNKNOWN)
7311 {
7312 end = start - 1;
7313 start = 0;
7314 }
7315 else
7316 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007317 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007318 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007319 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007320 }
7321
7322 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007323 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007324 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007325 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007326 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007327 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007328 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007329 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007330 list_T *list = rettv->vval.v_list;
7331
7332 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007333 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007334 // be called.
7335 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007336 list->lv_u.nonmat.lv_start = start;
7337 list->lv_u.nonmat.lv_end = end;
7338 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007339 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007340 }
7341}
7342
7343/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007344 * Materialize "list".
7345 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007346 */
7347 void
7348range_list_materialize(list_T *list)
7349{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007350 varnumber_T start = list->lv_u.nonmat.lv_start;
7351 varnumber_T end = list->lv_u.nonmat.lv_end;
7352 int stride = list->lv_u.nonmat.lv_stride;
7353 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007354
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007355 list->lv_first = NULL;
7356 list->lv_u.mat.lv_last = NULL;
7357 list->lv_len = 0;
7358 list->lv_u.mat.lv_idx_item = NULL;
7359 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7360 if (list_append_number(list, (varnumber_T)i) == FAIL)
7361 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007362}
7363
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007364/*
7365 * "getreginfo()" function
7366 */
7367 static void
7368f_getreginfo(typval_T *argvars, typval_T *rettv)
7369{
7370 char_u *strregname;
7371 int regname;
7372 char_u buf[NUMBUFLEN + 2];
7373 long reglen = 0;
7374 dict_T *dict;
7375 list_T *list;
7376
7377 if (argvars[0].v_type != VAR_UNKNOWN)
7378 {
7379 strregname = tv_get_string_chk(&argvars[0]);
7380 if (strregname == NULL)
7381 return;
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007382 if (in_vim9script() && STRLEN(strregname) > 1)
7383 {
7384 semsg(_(e_register_name_must_be_one_char_str), strregname);
7385 return;
7386 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007387 }
7388 else
7389 strregname = get_vim_var_str(VV_REG);
7390
7391 regname = (strregname == NULL ? '"' : *strregname);
7392 if (regname == 0 || regname == '@')
7393 regname = '"';
7394
7395 if (rettv_dict_alloc(rettv) == FAIL)
7396 return;
7397 dict = rettv->vval.v_dict;
7398
7399 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7400 if (list == NULL)
7401 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007402 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007403
7404 buf[0] = NUL;
7405 buf[1] = NUL;
7406 switch (get_reg_type(regname, &reglen))
7407 {
7408 case MLINE: buf[0] = 'V'; break;
7409 case MCHAR: buf[0] = 'v'; break;
7410 case MBLOCK:
7411 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7412 reglen + 1);
7413 break;
7414 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007415 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007416
7417 buf[0] = get_register_name(get_unname_register());
7418 buf[1] = NUL;
7419 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007420 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007421 else
7422 {
7423 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7424
7425 if (item != NULL)
7426 {
7427 item->di_tv.v_type = VAR_SPECIAL;
7428 item->di_tv.vval.v_number = regname == buf[0]
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007429 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007430 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007431 }
7432 }
7433}
7434
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007435 static void
7436return_register(int regname, typval_T *rettv)
7437{
7438 char_u buf[2] = {0, 0};
7439
7440 buf[0] = (char_u)regname;
7441 rettv->v_type = VAR_STRING;
7442 rettv->vval.v_string = vim_strsave(buf);
7443}
7444
7445/*
7446 * "reg_executing()" function
7447 */
7448 static void
7449f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7450{
7451 return_register(reg_executing, rettv);
7452}
7453
7454/*
7455 * "reg_recording()" function
7456 */
7457 static void
7458f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7459{
7460 return_register(reg_recording, rettv);
7461}
7462
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007463/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007464 * "rename({from}, {to})" function
7465 */
7466 static void
7467f_rename(typval_T *argvars, typval_T *rettv)
7468{
7469 char_u buf[NUMBUFLEN];
7470
7471 if (check_restricted() || check_secure())
7472 rettv->vval.v_number = -1;
7473 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007474 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7475 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007476}
7477
7478/*
7479 * "repeat()" function
7480 */
7481 static void
7482f_repeat(typval_T *argvars, typval_T *rettv)
7483{
7484 char_u *p;
7485 int n;
7486 int slen;
7487 int len;
7488 char_u *r;
7489 int i;
7490
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007491 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007492 if (argvars[0].v_type == VAR_LIST)
7493 {
7494 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7495 while (n-- > 0)
7496 if (list_extend(rettv->vval.v_list,
7497 argvars[0].vval.v_list, NULL) == FAIL)
7498 break;
7499 }
7500 else
7501 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007502 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007503 rettv->v_type = VAR_STRING;
7504 rettv->vval.v_string = NULL;
7505
7506 slen = (int)STRLEN(p);
7507 len = slen * n;
7508 if (len <= 0)
7509 return;
7510
7511 r = alloc(len + 1);
7512 if (r != NULL)
7513 {
7514 for (i = 0; i < n; i++)
7515 mch_memmove(r + i * slen, p, (size_t)slen);
7516 r[len] = NUL;
7517 }
7518
7519 rettv->vval.v_string = r;
7520 }
7521}
7522
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007523#define SP_NOMOVE 0x01 // don't move cursor
7524#define SP_REPEAT 0x02 // repeat to find outer pair
7525#define SP_RETCOUNT 0x04 // return matchcount
7526#define SP_SETPCMARK 0x08 // set previous context mark
7527#define SP_START 0x10 // accept match at start position
7528#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7529#define SP_END 0x40 // leave cursor at end of match
7530#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007531
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007532/*
7533 * Get flags for a search function.
7534 * Possibly sets "p_ws".
7535 * Returns BACKWARD, FORWARD or zero (for an error).
7536 */
7537 static int
7538get_search_arg(typval_T *varp, int *flagsp)
7539{
7540 int dir = FORWARD;
7541 char_u *flags;
7542 char_u nbuf[NUMBUFLEN];
7543 int mask;
7544
7545 if (varp->v_type != VAR_UNKNOWN)
7546 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007547 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007548 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007549 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007550 while (*flags != NUL)
7551 {
7552 switch (*flags)
7553 {
7554 case 'b': dir = BACKWARD; break;
7555 case 'w': p_ws = TRUE; break;
7556 case 'W': p_ws = FALSE; break;
7557 default: mask = 0;
7558 if (flagsp != NULL)
7559 switch (*flags)
7560 {
7561 case 'c': mask = SP_START; break;
7562 case 'e': mask = SP_END; break;
7563 case 'm': mask = SP_RETCOUNT; break;
7564 case 'n': mask = SP_NOMOVE; break;
7565 case 'p': mask = SP_SUBPAT; break;
7566 case 'r': mask = SP_REPEAT; break;
7567 case 's': mask = SP_SETPCMARK; break;
7568 case 'z': mask = SP_COLUMN; break;
7569 }
7570 if (mask == 0)
7571 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007572 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007573 dir = 0;
7574 }
7575 else
7576 *flagsp |= mask;
7577 }
7578 if (dir == 0)
7579 break;
7580 ++flags;
7581 }
7582 }
7583 return dir;
7584}
7585
7586/*
7587 * Shared by search() and searchpos() functions.
7588 */
7589 static int
7590search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7591{
7592 int flags;
7593 char_u *pat;
7594 pos_T pos;
7595 pos_T save_cursor;
7596 int save_p_ws = p_ws;
7597 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007598 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007599 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007600#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007601 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007602 long time_limit = 0;
7603#endif
7604 int options = SEARCH_KEEP;
7605 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007606 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007607 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007608 pos_T firstpos;
7609
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007610 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007611 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007612 if (dir == 0)
7613 goto theend;
7614 flags = *flagsp;
7615 if (flags & SP_START)
7616 options |= SEARCH_START;
7617 if (flags & SP_END)
7618 options |= SEARCH_END;
7619 if (flags & SP_COLUMN)
7620 options |= SEARCH_COL;
7621
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007622 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007623 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7624 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007625 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007626 if (lnum_stop < 0)
7627 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007628 if (argvars[3].v_type != VAR_UNKNOWN)
7629 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007630#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007631 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007632 if (time_limit < 0)
7633 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007634#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007635 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007636 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007637 }
7638
7639#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007640 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007641 profile_setlimit(time_limit, &tm);
7642#endif
7643
7644 /*
7645 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7646 * Check to make sure only those flags are set.
7647 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7648 * flags cannot be set. Check for that condition also.
7649 */
7650 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7651 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7652 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007653 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007654 goto theend;
7655 }
7656
7657 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007658 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007659 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007660 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7661#ifdef FEAT_RELTIME
7662 sia.sa_tm = &tm;
7663#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007664
7665 // Repeat until {skip} returns FALSE.
7666 for (;;)
7667 {
7668 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007669 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007670 // finding the first match again means there is no match where {skip}
7671 // evaluates to zero.
7672 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7673 subpatnum = FAIL;
7674
Bram Moolenaara9c01042020-06-07 14:50:50 +02007675 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007676 // didn't find it or no skip argument
7677 break;
7678 firstpos = pos;
7679
Bram Moolenaara9c01042020-06-07 14:50:50 +02007680 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007681 {
7682 int do_skip;
7683 int err;
7684 pos_T save_pos = curwin->w_cursor;
7685
7686 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007687 err = FALSE;
7688 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007689 curwin->w_cursor = save_pos;
7690 if (err)
7691 {
7692 // Evaluating {skip} caused an error, break here.
7693 subpatnum = FAIL;
7694 break;
7695 }
7696 if (!do_skip)
7697 break;
7698 }
7699 }
7700
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007701 if (subpatnum != FAIL)
7702 {
7703 if (flags & SP_SUBPAT)
7704 retval = subpatnum;
7705 else
7706 retval = pos.lnum;
7707 if (flags & SP_SETPCMARK)
7708 setpcmark();
7709 curwin->w_cursor = pos;
7710 if (match_pos != NULL)
7711 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007712 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007713 match_pos->lnum = pos.lnum;
7714 match_pos->col = pos.col + 1;
7715 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007716 // "/$" will put the cursor after the end of the line, may need to
7717 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007718 check_cursor();
7719 }
7720
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007721 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007722 if (flags & SP_NOMOVE)
7723 curwin->w_cursor = save_cursor;
7724 else
7725 curwin->w_set_curswant = TRUE;
7726theend:
7727 p_ws = save_p_ws;
7728
7729 return retval;
7730}
7731
7732#ifdef FEAT_FLOAT
7733
7734/*
7735 * round() is not in C90, use ceil() or floor() instead.
7736 */
7737 float_T
7738vim_round(float_T f)
7739{
7740 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
7741}
7742
7743/*
7744 * "round({float})" function
7745 */
7746 static void
7747f_round(typval_T *argvars, typval_T *rettv)
7748{
7749 float_T f = 0.0;
7750
7751 rettv->v_type = VAR_FLOAT;
7752 if (get_float_arg(argvars, &f) == OK)
7753 rettv->vval.v_float = vim_round(f);
7754 else
7755 rettv->vval.v_float = 0.0;
7756}
7757#endif
7758
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007759#ifdef FEAT_RUBY
7760/*
7761 * "rubyeval()" function
7762 */
7763 static void
7764f_rubyeval(typval_T *argvars, typval_T *rettv)
7765{
7766 char_u *str;
7767 char_u buf[NUMBUFLEN];
7768
7769 str = tv_get_string_buf(&argvars[0], buf);
7770 do_rubyeval(str, rettv);
7771}
7772#endif
7773
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007774/*
7775 * "screenattr()" function
7776 */
7777 static void
7778f_screenattr(typval_T *argvars, typval_T *rettv)
7779{
7780 int row;
7781 int col;
7782 int c;
7783
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007784 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7785 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007786 if (row < 0 || row >= screen_Rows
7787 || col < 0 || col >= screen_Columns)
7788 c = -1;
7789 else
7790 c = ScreenAttrs[LineOffset[row] + col];
7791 rettv->vval.v_number = c;
7792}
7793
7794/*
7795 * "screenchar()" function
7796 */
7797 static void
7798f_screenchar(typval_T *argvars, typval_T *rettv)
7799{
7800 int row;
7801 int col;
7802 int off;
7803 int c;
7804
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007805 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7806 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007807 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007808 c = -1;
7809 else
7810 {
7811 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007812 if (enc_utf8 && ScreenLinesUC[off] != 0)
7813 c = ScreenLinesUC[off];
7814 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007815 c = ScreenLines[off];
7816 }
7817 rettv->vval.v_number = c;
7818}
7819
7820/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007821 * "screenchars()" function
7822 */
7823 static void
7824f_screenchars(typval_T *argvars, typval_T *rettv)
7825{
7826 int row;
7827 int col;
7828 int off;
7829 int c;
7830 int i;
7831
7832 if (rettv_list_alloc(rettv) == FAIL)
7833 return;
7834 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7835 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7836 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7837 return;
7838
7839 off = LineOffset[row] + col;
7840 if (enc_utf8 && ScreenLinesUC[off] != 0)
7841 c = ScreenLinesUC[off];
7842 else
7843 c = ScreenLines[off];
7844 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7845
7846 if (enc_utf8)
7847
7848 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7849 list_append_number(rettv->vval.v_list,
7850 (varnumber_T)ScreenLinesC[i][off]);
7851}
7852
7853/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007854 * "screencol()" function
7855 *
7856 * First column is 1 to be consistent with virtcol().
7857 */
7858 static void
7859f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7860{
7861 rettv->vval.v_number = screen_screencol() + 1;
7862}
7863
7864/*
7865 * "screenrow()" function
7866 */
7867 static void
7868f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7869{
7870 rettv->vval.v_number = screen_screenrow() + 1;
7871}
7872
7873/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007874 * "screenstring()" function
7875 */
7876 static void
7877f_screenstring(typval_T *argvars, typval_T *rettv)
7878{
7879 int row;
7880 int col;
7881 int off;
7882 int c;
7883 int i;
7884 char_u buf[MB_MAXBYTES + 1];
7885 int buflen = 0;
7886
7887 rettv->vval.v_string = NULL;
7888 rettv->v_type = VAR_STRING;
7889
7890 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7891 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7892 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7893 return;
7894
7895 off = LineOffset[row] + col;
7896 if (enc_utf8 && ScreenLinesUC[off] != 0)
7897 c = ScreenLinesUC[off];
7898 else
7899 c = ScreenLines[off];
7900 buflen += mb_char2bytes(c, buf);
7901
7902 if (enc_utf8)
7903 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7904 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7905
7906 buf[buflen] = NUL;
7907 rettv->vval.v_string = vim_strsave(buf);
7908}
7909
7910/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007911 * "search()" function
7912 */
7913 static void
7914f_search(typval_T *argvars, typval_T *rettv)
7915{
7916 int flags = 0;
7917
7918 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7919}
7920
7921/*
7922 * "searchdecl()" function
7923 */
7924 static void
7925f_searchdecl(typval_T *argvars, typval_T *rettv)
7926{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007927 int locally = TRUE;
7928 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007929 int error = FALSE;
7930 char_u *name;
7931
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007932 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007933
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007934 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007935 if (argvars[1].v_type != VAR_UNKNOWN)
7936 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007937 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007938 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007939 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007940 }
7941 if (!error && name != NULL)
7942 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7943 locally, thisblock, SEARCH_KEEP) == FAIL;
7944}
7945
7946/*
7947 * Used by searchpair() and searchpairpos()
7948 */
7949 static int
7950searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7951{
7952 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007953 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007954 int save_p_ws = p_ws;
7955 int dir;
7956 int flags = 0;
7957 char_u nbuf1[NUMBUFLEN];
7958 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007959 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007960 long lnum_stop = 0;
7961 long time_limit = 0;
7962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007963 // Get the three pattern arguments: start, middle, end. Will result in an
7964 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007965 spat = tv_get_string_chk(&argvars[0]);
7966 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7967 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007968 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007969 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007970
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007971 // Handle the optional fourth argument: flags
7972 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007973 if (dir == 0)
7974 goto theend;
7975
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007976 // Don't accept SP_END or SP_SUBPAT.
7977 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007978 if ((flags & (SP_END | SP_SUBPAT)) != 0
7979 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7980 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007981 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007982 goto theend;
7983 }
7984
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007985 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007986 if (flags & SP_REPEAT)
7987 p_ws = FALSE;
7988
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007989 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007990 if (argvars[3].v_type == VAR_UNKNOWN
7991 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007992 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007993 else
7994 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007995 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007996 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007997
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007998 if (argvars[5].v_type != VAR_UNKNOWN)
7999 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008000 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008001 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008002 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008003 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008004 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008005 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008006#ifdef FEAT_RELTIME
8007 if (argvars[6].v_type != VAR_UNKNOWN)
8008 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008009 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008010 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008011 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008012 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008013 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008014 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008015 }
8016#endif
8017 }
8018 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008019
8020 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
8021 match_pos, lnum_stop, time_limit);
8022
8023theend:
8024 p_ws = save_p_ws;
8025
8026 return retval;
8027}
8028
8029/*
8030 * "searchpair()" function
8031 */
8032 static void
8033f_searchpair(typval_T *argvars, typval_T *rettv)
8034{
8035 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
8036}
8037
8038/*
8039 * "searchpairpos()" function
8040 */
8041 static void
8042f_searchpairpos(typval_T *argvars, typval_T *rettv)
8043{
8044 pos_T match_pos;
8045 int lnum = 0;
8046 int col = 0;
8047
8048 if (rettv_list_alloc(rettv) == FAIL)
8049 return;
8050
8051 if (searchpair_cmn(argvars, &match_pos) > 0)
8052 {
8053 lnum = match_pos.lnum;
8054 col = match_pos.col;
8055 }
8056
8057 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8058 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8059}
8060
8061/*
8062 * Search for a start/middle/end thing.
8063 * Used by searchpair(), see its documentation for the details.
8064 * Returns 0 or -1 for no match,
8065 */
8066 long
8067do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008068 char_u *spat, // start pattern
8069 char_u *mpat, // middle pattern
8070 char_u *epat, // end pattern
8071 int dir, // BACKWARD or FORWARD
8072 typval_T *skip, // skip expression
8073 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008074 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008075 linenr_T lnum_stop, // stop at this line if not zero
8076 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008077{
8078 char_u *save_cpo;
8079 char_u *pat, *pat2 = NULL, *pat3 = NULL;
8080 long retval = 0;
8081 pos_T pos;
8082 pos_T firstpos;
8083 pos_T foundpos;
8084 pos_T save_cursor;
8085 pos_T save_pos;
8086 int n;
8087 int r;
8088 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01008089 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008090 int err;
8091 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008092#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008093 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008094#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008095
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008096 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008097 save_cpo = p_cpo;
8098 p_cpo = empty_option;
8099
8100#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008101 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008102 profile_setlimit(time_limit, &tm);
8103#endif
8104
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008105 // Make two search patterns: start/end (pat2, for in nested pairs) and
8106 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02008107 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
8108 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008109 if (pat2 == NULL || pat3 == NULL)
8110 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008111 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008112 if (*mpat == NUL)
8113 STRCPY(pat3, pat2);
8114 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008115 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008116 spat, epat, mpat);
8117 if (flags & SP_START)
8118 options |= SEARCH_START;
8119
Bram Moolenaar48570482017-10-30 21:48:41 +01008120 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02008121 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01008122
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008123 save_cursor = curwin->w_cursor;
8124 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008125 CLEAR_POS(&firstpos);
8126 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008127 pat = pat3;
8128 for (;;)
8129 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008130 searchit_arg_T sia;
8131
Bram Moolenaara80faa82020-04-12 19:37:17 +02008132 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008133 sia.sa_stop_lnum = lnum_stop;
8134#ifdef FEAT_RELTIME
8135 sia.sa_tm = &tm;
8136#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01008137 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008138 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008139 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008140 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008141 break;
8142
8143 if (firstpos.lnum == 0)
8144 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008145 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008146 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008147 // Found the same position again. Can happen with a pattern that
8148 // has "\zs" at the end and searching backwards. Advance one
8149 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008150 if (dir == BACKWARD)
8151 decl(&pos);
8152 else
8153 incl(&pos);
8154 }
8155 foundpos = pos;
8156
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008157 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008158 options &= ~SEARCH_START;
8159
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008160 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01008161 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008162 {
8163 save_pos = curwin->w_cursor;
8164 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01008165 err = FALSE;
8166 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008167 curwin->w_cursor = save_pos;
8168 if (err)
8169 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008170 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008171 curwin->w_cursor = save_cursor;
8172 retval = -1;
8173 break;
8174 }
8175 if (r)
8176 continue;
8177 }
8178
8179 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8180 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008181 // Found end when searching backwards or start when searching
8182 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008183 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008184 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008185 }
8186 else
8187 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008188 // Found end when searching forward or start when searching
8189 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008190 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008191 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008192 }
8193
8194 if (nest == 0)
8195 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008196 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008197 if (flags & SP_RETCOUNT)
8198 ++retval;
8199 else
8200 retval = pos.lnum;
8201 if (flags & SP_SETPCMARK)
8202 setpcmark();
8203 curwin->w_cursor = pos;
8204 if (!(flags & SP_REPEAT))
8205 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008206 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008207 }
8208 }
8209
8210 if (match_pos != NULL)
8211 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008212 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008213 match_pos->lnum = curwin->w_cursor.lnum;
8214 match_pos->col = curwin->w_cursor.col + 1;
8215 }
8216
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008217 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008218 if ((flags & SP_NOMOVE) || retval == 0)
8219 curwin->w_cursor = save_cursor;
8220
8221theend:
8222 vim_free(pat2);
8223 vim_free(pat3);
8224 if (p_cpo == empty_option)
8225 p_cpo = save_cpo;
8226 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008227 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008228 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008229 // If it's still empty it was changed and restored, need to restore in
8230 // the complicated way.
8231 if (*p_cpo == NUL)
8232 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008233 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008234 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008235
8236 return retval;
8237}
8238
8239/*
8240 * "searchpos()" function
8241 */
8242 static void
8243f_searchpos(typval_T *argvars, typval_T *rettv)
8244{
8245 pos_T match_pos;
8246 int lnum = 0;
8247 int col = 0;
8248 int n;
8249 int flags = 0;
8250
8251 if (rettv_list_alloc(rettv) == FAIL)
8252 return;
8253
8254 n = search_cmn(argvars, &match_pos, &flags);
8255 if (n > 0)
8256 {
8257 lnum = match_pos.lnum;
8258 col = match_pos.col;
8259 }
8260
8261 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8262 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8263 if (flags & SP_SUBPAT)
8264 list_append_number(rettv->vval.v_list, (varnumber_T)n);
8265}
8266
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008267/*
8268 * Set the cursor or mark position.
8269 * If 'charpos' is TRUE, then use the column number as a character offet.
8270 * Otherwise use the column number as a byte offset.
8271 */
8272 static void
8273set_position(typval_T *argvars, typval_T *rettv, int charpos)
8274{
8275 pos_T pos;
8276 int fnum;
8277 char_u *name;
8278 colnr_T curswant = -1;
8279
8280 rettv->vval.v_number = -1;
8281
8282 name = tv_get_string_chk(argvars);
8283 if (name != NULL)
8284 {
8285 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
8286 {
8287 if (pos.col != MAXCOL && --pos.col < 0)
8288 pos.col = 0;
8289 if ((name[0] == '.' && name[1] == NUL))
8290 {
8291 // set cursor; "fnum" is ignored
8292 curwin->w_cursor = pos;
8293 if (curswant >= 0)
8294 {
8295 curwin->w_curswant = curswant - 1;
8296 curwin->w_set_curswant = FALSE;
8297 }
8298 check_cursor();
8299 rettv->vval.v_number = 0;
8300 }
8301 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8302 {
8303 // set mark
8304 if (setmark_pos(name[1], &pos, fnum) == OK)
8305 rettv->vval.v_number = 0;
8306 }
8307 else
8308 emsg(_(e_invarg));
8309 }
8310 }
8311}
8312/*
8313 * "setcharpos()" function
8314 */
8315 static void
8316f_setcharpos(typval_T *argvars, typval_T *rettv)
8317{
8318 set_position(argvars, rettv, TRUE);
8319}
8320
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008321 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008322f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8323{
8324 dict_T *d;
8325 dictitem_T *di;
8326 char_u *csearch;
8327
8328 if (argvars[0].v_type != VAR_DICT)
8329 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008330 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008331 return;
8332 }
8333
8334 if ((d = argvars[0].vval.v_dict) != NULL)
8335 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008336 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008337 if (csearch != NULL)
8338 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008339 if (enc_utf8)
8340 {
8341 int pcc[MAX_MCO];
8342 int c = utfc_ptr2char(csearch, pcc);
8343
8344 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8345 }
8346 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008347 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008348 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008349 }
8350
8351 di = dict_find(d, (char_u *)"forward", -1);
8352 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008353 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008354 ? FORWARD : BACKWARD);
8355
8356 di = dict_find(d, (char_u *)"until", -1);
8357 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008358 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008359 }
8360}
8361
8362/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008363 * "setcursorcharpos" function
8364 */
8365 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008366f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008367{
8368 set_cursorpos(argvars, rettv, TRUE);
8369}
8370
8371/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008372 * "setenv()" function
8373 */
8374 static void
8375f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8376{
8377 char_u namebuf[NUMBUFLEN];
8378 char_u valbuf[NUMBUFLEN];
8379 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8380
8381 if (argvars[1].v_type == VAR_SPECIAL
8382 && argvars[1].vval.v_number == VVAL_NULL)
8383 vim_unsetenv(name);
8384 else
8385 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8386}
8387
8388/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008389 * "setfperm({fname}, {mode})" function
8390 */
8391 static void
8392f_setfperm(typval_T *argvars, typval_T *rettv)
8393{
8394 char_u *fname;
8395 char_u modebuf[NUMBUFLEN];
8396 char_u *mode_str;
8397 int i;
8398 int mask;
8399 int mode = 0;
8400
8401 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008402 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008403 if (fname == NULL)
8404 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008405 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008406 if (mode_str == NULL)
8407 return;
8408 if (STRLEN(mode_str) != 9)
8409 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008410 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008411 return;
8412 }
8413
8414 mask = 1;
8415 for (i = 8; i >= 0; --i)
8416 {
8417 if (mode_str[i] != '-')
8418 mode |= mask;
8419 mask = mask << 1;
8420 }
8421 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8422}
8423
8424/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008425 * "setpos()" function
8426 */
8427 static void
8428f_setpos(typval_T *argvars, typval_T *rettv)
8429{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008430 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008431}
8432
8433/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008434 * Translate a register type string to the yank type and block length
8435 */
8436 static int
8437get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8438{
8439 char_u *stropt = *pp;
8440 switch (*stropt)
8441 {
8442 case 'v': case 'c': // character-wise selection
8443 *yank_type = MCHAR;
8444 break;
8445 case 'V': case 'l': // line-wise selection
8446 *yank_type = MLINE;
8447 break;
8448 case 'b': case Ctrl_V: // block-wise selection
8449 *yank_type = MBLOCK;
8450 if (VIM_ISDIGIT(stropt[1]))
8451 {
8452 ++stropt;
8453 *block_len = getdigits(&stropt) - 1;
8454 --stropt;
8455 }
8456 break;
8457 default:
8458 return FAIL;
8459 }
8460 *pp = stropt;
8461 return OK;
8462}
8463
8464/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008465 * "setreg()" function
8466 */
8467 static void
8468f_setreg(typval_T *argvars, typval_T *rettv)
8469{
8470 int regname;
8471 char_u *strregname;
8472 char_u *stropt;
8473 char_u *strval;
8474 int append;
8475 char_u yank_type;
8476 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008477 typval_T *regcontents;
8478 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008479
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008480 pointreg = 0;
8481 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008482 block_len = -1;
8483 yank_type = MAUTO;
8484 append = FALSE;
8485
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008486 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008487 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008488
8489 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008490 return; // type error; errmsg already given
Bram Moolenaar418a29f2021-02-10 22:23:41 +01008491 if (in_vim9script() && STRLEN(strregname) > 1)
8492 {
8493 semsg(_(e_register_name_must_be_one_char_str), strregname);
8494 return;
8495 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008496 regname = *strregname;
8497 if (regname == 0 || regname == '@')
8498 regname = '"';
8499
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008500 if (argvars[1].v_type == VAR_DICT)
8501 {
8502 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008503 dictitem_T *di;
8504
8505 if (d == NULL || d->dv_hashtab.ht_used == 0)
8506 {
8507 // Empty dict, clear the register (like setreg(0, []))
8508 char_u *lstval[2] = {NULL, NULL};
8509 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8510 return;
8511 }
8512
8513 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008514 if (di != NULL)
8515 regcontents = &di->di_tv;
8516
8517 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8518 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008519 {
8520 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8521
8522 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008523 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008524 semsg(_(e_invargval), "value");
8525 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008526 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008527 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008528
8529 if (regname == '"')
8530 {
8531 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8532 if (stropt != NULL)
8533 {
8534 pointreg = *stropt;
8535 regname = pointreg;
8536 }
8537 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008538 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008539 pointreg = regname;
8540 }
8541 else
8542 regcontents = &argvars[1];
8543
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008544 if (argvars[2].v_type != VAR_UNKNOWN)
8545 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008546 if (yank_type != MAUTO)
8547 {
8548 semsg(_(e_toomanyarg), "setreg");
8549 return;
8550 }
8551
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008552 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008553 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008554 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008555 for (; *stropt != NUL; ++stropt)
8556 switch (*stropt)
8557 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008558 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008559 append = TRUE;
8560 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008561 default:
8562 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008563 }
8564 }
8565
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008566 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008567 {
8568 char_u **lstval;
8569 char_u **allocval;
8570 char_u buf[NUMBUFLEN];
8571 char_u **curval;
8572 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008573 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008574 listitem_T *li;
8575 int len;
8576
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008577 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008578 len = ll == NULL ? 0 : ll->lv_len;
8579
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008580 // First half: use for pointers to result lines; second half: use for
8581 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008582 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583 if (lstval == NULL)
8584 return;
8585 curval = lstval;
8586 allocval = lstval + len + 2;
8587 curallocval = allocval;
8588
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008589 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008590 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008591 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008592 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008593 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008594 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008595 if (strval == NULL)
8596 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008597 if (strval == buf)
8598 {
8599 // Need to make a copy, next tv_get_string_buf_chk() will
8600 // overwrite the string.
8601 strval = vim_strsave(buf);
8602 if (strval == NULL)
8603 goto free_lstval;
8604 *curallocval++ = strval;
8605 }
8606 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008607 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008608 }
8609 *curval++ = NULL;
8610
8611 write_reg_contents_lst(regname, lstval, -1,
8612 append, yank_type, block_len);
8613free_lstval:
8614 while (curallocval > allocval)
8615 vim_free(*--curallocval);
8616 vim_free(lstval);
8617 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008618 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008619 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008620 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008621 if (strval == NULL)
8622 return;
8623 write_reg_contents_ex(regname, strval, -1,
8624 append, yank_type, block_len);
8625 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008626 if (pointreg != 0)
8627 get_yank_register(pointreg, TRUE);
8628
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008629 rettv->vval.v_number = 0;
8630}
8631
8632/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008633 * "settagstack()" function
8634 */
8635 static void
8636f_settagstack(typval_T *argvars, typval_T *rettv)
8637{
8638 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8639 win_T *wp;
8640 dict_T *d;
8641 int action = 'r';
8642
8643 rettv->vval.v_number = -1;
8644
8645 // first argument: window number or id
8646 wp = find_win_by_nr_or_id(&argvars[0]);
8647 if (wp == NULL)
8648 return;
8649
8650 // second argument: dict with items to set in the tag stack
8651 if (argvars[1].v_type != VAR_DICT)
8652 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008653 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008654 return;
8655 }
8656 d = argvars[1].vval.v_dict;
8657 if (d == NULL)
8658 return;
8659
8660 // third argument: action - 'a' for append and 'r' for replace.
8661 // default is to replace the stack.
8662 if (argvars[2].v_type == VAR_UNKNOWN)
8663 action = 'r';
8664 else if (argvars[2].v_type == VAR_STRING)
8665 {
8666 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008667 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008668 if (actstr == NULL)
8669 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008670 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8671 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008672 action = *actstr;
8673 else
8674 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008675 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008676 return;
8677 }
8678 }
8679 else
8680 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008681 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008682 return;
8683 }
8684
8685 if (set_tagstack(wp, d, action) == OK)
8686 rettv->vval.v_number = 0;
8687}
8688
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008689#ifdef FEAT_CRYPT
8690/*
8691 * "sha256({string})" function
8692 */
8693 static void
8694f_sha256(typval_T *argvars, typval_T *rettv)
8695{
8696 char_u *p;
8697
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008698 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008699 rettv->vval.v_string = vim_strsave(
8700 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8701 rettv->v_type = VAR_STRING;
8702}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008703#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008704
8705/*
8706 * "shellescape({string})" function
8707 */
8708 static void
8709f_shellescape(typval_T *argvars, typval_T *rettv)
8710{
Bram Moolenaar20615522017-06-05 18:46:26 +02008711 int do_special = non_zero_arg(&argvars[1]);
8712
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008713 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008714 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008715 rettv->v_type = VAR_STRING;
8716}
8717
8718/*
8719 * shiftwidth() function
8720 */
8721 static void
8722f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8723{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008724 rettv->vval.v_number = 0;
8725
8726 if (argvars[0].v_type != VAR_UNKNOWN)
8727 {
8728 long col;
8729
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008730 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008731 if (col < 0)
8732 return; // type error; errmsg already given
8733#ifdef FEAT_VARTABS
8734 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8735 return;
8736#endif
8737 }
8738
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008739 rettv->vval.v_number = get_sw_value(curbuf);
8740}
8741
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008742#ifdef FEAT_FLOAT
8743/*
8744 * "sin()" function
8745 */
8746 static void
8747f_sin(typval_T *argvars, typval_T *rettv)
8748{
8749 float_T f = 0.0;
8750
8751 rettv->v_type = VAR_FLOAT;
8752 if (get_float_arg(argvars, &f) == OK)
8753 rettv->vval.v_float = sin(f);
8754 else
8755 rettv->vval.v_float = 0.0;
8756}
8757
8758/*
8759 * "sinh()" function
8760 */
8761 static void
8762f_sinh(typval_T *argvars, typval_T *rettv)
8763{
8764 float_T f = 0.0;
8765
8766 rettv->v_type = VAR_FLOAT;
8767 if (get_float_arg(argvars, &f) == OK)
8768 rettv->vval.v_float = sinh(f);
8769 else
8770 rettv->vval.v_float = 0.0;
8771}
8772#endif
8773
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008774/*
8775 * "soundfold({word})" function
8776 */
8777 static void
8778f_soundfold(typval_T *argvars, typval_T *rettv)
8779{
8780 char_u *s;
8781
8782 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008783 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008784#ifdef FEAT_SPELL
8785 rettv->vval.v_string = eval_soundfold(s);
8786#else
8787 rettv->vval.v_string = vim_strsave(s);
8788#endif
8789}
8790
8791/*
8792 * "spellbadword()" function
8793 */
8794 static void
8795f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8796{
8797 char_u *word = (char_u *)"";
8798 hlf_T attr = HLF_COUNT;
8799 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008800#ifdef FEAT_SPELL
8801 int wo_spell_save = curwin->w_p_spell;
8802
8803 if (!curwin->w_p_spell)
8804 {
8805 did_set_spelllang(curwin);
8806 curwin->w_p_spell = TRUE;
8807 }
8808
8809 if (*curwin->w_s->b_p_spl == NUL)
8810 {
8811 emsg(_(e_no_spell));
8812 curwin->w_p_spell = wo_spell_save;
8813 return;
8814 }
8815#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008816
8817 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008818 {
8819#ifdef FEAT_SPELL
8820 curwin->w_p_spell = wo_spell_save;
8821#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008822 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008823 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008824
8825#ifdef FEAT_SPELL
8826 if (argvars[0].v_type == VAR_UNKNOWN)
8827 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008828 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008829 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8830 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008831 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008832 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008833 curwin->w_set_curswant = TRUE;
8834 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008836 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008837 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008838 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008839 int capcol = -1;
8840
8841 if (str != NULL)
8842 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008843 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008844 while (*str != NUL)
8845 {
8846 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8847 if (attr != HLF_COUNT)
8848 {
8849 word = str;
8850 break;
8851 }
8852 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008853 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008854 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008855 }
8856 }
8857 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008858 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008859#endif
8860
8861 list_append_string(rettv->vval.v_list, word, len);
8862 list_append_string(rettv->vval.v_list, (char_u *)(
8863 attr == HLF_SPB ? "bad" :
8864 attr == HLF_SPR ? "rare" :
8865 attr == HLF_SPL ? "local" :
8866 attr == HLF_SPC ? "caps" :
8867 ""), -1);
8868}
8869
8870/*
8871 * "spellsuggest()" function
8872 */
8873 static void
8874f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8875{
8876#ifdef FEAT_SPELL
8877 char_u *str;
8878 int typeerr = FALSE;
8879 int maxcount;
8880 garray_T ga;
8881 int i;
8882 listitem_T *li;
8883 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008884 int wo_spell_save = curwin->w_p_spell;
8885
8886 if (!curwin->w_p_spell)
8887 {
8888 did_set_spelllang(curwin);
8889 curwin->w_p_spell = TRUE;
8890 }
8891
8892 if (*curwin->w_s->b_p_spl == NUL)
8893 {
8894 emsg(_(e_no_spell));
8895 curwin->w_p_spell = wo_spell_save;
8896 return;
8897 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008898#endif
8899
8900 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008901 {
8902#ifdef FEAT_SPELL
8903 curwin->w_p_spell = wo_spell_save;
8904#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008905 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008906 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008907
8908#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008909 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008910 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008911 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008912 if (argvars[1].v_type != VAR_UNKNOWN)
8913 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008914 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008915 if (maxcount <= 0)
8916 return;
8917 if (argvars[2].v_type != VAR_UNKNOWN)
8918 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008919 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008920 if (typeerr)
8921 return;
8922 }
8923 }
8924 else
8925 maxcount = 25;
8926
8927 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8928
8929 for (i = 0; i < ga.ga_len; ++i)
8930 {
8931 str = ((char_u **)ga.ga_data)[i];
8932
8933 li = listitem_alloc();
8934 if (li == NULL)
8935 vim_free(str);
8936 else
8937 {
8938 li->li_tv.v_type = VAR_STRING;
8939 li->li_tv.v_lock = 0;
8940 li->li_tv.vval.v_string = str;
8941 list_append(rettv->vval.v_list, li);
8942 }
8943 }
8944 ga_clear(&ga);
8945 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008946 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008947#endif
8948}
8949
8950 static void
8951f_split(typval_T *argvars, typval_T *rettv)
8952{
8953 char_u *str;
8954 char_u *end;
8955 char_u *pat = NULL;
8956 regmatch_T regmatch;
8957 char_u patbuf[NUMBUFLEN];
8958 char_u *save_cpo;
8959 int match;
8960 colnr_T col = 0;
8961 int keepempty = FALSE;
8962 int typeerr = FALSE;
8963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008964 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008965 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008966 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008967
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008968 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008969 if (argvars[1].v_type != VAR_UNKNOWN)
8970 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008971 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008972 if (pat == NULL)
8973 typeerr = TRUE;
8974 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008975 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008976 }
8977 if (pat == NULL || *pat == NUL)
8978 pat = (char_u *)"[\\x01- ]\\+";
8979
8980 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008981 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008982 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008983 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008984
8985 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8986 if (regmatch.regprog != NULL)
8987 {
8988 regmatch.rm_ic = FALSE;
8989 while (*str != NUL || keepempty)
8990 {
8991 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008992 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008993 else
8994 match = vim_regexec_nl(&regmatch, str, col);
8995 if (match)
8996 end = regmatch.startp[0];
8997 else
8998 end = str + STRLEN(str);
8999 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
9000 && *str != NUL && match && end < regmatch.endp[0]))
9001 {
9002 if (list_append_string(rettv->vval.v_list, str,
9003 (int)(end - str)) == FAIL)
9004 break;
9005 }
9006 if (!match)
9007 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01009008 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009009 if (regmatch.endp[0] > str)
9010 col = 0;
9011 else
Bram Moolenaar13505972019-01-24 15:04:48 +01009012 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009013 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009014 str = regmatch.endp[0];
9015 }
9016
9017 vim_regfree(regmatch.regprog);
9018 }
9019
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02009020theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009021 p_cpo = save_cpo;
9022}
9023
9024#ifdef FEAT_FLOAT
9025/*
9026 * "sqrt()" function
9027 */
9028 static void
9029f_sqrt(typval_T *argvars, typval_T *rettv)
9030{
9031 float_T f = 0.0;
9032
9033 rettv->v_type = VAR_FLOAT;
9034 if (get_float_arg(argvars, &f) == OK)
9035 rettv->vval.v_float = sqrt(f);
9036 else
9037 rettv->vval.v_float = 0.0;
9038}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009039#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009040
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009041#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01009042/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009043 * "str2float()" function
9044 */
9045 static void
9046f_str2float(typval_T *argvars, typval_T *rettv)
9047{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009048 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009049 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009050
Bram Moolenaar08243d22017-01-10 16:12:29 +01009051 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009052 p = skipwhite(p + 1);
9053 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01009054 if (isneg)
9055 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009056 rettv->v_type = VAR_FLOAT;
9057}
9058#endif
9059
9060/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02009061 * "str2list()" function
9062 */
9063 static void
9064f_str2list(typval_T *argvars, typval_T *rettv)
9065{
9066 char_u *p;
9067 int utf8 = FALSE;
9068
9069 if (rettv_list_alloc(rettv) == FAIL)
9070 return;
9071
9072 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02009073 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02009074
9075 p = tv_get_string(&argvars[0]);
9076
9077 if (has_mbyte || utf8)
9078 {
9079 int (*ptr2len)(char_u *);
9080 int (*ptr2char)(char_u *);
9081
9082 if (utf8 || enc_utf8)
9083 {
9084 ptr2len = utf_ptr2len;
9085 ptr2char = utf_ptr2char;
9086 }
9087 else
9088 {
9089 ptr2len = mb_ptr2len;
9090 ptr2char = mb_ptr2char;
9091 }
9092
9093 for ( ; *p != NUL; p += (*ptr2len)(p))
9094 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
9095 }
9096 else
9097 for ( ; *p != NUL; ++p)
9098 list_append_number(rettv->vval.v_list, *p);
9099}
9100
9101/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009102 * "str2nr()" function
9103 */
9104 static void
9105f_str2nr(typval_T *argvars, typval_T *rettv)
9106{
9107 int base = 10;
9108 char_u *p;
9109 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009110 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01009111 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112
9113 if (argvars[1].v_type != VAR_UNKNOWN)
9114 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009115 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009116 if (base != 2 && base != 8 && base != 10 && base != 16)
9117 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009118 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009119 return;
9120 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02009121 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009122 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009123 }
9124
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01009125 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009126 isneg = (*p == '-');
9127 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009128 p = skipwhite(p + 1);
9129 switch (base)
9130 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009131 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02009132 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009133 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009134 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02009135 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
9136 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01009137 if (isneg)
9138 rettv->vval.v_number = -n;
9139 else
9140 rettv->vval.v_number = n;
9141
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009142}
9143
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009144/*
9145 * "strgetchar()" function
9146 */
9147 static void
9148f_strgetchar(typval_T *argvars, typval_T *rettv)
9149{
9150 char_u *str;
9151 int len;
9152 int error = FALSE;
9153 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01009154 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009155
9156 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009157 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009158 if (str == NULL)
9159 return;
9160 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009161 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009162 if (error)
9163 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009164
Bram Moolenaar13505972019-01-24 15:04:48 +01009165 while (charidx >= 0 && byteidx < len)
9166 {
9167 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009168 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009169 rettv->vval.v_number = mb_ptr2char(str + byteidx);
9170 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009171 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009172 --charidx;
9173 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009174 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009175}
9176
9177/*
9178 * "stridx()" function
9179 */
9180 static void
9181f_stridx(typval_T *argvars, typval_T *rettv)
9182{
9183 char_u buf[NUMBUFLEN];
9184 char_u *needle;
9185 char_u *haystack;
9186 char_u *save_haystack;
9187 char_u *pos;
9188 int start_idx;
9189
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009190 needle = tv_get_string_chk(&argvars[1]);
9191 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009192 rettv->vval.v_number = -1;
9193 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009194 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009195
9196 if (argvars[2].v_type != VAR_UNKNOWN)
9197 {
9198 int error = FALSE;
9199
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009200 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009201 if (error || start_idx >= (int)STRLEN(haystack))
9202 return;
9203 if (start_idx >= 0)
9204 haystack += start_idx;
9205 }
9206
9207 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9208 if (pos != NULL)
9209 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
9210}
9211
9212/*
9213 * "string()" function
9214 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009215 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009216f_string(typval_T *argvars, typval_T *rettv)
9217{
9218 char_u *tofree;
9219 char_u numbuf[NUMBUFLEN];
9220
9221 rettv->v_type = VAR_STRING;
9222 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
9223 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009224 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009225 if (rettv->vval.v_string != NULL && tofree == NULL)
9226 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
9227}
9228
9229/*
9230 * "strlen()" function
9231 */
9232 static void
9233f_strlen(typval_T *argvars, typval_T *rettv)
9234{
9235 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009236 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009237}
9238
9239/*
9240 * "strchars()" function
9241 */
9242 static void
9243f_strchars(typval_T *argvars, typval_T *rettv)
9244{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009245 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009246 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009247 varnumber_T len = 0;
9248 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009249
9250 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009251 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009252 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02009253 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009254 else
9255 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009256 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
9257 while (*s != NUL)
9258 {
9259 func_mb_ptr2char_adv(&s);
9260 ++len;
9261 }
9262 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009263 }
9264}
9265
9266/*
9267 * "strdisplaywidth()" function
9268 */
9269 static void
9270f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
9271{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009272 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009273 int col = 0;
9274
9275 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009276 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009277
9278 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
9279}
9280
9281/*
9282 * "strwidth()" function
9283 */
9284 static void
9285f_strwidth(typval_T *argvars, typval_T *rettv)
9286{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009287 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009288
Bram Moolenaar13505972019-01-24 15:04:48 +01009289 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009290}
9291
9292/*
9293 * "strcharpart()" function
9294 */
9295 static void
9296f_strcharpart(typval_T *argvars, typval_T *rettv)
9297{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009298 char_u *p;
9299 int nchar;
9300 int nbyte = 0;
9301 int charlen;
9302 int len = 0;
9303 int slen;
9304 int error = FALSE;
9305
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009306 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009307 slen = (int)STRLEN(p);
9308
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009309 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009310 if (!error)
9311 {
9312 if (nchar > 0)
9313 while (nchar > 0 && nbyte < slen)
9314 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009315 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009316 --nchar;
9317 }
9318 else
9319 nbyte = nchar;
9320 if (argvars[2].v_type != VAR_UNKNOWN)
9321 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009322 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009323 while (charlen > 0 && nbyte + len < slen)
9324 {
9325 int off = nbyte + len;
9326
9327 if (off < 0)
9328 len += 1;
9329 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009330 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009331 --charlen;
9332 }
9333 }
9334 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009335 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009336 }
9337
9338 /*
9339 * Only return the overlap between the specified part and the actual
9340 * string.
9341 */
9342 if (nbyte < 0)
9343 {
9344 len += nbyte;
9345 nbyte = 0;
9346 }
9347 else if (nbyte > slen)
9348 nbyte = slen;
9349 if (len < 0)
9350 len = 0;
9351 else if (nbyte + len > slen)
9352 len = slen - nbyte;
9353
9354 rettv->v_type = VAR_STRING;
9355 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009356}
9357
9358/*
9359 * "strpart()" function
9360 */
9361 static void
9362f_strpart(typval_T *argvars, typval_T *rettv)
9363{
9364 char_u *p;
9365 int n;
9366 int len;
9367 int slen;
9368 int error = FALSE;
9369
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009370 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009371 slen = (int)STRLEN(p);
9372
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009373 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009374 if (error)
9375 len = 0;
9376 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009377 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009378 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009379 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009380
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009381 // Only return the overlap between the specified part and the actual
9382 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009383 if (n < 0)
9384 {
9385 len += n;
9386 n = 0;
9387 }
9388 else if (n > slen)
9389 n = slen;
9390 if (len < 0)
9391 len = 0;
9392 else if (n + len > slen)
9393 len = slen - n;
9394
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009395 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9396 {
9397 int off;
9398
9399 // length in characters
9400 for (off = n; off < slen && len > 0; --len)
9401 off += mb_ptr2len(p + off);
9402 len = off - n;
9403 }
9404
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009405 rettv->v_type = VAR_STRING;
9406 rettv->vval.v_string = vim_strnsave(p + n, len);
9407}
9408
9409/*
9410 * "strridx()" function
9411 */
9412 static void
9413f_strridx(typval_T *argvars, typval_T *rettv)
9414{
9415 char_u buf[NUMBUFLEN];
9416 char_u *needle;
9417 char_u *haystack;
9418 char_u *rest;
9419 char_u *lastmatch = NULL;
9420 int haystack_len, end_idx;
9421
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009422 needle = tv_get_string_chk(&argvars[1]);
9423 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009424
9425 rettv->vval.v_number = -1;
9426 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009427 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009428
9429 haystack_len = (int)STRLEN(haystack);
9430 if (argvars[2].v_type != VAR_UNKNOWN)
9431 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009432 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009433 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009434 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009435 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009436 }
9437 else
9438 end_idx = haystack_len;
9439
9440 if (*needle == NUL)
9441 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009442 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009443 lastmatch = haystack + end_idx;
9444 }
9445 else
9446 {
9447 for (rest = haystack; *rest != '\0'; ++rest)
9448 {
9449 rest = (char_u *)strstr((char *)rest, (char *)needle);
9450 if (rest == NULL || rest > haystack + end_idx)
9451 break;
9452 lastmatch = rest;
9453 }
9454 }
9455
9456 if (lastmatch == NULL)
9457 rettv->vval.v_number = -1;
9458 else
9459 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9460}
9461
9462/*
9463 * "strtrans()" function
9464 */
9465 static void
9466f_strtrans(typval_T *argvars, typval_T *rettv)
9467{
9468 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009469 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009470}
9471
9472/*
9473 * "submatch()" function
9474 */
9475 static void
9476f_submatch(typval_T *argvars, typval_T *rettv)
9477{
9478 int error = FALSE;
9479 int no;
9480 int retList = 0;
9481
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009482 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009483 if (error)
9484 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009485 if (no < 0 || no >= NSUBEXP)
9486 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009487 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009488 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009489 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009490 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009491 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009492 if (error)
9493 return;
9494
9495 if (retList == 0)
9496 {
9497 rettv->v_type = VAR_STRING;
9498 rettv->vval.v_string = reg_submatch(no);
9499 }
9500 else
9501 {
9502 rettv->v_type = VAR_LIST;
9503 rettv->vval.v_list = reg_submatch_list(no);
9504 }
9505}
9506
9507/*
9508 * "substitute()" function
9509 */
9510 static void
9511f_substitute(typval_T *argvars, typval_T *rettv)
9512{
9513 char_u patbuf[NUMBUFLEN];
9514 char_u subbuf[NUMBUFLEN];
9515 char_u flagsbuf[NUMBUFLEN];
9516
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009517 char_u *str = tv_get_string_chk(&argvars[0]);
9518 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009519 char_u *sub = NULL;
9520 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009521 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009522
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009523 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9524 expr = &argvars[2];
9525 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009526 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009527
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009528 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009529 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9530 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009531 rettv->vval.v_string = NULL;
9532 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009533 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009534}
9535
9536/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009537 * "swapinfo(swap_filename)" function
9538 */
9539 static void
9540f_swapinfo(typval_T *argvars, typval_T *rettv)
9541{
9542 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009543 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009544}
9545
9546/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009547 * "swapname(expr)" function
9548 */
9549 static void
9550f_swapname(typval_T *argvars, typval_T *rettv)
9551{
9552 buf_T *buf;
9553
9554 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009555 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009556 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9557 || buf->b_ml.ml_mfp->mf_fname == NULL)
9558 rettv->vval.v_string = NULL;
9559 else
9560 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9561}
9562
9563/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009564 * "synID(lnum, col, trans)" function
9565 */
9566 static void
9567f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9568{
9569 int id = 0;
9570#ifdef FEAT_SYN_HL
9571 linenr_T lnum;
9572 colnr_T col;
9573 int trans;
9574 int transerr = FALSE;
9575
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009576 lnum = tv_get_lnum(argvars); // -1 on type error
9577 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009578 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009579
9580 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9581 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9582 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9583#endif
9584
9585 rettv->vval.v_number = id;
9586}
9587
9588/*
9589 * "synIDattr(id, what [, mode])" function
9590 */
9591 static void
9592f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9593{
9594 char_u *p = NULL;
9595#ifdef FEAT_SYN_HL
9596 int id;
9597 char_u *what;
9598 char_u *mode;
9599 char_u modebuf[NUMBUFLEN];
9600 int modec;
9601
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009602 id = (int)tv_get_number(&argvars[0]);
9603 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009604 if (argvars[2].v_type != VAR_UNKNOWN)
9605 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009606 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009607 modec = TOLOWER_ASC(mode[0]);
9608 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009609 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009610 }
9611 else
9612 {
9613#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9614 if (USE_24BIT)
9615 modec = 'g';
9616 else
9617#endif
9618 if (t_colors > 1)
9619 modec = 'c';
9620 else
9621 modec = 't';
9622 }
9623
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009624 switch (TOLOWER_ASC(what[0]))
9625 {
9626 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009627 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009628 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009629 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009630 p = highlight_has_attr(id, HL_BOLD, modec);
9631 break;
9632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009633 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009634 p = highlight_color(id, what, modec);
9635 break;
9636
9637 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009638 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009639 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009640 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009641 p = highlight_has_attr(id, HL_ITALIC, modec);
9642 break;
9643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009644 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009645 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009646 break;
9647
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009648 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009649 p = highlight_has_attr(id, HL_INVERSE, modec);
9650 break;
9651
9652 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009653 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009654 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009655 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009656 else if (TOLOWER_ASC(what[1]) == 't' &&
9657 TOLOWER_ASC(what[2]) == 'r')
9658 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009659 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009660 p = highlight_has_attr(id, HL_STANDOUT, modec);
9661 break;
9662
9663 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009664 if (TOLOWER_ASC(what[1]) == 'l') // ul
9665 p = highlight_color(id, what, modec);
9666 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009667 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009668 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9669 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009670 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009671 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9672 break;
9673 }
9674
9675 if (p != NULL)
9676 p = vim_strsave(p);
9677#endif
9678 rettv->v_type = VAR_STRING;
9679 rettv->vval.v_string = p;
9680}
9681
9682/*
9683 * "synIDtrans(id)" function
9684 */
9685 static void
9686f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9687{
9688 int id;
9689
9690#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009691 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009692
9693 if (id > 0)
9694 id = syn_get_final_id(id);
9695 else
9696#endif
9697 id = 0;
9698
9699 rettv->vval.v_number = id;
9700}
9701
9702/*
9703 * "synconcealed(lnum, col)" function
9704 */
9705 static void
9706f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9707{
9708#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9709 linenr_T lnum;
9710 colnr_T col;
9711 int syntax_flags = 0;
9712 int cchar;
9713 int matchid = 0;
9714 char_u str[NUMBUFLEN];
9715#endif
9716
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009717 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009718
9719#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009720 lnum = tv_get_lnum(argvars); // -1 on type error
9721 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009722
Bram Moolenaara80faa82020-04-12 19:37:17 +02009723 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009724
9725 if (rettv_list_alloc(rettv) != FAIL)
9726 {
9727 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9728 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9729 && curwin->w_p_cole > 0)
9730 {
9731 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9732 syntax_flags = get_syntax_info(&matchid);
9733
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009734 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009735 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9736 {
9737 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009738 if (cchar == NUL && curwin->w_p_cole == 1)
Bram Moolenaareed9d462021-02-15 20:38:25 +01009739 cchar = (curwin->w_lcs_chars.conceal == NUL) ? ' '
9740 : curwin->w_lcs_chars.conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009741 if (cchar != NUL)
9742 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009743 if (has_mbyte)
9744 (*mb_char2bytes)(cchar, str);
9745 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009746 str[0] = cchar;
9747 }
9748 }
9749 }
9750
9751 list_append_number(rettv->vval.v_list,
9752 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009753 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009754 list_append_string(rettv->vval.v_list, str, -1);
9755 list_append_number(rettv->vval.v_list, matchid);
9756 }
9757#endif
9758}
9759
9760/*
9761 * "synstack(lnum, col)" function
9762 */
9763 static void
9764f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9765{
9766#ifdef FEAT_SYN_HL
9767 linenr_T lnum;
9768 colnr_T col;
9769 int i;
9770 int id;
9771#endif
9772
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009773 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009774
9775#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009776 lnum = tv_get_lnum(argvars); // -1 on type error
9777 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009778
9779 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9780 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9781 && rettv_list_alloc(rettv) != FAIL)
9782 {
9783 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9784 for (i = 0; ; ++i)
9785 {
9786 id = syn_get_stack_item(i);
9787 if (id < 0)
9788 break;
9789 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9790 break;
9791 }
9792 }
9793#endif
9794}
9795
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009796/*
9797 * "tabpagebuflist()" function
9798 */
9799 static void
9800f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9801{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009802 tabpage_T *tp;
9803 win_T *wp = NULL;
9804
9805 if (argvars[0].v_type == VAR_UNKNOWN)
9806 wp = firstwin;
9807 else
9808 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009809 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009810 if (tp != NULL)
9811 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9812 }
9813 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9814 {
9815 for (; wp != NULL; wp = wp->w_next)
9816 if (list_append_number(rettv->vval.v_list,
9817 wp->w_buffer->b_fnum) == FAIL)
9818 break;
9819 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009820}
9821
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009822/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009823 * "tagfiles()" function
9824 */
9825 static void
9826f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9827{
9828 char_u *fname;
9829 tagname_T tn;
9830 int first;
9831
9832 if (rettv_list_alloc(rettv) == FAIL)
9833 return;
9834 fname = alloc(MAXPATHL);
9835 if (fname == NULL)
9836 return;
9837
9838 for (first = TRUE; ; first = FALSE)
9839 if (get_tagfname(&tn, first, fname) == FAIL
9840 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9841 break;
9842 tagname_free(&tn);
9843 vim_free(fname);
9844}
9845
9846/*
9847 * "taglist()" function
9848 */
9849 static void
9850f_taglist(typval_T *argvars, typval_T *rettv)
9851{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009852 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009853 char_u *tag_pattern;
9854
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009855 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009856
9857 rettv->vval.v_number = FALSE;
9858 if (*tag_pattern == NUL)
9859 return;
9860
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009861 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009862 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009863 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009864 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009865}
9866
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009867#ifdef FEAT_FLOAT
9868/*
9869 * "tan()" function
9870 */
9871 static void
9872f_tan(typval_T *argvars, typval_T *rettv)
9873{
9874 float_T f = 0.0;
9875
9876 rettv->v_type = VAR_FLOAT;
9877 if (get_float_arg(argvars, &f) == OK)
9878 rettv->vval.v_float = tan(f);
9879 else
9880 rettv->vval.v_float = 0.0;
9881}
9882
9883/*
9884 * "tanh()" function
9885 */
9886 static void
9887f_tanh(typval_T *argvars, typval_T *rettv)
9888{
9889 float_T f = 0.0;
9890
9891 rettv->v_type = VAR_FLOAT;
9892 if (get_float_arg(argvars, &f) == OK)
9893 rettv->vval.v_float = tanh(f);
9894 else
9895 rettv->vval.v_float = 0.0;
9896}
9897#endif
9898
9899/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009900 * "tolower(string)" function
9901 */
9902 static void
9903f_tolower(typval_T *argvars, typval_T *rettv)
9904{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009905 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009906 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009907}
9908
9909/*
9910 * "toupper(string)" function
9911 */
9912 static void
9913f_toupper(typval_T *argvars, typval_T *rettv)
9914{
9915 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009916 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009917}
9918
9919/*
9920 * "tr(string, fromstr, tostr)" function
9921 */
9922 static void
9923f_tr(typval_T *argvars, typval_T *rettv)
9924{
9925 char_u *in_str;
9926 char_u *fromstr;
9927 char_u *tostr;
9928 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009929 int inlen;
9930 int fromlen;
9931 int tolen;
9932 int idx;
9933 char_u *cpstr;
9934 int cplen;
9935 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009936 char_u buf[NUMBUFLEN];
9937 char_u buf2[NUMBUFLEN];
9938 garray_T ga;
9939
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009940 in_str = tv_get_string(&argvars[0]);
9941 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9942 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009944 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009945 rettv->v_type = VAR_STRING;
9946 rettv->vval.v_string = NULL;
9947 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009948 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009949 ga_init2(&ga, (int)sizeof(char), 80);
9950
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009951 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009952 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009953 if (STRLEN(fromstr) != STRLEN(tostr))
9954 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009955error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009956 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009957 ga_clear(&ga);
9958 return;
9959 }
9960
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009961 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009962 while (*in_str != NUL)
9963 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009964 if (has_mbyte)
9965 {
9966 inlen = (*mb_ptr2len)(in_str);
9967 cpstr = in_str;
9968 cplen = inlen;
9969 idx = 0;
9970 for (p = fromstr; *p != NUL; p += fromlen)
9971 {
9972 fromlen = (*mb_ptr2len)(p);
9973 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9974 {
9975 for (p = tostr; *p != NUL; p += tolen)
9976 {
9977 tolen = (*mb_ptr2len)(p);
9978 if (idx-- == 0)
9979 {
9980 cplen = tolen;
9981 cpstr = p;
9982 break;
9983 }
9984 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009985 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009986 goto error;
9987 break;
9988 }
9989 ++idx;
9990 }
9991
9992 if (first && cpstr == in_str)
9993 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009994 // Check that fromstr and tostr have the same number of
9995 // (multi-byte) characters. Done only once when a character
9996 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009997 first = FALSE;
9998 for (p = tostr; *p != NUL; p += tolen)
9999 {
10000 tolen = (*mb_ptr2len)(p);
10001 --idx;
10002 }
10003 if (idx != 0)
10004 goto error;
10005 }
10006
10007 (void)ga_grow(&ga, cplen);
10008 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
10009 ga.ga_len += cplen;
10010
10011 in_str += inlen;
10012 }
10013 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010014 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010015 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010016 p = vim_strchr(fromstr, *in_str);
10017 if (p != NULL)
10018 ga_append(&ga, tostr[p - fromstr]);
10019 else
10020 ga_append(&ga, *in_str);
10021 ++in_str;
10022 }
10023 }
10024
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010025 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010026 (void)ga_grow(&ga, 1);
10027 ga_append(&ga, NUL);
10028
10029 rettv->vval.v_string = ga.ga_data;
10030}
10031
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010032/*
10033 * "trim({expr})" function
10034 */
10035 static void
10036f_trim(typval_T *argvars, typval_T *rettv)
10037{
10038 char_u buf1[NUMBUFLEN];
10039 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010040 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010041 char_u *mask = NULL;
10042 char_u *tail;
10043 char_u *prev;
10044 char_u *p;
10045 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010046 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010047
10048 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010049 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010050 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010051 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010052
10053 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010054 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010055 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010056
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010057 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010058 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010059 int error = 0;
10060
10061 // leading or trailing characters to trim
10062 dir = (int)tv_get_number_chk(&argvars[2], &error);
10063 if (error)
10064 return;
10065 if (dir < 0 || dir > 2)
10066 {
10067 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
10068 return;
10069 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010070 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010071 }
10072
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010073 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010074 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010075 // Trim leading characters
10076 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010077 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010078 c1 = PTR2CHAR(head);
10079 if (mask == NULL)
10080 {
10081 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010082 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010083 }
10084 else
10085 {
10086 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10087 if (c1 == PTR2CHAR(p))
10088 break;
10089 if (*p == NUL)
10090 break;
10091 }
10092 MB_PTR_ADV(head);
10093 }
10094 }
10095
10096 tail = head + STRLEN(head);
10097 if (dir == 0 || dir == 2)
10098 {
10099 // Trim trailing characters
10100 for (; tail > head; tail = prev)
10101 {
10102 prev = tail;
10103 MB_PTR_BACK(head, prev);
10104 c1 = PTR2CHAR(prev);
10105 if (mask == NULL)
10106 {
10107 if (c1 > ' ' && c1 != 0xa0)
10108 break;
10109 }
10110 else
10111 {
10112 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10113 if (c1 == PTR2CHAR(p))
10114 break;
10115 if (*p == NUL)
10116 break;
10117 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010118 }
10119 }
Bram Moolenaardf44a272020-06-07 20:49:05 +020010120 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010121}
10122
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010123#ifdef FEAT_FLOAT
10124/*
10125 * "trunc({float})" function
10126 */
10127 static void
10128f_trunc(typval_T *argvars, typval_T *rettv)
10129{
10130 float_T f = 0.0;
10131
10132 rettv->v_type = VAR_FLOAT;
10133 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010134 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010135 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
10136 else
10137 rettv->vval.v_float = 0.0;
10138}
10139#endif
10140
10141/*
10142 * "type(expr)" function
10143 */
10144 static void
10145f_type(typval_T *argvars, typval_T *rettv)
10146{
10147 int n = -1;
10148
10149 switch (argvars[0].v_type)
10150 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010151 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
10152 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010153 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010154 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
10155 case VAR_LIST: n = VAR_TYPE_LIST; break;
10156 case VAR_DICT: n = VAR_TYPE_DICT; break;
10157 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
10158 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
10159 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020010160 case VAR_JOB: n = VAR_TYPE_JOB; break;
10161 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010162 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010163 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +020010164 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010165 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +010010166 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010167 n = -1;
10168 break;
10169 }
10170 rettv->vval.v_number = n;
10171}
10172
10173/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010174 * "virtcol(string)" function
10175 */
10176 static void
10177f_virtcol(typval_T *argvars, typval_T *rettv)
10178{
10179 colnr_T vcol = 0;
10180 pos_T *fp;
10181 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010182 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010183
Bram Moolenaar6f02b002021-01-10 20:22:54 +010010184 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010185 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
10186 && fnum == curbuf->b_fnum)
10187 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010188 // Limit the column to a valid value, getvvcol() doesn't check.
10189 if (fp->col < 0)
10190 fp->col = 0;
10191 else
10192 {
10193 len = (int)STRLEN(ml_get(fp->lnum));
10194 if (fp->col > len)
10195 fp->col = len;
10196 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010197 getvvcol(curwin, fp, NULL, NULL, &vcol);
10198 ++vcol;
10199 }
10200
10201 rettv->vval.v_number = vcol;
10202}
10203
10204/*
10205 * "visualmode()" function
10206 */
10207 static void
10208f_visualmode(typval_T *argvars, typval_T *rettv)
10209{
10210 char_u str[2];
10211
10212 rettv->v_type = VAR_STRING;
10213 str[0] = curbuf->b_visual_mode_eval;
10214 str[1] = NUL;
10215 rettv->vval.v_string = vim_strsave(str);
10216
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010217 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010218 if (non_zero_arg(&argvars[0]))
10219 curbuf->b_visual_mode_eval = NUL;
10220}
10221
10222/*
10223 * "wildmenumode()" function
10224 */
10225 static void
10226f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10227{
10228#ifdef FEAT_WILDMENU
10229 if (wild_menu_showing)
10230 rettv->vval.v_number = 1;
10231#endif
10232}
10233
10234/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +010010235 * "windowsversion()" function
10236 */
10237 static void
10238f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10239{
10240 rettv->v_type = VAR_STRING;
10241 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
10242}
10243
10244/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010245 * "wordcount()" function
10246 */
10247 static void
10248f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
10249{
10250 if (rettv_dict_alloc(rettv) == FAIL)
10251 return;
10252 cursor_pos_info(rettv->vval.v_dict);
10253}
10254
10255/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010256 * "xor(expr, expr)" function
10257 */
10258 static void
10259f_xor(typval_T *argvars, typval_T *rettv)
10260{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010261 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
10262 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010263}
10264
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010265#endif // FEAT_EVAL