blob: 6d845c1663d26a78ded0635252838e29e1d1bc57 [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);
Bram Moolenaar70ce8a12021-03-14 19:02:09 +0100226static void f_strcharlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200227static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200228static void f_strgetchar(typval_T *argvars, typval_T *rettv);
229static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200230static void f_strlen(typval_T *argvars, typval_T *rettv);
231static void f_strcharpart(typval_T *argvars, typval_T *rettv);
232static void f_strpart(typval_T *argvars, typval_T *rettv);
233static void f_strridx(typval_T *argvars, typval_T *rettv);
234static void f_strtrans(typval_T *argvars, typval_T *rettv);
235static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
236static void f_strwidth(typval_T *argvars, typval_T *rettv);
237static void f_submatch(typval_T *argvars, typval_T *rettv);
238static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200239static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200240static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200241static void f_synID(typval_T *argvars, typval_T *rettv);
242static void f_synIDattr(typval_T *argvars, typval_T *rettv);
243static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
244static void f_synstack(typval_T *argvars, typval_T *rettv);
245static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200246static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200247static void f_taglist(typval_T *argvars, typval_T *rettv);
248static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200249#ifdef FEAT_FLOAT
250static void f_tan(typval_T *argvars, typval_T *rettv);
251static void f_tanh(typval_T *argvars, typval_T *rettv);
252#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200253static void f_tolower(typval_T *argvars, typval_T *rettv);
254static void f_toupper(typval_T *argvars, typval_T *rettv);
255static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100256static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200257#ifdef FEAT_FLOAT
258static void f_trunc(typval_T *argvars, typval_T *rettv);
259#endif
260static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200261static void f_virtcol(typval_T *argvars, typval_T *rettv);
262static void f_visualmode(typval_T *argvars, typval_T *rettv);
263static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100264static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200265static void f_wordcount(typval_T *argvars, typval_T *rettv);
266static void f_xor(typval_T *argvars, typval_T *rettv);
267
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100268
Bram Moolenaar94738d82020-10-21 14:25:07 +0200269/*
270 * Functions that check the argument type of a builtin function.
271 * Each function returns FAIL and gives an error message if the type is wrong.
272 */
273
274// Context passed to an arg_ function.
275typedef struct {
Bram Moolenaarca174532020-10-21 16:42:22 +0200276 int arg_count; // actual argument count
277 type_T **arg_types; // list of argument types
278 int arg_idx; // current argument index (first arg is zero)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100279 cctx_T *arg_cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +0200280} argcontext_T;
281
282// A function to check one argument type. The first argument is the type to
283// check. If needed, other argument types can be obtained with the context.
284// E.g. if "arg_idx" is 1, then (type - 1) is the first argument type.
285typedef int (*argcheck_T)(type_T *, argcontext_T *);
286
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100287/*
Bram Moolenaar351ead02021-01-16 16:07:01 +0100288 * Call need_type() to check an argument type.
289 */
290 static int
291check_arg_type(
292 type_T *expected,
293 type_T *actual,
294 argcontext_T *context)
295{
296 // TODO: would be useful to know if "actual" is a constant and pass it to
297 // need_type() to get a compile time error if possible.
298 return need_type(actual, expected,
299 context->arg_idx - context->arg_count, context->arg_idx + 1,
300 context->arg_cctx, FALSE, FALSE);
301}
302
303/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100304 * Check "type" is a float or a number.
305 */
Bram Moolenaar94738d82020-10-21 14:25:07 +0200306 static int
307arg_float_or_nr(type_T *type, argcontext_T *context)
308{
Bram Moolenaarca174532020-10-21 16:42:22 +0200309 if (type->tt_type == VAR_ANY
310 || type->tt_type == VAR_FLOAT || type->tt_type == VAR_NUMBER)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200311 return OK;
312 arg_type_mismatch(&t_number, type, context->arg_idx + 1);
313 return FAIL;
314}
315
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100316/*
317 * Check "type" is a number.
318 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200319 static int
320arg_number(type_T *type, argcontext_T *context)
321{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100322 return check_arg_type(&t_number, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200323}
324
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100325/*
326 * Check "type" is a string.
327 */
328 static int
329arg_string(type_T *type, argcontext_T *context)
330{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100331 return check_arg_type(&t_string, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100332}
333
334/*
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100335 * Check "type" is a bool or number 0 or 1.
336 */
337 static int
338arg_bool(type_T *type, argcontext_T *context)
339{
340 if (type->tt_type == VAR_ANY
341 || type->tt_type == VAR_NUMBER || type->tt_type == VAR_BOOL)
342 return OK;
343 return check_arg_type(&t_bool, type, context);
344}
345
346/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100347 * Check "type" is a list or a blob.
348 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200349 static int
350arg_list_or_blob(type_T *type, argcontext_T *context)
351{
352 if (type->tt_type == VAR_ANY
353 || type->tt_type == VAR_LIST || type->tt_type == VAR_BLOB)
354 return OK;
355 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
356 return FAIL;
357}
358
359/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100360 * Check "type" is a list or a dict.
361 */
362 static int
363arg_list_or_dict(type_T *type, argcontext_T *context)
364{
365 if (type->tt_type == VAR_ANY
366 || type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
367 return OK;
368 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
369 return FAIL;
370}
371
372/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100373 * Check "type" is the same type as the previous argument.
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100374 * Must not be used for the first argcheck_T entry.
375 */
376 static int
377arg_same_as_prev(type_T *type, argcontext_T *context)
378{
379 type_T *prev_type = context->arg_types[context->arg_idx - 1];
380
Bram Moolenaar351ead02021-01-16 16:07:01 +0100381 return check_arg_type(prev_type, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100382}
383
384/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100385 * Check "type" is the same basic type as the previous argument, checks list or
386 * dict vs other type, but not member type.
387 * Must not be used for the first argcheck_T entry.
388 */
389 static int
390arg_same_struct_as_prev(type_T *type, argcontext_T *context)
391{
392 type_T *prev_type = context->arg_types[context->arg_idx - 1];
393
394 if (prev_type->tt_type != context->arg_types[context->arg_idx]->tt_type)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100395 return check_arg_type(prev_type, type, context);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100396 return OK;
397}
398
399/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100400 * Check "type" is an item of the list or blob of the previous arg.
Bram Moolenaarca174532020-10-21 16:42:22 +0200401 * Must not be used for the first argcheck_T entry.
402 */
403 static int
404arg_item_of_prev(type_T *type, argcontext_T *context)
405{
406 type_T *prev_type = context->arg_types[context->arg_idx - 1];
407 type_T *expected;
408
409 if (prev_type->tt_type == VAR_LIST)
410 expected = prev_type->tt_member;
411 else if (prev_type->tt_type == VAR_BLOB)
412 expected = &t_number;
413 else
414 // probably VAR_ANY, can't check
415 return OK;
416
Bram Moolenaar351ead02021-01-16 16:07:01 +0100417 return check_arg_type(expected, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200418}
419
Bram Moolenaar94738d82020-10-21 14:25:07 +0200420/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100421 * Check "type" which is the third argument of extend().
422 */
423 static int
424arg_extend3(type_T *type, argcontext_T *context)
425{
426 type_T *first_type = context->arg_types[context->arg_idx - 2];
427
428 if (first_type->tt_type == VAR_LIST)
429 return arg_number(type, context);
430 if (first_type->tt_type == VAR_DICT)
431 return arg_string(type, context);
432 return OK;
433}
434
435
436/*
Bram Moolenaar94738d82020-10-21 14:25:07 +0200437 * Lists of functions that check the argument types of a builtin function.
438 */
Bram Moolenaar80ad3e22021-01-31 20:48:58 +0100439argcheck_T arg1_string[] = {arg_string};
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100440argcheck_T arg3_string_nr_bool[] = {arg_string, arg_number, arg_bool};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200441argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100442argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev};
443argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3};
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100444argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
Bram Moolenaarca174532020-10-21 16:42:22 +0200445argcheck_T arg3_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200446
447/*
448 * Functions that return the return type of a builtin function.
Bram Moolenaara1224cb2020-10-22 12:31:49 +0200449 * Note that "argtypes" is NULL if "argcount" is zero.
Bram Moolenaar94738d82020-10-21 14:25:07 +0200450 */
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100451 static type_T *
452ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
453{
454 return &t_void;
455}
456 static type_T *
457ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
458{
459 return &t_any;
460}
461 static type_T *
Bram Moolenaar403dc312020-10-17 19:29:51 +0200462ret_bool(int argcount UNUSED, type_T **argtypes UNUSED)
463{
464 return &t_bool;
465}
466 static type_T *
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100467ret_number_bool(int argcount UNUSED, type_T **argtypes UNUSED)
468{
469 return &t_number_bool;
470}
471 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100472ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
473{
474 return &t_number;
475}
476 static type_T *
477ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
478{
479 return &t_float;
480}
481 static type_T *
482ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
483{
484 return &t_string;
485}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200486 static type_T *
487ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100488{
489 return &t_list_any;
490}
491 static type_T *
492ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
493{
494 return &t_list_number;
495}
496 static type_T *
497ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
498{
499 return &t_list_string;
500}
501 static type_T *
502ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
503{
504 return &t_list_dict_any;
505}
506 static type_T *
507ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
508{
509 return &t_dict_any;
510}
511 static type_T *
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100512ret_job_info(int argcount, type_T **argtypes UNUSED)
513{
514 if (argcount == 0)
515 return &t_list_job;
516 return &t_dict_any;
517}
518 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100519ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
520{
521 return &t_dict_number;
522}
523 static type_T *
524ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
525{
526 return &t_dict_string;
527}
528 static type_T *
529ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
530{
531 return &t_blob;
532}
533 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200534ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100535{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200536 return &t_func_any;
537}
538 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100539ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
540{
541 return &t_channel;
542}
543 static type_T *
544ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
545{
546 return &t_job;
547}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200548 static type_T *
549ret_first_arg(int argcount, type_T **argtypes)
550{
551 if (argcount > 0)
552 return argtypes[0];
553 return &t_void;
554}
Bram Moolenaarea696852020-11-09 18:31:39 +0100555// for map(): returns first argument but item type may differ
556 static type_T *
557ret_first_cont(int argcount UNUSED, type_T **argtypes)
558{
559 if (argtypes[0]->tt_type == VAR_LIST)
560 return &t_list_any;
561 if (argtypes[0]->tt_type == VAR_DICT)
562 return &t_dict_any;
563 if (argtypes[0]->tt_type == VAR_BLOB)
564 return argtypes[0];
565 return &t_any;
566}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200567
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200568/*
569 * Used for getqflist(): returns list if there is no argument, dict if there is
570 * one.
571 */
572 static type_T *
573ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
574{
575 if (argcount > 0)
576 return &t_dict_any;
577 return &t_list_dict_any;
578}
579
580/*
581 * Used for getloclist(): returns list if there is one argument, dict if there
582 * are two.
583 */
584 static type_T *
585ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
586{
587 if (argcount > 1)
588 return &t_dict_any;
589 return &t_list_dict_any;
590}
591
Bram Moolenaar846178a2020-07-05 17:04:13 +0200592 static type_T *
593ret_argv(int argcount, type_T **argtypes UNUSED)
594{
595 // argv() returns list of strings
596 if (argcount == 0)
597 return &t_list_string;
598
599 // argv(0) returns a string, but argv(-1] returns a list
600 return &t_any;
601}
602
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200603 static type_T *
604ret_remove(int argcount UNUSED, type_T **argtypes)
605{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200606 if (argtypes != NULL)
607 {
608 if (argtypes[0]->tt_type == VAR_LIST
609 || argtypes[0]->tt_type == VAR_DICT)
610 return argtypes[0]->tt_member;
611 if (argtypes[0]->tt_type == VAR_BLOB)
612 return &t_number;
613 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200614 return &t_any;
615}
616
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200617 static type_T *
618ret_getreg(int argcount, type_T **argtypes UNUSED)
619{
620 // Assume that if the third argument is passed it's non-zero
621 if (argcount == 3)
622 return &t_list_string;
623 return &t_string;
624}
625
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200626 static type_T *
627ret_maparg(int argcount, type_T **argtypes UNUSED)
628{
629 // Assume that if the fourth argument is passed it's non-zero
630 if (argcount == 4)
631 return &t_dict_any;
632 return &t_string;
633}
634
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100635static type_T *ret_f_function(int argcount, type_T **argtypes);
636
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200637/*
638 * Array with names and number of arguments of all internal functions
639 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
640 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200641typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200642{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200643 char *f_name; // function name
644 char f_min_argc; // minimal number of arguments
645 char f_max_argc; // maximal number of arguments
646 char f_argtype; // for method: FEARG_ values
Bram Moolenaar94738d82020-10-21 14:25:07 +0200647 argcheck_T *f_argcheck; // list of functions to check argument types
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100648 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
649 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200650 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200651 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200652} funcentry_T;
653
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200654// values for f_argtype; zero means it cannot be used as a method
655#define FEARG_1 1 // base is the first argument
656#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200657#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200658#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200659#define FEARG_LAST 9 // base is the last argument
660
Bram Moolenaar15c47602020-03-26 22:16:48 +0100661#ifdef FEAT_FLOAT
662# define FLOAT_FUNC(name) name
663#else
664# define FLOAT_FUNC(name) NULL
665#endif
666#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
667# define MATH_FUNC(name) name
668#else
669# define MATH_FUNC(name) NULL
670#endif
671#ifdef FEAT_TIMERS
672# define TIMER_FUNC(name) name
673#else
674# define TIMER_FUNC(name) NULL
675#endif
676#ifdef FEAT_JOB_CHANNEL
677# define JOB_FUNC(name) name
678#else
679# define JOB_FUNC(name) NULL
680#endif
681#ifdef FEAT_PROP_POPUP
682# define PROP_FUNC(name) name
683#else
684# define PROP_FUNC(name) NULL
685#endif
686#ifdef FEAT_SIGNS
687# define SIGN_FUNC(name) name
688#else
689# define SIGN_FUNC(name) NULL
690#endif
691#ifdef FEAT_SOUND
692# define SOUND_FUNC(name) name
693#else
694# define SOUND_FUNC(name) NULL
695#endif
696#ifdef FEAT_TERMINAL
697# define TERM_FUNC(name) name
698#else
699# define TERM_FUNC(name) NULL
700#endif
701
Bram Moolenaarac92e252019-08-03 21:58:38 +0200702static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200703{
Bram Moolenaar94738d82020-10-21 14:25:07 +0200704 {"abs", 1, 1, FEARG_1, arg1_float_or_nr,
705 ret_any, FLOAT_FUNC(f_abs)},
706 {"acos", 1, 1, FEARG_1, NULL,
707 ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100708 {"add", 2, 2, FEARG_1, NULL /* arg2_listblob_item */,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200709 ret_first_arg, f_add},
710 {"and", 2, 2, FEARG_1, NULL,
711 ret_number, f_and},
712 {"append", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100713 ret_number_bool, f_append},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200714 {"appendbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100715 ret_number_bool, f_appendbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200716 {"argc", 0, 1, 0, NULL,
717 ret_number, f_argc},
718 {"argidx", 0, 0, 0, NULL,
719 ret_number, f_argidx},
720 {"arglistid", 0, 2, 0, NULL,
721 ret_number, f_arglistid},
722 {"argv", 0, 2, 0, NULL,
723 ret_argv, f_argv},
724 {"asin", 1, 1, FEARG_1, NULL,
725 ret_float, FLOAT_FUNC(f_asin)},
726 {"assert_beeps", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100727 ret_number_bool, f_assert_beeps},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200728 {"assert_equal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100729 ret_number_bool, f_assert_equal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200730 {"assert_equalfile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100731 ret_number_bool, f_assert_equalfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200732 {"assert_exception", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100733 ret_number_bool, f_assert_exception},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200734 {"assert_fails", 1, 5, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100735 ret_number_bool, f_assert_fails},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200736 {"assert_false", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100737 ret_number_bool, f_assert_false},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200738 {"assert_inrange", 3, 4, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100739 ret_number_bool, f_assert_inrange},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200740 {"assert_match", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100741 ret_number_bool, f_assert_match},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200742 {"assert_notequal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100743 ret_number_bool, f_assert_notequal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200744 {"assert_notmatch", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100745 ret_number_bool, f_assert_notmatch},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200746 {"assert_report", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100747 ret_number_bool, f_assert_report},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200748 {"assert_true", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100749 ret_number_bool, f_assert_true},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200750 {"atan", 1, 1, FEARG_1, NULL,
751 ret_float, FLOAT_FUNC(f_atan)},
752 {"atan2", 2, 2, FEARG_1, NULL,
753 ret_float, FLOAT_FUNC(f_atan2)},
754 {"balloon_gettext", 0, 0, 0, NULL,
755 ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100756#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100757 f_balloon_gettext
758#else
759 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100760#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100761 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200762 {"balloon_show", 1, 1, FEARG_1, NULL,
763 ret_void,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100764#ifdef FEAT_BEVAL
765 f_balloon_show
766#else
767 NULL
768#endif
769 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200770 {"balloon_split", 1, 1, FEARG_1, NULL,
771 ret_list_string,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100772#if defined(FEAT_BEVAL_TERM)
773 f_balloon_split
774#else
775 NULL
776#endif
777 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200778 {"browse", 4, 4, 0, NULL,
779 ret_string, f_browse},
780 {"browsedir", 2, 2, 0, NULL,
781 ret_string, f_browsedir},
782 {"bufadd", 1, 1, FEARG_1, NULL,
783 ret_number, f_bufadd},
784 {"bufexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100785 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200786 {"buffer_exists", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100787 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200788 {"buffer_name", 0, 1, FEARG_1, NULL, // obsolete
789 ret_string, f_bufname},
790 {"buffer_number", 0, 1, FEARG_1, NULL, // obsolete
791 ret_number, f_bufnr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200792 {"buflisted", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100793 ret_number_bool, f_buflisted},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200794 {"bufload", 1, 1, FEARG_1, NULL,
795 ret_void, f_bufload},
796 {"bufloaded", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100797 ret_number_bool, f_bufloaded},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200798 {"bufname", 0, 1, FEARG_1, NULL,
799 ret_string, f_bufname},
800 {"bufnr", 0, 2, FEARG_1, NULL,
801 ret_number, f_bufnr},
802 {"bufwinid", 1, 1, FEARG_1, NULL,
803 ret_number, f_bufwinid},
804 {"bufwinnr", 1, 1, FEARG_1, NULL,
805 ret_number, f_bufwinnr},
806 {"byte2line", 1, 1, FEARG_1, NULL,
807 ret_number, f_byte2line},
808 {"byteidx", 2, 2, FEARG_1, NULL,
809 ret_number, f_byteidx},
810 {"byteidxcomp", 2, 2, FEARG_1, NULL,
811 ret_number, f_byteidxcomp},
812 {"call", 2, 3, FEARG_1, NULL,
813 ret_any, f_call},
814 {"ceil", 1, 1, FEARG_1, NULL,
815 ret_float, FLOAT_FUNC(f_ceil)},
816 {"ch_canread", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100817 ret_number_bool, JOB_FUNC(f_ch_canread)},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200818 {"ch_close", 1, 1, FEARG_1, NULL,
819 ret_void, JOB_FUNC(f_ch_close)},
820 {"ch_close_in", 1, 1, FEARG_1, NULL,
821 ret_void, JOB_FUNC(f_ch_close_in)},
822 {"ch_evalexpr", 2, 3, FEARG_1, NULL,
823 ret_any, JOB_FUNC(f_ch_evalexpr)},
824 {"ch_evalraw", 2, 3, FEARG_1, NULL,
825 ret_any, JOB_FUNC(f_ch_evalraw)},
826 {"ch_getbufnr", 2, 2, FEARG_1, NULL,
827 ret_number, JOB_FUNC(f_ch_getbufnr)},
828 {"ch_getjob", 1, 1, FEARG_1, NULL,
829 ret_job, JOB_FUNC(f_ch_getjob)},
830 {"ch_info", 1, 1, FEARG_1, NULL,
831 ret_dict_any, JOB_FUNC(f_ch_info)},
832 {"ch_log", 1, 2, FEARG_1, NULL,
833 ret_void, JOB_FUNC(f_ch_log)},
834 {"ch_logfile", 1, 2, FEARG_1, NULL,
835 ret_void, JOB_FUNC(f_ch_logfile)},
836 {"ch_open", 1, 2, FEARG_1, NULL,
837 ret_channel, JOB_FUNC(f_ch_open)},
838 {"ch_read", 1, 2, FEARG_1, NULL,
839 ret_string, JOB_FUNC(f_ch_read)},
840 {"ch_readblob", 1, 2, FEARG_1, NULL,
841 ret_blob, JOB_FUNC(f_ch_readblob)},
842 {"ch_readraw", 1, 2, FEARG_1, NULL,
843 ret_string, JOB_FUNC(f_ch_readraw)},
844 {"ch_sendexpr", 2, 3, FEARG_1, NULL,
845 ret_void, JOB_FUNC(f_ch_sendexpr)},
846 {"ch_sendraw", 2, 3, FEARG_1, NULL,
847 ret_void, JOB_FUNC(f_ch_sendraw)},
848 {"ch_setoptions", 2, 2, FEARG_1, NULL,
849 ret_void, JOB_FUNC(f_ch_setoptions)},
850 {"ch_status", 1, 2, FEARG_1, NULL,
851 ret_string, JOB_FUNC(f_ch_status)},
852 {"changenr", 0, 0, 0, NULL,
853 ret_number, f_changenr},
854 {"char2nr", 1, 2, FEARG_1, NULL,
855 ret_number, f_char2nr},
856 {"charclass", 1, 1, FEARG_1, NULL,
857 ret_number, f_charclass},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100858 {"charcol", 1, 1, FEARG_1, NULL,
859 ret_number, f_charcol},
Bram Moolenaar17793ef2020-12-28 12:56:58 +0100860 {"charidx", 2, 3, FEARG_1, NULL,
861 ret_number, f_charidx},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200862 {"chdir", 1, 1, FEARG_1, NULL,
863 ret_string, f_chdir},
864 {"cindent", 1, 1, FEARG_1, NULL,
865 ret_number, f_cindent},
866 {"clearmatches", 0, 1, FEARG_1, NULL,
867 ret_void, f_clearmatches},
868 {"col", 1, 1, FEARG_1, NULL,
869 ret_number, f_col},
870 {"complete", 2, 2, FEARG_2, NULL,
871 ret_void, f_complete},
872 {"complete_add", 1, 1, FEARG_1, NULL,
873 ret_number, f_complete_add},
874 {"complete_check", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100875 ret_number_bool, f_complete_check},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200876 {"complete_info", 0, 1, FEARG_1, NULL,
877 ret_dict_any, f_complete_info},
878 {"confirm", 1, 4, FEARG_1, NULL,
879 ret_number, f_confirm},
880 {"copy", 1, 1, FEARG_1, NULL,
881 ret_first_arg, f_copy},
882 {"cos", 1, 1, FEARG_1, NULL,
883 ret_float, FLOAT_FUNC(f_cos)},
884 {"cosh", 1, 1, FEARG_1, NULL,
885 ret_float, FLOAT_FUNC(f_cosh)},
886 {"count", 2, 4, FEARG_1, NULL,
887 ret_number, f_count},
888 {"cscope_connection",0,3, 0, NULL,
889 ret_number, f_cscope_connection},
890 {"cursor", 1, 3, FEARG_1, NULL,
891 ret_number, f_cursor},
892 {"debugbreak", 1, 1, FEARG_1, NULL,
893 ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100894#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100895 f_debugbreak
896#else
897 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200898#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100899 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200900 {"deepcopy", 1, 2, FEARG_1, NULL,
901 ret_first_arg, f_deepcopy},
902 {"delete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100903 ret_number_bool, f_delete},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200904 {"deletebufline", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100905 ret_number_bool, f_deletebufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200906 {"did_filetype", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100907 ret_number_bool, f_did_filetype},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200908 {"diff_filler", 1, 1, FEARG_1, NULL,
909 ret_number, f_diff_filler},
910 {"diff_hlID", 2, 2, FEARG_1, NULL,
911 ret_number, f_diff_hlID},
912 {"echoraw", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100913 ret_void, f_echoraw},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200914 {"empty", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100915 ret_number_bool, f_empty},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200916 {"environ", 0, 0, 0, NULL,
917 ret_dict_string, f_environ},
918 {"escape", 2, 2, FEARG_1, NULL,
919 ret_string, f_escape},
920 {"eval", 1, 1, FEARG_1, NULL,
921 ret_any, f_eval},
922 {"eventhandler", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100923 ret_number_bool, f_eventhandler},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200924 {"executable", 1, 1, FEARG_1, NULL,
925 ret_number, f_executable},
926 {"execute", 1, 2, FEARG_1, NULL,
927 ret_string, f_execute},
928 {"exepath", 1, 1, FEARG_1, NULL,
929 ret_string, f_exepath},
930 {"exists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100931 ret_number_bool, f_exists},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200932 {"exp", 1, 1, FEARG_1, NULL,
933 ret_float, FLOAT_FUNC(f_exp)},
934 {"expand", 1, 3, FEARG_1, NULL,
935 ret_any, f_expand},
936 {"expandcmd", 1, 1, FEARG_1, NULL,
937 ret_string, f_expandcmd},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100938 {"extend", 2, 3, FEARG_1, arg23_extend,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200939 ret_first_arg, f_extend},
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100940 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
941 ret_first_cont, f_extendnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200942 {"feedkeys", 1, 2, FEARG_1, NULL,
943 ret_void, f_feedkeys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200944 {"file_readable", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100945 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200946 {"filereadable", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100947 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200948 {"filewritable", 1, 1, FEARG_1, NULL,
949 ret_number, f_filewritable},
950 {"filter", 2, 2, FEARG_1, NULL,
951 ret_first_arg, f_filter},
952 {"finddir", 1, 3, FEARG_1, NULL,
953 ret_string, f_finddir},
954 {"findfile", 1, 3, FEARG_1, NULL,
955 ret_string, f_findfile},
956 {"flatten", 1, 2, FEARG_1, NULL,
957 ret_list_any, f_flatten},
Bram Moolenaar3b690062021-02-01 20:14:51 +0100958 {"flattennew", 1, 2, FEARG_1, NULL,
959 ret_list_any, f_flattennew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200960 {"float2nr", 1, 1, FEARG_1, NULL,
961 ret_number, FLOAT_FUNC(f_float2nr)},
962 {"floor", 1, 1, FEARG_1, NULL,
963 ret_float, FLOAT_FUNC(f_floor)},
964 {"fmod", 2, 2, FEARG_1, NULL,
965 ret_float, FLOAT_FUNC(f_fmod)},
966 {"fnameescape", 1, 1, FEARG_1, NULL,
967 ret_string, f_fnameescape},
968 {"fnamemodify", 2, 2, FEARG_1, NULL,
969 ret_string, f_fnamemodify},
970 {"foldclosed", 1, 1, FEARG_1, NULL,
971 ret_number, f_foldclosed},
972 {"foldclosedend", 1, 1, FEARG_1, NULL,
973 ret_number, f_foldclosedend},
974 {"foldlevel", 1, 1, FEARG_1, NULL,
975 ret_number, f_foldlevel},
976 {"foldtext", 0, 0, 0, NULL,
977 ret_string, f_foldtext},
978 {"foldtextresult", 1, 1, FEARG_1, NULL,
979 ret_string, f_foldtextresult},
980 {"foreground", 0, 0, 0, NULL,
981 ret_void, f_foreground},
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100982 {"fullcommand", 1, 1, FEARG_1, arg1_string,
983 ret_string, f_fullcommand},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200984 {"funcref", 1, 3, FEARG_1, NULL,
985 ret_func_any, f_funcref},
986 {"function", 1, 3, FEARG_1, NULL,
987 ret_f_function, f_function},
988 {"garbagecollect", 0, 1, 0, NULL,
989 ret_void, f_garbagecollect},
990 {"get", 2, 3, FEARG_1, NULL,
991 ret_any, f_get},
992 {"getbufinfo", 0, 1, FEARG_1, NULL,
993 ret_list_dict_any, f_getbufinfo},
994 {"getbufline", 2, 3, FEARG_1, NULL,
995 ret_list_string, f_getbufline},
996 {"getbufvar", 2, 3, FEARG_1, NULL,
997 ret_any, f_getbufvar},
998 {"getchangelist", 0, 1, FEARG_1, NULL,
999 ret_list_any, f_getchangelist},
1000 {"getchar", 0, 1, 0, NULL,
1001 ret_number, f_getchar},
1002 {"getcharmod", 0, 0, 0, NULL,
1003 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001004 {"getcharpos", 1, 1, FEARG_1, NULL,
1005 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001006 {"getcharsearch", 0, 0, 0, NULL,
1007 ret_dict_any, f_getcharsearch},
1008 {"getcmdline", 0, 0, 0, NULL,
1009 ret_string, f_getcmdline},
1010 {"getcmdpos", 0, 0, 0, NULL,
1011 ret_number, f_getcmdpos},
1012 {"getcmdtype", 0, 0, 0, NULL,
1013 ret_string, f_getcmdtype},
1014 {"getcmdwintype", 0, 0, 0, NULL,
1015 ret_string, f_getcmdwintype},
1016 {"getcompletion", 2, 3, FEARG_1, NULL,
1017 ret_list_string, f_getcompletion},
1018 {"getcurpos", 0, 1, FEARG_1, NULL,
1019 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001020 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
1021 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001022 {"getcwd", 0, 2, FEARG_1, NULL,
1023 ret_string, f_getcwd},
1024 {"getenv", 1, 1, FEARG_1, NULL,
Bram Moolenaar7ad67d12021-03-10 16:08:26 +01001025 ret_any, f_getenv},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001026 {"getfontname", 0, 1, 0, NULL,
1027 ret_string, f_getfontname},
1028 {"getfperm", 1, 1, FEARG_1, NULL,
1029 ret_string, f_getfperm},
1030 {"getfsize", 1, 1, FEARG_1, NULL,
1031 ret_number, f_getfsize},
1032 {"getftime", 1, 1, FEARG_1, NULL,
1033 ret_number, f_getftime},
1034 {"getftype", 1, 1, FEARG_1, NULL,
1035 ret_string, f_getftype},
1036 {"getimstatus", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001037 ret_number_bool, f_getimstatus},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001038 {"getjumplist", 0, 2, FEARG_1, NULL,
1039 ret_list_any, f_getjumplist},
1040 {"getline", 1, 2, FEARG_1, NULL,
1041 ret_f_getline, f_getline},
1042 {"getloclist", 1, 2, 0, NULL,
1043 ret_list_or_dict_1, f_getloclist},
1044 {"getmarklist", 0, 1, FEARG_1, NULL,
1045 ret_list_dict_any, f_getmarklist},
1046 {"getmatches", 0, 1, 0, NULL,
1047 ret_list_dict_any, f_getmatches},
1048 {"getmousepos", 0, 0, 0, NULL,
1049 ret_dict_number, f_getmousepos},
1050 {"getpid", 0, 0, 0, NULL,
1051 ret_number, f_getpid},
1052 {"getpos", 1, 1, FEARG_1, NULL,
1053 ret_list_number, f_getpos},
1054 {"getqflist", 0, 1, 0, NULL,
1055 ret_list_or_dict_0, f_getqflist},
1056 {"getreg", 0, 3, FEARG_1, NULL,
1057 ret_getreg, f_getreg},
1058 {"getreginfo", 0, 1, FEARG_1, NULL,
1059 ret_dict_any, f_getreginfo},
1060 {"getregtype", 0, 1, FEARG_1, NULL,
1061 ret_string, f_getregtype},
1062 {"gettabinfo", 0, 1, FEARG_1, NULL,
1063 ret_list_dict_any, f_gettabinfo},
1064 {"gettabvar", 2, 3, FEARG_1, NULL,
1065 ret_any, f_gettabvar},
1066 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1067 ret_any, f_gettabwinvar},
1068 {"gettagstack", 0, 1, FEARG_1, NULL,
1069 ret_dict_any, f_gettagstack},
1070 {"gettext", 1, 1, FEARG_1, NULL,
1071 ret_string, f_gettext},
1072 {"getwininfo", 0, 1, FEARG_1, NULL,
1073 ret_list_dict_any, f_getwininfo},
1074 {"getwinpos", 0, 1, FEARG_1, NULL,
1075 ret_list_number, f_getwinpos},
1076 {"getwinposx", 0, 0, 0, NULL,
1077 ret_number, f_getwinposx},
1078 {"getwinposy", 0, 0, 0, NULL,
1079 ret_number, f_getwinposy},
1080 {"getwinvar", 2, 3, FEARG_1, NULL,
1081 ret_any, f_getwinvar},
1082 {"glob", 1, 4, FEARG_1, NULL,
1083 ret_any, f_glob},
1084 {"glob2regpat", 1, 1, FEARG_1, NULL,
1085 ret_string, f_glob2regpat},
1086 {"globpath", 2, 5, FEARG_2, NULL,
1087 ret_any, f_globpath},
1088 {"has", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001089 ret_number_bool, f_has},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001090 {"has_key", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001091 ret_number_bool, f_has_key},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001092 {"haslocaldir", 0, 2, FEARG_1, NULL,
1093 ret_number, f_haslocaldir},
1094 {"hasmapto", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001095 ret_number_bool, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001096 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1097 ret_number, f_hlID},
1098 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001099 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001100 {"histadd", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001101 ret_number_bool, f_histadd},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001102 {"histdel", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001103 ret_number_bool, f_histdel},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001104 {"histget", 1, 2, FEARG_1, NULL,
1105 ret_string, f_histget},
1106 {"histnr", 1, 1, FEARG_1, NULL,
1107 ret_number, f_histnr},
1108 {"hlID", 1, 1, FEARG_1, NULL,
1109 ret_number, f_hlID},
1110 {"hlexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001111 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001112 {"hostname", 0, 0, 0, NULL,
1113 ret_string, f_hostname},
1114 {"iconv", 3, 3, FEARG_1, NULL,
1115 ret_string, f_iconv},
1116 {"indent", 1, 1, FEARG_1, NULL,
1117 ret_number, f_indent},
1118 {"index", 2, 4, FEARG_1, NULL,
1119 ret_number, f_index},
1120 {"input", 1, 3, FEARG_1, NULL,
1121 ret_string, f_input},
1122 {"inputdialog", 1, 3, FEARG_1, NULL,
1123 ret_string, f_inputdialog},
1124 {"inputlist", 1, 1, FEARG_1, NULL,
1125 ret_number, f_inputlist},
1126 {"inputrestore", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001127 ret_number_bool, f_inputrestore},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001128 {"inputsave", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001129 ret_number_bool, f_inputsave},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001130 {"inputsecret", 1, 2, FEARG_1, NULL,
1131 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001132 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001133 ret_first_arg, f_insert},
1134 {"interrupt", 0, 0, 0, NULL,
1135 ret_void, f_interrupt},
1136 {"invert", 1, 1, FEARG_1, NULL,
1137 ret_number, f_invert},
1138 {"isdirectory", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001139 ret_number_bool, f_isdirectory},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001140 {"isinf", 1, 1, FEARG_1, NULL,
1141 ret_number, MATH_FUNC(f_isinf)},
1142 {"islocked", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001143 ret_number_bool, f_islocked},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001144 {"isnan", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001145 ret_number_bool, MATH_FUNC(f_isnan)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001146 {"items", 1, 1, FEARG_1, NULL,
1147 ret_list_any, f_items},
1148 {"job_getchannel", 1, 1, FEARG_1, NULL,
1149 ret_channel, JOB_FUNC(f_job_getchannel)},
1150 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001151 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001152 {"job_setoptions", 2, 2, FEARG_1, NULL,
1153 ret_void, JOB_FUNC(f_job_setoptions)},
1154 {"job_start", 1, 2, FEARG_1, NULL,
1155 ret_job, JOB_FUNC(f_job_start)},
1156 {"job_status", 1, 1, FEARG_1, NULL,
1157 ret_string, JOB_FUNC(f_job_status)},
1158 {"job_stop", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001159 ret_number_bool, JOB_FUNC(f_job_stop)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001160 {"join", 1, 2, FEARG_1, NULL,
1161 ret_string, f_join},
1162 {"js_decode", 1, 1, FEARG_1, NULL,
1163 ret_any, f_js_decode},
1164 {"js_encode", 1, 1, FEARG_1, NULL,
1165 ret_string, f_js_encode},
1166 {"json_decode", 1, 1, FEARG_1, NULL,
1167 ret_any, f_json_decode},
1168 {"json_encode", 1, 1, FEARG_1, NULL,
1169 ret_string, f_json_encode},
1170 {"keys", 1, 1, FEARG_1, NULL,
1171 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001172 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1173 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001174 {"len", 1, 1, FEARG_1, NULL,
1175 ret_number, f_len},
1176 {"libcall", 3, 3, FEARG_3, NULL,
1177 ret_string, f_libcall},
1178 {"libcallnr", 3, 3, FEARG_3, NULL,
1179 ret_number, f_libcallnr},
1180 {"line", 1, 2, FEARG_1, NULL,
1181 ret_number, f_line},
1182 {"line2byte", 1, 1, FEARG_1, NULL,
1183 ret_number, f_line2byte},
1184 {"lispindent", 1, 1, FEARG_1, NULL,
1185 ret_number, f_lispindent},
1186 {"list2str", 1, 2, FEARG_1, NULL,
1187 ret_string, f_list2str},
1188 {"listener_add", 1, 2, FEARG_2, NULL,
1189 ret_number, f_listener_add},
1190 {"listener_flush", 0, 1, FEARG_1, NULL,
1191 ret_void, f_listener_flush},
1192 {"listener_remove", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001193 ret_number_bool, f_listener_remove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001194 {"localtime", 0, 0, 0, NULL,
1195 ret_number, f_localtime},
1196 {"log", 1, 1, FEARG_1, NULL,
1197 ret_float, FLOAT_FUNC(f_log)},
1198 {"log10", 1, 1, FEARG_1, NULL,
1199 ret_float, FLOAT_FUNC(f_log10)},
1200 {"luaeval", 1, 2, FEARG_1, NULL,
1201 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001202#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001203 f_luaeval
1204#else
1205 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001206#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001207 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001208 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001209 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001210 {"maparg", 1, 4, FEARG_1, NULL,
1211 ret_maparg, f_maparg},
1212 {"mapcheck", 1, 3, FEARG_1, NULL,
1213 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001214 {"mapnew", 2, 2, FEARG_1, NULL,
1215 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001216 {"mapset", 3, 3, FEARG_1, NULL,
1217 ret_void, f_mapset},
1218 {"match", 2, 4, FEARG_1, NULL,
1219 ret_any, f_match},
1220 {"matchadd", 2, 5, FEARG_1, NULL,
1221 ret_number, f_matchadd},
1222 {"matchaddpos", 2, 5, FEARG_1, NULL,
1223 ret_number, f_matchaddpos},
1224 {"matcharg", 1, 1, FEARG_1, NULL,
1225 ret_list_string, f_matcharg},
1226 {"matchdelete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001227 ret_number_bool, f_matchdelete},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001228 {"matchend", 2, 4, FEARG_1, NULL,
1229 ret_number, f_matchend},
1230 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1231 ret_list_string, f_matchfuzzy},
1232 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1233 ret_list_any, f_matchfuzzypos},
1234 {"matchlist", 2, 4, FEARG_1, NULL,
1235 ret_list_string, f_matchlist},
1236 {"matchstr", 2, 4, FEARG_1, NULL,
1237 ret_string, f_matchstr},
1238 {"matchstrpos", 2, 4, FEARG_1, NULL,
1239 ret_list_any, f_matchstrpos},
1240 {"max", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001241 ret_number, f_max},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001242 {"menu_info", 1, 2, FEARG_1, NULL,
1243 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001244#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001245 f_menu_info
1246#else
1247 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001248#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001249 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001250 {"min", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001251 ret_number, f_min},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001252 {"mkdir", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001253 ret_number_bool, f_mkdir},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001254 {"mode", 0, 1, FEARG_1, NULL,
1255 ret_string, f_mode},
1256 {"mzeval", 1, 1, FEARG_1, NULL,
1257 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001258#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001259 f_mzeval
1260#else
1261 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001262#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001263 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001264 {"nextnonblank", 1, 1, FEARG_1, NULL,
1265 ret_number, f_nextnonblank},
1266 {"nr2char", 1, 2, FEARG_1, NULL,
1267 ret_string, f_nr2char},
1268 {"or", 2, 2, FEARG_1, NULL,
1269 ret_number, f_or},
1270 {"pathshorten", 1, 2, FEARG_1, NULL,
1271 ret_string, f_pathshorten},
1272 {"perleval", 1, 1, FEARG_1, NULL,
1273 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001274#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001275 f_perleval
1276#else
1277 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001278#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001279 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001280 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1281 ret_number, PROP_FUNC(f_popup_atcursor)},
1282 {"popup_beval", 2, 2, FEARG_1, NULL,
1283 ret_number, PROP_FUNC(f_popup_beval)},
1284 {"popup_clear", 0, 1, 0, NULL,
1285 ret_void, PROP_FUNC(f_popup_clear)},
1286 {"popup_close", 1, 2, FEARG_1, NULL,
1287 ret_void, PROP_FUNC(f_popup_close)},
1288 {"popup_create", 2, 2, FEARG_1, NULL,
1289 ret_number, PROP_FUNC(f_popup_create)},
1290 {"popup_dialog", 2, 2, FEARG_1, NULL,
1291 ret_number, PROP_FUNC(f_popup_dialog)},
1292 {"popup_filter_menu", 2, 2, 0, NULL,
1293 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1294 {"popup_filter_yesno", 2, 2, 0, NULL,
1295 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1296 {"popup_findinfo", 0, 0, 0, NULL,
1297 ret_number, PROP_FUNC(f_popup_findinfo)},
1298 {"popup_findpreview", 0, 0, 0, NULL,
1299 ret_number, PROP_FUNC(f_popup_findpreview)},
1300 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1301 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1302 {"popup_getpos", 1, 1, FEARG_1, NULL,
1303 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1304 {"popup_hide", 1, 1, FEARG_1, NULL,
1305 ret_void, PROP_FUNC(f_popup_hide)},
1306 {"popup_list", 0, 0, 0, NULL,
1307 ret_list_number, PROP_FUNC(f_popup_list)},
1308 {"popup_locate", 2, 2, 0, NULL,
1309 ret_number, PROP_FUNC(f_popup_locate)},
1310 {"popup_menu", 2, 2, FEARG_1, NULL,
1311 ret_number, PROP_FUNC(f_popup_menu)},
1312 {"popup_move", 2, 2, FEARG_1, NULL,
1313 ret_void, PROP_FUNC(f_popup_move)},
1314 {"popup_notification", 2, 2, FEARG_1, NULL,
1315 ret_number, PROP_FUNC(f_popup_notification)},
1316 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1317 ret_void, PROP_FUNC(f_popup_setoptions)},
1318 {"popup_settext", 2, 2, FEARG_1, NULL,
1319 ret_void, PROP_FUNC(f_popup_settext)},
1320 {"popup_show", 1, 1, FEARG_1, NULL,
1321 ret_void, PROP_FUNC(f_popup_show)},
1322 {"pow", 2, 2, FEARG_1, NULL,
1323 ret_float, FLOAT_FUNC(f_pow)},
1324 {"prevnonblank", 1, 1, FEARG_1, NULL,
1325 ret_number, f_prevnonblank},
1326 {"printf", 1, 19, FEARG_2, NULL,
1327 ret_string, f_printf},
1328 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1329 ret_string, JOB_FUNC(f_prompt_getprompt)},
1330 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1331 ret_void, JOB_FUNC(f_prompt_setcallback)},
1332 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1333 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1334 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1335 ret_void, JOB_FUNC(f_prompt_setprompt)},
1336 {"prop_add", 3, 3, FEARG_1, NULL,
1337 ret_void, PROP_FUNC(f_prop_add)},
1338 {"prop_clear", 1, 3, FEARG_1, NULL,
1339 ret_void, PROP_FUNC(f_prop_clear)},
1340 {"prop_find", 1, 2, FEARG_1, NULL,
1341 ret_dict_any, PROP_FUNC(f_prop_find)},
1342 {"prop_list", 1, 2, FEARG_1, NULL,
1343 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1344 {"prop_remove", 1, 3, FEARG_1, NULL,
1345 ret_number, PROP_FUNC(f_prop_remove)},
1346 {"prop_type_add", 2, 2, FEARG_1, NULL,
1347 ret_void, PROP_FUNC(f_prop_type_add)},
1348 {"prop_type_change", 2, 2, FEARG_1, NULL,
1349 ret_void, PROP_FUNC(f_prop_type_change)},
1350 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1351 ret_void, PROP_FUNC(f_prop_type_delete)},
1352 {"prop_type_get", 1, 2, FEARG_1, NULL,
1353 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1354 {"prop_type_list", 0, 1, FEARG_1, NULL,
1355 ret_list_string, PROP_FUNC(f_prop_type_list)},
1356 {"pum_getpos", 0, 0, 0, NULL,
1357 ret_dict_number, f_pum_getpos},
1358 {"pumvisible", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001359 ret_number_bool, f_pumvisible},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001360 {"py3eval", 1, 1, FEARG_1, NULL,
1361 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001362#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001363 f_py3eval
1364#else
1365 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001366#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001367 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001368 {"pyeval", 1, 1, FEARG_1, NULL,
1369 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001370#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001371 f_pyeval
1372#else
1373 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001374#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001375 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001376 {"pyxeval", 1, 1, FEARG_1, NULL,
1377 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001378#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001379 f_pyxeval
1380#else
1381 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001382#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001383 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001384 {"rand", 0, 1, FEARG_1, NULL,
1385 ret_number, f_rand},
1386 {"range", 1, 3, FEARG_1, NULL,
1387 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001388 {"readblob", 1, 1, FEARG_1, NULL,
1389 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001390 {"readdir", 1, 3, FEARG_1, NULL,
1391 ret_list_string, f_readdir},
1392 {"readdirex", 1, 3, FEARG_1, NULL,
1393 ret_list_dict_any, f_readdirex},
1394 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001395 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001396 {"reduce", 2, 3, FEARG_1, NULL,
1397 ret_any, f_reduce},
1398 {"reg_executing", 0, 0, 0, NULL,
1399 ret_string, f_reg_executing},
1400 {"reg_recording", 0, 0, 0, NULL,
1401 ret_string, f_reg_recording},
1402 {"reltime", 0, 2, FEARG_1, NULL,
1403 ret_list_any, f_reltime},
1404 {"reltimefloat", 1, 1, FEARG_1, NULL,
1405 ret_float, FLOAT_FUNC(f_reltimefloat)},
1406 {"reltimestr", 1, 1, FEARG_1, NULL,
1407 ret_string, f_reltimestr},
1408 {"remote_expr", 2, 4, FEARG_1, NULL,
1409 ret_string, f_remote_expr},
1410 {"remote_foreground", 1, 1, FEARG_1, NULL,
1411 ret_string, f_remote_foreground},
1412 {"remote_peek", 1, 2, FEARG_1, NULL,
1413 ret_number, f_remote_peek},
1414 {"remote_read", 1, 2, FEARG_1, NULL,
1415 ret_string, f_remote_read},
1416 {"remote_send", 2, 3, FEARG_1, NULL,
1417 ret_string, f_remote_send},
1418 {"remote_startserver", 1, 1, FEARG_1, NULL,
1419 ret_void, f_remote_startserver},
1420 {"remove", 2, 3, FEARG_1, NULL,
1421 ret_remove, f_remove},
1422 {"rename", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001423 ret_number_bool, f_rename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001424 {"repeat", 2, 2, FEARG_1, NULL,
1425 ret_first_arg, f_repeat},
1426 {"resolve", 1, 1, FEARG_1, NULL,
1427 ret_string, f_resolve},
1428 {"reverse", 1, 1, FEARG_1, NULL,
1429 ret_first_arg, f_reverse},
1430 {"round", 1, 1, FEARG_1, NULL,
1431 ret_float, FLOAT_FUNC(f_round)},
1432 {"rubyeval", 1, 1, FEARG_1, NULL,
1433 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001434#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001435 f_rubyeval
1436#else
1437 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001438#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001439 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001440 {"screenattr", 2, 2, FEARG_1, NULL,
1441 ret_number, f_screenattr},
1442 {"screenchar", 2, 2, FEARG_1, NULL,
1443 ret_number, f_screenchar},
1444 {"screenchars", 2, 2, FEARG_1, NULL,
1445 ret_list_number, f_screenchars},
1446 {"screencol", 0, 0, 0, NULL,
1447 ret_number, f_screencol},
1448 {"screenpos", 3, 3, FEARG_1, NULL,
1449 ret_dict_number, f_screenpos},
1450 {"screenrow", 0, 0, 0, NULL,
1451 ret_number, f_screenrow},
1452 {"screenstring", 2, 2, FEARG_1, NULL,
1453 ret_string, f_screenstring},
1454 {"search", 1, 5, FEARG_1, NULL,
1455 ret_number, f_search},
1456 {"searchcount", 0, 1, FEARG_1, NULL,
1457 ret_dict_any, f_searchcount},
1458 {"searchdecl", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001459 ret_number_bool, f_searchdecl},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001460 {"searchpair", 3, 7, 0, NULL,
1461 ret_number, f_searchpair},
1462 {"searchpairpos", 3, 7, 0, NULL,
1463 ret_list_number, f_searchpairpos},
1464 {"searchpos", 1, 5, FEARG_1, NULL,
1465 ret_list_number, f_searchpos},
1466 {"server2client", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001467 ret_number_bool, f_server2client},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001468 {"serverlist", 0, 0, 0, NULL,
1469 ret_string, f_serverlist},
1470 {"setbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001471 ret_number_bool, f_setbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001472 {"setbufvar", 3, 3, FEARG_3, NULL,
1473 ret_void, f_setbufvar},
1474 {"setcellwidths", 1, 1, FEARG_1, NULL,
1475 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001476 {"setcharpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001477 ret_number_bool, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001478 {"setcharsearch", 1, 1, FEARG_1, NULL,
1479 ret_void, f_setcharsearch},
1480 {"setcmdpos", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001481 ret_number_bool, f_setcmdpos},
1482 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1483 ret_number_bool, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001484 {"setenv", 2, 2, FEARG_2, NULL,
1485 ret_void, f_setenv},
1486 {"setfperm", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001487 ret_number_bool, f_setfperm},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001488 {"setline", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001489 ret_number_bool, f_setline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001490 {"setloclist", 2, 4, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001491 ret_number_bool, f_setloclist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001492 {"setmatches", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001493 ret_number_bool, f_setmatches},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001494 {"setpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001495 ret_number_bool, f_setpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001496 {"setqflist", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001497 ret_number_bool, f_setqflist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001498 {"setreg", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001499 ret_number_bool, f_setreg},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001500 {"settabvar", 3, 3, FEARG_3, NULL,
1501 ret_void, f_settabvar},
1502 {"settabwinvar", 4, 4, FEARG_4, NULL,
1503 ret_void, f_settabwinvar},
1504 {"settagstack", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001505 ret_number_bool, f_settagstack},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001506 {"setwinvar", 3, 3, FEARG_3, NULL,
1507 ret_void, f_setwinvar},
1508 {"sha256", 1, 1, FEARG_1, NULL,
1509 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001510#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001511 f_sha256
1512#else
1513 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001514#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001515 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001516 {"shellescape", 1, 2, FEARG_1, NULL,
1517 ret_string, f_shellescape},
1518 {"shiftwidth", 0, 1, FEARG_1, NULL,
1519 ret_number, f_shiftwidth},
1520 {"sign_define", 1, 2, FEARG_1, NULL,
1521 ret_any, SIGN_FUNC(f_sign_define)},
1522 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1523 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1524 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1525 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1526 {"sign_jump", 3, 3, FEARG_1, NULL,
1527 ret_number, SIGN_FUNC(f_sign_jump)},
1528 {"sign_place", 4, 5, FEARG_1, NULL,
1529 ret_number, SIGN_FUNC(f_sign_place)},
1530 {"sign_placelist", 1, 1, FEARG_1, NULL,
1531 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1532 {"sign_undefine", 0, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001533 ret_number_bool, SIGN_FUNC(f_sign_undefine)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001534 {"sign_unplace", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001535 ret_number_bool, SIGN_FUNC(f_sign_unplace)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001536 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1537 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1538 {"simplify", 1, 1, FEARG_1, NULL,
1539 ret_string, f_simplify},
1540 {"sin", 1, 1, FEARG_1, NULL,
1541 ret_float, FLOAT_FUNC(f_sin)},
1542 {"sinh", 1, 1, FEARG_1, NULL,
1543 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001544 {"slice", 2, 3, FEARG_1, NULL,
1545 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001546 {"sort", 1, 3, FEARG_1, NULL,
1547 ret_first_arg, f_sort},
1548 {"sound_clear", 0, 0, 0, NULL,
1549 ret_void, SOUND_FUNC(f_sound_clear)},
1550 {"sound_playevent", 1, 2, FEARG_1, NULL,
1551 ret_number, SOUND_FUNC(f_sound_playevent)},
1552 {"sound_playfile", 1, 2, FEARG_1, NULL,
1553 ret_number, SOUND_FUNC(f_sound_playfile)},
1554 {"sound_stop", 1, 1, FEARG_1, NULL,
1555 ret_void, SOUND_FUNC(f_sound_stop)},
1556 {"soundfold", 1, 1, FEARG_1, NULL,
1557 ret_string, f_soundfold},
1558 {"spellbadword", 0, 1, FEARG_1, NULL,
1559 ret_list_string, f_spellbadword},
1560 {"spellsuggest", 1, 3, FEARG_1, NULL,
1561 ret_list_string, f_spellsuggest},
1562 {"split", 1, 3, FEARG_1, NULL,
1563 ret_list_string, f_split},
1564 {"sqrt", 1, 1, FEARG_1, NULL,
1565 ret_float, FLOAT_FUNC(f_sqrt)},
1566 {"srand", 0, 1, FEARG_1, NULL,
1567 ret_list_number, f_srand},
1568 {"state", 0, 1, FEARG_1, NULL,
1569 ret_string, f_state},
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001570 {"str2float", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001571 ret_float, FLOAT_FUNC(f_str2float)},
1572 {"str2list", 1, 2, FEARG_1, NULL,
1573 ret_list_number, f_str2list},
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001574 {"str2nr", 1, 3, FEARG_1, arg3_string_nr_bool,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001575 ret_number, f_str2nr},
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01001576 {"strcharlen", 1, 1, FEARG_1, NULL,
1577 ret_number, f_strcharlen},
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01001578 {"strcharpart", 2, 4, FEARG_1, NULL,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001579 ret_string, f_strcharpart},
1580 {"strchars", 1, 2, FEARG_1, NULL,
1581 ret_number, f_strchars},
1582 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1583 ret_number, f_strdisplaywidth},
1584 {"strftime", 1, 2, FEARG_1, NULL,
1585 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001586#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001587 f_strftime
1588#else
1589 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001590#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001591 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001592 {"strgetchar", 2, 2, FEARG_1, NULL,
1593 ret_number, f_strgetchar},
1594 {"stridx", 2, 3, FEARG_1, NULL,
1595 ret_number, f_stridx},
1596 {"string", 1, 1, FEARG_1, NULL,
1597 ret_string, f_string},
1598 {"strlen", 1, 1, FEARG_1, NULL,
1599 ret_number, f_strlen},
1600 {"strpart", 2, 4, FEARG_1, NULL,
1601 ret_string, f_strpart},
1602 {"strptime", 2, 2, FEARG_1, NULL,
1603 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001604#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001605 f_strptime
1606#else
1607 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001608#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001609 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001610 {"strridx", 2, 3, FEARG_1, NULL,
1611 ret_number, f_strridx},
1612 {"strtrans", 1, 1, FEARG_1, NULL,
1613 ret_string, f_strtrans},
1614 {"strwidth", 1, 1, FEARG_1, NULL,
1615 ret_number, f_strwidth},
1616 {"submatch", 1, 2, FEARG_1, NULL,
1617 ret_string, f_submatch},
1618 {"substitute", 4, 4, FEARG_1, NULL,
1619 ret_string, f_substitute},
1620 {"swapinfo", 1, 1, FEARG_1, NULL,
1621 ret_dict_any, f_swapinfo},
1622 {"swapname", 1, 1, FEARG_1, NULL,
1623 ret_string, f_swapname},
1624 {"synID", 3, 3, 0, NULL,
1625 ret_number, f_synID},
1626 {"synIDattr", 2, 3, FEARG_1, NULL,
1627 ret_string, f_synIDattr},
1628 {"synIDtrans", 1, 1, FEARG_1, NULL,
1629 ret_number, f_synIDtrans},
1630 {"synconcealed", 2, 2, 0, NULL,
1631 ret_list_any, f_synconcealed},
1632 {"synstack", 2, 2, 0, NULL,
1633 ret_list_number, f_synstack},
1634 {"system", 1, 2, FEARG_1, NULL,
1635 ret_string, f_system},
1636 {"systemlist", 1, 2, FEARG_1, NULL,
1637 ret_list_string, f_systemlist},
1638 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1639 ret_list_number, f_tabpagebuflist},
1640 {"tabpagenr", 0, 1, 0, NULL,
1641 ret_number, f_tabpagenr},
1642 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1643 ret_number, f_tabpagewinnr},
1644 {"tagfiles", 0, 0, 0, NULL,
1645 ret_list_string, f_tagfiles},
1646 {"taglist", 1, 2, FEARG_1, NULL,
1647 ret_list_dict_any, f_taglist},
1648 {"tan", 1, 1, FEARG_1, NULL,
1649 ret_float, FLOAT_FUNC(f_tan)},
1650 {"tanh", 1, 1, FEARG_1, NULL,
1651 ret_float, FLOAT_FUNC(f_tanh)},
1652 {"tempname", 0, 0, 0, NULL,
1653 ret_string, f_tempname},
1654 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1655 ret_number, TERM_FUNC(f_term_dumpdiff)},
1656 {"term_dumpload", 1, 2, FEARG_1, NULL,
1657 ret_number, TERM_FUNC(f_term_dumpload)},
1658 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1659 ret_void, TERM_FUNC(f_term_dumpwrite)},
1660 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1661 ret_number, TERM_FUNC(f_term_getaltscreen)},
1662 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1663 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001664#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001665 f_term_getansicolors
1666#else
1667 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001668#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001669 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001670 {"term_getattr", 2, 2, FEARG_1, NULL,
1671 ret_number, TERM_FUNC(f_term_getattr)},
1672 {"term_getcursor", 1, 1, FEARG_1, NULL,
1673 ret_list_any, TERM_FUNC(f_term_getcursor)},
1674 {"term_getjob", 1, 1, FEARG_1, NULL,
1675 ret_job, TERM_FUNC(f_term_getjob)},
1676 {"term_getline", 2, 2, FEARG_1, NULL,
1677 ret_string, TERM_FUNC(f_term_getline)},
1678 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1679 ret_number, TERM_FUNC(f_term_getscrolled)},
1680 {"term_getsize", 1, 1, FEARG_1, NULL,
1681 ret_list_number, TERM_FUNC(f_term_getsize)},
1682 {"term_getstatus", 1, 1, FEARG_1, NULL,
1683 ret_string, TERM_FUNC(f_term_getstatus)},
1684 {"term_gettitle", 1, 1, FEARG_1, NULL,
1685 ret_string, TERM_FUNC(f_term_gettitle)},
1686 {"term_gettty", 1, 2, FEARG_1, NULL,
1687 ret_string, TERM_FUNC(f_term_gettty)},
1688 {"term_list", 0, 0, 0, NULL,
1689 ret_list_number, TERM_FUNC(f_term_list)},
1690 {"term_scrape", 2, 2, FEARG_1, NULL,
1691 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1692 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1693 ret_void, TERM_FUNC(f_term_sendkeys)},
1694 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1695 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001696#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001697 f_term_setansicolors
1698#else
1699 NULL
1700#endif
1701 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001702 {"term_setapi", 2, 2, FEARG_1, NULL,
1703 ret_void, TERM_FUNC(f_term_setapi)},
1704 {"term_setkill", 2, 2, FEARG_1, NULL,
1705 ret_void, TERM_FUNC(f_term_setkill)},
1706 {"term_setrestore", 2, 2, FEARG_1, NULL,
1707 ret_void, TERM_FUNC(f_term_setrestore)},
1708 {"term_setsize", 3, 3, FEARG_1, NULL,
1709 ret_void, TERM_FUNC(f_term_setsize)},
1710 {"term_start", 1, 2, FEARG_1, NULL,
1711 ret_number, TERM_FUNC(f_term_start)},
1712 {"term_wait", 1, 2, FEARG_1, NULL,
1713 ret_void, TERM_FUNC(f_term_wait)},
1714 {"terminalprops", 0, 0, 0, NULL,
1715 ret_dict_string, f_terminalprops},
1716 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1717 ret_void, f_test_alloc_fail},
1718 {"test_autochdir", 0, 0, 0, NULL,
1719 ret_void, f_test_autochdir},
1720 {"test_feedinput", 1, 1, FEARG_1, NULL,
1721 ret_void, f_test_feedinput},
1722 {"test_garbagecollect_now", 0, 0, 0, NULL,
1723 ret_void, f_test_garbagecollect_now},
1724 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1725 ret_void, f_test_garbagecollect_soon},
1726 {"test_getvalue", 1, 1, FEARG_1, NULL,
1727 ret_number, f_test_getvalue},
1728 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1729 ret_void, f_test_ignore_error},
1730 {"test_null_blob", 0, 0, 0, NULL,
1731 ret_blob, f_test_null_blob},
1732 {"test_null_channel", 0, 0, 0, NULL,
1733 ret_channel, JOB_FUNC(f_test_null_channel)},
1734 {"test_null_dict", 0, 0, 0, NULL,
1735 ret_dict_any, f_test_null_dict},
1736 {"test_null_function", 0, 0, 0, NULL,
1737 ret_func_any, f_test_null_function},
1738 {"test_null_job", 0, 0, 0, NULL,
1739 ret_job, JOB_FUNC(f_test_null_job)},
1740 {"test_null_list", 0, 0, 0, NULL,
1741 ret_list_any, f_test_null_list},
1742 {"test_null_partial", 0, 0, 0, NULL,
1743 ret_func_any, f_test_null_partial},
1744 {"test_null_string", 0, 0, 0, NULL,
1745 ret_string, f_test_null_string},
1746 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1747 ret_void, f_test_option_not_set},
1748 {"test_override", 2, 2, FEARG_2, NULL,
1749 ret_void, f_test_override},
1750 {"test_refcount", 1, 1, FEARG_1, NULL,
1751 ret_number, f_test_refcount},
1752 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1753 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001754#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001755 f_test_scrollbar
1756#else
1757 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001758#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001759 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001760 {"test_setmouse", 2, 2, 0, NULL,
1761 ret_void, f_test_setmouse},
1762 {"test_settime", 1, 1, FEARG_1, NULL,
1763 ret_void, f_test_settime},
1764 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1765 ret_void, f_test_srand_seed},
1766 {"test_unknown", 0, 0, 0, NULL,
1767 ret_any, f_test_unknown},
1768 {"test_void", 0, 0, 0, NULL,
1769 ret_void, f_test_void},
1770 {"timer_info", 0, 1, FEARG_1, NULL,
1771 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1772 {"timer_pause", 2, 2, FEARG_1, NULL,
1773 ret_void, TIMER_FUNC(f_timer_pause)},
1774 {"timer_start", 2, 3, FEARG_1, NULL,
1775 ret_number, TIMER_FUNC(f_timer_start)},
1776 {"timer_stop", 1, 1, FEARG_1, NULL,
1777 ret_void, TIMER_FUNC(f_timer_stop)},
1778 {"timer_stopall", 0, 0, 0, NULL,
1779 ret_void, TIMER_FUNC(f_timer_stopall)},
1780 {"tolower", 1, 1, FEARG_1, NULL,
1781 ret_string, f_tolower},
1782 {"toupper", 1, 1, FEARG_1, NULL,
1783 ret_string, f_toupper},
1784 {"tr", 3, 3, FEARG_1, NULL,
1785 ret_string, f_tr},
1786 {"trim", 1, 3, FEARG_1, NULL,
1787 ret_string, f_trim},
1788 {"trunc", 1, 1, FEARG_1, NULL,
1789 ret_float, FLOAT_FUNC(f_trunc)},
1790 {"type", 1, 1, FEARG_1, NULL,
1791 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001792 {"typename", 1, 1, FEARG_1, NULL,
1793 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001794 {"undofile", 1, 1, FEARG_1, NULL,
1795 ret_string, f_undofile},
1796 {"undotree", 0, 0, 0, NULL,
1797 ret_dict_any, f_undotree},
1798 {"uniq", 1, 3, FEARG_1, NULL,
1799 ret_list_any, f_uniq},
1800 {"values", 1, 1, FEARG_1, NULL,
1801 ret_list_any, f_values},
1802 {"virtcol", 1, 1, FEARG_1, NULL,
1803 ret_number, f_virtcol},
1804 {"visualmode", 0, 1, 0, NULL,
1805 ret_string, f_visualmode},
1806 {"wildmenumode", 0, 0, 0, NULL,
1807 ret_number, f_wildmenumode},
1808 {"win_execute", 2, 3, FEARG_2, NULL,
1809 ret_string, f_win_execute},
1810 {"win_findbuf", 1, 1, FEARG_1, NULL,
1811 ret_list_number, f_win_findbuf},
1812 {"win_getid", 0, 2, FEARG_1, NULL,
1813 ret_number, f_win_getid},
1814 {"win_gettype", 0, 1, FEARG_1, NULL,
1815 ret_string, f_win_gettype},
1816 {"win_gotoid", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001817 ret_number_bool, f_win_gotoid},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001818 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1819 ret_list_number, f_win_id2tabwin},
1820 {"win_id2win", 1, 1, FEARG_1, NULL,
1821 ret_number, f_win_id2win},
1822 {"win_screenpos", 1, 1, FEARG_1, NULL,
1823 ret_list_number, f_win_screenpos},
1824 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001825 ret_number_bool, f_win_splitmove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001826 {"winbufnr", 1, 1, FEARG_1, NULL,
1827 ret_number, f_winbufnr},
1828 {"wincol", 0, 0, 0, NULL,
1829 ret_number, f_wincol},
1830 {"windowsversion", 0, 0, 0, NULL,
1831 ret_string, f_windowsversion},
1832 {"winheight", 1, 1, FEARG_1, NULL,
1833 ret_number, f_winheight},
1834 {"winlayout", 0, 1, FEARG_1, NULL,
1835 ret_list_any, f_winlayout},
1836 {"winline", 0, 0, 0, NULL,
1837 ret_number, f_winline},
1838 {"winnr", 0, 1, FEARG_1, NULL,
1839 ret_number, f_winnr},
1840 {"winrestcmd", 0, 0, 0, NULL,
1841 ret_string, f_winrestcmd},
1842 {"winrestview", 1, 1, FEARG_1, NULL,
1843 ret_void, f_winrestview},
1844 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001845 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001846 {"winwidth", 1, 1, FEARG_1, NULL,
1847 ret_number, f_winwidth},
1848 {"wordcount", 0, 0, 0, NULL,
1849 ret_dict_number, f_wordcount},
1850 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001851 ret_number_bool, f_writefile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001852 {"xor", 2, 2, FEARG_1, NULL,
1853 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001854};
1855
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001856/*
1857 * Function given to ExpandGeneric() to obtain the list of internal
1858 * or user defined function names.
1859 */
1860 char_u *
1861get_function_name(expand_T *xp, int idx)
1862{
1863 static int intidx = -1;
1864 char_u *name;
1865
1866 if (idx == 0)
1867 intidx = -1;
1868 if (intidx < 0)
1869 {
1870 name = get_user_func_name(xp, idx);
1871 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001872 {
1873 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1874 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001875 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001876 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001877 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001878 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001879 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001880 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001881 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001882 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001883 STRCAT(IObuff, ")");
1884 return IObuff;
1885 }
1886
1887 return NULL;
1888}
1889
1890/*
1891 * Function given to ExpandGeneric() to obtain the list of internal or
1892 * user defined variable or function names.
1893 */
1894 char_u *
1895get_expr_name(expand_T *xp, int idx)
1896{
1897 static int intidx = -1;
1898 char_u *name;
1899
1900 if (idx == 0)
1901 intidx = -1;
1902 if (intidx < 0)
1903 {
1904 name = get_function_name(xp, idx);
1905 if (name != NULL)
1906 return name;
1907 }
1908 return get_user_var_name(xp, ++intidx);
1909}
1910
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001911/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001912 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001913 * Return index, or -1 if not found or "implemented" is TRUE and the function
1914 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001915 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001916 static int
1917find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001918{
1919 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001920 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001921 int cmp;
1922 int x;
1923
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001924 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001925
1926 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927 while (first <= last)
1928 {
1929 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001930 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001931 if (cmp < 0)
1932 last = x - 1;
1933 else if (cmp > 0)
1934 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001935 else if (implemented && global_functions[x].f_func == NULL)
1936 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001937 else
1938 return x;
1939 }
1940 return -1;
1941}
1942
Bram Moolenaar15c47602020-03-26 22:16:48 +01001943/*
1944 * Find internal function "name" in table "global_functions".
1945 * Return index, or -1 if not found or the function is not implemented.
1946 */
1947 int
1948find_internal_func(char_u *name)
1949{
1950 return find_internal_func_opt(name, TRUE);
1951}
1952
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001953 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001954has_internal_func(char_u *name)
1955{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001956 return find_internal_func_opt(name, TRUE) >= 0;
1957}
1958
1959 static int
1960has_internal_func_name(char_u *name)
1961{
1962 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001963}
1964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 char *
1966internal_func_name(int idx)
1967{
1968 return global_functions[idx].f_name;
1969}
1970
Bram Moolenaar94738d82020-10-21 14:25:07 +02001971/*
1972 * Check the argument types for builting function "idx".
1973 * Uses the list of types on the type stack: "types".
1974 * Return FAIL and gives an error message when a type is wrong.
1975 */
1976 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001977internal_func_check_arg_types(
1978 type_T **types,
1979 int idx,
1980 int argcount,
1981 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001982{
1983 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1984 int i;
1985
1986 if (argchecks != NULL)
1987 {
1988 argcontext_T context;
1989
1990 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001991 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001992 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001993 for (i = 0; i < argcount; ++i)
1994 if (argchecks[i] != NULL)
1995 {
1996 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001997 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001998 return FAIL;
1999 }
2000 }
2001 return OK;
2002}
2003
Bram Moolenaara1224cb2020-10-22 12:31:49 +02002004/*
2005 * Call the "f_retfunc" function to obtain the return type of function "idx".
2006 * "argtypes" is the list of argument types or NULL when there are no
2007 * arguments.
2008 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002010internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002012 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013}
2014
2015/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002016 * Return TRUE if "idx" is for the map() function.
2017 */
2018 int
2019internal_func_is_map(int idx)
2020{
2021 return global_functions[idx].f_func == f_map;
2022}
2023
2024/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002026 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2027 * first argument, 2 if method base is second argument, etc. 9 if method base
2028 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 */
2030 int
2031check_internal_func(int idx, int argcount)
2032{
2033 int res;
2034 char *name;
2035
2036 if (argcount < global_functions[idx].f_min_argc)
2037 res = FCERR_TOOFEW;
2038 else if (argcount > global_functions[idx].f_max_argc)
2039 res = FCERR_TOOMANY;
2040 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002041 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042
2043 name = internal_func_name(idx);
2044 if (res == FCERR_TOOMANY)
2045 semsg(_(e_toomanyarg), name);
2046 else
2047 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002048 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049}
2050
Bram Moolenaarac92e252019-08-03 21:58:38 +02002051 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002052call_internal_func(
2053 char_u *name,
2054 int argcount,
2055 typval_T *argvars,
2056 typval_T *rettv)
2057{
2058 int i;
2059
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002060 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002061 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002062 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002063 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002064 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002065 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002066 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002067 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002068 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002069 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002070}
2071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 void
2073call_internal_func_by_idx(
2074 int idx,
2075 typval_T *argvars,
2076 typval_T *rettv)
2077{
2078 global_functions[idx].f_func(argvars, rettv);
2079}
2080
Bram Moolenaarac92e252019-08-03 21:58:38 +02002081/*
2082 * Invoke a method for base->method().
2083 */
2084 int
2085call_internal_method(
2086 char_u *name,
2087 int argcount,
2088 typval_T *argvars,
2089 typval_T *rettv,
2090 typval_T *basetv)
2091{
2092 int i;
2093 int fi;
2094 typval_T argv[MAX_FUNC_ARGS + 1];
2095
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002096 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002097 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002098 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002099 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002100 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002101 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002102 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002103 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002104 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002105
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002106 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002107 {
2108 // base value goes last
2109 for (i = 0; i < argcount; ++i)
2110 argv[i] = argvars[i];
2111 argv[argcount] = *basetv;
2112 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002113 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002114 {
2115 // base value goes second
2116 argv[0] = argvars[0];
2117 argv[1] = *basetv;
2118 for (i = 1; i < argcount; ++i)
2119 argv[i + 1] = argvars[i];
2120 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002121 else if (global_functions[fi].f_argtype == FEARG_3)
2122 {
2123 // base value goes third
2124 argv[0] = argvars[0];
2125 argv[1] = argvars[1];
2126 argv[2] = *basetv;
2127 for (i = 2; i < argcount; ++i)
2128 argv[i + 1] = argvars[i];
2129 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002130 else if (global_functions[fi].f_argtype == FEARG_4)
2131 {
2132 // base value goes fourth
2133 argv[0] = argvars[0];
2134 argv[1] = argvars[1];
2135 argv[2] = argvars[2];
2136 argv[3] = *basetv;
2137 for (i = 3; i < argcount; ++i)
2138 argv[i + 1] = argvars[i];
2139 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002140 else
2141 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002142 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002143 argv[0] = *basetv;
2144 for (i = 0; i < argcount; ++i)
2145 argv[i + 1] = argvars[i];
2146 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002147 argv[argcount + 1].v_type = VAR_UNKNOWN;
2148
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002149 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002150 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002151}
2152
2153/*
2154 * Return TRUE for a non-zero Number and a non-empty String.
2155 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002156 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157non_zero_arg(typval_T *argvars)
2158{
2159 return ((argvars[0].v_type == VAR_NUMBER
2160 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002161 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002162 && argvars[0].vval.v_number == VVAL_TRUE)
2163 || (argvars[0].v_type == VAR_STRING
2164 && argvars[0].vval.v_string != NULL
2165 && *argvars[0].vval.v_string != NUL));
2166}
2167
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002168#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002169/*
2170 * Get the float value of "argvars[0]" into "f".
2171 * Returns FAIL when the argument is not a Number or Float.
2172 */
2173 static int
2174get_float_arg(typval_T *argvars, float_T *f)
2175{
2176 if (argvars[0].v_type == VAR_FLOAT)
2177 {
2178 *f = argvars[0].vval.v_float;
2179 return OK;
2180 }
2181 if (argvars[0].v_type == VAR_NUMBER)
2182 {
2183 *f = (float_T)argvars[0].vval.v_number;
2184 return OK;
2185 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002186 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002187 return FAIL;
2188}
2189
2190/*
2191 * "abs(expr)" function
2192 */
2193 static void
2194f_abs(typval_T *argvars, typval_T *rettv)
2195{
2196 if (argvars[0].v_type == VAR_FLOAT)
2197 {
2198 rettv->v_type = VAR_FLOAT;
2199 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
2200 }
2201 else
2202 {
2203 varnumber_T n;
2204 int error = FALSE;
2205
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002206 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002207 if (error)
2208 rettv->vval.v_number = -1;
2209 else if (n > 0)
2210 rettv->vval.v_number = n;
2211 else
2212 rettv->vval.v_number = -n;
2213 }
2214}
2215
2216/*
2217 * "acos()" function
2218 */
2219 static void
2220f_acos(typval_T *argvars, typval_T *rettv)
2221{
2222 float_T f = 0.0;
2223
2224 rettv->v_type = VAR_FLOAT;
2225 if (get_float_arg(argvars, &f) == OK)
2226 rettv->vval.v_float = acos(f);
2227 else
2228 rettv->vval.v_float = 0.0;
2229}
2230#endif
2231
2232/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002233 * "and(expr, expr)" function
2234 */
2235 static void
2236f_and(typval_T *argvars, typval_T *rettv)
2237{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002238 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2239 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002240}
2241
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002242#ifdef FEAT_FLOAT
2243/*
2244 * "asin()" function
2245 */
2246 static void
2247f_asin(typval_T *argvars, typval_T *rettv)
2248{
2249 float_T f = 0.0;
2250
2251 rettv->v_type = VAR_FLOAT;
2252 if (get_float_arg(argvars, &f) == OK)
2253 rettv->vval.v_float = asin(f);
2254 else
2255 rettv->vval.v_float = 0.0;
2256}
2257
2258/*
2259 * "atan()" function
2260 */
2261 static void
2262f_atan(typval_T *argvars, typval_T *rettv)
2263{
2264 float_T f = 0.0;
2265
2266 rettv->v_type = VAR_FLOAT;
2267 if (get_float_arg(argvars, &f) == OK)
2268 rettv->vval.v_float = atan(f);
2269 else
2270 rettv->vval.v_float = 0.0;
2271}
2272
2273/*
2274 * "atan2()" function
2275 */
2276 static void
2277f_atan2(typval_T *argvars, typval_T *rettv)
2278{
2279 float_T fx = 0.0, fy = 0.0;
2280
2281 rettv->v_type = VAR_FLOAT;
2282 if (get_float_arg(argvars, &fx) == OK
2283 && get_float_arg(&argvars[1], &fy) == OK)
2284 rettv->vval.v_float = atan2(fx, fy);
2285 else
2286 rettv->vval.v_float = 0.0;
2287}
2288#endif
2289
2290/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002291 * "balloon_show()" function
2292 */
2293#ifdef FEAT_BEVAL
2294 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002295f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2296{
2297 rettv->v_type = VAR_STRING;
2298 if (balloonEval != NULL)
2299 {
2300 if (balloonEval->msg == NULL)
2301 rettv->vval.v_string = NULL;
2302 else
2303 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2304 }
2305}
2306
2307 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002308f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2309{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002310 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002311 {
2312 if (argvars[0].v_type == VAR_LIST
2313# ifdef FEAT_GUI
2314 && !gui.in_use
2315# endif
2316 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002317 {
2318 list_T *l = argvars[0].vval.v_list;
2319
2320 // empty list removes the balloon
2321 post_balloon(balloonEval, NULL,
2322 l == NULL || l->lv_len == 0 ? NULL : l);
2323 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002324 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002325 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002326 char_u *mesg;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002327
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002328 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2329 return;
2330
2331 mesg = tv_get_string_chk(&argvars[0]);
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002332 if (mesg != NULL)
2333 // empty string removes the balloon
2334 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2335 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002336 }
2337}
2338
Bram Moolenaar669a8282017-11-19 20:13:05 +01002339# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002340 static void
2341f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2342{
2343 if (rettv_list_alloc(rettv) == OK)
2344 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002345 char_u *msg;
Bram Moolenaar246fe032017-11-19 19:56:27 +01002346
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002347 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2348 return;
2349 msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002350 if (msg != NULL)
2351 {
2352 pumitem_T *array;
2353 int size = split_message(msg, &array);
2354 int i;
2355
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002356 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002357 for (i = 1; i < size - 1; ++i)
2358 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002359 while (size > 0)
2360 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002361 vim_free(array);
2362 }
2363 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002364}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002365# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002366#endif
2367
2368/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002369 * Get the buffer from "arg" and give an error and return NULL if it is not
2370 * valid.
2371 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002372 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002373get_buf_arg(typval_T *arg)
2374{
2375 buf_T *buf;
2376
2377 ++emsg_off;
2378 buf = tv_get_buf(arg, FALSE);
2379 --emsg_off;
2380 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002381 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002382 return buf;
2383}
2384
2385/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002386 * "byte2line(byte)" function
2387 */
2388 static void
2389f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2390{
2391#ifndef FEAT_BYTEOFF
2392 rettv->vval.v_number = -1;
2393#else
2394 long boff = 0;
2395
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002396 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002397 if (boff < 0)
2398 rettv->vval.v_number = -1;
2399 else
2400 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2401 (linenr_T)0, &boff);
2402#endif
2403}
2404
2405 static void
2406byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2407{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002408 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002409 char_u *str;
2410 varnumber_T idx;
2411
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002412 str = tv_get_string_chk(&argvars[0]);
2413 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002414 rettv->vval.v_number = -1;
2415 if (str == NULL || idx < 0)
2416 return;
2417
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002418 t = str;
2419 for ( ; idx > 0; idx--)
2420 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002421 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002422 return;
2423 if (enc_utf8 && comp)
2424 t += utf_ptr2len(t);
2425 else
2426 t += (*mb_ptr2len)(t);
2427 }
2428 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002429}
2430
2431/*
2432 * "byteidx()" function
2433 */
2434 static void
2435f_byteidx(typval_T *argvars, typval_T *rettv)
2436{
2437 byteidx(argvars, rettv, FALSE);
2438}
2439
2440/*
2441 * "byteidxcomp()" function
2442 */
2443 static void
2444f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2445{
2446 byteidx(argvars, rettv, TRUE);
2447}
2448
2449/*
2450 * "call(func, arglist [, dict])" function
2451 */
2452 static void
2453f_call(typval_T *argvars, typval_T *rettv)
2454{
2455 char_u *func;
2456 partial_T *partial = NULL;
2457 dict_T *selfdict = NULL;
2458
2459 if (argvars[1].v_type != VAR_LIST)
2460 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002461 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002462 return;
2463 }
2464 if (argvars[1].vval.v_list == NULL)
2465 return;
2466
2467 if (argvars[0].v_type == VAR_FUNC)
2468 func = argvars[0].vval.v_string;
2469 else if (argvars[0].v_type == VAR_PARTIAL)
2470 {
2471 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002472 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002473 }
2474 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002475 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002476 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002477 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002478
2479 if (argvars[2].v_type != VAR_UNKNOWN)
2480 {
2481 if (argvars[2].v_type != VAR_DICT)
2482 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002483 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002484 return;
2485 }
2486 selfdict = argvars[2].vval.v_dict;
2487 }
2488
2489 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2490}
2491
2492#ifdef FEAT_FLOAT
2493/*
2494 * "ceil({float})" function
2495 */
2496 static void
2497f_ceil(typval_T *argvars, typval_T *rettv)
2498{
2499 float_T f = 0.0;
2500
2501 rettv->v_type = VAR_FLOAT;
2502 if (get_float_arg(argvars, &f) == OK)
2503 rettv->vval.v_float = ceil(f);
2504 else
2505 rettv->vval.v_float = 0.0;
2506}
2507#endif
2508
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002509/*
2510 * "changenr()" function
2511 */
2512 static void
2513f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2514{
2515 rettv->vval.v_number = curbuf->b_u_seq_cur;
2516}
2517
2518/*
2519 * "char2nr(string)" function
2520 */
2521 static void
2522f_char2nr(typval_T *argvars, typval_T *rettv)
2523{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002524 if (has_mbyte)
2525 {
2526 int utf8 = 0;
2527
2528 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002529 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002530
2531 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002532 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002533 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002534 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002535 }
2536 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002537 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002538}
2539
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002540/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002541 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2542 * returns the character index of the column. Otherwise, returns the byte index
2543 * of the column.
2544 */
2545 static void
2546get_col(typval_T *argvars, typval_T *rettv, int charcol)
2547{
2548 colnr_T col = 0;
2549 pos_T *fp;
2550 int fnum = curbuf->b_fnum;
2551
2552 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2553 if (fp != NULL && fnum == curbuf->b_fnum)
2554 {
2555 if (fp->col == MAXCOL)
2556 {
2557 // '> can be MAXCOL, get the length of the line then
2558 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2559 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2560 else
2561 col = MAXCOL;
2562 }
2563 else
2564 {
2565 col = fp->col + 1;
2566 // col(".") when the cursor is on the NUL at the end of the line
2567 // because of "coladd" can be seen as an extra column.
2568 if (virtual_active() && fp == &curwin->w_cursor)
2569 {
2570 char_u *p = ml_get_cursor();
2571
2572 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2573 curwin->w_virtcol - curwin->w_cursor.coladd))
2574 {
2575 int l;
2576
2577 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2578 col += l;
2579 }
2580 }
2581 }
2582 }
2583 rettv->vval.v_number = col;
2584}
2585
2586/*
2587 * "charcol()" function
2588 */
2589 static void
2590f_charcol(typval_T *argvars, typval_T *rettv)
2591{
2592 get_col(argvars, rettv, TRUE);
2593}
2594
2595/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002596 * "charidx()" function
2597 */
2598 static void
2599f_charidx(typval_T *argvars, typval_T *rettv)
2600{
2601 char_u *str;
2602 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002603 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002604 char_u *p;
2605 int len;
2606 int (*ptr2len)(char_u *);
2607
2608 rettv->vval.v_number = -1;
2609
2610 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2611 || (argvars[2].v_type != VAR_UNKNOWN
2612 && argvars[2].v_type != VAR_NUMBER))
2613 {
2614 emsg(_(e_invarg));
2615 return;
2616 }
2617
2618 str = tv_get_string_chk(&argvars[0]);
2619 idx = tv_get_number_chk(&argvars[1], NULL);
2620 if (str == NULL || idx < 0)
2621 return;
2622
2623 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002624 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002625 if (countcc < 0 || countcc > 1)
2626 {
2627 semsg(_(e_using_number_as_bool_nr), countcc);
2628 return;
2629 }
2630
2631 if (enc_utf8 && countcc)
2632 ptr2len = utf_ptr2len;
2633 else
2634 ptr2len = mb_ptr2len;
2635
2636 for (p = str, len = 0; p <= str + idx; len++)
2637 {
2638 if (*p == NUL)
2639 return;
2640 p += ptr2len(p);
2641 }
2642
2643 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2644}
2645
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002646 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002647get_optional_window(typval_T *argvars, int idx)
2648{
2649 win_T *win = curwin;
2650
2651 if (argvars[idx].v_type != VAR_UNKNOWN)
2652 {
2653 win = find_win_by_nr_or_id(&argvars[idx]);
2654 if (win == NULL)
2655 {
2656 emsg(_(e_invalwindow));
2657 return NULL;
2658 }
2659 }
2660 return win;
2661}
2662
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002663/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002664 * "col(string)" function
2665 */
2666 static void
2667f_col(typval_T *argvars, typval_T *rettv)
2668{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002669 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002670}
2671
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002672/*
2673 * "confirm(message, buttons[, default [, type]])" function
2674 */
2675 static void
2676f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2677{
2678#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2679 char_u *message;
2680 char_u *buttons = NULL;
2681 char_u buf[NUMBUFLEN];
2682 char_u buf2[NUMBUFLEN];
2683 int def = 1;
2684 int type = VIM_GENERIC;
2685 char_u *typestr;
2686 int error = FALSE;
2687
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002688 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002689 if (message == NULL)
2690 error = TRUE;
2691 if (argvars[1].v_type != VAR_UNKNOWN)
2692 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002693 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002694 if (buttons == NULL)
2695 error = TRUE;
2696 if (argvars[2].v_type != VAR_UNKNOWN)
2697 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002698 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002699 if (argvars[3].v_type != VAR_UNKNOWN)
2700 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002701 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002702 if (typestr == NULL)
2703 error = TRUE;
2704 else
2705 {
2706 switch (TOUPPER_ASC(*typestr))
2707 {
2708 case 'E': type = VIM_ERROR; break;
2709 case 'Q': type = VIM_QUESTION; break;
2710 case 'I': type = VIM_INFO; break;
2711 case 'W': type = VIM_WARNING; break;
2712 case 'G': type = VIM_GENERIC; break;
2713 }
2714 }
2715 }
2716 }
2717 }
2718
2719 if (buttons == NULL || *buttons == NUL)
2720 buttons = (char_u *)_("&Ok");
2721
2722 if (!error)
2723 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2724 def, NULL, FALSE);
2725#endif
2726}
2727
2728/*
2729 * "copy()" function
2730 */
2731 static void
2732f_copy(typval_T *argvars, typval_T *rettv)
2733{
2734 item_copy(&argvars[0], rettv, FALSE, 0);
2735}
2736
2737#ifdef FEAT_FLOAT
2738/*
2739 * "cos()" function
2740 */
2741 static void
2742f_cos(typval_T *argvars, typval_T *rettv)
2743{
2744 float_T f = 0.0;
2745
2746 rettv->v_type = VAR_FLOAT;
2747 if (get_float_arg(argvars, &f) == OK)
2748 rettv->vval.v_float = cos(f);
2749 else
2750 rettv->vval.v_float = 0.0;
2751}
2752
2753/*
2754 * "cosh()" function
2755 */
2756 static void
2757f_cosh(typval_T *argvars, typval_T *rettv)
2758{
2759 float_T f = 0.0;
2760
2761 rettv->v_type = VAR_FLOAT;
2762 if (get_float_arg(argvars, &f) == OK)
2763 rettv->vval.v_float = cosh(f);
2764 else
2765 rettv->vval.v_float = 0.0;
2766}
2767#endif
2768
2769/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002770 * Set the cursor position.
2771 * If 'charcol' is TRUE, then use the column number as a character offet.
2772 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002773 */
2774 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002775set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002776{
2777 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002778 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002779 int set_curswant = TRUE;
2780
2781 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002782 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002783 {
2784 pos_T pos;
2785 colnr_T curswant = -1;
2786
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002787 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002788 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002789 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002790 return;
2791 }
2792 line = pos.lnum;
2793 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002794 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002795 if (curswant >= 0)
2796 {
2797 curwin->w_curswant = curswant - 1;
2798 set_curswant = FALSE;
2799 }
2800 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002801 else if ((argvars[0].v_type == VAR_NUMBER ||
2802 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002803 && (argvars[1].v_type == VAR_NUMBER ||
2804 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002805 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002806 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002807 if (line < 0)
2808 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002809 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002810 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002811 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002812 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002813 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002814 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002815 else
2816 {
2817 emsg(_(e_invarg));
2818 return;
2819 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002820 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002821 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002822 if (line > 0)
2823 curwin->w_cursor.lnum = line;
2824 if (col > 0)
2825 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002826 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002827
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002828 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002829 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002830 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002831 if (has_mbyte)
2832 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002833
2834 curwin->w_set_curswant = set_curswant;
2835 rettv->vval.v_number = 0;
2836}
2837
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002838/*
2839 * "cursor(lnum, col)" function, or
2840 * "cursor(list)"
2841 *
2842 * Moves the cursor to the specified line and column.
2843 * Returns 0 when the position could be set, -1 otherwise.
2844 */
2845 static void
2846f_cursor(typval_T *argvars, typval_T *rettv)
2847{
2848 set_cursorpos(argvars, rettv, FALSE);
2849}
2850
Bram Moolenaar4f974752019-02-17 17:44:42 +01002851#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002852/*
2853 * "debugbreak()" function
2854 */
2855 static void
2856f_debugbreak(typval_T *argvars, typval_T *rettv)
2857{
2858 int pid;
2859
2860 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002861 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002862 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002863 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002864 else
2865 {
2866 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2867
2868 if (hProcess != NULL)
2869 {
2870 DebugBreakProcess(hProcess);
2871 CloseHandle(hProcess);
2872 rettv->vval.v_number = OK;
2873 }
2874 }
2875}
2876#endif
2877
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002878/*
2879 * "deepcopy()" function
2880 */
2881 static void
2882f_deepcopy(typval_T *argvars, typval_T *rettv)
2883{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002884 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002885 int copyID;
2886
2887 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002888 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002889 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002890 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002891 else
2892 {
2893 copyID = get_copyID();
2894 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2895 }
2896}
2897
2898/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002899 * "did_filetype()" function
2900 */
2901 static void
2902f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2903{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002904 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002905}
2906
2907/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002908 * "echoraw({expr})" function
2909 */
2910 static void
2911f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2912{
2913 char_u *str = tv_get_string_chk(&argvars[0]);
2914
2915 if (str != NULL && *str != NUL)
2916 {
2917 out_str(str);
2918 out_flush();
2919 }
2920}
2921
2922/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002923 * "empty({expr})" function
2924 */
2925 static void
2926f_empty(typval_T *argvars, typval_T *rettv)
2927{
2928 int n = FALSE;
2929
2930 switch (argvars[0].v_type)
2931 {
2932 case VAR_STRING:
2933 case VAR_FUNC:
2934 n = argvars[0].vval.v_string == NULL
2935 || *argvars[0].vval.v_string == NUL;
2936 break;
2937 case VAR_PARTIAL:
2938 n = FALSE;
2939 break;
2940 case VAR_NUMBER:
2941 n = argvars[0].vval.v_number == 0;
2942 break;
2943 case VAR_FLOAT:
2944#ifdef FEAT_FLOAT
2945 n = argvars[0].vval.v_float == 0.0;
2946 break;
2947#endif
2948 case VAR_LIST:
2949 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002950 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002951 break;
2952 case VAR_DICT:
2953 n = argvars[0].vval.v_dict == NULL
2954 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2955 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002956 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002957 case VAR_SPECIAL:
2958 n = argvars[0].vval.v_number != VVAL_TRUE;
2959 break;
2960
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002961 case VAR_BLOB:
2962 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002963 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2964 break;
2965
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002966 case VAR_JOB:
2967#ifdef FEAT_JOB_CHANNEL
2968 n = argvars[0].vval.v_job == NULL
2969 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2970 break;
2971#endif
2972 case VAR_CHANNEL:
2973#ifdef FEAT_JOB_CHANNEL
2974 n = argvars[0].vval.v_channel == NULL
2975 || !channel_is_open(argvars[0].vval.v_channel);
2976 break;
2977#endif
2978 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002979 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002981 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002982 n = TRUE;
2983 break;
2984 }
2985
2986 rettv->vval.v_number = n;
2987}
2988
2989/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002990 * "environ()" function
2991 */
2992 static void
2993f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2994{
2995#if !defined(AMIGA)
2996 int i = 0;
2997 char_u *entry, *value;
2998# ifdef MSWIN
2999 extern wchar_t **_wenviron;
3000# else
3001 extern char **environ;
3002# endif
3003
3004 if (rettv_dict_alloc(rettv) != OK)
3005 return;
3006
3007# ifdef MSWIN
3008 if (*_wenviron == NULL)
3009 return;
3010# else
3011 if (*environ == NULL)
3012 return;
3013# endif
3014
3015 for (i = 0; ; ++i)
3016 {
3017# ifdef MSWIN
3018 short_u *p;
3019
3020 if ((p = (short_u *)_wenviron[i]) == NULL)
3021 return;
3022 entry = utf16_to_enc(p, NULL);
3023# else
3024 if ((entry = (char_u *)environ[i]) == NULL)
3025 return;
3026 entry = vim_strsave(entry);
3027# endif
3028 if (entry == NULL) // out of memory
3029 return;
3030 if ((value = vim_strchr(entry, '=')) == NULL)
3031 {
3032 vim_free(entry);
3033 continue;
3034 }
3035 *value++ = NUL;
3036 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3037 vim_free(entry);
3038 }
3039#endif
3040}
3041
3042/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003043 * "escape({string}, {chars})" function
3044 */
3045 static void
3046f_escape(typval_T *argvars, typval_T *rettv)
3047{
3048 char_u buf[NUMBUFLEN];
3049
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003050 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3051 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003052 rettv->v_type = VAR_STRING;
3053}
3054
3055/*
3056 * "eval()" function
3057 */
3058 static void
3059f_eval(typval_T *argvars, typval_T *rettv)
3060{
3061 char_u *s, *p;
3062
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003063 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003064 if (s != NULL)
3065 s = skipwhite(s);
3066
3067 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003068 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003069 {
3070 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003071 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003072 need_clr_eos = FALSE;
3073 rettv->v_type = VAR_NUMBER;
3074 rettv->vval.v_number = 0;
3075 }
3076 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003077 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003078}
3079
3080/*
3081 * "eventhandler()" function
3082 */
3083 static void
3084f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3085{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003086 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003087}
3088
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003089static garray_T redir_execute_ga;
3090
3091/*
3092 * Append "value[value_len]" to the execute() output.
3093 */
3094 void
3095execute_redir_str(char_u *value, int value_len)
3096{
3097 int len;
3098
3099 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003100 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003101 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003102 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003103 if (ga_grow(&redir_execute_ga, len) == OK)
3104 {
3105 mch_memmove((char *)redir_execute_ga.ga_data
3106 + redir_execute_ga.ga_len, value, len);
3107 redir_execute_ga.ga_len += len;
3108 }
3109}
3110
3111/*
3112 * Get next line from a list.
3113 * Called by do_cmdline() to get the next line.
3114 * Returns allocated string, or NULL for end of function.
3115 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003116 static char_u *
3117get_list_line(
3118 int c UNUSED,
3119 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003120 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02003121 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003122{
3123 listitem_T **p = (listitem_T **)cookie;
3124 listitem_T *item = *p;
3125 char_u buf[NUMBUFLEN];
3126 char_u *s;
3127
3128 if (item == NULL)
3129 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003130 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003131 *p = item->li_next;
3132 return s == NULL ? NULL : vim_strsave(s);
3133}
3134
3135/*
3136 * "execute()" function
3137 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02003138 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003139execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003140{
3141 char_u *cmd = NULL;
3142 list_T *list = NULL;
3143 int save_msg_silent = msg_silent;
3144 int save_emsg_silent = emsg_silent;
3145 int save_emsg_noredir = emsg_noredir;
3146 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003147 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003148 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003149 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003150 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003151
3152 rettv->vval.v_string = NULL;
3153 rettv->v_type = VAR_STRING;
3154
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003155 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003156 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003157 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003158 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003159 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003160 return;
3161 ++list->lv_refcount;
3162 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003163 else if (argvars[arg_off].v_type == VAR_JOB
3164 || argvars[arg_off].v_type == VAR_CHANNEL)
3165 {
3166 emsg(_(e_inval_string));
3167 return;
3168 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003169 else
3170 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003171 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003172 if (cmd == NULL)
3173 return;
3174 }
3175
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003176 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003177 {
3178 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003179 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003180
3181 if (s == NULL)
3182 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003183 if (*s == NUL)
3184 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003185 if (STRNCMP(s, "silent", 6) == 0)
3186 ++msg_silent;
3187 if (STRCMP(s, "silent!") == 0)
3188 {
3189 emsg_silent = TRUE;
3190 emsg_noredir = TRUE;
3191 }
3192 }
3193 else
3194 ++msg_silent;
3195
3196 if (redir_execute)
3197 save_ga = redir_execute_ga;
3198 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3199 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003200 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003201 if (!echo_output)
3202 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003203
3204 if (cmd != NULL)
3205 do_cmdline_cmd(cmd);
3206 else
3207 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003208 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003209
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003210 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003211 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003212 do_cmdline(NULL, get_list_line, (void *)&item,
3213 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3214 --list->lv_refcount;
3215 }
3216
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003217 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003218 if (ga_grow(&redir_execute_ga, 1) == OK)
3219 {
3220 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3221 rettv->vval.v_string = redir_execute_ga.ga_data;
3222 }
3223 else
3224 {
3225 ga_clear(&redir_execute_ga);
3226 rettv->vval.v_string = NULL;
3227 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003228 msg_silent = save_msg_silent;
3229 emsg_silent = save_emsg_silent;
3230 emsg_noredir = save_emsg_noredir;
3231
3232 redir_execute = save_redir_execute;
3233 if (redir_execute)
3234 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003235 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003236
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003237 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003238 if (echo_output)
3239 // When not working silently: put it in column zero. A following
3240 // "echon" will overwrite the message, unavoidably.
3241 msg_col = 0;
3242 else
3243 // When working silently: Put it back where it was, since nothing
3244 // should have been written.
3245 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003246}
3247
3248/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003249 * "execute()" function
3250 */
3251 static void
3252f_execute(typval_T *argvars, typval_T *rettv)
3253{
3254 execute_common(argvars, rettv, 0);
3255}
3256
3257/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003258 * "exists()" function
3259 */
3260 static void
3261f_exists(typval_T *argvars, typval_T *rettv)
3262{
3263 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003264 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003265
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003266 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003267 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003268 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003269 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003270 if (mch_getenv(p + 1) != NULL)
3271 n = TRUE;
3272 else
3273 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003274 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003275 p = expand_env_save(p);
3276 if (p != NULL && *p != '$')
3277 n = TRUE;
3278 vim_free(p);
3279 }
3280 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003281 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003282 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003283 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003284 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003285 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003286 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003287 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003288 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003289 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003290 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003291 else if (*p == '?') // internal function only
3292 {
3293 n = has_internal_func_name(p + 1);
3294 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003295 else if (*p == ':')
3296 {
3297 n = cmd_exists(p + 1);
3298 }
3299 else if (*p == '#')
3300 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003301 if (p[1] == '#')
3302 n = autocmd_supported(p + 2);
3303 else
3304 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003305 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003306 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003307 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003308 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003309 }
3310
3311 rettv->vval.v_number = n;
3312}
3313
3314#ifdef FEAT_FLOAT
3315/*
3316 * "exp()" function
3317 */
3318 static void
3319f_exp(typval_T *argvars, typval_T *rettv)
3320{
3321 float_T f = 0.0;
3322
3323 rettv->v_type = VAR_FLOAT;
3324 if (get_float_arg(argvars, &f) == OK)
3325 rettv->vval.v_float = exp(f);
3326 else
3327 rettv->vval.v_float = 0.0;
3328}
3329#endif
3330
3331/*
3332 * "expand()" function
3333 */
3334 static void
3335f_expand(typval_T *argvars, typval_T *rettv)
3336{
3337 char_u *s;
3338 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003339 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003340 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3341 expand_T xpc;
3342 int error = FALSE;
3343 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003344#ifdef BACKSLASH_IN_FILENAME
3345 char_u *p_csl_save = p_csl;
3346
3347 // avoid using 'completeslash' here
3348 p_csl = empty_option;
3349#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003350
3351 rettv->v_type = VAR_STRING;
3352 if (argvars[1].v_type != VAR_UNKNOWN
3353 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003354 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003355 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003356 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003357
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003358 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003359 if (*s == '%' || *s == '#' || *s == '<')
3360 {
3361 ++emsg_off;
3362 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3363 --emsg_off;
3364 if (rettv->v_type == VAR_LIST)
3365 {
3366 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3367 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003368 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003369 }
3370 else
3371 rettv->vval.v_string = result;
3372 }
3373 else
3374 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003375 // When the optional second argument is non-zero, don't remove matches
3376 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003377 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003378 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003379 options |= WILD_KEEP_ALL;
3380 if (!error)
3381 {
3382 ExpandInit(&xpc);
3383 xpc.xp_context = EXPAND_FILES;
3384 if (p_wic)
3385 options += WILD_ICASE;
3386 if (rettv->v_type == VAR_STRING)
3387 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3388 options, WILD_ALL);
3389 else if (rettv_list_alloc(rettv) != FAIL)
3390 {
3391 int i;
3392
3393 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3394 for (i = 0; i < xpc.xp_numfiles; i++)
3395 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3396 ExpandCleanup(&xpc);
3397 }
3398 }
3399 else
3400 rettv->vval.v_string = NULL;
3401 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003402#ifdef BACKSLASH_IN_FILENAME
3403 p_csl = p_csl_save;
3404#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003405}
3406
3407/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003408 * "expandcmd()" function
3409 * Expand all the special characters in a command string.
3410 */
3411 static void
3412f_expandcmd(typval_T *argvars, typval_T *rettv)
3413{
3414 exarg_T eap;
3415 char_u *cmdstr;
3416 char *errormsg = NULL;
3417
3418 rettv->v_type = VAR_STRING;
3419 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3420
3421 memset(&eap, 0, sizeof(eap));
3422 eap.cmd = cmdstr;
3423 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003424 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003425 eap.usefilter = FALSE;
3426 eap.nextcmd = NULL;
3427 eap.cmdidx = CMD_USER;
3428
3429 expand_filename(&eap, &cmdstr, &errormsg);
3430 if (errormsg != NULL && *errormsg != NUL)
3431 emsg(errormsg);
3432
3433 rettv->vval.v_string = cmdstr;
3434}
3435
3436/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003437 * "feedkeys()" function
3438 */
3439 static void
3440f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3441{
3442 int remap = TRUE;
3443 int insert = FALSE;
3444 char_u *keys, *flags;
3445 char_u nbuf[NUMBUFLEN];
3446 int typed = FALSE;
3447 int execute = FALSE;
3448 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003449 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003450 char_u *keys_esc;
3451
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003452 // This is not allowed in the sandbox. If the commands would still be
3453 // executed in the sandbox it would be OK, but it probably happens later,
3454 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003455 if (check_secure())
3456 return;
3457
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003458 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003459
3460 if (argvars[1].v_type != VAR_UNKNOWN)
3461 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003462 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003463 for ( ; *flags != NUL; ++flags)
3464 {
3465 switch (*flags)
3466 {
3467 case 'n': remap = FALSE; break;
3468 case 'm': remap = TRUE; break;
3469 case 't': typed = TRUE; break;
3470 case 'i': insert = TRUE; break;
3471 case 'x': execute = TRUE; break;
3472 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003473 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003474 }
3475 }
3476 }
3477
3478 if (*keys != NUL || execute)
3479 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003480 // Need to escape K_SPECIAL and CSI before putting the string in the
3481 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003482 keys_esc = vim_strsave_escape_csi(keys);
3483 if (keys_esc != NULL)
3484 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003485 if (lowlevel)
3486 {
3487#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003488 int idx;
3489 int len = (int)STRLEN(keys);
3490
3491 for (idx = 0; idx < len; ++idx)
3492 {
3493 // if a CTRL-C was typed, set got_int, similar to what
3494 // happens in fill_input_buf()
3495 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3496 got_int = TRUE;
3497 add_to_input_buf(keys + idx, 1);
3498 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003499#else
3500 emsg(_("E980: lowlevel input not supported"));
3501#endif
3502 }
3503 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003504 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003505 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003506 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003507 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003508#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003509 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003510#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003511 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003512 typebuf_was_filled = TRUE;
3513 }
3514 vim_free(keys_esc);
3515
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003516 if (execute)
3517 {
3518 int save_msg_scroll = msg_scroll;
3519
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003520 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003521 msg_scroll = FALSE;
3522
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003523 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003524 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003525 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003526 ++in_feedkeys;
3527 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003528 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003529 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003530 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003531 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003532 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003533 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003534
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003535 msg_scroll |= save_msg_scroll;
3536 }
3537 }
3538 }
3539}
3540
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003541#ifdef FEAT_FLOAT
3542/*
3543 * "float2nr({float})" function
3544 */
3545 static void
3546f_float2nr(typval_T *argvars, typval_T *rettv)
3547{
3548 float_T f = 0.0;
3549
3550 if (get_float_arg(argvars, &f) == OK)
3551 {
Bram Moolenaar37184272020-05-23 19:30:05 +02003552 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003553 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02003554 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003555 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003556 else
3557 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003558 }
3559}
3560
3561/*
3562 * "floor({float})" function
3563 */
3564 static void
3565f_floor(typval_T *argvars, typval_T *rettv)
3566{
3567 float_T f = 0.0;
3568
3569 rettv->v_type = VAR_FLOAT;
3570 if (get_float_arg(argvars, &f) == OK)
3571 rettv->vval.v_float = floor(f);
3572 else
3573 rettv->vval.v_float = 0.0;
3574}
3575
3576/*
3577 * "fmod()" function
3578 */
3579 static void
3580f_fmod(typval_T *argvars, typval_T *rettv)
3581{
3582 float_T fx = 0.0, fy = 0.0;
3583
3584 rettv->v_type = VAR_FLOAT;
3585 if (get_float_arg(argvars, &fx) == OK
3586 && get_float_arg(&argvars[1], &fy) == OK)
3587 rettv->vval.v_float = fmod(fx, fy);
3588 else
3589 rettv->vval.v_float = 0.0;
3590}
3591#endif
3592
3593/*
3594 * "fnameescape({string})" function
3595 */
3596 static void
3597f_fnameescape(typval_T *argvars, typval_T *rettv)
3598{
3599 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003600 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003601 rettv->v_type = VAR_STRING;
3602}
3603
3604/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003605 * "foreground()" function
3606 */
3607 static void
3608f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3609{
3610#ifdef FEAT_GUI
3611 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003612 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003613 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003614 return;
3615 }
3616#endif
3617#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003618 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003619#endif
3620}
3621
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003622 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003623common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003624{
3625 char_u *s;
3626 char_u *name;
3627 int use_string = FALSE;
3628 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003629 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003630 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003631
3632 if (argvars[0].v_type == VAR_FUNC)
3633 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003634 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003635 s = argvars[0].vval.v_string;
3636 }
3637 else if (argvars[0].v_type == VAR_PARTIAL
3638 && argvars[0].vval.v_partial != NULL)
3639 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003640 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003641 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003642 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003643 }
3644 else
3645 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003646 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003647 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003648 use_string = TRUE;
3649 }
3650
Bram Moolenaar843b8842016-08-21 14:36:15 +02003651 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003652 {
3653 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003654 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003655 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3656 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003657 if (*name != NUL)
3658 s = NULL;
3659 }
3660
Bram Moolenaar843b8842016-08-21 14:36:15 +02003661 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3662 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003663 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003664 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003665 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003666 ? find_func(trans_name, is_global, NULL) == NULL
3667 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003668 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003669 else
3670 {
3671 int dict_idx = 0;
3672 int arg_idx = 0;
3673 list_T *list = NULL;
3674
3675 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3676 {
3677 char sid_buf[25];
3678 int off = *s == 's' ? 2 : 5;
3679
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003680 // Expand s: and <SID> into <SNR>nr_, so that the function can
3681 // also be called from another script. Using trans_function_name()
3682 // would also work, but some plugins depend on the name being
3683 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003684 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003685 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003686 if (name != NULL)
3687 {
3688 STRCPY(name, sid_buf);
3689 STRCAT(name, s + off);
3690 }
3691 }
3692 else
3693 name = vim_strsave(s);
3694
3695 if (argvars[1].v_type != VAR_UNKNOWN)
3696 {
3697 if (argvars[2].v_type != VAR_UNKNOWN)
3698 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003699 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003700 arg_idx = 1;
3701 dict_idx = 2;
3702 }
3703 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003704 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003705 dict_idx = 1;
3706 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003707 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003708 arg_idx = 1;
3709 if (dict_idx > 0)
3710 {
3711 if (argvars[dict_idx].v_type != VAR_DICT)
3712 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003713 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003714 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003715 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003716 }
3717 if (argvars[dict_idx].vval.v_dict == NULL)
3718 dict_idx = 0;
3719 }
3720 if (arg_idx > 0)
3721 {
3722 if (argvars[arg_idx].v_type != VAR_LIST)
3723 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003724 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003725 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003726 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003727 }
3728 list = argvars[arg_idx].vval.v_list;
3729 if (list == NULL || list->lv_len == 0)
3730 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003731 else if (list->lv_len > MAX_FUNC_ARGS)
3732 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003733 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003734 vim_free(name);
3735 goto theend;
3736 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003737 }
3738 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003739 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003740 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003741 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003742
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003743 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003744 if (pt == NULL)
3745 vim_free(name);
3746 else
3747 {
3748 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3749 {
3750 listitem_T *li;
3751 int i = 0;
3752 int arg_len = 0;
3753 int lv_len = 0;
3754
3755 if (arg_pt != NULL)
3756 arg_len = arg_pt->pt_argc;
3757 if (list != NULL)
3758 lv_len = list->lv_len;
3759 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003760 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003761 if (pt->pt_argv == NULL)
3762 {
3763 vim_free(pt);
3764 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003765 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003766 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003767 for (i = 0; i < arg_len; i++)
3768 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3769 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003770 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003771 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003772 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003773 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003774 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003775 }
3776
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003777 // For "function(dict.func, [], dict)" and "func" is a partial
3778 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003779 if (dict_idx > 0)
3780 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003781 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003782 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3783 ++pt->pt_dict->dv_refcount;
3784 }
3785 else if (arg_pt != NULL)
3786 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003787 // If the dict was bound automatically the result is also
3788 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003789 pt->pt_dict = arg_pt->pt_dict;
3790 pt->pt_auto = arg_pt->pt_auto;
3791 if (pt->pt_dict != NULL)
3792 ++pt->pt_dict->dv_refcount;
3793 }
3794
3795 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003796 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3797 {
3798 pt->pt_func = arg_pt->pt_func;
3799 func_ptr_ref(pt->pt_func);
3800 vim_free(name);
3801 }
3802 else if (is_funcref)
3803 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003804 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003805 func_ptr_ref(pt->pt_func);
3806 vim_free(name);
3807 }
3808 else
3809 {
3810 pt->pt_name = name;
3811 func_ref(name);
3812 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003813 }
3814 rettv->v_type = VAR_PARTIAL;
3815 rettv->vval.v_partial = pt;
3816 }
3817 else
3818 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003819 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003820 rettv->v_type = VAR_FUNC;
3821 rettv->vval.v_string = name;
3822 func_ref(name);
3823 }
3824 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003825theend:
3826 vim_free(trans_name);
3827}
3828
3829/*
3830 * "funcref()" function
3831 */
3832 static void
3833f_funcref(typval_T *argvars, typval_T *rettv)
3834{
3835 common_function(argvars, rettv, TRUE);
3836}
3837
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003838 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003839ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003840{
3841 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3842 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003843 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003844}
3845
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003846/*
3847 * "function()" function
3848 */
3849 static void
3850f_function(typval_T *argvars, typval_T *rettv)
3851{
3852 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003853}
3854
3855/*
3856 * "garbagecollect()" function
3857 */
3858 static void
3859f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3860{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003861 // This is postponed until we are back at the toplevel, because we may be
3862 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003863 want_garbage_collect = TRUE;
3864
Bram Moolenaar2df47312020-09-05 17:30:44 +02003865 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003866 garbage_collect_at_exit = TRUE;
3867}
3868
3869/*
3870 * "get()" function
3871 */
3872 static void
3873f_get(typval_T *argvars, typval_T *rettv)
3874{
3875 listitem_T *li;
3876 list_T *l;
3877 dictitem_T *di;
3878 dict_T *d;
3879 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003880 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003881
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003882 if (argvars[0].v_type == VAR_BLOB)
3883 {
3884 int error = FALSE;
3885 int idx = tv_get_number_chk(&argvars[1], &error);
3886
3887 if (!error)
3888 {
3889 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003890 if (idx < 0)
3891 idx = blob_len(argvars[0].vval.v_blob) + idx;
3892 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3893 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003894 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003895 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003896 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003897 tv = rettv;
3898 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003899 }
3900 }
3901 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003902 {
3903 if ((l = argvars[0].vval.v_list) != NULL)
3904 {
3905 int error = FALSE;
3906
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003907 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003908 if (!error && li != NULL)
3909 tv = &li->li_tv;
3910 }
3911 }
3912 else if (argvars[0].v_type == VAR_DICT)
3913 {
3914 if ((d = argvars[0].vval.v_dict) != NULL)
3915 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003916 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003917 if (di != NULL)
3918 tv = &di->di_tv;
3919 }
3920 }
3921 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3922 {
3923 partial_T *pt;
3924 partial_T fref_pt;
3925
3926 if (argvars[0].v_type == VAR_PARTIAL)
3927 pt = argvars[0].vval.v_partial;
3928 else
3929 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003930 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003931 fref_pt.pt_name = argvars[0].vval.v_string;
3932 pt = &fref_pt;
3933 }
3934
3935 if (pt != NULL)
3936 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003937 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003938 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003939
3940 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3941 {
3942 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003943 n = partial_name(pt);
3944 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003945 rettv->vval.v_string = NULL;
3946 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003947 {
3948 rettv->vval.v_string = vim_strsave(n);
3949 if (rettv->v_type == VAR_FUNC)
3950 func_ref(rettv->vval.v_string);
3951 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003952 }
3953 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003954 {
3955 what_is_dict = TRUE;
3956 if (pt->pt_dict != NULL)
3957 rettv_dict_set(rettv, pt->pt_dict);
3958 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003959 else if (STRCMP(what, "args") == 0)
3960 {
3961 rettv->v_type = VAR_LIST;
3962 if (rettv_list_alloc(rettv) == OK)
3963 {
3964 int i;
3965
3966 for (i = 0; i < pt->pt_argc; ++i)
3967 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3968 }
3969 }
3970 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003971 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003972
3973 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3974 // third argument
3975 if (!what_is_dict)
3976 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003977 }
3978 }
3979 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003980 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003981
3982 if (tv == NULL)
3983 {
3984 if (argvars[2].v_type != VAR_UNKNOWN)
3985 copy_tv(&argvars[2], rettv);
3986 }
3987 else
3988 copy_tv(tv, rettv);
3989}
3990
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003991/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003992 * "getchangelist()" function
3993 */
3994 static void
3995f_getchangelist(typval_T *argvars, typval_T *rettv)
3996{
3997#ifdef FEAT_JUMPLIST
3998 buf_T *buf;
3999 int i;
4000 list_T *l;
4001 dict_T *d;
4002#endif
4003
4004 if (rettv_list_alloc(rettv) != OK)
4005 return;
4006
4007#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02004008 if (argvars[0].v_type == VAR_UNKNOWN)
4009 buf = curbuf;
4010 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02004011 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004012 if (buf == NULL)
4013 return;
4014
4015 l = list_alloc();
4016 if (l == NULL)
4017 return;
4018
4019 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4020 return;
4021 /*
4022 * The current window change list index tracks only the position in the
4023 * current buffer change list. For other buffers, use the change list
4024 * length as the current index.
4025 */
4026 list_append_number(rettv->vval.v_list,
4027 (varnumber_T)((buf == curwin->w_buffer)
4028 ? curwin->w_changelistidx : buf->b_changelistlen));
4029
4030 for (i = 0; i < buf->b_changelistlen; ++i)
4031 {
4032 if (buf->b_changelist[i].lnum == 0)
4033 continue;
4034 if ((d = dict_alloc()) == NULL)
4035 return;
4036 if (list_append_dict(l, d) == FAIL)
4037 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004038 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4039 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004040 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004041 }
4042#endif
4043}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004044
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004045 static void
4046getpos_both(
4047 typval_T *argvars,
4048 typval_T *rettv,
4049 int getcurpos,
4050 int charcol)
4051{
4052 pos_T *fp = NULL;
4053 pos_T pos;
4054 win_T *wp = curwin;
4055 list_T *l;
4056 int fnum = -1;
4057
4058 if (rettv_list_alloc(rettv) == OK)
4059 {
4060 l = rettv->vval.v_list;
4061 if (getcurpos)
4062 {
4063 if (argvars[0].v_type != VAR_UNKNOWN)
4064 {
4065 wp = find_win_by_nr_or_id(&argvars[0]);
4066 if (wp != NULL)
4067 fp = &wp->w_cursor;
4068 }
4069 else
4070 fp = &curwin->w_cursor;
4071 if (fp != NULL && charcol)
4072 {
4073 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01004074 pos.col =
4075 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004076 fp = &pos;
4077 }
4078 }
4079 else
4080 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
4081 if (fnum != -1)
4082 list_append_number(l, (varnumber_T)fnum);
4083 else
4084 list_append_number(l, (varnumber_T)0);
4085 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4086 : (varnumber_T)0);
4087 list_append_number(l, (fp != NULL)
4088 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4089 : (varnumber_T)0);
4090 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
4091 (varnumber_T)0);
4092 if (getcurpos)
4093 {
4094 int save_set_curswant = curwin->w_set_curswant;
4095 colnr_T save_curswant = curwin->w_curswant;
4096 colnr_T save_virtcol = curwin->w_virtcol;
4097
4098 if (wp == curwin)
4099 update_curswant();
4100 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
4101 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
4102
4103 // Do not change "curswant", as it is unexpected that a get
4104 // function has a side effect.
4105 if (wp == curwin && save_set_curswant)
4106 {
4107 curwin->w_set_curswant = save_set_curswant;
4108 curwin->w_curswant = save_curswant;
4109 curwin->w_virtcol = save_virtcol;
4110 curwin->w_valid &= ~VALID_VIRTCOL;
4111 }
4112 }
4113 }
4114 else
4115 rettv->vval.v_number = FALSE;
4116}
4117
4118/*
4119 * "getcharpos()" function
4120 */
4121 static void
4122f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
4123{
4124 getpos_both(argvars, rettv, FALSE, TRUE);
4125}
4126
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004127/*
4128 * "getcharsearch()" function
4129 */
4130 static void
4131f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4132{
4133 if (rettv_dict_alloc(rettv) != FAIL)
4134 {
4135 dict_T *dict = rettv->vval.v_dict;
4136
Bram Moolenaare0be1672018-07-08 16:50:37 +02004137 dict_add_string(dict, "char", last_csearch());
4138 dict_add_number(dict, "forward", last_csearch_forward());
4139 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004140 }
4141}
4142
4143/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004144 * "getenv()" function
4145 */
4146 static void
4147f_getenv(typval_T *argvars, typval_T *rettv)
4148{
4149 int mustfree = FALSE;
4150 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4151
4152 if (p == NULL)
4153 {
4154 rettv->v_type = VAR_SPECIAL;
4155 rettv->vval.v_number = VVAL_NULL;
4156 return;
4157 }
4158 if (!mustfree)
4159 p = vim_strsave(p);
4160 rettv->vval.v_string = p;
4161 rettv->v_type = VAR_STRING;
4162}
4163
4164/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004165 * "getfontname()" function
4166 */
4167 static void
4168f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4169{
4170 rettv->v_type = VAR_STRING;
4171 rettv->vval.v_string = NULL;
4172#ifdef FEAT_GUI
4173 if (gui.in_use)
4174 {
4175 GuiFont font;
4176 char_u *name = NULL;
4177
4178 if (argvars[0].v_type == VAR_UNKNOWN)
4179 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004180 // Get the "Normal" font. Either the name saved by
4181 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004182 font = gui.norm_font;
4183 name = hl_get_font_name();
4184 }
4185 else
4186 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004187 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004188 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004189 return;
4190 font = gui_mch_get_font(name, FALSE);
4191 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004192 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004193 }
4194 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4195 if (argvars[0].v_type != VAR_UNKNOWN)
4196 gui_mch_free_font(font);
4197 }
4198#endif
4199}
4200
4201/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004202 * "getjumplist()" function
4203 */
4204 static void
4205f_getjumplist(typval_T *argvars, typval_T *rettv)
4206{
4207#ifdef FEAT_JUMPLIST
4208 win_T *wp;
4209 int i;
4210 list_T *l;
4211 dict_T *d;
4212#endif
4213
4214 if (rettv_list_alloc(rettv) != OK)
4215 return;
4216
4217#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004218 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004219 if (wp == NULL)
4220 return;
4221
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004222 cleanup_jumplist(wp, TRUE);
4223
Bram Moolenaar4f505882018-02-10 21:06:32 +01004224 l = list_alloc();
4225 if (l == NULL)
4226 return;
4227
4228 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4229 return;
4230 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4231
4232 for (i = 0; i < wp->w_jumplistlen; ++i)
4233 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004234 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4235 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004236 if ((d = dict_alloc()) == NULL)
4237 return;
4238 if (list_append_dict(l, d) == FAIL)
4239 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004240 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4241 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004242 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004243 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004244 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004245 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004246 }
4247#endif
4248}
4249
4250/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004251 * "getpid()" function
4252 */
4253 static void
4254f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4255{
4256 rettv->vval.v_number = mch_get_pid();
4257}
4258
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004259/*
4260 * "getcurpos()" function
4261 */
4262 static void
4263f_getcurpos(typval_T *argvars, typval_T *rettv)
4264{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004265 getpos_both(argvars, rettv, TRUE, FALSE);
4266}
4267
4268 static void
4269f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4270{
4271 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004272}
4273
4274/*
4275 * "getpos(string)" function
4276 */
4277 static void
4278f_getpos(typval_T *argvars, typval_T *rettv)
4279{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004280 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004281}
4282
4283/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004284 * "getreg()" function
4285 */
4286 static void
4287f_getreg(typval_T *argvars, typval_T *rettv)
4288{
4289 char_u *strregname;
4290 int regname;
4291 int arg2 = FALSE;
4292 int return_list = FALSE;
4293 int error = FALSE;
4294
4295 if (argvars[0].v_type != VAR_UNKNOWN)
4296 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004297 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004298 if (strregname == NULL)
4299 error = TRUE;
4300 else if (in_vim9script() && STRLEN(strregname) > 1)
4301 {
4302 semsg(_(e_register_name_must_be_one_char_str), strregname);
4303 error = TRUE;
4304 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004305 if (argvars[1].v_type != VAR_UNKNOWN)
4306 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004307 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004308 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004309 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004310 }
4311 }
4312 else
4313 strregname = get_vim_var_str(VV_REG);
4314
4315 if (error)
4316 return;
4317
4318 regname = (strregname == NULL ? '"' : *strregname);
4319 if (regname == 0)
4320 regname = '"';
4321
4322 if (return_list)
4323 {
4324 rettv->v_type = VAR_LIST;
4325 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4326 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4327 if (rettv->vval.v_list == NULL)
4328 (void)rettv_list_alloc(rettv);
4329 else
4330 ++rettv->vval.v_list->lv_refcount;
4331 }
4332 else
4333 {
4334 rettv->v_type = VAR_STRING;
4335 rettv->vval.v_string = get_reg_contents(regname,
4336 arg2 ? GREG_EXPR_SRC : 0);
4337 }
4338}
4339
4340/*
4341 * "getregtype()" function
4342 */
4343 static void
4344f_getregtype(typval_T *argvars, typval_T *rettv)
4345{
4346 char_u *strregname;
4347 int regname;
4348 char_u buf[NUMBUFLEN + 2];
4349 long reglen = 0;
4350
4351 if (argvars[0].v_type != VAR_UNKNOWN)
4352 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004353 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004354 if (strregname != NULL && in_vim9script() && STRLEN(strregname) > 1)
4355 {
4356 semsg(_(e_register_name_must_be_one_char_str), strregname);
4357 strregname = NULL;
4358 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004359 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004360 {
4361 rettv->v_type = VAR_STRING;
4362 rettv->vval.v_string = NULL;
4363 return;
4364 }
4365 }
4366 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004367 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004368 strregname = get_vim_var_str(VV_REG);
4369
4370 regname = (strregname == NULL ? '"' : *strregname);
4371 if (regname == 0)
4372 regname = '"';
4373
4374 buf[0] = NUL;
4375 buf[1] = NUL;
4376 switch (get_reg_type(regname, &reglen))
4377 {
4378 case MLINE: buf[0] = 'V'; break;
4379 case MCHAR: buf[0] = 'v'; break;
4380 case MBLOCK:
4381 buf[0] = Ctrl_V;
4382 sprintf((char *)buf + 1, "%ld", reglen + 1);
4383 break;
4384 }
4385 rettv->v_type = VAR_STRING;
4386 rettv->vval.v_string = vim_strsave(buf);
4387}
4388
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004389/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004390 * "gettagstack()" function
4391 */
4392 static void
4393f_gettagstack(typval_T *argvars, typval_T *rettv)
4394{
4395 win_T *wp = curwin; // default is current window
4396
4397 if (rettv_dict_alloc(rettv) != OK)
4398 return;
4399
4400 if (argvars[0].v_type != VAR_UNKNOWN)
4401 {
4402 wp = find_win_by_nr_or_id(&argvars[0]);
4403 if (wp == NULL)
4404 return;
4405 }
4406
4407 get_tagstack(wp, rettv->vval.v_dict);
4408}
4409
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004410/*
4411 * "gettext()" function
4412 */
4413 static void
4414f_gettext(typval_T *argvars, typval_T *rettv)
4415{
4416 if (argvars[0].v_type != VAR_STRING
4417 || argvars[0].vval.v_string == NULL
4418 || *argvars[0].vval.v_string == NUL)
4419 {
4420 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4421 }
4422 else
4423 {
4424 rettv->v_type = VAR_STRING;
4425 rettv->vval.v_string = vim_strsave(
4426 (char_u *)_(argvars[0].vval.v_string));
4427 }
4428}
4429
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004430// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004431#include "version.h"
4432
4433/*
4434 * "has()" function
4435 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004436 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004437f_has(typval_T *argvars, typval_T *rettv)
4438{
4439 int i;
4440 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004441 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004442 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004443 typedef struct {
4444 char *name;
4445 short present;
4446 } has_item_T;
4447 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004448 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004449 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004450#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004451 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004452#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004453 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004454#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004455 },
4456 {"arp",
4457#if defined(AMIGA) && defined(FEAT_ARP)
4458 1
4459#else
4460 0
4461#endif
4462 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004463 {"haiku",
4464#ifdef __HAIKU__
4465 1
4466#else
4467 0
4468#endif
4469 },
4470 {"bsd",
4471#if defined(BSD) && !defined(MACOS_X)
4472 1
4473#else
4474 0
4475#endif
4476 },
4477 {"hpux",
4478#ifdef hpux
4479 1
4480#else
4481 0
4482#endif
4483 },
4484 {"linux",
4485#ifdef __linux__
4486 1
4487#else
4488 0
4489#endif
4490 },
4491 {"mac", // Mac OS X (and, once, Mac OS Classic)
4492#ifdef MACOS_X
4493 1
4494#else
4495 0
4496#endif
4497 },
4498 {"osx", // Mac OS X
4499#ifdef MACOS_X
4500 1
4501#else
4502 0
4503#endif
4504 },
4505 {"macunix", // Mac OS X, with the darwin feature
4506#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4507 1
4508#else
4509 0
4510#endif
4511 },
4512 {"osxdarwin", // synonym for macunix
4513#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4514 1
4515#else
4516 0
4517#endif
4518 },
4519 {"qnx",
4520#ifdef __QNX__
4521 1
4522#else
4523 0
4524#endif
4525 },
4526 {"sun",
4527#ifdef SUN_SYSTEM
4528 1
4529#else
4530 0
4531#endif
4532 },
4533 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004534#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004535 1
4536#else
4537 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004538#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004539 },
4540 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004541#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004542 1
4543#else
4544 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004545#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004546 },
4547 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004548#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004549 1
4550#else
4551 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004552#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004553 },
4554 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004555#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004556 1
4557#else
4558 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004559#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004560 },
4561 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004562#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004563 1
4564#else
4565 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004566#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004567 },
4568 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004569#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004570 1
4571#else
4572 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004573#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004574 },
4575 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004576#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004577 1
4578#else
4579 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004580#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004581 },
4582 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004583#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004584 1
4585#else
4586 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004587#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004588 },
4589 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004590#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004591 1
4592#else
4593 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004594#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004595 },
4596 {"autocmd", 1},
4597 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004598#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004599 1
4600#else
4601 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004602#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004603 },
4604 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004605#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004606 1
4607#else
4608 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004609#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004610 },
4611 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004612#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004613 1
4614#else
4615 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004616#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004617 },
4618 {"balloon_multiline",
4619#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4620 // MS-Windows requires runtime check, see below
4621 1
4622#else
4623 0
4624#endif
4625 },
4626 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004627#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004628 1
4629#else
4630 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004631#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004632 },
4633 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004634#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004635 1
4636#else
4637 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004638#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004639 },
4640 {"all_builtin_terms",
4641#if defined(ALL_BUILTIN_TCAPS)
4642 1
4643#else
4644 0
4645#endif
4646 },
4647 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004648#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004649 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004650 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004651 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004652#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004653 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004654#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004655 },
4656 {"byte_offset",
4657#ifdef FEAT_BYTEOFF
4658 1
4659#else
4660 0
4661#endif
4662 },
4663 {"channel",
4664#ifdef FEAT_JOB_CHANNEL
4665 1
4666#else
4667 0
4668#endif
4669 },
4670 {"cindent",
4671#ifdef FEAT_CINDENT
4672 1
4673#else
4674 0
4675#endif
4676 },
4677 {"clientserver",
4678#ifdef FEAT_CLIENTSERVER
4679 1
4680#else
4681 0
4682#endif
4683 },
4684 {"clipboard",
4685#ifdef FEAT_CLIPBOARD
4686 1
4687#else
4688 0
4689#endif
4690 },
4691 {"cmdline_compl", 1},
4692 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004693 {"cmdwin",
4694#ifdef FEAT_CMDWIN
4695 1
4696#else
4697 0
4698#endif
4699 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004700 {"comments", 1},
4701 {"conceal",
4702#ifdef FEAT_CONCEAL
4703 1
4704#else
4705 0
4706#endif
4707 },
4708 {"cryptv",
4709#ifdef FEAT_CRYPT
4710 1
4711#else
4712 0
4713#endif
4714 },
4715 {"crypt-blowfish",
4716#ifdef FEAT_CRYPT
4717 1
4718#else
4719 0
4720#endif
4721 },
4722 {"crypt-blowfish2",
4723#ifdef FEAT_CRYPT
4724 1
4725#else
4726 0
4727#endif
4728 },
4729 {"cscope",
4730#ifdef FEAT_CSCOPE
4731 1
4732#else
4733 0
4734#endif
4735 },
4736 {"cursorbind", 1},
4737 {"cursorshape",
4738#ifdef CURSOR_SHAPE
4739 1
4740#else
4741 0
4742#endif
4743 },
4744 {"debug",
4745#ifdef DEBUG
4746 1
4747#else
4748 0
4749#endif
4750 },
4751 {"dialog_con",
4752#ifdef FEAT_CON_DIALOG
4753 1
4754#else
4755 0
4756#endif
4757 },
4758 {"dialog_gui",
4759#ifdef FEAT_GUI_DIALOG
4760 1
4761#else
4762 0
4763#endif
4764 },
4765 {"diff",
4766#ifdef FEAT_DIFF
4767 1
4768#else
4769 0
4770#endif
4771 },
4772 {"digraphs",
4773#ifdef FEAT_DIGRAPHS
4774 1
4775#else
4776 0
4777#endif
4778 },
4779 {"directx",
4780#ifdef FEAT_DIRECTX
4781 1
4782#else
4783 0
4784#endif
4785 },
4786 {"dnd",
4787#ifdef FEAT_DND
4788 1
4789#else
4790 0
4791#endif
4792 },
4793 {"emacs_tags",
4794#ifdef FEAT_EMACS_TAGS
4795 1
4796#else
4797 0
4798#endif
4799 },
4800 {"eval", 1}, // always present, of course!
4801 {"ex_extra", 1}, // graduated feature
4802 {"extra_search",
4803#ifdef FEAT_SEARCH_EXTRA
4804 1
4805#else
4806 0
4807#endif
4808 },
4809 {"file_in_path",
4810#ifdef FEAT_SEARCHPATH
4811 1
4812#else
4813 0
4814#endif
4815 },
4816 {"filterpipe",
4817#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4818 1
4819#else
4820 0
4821#endif
4822 },
4823 {"find_in_path",
4824#ifdef FEAT_FIND_ID
4825 1
4826#else
4827 0
4828#endif
4829 },
4830 {"float",
4831#ifdef FEAT_FLOAT
4832 1
4833#else
4834 0
4835#endif
4836 },
4837 {"folding",
4838#ifdef FEAT_FOLDING
4839 1
4840#else
4841 0
4842#endif
4843 },
4844 {"footer",
4845#ifdef FEAT_FOOTER
4846 1
4847#else
4848 0
4849#endif
4850 },
4851 {"fork",
4852#if !defined(USE_SYSTEM) && defined(UNIX)
4853 1
4854#else
4855 0
4856#endif
4857 },
4858 {"gettext",
4859#ifdef FEAT_GETTEXT
4860 1
4861#else
4862 0
4863#endif
4864 },
4865 {"gui",
4866#ifdef FEAT_GUI
4867 1
4868#else
4869 0
4870#endif
4871 },
4872 {"gui_neXtaw",
4873#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4874 1
4875#else
4876 0
4877#endif
4878 },
4879 {"gui_athena",
4880#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4881 1
4882#else
4883 0
4884#endif
4885 },
4886 {"gui_gtk",
4887#ifdef FEAT_GUI_GTK
4888 1
4889#else
4890 0
4891#endif
4892 },
4893 {"gui_gtk2",
4894#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4895 1
4896#else
4897 0
4898#endif
4899 },
4900 {"gui_gtk3",
4901#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4902 1
4903#else
4904 0
4905#endif
4906 },
4907 {"gui_gnome",
4908#ifdef FEAT_GUI_GNOME
4909 1
4910#else
4911 0
4912#endif
4913 },
4914 {"gui_haiku",
4915#ifdef FEAT_GUI_HAIKU
4916 1
4917#else
4918 0
4919#endif
4920 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004921 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004922 {"gui_motif",
4923#ifdef FEAT_GUI_MOTIF
4924 1
4925#else
4926 0
4927#endif
4928 },
4929 {"gui_photon",
4930#ifdef FEAT_GUI_PHOTON
4931 1
4932#else
4933 0
4934#endif
4935 },
4936 {"gui_win32",
4937#ifdef FEAT_GUI_MSWIN
4938 1
4939#else
4940 0
4941#endif
4942 },
4943 {"iconv",
4944#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4945 1
4946#else
4947 0
4948#endif
4949 },
4950 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004951 {"ipv6",
4952#ifdef FEAT_IPV6
4953 1
4954#else
4955 0
4956#endif
4957 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004958 {"job",
4959#ifdef FEAT_JOB_CHANNEL
4960 1
4961#else
4962 0
4963#endif
4964 },
4965 {"jumplist",
4966#ifdef FEAT_JUMPLIST
4967 1
4968#else
4969 0
4970#endif
4971 },
4972 {"keymap",
4973#ifdef FEAT_KEYMAP
4974 1
4975#else
4976 0
4977#endif
4978 },
4979 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4980 {"langmap",
4981#ifdef FEAT_LANGMAP
4982 1
4983#else
4984 0
4985#endif
4986 },
4987 {"libcall",
4988#ifdef FEAT_LIBCALL
4989 1
4990#else
4991 0
4992#endif
4993 },
4994 {"linebreak",
4995#ifdef FEAT_LINEBREAK
4996 1
4997#else
4998 0
4999#endif
5000 },
5001 {"lispindent",
5002#ifdef FEAT_LISP
5003 1
5004#else
5005 0
5006#endif
5007 },
5008 {"listcmds", 1},
5009 {"localmap", 1},
5010 {"lua",
5011#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
5012 1
5013#else
5014 0
5015#endif
5016 },
5017 {"menu",
5018#ifdef FEAT_MENU
5019 1
5020#else
5021 0
5022#endif
5023 },
5024 {"mksession",
5025#ifdef FEAT_SESSION
5026 1
5027#else
5028 0
5029#endif
5030 },
5031 {"modify_fname", 1},
5032 {"mouse", 1},
5033 {"mouseshape",
5034#ifdef FEAT_MOUSESHAPE
5035 1
5036#else
5037 0
5038#endif
5039 },
5040 {"mouse_dec",
5041#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
5042 1
5043#else
5044 0
5045#endif
5046 },
5047 {"mouse_gpm",
5048#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
5049 1
5050#else
5051 0
5052#endif
5053 },
5054 {"mouse_jsbterm",
5055#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
5056 1
5057#else
5058 0
5059#endif
5060 },
5061 {"mouse_netterm",
5062#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
5063 1
5064#else
5065 0
5066#endif
5067 },
5068 {"mouse_pterm",
5069#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
5070 1
5071#else
5072 0
5073#endif
5074 },
5075 {"mouse_sgr",
5076#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5077 1
5078#else
5079 0
5080#endif
5081 },
5082 {"mouse_sysmouse",
5083#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
5084 1
5085#else
5086 0
5087#endif
5088 },
5089 {"mouse_urxvt",
5090#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
5091 1
5092#else
5093 0
5094#endif
5095 },
5096 {"mouse_xterm",
5097#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5098 1
5099#else
5100 0
5101#endif
5102 },
5103 {"multi_byte", 1},
5104 {"multi_byte_ime",
5105#ifdef FEAT_MBYTE_IME
5106 1
5107#else
5108 0
5109#endif
5110 },
5111 {"multi_lang",
5112#ifdef FEAT_MULTI_LANG
5113 1
5114#else
5115 0
5116#endif
5117 },
5118 {"mzscheme",
5119#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
5120 1
5121#else
5122 0
5123#endif
5124 },
5125 {"num64", 1},
5126 {"ole",
5127#ifdef FEAT_OLE
5128 1
5129#else
5130 0
5131#endif
5132 },
5133 {"packages",
5134#ifdef FEAT_EVAL
5135 1
5136#else
5137 0
5138#endif
5139 },
5140 {"path_extra",
5141#ifdef FEAT_PATH_EXTRA
5142 1
5143#else
5144 0
5145#endif
5146 },
5147 {"perl",
5148#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
5149 1
5150#else
5151 0
5152#endif
5153 },
5154 {"persistent_undo",
5155#ifdef FEAT_PERSISTENT_UNDO
5156 1
5157#else
5158 0
5159#endif
5160 },
5161 {"python_compiled",
5162#if defined(FEAT_PYTHON)
5163 1
5164#else
5165 0
5166#endif
5167 },
5168 {"python_dynamic",
5169#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
5170 1
5171#else
5172 0
5173#endif
5174 },
5175 {"python",
5176#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
5177 1
5178#else
5179 0
5180#endif
5181 },
5182 {"pythonx",
5183#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
5184 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
5185 1
5186#else
5187 0
5188#endif
5189 },
5190 {"python3_compiled",
5191#if defined(FEAT_PYTHON3)
5192 1
5193#else
5194 0
5195#endif
5196 },
5197 {"python3_dynamic",
5198#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
5199 1
5200#else
5201 0
5202#endif
5203 },
5204 {"python3",
5205#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
5206 1
5207#else
5208 0
5209#endif
5210 },
5211 {"popupwin",
5212#ifdef FEAT_PROP_POPUP
5213 1
5214#else
5215 0
5216#endif
5217 },
5218 {"postscript",
5219#ifdef FEAT_POSTSCRIPT
5220 1
5221#else
5222 0
5223#endif
5224 },
5225 {"printer",
5226#ifdef FEAT_PRINTER
5227 1
5228#else
5229 0
5230#endif
5231 },
5232 {"profile",
5233#ifdef FEAT_PROFILE
5234 1
5235#else
5236 0
5237#endif
5238 },
5239 {"reltime",
5240#ifdef FEAT_RELTIME
5241 1
5242#else
5243 0
5244#endif
5245 },
5246 {"quickfix",
5247#ifdef FEAT_QUICKFIX
5248 1
5249#else
5250 0
5251#endif
5252 },
5253 {"rightleft",
5254#ifdef FEAT_RIGHTLEFT
5255 1
5256#else
5257 0
5258#endif
5259 },
5260 {"ruby",
5261#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5262 1
5263#else
5264 0
5265#endif
5266 },
5267 {"scrollbind", 1},
5268 {"showcmd",
5269#ifdef FEAT_CMDL_INFO
5270 1
5271#else
5272 0
5273#endif
5274 },
5275 {"cmdline_info",
5276#ifdef FEAT_CMDL_INFO
5277 1
5278#else
5279 0
5280#endif
5281 },
5282 {"signs",
5283#ifdef FEAT_SIGNS
5284 1
5285#else
5286 0
5287#endif
5288 },
5289 {"smartindent",
5290#ifdef FEAT_SMARTINDENT
5291 1
5292#else
5293 0
5294#endif
5295 },
5296 {"startuptime",
5297#ifdef STARTUPTIME
5298 1
5299#else
5300 0
5301#endif
5302 },
5303 {"statusline",
5304#ifdef FEAT_STL_OPT
5305 1
5306#else
5307 0
5308#endif
5309 },
5310 {"netbeans_intg",
5311#ifdef FEAT_NETBEANS_INTG
5312 1
5313#else
5314 0
5315#endif
5316 },
5317 {"sound",
5318#ifdef FEAT_SOUND
5319 1
5320#else
5321 0
5322#endif
5323 },
5324 {"spell",
5325#ifdef FEAT_SPELL
5326 1
5327#else
5328 0
5329#endif
5330 },
5331 {"syntax",
5332#ifdef FEAT_SYN_HL
5333 1
5334#else
5335 0
5336#endif
5337 },
5338 {"system",
5339#if defined(USE_SYSTEM) || !defined(UNIX)
5340 1
5341#else
5342 0
5343#endif
5344 },
5345 {"tag_binary",
5346#ifdef FEAT_TAG_BINS
5347 1
5348#else
5349 0
5350#endif
5351 },
5352 {"tcl",
5353#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5354 1
5355#else
5356 0
5357#endif
5358 },
5359 {"termguicolors",
5360#ifdef FEAT_TERMGUICOLORS
5361 1
5362#else
5363 0
5364#endif
5365 },
5366 {"terminal",
5367#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5368 1
5369#else
5370 0
5371#endif
5372 },
5373 {"terminfo",
5374#ifdef TERMINFO
5375 1
5376#else
5377 0
5378#endif
5379 },
5380 {"termresponse",
5381#ifdef FEAT_TERMRESPONSE
5382 1
5383#else
5384 0
5385#endif
5386 },
5387 {"textobjects",
5388#ifdef FEAT_TEXTOBJ
5389 1
5390#else
5391 0
5392#endif
5393 },
5394 {"textprop",
5395#ifdef FEAT_PROP_POPUP
5396 1
5397#else
5398 0
5399#endif
5400 },
5401 {"tgetent",
5402#ifdef HAVE_TGETENT
5403 1
5404#else
5405 0
5406#endif
5407 },
5408 {"timers",
5409#ifdef FEAT_TIMERS
5410 1
5411#else
5412 0
5413#endif
5414 },
5415 {"title",
5416#ifdef FEAT_TITLE
5417 1
5418#else
5419 0
5420#endif
5421 },
5422 {"toolbar",
5423#ifdef FEAT_TOOLBAR
5424 1
5425#else
5426 0
5427#endif
5428 },
5429 {"unnamedplus",
5430#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5431 1
5432#else
5433 0
5434#endif
5435 },
5436 {"user-commands", 1}, // was accidentally included in 5.4
5437 {"user_commands", 1},
5438 {"vartabs",
5439#ifdef FEAT_VARTABS
5440 1
5441#else
5442 0
5443#endif
5444 },
5445 {"vertsplit", 1},
5446 {"viminfo",
5447#ifdef FEAT_VIMINFO
5448 1
5449#else
5450 0
5451#endif
5452 },
5453 {"vimscript-1", 1},
5454 {"vimscript-2", 1},
5455 {"vimscript-3", 1},
5456 {"vimscript-4", 1},
5457 {"virtualedit", 1},
5458 {"visual", 1},
5459 {"visualextra", 1},
5460 {"vreplace", 1},
5461 {"vtp",
5462#ifdef FEAT_VTP
5463 1
5464#else
5465 0
5466#endif
5467 },
5468 {"wildignore",
5469#ifdef FEAT_WILDIGN
5470 1
5471#else
5472 0
5473#endif
5474 },
5475 {"wildmenu",
5476#ifdef FEAT_WILDMENU
5477 1
5478#else
5479 0
5480#endif
5481 },
5482 {"windows", 1},
5483 {"winaltkeys",
5484#ifdef FEAT_WAK
5485 1
5486#else
5487 0
5488#endif
5489 },
5490 {"writebackup",
5491#ifdef FEAT_WRITEBACKUP
5492 1
5493#else
5494 0
5495#endif
5496 },
5497 {"xim",
5498#ifdef FEAT_XIM
5499 1
5500#else
5501 0
5502#endif
5503 },
5504 {"xfontset",
5505#ifdef FEAT_XFONTSET
5506 1
5507#else
5508 0
5509#endif
5510 },
5511 {"xpm",
5512#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5513 1
5514#else
5515 0
5516#endif
5517 },
5518 {"xpm_w32", // for backward compatibility
5519#ifdef FEAT_XPM_W32
5520 1
5521#else
5522 0
5523#endif
5524 },
5525 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005526#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005527 1
5528#else
5529 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005530#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005531 },
5532 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005533#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005534 1
5535#else
5536 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005537#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005538 },
5539 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005540#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005541 1
5542#else
5543 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005544#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005545 },
5546 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005547#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005548 1
5549#else
5550 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005551#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005552 },
5553 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005554#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005555 1
5556#else
5557 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005558#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005559 },
5560 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005561 };
5562
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005563 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005564 for (i = 0; has_list[i].name != NULL; ++i)
5565 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005566 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005567 x = TRUE;
5568 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005569 break;
5570 }
5571
Bram Moolenaar79296512020-03-22 16:17:14 +01005572 // features also in has_list[] but sometimes enabled at runtime
5573 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005574 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005575 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005576 {
5577 // intentionally empty
5578 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005579#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005580 else if (STRICMP(name, "balloon_multiline") == 0)
5581 n = multiline_balloon_available();
5582#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005583#ifdef VIMDLL
5584 else if (STRICMP(name, "filterpipe") == 0)
5585 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005586#endif
5587#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5588 else if (STRICMP(name, "iconv") == 0)
5589 n = iconv_enabled(FALSE);
5590#endif
5591#ifdef DYNAMIC_LUA
5592 else if (STRICMP(name, "lua") == 0)
5593 n = lua_enabled(FALSE);
5594#endif
5595#ifdef DYNAMIC_MZSCHEME
5596 else if (STRICMP(name, "mzscheme") == 0)
5597 n = mzscheme_enabled(FALSE);
5598#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005599#ifdef DYNAMIC_PERL
5600 else if (STRICMP(name, "perl") == 0)
5601 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005602#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005603#ifdef DYNAMIC_PYTHON
5604 else if (STRICMP(name, "python") == 0)
5605 n = python_enabled(FALSE);
5606#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005607#ifdef DYNAMIC_PYTHON3
5608 else if (STRICMP(name, "python3") == 0)
5609 n = python3_enabled(FALSE);
5610#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005611#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5612 else if (STRICMP(name, "pythonx") == 0)
5613 {
5614# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5615 if (p_pyx == 0)
5616 n = python3_enabled(FALSE) || python_enabled(FALSE);
5617 else if (p_pyx == 3)
5618 n = python3_enabled(FALSE);
5619 else if (p_pyx == 2)
5620 n = python_enabled(FALSE);
5621# elif defined(DYNAMIC_PYTHON)
5622 n = python_enabled(FALSE);
5623# elif defined(DYNAMIC_PYTHON3)
5624 n = python3_enabled(FALSE);
5625# endif
5626 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005627#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005628#ifdef DYNAMIC_RUBY
5629 else if (STRICMP(name, "ruby") == 0)
5630 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005631#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005632#ifdef DYNAMIC_TCL
5633 else if (STRICMP(name, "tcl") == 0)
5634 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005635#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005636#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005637 else if (STRICMP(name, "terminal") == 0)
5638 n = terminal_enabled();
5639#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005640 }
5641
Bram Moolenaar79296512020-03-22 16:17:14 +01005642 // features not in has_list[]
5643 if (x == FALSE)
5644 {
5645 if (STRNICMP(name, "patch", 5) == 0)
5646 {
5647 x = TRUE;
5648 if (name[5] == '-'
5649 && STRLEN(name) >= 11
5650 && vim_isdigit(name[6])
5651 && vim_isdigit(name[8])
5652 && vim_isdigit(name[10]))
5653 {
5654 int major = atoi((char *)name + 6);
5655 int minor = atoi((char *)name + 8);
5656
5657 // Expect "patch-9.9.01234".
5658 n = (major < VIM_VERSION_MAJOR
5659 || (major == VIM_VERSION_MAJOR
5660 && (minor < VIM_VERSION_MINOR
5661 || (minor == VIM_VERSION_MINOR
5662 && has_patch(atoi((char *)name + 10))))));
5663 }
5664 else
5665 n = has_patch(atoi((char *)name + 5));
5666 }
5667 else if (STRICMP(name, "vim_starting") == 0)
5668 {
5669 x = TRUE;
5670 n = (starting != 0);
5671 }
5672 else if (STRICMP(name, "ttyin") == 0)
5673 {
5674 x = TRUE;
5675 n = mch_input_isatty();
5676 }
5677 else if (STRICMP(name, "ttyout") == 0)
5678 {
5679 x = TRUE;
5680 n = stdout_isatty;
5681 }
5682 else if (STRICMP(name, "multi_byte_encoding") == 0)
5683 {
5684 x = TRUE;
5685 n = has_mbyte;
5686 }
5687 else if (STRICMP(name, "gui_running") == 0)
5688 {
5689 x = TRUE;
5690#ifdef FEAT_GUI
5691 n = (gui.in_use || gui.starting);
5692#endif
5693 }
5694 else if (STRICMP(name, "browse") == 0)
5695 {
5696 x = TRUE;
5697#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5698 n = gui.in_use; // gui_mch_browse() works when GUI is running
5699#endif
5700 }
5701 else if (STRICMP(name, "syntax_items") == 0)
5702 {
5703 x = TRUE;
5704#ifdef FEAT_SYN_HL
5705 n = syntax_present(curwin);
5706#endif
5707 }
5708 else if (STRICMP(name, "vcon") == 0)
5709 {
5710 x = TRUE;
5711#ifdef FEAT_VTP
5712 n = is_term_win32() && has_vtp_working();
5713#endif
5714 }
5715 else if (STRICMP(name, "netbeans_enabled") == 0)
5716 {
5717 x = TRUE;
5718#ifdef FEAT_NETBEANS_INTG
5719 n = netbeans_active();
5720#endif
5721 }
5722 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5723 {
5724 x = TRUE;
5725#ifdef FEAT_MOUSE_GPM
5726 n = gpm_enabled();
5727#endif
5728 }
5729 else if (STRICMP(name, "conpty") == 0)
5730 {
5731 x = TRUE;
5732#if defined(FEAT_TERMINAL) && defined(MSWIN)
5733 n = use_conpty();
5734#endif
5735 }
5736 else if (STRICMP(name, "clipboard_working") == 0)
5737 {
5738 x = TRUE;
5739#ifdef FEAT_CLIPBOARD
5740 n = clip_star.available;
5741#endif
5742 }
5743 }
5744
Bram Moolenaar04637e22020-09-05 18:45:29 +02005745 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005746 // return whether feature could ever be enabled
5747 rettv->vval.v_number = x;
5748 else
5749 // return whether feature is enabled
5750 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005751}
5752
5753/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005754 * Return TRUE if "feature" can change later.
5755 * Also when checking for the feature has side effects, such as loading a DLL.
5756 */
5757 int
5758dynamic_feature(char_u *feature)
5759{
5760 return (feature == NULL
5761#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5762 || STRICMP(feature, "balloon_multiline") == 0
5763#endif
5764#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5765 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5766#endif
5767#ifdef VIMDLL
5768 || STRICMP(feature, "filterpipe") == 0
5769#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005770#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005771 // this can only change on Unix where the ":gui" command could be
5772 // used.
5773 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5774#endif
5775#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5776 || STRICMP(feature, "iconv") == 0
5777#endif
5778#ifdef DYNAMIC_LUA
5779 || STRICMP(feature, "lua") == 0
5780#endif
5781#ifdef FEAT_MOUSE_GPM
5782 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5783#endif
5784#ifdef DYNAMIC_MZSCHEME
5785 || STRICMP(feature, "mzscheme") == 0
5786#endif
5787#ifdef FEAT_NETBEANS_INTG
5788 || STRICMP(feature, "netbeans_enabled") == 0
5789#endif
5790#ifdef DYNAMIC_PERL
5791 || STRICMP(feature, "perl") == 0
5792#endif
5793#ifdef DYNAMIC_PYTHON
5794 || STRICMP(feature, "python") == 0
5795#endif
5796#ifdef DYNAMIC_PYTHON3
5797 || STRICMP(feature, "python3") == 0
5798#endif
5799#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5800 || STRICMP(feature, "pythonx") == 0
5801#endif
5802#ifdef DYNAMIC_RUBY
5803 || STRICMP(feature, "ruby") == 0
5804#endif
5805#ifdef FEAT_SYN_HL
5806 || STRICMP(feature, "syntax_items") == 0
5807#endif
5808#ifdef DYNAMIC_TCL
5809 || STRICMP(feature, "tcl") == 0
5810#endif
5811 // once "starting" is zero it will stay that way
5812 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5813 || STRICMP(feature, "multi_byte_encoding") == 0
5814#if defined(FEAT_TERMINAL) && defined(MSWIN)
5815 || STRICMP(feature, "conpty") == 0
5816#endif
5817 );
5818}
5819
5820/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005821 * "haslocaldir()" function
5822 */
5823 static void
5824f_haslocaldir(typval_T *argvars, typval_T *rettv)
5825{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005826 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005827 win_T *wp = NULL;
5828
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005829 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5830
5831 // Check for window-local and tab-local directories
5832 if (wp != NULL && wp->w_localdir != NULL)
5833 rettv->vval.v_number = 1;
5834 else if (tp != NULL && tp->tp_localdir != NULL)
5835 rettv->vval.v_number = 2;
5836 else
5837 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005838}
5839
5840/*
5841 * "hasmapto()" function
5842 */
5843 static void
5844f_hasmapto(typval_T *argvars, typval_T *rettv)
5845{
5846 char_u *name;
5847 char_u *mode;
5848 char_u buf[NUMBUFLEN];
5849 int abbr = FALSE;
5850
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005851 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005852 if (argvars[1].v_type == VAR_UNKNOWN)
5853 mode = (char_u *)"nvo";
5854 else
5855 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005856 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005857 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005858 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005859 }
5860
5861 if (map_to_exists(name, mode, abbr))
5862 rettv->vval.v_number = TRUE;
5863 else
5864 rettv->vval.v_number = FALSE;
5865}
5866
5867/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005868 * "highlightID(name)" function
5869 */
5870 static void
5871f_hlID(typval_T *argvars, typval_T *rettv)
5872{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005873 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005874}
5875
5876/*
5877 * "highlight_exists()" function
5878 */
5879 static void
5880f_hlexists(typval_T *argvars, typval_T *rettv)
5881{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005882 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005883}
5884
5885/*
5886 * "hostname()" function
5887 */
5888 static void
5889f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5890{
5891 char_u hostname[256];
5892
5893 mch_get_host_name(hostname, 256);
5894 rettv->v_type = VAR_STRING;
5895 rettv->vval.v_string = vim_strsave(hostname);
5896}
5897
5898/*
5899 * iconv() function
5900 */
5901 static void
5902f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5903{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005904 char_u buf1[NUMBUFLEN];
5905 char_u buf2[NUMBUFLEN];
5906 char_u *from, *to, *str;
5907 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005908
5909 rettv->v_type = VAR_STRING;
5910 rettv->vval.v_string = NULL;
5911
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005912 str = tv_get_string(&argvars[0]);
5913 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5914 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005915 vimconv.vc_type = CONV_NONE;
5916 convert_setup(&vimconv, from, to);
5917
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005918 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005919 if (vimconv.vc_type == CONV_NONE)
5920 rettv->vval.v_string = vim_strsave(str);
5921 else
5922 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5923
5924 convert_setup(&vimconv, NULL, NULL);
5925 vim_free(from);
5926 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005927}
5928
5929/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005930 * "index()" function
5931 */
5932 static void
5933f_index(typval_T *argvars, typval_T *rettv)
5934{
5935 list_T *l;
5936 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005937 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005938 long idx = 0;
5939 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005940 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005941
5942 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005943 if (argvars[0].v_type == VAR_BLOB)
5944 {
5945 typval_T tv;
5946 int start = 0;
5947
5948 if (argvars[2].v_type != VAR_UNKNOWN)
5949 {
5950 start = tv_get_number_chk(&argvars[2], &error);
5951 if (error)
5952 return;
5953 }
5954 b = argvars[0].vval.v_blob;
5955 if (b == NULL)
5956 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005957 if (start < 0)
5958 {
5959 start = blob_len(b) + start;
5960 if (start < 0)
5961 start = 0;
5962 }
5963
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005964 for (idx = start; idx < blob_len(b); ++idx)
5965 {
5966 tv.v_type = VAR_NUMBER;
5967 tv.vval.v_number = blob_get(b, idx);
5968 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5969 {
5970 rettv->vval.v_number = idx;
5971 return;
5972 }
5973 }
5974 return;
5975 }
5976 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005977 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005978 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005979 return;
5980 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005981
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005982 l = argvars[0].vval.v_list;
5983 if (l != NULL)
5984 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005985 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005986 item = l->lv_first;
5987 if (argvars[2].v_type != VAR_UNKNOWN)
5988 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005989 // Start at specified item. Use the cached index that list_find()
5990 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005991 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005992 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005993 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005994 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005995 if (error)
5996 item = NULL;
5997 }
5998
5999 for ( ; item != NULL; item = item->li_next, ++idx)
6000 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6001 {
6002 rettv->vval.v_number = idx;
6003 break;
6004 }
6005 }
6006}
6007
6008static int inputsecret_flag = 0;
6009
6010/*
6011 * "input()" function
6012 * Also handles inputsecret() when inputsecret is set.
6013 */
6014 static void
6015f_input(typval_T *argvars, typval_T *rettv)
6016{
6017 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6018}
6019
6020/*
6021 * "inputdialog()" function
6022 */
6023 static void
6024f_inputdialog(typval_T *argvars, typval_T *rettv)
6025{
6026#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006027 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006028 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6029 {
6030 char_u *message;
6031 char_u buf[NUMBUFLEN];
6032 char_u *defstr = (char_u *)"";
6033
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006034 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006035 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006036 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006037 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6038 else
6039 IObuff[0] = NUL;
6040 if (message != NULL && defstr != NULL
6041 && do_dialog(VIM_QUESTION, NULL, message,
6042 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6043 rettv->vval.v_string = vim_strsave(IObuff);
6044 else
6045 {
6046 if (message != NULL && defstr != NULL
6047 && argvars[1].v_type != VAR_UNKNOWN
6048 && argvars[2].v_type != VAR_UNKNOWN)
6049 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006050 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006051 else
6052 rettv->vval.v_string = NULL;
6053 }
6054 rettv->v_type = VAR_STRING;
6055 }
6056 else
6057#endif
6058 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6059}
6060
6061/*
6062 * "inputlist()" function
6063 */
6064 static void
6065f_inputlist(typval_T *argvars, typval_T *rettv)
6066{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006067 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006068 listitem_T *li;
6069 int selected;
6070 int mouse_used;
6071
6072#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006073 // While starting up, there is no place to enter text. When running tests
6074 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006075 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006076 return;
6077#endif
6078 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6079 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006080 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006081 return;
6082 }
6083
6084 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006085 msg_row = Rows - 1; // for when 'cmdheight' > 1
6086 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006087 msg_scroll = TRUE;
6088 msg_clr_eos();
6089
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006090 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006091 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006092 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006093 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006094 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006095 msg_putchar('\n');
6096 }
6097
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006098 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006099 selected = prompt_for_number(&mouse_used);
6100 if (mouse_used)
6101 selected -= lines_left;
6102
6103 rettv->vval.v_number = selected;
6104}
6105
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006106static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6107
6108/*
6109 * "inputrestore()" function
6110 */
6111 static void
6112f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6113{
6114 if (ga_userinput.ga_len > 0)
6115 {
6116 --ga_userinput.ga_len;
6117 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6118 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006119 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006120 }
6121 else if (p_verbose > 1)
6122 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006123 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006124 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006125 }
6126}
6127
6128/*
6129 * "inputsave()" function
6130 */
6131 static void
6132f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6133{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006134 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006135 if (ga_grow(&ga_userinput, 1) == OK)
6136 {
6137 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6138 + ga_userinput.ga_len);
6139 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006140 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006141 }
6142 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006143 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006144}
6145
6146/*
6147 * "inputsecret()" function
6148 */
6149 static void
6150f_inputsecret(typval_T *argvars, typval_T *rettv)
6151{
6152 ++cmdline_star;
6153 ++inputsecret_flag;
6154 f_input(argvars, rettv);
6155 --cmdline_star;
6156 --inputsecret_flag;
6157}
6158
6159/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01006160 * "interrupt()" function
6161 */
6162 static void
6163f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6164{
6165 got_int = TRUE;
6166}
6167
6168/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006169 * "invert(expr)" function
6170 */
6171 static void
6172f_invert(typval_T *argvars, typval_T *rettv)
6173{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006174 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006175}
6176
6177/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006178 * "islocked()" function
6179 */
6180 static void
6181f_islocked(typval_T *argvars, typval_T *rettv)
6182{
6183 lval_T lv;
6184 char_u *end;
6185 dictitem_T *di;
6186
6187 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006188 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006189 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006190 if (end != NULL && lv.ll_name != NULL)
6191 {
6192 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02006193 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006194 else
6195 {
6196 if (lv.ll_tv == NULL)
6197 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006198 di = find_var(lv.ll_name, NULL, TRUE);
6199 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006200 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006201 // Consider a variable locked when:
6202 // 1. the variable itself is locked
6203 // 2. the value of the variable is locked.
6204 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01006205 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6206 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006207 }
6208 }
6209 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006210 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006211 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006212 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006213 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006214 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006215 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6216 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006217 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006218 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6219 }
6220 }
6221
6222 clear_lval(&lv);
6223}
6224
6225#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6226/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006227 * "isinf()" function
6228 */
6229 static void
6230f_isinf(typval_T *argvars, typval_T *rettv)
6231{
6232 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6233 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6234}
6235
6236/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006237 * "isnan()" function
6238 */
6239 static void
6240f_isnan(typval_T *argvars, typval_T *rettv)
6241{
6242 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6243 && isnan(argvars[0].vval.v_float);
6244}
6245#endif
6246
6247/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006248 * "last_buffer_nr()" function.
6249 */
6250 static void
6251f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6252{
6253 int n = 0;
6254 buf_T *buf;
6255
Bram Moolenaar29323592016-07-24 22:04:11 +02006256 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006257 if (n < buf->b_fnum)
6258 n = buf->b_fnum;
6259
6260 rettv->vval.v_number = n;
6261}
6262
6263/*
6264 * "len()" function
6265 */
6266 static void
6267f_len(typval_T *argvars, typval_T *rettv)
6268{
6269 switch (argvars[0].v_type)
6270 {
6271 case VAR_STRING:
6272 case VAR_NUMBER:
6273 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006274 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006275 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006276 case VAR_BLOB:
6277 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6278 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006279 case VAR_LIST:
6280 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6281 break;
6282 case VAR_DICT:
6283 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6284 break;
6285 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006286 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006287 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006288 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006289 case VAR_SPECIAL:
6290 case VAR_FLOAT:
6291 case VAR_FUNC:
6292 case VAR_PARTIAL:
6293 case VAR_JOB:
6294 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006295 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006296 break;
6297 }
6298}
6299
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006300 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006301libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006302{
6303#ifdef FEAT_LIBCALL
6304 char_u *string_in;
6305 char_u **string_result;
6306 int nr_result;
6307#endif
6308
6309 rettv->v_type = type;
6310 if (type != VAR_NUMBER)
6311 rettv->vval.v_string = NULL;
6312
6313 if (check_restricted() || check_secure())
6314 return;
6315
6316#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006317 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006318 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6319 {
6320 string_in = NULL;
6321 if (argvars[2].v_type == VAR_STRING)
6322 string_in = argvars[2].vval.v_string;
6323 if (type == VAR_NUMBER)
6324 string_result = NULL;
6325 else
6326 string_result = &rettv->vval.v_string;
6327 if (mch_libcall(argvars[0].vval.v_string,
6328 argvars[1].vval.v_string,
6329 string_in,
6330 argvars[2].vval.v_number,
6331 string_result,
6332 &nr_result) == OK
6333 && type == VAR_NUMBER)
6334 rettv->vval.v_number = nr_result;
6335 }
6336#endif
6337}
6338
6339/*
6340 * "libcall()" function
6341 */
6342 static void
6343f_libcall(typval_T *argvars, typval_T *rettv)
6344{
6345 libcall_common(argvars, rettv, VAR_STRING);
6346}
6347
6348/*
6349 * "libcallnr()" function
6350 */
6351 static void
6352f_libcallnr(typval_T *argvars, typval_T *rettv)
6353{
6354 libcall_common(argvars, rettv, VAR_NUMBER);
6355}
6356
6357/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006358 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006359 */
6360 static void
6361f_line(typval_T *argvars, typval_T *rettv)
6362{
6363 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006364 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006365 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006366 int id;
6367 tabpage_T *tp;
6368 win_T *wp;
6369 win_T *save_curwin;
6370 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006371
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006372 if (argvars[1].v_type != VAR_UNKNOWN)
6373 {
6374 // use window specified in the second argument
6375 id = (int)tv_get_number(&argvars[1]);
6376 wp = win_id2wp_tp(id, &tp);
6377 if (wp != NULL && tp != NULL)
6378 {
6379 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6380 == OK)
6381 {
6382 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006383 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006384 }
6385 restore_win_noblock(save_curwin, save_curtab, TRUE);
6386 }
6387 }
6388 else
6389 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006390 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006391
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006392 if (fp != NULL)
6393 lnum = fp->lnum;
6394 rettv->vval.v_number = lnum;
6395}
6396
6397/*
6398 * "line2byte(lnum)" function
6399 */
6400 static void
6401f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6402{
6403#ifndef FEAT_BYTEOFF
6404 rettv->vval.v_number = -1;
6405#else
6406 linenr_T lnum;
6407
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006408 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006409 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6410 rettv->vval.v_number = -1;
6411 else
6412 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6413 if (rettv->vval.v_number >= 0)
6414 ++rettv->vval.v_number;
6415#endif
6416}
6417
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006418#ifdef FEAT_FLOAT
6419/*
6420 * "log()" function
6421 */
6422 static void
6423f_log(typval_T *argvars, typval_T *rettv)
6424{
6425 float_T f = 0.0;
6426
6427 rettv->v_type = VAR_FLOAT;
6428 if (get_float_arg(argvars, &f) == OK)
6429 rettv->vval.v_float = log(f);
6430 else
6431 rettv->vval.v_float = 0.0;
6432}
6433
6434/*
6435 * "log10()" function
6436 */
6437 static void
6438f_log10(typval_T *argvars, typval_T *rettv)
6439{
6440 float_T f = 0.0;
6441
6442 rettv->v_type = VAR_FLOAT;
6443 if (get_float_arg(argvars, &f) == OK)
6444 rettv->vval.v_float = log10(f);
6445 else
6446 rettv->vval.v_float = 0.0;
6447}
6448#endif
6449
6450#ifdef FEAT_LUA
6451/*
6452 * "luaeval()" function
6453 */
6454 static void
6455f_luaeval(typval_T *argvars, typval_T *rettv)
6456{
6457 char_u *str;
6458 char_u buf[NUMBUFLEN];
6459
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006460 if (check_restricted() || check_secure())
6461 return;
6462
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006463 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006464 do_luaeval(str, argvars + 1, rettv);
6465}
6466#endif
6467
6468/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006469 * "maparg()" function
6470 */
6471 static void
6472f_maparg(typval_T *argvars, typval_T *rettv)
6473{
6474 get_maparg(argvars, rettv, TRUE);
6475}
6476
6477/*
6478 * "mapcheck()" function
6479 */
6480 static void
6481f_mapcheck(typval_T *argvars, typval_T *rettv)
6482{
6483 get_maparg(argvars, rettv, FALSE);
6484}
6485
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006486typedef enum
6487{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006488 MATCH_END, // matchend()
6489 MATCH_MATCH, // match()
6490 MATCH_STR, // matchstr()
6491 MATCH_LIST, // matchlist()
6492 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006493} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006494
6495 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006496find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006497{
6498 char_u *str = NULL;
6499 long len = 0;
6500 char_u *expr = NULL;
6501 char_u *pat;
6502 regmatch_T regmatch;
6503 char_u patbuf[NUMBUFLEN];
6504 char_u strbuf[NUMBUFLEN];
6505 char_u *save_cpo;
6506 long start = 0;
6507 long nth = 1;
6508 colnr_T startcol = 0;
6509 int match = 0;
6510 list_T *l = NULL;
6511 listitem_T *li = NULL;
6512 long idx = 0;
6513 char_u *tofree = NULL;
6514
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006515 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006516 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006517 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006518
6519 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006520 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006521 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006522 // type MATCH_LIST: return empty list when there are no matches.
6523 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006524 if (rettv_list_alloc(rettv) == FAIL)
6525 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006526 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006527 && (list_append_string(rettv->vval.v_list,
6528 (char_u *)"", 0) == FAIL
6529 || list_append_number(rettv->vval.v_list,
6530 (varnumber_T)-1) == FAIL
6531 || list_append_number(rettv->vval.v_list,
6532 (varnumber_T)-1) == FAIL
6533 || list_append_number(rettv->vval.v_list,
6534 (varnumber_T)-1) == FAIL))
6535 {
6536 list_free(rettv->vval.v_list);
6537 rettv->vval.v_list = NULL;
6538 goto theend;
6539 }
6540 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006541 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006542 {
6543 rettv->v_type = VAR_STRING;
6544 rettv->vval.v_string = NULL;
6545 }
6546
6547 if (argvars[0].v_type == VAR_LIST)
6548 {
6549 if ((l = argvars[0].vval.v_list) == NULL)
6550 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006551 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006552 li = l->lv_first;
6553 }
6554 else
6555 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006556 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006557 len = (long)STRLEN(str);
6558 }
6559
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006560 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006561 if (pat == NULL)
6562 goto theend;
6563
6564 if (argvars[2].v_type != VAR_UNKNOWN)
6565 {
6566 int error = FALSE;
6567
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006568 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006569 if (error)
6570 goto theend;
6571 if (l != NULL)
6572 {
6573 li = list_find(l, start);
6574 if (li == NULL)
6575 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006576 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006577 }
6578 else
6579 {
6580 if (start < 0)
6581 start = 0;
6582 if (start > len)
6583 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006584 // When "count" argument is there ignore matches before "start",
6585 // otherwise skip part of the string. Differs when pattern is "^"
6586 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006587 if (argvars[3].v_type != VAR_UNKNOWN)
6588 startcol = start;
6589 else
6590 {
6591 str += start;
6592 len -= start;
6593 }
6594 }
6595
6596 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006597 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006598 if (error)
6599 goto theend;
6600 }
6601
6602 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6603 if (regmatch.regprog != NULL)
6604 {
6605 regmatch.rm_ic = p_ic;
6606
6607 for (;;)
6608 {
6609 if (l != NULL)
6610 {
6611 if (li == NULL)
6612 {
6613 match = FALSE;
6614 break;
6615 }
6616 vim_free(tofree);
6617 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6618 if (str == NULL)
6619 break;
6620 }
6621
6622 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6623
6624 if (match && --nth <= 0)
6625 break;
6626 if (l == NULL && !match)
6627 break;
6628
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006629 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006630 if (l != NULL)
6631 {
6632 li = li->li_next;
6633 ++idx;
6634 }
6635 else
6636 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006637 startcol = (colnr_T)(regmatch.startp[0]
6638 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006639 if (startcol > (colnr_T)len
6640 || str + startcol <= regmatch.startp[0])
6641 {
6642 match = FALSE;
6643 break;
6644 }
6645 }
6646 }
6647
6648 if (match)
6649 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006650 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006651 {
6652 listitem_T *li1 = rettv->vval.v_list->lv_first;
6653 listitem_T *li2 = li1->li_next;
6654 listitem_T *li3 = li2->li_next;
6655 listitem_T *li4 = li3->li_next;
6656
6657 vim_free(li1->li_tv.vval.v_string);
6658 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006659 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006660 li3->li_tv.vval.v_number =
6661 (varnumber_T)(regmatch.startp[0] - expr);
6662 li4->li_tv.vval.v_number =
6663 (varnumber_T)(regmatch.endp[0] - expr);
6664 if (l != NULL)
6665 li2->li_tv.vval.v_number = (varnumber_T)idx;
6666 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006667 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006668 {
6669 int i;
6670
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006671 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006672 for (i = 0; i < NSUBEXP; ++i)
6673 {
6674 if (regmatch.endp[i] == NULL)
6675 {
6676 if (list_append_string(rettv->vval.v_list,
6677 (char_u *)"", 0) == FAIL)
6678 break;
6679 }
6680 else if (list_append_string(rettv->vval.v_list,
6681 regmatch.startp[i],
6682 (int)(regmatch.endp[i] - regmatch.startp[i]))
6683 == FAIL)
6684 break;
6685 }
6686 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006687 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006688 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006689 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006690 if (l != NULL)
6691 copy_tv(&li->li_tv, rettv);
6692 else
6693 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006694 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006695 }
6696 else if (l != NULL)
6697 rettv->vval.v_number = idx;
6698 else
6699 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006700 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006701 rettv->vval.v_number =
6702 (varnumber_T)(regmatch.startp[0] - str);
6703 else
6704 rettv->vval.v_number =
6705 (varnumber_T)(regmatch.endp[0] - str);
6706 rettv->vval.v_number += (varnumber_T)(str - expr);
6707 }
6708 }
6709 vim_regfree(regmatch.regprog);
6710 }
6711
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006712theend:
6713 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006714 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006715 listitem_remove(rettv->vval.v_list,
6716 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006717 vim_free(tofree);
6718 p_cpo = save_cpo;
6719}
6720
6721/*
6722 * "match()" function
6723 */
6724 static void
6725f_match(typval_T *argvars, typval_T *rettv)
6726{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006727 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006728}
6729
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006730/*
6731 * "matchend()" function
6732 */
6733 static void
6734f_matchend(typval_T *argvars, typval_T *rettv)
6735{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006736 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006737}
6738
6739/*
6740 * "matchlist()" function
6741 */
6742 static void
6743f_matchlist(typval_T *argvars, typval_T *rettv)
6744{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006745 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006746}
6747
6748/*
6749 * "matchstr()" function
6750 */
6751 static void
6752f_matchstr(typval_T *argvars, typval_T *rettv)
6753{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006754 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006755}
6756
6757/*
6758 * "matchstrpos()" function
6759 */
6760 static void
6761f_matchstrpos(typval_T *argvars, typval_T *rettv)
6762{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006763 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006764}
6765
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006766 static void
6767max_min(typval_T *argvars, typval_T *rettv, int domax)
6768{
6769 varnumber_T n = 0;
6770 varnumber_T i;
6771 int error = FALSE;
6772
6773 if (argvars[0].v_type == VAR_LIST)
6774 {
6775 list_T *l;
6776 listitem_T *li;
6777
6778 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006779 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006780 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006781 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006782 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006783 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6784 n = l->lv_u.nonmat.lv_start;
6785 else
6786 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6787 * l->lv_u.nonmat.lv_stride;
6788 }
6789 else
6790 {
6791 li = l->lv_first;
6792 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006793 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006794 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006795 if (error)
6796 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006797 for (;;)
6798 {
6799 li = li->li_next;
6800 if (li == NULL)
6801 break;
6802 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006803 if (error)
6804 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006805 if (domax ? i > n : i < n)
6806 n = i;
6807 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006808 }
6809 }
6810 }
6811 }
6812 else if (argvars[0].v_type == VAR_DICT)
6813 {
6814 dict_T *d;
6815 int first = TRUE;
6816 hashitem_T *hi;
6817 int todo;
6818
6819 d = argvars[0].vval.v_dict;
6820 if (d != NULL)
6821 {
6822 todo = (int)d->dv_hashtab.ht_used;
6823 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6824 {
6825 if (!HASHITEM_EMPTY(hi))
6826 {
6827 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006828 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006829 if (error)
6830 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006831 if (first)
6832 {
6833 n = i;
6834 first = FALSE;
6835 }
6836 else if (domax ? i > n : i < n)
6837 n = i;
6838 }
6839 }
6840 }
6841 }
6842 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006843 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006844
6845 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006846}
6847
6848/*
6849 * "max()" function
6850 */
6851 static void
6852f_max(typval_T *argvars, typval_T *rettv)
6853{
6854 max_min(argvars, rettv, TRUE);
6855}
6856
6857/*
6858 * "min()" function
6859 */
6860 static void
6861f_min(typval_T *argvars, typval_T *rettv)
6862{
6863 max_min(argvars, rettv, FALSE);
6864}
6865
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006866#if defined(FEAT_MZSCHEME) || defined(PROTO)
6867/*
6868 * "mzeval()" function
6869 */
6870 static void
6871f_mzeval(typval_T *argvars, typval_T *rettv)
6872{
6873 char_u *str;
6874 char_u buf[NUMBUFLEN];
6875
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006876 if (check_restricted() || check_secure())
6877 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006878 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006879 do_mzeval(str, rettv);
6880}
6881
6882 void
6883mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6884{
6885 typval_T argvars[3];
6886
6887 argvars[0].v_type = VAR_STRING;
6888 argvars[0].vval.v_string = name;
6889 copy_tv(args, &argvars[1]);
6890 argvars[2].v_type = VAR_UNKNOWN;
6891 f_call(argvars, rettv);
6892 clear_tv(&argvars[1]);
6893}
6894#endif
6895
6896/*
6897 * "nextnonblank()" function
6898 */
6899 static void
6900f_nextnonblank(typval_T *argvars, typval_T *rettv)
6901{
6902 linenr_T lnum;
6903
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006904 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006905 {
6906 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6907 {
6908 lnum = 0;
6909 break;
6910 }
6911 if (*skipwhite(ml_get(lnum)) != NUL)
6912 break;
6913 }
6914 rettv->vval.v_number = lnum;
6915}
6916
6917/*
6918 * "nr2char()" function
6919 */
6920 static void
6921f_nr2char(typval_T *argvars, typval_T *rettv)
6922{
6923 char_u buf[NUMBUFLEN];
6924
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006925 if (has_mbyte)
6926 {
6927 int utf8 = 0;
6928
6929 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006930 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006931 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006932 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006933 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006934 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006935 }
6936 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006937 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006938 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006939 buf[1] = NUL;
6940 }
6941 rettv->v_type = VAR_STRING;
6942 rettv->vval.v_string = vim_strsave(buf);
6943}
6944
6945/*
6946 * "or(expr, expr)" function
6947 */
6948 static void
6949f_or(typval_T *argvars, typval_T *rettv)
6950{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006951 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6952 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006953}
6954
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006955#ifdef FEAT_PERL
6956/*
6957 * "perleval()" function
6958 */
6959 static void
6960f_perleval(typval_T *argvars, typval_T *rettv)
6961{
6962 char_u *str;
6963 char_u buf[NUMBUFLEN];
6964
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006965 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006966 do_perleval(str, rettv);
6967}
6968#endif
6969
6970#ifdef FEAT_FLOAT
6971/*
6972 * "pow()" function
6973 */
6974 static void
6975f_pow(typval_T *argvars, typval_T *rettv)
6976{
6977 float_T fx = 0.0, fy = 0.0;
6978
6979 rettv->v_type = VAR_FLOAT;
6980 if (get_float_arg(argvars, &fx) == OK
6981 && get_float_arg(&argvars[1], &fy) == OK)
6982 rettv->vval.v_float = pow(fx, fy);
6983 else
6984 rettv->vval.v_float = 0.0;
6985}
6986#endif
6987
6988/*
6989 * "prevnonblank()" function
6990 */
6991 static void
6992f_prevnonblank(typval_T *argvars, typval_T *rettv)
6993{
6994 linenr_T lnum;
6995
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006996 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006997 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6998 lnum = 0;
6999 else
7000 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
7001 --lnum;
7002 rettv->vval.v_number = lnum;
7003}
7004
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007005// This dummy va_list is here because:
7006// - passing a NULL pointer doesn't work when va_list isn't a pointer
7007// - locally in the function results in a "used before set" warning
7008// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007009static va_list ap;
7010
7011/*
7012 * "printf()" function
7013 */
7014 static void
7015f_printf(typval_T *argvars, typval_T *rettv)
7016{
7017 char_u buf[NUMBUFLEN];
7018 int len;
7019 char_u *s;
7020 int saved_did_emsg = did_emsg;
7021 char *fmt;
7022
7023 rettv->v_type = VAR_STRING;
7024 rettv->vval.v_string = NULL;
7025
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007026 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007027 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007028 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007029 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007030 if (!did_emsg)
7031 {
7032 s = alloc(len + 1);
7033 if (s != NULL)
7034 {
7035 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007036 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
7037 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007038 }
7039 }
7040 did_emsg |= saved_did_emsg;
7041}
7042
7043/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007044 * "pum_getpos()" function
7045 */
7046 static void
7047f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7048{
7049 if (rettv_dict_alloc(rettv) != OK)
7050 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007051 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007052}
7053
7054/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007055 * "pumvisible()" function
7056 */
7057 static void
7058f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7059{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007060 if (pum_visible())
7061 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007062}
7063
7064#ifdef FEAT_PYTHON3
7065/*
7066 * "py3eval()" function
7067 */
7068 static void
7069f_py3eval(typval_T *argvars, typval_T *rettv)
7070{
7071 char_u *str;
7072 char_u buf[NUMBUFLEN];
7073
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007074 if (check_restricted() || check_secure())
7075 return;
7076
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007077 if (p_pyx == 0)
7078 p_pyx = 3;
7079
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007080 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007081 do_py3eval(str, rettv);
7082}
7083#endif
7084
7085#ifdef FEAT_PYTHON
7086/*
7087 * "pyeval()" function
7088 */
7089 static void
7090f_pyeval(typval_T *argvars, typval_T *rettv)
7091{
7092 char_u *str;
7093 char_u buf[NUMBUFLEN];
7094
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007095 if (check_restricted() || check_secure())
7096 return;
7097
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007098 if (p_pyx == 0)
7099 p_pyx = 2;
7100
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007101 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007102 do_pyeval(str, rettv);
7103}
7104#endif
7105
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007106#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
7107/*
7108 * "pyxeval()" function
7109 */
7110 static void
7111f_pyxeval(typval_T *argvars, typval_T *rettv)
7112{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007113 if (check_restricted() || check_secure())
7114 return;
7115
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007116# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
7117 init_pyxversion();
7118 if (p_pyx == 2)
7119 f_pyeval(argvars, rettv);
7120 else
7121 f_py3eval(argvars, rettv);
7122# elif defined(FEAT_PYTHON)
7123 f_pyeval(argvars, rettv);
7124# elif defined(FEAT_PYTHON3)
7125 f_py3eval(argvars, rettv);
7126# endif
7127}
7128#endif
7129
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007130static UINT32_T srand_seed_for_testing = 0;
7131static int srand_seed_for_testing_is_used = FALSE;
7132
7133 static void
7134f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
7135{
7136 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007137 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007138 else
7139 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007140 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
7141 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007142 }
7143}
7144
7145 static void
7146init_srand(UINT32_T *x)
7147{
7148#ifndef MSWIN
7149 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
7150#endif
7151
7152 if (srand_seed_for_testing_is_used)
7153 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007154 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007155 return;
7156 }
7157#ifndef MSWIN
7158 if (dev_urandom_state != FAIL)
7159 {
7160 int fd = open("/dev/urandom", O_RDONLY);
7161 struct {
7162 union {
7163 UINT32_T number;
7164 char bytes[sizeof(UINT32_T)];
7165 } contents;
7166 } buf;
7167
7168 // Attempt reading /dev/urandom.
7169 if (fd == -1)
7170 dev_urandom_state = FAIL;
7171 else
7172 {
7173 buf.contents.number = 0;
7174 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
7175 != sizeof(UINT32_T))
7176 dev_urandom_state = FAIL;
7177 else
7178 {
7179 dev_urandom_state = OK;
7180 *x = buf.contents.number;
7181 }
7182 close(fd);
7183 }
7184 }
7185 if (dev_urandom_state != OK)
7186 // Reading /dev/urandom doesn't work, fall back to time().
7187#endif
7188 *x = vim_time();
7189}
7190
7191#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
7192#define SPLITMIX32(x, z) ( \
7193 z = (x += 0x9e3779b9), \
7194 z = (z ^ (z >> 16)) * 0x85ebca6b, \
7195 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
7196 z ^ (z >> 16) \
7197 )
7198#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
7199 result = ROTL(y * 5, 7) * 9; \
7200 t = y << 9; \
7201 z ^= x; \
7202 w ^= y; \
7203 y ^= z, x ^= w; \
7204 z ^= t; \
7205 w = ROTL(w, 11);
7206
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007207/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007208 * "rand()" function
7209 */
7210 static void
7211f_rand(typval_T *argvars, typval_T *rettv)
7212{
7213 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007214 static UINT32_T gx, gy, gz, gw;
7215 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007216 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01007217 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007218
7219 if (argvars[0].v_type == VAR_UNKNOWN)
7220 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007221 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007222 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007223 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007224 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007225 init_srand(&x);
7226
7227 gx = SPLITMIX32(x, z);
7228 gy = SPLITMIX32(x, z);
7229 gz = SPLITMIX32(x, z);
7230 gw = SPLITMIX32(x, z);
7231 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007232 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007233
7234 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007235 }
7236 else if (argvars[0].v_type == VAR_LIST)
7237 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007238 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007239 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007240 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007241
7242 lx = list_find(l, 0L);
7243 ly = list_find(l, 1L);
7244 lz = list_find(l, 2L);
7245 lw = list_find(l, 3L);
7246 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
7247 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
7248 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
7249 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
7250 x = (UINT32_T)lx->li_tv.vval.v_number;
7251 y = (UINT32_T)ly->li_tv.vval.v_number;
7252 z = (UINT32_T)lz->li_tv.vval.v_number;
7253 w = (UINT32_T)lw->li_tv.vval.v_number;
7254
7255 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
7256
7257 lx->li_tv.vval.v_number = (varnumber_T)x;
7258 ly->li_tv.vval.v_number = (varnumber_T)y;
7259 lz->li_tv.vval.v_number = (varnumber_T)z;
7260 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007261 }
7262 else
7263 goto theend;
7264
7265 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007266 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007267 return;
7268
7269theend:
7270 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007271 rettv->v_type = VAR_NUMBER;
7272 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007273}
7274
7275/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007276 * "srand()" function
7277 */
7278 static void
7279f_srand(typval_T *argvars, typval_T *rettv)
7280{
7281 UINT32_T x = 0, z;
7282
7283 if (rettv_list_alloc(rettv) == FAIL)
7284 return;
7285 if (argvars[0].v_type == VAR_UNKNOWN)
7286 {
7287 init_srand(&x);
7288 }
7289 else
7290 {
7291 int error = FALSE;
7292
7293 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7294 if (error)
7295 return;
7296 }
7297
7298 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7299 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7300 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7301 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7302}
7303
7304#undef ROTL
7305#undef SPLITMIX32
7306#undef SHUFFLE_XOSHIRO128STARSTAR
7307
7308/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007309 * "range()" function
7310 */
7311 static void
7312f_range(typval_T *argvars, typval_T *rettv)
7313{
7314 varnumber_T start;
7315 varnumber_T end;
7316 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007317 int error = FALSE;
7318
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007319 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007320 if (argvars[1].v_type == VAR_UNKNOWN)
7321 {
7322 end = start - 1;
7323 start = 0;
7324 }
7325 else
7326 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007327 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007328 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007329 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007330 }
7331
7332 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007333 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007334 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007335 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007336 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007337 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007338 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007339 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007340 list_T *list = rettv->vval.v_list;
7341
7342 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007343 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 // be called.
7345 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007346 list->lv_u.nonmat.lv_start = start;
7347 list->lv_u.nonmat.lv_end = end;
7348 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007349 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007350 }
7351}
7352
7353/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007354 * Materialize "list".
7355 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356 */
7357 void
7358range_list_materialize(list_T *list)
7359{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007360 varnumber_T start = list->lv_u.nonmat.lv_start;
7361 varnumber_T end = list->lv_u.nonmat.lv_end;
7362 int stride = list->lv_u.nonmat.lv_stride;
7363 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007364
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007365 list->lv_first = NULL;
7366 list->lv_u.mat.lv_last = NULL;
7367 list->lv_len = 0;
7368 list->lv_u.mat.lv_idx_item = NULL;
7369 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7370 if (list_append_number(list, (varnumber_T)i) == FAIL)
7371 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007372}
7373
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007374/*
7375 * "getreginfo()" function
7376 */
7377 static void
7378f_getreginfo(typval_T *argvars, typval_T *rettv)
7379{
7380 char_u *strregname;
7381 int regname;
7382 char_u buf[NUMBUFLEN + 2];
7383 long reglen = 0;
7384 dict_T *dict;
7385 list_T *list;
7386
7387 if (argvars[0].v_type != VAR_UNKNOWN)
7388 {
7389 strregname = tv_get_string_chk(&argvars[0]);
7390 if (strregname == NULL)
7391 return;
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007392 if (in_vim9script() && STRLEN(strregname) > 1)
7393 {
7394 semsg(_(e_register_name_must_be_one_char_str), strregname);
7395 return;
7396 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007397 }
7398 else
7399 strregname = get_vim_var_str(VV_REG);
7400
7401 regname = (strregname == NULL ? '"' : *strregname);
7402 if (regname == 0 || regname == '@')
7403 regname = '"';
7404
7405 if (rettv_dict_alloc(rettv) == FAIL)
7406 return;
7407 dict = rettv->vval.v_dict;
7408
7409 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7410 if (list == NULL)
7411 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007412 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007413
7414 buf[0] = NUL;
7415 buf[1] = NUL;
7416 switch (get_reg_type(regname, &reglen))
7417 {
7418 case MLINE: buf[0] = 'V'; break;
7419 case MCHAR: buf[0] = 'v'; break;
7420 case MBLOCK:
7421 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7422 reglen + 1);
7423 break;
7424 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007425 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007426
7427 buf[0] = get_register_name(get_unname_register());
7428 buf[1] = NUL;
7429 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007430 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007431 else
7432 {
7433 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7434
7435 if (item != NULL)
7436 {
7437 item->di_tv.v_type = VAR_SPECIAL;
7438 item->di_tv.vval.v_number = regname == buf[0]
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007439 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007440 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007441 }
7442 }
7443}
7444
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007445 static void
7446return_register(int regname, typval_T *rettv)
7447{
7448 char_u buf[2] = {0, 0};
7449
7450 buf[0] = (char_u)regname;
7451 rettv->v_type = VAR_STRING;
7452 rettv->vval.v_string = vim_strsave(buf);
7453}
7454
7455/*
7456 * "reg_executing()" function
7457 */
7458 static void
7459f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7460{
7461 return_register(reg_executing, rettv);
7462}
7463
7464/*
7465 * "reg_recording()" function
7466 */
7467 static void
7468f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7469{
7470 return_register(reg_recording, rettv);
7471}
7472
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007473/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007474 * "rename({from}, {to})" function
7475 */
7476 static void
7477f_rename(typval_T *argvars, typval_T *rettv)
7478{
7479 char_u buf[NUMBUFLEN];
7480
7481 if (check_restricted() || check_secure())
7482 rettv->vval.v_number = -1;
7483 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007484 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7485 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007486}
7487
7488/*
7489 * "repeat()" function
7490 */
7491 static void
7492f_repeat(typval_T *argvars, typval_T *rettv)
7493{
7494 char_u *p;
7495 int n;
7496 int slen;
7497 int len;
7498 char_u *r;
7499 int i;
7500
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007501 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007502 if (argvars[0].v_type == VAR_LIST)
7503 {
7504 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7505 while (n-- > 0)
7506 if (list_extend(rettv->vval.v_list,
7507 argvars[0].vval.v_list, NULL) == FAIL)
7508 break;
7509 }
7510 else
7511 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007512 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007513 rettv->v_type = VAR_STRING;
7514 rettv->vval.v_string = NULL;
7515
7516 slen = (int)STRLEN(p);
7517 len = slen * n;
7518 if (len <= 0)
7519 return;
7520
7521 r = alloc(len + 1);
7522 if (r != NULL)
7523 {
7524 for (i = 0; i < n; i++)
7525 mch_memmove(r + i * slen, p, (size_t)slen);
7526 r[len] = NUL;
7527 }
7528
7529 rettv->vval.v_string = r;
7530 }
7531}
7532
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007533#define SP_NOMOVE 0x01 // don't move cursor
7534#define SP_REPEAT 0x02 // repeat to find outer pair
7535#define SP_RETCOUNT 0x04 // return matchcount
7536#define SP_SETPCMARK 0x08 // set previous context mark
7537#define SP_START 0x10 // accept match at start position
7538#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7539#define SP_END 0x40 // leave cursor at end of match
7540#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007541
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007542/*
7543 * Get flags for a search function.
7544 * Possibly sets "p_ws".
7545 * Returns BACKWARD, FORWARD or zero (for an error).
7546 */
7547 static int
7548get_search_arg(typval_T *varp, int *flagsp)
7549{
7550 int dir = FORWARD;
7551 char_u *flags;
7552 char_u nbuf[NUMBUFLEN];
7553 int mask;
7554
7555 if (varp->v_type != VAR_UNKNOWN)
7556 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007557 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007558 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007559 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007560 while (*flags != NUL)
7561 {
7562 switch (*flags)
7563 {
7564 case 'b': dir = BACKWARD; break;
7565 case 'w': p_ws = TRUE; break;
7566 case 'W': p_ws = FALSE; break;
7567 default: mask = 0;
7568 if (flagsp != NULL)
7569 switch (*flags)
7570 {
7571 case 'c': mask = SP_START; break;
7572 case 'e': mask = SP_END; break;
7573 case 'm': mask = SP_RETCOUNT; break;
7574 case 'n': mask = SP_NOMOVE; break;
7575 case 'p': mask = SP_SUBPAT; break;
7576 case 'r': mask = SP_REPEAT; break;
7577 case 's': mask = SP_SETPCMARK; break;
7578 case 'z': mask = SP_COLUMN; break;
7579 }
7580 if (mask == 0)
7581 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007582 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007583 dir = 0;
7584 }
7585 else
7586 *flagsp |= mask;
7587 }
7588 if (dir == 0)
7589 break;
7590 ++flags;
7591 }
7592 }
7593 return dir;
7594}
7595
7596/*
7597 * Shared by search() and searchpos() functions.
7598 */
7599 static int
7600search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7601{
7602 int flags;
7603 char_u *pat;
7604 pos_T pos;
7605 pos_T save_cursor;
7606 int save_p_ws = p_ws;
7607 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007608 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007609 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007610#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007611 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007612 long time_limit = 0;
7613#endif
7614 int options = SEARCH_KEEP;
7615 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007616 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007617 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007618 pos_T firstpos;
7619
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007620 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007621 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007622 if (dir == 0)
7623 goto theend;
7624 flags = *flagsp;
7625 if (flags & SP_START)
7626 options |= SEARCH_START;
7627 if (flags & SP_END)
7628 options |= SEARCH_END;
7629 if (flags & SP_COLUMN)
7630 options |= SEARCH_COL;
7631
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007632 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007633 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7634 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007635 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007636 if (lnum_stop < 0)
7637 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007638 if (argvars[3].v_type != VAR_UNKNOWN)
7639 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007640#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007641 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007642 if (time_limit < 0)
7643 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007644#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007645 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007646 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007647 }
7648
7649#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007650 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007651 profile_setlimit(time_limit, &tm);
7652#endif
7653
7654 /*
7655 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7656 * Check to make sure only those flags are set.
7657 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7658 * flags cannot be set. Check for that condition also.
7659 */
7660 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7661 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7662 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007663 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007664 goto theend;
7665 }
7666
7667 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007668 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007669 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007670 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7671#ifdef FEAT_RELTIME
7672 sia.sa_tm = &tm;
7673#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007674
7675 // Repeat until {skip} returns FALSE.
7676 for (;;)
7677 {
7678 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007679 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007680 // finding the first match again means there is no match where {skip}
7681 // evaluates to zero.
7682 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7683 subpatnum = FAIL;
7684
Bram Moolenaara9c01042020-06-07 14:50:50 +02007685 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007686 // didn't find it or no skip argument
7687 break;
7688 firstpos = pos;
7689
Bram Moolenaara9c01042020-06-07 14:50:50 +02007690 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007691 {
7692 int do_skip;
7693 int err;
7694 pos_T save_pos = curwin->w_cursor;
7695
7696 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007697 err = FALSE;
7698 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007699 curwin->w_cursor = save_pos;
7700 if (err)
7701 {
7702 // Evaluating {skip} caused an error, break here.
7703 subpatnum = FAIL;
7704 break;
7705 }
7706 if (!do_skip)
7707 break;
7708 }
7709 }
7710
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007711 if (subpatnum != FAIL)
7712 {
7713 if (flags & SP_SUBPAT)
7714 retval = subpatnum;
7715 else
7716 retval = pos.lnum;
7717 if (flags & SP_SETPCMARK)
7718 setpcmark();
7719 curwin->w_cursor = pos;
7720 if (match_pos != NULL)
7721 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007722 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007723 match_pos->lnum = pos.lnum;
7724 match_pos->col = pos.col + 1;
7725 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007726 // "/$" will put the cursor after the end of the line, may need to
7727 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007728 check_cursor();
7729 }
7730
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007731 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007732 if (flags & SP_NOMOVE)
7733 curwin->w_cursor = save_cursor;
7734 else
7735 curwin->w_set_curswant = TRUE;
7736theend:
7737 p_ws = save_p_ws;
7738
7739 return retval;
7740}
7741
7742#ifdef FEAT_FLOAT
7743
7744/*
7745 * round() is not in C90, use ceil() or floor() instead.
7746 */
7747 float_T
7748vim_round(float_T f)
7749{
7750 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
7751}
7752
7753/*
7754 * "round({float})" function
7755 */
7756 static void
7757f_round(typval_T *argvars, typval_T *rettv)
7758{
7759 float_T f = 0.0;
7760
7761 rettv->v_type = VAR_FLOAT;
7762 if (get_float_arg(argvars, &f) == OK)
7763 rettv->vval.v_float = vim_round(f);
7764 else
7765 rettv->vval.v_float = 0.0;
7766}
7767#endif
7768
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007769#ifdef FEAT_RUBY
7770/*
7771 * "rubyeval()" function
7772 */
7773 static void
7774f_rubyeval(typval_T *argvars, typval_T *rettv)
7775{
7776 char_u *str;
7777 char_u buf[NUMBUFLEN];
7778
7779 str = tv_get_string_buf(&argvars[0], buf);
7780 do_rubyeval(str, rettv);
7781}
7782#endif
7783
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007784/*
7785 * "screenattr()" function
7786 */
7787 static void
7788f_screenattr(typval_T *argvars, typval_T *rettv)
7789{
7790 int row;
7791 int col;
7792 int c;
7793
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007794 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7795 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007796 if (row < 0 || row >= screen_Rows
7797 || col < 0 || col >= screen_Columns)
7798 c = -1;
7799 else
7800 c = ScreenAttrs[LineOffset[row] + col];
7801 rettv->vval.v_number = c;
7802}
7803
7804/*
7805 * "screenchar()" function
7806 */
7807 static void
7808f_screenchar(typval_T *argvars, typval_T *rettv)
7809{
7810 int row;
7811 int col;
7812 int off;
7813 int c;
7814
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007815 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7816 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007817 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007818 c = -1;
7819 else
7820 {
7821 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007822 if (enc_utf8 && ScreenLinesUC[off] != 0)
7823 c = ScreenLinesUC[off];
7824 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007825 c = ScreenLines[off];
7826 }
7827 rettv->vval.v_number = c;
7828}
7829
7830/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007831 * "screenchars()" function
7832 */
7833 static void
7834f_screenchars(typval_T *argvars, typval_T *rettv)
7835{
7836 int row;
7837 int col;
7838 int off;
7839 int c;
7840 int i;
7841
7842 if (rettv_list_alloc(rettv) == FAIL)
7843 return;
7844 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7845 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7846 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7847 return;
7848
7849 off = LineOffset[row] + col;
7850 if (enc_utf8 && ScreenLinesUC[off] != 0)
7851 c = ScreenLinesUC[off];
7852 else
7853 c = ScreenLines[off];
7854 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7855
7856 if (enc_utf8)
7857
7858 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7859 list_append_number(rettv->vval.v_list,
7860 (varnumber_T)ScreenLinesC[i][off]);
7861}
7862
7863/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007864 * "screencol()" function
7865 *
7866 * First column is 1 to be consistent with virtcol().
7867 */
7868 static void
7869f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7870{
7871 rettv->vval.v_number = screen_screencol() + 1;
7872}
7873
7874/*
7875 * "screenrow()" function
7876 */
7877 static void
7878f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7879{
7880 rettv->vval.v_number = screen_screenrow() + 1;
7881}
7882
7883/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007884 * "screenstring()" function
7885 */
7886 static void
7887f_screenstring(typval_T *argvars, typval_T *rettv)
7888{
7889 int row;
7890 int col;
7891 int off;
7892 int c;
7893 int i;
7894 char_u buf[MB_MAXBYTES + 1];
7895 int buflen = 0;
7896
7897 rettv->vval.v_string = NULL;
7898 rettv->v_type = VAR_STRING;
7899
7900 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7901 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7902 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7903 return;
7904
7905 off = LineOffset[row] + col;
7906 if (enc_utf8 && ScreenLinesUC[off] != 0)
7907 c = ScreenLinesUC[off];
7908 else
7909 c = ScreenLines[off];
7910 buflen += mb_char2bytes(c, buf);
7911
Bram Moolenaarf1387282021-03-22 17:11:15 +01007912 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007913 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7914 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7915
7916 buf[buflen] = NUL;
7917 rettv->vval.v_string = vim_strsave(buf);
7918}
7919
7920/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007921 * "search()" function
7922 */
7923 static void
7924f_search(typval_T *argvars, typval_T *rettv)
7925{
7926 int flags = 0;
7927
7928 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7929}
7930
7931/*
7932 * "searchdecl()" function
7933 */
7934 static void
7935f_searchdecl(typval_T *argvars, typval_T *rettv)
7936{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007937 int locally = TRUE;
7938 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007939 int error = FALSE;
7940 char_u *name;
7941
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007942 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007943
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007944 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007945 if (argvars[1].v_type != VAR_UNKNOWN)
7946 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007947 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007948 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007949 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007950 }
7951 if (!error && name != NULL)
7952 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7953 locally, thisblock, SEARCH_KEEP) == FAIL;
7954}
7955
7956/*
7957 * Used by searchpair() and searchpairpos()
7958 */
7959 static int
7960searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7961{
7962 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007963 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007964 int save_p_ws = p_ws;
7965 int dir;
7966 int flags = 0;
7967 char_u nbuf1[NUMBUFLEN];
7968 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007969 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007970 long lnum_stop = 0;
7971 long time_limit = 0;
7972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007973 // Get the three pattern arguments: start, middle, end. Will result in an
7974 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007975 spat = tv_get_string_chk(&argvars[0]);
7976 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7977 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007978 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007979 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007980
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007981 // Handle the optional fourth argument: flags
7982 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007983 if (dir == 0)
7984 goto theend;
7985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007986 // Don't accept SP_END or SP_SUBPAT.
7987 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007988 if ((flags & (SP_END | SP_SUBPAT)) != 0
7989 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7990 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007991 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007992 goto theend;
7993 }
7994
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007995 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007996 if (flags & SP_REPEAT)
7997 p_ws = FALSE;
7998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007999 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008000 if (argvars[3].v_type == VAR_UNKNOWN
8001 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01008002 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008003 else
8004 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02008005 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01008006 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02008007
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008008 if (argvars[5].v_type != VAR_UNKNOWN)
8009 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008010 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008011 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008012 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008013 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008014 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008015 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008016#ifdef FEAT_RELTIME
8017 if (argvars[6].v_type != VAR_UNKNOWN)
8018 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008019 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008020 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008022 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008023 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008024 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008025 }
8026#endif
8027 }
8028 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008029
8030 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
8031 match_pos, lnum_stop, time_limit);
8032
8033theend:
8034 p_ws = save_p_ws;
8035
8036 return retval;
8037}
8038
8039/*
8040 * "searchpair()" function
8041 */
8042 static void
8043f_searchpair(typval_T *argvars, typval_T *rettv)
8044{
8045 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
8046}
8047
8048/*
8049 * "searchpairpos()" function
8050 */
8051 static void
8052f_searchpairpos(typval_T *argvars, typval_T *rettv)
8053{
8054 pos_T match_pos;
8055 int lnum = 0;
8056 int col = 0;
8057
8058 if (rettv_list_alloc(rettv) == FAIL)
8059 return;
8060
8061 if (searchpair_cmn(argvars, &match_pos) > 0)
8062 {
8063 lnum = match_pos.lnum;
8064 col = match_pos.col;
8065 }
8066
8067 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8068 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8069}
8070
8071/*
8072 * Search for a start/middle/end thing.
8073 * Used by searchpair(), see its documentation for the details.
8074 * Returns 0 or -1 for no match,
8075 */
8076 long
8077do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008078 char_u *spat, // start pattern
8079 char_u *mpat, // middle pattern
8080 char_u *epat, // end pattern
8081 int dir, // BACKWARD or FORWARD
8082 typval_T *skip, // skip expression
8083 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008084 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008085 linenr_T lnum_stop, // stop at this line if not zero
8086 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008087{
8088 char_u *save_cpo;
8089 char_u *pat, *pat2 = NULL, *pat3 = NULL;
8090 long retval = 0;
8091 pos_T pos;
8092 pos_T firstpos;
8093 pos_T foundpos;
8094 pos_T save_cursor;
8095 pos_T save_pos;
8096 int n;
8097 int r;
8098 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01008099 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008100 int err;
8101 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008102#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008103 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008104#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008105
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008106 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008107 save_cpo = p_cpo;
8108 p_cpo = empty_option;
8109
8110#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008111 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008112 profile_setlimit(time_limit, &tm);
8113#endif
8114
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008115 // Make two search patterns: start/end (pat2, for in nested pairs) and
8116 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02008117 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
8118 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008119 if (pat2 == NULL || pat3 == NULL)
8120 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008121 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008122 if (*mpat == NUL)
8123 STRCPY(pat3, pat2);
8124 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008125 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008126 spat, epat, mpat);
8127 if (flags & SP_START)
8128 options |= SEARCH_START;
8129
Bram Moolenaar48570482017-10-30 21:48:41 +01008130 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02008131 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01008132
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008133 save_cursor = curwin->w_cursor;
8134 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008135 CLEAR_POS(&firstpos);
8136 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008137 pat = pat3;
8138 for (;;)
8139 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008140 searchit_arg_T sia;
8141
Bram Moolenaara80faa82020-04-12 19:37:17 +02008142 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008143 sia.sa_stop_lnum = lnum_stop;
8144#ifdef FEAT_RELTIME
8145 sia.sa_tm = &tm;
8146#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01008147 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008148 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008149 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008150 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008151 break;
8152
8153 if (firstpos.lnum == 0)
8154 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008155 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008156 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008157 // Found the same position again. Can happen with a pattern that
8158 // has "\zs" at the end and searching backwards. Advance one
8159 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008160 if (dir == BACKWARD)
8161 decl(&pos);
8162 else
8163 incl(&pos);
8164 }
8165 foundpos = pos;
8166
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008167 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008168 options &= ~SEARCH_START;
8169
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008170 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01008171 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008172 {
8173 save_pos = curwin->w_cursor;
8174 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01008175 err = FALSE;
8176 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008177 curwin->w_cursor = save_pos;
8178 if (err)
8179 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008180 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008181 curwin->w_cursor = save_cursor;
8182 retval = -1;
8183 break;
8184 }
8185 if (r)
8186 continue;
8187 }
8188
8189 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8190 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008191 // Found end when searching backwards or start when searching
8192 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008193 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008194 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008195 }
8196 else
8197 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008198 // Found end when searching forward or start when searching
8199 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008200 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008201 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008202 }
8203
8204 if (nest == 0)
8205 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008206 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008207 if (flags & SP_RETCOUNT)
8208 ++retval;
8209 else
8210 retval = pos.lnum;
8211 if (flags & SP_SETPCMARK)
8212 setpcmark();
8213 curwin->w_cursor = pos;
8214 if (!(flags & SP_REPEAT))
8215 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008216 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008217 }
8218 }
8219
8220 if (match_pos != NULL)
8221 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008222 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008223 match_pos->lnum = curwin->w_cursor.lnum;
8224 match_pos->col = curwin->w_cursor.col + 1;
8225 }
8226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008227 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008228 if ((flags & SP_NOMOVE) || retval == 0)
8229 curwin->w_cursor = save_cursor;
8230
8231theend:
8232 vim_free(pat2);
8233 vim_free(pat3);
8234 if (p_cpo == empty_option)
8235 p_cpo = save_cpo;
8236 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008237 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008238 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008239 // If it's still empty it was changed and restored, need to restore in
8240 // the complicated way.
8241 if (*p_cpo == NUL)
8242 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008243 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008244 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008245
8246 return retval;
8247}
8248
8249/*
8250 * "searchpos()" function
8251 */
8252 static void
8253f_searchpos(typval_T *argvars, typval_T *rettv)
8254{
8255 pos_T match_pos;
8256 int lnum = 0;
8257 int col = 0;
8258 int n;
8259 int flags = 0;
8260
8261 if (rettv_list_alloc(rettv) == FAIL)
8262 return;
8263
8264 n = search_cmn(argvars, &match_pos, &flags);
8265 if (n > 0)
8266 {
8267 lnum = match_pos.lnum;
8268 col = match_pos.col;
8269 }
8270
8271 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8272 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8273 if (flags & SP_SUBPAT)
8274 list_append_number(rettv->vval.v_list, (varnumber_T)n);
8275}
8276
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008277/*
8278 * Set the cursor or mark position.
8279 * If 'charpos' is TRUE, then use the column number as a character offet.
8280 * Otherwise use the column number as a byte offset.
8281 */
8282 static void
8283set_position(typval_T *argvars, typval_T *rettv, int charpos)
8284{
8285 pos_T pos;
8286 int fnum;
8287 char_u *name;
8288 colnr_T curswant = -1;
8289
8290 rettv->vval.v_number = -1;
8291
8292 name = tv_get_string_chk(argvars);
8293 if (name != NULL)
8294 {
8295 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
8296 {
8297 if (pos.col != MAXCOL && --pos.col < 0)
8298 pos.col = 0;
8299 if ((name[0] == '.' && name[1] == NUL))
8300 {
8301 // set cursor; "fnum" is ignored
8302 curwin->w_cursor = pos;
8303 if (curswant >= 0)
8304 {
8305 curwin->w_curswant = curswant - 1;
8306 curwin->w_set_curswant = FALSE;
8307 }
8308 check_cursor();
8309 rettv->vval.v_number = 0;
8310 }
8311 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8312 {
8313 // set mark
8314 if (setmark_pos(name[1], &pos, fnum) == OK)
8315 rettv->vval.v_number = 0;
8316 }
8317 else
8318 emsg(_(e_invarg));
8319 }
8320 }
8321}
8322/*
8323 * "setcharpos()" function
8324 */
8325 static void
8326f_setcharpos(typval_T *argvars, typval_T *rettv)
8327{
8328 set_position(argvars, rettv, TRUE);
8329}
8330
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008331 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008332f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8333{
8334 dict_T *d;
8335 dictitem_T *di;
8336 char_u *csearch;
8337
8338 if (argvars[0].v_type != VAR_DICT)
8339 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008340 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008341 return;
8342 }
8343
8344 if ((d = argvars[0].vval.v_dict) != NULL)
8345 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008346 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008347 if (csearch != NULL)
8348 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008349 if (enc_utf8)
8350 {
8351 int pcc[MAX_MCO];
8352 int c = utfc_ptr2char(csearch, pcc);
8353
8354 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8355 }
8356 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008357 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008358 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008359 }
8360
8361 di = dict_find(d, (char_u *)"forward", -1);
8362 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008363 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008364 ? FORWARD : BACKWARD);
8365
8366 di = dict_find(d, (char_u *)"until", -1);
8367 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008368 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008369 }
8370}
8371
8372/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008373 * "setcursorcharpos" function
8374 */
8375 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008376f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008377{
8378 set_cursorpos(argvars, rettv, TRUE);
8379}
8380
8381/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008382 * "setenv()" function
8383 */
8384 static void
8385f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8386{
8387 char_u namebuf[NUMBUFLEN];
8388 char_u valbuf[NUMBUFLEN];
8389 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8390
8391 if (argvars[1].v_type == VAR_SPECIAL
8392 && argvars[1].vval.v_number == VVAL_NULL)
8393 vim_unsetenv(name);
8394 else
8395 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8396}
8397
8398/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008399 * "setfperm({fname}, {mode})" function
8400 */
8401 static void
8402f_setfperm(typval_T *argvars, typval_T *rettv)
8403{
8404 char_u *fname;
8405 char_u modebuf[NUMBUFLEN];
8406 char_u *mode_str;
8407 int i;
8408 int mask;
8409 int mode = 0;
8410
8411 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008412 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413 if (fname == NULL)
8414 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008415 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008416 if (mode_str == NULL)
8417 return;
8418 if (STRLEN(mode_str) != 9)
8419 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008420 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008421 return;
8422 }
8423
8424 mask = 1;
8425 for (i = 8; i >= 0; --i)
8426 {
8427 if (mode_str[i] != '-')
8428 mode |= mask;
8429 mask = mask << 1;
8430 }
8431 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8432}
8433
8434/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008435 * "setpos()" function
8436 */
8437 static void
8438f_setpos(typval_T *argvars, typval_T *rettv)
8439{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008440 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008441}
8442
8443/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008444 * Translate a register type string to the yank type and block length
8445 */
8446 static int
8447get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8448{
8449 char_u *stropt = *pp;
8450 switch (*stropt)
8451 {
8452 case 'v': case 'c': // character-wise selection
8453 *yank_type = MCHAR;
8454 break;
8455 case 'V': case 'l': // line-wise selection
8456 *yank_type = MLINE;
8457 break;
8458 case 'b': case Ctrl_V: // block-wise selection
8459 *yank_type = MBLOCK;
8460 if (VIM_ISDIGIT(stropt[1]))
8461 {
8462 ++stropt;
8463 *block_len = getdigits(&stropt) - 1;
8464 --stropt;
8465 }
8466 break;
8467 default:
8468 return FAIL;
8469 }
8470 *pp = stropt;
8471 return OK;
8472}
8473
8474/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008475 * "setreg()" function
8476 */
8477 static void
8478f_setreg(typval_T *argvars, typval_T *rettv)
8479{
8480 int regname;
8481 char_u *strregname;
8482 char_u *stropt;
8483 char_u *strval;
8484 int append;
8485 char_u yank_type;
8486 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008487 typval_T *regcontents;
8488 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008489
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008490 pointreg = 0;
8491 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008492 block_len = -1;
8493 yank_type = MAUTO;
8494 append = FALSE;
8495
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008496 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008497 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008498
8499 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008500 return; // type error; errmsg already given
Bram Moolenaar418a29f2021-02-10 22:23:41 +01008501 if (in_vim9script() && STRLEN(strregname) > 1)
8502 {
8503 semsg(_(e_register_name_must_be_one_char_str), strregname);
8504 return;
8505 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008506 regname = *strregname;
8507 if (regname == 0 || regname == '@')
8508 regname = '"';
8509
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008510 if (argvars[1].v_type == VAR_DICT)
8511 {
8512 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008513 dictitem_T *di;
8514
8515 if (d == NULL || d->dv_hashtab.ht_used == 0)
8516 {
8517 // Empty dict, clear the register (like setreg(0, []))
8518 char_u *lstval[2] = {NULL, NULL};
8519 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8520 return;
8521 }
8522
8523 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008524 if (di != NULL)
8525 regcontents = &di->di_tv;
8526
8527 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8528 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008529 {
8530 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8531
8532 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008533 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008534 semsg(_(e_invargval), "value");
8535 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008536 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008537 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008538
8539 if (regname == '"')
8540 {
8541 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8542 if (stropt != NULL)
8543 {
8544 pointreg = *stropt;
8545 regname = pointreg;
8546 }
8547 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008548 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008549 pointreg = regname;
8550 }
8551 else
8552 regcontents = &argvars[1];
8553
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008554 if (argvars[2].v_type != VAR_UNKNOWN)
8555 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008556 if (yank_type != MAUTO)
8557 {
8558 semsg(_(e_toomanyarg), "setreg");
8559 return;
8560 }
8561
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008562 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008563 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008564 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008565 for (; *stropt != NUL; ++stropt)
8566 switch (*stropt)
8567 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008568 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008569 append = TRUE;
8570 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008571 default:
8572 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008573 }
8574 }
8575
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008576 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008577 {
8578 char_u **lstval;
8579 char_u **allocval;
8580 char_u buf[NUMBUFLEN];
8581 char_u **curval;
8582 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008583 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008584 listitem_T *li;
8585 int len;
8586
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008587 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008588 len = ll == NULL ? 0 : ll->lv_len;
8589
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008590 // First half: use for pointers to result lines; second half: use for
8591 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008592 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008593 if (lstval == NULL)
8594 return;
8595 curval = lstval;
8596 allocval = lstval + len + 2;
8597 curallocval = allocval;
8598
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008599 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008600 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008601 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008602 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008603 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008604 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008605 if (strval == NULL)
8606 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008607 if (strval == buf)
8608 {
8609 // Need to make a copy, next tv_get_string_buf_chk() will
8610 // overwrite the string.
8611 strval = vim_strsave(buf);
8612 if (strval == NULL)
8613 goto free_lstval;
8614 *curallocval++ = strval;
8615 }
8616 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008617 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008618 }
8619 *curval++ = NULL;
8620
8621 write_reg_contents_lst(regname, lstval, -1,
8622 append, yank_type, block_len);
8623free_lstval:
8624 while (curallocval > allocval)
8625 vim_free(*--curallocval);
8626 vim_free(lstval);
8627 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008628 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008629 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008630 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008631 if (strval == NULL)
8632 return;
8633 write_reg_contents_ex(regname, strval, -1,
8634 append, yank_type, block_len);
8635 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008636 if (pointreg != 0)
8637 get_yank_register(pointreg, TRUE);
8638
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008639 rettv->vval.v_number = 0;
8640}
8641
8642/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008643 * "settagstack()" function
8644 */
8645 static void
8646f_settagstack(typval_T *argvars, typval_T *rettv)
8647{
8648 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8649 win_T *wp;
8650 dict_T *d;
8651 int action = 'r';
8652
8653 rettv->vval.v_number = -1;
8654
8655 // first argument: window number or id
8656 wp = find_win_by_nr_or_id(&argvars[0]);
8657 if (wp == NULL)
8658 return;
8659
8660 // second argument: dict with items to set in the tag stack
8661 if (argvars[1].v_type != VAR_DICT)
8662 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008663 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008664 return;
8665 }
8666 d = argvars[1].vval.v_dict;
8667 if (d == NULL)
8668 return;
8669
8670 // third argument: action - 'a' for append and 'r' for replace.
8671 // default is to replace the stack.
8672 if (argvars[2].v_type == VAR_UNKNOWN)
8673 action = 'r';
8674 else if (argvars[2].v_type == VAR_STRING)
8675 {
8676 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008677 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008678 if (actstr == NULL)
8679 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008680 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8681 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008682 action = *actstr;
8683 else
8684 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008685 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008686 return;
8687 }
8688 }
8689 else
8690 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008691 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008692 return;
8693 }
8694
8695 if (set_tagstack(wp, d, action) == OK)
8696 rettv->vval.v_number = 0;
8697}
8698
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008699#ifdef FEAT_CRYPT
8700/*
8701 * "sha256({string})" function
8702 */
8703 static void
8704f_sha256(typval_T *argvars, typval_T *rettv)
8705{
8706 char_u *p;
8707
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008708 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709 rettv->vval.v_string = vim_strsave(
8710 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8711 rettv->v_type = VAR_STRING;
8712}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008713#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008714
8715/*
8716 * "shellescape({string})" function
8717 */
8718 static void
8719f_shellescape(typval_T *argvars, typval_T *rettv)
8720{
Bram Moolenaar20615522017-06-05 18:46:26 +02008721 int do_special = non_zero_arg(&argvars[1]);
8722
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008724 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008725 rettv->v_type = VAR_STRING;
8726}
8727
8728/*
8729 * shiftwidth() function
8730 */
8731 static void
8732f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8733{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008734 rettv->vval.v_number = 0;
8735
8736 if (argvars[0].v_type != VAR_UNKNOWN)
8737 {
8738 long col;
8739
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008740 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008741 if (col < 0)
8742 return; // type error; errmsg already given
8743#ifdef FEAT_VARTABS
8744 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8745 return;
8746#endif
8747 }
8748
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008749 rettv->vval.v_number = get_sw_value(curbuf);
8750}
8751
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008752#ifdef FEAT_FLOAT
8753/*
8754 * "sin()" function
8755 */
8756 static void
8757f_sin(typval_T *argvars, typval_T *rettv)
8758{
8759 float_T f = 0.0;
8760
8761 rettv->v_type = VAR_FLOAT;
8762 if (get_float_arg(argvars, &f) == OK)
8763 rettv->vval.v_float = sin(f);
8764 else
8765 rettv->vval.v_float = 0.0;
8766}
8767
8768/*
8769 * "sinh()" function
8770 */
8771 static void
8772f_sinh(typval_T *argvars, typval_T *rettv)
8773{
8774 float_T f = 0.0;
8775
8776 rettv->v_type = VAR_FLOAT;
8777 if (get_float_arg(argvars, &f) == OK)
8778 rettv->vval.v_float = sinh(f);
8779 else
8780 rettv->vval.v_float = 0.0;
8781}
8782#endif
8783
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008784/*
8785 * "soundfold({word})" function
8786 */
8787 static void
8788f_soundfold(typval_T *argvars, typval_T *rettv)
8789{
8790 char_u *s;
8791
8792 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008793 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794#ifdef FEAT_SPELL
8795 rettv->vval.v_string = eval_soundfold(s);
8796#else
8797 rettv->vval.v_string = vim_strsave(s);
8798#endif
8799}
8800
8801/*
8802 * "spellbadword()" function
8803 */
8804 static void
8805f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8806{
8807 char_u *word = (char_u *)"";
8808 hlf_T attr = HLF_COUNT;
8809 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008810#ifdef FEAT_SPELL
8811 int wo_spell_save = curwin->w_p_spell;
8812
8813 if (!curwin->w_p_spell)
8814 {
8815 did_set_spelllang(curwin);
8816 curwin->w_p_spell = TRUE;
8817 }
8818
8819 if (*curwin->w_s->b_p_spl == NUL)
8820 {
8821 emsg(_(e_no_spell));
8822 curwin->w_p_spell = wo_spell_save;
8823 return;
8824 }
8825#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008826
8827 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008828 {
8829#ifdef FEAT_SPELL
8830 curwin->w_p_spell = wo_spell_save;
8831#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008832 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008833 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008834
8835#ifdef FEAT_SPELL
8836 if (argvars[0].v_type == VAR_UNKNOWN)
8837 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008838 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008839 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8840 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008841 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008842 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008843 curwin->w_set_curswant = TRUE;
8844 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008845 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008846 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008847 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008848 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008849 int capcol = -1;
8850
8851 if (str != NULL)
8852 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008853 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008854 while (*str != NUL)
8855 {
8856 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8857 if (attr != HLF_COUNT)
8858 {
8859 word = str;
8860 break;
8861 }
8862 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008863 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008864 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008865 }
8866 }
8867 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008868 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008869#endif
8870
8871 list_append_string(rettv->vval.v_list, word, len);
8872 list_append_string(rettv->vval.v_list, (char_u *)(
8873 attr == HLF_SPB ? "bad" :
8874 attr == HLF_SPR ? "rare" :
8875 attr == HLF_SPL ? "local" :
8876 attr == HLF_SPC ? "caps" :
8877 ""), -1);
8878}
8879
8880/*
8881 * "spellsuggest()" function
8882 */
8883 static void
8884f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8885{
8886#ifdef FEAT_SPELL
8887 char_u *str;
8888 int typeerr = FALSE;
8889 int maxcount;
8890 garray_T ga;
8891 int i;
8892 listitem_T *li;
8893 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008894 int wo_spell_save = curwin->w_p_spell;
8895
8896 if (!curwin->w_p_spell)
8897 {
8898 did_set_spelllang(curwin);
8899 curwin->w_p_spell = TRUE;
8900 }
8901
8902 if (*curwin->w_s->b_p_spl == NUL)
8903 {
8904 emsg(_(e_no_spell));
8905 curwin->w_p_spell = wo_spell_save;
8906 return;
8907 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008908#endif
8909
8910 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008911 {
8912#ifdef FEAT_SPELL
8913 curwin->w_p_spell = wo_spell_save;
8914#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008915 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008916 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008917
8918#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008919 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008920 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008921 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008922 if (argvars[1].v_type != VAR_UNKNOWN)
8923 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008924 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008925 if (maxcount <= 0)
8926 return;
8927 if (argvars[2].v_type != VAR_UNKNOWN)
8928 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008929 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008930 if (typeerr)
8931 return;
8932 }
8933 }
8934 else
8935 maxcount = 25;
8936
8937 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8938
8939 for (i = 0; i < ga.ga_len; ++i)
8940 {
8941 str = ((char_u **)ga.ga_data)[i];
8942
8943 li = listitem_alloc();
8944 if (li == NULL)
8945 vim_free(str);
8946 else
8947 {
8948 li->li_tv.v_type = VAR_STRING;
8949 li->li_tv.v_lock = 0;
8950 li->li_tv.vval.v_string = str;
8951 list_append(rettv->vval.v_list, li);
8952 }
8953 }
8954 ga_clear(&ga);
8955 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008956 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008957#endif
8958}
8959
8960 static void
8961f_split(typval_T *argvars, typval_T *rettv)
8962{
8963 char_u *str;
8964 char_u *end;
8965 char_u *pat = NULL;
8966 regmatch_T regmatch;
8967 char_u patbuf[NUMBUFLEN];
8968 char_u *save_cpo;
8969 int match;
8970 colnr_T col = 0;
8971 int keepempty = FALSE;
8972 int typeerr = FALSE;
8973
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008974 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008975 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008976 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008977
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008978 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008979 if (argvars[1].v_type != VAR_UNKNOWN)
8980 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008981 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008982 if (pat == NULL)
8983 typeerr = TRUE;
8984 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008985 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008986 }
8987 if (pat == NULL || *pat == NUL)
8988 pat = (char_u *)"[\\x01- ]\\+";
8989
8990 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008991 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008992 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008993 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008994
8995 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8996 if (regmatch.regprog != NULL)
8997 {
8998 regmatch.rm_ic = FALSE;
8999 while (*str != NUL || keepempty)
9000 {
9001 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009002 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009003 else
9004 match = vim_regexec_nl(&regmatch, str, col);
9005 if (match)
9006 end = regmatch.startp[0];
9007 else
9008 end = str + STRLEN(str);
9009 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
9010 && *str != NUL && match && end < regmatch.endp[0]))
9011 {
9012 if (list_append_string(rettv->vval.v_list, str,
9013 (int)(end - str)) == FAIL)
9014 break;
9015 }
9016 if (!match)
9017 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01009018 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009019 if (regmatch.endp[0] > str)
9020 col = 0;
9021 else
Bram Moolenaar13505972019-01-24 15:04:48 +01009022 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009023 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009024 str = regmatch.endp[0];
9025 }
9026
9027 vim_regfree(regmatch.regprog);
9028 }
9029
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02009030theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009031 p_cpo = save_cpo;
9032}
9033
9034#ifdef FEAT_FLOAT
9035/*
9036 * "sqrt()" function
9037 */
9038 static void
9039f_sqrt(typval_T *argvars, typval_T *rettv)
9040{
9041 float_T f = 0.0;
9042
9043 rettv->v_type = VAR_FLOAT;
9044 if (get_float_arg(argvars, &f) == OK)
9045 rettv->vval.v_float = sqrt(f);
9046 else
9047 rettv->vval.v_float = 0.0;
9048}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009049#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009050
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009051#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01009052/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009053 * "str2float()" function
9054 */
9055 static void
9056f_str2float(typval_T *argvars, typval_T *rettv)
9057{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009058 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009059 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009060
Bram Moolenaar08243d22017-01-10 16:12:29 +01009061 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009062 p = skipwhite(p + 1);
9063 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01009064 if (isneg)
9065 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009066 rettv->v_type = VAR_FLOAT;
9067}
9068#endif
9069
9070/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02009071 * "str2list()" function
9072 */
9073 static void
9074f_str2list(typval_T *argvars, typval_T *rettv)
9075{
9076 char_u *p;
9077 int utf8 = FALSE;
9078
9079 if (rettv_list_alloc(rettv) == FAIL)
9080 return;
9081
9082 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02009083 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02009084
9085 p = tv_get_string(&argvars[0]);
9086
9087 if (has_mbyte || utf8)
9088 {
9089 int (*ptr2len)(char_u *);
9090 int (*ptr2char)(char_u *);
9091
9092 if (utf8 || enc_utf8)
9093 {
9094 ptr2len = utf_ptr2len;
9095 ptr2char = utf_ptr2char;
9096 }
9097 else
9098 {
9099 ptr2len = mb_ptr2len;
9100 ptr2char = mb_ptr2char;
9101 }
9102
9103 for ( ; *p != NUL; p += (*ptr2len)(p))
9104 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
9105 }
9106 else
9107 for ( ; *p != NUL; ++p)
9108 list_append_number(rettv->vval.v_list, *p);
9109}
9110
9111/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112 * "str2nr()" function
9113 */
9114 static void
9115f_str2nr(typval_T *argvars, typval_T *rettv)
9116{
9117 int base = 10;
9118 char_u *p;
9119 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009120 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01009121 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009122
9123 if (argvars[1].v_type != VAR_UNKNOWN)
9124 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009125 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009126 if (base != 2 && base != 8 && base != 10 && base != 16)
9127 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009128 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009129 return;
9130 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02009131 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009132 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009133 }
9134
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01009135 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009136 isneg = (*p == '-');
9137 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009138 p = skipwhite(p + 1);
9139 switch (base)
9140 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009141 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02009142 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009143 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009144 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02009145 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
9146 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01009147 if (isneg)
9148 rettv->vval.v_number = -n;
9149 else
9150 rettv->vval.v_number = n;
9151
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009152}
9153
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009154/*
9155 * "strgetchar()" function
9156 */
9157 static void
9158f_strgetchar(typval_T *argvars, typval_T *rettv)
9159{
9160 char_u *str;
9161 int len;
9162 int error = FALSE;
9163 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01009164 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009165
9166 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009167 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009168 if (str == NULL)
9169 return;
9170 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009171 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009172 if (error)
9173 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009174
Bram Moolenaar13505972019-01-24 15:04:48 +01009175 while (charidx >= 0 && byteidx < len)
9176 {
9177 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009178 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009179 rettv->vval.v_number = mb_ptr2char(str + byteidx);
9180 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009181 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009182 --charidx;
9183 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009184 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009185}
9186
9187/*
9188 * "stridx()" function
9189 */
9190 static void
9191f_stridx(typval_T *argvars, typval_T *rettv)
9192{
9193 char_u buf[NUMBUFLEN];
9194 char_u *needle;
9195 char_u *haystack;
9196 char_u *save_haystack;
9197 char_u *pos;
9198 int start_idx;
9199
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009200 needle = tv_get_string_chk(&argvars[1]);
9201 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009202 rettv->vval.v_number = -1;
9203 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009204 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009205
9206 if (argvars[2].v_type != VAR_UNKNOWN)
9207 {
9208 int error = FALSE;
9209
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009210 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009211 if (error || start_idx >= (int)STRLEN(haystack))
9212 return;
9213 if (start_idx >= 0)
9214 haystack += start_idx;
9215 }
9216
9217 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9218 if (pos != NULL)
9219 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
9220}
9221
9222/*
9223 * "string()" function
9224 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009225 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009226f_string(typval_T *argvars, typval_T *rettv)
9227{
9228 char_u *tofree;
9229 char_u numbuf[NUMBUFLEN];
9230
9231 rettv->v_type = VAR_STRING;
9232 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
9233 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009234 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009235 if (rettv->vval.v_string != NULL && tofree == NULL)
9236 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
9237}
9238
9239/*
9240 * "strlen()" function
9241 */
9242 static void
9243f_strlen(typval_T *argvars, typval_T *rettv)
9244{
9245 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009246 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009247}
9248
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01009249 static void
9250strchar_common(typval_T *argvars, typval_T *rettv, int skipcc)
9251{
9252 char_u *s = tv_get_string(&argvars[0]);
9253 varnumber_T len = 0;
9254 int (*func_mb_ptr2char_adv)(char_u **pp);
9255
9256 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
9257 while (*s != NUL)
9258 {
9259 func_mb_ptr2char_adv(&s);
9260 ++len;
9261 }
9262 rettv->vval.v_number = len;
9263}
9264
9265/*
9266 * "strcharlen()" function
9267 */
9268 static void
9269f_strcharlen(typval_T *argvars, typval_T *rettv)
9270{
9271 strchar_common(argvars, rettv, TRUE);
9272}
9273
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009274/*
9275 * "strchars()" function
9276 */
9277 static void
9278f_strchars(typval_T *argvars, typval_T *rettv)
9279{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009280 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009281
9282 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009283 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009284 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02009285 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009286 else
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01009287 strchar_common(argvars, rettv, skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009288}
9289
9290/*
9291 * "strdisplaywidth()" function
9292 */
9293 static void
9294f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
9295{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009296 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009297 int col = 0;
9298
9299 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009300 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009301
9302 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
9303}
9304
9305/*
9306 * "strwidth()" function
9307 */
9308 static void
9309f_strwidth(typval_T *argvars, typval_T *rettv)
9310{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009311 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009312
Bram Moolenaar13505972019-01-24 15:04:48 +01009313 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009314}
9315
9316/*
9317 * "strcharpart()" function
9318 */
9319 static void
9320f_strcharpart(typval_T *argvars, typval_T *rettv)
9321{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009322 char_u *p;
9323 int nchar;
9324 int nbyte = 0;
9325 int charlen;
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009326 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009327 int len = 0;
9328 int slen;
9329 int error = FALSE;
9330
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009331 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009332 slen = (int)STRLEN(p);
9333
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009334 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009335 if (!error)
9336 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009337 if (argvars[2].v_type != VAR_UNKNOWN
9338 && argvars[3].v_type != VAR_UNKNOWN)
9339 {
9340 skipcc = tv_get_bool(&argvars[3]);
9341 if (skipcc < 0 || skipcc > 1)
9342 {
9343 semsg(_(e_using_number_as_bool_nr), skipcc);
9344 return;
9345 }
9346 }
9347
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009348 if (nchar > 0)
9349 while (nchar > 0 && nbyte < slen)
9350 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009351 if (skipcc)
9352 nbyte += mb_ptr2len(p + nbyte);
9353 else
9354 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009355 --nchar;
9356 }
9357 else
9358 nbyte = nchar;
9359 if (argvars[2].v_type != VAR_UNKNOWN)
9360 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009361 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009362 while (charlen > 0 && nbyte + len < slen)
9363 {
9364 int off = nbyte + len;
9365
9366 if (off < 0)
9367 len += 1;
9368 else
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009369 {
9370 if (skipcc)
9371 len += mb_ptr2len(p + off);
9372 else
9373 len += MB_CPTR2LEN(p + off);
9374 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009375 --charlen;
9376 }
9377 }
9378 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009379 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009380 }
9381
9382 /*
9383 * Only return the overlap between the specified part and the actual
9384 * string.
9385 */
9386 if (nbyte < 0)
9387 {
9388 len += nbyte;
9389 nbyte = 0;
9390 }
9391 else if (nbyte > slen)
9392 nbyte = slen;
9393 if (len < 0)
9394 len = 0;
9395 else if (nbyte + len > slen)
9396 len = slen - nbyte;
9397
9398 rettv->v_type = VAR_STRING;
9399 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009400}
9401
9402/*
9403 * "strpart()" function
9404 */
9405 static void
9406f_strpart(typval_T *argvars, typval_T *rettv)
9407{
9408 char_u *p;
9409 int n;
9410 int len;
9411 int slen;
9412 int error = FALSE;
9413
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009414 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009415 slen = (int)STRLEN(p);
9416
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009417 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009418 if (error)
9419 len = 0;
9420 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009421 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009422 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009423 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009424
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009425 // Only return the overlap between the specified part and the actual
9426 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009427 if (n < 0)
9428 {
9429 len += n;
9430 n = 0;
9431 }
9432 else if (n > slen)
9433 n = slen;
9434 if (len < 0)
9435 len = 0;
9436 else if (n + len > slen)
9437 len = slen - n;
9438
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009439 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9440 {
9441 int off;
9442
9443 // length in characters
9444 for (off = n; off < slen && len > 0; --len)
9445 off += mb_ptr2len(p + off);
9446 len = off - n;
9447 }
9448
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009449 rettv->v_type = VAR_STRING;
9450 rettv->vval.v_string = vim_strnsave(p + n, len);
9451}
9452
9453/*
9454 * "strridx()" function
9455 */
9456 static void
9457f_strridx(typval_T *argvars, typval_T *rettv)
9458{
9459 char_u buf[NUMBUFLEN];
9460 char_u *needle;
9461 char_u *haystack;
9462 char_u *rest;
9463 char_u *lastmatch = NULL;
9464 int haystack_len, end_idx;
9465
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009466 needle = tv_get_string_chk(&argvars[1]);
9467 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009468
9469 rettv->vval.v_number = -1;
9470 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009471 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009472
9473 haystack_len = (int)STRLEN(haystack);
9474 if (argvars[2].v_type != VAR_UNKNOWN)
9475 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009476 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009477 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009478 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009479 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009480 }
9481 else
9482 end_idx = haystack_len;
9483
9484 if (*needle == NUL)
9485 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009486 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009487 lastmatch = haystack + end_idx;
9488 }
9489 else
9490 {
9491 for (rest = haystack; *rest != '\0'; ++rest)
9492 {
9493 rest = (char_u *)strstr((char *)rest, (char *)needle);
9494 if (rest == NULL || rest > haystack + end_idx)
9495 break;
9496 lastmatch = rest;
9497 }
9498 }
9499
9500 if (lastmatch == NULL)
9501 rettv->vval.v_number = -1;
9502 else
9503 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9504}
9505
9506/*
9507 * "strtrans()" function
9508 */
9509 static void
9510f_strtrans(typval_T *argvars, typval_T *rettv)
9511{
9512 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009513 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009514}
9515
9516/*
9517 * "submatch()" function
9518 */
9519 static void
9520f_submatch(typval_T *argvars, typval_T *rettv)
9521{
9522 int error = FALSE;
9523 int no;
9524 int retList = 0;
9525
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009526 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009527 if (error)
9528 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009529 if (no < 0 || no >= NSUBEXP)
9530 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009531 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009532 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009533 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009534 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009535 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009536 if (error)
9537 return;
9538
9539 if (retList == 0)
9540 {
9541 rettv->v_type = VAR_STRING;
9542 rettv->vval.v_string = reg_submatch(no);
9543 }
9544 else
9545 {
9546 rettv->v_type = VAR_LIST;
9547 rettv->vval.v_list = reg_submatch_list(no);
9548 }
9549}
9550
9551/*
9552 * "substitute()" function
9553 */
9554 static void
9555f_substitute(typval_T *argvars, typval_T *rettv)
9556{
9557 char_u patbuf[NUMBUFLEN];
9558 char_u subbuf[NUMBUFLEN];
9559 char_u flagsbuf[NUMBUFLEN];
9560
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009561 char_u *str = tv_get_string_chk(&argvars[0]);
9562 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009563 char_u *sub = NULL;
9564 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009565 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009566
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009567 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9568 expr = &argvars[2];
9569 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009570 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009571
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009572 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009573 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9574 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009575 rettv->vval.v_string = NULL;
9576 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009577 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009578}
9579
9580/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009581 * "swapinfo(swap_filename)" function
9582 */
9583 static void
9584f_swapinfo(typval_T *argvars, typval_T *rettv)
9585{
9586 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009587 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009588}
9589
9590/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009591 * "swapname(expr)" function
9592 */
9593 static void
9594f_swapname(typval_T *argvars, typval_T *rettv)
9595{
9596 buf_T *buf;
9597
9598 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009599 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009600 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9601 || buf->b_ml.ml_mfp->mf_fname == NULL)
9602 rettv->vval.v_string = NULL;
9603 else
9604 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9605}
9606
9607/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009608 * "synID(lnum, col, trans)" function
9609 */
9610 static void
9611f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9612{
9613 int id = 0;
9614#ifdef FEAT_SYN_HL
9615 linenr_T lnum;
9616 colnr_T col;
9617 int trans;
9618 int transerr = FALSE;
9619
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009620 lnum = tv_get_lnum(argvars); // -1 on type error
9621 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009622 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009623
9624 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9625 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9626 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9627#endif
9628
9629 rettv->vval.v_number = id;
9630}
9631
9632/*
9633 * "synIDattr(id, what [, mode])" function
9634 */
9635 static void
9636f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9637{
9638 char_u *p = NULL;
9639#ifdef FEAT_SYN_HL
9640 int id;
9641 char_u *what;
9642 char_u *mode;
9643 char_u modebuf[NUMBUFLEN];
9644 int modec;
9645
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009646 id = (int)tv_get_number(&argvars[0]);
9647 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009648 if (argvars[2].v_type != VAR_UNKNOWN)
9649 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009650 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009651 modec = TOLOWER_ASC(mode[0]);
9652 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009653 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009654 }
9655 else
9656 {
9657#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9658 if (USE_24BIT)
9659 modec = 'g';
9660 else
9661#endif
9662 if (t_colors > 1)
9663 modec = 'c';
9664 else
9665 modec = 't';
9666 }
9667
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009668 switch (TOLOWER_ASC(what[0]))
9669 {
9670 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009671 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009672 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009673 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009674 p = highlight_has_attr(id, HL_BOLD, modec);
9675 break;
9676
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009677 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009678 p = highlight_color(id, what, modec);
9679 break;
9680
9681 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009682 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009683 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009684 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009685 p = highlight_has_attr(id, HL_ITALIC, modec);
9686 break;
9687
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009688 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009689 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009690 break;
9691
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009692 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009693 p = highlight_has_attr(id, HL_INVERSE, modec);
9694 break;
9695
9696 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009697 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009698 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009699 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009700 else if (TOLOWER_ASC(what[1]) == 't' &&
9701 TOLOWER_ASC(what[2]) == 'r')
9702 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009703 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009704 p = highlight_has_attr(id, HL_STANDOUT, modec);
9705 break;
9706
9707 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009708 if (TOLOWER_ASC(what[1]) == 'l') // ul
9709 p = highlight_color(id, what, modec);
9710 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009711 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009712 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9713 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009714 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009715 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9716 break;
9717 }
9718
9719 if (p != NULL)
9720 p = vim_strsave(p);
9721#endif
9722 rettv->v_type = VAR_STRING;
9723 rettv->vval.v_string = p;
9724}
9725
9726/*
9727 * "synIDtrans(id)" function
9728 */
9729 static void
9730f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9731{
9732 int id;
9733
9734#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009735 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009736
9737 if (id > 0)
9738 id = syn_get_final_id(id);
9739 else
9740#endif
9741 id = 0;
9742
9743 rettv->vval.v_number = id;
9744}
9745
9746/*
9747 * "synconcealed(lnum, col)" function
9748 */
9749 static void
9750f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9751{
9752#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9753 linenr_T lnum;
9754 colnr_T col;
9755 int syntax_flags = 0;
9756 int cchar;
9757 int matchid = 0;
9758 char_u str[NUMBUFLEN];
9759#endif
9760
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009761 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009762
9763#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009764 lnum = tv_get_lnum(argvars); // -1 on type error
9765 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009766
Bram Moolenaara80faa82020-04-12 19:37:17 +02009767 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009768
9769 if (rettv_list_alloc(rettv) != FAIL)
9770 {
9771 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9772 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9773 && curwin->w_p_cole > 0)
9774 {
9775 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9776 syntax_flags = get_syntax_info(&matchid);
9777
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009778 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009779 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9780 {
9781 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009782 if (cchar == NUL && curwin->w_p_cole == 1)
Bram Moolenaareed9d462021-02-15 20:38:25 +01009783 cchar = (curwin->w_lcs_chars.conceal == NUL) ? ' '
9784 : curwin->w_lcs_chars.conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009785 if (cchar != NUL)
9786 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009787 if (has_mbyte)
9788 (*mb_char2bytes)(cchar, str);
9789 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009790 str[0] = cchar;
9791 }
9792 }
9793 }
9794
9795 list_append_number(rettv->vval.v_list,
9796 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009797 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009798 list_append_string(rettv->vval.v_list, str, -1);
9799 list_append_number(rettv->vval.v_list, matchid);
9800 }
9801#endif
9802}
9803
9804/*
9805 * "synstack(lnum, col)" function
9806 */
9807 static void
9808f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9809{
9810#ifdef FEAT_SYN_HL
9811 linenr_T lnum;
9812 colnr_T col;
9813 int i;
9814 int id;
9815#endif
9816
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009817 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009818
9819#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009820 lnum = tv_get_lnum(argvars); // -1 on type error
9821 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009822
9823 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9824 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9825 && rettv_list_alloc(rettv) != FAIL)
9826 {
9827 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9828 for (i = 0; ; ++i)
9829 {
9830 id = syn_get_stack_item(i);
9831 if (id < 0)
9832 break;
9833 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9834 break;
9835 }
9836 }
9837#endif
9838}
9839
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009840/*
9841 * "tabpagebuflist()" function
9842 */
9843 static void
9844f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9845{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009846 tabpage_T *tp;
9847 win_T *wp = NULL;
9848
9849 if (argvars[0].v_type == VAR_UNKNOWN)
9850 wp = firstwin;
9851 else
9852 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009853 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009854 if (tp != NULL)
9855 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9856 }
9857 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9858 {
9859 for (; wp != NULL; wp = wp->w_next)
9860 if (list_append_number(rettv->vval.v_list,
9861 wp->w_buffer->b_fnum) == FAIL)
9862 break;
9863 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009864}
9865
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009866/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009867 * "tagfiles()" function
9868 */
9869 static void
9870f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9871{
9872 char_u *fname;
9873 tagname_T tn;
9874 int first;
9875
9876 if (rettv_list_alloc(rettv) == FAIL)
9877 return;
9878 fname = alloc(MAXPATHL);
9879 if (fname == NULL)
9880 return;
9881
9882 for (first = TRUE; ; first = FALSE)
9883 if (get_tagfname(&tn, first, fname) == FAIL
9884 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9885 break;
9886 tagname_free(&tn);
9887 vim_free(fname);
9888}
9889
9890/*
9891 * "taglist()" function
9892 */
9893 static void
9894f_taglist(typval_T *argvars, typval_T *rettv)
9895{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009896 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009897 char_u *tag_pattern;
9898
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009899 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009900
9901 rettv->vval.v_number = FALSE;
9902 if (*tag_pattern == NUL)
9903 return;
9904
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009905 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009906 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009907 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009908 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009909}
9910
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009911#ifdef FEAT_FLOAT
9912/*
9913 * "tan()" function
9914 */
9915 static void
9916f_tan(typval_T *argvars, typval_T *rettv)
9917{
9918 float_T f = 0.0;
9919
9920 rettv->v_type = VAR_FLOAT;
9921 if (get_float_arg(argvars, &f) == OK)
9922 rettv->vval.v_float = tan(f);
9923 else
9924 rettv->vval.v_float = 0.0;
9925}
9926
9927/*
9928 * "tanh()" function
9929 */
9930 static void
9931f_tanh(typval_T *argvars, typval_T *rettv)
9932{
9933 float_T f = 0.0;
9934
9935 rettv->v_type = VAR_FLOAT;
9936 if (get_float_arg(argvars, &f) == OK)
9937 rettv->vval.v_float = tanh(f);
9938 else
9939 rettv->vval.v_float = 0.0;
9940}
9941#endif
9942
9943/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009944 * "tolower(string)" function
9945 */
9946 static void
9947f_tolower(typval_T *argvars, typval_T *rettv)
9948{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009949 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009950 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009951}
9952
9953/*
9954 * "toupper(string)" function
9955 */
9956 static void
9957f_toupper(typval_T *argvars, typval_T *rettv)
9958{
9959 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009960 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009961}
9962
9963/*
9964 * "tr(string, fromstr, tostr)" function
9965 */
9966 static void
9967f_tr(typval_T *argvars, typval_T *rettv)
9968{
9969 char_u *in_str;
9970 char_u *fromstr;
9971 char_u *tostr;
9972 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009973 int inlen;
9974 int fromlen;
9975 int tolen;
9976 int idx;
9977 char_u *cpstr;
9978 int cplen;
9979 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009980 char_u buf[NUMBUFLEN];
9981 char_u buf2[NUMBUFLEN];
9982 garray_T ga;
9983
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009984 in_str = tv_get_string(&argvars[0]);
9985 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9986 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009987
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009988 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009989 rettv->v_type = VAR_STRING;
9990 rettv->vval.v_string = NULL;
9991 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009992 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009993 ga_init2(&ga, (int)sizeof(char), 80);
9994
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009995 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009996 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009997 if (STRLEN(fromstr) != STRLEN(tostr))
9998 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009999error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010000 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010001 ga_clear(&ga);
10002 return;
10003 }
10004
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010005 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010006 while (*in_str != NUL)
10007 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010008 if (has_mbyte)
10009 {
10010 inlen = (*mb_ptr2len)(in_str);
10011 cpstr = in_str;
10012 cplen = inlen;
10013 idx = 0;
10014 for (p = fromstr; *p != NUL; p += fromlen)
10015 {
10016 fromlen = (*mb_ptr2len)(p);
10017 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
10018 {
10019 for (p = tostr; *p != NUL; p += tolen)
10020 {
10021 tolen = (*mb_ptr2len)(p);
10022 if (idx-- == 0)
10023 {
10024 cplen = tolen;
10025 cpstr = p;
10026 break;
10027 }
10028 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010029 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010030 goto error;
10031 break;
10032 }
10033 ++idx;
10034 }
10035
10036 if (first && cpstr == in_str)
10037 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010038 // Check that fromstr and tostr have the same number of
10039 // (multi-byte) characters. Done only once when a character
10040 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010041 first = FALSE;
10042 for (p = tostr; *p != NUL; p += tolen)
10043 {
10044 tolen = (*mb_ptr2len)(p);
10045 --idx;
10046 }
10047 if (idx != 0)
10048 goto error;
10049 }
10050
10051 (void)ga_grow(&ga, cplen);
10052 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
10053 ga.ga_len += cplen;
10054
10055 in_str += inlen;
10056 }
10057 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010058 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010059 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010060 p = vim_strchr(fromstr, *in_str);
10061 if (p != NULL)
10062 ga_append(&ga, tostr[p - fromstr]);
10063 else
10064 ga_append(&ga, *in_str);
10065 ++in_str;
10066 }
10067 }
10068
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010069 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010070 (void)ga_grow(&ga, 1);
10071 ga_append(&ga, NUL);
10072
10073 rettv->vval.v_string = ga.ga_data;
10074}
10075
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010076/*
10077 * "trim({expr})" function
10078 */
10079 static void
10080f_trim(typval_T *argvars, typval_T *rettv)
10081{
10082 char_u buf1[NUMBUFLEN];
10083 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010084 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010085 char_u *mask = NULL;
10086 char_u *tail;
10087 char_u *prev;
10088 char_u *p;
10089 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010090 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010091
10092 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010093 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010094 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010095 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010096
10097 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010098 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010099 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010100
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010101 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010102 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010103 int error = 0;
10104
10105 // leading or trailing characters to trim
10106 dir = (int)tv_get_number_chk(&argvars[2], &error);
10107 if (error)
10108 return;
10109 if (dir < 0 || dir > 2)
10110 {
10111 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
10112 return;
10113 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010114 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010115 }
10116
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010117 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010118 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010119 // Trim leading characters
10120 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010121 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010122 c1 = PTR2CHAR(head);
10123 if (mask == NULL)
10124 {
10125 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010126 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010127 }
10128 else
10129 {
10130 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10131 if (c1 == PTR2CHAR(p))
10132 break;
10133 if (*p == NUL)
10134 break;
10135 }
10136 MB_PTR_ADV(head);
10137 }
10138 }
10139
10140 tail = head + STRLEN(head);
10141 if (dir == 0 || dir == 2)
10142 {
10143 // Trim trailing characters
10144 for (; tail > head; tail = prev)
10145 {
10146 prev = tail;
10147 MB_PTR_BACK(head, prev);
10148 c1 = PTR2CHAR(prev);
10149 if (mask == NULL)
10150 {
10151 if (c1 > ' ' && c1 != 0xa0)
10152 break;
10153 }
10154 else
10155 {
10156 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10157 if (c1 == PTR2CHAR(p))
10158 break;
10159 if (*p == NUL)
10160 break;
10161 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010162 }
10163 }
Bram Moolenaardf44a272020-06-07 20:49:05 +020010164 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010165}
10166
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010167#ifdef FEAT_FLOAT
10168/*
10169 * "trunc({float})" function
10170 */
10171 static void
10172f_trunc(typval_T *argvars, typval_T *rettv)
10173{
10174 float_T f = 0.0;
10175
10176 rettv->v_type = VAR_FLOAT;
10177 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010178 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010179 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
10180 else
10181 rettv->vval.v_float = 0.0;
10182}
10183#endif
10184
10185/*
10186 * "type(expr)" function
10187 */
10188 static void
10189f_type(typval_T *argvars, typval_T *rettv)
10190{
10191 int n = -1;
10192
10193 switch (argvars[0].v_type)
10194 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010195 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
10196 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010197 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010198 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
10199 case VAR_LIST: n = VAR_TYPE_LIST; break;
10200 case VAR_DICT: n = VAR_TYPE_DICT; break;
10201 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
10202 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
10203 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020010204 case VAR_JOB: n = VAR_TYPE_JOB; break;
10205 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010206 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010207 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +020010208 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010209 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +010010210 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010211 n = -1;
10212 break;
10213 }
10214 rettv->vval.v_number = n;
10215}
10216
10217/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010218 * "virtcol(string)" function
10219 */
10220 static void
10221f_virtcol(typval_T *argvars, typval_T *rettv)
10222{
10223 colnr_T vcol = 0;
10224 pos_T *fp;
10225 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010226 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010227
Bram Moolenaar6f02b002021-01-10 20:22:54 +010010228 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010229 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
10230 && fnum == curbuf->b_fnum)
10231 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010232 // Limit the column to a valid value, getvvcol() doesn't check.
10233 if (fp->col < 0)
10234 fp->col = 0;
10235 else
10236 {
10237 len = (int)STRLEN(ml_get(fp->lnum));
10238 if (fp->col > len)
10239 fp->col = len;
10240 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010241 getvvcol(curwin, fp, NULL, NULL, &vcol);
10242 ++vcol;
10243 }
10244
10245 rettv->vval.v_number = vcol;
10246}
10247
10248/*
10249 * "visualmode()" function
10250 */
10251 static void
10252f_visualmode(typval_T *argvars, typval_T *rettv)
10253{
10254 char_u str[2];
10255
10256 rettv->v_type = VAR_STRING;
10257 str[0] = curbuf->b_visual_mode_eval;
10258 str[1] = NUL;
10259 rettv->vval.v_string = vim_strsave(str);
10260
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010261 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010262 if (non_zero_arg(&argvars[0]))
10263 curbuf->b_visual_mode_eval = NUL;
10264}
10265
10266/*
10267 * "wildmenumode()" function
10268 */
10269 static void
10270f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10271{
10272#ifdef FEAT_WILDMENU
10273 if (wild_menu_showing)
10274 rettv->vval.v_number = 1;
10275#endif
10276}
10277
10278/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +010010279 * "windowsversion()" function
10280 */
10281 static void
10282f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10283{
10284 rettv->v_type = VAR_STRING;
10285 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
10286}
10287
10288/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010289 * "wordcount()" function
10290 */
10291 static void
10292f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
10293{
10294 if (rettv_dict_alloc(rettv) == FAIL)
10295 return;
10296 cursor_pos_info(rettv->vval.v_dict);
10297}
10298
10299/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010300 * "xor(expr, expr)" function
10301 */
10302 static void
10303f_xor(typval_T *argvars, typval_T *rettv)
10304{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010305 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
10306 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010307}
10308
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010309#endif // FEAT_EVAL