blob: cc812ce33662d0bc887fad91d8e18a8f5a08346d [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},
4654 {"comments", 1},
4655 {"conceal",
4656#ifdef FEAT_CONCEAL
4657 1
4658#else
4659 0
4660#endif
4661 },
4662 {"cryptv",
4663#ifdef FEAT_CRYPT
4664 1
4665#else
4666 0
4667#endif
4668 },
4669 {"crypt-blowfish",
4670#ifdef FEAT_CRYPT
4671 1
4672#else
4673 0
4674#endif
4675 },
4676 {"crypt-blowfish2",
4677#ifdef FEAT_CRYPT
4678 1
4679#else
4680 0
4681#endif
4682 },
4683 {"cscope",
4684#ifdef FEAT_CSCOPE
4685 1
4686#else
4687 0
4688#endif
4689 },
4690 {"cursorbind", 1},
4691 {"cursorshape",
4692#ifdef CURSOR_SHAPE
4693 1
4694#else
4695 0
4696#endif
4697 },
4698 {"debug",
4699#ifdef DEBUG
4700 1
4701#else
4702 0
4703#endif
4704 },
4705 {"dialog_con",
4706#ifdef FEAT_CON_DIALOG
4707 1
4708#else
4709 0
4710#endif
4711 },
4712 {"dialog_gui",
4713#ifdef FEAT_GUI_DIALOG
4714 1
4715#else
4716 0
4717#endif
4718 },
4719 {"diff",
4720#ifdef FEAT_DIFF
4721 1
4722#else
4723 0
4724#endif
4725 },
4726 {"digraphs",
4727#ifdef FEAT_DIGRAPHS
4728 1
4729#else
4730 0
4731#endif
4732 },
4733 {"directx",
4734#ifdef FEAT_DIRECTX
4735 1
4736#else
4737 0
4738#endif
4739 },
4740 {"dnd",
4741#ifdef FEAT_DND
4742 1
4743#else
4744 0
4745#endif
4746 },
4747 {"emacs_tags",
4748#ifdef FEAT_EMACS_TAGS
4749 1
4750#else
4751 0
4752#endif
4753 },
4754 {"eval", 1}, // always present, of course!
4755 {"ex_extra", 1}, // graduated feature
4756 {"extra_search",
4757#ifdef FEAT_SEARCH_EXTRA
4758 1
4759#else
4760 0
4761#endif
4762 },
4763 {"file_in_path",
4764#ifdef FEAT_SEARCHPATH
4765 1
4766#else
4767 0
4768#endif
4769 },
4770 {"filterpipe",
4771#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4772 1
4773#else
4774 0
4775#endif
4776 },
4777 {"find_in_path",
4778#ifdef FEAT_FIND_ID
4779 1
4780#else
4781 0
4782#endif
4783 },
4784 {"float",
4785#ifdef FEAT_FLOAT
4786 1
4787#else
4788 0
4789#endif
4790 },
4791 {"folding",
4792#ifdef FEAT_FOLDING
4793 1
4794#else
4795 0
4796#endif
4797 },
4798 {"footer",
4799#ifdef FEAT_FOOTER
4800 1
4801#else
4802 0
4803#endif
4804 },
4805 {"fork",
4806#if !defined(USE_SYSTEM) && defined(UNIX)
4807 1
4808#else
4809 0
4810#endif
4811 },
4812 {"gettext",
4813#ifdef FEAT_GETTEXT
4814 1
4815#else
4816 0
4817#endif
4818 },
4819 {"gui",
4820#ifdef FEAT_GUI
4821 1
4822#else
4823 0
4824#endif
4825 },
4826 {"gui_neXtaw",
4827#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4828 1
4829#else
4830 0
4831#endif
4832 },
4833 {"gui_athena",
4834#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4835 1
4836#else
4837 0
4838#endif
4839 },
4840 {"gui_gtk",
4841#ifdef FEAT_GUI_GTK
4842 1
4843#else
4844 0
4845#endif
4846 },
4847 {"gui_gtk2",
4848#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4849 1
4850#else
4851 0
4852#endif
4853 },
4854 {"gui_gtk3",
4855#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4856 1
4857#else
4858 0
4859#endif
4860 },
4861 {"gui_gnome",
4862#ifdef FEAT_GUI_GNOME
4863 1
4864#else
4865 0
4866#endif
4867 },
4868 {"gui_haiku",
4869#ifdef FEAT_GUI_HAIKU
4870 1
4871#else
4872 0
4873#endif
4874 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004875 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004876 {"gui_motif",
4877#ifdef FEAT_GUI_MOTIF
4878 1
4879#else
4880 0
4881#endif
4882 },
4883 {"gui_photon",
4884#ifdef FEAT_GUI_PHOTON
4885 1
4886#else
4887 0
4888#endif
4889 },
4890 {"gui_win32",
4891#ifdef FEAT_GUI_MSWIN
4892 1
4893#else
4894 0
4895#endif
4896 },
4897 {"iconv",
4898#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4899 1
4900#else
4901 0
4902#endif
4903 },
4904 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004905 {"ipv6",
4906#ifdef FEAT_IPV6
4907 1
4908#else
4909 0
4910#endif
4911 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004912 {"job",
4913#ifdef FEAT_JOB_CHANNEL
4914 1
4915#else
4916 0
4917#endif
4918 },
4919 {"jumplist",
4920#ifdef FEAT_JUMPLIST
4921 1
4922#else
4923 0
4924#endif
4925 },
4926 {"keymap",
4927#ifdef FEAT_KEYMAP
4928 1
4929#else
4930 0
4931#endif
4932 },
4933 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4934 {"langmap",
4935#ifdef FEAT_LANGMAP
4936 1
4937#else
4938 0
4939#endif
4940 },
4941 {"libcall",
4942#ifdef FEAT_LIBCALL
4943 1
4944#else
4945 0
4946#endif
4947 },
4948 {"linebreak",
4949#ifdef FEAT_LINEBREAK
4950 1
4951#else
4952 0
4953#endif
4954 },
4955 {"lispindent",
4956#ifdef FEAT_LISP
4957 1
4958#else
4959 0
4960#endif
4961 },
4962 {"listcmds", 1},
4963 {"localmap", 1},
4964 {"lua",
4965#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4966 1
4967#else
4968 0
4969#endif
4970 },
4971 {"menu",
4972#ifdef FEAT_MENU
4973 1
4974#else
4975 0
4976#endif
4977 },
4978 {"mksession",
4979#ifdef FEAT_SESSION
4980 1
4981#else
4982 0
4983#endif
4984 },
4985 {"modify_fname", 1},
4986 {"mouse", 1},
4987 {"mouseshape",
4988#ifdef FEAT_MOUSESHAPE
4989 1
4990#else
4991 0
4992#endif
4993 },
4994 {"mouse_dec",
4995#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4996 1
4997#else
4998 0
4999#endif
5000 },
5001 {"mouse_gpm",
5002#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
5003 1
5004#else
5005 0
5006#endif
5007 },
5008 {"mouse_jsbterm",
5009#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
5010 1
5011#else
5012 0
5013#endif
5014 },
5015 {"mouse_netterm",
5016#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
5017 1
5018#else
5019 0
5020#endif
5021 },
5022 {"mouse_pterm",
5023#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
5024 1
5025#else
5026 0
5027#endif
5028 },
5029 {"mouse_sgr",
5030#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5031 1
5032#else
5033 0
5034#endif
5035 },
5036 {"mouse_sysmouse",
5037#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
5038 1
5039#else
5040 0
5041#endif
5042 },
5043 {"mouse_urxvt",
5044#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
5045 1
5046#else
5047 0
5048#endif
5049 },
5050 {"mouse_xterm",
5051#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5052 1
5053#else
5054 0
5055#endif
5056 },
5057 {"multi_byte", 1},
5058 {"multi_byte_ime",
5059#ifdef FEAT_MBYTE_IME
5060 1
5061#else
5062 0
5063#endif
5064 },
5065 {"multi_lang",
5066#ifdef FEAT_MULTI_LANG
5067 1
5068#else
5069 0
5070#endif
5071 },
5072 {"mzscheme",
5073#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
5074 1
5075#else
5076 0
5077#endif
5078 },
5079 {"num64", 1},
5080 {"ole",
5081#ifdef FEAT_OLE
5082 1
5083#else
5084 0
5085#endif
5086 },
5087 {"packages",
5088#ifdef FEAT_EVAL
5089 1
5090#else
5091 0
5092#endif
5093 },
5094 {"path_extra",
5095#ifdef FEAT_PATH_EXTRA
5096 1
5097#else
5098 0
5099#endif
5100 },
5101 {"perl",
5102#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
5103 1
5104#else
5105 0
5106#endif
5107 },
5108 {"persistent_undo",
5109#ifdef FEAT_PERSISTENT_UNDO
5110 1
5111#else
5112 0
5113#endif
5114 },
5115 {"python_compiled",
5116#if defined(FEAT_PYTHON)
5117 1
5118#else
5119 0
5120#endif
5121 },
5122 {"python_dynamic",
5123#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
5124 1
5125#else
5126 0
5127#endif
5128 },
5129 {"python",
5130#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
5131 1
5132#else
5133 0
5134#endif
5135 },
5136 {"pythonx",
5137#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
5138 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
5139 1
5140#else
5141 0
5142#endif
5143 },
5144 {"python3_compiled",
5145#if defined(FEAT_PYTHON3)
5146 1
5147#else
5148 0
5149#endif
5150 },
5151 {"python3_dynamic",
5152#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
5153 1
5154#else
5155 0
5156#endif
5157 },
5158 {"python3",
5159#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
5160 1
5161#else
5162 0
5163#endif
5164 },
5165 {"popupwin",
5166#ifdef FEAT_PROP_POPUP
5167 1
5168#else
5169 0
5170#endif
5171 },
5172 {"postscript",
5173#ifdef FEAT_POSTSCRIPT
5174 1
5175#else
5176 0
5177#endif
5178 },
5179 {"printer",
5180#ifdef FEAT_PRINTER
5181 1
5182#else
5183 0
5184#endif
5185 },
5186 {"profile",
5187#ifdef FEAT_PROFILE
5188 1
5189#else
5190 0
5191#endif
5192 },
5193 {"reltime",
5194#ifdef FEAT_RELTIME
5195 1
5196#else
5197 0
5198#endif
5199 },
5200 {"quickfix",
5201#ifdef FEAT_QUICKFIX
5202 1
5203#else
5204 0
5205#endif
5206 },
5207 {"rightleft",
5208#ifdef FEAT_RIGHTLEFT
5209 1
5210#else
5211 0
5212#endif
5213 },
5214 {"ruby",
5215#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5216 1
5217#else
5218 0
5219#endif
5220 },
5221 {"scrollbind", 1},
5222 {"showcmd",
5223#ifdef FEAT_CMDL_INFO
5224 1
5225#else
5226 0
5227#endif
5228 },
5229 {"cmdline_info",
5230#ifdef FEAT_CMDL_INFO
5231 1
5232#else
5233 0
5234#endif
5235 },
5236 {"signs",
5237#ifdef FEAT_SIGNS
5238 1
5239#else
5240 0
5241#endif
5242 },
5243 {"smartindent",
5244#ifdef FEAT_SMARTINDENT
5245 1
5246#else
5247 0
5248#endif
5249 },
5250 {"startuptime",
5251#ifdef STARTUPTIME
5252 1
5253#else
5254 0
5255#endif
5256 },
5257 {"statusline",
5258#ifdef FEAT_STL_OPT
5259 1
5260#else
5261 0
5262#endif
5263 },
5264 {"netbeans_intg",
5265#ifdef FEAT_NETBEANS_INTG
5266 1
5267#else
5268 0
5269#endif
5270 },
5271 {"sound",
5272#ifdef FEAT_SOUND
5273 1
5274#else
5275 0
5276#endif
5277 },
5278 {"spell",
5279#ifdef FEAT_SPELL
5280 1
5281#else
5282 0
5283#endif
5284 },
5285 {"syntax",
5286#ifdef FEAT_SYN_HL
5287 1
5288#else
5289 0
5290#endif
5291 },
5292 {"system",
5293#if defined(USE_SYSTEM) || !defined(UNIX)
5294 1
5295#else
5296 0
5297#endif
5298 },
5299 {"tag_binary",
5300#ifdef FEAT_TAG_BINS
5301 1
5302#else
5303 0
5304#endif
5305 },
5306 {"tcl",
5307#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5308 1
5309#else
5310 0
5311#endif
5312 },
5313 {"termguicolors",
5314#ifdef FEAT_TERMGUICOLORS
5315 1
5316#else
5317 0
5318#endif
5319 },
5320 {"terminal",
5321#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5322 1
5323#else
5324 0
5325#endif
5326 },
5327 {"terminfo",
5328#ifdef TERMINFO
5329 1
5330#else
5331 0
5332#endif
5333 },
5334 {"termresponse",
5335#ifdef FEAT_TERMRESPONSE
5336 1
5337#else
5338 0
5339#endif
5340 },
5341 {"textobjects",
5342#ifdef FEAT_TEXTOBJ
5343 1
5344#else
5345 0
5346#endif
5347 },
5348 {"textprop",
5349#ifdef FEAT_PROP_POPUP
5350 1
5351#else
5352 0
5353#endif
5354 },
5355 {"tgetent",
5356#ifdef HAVE_TGETENT
5357 1
5358#else
5359 0
5360#endif
5361 },
5362 {"timers",
5363#ifdef FEAT_TIMERS
5364 1
5365#else
5366 0
5367#endif
5368 },
5369 {"title",
5370#ifdef FEAT_TITLE
5371 1
5372#else
5373 0
5374#endif
5375 },
5376 {"toolbar",
5377#ifdef FEAT_TOOLBAR
5378 1
5379#else
5380 0
5381#endif
5382 },
5383 {"unnamedplus",
5384#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5385 1
5386#else
5387 0
5388#endif
5389 },
5390 {"user-commands", 1}, // was accidentally included in 5.4
5391 {"user_commands", 1},
5392 {"vartabs",
5393#ifdef FEAT_VARTABS
5394 1
5395#else
5396 0
5397#endif
5398 },
5399 {"vertsplit", 1},
5400 {"viminfo",
5401#ifdef FEAT_VIMINFO
5402 1
5403#else
5404 0
5405#endif
5406 },
5407 {"vimscript-1", 1},
5408 {"vimscript-2", 1},
5409 {"vimscript-3", 1},
5410 {"vimscript-4", 1},
5411 {"virtualedit", 1},
5412 {"visual", 1},
5413 {"visualextra", 1},
5414 {"vreplace", 1},
5415 {"vtp",
5416#ifdef FEAT_VTP
5417 1
5418#else
5419 0
5420#endif
5421 },
5422 {"wildignore",
5423#ifdef FEAT_WILDIGN
5424 1
5425#else
5426 0
5427#endif
5428 },
5429 {"wildmenu",
5430#ifdef FEAT_WILDMENU
5431 1
5432#else
5433 0
5434#endif
5435 },
5436 {"windows", 1},
5437 {"winaltkeys",
5438#ifdef FEAT_WAK
5439 1
5440#else
5441 0
5442#endif
5443 },
5444 {"writebackup",
5445#ifdef FEAT_WRITEBACKUP
5446 1
5447#else
5448 0
5449#endif
5450 },
5451 {"xim",
5452#ifdef FEAT_XIM
5453 1
5454#else
5455 0
5456#endif
5457 },
5458 {"xfontset",
5459#ifdef FEAT_XFONTSET
5460 1
5461#else
5462 0
5463#endif
5464 },
5465 {"xpm",
5466#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5467 1
5468#else
5469 0
5470#endif
5471 },
5472 {"xpm_w32", // for backward compatibility
5473#ifdef FEAT_XPM_W32
5474 1
5475#else
5476 0
5477#endif
5478 },
5479 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005480#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005481 1
5482#else
5483 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005484#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005485 },
5486 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005487#ifdef USE_XSMP_INTERACT
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 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005494#ifdef FEAT_XCLIPBOARD
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_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005501#ifdef FEAT_XTERM_SAVE
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 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005508#if defined(UNIX) && defined(FEAT_X11)
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 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005515 };
5516
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005517 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005518 for (i = 0; has_list[i].name != NULL; ++i)
5519 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005520 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005521 x = TRUE;
5522 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005523 break;
5524 }
5525
Bram Moolenaar79296512020-03-22 16:17:14 +01005526 // features also in has_list[] but sometimes enabled at runtime
5527 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005528 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005529 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005530 {
5531 // intentionally empty
5532 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005533#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005534 else if (STRICMP(name, "balloon_multiline") == 0)
5535 n = multiline_balloon_available();
5536#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005537#ifdef VIMDLL
5538 else if (STRICMP(name, "filterpipe") == 0)
5539 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005540#endif
5541#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5542 else if (STRICMP(name, "iconv") == 0)
5543 n = iconv_enabled(FALSE);
5544#endif
5545#ifdef DYNAMIC_LUA
5546 else if (STRICMP(name, "lua") == 0)
5547 n = lua_enabled(FALSE);
5548#endif
5549#ifdef DYNAMIC_MZSCHEME
5550 else if (STRICMP(name, "mzscheme") == 0)
5551 n = mzscheme_enabled(FALSE);
5552#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005553#ifdef DYNAMIC_PERL
5554 else if (STRICMP(name, "perl") == 0)
5555 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005556#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005557#ifdef DYNAMIC_PYTHON
5558 else if (STRICMP(name, "python") == 0)
5559 n = python_enabled(FALSE);
5560#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005561#ifdef DYNAMIC_PYTHON3
5562 else if (STRICMP(name, "python3") == 0)
5563 n = python3_enabled(FALSE);
5564#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005565#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5566 else if (STRICMP(name, "pythonx") == 0)
5567 {
5568# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5569 if (p_pyx == 0)
5570 n = python3_enabled(FALSE) || python_enabled(FALSE);
5571 else if (p_pyx == 3)
5572 n = python3_enabled(FALSE);
5573 else if (p_pyx == 2)
5574 n = python_enabled(FALSE);
5575# elif defined(DYNAMIC_PYTHON)
5576 n = python_enabled(FALSE);
5577# elif defined(DYNAMIC_PYTHON3)
5578 n = python3_enabled(FALSE);
5579# endif
5580 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005581#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005582#ifdef DYNAMIC_RUBY
5583 else if (STRICMP(name, "ruby") == 0)
5584 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005585#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005586#ifdef DYNAMIC_TCL
5587 else if (STRICMP(name, "tcl") == 0)
5588 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005589#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005590#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005591 else if (STRICMP(name, "terminal") == 0)
5592 n = terminal_enabled();
5593#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005594 }
5595
Bram Moolenaar79296512020-03-22 16:17:14 +01005596 // features not in has_list[]
5597 if (x == FALSE)
5598 {
5599 if (STRNICMP(name, "patch", 5) == 0)
5600 {
5601 x = TRUE;
5602 if (name[5] == '-'
5603 && STRLEN(name) >= 11
5604 && vim_isdigit(name[6])
5605 && vim_isdigit(name[8])
5606 && vim_isdigit(name[10]))
5607 {
5608 int major = atoi((char *)name + 6);
5609 int minor = atoi((char *)name + 8);
5610
5611 // Expect "patch-9.9.01234".
5612 n = (major < VIM_VERSION_MAJOR
5613 || (major == VIM_VERSION_MAJOR
5614 && (minor < VIM_VERSION_MINOR
5615 || (minor == VIM_VERSION_MINOR
5616 && has_patch(atoi((char *)name + 10))))));
5617 }
5618 else
5619 n = has_patch(atoi((char *)name + 5));
5620 }
5621 else if (STRICMP(name, "vim_starting") == 0)
5622 {
5623 x = TRUE;
5624 n = (starting != 0);
5625 }
5626 else if (STRICMP(name, "ttyin") == 0)
5627 {
5628 x = TRUE;
5629 n = mch_input_isatty();
5630 }
5631 else if (STRICMP(name, "ttyout") == 0)
5632 {
5633 x = TRUE;
5634 n = stdout_isatty;
5635 }
5636 else if (STRICMP(name, "multi_byte_encoding") == 0)
5637 {
5638 x = TRUE;
5639 n = has_mbyte;
5640 }
5641 else if (STRICMP(name, "gui_running") == 0)
5642 {
5643 x = TRUE;
5644#ifdef FEAT_GUI
5645 n = (gui.in_use || gui.starting);
5646#endif
5647 }
5648 else if (STRICMP(name, "browse") == 0)
5649 {
5650 x = TRUE;
5651#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5652 n = gui.in_use; // gui_mch_browse() works when GUI is running
5653#endif
5654 }
5655 else if (STRICMP(name, "syntax_items") == 0)
5656 {
5657 x = TRUE;
5658#ifdef FEAT_SYN_HL
5659 n = syntax_present(curwin);
5660#endif
5661 }
5662 else if (STRICMP(name, "vcon") == 0)
5663 {
5664 x = TRUE;
5665#ifdef FEAT_VTP
5666 n = is_term_win32() && has_vtp_working();
5667#endif
5668 }
5669 else if (STRICMP(name, "netbeans_enabled") == 0)
5670 {
5671 x = TRUE;
5672#ifdef FEAT_NETBEANS_INTG
5673 n = netbeans_active();
5674#endif
5675 }
5676 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5677 {
5678 x = TRUE;
5679#ifdef FEAT_MOUSE_GPM
5680 n = gpm_enabled();
5681#endif
5682 }
5683 else if (STRICMP(name, "conpty") == 0)
5684 {
5685 x = TRUE;
5686#if defined(FEAT_TERMINAL) && defined(MSWIN)
5687 n = use_conpty();
5688#endif
5689 }
5690 else if (STRICMP(name, "clipboard_working") == 0)
5691 {
5692 x = TRUE;
5693#ifdef FEAT_CLIPBOARD
5694 n = clip_star.available;
5695#endif
5696 }
5697 }
5698
Bram Moolenaar04637e22020-09-05 18:45:29 +02005699 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005700 // return whether feature could ever be enabled
5701 rettv->vval.v_number = x;
5702 else
5703 // return whether feature is enabled
5704 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005705}
5706
5707/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005708 * Return TRUE if "feature" can change later.
5709 * Also when checking for the feature has side effects, such as loading a DLL.
5710 */
5711 int
5712dynamic_feature(char_u *feature)
5713{
5714 return (feature == NULL
5715#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5716 || STRICMP(feature, "balloon_multiline") == 0
5717#endif
5718#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5719 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5720#endif
5721#ifdef VIMDLL
5722 || STRICMP(feature, "filterpipe") == 0
5723#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005724#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005725 // this can only change on Unix where the ":gui" command could be
5726 // used.
5727 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5728#endif
5729#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5730 || STRICMP(feature, "iconv") == 0
5731#endif
5732#ifdef DYNAMIC_LUA
5733 || STRICMP(feature, "lua") == 0
5734#endif
5735#ifdef FEAT_MOUSE_GPM
5736 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5737#endif
5738#ifdef DYNAMIC_MZSCHEME
5739 || STRICMP(feature, "mzscheme") == 0
5740#endif
5741#ifdef FEAT_NETBEANS_INTG
5742 || STRICMP(feature, "netbeans_enabled") == 0
5743#endif
5744#ifdef DYNAMIC_PERL
5745 || STRICMP(feature, "perl") == 0
5746#endif
5747#ifdef DYNAMIC_PYTHON
5748 || STRICMP(feature, "python") == 0
5749#endif
5750#ifdef DYNAMIC_PYTHON3
5751 || STRICMP(feature, "python3") == 0
5752#endif
5753#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5754 || STRICMP(feature, "pythonx") == 0
5755#endif
5756#ifdef DYNAMIC_RUBY
5757 || STRICMP(feature, "ruby") == 0
5758#endif
5759#ifdef FEAT_SYN_HL
5760 || STRICMP(feature, "syntax_items") == 0
5761#endif
5762#ifdef DYNAMIC_TCL
5763 || STRICMP(feature, "tcl") == 0
5764#endif
5765 // once "starting" is zero it will stay that way
5766 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5767 || STRICMP(feature, "multi_byte_encoding") == 0
5768#if defined(FEAT_TERMINAL) && defined(MSWIN)
5769 || STRICMP(feature, "conpty") == 0
5770#endif
5771 );
5772}
5773
5774/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005775 * "haslocaldir()" function
5776 */
5777 static void
5778f_haslocaldir(typval_T *argvars, typval_T *rettv)
5779{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005780 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005781 win_T *wp = NULL;
5782
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005783 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5784
5785 // Check for window-local and tab-local directories
5786 if (wp != NULL && wp->w_localdir != NULL)
5787 rettv->vval.v_number = 1;
5788 else if (tp != NULL && tp->tp_localdir != NULL)
5789 rettv->vval.v_number = 2;
5790 else
5791 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005792}
5793
5794/*
5795 * "hasmapto()" function
5796 */
5797 static void
5798f_hasmapto(typval_T *argvars, typval_T *rettv)
5799{
5800 char_u *name;
5801 char_u *mode;
5802 char_u buf[NUMBUFLEN];
5803 int abbr = FALSE;
5804
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005805 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005806 if (argvars[1].v_type == VAR_UNKNOWN)
5807 mode = (char_u *)"nvo";
5808 else
5809 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005810 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005811 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005812 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005813 }
5814
5815 if (map_to_exists(name, mode, abbr))
5816 rettv->vval.v_number = TRUE;
5817 else
5818 rettv->vval.v_number = FALSE;
5819}
5820
5821/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005822 * "highlightID(name)" function
5823 */
5824 static void
5825f_hlID(typval_T *argvars, typval_T *rettv)
5826{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005827 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005828}
5829
5830/*
5831 * "highlight_exists()" function
5832 */
5833 static void
5834f_hlexists(typval_T *argvars, typval_T *rettv)
5835{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005836 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005837}
5838
5839/*
5840 * "hostname()" function
5841 */
5842 static void
5843f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5844{
5845 char_u hostname[256];
5846
5847 mch_get_host_name(hostname, 256);
5848 rettv->v_type = VAR_STRING;
5849 rettv->vval.v_string = vim_strsave(hostname);
5850}
5851
5852/*
5853 * iconv() function
5854 */
5855 static void
5856f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5857{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005858 char_u buf1[NUMBUFLEN];
5859 char_u buf2[NUMBUFLEN];
5860 char_u *from, *to, *str;
5861 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005862
5863 rettv->v_type = VAR_STRING;
5864 rettv->vval.v_string = NULL;
5865
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005866 str = tv_get_string(&argvars[0]);
5867 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5868 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005869 vimconv.vc_type = CONV_NONE;
5870 convert_setup(&vimconv, from, to);
5871
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005872 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005873 if (vimconv.vc_type == CONV_NONE)
5874 rettv->vval.v_string = vim_strsave(str);
5875 else
5876 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5877
5878 convert_setup(&vimconv, NULL, NULL);
5879 vim_free(from);
5880 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005881}
5882
5883/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005884 * "index()" function
5885 */
5886 static void
5887f_index(typval_T *argvars, typval_T *rettv)
5888{
5889 list_T *l;
5890 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005891 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005892 long idx = 0;
5893 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005894 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005895
5896 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005897 if (argvars[0].v_type == VAR_BLOB)
5898 {
5899 typval_T tv;
5900 int start = 0;
5901
5902 if (argvars[2].v_type != VAR_UNKNOWN)
5903 {
5904 start = tv_get_number_chk(&argvars[2], &error);
5905 if (error)
5906 return;
5907 }
5908 b = argvars[0].vval.v_blob;
5909 if (b == NULL)
5910 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005911 if (start < 0)
5912 {
5913 start = blob_len(b) + start;
5914 if (start < 0)
5915 start = 0;
5916 }
5917
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005918 for (idx = start; idx < blob_len(b); ++idx)
5919 {
5920 tv.v_type = VAR_NUMBER;
5921 tv.vval.v_number = blob_get(b, idx);
5922 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5923 {
5924 rettv->vval.v_number = idx;
5925 return;
5926 }
5927 }
5928 return;
5929 }
5930 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005931 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005932 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005933 return;
5934 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005935
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005936 l = argvars[0].vval.v_list;
5937 if (l != NULL)
5938 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005939 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005940 item = l->lv_first;
5941 if (argvars[2].v_type != VAR_UNKNOWN)
5942 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005943 // Start at specified item. Use the cached index that list_find()
5944 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005945 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005946 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005947 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005948 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005949 if (error)
5950 item = NULL;
5951 }
5952
5953 for ( ; item != NULL; item = item->li_next, ++idx)
5954 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5955 {
5956 rettv->vval.v_number = idx;
5957 break;
5958 }
5959 }
5960}
5961
5962static int inputsecret_flag = 0;
5963
5964/*
5965 * "input()" function
5966 * Also handles inputsecret() when inputsecret is set.
5967 */
5968 static void
5969f_input(typval_T *argvars, typval_T *rettv)
5970{
5971 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5972}
5973
5974/*
5975 * "inputdialog()" function
5976 */
5977 static void
5978f_inputdialog(typval_T *argvars, typval_T *rettv)
5979{
5980#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005981 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005982 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5983 {
5984 char_u *message;
5985 char_u buf[NUMBUFLEN];
5986 char_u *defstr = (char_u *)"";
5987
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005988 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005989 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005990 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005991 vim_strncpy(IObuff, defstr, IOSIZE - 1);
5992 else
5993 IObuff[0] = NUL;
5994 if (message != NULL && defstr != NULL
5995 && do_dialog(VIM_QUESTION, NULL, message,
5996 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
5997 rettv->vval.v_string = vim_strsave(IObuff);
5998 else
5999 {
6000 if (message != NULL && defstr != NULL
6001 && argvars[1].v_type != VAR_UNKNOWN
6002 && argvars[2].v_type != VAR_UNKNOWN)
6003 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006004 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006005 else
6006 rettv->vval.v_string = NULL;
6007 }
6008 rettv->v_type = VAR_STRING;
6009 }
6010 else
6011#endif
6012 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6013}
6014
6015/*
6016 * "inputlist()" function
6017 */
6018 static void
6019f_inputlist(typval_T *argvars, typval_T *rettv)
6020{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006021 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006022 listitem_T *li;
6023 int selected;
6024 int mouse_used;
6025
6026#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006027 // While starting up, there is no place to enter text. When running tests
6028 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006029 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006030 return;
6031#endif
6032 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6033 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006034 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006035 return;
6036 }
6037
6038 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006039 msg_row = Rows - 1; // for when 'cmdheight' > 1
6040 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006041 msg_scroll = TRUE;
6042 msg_clr_eos();
6043
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006044 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006045 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006046 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006047 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006048 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006049 msg_putchar('\n');
6050 }
6051
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006052 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006053 selected = prompt_for_number(&mouse_used);
6054 if (mouse_used)
6055 selected -= lines_left;
6056
6057 rettv->vval.v_number = selected;
6058}
6059
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006060static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6061
6062/*
6063 * "inputrestore()" function
6064 */
6065 static void
6066f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6067{
6068 if (ga_userinput.ga_len > 0)
6069 {
6070 --ga_userinput.ga_len;
6071 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6072 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006073 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006074 }
6075 else if (p_verbose > 1)
6076 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006077 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006078 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006079 }
6080}
6081
6082/*
6083 * "inputsave()" function
6084 */
6085 static void
6086f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6087{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006088 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006089 if (ga_grow(&ga_userinput, 1) == OK)
6090 {
6091 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6092 + ga_userinput.ga_len);
6093 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006094 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006095 }
6096 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006097 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006098}
6099
6100/*
6101 * "inputsecret()" function
6102 */
6103 static void
6104f_inputsecret(typval_T *argvars, typval_T *rettv)
6105{
6106 ++cmdline_star;
6107 ++inputsecret_flag;
6108 f_input(argvars, rettv);
6109 --cmdline_star;
6110 --inputsecret_flag;
6111}
6112
6113/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01006114 * "interrupt()" function
6115 */
6116 static void
6117f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6118{
6119 got_int = TRUE;
6120}
6121
6122/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006123 * "invert(expr)" function
6124 */
6125 static void
6126f_invert(typval_T *argvars, typval_T *rettv)
6127{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006128 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006129}
6130
6131/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006132 * "islocked()" function
6133 */
6134 static void
6135f_islocked(typval_T *argvars, typval_T *rettv)
6136{
6137 lval_T lv;
6138 char_u *end;
6139 dictitem_T *di;
6140
6141 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006142 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006143 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006144 if (end != NULL && lv.ll_name != NULL)
6145 {
6146 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02006147 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006148 else
6149 {
6150 if (lv.ll_tv == NULL)
6151 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006152 di = find_var(lv.ll_name, NULL, TRUE);
6153 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006154 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006155 // Consider a variable locked when:
6156 // 1. the variable itself is locked
6157 // 2. the value of the variable is locked.
6158 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01006159 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6160 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006161 }
6162 }
6163 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006164 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006165 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006166 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006167 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006168 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006169 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6170 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006171 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006172 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6173 }
6174 }
6175
6176 clear_lval(&lv);
6177}
6178
6179#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6180/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006181 * "isinf()" function
6182 */
6183 static void
6184f_isinf(typval_T *argvars, typval_T *rettv)
6185{
6186 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6187 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6188}
6189
6190/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006191 * "isnan()" function
6192 */
6193 static void
6194f_isnan(typval_T *argvars, typval_T *rettv)
6195{
6196 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6197 && isnan(argvars[0].vval.v_float);
6198}
6199#endif
6200
6201/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006202 * "last_buffer_nr()" function.
6203 */
6204 static void
6205f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6206{
6207 int n = 0;
6208 buf_T *buf;
6209
Bram Moolenaar29323592016-07-24 22:04:11 +02006210 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006211 if (n < buf->b_fnum)
6212 n = buf->b_fnum;
6213
6214 rettv->vval.v_number = n;
6215}
6216
6217/*
6218 * "len()" function
6219 */
6220 static void
6221f_len(typval_T *argvars, typval_T *rettv)
6222{
6223 switch (argvars[0].v_type)
6224 {
6225 case VAR_STRING:
6226 case VAR_NUMBER:
6227 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006228 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006229 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006230 case VAR_BLOB:
6231 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6232 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006233 case VAR_LIST:
6234 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6235 break;
6236 case VAR_DICT:
6237 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6238 break;
6239 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006240 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006241 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006242 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006243 case VAR_SPECIAL:
6244 case VAR_FLOAT:
6245 case VAR_FUNC:
6246 case VAR_PARTIAL:
6247 case VAR_JOB:
6248 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006249 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006250 break;
6251 }
6252}
6253
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006254 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006255libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006256{
6257#ifdef FEAT_LIBCALL
6258 char_u *string_in;
6259 char_u **string_result;
6260 int nr_result;
6261#endif
6262
6263 rettv->v_type = type;
6264 if (type != VAR_NUMBER)
6265 rettv->vval.v_string = NULL;
6266
6267 if (check_restricted() || check_secure())
6268 return;
6269
6270#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006271 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006272 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6273 {
6274 string_in = NULL;
6275 if (argvars[2].v_type == VAR_STRING)
6276 string_in = argvars[2].vval.v_string;
6277 if (type == VAR_NUMBER)
6278 string_result = NULL;
6279 else
6280 string_result = &rettv->vval.v_string;
6281 if (mch_libcall(argvars[0].vval.v_string,
6282 argvars[1].vval.v_string,
6283 string_in,
6284 argvars[2].vval.v_number,
6285 string_result,
6286 &nr_result) == OK
6287 && type == VAR_NUMBER)
6288 rettv->vval.v_number = nr_result;
6289 }
6290#endif
6291}
6292
6293/*
6294 * "libcall()" function
6295 */
6296 static void
6297f_libcall(typval_T *argvars, typval_T *rettv)
6298{
6299 libcall_common(argvars, rettv, VAR_STRING);
6300}
6301
6302/*
6303 * "libcallnr()" function
6304 */
6305 static void
6306f_libcallnr(typval_T *argvars, typval_T *rettv)
6307{
6308 libcall_common(argvars, rettv, VAR_NUMBER);
6309}
6310
6311/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006312 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006313 */
6314 static void
6315f_line(typval_T *argvars, typval_T *rettv)
6316{
6317 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006318 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006319 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006320 int id;
6321 tabpage_T *tp;
6322 win_T *wp;
6323 win_T *save_curwin;
6324 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006325
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006326 if (argvars[1].v_type != VAR_UNKNOWN)
6327 {
6328 // use window specified in the second argument
6329 id = (int)tv_get_number(&argvars[1]);
6330 wp = win_id2wp_tp(id, &tp);
6331 if (wp != NULL && tp != NULL)
6332 {
6333 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6334 == OK)
6335 {
6336 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006337 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006338 }
6339 restore_win_noblock(save_curwin, save_curtab, TRUE);
6340 }
6341 }
6342 else
6343 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006344 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006345
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006346 if (fp != NULL)
6347 lnum = fp->lnum;
6348 rettv->vval.v_number = lnum;
6349}
6350
6351/*
6352 * "line2byte(lnum)" function
6353 */
6354 static void
6355f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6356{
6357#ifndef FEAT_BYTEOFF
6358 rettv->vval.v_number = -1;
6359#else
6360 linenr_T lnum;
6361
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006362 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006363 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6364 rettv->vval.v_number = -1;
6365 else
6366 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6367 if (rettv->vval.v_number >= 0)
6368 ++rettv->vval.v_number;
6369#endif
6370}
6371
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006372#ifdef FEAT_FLOAT
6373/*
6374 * "log()" function
6375 */
6376 static void
6377f_log(typval_T *argvars, typval_T *rettv)
6378{
6379 float_T f = 0.0;
6380
6381 rettv->v_type = VAR_FLOAT;
6382 if (get_float_arg(argvars, &f) == OK)
6383 rettv->vval.v_float = log(f);
6384 else
6385 rettv->vval.v_float = 0.0;
6386}
6387
6388/*
6389 * "log10()" function
6390 */
6391 static void
6392f_log10(typval_T *argvars, typval_T *rettv)
6393{
6394 float_T f = 0.0;
6395
6396 rettv->v_type = VAR_FLOAT;
6397 if (get_float_arg(argvars, &f) == OK)
6398 rettv->vval.v_float = log10(f);
6399 else
6400 rettv->vval.v_float = 0.0;
6401}
6402#endif
6403
6404#ifdef FEAT_LUA
6405/*
6406 * "luaeval()" function
6407 */
6408 static void
6409f_luaeval(typval_T *argvars, typval_T *rettv)
6410{
6411 char_u *str;
6412 char_u buf[NUMBUFLEN];
6413
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006414 if (check_restricted() || check_secure())
6415 return;
6416
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006417 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006418 do_luaeval(str, argvars + 1, rettv);
6419}
6420#endif
6421
6422/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006423 * "maparg()" function
6424 */
6425 static void
6426f_maparg(typval_T *argvars, typval_T *rettv)
6427{
6428 get_maparg(argvars, rettv, TRUE);
6429}
6430
6431/*
6432 * "mapcheck()" function
6433 */
6434 static void
6435f_mapcheck(typval_T *argvars, typval_T *rettv)
6436{
6437 get_maparg(argvars, rettv, FALSE);
6438}
6439
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006440typedef enum
6441{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006442 MATCH_END, // matchend()
6443 MATCH_MATCH, // match()
6444 MATCH_STR, // matchstr()
6445 MATCH_LIST, // matchlist()
6446 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006447} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006448
6449 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006450find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006451{
6452 char_u *str = NULL;
6453 long len = 0;
6454 char_u *expr = NULL;
6455 char_u *pat;
6456 regmatch_T regmatch;
6457 char_u patbuf[NUMBUFLEN];
6458 char_u strbuf[NUMBUFLEN];
6459 char_u *save_cpo;
6460 long start = 0;
6461 long nth = 1;
6462 colnr_T startcol = 0;
6463 int match = 0;
6464 list_T *l = NULL;
6465 listitem_T *li = NULL;
6466 long idx = 0;
6467 char_u *tofree = NULL;
6468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006469 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006470 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006471 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006472
6473 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006474 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006475 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006476 // type MATCH_LIST: return empty list when there are no matches.
6477 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006478 if (rettv_list_alloc(rettv) == FAIL)
6479 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006480 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006481 && (list_append_string(rettv->vval.v_list,
6482 (char_u *)"", 0) == FAIL
6483 || list_append_number(rettv->vval.v_list,
6484 (varnumber_T)-1) == FAIL
6485 || list_append_number(rettv->vval.v_list,
6486 (varnumber_T)-1) == FAIL
6487 || list_append_number(rettv->vval.v_list,
6488 (varnumber_T)-1) == FAIL))
6489 {
6490 list_free(rettv->vval.v_list);
6491 rettv->vval.v_list = NULL;
6492 goto theend;
6493 }
6494 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006495 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006496 {
6497 rettv->v_type = VAR_STRING;
6498 rettv->vval.v_string = NULL;
6499 }
6500
6501 if (argvars[0].v_type == VAR_LIST)
6502 {
6503 if ((l = argvars[0].vval.v_list) == NULL)
6504 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006505 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506 li = l->lv_first;
6507 }
6508 else
6509 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006510 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006511 len = (long)STRLEN(str);
6512 }
6513
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006514 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006515 if (pat == NULL)
6516 goto theend;
6517
6518 if (argvars[2].v_type != VAR_UNKNOWN)
6519 {
6520 int error = FALSE;
6521
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006522 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006523 if (error)
6524 goto theend;
6525 if (l != NULL)
6526 {
6527 li = list_find(l, start);
6528 if (li == NULL)
6529 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006530 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006531 }
6532 else
6533 {
6534 if (start < 0)
6535 start = 0;
6536 if (start > len)
6537 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006538 // When "count" argument is there ignore matches before "start",
6539 // otherwise skip part of the string. Differs when pattern is "^"
6540 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006541 if (argvars[3].v_type != VAR_UNKNOWN)
6542 startcol = start;
6543 else
6544 {
6545 str += start;
6546 len -= start;
6547 }
6548 }
6549
6550 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006551 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006552 if (error)
6553 goto theend;
6554 }
6555
6556 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6557 if (regmatch.regprog != NULL)
6558 {
6559 regmatch.rm_ic = p_ic;
6560
6561 for (;;)
6562 {
6563 if (l != NULL)
6564 {
6565 if (li == NULL)
6566 {
6567 match = FALSE;
6568 break;
6569 }
6570 vim_free(tofree);
6571 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6572 if (str == NULL)
6573 break;
6574 }
6575
6576 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6577
6578 if (match && --nth <= 0)
6579 break;
6580 if (l == NULL && !match)
6581 break;
6582
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006583 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006584 if (l != NULL)
6585 {
6586 li = li->li_next;
6587 ++idx;
6588 }
6589 else
6590 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006591 startcol = (colnr_T)(regmatch.startp[0]
6592 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006593 if (startcol > (colnr_T)len
6594 || str + startcol <= regmatch.startp[0])
6595 {
6596 match = FALSE;
6597 break;
6598 }
6599 }
6600 }
6601
6602 if (match)
6603 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006604 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006605 {
6606 listitem_T *li1 = rettv->vval.v_list->lv_first;
6607 listitem_T *li2 = li1->li_next;
6608 listitem_T *li3 = li2->li_next;
6609 listitem_T *li4 = li3->li_next;
6610
6611 vim_free(li1->li_tv.vval.v_string);
6612 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006613 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006614 li3->li_tv.vval.v_number =
6615 (varnumber_T)(regmatch.startp[0] - expr);
6616 li4->li_tv.vval.v_number =
6617 (varnumber_T)(regmatch.endp[0] - expr);
6618 if (l != NULL)
6619 li2->li_tv.vval.v_number = (varnumber_T)idx;
6620 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006621 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006622 {
6623 int i;
6624
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006625 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006626 for (i = 0; i < NSUBEXP; ++i)
6627 {
6628 if (regmatch.endp[i] == NULL)
6629 {
6630 if (list_append_string(rettv->vval.v_list,
6631 (char_u *)"", 0) == FAIL)
6632 break;
6633 }
6634 else if (list_append_string(rettv->vval.v_list,
6635 regmatch.startp[i],
6636 (int)(regmatch.endp[i] - regmatch.startp[i]))
6637 == FAIL)
6638 break;
6639 }
6640 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006641 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006642 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006643 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006644 if (l != NULL)
6645 copy_tv(&li->li_tv, rettv);
6646 else
6647 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006648 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006649 }
6650 else if (l != NULL)
6651 rettv->vval.v_number = idx;
6652 else
6653 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006654 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006655 rettv->vval.v_number =
6656 (varnumber_T)(regmatch.startp[0] - str);
6657 else
6658 rettv->vval.v_number =
6659 (varnumber_T)(regmatch.endp[0] - str);
6660 rettv->vval.v_number += (varnumber_T)(str - expr);
6661 }
6662 }
6663 vim_regfree(regmatch.regprog);
6664 }
6665
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006666theend:
6667 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006668 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006669 listitem_remove(rettv->vval.v_list,
6670 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006671 vim_free(tofree);
6672 p_cpo = save_cpo;
6673}
6674
6675/*
6676 * "match()" function
6677 */
6678 static void
6679f_match(typval_T *argvars, typval_T *rettv)
6680{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006681 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006682}
6683
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006684/*
6685 * "matchend()" function
6686 */
6687 static void
6688f_matchend(typval_T *argvars, typval_T *rettv)
6689{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006690 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006691}
6692
6693/*
6694 * "matchlist()" function
6695 */
6696 static void
6697f_matchlist(typval_T *argvars, typval_T *rettv)
6698{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006699 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006700}
6701
6702/*
6703 * "matchstr()" function
6704 */
6705 static void
6706f_matchstr(typval_T *argvars, typval_T *rettv)
6707{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006708 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006709}
6710
6711/*
6712 * "matchstrpos()" function
6713 */
6714 static void
6715f_matchstrpos(typval_T *argvars, typval_T *rettv)
6716{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006717 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006718}
6719
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006720 static void
6721max_min(typval_T *argvars, typval_T *rettv, int domax)
6722{
6723 varnumber_T n = 0;
6724 varnumber_T i;
6725 int error = FALSE;
6726
6727 if (argvars[0].v_type == VAR_LIST)
6728 {
6729 list_T *l;
6730 listitem_T *li;
6731
6732 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006733 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006734 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006735 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006736 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006737 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6738 n = l->lv_u.nonmat.lv_start;
6739 else
6740 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6741 * l->lv_u.nonmat.lv_stride;
6742 }
6743 else
6744 {
6745 li = l->lv_first;
6746 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006747 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006748 n = tv_get_number_chk(&li->li_tv, &error);
6749 for (;;)
6750 {
6751 li = li->li_next;
6752 if (li == NULL)
6753 break;
6754 i = tv_get_number_chk(&li->li_tv, &error);
6755 if (domax ? i > n : i < n)
6756 n = i;
6757 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006758 }
6759 }
6760 }
6761 }
6762 else if (argvars[0].v_type == VAR_DICT)
6763 {
6764 dict_T *d;
6765 int first = TRUE;
6766 hashitem_T *hi;
6767 int todo;
6768
6769 d = argvars[0].vval.v_dict;
6770 if (d != NULL)
6771 {
6772 todo = (int)d->dv_hashtab.ht_used;
6773 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6774 {
6775 if (!HASHITEM_EMPTY(hi))
6776 {
6777 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006778 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006779 if (first)
6780 {
6781 n = i;
6782 first = FALSE;
6783 }
6784 else if (domax ? i > n : i < n)
6785 n = i;
6786 }
6787 }
6788 }
6789 }
6790 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006791 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006792 rettv->vval.v_number = error ? 0 : n;
6793}
6794
6795/*
6796 * "max()" function
6797 */
6798 static void
6799f_max(typval_T *argvars, typval_T *rettv)
6800{
6801 max_min(argvars, rettv, TRUE);
6802}
6803
6804/*
6805 * "min()" function
6806 */
6807 static void
6808f_min(typval_T *argvars, typval_T *rettv)
6809{
6810 max_min(argvars, rettv, FALSE);
6811}
6812
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006813#if defined(FEAT_MZSCHEME) || defined(PROTO)
6814/*
6815 * "mzeval()" function
6816 */
6817 static void
6818f_mzeval(typval_T *argvars, typval_T *rettv)
6819{
6820 char_u *str;
6821 char_u buf[NUMBUFLEN];
6822
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006823 if (check_restricted() || check_secure())
6824 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006825 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006826 do_mzeval(str, rettv);
6827}
6828
6829 void
6830mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6831{
6832 typval_T argvars[3];
6833
6834 argvars[0].v_type = VAR_STRING;
6835 argvars[0].vval.v_string = name;
6836 copy_tv(args, &argvars[1]);
6837 argvars[2].v_type = VAR_UNKNOWN;
6838 f_call(argvars, rettv);
6839 clear_tv(&argvars[1]);
6840}
6841#endif
6842
6843/*
6844 * "nextnonblank()" function
6845 */
6846 static void
6847f_nextnonblank(typval_T *argvars, typval_T *rettv)
6848{
6849 linenr_T lnum;
6850
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006851 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006852 {
6853 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6854 {
6855 lnum = 0;
6856 break;
6857 }
6858 if (*skipwhite(ml_get(lnum)) != NUL)
6859 break;
6860 }
6861 rettv->vval.v_number = lnum;
6862}
6863
6864/*
6865 * "nr2char()" function
6866 */
6867 static void
6868f_nr2char(typval_T *argvars, typval_T *rettv)
6869{
6870 char_u buf[NUMBUFLEN];
6871
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006872 if (has_mbyte)
6873 {
6874 int utf8 = 0;
6875
6876 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006877 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006878 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006879 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006880 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006881 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006882 }
6883 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006884 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006885 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006886 buf[1] = NUL;
6887 }
6888 rettv->v_type = VAR_STRING;
6889 rettv->vval.v_string = vim_strsave(buf);
6890}
6891
6892/*
6893 * "or(expr, expr)" function
6894 */
6895 static void
6896f_or(typval_T *argvars, typval_T *rettv)
6897{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006898 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6899 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900}
6901
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006902#ifdef FEAT_PERL
6903/*
6904 * "perleval()" function
6905 */
6906 static void
6907f_perleval(typval_T *argvars, typval_T *rettv)
6908{
6909 char_u *str;
6910 char_u buf[NUMBUFLEN];
6911
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006912 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006913 do_perleval(str, rettv);
6914}
6915#endif
6916
6917#ifdef FEAT_FLOAT
6918/*
6919 * "pow()" function
6920 */
6921 static void
6922f_pow(typval_T *argvars, typval_T *rettv)
6923{
6924 float_T fx = 0.0, fy = 0.0;
6925
6926 rettv->v_type = VAR_FLOAT;
6927 if (get_float_arg(argvars, &fx) == OK
6928 && get_float_arg(&argvars[1], &fy) == OK)
6929 rettv->vval.v_float = pow(fx, fy);
6930 else
6931 rettv->vval.v_float = 0.0;
6932}
6933#endif
6934
6935/*
6936 * "prevnonblank()" function
6937 */
6938 static void
6939f_prevnonblank(typval_T *argvars, typval_T *rettv)
6940{
6941 linenr_T lnum;
6942
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006943 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006944 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6945 lnum = 0;
6946 else
6947 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6948 --lnum;
6949 rettv->vval.v_number = lnum;
6950}
6951
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006952// This dummy va_list is here because:
6953// - passing a NULL pointer doesn't work when va_list isn't a pointer
6954// - locally in the function results in a "used before set" warning
6955// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006956static va_list ap;
6957
6958/*
6959 * "printf()" function
6960 */
6961 static void
6962f_printf(typval_T *argvars, typval_T *rettv)
6963{
6964 char_u buf[NUMBUFLEN];
6965 int len;
6966 char_u *s;
6967 int saved_did_emsg = did_emsg;
6968 char *fmt;
6969
6970 rettv->v_type = VAR_STRING;
6971 rettv->vval.v_string = NULL;
6972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006973 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006974 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006975 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006976 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006977 if (!did_emsg)
6978 {
6979 s = alloc(len + 1);
6980 if (s != NULL)
6981 {
6982 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006983 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
6984 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006985 }
6986 }
6987 did_emsg |= saved_did_emsg;
6988}
6989
6990/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006991 * "pum_getpos()" function
6992 */
6993 static void
6994f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6995{
6996 if (rettv_dict_alloc(rettv) != OK)
6997 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006998 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006999}
7000
7001/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007002 * "pumvisible()" function
7003 */
7004 static void
7005f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7006{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007007 if (pum_visible())
7008 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007009}
7010
7011#ifdef FEAT_PYTHON3
7012/*
7013 * "py3eval()" function
7014 */
7015 static void
7016f_py3eval(typval_T *argvars, typval_T *rettv)
7017{
7018 char_u *str;
7019 char_u buf[NUMBUFLEN];
7020
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007021 if (check_restricted() || check_secure())
7022 return;
7023
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007024 if (p_pyx == 0)
7025 p_pyx = 3;
7026
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007027 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028 do_py3eval(str, rettv);
7029}
7030#endif
7031
7032#ifdef FEAT_PYTHON
7033/*
7034 * "pyeval()" function
7035 */
7036 static void
7037f_pyeval(typval_T *argvars, typval_T *rettv)
7038{
7039 char_u *str;
7040 char_u buf[NUMBUFLEN];
7041
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007042 if (check_restricted() || check_secure())
7043 return;
7044
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007045 if (p_pyx == 0)
7046 p_pyx = 2;
7047
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007048 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007049 do_pyeval(str, rettv);
7050}
7051#endif
7052
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007053#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
7054/*
7055 * "pyxeval()" function
7056 */
7057 static void
7058f_pyxeval(typval_T *argvars, typval_T *rettv)
7059{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007060 if (check_restricted() || check_secure())
7061 return;
7062
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007063# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
7064 init_pyxversion();
7065 if (p_pyx == 2)
7066 f_pyeval(argvars, rettv);
7067 else
7068 f_py3eval(argvars, rettv);
7069# elif defined(FEAT_PYTHON)
7070 f_pyeval(argvars, rettv);
7071# elif defined(FEAT_PYTHON3)
7072 f_py3eval(argvars, rettv);
7073# endif
7074}
7075#endif
7076
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007077static UINT32_T srand_seed_for_testing = 0;
7078static int srand_seed_for_testing_is_used = FALSE;
7079
7080 static void
7081f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
7082{
7083 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007084 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007085 else
7086 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007087 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
7088 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007089 }
7090}
7091
7092 static void
7093init_srand(UINT32_T *x)
7094{
7095#ifndef MSWIN
7096 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
7097#endif
7098
7099 if (srand_seed_for_testing_is_used)
7100 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007101 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007102 return;
7103 }
7104#ifndef MSWIN
7105 if (dev_urandom_state != FAIL)
7106 {
7107 int fd = open("/dev/urandom", O_RDONLY);
7108 struct {
7109 union {
7110 UINT32_T number;
7111 char bytes[sizeof(UINT32_T)];
7112 } contents;
7113 } buf;
7114
7115 // Attempt reading /dev/urandom.
7116 if (fd == -1)
7117 dev_urandom_state = FAIL;
7118 else
7119 {
7120 buf.contents.number = 0;
7121 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
7122 != sizeof(UINT32_T))
7123 dev_urandom_state = FAIL;
7124 else
7125 {
7126 dev_urandom_state = OK;
7127 *x = buf.contents.number;
7128 }
7129 close(fd);
7130 }
7131 }
7132 if (dev_urandom_state != OK)
7133 // Reading /dev/urandom doesn't work, fall back to time().
7134#endif
7135 *x = vim_time();
7136}
7137
7138#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
7139#define SPLITMIX32(x, z) ( \
7140 z = (x += 0x9e3779b9), \
7141 z = (z ^ (z >> 16)) * 0x85ebca6b, \
7142 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
7143 z ^ (z >> 16) \
7144 )
7145#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
7146 result = ROTL(y * 5, 7) * 9; \
7147 t = y << 9; \
7148 z ^= x; \
7149 w ^= y; \
7150 y ^= z, x ^= w; \
7151 z ^= t; \
7152 w = ROTL(w, 11);
7153
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007154/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007155 * "rand()" function
7156 */
7157 static void
7158f_rand(typval_T *argvars, typval_T *rettv)
7159{
7160 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007161 static UINT32_T gx, gy, gz, gw;
7162 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007163 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01007164 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007165
7166 if (argvars[0].v_type == VAR_UNKNOWN)
7167 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007168 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007169 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007170 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007171 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007172 init_srand(&x);
7173
7174 gx = SPLITMIX32(x, z);
7175 gy = SPLITMIX32(x, z);
7176 gz = SPLITMIX32(x, z);
7177 gw = SPLITMIX32(x, z);
7178 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007179 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007180
7181 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007182 }
7183 else if (argvars[0].v_type == VAR_LIST)
7184 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007185 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007186 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007187 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007188
7189 lx = list_find(l, 0L);
7190 ly = list_find(l, 1L);
7191 lz = list_find(l, 2L);
7192 lw = list_find(l, 3L);
7193 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
7194 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
7195 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
7196 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
7197 x = (UINT32_T)lx->li_tv.vval.v_number;
7198 y = (UINT32_T)ly->li_tv.vval.v_number;
7199 z = (UINT32_T)lz->li_tv.vval.v_number;
7200 w = (UINT32_T)lw->li_tv.vval.v_number;
7201
7202 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
7203
7204 lx->li_tv.vval.v_number = (varnumber_T)x;
7205 ly->li_tv.vval.v_number = (varnumber_T)y;
7206 lz->li_tv.vval.v_number = (varnumber_T)z;
7207 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007208 }
7209 else
7210 goto theend;
7211
7212 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007213 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007214 return;
7215
7216theend:
7217 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007218 rettv->v_type = VAR_NUMBER;
7219 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007220}
7221
7222/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007223 * "srand()" function
7224 */
7225 static void
7226f_srand(typval_T *argvars, typval_T *rettv)
7227{
7228 UINT32_T x = 0, z;
7229
7230 if (rettv_list_alloc(rettv) == FAIL)
7231 return;
7232 if (argvars[0].v_type == VAR_UNKNOWN)
7233 {
7234 init_srand(&x);
7235 }
7236 else
7237 {
7238 int error = FALSE;
7239
7240 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7241 if (error)
7242 return;
7243 }
7244
7245 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7246 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7247 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7248 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7249}
7250
7251#undef ROTL
7252#undef SPLITMIX32
7253#undef SHUFFLE_XOSHIRO128STARSTAR
7254
7255/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007256 * "range()" function
7257 */
7258 static void
7259f_range(typval_T *argvars, typval_T *rettv)
7260{
7261 varnumber_T start;
7262 varnumber_T end;
7263 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007264 int error = FALSE;
7265
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007266 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007267 if (argvars[1].v_type == VAR_UNKNOWN)
7268 {
7269 end = start - 1;
7270 start = 0;
7271 }
7272 else
7273 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007274 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007275 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007276 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007277 }
7278
7279 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007280 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007281 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007282 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007283 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007284 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007285 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007286 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007287 list_T *list = rettv->vval.v_list;
7288
7289 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007290 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007291 // be called.
7292 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007293 list->lv_u.nonmat.lv_start = start;
7294 list->lv_u.nonmat.lv_end = end;
7295 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007296 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007297 }
7298}
7299
7300/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007301 * Materialize "list".
7302 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007303 */
7304 void
7305range_list_materialize(list_T *list)
7306{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007307 varnumber_T start = list->lv_u.nonmat.lv_start;
7308 varnumber_T end = list->lv_u.nonmat.lv_end;
7309 int stride = list->lv_u.nonmat.lv_stride;
7310 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007311
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007312 list->lv_first = NULL;
7313 list->lv_u.mat.lv_last = NULL;
7314 list->lv_len = 0;
7315 list->lv_u.mat.lv_idx_item = NULL;
7316 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7317 if (list_append_number(list, (varnumber_T)i) == FAIL)
7318 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007319}
7320
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007321/*
7322 * "getreginfo()" function
7323 */
7324 static void
7325f_getreginfo(typval_T *argvars, typval_T *rettv)
7326{
7327 char_u *strregname;
7328 int regname;
7329 char_u buf[NUMBUFLEN + 2];
7330 long reglen = 0;
7331 dict_T *dict;
7332 list_T *list;
7333
7334 if (argvars[0].v_type != VAR_UNKNOWN)
7335 {
7336 strregname = tv_get_string_chk(&argvars[0]);
7337 if (strregname == NULL)
7338 return;
7339 }
7340 else
7341 strregname = get_vim_var_str(VV_REG);
7342
7343 regname = (strregname == NULL ? '"' : *strregname);
7344 if (regname == 0 || regname == '@')
7345 regname = '"';
7346
7347 if (rettv_dict_alloc(rettv) == FAIL)
7348 return;
7349 dict = rettv->vval.v_dict;
7350
7351 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7352 if (list == NULL)
7353 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007354 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007355
7356 buf[0] = NUL;
7357 buf[1] = NUL;
7358 switch (get_reg_type(regname, &reglen))
7359 {
7360 case MLINE: buf[0] = 'V'; break;
7361 case MCHAR: buf[0] = 'v'; break;
7362 case MBLOCK:
7363 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7364 reglen + 1);
7365 break;
7366 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007367 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007368
7369 buf[0] = get_register_name(get_unname_register());
7370 buf[1] = NUL;
7371 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007372 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007373 else
7374 {
7375 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7376
7377 if (item != NULL)
7378 {
7379 item->di_tv.v_type = VAR_SPECIAL;
7380 item->di_tv.vval.v_number = regname == buf[0]
7381 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007382 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007383 }
7384 }
7385}
7386
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007387 static void
7388return_register(int regname, typval_T *rettv)
7389{
7390 char_u buf[2] = {0, 0};
7391
7392 buf[0] = (char_u)regname;
7393 rettv->v_type = VAR_STRING;
7394 rettv->vval.v_string = vim_strsave(buf);
7395}
7396
7397/*
7398 * "reg_executing()" function
7399 */
7400 static void
7401f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7402{
7403 return_register(reg_executing, rettv);
7404}
7405
7406/*
7407 * "reg_recording()" function
7408 */
7409 static void
7410f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7411{
7412 return_register(reg_recording, rettv);
7413}
7414
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007415/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007416 * "rename({from}, {to})" function
7417 */
7418 static void
7419f_rename(typval_T *argvars, typval_T *rettv)
7420{
7421 char_u buf[NUMBUFLEN];
7422
7423 if (check_restricted() || check_secure())
7424 rettv->vval.v_number = -1;
7425 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007426 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7427 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007428}
7429
7430/*
7431 * "repeat()" function
7432 */
7433 static void
7434f_repeat(typval_T *argvars, typval_T *rettv)
7435{
7436 char_u *p;
7437 int n;
7438 int slen;
7439 int len;
7440 char_u *r;
7441 int i;
7442
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007443 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007444 if (argvars[0].v_type == VAR_LIST)
7445 {
7446 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7447 while (n-- > 0)
7448 if (list_extend(rettv->vval.v_list,
7449 argvars[0].vval.v_list, NULL) == FAIL)
7450 break;
7451 }
7452 else
7453 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007454 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007455 rettv->v_type = VAR_STRING;
7456 rettv->vval.v_string = NULL;
7457
7458 slen = (int)STRLEN(p);
7459 len = slen * n;
7460 if (len <= 0)
7461 return;
7462
7463 r = alloc(len + 1);
7464 if (r != NULL)
7465 {
7466 for (i = 0; i < n; i++)
7467 mch_memmove(r + i * slen, p, (size_t)slen);
7468 r[len] = NUL;
7469 }
7470
7471 rettv->vval.v_string = r;
7472 }
7473}
7474
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007475#define SP_NOMOVE 0x01 // don't move cursor
7476#define SP_REPEAT 0x02 // repeat to find outer pair
7477#define SP_RETCOUNT 0x04 // return matchcount
7478#define SP_SETPCMARK 0x08 // set previous context mark
7479#define SP_START 0x10 // accept match at start position
7480#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7481#define SP_END 0x40 // leave cursor at end of match
7482#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007483
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007484/*
7485 * Get flags for a search function.
7486 * Possibly sets "p_ws".
7487 * Returns BACKWARD, FORWARD or zero (for an error).
7488 */
7489 static int
7490get_search_arg(typval_T *varp, int *flagsp)
7491{
7492 int dir = FORWARD;
7493 char_u *flags;
7494 char_u nbuf[NUMBUFLEN];
7495 int mask;
7496
7497 if (varp->v_type != VAR_UNKNOWN)
7498 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007499 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007500 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007501 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007502 while (*flags != NUL)
7503 {
7504 switch (*flags)
7505 {
7506 case 'b': dir = BACKWARD; break;
7507 case 'w': p_ws = TRUE; break;
7508 case 'W': p_ws = FALSE; break;
7509 default: mask = 0;
7510 if (flagsp != NULL)
7511 switch (*flags)
7512 {
7513 case 'c': mask = SP_START; break;
7514 case 'e': mask = SP_END; break;
7515 case 'm': mask = SP_RETCOUNT; break;
7516 case 'n': mask = SP_NOMOVE; break;
7517 case 'p': mask = SP_SUBPAT; break;
7518 case 'r': mask = SP_REPEAT; break;
7519 case 's': mask = SP_SETPCMARK; break;
7520 case 'z': mask = SP_COLUMN; break;
7521 }
7522 if (mask == 0)
7523 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007524 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007525 dir = 0;
7526 }
7527 else
7528 *flagsp |= mask;
7529 }
7530 if (dir == 0)
7531 break;
7532 ++flags;
7533 }
7534 }
7535 return dir;
7536}
7537
7538/*
7539 * Shared by search() and searchpos() functions.
7540 */
7541 static int
7542search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7543{
7544 int flags;
7545 char_u *pat;
7546 pos_T pos;
7547 pos_T save_cursor;
7548 int save_p_ws = p_ws;
7549 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007550 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007551 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007552#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007553 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007554 long time_limit = 0;
7555#endif
7556 int options = SEARCH_KEEP;
7557 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007558 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007559 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007560 pos_T firstpos;
7561
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007562 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007563 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007564 if (dir == 0)
7565 goto theend;
7566 flags = *flagsp;
7567 if (flags & SP_START)
7568 options |= SEARCH_START;
7569 if (flags & SP_END)
7570 options |= SEARCH_END;
7571 if (flags & SP_COLUMN)
7572 options |= SEARCH_COL;
7573
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007574 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007575 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7576 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007577 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007578 if (lnum_stop < 0)
7579 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007580 if (argvars[3].v_type != VAR_UNKNOWN)
7581 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007582#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007583 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007584 if (time_limit < 0)
7585 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007586#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007587 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007588 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007589 }
7590
7591#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007592 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007593 profile_setlimit(time_limit, &tm);
7594#endif
7595
7596 /*
7597 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7598 * Check to make sure only those flags are set.
7599 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7600 * flags cannot be set. Check for that condition also.
7601 */
7602 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7603 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7604 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007605 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007606 goto theend;
7607 }
7608
7609 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007610 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007611 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007612 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7613#ifdef FEAT_RELTIME
7614 sia.sa_tm = &tm;
7615#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007616
7617 // Repeat until {skip} returns FALSE.
7618 for (;;)
7619 {
7620 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007621 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007622 // finding the first match again means there is no match where {skip}
7623 // evaluates to zero.
7624 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7625 subpatnum = FAIL;
7626
Bram Moolenaara9c01042020-06-07 14:50:50 +02007627 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007628 // didn't find it or no skip argument
7629 break;
7630 firstpos = pos;
7631
Bram Moolenaara9c01042020-06-07 14:50:50 +02007632 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007633 {
7634 int do_skip;
7635 int err;
7636 pos_T save_pos = curwin->w_cursor;
7637
7638 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007639 err = FALSE;
7640 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007641 curwin->w_cursor = save_pos;
7642 if (err)
7643 {
7644 // Evaluating {skip} caused an error, break here.
7645 subpatnum = FAIL;
7646 break;
7647 }
7648 if (!do_skip)
7649 break;
7650 }
7651 }
7652
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007653 if (subpatnum != FAIL)
7654 {
7655 if (flags & SP_SUBPAT)
7656 retval = subpatnum;
7657 else
7658 retval = pos.lnum;
7659 if (flags & SP_SETPCMARK)
7660 setpcmark();
7661 curwin->w_cursor = pos;
7662 if (match_pos != NULL)
7663 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007664 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007665 match_pos->lnum = pos.lnum;
7666 match_pos->col = pos.col + 1;
7667 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007668 // "/$" will put the cursor after the end of the line, may need to
7669 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007670 check_cursor();
7671 }
7672
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007673 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007674 if (flags & SP_NOMOVE)
7675 curwin->w_cursor = save_cursor;
7676 else
7677 curwin->w_set_curswant = TRUE;
7678theend:
7679 p_ws = save_p_ws;
7680
7681 return retval;
7682}
7683
7684#ifdef FEAT_FLOAT
7685
7686/*
7687 * round() is not in C90, use ceil() or floor() instead.
7688 */
7689 float_T
7690vim_round(float_T f)
7691{
7692 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
7693}
7694
7695/*
7696 * "round({float})" function
7697 */
7698 static void
7699f_round(typval_T *argvars, typval_T *rettv)
7700{
7701 float_T f = 0.0;
7702
7703 rettv->v_type = VAR_FLOAT;
7704 if (get_float_arg(argvars, &f) == OK)
7705 rettv->vval.v_float = vim_round(f);
7706 else
7707 rettv->vval.v_float = 0.0;
7708}
7709#endif
7710
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007711#ifdef FEAT_RUBY
7712/*
7713 * "rubyeval()" function
7714 */
7715 static void
7716f_rubyeval(typval_T *argvars, typval_T *rettv)
7717{
7718 char_u *str;
7719 char_u buf[NUMBUFLEN];
7720
7721 str = tv_get_string_buf(&argvars[0], buf);
7722 do_rubyeval(str, rettv);
7723}
7724#endif
7725
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007726/*
7727 * "screenattr()" function
7728 */
7729 static void
7730f_screenattr(typval_T *argvars, typval_T *rettv)
7731{
7732 int row;
7733 int col;
7734 int c;
7735
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007736 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7737 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007738 if (row < 0 || row >= screen_Rows
7739 || col < 0 || col >= screen_Columns)
7740 c = -1;
7741 else
7742 c = ScreenAttrs[LineOffset[row] + col];
7743 rettv->vval.v_number = c;
7744}
7745
7746/*
7747 * "screenchar()" function
7748 */
7749 static void
7750f_screenchar(typval_T *argvars, typval_T *rettv)
7751{
7752 int row;
7753 int col;
7754 int off;
7755 int c;
7756
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007757 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7758 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007759 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007760 c = -1;
7761 else
7762 {
7763 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007764 if (enc_utf8 && ScreenLinesUC[off] != 0)
7765 c = ScreenLinesUC[off];
7766 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007767 c = ScreenLines[off];
7768 }
7769 rettv->vval.v_number = c;
7770}
7771
7772/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007773 * "screenchars()" function
7774 */
7775 static void
7776f_screenchars(typval_T *argvars, typval_T *rettv)
7777{
7778 int row;
7779 int col;
7780 int off;
7781 int c;
7782 int i;
7783
7784 if (rettv_list_alloc(rettv) == FAIL)
7785 return;
7786 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7787 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7788 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7789 return;
7790
7791 off = LineOffset[row] + col;
7792 if (enc_utf8 && ScreenLinesUC[off] != 0)
7793 c = ScreenLinesUC[off];
7794 else
7795 c = ScreenLines[off];
7796 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7797
7798 if (enc_utf8)
7799
7800 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7801 list_append_number(rettv->vval.v_list,
7802 (varnumber_T)ScreenLinesC[i][off]);
7803}
7804
7805/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007806 * "screencol()" function
7807 *
7808 * First column is 1 to be consistent with virtcol().
7809 */
7810 static void
7811f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7812{
7813 rettv->vval.v_number = screen_screencol() + 1;
7814}
7815
7816/*
7817 * "screenrow()" function
7818 */
7819 static void
7820f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7821{
7822 rettv->vval.v_number = screen_screenrow() + 1;
7823}
7824
7825/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007826 * "screenstring()" function
7827 */
7828 static void
7829f_screenstring(typval_T *argvars, typval_T *rettv)
7830{
7831 int row;
7832 int col;
7833 int off;
7834 int c;
7835 int i;
7836 char_u buf[MB_MAXBYTES + 1];
7837 int buflen = 0;
7838
7839 rettv->vval.v_string = NULL;
7840 rettv->v_type = VAR_STRING;
7841
7842 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7843 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7844 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7845 return;
7846
7847 off = LineOffset[row] + col;
7848 if (enc_utf8 && ScreenLinesUC[off] != 0)
7849 c = ScreenLinesUC[off];
7850 else
7851 c = ScreenLines[off];
7852 buflen += mb_char2bytes(c, buf);
7853
7854 if (enc_utf8)
7855 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7856 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7857
7858 buf[buflen] = NUL;
7859 rettv->vval.v_string = vim_strsave(buf);
7860}
7861
7862/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007863 * "search()" function
7864 */
7865 static void
7866f_search(typval_T *argvars, typval_T *rettv)
7867{
7868 int flags = 0;
7869
7870 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7871}
7872
7873/*
7874 * "searchdecl()" function
7875 */
7876 static void
7877f_searchdecl(typval_T *argvars, typval_T *rettv)
7878{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007879 int locally = TRUE;
7880 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007881 int error = FALSE;
7882 char_u *name;
7883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007884 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007885
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007886 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007887 if (argvars[1].v_type != VAR_UNKNOWN)
7888 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007889 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007890 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007891 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007892 }
7893 if (!error && name != NULL)
7894 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7895 locally, thisblock, SEARCH_KEEP) == FAIL;
7896}
7897
7898/*
7899 * Used by searchpair() and searchpairpos()
7900 */
7901 static int
7902searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7903{
7904 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007905 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007906 int save_p_ws = p_ws;
7907 int dir;
7908 int flags = 0;
7909 char_u nbuf1[NUMBUFLEN];
7910 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007911 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007912 long lnum_stop = 0;
7913 long time_limit = 0;
7914
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007915 // Get the three pattern arguments: start, middle, end. Will result in an
7916 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007917 spat = tv_get_string_chk(&argvars[0]);
7918 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7919 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007920 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007921 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007922
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007923 // Handle the optional fourth argument: flags
7924 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007925 if (dir == 0)
7926 goto theend;
7927
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007928 // Don't accept SP_END or SP_SUBPAT.
7929 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007930 if ((flags & (SP_END | SP_SUBPAT)) != 0
7931 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7932 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007933 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007934 goto theend;
7935 }
7936
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007937 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007938 if (flags & SP_REPEAT)
7939 p_ws = FALSE;
7940
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007941 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007942 if (argvars[3].v_type == VAR_UNKNOWN
7943 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007944 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007945 else
7946 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007947 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007948 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007949
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007950 if (argvars[5].v_type != VAR_UNKNOWN)
7951 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007952 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007953 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007954 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007955 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007956 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007957 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007958#ifdef FEAT_RELTIME
7959 if (argvars[6].v_type != VAR_UNKNOWN)
7960 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007961 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007962 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007963 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007964 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007965 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007966 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007967 }
7968#endif
7969 }
7970 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007971
7972 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
7973 match_pos, lnum_stop, time_limit);
7974
7975theend:
7976 p_ws = save_p_ws;
7977
7978 return retval;
7979}
7980
7981/*
7982 * "searchpair()" function
7983 */
7984 static void
7985f_searchpair(typval_T *argvars, typval_T *rettv)
7986{
7987 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
7988}
7989
7990/*
7991 * "searchpairpos()" function
7992 */
7993 static void
7994f_searchpairpos(typval_T *argvars, typval_T *rettv)
7995{
7996 pos_T match_pos;
7997 int lnum = 0;
7998 int col = 0;
7999
8000 if (rettv_list_alloc(rettv) == FAIL)
8001 return;
8002
8003 if (searchpair_cmn(argvars, &match_pos) > 0)
8004 {
8005 lnum = match_pos.lnum;
8006 col = match_pos.col;
8007 }
8008
8009 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8010 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8011}
8012
8013/*
8014 * Search for a start/middle/end thing.
8015 * Used by searchpair(), see its documentation for the details.
8016 * Returns 0 or -1 for no match,
8017 */
8018 long
8019do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008020 char_u *spat, // start pattern
8021 char_u *mpat, // middle pattern
8022 char_u *epat, // end pattern
8023 int dir, // BACKWARD or FORWARD
8024 typval_T *skip, // skip expression
8025 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008026 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008027 linenr_T lnum_stop, // stop at this line if not zero
8028 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008029{
8030 char_u *save_cpo;
8031 char_u *pat, *pat2 = NULL, *pat3 = NULL;
8032 long retval = 0;
8033 pos_T pos;
8034 pos_T firstpos;
8035 pos_T foundpos;
8036 pos_T save_cursor;
8037 pos_T save_pos;
8038 int n;
8039 int r;
8040 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01008041 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008042 int err;
8043 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008044#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008045 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008046#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008047
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008048 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008049 save_cpo = p_cpo;
8050 p_cpo = empty_option;
8051
8052#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008053 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008054 profile_setlimit(time_limit, &tm);
8055#endif
8056
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008057 // Make two search patterns: start/end (pat2, for in nested pairs) and
8058 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02008059 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
8060 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008061 if (pat2 == NULL || pat3 == NULL)
8062 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008063 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008064 if (*mpat == NUL)
8065 STRCPY(pat3, pat2);
8066 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008067 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068 spat, epat, mpat);
8069 if (flags & SP_START)
8070 options |= SEARCH_START;
8071
Bram Moolenaar48570482017-10-30 21:48:41 +01008072 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02008073 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01008074
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008075 save_cursor = curwin->w_cursor;
8076 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008077 CLEAR_POS(&firstpos);
8078 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008079 pat = pat3;
8080 for (;;)
8081 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008082 searchit_arg_T sia;
8083
Bram Moolenaara80faa82020-04-12 19:37:17 +02008084 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008085 sia.sa_stop_lnum = lnum_stop;
8086#ifdef FEAT_RELTIME
8087 sia.sa_tm = &tm;
8088#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01008089 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008090 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008091 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008092 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008093 break;
8094
8095 if (firstpos.lnum == 0)
8096 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008097 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008098 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008099 // Found the same position again. Can happen with a pattern that
8100 // has "\zs" at the end and searching backwards. Advance one
8101 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008102 if (dir == BACKWARD)
8103 decl(&pos);
8104 else
8105 incl(&pos);
8106 }
8107 foundpos = pos;
8108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008109 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008110 options &= ~SEARCH_START;
8111
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008112 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01008113 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008114 {
8115 save_pos = curwin->w_cursor;
8116 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01008117 err = FALSE;
8118 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008119 curwin->w_cursor = save_pos;
8120 if (err)
8121 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008122 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008123 curwin->w_cursor = save_cursor;
8124 retval = -1;
8125 break;
8126 }
8127 if (r)
8128 continue;
8129 }
8130
8131 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8132 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008133 // Found end when searching backwards or start when searching
8134 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008135 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008136 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008137 }
8138 else
8139 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008140 // Found end when searching forward or start when searching
8141 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008142 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008143 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008144 }
8145
8146 if (nest == 0)
8147 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008148 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008149 if (flags & SP_RETCOUNT)
8150 ++retval;
8151 else
8152 retval = pos.lnum;
8153 if (flags & SP_SETPCMARK)
8154 setpcmark();
8155 curwin->w_cursor = pos;
8156 if (!(flags & SP_REPEAT))
8157 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008158 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008159 }
8160 }
8161
8162 if (match_pos != NULL)
8163 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008164 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165 match_pos->lnum = curwin->w_cursor.lnum;
8166 match_pos->col = curwin->w_cursor.col + 1;
8167 }
8168
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008169 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008170 if ((flags & SP_NOMOVE) || retval == 0)
8171 curwin->w_cursor = save_cursor;
8172
8173theend:
8174 vim_free(pat2);
8175 vim_free(pat3);
8176 if (p_cpo == empty_option)
8177 p_cpo = save_cpo;
8178 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008179 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008180 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008181 // If it's still empty it was changed and restored, need to restore in
8182 // the complicated way.
8183 if (*p_cpo == NUL)
8184 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008185 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008186 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008187
8188 return retval;
8189}
8190
8191/*
8192 * "searchpos()" function
8193 */
8194 static void
8195f_searchpos(typval_T *argvars, typval_T *rettv)
8196{
8197 pos_T match_pos;
8198 int lnum = 0;
8199 int col = 0;
8200 int n;
8201 int flags = 0;
8202
8203 if (rettv_list_alloc(rettv) == FAIL)
8204 return;
8205
8206 n = search_cmn(argvars, &match_pos, &flags);
8207 if (n > 0)
8208 {
8209 lnum = match_pos.lnum;
8210 col = match_pos.col;
8211 }
8212
8213 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8214 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8215 if (flags & SP_SUBPAT)
8216 list_append_number(rettv->vval.v_list, (varnumber_T)n);
8217}
8218
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008219/*
8220 * Set the cursor or mark position.
8221 * If 'charpos' is TRUE, then use the column number as a character offet.
8222 * Otherwise use the column number as a byte offset.
8223 */
8224 static void
8225set_position(typval_T *argvars, typval_T *rettv, int charpos)
8226{
8227 pos_T pos;
8228 int fnum;
8229 char_u *name;
8230 colnr_T curswant = -1;
8231
8232 rettv->vval.v_number = -1;
8233
8234 name = tv_get_string_chk(argvars);
8235 if (name != NULL)
8236 {
8237 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
8238 {
8239 if (pos.col != MAXCOL && --pos.col < 0)
8240 pos.col = 0;
8241 if ((name[0] == '.' && name[1] == NUL))
8242 {
8243 // set cursor; "fnum" is ignored
8244 curwin->w_cursor = pos;
8245 if (curswant >= 0)
8246 {
8247 curwin->w_curswant = curswant - 1;
8248 curwin->w_set_curswant = FALSE;
8249 }
8250 check_cursor();
8251 rettv->vval.v_number = 0;
8252 }
8253 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8254 {
8255 // set mark
8256 if (setmark_pos(name[1], &pos, fnum) == OK)
8257 rettv->vval.v_number = 0;
8258 }
8259 else
8260 emsg(_(e_invarg));
8261 }
8262 }
8263}
8264/*
8265 * "setcharpos()" function
8266 */
8267 static void
8268f_setcharpos(typval_T *argvars, typval_T *rettv)
8269{
8270 set_position(argvars, rettv, TRUE);
8271}
8272
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008273 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008274f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8275{
8276 dict_T *d;
8277 dictitem_T *di;
8278 char_u *csearch;
8279
8280 if (argvars[0].v_type != VAR_DICT)
8281 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008282 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008283 return;
8284 }
8285
8286 if ((d = argvars[0].vval.v_dict) != NULL)
8287 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008288 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289 if (csearch != NULL)
8290 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008291 if (enc_utf8)
8292 {
8293 int pcc[MAX_MCO];
8294 int c = utfc_ptr2char(csearch, pcc);
8295
8296 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8297 }
8298 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008299 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008300 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008301 }
8302
8303 di = dict_find(d, (char_u *)"forward", -1);
8304 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008305 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008306 ? FORWARD : BACKWARD);
8307
8308 di = dict_find(d, (char_u *)"until", -1);
8309 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008310 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008311 }
8312}
8313
8314/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008315 * "setcursorcharpos" function
8316 */
8317 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008318f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008319{
8320 set_cursorpos(argvars, rettv, TRUE);
8321}
8322
8323/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008324 * "setenv()" function
8325 */
8326 static void
8327f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8328{
8329 char_u namebuf[NUMBUFLEN];
8330 char_u valbuf[NUMBUFLEN];
8331 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8332
8333 if (argvars[1].v_type == VAR_SPECIAL
8334 && argvars[1].vval.v_number == VVAL_NULL)
8335 vim_unsetenv(name);
8336 else
8337 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8338}
8339
8340/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008341 * "setfperm({fname}, {mode})" function
8342 */
8343 static void
8344f_setfperm(typval_T *argvars, typval_T *rettv)
8345{
8346 char_u *fname;
8347 char_u modebuf[NUMBUFLEN];
8348 char_u *mode_str;
8349 int i;
8350 int mask;
8351 int mode = 0;
8352
8353 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008354 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008355 if (fname == NULL)
8356 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008357 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008358 if (mode_str == NULL)
8359 return;
8360 if (STRLEN(mode_str) != 9)
8361 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008362 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008363 return;
8364 }
8365
8366 mask = 1;
8367 for (i = 8; i >= 0; --i)
8368 {
8369 if (mode_str[i] != '-')
8370 mode |= mask;
8371 mask = mask << 1;
8372 }
8373 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8374}
8375
8376/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008377 * "setpos()" function
8378 */
8379 static void
8380f_setpos(typval_T *argvars, typval_T *rettv)
8381{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008382 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008383}
8384
8385/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008386 * Translate a register type string to the yank type and block length
8387 */
8388 static int
8389get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8390{
8391 char_u *stropt = *pp;
8392 switch (*stropt)
8393 {
8394 case 'v': case 'c': // character-wise selection
8395 *yank_type = MCHAR;
8396 break;
8397 case 'V': case 'l': // line-wise selection
8398 *yank_type = MLINE;
8399 break;
8400 case 'b': case Ctrl_V: // block-wise selection
8401 *yank_type = MBLOCK;
8402 if (VIM_ISDIGIT(stropt[1]))
8403 {
8404 ++stropt;
8405 *block_len = getdigits(&stropt) - 1;
8406 --stropt;
8407 }
8408 break;
8409 default:
8410 return FAIL;
8411 }
8412 *pp = stropt;
8413 return OK;
8414}
8415
8416/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008417 * "setreg()" function
8418 */
8419 static void
8420f_setreg(typval_T *argvars, typval_T *rettv)
8421{
8422 int regname;
8423 char_u *strregname;
8424 char_u *stropt;
8425 char_u *strval;
8426 int append;
8427 char_u yank_type;
8428 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008429 typval_T *regcontents;
8430 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008431
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008432 pointreg = 0;
8433 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008434 block_len = -1;
8435 yank_type = MAUTO;
8436 append = FALSE;
8437
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008438 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008439 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008440
8441 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008442 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008443 regname = *strregname;
8444 if (regname == 0 || regname == '@')
8445 regname = '"';
8446
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008447 if (argvars[1].v_type == VAR_DICT)
8448 {
8449 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008450 dictitem_T *di;
8451
8452 if (d == NULL || d->dv_hashtab.ht_used == 0)
8453 {
8454 // Empty dict, clear the register (like setreg(0, []))
8455 char_u *lstval[2] = {NULL, NULL};
8456 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8457 return;
8458 }
8459
8460 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008461 if (di != NULL)
8462 regcontents = &di->di_tv;
8463
8464 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8465 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008466 {
8467 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8468
8469 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008470 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008471 semsg(_(e_invargval), "value");
8472 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008473 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008474 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008475
8476 if (regname == '"')
8477 {
8478 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8479 if (stropt != NULL)
8480 {
8481 pointreg = *stropt;
8482 regname = pointreg;
8483 }
8484 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008485 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008486 pointreg = regname;
8487 }
8488 else
8489 regcontents = &argvars[1];
8490
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008491 if (argvars[2].v_type != VAR_UNKNOWN)
8492 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008493 if (yank_type != MAUTO)
8494 {
8495 semsg(_(e_toomanyarg), "setreg");
8496 return;
8497 }
8498
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008499 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008500 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008501 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008502 for (; *stropt != NUL; ++stropt)
8503 switch (*stropt)
8504 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008505 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008506 append = TRUE;
8507 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008508 default:
8509 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008510 }
8511 }
8512
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008513 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008514 {
8515 char_u **lstval;
8516 char_u **allocval;
8517 char_u buf[NUMBUFLEN];
8518 char_u **curval;
8519 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008520 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008521 listitem_T *li;
8522 int len;
8523
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008524 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008525 len = ll == NULL ? 0 : ll->lv_len;
8526
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008527 // First half: use for pointers to result lines; second half: use for
8528 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008529 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008530 if (lstval == NULL)
8531 return;
8532 curval = lstval;
8533 allocval = lstval + len + 2;
8534 curallocval = allocval;
8535
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008536 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008537 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008538 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008539 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008540 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008541 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008542 if (strval == NULL)
8543 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008544 if (strval == buf)
8545 {
8546 // Need to make a copy, next tv_get_string_buf_chk() will
8547 // overwrite the string.
8548 strval = vim_strsave(buf);
8549 if (strval == NULL)
8550 goto free_lstval;
8551 *curallocval++ = strval;
8552 }
8553 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008554 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008555 }
8556 *curval++ = NULL;
8557
8558 write_reg_contents_lst(regname, lstval, -1,
8559 append, yank_type, block_len);
8560free_lstval:
8561 while (curallocval > allocval)
8562 vim_free(*--curallocval);
8563 vim_free(lstval);
8564 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008565 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008566 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008567 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008568 if (strval == NULL)
8569 return;
8570 write_reg_contents_ex(regname, strval, -1,
8571 append, yank_type, block_len);
8572 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008573 if (pointreg != 0)
8574 get_yank_register(pointreg, TRUE);
8575
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008576 rettv->vval.v_number = 0;
8577}
8578
8579/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008580 * "settagstack()" function
8581 */
8582 static void
8583f_settagstack(typval_T *argvars, typval_T *rettv)
8584{
8585 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8586 win_T *wp;
8587 dict_T *d;
8588 int action = 'r';
8589
8590 rettv->vval.v_number = -1;
8591
8592 // first argument: window number or id
8593 wp = find_win_by_nr_or_id(&argvars[0]);
8594 if (wp == NULL)
8595 return;
8596
8597 // second argument: dict with items to set in the tag stack
8598 if (argvars[1].v_type != VAR_DICT)
8599 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008600 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008601 return;
8602 }
8603 d = argvars[1].vval.v_dict;
8604 if (d == NULL)
8605 return;
8606
8607 // third argument: action - 'a' for append and 'r' for replace.
8608 // default is to replace the stack.
8609 if (argvars[2].v_type == VAR_UNKNOWN)
8610 action = 'r';
8611 else if (argvars[2].v_type == VAR_STRING)
8612 {
8613 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008614 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008615 if (actstr == NULL)
8616 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008617 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8618 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008619 action = *actstr;
8620 else
8621 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008622 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008623 return;
8624 }
8625 }
8626 else
8627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008628 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008629 return;
8630 }
8631
8632 if (set_tagstack(wp, d, action) == OK)
8633 rettv->vval.v_number = 0;
8634}
8635
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008636#ifdef FEAT_CRYPT
8637/*
8638 * "sha256({string})" function
8639 */
8640 static void
8641f_sha256(typval_T *argvars, typval_T *rettv)
8642{
8643 char_u *p;
8644
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008645 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008646 rettv->vval.v_string = vim_strsave(
8647 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8648 rettv->v_type = VAR_STRING;
8649}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008650#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008651
8652/*
8653 * "shellescape({string})" function
8654 */
8655 static void
8656f_shellescape(typval_T *argvars, typval_T *rettv)
8657{
Bram Moolenaar20615522017-06-05 18:46:26 +02008658 int do_special = non_zero_arg(&argvars[1]);
8659
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008660 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008661 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008662 rettv->v_type = VAR_STRING;
8663}
8664
8665/*
8666 * shiftwidth() function
8667 */
8668 static void
8669f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8670{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008671 rettv->vval.v_number = 0;
8672
8673 if (argvars[0].v_type != VAR_UNKNOWN)
8674 {
8675 long col;
8676
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008677 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008678 if (col < 0)
8679 return; // type error; errmsg already given
8680#ifdef FEAT_VARTABS
8681 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8682 return;
8683#endif
8684 }
8685
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008686 rettv->vval.v_number = get_sw_value(curbuf);
8687}
8688
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008689#ifdef FEAT_FLOAT
8690/*
8691 * "sin()" function
8692 */
8693 static void
8694f_sin(typval_T *argvars, typval_T *rettv)
8695{
8696 float_T f = 0.0;
8697
8698 rettv->v_type = VAR_FLOAT;
8699 if (get_float_arg(argvars, &f) == OK)
8700 rettv->vval.v_float = sin(f);
8701 else
8702 rettv->vval.v_float = 0.0;
8703}
8704
8705/*
8706 * "sinh()" function
8707 */
8708 static void
8709f_sinh(typval_T *argvars, typval_T *rettv)
8710{
8711 float_T f = 0.0;
8712
8713 rettv->v_type = VAR_FLOAT;
8714 if (get_float_arg(argvars, &f) == OK)
8715 rettv->vval.v_float = sinh(f);
8716 else
8717 rettv->vval.v_float = 0.0;
8718}
8719#endif
8720
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008721/*
8722 * "soundfold({word})" function
8723 */
8724 static void
8725f_soundfold(typval_T *argvars, typval_T *rettv)
8726{
8727 char_u *s;
8728
8729 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008730 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008731#ifdef FEAT_SPELL
8732 rettv->vval.v_string = eval_soundfold(s);
8733#else
8734 rettv->vval.v_string = vim_strsave(s);
8735#endif
8736}
8737
8738/*
8739 * "spellbadword()" function
8740 */
8741 static void
8742f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8743{
8744 char_u *word = (char_u *)"";
8745 hlf_T attr = HLF_COUNT;
8746 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008747#ifdef FEAT_SPELL
8748 int wo_spell_save = curwin->w_p_spell;
8749
8750 if (!curwin->w_p_spell)
8751 {
8752 did_set_spelllang(curwin);
8753 curwin->w_p_spell = TRUE;
8754 }
8755
8756 if (*curwin->w_s->b_p_spl == NUL)
8757 {
8758 emsg(_(e_no_spell));
8759 curwin->w_p_spell = wo_spell_save;
8760 return;
8761 }
8762#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008763
8764 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008765 {
8766#ifdef FEAT_SPELL
8767 curwin->w_p_spell = wo_spell_save;
8768#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008769 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008770 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008771
8772#ifdef FEAT_SPELL
8773 if (argvars[0].v_type == VAR_UNKNOWN)
8774 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008775 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8777 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008778 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008779 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008780 curwin->w_set_curswant = TRUE;
8781 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008782 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008783 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008784 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008785 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008786 int capcol = -1;
8787
8788 if (str != NULL)
8789 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008790 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008791 while (*str != NUL)
8792 {
8793 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8794 if (attr != HLF_COUNT)
8795 {
8796 word = str;
8797 break;
8798 }
8799 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008800 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008801 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008802 }
8803 }
8804 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008805 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008806#endif
8807
8808 list_append_string(rettv->vval.v_list, word, len);
8809 list_append_string(rettv->vval.v_list, (char_u *)(
8810 attr == HLF_SPB ? "bad" :
8811 attr == HLF_SPR ? "rare" :
8812 attr == HLF_SPL ? "local" :
8813 attr == HLF_SPC ? "caps" :
8814 ""), -1);
8815}
8816
8817/*
8818 * "spellsuggest()" function
8819 */
8820 static void
8821f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8822{
8823#ifdef FEAT_SPELL
8824 char_u *str;
8825 int typeerr = FALSE;
8826 int maxcount;
8827 garray_T ga;
8828 int i;
8829 listitem_T *li;
8830 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008831 int wo_spell_save = curwin->w_p_spell;
8832
8833 if (!curwin->w_p_spell)
8834 {
8835 did_set_spelllang(curwin);
8836 curwin->w_p_spell = TRUE;
8837 }
8838
8839 if (*curwin->w_s->b_p_spl == NUL)
8840 {
8841 emsg(_(e_no_spell));
8842 curwin->w_p_spell = wo_spell_save;
8843 return;
8844 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008845#endif
8846
8847 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008848 {
8849#ifdef FEAT_SPELL
8850 curwin->w_p_spell = wo_spell_save;
8851#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008852 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008853 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008854
8855#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008856 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008857 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008858 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008859 if (argvars[1].v_type != VAR_UNKNOWN)
8860 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008861 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008862 if (maxcount <= 0)
8863 return;
8864 if (argvars[2].v_type != VAR_UNKNOWN)
8865 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008866 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008867 if (typeerr)
8868 return;
8869 }
8870 }
8871 else
8872 maxcount = 25;
8873
8874 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8875
8876 for (i = 0; i < ga.ga_len; ++i)
8877 {
8878 str = ((char_u **)ga.ga_data)[i];
8879
8880 li = listitem_alloc();
8881 if (li == NULL)
8882 vim_free(str);
8883 else
8884 {
8885 li->li_tv.v_type = VAR_STRING;
8886 li->li_tv.v_lock = 0;
8887 li->li_tv.vval.v_string = str;
8888 list_append(rettv->vval.v_list, li);
8889 }
8890 }
8891 ga_clear(&ga);
8892 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008893 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008894#endif
8895}
8896
8897 static void
8898f_split(typval_T *argvars, typval_T *rettv)
8899{
8900 char_u *str;
8901 char_u *end;
8902 char_u *pat = NULL;
8903 regmatch_T regmatch;
8904 char_u patbuf[NUMBUFLEN];
8905 char_u *save_cpo;
8906 int match;
8907 colnr_T col = 0;
8908 int keepempty = FALSE;
8909 int typeerr = FALSE;
8910
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008911 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008912 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008913 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008914
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008915 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008916 if (argvars[1].v_type != VAR_UNKNOWN)
8917 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008918 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008919 if (pat == NULL)
8920 typeerr = TRUE;
8921 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008922 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008923 }
8924 if (pat == NULL || *pat == NUL)
8925 pat = (char_u *)"[\\x01- ]\\+";
8926
8927 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008928 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008929 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008930 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008931
8932 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8933 if (regmatch.regprog != NULL)
8934 {
8935 regmatch.rm_ic = FALSE;
8936 while (*str != NUL || keepempty)
8937 {
8938 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008939 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008940 else
8941 match = vim_regexec_nl(&regmatch, str, col);
8942 if (match)
8943 end = regmatch.startp[0];
8944 else
8945 end = str + STRLEN(str);
8946 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
8947 && *str != NUL && match && end < regmatch.endp[0]))
8948 {
8949 if (list_append_string(rettv->vval.v_list, str,
8950 (int)(end - str)) == FAIL)
8951 break;
8952 }
8953 if (!match)
8954 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01008955 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008956 if (regmatch.endp[0] > str)
8957 col = 0;
8958 else
Bram Moolenaar13505972019-01-24 15:04:48 +01008959 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008960 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008961 str = regmatch.endp[0];
8962 }
8963
8964 vim_regfree(regmatch.regprog);
8965 }
8966
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008967theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008968 p_cpo = save_cpo;
8969}
8970
8971#ifdef FEAT_FLOAT
8972/*
8973 * "sqrt()" function
8974 */
8975 static void
8976f_sqrt(typval_T *argvars, typval_T *rettv)
8977{
8978 float_T f = 0.0;
8979
8980 rettv->v_type = VAR_FLOAT;
8981 if (get_float_arg(argvars, &f) == OK)
8982 rettv->vval.v_float = sqrt(f);
8983 else
8984 rettv->vval.v_float = 0.0;
8985}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01008986#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008987
Bram Moolenaar0387cae2019-11-29 21:07:58 +01008988#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01008989/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008990 * "str2float()" function
8991 */
8992 static void
8993f_str2float(typval_T *argvars, typval_T *rettv)
8994{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008995 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008996 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008997
Bram Moolenaar08243d22017-01-10 16:12:29 +01008998 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008999 p = skipwhite(p + 1);
9000 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01009001 if (isneg)
9002 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009003 rettv->v_type = VAR_FLOAT;
9004}
9005#endif
9006
9007/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02009008 * "str2list()" function
9009 */
9010 static void
9011f_str2list(typval_T *argvars, typval_T *rettv)
9012{
9013 char_u *p;
9014 int utf8 = FALSE;
9015
9016 if (rettv_list_alloc(rettv) == FAIL)
9017 return;
9018
9019 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02009020 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02009021
9022 p = tv_get_string(&argvars[0]);
9023
9024 if (has_mbyte || utf8)
9025 {
9026 int (*ptr2len)(char_u *);
9027 int (*ptr2char)(char_u *);
9028
9029 if (utf8 || enc_utf8)
9030 {
9031 ptr2len = utf_ptr2len;
9032 ptr2char = utf_ptr2char;
9033 }
9034 else
9035 {
9036 ptr2len = mb_ptr2len;
9037 ptr2char = mb_ptr2char;
9038 }
9039
9040 for ( ; *p != NUL; p += (*ptr2len)(p))
9041 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
9042 }
9043 else
9044 for ( ; *p != NUL; ++p)
9045 list_append_number(rettv->vval.v_list, *p);
9046}
9047
9048/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009049 * "str2nr()" function
9050 */
9051 static void
9052f_str2nr(typval_T *argvars, typval_T *rettv)
9053{
9054 int base = 10;
9055 char_u *p;
9056 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009057 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01009058 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009059
9060 if (argvars[1].v_type != VAR_UNKNOWN)
9061 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009062 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009063 if (base != 2 && base != 8 && base != 10 && base != 16)
9064 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009065 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009066 return;
9067 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02009068 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009069 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009070 }
9071
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009072 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009073 isneg = (*p == '-');
9074 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009075 p = skipwhite(p + 1);
9076 switch (base)
9077 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009078 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02009079 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009080 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009081 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02009082 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
9083 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01009084 if (isneg)
9085 rettv->vval.v_number = -n;
9086 else
9087 rettv->vval.v_number = n;
9088
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009089}
9090
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009091/*
9092 * "strgetchar()" function
9093 */
9094 static void
9095f_strgetchar(typval_T *argvars, typval_T *rettv)
9096{
9097 char_u *str;
9098 int len;
9099 int error = FALSE;
9100 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01009101 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009102
9103 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009104 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009105 if (str == NULL)
9106 return;
9107 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009108 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009109 if (error)
9110 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009111
Bram Moolenaar13505972019-01-24 15:04:48 +01009112 while (charidx >= 0 && byteidx < len)
9113 {
9114 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009115 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009116 rettv->vval.v_number = mb_ptr2char(str + byteidx);
9117 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009118 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009119 --charidx;
9120 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009121 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009122}
9123
9124/*
9125 * "stridx()" function
9126 */
9127 static void
9128f_stridx(typval_T *argvars, typval_T *rettv)
9129{
9130 char_u buf[NUMBUFLEN];
9131 char_u *needle;
9132 char_u *haystack;
9133 char_u *save_haystack;
9134 char_u *pos;
9135 int start_idx;
9136
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009137 needle = tv_get_string_chk(&argvars[1]);
9138 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009139 rettv->vval.v_number = -1;
9140 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009141 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009142
9143 if (argvars[2].v_type != VAR_UNKNOWN)
9144 {
9145 int error = FALSE;
9146
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009147 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009148 if (error || start_idx >= (int)STRLEN(haystack))
9149 return;
9150 if (start_idx >= 0)
9151 haystack += start_idx;
9152 }
9153
9154 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9155 if (pos != NULL)
9156 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
9157}
9158
9159/*
9160 * "string()" function
9161 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009162 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009163f_string(typval_T *argvars, typval_T *rettv)
9164{
9165 char_u *tofree;
9166 char_u numbuf[NUMBUFLEN];
9167
9168 rettv->v_type = VAR_STRING;
9169 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
9170 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009171 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009172 if (rettv->vval.v_string != NULL && tofree == NULL)
9173 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
9174}
9175
9176/*
9177 * "strlen()" function
9178 */
9179 static void
9180f_strlen(typval_T *argvars, typval_T *rettv)
9181{
9182 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009183 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009184}
9185
9186/*
9187 * "strchars()" function
9188 */
9189 static void
9190f_strchars(typval_T *argvars, typval_T *rettv)
9191{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009192 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009193 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009194 varnumber_T len = 0;
9195 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009196
9197 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009198 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009199 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02009200 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009201 else
9202 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009203 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
9204 while (*s != NUL)
9205 {
9206 func_mb_ptr2char_adv(&s);
9207 ++len;
9208 }
9209 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009210 }
9211}
9212
9213/*
9214 * "strdisplaywidth()" function
9215 */
9216 static void
9217f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
9218{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009219 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009220 int col = 0;
9221
9222 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009223 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009224
9225 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
9226}
9227
9228/*
9229 * "strwidth()" function
9230 */
9231 static void
9232f_strwidth(typval_T *argvars, typval_T *rettv)
9233{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009234 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009235
Bram Moolenaar13505972019-01-24 15:04:48 +01009236 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009237}
9238
9239/*
9240 * "strcharpart()" function
9241 */
9242 static void
9243f_strcharpart(typval_T *argvars, typval_T *rettv)
9244{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009245 char_u *p;
9246 int nchar;
9247 int nbyte = 0;
9248 int charlen;
9249 int len = 0;
9250 int slen;
9251 int error = FALSE;
9252
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009253 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009254 slen = (int)STRLEN(p);
9255
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009256 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009257 if (!error)
9258 {
9259 if (nchar > 0)
9260 while (nchar > 0 && nbyte < slen)
9261 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009262 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009263 --nchar;
9264 }
9265 else
9266 nbyte = nchar;
9267 if (argvars[2].v_type != VAR_UNKNOWN)
9268 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009269 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009270 while (charlen > 0 && nbyte + len < slen)
9271 {
9272 int off = nbyte + len;
9273
9274 if (off < 0)
9275 len += 1;
9276 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009277 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009278 --charlen;
9279 }
9280 }
9281 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009282 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009283 }
9284
9285 /*
9286 * Only return the overlap between the specified part and the actual
9287 * string.
9288 */
9289 if (nbyte < 0)
9290 {
9291 len += nbyte;
9292 nbyte = 0;
9293 }
9294 else if (nbyte > slen)
9295 nbyte = slen;
9296 if (len < 0)
9297 len = 0;
9298 else if (nbyte + len > slen)
9299 len = slen - nbyte;
9300
9301 rettv->v_type = VAR_STRING;
9302 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009303}
9304
9305/*
9306 * "strpart()" function
9307 */
9308 static void
9309f_strpart(typval_T *argvars, typval_T *rettv)
9310{
9311 char_u *p;
9312 int n;
9313 int len;
9314 int slen;
9315 int error = FALSE;
9316
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009317 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009318 slen = (int)STRLEN(p);
9319
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009320 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009321 if (error)
9322 len = 0;
9323 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009324 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009325 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009326 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009327
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009328 // Only return the overlap between the specified part and the actual
9329 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009330 if (n < 0)
9331 {
9332 len += n;
9333 n = 0;
9334 }
9335 else if (n > slen)
9336 n = slen;
9337 if (len < 0)
9338 len = 0;
9339 else if (n + len > slen)
9340 len = slen - n;
9341
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009342 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9343 {
9344 int off;
9345
9346 // length in characters
9347 for (off = n; off < slen && len > 0; --len)
9348 off += mb_ptr2len(p + off);
9349 len = off - n;
9350 }
9351
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009352 rettv->v_type = VAR_STRING;
9353 rettv->vval.v_string = vim_strnsave(p + n, len);
9354}
9355
9356/*
9357 * "strridx()" function
9358 */
9359 static void
9360f_strridx(typval_T *argvars, typval_T *rettv)
9361{
9362 char_u buf[NUMBUFLEN];
9363 char_u *needle;
9364 char_u *haystack;
9365 char_u *rest;
9366 char_u *lastmatch = NULL;
9367 int haystack_len, end_idx;
9368
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009369 needle = tv_get_string_chk(&argvars[1]);
9370 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009371
9372 rettv->vval.v_number = -1;
9373 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009374 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009375
9376 haystack_len = (int)STRLEN(haystack);
9377 if (argvars[2].v_type != VAR_UNKNOWN)
9378 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009379 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009380 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009381 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009382 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009383 }
9384 else
9385 end_idx = haystack_len;
9386
9387 if (*needle == NUL)
9388 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009389 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009390 lastmatch = haystack + end_idx;
9391 }
9392 else
9393 {
9394 for (rest = haystack; *rest != '\0'; ++rest)
9395 {
9396 rest = (char_u *)strstr((char *)rest, (char *)needle);
9397 if (rest == NULL || rest > haystack + end_idx)
9398 break;
9399 lastmatch = rest;
9400 }
9401 }
9402
9403 if (lastmatch == NULL)
9404 rettv->vval.v_number = -1;
9405 else
9406 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9407}
9408
9409/*
9410 * "strtrans()" function
9411 */
9412 static void
9413f_strtrans(typval_T *argvars, typval_T *rettv)
9414{
9415 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009416 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009417}
9418
9419/*
9420 * "submatch()" function
9421 */
9422 static void
9423f_submatch(typval_T *argvars, typval_T *rettv)
9424{
9425 int error = FALSE;
9426 int no;
9427 int retList = 0;
9428
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009429 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009430 if (error)
9431 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009432 if (no < 0 || no >= NSUBEXP)
9433 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009434 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009435 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009436 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009437 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009438 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009439 if (error)
9440 return;
9441
9442 if (retList == 0)
9443 {
9444 rettv->v_type = VAR_STRING;
9445 rettv->vval.v_string = reg_submatch(no);
9446 }
9447 else
9448 {
9449 rettv->v_type = VAR_LIST;
9450 rettv->vval.v_list = reg_submatch_list(no);
9451 }
9452}
9453
9454/*
9455 * "substitute()" function
9456 */
9457 static void
9458f_substitute(typval_T *argvars, typval_T *rettv)
9459{
9460 char_u patbuf[NUMBUFLEN];
9461 char_u subbuf[NUMBUFLEN];
9462 char_u flagsbuf[NUMBUFLEN];
9463
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009464 char_u *str = tv_get_string_chk(&argvars[0]);
9465 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009466 char_u *sub = NULL;
9467 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009468 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009469
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009470 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9471 expr = &argvars[2];
9472 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009473 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009474
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009475 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009476 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9477 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009478 rettv->vval.v_string = NULL;
9479 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009480 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009481}
9482
9483/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009484 * "swapinfo(swap_filename)" function
9485 */
9486 static void
9487f_swapinfo(typval_T *argvars, typval_T *rettv)
9488{
9489 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009490 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009491}
9492
9493/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009494 * "swapname(expr)" function
9495 */
9496 static void
9497f_swapname(typval_T *argvars, typval_T *rettv)
9498{
9499 buf_T *buf;
9500
9501 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009502 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009503 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9504 || buf->b_ml.ml_mfp->mf_fname == NULL)
9505 rettv->vval.v_string = NULL;
9506 else
9507 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9508}
9509
9510/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009511 * "synID(lnum, col, trans)" function
9512 */
9513 static void
9514f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9515{
9516 int id = 0;
9517#ifdef FEAT_SYN_HL
9518 linenr_T lnum;
9519 colnr_T col;
9520 int trans;
9521 int transerr = FALSE;
9522
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009523 lnum = tv_get_lnum(argvars); // -1 on type error
9524 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009525 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009526
9527 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9528 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9529 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9530#endif
9531
9532 rettv->vval.v_number = id;
9533}
9534
9535/*
9536 * "synIDattr(id, what [, mode])" function
9537 */
9538 static void
9539f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9540{
9541 char_u *p = NULL;
9542#ifdef FEAT_SYN_HL
9543 int id;
9544 char_u *what;
9545 char_u *mode;
9546 char_u modebuf[NUMBUFLEN];
9547 int modec;
9548
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009549 id = (int)tv_get_number(&argvars[0]);
9550 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009551 if (argvars[2].v_type != VAR_UNKNOWN)
9552 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009553 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009554 modec = TOLOWER_ASC(mode[0]);
9555 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009556 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009557 }
9558 else
9559 {
9560#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9561 if (USE_24BIT)
9562 modec = 'g';
9563 else
9564#endif
9565 if (t_colors > 1)
9566 modec = 'c';
9567 else
9568 modec = 't';
9569 }
9570
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009571 switch (TOLOWER_ASC(what[0]))
9572 {
9573 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009574 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009575 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009576 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009577 p = highlight_has_attr(id, HL_BOLD, modec);
9578 break;
9579
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009580 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009581 p = highlight_color(id, what, modec);
9582 break;
9583
9584 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009585 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009586 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009587 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009588 p = highlight_has_attr(id, HL_ITALIC, modec);
9589 break;
9590
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009591 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009592 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009593 break;
9594
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009595 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009596 p = highlight_has_attr(id, HL_INVERSE, modec);
9597 break;
9598
9599 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009600 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009601 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009602 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009603 else if (TOLOWER_ASC(what[1]) == 't' &&
9604 TOLOWER_ASC(what[2]) == 'r')
9605 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009606 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009607 p = highlight_has_attr(id, HL_STANDOUT, modec);
9608 break;
9609
9610 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009611 if (TOLOWER_ASC(what[1]) == 'l') // ul
9612 p = highlight_color(id, what, modec);
9613 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009614 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009615 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9616 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009617 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009618 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9619 break;
9620 }
9621
9622 if (p != NULL)
9623 p = vim_strsave(p);
9624#endif
9625 rettv->v_type = VAR_STRING;
9626 rettv->vval.v_string = p;
9627}
9628
9629/*
9630 * "synIDtrans(id)" function
9631 */
9632 static void
9633f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9634{
9635 int id;
9636
9637#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009638 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009639
9640 if (id > 0)
9641 id = syn_get_final_id(id);
9642 else
9643#endif
9644 id = 0;
9645
9646 rettv->vval.v_number = id;
9647}
9648
9649/*
9650 * "synconcealed(lnum, col)" function
9651 */
9652 static void
9653f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9654{
9655#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9656 linenr_T lnum;
9657 colnr_T col;
9658 int syntax_flags = 0;
9659 int cchar;
9660 int matchid = 0;
9661 char_u str[NUMBUFLEN];
9662#endif
9663
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009664 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009665
9666#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009667 lnum = tv_get_lnum(argvars); // -1 on type error
9668 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009669
Bram Moolenaara80faa82020-04-12 19:37:17 +02009670 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009671
9672 if (rettv_list_alloc(rettv) != FAIL)
9673 {
9674 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9675 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9676 && curwin->w_p_cole > 0)
9677 {
9678 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9679 syntax_flags = get_syntax_info(&matchid);
9680
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009681 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009682 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9683 {
9684 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009685 if (cchar == NUL && curwin->w_p_cole == 1)
9686 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009687 if (cchar != NUL)
9688 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009689 if (has_mbyte)
9690 (*mb_char2bytes)(cchar, str);
9691 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009692 str[0] = cchar;
9693 }
9694 }
9695 }
9696
9697 list_append_number(rettv->vval.v_list,
9698 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009699 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009700 list_append_string(rettv->vval.v_list, str, -1);
9701 list_append_number(rettv->vval.v_list, matchid);
9702 }
9703#endif
9704}
9705
9706/*
9707 * "synstack(lnum, col)" function
9708 */
9709 static void
9710f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9711{
9712#ifdef FEAT_SYN_HL
9713 linenr_T lnum;
9714 colnr_T col;
9715 int i;
9716 int id;
9717#endif
9718
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009719 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009720
9721#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009722 lnum = tv_get_lnum(argvars); // -1 on type error
9723 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009724
9725 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9726 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9727 && rettv_list_alloc(rettv) != FAIL)
9728 {
9729 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9730 for (i = 0; ; ++i)
9731 {
9732 id = syn_get_stack_item(i);
9733 if (id < 0)
9734 break;
9735 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9736 break;
9737 }
9738 }
9739#endif
9740}
9741
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009742/*
9743 * "tabpagebuflist()" function
9744 */
9745 static void
9746f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9747{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009748 tabpage_T *tp;
9749 win_T *wp = NULL;
9750
9751 if (argvars[0].v_type == VAR_UNKNOWN)
9752 wp = firstwin;
9753 else
9754 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009755 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009756 if (tp != NULL)
9757 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9758 }
9759 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9760 {
9761 for (; wp != NULL; wp = wp->w_next)
9762 if (list_append_number(rettv->vval.v_list,
9763 wp->w_buffer->b_fnum) == FAIL)
9764 break;
9765 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009766}
9767
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009768/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009769 * "tagfiles()" function
9770 */
9771 static void
9772f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9773{
9774 char_u *fname;
9775 tagname_T tn;
9776 int first;
9777
9778 if (rettv_list_alloc(rettv) == FAIL)
9779 return;
9780 fname = alloc(MAXPATHL);
9781 if (fname == NULL)
9782 return;
9783
9784 for (first = TRUE; ; first = FALSE)
9785 if (get_tagfname(&tn, first, fname) == FAIL
9786 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9787 break;
9788 tagname_free(&tn);
9789 vim_free(fname);
9790}
9791
9792/*
9793 * "taglist()" function
9794 */
9795 static void
9796f_taglist(typval_T *argvars, typval_T *rettv)
9797{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009798 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009799 char_u *tag_pattern;
9800
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009801 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009802
9803 rettv->vval.v_number = FALSE;
9804 if (*tag_pattern == NUL)
9805 return;
9806
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009807 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009808 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009809 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009810 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009811}
9812
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009813#ifdef FEAT_FLOAT
9814/*
9815 * "tan()" function
9816 */
9817 static void
9818f_tan(typval_T *argvars, typval_T *rettv)
9819{
9820 float_T f = 0.0;
9821
9822 rettv->v_type = VAR_FLOAT;
9823 if (get_float_arg(argvars, &f) == OK)
9824 rettv->vval.v_float = tan(f);
9825 else
9826 rettv->vval.v_float = 0.0;
9827}
9828
9829/*
9830 * "tanh()" function
9831 */
9832 static void
9833f_tanh(typval_T *argvars, typval_T *rettv)
9834{
9835 float_T f = 0.0;
9836
9837 rettv->v_type = VAR_FLOAT;
9838 if (get_float_arg(argvars, &f) == OK)
9839 rettv->vval.v_float = tanh(f);
9840 else
9841 rettv->vval.v_float = 0.0;
9842}
9843#endif
9844
9845/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009846 * "tolower(string)" function
9847 */
9848 static void
9849f_tolower(typval_T *argvars, typval_T *rettv)
9850{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009851 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009852 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009853}
9854
9855/*
9856 * "toupper(string)" function
9857 */
9858 static void
9859f_toupper(typval_T *argvars, typval_T *rettv)
9860{
9861 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009862 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009863}
9864
9865/*
9866 * "tr(string, fromstr, tostr)" function
9867 */
9868 static void
9869f_tr(typval_T *argvars, typval_T *rettv)
9870{
9871 char_u *in_str;
9872 char_u *fromstr;
9873 char_u *tostr;
9874 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009875 int inlen;
9876 int fromlen;
9877 int tolen;
9878 int idx;
9879 char_u *cpstr;
9880 int cplen;
9881 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009882 char_u buf[NUMBUFLEN];
9883 char_u buf2[NUMBUFLEN];
9884 garray_T ga;
9885
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009886 in_str = tv_get_string(&argvars[0]);
9887 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9888 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009889
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009890 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009891 rettv->v_type = VAR_STRING;
9892 rettv->vval.v_string = NULL;
9893 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009894 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009895 ga_init2(&ga, (int)sizeof(char), 80);
9896
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009897 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009898 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009899 if (STRLEN(fromstr) != STRLEN(tostr))
9900 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009901error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009902 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009903 ga_clear(&ga);
9904 return;
9905 }
9906
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009907 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009908 while (*in_str != NUL)
9909 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009910 if (has_mbyte)
9911 {
9912 inlen = (*mb_ptr2len)(in_str);
9913 cpstr = in_str;
9914 cplen = inlen;
9915 idx = 0;
9916 for (p = fromstr; *p != NUL; p += fromlen)
9917 {
9918 fromlen = (*mb_ptr2len)(p);
9919 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9920 {
9921 for (p = tostr; *p != NUL; p += tolen)
9922 {
9923 tolen = (*mb_ptr2len)(p);
9924 if (idx-- == 0)
9925 {
9926 cplen = tolen;
9927 cpstr = p;
9928 break;
9929 }
9930 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009931 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009932 goto error;
9933 break;
9934 }
9935 ++idx;
9936 }
9937
9938 if (first && cpstr == in_str)
9939 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009940 // Check that fromstr and tostr have the same number of
9941 // (multi-byte) characters. Done only once when a character
9942 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009943 first = FALSE;
9944 for (p = tostr; *p != NUL; p += tolen)
9945 {
9946 tolen = (*mb_ptr2len)(p);
9947 --idx;
9948 }
9949 if (idx != 0)
9950 goto error;
9951 }
9952
9953 (void)ga_grow(&ga, cplen);
9954 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
9955 ga.ga_len += cplen;
9956
9957 in_str += inlen;
9958 }
9959 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009960 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009961 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009962 p = vim_strchr(fromstr, *in_str);
9963 if (p != NULL)
9964 ga_append(&ga, tostr[p - fromstr]);
9965 else
9966 ga_append(&ga, *in_str);
9967 ++in_str;
9968 }
9969 }
9970
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009971 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009972 (void)ga_grow(&ga, 1);
9973 ga_append(&ga, NUL);
9974
9975 rettv->vval.v_string = ga.ga_data;
9976}
9977
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009978/*
9979 * "trim({expr})" function
9980 */
9981 static void
9982f_trim(typval_T *argvars, typval_T *rettv)
9983{
9984 char_u buf1[NUMBUFLEN];
9985 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009986 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009987 char_u *mask = NULL;
9988 char_u *tail;
9989 char_u *prev;
9990 char_u *p;
9991 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009992 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009993
9994 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009995 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009996 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009997 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009998
9999 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010000 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010001 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010002
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010003 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010004 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010005 int error = 0;
10006
10007 // leading or trailing characters to trim
10008 dir = (int)tv_get_number_chk(&argvars[2], &error);
10009 if (error)
10010 return;
10011 if (dir < 0 || dir > 2)
10012 {
10013 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
10014 return;
10015 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010016 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010017 }
10018
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010019 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010020 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010021 // Trim leading characters
10022 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010023 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010024 c1 = PTR2CHAR(head);
10025 if (mask == NULL)
10026 {
10027 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010028 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010029 }
10030 else
10031 {
10032 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10033 if (c1 == PTR2CHAR(p))
10034 break;
10035 if (*p == NUL)
10036 break;
10037 }
10038 MB_PTR_ADV(head);
10039 }
10040 }
10041
10042 tail = head + STRLEN(head);
10043 if (dir == 0 || dir == 2)
10044 {
10045 // Trim trailing characters
10046 for (; tail > head; tail = prev)
10047 {
10048 prev = tail;
10049 MB_PTR_BACK(head, prev);
10050 c1 = PTR2CHAR(prev);
10051 if (mask == NULL)
10052 {
10053 if (c1 > ' ' && c1 != 0xa0)
10054 break;
10055 }
10056 else
10057 {
10058 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10059 if (c1 == PTR2CHAR(p))
10060 break;
10061 if (*p == NUL)
10062 break;
10063 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010064 }
10065 }
Bram Moolenaardf44a272020-06-07 20:49:05 +020010066 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010067}
10068
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010069#ifdef FEAT_FLOAT
10070/*
10071 * "trunc({float})" function
10072 */
10073 static void
10074f_trunc(typval_T *argvars, typval_T *rettv)
10075{
10076 float_T f = 0.0;
10077
10078 rettv->v_type = VAR_FLOAT;
10079 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010080 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010081 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
10082 else
10083 rettv->vval.v_float = 0.0;
10084}
10085#endif
10086
10087/*
10088 * "type(expr)" function
10089 */
10090 static void
10091f_type(typval_T *argvars, typval_T *rettv)
10092{
10093 int n = -1;
10094
10095 switch (argvars[0].v_type)
10096 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010097 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
10098 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010099 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010100 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
10101 case VAR_LIST: n = VAR_TYPE_LIST; break;
10102 case VAR_DICT: n = VAR_TYPE_DICT; break;
10103 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
10104 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
10105 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020010106 case VAR_JOB: n = VAR_TYPE_JOB; break;
10107 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010108 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010109 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +020010110 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010111 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +010010112 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010113 n = -1;
10114 break;
10115 }
10116 rettv->vval.v_number = n;
10117}
10118
10119/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010120 * "virtcol(string)" function
10121 */
10122 static void
10123f_virtcol(typval_T *argvars, typval_T *rettv)
10124{
10125 colnr_T vcol = 0;
10126 pos_T *fp;
10127 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010128 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010129
Bram Moolenaar6f02b002021-01-10 20:22:54 +010010130 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010131 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
10132 && fnum == curbuf->b_fnum)
10133 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010134 // Limit the column to a valid value, getvvcol() doesn't check.
10135 if (fp->col < 0)
10136 fp->col = 0;
10137 else
10138 {
10139 len = (int)STRLEN(ml_get(fp->lnum));
10140 if (fp->col > len)
10141 fp->col = len;
10142 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010143 getvvcol(curwin, fp, NULL, NULL, &vcol);
10144 ++vcol;
10145 }
10146
10147 rettv->vval.v_number = vcol;
10148}
10149
10150/*
10151 * "visualmode()" function
10152 */
10153 static void
10154f_visualmode(typval_T *argvars, typval_T *rettv)
10155{
10156 char_u str[2];
10157
10158 rettv->v_type = VAR_STRING;
10159 str[0] = curbuf->b_visual_mode_eval;
10160 str[1] = NUL;
10161 rettv->vval.v_string = vim_strsave(str);
10162
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010163 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010164 if (non_zero_arg(&argvars[0]))
10165 curbuf->b_visual_mode_eval = NUL;
10166}
10167
10168/*
10169 * "wildmenumode()" function
10170 */
10171 static void
10172f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10173{
10174#ifdef FEAT_WILDMENU
10175 if (wild_menu_showing)
10176 rettv->vval.v_number = 1;
10177#endif
10178}
10179
10180/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +010010181 * "windowsversion()" function
10182 */
10183 static void
10184f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10185{
10186 rettv->v_type = VAR_STRING;
10187 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
10188}
10189
10190/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010191 * "wordcount()" function
10192 */
10193 static void
10194f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
10195{
10196 if (rettv_dict_alloc(rettv) == FAIL)
10197 return;
10198 cursor_pos_info(rettv->vval.v_dict);
10199}
10200
10201/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010202 * "xor(expr, expr)" function
10203 */
10204 static void
10205f_xor(typval_T *argvars, typval_T *rettv)
10206{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010207 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
10208 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010209}
10210
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010211#endif // FEAT_EVAL