blob: b752c51d10d2d2c56ebf74f12df9474faed3af17 [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/*
334 * Check "type" is a list or a blob.
335 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200336 static int
337arg_list_or_blob(type_T *type, argcontext_T *context)
338{
339 if (type->tt_type == VAR_ANY
340 || type->tt_type == VAR_LIST || type->tt_type == VAR_BLOB)
341 return OK;
342 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
343 return FAIL;
344}
345
346/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100347 * Check "type" is a list or a dict.
348 */
349 static int
350arg_list_or_dict(type_T *type, argcontext_T *context)
351{
352 if (type->tt_type == VAR_ANY
353 || type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
354 return OK;
355 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
356 return FAIL;
357}
358
359/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100360 * Check "type" is the same type as the previous argument.
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100361 * Must not be used for the first argcheck_T entry.
362 */
363 static int
364arg_same_as_prev(type_T *type, argcontext_T *context)
365{
366 type_T *prev_type = context->arg_types[context->arg_idx - 1];
367
Bram Moolenaar351ead02021-01-16 16:07:01 +0100368 return check_arg_type(prev_type, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100369}
370
371/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100372 * Check "type" is the same basic type as the previous argument, checks list or
373 * dict vs other type, but not member type.
374 * Must not be used for the first argcheck_T entry.
375 */
376 static int
377arg_same_struct_as_prev(type_T *type, argcontext_T *context)
378{
379 type_T *prev_type = context->arg_types[context->arg_idx - 1];
380
381 if (prev_type->tt_type != context->arg_types[context->arg_idx]->tt_type)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100382 return check_arg_type(prev_type, type, context);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100383 return OK;
384}
385
386/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100387 * Check "type" is an item of the list or blob of the previous arg.
Bram Moolenaarca174532020-10-21 16:42:22 +0200388 * Must not be used for the first argcheck_T entry.
389 */
390 static int
391arg_item_of_prev(type_T *type, argcontext_T *context)
392{
393 type_T *prev_type = context->arg_types[context->arg_idx - 1];
394 type_T *expected;
395
396 if (prev_type->tt_type == VAR_LIST)
397 expected = prev_type->tt_member;
398 else if (prev_type->tt_type == VAR_BLOB)
399 expected = &t_number;
400 else
401 // probably VAR_ANY, can't check
402 return OK;
403
Bram Moolenaar351ead02021-01-16 16:07:01 +0100404 return check_arg_type(expected, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200405}
406
Bram Moolenaar94738d82020-10-21 14:25:07 +0200407/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100408 * Check "type" which is the third argument of extend().
409 */
410 static int
411arg_extend3(type_T *type, argcontext_T *context)
412{
413 type_T *first_type = context->arg_types[context->arg_idx - 2];
414
415 if (first_type->tt_type == VAR_LIST)
416 return arg_number(type, context);
417 if (first_type->tt_type == VAR_DICT)
418 return arg_string(type, context);
419 return OK;
420}
421
422
423/*
Bram Moolenaar94738d82020-10-21 14:25:07 +0200424 * Lists of functions that check the argument types of a builtin function.
425 */
426argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100427argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev};
428argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3};
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100429argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
Bram Moolenaarca174532020-10-21 16:42:22 +0200430argcheck_T arg3_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200431
432/*
433 * Functions that return the return type of a builtin function.
Bram Moolenaara1224cb2020-10-22 12:31:49 +0200434 * Note that "argtypes" is NULL if "argcount" is zero.
Bram Moolenaar94738d82020-10-21 14:25:07 +0200435 */
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100436 static type_T *
437ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
438{
439 return &t_void;
440}
441 static type_T *
442ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
443{
444 return &t_any;
445}
446 static type_T *
Bram Moolenaar403dc312020-10-17 19:29:51 +0200447ret_bool(int argcount UNUSED, type_T **argtypes UNUSED)
448{
449 return &t_bool;
450}
451 static type_T *
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100452ret_number_bool(int argcount UNUSED, type_T **argtypes UNUSED)
453{
454 return &t_number_bool;
455}
456 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100457ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
458{
459 return &t_number;
460}
461 static type_T *
462ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
463{
464 return &t_float;
465}
466 static type_T *
467ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
468{
469 return &t_string;
470}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200471 static type_T *
472ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100473{
474 return &t_list_any;
475}
476 static type_T *
477ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
478{
479 return &t_list_number;
480}
481 static type_T *
482ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
483{
484 return &t_list_string;
485}
486 static type_T *
487ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
488{
489 return &t_list_dict_any;
490}
491 static type_T *
492ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
493{
494 return &t_dict_any;
495}
496 static type_T *
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100497ret_job_info(int argcount, type_T **argtypes UNUSED)
498{
499 if (argcount == 0)
500 return &t_list_job;
501 return &t_dict_any;
502}
503 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100504ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
505{
506 return &t_dict_number;
507}
508 static type_T *
509ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
510{
511 return &t_dict_string;
512}
513 static type_T *
514ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
515{
516 return &t_blob;
517}
518 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200519ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100520{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200521 return &t_func_any;
522}
523 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100524ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
525{
526 return &t_channel;
527}
528 static type_T *
529ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
530{
531 return &t_job;
532}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200533 static type_T *
534ret_first_arg(int argcount, type_T **argtypes)
535{
536 if (argcount > 0)
537 return argtypes[0];
538 return &t_void;
539}
Bram Moolenaarea696852020-11-09 18:31:39 +0100540// for map(): returns first argument but item type may differ
541 static type_T *
542ret_first_cont(int argcount UNUSED, type_T **argtypes)
543{
544 if (argtypes[0]->tt_type == VAR_LIST)
545 return &t_list_any;
546 if (argtypes[0]->tt_type == VAR_DICT)
547 return &t_dict_any;
548 if (argtypes[0]->tt_type == VAR_BLOB)
549 return argtypes[0];
550 return &t_any;
551}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200552
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200553/*
554 * Used for getqflist(): returns list if there is no argument, dict if there is
555 * one.
556 */
557 static type_T *
558ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
559{
560 if (argcount > 0)
561 return &t_dict_any;
562 return &t_list_dict_any;
563}
564
565/*
566 * Used for getloclist(): returns list if there is one argument, dict if there
567 * are two.
568 */
569 static type_T *
570ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
571{
572 if (argcount > 1)
573 return &t_dict_any;
574 return &t_list_dict_any;
575}
576
Bram Moolenaar846178a2020-07-05 17:04:13 +0200577 static type_T *
578ret_argv(int argcount, type_T **argtypes UNUSED)
579{
580 // argv() returns list of strings
581 if (argcount == 0)
582 return &t_list_string;
583
584 // argv(0) returns a string, but argv(-1] returns a list
585 return &t_any;
586}
587
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200588 static type_T *
589ret_remove(int argcount UNUSED, type_T **argtypes)
590{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200591 if (argtypes != NULL)
592 {
593 if (argtypes[0]->tt_type == VAR_LIST
594 || argtypes[0]->tt_type == VAR_DICT)
595 return argtypes[0]->tt_member;
596 if (argtypes[0]->tt_type == VAR_BLOB)
597 return &t_number;
598 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200599 return &t_any;
600}
601
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200602 static type_T *
603ret_getreg(int argcount, type_T **argtypes UNUSED)
604{
605 // Assume that if the third argument is passed it's non-zero
606 if (argcount == 3)
607 return &t_list_string;
608 return &t_string;
609}
610
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200611 static type_T *
612ret_maparg(int argcount, type_T **argtypes UNUSED)
613{
614 // Assume that if the fourth argument is passed it's non-zero
615 if (argcount == 4)
616 return &t_dict_any;
617 return &t_string;
618}
619
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100620static type_T *ret_f_function(int argcount, type_T **argtypes);
621
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200622/*
623 * Array with names and number of arguments of all internal functions
624 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
625 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200626typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200627{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200628 char *f_name; // function name
629 char f_min_argc; // minimal number of arguments
630 char f_max_argc; // maximal number of arguments
631 char f_argtype; // for method: FEARG_ values
Bram Moolenaar94738d82020-10-21 14:25:07 +0200632 argcheck_T *f_argcheck; // list of functions to check argument types
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100633 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
634 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200635 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200636 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200637} funcentry_T;
638
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200639// values for f_argtype; zero means it cannot be used as a method
640#define FEARG_1 1 // base is the first argument
641#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200642#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200643#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200644#define FEARG_LAST 9 // base is the last argument
645
Bram Moolenaar15c47602020-03-26 22:16:48 +0100646#ifdef FEAT_FLOAT
647# define FLOAT_FUNC(name) name
648#else
649# define FLOAT_FUNC(name) NULL
650#endif
651#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
652# define MATH_FUNC(name) name
653#else
654# define MATH_FUNC(name) NULL
655#endif
656#ifdef FEAT_TIMERS
657# define TIMER_FUNC(name) name
658#else
659# define TIMER_FUNC(name) NULL
660#endif
661#ifdef FEAT_JOB_CHANNEL
662# define JOB_FUNC(name) name
663#else
664# define JOB_FUNC(name) NULL
665#endif
666#ifdef FEAT_PROP_POPUP
667# define PROP_FUNC(name) name
668#else
669# define PROP_FUNC(name) NULL
670#endif
671#ifdef FEAT_SIGNS
672# define SIGN_FUNC(name) name
673#else
674# define SIGN_FUNC(name) NULL
675#endif
676#ifdef FEAT_SOUND
677# define SOUND_FUNC(name) name
678#else
679# define SOUND_FUNC(name) NULL
680#endif
681#ifdef FEAT_TERMINAL
682# define TERM_FUNC(name) name
683#else
684# define TERM_FUNC(name) NULL
685#endif
686
Bram Moolenaarac92e252019-08-03 21:58:38 +0200687static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200688{
Bram Moolenaar94738d82020-10-21 14:25:07 +0200689 {"abs", 1, 1, FEARG_1, arg1_float_or_nr,
690 ret_any, FLOAT_FUNC(f_abs)},
691 {"acos", 1, 1, FEARG_1, NULL,
692 ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100693 {"add", 2, 2, FEARG_1, NULL /* arg2_listblob_item */,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200694 ret_first_arg, f_add},
695 {"and", 2, 2, FEARG_1, NULL,
696 ret_number, f_and},
697 {"append", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100698 ret_number_bool, f_append},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200699 {"appendbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100700 ret_number_bool, f_appendbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200701 {"argc", 0, 1, 0, NULL,
702 ret_number, f_argc},
703 {"argidx", 0, 0, 0, NULL,
704 ret_number, f_argidx},
705 {"arglistid", 0, 2, 0, NULL,
706 ret_number, f_arglistid},
707 {"argv", 0, 2, 0, NULL,
708 ret_argv, f_argv},
709 {"asin", 1, 1, FEARG_1, NULL,
710 ret_float, FLOAT_FUNC(f_asin)},
711 {"assert_beeps", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100712 ret_number_bool, f_assert_beeps},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200713 {"assert_equal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100714 ret_number_bool, f_assert_equal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200715 {"assert_equalfile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100716 ret_number_bool, f_assert_equalfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200717 {"assert_exception", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100718 ret_number_bool, f_assert_exception},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200719 {"assert_fails", 1, 5, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100720 ret_number_bool, f_assert_fails},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200721 {"assert_false", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100722 ret_number_bool, f_assert_false},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200723 {"assert_inrange", 3, 4, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100724 ret_number_bool, f_assert_inrange},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200725 {"assert_match", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100726 ret_number_bool, f_assert_match},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200727 {"assert_notequal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100728 ret_number_bool, f_assert_notequal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200729 {"assert_notmatch", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100730 ret_number_bool, f_assert_notmatch},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200731 {"assert_report", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100732 ret_number_bool, f_assert_report},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200733 {"assert_true", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100734 ret_number_bool, f_assert_true},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200735 {"atan", 1, 1, FEARG_1, NULL,
736 ret_float, FLOAT_FUNC(f_atan)},
737 {"atan2", 2, 2, FEARG_1, NULL,
738 ret_float, FLOAT_FUNC(f_atan2)},
739 {"balloon_gettext", 0, 0, 0, NULL,
740 ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100741#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100742 f_balloon_gettext
743#else
744 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100745#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100746 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200747 {"balloon_show", 1, 1, FEARG_1, NULL,
748 ret_void,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100749#ifdef FEAT_BEVAL
750 f_balloon_show
751#else
752 NULL
753#endif
754 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200755 {"balloon_split", 1, 1, FEARG_1, NULL,
756 ret_list_string,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100757#if defined(FEAT_BEVAL_TERM)
758 f_balloon_split
759#else
760 NULL
761#endif
762 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200763 {"browse", 4, 4, 0, NULL,
764 ret_string, f_browse},
765 {"browsedir", 2, 2, 0, NULL,
766 ret_string, f_browsedir},
767 {"bufadd", 1, 1, FEARG_1, NULL,
768 ret_number, f_bufadd},
769 {"bufexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100770 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200771 {"buffer_exists", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100772 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200773 {"buffer_name", 0, 1, FEARG_1, NULL, // obsolete
774 ret_string, f_bufname},
775 {"buffer_number", 0, 1, FEARG_1, NULL, // obsolete
776 ret_number, f_bufnr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200777 {"buflisted", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100778 ret_number_bool, f_buflisted},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200779 {"bufload", 1, 1, FEARG_1, NULL,
780 ret_void, f_bufload},
781 {"bufloaded", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100782 ret_number_bool, f_bufloaded},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200783 {"bufname", 0, 1, FEARG_1, NULL,
784 ret_string, f_bufname},
785 {"bufnr", 0, 2, FEARG_1, NULL,
786 ret_number, f_bufnr},
787 {"bufwinid", 1, 1, FEARG_1, NULL,
788 ret_number, f_bufwinid},
789 {"bufwinnr", 1, 1, FEARG_1, NULL,
790 ret_number, f_bufwinnr},
791 {"byte2line", 1, 1, FEARG_1, NULL,
792 ret_number, f_byte2line},
793 {"byteidx", 2, 2, FEARG_1, NULL,
794 ret_number, f_byteidx},
795 {"byteidxcomp", 2, 2, FEARG_1, NULL,
796 ret_number, f_byteidxcomp},
797 {"call", 2, 3, FEARG_1, NULL,
798 ret_any, f_call},
799 {"ceil", 1, 1, FEARG_1, NULL,
800 ret_float, FLOAT_FUNC(f_ceil)},
801 {"ch_canread", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100802 ret_number_bool, JOB_FUNC(f_ch_canread)},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200803 {"ch_close", 1, 1, FEARG_1, NULL,
804 ret_void, JOB_FUNC(f_ch_close)},
805 {"ch_close_in", 1, 1, FEARG_1, NULL,
806 ret_void, JOB_FUNC(f_ch_close_in)},
807 {"ch_evalexpr", 2, 3, FEARG_1, NULL,
808 ret_any, JOB_FUNC(f_ch_evalexpr)},
809 {"ch_evalraw", 2, 3, FEARG_1, NULL,
810 ret_any, JOB_FUNC(f_ch_evalraw)},
811 {"ch_getbufnr", 2, 2, FEARG_1, NULL,
812 ret_number, JOB_FUNC(f_ch_getbufnr)},
813 {"ch_getjob", 1, 1, FEARG_1, NULL,
814 ret_job, JOB_FUNC(f_ch_getjob)},
815 {"ch_info", 1, 1, FEARG_1, NULL,
816 ret_dict_any, JOB_FUNC(f_ch_info)},
817 {"ch_log", 1, 2, FEARG_1, NULL,
818 ret_void, JOB_FUNC(f_ch_log)},
819 {"ch_logfile", 1, 2, FEARG_1, NULL,
820 ret_void, JOB_FUNC(f_ch_logfile)},
821 {"ch_open", 1, 2, FEARG_1, NULL,
822 ret_channel, JOB_FUNC(f_ch_open)},
823 {"ch_read", 1, 2, FEARG_1, NULL,
824 ret_string, JOB_FUNC(f_ch_read)},
825 {"ch_readblob", 1, 2, FEARG_1, NULL,
826 ret_blob, JOB_FUNC(f_ch_readblob)},
827 {"ch_readraw", 1, 2, FEARG_1, NULL,
828 ret_string, JOB_FUNC(f_ch_readraw)},
829 {"ch_sendexpr", 2, 3, FEARG_1, NULL,
830 ret_void, JOB_FUNC(f_ch_sendexpr)},
831 {"ch_sendraw", 2, 3, FEARG_1, NULL,
832 ret_void, JOB_FUNC(f_ch_sendraw)},
833 {"ch_setoptions", 2, 2, FEARG_1, NULL,
834 ret_void, JOB_FUNC(f_ch_setoptions)},
835 {"ch_status", 1, 2, FEARG_1, NULL,
836 ret_string, JOB_FUNC(f_ch_status)},
837 {"changenr", 0, 0, 0, NULL,
838 ret_number, f_changenr},
839 {"char2nr", 1, 2, FEARG_1, NULL,
840 ret_number, f_char2nr},
841 {"charclass", 1, 1, FEARG_1, NULL,
842 ret_number, f_charclass},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100843 {"charcol", 1, 1, FEARG_1, NULL,
844 ret_number, f_charcol},
Bram Moolenaar17793ef2020-12-28 12:56:58 +0100845 {"charidx", 2, 3, FEARG_1, NULL,
846 ret_number, f_charidx},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200847 {"chdir", 1, 1, FEARG_1, NULL,
848 ret_string, f_chdir},
849 {"cindent", 1, 1, FEARG_1, NULL,
850 ret_number, f_cindent},
851 {"clearmatches", 0, 1, FEARG_1, NULL,
852 ret_void, f_clearmatches},
853 {"col", 1, 1, FEARG_1, NULL,
854 ret_number, f_col},
855 {"complete", 2, 2, FEARG_2, NULL,
856 ret_void, f_complete},
857 {"complete_add", 1, 1, FEARG_1, NULL,
858 ret_number, f_complete_add},
859 {"complete_check", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100860 ret_number_bool, f_complete_check},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200861 {"complete_info", 0, 1, FEARG_1, NULL,
862 ret_dict_any, f_complete_info},
863 {"confirm", 1, 4, FEARG_1, NULL,
864 ret_number, f_confirm},
865 {"copy", 1, 1, FEARG_1, NULL,
866 ret_first_arg, f_copy},
867 {"cos", 1, 1, FEARG_1, NULL,
868 ret_float, FLOAT_FUNC(f_cos)},
869 {"cosh", 1, 1, FEARG_1, NULL,
870 ret_float, FLOAT_FUNC(f_cosh)},
871 {"count", 2, 4, FEARG_1, NULL,
872 ret_number, f_count},
873 {"cscope_connection",0,3, 0, NULL,
874 ret_number, f_cscope_connection},
875 {"cursor", 1, 3, FEARG_1, NULL,
876 ret_number, f_cursor},
877 {"debugbreak", 1, 1, FEARG_1, NULL,
878 ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100879#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100880 f_debugbreak
881#else
882 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200883#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100884 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200885 {"deepcopy", 1, 2, FEARG_1, NULL,
886 ret_first_arg, f_deepcopy},
887 {"delete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100888 ret_number_bool, f_delete},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200889 {"deletebufline", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100890 ret_number_bool, f_deletebufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200891 {"did_filetype", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100892 ret_number_bool, f_did_filetype},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200893 {"diff_filler", 1, 1, FEARG_1, NULL,
894 ret_number, f_diff_filler},
895 {"diff_hlID", 2, 2, FEARG_1, NULL,
896 ret_number, f_diff_hlID},
897 {"echoraw", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100898 ret_void, f_echoraw},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200899 {"empty", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100900 ret_number_bool, f_empty},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200901 {"environ", 0, 0, 0, NULL,
902 ret_dict_string, f_environ},
903 {"escape", 2, 2, FEARG_1, NULL,
904 ret_string, f_escape},
905 {"eval", 1, 1, FEARG_1, NULL,
906 ret_any, f_eval},
907 {"eventhandler", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100908 ret_number_bool, f_eventhandler},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200909 {"executable", 1, 1, FEARG_1, NULL,
910 ret_number, f_executable},
911 {"execute", 1, 2, FEARG_1, NULL,
912 ret_string, f_execute},
913 {"exepath", 1, 1, FEARG_1, NULL,
914 ret_string, f_exepath},
915 {"exists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100916 ret_number_bool, f_exists},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200917 {"exp", 1, 1, FEARG_1, NULL,
918 ret_float, FLOAT_FUNC(f_exp)},
919 {"expand", 1, 3, FEARG_1, NULL,
920 ret_any, f_expand},
921 {"expandcmd", 1, 1, FEARG_1, NULL,
922 ret_string, f_expandcmd},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100923 {"extend", 2, 3, FEARG_1, arg23_extend,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200924 ret_first_arg, f_extend},
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100925 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
926 ret_first_cont, f_extendnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200927 {"feedkeys", 1, 2, FEARG_1, NULL,
928 ret_void, f_feedkeys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200929 {"file_readable", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100930 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200931 {"filereadable", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100932 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200933 {"filewritable", 1, 1, FEARG_1, NULL,
934 ret_number, f_filewritable},
935 {"filter", 2, 2, FEARG_1, NULL,
936 ret_first_arg, f_filter},
937 {"finddir", 1, 3, FEARG_1, NULL,
938 ret_string, f_finddir},
939 {"findfile", 1, 3, FEARG_1, NULL,
940 ret_string, f_findfile},
941 {"flatten", 1, 2, FEARG_1, NULL,
942 ret_list_any, f_flatten},
943 {"float2nr", 1, 1, FEARG_1, NULL,
944 ret_number, FLOAT_FUNC(f_float2nr)},
945 {"floor", 1, 1, FEARG_1, NULL,
946 ret_float, FLOAT_FUNC(f_floor)},
947 {"fmod", 2, 2, FEARG_1, NULL,
948 ret_float, FLOAT_FUNC(f_fmod)},
949 {"fnameescape", 1, 1, FEARG_1, NULL,
950 ret_string, f_fnameescape},
951 {"fnamemodify", 2, 2, FEARG_1, NULL,
952 ret_string, f_fnamemodify},
953 {"foldclosed", 1, 1, FEARG_1, NULL,
954 ret_number, f_foldclosed},
955 {"foldclosedend", 1, 1, FEARG_1, NULL,
956 ret_number, f_foldclosedend},
957 {"foldlevel", 1, 1, FEARG_1, NULL,
958 ret_number, f_foldlevel},
959 {"foldtext", 0, 0, 0, NULL,
960 ret_string, f_foldtext},
961 {"foldtextresult", 1, 1, FEARG_1, NULL,
962 ret_string, f_foldtextresult},
963 {"foreground", 0, 0, 0, NULL,
964 ret_void, f_foreground},
965 {"funcref", 1, 3, FEARG_1, NULL,
966 ret_func_any, f_funcref},
967 {"function", 1, 3, FEARG_1, NULL,
968 ret_f_function, f_function},
969 {"garbagecollect", 0, 1, 0, NULL,
970 ret_void, f_garbagecollect},
971 {"get", 2, 3, FEARG_1, NULL,
972 ret_any, f_get},
973 {"getbufinfo", 0, 1, FEARG_1, NULL,
974 ret_list_dict_any, f_getbufinfo},
975 {"getbufline", 2, 3, FEARG_1, NULL,
976 ret_list_string, f_getbufline},
977 {"getbufvar", 2, 3, FEARG_1, NULL,
978 ret_any, f_getbufvar},
979 {"getchangelist", 0, 1, FEARG_1, NULL,
980 ret_list_any, f_getchangelist},
981 {"getchar", 0, 1, 0, NULL,
982 ret_number, f_getchar},
983 {"getcharmod", 0, 0, 0, NULL,
984 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100985 {"getcharpos", 1, 1, FEARG_1, NULL,
986 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200987 {"getcharsearch", 0, 0, 0, NULL,
988 ret_dict_any, f_getcharsearch},
989 {"getcmdline", 0, 0, 0, NULL,
990 ret_string, f_getcmdline},
991 {"getcmdpos", 0, 0, 0, NULL,
992 ret_number, f_getcmdpos},
993 {"getcmdtype", 0, 0, 0, NULL,
994 ret_string, f_getcmdtype},
995 {"getcmdwintype", 0, 0, 0, NULL,
996 ret_string, f_getcmdwintype},
997 {"getcompletion", 2, 3, FEARG_1, NULL,
998 ret_list_string, f_getcompletion},
999 {"getcurpos", 0, 1, FEARG_1, NULL,
1000 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001001 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
1002 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001003 {"getcwd", 0, 2, FEARG_1, NULL,
1004 ret_string, f_getcwd},
1005 {"getenv", 1, 1, FEARG_1, NULL,
1006 ret_string, f_getenv},
1007 {"getfontname", 0, 1, 0, NULL,
1008 ret_string, f_getfontname},
1009 {"getfperm", 1, 1, FEARG_1, NULL,
1010 ret_string, f_getfperm},
1011 {"getfsize", 1, 1, FEARG_1, NULL,
1012 ret_number, f_getfsize},
1013 {"getftime", 1, 1, FEARG_1, NULL,
1014 ret_number, f_getftime},
1015 {"getftype", 1, 1, FEARG_1, NULL,
1016 ret_string, f_getftype},
1017 {"getimstatus", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001018 ret_number_bool, f_getimstatus},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001019 {"getjumplist", 0, 2, FEARG_1, NULL,
1020 ret_list_any, f_getjumplist},
1021 {"getline", 1, 2, FEARG_1, NULL,
1022 ret_f_getline, f_getline},
1023 {"getloclist", 1, 2, 0, NULL,
1024 ret_list_or_dict_1, f_getloclist},
1025 {"getmarklist", 0, 1, FEARG_1, NULL,
1026 ret_list_dict_any, f_getmarklist},
1027 {"getmatches", 0, 1, 0, NULL,
1028 ret_list_dict_any, f_getmatches},
1029 {"getmousepos", 0, 0, 0, NULL,
1030 ret_dict_number, f_getmousepos},
1031 {"getpid", 0, 0, 0, NULL,
1032 ret_number, f_getpid},
1033 {"getpos", 1, 1, FEARG_1, NULL,
1034 ret_list_number, f_getpos},
1035 {"getqflist", 0, 1, 0, NULL,
1036 ret_list_or_dict_0, f_getqflist},
1037 {"getreg", 0, 3, FEARG_1, NULL,
1038 ret_getreg, f_getreg},
1039 {"getreginfo", 0, 1, FEARG_1, NULL,
1040 ret_dict_any, f_getreginfo},
1041 {"getregtype", 0, 1, FEARG_1, NULL,
1042 ret_string, f_getregtype},
1043 {"gettabinfo", 0, 1, FEARG_1, NULL,
1044 ret_list_dict_any, f_gettabinfo},
1045 {"gettabvar", 2, 3, FEARG_1, NULL,
1046 ret_any, f_gettabvar},
1047 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1048 ret_any, f_gettabwinvar},
1049 {"gettagstack", 0, 1, FEARG_1, NULL,
1050 ret_dict_any, f_gettagstack},
1051 {"gettext", 1, 1, FEARG_1, NULL,
1052 ret_string, f_gettext},
1053 {"getwininfo", 0, 1, FEARG_1, NULL,
1054 ret_list_dict_any, f_getwininfo},
1055 {"getwinpos", 0, 1, FEARG_1, NULL,
1056 ret_list_number, f_getwinpos},
1057 {"getwinposx", 0, 0, 0, NULL,
1058 ret_number, f_getwinposx},
1059 {"getwinposy", 0, 0, 0, NULL,
1060 ret_number, f_getwinposy},
1061 {"getwinvar", 2, 3, FEARG_1, NULL,
1062 ret_any, f_getwinvar},
1063 {"glob", 1, 4, FEARG_1, NULL,
1064 ret_any, f_glob},
1065 {"glob2regpat", 1, 1, FEARG_1, NULL,
1066 ret_string, f_glob2regpat},
1067 {"globpath", 2, 5, FEARG_2, NULL,
1068 ret_any, f_globpath},
1069 {"has", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001070 ret_number_bool, f_has},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001071 {"has_key", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001072 ret_number_bool, f_has_key},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001073 {"haslocaldir", 0, 2, FEARG_1, NULL,
1074 ret_number, f_haslocaldir},
1075 {"hasmapto", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001076 ret_number_bool, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001077 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1078 ret_number, f_hlID},
1079 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001080 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001081 {"histadd", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001082 ret_number_bool, f_histadd},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001083 {"histdel", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001084 ret_number_bool, f_histdel},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001085 {"histget", 1, 2, FEARG_1, NULL,
1086 ret_string, f_histget},
1087 {"histnr", 1, 1, FEARG_1, NULL,
1088 ret_number, f_histnr},
1089 {"hlID", 1, 1, FEARG_1, NULL,
1090 ret_number, f_hlID},
1091 {"hlexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001092 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001093 {"hostname", 0, 0, 0, NULL,
1094 ret_string, f_hostname},
1095 {"iconv", 3, 3, FEARG_1, NULL,
1096 ret_string, f_iconv},
1097 {"indent", 1, 1, FEARG_1, NULL,
1098 ret_number, f_indent},
1099 {"index", 2, 4, FEARG_1, NULL,
1100 ret_number, f_index},
1101 {"input", 1, 3, FEARG_1, NULL,
1102 ret_string, f_input},
1103 {"inputdialog", 1, 3, FEARG_1, NULL,
1104 ret_string, f_inputdialog},
1105 {"inputlist", 1, 1, FEARG_1, NULL,
1106 ret_number, f_inputlist},
1107 {"inputrestore", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001108 ret_number_bool, f_inputrestore},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001109 {"inputsave", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001110 ret_number_bool, f_inputsave},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001111 {"inputsecret", 1, 2, FEARG_1, NULL,
1112 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001113 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001114 ret_first_arg, f_insert},
1115 {"interrupt", 0, 0, 0, NULL,
1116 ret_void, f_interrupt},
1117 {"invert", 1, 1, FEARG_1, NULL,
1118 ret_number, f_invert},
1119 {"isdirectory", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001120 ret_number_bool, f_isdirectory},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001121 {"isinf", 1, 1, FEARG_1, NULL,
1122 ret_number, MATH_FUNC(f_isinf)},
1123 {"islocked", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001124 ret_number_bool, f_islocked},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001125 {"isnan", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001126 ret_number_bool, MATH_FUNC(f_isnan)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001127 {"items", 1, 1, FEARG_1, NULL,
1128 ret_list_any, f_items},
1129 {"job_getchannel", 1, 1, FEARG_1, NULL,
1130 ret_channel, JOB_FUNC(f_job_getchannel)},
1131 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001132 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001133 {"job_setoptions", 2, 2, FEARG_1, NULL,
1134 ret_void, JOB_FUNC(f_job_setoptions)},
1135 {"job_start", 1, 2, FEARG_1, NULL,
1136 ret_job, JOB_FUNC(f_job_start)},
1137 {"job_status", 1, 1, FEARG_1, NULL,
1138 ret_string, JOB_FUNC(f_job_status)},
1139 {"job_stop", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001140 ret_number_bool, JOB_FUNC(f_job_stop)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001141 {"join", 1, 2, FEARG_1, NULL,
1142 ret_string, f_join},
1143 {"js_decode", 1, 1, FEARG_1, NULL,
1144 ret_any, f_js_decode},
1145 {"js_encode", 1, 1, FEARG_1, NULL,
1146 ret_string, f_js_encode},
1147 {"json_decode", 1, 1, FEARG_1, NULL,
1148 ret_any, f_json_decode},
1149 {"json_encode", 1, 1, FEARG_1, NULL,
1150 ret_string, f_json_encode},
1151 {"keys", 1, 1, FEARG_1, NULL,
1152 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001153 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1154 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001155 {"len", 1, 1, FEARG_1, NULL,
1156 ret_number, f_len},
1157 {"libcall", 3, 3, FEARG_3, NULL,
1158 ret_string, f_libcall},
1159 {"libcallnr", 3, 3, FEARG_3, NULL,
1160 ret_number, f_libcallnr},
1161 {"line", 1, 2, FEARG_1, NULL,
1162 ret_number, f_line},
1163 {"line2byte", 1, 1, FEARG_1, NULL,
1164 ret_number, f_line2byte},
1165 {"lispindent", 1, 1, FEARG_1, NULL,
1166 ret_number, f_lispindent},
1167 {"list2str", 1, 2, FEARG_1, NULL,
1168 ret_string, f_list2str},
1169 {"listener_add", 1, 2, FEARG_2, NULL,
1170 ret_number, f_listener_add},
1171 {"listener_flush", 0, 1, FEARG_1, NULL,
1172 ret_void, f_listener_flush},
1173 {"listener_remove", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001174 ret_number_bool, f_listener_remove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001175 {"localtime", 0, 0, 0, NULL,
1176 ret_number, f_localtime},
1177 {"log", 1, 1, FEARG_1, NULL,
1178 ret_float, FLOAT_FUNC(f_log)},
1179 {"log10", 1, 1, FEARG_1, NULL,
1180 ret_float, FLOAT_FUNC(f_log10)},
1181 {"luaeval", 1, 2, FEARG_1, NULL,
1182 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001183#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001184 f_luaeval
1185#else
1186 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001187#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001188 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001189 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001190 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001191 {"maparg", 1, 4, FEARG_1, NULL,
1192 ret_maparg, f_maparg},
1193 {"mapcheck", 1, 3, FEARG_1, NULL,
1194 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001195 {"mapnew", 2, 2, FEARG_1, NULL,
1196 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001197 {"mapset", 3, 3, FEARG_1, NULL,
1198 ret_void, f_mapset},
1199 {"match", 2, 4, FEARG_1, NULL,
1200 ret_any, f_match},
1201 {"matchadd", 2, 5, FEARG_1, NULL,
1202 ret_number, f_matchadd},
1203 {"matchaddpos", 2, 5, FEARG_1, NULL,
1204 ret_number, f_matchaddpos},
1205 {"matcharg", 1, 1, FEARG_1, NULL,
1206 ret_list_string, f_matcharg},
1207 {"matchdelete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001208 ret_number_bool, f_matchdelete},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001209 {"matchend", 2, 4, FEARG_1, NULL,
1210 ret_number, f_matchend},
1211 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1212 ret_list_string, f_matchfuzzy},
1213 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1214 ret_list_any, f_matchfuzzypos},
1215 {"matchlist", 2, 4, FEARG_1, NULL,
1216 ret_list_string, f_matchlist},
1217 {"matchstr", 2, 4, FEARG_1, NULL,
1218 ret_string, f_matchstr},
1219 {"matchstrpos", 2, 4, FEARG_1, NULL,
1220 ret_list_any, f_matchstrpos},
1221 {"max", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001222 ret_number, f_max},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001223 {"menu_info", 1, 2, FEARG_1, NULL,
1224 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001225#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001226 f_menu_info
1227#else
1228 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001229#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001230 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001231 {"min", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001232 ret_number, f_min},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001233 {"mkdir", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001234 ret_number_bool, f_mkdir},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001235 {"mode", 0, 1, FEARG_1, NULL,
1236 ret_string, f_mode},
1237 {"mzeval", 1, 1, FEARG_1, NULL,
1238 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001239#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001240 f_mzeval
1241#else
1242 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001243#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001244 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001245 {"nextnonblank", 1, 1, FEARG_1, NULL,
1246 ret_number, f_nextnonblank},
1247 {"nr2char", 1, 2, FEARG_1, NULL,
1248 ret_string, f_nr2char},
1249 {"or", 2, 2, FEARG_1, NULL,
1250 ret_number, f_or},
1251 {"pathshorten", 1, 2, FEARG_1, NULL,
1252 ret_string, f_pathshorten},
1253 {"perleval", 1, 1, FEARG_1, NULL,
1254 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001255#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001256 f_perleval
1257#else
1258 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001259#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001260 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001261 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1262 ret_number, PROP_FUNC(f_popup_atcursor)},
1263 {"popup_beval", 2, 2, FEARG_1, NULL,
1264 ret_number, PROP_FUNC(f_popup_beval)},
1265 {"popup_clear", 0, 1, 0, NULL,
1266 ret_void, PROP_FUNC(f_popup_clear)},
1267 {"popup_close", 1, 2, FEARG_1, NULL,
1268 ret_void, PROP_FUNC(f_popup_close)},
1269 {"popup_create", 2, 2, FEARG_1, NULL,
1270 ret_number, PROP_FUNC(f_popup_create)},
1271 {"popup_dialog", 2, 2, FEARG_1, NULL,
1272 ret_number, PROP_FUNC(f_popup_dialog)},
1273 {"popup_filter_menu", 2, 2, 0, NULL,
1274 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1275 {"popup_filter_yesno", 2, 2, 0, NULL,
1276 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1277 {"popup_findinfo", 0, 0, 0, NULL,
1278 ret_number, PROP_FUNC(f_popup_findinfo)},
1279 {"popup_findpreview", 0, 0, 0, NULL,
1280 ret_number, PROP_FUNC(f_popup_findpreview)},
1281 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1282 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1283 {"popup_getpos", 1, 1, FEARG_1, NULL,
1284 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1285 {"popup_hide", 1, 1, FEARG_1, NULL,
1286 ret_void, PROP_FUNC(f_popup_hide)},
1287 {"popup_list", 0, 0, 0, NULL,
1288 ret_list_number, PROP_FUNC(f_popup_list)},
1289 {"popup_locate", 2, 2, 0, NULL,
1290 ret_number, PROP_FUNC(f_popup_locate)},
1291 {"popup_menu", 2, 2, FEARG_1, NULL,
1292 ret_number, PROP_FUNC(f_popup_menu)},
1293 {"popup_move", 2, 2, FEARG_1, NULL,
1294 ret_void, PROP_FUNC(f_popup_move)},
1295 {"popup_notification", 2, 2, FEARG_1, NULL,
1296 ret_number, PROP_FUNC(f_popup_notification)},
1297 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1298 ret_void, PROP_FUNC(f_popup_setoptions)},
1299 {"popup_settext", 2, 2, FEARG_1, NULL,
1300 ret_void, PROP_FUNC(f_popup_settext)},
1301 {"popup_show", 1, 1, FEARG_1, NULL,
1302 ret_void, PROP_FUNC(f_popup_show)},
1303 {"pow", 2, 2, FEARG_1, NULL,
1304 ret_float, FLOAT_FUNC(f_pow)},
1305 {"prevnonblank", 1, 1, FEARG_1, NULL,
1306 ret_number, f_prevnonblank},
1307 {"printf", 1, 19, FEARG_2, NULL,
1308 ret_string, f_printf},
1309 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1310 ret_string, JOB_FUNC(f_prompt_getprompt)},
1311 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1312 ret_void, JOB_FUNC(f_prompt_setcallback)},
1313 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1314 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1315 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1316 ret_void, JOB_FUNC(f_prompt_setprompt)},
1317 {"prop_add", 3, 3, FEARG_1, NULL,
1318 ret_void, PROP_FUNC(f_prop_add)},
1319 {"prop_clear", 1, 3, FEARG_1, NULL,
1320 ret_void, PROP_FUNC(f_prop_clear)},
1321 {"prop_find", 1, 2, FEARG_1, NULL,
1322 ret_dict_any, PROP_FUNC(f_prop_find)},
1323 {"prop_list", 1, 2, FEARG_1, NULL,
1324 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1325 {"prop_remove", 1, 3, FEARG_1, NULL,
1326 ret_number, PROP_FUNC(f_prop_remove)},
1327 {"prop_type_add", 2, 2, FEARG_1, NULL,
1328 ret_void, PROP_FUNC(f_prop_type_add)},
1329 {"prop_type_change", 2, 2, FEARG_1, NULL,
1330 ret_void, PROP_FUNC(f_prop_type_change)},
1331 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1332 ret_void, PROP_FUNC(f_prop_type_delete)},
1333 {"prop_type_get", 1, 2, FEARG_1, NULL,
1334 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1335 {"prop_type_list", 0, 1, FEARG_1, NULL,
1336 ret_list_string, PROP_FUNC(f_prop_type_list)},
1337 {"pum_getpos", 0, 0, 0, NULL,
1338 ret_dict_number, f_pum_getpos},
1339 {"pumvisible", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001340 ret_number_bool, f_pumvisible},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001341 {"py3eval", 1, 1, FEARG_1, NULL,
1342 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001343#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001344 f_py3eval
1345#else
1346 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001347#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001348 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001349 {"pyeval", 1, 1, FEARG_1, NULL,
1350 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001351#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001352 f_pyeval
1353#else
1354 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001355#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001356 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001357 {"pyxeval", 1, 1, FEARG_1, NULL,
1358 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001359#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001360 f_pyxeval
1361#else
1362 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001363#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001364 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001365 {"rand", 0, 1, FEARG_1, NULL,
1366 ret_number, f_rand},
1367 {"range", 1, 3, FEARG_1, NULL,
1368 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001369 {"readblob", 1, 1, FEARG_1, NULL,
1370 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001371 {"readdir", 1, 3, FEARG_1, NULL,
1372 ret_list_string, f_readdir},
1373 {"readdirex", 1, 3, FEARG_1, NULL,
1374 ret_list_dict_any, f_readdirex},
1375 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001376 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001377 {"reduce", 2, 3, FEARG_1, NULL,
1378 ret_any, f_reduce},
1379 {"reg_executing", 0, 0, 0, NULL,
1380 ret_string, f_reg_executing},
1381 {"reg_recording", 0, 0, 0, NULL,
1382 ret_string, f_reg_recording},
1383 {"reltime", 0, 2, FEARG_1, NULL,
1384 ret_list_any, f_reltime},
1385 {"reltimefloat", 1, 1, FEARG_1, NULL,
1386 ret_float, FLOAT_FUNC(f_reltimefloat)},
1387 {"reltimestr", 1, 1, FEARG_1, NULL,
1388 ret_string, f_reltimestr},
1389 {"remote_expr", 2, 4, FEARG_1, NULL,
1390 ret_string, f_remote_expr},
1391 {"remote_foreground", 1, 1, FEARG_1, NULL,
1392 ret_string, f_remote_foreground},
1393 {"remote_peek", 1, 2, FEARG_1, NULL,
1394 ret_number, f_remote_peek},
1395 {"remote_read", 1, 2, FEARG_1, NULL,
1396 ret_string, f_remote_read},
1397 {"remote_send", 2, 3, FEARG_1, NULL,
1398 ret_string, f_remote_send},
1399 {"remote_startserver", 1, 1, FEARG_1, NULL,
1400 ret_void, f_remote_startserver},
1401 {"remove", 2, 3, FEARG_1, NULL,
1402 ret_remove, f_remove},
1403 {"rename", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001404 ret_number_bool, f_rename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001405 {"repeat", 2, 2, FEARG_1, NULL,
1406 ret_first_arg, f_repeat},
1407 {"resolve", 1, 1, FEARG_1, NULL,
1408 ret_string, f_resolve},
1409 {"reverse", 1, 1, FEARG_1, NULL,
1410 ret_first_arg, f_reverse},
1411 {"round", 1, 1, FEARG_1, NULL,
1412 ret_float, FLOAT_FUNC(f_round)},
1413 {"rubyeval", 1, 1, FEARG_1, NULL,
1414 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001415#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001416 f_rubyeval
1417#else
1418 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001419#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001420 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001421 {"screenattr", 2, 2, FEARG_1, NULL,
1422 ret_number, f_screenattr},
1423 {"screenchar", 2, 2, FEARG_1, NULL,
1424 ret_number, f_screenchar},
1425 {"screenchars", 2, 2, FEARG_1, NULL,
1426 ret_list_number, f_screenchars},
1427 {"screencol", 0, 0, 0, NULL,
1428 ret_number, f_screencol},
1429 {"screenpos", 3, 3, FEARG_1, NULL,
1430 ret_dict_number, f_screenpos},
1431 {"screenrow", 0, 0, 0, NULL,
1432 ret_number, f_screenrow},
1433 {"screenstring", 2, 2, FEARG_1, NULL,
1434 ret_string, f_screenstring},
1435 {"search", 1, 5, FEARG_1, NULL,
1436 ret_number, f_search},
1437 {"searchcount", 0, 1, FEARG_1, NULL,
1438 ret_dict_any, f_searchcount},
1439 {"searchdecl", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001440 ret_number_bool, f_searchdecl},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001441 {"searchpair", 3, 7, 0, NULL,
1442 ret_number, f_searchpair},
1443 {"searchpairpos", 3, 7, 0, NULL,
1444 ret_list_number, f_searchpairpos},
1445 {"searchpos", 1, 5, FEARG_1, NULL,
1446 ret_list_number, f_searchpos},
1447 {"server2client", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001448 ret_number_bool, f_server2client},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001449 {"serverlist", 0, 0, 0, NULL,
1450 ret_string, f_serverlist},
1451 {"setbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001452 ret_number_bool, f_setbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001453 {"setbufvar", 3, 3, FEARG_3, NULL,
1454 ret_void, f_setbufvar},
1455 {"setcellwidths", 1, 1, FEARG_1, NULL,
1456 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001457 {"setcharpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001458 ret_number_bool, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001459 {"setcharsearch", 1, 1, FEARG_1, NULL,
1460 ret_void, f_setcharsearch},
1461 {"setcmdpos", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001462 ret_number_bool, f_setcmdpos},
1463 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1464 ret_number_bool, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001465 {"setenv", 2, 2, FEARG_2, NULL,
1466 ret_void, f_setenv},
1467 {"setfperm", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001468 ret_number_bool, f_setfperm},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001469 {"setline", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001470 ret_number_bool, f_setline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001471 {"setloclist", 2, 4, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001472 ret_number_bool, f_setloclist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001473 {"setmatches", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001474 ret_number_bool, f_setmatches},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001475 {"setpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001476 ret_number_bool, f_setpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001477 {"setqflist", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001478 ret_number_bool, f_setqflist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001479 {"setreg", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001480 ret_number_bool, f_setreg},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001481 {"settabvar", 3, 3, FEARG_3, NULL,
1482 ret_void, f_settabvar},
1483 {"settabwinvar", 4, 4, FEARG_4, NULL,
1484 ret_void, f_settabwinvar},
1485 {"settagstack", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001486 ret_number_bool, f_settagstack},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001487 {"setwinvar", 3, 3, FEARG_3, NULL,
1488 ret_void, f_setwinvar},
1489 {"sha256", 1, 1, FEARG_1, NULL,
1490 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001491#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001492 f_sha256
1493#else
1494 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001495#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001496 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001497 {"shellescape", 1, 2, FEARG_1, NULL,
1498 ret_string, f_shellescape},
1499 {"shiftwidth", 0, 1, FEARG_1, NULL,
1500 ret_number, f_shiftwidth},
1501 {"sign_define", 1, 2, FEARG_1, NULL,
1502 ret_any, SIGN_FUNC(f_sign_define)},
1503 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1504 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1505 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1506 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1507 {"sign_jump", 3, 3, FEARG_1, NULL,
1508 ret_number, SIGN_FUNC(f_sign_jump)},
1509 {"sign_place", 4, 5, FEARG_1, NULL,
1510 ret_number, SIGN_FUNC(f_sign_place)},
1511 {"sign_placelist", 1, 1, FEARG_1, NULL,
1512 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1513 {"sign_undefine", 0, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001514 ret_number_bool, SIGN_FUNC(f_sign_undefine)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001515 {"sign_unplace", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001516 ret_number_bool, SIGN_FUNC(f_sign_unplace)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001517 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1518 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1519 {"simplify", 1, 1, FEARG_1, NULL,
1520 ret_string, f_simplify},
1521 {"sin", 1, 1, FEARG_1, NULL,
1522 ret_float, FLOAT_FUNC(f_sin)},
1523 {"sinh", 1, 1, FEARG_1, NULL,
1524 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001525 {"slice", 2, 3, FEARG_1, NULL,
1526 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001527 {"sort", 1, 3, FEARG_1, NULL,
1528 ret_first_arg, f_sort},
1529 {"sound_clear", 0, 0, 0, NULL,
1530 ret_void, SOUND_FUNC(f_sound_clear)},
1531 {"sound_playevent", 1, 2, FEARG_1, NULL,
1532 ret_number, SOUND_FUNC(f_sound_playevent)},
1533 {"sound_playfile", 1, 2, FEARG_1, NULL,
1534 ret_number, SOUND_FUNC(f_sound_playfile)},
1535 {"sound_stop", 1, 1, FEARG_1, NULL,
1536 ret_void, SOUND_FUNC(f_sound_stop)},
1537 {"soundfold", 1, 1, FEARG_1, NULL,
1538 ret_string, f_soundfold},
1539 {"spellbadword", 0, 1, FEARG_1, NULL,
1540 ret_list_string, f_spellbadword},
1541 {"spellsuggest", 1, 3, FEARG_1, NULL,
1542 ret_list_string, f_spellsuggest},
1543 {"split", 1, 3, FEARG_1, NULL,
1544 ret_list_string, f_split},
1545 {"sqrt", 1, 1, FEARG_1, NULL,
1546 ret_float, FLOAT_FUNC(f_sqrt)},
1547 {"srand", 0, 1, FEARG_1, NULL,
1548 ret_list_number, f_srand},
1549 {"state", 0, 1, FEARG_1, NULL,
1550 ret_string, f_state},
1551 {"str2float", 1, 1, FEARG_1, NULL,
1552 ret_float, FLOAT_FUNC(f_str2float)},
1553 {"str2list", 1, 2, FEARG_1, NULL,
1554 ret_list_number, f_str2list},
1555 {"str2nr", 1, 3, FEARG_1, NULL,
1556 ret_number, f_str2nr},
1557 {"strcharpart", 2, 3, FEARG_1, NULL,
1558 ret_string, f_strcharpart},
1559 {"strchars", 1, 2, FEARG_1, NULL,
1560 ret_number, f_strchars},
1561 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1562 ret_number, f_strdisplaywidth},
1563 {"strftime", 1, 2, FEARG_1, NULL,
1564 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001565#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001566 f_strftime
1567#else
1568 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001569#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001570 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001571 {"strgetchar", 2, 2, FEARG_1, NULL,
1572 ret_number, f_strgetchar},
1573 {"stridx", 2, 3, FEARG_1, NULL,
1574 ret_number, f_stridx},
1575 {"string", 1, 1, FEARG_1, NULL,
1576 ret_string, f_string},
1577 {"strlen", 1, 1, FEARG_1, NULL,
1578 ret_number, f_strlen},
1579 {"strpart", 2, 4, FEARG_1, NULL,
1580 ret_string, f_strpart},
1581 {"strptime", 2, 2, FEARG_1, NULL,
1582 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001583#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001584 f_strptime
1585#else
1586 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001587#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001588 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001589 {"strridx", 2, 3, FEARG_1, NULL,
1590 ret_number, f_strridx},
1591 {"strtrans", 1, 1, FEARG_1, NULL,
1592 ret_string, f_strtrans},
1593 {"strwidth", 1, 1, FEARG_1, NULL,
1594 ret_number, f_strwidth},
1595 {"submatch", 1, 2, FEARG_1, NULL,
1596 ret_string, f_submatch},
1597 {"substitute", 4, 4, FEARG_1, NULL,
1598 ret_string, f_substitute},
1599 {"swapinfo", 1, 1, FEARG_1, NULL,
1600 ret_dict_any, f_swapinfo},
1601 {"swapname", 1, 1, FEARG_1, NULL,
1602 ret_string, f_swapname},
1603 {"synID", 3, 3, 0, NULL,
1604 ret_number, f_synID},
1605 {"synIDattr", 2, 3, FEARG_1, NULL,
1606 ret_string, f_synIDattr},
1607 {"synIDtrans", 1, 1, FEARG_1, NULL,
1608 ret_number, f_synIDtrans},
1609 {"synconcealed", 2, 2, 0, NULL,
1610 ret_list_any, f_synconcealed},
1611 {"synstack", 2, 2, 0, NULL,
1612 ret_list_number, f_synstack},
1613 {"system", 1, 2, FEARG_1, NULL,
1614 ret_string, f_system},
1615 {"systemlist", 1, 2, FEARG_1, NULL,
1616 ret_list_string, f_systemlist},
1617 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1618 ret_list_number, f_tabpagebuflist},
1619 {"tabpagenr", 0, 1, 0, NULL,
1620 ret_number, f_tabpagenr},
1621 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1622 ret_number, f_tabpagewinnr},
1623 {"tagfiles", 0, 0, 0, NULL,
1624 ret_list_string, f_tagfiles},
1625 {"taglist", 1, 2, FEARG_1, NULL,
1626 ret_list_dict_any, f_taglist},
1627 {"tan", 1, 1, FEARG_1, NULL,
1628 ret_float, FLOAT_FUNC(f_tan)},
1629 {"tanh", 1, 1, FEARG_1, NULL,
1630 ret_float, FLOAT_FUNC(f_tanh)},
1631 {"tempname", 0, 0, 0, NULL,
1632 ret_string, f_tempname},
1633 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1634 ret_number, TERM_FUNC(f_term_dumpdiff)},
1635 {"term_dumpload", 1, 2, FEARG_1, NULL,
1636 ret_number, TERM_FUNC(f_term_dumpload)},
1637 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1638 ret_void, TERM_FUNC(f_term_dumpwrite)},
1639 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1640 ret_number, TERM_FUNC(f_term_getaltscreen)},
1641 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1642 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001643#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001644 f_term_getansicolors
1645#else
1646 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001647#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001648 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001649 {"term_getattr", 2, 2, FEARG_1, NULL,
1650 ret_number, TERM_FUNC(f_term_getattr)},
1651 {"term_getcursor", 1, 1, FEARG_1, NULL,
1652 ret_list_any, TERM_FUNC(f_term_getcursor)},
1653 {"term_getjob", 1, 1, FEARG_1, NULL,
1654 ret_job, TERM_FUNC(f_term_getjob)},
1655 {"term_getline", 2, 2, FEARG_1, NULL,
1656 ret_string, TERM_FUNC(f_term_getline)},
1657 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1658 ret_number, TERM_FUNC(f_term_getscrolled)},
1659 {"term_getsize", 1, 1, FEARG_1, NULL,
1660 ret_list_number, TERM_FUNC(f_term_getsize)},
1661 {"term_getstatus", 1, 1, FEARG_1, NULL,
1662 ret_string, TERM_FUNC(f_term_getstatus)},
1663 {"term_gettitle", 1, 1, FEARG_1, NULL,
1664 ret_string, TERM_FUNC(f_term_gettitle)},
1665 {"term_gettty", 1, 2, FEARG_1, NULL,
1666 ret_string, TERM_FUNC(f_term_gettty)},
1667 {"term_list", 0, 0, 0, NULL,
1668 ret_list_number, TERM_FUNC(f_term_list)},
1669 {"term_scrape", 2, 2, FEARG_1, NULL,
1670 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1671 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1672 ret_void, TERM_FUNC(f_term_sendkeys)},
1673 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1674 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001675#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001676 f_term_setansicolors
1677#else
1678 NULL
1679#endif
1680 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001681 {"term_setapi", 2, 2, FEARG_1, NULL,
1682 ret_void, TERM_FUNC(f_term_setapi)},
1683 {"term_setkill", 2, 2, FEARG_1, NULL,
1684 ret_void, TERM_FUNC(f_term_setkill)},
1685 {"term_setrestore", 2, 2, FEARG_1, NULL,
1686 ret_void, TERM_FUNC(f_term_setrestore)},
1687 {"term_setsize", 3, 3, FEARG_1, NULL,
1688 ret_void, TERM_FUNC(f_term_setsize)},
1689 {"term_start", 1, 2, FEARG_1, NULL,
1690 ret_number, TERM_FUNC(f_term_start)},
1691 {"term_wait", 1, 2, FEARG_1, NULL,
1692 ret_void, TERM_FUNC(f_term_wait)},
1693 {"terminalprops", 0, 0, 0, NULL,
1694 ret_dict_string, f_terminalprops},
1695 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1696 ret_void, f_test_alloc_fail},
1697 {"test_autochdir", 0, 0, 0, NULL,
1698 ret_void, f_test_autochdir},
1699 {"test_feedinput", 1, 1, FEARG_1, NULL,
1700 ret_void, f_test_feedinput},
1701 {"test_garbagecollect_now", 0, 0, 0, NULL,
1702 ret_void, f_test_garbagecollect_now},
1703 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1704 ret_void, f_test_garbagecollect_soon},
1705 {"test_getvalue", 1, 1, FEARG_1, NULL,
1706 ret_number, f_test_getvalue},
1707 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1708 ret_void, f_test_ignore_error},
1709 {"test_null_blob", 0, 0, 0, NULL,
1710 ret_blob, f_test_null_blob},
1711 {"test_null_channel", 0, 0, 0, NULL,
1712 ret_channel, JOB_FUNC(f_test_null_channel)},
1713 {"test_null_dict", 0, 0, 0, NULL,
1714 ret_dict_any, f_test_null_dict},
1715 {"test_null_function", 0, 0, 0, NULL,
1716 ret_func_any, f_test_null_function},
1717 {"test_null_job", 0, 0, 0, NULL,
1718 ret_job, JOB_FUNC(f_test_null_job)},
1719 {"test_null_list", 0, 0, 0, NULL,
1720 ret_list_any, f_test_null_list},
1721 {"test_null_partial", 0, 0, 0, NULL,
1722 ret_func_any, f_test_null_partial},
1723 {"test_null_string", 0, 0, 0, NULL,
1724 ret_string, f_test_null_string},
1725 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1726 ret_void, f_test_option_not_set},
1727 {"test_override", 2, 2, FEARG_2, NULL,
1728 ret_void, f_test_override},
1729 {"test_refcount", 1, 1, FEARG_1, NULL,
1730 ret_number, f_test_refcount},
1731 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1732 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001733#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001734 f_test_scrollbar
1735#else
1736 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001737#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001738 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001739 {"test_setmouse", 2, 2, 0, NULL,
1740 ret_void, f_test_setmouse},
1741 {"test_settime", 1, 1, FEARG_1, NULL,
1742 ret_void, f_test_settime},
1743 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1744 ret_void, f_test_srand_seed},
1745 {"test_unknown", 0, 0, 0, NULL,
1746 ret_any, f_test_unknown},
1747 {"test_void", 0, 0, 0, NULL,
1748 ret_void, f_test_void},
1749 {"timer_info", 0, 1, FEARG_1, NULL,
1750 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1751 {"timer_pause", 2, 2, FEARG_1, NULL,
1752 ret_void, TIMER_FUNC(f_timer_pause)},
1753 {"timer_start", 2, 3, FEARG_1, NULL,
1754 ret_number, TIMER_FUNC(f_timer_start)},
1755 {"timer_stop", 1, 1, FEARG_1, NULL,
1756 ret_void, TIMER_FUNC(f_timer_stop)},
1757 {"timer_stopall", 0, 0, 0, NULL,
1758 ret_void, TIMER_FUNC(f_timer_stopall)},
1759 {"tolower", 1, 1, FEARG_1, NULL,
1760 ret_string, f_tolower},
1761 {"toupper", 1, 1, FEARG_1, NULL,
1762 ret_string, f_toupper},
1763 {"tr", 3, 3, FEARG_1, NULL,
1764 ret_string, f_tr},
1765 {"trim", 1, 3, FEARG_1, NULL,
1766 ret_string, f_trim},
1767 {"trunc", 1, 1, FEARG_1, NULL,
1768 ret_float, FLOAT_FUNC(f_trunc)},
1769 {"type", 1, 1, FEARG_1, NULL,
1770 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001771 {"typename", 1, 1, FEARG_1, NULL,
1772 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001773 {"undofile", 1, 1, FEARG_1, NULL,
1774 ret_string, f_undofile},
1775 {"undotree", 0, 0, 0, NULL,
1776 ret_dict_any, f_undotree},
1777 {"uniq", 1, 3, FEARG_1, NULL,
1778 ret_list_any, f_uniq},
1779 {"values", 1, 1, FEARG_1, NULL,
1780 ret_list_any, f_values},
1781 {"virtcol", 1, 1, FEARG_1, NULL,
1782 ret_number, f_virtcol},
1783 {"visualmode", 0, 1, 0, NULL,
1784 ret_string, f_visualmode},
1785 {"wildmenumode", 0, 0, 0, NULL,
1786 ret_number, f_wildmenumode},
1787 {"win_execute", 2, 3, FEARG_2, NULL,
1788 ret_string, f_win_execute},
1789 {"win_findbuf", 1, 1, FEARG_1, NULL,
1790 ret_list_number, f_win_findbuf},
1791 {"win_getid", 0, 2, FEARG_1, NULL,
1792 ret_number, f_win_getid},
1793 {"win_gettype", 0, 1, FEARG_1, NULL,
1794 ret_string, f_win_gettype},
1795 {"win_gotoid", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001796 ret_number_bool, f_win_gotoid},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001797 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1798 ret_list_number, f_win_id2tabwin},
1799 {"win_id2win", 1, 1, FEARG_1, NULL,
1800 ret_number, f_win_id2win},
1801 {"win_screenpos", 1, 1, FEARG_1, NULL,
1802 ret_list_number, f_win_screenpos},
1803 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001804 ret_number_bool, f_win_splitmove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001805 {"winbufnr", 1, 1, FEARG_1, NULL,
1806 ret_number, f_winbufnr},
1807 {"wincol", 0, 0, 0, NULL,
1808 ret_number, f_wincol},
1809 {"windowsversion", 0, 0, 0, NULL,
1810 ret_string, f_windowsversion},
1811 {"winheight", 1, 1, FEARG_1, NULL,
1812 ret_number, f_winheight},
1813 {"winlayout", 0, 1, FEARG_1, NULL,
1814 ret_list_any, f_winlayout},
1815 {"winline", 0, 0, 0, NULL,
1816 ret_number, f_winline},
1817 {"winnr", 0, 1, FEARG_1, NULL,
1818 ret_number, f_winnr},
1819 {"winrestcmd", 0, 0, 0, NULL,
1820 ret_string, f_winrestcmd},
1821 {"winrestview", 1, 1, FEARG_1, NULL,
1822 ret_void, f_winrestview},
1823 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001824 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001825 {"winwidth", 1, 1, FEARG_1, NULL,
1826 ret_number, f_winwidth},
1827 {"wordcount", 0, 0, 0, NULL,
1828 ret_dict_number, f_wordcount},
1829 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001830 ret_number_bool, f_writefile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001831 {"xor", 2, 2, FEARG_1, NULL,
1832 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001833};
1834
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001835/*
1836 * Function given to ExpandGeneric() to obtain the list of internal
1837 * or user defined function names.
1838 */
1839 char_u *
1840get_function_name(expand_T *xp, int idx)
1841{
1842 static int intidx = -1;
1843 char_u *name;
1844
1845 if (idx == 0)
1846 intidx = -1;
1847 if (intidx < 0)
1848 {
1849 name = get_user_func_name(xp, idx);
1850 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001851 {
1852 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1853 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001854 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001855 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001856 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001857 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001858 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001859 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001860 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001861 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001862 STRCAT(IObuff, ")");
1863 return IObuff;
1864 }
1865
1866 return NULL;
1867}
1868
1869/*
1870 * Function given to ExpandGeneric() to obtain the list of internal or
1871 * user defined variable or function names.
1872 */
1873 char_u *
1874get_expr_name(expand_T *xp, int idx)
1875{
1876 static int intidx = -1;
1877 char_u *name;
1878
1879 if (idx == 0)
1880 intidx = -1;
1881 if (intidx < 0)
1882 {
1883 name = get_function_name(xp, idx);
1884 if (name != NULL)
1885 return name;
1886 }
1887 return get_user_var_name(xp, ++intidx);
1888}
1889
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001890/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001891 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001892 * Return index, or -1 if not found or "implemented" is TRUE and the function
1893 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001894 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001895 static int
1896find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001897{
1898 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001899 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001900 int cmp;
1901 int x;
1902
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001903 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001904
1905 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001906 while (first <= last)
1907 {
1908 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001909 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001910 if (cmp < 0)
1911 last = x - 1;
1912 else if (cmp > 0)
1913 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001914 else if (implemented && global_functions[x].f_func == NULL)
1915 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001916 else
1917 return x;
1918 }
1919 return -1;
1920}
1921
Bram Moolenaar15c47602020-03-26 22:16:48 +01001922/*
1923 * Find internal function "name" in table "global_functions".
1924 * Return index, or -1 if not found or the function is not implemented.
1925 */
1926 int
1927find_internal_func(char_u *name)
1928{
1929 return find_internal_func_opt(name, TRUE);
1930}
1931
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001932 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001933has_internal_func(char_u *name)
1934{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001935 return find_internal_func_opt(name, TRUE) >= 0;
1936}
1937
1938 static int
1939has_internal_func_name(char_u *name)
1940{
1941 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001942}
1943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 char *
1945internal_func_name(int idx)
1946{
1947 return global_functions[idx].f_name;
1948}
1949
Bram Moolenaar94738d82020-10-21 14:25:07 +02001950/*
1951 * Check the argument types for builting function "idx".
1952 * Uses the list of types on the type stack: "types".
1953 * Return FAIL and gives an error message when a type is wrong.
1954 */
1955 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001956internal_func_check_arg_types(
1957 type_T **types,
1958 int idx,
1959 int argcount,
1960 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001961{
1962 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1963 int i;
1964
1965 if (argchecks != NULL)
1966 {
1967 argcontext_T context;
1968
1969 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001970 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001971 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001972 for (i = 0; i < argcount; ++i)
1973 if (argchecks[i] != NULL)
1974 {
1975 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001976 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001977 return FAIL;
1978 }
1979 }
1980 return OK;
1981}
1982
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001983/*
1984 * Call the "f_retfunc" function to obtain the return type of function "idx".
1985 * "argtypes" is the list of argument types or NULL when there are no
1986 * arguments.
1987 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001989internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001991 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992}
1993
1994/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001995 * Return TRUE if "idx" is for the map() function.
1996 */
1997 int
1998internal_func_is_map(int idx)
1999{
2000 return global_functions[idx].f_func == f_map;
2001}
2002
2003/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002005 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2006 * first argument, 2 if method base is second argument, etc. 9 if method base
2007 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 */
2009 int
2010check_internal_func(int idx, int argcount)
2011{
2012 int res;
2013 char *name;
2014
2015 if (argcount < global_functions[idx].f_min_argc)
2016 res = FCERR_TOOFEW;
2017 else if (argcount > global_functions[idx].f_max_argc)
2018 res = FCERR_TOOMANY;
2019 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002020 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021
2022 name = internal_func_name(idx);
2023 if (res == FCERR_TOOMANY)
2024 semsg(_(e_toomanyarg), name);
2025 else
2026 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002027 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028}
2029
Bram Moolenaarac92e252019-08-03 21:58:38 +02002030 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002031call_internal_func(
2032 char_u *name,
2033 int argcount,
2034 typval_T *argvars,
2035 typval_T *rettv)
2036{
2037 int i;
2038
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002039 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002040 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002041 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002042 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002043 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002044 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002045 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002046 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002047 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002048 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002049}
2050
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051 void
2052call_internal_func_by_idx(
2053 int idx,
2054 typval_T *argvars,
2055 typval_T *rettv)
2056{
2057 global_functions[idx].f_func(argvars, rettv);
2058}
2059
Bram Moolenaarac92e252019-08-03 21:58:38 +02002060/*
2061 * Invoke a method for base->method().
2062 */
2063 int
2064call_internal_method(
2065 char_u *name,
2066 int argcount,
2067 typval_T *argvars,
2068 typval_T *rettv,
2069 typval_T *basetv)
2070{
2071 int i;
2072 int fi;
2073 typval_T argv[MAX_FUNC_ARGS + 1];
2074
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002075 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002076 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002077 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002078 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002079 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002080 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002081 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002082 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002083 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002084
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002085 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002086 {
2087 // base value goes last
2088 for (i = 0; i < argcount; ++i)
2089 argv[i] = argvars[i];
2090 argv[argcount] = *basetv;
2091 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002092 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002093 {
2094 // base value goes second
2095 argv[0] = argvars[0];
2096 argv[1] = *basetv;
2097 for (i = 1; i < argcount; ++i)
2098 argv[i + 1] = argvars[i];
2099 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002100 else if (global_functions[fi].f_argtype == FEARG_3)
2101 {
2102 // base value goes third
2103 argv[0] = argvars[0];
2104 argv[1] = argvars[1];
2105 argv[2] = *basetv;
2106 for (i = 2; i < argcount; ++i)
2107 argv[i + 1] = argvars[i];
2108 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002109 else if (global_functions[fi].f_argtype == FEARG_4)
2110 {
2111 // base value goes fourth
2112 argv[0] = argvars[0];
2113 argv[1] = argvars[1];
2114 argv[2] = argvars[2];
2115 argv[3] = *basetv;
2116 for (i = 3; i < argcount; ++i)
2117 argv[i + 1] = argvars[i];
2118 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002119 else
2120 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002121 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002122 argv[0] = *basetv;
2123 for (i = 0; i < argcount; ++i)
2124 argv[i + 1] = argvars[i];
2125 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002126 argv[argcount + 1].v_type = VAR_UNKNOWN;
2127
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002128 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002129 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002130}
2131
2132/*
2133 * Return TRUE for a non-zero Number and a non-empty String.
2134 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002135 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002136non_zero_arg(typval_T *argvars)
2137{
2138 return ((argvars[0].v_type == VAR_NUMBER
2139 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002140 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002141 && argvars[0].vval.v_number == VVAL_TRUE)
2142 || (argvars[0].v_type == VAR_STRING
2143 && argvars[0].vval.v_string != NULL
2144 && *argvars[0].vval.v_string != NUL));
2145}
2146
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002147#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002148/*
2149 * Get the float value of "argvars[0]" into "f".
2150 * Returns FAIL when the argument is not a Number or Float.
2151 */
2152 static int
2153get_float_arg(typval_T *argvars, float_T *f)
2154{
2155 if (argvars[0].v_type == VAR_FLOAT)
2156 {
2157 *f = argvars[0].vval.v_float;
2158 return OK;
2159 }
2160 if (argvars[0].v_type == VAR_NUMBER)
2161 {
2162 *f = (float_T)argvars[0].vval.v_number;
2163 return OK;
2164 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002165 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002166 return FAIL;
2167}
2168
2169/*
2170 * "abs(expr)" function
2171 */
2172 static void
2173f_abs(typval_T *argvars, typval_T *rettv)
2174{
2175 if (argvars[0].v_type == VAR_FLOAT)
2176 {
2177 rettv->v_type = VAR_FLOAT;
2178 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
2179 }
2180 else
2181 {
2182 varnumber_T n;
2183 int error = FALSE;
2184
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002185 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002186 if (error)
2187 rettv->vval.v_number = -1;
2188 else if (n > 0)
2189 rettv->vval.v_number = n;
2190 else
2191 rettv->vval.v_number = -n;
2192 }
2193}
2194
2195/*
2196 * "acos()" function
2197 */
2198 static void
2199f_acos(typval_T *argvars, typval_T *rettv)
2200{
2201 float_T f = 0.0;
2202
2203 rettv->v_type = VAR_FLOAT;
2204 if (get_float_arg(argvars, &f) == OK)
2205 rettv->vval.v_float = acos(f);
2206 else
2207 rettv->vval.v_float = 0.0;
2208}
2209#endif
2210
2211/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002212 * "and(expr, expr)" function
2213 */
2214 static void
2215f_and(typval_T *argvars, typval_T *rettv)
2216{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002217 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2218 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002219}
2220
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002221#ifdef FEAT_FLOAT
2222/*
2223 * "asin()" function
2224 */
2225 static void
2226f_asin(typval_T *argvars, typval_T *rettv)
2227{
2228 float_T f = 0.0;
2229
2230 rettv->v_type = VAR_FLOAT;
2231 if (get_float_arg(argvars, &f) == OK)
2232 rettv->vval.v_float = asin(f);
2233 else
2234 rettv->vval.v_float = 0.0;
2235}
2236
2237/*
2238 * "atan()" function
2239 */
2240 static void
2241f_atan(typval_T *argvars, typval_T *rettv)
2242{
2243 float_T f = 0.0;
2244
2245 rettv->v_type = VAR_FLOAT;
2246 if (get_float_arg(argvars, &f) == OK)
2247 rettv->vval.v_float = atan(f);
2248 else
2249 rettv->vval.v_float = 0.0;
2250}
2251
2252/*
2253 * "atan2()" function
2254 */
2255 static void
2256f_atan2(typval_T *argvars, typval_T *rettv)
2257{
2258 float_T fx = 0.0, fy = 0.0;
2259
2260 rettv->v_type = VAR_FLOAT;
2261 if (get_float_arg(argvars, &fx) == OK
2262 && get_float_arg(&argvars[1], &fy) == OK)
2263 rettv->vval.v_float = atan2(fx, fy);
2264 else
2265 rettv->vval.v_float = 0.0;
2266}
2267#endif
2268
2269/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002270 * "balloon_show()" function
2271 */
2272#ifdef FEAT_BEVAL
2273 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002274f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2275{
2276 rettv->v_type = VAR_STRING;
2277 if (balloonEval != NULL)
2278 {
2279 if (balloonEval->msg == NULL)
2280 rettv->vval.v_string = NULL;
2281 else
2282 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2283 }
2284}
2285
2286 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002287f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2288{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002289 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002290 {
2291 if (argvars[0].v_type == VAR_LIST
2292# ifdef FEAT_GUI
2293 && !gui.in_use
2294# endif
2295 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002296 {
2297 list_T *l = argvars[0].vval.v_list;
2298
2299 // empty list removes the balloon
2300 post_balloon(balloonEval, NULL,
2301 l == NULL || l->lv_len == 0 ? NULL : l);
2302 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002303 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002304 {
2305 char_u *mesg = tv_get_string_chk(&argvars[0]);
2306
2307 if (mesg != NULL)
2308 // empty string removes the balloon
2309 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2310 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002311 }
2312}
2313
Bram Moolenaar669a8282017-11-19 20:13:05 +01002314# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002315 static void
2316f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2317{
2318 if (rettv_list_alloc(rettv) == OK)
2319 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002320 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002321
2322 if (msg != NULL)
2323 {
2324 pumitem_T *array;
2325 int size = split_message(msg, &array);
2326 int i;
2327
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002328 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002329 for (i = 1; i < size - 1; ++i)
2330 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002331 while (size > 0)
2332 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002333 vim_free(array);
2334 }
2335 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002336}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002337# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002338#endif
2339
2340/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002341 * Get the buffer from "arg" and give an error and return NULL if it is not
2342 * valid.
2343 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002344 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002345get_buf_arg(typval_T *arg)
2346{
2347 buf_T *buf;
2348
2349 ++emsg_off;
2350 buf = tv_get_buf(arg, FALSE);
2351 --emsg_off;
2352 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002353 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002354 return buf;
2355}
2356
2357/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002358 * "byte2line(byte)" function
2359 */
2360 static void
2361f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2362{
2363#ifndef FEAT_BYTEOFF
2364 rettv->vval.v_number = -1;
2365#else
2366 long boff = 0;
2367
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002368 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002369 if (boff < 0)
2370 rettv->vval.v_number = -1;
2371 else
2372 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2373 (linenr_T)0, &boff);
2374#endif
2375}
2376
2377 static void
2378byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2379{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002380 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002381 char_u *str;
2382 varnumber_T idx;
2383
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002384 str = tv_get_string_chk(&argvars[0]);
2385 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002386 rettv->vval.v_number = -1;
2387 if (str == NULL || idx < 0)
2388 return;
2389
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002390 t = str;
2391 for ( ; idx > 0; idx--)
2392 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002393 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002394 return;
2395 if (enc_utf8 && comp)
2396 t += utf_ptr2len(t);
2397 else
2398 t += (*mb_ptr2len)(t);
2399 }
2400 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002401}
2402
2403/*
2404 * "byteidx()" function
2405 */
2406 static void
2407f_byteidx(typval_T *argvars, typval_T *rettv)
2408{
2409 byteidx(argvars, rettv, FALSE);
2410}
2411
2412/*
2413 * "byteidxcomp()" function
2414 */
2415 static void
2416f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2417{
2418 byteidx(argvars, rettv, TRUE);
2419}
2420
2421/*
2422 * "call(func, arglist [, dict])" function
2423 */
2424 static void
2425f_call(typval_T *argvars, typval_T *rettv)
2426{
2427 char_u *func;
2428 partial_T *partial = NULL;
2429 dict_T *selfdict = NULL;
2430
2431 if (argvars[1].v_type != VAR_LIST)
2432 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002433 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002434 return;
2435 }
2436 if (argvars[1].vval.v_list == NULL)
2437 return;
2438
2439 if (argvars[0].v_type == VAR_FUNC)
2440 func = argvars[0].vval.v_string;
2441 else if (argvars[0].v_type == VAR_PARTIAL)
2442 {
2443 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002444 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002445 }
2446 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002447 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002448 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002449 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002450
2451 if (argvars[2].v_type != VAR_UNKNOWN)
2452 {
2453 if (argvars[2].v_type != VAR_DICT)
2454 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002455 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002456 return;
2457 }
2458 selfdict = argvars[2].vval.v_dict;
2459 }
2460
2461 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2462}
2463
2464#ifdef FEAT_FLOAT
2465/*
2466 * "ceil({float})" function
2467 */
2468 static void
2469f_ceil(typval_T *argvars, typval_T *rettv)
2470{
2471 float_T f = 0.0;
2472
2473 rettv->v_type = VAR_FLOAT;
2474 if (get_float_arg(argvars, &f) == OK)
2475 rettv->vval.v_float = ceil(f);
2476 else
2477 rettv->vval.v_float = 0.0;
2478}
2479#endif
2480
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002481/*
2482 * "changenr()" function
2483 */
2484 static void
2485f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2486{
2487 rettv->vval.v_number = curbuf->b_u_seq_cur;
2488}
2489
2490/*
2491 * "char2nr(string)" function
2492 */
2493 static void
2494f_char2nr(typval_T *argvars, typval_T *rettv)
2495{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002496 if (has_mbyte)
2497 {
2498 int utf8 = 0;
2499
2500 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002501 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002502
2503 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002504 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002505 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002506 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002507 }
2508 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002509 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002510}
2511
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002512/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002513 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2514 * returns the character index of the column. Otherwise, returns the byte index
2515 * of the column.
2516 */
2517 static void
2518get_col(typval_T *argvars, typval_T *rettv, int charcol)
2519{
2520 colnr_T col = 0;
2521 pos_T *fp;
2522 int fnum = curbuf->b_fnum;
2523
2524 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2525 if (fp != NULL && fnum == curbuf->b_fnum)
2526 {
2527 if (fp->col == MAXCOL)
2528 {
2529 // '> can be MAXCOL, get the length of the line then
2530 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2531 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2532 else
2533 col = MAXCOL;
2534 }
2535 else
2536 {
2537 col = fp->col + 1;
2538 // col(".") when the cursor is on the NUL at the end of the line
2539 // because of "coladd" can be seen as an extra column.
2540 if (virtual_active() && fp == &curwin->w_cursor)
2541 {
2542 char_u *p = ml_get_cursor();
2543
2544 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2545 curwin->w_virtcol - curwin->w_cursor.coladd))
2546 {
2547 int l;
2548
2549 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2550 col += l;
2551 }
2552 }
2553 }
2554 }
2555 rettv->vval.v_number = col;
2556}
2557
2558/*
2559 * "charcol()" function
2560 */
2561 static void
2562f_charcol(typval_T *argvars, typval_T *rettv)
2563{
2564 get_col(argvars, rettv, TRUE);
2565}
2566
2567/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002568 * "charidx()" function
2569 */
2570 static void
2571f_charidx(typval_T *argvars, typval_T *rettv)
2572{
2573 char_u *str;
2574 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002575 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002576 char_u *p;
2577 int len;
2578 int (*ptr2len)(char_u *);
2579
2580 rettv->vval.v_number = -1;
2581
2582 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2583 || (argvars[2].v_type != VAR_UNKNOWN
2584 && argvars[2].v_type != VAR_NUMBER))
2585 {
2586 emsg(_(e_invarg));
2587 return;
2588 }
2589
2590 str = tv_get_string_chk(&argvars[0]);
2591 idx = tv_get_number_chk(&argvars[1], NULL);
2592 if (str == NULL || idx < 0)
2593 return;
2594
2595 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002596 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002597 if (countcc < 0 || countcc > 1)
2598 {
2599 semsg(_(e_using_number_as_bool_nr), countcc);
2600 return;
2601 }
2602
2603 if (enc_utf8 && countcc)
2604 ptr2len = utf_ptr2len;
2605 else
2606 ptr2len = mb_ptr2len;
2607
2608 for (p = str, len = 0; p <= str + idx; len++)
2609 {
2610 if (*p == NUL)
2611 return;
2612 p += ptr2len(p);
2613 }
2614
2615 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2616}
2617
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002618 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002619get_optional_window(typval_T *argvars, int idx)
2620{
2621 win_T *win = curwin;
2622
2623 if (argvars[idx].v_type != VAR_UNKNOWN)
2624 {
2625 win = find_win_by_nr_or_id(&argvars[idx]);
2626 if (win == NULL)
2627 {
2628 emsg(_(e_invalwindow));
2629 return NULL;
2630 }
2631 }
2632 return win;
2633}
2634
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002635/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002636 * "col(string)" function
2637 */
2638 static void
2639f_col(typval_T *argvars, typval_T *rettv)
2640{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002641 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002642}
2643
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002644/*
2645 * "confirm(message, buttons[, default [, type]])" function
2646 */
2647 static void
2648f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2649{
2650#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2651 char_u *message;
2652 char_u *buttons = NULL;
2653 char_u buf[NUMBUFLEN];
2654 char_u buf2[NUMBUFLEN];
2655 int def = 1;
2656 int type = VIM_GENERIC;
2657 char_u *typestr;
2658 int error = FALSE;
2659
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002660 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002661 if (message == NULL)
2662 error = TRUE;
2663 if (argvars[1].v_type != VAR_UNKNOWN)
2664 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002665 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002666 if (buttons == NULL)
2667 error = TRUE;
2668 if (argvars[2].v_type != VAR_UNKNOWN)
2669 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002670 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002671 if (argvars[3].v_type != VAR_UNKNOWN)
2672 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002673 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002674 if (typestr == NULL)
2675 error = TRUE;
2676 else
2677 {
2678 switch (TOUPPER_ASC(*typestr))
2679 {
2680 case 'E': type = VIM_ERROR; break;
2681 case 'Q': type = VIM_QUESTION; break;
2682 case 'I': type = VIM_INFO; break;
2683 case 'W': type = VIM_WARNING; break;
2684 case 'G': type = VIM_GENERIC; break;
2685 }
2686 }
2687 }
2688 }
2689 }
2690
2691 if (buttons == NULL || *buttons == NUL)
2692 buttons = (char_u *)_("&Ok");
2693
2694 if (!error)
2695 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2696 def, NULL, FALSE);
2697#endif
2698}
2699
2700/*
2701 * "copy()" function
2702 */
2703 static void
2704f_copy(typval_T *argvars, typval_T *rettv)
2705{
2706 item_copy(&argvars[0], rettv, FALSE, 0);
2707}
2708
2709#ifdef FEAT_FLOAT
2710/*
2711 * "cos()" function
2712 */
2713 static void
2714f_cos(typval_T *argvars, typval_T *rettv)
2715{
2716 float_T f = 0.0;
2717
2718 rettv->v_type = VAR_FLOAT;
2719 if (get_float_arg(argvars, &f) == OK)
2720 rettv->vval.v_float = cos(f);
2721 else
2722 rettv->vval.v_float = 0.0;
2723}
2724
2725/*
2726 * "cosh()" function
2727 */
2728 static void
2729f_cosh(typval_T *argvars, typval_T *rettv)
2730{
2731 float_T f = 0.0;
2732
2733 rettv->v_type = VAR_FLOAT;
2734 if (get_float_arg(argvars, &f) == OK)
2735 rettv->vval.v_float = cosh(f);
2736 else
2737 rettv->vval.v_float = 0.0;
2738}
2739#endif
2740
2741/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002742 * Set the cursor position.
2743 * If 'charcol' is TRUE, then use the column number as a character offet.
2744 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002745 */
2746 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002747set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002748{
2749 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002750 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002751 int set_curswant = TRUE;
2752
2753 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002754 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002755 {
2756 pos_T pos;
2757 colnr_T curswant = -1;
2758
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002759 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002760 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002761 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002762 return;
2763 }
2764 line = pos.lnum;
2765 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002766 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002767 if (curswant >= 0)
2768 {
2769 curwin->w_curswant = curswant - 1;
2770 set_curswant = FALSE;
2771 }
2772 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002773 else if ((argvars[0].v_type == VAR_NUMBER ||
2774 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002775 && (argvars[1].v_type == VAR_NUMBER ||
2776 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002777 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002778 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002779 if (line < 0)
2780 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002781 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002782 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002783 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002784 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002785 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002786 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002787 else
2788 {
2789 emsg(_(e_invarg));
2790 return;
2791 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002792 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002793 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002794 if (line > 0)
2795 curwin->w_cursor.lnum = line;
2796 if (col > 0)
2797 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002798 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002799
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002800 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002801 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002802 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002803 if (has_mbyte)
2804 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002805
2806 curwin->w_set_curswant = set_curswant;
2807 rettv->vval.v_number = 0;
2808}
2809
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002810/*
2811 * "cursor(lnum, col)" function, or
2812 * "cursor(list)"
2813 *
2814 * Moves the cursor to the specified line and column.
2815 * Returns 0 when the position could be set, -1 otherwise.
2816 */
2817 static void
2818f_cursor(typval_T *argvars, typval_T *rettv)
2819{
2820 set_cursorpos(argvars, rettv, FALSE);
2821}
2822
Bram Moolenaar4f974752019-02-17 17:44:42 +01002823#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002824/*
2825 * "debugbreak()" function
2826 */
2827 static void
2828f_debugbreak(typval_T *argvars, typval_T *rettv)
2829{
2830 int pid;
2831
2832 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002833 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002834 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002835 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002836 else
2837 {
2838 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2839
2840 if (hProcess != NULL)
2841 {
2842 DebugBreakProcess(hProcess);
2843 CloseHandle(hProcess);
2844 rettv->vval.v_number = OK;
2845 }
2846 }
2847}
2848#endif
2849
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002850/*
2851 * "deepcopy()" function
2852 */
2853 static void
2854f_deepcopy(typval_T *argvars, typval_T *rettv)
2855{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002856 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002857 int copyID;
2858
2859 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002860 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002861 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002862 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002863 else
2864 {
2865 copyID = get_copyID();
2866 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2867 }
2868}
2869
2870/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002871 * "did_filetype()" function
2872 */
2873 static void
2874f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2875{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002876 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002877}
2878
2879/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002880 * "echoraw({expr})" function
2881 */
2882 static void
2883f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2884{
2885 char_u *str = tv_get_string_chk(&argvars[0]);
2886
2887 if (str != NULL && *str != NUL)
2888 {
2889 out_str(str);
2890 out_flush();
2891 }
2892}
2893
2894/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002895 * "empty({expr})" function
2896 */
2897 static void
2898f_empty(typval_T *argvars, typval_T *rettv)
2899{
2900 int n = FALSE;
2901
2902 switch (argvars[0].v_type)
2903 {
2904 case VAR_STRING:
2905 case VAR_FUNC:
2906 n = argvars[0].vval.v_string == NULL
2907 || *argvars[0].vval.v_string == NUL;
2908 break;
2909 case VAR_PARTIAL:
2910 n = FALSE;
2911 break;
2912 case VAR_NUMBER:
2913 n = argvars[0].vval.v_number == 0;
2914 break;
2915 case VAR_FLOAT:
2916#ifdef FEAT_FLOAT
2917 n = argvars[0].vval.v_float == 0.0;
2918 break;
2919#endif
2920 case VAR_LIST:
2921 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002922 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002923 break;
2924 case VAR_DICT:
2925 n = argvars[0].vval.v_dict == NULL
2926 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2927 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002928 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002929 case VAR_SPECIAL:
2930 n = argvars[0].vval.v_number != VVAL_TRUE;
2931 break;
2932
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002933 case VAR_BLOB:
2934 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002935 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2936 break;
2937
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002938 case VAR_JOB:
2939#ifdef FEAT_JOB_CHANNEL
2940 n = argvars[0].vval.v_job == NULL
2941 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2942 break;
2943#endif
2944 case VAR_CHANNEL:
2945#ifdef FEAT_JOB_CHANNEL
2946 n = argvars[0].vval.v_channel == NULL
2947 || !channel_is_open(argvars[0].vval.v_channel);
2948 break;
2949#endif
2950 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002951 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002953 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002954 n = TRUE;
2955 break;
2956 }
2957
2958 rettv->vval.v_number = n;
2959}
2960
2961/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002962 * "environ()" function
2963 */
2964 static void
2965f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2966{
2967#if !defined(AMIGA)
2968 int i = 0;
2969 char_u *entry, *value;
2970# ifdef MSWIN
2971 extern wchar_t **_wenviron;
2972# else
2973 extern char **environ;
2974# endif
2975
2976 if (rettv_dict_alloc(rettv) != OK)
2977 return;
2978
2979# ifdef MSWIN
2980 if (*_wenviron == NULL)
2981 return;
2982# else
2983 if (*environ == NULL)
2984 return;
2985# endif
2986
2987 for (i = 0; ; ++i)
2988 {
2989# ifdef MSWIN
2990 short_u *p;
2991
2992 if ((p = (short_u *)_wenviron[i]) == NULL)
2993 return;
2994 entry = utf16_to_enc(p, NULL);
2995# else
2996 if ((entry = (char_u *)environ[i]) == NULL)
2997 return;
2998 entry = vim_strsave(entry);
2999# endif
3000 if (entry == NULL) // out of memory
3001 return;
3002 if ((value = vim_strchr(entry, '=')) == NULL)
3003 {
3004 vim_free(entry);
3005 continue;
3006 }
3007 *value++ = NUL;
3008 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3009 vim_free(entry);
3010 }
3011#endif
3012}
3013
3014/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003015 * "escape({string}, {chars})" function
3016 */
3017 static void
3018f_escape(typval_T *argvars, typval_T *rettv)
3019{
3020 char_u buf[NUMBUFLEN];
3021
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003022 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3023 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003024 rettv->v_type = VAR_STRING;
3025}
3026
3027/*
3028 * "eval()" function
3029 */
3030 static void
3031f_eval(typval_T *argvars, typval_T *rettv)
3032{
3033 char_u *s, *p;
3034
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003035 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003036 if (s != NULL)
3037 s = skipwhite(s);
3038
3039 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003040 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003041 {
3042 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003043 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003044 need_clr_eos = FALSE;
3045 rettv->v_type = VAR_NUMBER;
3046 rettv->vval.v_number = 0;
3047 }
3048 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003049 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003050}
3051
3052/*
3053 * "eventhandler()" function
3054 */
3055 static void
3056f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3057{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003058 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003059}
3060
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003061static garray_T redir_execute_ga;
3062
3063/*
3064 * Append "value[value_len]" to the execute() output.
3065 */
3066 void
3067execute_redir_str(char_u *value, int value_len)
3068{
3069 int len;
3070
3071 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003072 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003073 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003074 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003075 if (ga_grow(&redir_execute_ga, len) == OK)
3076 {
3077 mch_memmove((char *)redir_execute_ga.ga_data
3078 + redir_execute_ga.ga_len, value, len);
3079 redir_execute_ga.ga_len += len;
3080 }
3081}
3082
3083/*
3084 * Get next line from a list.
3085 * Called by do_cmdline() to get the next line.
3086 * Returns allocated string, or NULL for end of function.
3087 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003088 static char_u *
3089get_list_line(
3090 int c UNUSED,
3091 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003092 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02003093 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003094{
3095 listitem_T **p = (listitem_T **)cookie;
3096 listitem_T *item = *p;
3097 char_u buf[NUMBUFLEN];
3098 char_u *s;
3099
3100 if (item == NULL)
3101 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003102 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003103 *p = item->li_next;
3104 return s == NULL ? NULL : vim_strsave(s);
3105}
3106
3107/*
3108 * "execute()" function
3109 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02003110 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003111execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003112{
3113 char_u *cmd = NULL;
3114 list_T *list = NULL;
3115 int save_msg_silent = msg_silent;
3116 int save_emsg_silent = emsg_silent;
3117 int save_emsg_noredir = emsg_noredir;
3118 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003119 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003120 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003121 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003122 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003123
3124 rettv->vval.v_string = NULL;
3125 rettv->v_type = VAR_STRING;
3126
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003127 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003128 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003129 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003130 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003131 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003132 return;
3133 ++list->lv_refcount;
3134 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003135 else if (argvars[arg_off].v_type == VAR_JOB
3136 || argvars[arg_off].v_type == VAR_CHANNEL)
3137 {
3138 emsg(_(e_inval_string));
3139 return;
3140 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003141 else
3142 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003143 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003144 if (cmd == NULL)
3145 return;
3146 }
3147
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003148 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003149 {
3150 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003151 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003152
3153 if (s == NULL)
3154 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003155 if (*s == NUL)
3156 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003157 if (STRNCMP(s, "silent", 6) == 0)
3158 ++msg_silent;
3159 if (STRCMP(s, "silent!") == 0)
3160 {
3161 emsg_silent = TRUE;
3162 emsg_noredir = TRUE;
3163 }
3164 }
3165 else
3166 ++msg_silent;
3167
3168 if (redir_execute)
3169 save_ga = redir_execute_ga;
3170 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3171 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003172 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003173 if (!echo_output)
3174 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003175
3176 if (cmd != NULL)
3177 do_cmdline_cmd(cmd);
3178 else
3179 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003180 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003181
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003182 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003183 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003184 do_cmdline(NULL, get_list_line, (void *)&item,
3185 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3186 --list->lv_refcount;
3187 }
3188
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003189 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003190 if (ga_grow(&redir_execute_ga, 1) == OK)
3191 {
3192 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3193 rettv->vval.v_string = redir_execute_ga.ga_data;
3194 }
3195 else
3196 {
3197 ga_clear(&redir_execute_ga);
3198 rettv->vval.v_string = NULL;
3199 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003200 msg_silent = save_msg_silent;
3201 emsg_silent = save_emsg_silent;
3202 emsg_noredir = save_emsg_noredir;
3203
3204 redir_execute = save_redir_execute;
3205 if (redir_execute)
3206 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003207 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003208
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003209 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003210 if (echo_output)
3211 // When not working silently: put it in column zero. A following
3212 // "echon" will overwrite the message, unavoidably.
3213 msg_col = 0;
3214 else
3215 // When working silently: Put it back where it was, since nothing
3216 // should have been written.
3217 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003218}
3219
3220/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003221 * "execute()" function
3222 */
3223 static void
3224f_execute(typval_T *argvars, typval_T *rettv)
3225{
3226 execute_common(argvars, rettv, 0);
3227}
3228
3229/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003230 * "exists()" function
3231 */
3232 static void
3233f_exists(typval_T *argvars, typval_T *rettv)
3234{
3235 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003236 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003237
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003238 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003239 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003240 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003241 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003242 if (mch_getenv(p + 1) != NULL)
3243 n = TRUE;
3244 else
3245 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003246 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003247 p = expand_env_save(p);
3248 if (p != NULL && *p != '$')
3249 n = TRUE;
3250 vim_free(p);
3251 }
3252 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003253 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003254 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003255 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003256 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003257 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003258 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003259 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003260 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003261 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003262 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003263 else if (*p == '?') // internal function only
3264 {
3265 n = has_internal_func_name(p + 1);
3266 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003267 else if (*p == ':')
3268 {
3269 n = cmd_exists(p + 1);
3270 }
3271 else if (*p == '#')
3272 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003273 if (p[1] == '#')
3274 n = autocmd_supported(p + 2);
3275 else
3276 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003277 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003278 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003279 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003280 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003281 }
3282
3283 rettv->vval.v_number = n;
3284}
3285
3286#ifdef FEAT_FLOAT
3287/*
3288 * "exp()" function
3289 */
3290 static void
3291f_exp(typval_T *argvars, typval_T *rettv)
3292{
3293 float_T f = 0.0;
3294
3295 rettv->v_type = VAR_FLOAT;
3296 if (get_float_arg(argvars, &f) == OK)
3297 rettv->vval.v_float = exp(f);
3298 else
3299 rettv->vval.v_float = 0.0;
3300}
3301#endif
3302
3303/*
3304 * "expand()" function
3305 */
3306 static void
3307f_expand(typval_T *argvars, typval_T *rettv)
3308{
3309 char_u *s;
3310 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003311 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003312 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3313 expand_T xpc;
3314 int error = FALSE;
3315 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003316#ifdef BACKSLASH_IN_FILENAME
3317 char_u *p_csl_save = p_csl;
3318
3319 // avoid using 'completeslash' here
3320 p_csl = empty_option;
3321#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003322
3323 rettv->v_type = VAR_STRING;
3324 if (argvars[1].v_type != VAR_UNKNOWN
3325 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003326 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003327 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003328 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003329
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003330 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003331 if (*s == '%' || *s == '#' || *s == '<')
3332 {
3333 ++emsg_off;
3334 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3335 --emsg_off;
3336 if (rettv->v_type == VAR_LIST)
3337 {
3338 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3339 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003340 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003341 }
3342 else
3343 rettv->vval.v_string = result;
3344 }
3345 else
3346 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003347 // When the optional second argument is non-zero, don't remove matches
3348 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003349 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003350 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003351 options |= WILD_KEEP_ALL;
3352 if (!error)
3353 {
3354 ExpandInit(&xpc);
3355 xpc.xp_context = EXPAND_FILES;
3356 if (p_wic)
3357 options += WILD_ICASE;
3358 if (rettv->v_type == VAR_STRING)
3359 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3360 options, WILD_ALL);
3361 else if (rettv_list_alloc(rettv) != FAIL)
3362 {
3363 int i;
3364
3365 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3366 for (i = 0; i < xpc.xp_numfiles; i++)
3367 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3368 ExpandCleanup(&xpc);
3369 }
3370 }
3371 else
3372 rettv->vval.v_string = NULL;
3373 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003374#ifdef BACKSLASH_IN_FILENAME
3375 p_csl = p_csl_save;
3376#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003377}
3378
3379/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003380 * "expandcmd()" function
3381 * Expand all the special characters in a command string.
3382 */
3383 static void
3384f_expandcmd(typval_T *argvars, typval_T *rettv)
3385{
3386 exarg_T eap;
3387 char_u *cmdstr;
3388 char *errormsg = NULL;
3389
3390 rettv->v_type = VAR_STRING;
3391 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3392
3393 memset(&eap, 0, sizeof(eap));
3394 eap.cmd = cmdstr;
3395 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003396 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003397 eap.usefilter = FALSE;
3398 eap.nextcmd = NULL;
3399 eap.cmdidx = CMD_USER;
3400
3401 expand_filename(&eap, &cmdstr, &errormsg);
3402 if (errormsg != NULL && *errormsg != NUL)
3403 emsg(errormsg);
3404
3405 rettv->vval.v_string = cmdstr;
3406}
3407
3408/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003409 * "feedkeys()" function
3410 */
3411 static void
3412f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3413{
3414 int remap = TRUE;
3415 int insert = FALSE;
3416 char_u *keys, *flags;
3417 char_u nbuf[NUMBUFLEN];
3418 int typed = FALSE;
3419 int execute = FALSE;
3420 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003421 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003422 char_u *keys_esc;
3423
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003424 // This is not allowed in the sandbox. If the commands would still be
3425 // executed in the sandbox it would be OK, but it probably happens later,
3426 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003427 if (check_secure())
3428 return;
3429
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003430 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003431
3432 if (argvars[1].v_type != VAR_UNKNOWN)
3433 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003434 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003435 for ( ; *flags != NUL; ++flags)
3436 {
3437 switch (*flags)
3438 {
3439 case 'n': remap = FALSE; break;
3440 case 'm': remap = TRUE; break;
3441 case 't': typed = TRUE; break;
3442 case 'i': insert = TRUE; break;
3443 case 'x': execute = TRUE; break;
3444 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003445 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003446 }
3447 }
3448 }
3449
3450 if (*keys != NUL || execute)
3451 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003452 // Need to escape K_SPECIAL and CSI before putting the string in the
3453 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003454 keys_esc = vim_strsave_escape_csi(keys);
3455 if (keys_esc != NULL)
3456 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003457 if (lowlevel)
3458 {
3459#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003460 int idx;
3461 int len = (int)STRLEN(keys);
3462
3463 for (idx = 0; idx < len; ++idx)
3464 {
3465 // if a CTRL-C was typed, set got_int, similar to what
3466 // happens in fill_input_buf()
3467 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3468 got_int = TRUE;
3469 add_to_input_buf(keys + idx, 1);
3470 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003471#else
3472 emsg(_("E980: lowlevel input not supported"));
3473#endif
3474 }
3475 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003476 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003477 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003478 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003479 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003480#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003481 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003482#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003483 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003484 typebuf_was_filled = TRUE;
3485 }
3486 vim_free(keys_esc);
3487
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003488 if (execute)
3489 {
3490 int save_msg_scroll = msg_scroll;
3491
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003492 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003493 msg_scroll = FALSE;
3494
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003495 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003496 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003497 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003498 ++in_feedkeys;
3499 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003500 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003501 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003502 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003503 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003504 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003505 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003506
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003507 msg_scroll |= save_msg_scroll;
3508 }
3509 }
3510 }
3511}
3512
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003513#ifdef FEAT_FLOAT
3514/*
3515 * "float2nr({float})" function
3516 */
3517 static void
3518f_float2nr(typval_T *argvars, typval_T *rettv)
3519{
3520 float_T f = 0.0;
3521
3522 if (get_float_arg(argvars, &f) == OK)
3523 {
Bram Moolenaar37184272020-05-23 19:30:05 +02003524 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003525 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02003526 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003527 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003528 else
3529 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003530 }
3531}
3532
3533/*
3534 * "floor({float})" function
3535 */
3536 static void
3537f_floor(typval_T *argvars, typval_T *rettv)
3538{
3539 float_T f = 0.0;
3540
3541 rettv->v_type = VAR_FLOAT;
3542 if (get_float_arg(argvars, &f) == OK)
3543 rettv->vval.v_float = floor(f);
3544 else
3545 rettv->vval.v_float = 0.0;
3546}
3547
3548/*
3549 * "fmod()" function
3550 */
3551 static void
3552f_fmod(typval_T *argvars, typval_T *rettv)
3553{
3554 float_T fx = 0.0, fy = 0.0;
3555
3556 rettv->v_type = VAR_FLOAT;
3557 if (get_float_arg(argvars, &fx) == OK
3558 && get_float_arg(&argvars[1], &fy) == OK)
3559 rettv->vval.v_float = fmod(fx, fy);
3560 else
3561 rettv->vval.v_float = 0.0;
3562}
3563#endif
3564
3565/*
3566 * "fnameescape({string})" function
3567 */
3568 static void
3569f_fnameescape(typval_T *argvars, typval_T *rettv)
3570{
3571 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003572 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003573 rettv->v_type = VAR_STRING;
3574}
3575
3576/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003577 * "foreground()" function
3578 */
3579 static void
3580f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3581{
3582#ifdef FEAT_GUI
3583 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003584 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003585 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003586 return;
3587 }
3588#endif
3589#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003590 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003591#endif
3592}
3593
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003594 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003595common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003596{
3597 char_u *s;
3598 char_u *name;
3599 int use_string = FALSE;
3600 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003601 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003602 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003603
3604 if (argvars[0].v_type == VAR_FUNC)
3605 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003606 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003607 s = argvars[0].vval.v_string;
3608 }
3609 else if (argvars[0].v_type == VAR_PARTIAL
3610 && argvars[0].vval.v_partial != NULL)
3611 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003612 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003613 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003614 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003615 }
3616 else
3617 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003618 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003619 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003620 use_string = TRUE;
3621 }
3622
Bram Moolenaar843b8842016-08-21 14:36:15 +02003623 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003624 {
3625 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003626 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003627 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3628 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003629 if (*name != NUL)
3630 s = NULL;
3631 }
3632
Bram Moolenaar843b8842016-08-21 14:36:15 +02003633 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3634 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003635 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003636 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003637 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003638 ? find_func(trans_name, is_global, NULL) == NULL
3639 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003640 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003641 else
3642 {
3643 int dict_idx = 0;
3644 int arg_idx = 0;
3645 list_T *list = NULL;
3646
3647 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3648 {
3649 char sid_buf[25];
3650 int off = *s == 's' ? 2 : 5;
3651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003652 // Expand s: and <SID> into <SNR>nr_, so that the function can
3653 // also be called from another script. Using trans_function_name()
3654 // would also work, but some plugins depend on the name being
3655 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003656 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003657 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003658 if (name != NULL)
3659 {
3660 STRCPY(name, sid_buf);
3661 STRCAT(name, s + off);
3662 }
3663 }
3664 else
3665 name = vim_strsave(s);
3666
3667 if (argvars[1].v_type != VAR_UNKNOWN)
3668 {
3669 if (argvars[2].v_type != VAR_UNKNOWN)
3670 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003671 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003672 arg_idx = 1;
3673 dict_idx = 2;
3674 }
3675 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003676 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003677 dict_idx = 1;
3678 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003679 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003680 arg_idx = 1;
3681 if (dict_idx > 0)
3682 {
3683 if (argvars[dict_idx].v_type != VAR_DICT)
3684 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003685 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003686 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003687 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003688 }
3689 if (argvars[dict_idx].vval.v_dict == NULL)
3690 dict_idx = 0;
3691 }
3692 if (arg_idx > 0)
3693 {
3694 if (argvars[arg_idx].v_type != VAR_LIST)
3695 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003696 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003697 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003698 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003699 }
3700 list = argvars[arg_idx].vval.v_list;
3701 if (list == NULL || list->lv_len == 0)
3702 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003703 else if (list->lv_len > MAX_FUNC_ARGS)
3704 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003705 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003706 vim_free(name);
3707 goto theend;
3708 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003709 }
3710 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003711 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003712 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003713 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003714
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003715 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003716 if (pt == NULL)
3717 vim_free(name);
3718 else
3719 {
3720 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3721 {
3722 listitem_T *li;
3723 int i = 0;
3724 int arg_len = 0;
3725 int lv_len = 0;
3726
3727 if (arg_pt != NULL)
3728 arg_len = arg_pt->pt_argc;
3729 if (list != NULL)
3730 lv_len = list->lv_len;
3731 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003732 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003733 if (pt->pt_argv == NULL)
3734 {
3735 vim_free(pt);
3736 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003737 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003738 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003739 for (i = 0; i < arg_len; i++)
3740 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3741 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003742 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003743 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003744 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003745 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003746 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003747 }
3748
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003749 // For "function(dict.func, [], dict)" and "func" is a partial
3750 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003751 if (dict_idx > 0)
3752 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003753 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003754 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3755 ++pt->pt_dict->dv_refcount;
3756 }
3757 else if (arg_pt != NULL)
3758 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003759 // If the dict was bound automatically the result is also
3760 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003761 pt->pt_dict = arg_pt->pt_dict;
3762 pt->pt_auto = arg_pt->pt_auto;
3763 if (pt->pt_dict != NULL)
3764 ++pt->pt_dict->dv_refcount;
3765 }
3766
3767 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003768 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3769 {
3770 pt->pt_func = arg_pt->pt_func;
3771 func_ptr_ref(pt->pt_func);
3772 vim_free(name);
3773 }
3774 else if (is_funcref)
3775 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003776 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003777 func_ptr_ref(pt->pt_func);
3778 vim_free(name);
3779 }
3780 else
3781 {
3782 pt->pt_name = name;
3783 func_ref(name);
3784 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003785 }
3786 rettv->v_type = VAR_PARTIAL;
3787 rettv->vval.v_partial = pt;
3788 }
3789 else
3790 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003791 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003792 rettv->v_type = VAR_FUNC;
3793 rettv->vval.v_string = name;
3794 func_ref(name);
3795 }
3796 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003797theend:
3798 vim_free(trans_name);
3799}
3800
3801/*
3802 * "funcref()" function
3803 */
3804 static void
3805f_funcref(typval_T *argvars, typval_T *rettv)
3806{
3807 common_function(argvars, rettv, TRUE);
3808}
3809
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003810 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003811ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003812{
3813 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3814 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003815 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003816}
3817
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003818/*
3819 * "function()" function
3820 */
3821 static void
3822f_function(typval_T *argvars, typval_T *rettv)
3823{
3824 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003825}
3826
3827/*
3828 * "garbagecollect()" function
3829 */
3830 static void
3831f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3832{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003833 // This is postponed until we are back at the toplevel, because we may be
3834 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003835 want_garbage_collect = TRUE;
3836
Bram Moolenaar2df47312020-09-05 17:30:44 +02003837 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003838 garbage_collect_at_exit = TRUE;
3839}
3840
3841/*
3842 * "get()" function
3843 */
3844 static void
3845f_get(typval_T *argvars, typval_T *rettv)
3846{
3847 listitem_T *li;
3848 list_T *l;
3849 dictitem_T *di;
3850 dict_T *d;
3851 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003852 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003853
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003854 if (argvars[0].v_type == VAR_BLOB)
3855 {
3856 int error = FALSE;
3857 int idx = tv_get_number_chk(&argvars[1], &error);
3858
3859 if (!error)
3860 {
3861 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003862 if (idx < 0)
3863 idx = blob_len(argvars[0].vval.v_blob) + idx;
3864 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3865 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003866 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003867 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003868 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003869 tv = rettv;
3870 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003871 }
3872 }
3873 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003874 {
3875 if ((l = argvars[0].vval.v_list) != NULL)
3876 {
3877 int error = FALSE;
3878
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003879 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003880 if (!error && li != NULL)
3881 tv = &li->li_tv;
3882 }
3883 }
3884 else if (argvars[0].v_type == VAR_DICT)
3885 {
3886 if ((d = argvars[0].vval.v_dict) != NULL)
3887 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003888 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003889 if (di != NULL)
3890 tv = &di->di_tv;
3891 }
3892 }
3893 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3894 {
3895 partial_T *pt;
3896 partial_T fref_pt;
3897
3898 if (argvars[0].v_type == VAR_PARTIAL)
3899 pt = argvars[0].vval.v_partial;
3900 else
3901 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003902 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003903 fref_pt.pt_name = argvars[0].vval.v_string;
3904 pt = &fref_pt;
3905 }
3906
3907 if (pt != NULL)
3908 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003909 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003910 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003911
3912 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3913 {
3914 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003915 n = partial_name(pt);
3916 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003917 rettv->vval.v_string = NULL;
3918 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003919 {
3920 rettv->vval.v_string = vim_strsave(n);
3921 if (rettv->v_type == VAR_FUNC)
3922 func_ref(rettv->vval.v_string);
3923 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003924 }
3925 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003926 {
3927 what_is_dict = TRUE;
3928 if (pt->pt_dict != NULL)
3929 rettv_dict_set(rettv, pt->pt_dict);
3930 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003931 else if (STRCMP(what, "args") == 0)
3932 {
3933 rettv->v_type = VAR_LIST;
3934 if (rettv_list_alloc(rettv) == OK)
3935 {
3936 int i;
3937
3938 for (i = 0; i < pt->pt_argc; ++i)
3939 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3940 }
3941 }
3942 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003943 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003944
3945 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3946 // third argument
3947 if (!what_is_dict)
3948 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003949 }
3950 }
3951 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003952 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003953
3954 if (tv == NULL)
3955 {
3956 if (argvars[2].v_type != VAR_UNKNOWN)
3957 copy_tv(&argvars[2], rettv);
3958 }
3959 else
3960 copy_tv(tv, rettv);
3961}
3962
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003963/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003964 * "getchangelist()" function
3965 */
3966 static void
3967f_getchangelist(typval_T *argvars, typval_T *rettv)
3968{
3969#ifdef FEAT_JUMPLIST
3970 buf_T *buf;
3971 int i;
3972 list_T *l;
3973 dict_T *d;
3974#endif
3975
3976 if (rettv_list_alloc(rettv) != OK)
3977 return;
3978
3979#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003980 if (argvars[0].v_type == VAR_UNKNOWN)
3981 buf = curbuf;
3982 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003983 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003984 if (buf == NULL)
3985 return;
3986
3987 l = list_alloc();
3988 if (l == NULL)
3989 return;
3990
3991 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3992 return;
3993 /*
3994 * The current window change list index tracks only the position in the
3995 * current buffer change list. For other buffers, use the change list
3996 * length as the current index.
3997 */
3998 list_append_number(rettv->vval.v_list,
3999 (varnumber_T)((buf == curwin->w_buffer)
4000 ? curwin->w_changelistidx : buf->b_changelistlen));
4001
4002 for (i = 0; i < buf->b_changelistlen; ++i)
4003 {
4004 if (buf->b_changelist[i].lnum == 0)
4005 continue;
4006 if ((d = dict_alloc()) == NULL)
4007 return;
4008 if (list_append_dict(l, d) == FAIL)
4009 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004010 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4011 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004012 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004013 }
4014#endif
4015}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004016
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004017 static void
4018getpos_both(
4019 typval_T *argvars,
4020 typval_T *rettv,
4021 int getcurpos,
4022 int charcol)
4023{
4024 pos_T *fp = NULL;
4025 pos_T pos;
4026 win_T *wp = curwin;
4027 list_T *l;
4028 int fnum = -1;
4029
4030 if (rettv_list_alloc(rettv) == OK)
4031 {
4032 l = rettv->vval.v_list;
4033 if (getcurpos)
4034 {
4035 if (argvars[0].v_type != VAR_UNKNOWN)
4036 {
4037 wp = find_win_by_nr_or_id(&argvars[0]);
4038 if (wp != NULL)
4039 fp = &wp->w_cursor;
4040 }
4041 else
4042 fp = &curwin->w_cursor;
4043 if (fp != NULL && charcol)
4044 {
4045 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01004046 pos.col =
4047 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004048 fp = &pos;
4049 }
4050 }
4051 else
4052 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
4053 if (fnum != -1)
4054 list_append_number(l, (varnumber_T)fnum);
4055 else
4056 list_append_number(l, (varnumber_T)0);
4057 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4058 : (varnumber_T)0);
4059 list_append_number(l, (fp != NULL)
4060 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4061 : (varnumber_T)0);
4062 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
4063 (varnumber_T)0);
4064 if (getcurpos)
4065 {
4066 int save_set_curswant = curwin->w_set_curswant;
4067 colnr_T save_curswant = curwin->w_curswant;
4068 colnr_T save_virtcol = curwin->w_virtcol;
4069
4070 if (wp == curwin)
4071 update_curswant();
4072 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
4073 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
4074
4075 // Do not change "curswant", as it is unexpected that a get
4076 // function has a side effect.
4077 if (wp == curwin && save_set_curswant)
4078 {
4079 curwin->w_set_curswant = save_set_curswant;
4080 curwin->w_curswant = save_curswant;
4081 curwin->w_virtcol = save_virtcol;
4082 curwin->w_valid &= ~VALID_VIRTCOL;
4083 }
4084 }
4085 }
4086 else
4087 rettv->vval.v_number = FALSE;
4088}
4089
4090/*
4091 * "getcharpos()" function
4092 */
4093 static void
4094f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
4095{
4096 getpos_both(argvars, rettv, FALSE, TRUE);
4097}
4098
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004099/*
4100 * "getcharsearch()" function
4101 */
4102 static void
4103f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4104{
4105 if (rettv_dict_alloc(rettv) != FAIL)
4106 {
4107 dict_T *dict = rettv->vval.v_dict;
4108
Bram Moolenaare0be1672018-07-08 16:50:37 +02004109 dict_add_string(dict, "char", last_csearch());
4110 dict_add_number(dict, "forward", last_csearch_forward());
4111 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004112 }
4113}
4114
4115/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004116 * "getenv()" function
4117 */
4118 static void
4119f_getenv(typval_T *argvars, typval_T *rettv)
4120{
4121 int mustfree = FALSE;
4122 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4123
4124 if (p == NULL)
4125 {
4126 rettv->v_type = VAR_SPECIAL;
4127 rettv->vval.v_number = VVAL_NULL;
4128 return;
4129 }
4130 if (!mustfree)
4131 p = vim_strsave(p);
4132 rettv->vval.v_string = p;
4133 rettv->v_type = VAR_STRING;
4134}
4135
4136/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004137 * "getfontname()" function
4138 */
4139 static void
4140f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4141{
4142 rettv->v_type = VAR_STRING;
4143 rettv->vval.v_string = NULL;
4144#ifdef FEAT_GUI
4145 if (gui.in_use)
4146 {
4147 GuiFont font;
4148 char_u *name = NULL;
4149
4150 if (argvars[0].v_type == VAR_UNKNOWN)
4151 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004152 // Get the "Normal" font. Either the name saved by
4153 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004154 font = gui.norm_font;
4155 name = hl_get_font_name();
4156 }
4157 else
4158 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004159 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004160 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004161 return;
4162 font = gui_mch_get_font(name, FALSE);
4163 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004164 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004165 }
4166 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4167 if (argvars[0].v_type != VAR_UNKNOWN)
4168 gui_mch_free_font(font);
4169 }
4170#endif
4171}
4172
4173/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004174 * "getjumplist()" function
4175 */
4176 static void
4177f_getjumplist(typval_T *argvars, typval_T *rettv)
4178{
4179#ifdef FEAT_JUMPLIST
4180 win_T *wp;
4181 int i;
4182 list_T *l;
4183 dict_T *d;
4184#endif
4185
4186 if (rettv_list_alloc(rettv) != OK)
4187 return;
4188
4189#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004190 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004191 if (wp == NULL)
4192 return;
4193
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004194 cleanup_jumplist(wp, TRUE);
4195
Bram Moolenaar4f505882018-02-10 21:06:32 +01004196 l = list_alloc();
4197 if (l == NULL)
4198 return;
4199
4200 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4201 return;
4202 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4203
4204 for (i = 0; i < wp->w_jumplistlen; ++i)
4205 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004206 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4207 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004208 if ((d = dict_alloc()) == NULL)
4209 return;
4210 if (list_append_dict(l, d) == FAIL)
4211 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004212 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4213 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004214 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004215 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004216 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004217 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004218 }
4219#endif
4220}
4221
4222/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004223 * "getpid()" function
4224 */
4225 static void
4226f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4227{
4228 rettv->vval.v_number = mch_get_pid();
4229}
4230
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004231/*
4232 * "getcurpos()" function
4233 */
4234 static void
4235f_getcurpos(typval_T *argvars, typval_T *rettv)
4236{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004237 getpos_both(argvars, rettv, TRUE, FALSE);
4238}
4239
4240 static void
4241f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4242{
4243 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004244}
4245
4246/*
4247 * "getpos(string)" function
4248 */
4249 static void
4250f_getpos(typval_T *argvars, typval_T *rettv)
4251{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004252 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004253}
4254
4255/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004256 * "getreg()" function
4257 */
4258 static void
4259f_getreg(typval_T *argvars, typval_T *rettv)
4260{
4261 char_u *strregname;
4262 int regname;
4263 int arg2 = FALSE;
4264 int return_list = FALSE;
4265 int error = FALSE;
4266
4267 if (argvars[0].v_type != VAR_UNKNOWN)
4268 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004269 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004270 error = strregname == NULL;
4271 if (argvars[1].v_type != VAR_UNKNOWN)
4272 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004273 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004274 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004275 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004276 }
4277 }
4278 else
4279 strregname = get_vim_var_str(VV_REG);
4280
4281 if (error)
4282 return;
4283
4284 regname = (strregname == NULL ? '"' : *strregname);
4285 if (regname == 0)
4286 regname = '"';
4287
4288 if (return_list)
4289 {
4290 rettv->v_type = VAR_LIST;
4291 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4292 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4293 if (rettv->vval.v_list == NULL)
4294 (void)rettv_list_alloc(rettv);
4295 else
4296 ++rettv->vval.v_list->lv_refcount;
4297 }
4298 else
4299 {
4300 rettv->v_type = VAR_STRING;
4301 rettv->vval.v_string = get_reg_contents(regname,
4302 arg2 ? GREG_EXPR_SRC : 0);
4303 }
4304}
4305
4306/*
4307 * "getregtype()" function
4308 */
4309 static void
4310f_getregtype(typval_T *argvars, typval_T *rettv)
4311{
4312 char_u *strregname;
4313 int regname;
4314 char_u buf[NUMBUFLEN + 2];
4315 long reglen = 0;
4316
4317 if (argvars[0].v_type != VAR_UNKNOWN)
4318 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004319 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004320 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004321 {
4322 rettv->v_type = VAR_STRING;
4323 rettv->vval.v_string = NULL;
4324 return;
4325 }
4326 }
4327 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004328 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004329 strregname = get_vim_var_str(VV_REG);
4330
4331 regname = (strregname == NULL ? '"' : *strregname);
4332 if (regname == 0)
4333 regname = '"';
4334
4335 buf[0] = NUL;
4336 buf[1] = NUL;
4337 switch (get_reg_type(regname, &reglen))
4338 {
4339 case MLINE: buf[0] = 'V'; break;
4340 case MCHAR: buf[0] = 'v'; break;
4341 case MBLOCK:
4342 buf[0] = Ctrl_V;
4343 sprintf((char *)buf + 1, "%ld", reglen + 1);
4344 break;
4345 }
4346 rettv->v_type = VAR_STRING;
4347 rettv->vval.v_string = vim_strsave(buf);
4348}
4349
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004350/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004351 * "gettagstack()" function
4352 */
4353 static void
4354f_gettagstack(typval_T *argvars, typval_T *rettv)
4355{
4356 win_T *wp = curwin; // default is current window
4357
4358 if (rettv_dict_alloc(rettv) != OK)
4359 return;
4360
4361 if (argvars[0].v_type != VAR_UNKNOWN)
4362 {
4363 wp = find_win_by_nr_or_id(&argvars[0]);
4364 if (wp == NULL)
4365 return;
4366 }
4367
4368 get_tagstack(wp, rettv->vval.v_dict);
4369}
4370
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004371/*
4372 * "gettext()" function
4373 */
4374 static void
4375f_gettext(typval_T *argvars, typval_T *rettv)
4376{
4377 if (argvars[0].v_type != VAR_STRING
4378 || argvars[0].vval.v_string == NULL
4379 || *argvars[0].vval.v_string == NUL)
4380 {
4381 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4382 }
4383 else
4384 {
4385 rettv->v_type = VAR_STRING;
4386 rettv->vval.v_string = vim_strsave(
4387 (char_u *)_(argvars[0].vval.v_string));
4388 }
4389}
4390
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004391// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004392#include "version.h"
4393
4394/*
4395 * "has()" function
4396 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004397 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004398f_has(typval_T *argvars, typval_T *rettv)
4399{
4400 int i;
4401 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004402 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004403 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004404 typedef struct {
4405 char *name;
4406 short present;
4407 } has_item_T;
4408 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004409 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004410 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004411#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004412 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004413#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004414 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004415#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004416 },
4417 {"arp",
4418#if defined(AMIGA) && defined(FEAT_ARP)
4419 1
4420#else
4421 0
4422#endif
4423 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004424 {"haiku",
4425#ifdef __HAIKU__
4426 1
4427#else
4428 0
4429#endif
4430 },
4431 {"bsd",
4432#if defined(BSD) && !defined(MACOS_X)
4433 1
4434#else
4435 0
4436#endif
4437 },
4438 {"hpux",
4439#ifdef hpux
4440 1
4441#else
4442 0
4443#endif
4444 },
4445 {"linux",
4446#ifdef __linux__
4447 1
4448#else
4449 0
4450#endif
4451 },
4452 {"mac", // Mac OS X (and, once, Mac OS Classic)
4453#ifdef MACOS_X
4454 1
4455#else
4456 0
4457#endif
4458 },
4459 {"osx", // Mac OS X
4460#ifdef MACOS_X
4461 1
4462#else
4463 0
4464#endif
4465 },
4466 {"macunix", // Mac OS X, with the darwin feature
4467#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4468 1
4469#else
4470 0
4471#endif
4472 },
4473 {"osxdarwin", // synonym for macunix
4474#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4475 1
4476#else
4477 0
4478#endif
4479 },
4480 {"qnx",
4481#ifdef __QNX__
4482 1
4483#else
4484 0
4485#endif
4486 },
4487 {"sun",
4488#ifdef SUN_SYSTEM
4489 1
4490#else
4491 0
4492#endif
4493 },
4494 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004495#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004496 1
4497#else
4498 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004499#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004500 },
4501 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004502#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004503 1
4504#else
4505 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004506#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004507 },
4508 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004509#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004510 1
4511#else
4512 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004513#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004514 },
4515 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004516#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004517 1
4518#else
4519 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004520#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004521 },
4522 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004523#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004524 1
4525#else
4526 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004527#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004528 },
4529 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004530#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004531 1
4532#else
4533 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004534#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004535 },
4536 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004537#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004538 1
4539#else
4540 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004541#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004542 },
4543 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004544#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004545 1
4546#else
4547 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004548#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004549 },
4550 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004551#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004552 1
4553#else
4554 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004555#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004556 },
4557 {"autocmd", 1},
4558 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004559#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004560 1
4561#else
4562 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004563#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004564 },
4565 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004566#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004567 1
4568#else
4569 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004570#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004571 },
4572 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004573#ifdef FEAT_BEVAL_GUI
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 {"balloon_multiline",
4580#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4581 // MS-Windows requires runtime check, see below
4582 1
4583#else
4584 0
4585#endif
4586 },
4587 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004588#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004589 1
4590#else
4591 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004592#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004593 },
4594 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004595#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004596 1
4597#else
4598 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004599#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004600 },
4601 {"all_builtin_terms",
4602#if defined(ALL_BUILTIN_TCAPS)
4603 1
4604#else
4605 0
4606#endif
4607 },
4608 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004609#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004610 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004611 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004612 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004613#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004614 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004615#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004616 },
4617 {"byte_offset",
4618#ifdef FEAT_BYTEOFF
4619 1
4620#else
4621 0
4622#endif
4623 },
4624 {"channel",
4625#ifdef FEAT_JOB_CHANNEL
4626 1
4627#else
4628 0
4629#endif
4630 },
4631 {"cindent",
4632#ifdef FEAT_CINDENT
4633 1
4634#else
4635 0
4636#endif
4637 },
4638 {"clientserver",
4639#ifdef FEAT_CLIENTSERVER
4640 1
4641#else
4642 0
4643#endif
4644 },
4645 {"clipboard",
4646#ifdef FEAT_CLIPBOARD
4647 1
4648#else
4649 0
4650#endif
4651 },
4652 {"cmdline_compl", 1},
4653 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004654 {"cmdwin",
4655#ifdef FEAT_CMDWIN
4656 1
4657#else
4658 0
4659#endif
4660 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004661 {"comments", 1},
4662 {"conceal",
4663#ifdef FEAT_CONCEAL
4664 1
4665#else
4666 0
4667#endif
4668 },
4669 {"cryptv",
4670#ifdef FEAT_CRYPT
4671 1
4672#else
4673 0
4674#endif
4675 },
4676 {"crypt-blowfish",
4677#ifdef FEAT_CRYPT
4678 1
4679#else
4680 0
4681#endif
4682 },
4683 {"crypt-blowfish2",
4684#ifdef FEAT_CRYPT
4685 1
4686#else
4687 0
4688#endif
4689 },
4690 {"cscope",
4691#ifdef FEAT_CSCOPE
4692 1
4693#else
4694 0
4695#endif
4696 },
4697 {"cursorbind", 1},
4698 {"cursorshape",
4699#ifdef CURSOR_SHAPE
4700 1
4701#else
4702 0
4703#endif
4704 },
4705 {"debug",
4706#ifdef DEBUG
4707 1
4708#else
4709 0
4710#endif
4711 },
4712 {"dialog_con",
4713#ifdef FEAT_CON_DIALOG
4714 1
4715#else
4716 0
4717#endif
4718 },
4719 {"dialog_gui",
4720#ifdef FEAT_GUI_DIALOG
4721 1
4722#else
4723 0
4724#endif
4725 },
4726 {"diff",
4727#ifdef FEAT_DIFF
4728 1
4729#else
4730 0
4731#endif
4732 },
4733 {"digraphs",
4734#ifdef FEAT_DIGRAPHS
4735 1
4736#else
4737 0
4738#endif
4739 },
4740 {"directx",
4741#ifdef FEAT_DIRECTX
4742 1
4743#else
4744 0
4745#endif
4746 },
4747 {"dnd",
4748#ifdef FEAT_DND
4749 1
4750#else
4751 0
4752#endif
4753 },
4754 {"emacs_tags",
4755#ifdef FEAT_EMACS_TAGS
4756 1
4757#else
4758 0
4759#endif
4760 },
4761 {"eval", 1}, // always present, of course!
4762 {"ex_extra", 1}, // graduated feature
4763 {"extra_search",
4764#ifdef FEAT_SEARCH_EXTRA
4765 1
4766#else
4767 0
4768#endif
4769 },
4770 {"file_in_path",
4771#ifdef FEAT_SEARCHPATH
4772 1
4773#else
4774 0
4775#endif
4776 },
4777 {"filterpipe",
4778#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4779 1
4780#else
4781 0
4782#endif
4783 },
4784 {"find_in_path",
4785#ifdef FEAT_FIND_ID
4786 1
4787#else
4788 0
4789#endif
4790 },
4791 {"float",
4792#ifdef FEAT_FLOAT
4793 1
4794#else
4795 0
4796#endif
4797 },
4798 {"folding",
4799#ifdef FEAT_FOLDING
4800 1
4801#else
4802 0
4803#endif
4804 },
4805 {"footer",
4806#ifdef FEAT_FOOTER
4807 1
4808#else
4809 0
4810#endif
4811 },
4812 {"fork",
4813#if !defined(USE_SYSTEM) && defined(UNIX)
4814 1
4815#else
4816 0
4817#endif
4818 },
4819 {"gettext",
4820#ifdef FEAT_GETTEXT
4821 1
4822#else
4823 0
4824#endif
4825 },
4826 {"gui",
4827#ifdef FEAT_GUI
4828 1
4829#else
4830 0
4831#endif
4832 },
4833 {"gui_neXtaw",
4834#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4835 1
4836#else
4837 0
4838#endif
4839 },
4840 {"gui_athena",
4841#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4842 1
4843#else
4844 0
4845#endif
4846 },
4847 {"gui_gtk",
4848#ifdef FEAT_GUI_GTK
4849 1
4850#else
4851 0
4852#endif
4853 },
4854 {"gui_gtk2",
4855#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4856 1
4857#else
4858 0
4859#endif
4860 },
4861 {"gui_gtk3",
4862#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4863 1
4864#else
4865 0
4866#endif
4867 },
4868 {"gui_gnome",
4869#ifdef FEAT_GUI_GNOME
4870 1
4871#else
4872 0
4873#endif
4874 },
4875 {"gui_haiku",
4876#ifdef FEAT_GUI_HAIKU
4877 1
4878#else
4879 0
4880#endif
4881 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004882 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004883 {"gui_motif",
4884#ifdef FEAT_GUI_MOTIF
4885 1
4886#else
4887 0
4888#endif
4889 },
4890 {"gui_photon",
4891#ifdef FEAT_GUI_PHOTON
4892 1
4893#else
4894 0
4895#endif
4896 },
4897 {"gui_win32",
4898#ifdef FEAT_GUI_MSWIN
4899 1
4900#else
4901 0
4902#endif
4903 },
4904 {"iconv",
4905#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4906 1
4907#else
4908 0
4909#endif
4910 },
4911 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004912 {"ipv6",
4913#ifdef FEAT_IPV6
4914 1
4915#else
4916 0
4917#endif
4918 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004919 {"job",
4920#ifdef FEAT_JOB_CHANNEL
4921 1
4922#else
4923 0
4924#endif
4925 },
4926 {"jumplist",
4927#ifdef FEAT_JUMPLIST
4928 1
4929#else
4930 0
4931#endif
4932 },
4933 {"keymap",
4934#ifdef FEAT_KEYMAP
4935 1
4936#else
4937 0
4938#endif
4939 },
4940 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4941 {"langmap",
4942#ifdef FEAT_LANGMAP
4943 1
4944#else
4945 0
4946#endif
4947 },
4948 {"libcall",
4949#ifdef FEAT_LIBCALL
4950 1
4951#else
4952 0
4953#endif
4954 },
4955 {"linebreak",
4956#ifdef FEAT_LINEBREAK
4957 1
4958#else
4959 0
4960#endif
4961 },
4962 {"lispindent",
4963#ifdef FEAT_LISP
4964 1
4965#else
4966 0
4967#endif
4968 },
4969 {"listcmds", 1},
4970 {"localmap", 1},
4971 {"lua",
4972#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4973 1
4974#else
4975 0
4976#endif
4977 },
4978 {"menu",
4979#ifdef FEAT_MENU
4980 1
4981#else
4982 0
4983#endif
4984 },
4985 {"mksession",
4986#ifdef FEAT_SESSION
4987 1
4988#else
4989 0
4990#endif
4991 },
4992 {"modify_fname", 1},
4993 {"mouse", 1},
4994 {"mouseshape",
4995#ifdef FEAT_MOUSESHAPE
4996 1
4997#else
4998 0
4999#endif
5000 },
5001 {"mouse_dec",
5002#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
5003 1
5004#else
5005 0
5006#endif
5007 },
5008 {"mouse_gpm",
5009#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
5010 1
5011#else
5012 0
5013#endif
5014 },
5015 {"mouse_jsbterm",
5016#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
5017 1
5018#else
5019 0
5020#endif
5021 },
5022 {"mouse_netterm",
5023#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
5024 1
5025#else
5026 0
5027#endif
5028 },
5029 {"mouse_pterm",
5030#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
5031 1
5032#else
5033 0
5034#endif
5035 },
5036 {"mouse_sgr",
5037#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5038 1
5039#else
5040 0
5041#endif
5042 },
5043 {"mouse_sysmouse",
5044#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
5045 1
5046#else
5047 0
5048#endif
5049 },
5050 {"mouse_urxvt",
5051#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
5052 1
5053#else
5054 0
5055#endif
5056 },
5057 {"mouse_xterm",
5058#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5059 1
5060#else
5061 0
5062#endif
5063 },
5064 {"multi_byte", 1},
5065 {"multi_byte_ime",
5066#ifdef FEAT_MBYTE_IME
5067 1
5068#else
5069 0
5070#endif
5071 },
5072 {"multi_lang",
5073#ifdef FEAT_MULTI_LANG
5074 1
5075#else
5076 0
5077#endif
5078 },
5079 {"mzscheme",
5080#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
5081 1
5082#else
5083 0
5084#endif
5085 },
5086 {"num64", 1},
5087 {"ole",
5088#ifdef FEAT_OLE
5089 1
5090#else
5091 0
5092#endif
5093 },
5094 {"packages",
5095#ifdef FEAT_EVAL
5096 1
5097#else
5098 0
5099#endif
5100 },
5101 {"path_extra",
5102#ifdef FEAT_PATH_EXTRA
5103 1
5104#else
5105 0
5106#endif
5107 },
5108 {"perl",
5109#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
5110 1
5111#else
5112 0
5113#endif
5114 },
5115 {"persistent_undo",
5116#ifdef FEAT_PERSISTENT_UNDO
5117 1
5118#else
5119 0
5120#endif
5121 },
5122 {"python_compiled",
5123#if defined(FEAT_PYTHON)
5124 1
5125#else
5126 0
5127#endif
5128 },
5129 {"python_dynamic",
5130#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
5131 1
5132#else
5133 0
5134#endif
5135 },
5136 {"python",
5137#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
5138 1
5139#else
5140 0
5141#endif
5142 },
5143 {"pythonx",
5144#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
5145 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
5146 1
5147#else
5148 0
5149#endif
5150 },
5151 {"python3_compiled",
5152#if defined(FEAT_PYTHON3)
5153 1
5154#else
5155 0
5156#endif
5157 },
5158 {"python3_dynamic",
5159#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
5160 1
5161#else
5162 0
5163#endif
5164 },
5165 {"python3",
5166#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
5167 1
5168#else
5169 0
5170#endif
5171 },
5172 {"popupwin",
5173#ifdef FEAT_PROP_POPUP
5174 1
5175#else
5176 0
5177#endif
5178 },
5179 {"postscript",
5180#ifdef FEAT_POSTSCRIPT
5181 1
5182#else
5183 0
5184#endif
5185 },
5186 {"printer",
5187#ifdef FEAT_PRINTER
5188 1
5189#else
5190 0
5191#endif
5192 },
5193 {"profile",
5194#ifdef FEAT_PROFILE
5195 1
5196#else
5197 0
5198#endif
5199 },
5200 {"reltime",
5201#ifdef FEAT_RELTIME
5202 1
5203#else
5204 0
5205#endif
5206 },
5207 {"quickfix",
5208#ifdef FEAT_QUICKFIX
5209 1
5210#else
5211 0
5212#endif
5213 },
5214 {"rightleft",
5215#ifdef FEAT_RIGHTLEFT
5216 1
5217#else
5218 0
5219#endif
5220 },
5221 {"ruby",
5222#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5223 1
5224#else
5225 0
5226#endif
5227 },
5228 {"scrollbind", 1},
5229 {"showcmd",
5230#ifdef FEAT_CMDL_INFO
5231 1
5232#else
5233 0
5234#endif
5235 },
5236 {"cmdline_info",
5237#ifdef FEAT_CMDL_INFO
5238 1
5239#else
5240 0
5241#endif
5242 },
5243 {"signs",
5244#ifdef FEAT_SIGNS
5245 1
5246#else
5247 0
5248#endif
5249 },
5250 {"smartindent",
5251#ifdef FEAT_SMARTINDENT
5252 1
5253#else
5254 0
5255#endif
5256 },
5257 {"startuptime",
5258#ifdef STARTUPTIME
5259 1
5260#else
5261 0
5262#endif
5263 },
5264 {"statusline",
5265#ifdef FEAT_STL_OPT
5266 1
5267#else
5268 0
5269#endif
5270 },
5271 {"netbeans_intg",
5272#ifdef FEAT_NETBEANS_INTG
5273 1
5274#else
5275 0
5276#endif
5277 },
5278 {"sound",
5279#ifdef FEAT_SOUND
5280 1
5281#else
5282 0
5283#endif
5284 },
5285 {"spell",
5286#ifdef FEAT_SPELL
5287 1
5288#else
5289 0
5290#endif
5291 },
5292 {"syntax",
5293#ifdef FEAT_SYN_HL
5294 1
5295#else
5296 0
5297#endif
5298 },
5299 {"system",
5300#if defined(USE_SYSTEM) || !defined(UNIX)
5301 1
5302#else
5303 0
5304#endif
5305 },
5306 {"tag_binary",
5307#ifdef FEAT_TAG_BINS
5308 1
5309#else
5310 0
5311#endif
5312 },
5313 {"tcl",
5314#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5315 1
5316#else
5317 0
5318#endif
5319 },
5320 {"termguicolors",
5321#ifdef FEAT_TERMGUICOLORS
5322 1
5323#else
5324 0
5325#endif
5326 },
5327 {"terminal",
5328#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5329 1
5330#else
5331 0
5332#endif
5333 },
5334 {"terminfo",
5335#ifdef TERMINFO
5336 1
5337#else
5338 0
5339#endif
5340 },
5341 {"termresponse",
5342#ifdef FEAT_TERMRESPONSE
5343 1
5344#else
5345 0
5346#endif
5347 },
5348 {"textobjects",
5349#ifdef FEAT_TEXTOBJ
5350 1
5351#else
5352 0
5353#endif
5354 },
5355 {"textprop",
5356#ifdef FEAT_PROP_POPUP
5357 1
5358#else
5359 0
5360#endif
5361 },
5362 {"tgetent",
5363#ifdef HAVE_TGETENT
5364 1
5365#else
5366 0
5367#endif
5368 },
5369 {"timers",
5370#ifdef FEAT_TIMERS
5371 1
5372#else
5373 0
5374#endif
5375 },
5376 {"title",
5377#ifdef FEAT_TITLE
5378 1
5379#else
5380 0
5381#endif
5382 },
5383 {"toolbar",
5384#ifdef FEAT_TOOLBAR
5385 1
5386#else
5387 0
5388#endif
5389 },
5390 {"unnamedplus",
5391#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5392 1
5393#else
5394 0
5395#endif
5396 },
5397 {"user-commands", 1}, // was accidentally included in 5.4
5398 {"user_commands", 1},
5399 {"vartabs",
5400#ifdef FEAT_VARTABS
5401 1
5402#else
5403 0
5404#endif
5405 },
5406 {"vertsplit", 1},
5407 {"viminfo",
5408#ifdef FEAT_VIMINFO
5409 1
5410#else
5411 0
5412#endif
5413 },
5414 {"vimscript-1", 1},
5415 {"vimscript-2", 1},
5416 {"vimscript-3", 1},
5417 {"vimscript-4", 1},
5418 {"virtualedit", 1},
5419 {"visual", 1},
5420 {"visualextra", 1},
5421 {"vreplace", 1},
5422 {"vtp",
5423#ifdef FEAT_VTP
5424 1
5425#else
5426 0
5427#endif
5428 },
5429 {"wildignore",
5430#ifdef FEAT_WILDIGN
5431 1
5432#else
5433 0
5434#endif
5435 },
5436 {"wildmenu",
5437#ifdef FEAT_WILDMENU
5438 1
5439#else
5440 0
5441#endif
5442 },
5443 {"windows", 1},
5444 {"winaltkeys",
5445#ifdef FEAT_WAK
5446 1
5447#else
5448 0
5449#endif
5450 },
5451 {"writebackup",
5452#ifdef FEAT_WRITEBACKUP
5453 1
5454#else
5455 0
5456#endif
5457 },
5458 {"xim",
5459#ifdef FEAT_XIM
5460 1
5461#else
5462 0
5463#endif
5464 },
5465 {"xfontset",
5466#ifdef FEAT_XFONTSET
5467 1
5468#else
5469 0
5470#endif
5471 },
5472 {"xpm",
5473#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5474 1
5475#else
5476 0
5477#endif
5478 },
5479 {"xpm_w32", // for backward compatibility
5480#ifdef FEAT_XPM_W32
5481 1
5482#else
5483 0
5484#endif
5485 },
5486 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005487#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005488 1
5489#else
5490 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005491#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005492 },
5493 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005494#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005495 1
5496#else
5497 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005498#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005499 },
5500 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005501#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005502 1
5503#else
5504 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005505#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005506 },
5507 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005508#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005509 1
5510#else
5511 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005512#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005513 },
5514 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005515#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005516 1
5517#else
5518 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005519#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005520 },
5521 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005522 };
5523
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005524 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005525 for (i = 0; has_list[i].name != NULL; ++i)
5526 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005527 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005528 x = TRUE;
5529 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005530 break;
5531 }
5532
Bram Moolenaar79296512020-03-22 16:17:14 +01005533 // features also in has_list[] but sometimes enabled at runtime
5534 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005535 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005536 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005537 {
5538 // intentionally empty
5539 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005540#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005541 else if (STRICMP(name, "balloon_multiline") == 0)
5542 n = multiline_balloon_available();
5543#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005544#ifdef VIMDLL
5545 else if (STRICMP(name, "filterpipe") == 0)
5546 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005547#endif
5548#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5549 else if (STRICMP(name, "iconv") == 0)
5550 n = iconv_enabled(FALSE);
5551#endif
5552#ifdef DYNAMIC_LUA
5553 else if (STRICMP(name, "lua") == 0)
5554 n = lua_enabled(FALSE);
5555#endif
5556#ifdef DYNAMIC_MZSCHEME
5557 else if (STRICMP(name, "mzscheme") == 0)
5558 n = mzscheme_enabled(FALSE);
5559#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005560#ifdef DYNAMIC_PERL
5561 else if (STRICMP(name, "perl") == 0)
5562 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005563#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005564#ifdef DYNAMIC_PYTHON
5565 else if (STRICMP(name, "python") == 0)
5566 n = python_enabled(FALSE);
5567#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005568#ifdef DYNAMIC_PYTHON3
5569 else if (STRICMP(name, "python3") == 0)
5570 n = python3_enabled(FALSE);
5571#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005572#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5573 else if (STRICMP(name, "pythonx") == 0)
5574 {
5575# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5576 if (p_pyx == 0)
5577 n = python3_enabled(FALSE) || python_enabled(FALSE);
5578 else if (p_pyx == 3)
5579 n = python3_enabled(FALSE);
5580 else if (p_pyx == 2)
5581 n = python_enabled(FALSE);
5582# elif defined(DYNAMIC_PYTHON)
5583 n = python_enabled(FALSE);
5584# elif defined(DYNAMIC_PYTHON3)
5585 n = python3_enabled(FALSE);
5586# endif
5587 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005588#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005589#ifdef DYNAMIC_RUBY
5590 else if (STRICMP(name, "ruby") == 0)
5591 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005592#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005593#ifdef DYNAMIC_TCL
5594 else if (STRICMP(name, "tcl") == 0)
5595 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005596#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005597#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005598 else if (STRICMP(name, "terminal") == 0)
5599 n = terminal_enabled();
5600#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005601 }
5602
Bram Moolenaar79296512020-03-22 16:17:14 +01005603 // features not in has_list[]
5604 if (x == FALSE)
5605 {
5606 if (STRNICMP(name, "patch", 5) == 0)
5607 {
5608 x = TRUE;
5609 if (name[5] == '-'
5610 && STRLEN(name) >= 11
5611 && vim_isdigit(name[6])
5612 && vim_isdigit(name[8])
5613 && vim_isdigit(name[10]))
5614 {
5615 int major = atoi((char *)name + 6);
5616 int minor = atoi((char *)name + 8);
5617
5618 // Expect "patch-9.9.01234".
5619 n = (major < VIM_VERSION_MAJOR
5620 || (major == VIM_VERSION_MAJOR
5621 && (minor < VIM_VERSION_MINOR
5622 || (minor == VIM_VERSION_MINOR
5623 && has_patch(atoi((char *)name + 10))))));
5624 }
5625 else
5626 n = has_patch(atoi((char *)name + 5));
5627 }
5628 else if (STRICMP(name, "vim_starting") == 0)
5629 {
5630 x = TRUE;
5631 n = (starting != 0);
5632 }
5633 else if (STRICMP(name, "ttyin") == 0)
5634 {
5635 x = TRUE;
5636 n = mch_input_isatty();
5637 }
5638 else if (STRICMP(name, "ttyout") == 0)
5639 {
5640 x = TRUE;
5641 n = stdout_isatty;
5642 }
5643 else if (STRICMP(name, "multi_byte_encoding") == 0)
5644 {
5645 x = TRUE;
5646 n = has_mbyte;
5647 }
5648 else if (STRICMP(name, "gui_running") == 0)
5649 {
5650 x = TRUE;
5651#ifdef FEAT_GUI
5652 n = (gui.in_use || gui.starting);
5653#endif
5654 }
5655 else if (STRICMP(name, "browse") == 0)
5656 {
5657 x = TRUE;
5658#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5659 n = gui.in_use; // gui_mch_browse() works when GUI is running
5660#endif
5661 }
5662 else if (STRICMP(name, "syntax_items") == 0)
5663 {
5664 x = TRUE;
5665#ifdef FEAT_SYN_HL
5666 n = syntax_present(curwin);
5667#endif
5668 }
5669 else if (STRICMP(name, "vcon") == 0)
5670 {
5671 x = TRUE;
5672#ifdef FEAT_VTP
5673 n = is_term_win32() && has_vtp_working();
5674#endif
5675 }
5676 else if (STRICMP(name, "netbeans_enabled") == 0)
5677 {
5678 x = TRUE;
5679#ifdef FEAT_NETBEANS_INTG
5680 n = netbeans_active();
5681#endif
5682 }
5683 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5684 {
5685 x = TRUE;
5686#ifdef FEAT_MOUSE_GPM
5687 n = gpm_enabled();
5688#endif
5689 }
5690 else if (STRICMP(name, "conpty") == 0)
5691 {
5692 x = TRUE;
5693#if defined(FEAT_TERMINAL) && defined(MSWIN)
5694 n = use_conpty();
5695#endif
5696 }
5697 else if (STRICMP(name, "clipboard_working") == 0)
5698 {
5699 x = TRUE;
5700#ifdef FEAT_CLIPBOARD
5701 n = clip_star.available;
5702#endif
5703 }
5704 }
5705
Bram Moolenaar04637e22020-09-05 18:45:29 +02005706 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005707 // return whether feature could ever be enabled
5708 rettv->vval.v_number = x;
5709 else
5710 // return whether feature is enabled
5711 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005712}
5713
5714/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005715 * Return TRUE if "feature" can change later.
5716 * Also when checking for the feature has side effects, such as loading a DLL.
5717 */
5718 int
5719dynamic_feature(char_u *feature)
5720{
5721 return (feature == NULL
5722#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5723 || STRICMP(feature, "balloon_multiline") == 0
5724#endif
5725#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5726 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5727#endif
5728#ifdef VIMDLL
5729 || STRICMP(feature, "filterpipe") == 0
5730#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005731#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005732 // this can only change on Unix where the ":gui" command could be
5733 // used.
5734 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5735#endif
5736#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5737 || STRICMP(feature, "iconv") == 0
5738#endif
5739#ifdef DYNAMIC_LUA
5740 || STRICMP(feature, "lua") == 0
5741#endif
5742#ifdef FEAT_MOUSE_GPM
5743 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5744#endif
5745#ifdef DYNAMIC_MZSCHEME
5746 || STRICMP(feature, "mzscheme") == 0
5747#endif
5748#ifdef FEAT_NETBEANS_INTG
5749 || STRICMP(feature, "netbeans_enabled") == 0
5750#endif
5751#ifdef DYNAMIC_PERL
5752 || STRICMP(feature, "perl") == 0
5753#endif
5754#ifdef DYNAMIC_PYTHON
5755 || STRICMP(feature, "python") == 0
5756#endif
5757#ifdef DYNAMIC_PYTHON3
5758 || STRICMP(feature, "python3") == 0
5759#endif
5760#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5761 || STRICMP(feature, "pythonx") == 0
5762#endif
5763#ifdef DYNAMIC_RUBY
5764 || STRICMP(feature, "ruby") == 0
5765#endif
5766#ifdef FEAT_SYN_HL
5767 || STRICMP(feature, "syntax_items") == 0
5768#endif
5769#ifdef DYNAMIC_TCL
5770 || STRICMP(feature, "tcl") == 0
5771#endif
5772 // once "starting" is zero it will stay that way
5773 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5774 || STRICMP(feature, "multi_byte_encoding") == 0
5775#if defined(FEAT_TERMINAL) && defined(MSWIN)
5776 || STRICMP(feature, "conpty") == 0
5777#endif
5778 );
5779}
5780
5781/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005782 * "haslocaldir()" function
5783 */
5784 static void
5785f_haslocaldir(typval_T *argvars, typval_T *rettv)
5786{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005787 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005788 win_T *wp = NULL;
5789
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005790 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5791
5792 // Check for window-local and tab-local directories
5793 if (wp != NULL && wp->w_localdir != NULL)
5794 rettv->vval.v_number = 1;
5795 else if (tp != NULL && tp->tp_localdir != NULL)
5796 rettv->vval.v_number = 2;
5797 else
5798 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005799}
5800
5801/*
5802 * "hasmapto()" function
5803 */
5804 static void
5805f_hasmapto(typval_T *argvars, typval_T *rettv)
5806{
5807 char_u *name;
5808 char_u *mode;
5809 char_u buf[NUMBUFLEN];
5810 int abbr = FALSE;
5811
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005812 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005813 if (argvars[1].v_type == VAR_UNKNOWN)
5814 mode = (char_u *)"nvo";
5815 else
5816 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005817 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005818 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005819 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005820 }
5821
5822 if (map_to_exists(name, mode, abbr))
5823 rettv->vval.v_number = TRUE;
5824 else
5825 rettv->vval.v_number = FALSE;
5826}
5827
5828/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005829 * "highlightID(name)" function
5830 */
5831 static void
5832f_hlID(typval_T *argvars, typval_T *rettv)
5833{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005834 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005835}
5836
5837/*
5838 * "highlight_exists()" function
5839 */
5840 static void
5841f_hlexists(typval_T *argvars, typval_T *rettv)
5842{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005843 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005844}
5845
5846/*
5847 * "hostname()" function
5848 */
5849 static void
5850f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5851{
5852 char_u hostname[256];
5853
5854 mch_get_host_name(hostname, 256);
5855 rettv->v_type = VAR_STRING;
5856 rettv->vval.v_string = vim_strsave(hostname);
5857}
5858
5859/*
5860 * iconv() function
5861 */
5862 static void
5863f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5864{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005865 char_u buf1[NUMBUFLEN];
5866 char_u buf2[NUMBUFLEN];
5867 char_u *from, *to, *str;
5868 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005869
5870 rettv->v_type = VAR_STRING;
5871 rettv->vval.v_string = NULL;
5872
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005873 str = tv_get_string(&argvars[0]);
5874 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5875 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005876 vimconv.vc_type = CONV_NONE;
5877 convert_setup(&vimconv, from, to);
5878
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005879 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005880 if (vimconv.vc_type == CONV_NONE)
5881 rettv->vval.v_string = vim_strsave(str);
5882 else
5883 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5884
5885 convert_setup(&vimconv, NULL, NULL);
5886 vim_free(from);
5887 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005888}
5889
5890/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005891 * "index()" function
5892 */
5893 static void
5894f_index(typval_T *argvars, typval_T *rettv)
5895{
5896 list_T *l;
5897 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005898 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005899 long idx = 0;
5900 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005901 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005902
5903 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005904 if (argvars[0].v_type == VAR_BLOB)
5905 {
5906 typval_T tv;
5907 int start = 0;
5908
5909 if (argvars[2].v_type != VAR_UNKNOWN)
5910 {
5911 start = tv_get_number_chk(&argvars[2], &error);
5912 if (error)
5913 return;
5914 }
5915 b = argvars[0].vval.v_blob;
5916 if (b == NULL)
5917 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005918 if (start < 0)
5919 {
5920 start = blob_len(b) + start;
5921 if (start < 0)
5922 start = 0;
5923 }
5924
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005925 for (idx = start; idx < blob_len(b); ++idx)
5926 {
5927 tv.v_type = VAR_NUMBER;
5928 tv.vval.v_number = blob_get(b, idx);
5929 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5930 {
5931 rettv->vval.v_number = idx;
5932 return;
5933 }
5934 }
5935 return;
5936 }
5937 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005938 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005939 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005940 return;
5941 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005942
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005943 l = argvars[0].vval.v_list;
5944 if (l != NULL)
5945 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005946 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005947 item = l->lv_first;
5948 if (argvars[2].v_type != VAR_UNKNOWN)
5949 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005950 // Start at specified item. Use the cached index that list_find()
5951 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005952 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005953 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005954 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005955 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005956 if (error)
5957 item = NULL;
5958 }
5959
5960 for ( ; item != NULL; item = item->li_next, ++idx)
5961 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5962 {
5963 rettv->vval.v_number = idx;
5964 break;
5965 }
5966 }
5967}
5968
5969static int inputsecret_flag = 0;
5970
5971/*
5972 * "input()" function
5973 * Also handles inputsecret() when inputsecret is set.
5974 */
5975 static void
5976f_input(typval_T *argvars, typval_T *rettv)
5977{
5978 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5979}
5980
5981/*
5982 * "inputdialog()" function
5983 */
5984 static void
5985f_inputdialog(typval_T *argvars, typval_T *rettv)
5986{
5987#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005988 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005989 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5990 {
5991 char_u *message;
5992 char_u buf[NUMBUFLEN];
5993 char_u *defstr = (char_u *)"";
5994
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005995 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005996 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005997 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005998 vim_strncpy(IObuff, defstr, IOSIZE - 1);
5999 else
6000 IObuff[0] = NUL;
6001 if (message != NULL && defstr != NULL
6002 && do_dialog(VIM_QUESTION, NULL, message,
6003 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6004 rettv->vval.v_string = vim_strsave(IObuff);
6005 else
6006 {
6007 if (message != NULL && defstr != NULL
6008 && argvars[1].v_type != VAR_UNKNOWN
6009 && argvars[2].v_type != VAR_UNKNOWN)
6010 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006011 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006012 else
6013 rettv->vval.v_string = NULL;
6014 }
6015 rettv->v_type = VAR_STRING;
6016 }
6017 else
6018#endif
6019 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6020}
6021
6022/*
6023 * "inputlist()" function
6024 */
6025 static void
6026f_inputlist(typval_T *argvars, typval_T *rettv)
6027{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006028 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006029 listitem_T *li;
6030 int selected;
6031 int mouse_used;
6032
6033#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006034 // While starting up, there is no place to enter text. When running tests
6035 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006036 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006037 return;
6038#endif
6039 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6040 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006041 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006042 return;
6043 }
6044
6045 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006046 msg_row = Rows - 1; // for when 'cmdheight' > 1
6047 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006048 msg_scroll = TRUE;
6049 msg_clr_eos();
6050
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006051 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006052 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006053 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006054 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006055 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006056 msg_putchar('\n');
6057 }
6058
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006059 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006060 selected = prompt_for_number(&mouse_used);
6061 if (mouse_used)
6062 selected -= lines_left;
6063
6064 rettv->vval.v_number = selected;
6065}
6066
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006067static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6068
6069/*
6070 * "inputrestore()" function
6071 */
6072 static void
6073f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6074{
6075 if (ga_userinput.ga_len > 0)
6076 {
6077 --ga_userinput.ga_len;
6078 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6079 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006080 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006081 }
6082 else if (p_verbose > 1)
6083 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006084 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006085 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006086 }
6087}
6088
6089/*
6090 * "inputsave()" function
6091 */
6092 static void
6093f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6094{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006095 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006096 if (ga_grow(&ga_userinput, 1) == OK)
6097 {
6098 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6099 + ga_userinput.ga_len);
6100 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006101 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006102 }
6103 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006104 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006105}
6106
6107/*
6108 * "inputsecret()" function
6109 */
6110 static void
6111f_inputsecret(typval_T *argvars, typval_T *rettv)
6112{
6113 ++cmdline_star;
6114 ++inputsecret_flag;
6115 f_input(argvars, rettv);
6116 --cmdline_star;
6117 --inputsecret_flag;
6118}
6119
6120/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01006121 * "interrupt()" function
6122 */
6123 static void
6124f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6125{
6126 got_int = TRUE;
6127}
6128
6129/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006130 * "invert(expr)" function
6131 */
6132 static void
6133f_invert(typval_T *argvars, typval_T *rettv)
6134{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006135 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006136}
6137
6138/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006139 * "islocked()" function
6140 */
6141 static void
6142f_islocked(typval_T *argvars, typval_T *rettv)
6143{
6144 lval_T lv;
6145 char_u *end;
6146 dictitem_T *di;
6147
6148 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006149 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006150 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006151 if (end != NULL && lv.ll_name != NULL)
6152 {
6153 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02006154 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006155 else
6156 {
6157 if (lv.ll_tv == NULL)
6158 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006159 di = find_var(lv.ll_name, NULL, TRUE);
6160 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006161 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006162 // Consider a variable locked when:
6163 // 1. the variable itself is locked
6164 // 2. the value of the variable is locked.
6165 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01006166 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6167 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006168 }
6169 }
6170 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006171 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006172 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006173 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006174 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006175 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006176 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6177 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006178 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006179 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6180 }
6181 }
6182
6183 clear_lval(&lv);
6184}
6185
6186#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6187/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006188 * "isinf()" function
6189 */
6190 static void
6191f_isinf(typval_T *argvars, typval_T *rettv)
6192{
6193 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6194 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6195}
6196
6197/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006198 * "isnan()" function
6199 */
6200 static void
6201f_isnan(typval_T *argvars, typval_T *rettv)
6202{
6203 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6204 && isnan(argvars[0].vval.v_float);
6205}
6206#endif
6207
6208/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006209 * "last_buffer_nr()" function.
6210 */
6211 static void
6212f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6213{
6214 int n = 0;
6215 buf_T *buf;
6216
Bram Moolenaar29323592016-07-24 22:04:11 +02006217 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006218 if (n < buf->b_fnum)
6219 n = buf->b_fnum;
6220
6221 rettv->vval.v_number = n;
6222}
6223
6224/*
6225 * "len()" function
6226 */
6227 static void
6228f_len(typval_T *argvars, typval_T *rettv)
6229{
6230 switch (argvars[0].v_type)
6231 {
6232 case VAR_STRING:
6233 case VAR_NUMBER:
6234 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006235 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006236 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006237 case VAR_BLOB:
6238 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6239 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006240 case VAR_LIST:
6241 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6242 break;
6243 case VAR_DICT:
6244 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6245 break;
6246 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006247 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006248 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006249 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006250 case VAR_SPECIAL:
6251 case VAR_FLOAT:
6252 case VAR_FUNC:
6253 case VAR_PARTIAL:
6254 case VAR_JOB:
6255 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006256 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006257 break;
6258 }
6259}
6260
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006261 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006262libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006263{
6264#ifdef FEAT_LIBCALL
6265 char_u *string_in;
6266 char_u **string_result;
6267 int nr_result;
6268#endif
6269
6270 rettv->v_type = type;
6271 if (type != VAR_NUMBER)
6272 rettv->vval.v_string = NULL;
6273
6274 if (check_restricted() || check_secure())
6275 return;
6276
6277#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006278 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006279 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6280 {
6281 string_in = NULL;
6282 if (argvars[2].v_type == VAR_STRING)
6283 string_in = argvars[2].vval.v_string;
6284 if (type == VAR_NUMBER)
6285 string_result = NULL;
6286 else
6287 string_result = &rettv->vval.v_string;
6288 if (mch_libcall(argvars[0].vval.v_string,
6289 argvars[1].vval.v_string,
6290 string_in,
6291 argvars[2].vval.v_number,
6292 string_result,
6293 &nr_result) == OK
6294 && type == VAR_NUMBER)
6295 rettv->vval.v_number = nr_result;
6296 }
6297#endif
6298}
6299
6300/*
6301 * "libcall()" function
6302 */
6303 static void
6304f_libcall(typval_T *argvars, typval_T *rettv)
6305{
6306 libcall_common(argvars, rettv, VAR_STRING);
6307}
6308
6309/*
6310 * "libcallnr()" function
6311 */
6312 static void
6313f_libcallnr(typval_T *argvars, typval_T *rettv)
6314{
6315 libcall_common(argvars, rettv, VAR_NUMBER);
6316}
6317
6318/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006319 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006320 */
6321 static void
6322f_line(typval_T *argvars, typval_T *rettv)
6323{
6324 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006325 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006326 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006327 int id;
6328 tabpage_T *tp;
6329 win_T *wp;
6330 win_T *save_curwin;
6331 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006332
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006333 if (argvars[1].v_type != VAR_UNKNOWN)
6334 {
6335 // use window specified in the second argument
6336 id = (int)tv_get_number(&argvars[1]);
6337 wp = win_id2wp_tp(id, &tp);
6338 if (wp != NULL && tp != NULL)
6339 {
6340 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6341 == OK)
6342 {
6343 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006344 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006345 }
6346 restore_win_noblock(save_curwin, save_curtab, TRUE);
6347 }
6348 }
6349 else
6350 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006351 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006352
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006353 if (fp != NULL)
6354 lnum = fp->lnum;
6355 rettv->vval.v_number = lnum;
6356}
6357
6358/*
6359 * "line2byte(lnum)" function
6360 */
6361 static void
6362f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6363{
6364#ifndef FEAT_BYTEOFF
6365 rettv->vval.v_number = -1;
6366#else
6367 linenr_T lnum;
6368
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006369 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006370 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6371 rettv->vval.v_number = -1;
6372 else
6373 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6374 if (rettv->vval.v_number >= 0)
6375 ++rettv->vval.v_number;
6376#endif
6377}
6378
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006379#ifdef FEAT_FLOAT
6380/*
6381 * "log()" function
6382 */
6383 static void
6384f_log(typval_T *argvars, typval_T *rettv)
6385{
6386 float_T f = 0.0;
6387
6388 rettv->v_type = VAR_FLOAT;
6389 if (get_float_arg(argvars, &f) == OK)
6390 rettv->vval.v_float = log(f);
6391 else
6392 rettv->vval.v_float = 0.0;
6393}
6394
6395/*
6396 * "log10()" function
6397 */
6398 static void
6399f_log10(typval_T *argvars, typval_T *rettv)
6400{
6401 float_T f = 0.0;
6402
6403 rettv->v_type = VAR_FLOAT;
6404 if (get_float_arg(argvars, &f) == OK)
6405 rettv->vval.v_float = log10(f);
6406 else
6407 rettv->vval.v_float = 0.0;
6408}
6409#endif
6410
6411#ifdef FEAT_LUA
6412/*
6413 * "luaeval()" function
6414 */
6415 static void
6416f_luaeval(typval_T *argvars, typval_T *rettv)
6417{
6418 char_u *str;
6419 char_u buf[NUMBUFLEN];
6420
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006421 if (check_restricted() || check_secure())
6422 return;
6423
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006424 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006425 do_luaeval(str, argvars + 1, rettv);
6426}
6427#endif
6428
6429/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006430 * "maparg()" function
6431 */
6432 static void
6433f_maparg(typval_T *argvars, typval_T *rettv)
6434{
6435 get_maparg(argvars, rettv, TRUE);
6436}
6437
6438/*
6439 * "mapcheck()" function
6440 */
6441 static void
6442f_mapcheck(typval_T *argvars, typval_T *rettv)
6443{
6444 get_maparg(argvars, rettv, FALSE);
6445}
6446
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006447typedef enum
6448{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006449 MATCH_END, // matchend()
6450 MATCH_MATCH, // match()
6451 MATCH_STR, // matchstr()
6452 MATCH_LIST, // matchlist()
6453 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006454} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006455
6456 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006457find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006458{
6459 char_u *str = NULL;
6460 long len = 0;
6461 char_u *expr = NULL;
6462 char_u *pat;
6463 regmatch_T regmatch;
6464 char_u patbuf[NUMBUFLEN];
6465 char_u strbuf[NUMBUFLEN];
6466 char_u *save_cpo;
6467 long start = 0;
6468 long nth = 1;
6469 colnr_T startcol = 0;
6470 int match = 0;
6471 list_T *l = NULL;
6472 listitem_T *li = NULL;
6473 long idx = 0;
6474 char_u *tofree = NULL;
6475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006476 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006477 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006478 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006479
6480 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006481 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006482 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006483 // type MATCH_LIST: return empty list when there are no matches.
6484 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006485 if (rettv_list_alloc(rettv) == FAIL)
6486 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006487 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006488 && (list_append_string(rettv->vval.v_list,
6489 (char_u *)"", 0) == FAIL
6490 || list_append_number(rettv->vval.v_list,
6491 (varnumber_T)-1) == FAIL
6492 || list_append_number(rettv->vval.v_list,
6493 (varnumber_T)-1) == FAIL
6494 || list_append_number(rettv->vval.v_list,
6495 (varnumber_T)-1) == FAIL))
6496 {
6497 list_free(rettv->vval.v_list);
6498 rettv->vval.v_list = NULL;
6499 goto theend;
6500 }
6501 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006502 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006503 {
6504 rettv->v_type = VAR_STRING;
6505 rettv->vval.v_string = NULL;
6506 }
6507
6508 if (argvars[0].v_type == VAR_LIST)
6509 {
6510 if ((l = argvars[0].vval.v_list) == NULL)
6511 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006512 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006513 li = l->lv_first;
6514 }
6515 else
6516 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006517 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006518 len = (long)STRLEN(str);
6519 }
6520
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006521 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006522 if (pat == NULL)
6523 goto theend;
6524
6525 if (argvars[2].v_type != VAR_UNKNOWN)
6526 {
6527 int error = FALSE;
6528
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006529 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006530 if (error)
6531 goto theend;
6532 if (l != NULL)
6533 {
6534 li = list_find(l, start);
6535 if (li == NULL)
6536 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006537 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006538 }
6539 else
6540 {
6541 if (start < 0)
6542 start = 0;
6543 if (start > len)
6544 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006545 // When "count" argument is there ignore matches before "start",
6546 // otherwise skip part of the string. Differs when pattern is "^"
6547 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006548 if (argvars[3].v_type != VAR_UNKNOWN)
6549 startcol = start;
6550 else
6551 {
6552 str += start;
6553 len -= start;
6554 }
6555 }
6556
6557 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006558 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006559 if (error)
6560 goto theend;
6561 }
6562
6563 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6564 if (regmatch.regprog != NULL)
6565 {
6566 regmatch.rm_ic = p_ic;
6567
6568 for (;;)
6569 {
6570 if (l != NULL)
6571 {
6572 if (li == NULL)
6573 {
6574 match = FALSE;
6575 break;
6576 }
6577 vim_free(tofree);
6578 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6579 if (str == NULL)
6580 break;
6581 }
6582
6583 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6584
6585 if (match && --nth <= 0)
6586 break;
6587 if (l == NULL && !match)
6588 break;
6589
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006590 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006591 if (l != NULL)
6592 {
6593 li = li->li_next;
6594 ++idx;
6595 }
6596 else
6597 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006598 startcol = (colnr_T)(regmatch.startp[0]
6599 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006600 if (startcol > (colnr_T)len
6601 || str + startcol <= regmatch.startp[0])
6602 {
6603 match = FALSE;
6604 break;
6605 }
6606 }
6607 }
6608
6609 if (match)
6610 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006611 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006612 {
6613 listitem_T *li1 = rettv->vval.v_list->lv_first;
6614 listitem_T *li2 = li1->li_next;
6615 listitem_T *li3 = li2->li_next;
6616 listitem_T *li4 = li3->li_next;
6617
6618 vim_free(li1->li_tv.vval.v_string);
6619 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006620 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006621 li3->li_tv.vval.v_number =
6622 (varnumber_T)(regmatch.startp[0] - expr);
6623 li4->li_tv.vval.v_number =
6624 (varnumber_T)(regmatch.endp[0] - expr);
6625 if (l != NULL)
6626 li2->li_tv.vval.v_number = (varnumber_T)idx;
6627 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006628 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006629 {
6630 int i;
6631
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006632 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006633 for (i = 0; i < NSUBEXP; ++i)
6634 {
6635 if (regmatch.endp[i] == NULL)
6636 {
6637 if (list_append_string(rettv->vval.v_list,
6638 (char_u *)"", 0) == FAIL)
6639 break;
6640 }
6641 else if (list_append_string(rettv->vval.v_list,
6642 regmatch.startp[i],
6643 (int)(regmatch.endp[i] - regmatch.startp[i]))
6644 == FAIL)
6645 break;
6646 }
6647 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006648 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006649 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006650 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006651 if (l != NULL)
6652 copy_tv(&li->li_tv, rettv);
6653 else
6654 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006655 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006656 }
6657 else if (l != NULL)
6658 rettv->vval.v_number = idx;
6659 else
6660 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006661 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006662 rettv->vval.v_number =
6663 (varnumber_T)(regmatch.startp[0] - str);
6664 else
6665 rettv->vval.v_number =
6666 (varnumber_T)(regmatch.endp[0] - str);
6667 rettv->vval.v_number += (varnumber_T)(str - expr);
6668 }
6669 }
6670 vim_regfree(regmatch.regprog);
6671 }
6672
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006673theend:
6674 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006675 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006676 listitem_remove(rettv->vval.v_list,
6677 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006678 vim_free(tofree);
6679 p_cpo = save_cpo;
6680}
6681
6682/*
6683 * "match()" function
6684 */
6685 static void
6686f_match(typval_T *argvars, typval_T *rettv)
6687{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006688 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006689}
6690
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006691/*
6692 * "matchend()" function
6693 */
6694 static void
6695f_matchend(typval_T *argvars, typval_T *rettv)
6696{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006697 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006698}
6699
6700/*
6701 * "matchlist()" function
6702 */
6703 static void
6704f_matchlist(typval_T *argvars, typval_T *rettv)
6705{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006706 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006707}
6708
6709/*
6710 * "matchstr()" function
6711 */
6712 static void
6713f_matchstr(typval_T *argvars, typval_T *rettv)
6714{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006715 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006716}
6717
6718/*
6719 * "matchstrpos()" function
6720 */
6721 static void
6722f_matchstrpos(typval_T *argvars, typval_T *rettv)
6723{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006724 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006725}
6726
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006727 static void
6728max_min(typval_T *argvars, typval_T *rettv, int domax)
6729{
6730 varnumber_T n = 0;
6731 varnumber_T i;
6732 int error = FALSE;
6733
6734 if (argvars[0].v_type == VAR_LIST)
6735 {
6736 list_T *l;
6737 listitem_T *li;
6738
6739 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006740 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006741 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006742 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006743 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006744 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6745 n = l->lv_u.nonmat.lv_start;
6746 else
6747 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6748 * l->lv_u.nonmat.lv_stride;
6749 }
6750 else
6751 {
6752 li = l->lv_first;
6753 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006754 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006755 n = tv_get_number_chk(&li->li_tv, &error);
6756 for (;;)
6757 {
6758 li = li->li_next;
6759 if (li == NULL)
6760 break;
6761 i = tv_get_number_chk(&li->li_tv, &error);
6762 if (domax ? i > n : i < n)
6763 n = i;
6764 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006765 }
6766 }
6767 }
6768 }
6769 else if (argvars[0].v_type == VAR_DICT)
6770 {
6771 dict_T *d;
6772 int first = TRUE;
6773 hashitem_T *hi;
6774 int todo;
6775
6776 d = argvars[0].vval.v_dict;
6777 if (d != NULL)
6778 {
6779 todo = (int)d->dv_hashtab.ht_used;
6780 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6781 {
6782 if (!HASHITEM_EMPTY(hi))
6783 {
6784 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006785 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006786 if (first)
6787 {
6788 n = i;
6789 first = FALSE;
6790 }
6791 else if (domax ? i > n : i < n)
6792 n = i;
6793 }
6794 }
6795 }
6796 }
6797 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006798 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006799 rettv->vval.v_number = error ? 0 : n;
6800}
6801
6802/*
6803 * "max()" function
6804 */
6805 static void
6806f_max(typval_T *argvars, typval_T *rettv)
6807{
6808 max_min(argvars, rettv, TRUE);
6809}
6810
6811/*
6812 * "min()" function
6813 */
6814 static void
6815f_min(typval_T *argvars, typval_T *rettv)
6816{
6817 max_min(argvars, rettv, FALSE);
6818}
6819
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006820#if defined(FEAT_MZSCHEME) || defined(PROTO)
6821/*
6822 * "mzeval()" function
6823 */
6824 static void
6825f_mzeval(typval_T *argvars, typval_T *rettv)
6826{
6827 char_u *str;
6828 char_u buf[NUMBUFLEN];
6829
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006830 if (check_restricted() || check_secure())
6831 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006832 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006833 do_mzeval(str, rettv);
6834}
6835
6836 void
6837mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6838{
6839 typval_T argvars[3];
6840
6841 argvars[0].v_type = VAR_STRING;
6842 argvars[0].vval.v_string = name;
6843 copy_tv(args, &argvars[1]);
6844 argvars[2].v_type = VAR_UNKNOWN;
6845 f_call(argvars, rettv);
6846 clear_tv(&argvars[1]);
6847}
6848#endif
6849
6850/*
6851 * "nextnonblank()" function
6852 */
6853 static void
6854f_nextnonblank(typval_T *argvars, typval_T *rettv)
6855{
6856 linenr_T lnum;
6857
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006858 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006859 {
6860 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6861 {
6862 lnum = 0;
6863 break;
6864 }
6865 if (*skipwhite(ml_get(lnum)) != NUL)
6866 break;
6867 }
6868 rettv->vval.v_number = lnum;
6869}
6870
6871/*
6872 * "nr2char()" function
6873 */
6874 static void
6875f_nr2char(typval_T *argvars, typval_T *rettv)
6876{
6877 char_u buf[NUMBUFLEN];
6878
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006879 if (has_mbyte)
6880 {
6881 int utf8 = 0;
6882
6883 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006884 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006885 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006886 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006887 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006888 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006889 }
6890 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006891 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006892 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006893 buf[1] = NUL;
6894 }
6895 rettv->v_type = VAR_STRING;
6896 rettv->vval.v_string = vim_strsave(buf);
6897}
6898
6899/*
6900 * "or(expr, expr)" function
6901 */
6902 static void
6903f_or(typval_T *argvars, typval_T *rettv)
6904{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006905 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6906 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006907}
6908
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006909#ifdef FEAT_PERL
6910/*
6911 * "perleval()" function
6912 */
6913 static void
6914f_perleval(typval_T *argvars, typval_T *rettv)
6915{
6916 char_u *str;
6917 char_u buf[NUMBUFLEN];
6918
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006919 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006920 do_perleval(str, rettv);
6921}
6922#endif
6923
6924#ifdef FEAT_FLOAT
6925/*
6926 * "pow()" function
6927 */
6928 static void
6929f_pow(typval_T *argvars, typval_T *rettv)
6930{
6931 float_T fx = 0.0, fy = 0.0;
6932
6933 rettv->v_type = VAR_FLOAT;
6934 if (get_float_arg(argvars, &fx) == OK
6935 && get_float_arg(&argvars[1], &fy) == OK)
6936 rettv->vval.v_float = pow(fx, fy);
6937 else
6938 rettv->vval.v_float = 0.0;
6939}
6940#endif
6941
6942/*
6943 * "prevnonblank()" function
6944 */
6945 static void
6946f_prevnonblank(typval_T *argvars, typval_T *rettv)
6947{
6948 linenr_T lnum;
6949
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006950 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006951 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6952 lnum = 0;
6953 else
6954 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6955 --lnum;
6956 rettv->vval.v_number = lnum;
6957}
6958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006959// This dummy va_list is here because:
6960// - passing a NULL pointer doesn't work when va_list isn't a pointer
6961// - locally in the function results in a "used before set" warning
6962// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006963static va_list ap;
6964
6965/*
6966 * "printf()" function
6967 */
6968 static void
6969f_printf(typval_T *argvars, typval_T *rettv)
6970{
6971 char_u buf[NUMBUFLEN];
6972 int len;
6973 char_u *s;
6974 int saved_did_emsg = did_emsg;
6975 char *fmt;
6976
6977 rettv->v_type = VAR_STRING;
6978 rettv->vval.v_string = NULL;
6979
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006980 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006981 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006982 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006983 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006984 if (!did_emsg)
6985 {
6986 s = alloc(len + 1);
6987 if (s != NULL)
6988 {
6989 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006990 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
6991 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006992 }
6993 }
6994 did_emsg |= saved_did_emsg;
6995}
6996
6997/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006998 * "pum_getpos()" function
6999 */
7000 static void
7001f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7002{
7003 if (rettv_dict_alloc(rettv) != OK)
7004 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007005 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007006}
7007
7008/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007009 * "pumvisible()" function
7010 */
7011 static void
7012f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7013{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007014 if (pum_visible())
7015 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007016}
7017
7018#ifdef FEAT_PYTHON3
7019/*
7020 * "py3eval()" function
7021 */
7022 static void
7023f_py3eval(typval_T *argvars, typval_T *rettv)
7024{
7025 char_u *str;
7026 char_u buf[NUMBUFLEN];
7027
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007028 if (check_restricted() || check_secure())
7029 return;
7030
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007031 if (p_pyx == 0)
7032 p_pyx = 3;
7033
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007034 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007035 do_py3eval(str, rettv);
7036}
7037#endif
7038
7039#ifdef FEAT_PYTHON
7040/*
7041 * "pyeval()" function
7042 */
7043 static void
7044f_pyeval(typval_T *argvars, typval_T *rettv)
7045{
7046 char_u *str;
7047 char_u buf[NUMBUFLEN];
7048
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007049 if (check_restricted() || check_secure())
7050 return;
7051
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007052 if (p_pyx == 0)
7053 p_pyx = 2;
7054
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007055 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007056 do_pyeval(str, rettv);
7057}
7058#endif
7059
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007060#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
7061/*
7062 * "pyxeval()" function
7063 */
7064 static void
7065f_pyxeval(typval_T *argvars, typval_T *rettv)
7066{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007067 if (check_restricted() || check_secure())
7068 return;
7069
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007070# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
7071 init_pyxversion();
7072 if (p_pyx == 2)
7073 f_pyeval(argvars, rettv);
7074 else
7075 f_py3eval(argvars, rettv);
7076# elif defined(FEAT_PYTHON)
7077 f_pyeval(argvars, rettv);
7078# elif defined(FEAT_PYTHON3)
7079 f_py3eval(argvars, rettv);
7080# endif
7081}
7082#endif
7083
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007084static UINT32_T srand_seed_for_testing = 0;
7085static int srand_seed_for_testing_is_used = FALSE;
7086
7087 static void
7088f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
7089{
7090 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007091 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007092 else
7093 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007094 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
7095 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007096 }
7097}
7098
7099 static void
7100init_srand(UINT32_T *x)
7101{
7102#ifndef MSWIN
7103 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
7104#endif
7105
7106 if (srand_seed_for_testing_is_used)
7107 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007108 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007109 return;
7110 }
7111#ifndef MSWIN
7112 if (dev_urandom_state != FAIL)
7113 {
7114 int fd = open("/dev/urandom", O_RDONLY);
7115 struct {
7116 union {
7117 UINT32_T number;
7118 char bytes[sizeof(UINT32_T)];
7119 } contents;
7120 } buf;
7121
7122 // Attempt reading /dev/urandom.
7123 if (fd == -1)
7124 dev_urandom_state = FAIL;
7125 else
7126 {
7127 buf.contents.number = 0;
7128 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
7129 != sizeof(UINT32_T))
7130 dev_urandom_state = FAIL;
7131 else
7132 {
7133 dev_urandom_state = OK;
7134 *x = buf.contents.number;
7135 }
7136 close(fd);
7137 }
7138 }
7139 if (dev_urandom_state != OK)
7140 // Reading /dev/urandom doesn't work, fall back to time().
7141#endif
7142 *x = vim_time();
7143}
7144
7145#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
7146#define SPLITMIX32(x, z) ( \
7147 z = (x += 0x9e3779b9), \
7148 z = (z ^ (z >> 16)) * 0x85ebca6b, \
7149 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
7150 z ^ (z >> 16) \
7151 )
7152#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
7153 result = ROTL(y * 5, 7) * 9; \
7154 t = y << 9; \
7155 z ^= x; \
7156 w ^= y; \
7157 y ^= z, x ^= w; \
7158 z ^= t; \
7159 w = ROTL(w, 11);
7160
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007161/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007162 * "rand()" function
7163 */
7164 static void
7165f_rand(typval_T *argvars, typval_T *rettv)
7166{
7167 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007168 static UINT32_T gx, gy, gz, gw;
7169 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007170 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01007171 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007172
7173 if (argvars[0].v_type == VAR_UNKNOWN)
7174 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007175 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007176 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007177 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007178 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007179 init_srand(&x);
7180
7181 gx = SPLITMIX32(x, z);
7182 gy = SPLITMIX32(x, z);
7183 gz = SPLITMIX32(x, z);
7184 gw = SPLITMIX32(x, z);
7185 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007186 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007187
7188 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007189 }
7190 else if (argvars[0].v_type == VAR_LIST)
7191 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007192 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007193 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007194 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007195
7196 lx = list_find(l, 0L);
7197 ly = list_find(l, 1L);
7198 lz = list_find(l, 2L);
7199 lw = list_find(l, 3L);
7200 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
7201 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
7202 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
7203 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
7204 x = (UINT32_T)lx->li_tv.vval.v_number;
7205 y = (UINT32_T)ly->li_tv.vval.v_number;
7206 z = (UINT32_T)lz->li_tv.vval.v_number;
7207 w = (UINT32_T)lw->li_tv.vval.v_number;
7208
7209 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
7210
7211 lx->li_tv.vval.v_number = (varnumber_T)x;
7212 ly->li_tv.vval.v_number = (varnumber_T)y;
7213 lz->li_tv.vval.v_number = (varnumber_T)z;
7214 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007215 }
7216 else
7217 goto theend;
7218
7219 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007220 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007221 return;
7222
7223theend:
7224 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007225 rettv->v_type = VAR_NUMBER;
7226 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007227}
7228
7229/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007230 * "srand()" function
7231 */
7232 static void
7233f_srand(typval_T *argvars, typval_T *rettv)
7234{
7235 UINT32_T x = 0, z;
7236
7237 if (rettv_list_alloc(rettv) == FAIL)
7238 return;
7239 if (argvars[0].v_type == VAR_UNKNOWN)
7240 {
7241 init_srand(&x);
7242 }
7243 else
7244 {
7245 int error = FALSE;
7246
7247 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7248 if (error)
7249 return;
7250 }
7251
7252 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7253 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7254 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7255 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7256}
7257
7258#undef ROTL
7259#undef SPLITMIX32
7260#undef SHUFFLE_XOSHIRO128STARSTAR
7261
7262/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007263 * "range()" function
7264 */
7265 static void
7266f_range(typval_T *argvars, typval_T *rettv)
7267{
7268 varnumber_T start;
7269 varnumber_T end;
7270 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007271 int error = FALSE;
7272
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007273 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007274 if (argvars[1].v_type == VAR_UNKNOWN)
7275 {
7276 end = start - 1;
7277 start = 0;
7278 }
7279 else
7280 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007281 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007282 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007283 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007284 }
7285
7286 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007287 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007288 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007289 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007290 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007291 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007292 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007293 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007294 list_T *list = rettv->vval.v_list;
7295
7296 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007297 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007298 // be called.
7299 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007300 list->lv_u.nonmat.lv_start = start;
7301 list->lv_u.nonmat.lv_end = end;
7302 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007303 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007304 }
7305}
7306
7307/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007308 * Materialize "list".
7309 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310 */
7311 void
7312range_list_materialize(list_T *list)
7313{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007314 varnumber_T start = list->lv_u.nonmat.lv_start;
7315 varnumber_T end = list->lv_u.nonmat.lv_end;
7316 int stride = list->lv_u.nonmat.lv_stride;
7317 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007318
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007319 list->lv_first = NULL;
7320 list->lv_u.mat.lv_last = NULL;
7321 list->lv_len = 0;
7322 list->lv_u.mat.lv_idx_item = NULL;
7323 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7324 if (list_append_number(list, (varnumber_T)i) == FAIL)
7325 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007326}
7327
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007328/*
7329 * "getreginfo()" function
7330 */
7331 static void
7332f_getreginfo(typval_T *argvars, typval_T *rettv)
7333{
7334 char_u *strregname;
7335 int regname;
7336 char_u buf[NUMBUFLEN + 2];
7337 long reglen = 0;
7338 dict_T *dict;
7339 list_T *list;
7340
7341 if (argvars[0].v_type != VAR_UNKNOWN)
7342 {
7343 strregname = tv_get_string_chk(&argvars[0]);
7344 if (strregname == NULL)
7345 return;
7346 }
7347 else
7348 strregname = get_vim_var_str(VV_REG);
7349
7350 regname = (strregname == NULL ? '"' : *strregname);
7351 if (regname == 0 || regname == '@')
7352 regname = '"';
7353
7354 if (rettv_dict_alloc(rettv) == FAIL)
7355 return;
7356 dict = rettv->vval.v_dict;
7357
7358 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7359 if (list == NULL)
7360 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007361 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007362
7363 buf[0] = NUL;
7364 buf[1] = NUL;
7365 switch (get_reg_type(regname, &reglen))
7366 {
7367 case MLINE: buf[0] = 'V'; break;
7368 case MCHAR: buf[0] = 'v'; break;
7369 case MBLOCK:
7370 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7371 reglen + 1);
7372 break;
7373 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007374 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007375
7376 buf[0] = get_register_name(get_unname_register());
7377 buf[1] = NUL;
7378 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007379 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007380 else
7381 {
7382 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7383
7384 if (item != NULL)
7385 {
7386 item->di_tv.v_type = VAR_SPECIAL;
7387 item->di_tv.vval.v_number = regname == buf[0]
7388 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007389 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007390 }
7391 }
7392}
7393
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007394 static void
7395return_register(int regname, typval_T *rettv)
7396{
7397 char_u buf[2] = {0, 0};
7398
7399 buf[0] = (char_u)regname;
7400 rettv->v_type = VAR_STRING;
7401 rettv->vval.v_string = vim_strsave(buf);
7402}
7403
7404/*
7405 * "reg_executing()" function
7406 */
7407 static void
7408f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7409{
7410 return_register(reg_executing, rettv);
7411}
7412
7413/*
7414 * "reg_recording()" function
7415 */
7416 static void
7417f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7418{
7419 return_register(reg_recording, rettv);
7420}
7421
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007422/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007423 * "rename({from}, {to})" function
7424 */
7425 static void
7426f_rename(typval_T *argvars, typval_T *rettv)
7427{
7428 char_u buf[NUMBUFLEN];
7429
7430 if (check_restricted() || check_secure())
7431 rettv->vval.v_number = -1;
7432 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007433 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7434 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007435}
7436
7437/*
7438 * "repeat()" function
7439 */
7440 static void
7441f_repeat(typval_T *argvars, typval_T *rettv)
7442{
7443 char_u *p;
7444 int n;
7445 int slen;
7446 int len;
7447 char_u *r;
7448 int i;
7449
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007450 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007451 if (argvars[0].v_type == VAR_LIST)
7452 {
7453 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7454 while (n-- > 0)
7455 if (list_extend(rettv->vval.v_list,
7456 argvars[0].vval.v_list, NULL) == FAIL)
7457 break;
7458 }
7459 else
7460 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007461 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007462 rettv->v_type = VAR_STRING;
7463 rettv->vval.v_string = NULL;
7464
7465 slen = (int)STRLEN(p);
7466 len = slen * n;
7467 if (len <= 0)
7468 return;
7469
7470 r = alloc(len + 1);
7471 if (r != NULL)
7472 {
7473 for (i = 0; i < n; i++)
7474 mch_memmove(r + i * slen, p, (size_t)slen);
7475 r[len] = NUL;
7476 }
7477
7478 rettv->vval.v_string = r;
7479 }
7480}
7481
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007482#define SP_NOMOVE 0x01 // don't move cursor
7483#define SP_REPEAT 0x02 // repeat to find outer pair
7484#define SP_RETCOUNT 0x04 // return matchcount
7485#define SP_SETPCMARK 0x08 // set previous context mark
7486#define SP_START 0x10 // accept match at start position
7487#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7488#define SP_END 0x40 // leave cursor at end of match
7489#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007490
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007491/*
7492 * Get flags for a search function.
7493 * Possibly sets "p_ws".
7494 * Returns BACKWARD, FORWARD or zero (for an error).
7495 */
7496 static int
7497get_search_arg(typval_T *varp, int *flagsp)
7498{
7499 int dir = FORWARD;
7500 char_u *flags;
7501 char_u nbuf[NUMBUFLEN];
7502 int mask;
7503
7504 if (varp->v_type != VAR_UNKNOWN)
7505 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007506 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007507 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007508 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007509 while (*flags != NUL)
7510 {
7511 switch (*flags)
7512 {
7513 case 'b': dir = BACKWARD; break;
7514 case 'w': p_ws = TRUE; break;
7515 case 'W': p_ws = FALSE; break;
7516 default: mask = 0;
7517 if (flagsp != NULL)
7518 switch (*flags)
7519 {
7520 case 'c': mask = SP_START; break;
7521 case 'e': mask = SP_END; break;
7522 case 'm': mask = SP_RETCOUNT; break;
7523 case 'n': mask = SP_NOMOVE; break;
7524 case 'p': mask = SP_SUBPAT; break;
7525 case 'r': mask = SP_REPEAT; break;
7526 case 's': mask = SP_SETPCMARK; break;
7527 case 'z': mask = SP_COLUMN; break;
7528 }
7529 if (mask == 0)
7530 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007531 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007532 dir = 0;
7533 }
7534 else
7535 *flagsp |= mask;
7536 }
7537 if (dir == 0)
7538 break;
7539 ++flags;
7540 }
7541 }
7542 return dir;
7543}
7544
7545/*
7546 * Shared by search() and searchpos() functions.
7547 */
7548 static int
7549search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7550{
7551 int flags;
7552 char_u *pat;
7553 pos_T pos;
7554 pos_T save_cursor;
7555 int save_p_ws = p_ws;
7556 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007557 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007558 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007559#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007560 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007561 long time_limit = 0;
7562#endif
7563 int options = SEARCH_KEEP;
7564 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007565 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007566 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007567 pos_T firstpos;
7568
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007569 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007570 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007571 if (dir == 0)
7572 goto theend;
7573 flags = *flagsp;
7574 if (flags & SP_START)
7575 options |= SEARCH_START;
7576 if (flags & SP_END)
7577 options |= SEARCH_END;
7578 if (flags & SP_COLUMN)
7579 options |= SEARCH_COL;
7580
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007581 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007582 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7583 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007584 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007585 if (lnum_stop < 0)
7586 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007587 if (argvars[3].v_type != VAR_UNKNOWN)
7588 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007589#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007590 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007591 if (time_limit < 0)
7592 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007593#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007594 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007595 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007596 }
7597
7598#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007599 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007600 profile_setlimit(time_limit, &tm);
7601#endif
7602
7603 /*
7604 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7605 * Check to make sure only those flags are set.
7606 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7607 * flags cannot be set. Check for that condition also.
7608 */
7609 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7610 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7611 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007612 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007613 goto theend;
7614 }
7615
7616 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007617 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007618 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007619 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7620#ifdef FEAT_RELTIME
7621 sia.sa_tm = &tm;
7622#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007623
7624 // Repeat until {skip} returns FALSE.
7625 for (;;)
7626 {
7627 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007628 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007629 // finding the first match again means there is no match where {skip}
7630 // evaluates to zero.
7631 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7632 subpatnum = FAIL;
7633
Bram Moolenaara9c01042020-06-07 14:50:50 +02007634 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007635 // didn't find it or no skip argument
7636 break;
7637 firstpos = pos;
7638
Bram Moolenaara9c01042020-06-07 14:50:50 +02007639 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007640 {
7641 int do_skip;
7642 int err;
7643 pos_T save_pos = curwin->w_cursor;
7644
7645 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007646 err = FALSE;
7647 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007648 curwin->w_cursor = save_pos;
7649 if (err)
7650 {
7651 // Evaluating {skip} caused an error, break here.
7652 subpatnum = FAIL;
7653 break;
7654 }
7655 if (!do_skip)
7656 break;
7657 }
7658 }
7659
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007660 if (subpatnum != FAIL)
7661 {
7662 if (flags & SP_SUBPAT)
7663 retval = subpatnum;
7664 else
7665 retval = pos.lnum;
7666 if (flags & SP_SETPCMARK)
7667 setpcmark();
7668 curwin->w_cursor = pos;
7669 if (match_pos != NULL)
7670 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007671 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007672 match_pos->lnum = pos.lnum;
7673 match_pos->col = pos.col + 1;
7674 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007675 // "/$" will put the cursor after the end of the line, may need to
7676 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007677 check_cursor();
7678 }
7679
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007680 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007681 if (flags & SP_NOMOVE)
7682 curwin->w_cursor = save_cursor;
7683 else
7684 curwin->w_set_curswant = TRUE;
7685theend:
7686 p_ws = save_p_ws;
7687
7688 return retval;
7689}
7690
7691#ifdef FEAT_FLOAT
7692
7693/*
7694 * round() is not in C90, use ceil() or floor() instead.
7695 */
7696 float_T
7697vim_round(float_T f)
7698{
7699 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
7700}
7701
7702/*
7703 * "round({float})" function
7704 */
7705 static void
7706f_round(typval_T *argvars, typval_T *rettv)
7707{
7708 float_T f = 0.0;
7709
7710 rettv->v_type = VAR_FLOAT;
7711 if (get_float_arg(argvars, &f) == OK)
7712 rettv->vval.v_float = vim_round(f);
7713 else
7714 rettv->vval.v_float = 0.0;
7715}
7716#endif
7717
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007718#ifdef FEAT_RUBY
7719/*
7720 * "rubyeval()" function
7721 */
7722 static void
7723f_rubyeval(typval_T *argvars, typval_T *rettv)
7724{
7725 char_u *str;
7726 char_u buf[NUMBUFLEN];
7727
7728 str = tv_get_string_buf(&argvars[0], buf);
7729 do_rubyeval(str, rettv);
7730}
7731#endif
7732
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007733/*
7734 * "screenattr()" function
7735 */
7736 static void
7737f_screenattr(typval_T *argvars, typval_T *rettv)
7738{
7739 int row;
7740 int col;
7741 int c;
7742
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007743 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7744 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007745 if (row < 0 || row >= screen_Rows
7746 || col < 0 || col >= screen_Columns)
7747 c = -1;
7748 else
7749 c = ScreenAttrs[LineOffset[row] + col];
7750 rettv->vval.v_number = c;
7751}
7752
7753/*
7754 * "screenchar()" function
7755 */
7756 static void
7757f_screenchar(typval_T *argvars, typval_T *rettv)
7758{
7759 int row;
7760 int col;
7761 int off;
7762 int c;
7763
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007764 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7765 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007766 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007767 c = -1;
7768 else
7769 {
7770 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007771 if (enc_utf8 && ScreenLinesUC[off] != 0)
7772 c = ScreenLinesUC[off];
7773 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007774 c = ScreenLines[off];
7775 }
7776 rettv->vval.v_number = c;
7777}
7778
7779/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007780 * "screenchars()" function
7781 */
7782 static void
7783f_screenchars(typval_T *argvars, typval_T *rettv)
7784{
7785 int row;
7786 int col;
7787 int off;
7788 int c;
7789 int i;
7790
7791 if (rettv_list_alloc(rettv) == FAIL)
7792 return;
7793 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7794 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7795 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7796 return;
7797
7798 off = LineOffset[row] + col;
7799 if (enc_utf8 && ScreenLinesUC[off] != 0)
7800 c = ScreenLinesUC[off];
7801 else
7802 c = ScreenLines[off];
7803 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7804
7805 if (enc_utf8)
7806
7807 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7808 list_append_number(rettv->vval.v_list,
7809 (varnumber_T)ScreenLinesC[i][off]);
7810}
7811
7812/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007813 * "screencol()" function
7814 *
7815 * First column is 1 to be consistent with virtcol().
7816 */
7817 static void
7818f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7819{
7820 rettv->vval.v_number = screen_screencol() + 1;
7821}
7822
7823/*
7824 * "screenrow()" function
7825 */
7826 static void
7827f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7828{
7829 rettv->vval.v_number = screen_screenrow() + 1;
7830}
7831
7832/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007833 * "screenstring()" function
7834 */
7835 static void
7836f_screenstring(typval_T *argvars, typval_T *rettv)
7837{
7838 int row;
7839 int col;
7840 int off;
7841 int c;
7842 int i;
7843 char_u buf[MB_MAXBYTES + 1];
7844 int buflen = 0;
7845
7846 rettv->vval.v_string = NULL;
7847 rettv->v_type = VAR_STRING;
7848
7849 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7850 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7851 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7852 return;
7853
7854 off = LineOffset[row] + col;
7855 if (enc_utf8 && ScreenLinesUC[off] != 0)
7856 c = ScreenLinesUC[off];
7857 else
7858 c = ScreenLines[off];
7859 buflen += mb_char2bytes(c, buf);
7860
7861 if (enc_utf8)
7862 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7863 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7864
7865 buf[buflen] = NUL;
7866 rettv->vval.v_string = vim_strsave(buf);
7867}
7868
7869/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007870 * "search()" function
7871 */
7872 static void
7873f_search(typval_T *argvars, typval_T *rettv)
7874{
7875 int flags = 0;
7876
7877 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7878}
7879
7880/*
7881 * "searchdecl()" function
7882 */
7883 static void
7884f_searchdecl(typval_T *argvars, typval_T *rettv)
7885{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007886 int locally = TRUE;
7887 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007888 int error = FALSE;
7889 char_u *name;
7890
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007891 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007892
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007893 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007894 if (argvars[1].v_type != VAR_UNKNOWN)
7895 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007896 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007897 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007898 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007899 }
7900 if (!error && name != NULL)
7901 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7902 locally, thisblock, SEARCH_KEEP) == FAIL;
7903}
7904
7905/*
7906 * Used by searchpair() and searchpairpos()
7907 */
7908 static int
7909searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7910{
7911 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007912 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007913 int save_p_ws = p_ws;
7914 int dir;
7915 int flags = 0;
7916 char_u nbuf1[NUMBUFLEN];
7917 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007918 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007919 long lnum_stop = 0;
7920 long time_limit = 0;
7921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007922 // Get the three pattern arguments: start, middle, end. Will result in an
7923 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007924 spat = tv_get_string_chk(&argvars[0]);
7925 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7926 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007927 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007928 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007930 // Handle the optional fourth argument: flags
7931 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007932 if (dir == 0)
7933 goto theend;
7934
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007935 // Don't accept SP_END or SP_SUBPAT.
7936 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007937 if ((flags & (SP_END | SP_SUBPAT)) != 0
7938 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7939 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007940 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007941 goto theend;
7942 }
7943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007944 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007945 if (flags & SP_REPEAT)
7946 p_ws = FALSE;
7947
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007948 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007949 if (argvars[3].v_type == VAR_UNKNOWN
7950 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007951 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007952 else
7953 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007954 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007955 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007956
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007957 if (argvars[5].v_type != VAR_UNKNOWN)
7958 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007959 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007960 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007961 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007962 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007963 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007964 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007965#ifdef FEAT_RELTIME
7966 if (argvars[6].v_type != VAR_UNKNOWN)
7967 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007968 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007969 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007970 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007971 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007972 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007973 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007974 }
7975#endif
7976 }
7977 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007978
7979 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
7980 match_pos, lnum_stop, time_limit);
7981
7982theend:
7983 p_ws = save_p_ws;
7984
7985 return retval;
7986}
7987
7988/*
7989 * "searchpair()" function
7990 */
7991 static void
7992f_searchpair(typval_T *argvars, typval_T *rettv)
7993{
7994 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
7995}
7996
7997/*
7998 * "searchpairpos()" function
7999 */
8000 static void
8001f_searchpairpos(typval_T *argvars, typval_T *rettv)
8002{
8003 pos_T match_pos;
8004 int lnum = 0;
8005 int col = 0;
8006
8007 if (rettv_list_alloc(rettv) == FAIL)
8008 return;
8009
8010 if (searchpair_cmn(argvars, &match_pos) > 0)
8011 {
8012 lnum = match_pos.lnum;
8013 col = match_pos.col;
8014 }
8015
8016 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8017 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8018}
8019
8020/*
8021 * Search for a start/middle/end thing.
8022 * Used by searchpair(), see its documentation for the details.
8023 * Returns 0 or -1 for no match,
8024 */
8025 long
8026do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008027 char_u *spat, // start pattern
8028 char_u *mpat, // middle pattern
8029 char_u *epat, // end pattern
8030 int dir, // BACKWARD or FORWARD
8031 typval_T *skip, // skip expression
8032 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008033 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008034 linenr_T lnum_stop, // stop at this line if not zero
8035 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036{
8037 char_u *save_cpo;
8038 char_u *pat, *pat2 = NULL, *pat3 = NULL;
8039 long retval = 0;
8040 pos_T pos;
8041 pos_T firstpos;
8042 pos_T foundpos;
8043 pos_T save_cursor;
8044 pos_T save_pos;
8045 int n;
8046 int r;
8047 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01008048 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008049 int err;
8050 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008051#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008052 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008053#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008054
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008055 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008056 save_cpo = p_cpo;
8057 p_cpo = empty_option;
8058
8059#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008060 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008061 profile_setlimit(time_limit, &tm);
8062#endif
8063
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008064 // Make two search patterns: start/end (pat2, for in nested pairs) and
8065 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02008066 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
8067 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068 if (pat2 == NULL || pat3 == NULL)
8069 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008070 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008071 if (*mpat == NUL)
8072 STRCPY(pat3, pat2);
8073 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008074 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008075 spat, epat, mpat);
8076 if (flags & SP_START)
8077 options |= SEARCH_START;
8078
Bram Moolenaar48570482017-10-30 21:48:41 +01008079 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02008080 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01008081
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008082 save_cursor = curwin->w_cursor;
8083 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008084 CLEAR_POS(&firstpos);
8085 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008086 pat = pat3;
8087 for (;;)
8088 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008089 searchit_arg_T sia;
8090
Bram Moolenaara80faa82020-04-12 19:37:17 +02008091 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008092 sia.sa_stop_lnum = lnum_stop;
8093#ifdef FEAT_RELTIME
8094 sia.sa_tm = &tm;
8095#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01008096 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008097 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008098 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008099 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008100 break;
8101
8102 if (firstpos.lnum == 0)
8103 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008104 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008105 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008106 // Found the same position again. Can happen with a pattern that
8107 // has "\zs" at the end and searching backwards. Advance one
8108 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008109 if (dir == BACKWARD)
8110 decl(&pos);
8111 else
8112 incl(&pos);
8113 }
8114 foundpos = pos;
8115
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008116 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008117 options &= ~SEARCH_START;
8118
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008119 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01008120 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008121 {
8122 save_pos = curwin->w_cursor;
8123 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01008124 err = FALSE;
8125 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008126 curwin->w_cursor = save_pos;
8127 if (err)
8128 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008129 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008130 curwin->w_cursor = save_cursor;
8131 retval = -1;
8132 break;
8133 }
8134 if (r)
8135 continue;
8136 }
8137
8138 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8139 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008140 // Found end when searching backwards or start when searching
8141 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008142 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008143 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008144 }
8145 else
8146 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008147 // Found end when searching forward or start when searching
8148 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008149 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008150 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008151 }
8152
8153 if (nest == 0)
8154 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008155 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008156 if (flags & SP_RETCOUNT)
8157 ++retval;
8158 else
8159 retval = pos.lnum;
8160 if (flags & SP_SETPCMARK)
8161 setpcmark();
8162 curwin->w_cursor = pos;
8163 if (!(flags & SP_REPEAT))
8164 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008165 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008166 }
8167 }
8168
8169 if (match_pos != NULL)
8170 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008171 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008172 match_pos->lnum = curwin->w_cursor.lnum;
8173 match_pos->col = curwin->w_cursor.col + 1;
8174 }
8175
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008176 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008177 if ((flags & SP_NOMOVE) || retval == 0)
8178 curwin->w_cursor = save_cursor;
8179
8180theend:
8181 vim_free(pat2);
8182 vim_free(pat3);
8183 if (p_cpo == empty_option)
8184 p_cpo = save_cpo;
8185 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008186 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008187 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008188 // If it's still empty it was changed and restored, need to restore in
8189 // the complicated way.
8190 if (*p_cpo == NUL)
8191 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008192 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008193 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008194
8195 return retval;
8196}
8197
8198/*
8199 * "searchpos()" function
8200 */
8201 static void
8202f_searchpos(typval_T *argvars, typval_T *rettv)
8203{
8204 pos_T match_pos;
8205 int lnum = 0;
8206 int col = 0;
8207 int n;
8208 int flags = 0;
8209
8210 if (rettv_list_alloc(rettv) == FAIL)
8211 return;
8212
8213 n = search_cmn(argvars, &match_pos, &flags);
8214 if (n > 0)
8215 {
8216 lnum = match_pos.lnum;
8217 col = match_pos.col;
8218 }
8219
8220 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8221 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8222 if (flags & SP_SUBPAT)
8223 list_append_number(rettv->vval.v_list, (varnumber_T)n);
8224}
8225
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008226/*
8227 * Set the cursor or mark position.
8228 * If 'charpos' is TRUE, then use the column number as a character offet.
8229 * Otherwise use the column number as a byte offset.
8230 */
8231 static void
8232set_position(typval_T *argvars, typval_T *rettv, int charpos)
8233{
8234 pos_T pos;
8235 int fnum;
8236 char_u *name;
8237 colnr_T curswant = -1;
8238
8239 rettv->vval.v_number = -1;
8240
8241 name = tv_get_string_chk(argvars);
8242 if (name != NULL)
8243 {
8244 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
8245 {
8246 if (pos.col != MAXCOL && --pos.col < 0)
8247 pos.col = 0;
8248 if ((name[0] == '.' && name[1] == NUL))
8249 {
8250 // set cursor; "fnum" is ignored
8251 curwin->w_cursor = pos;
8252 if (curswant >= 0)
8253 {
8254 curwin->w_curswant = curswant - 1;
8255 curwin->w_set_curswant = FALSE;
8256 }
8257 check_cursor();
8258 rettv->vval.v_number = 0;
8259 }
8260 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8261 {
8262 // set mark
8263 if (setmark_pos(name[1], &pos, fnum) == OK)
8264 rettv->vval.v_number = 0;
8265 }
8266 else
8267 emsg(_(e_invarg));
8268 }
8269 }
8270}
8271/*
8272 * "setcharpos()" function
8273 */
8274 static void
8275f_setcharpos(typval_T *argvars, typval_T *rettv)
8276{
8277 set_position(argvars, rettv, TRUE);
8278}
8279
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008280 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008281f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8282{
8283 dict_T *d;
8284 dictitem_T *di;
8285 char_u *csearch;
8286
8287 if (argvars[0].v_type != VAR_DICT)
8288 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008289 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008290 return;
8291 }
8292
8293 if ((d = argvars[0].vval.v_dict) != NULL)
8294 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008295 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008296 if (csearch != NULL)
8297 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008298 if (enc_utf8)
8299 {
8300 int pcc[MAX_MCO];
8301 int c = utfc_ptr2char(csearch, pcc);
8302
8303 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8304 }
8305 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008306 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008307 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008308 }
8309
8310 di = dict_find(d, (char_u *)"forward", -1);
8311 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008312 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008313 ? FORWARD : BACKWARD);
8314
8315 di = dict_find(d, (char_u *)"until", -1);
8316 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008317 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008318 }
8319}
8320
8321/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008322 * "setcursorcharpos" function
8323 */
8324 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008325f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008326{
8327 set_cursorpos(argvars, rettv, TRUE);
8328}
8329
8330/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008331 * "setenv()" function
8332 */
8333 static void
8334f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8335{
8336 char_u namebuf[NUMBUFLEN];
8337 char_u valbuf[NUMBUFLEN];
8338 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8339
8340 if (argvars[1].v_type == VAR_SPECIAL
8341 && argvars[1].vval.v_number == VVAL_NULL)
8342 vim_unsetenv(name);
8343 else
8344 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8345}
8346
8347/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008348 * "setfperm({fname}, {mode})" function
8349 */
8350 static void
8351f_setfperm(typval_T *argvars, typval_T *rettv)
8352{
8353 char_u *fname;
8354 char_u modebuf[NUMBUFLEN];
8355 char_u *mode_str;
8356 int i;
8357 int mask;
8358 int mode = 0;
8359
8360 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008361 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362 if (fname == NULL)
8363 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008364 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008365 if (mode_str == NULL)
8366 return;
8367 if (STRLEN(mode_str) != 9)
8368 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008369 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008370 return;
8371 }
8372
8373 mask = 1;
8374 for (i = 8; i >= 0; --i)
8375 {
8376 if (mode_str[i] != '-')
8377 mode |= mask;
8378 mask = mask << 1;
8379 }
8380 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8381}
8382
8383/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008384 * "setpos()" function
8385 */
8386 static void
8387f_setpos(typval_T *argvars, typval_T *rettv)
8388{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008389 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008390}
8391
8392/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008393 * Translate a register type string to the yank type and block length
8394 */
8395 static int
8396get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8397{
8398 char_u *stropt = *pp;
8399 switch (*stropt)
8400 {
8401 case 'v': case 'c': // character-wise selection
8402 *yank_type = MCHAR;
8403 break;
8404 case 'V': case 'l': // line-wise selection
8405 *yank_type = MLINE;
8406 break;
8407 case 'b': case Ctrl_V: // block-wise selection
8408 *yank_type = MBLOCK;
8409 if (VIM_ISDIGIT(stropt[1]))
8410 {
8411 ++stropt;
8412 *block_len = getdigits(&stropt) - 1;
8413 --stropt;
8414 }
8415 break;
8416 default:
8417 return FAIL;
8418 }
8419 *pp = stropt;
8420 return OK;
8421}
8422
8423/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008424 * "setreg()" function
8425 */
8426 static void
8427f_setreg(typval_T *argvars, typval_T *rettv)
8428{
8429 int regname;
8430 char_u *strregname;
8431 char_u *stropt;
8432 char_u *strval;
8433 int append;
8434 char_u yank_type;
8435 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008436 typval_T *regcontents;
8437 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008438
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008439 pointreg = 0;
8440 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008441 block_len = -1;
8442 yank_type = MAUTO;
8443 append = FALSE;
8444
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008445 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008446 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008447
8448 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008449 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008450 regname = *strregname;
8451 if (regname == 0 || regname == '@')
8452 regname = '"';
8453
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008454 if (argvars[1].v_type == VAR_DICT)
8455 {
8456 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008457 dictitem_T *di;
8458
8459 if (d == NULL || d->dv_hashtab.ht_used == 0)
8460 {
8461 // Empty dict, clear the register (like setreg(0, []))
8462 char_u *lstval[2] = {NULL, NULL};
8463 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8464 return;
8465 }
8466
8467 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008468 if (di != NULL)
8469 regcontents = &di->di_tv;
8470
8471 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8472 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008473 {
8474 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8475
8476 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008477 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008478 semsg(_(e_invargval), "value");
8479 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008480 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008481 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008482
8483 if (regname == '"')
8484 {
8485 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8486 if (stropt != NULL)
8487 {
8488 pointreg = *stropt;
8489 regname = pointreg;
8490 }
8491 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008492 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008493 pointreg = regname;
8494 }
8495 else
8496 regcontents = &argvars[1];
8497
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008498 if (argvars[2].v_type != VAR_UNKNOWN)
8499 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008500 if (yank_type != MAUTO)
8501 {
8502 semsg(_(e_toomanyarg), "setreg");
8503 return;
8504 }
8505
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008506 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008507 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008508 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008509 for (; *stropt != NUL; ++stropt)
8510 switch (*stropt)
8511 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008512 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008513 append = TRUE;
8514 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008515 default:
8516 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008517 }
8518 }
8519
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008520 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008521 {
8522 char_u **lstval;
8523 char_u **allocval;
8524 char_u buf[NUMBUFLEN];
8525 char_u **curval;
8526 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008527 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008528 listitem_T *li;
8529 int len;
8530
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008531 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532 len = ll == NULL ? 0 : ll->lv_len;
8533
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008534 // First half: use for pointers to result lines; second half: use for
8535 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008536 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008537 if (lstval == NULL)
8538 return;
8539 curval = lstval;
8540 allocval = lstval + len + 2;
8541 curallocval = allocval;
8542
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008543 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008544 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008545 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008546 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008547 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008548 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008549 if (strval == NULL)
8550 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008551 if (strval == buf)
8552 {
8553 // Need to make a copy, next tv_get_string_buf_chk() will
8554 // overwrite the string.
8555 strval = vim_strsave(buf);
8556 if (strval == NULL)
8557 goto free_lstval;
8558 *curallocval++ = strval;
8559 }
8560 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008561 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562 }
8563 *curval++ = NULL;
8564
8565 write_reg_contents_lst(regname, lstval, -1,
8566 append, yank_type, block_len);
8567free_lstval:
8568 while (curallocval > allocval)
8569 vim_free(*--curallocval);
8570 vim_free(lstval);
8571 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008572 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008573 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008574 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008575 if (strval == NULL)
8576 return;
8577 write_reg_contents_ex(regname, strval, -1,
8578 append, yank_type, block_len);
8579 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008580 if (pointreg != 0)
8581 get_yank_register(pointreg, TRUE);
8582
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583 rettv->vval.v_number = 0;
8584}
8585
8586/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008587 * "settagstack()" function
8588 */
8589 static void
8590f_settagstack(typval_T *argvars, typval_T *rettv)
8591{
8592 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8593 win_T *wp;
8594 dict_T *d;
8595 int action = 'r';
8596
8597 rettv->vval.v_number = -1;
8598
8599 // first argument: window number or id
8600 wp = find_win_by_nr_or_id(&argvars[0]);
8601 if (wp == NULL)
8602 return;
8603
8604 // second argument: dict with items to set in the tag stack
8605 if (argvars[1].v_type != VAR_DICT)
8606 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008607 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008608 return;
8609 }
8610 d = argvars[1].vval.v_dict;
8611 if (d == NULL)
8612 return;
8613
8614 // third argument: action - 'a' for append and 'r' for replace.
8615 // default is to replace the stack.
8616 if (argvars[2].v_type == VAR_UNKNOWN)
8617 action = 'r';
8618 else if (argvars[2].v_type == VAR_STRING)
8619 {
8620 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008621 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008622 if (actstr == NULL)
8623 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008624 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8625 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008626 action = *actstr;
8627 else
8628 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008629 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008630 return;
8631 }
8632 }
8633 else
8634 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008635 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008636 return;
8637 }
8638
8639 if (set_tagstack(wp, d, action) == OK)
8640 rettv->vval.v_number = 0;
8641}
8642
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008643#ifdef FEAT_CRYPT
8644/*
8645 * "sha256({string})" function
8646 */
8647 static void
8648f_sha256(typval_T *argvars, typval_T *rettv)
8649{
8650 char_u *p;
8651
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008652 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008653 rettv->vval.v_string = vim_strsave(
8654 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8655 rettv->v_type = VAR_STRING;
8656}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008657#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008658
8659/*
8660 * "shellescape({string})" function
8661 */
8662 static void
8663f_shellescape(typval_T *argvars, typval_T *rettv)
8664{
Bram Moolenaar20615522017-06-05 18:46:26 +02008665 int do_special = non_zero_arg(&argvars[1]);
8666
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008667 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008668 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008669 rettv->v_type = VAR_STRING;
8670}
8671
8672/*
8673 * shiftwidth() function
8674 */
8675 static void
8676f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8677{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008678 rettv->vval.v_number = 0;
8679
8680 if (argvars[0].v_type != VAR_UNKNOWN)
8681 {
8682 long col;
8683
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008684 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008685 if (col < 0)
8686 return; // type error; errmsg already given
8687#ifdef FEAT_VARTABS
8688 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8689 return;
8690#endif
8691 }
8692
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008693 rettv->vval.v_number = get_sw_value(curbuf);
8694}
8695
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008696#ifdef FEAT_FLOAT
8697/*
8698 * "sin()" function
8699 */
8700 static void
8701f_sin(typval_T *argvars, typval_T *rettv)
8702{
8703 float_T f = 0.0;
8704
8705 rettv->v_type = VAR_FLOAT;
8706 if (get_float_arg(argvars, &f) == OK)
8707 rettv->vval.v_float = sin(f);
8708 else
8709 rettv->vval.v_float = 0.0;
8710}
8711
8712/*
8713 * "sinh()" function
8714 */
8715 static void
8716f_sinh(typval_T *argvars, typval_T *rettv)
8717{
8718 float_T f = 0.0;
8719
8720 rettv->v_type = VAR_FLOAT;
8721 if (get_float_arg(argvars, &f) == OK)
8722 rettv->vval.v_float = sinh(f);
8723 else
8724 rettv->vval.v_float = 0.0;
8725}
8726#endif
8727
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008728/*
8729 * "soundfold({word})" function
8730 */
8731 static void
8732f_soundfold(typval_T *argvars, typval_T *rettv)
8733{
8734 char_u *s;
8735
8736 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008737 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008738#ifdef FEAT_SPELL
8739 rettv->vval.v_string = eval_soundfold(s);
8740#else
8741 rettv->vval.v_string = vim_strsave(s);
8742#endif
8743}
8744
8745/*
8746 * "spellbadword()" function
8747 */
8748 static void
8749f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8750{
8751 char_u *word = (char_u *)"";
8752 hlf_T attr = HLF_COUNT;
8753 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008754#ifdef FEAT_SPELL
8755 int wo_spell_save = curwin->w_p_spell;
8756
8757 if (!curwin->w_p_spell)
8758 {
8759 did_set_spelllang(curwin);
8760 curwin->w_p_spell = TRUE;
8761 }
8762
8763 if (*curwin->w_s->b_p_spl == NUL)
8764 {
8765 emsg(_(e_no_spell));
8766 curwin->w_p_spell = wo_spell_save;
8767 return;
8768 }
8769#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770
8771 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008772 {
8773#ifdef FEAT_SPELL
8774 curwin->w_p_spell = wo_spell_save;
8775#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008777 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008778
8779#ifdef FEAT_SPELL
8780 if (argvars[0].v_type == VAR_UNKNOWN)
8781 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008782 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008783 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8784 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008785 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008786 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008787 curwin->w_set_curswant = TRUE;
8788 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008789 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008790 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008791 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008792 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008793 int capcol = -1;
8794
8795 if (str != NULL)
8796 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008797 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008798 while (*str != NUL)
8799 {
8800 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8801 if (attr != HLF_COUNT)
8802 {
8803 word = str;
8804 break;
8805 }
8806 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008807 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008808 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008809 }
8810 }
8811 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008812 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008813#endif
8814
8815 list_append_string(rettv->vval.v_list, word, len);
8816 list_append_string(rettv->vval.v_list, (char_u *)(
8817 attr == HLF_SPB ? "bad" :
8818 attr == HLF_SPR ? "rare" :
8819 attr == HLF_SPL ? "local" :
8820 attr == HLF_SPC ? "caps" :
8821 ""), -1);
8822}
8823
8824/*
8825 * "spellsuggest()" function
8826 */
8827 static void
8828f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8829{
8830#ifdef FEAT_SPELL
8831 char_u *str;
8832 int typeerr = FALSE;
8833 int maxcount;
8834 garray_T ga;
8835 int i;
8836 listitem_T *li;
8837 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008838 int wo_spell_save = curwin->w_p_spell;
8839
8840 if (!curwin->w_p_spell)
8841 {
8842 did_set_spelllang(curwin);
8843 curwin->w_p_spell = TRUE;
8844 }
8845
8846 if (*curwin->w_s->b_p_spl == NUL)
8847 {
8848 emsg(_(e_no_spell));
8849 curwin->w_p_spell = wo_spell_save;
8850 return;
8851 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008852#endif
8853
8854 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008855 {
8856#ifdef FEAT_SPELL
8857 curwin->w_p_spell = wo_spell_save;
8858#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008859 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008860 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008861
8862#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008863 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008864 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008865 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008866 if (argvars[1].v_type != VAR_UNKNOWN)
8867 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008868 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008869 if (maxcount <= 0)
8870 return;
8871 if (argvars[2].v_type != VAR_UNKNOWN)
8872 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008873 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008874 if (typeerr)
8875 return;
8876 }
8877 }
8878 else
8879 maxcount = 25;
8880
8881 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8882
8883 for (i = 0; i < ga.ga_len; ++i)
8884 {
8885 str = ((char_u **)ga.ga_data)[i];
8886
8887 li = listitem_alloc();
8888 if (li == NULL)
8889 vim_free(str);
8890 else
8891 {
8892 li->li_tv.v_type = VAR_STRING;
8893 li->li_tv.v_lock = 0;
8894 li->li_tv.vval.v_string = str;
8895 list_append(rettv->vval.v_list, li);
8896 }
8897 }
8898 ga_clear(&ga);
8899 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008900 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008901#endif
8902}
8903
8904 static void
8905f_split(typval_T *argvars, typval_T *rettv)
8906{
8907 char_u *str;
8908 char_u *end;
8909 char_u *pat = NULL;
8910 regmatch_T regmatch;
8911 char_u patbuf[NUMBUFLEN];
8912 char_u *save_cpo;
8913 int match;
8914 colnr_T col = 0;
8915 int keepempty = FALSE;
8916 int typeerr = FALSE;
8917
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008918 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008919 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008920 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008921
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008922 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008923 if (argvars[1].v_type != VAR_UNKNOWN)
8924 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008925 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008926 if (pat == NULL)
8927 typeerr = TRUE;
8928 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008929 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008930 }
8931 if (pat == NULL || *pat == NUL)
8932 pat = (char_u *)"[\\x01- ]\\+";
8933
8934 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008935 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008936 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008937 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008938
8939 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8940 if (regmatch.regprog != NULL)
8941 {
8942 regmatch.rm_ic = FALSE;
8943 while (*str != NUL || keepempty)
8944 {
8945 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008946 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008947 else
8948 match = vim_regexec_nl(&regmatch, str, col);
8949 if (match)
8950 end = regmatch.startp[0];
8951 else
8952 end = str + STRLEN(str);
8953 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
8954 && *str != NUL && match && end < regmatch.endp[0]))
8955 {
8956 if (list_append_string(rettv->vval.v_list, str,
8957 (int)(end - str)) == FAIL)
8958 break;
8959 }
8960 if (!match)
8961 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01008962 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008963 if (regmatch.endp[0] > str)
8964 col = 0;
8965 else
Bram Moolenaar13505972019-01-24 15:04:48 +01008966 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008967 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008968 str = regmatch.endp[0];
8969 }
8970
8971 vim_regfree(regmatch.regprog);
8972 }
8973
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008974theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008975 p_cpo = save_cpo;
8976}
8977
8978#ifdef FEAT_FLOAT
8979/*
8980 * "sqrt()" function
8981 */
8982 static void
8983f_sqrt(typval_T *argvars, typval_T *rettv)
8984{
8985 float_T f = 0.0;
8986
8987 rettv->v_type = VAR_FLOAT;
8988 if (get_float_arg(argvars, &f) == OK)
8989 rettv->vval.v_float = sqrt(f);
8990 else
8991 rettv->vval.v_float = 0.0;
8992}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01008993#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008994
Bram Moolenaar0387cae2019-11-29 21:07:58 +01008995#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01008996/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008997 * "str2float()" function
8998 */
8999 static void
9000f_str2float(typval_T *argvars, typval_T *rettv)
9001{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009002 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009003 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009004
Bram Moolenaar08243d22017-01-10 16:12:29 +01009005 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009006 p = skipwhite(p + 1);
9007 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01009008 if (isneg)
9009 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009010 rettv->v_type = VAR_FLOAT;
9011}
9012#endif
9013
9014/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02009015 * "str2list()" function
9016 */
9017 static void
9018f_str2list(typval_T *argvars, typval_T *rettv)
9019{
9020 char_u *p;
9021 int utf8 = FALSE;
9022
9023 if (rettv_list_alloc(rettv) == FAIL)
9024 return;
9025
9026 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02009027 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02009028
9029 p = tv_get_string(&argvars[0]);
9030
9031 if (has_mbyte || utf8)
9032 {
9033 int (*ptr2len)(char_u *);
9034 int (*ptr2char)(char_u *);
9035
9036 if (utf8 || enc_utf8)
9037 {
9038 ptr2len = utf_ptr2len;
9039 ptr2char = utf_ptr2char;
9040 }
9041 else
9042 {
9043 ptr2len = mb_ptr2len;
9044 ptr2char = mb_ptr2char;
9045 }
9046
9047 for ( ; *p != NUL; p += (*ptr2len)(p))
9048 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
9049 }
9050 else
9051 for ( ; *p != NUL; ++p)
9052 list_append_number(rettv->vval.v_list, *p);
9053}
9054
9055/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009056 * "str2nr()" function
9057 */
9058 static void
9059f_str2nr(typval_T *argvars, typval_T *rettv)
9060{
9061 int base = 10;
9062 char_u *p;
9063 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009064 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01009065 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009066
9067 if (argvars[1].v_type != VAR_UNKNOWN)
9068 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009069 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009070 if (base != 2 && base != 8 && base != 10 && base != 16)
9071 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009072 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009073 return;
9074 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02009075 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009076 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009077 }
9078
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009079 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009080 isneg = (*p == '-');
9081 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009082 p = skipwhite(p + 1);
9083 switch (base)
9084 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009085 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02009086 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009087 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009088 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02009089 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
9090 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01009091 if (isneg)
9092 rettv->vval.v_number = -n;
9093 else
9094 rettv->vval.v_number = n;
9095
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009096}
9097
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009098/*
9099 * "strgetchar()" function
9100 */
9101 static void
9102f_strgetchar(typval_T *argvars, typval_T *rettv)
9103{
9104 char_u *str;
9105 int len;
9106 int error = FALSE;
9107 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01009108 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009109
9110 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009111 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112 if (str == NULL)
9113 return;
9114 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009115 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009116 if (error)
9117 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009118
Bram Moolenaar13505972019-01-24 15:04:48 +01009119 while (charidx >= 0 && byteidx < len)
9120 {
9121 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009122 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009123 rettv->vval.v_number = mb_ptr2char(str + byteidx);
9124 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009125 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009126 --charidx;
9127 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009128 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009129}
9130
9131/*
9132 * "stridx()" function
9133 */
9134 static void
9135f_stridx(typval_T *argvars, typval_T *rettv)
9136{
9137 char_u buf[NUMBUFLEN];
9138 char_u *needle;
9139 char_u *haystack;
9140 char_u *save_haystack;
9141 char_u *pos;
9142 int start_idx;
9143
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009144 needle = tv_get_string_chk(&argvars[1]);
9145 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009146 rettv->vval.v_number = -1;
9147 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009148 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009149
9150 if (argvars[2].v_type != VAR_UNKNOWN)
9151 {
9152 int error = FALSE;
9153
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009154 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009155 if (error || start_idx >= (int)STRLEN(haystack))
9156 return;
9157 if (start_idx >= 0)
9158 haystack += start_idx;
9159 }
9160
9161 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9162 if (pos != NULL)
9163 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
9164}
9165
9166/*
9167 * "string()" function
9168 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009169 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009170f_string(typval_T *argvars, typval_T *rettv)
9171{
9172 char_u *tofree;
9173 char_u numbuf[NUMBUFLEN];
9174
9175 rettv->v_type = VAR_STRING;
9176 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
9177 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009178 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009179 if (rettv->vval.v_string != NULL && tofree == NULL)
9180 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
9181}
9182
9183/*
9184 * "strlen()" function
9185 */
9186 static void
9187f_strlen(typval_T *argvars, typval_T *rettv)
9188{
9189 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009190 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009191}
9192
9193/*
9194 * "strchars()" function
9195 */
9196 static void
9197f_strchars(typval_T *argvars, typval_T *rettv)
9198{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009199 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009200 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009201 varnumber_T len = 0;
9202 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009203
9204 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009205 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009206 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02009207 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009208 else
9209 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009210 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
9211 while (*s != NUL)
9212 {
9213 func_mb_ptr2char_adv(&s);
9214 ++len;
9215 }
9216 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009217 }
9218}
9219
9220/*
9221 * "strdisplaywidth()" function
9222 */
9223 static void
9224f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
9225{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009226 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009227 int col = 0;
9228
9229 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009230 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009231
9232 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
9233}
9234
9235/*
9236 * "strwidth()" function
9237 */
9238 static void
9239f_strwidth(typval_T *argvars, typval_T *rettv)
9240{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009241 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009242
Bram Moolenaar13505972019-01-24 15:04:48 +01009243 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009244}
9245
9246/*
9247 * "strcharpart()" function
9248 */
9249 static void
9250f_strcharpart(typval_T *argvars, typval_T *rettv)
9251{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009252 char_u *p;
9253 int nchar;
9254 int nbyte = 0;
9255 int charlen;
9256 int len = 0;
9257 int slen;
9258 int error = FALSE;
9259
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009260 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009261 slen = (int)STRLEN(p);
9262
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009263 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009264 if (!error)
9265 {
9266 if (nchar > 0)
9267 while (nchar > 0 && nbyte < slen)
9268 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009269 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009270 --nchar;
9271 }
9272 else
9273 nbyte = nchar;
9274 if (argvars[2].v_type != VAR_UNKNOWN)
9275 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009276 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009277 while (charlen > 0 && nbyte + len < slen)
9278 {
9279 int off = nbyte + len;
9280
9281 if (off < 0)
9282 len += 1;
9283 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009284 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009285 --charlen;
9286 }
9287 }
9288 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009289 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009290 }
9291
9292 /*
9293 * Only return the overlap between the specified part and the actual
9294 * string.
9295 */
9296 if (nbyte < 0)
9297 {
9298 len += nbyte;
9299 nbyte = 0;
9300 }
9301 else if (nbyte > slen)
9302 nbyte = slen;
9303 if (len < 0)
9304 len = 0;
9305 else if (nbyte + len > slen)
9306 len = slen - nbyte;
9307
9308 rettv->v_type = VAR_STRING;
9309 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009310}
9311
9312/*
9313 * "strpart()" function
9314 */
9315 static void
9316f_strpart(typval_T *argvars, typval_T *rettv)
9317{
9318 char_u *p;
9319 int n;
9320 int len;
9321 int slen;
9322 int error = FALSE;
9323
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009324 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009325 slen = (int)STRLEN(p);
9326
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009327 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009328 if (error)
9329 len = 0;
9330 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009331 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009332 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009333 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009334
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009335 // Only return the overlap between the specified part and the actual
9336 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009337 if (n < 0)
9338 {
9339 len += n;
9340 n = 0;
9341 }
9342 else if (n > slen)
9343 n = slen;
9344 if (len < 0)
9345 len = 0;
9346 else if (n + len > slen)
9347 len = slen - n;
9348
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009349 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9350 {
9351 int off;
9352
9353 // length in characters
9354 for (off = n; off < slen && len > 0; --len)
9355 off += mb_ptr2len(p + off);
9356 len = off - n;
9357 }
9358
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009359 rettv->v_type = VAR_STRING;
9360 rettv->vval.v_string = vim_strnsave(p + n, len);
9361}
9362
9363/*
9364 * "strridx()" function
9365 */
9366 static void
9367f_strridx(typval_T *argvars, typval_T *rettv)
9368{
9369 char_u buf[NUMBUFLEN];
9370 char_u *needle;
9371 char_u *haystack;
9372 char_u *rest;
9373 char_u *lastmatch = NULL;
9374 int haystack_len, end_idx;
9375
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009376 needle = tv_get_string_chk(&argvars[1]);
9377 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009378
9379 rettv->vval.v_number = -1;
9380 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009381 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009382
9383 haystack_len = (int)STRLEN(haystack);
9384 if (argvars[2].v_type != VAR_UNKNOWN)
9385 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009386 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009387 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009388 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009389 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009390 }
9391 else
9392 end_idx = haystack_len;
9393
9394 if (*needle == NUL)
9395 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009396 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009397 lastmatch = haystack + end_idx;
9398 }
9399 else
9400 {
9401 for (rest = haystack; *rest != '\0'; ++rest)
9402 {
9403 rest = (char_u *)strstr((char *)rest, (char *)needle);
9404 if (rest == NULL || rest > haystack + end_idx)
9405 break;
9406 lastmatch = rest;
9407 }
9408 }
9409
9410 if (lastmatch == NULL)
9411 rettv->vval.v_number = -1;
9412 else
9413 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9414}
9415
9416/*
9417 * "strtrans()" function
9418 */
9419 static void
9420f_strtrans(typval_T *argvars, typval_T *rettv)
9421{
9422 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009423 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009424}
9425
9426/*
9427 * "submatch()" function
9428 */
9429 static void
9430f_submatch(typval_T *argvars, typval_T *rettv)
9431{
9432 int error = FALSE;
9433 int no;
9434 int retList = 0;
9435
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009436 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009437 if (error)
9438 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009439 if (no < 0 || no >= NSUBEXP)
9440 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009441 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009442 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009443 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009444 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009445 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009446 if (error)
9447 return;
9448
9449 if (retList == 0)
9450 {
9451 rettv->v_type = VAR_STRING;
9452 rettv->vval.v_string = reg_submatch(no);
9453 }
9454 else
9455 {
9456 rettv->v_type = VAR_LIST;
9457 rettv->vval.v_list = reg_submatch_list(no);
9458 }
9459}
9460
9461/*
9462 * "substitute()" function
9463 */
9464 static void
9465f_substitute(typval_T *argvars, typval_T *rettv)
9466{
9467 char_u patbuf[NUMBUFLEN];
9468 char_u subbuf[NUMBUFLEN];
9469 char_u flagsbuf[NUMBUFLEN];
9470
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009471 char_u *str = tv_get_string_chk(&argvars[0]);
9472 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009473 char_u *sub = NULL;
9474 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009475 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009476
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009477 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9478 expr = &argvars[2];
9479 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009480 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009481
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009482 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009483 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9484 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009485 rettv->vval.v_string = NULL;
9486 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009487 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009488}
9489
9490/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009491 * "swapinfo(swap_filename)" function
9492 */
9493 static void
9494f_swapinfo(typval_T *argvars, typval_T *rettv)
9495{
9496 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009497 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009498}
9499
9500/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009501 * "swapname(expr)" function
9502 */
9503 static void
9504f_swapname(typval_T *argvars, typval_T *rettv)
9505{
9506 buf_T *buf;
9507
9508 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009509 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009510 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9511 || buf->b_ml.ml_mfp->mf_fname == NULL)
9512 rettv->vval.v_string = NULL;
9513 else
9514 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9515}
9516
9517/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009518 * "synID(lnum, col, trans)" function
9519 */
9520 static void
9521f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9522{
9523 int id = 0;
9524#ifdef FEAT_SYN_HL
9525 linenr_T lnum;
9526 colnr_T col;
9527 int trans;
9528 int transerr = FALSE;
9529
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009530 lnum = tv_get_lnum(argvars); // -1 on type error
9531 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009532 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009533
9534 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9535 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9536 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9537#endif
9538
9539 rettv->vval.v_number = id;
9540}
9541
9542/*
9543 * "synIDattr(id, what [, mode])" function
9544 */
9545 static void
9546f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9547{
9548 char_u *p = NULL;
9549#ifdef FEAT_SYN_HL
9550 int id;
9551 char_u *what;
9552 char_u *mode;
9553 char_u modebuf[NUMBUFLEN];
9554 int modec;
9555
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009556 id = (int)tv_get_number(&argvars[0]);
9557 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009558 if (argvars[2].v_type != VAR_UNKNOWN)
9559 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009560 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009561 modec = TOLOWER_ASC(mode[0]);
9562 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009563 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009564 }
9565 else
9566 {
9567#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9568 if (USE_24BIT)
9569 modec = 'g';
9570 else
9571#endif
9572 if (t_colors > 1)
9573 modec = 'c';
9574 else
9575 modec = 't';
9576 }
9577
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009578 switch (TOLOWER_ASC(what[0]))
9579 {
9580 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009581 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009582 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009583 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009584 p = highlight_has_attr(id, HL_BOLD, modec);
9585 break;
9586
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009587 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009588 p = highlight_color(id, what, modec);
9589 break;
9590
9591 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009592 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009593 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009594 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009595 p = highlight_has_attr(id, HL_ITALIC, modec);
9596 break;
9597
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009598 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009599 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009600 break;
9601
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009602 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009603 p = highlight_has_attr(id, HL_INVERSE, modec);
9604 break;
9605
9606 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009607 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009608 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009609 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009610 else if (TOLOWER_ASC(what[1]) == 't' &&
9611 TOLOWER_ASC(what[2]) == 'r')
9612 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009613 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009614 p = highlight_has_attr(id, HL_STANDOUT, modec);
9615 break;
9616
9617 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009618 if (TOLOWER_ASC(what[1]) == 'l') // ul
9619 p = highlight_color(id, what, modec);
9620 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009621 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009622 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9623 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009624 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009625 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9626 break;
9627 }
9628
9629 if (p != NULL)
9630 p = vim_strsave(p);
9631#endif
9632 rettv->v_type = VAR_STRING;
9633 rettv->vval.v_string = p;
9634}
9635
9636/*
9637 * "synIDtrans(id)" function
9638 */
9639 static void
9640f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9641{
9642 int id;
9643
9644#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009645 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009646
9647 if (id > 0)
9648 id = syn_get_final_id(id);
9649 else
9650#endif
9651 id = 0;
9652
9653 rettv->vval.v_number = id;
9654}
9655
9656/*
9657 * "synconcealed(lnum, col)" function
9658 */
9659 static void
9660f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9661{
9662#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9663 linenr_T lnum;
9664 colnr_T col;
9665 int syntax_flags = 0;
9666 int cchar;
9667 int matchid = 0;
9668 char_u str[NUMBUFLEN];
9669#endif
9670
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009671 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009672
9673#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009674 lnum = tv_get_lnum(argvars); // -1 on type error
9675 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009676
Bram Moolenaara80faa82020-04-12 19:37:17 +02009677 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009678
9679 if (rettv_list_alloc(rettv) != FAIL)
9680 {
9681 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9682 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9683 && curwin->w_p_cole > 0)
9684 {
9685 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9686 syntax_flags = get_syntax_info(&matchid);
9687
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009688 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009689 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9690 {
9691 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009692 if (cchar == NUL && curwin->w_p_cole == 1)
9693 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009694 if (cchar != NUL)
9695 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009696 if (has_mbyte)
9697 (*mb_char2bytes)(cchar, str);
9698 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009699 str[0] = cchar;
9700 }
9701 }
9702 }
9703
9704 list_append_number(rettv->vval.v_list,
9705 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009706 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009707 list_append_string(rettv->vval.v_list, str, -1);
9708 list_append_number(rettv->vval.v_list, matchid);
9709 }
9710#endif
9711}
9712
9713/*
9714 * "synstack(lnum, col)" function
9715 */
9716 static void
9717f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9718{
9719#ifdef FEAT_SYN_HL
9720 linenr_T lnum;
9721 colnr_T col;
9722 int i;
9723 int id;
9724#endif
9725
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009726 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009727
9728#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009729 lnum = tv_get_lnum(argvars); // -1 on type error
9730 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009731
9732 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9733 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9734 && rettv_list_alloc(rettv) != FAIL)
9735 {
9736 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9737 for (i = 0; ; ++i)
9738 {
9739 id = syn_get_stack_item(i);
9740 if (id < 0)
9741 break;
9742 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9743 break;
9744 }
9745 }
9746#endif
9747}
9748
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009749/*
9750 * "tabpagebuflist()" function
9751 */
9752 static void
9753f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9754{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009755 tabpage_T *tp;
9756 win_T *wp = NULL;
9757
9758 if (argvars[0].v_type == VAR_UNKNOWN)
9759 wp = firstwin;
9760 else
9761 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009762 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009763 if (tp != NULL)
9764 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9765 }
9766 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9767 {
9768 for (; wp != NULL; wp = wp->w_next)
9769 if (list_append_number(rettv->vval.v_list,
9770 wp->w_buffer->b_fnum) == FAIL)
9771 break;
9772 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009773}
9774
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009775/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009776 * "tagfiles()" function
9777 */
9778 static void
9779f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9780{
9781 char_u *fname;
9782 tagname_T tn;
9783 int first;
9784
9785 if (rettv_list_alloc(rettv) == FAIL)
9786 return;
9787 fname = alloc(MAXPATHL);
9788 if (fname == NULL)
9789 return;
9790
9791 for (first = TRUE; ; first = FALSE)
9792 if (get_tagfname(&tn, first, fname) == FAIL
9793 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9794 break;
9795 tagname_free(&tn);
9796 vim_free(fname);
9797}
9798
9799/*
9800 * "taglist()" function
9801 */
9802 static void
9803f_taglist(typval_T *argvars, typval_T *rettv)
9804{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009805 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009806 char_u *tag_pattern;
9807
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009808 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009809
9810 rettv->vval.v_number = FALSE;
9811 if (*tag_pattern == NUL)
9812 return;
9813
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009814 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009815 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009816 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009817 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009818}
9819
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009820#ifdef FEAT_FLOAT
9821/*
9822 * "tan()" function
9823 */
9824 static void
9825f_tan(typval_T *argvars, typval_T *rettv)
9826{
9827 float_T f = 0.0;
9828
9829 rettv->v_type = VAR_FLOAT;
9830 if (get_float_arg(argvars, &f) == OK)
9831 rettv->vval.v_float = tan(f);
9832 else
9833 rettv->vval.v_float = 0.0;
9834}
9835
9836/*
9837 * "tanh()" function
9838 */
9839 static void
9840f_tanh(typval_T *argvars, typval_T *rettv)
9841{
9842 float_T f = 0.0;
9843
9844 rettv->v_type = VAR_FLOAT;
9845 if (get_float_arg(argvars, &f) == OK)
9846 rettv->vval.v_float = tanh(f);
9847 else
9848 rettv->vval.v_float = 0.0;
9849}
9850#endif
9851
9852/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009853 * "tolower(string)" function
9854 */
9855 static void
9856f_tolower(typval_T *argvars, typval_T *rettv)
9857{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009858 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009859 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009860}
9861
9862/*
9863 * "toupper(string)" function
9864 */
9865 static void
9866f_toupper(typval_T *argvars, typval_T *rettv)
9867{
9868 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009869 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009870}
9871
9872/*
9873 * "tr(string, fromstr, tostr)" function
9874 */
9875 static void
9876f_tr(typval_T *argvars, typval_T *rettv)
9877{
9878 char_u *in_str;
9879 char_u *fromstr;
9880 char_u *tostr;
9881 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009882 int inlen;
9883 int fromlen;
9884 int tolen;
9885 int idx;
9886 char_u *cpstr;
9887 int cplen;
9888 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009889 char_u buf[NUMBUFLEN];
9890 char_u buf2[NUMBUFLEN];
9891 garray_T ga;
9892
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009893 in_str = tv_get_string(&argvars[0]);
9894 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9895 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009896
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009897 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009898 rettv->v_type = VAR_STRING;
9899 rettv->vval.v_string = NULL;
9900 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009901 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009902 ga_init2(&ga, (int)sizeof(char), 80);
9903
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009904 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009905 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009906 if (STRLEN(fromstr) != STRLEN(tostr))
9907 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009908error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009909 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009910 ga_clear(&ga);
9911 return;
9912 }
9913
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009914 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009915 while (*in_str != NUL)
9916 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009917 if (has_mbyte)
9918 {
9919 inlen = (*mb_ptr2len)(in_str);
9920 cpstr = in_str;
9921 cplen = inlen;
9922 idx = 0;
9923 for (p = fromstr; *p != NUL; p += fromlen)
9924 {
9925 fromlen = (*mb_ptr2len)(p);
9926 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9927 {
9928 for (p = tostr; *p != NUL; p += tolen)
9929 {
9930 tolen = (*mb_ptr2len)(p);
9931 if (idx-- == 0)
9932 {
9933 cplen = tolen;
9934 cpstr = p;
9935 break;
9936 }
9937 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009938 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009939 goto error;
9940 break;
9941 }
9942 ++idx;
9943 }
9944
9945 if (first && cpstr == in_str)
9946 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009947 // Check that fromstr and tostr have the same number of
9948 // (multi-byte) characters. Done only once when a character
9949 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009950 first = FALSE;
9951 for (p = tostr; *p != NUL; p += tolen)
9952 {
9953 tolen = (*mb_ptr2len)(p);
9954 --idx;
9955 }
9956 if (idx != 0)
9957 goto error;
9958 }
9959
9960 (void)ga_grow(&ga, cplen);
9961 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
9962 ga.ga_len += cplen;
9963
9964 in_str += inlen;
9965 }
9966 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009968 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009969 p = vim_strchr(fromstr, *in_str);
9970 if (p != NULL)
9971 ga_append(&ga, tostr[p - fromstr]);
9972 else
9973 ga_append(&ga, *in_str);
9974 ++in_str;
9975 }
9976 }
9977
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009978 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009979 (void)ga_grow(&ga, 1);
9980 ga_append(&ga, NUL);
9981
9982 rettv->vval.v_string = ga.ga_data;
9983}
9984
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009985/*
9986 * "trim({expr})" function
9987 */
9988 static void
9989f_trim(typval_T *argvars, typval_T *rettv)
9990{
9991 char_u buf1[NUMBUFLEN];
9992 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009993 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009994 char_u *mask = NULL;
9995 char_u *tail;
9996 char_u *prev;
9997 char_u *p;
9998 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009999 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010000
10001 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010002 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010003 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010004 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010005
10006 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010007 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010008 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010009
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010010 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010011 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010012 int error = 0;
10013
10014 // leading or trailing characters to trim
10015 dir = (int)tv_get_number_chk(&argvars[2], &error);
10016 if (error)
10017 return;
10018 if (dir < 0 || dir > 2)
10019 {
10020 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
10021 return;
10022 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010023 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010024 }
10025
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010026 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010027 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010028 // Trim leading characters
10029 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010030 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010031 c1 = PTR2CHAR(head);
10032 if (mask == NULL)
10033 {
10034 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010035 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010036 }
10037 else
10038 {
10039 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10040 if (c1 == PTR2CHAR(p))
10041 break;
10042 if (*p == NUL)
10043 break;
10044 }
10045 MB_PTR_ADV(head);
10046 }
10047 }
10048
10049 tail = head + STRLEN(head);
10050 if (dir == 0 || dir == 2)
10051 {
10052 // Trim trailing characters
10053 for (; tail > head; tail = prev)
10054 {
10055 prev = tail;
10056 MB_PTR_BACK(head, prev);
10057 c1 = PTR2CHAR(prev);
10058 if (mask == NULL)
10059 {
10060 if (c1 > ' ' && c1 != 0xa0)
10061 break;
10062 }
10063 else
10064 {
10065 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10066 if (c1 == PTR2CHAR(p))
10067 break;
10068 if (*p == NUL)
10069 break;
10070 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010071 }
10072 }
Bram Moolenaardf44a272020-06-07 20:49:05 +020010073 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010074}
10075
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010076#ifdef FEAT_FLOAT
10077/*
10078 * "trunc({float})" function
10079 */
10080 static void
10081f_trunc(typval_T *argvars, typval_T *rettv)
10082{
10083 float_T f = 0.0;
10084
10085 rettv->v_type = VAR_FLOAT;
10086 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010087 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010088 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
10089 else
10090 rettv->vval.v_float = 0.0;
10091}
10092#endif
10093
10094/*
10095 * "type(expr)" function
10096 */
10097 static void
10098f_type(typval_T *argvars, typval_T *rettv)
10099{
10100 int n = -1;
10101
10102 switch (argvars[0].v_type)
10103 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010104 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
10105 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010106 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010107 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
10108 case VAR_LIST: n = VAR_TYPE_LIST; break;
10109 case VAR_DICT: n = VAR_TYPE_DICT; break;
10110 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
10111 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
10112 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020010113 case VAR_JOB: n = VAR_TYPE_JOB; break;
10114 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010115 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010116 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +020010117 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010118 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +010010119 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010120 n = -1;
10121 break;
10122 }
10123 rettv->vval.v_number = n;
10124}
10125
10126/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010127 * "virtcol(string)" function
10128 */
10129 static void
10130f_virtcol(typval_T *argvars, typval_T *rettv)
10131{
10132 colnr_T vcol = 0;
10133 pos_T *fp;
10134 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010135 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010136
Bram Moolenaar6f02b002021-01-10 20:22:54 +010010137 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010138 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
10139 && fnum == curbuf->b_fnum)
10140 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010141 // Limit the column to a valid value, getvvcol() doesn't check.
10142 if (fp->col < 0)
10143 fp->col = 0;
10144 else
10145 {
10146 len = (int)STRLEN(ml_get(fp->lnum));
10147 if (fp->col > len)
10148 fp->col = len;
10149 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010150 getvvcol(curwin, fp, NULL, NULL, &vcol);
10151 ++vcol;
10152 }
10153
10154 rettv->vval.v_number = vcol;
10155}
10156
10157/*
10158 * "visualmode()" function
10159 */
10160 static void
10161f_visualmode(typval_T *argvars, typval_T *rettv)
10162{
10163 char_u str[2];
10164
10165 rettv->v_type = VAR_STRING;
10166 str[0] = curbuf->b_visual_mode_eval;
10167 str[1] = NUL;
10168 rettv->vval.v_string = vim_strsave(str);
10169
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010170 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010171 if (non_zero_arg(&argvars[0]))
10172 curbuf->b_visual_mode_eval = NUL;
10173}
10174
10175/*
10176 * "wildmenumode()" function
10177 */
10178 static void
10179f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10180{
10181#ifdef FEAT_WILDMENU
10182 if (wild_menu_showing)
10183 rettv->vval.v_number = 1;
10184#endif
10185}
10186
10187/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +010010188 * "windowsversion()" function
10189 */
10190 static void
10191f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10192{
10193 rettv->v_type = VAR_STRING;
10194 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
10195}
10196
10197/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010198 * "wordcount()" function
10199 */
10200 static void
10201f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
10202{
10203 if (rettv_dict_alloc(rettv) == FAIL)
10204 return;
10205 cursor_pos_info(rettv->vval.v_dict);
10206}
10207
10208/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010209 * "xor(expr, expr)" function
10210 */
10211 static void
10212f_xor(typval_T *argvars, typval_T *rettv)
10213{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010214 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
10215 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010216}
10217
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010218#endif // FEAT_EVAL