blob: f83559dbbe04845e20652157549ca899439d1bc1 [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 Moolenaarc5809432021-03-27 21:23:30 +01002524 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2525 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002526 if (has_mbyte)
2527 {
2528 int utf8 = 0;
2529
2530 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002531 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002532
2533 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002534 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002535 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002536 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002537 }
2538 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002539 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002540}
2541
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002542/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002543 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2544 * returns the character index of the column. Otherwise, returns the byte index
2545 * of the column.
2546 */
2547 static void
2548get_col(typval_T *argvars, typval_T *rettv, int charcol)
2549{
2550 colnr_T col = 0;
2551 pos_T *fp;
2552 int fnum = curbuf->b_fnum;
2553
2554 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2555 if (fp != NULL && fnum == curbuf->b_fnum)
2556 {
2557 if (fp->col == MAXCOL)
2558 {
2559 // '> can be MAXCOL, get the length of the line then
2560 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2561 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2562 else
2563 col = MAXCOL;
2564 }
2565 else
2566 {
2567 col = fp->col + 1;
2568 // col(".") when the cursor is on the NUL at the end of the line
2569 // because of "coladd" can be seen as an extra column.
2570 if (virtual_active() && fp == &curwin->w_cursor)
2571 {
2572 char_u *p = ml_get_cursor();
2573
2574 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2575 curwin->w_virtcol - curwin->w_cursor.coladd))
2576 {
2577 int l;
2578
2579 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2580 col += l;
2581 }
2582 }
2583 }
2584 }
2585 rettv->vval.v_number = col;
2586}
2587
2588/*
2589 * "charcol()" function
2590 */
2591 static void
2592f_charcol(typval_T *argvars, typval_T *rettv)
2593{
2594 get_col(argvars, rettv, TRUE);
2595}
2596
2597/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002598 * "charidx()" function
2599 */
2600 static void
2601f_charidx(typval_T *argvars, typval_T *rettv)
2602{
2603 char_u *str;
2604 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002605 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002606 char_u *p;
2607 int len;
2608 int (*ptr2len)(char_u *);
2609
2610 rettv->vval.v_number = -1;
2611
2612 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2613 || (argvars[2].v_type != VAR_UNKNOWN
2614 && argvars[2].v_type != VAR_NUMBER))
2615 {
2616 emsg(_(e_invarg));
2617 return;
2618 }
2619
2620 str = tv_get_string_chk(&argvars[0]);
2621 idx = tv_get_number_chk(&argvars[1], NULL);
2622 if (str == NULL || idx < 0)
2623 return;
2624
2625 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002626 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002627 if (countcc < 0 || countcc > 1)
2628 {
2629 semsg(_(e_using_number_as_bool_nr), countcc);
2630 return;
2631 }
2632
2633 if (enc_utf8 && countcc)
2634 ptr2len = utf_ptr2len;
2635 else
2636 ptr2len = mb_ptr2len;
2637
2638 for (p = str, len = 0; p <= str + idx; len++)
2639 {
2640 if (*p == NUL)
2641 return;
2642 p += ptr2len(p);
2643 }
2644
2645 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2646}
2647
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002648 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002649get_optional_window(typval_T *argvars, int idx)
2650{
2651 win_T *win = curwin;
2652
2653 if (argvars[idx].v_type != VAR_UNKNOWN)
2654 {
2655 win = find_win_by_nr_or_id(&argvars[idx]);
2656 if (win == NULL)
2657 {
2658 emsg(_(e_invalwindow));
2659 return NULL;
2660 }
2661 }
2662 return win;
2663}
2664
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002665/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002666 * "col(string)" function
2667 */
2668 static void
2669f_col(typval_T *argvars, typval_T *rettv)
2670{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002671 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002672}
2673
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002674/*
2675 * "confirm(message, buttons[, default [, type]])" function
2676 */
2677 static void
2678f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2679{
2680#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2681 char_u *message;
2682 char_u *buttons = NULL;
2683 char_u buf[NUMBUFLEN];
2684 char_u buf2[NUMBUFLEN];
2685 int def = 1;
2686 int type = VIM_GENERIC;
2687 char_u *typestr;
2688 int error = FALSE;
2689
Bram Moolenaarc5809432021-03-27 21:23:30 +01002690 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2691 return;
2692
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002693 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002694 if (message == NULL)
2695 error = TRUE;
2696 if (argvars[1].v_type != VAR_UNKNOWN)
2697 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002698 if (in_vim9script() && check_for_string_arg(argvars, 1) == FAIL)
2699 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002700 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002701 if (buttons == NULL)
2702 error = TRUE;
2703 if (argvars[2].v_type != VAR_UNKNOWN)
2704 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002705 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002706 if (argvars[3].v_type != VAR_UNKNOWN)
2707 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002708 if (in_vim9script() && check_for_string_arg(argvars, 3) == FAIL)
2709 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002710 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002711 if (typestr == NULL)
2712 error = TRUE;
2713 else
2714 {
2715 switch (TOUPPER_ASC(*typestr))
2716 {
2717 case 'E': type = VIM_ERROR; break;
2718 case 'Q': type = VIM_QUESTION; break;
2719 case 'I': type = VIM_INFO; break;
2720 case 'W': type = VIM_WARNING; break;
2721 case 'G': type = VIM_GENERIC; break;
2722 }
2723 }
2724 }
2725 }
2726 }
2727
2728 if (buttons == NULL || *buttons == NUL)
2729 buttons = (char_u *)_("&Ok");
2730
2731 if (!error)
2732 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2733 def, NULL, FALSE);
2734#endif
2735}
2736
2737/*
2738 * "copy()" function
2739 */
2740 static void
2741f_copy(typval_T *argvars, typval_T *rettv)
2742{
2743 item_copy(&argvars[0], rettv, FALSE, 0);
2744}
2745
2746#ifdef FEAT_FLOAT
2747/*
2748 * "cos()" function
2749 */
2750 static void
2751f_cos(typval_T *argvars, typval_T *rettv)
2752{
2753 float_T f = 0.0;
2754
2755 rettv->v_type = VAR_FLOAT;
2756 if (get_float_arg(argvars, &f) == OK)
2757 rettv->vval.v_float = cos(f);
2758 else
2759 rettv->vval.v_float = 0.0;
2760}
2761
2762/*
2763 * "cosh()" function
2764 */
2765 static void
2766f_cosh(typval_T *argvars, typval_T *rettv)
2767{
2768 float_T f = 0.0;
2769
2770 rettv->v_type = VAR_FLOAT;
2771 if (get_float_arg(argvars, &f) == OK)
2772 rettv->vval.v_float = cosh(f);
2773 else
2774 rettv->vval.v_float = 0.0;
2775}
2776#endif
2777
2778/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002779 * Set the cursor position.
2780 * If 'charcol' is TRUE, then use the column number as a character offet.
2781 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782 */
2783 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002784set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002785{
2786 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002787 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002788 int set_curswant = TRUE;
2789
2790 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002791 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002792 {
2793 pos_T pos;
2794 colnr_T curswant = -1;
2795
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002796 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002797 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002798 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002799 return;
2800 }
2801 line = pos.lnum;
2802 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002803 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002804 if (curswant >= 0)
2805 {
2806 curwin->w_curswant = curswant - 1;
2807 set_curswant = FALSE;
2808 }
2809 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002810 else if ((argvars[0].v_type == VAR_NUMBER ||
2811 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002812 && (argvars[1].v_type == VAR_NUMBER ||
2813 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002814 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002815 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002816 if (line < 0)
2817 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002818 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002819 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002820 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002821 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002822 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002823 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002824 else
2825 {
2826 emsg(_(e_invarg));
2827 return;
2828 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002829 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002830 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002831 if (line > 0)
2832 curwin->w_cursor.lnum = line;
2833 if (col > 0)
2834 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002835 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002836
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002837 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002838 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002839 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002840 if (has_mbyte)
2841 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002842
2843 curwin->w_set_curswant = set_curswant;
2844 rettv->vval.v_number = 0;
2845}
2846
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002847/*
2848 * "cursor(lnum, col)" function, or
2849 * "cursor(list)"
2850 *
2851 * Moves the cursor to the specified line and column.
2852 * Returns 0 when the position could be set, -1 otherwise.
2853 */
2854 static void
2855f_cursor(typval_T *argvars, typval_T *rettv)
2856{
2857 set_cursorpos(argvars, rettv, FALSE);
2858}
2859
Bram Moolenaar4f974752019-02-17 17:44:42 +01002860#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002861/*
2862 * "debugbreak()" function
2863 */
2864 static void
2865f_debugbreak(typval_T *argvars, typval_T *rettv)
2866{
2867 int pid;
2868
2869 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002870 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002871 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002872 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002873 else
2874 {
2875 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2876
2877 if (hProcess != NULL)
2878 {
2879 DebugBreakProcess(hProcess);
2880 CloseHandle(hProcess);
2881 rettv->vval.v_number = OK;
2882 }
2883 }
2884}
2885#endif
2886
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002887/*
2888 * "deepcopy()" function
2889 */
2890 static void
2891f_deepcopy(typval_T *argvars, typval_T *rettv)
2892{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002893 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002894 int copyID;
2895
2896 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002897 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002898 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002899 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002900 else
2901 {
2902 copyID = get_copyID();
2903 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2904 }
2905}
2906
2907/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002908 * "did_filetype()" function
2909 */
2910 static void
2911f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2912{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002913 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002914}
2915
2916/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002917 * "echoraw({expr})" function
2918 */
2919 static void
2920f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2921{
2922 char_u *str = tv_get_string_chk(&argvars[0]);
2923
2924 if (str != NULL && *str != NUL)
2925 {
2926 out_str(str);
2927 out_flush();
2928 }
2929}
2930
2931/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002932 * "empty({expr})" function
2933 */
2934 static void
2935f_empty(typval_T *argvars, typval_T *rettv)
2936{
2937 int n = FALSE;
2938
2939 switch (argvars[0].v_type)
2940 {
2941 case VAR_STRING:
2942 case VAR_FUNC:
2943 n = argvars[0].vval.v_string == NULL
2944 || *argvars[0].vval.v_string == NUL;
2945 break;
2946 case VAR_PARTIAL:
2947 n = FALSE;
2948 break;
2949 case VAR_NUMBER:
2950 n = argvars[0].vval.v_number == 0;
2951 break;
2952 case VAR_FLOAT:
2953#ifdef FEAT_FLOAT
2954 n = argvars[0].vval.v_float == 0.0;
2955 break;
2956#endif
2957 case VAR_LIST:
2958 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002959 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002960 break;
2961 case VAR_DICT:
2962 n = argvars[0].vval.v_dict == NULL
2963 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2964 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002965 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002966 case VAR_SPECIAL:
2967 n = argvars[0].vval.v_number != VVAL_TRUE;
2968 break;
2969
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002970 case VAR_BLOB:
2971 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002972 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2973 break;
2974
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002975 case VAR_JOB:
2976#ifdef FEAT_JOB_CHANNEL
2977 n = argvars[0].vval.v_job == NULL
2978 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2979 break;
2980#endif
2981 case VAR_CHANNEL:
2982#ifdef FEAT_JOB_CHANNEL
2983 n = argvars[0].vval.v_channel == NULL
2984 || !channel_is_open(argvars[0].vval.v_channel);
2985 break;
2986#endif
2987 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002988 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002990 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002991 n = TRUE;
2992 break;
2993 }
2994
2995 rettv->vval.v_number = n;
2996}
2997
2998/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002999 * "environ()" function
3000 */
3001 static void
3002f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3003{
3004#if !defined(AMIGA)
3005 int i = 0;
3006 char_u *entry, *value;
3007# ifdef MSWIN
3008 extern wchar_t **_wenviron;
3009# else
3010 extern char **environ;
3011# endif
3012
3013 if (rettv_dict_alloc(rettv) != OK)
3014 return;
3015
3016# ifdef MSWIN
3017 if (*_wenviron == NULL)
3018 return;
3019# else
3020 if (*environ == NULL)
3021 return;
3022# endif
3023
3024 for (i = 0; ; ++i)
3025 {
3026# ifdef MSWIN
3027 short_u *p;
3028
3029 if ((p = (short_u *)_wenviron[i]) == NULL)
3030 return;
3031 entry = utf16_to_enc(p, NULL);
3032# else
3033 if ((entry = (char_u *)environ[i]) == NULL)
3034 return;
3035 entry = vim_strsave(entry);
3036# endif
3037 if (entry == NULL) // out of memory
3038 return;
3039 if ((value = vim_strchr(entry, '=')) == NULL)
3040 {
3041 vim_free(entry);
3042 continue;
3043 }
3044 *value++ = NUL;
3045 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3046 vim_free(entry);
3047 }
3048#endif
3049}
3050
3051/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003052 * "escape({string}, {chars})" function
3053 */
3054 static void
3055f_escape(typval_T *argvars, typval_T *rettv)
3056{
3057 char_u buf[NUMBUFLEN];
3058
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003059 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3060 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003061 rettv->v_type = VAR_STRING;
3062}
3063
3064/*
3065 * "eval()" function
3066 */
3067 static void
3068f_eval(typval_T *argvars, typval_T *rettv)
3069{
3070 char_u *s, *p;
3071
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003072 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003073 if (s != NULL)
3074 s = skipwhite(s);
3075
3076 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003077 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003078 {
3079 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003080 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003081 need_clr_eos = FALSE;
3082 rettv->v_type = VAR_NUMBER;
3083 rettv->vval.v_number = 0;
3084 }
3085 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003086 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003087}
3088
3089/*
3090 * "eventhandler()" function
3091 */
3092 static void
3093f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3094{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003095 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003096}
3097
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003098static garray_T redir_execute_ga;
3099
3100/*
3101 * Append "value[value_len]" to the execute() output.
3102 */
3103 void
3104execute_redir_str(char_u *value, int value_len)
3105{
3106 int len;
3107
3108 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003109 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003110 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003111 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003112 if (ga_grow(&redir_execute_ga, len) == OK)
3113 {
3114 mch_memmove((char *)redir_execute_ga.ga_data
3115 + redir_execute_ga.ga_len, value, len);
3116 redir_execute_ga.ga_len += len;
3117 }
3118}
3119
3120/*
3121 * Get next line from a list.
3122 * Called by do_cmdline() to get the next line.
3123 * Returns allocated string, or NULL for end of function.
3124 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003125 static char_u *
3126get_list_line(
3127 int c UNUSED,
3128 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003129 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02003130 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003131{
3132 listitem_T **p = (listitem_T **)cookie;
3133 listitem_T *item = *p;
3134 char_u buf[NUMBUFLEN];
3135 char_u *s;
3136
3137 if (item == NULL)
3138 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003139 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003140 *p = item->li_next;
3141 return s == NULL ? NULL : vim_strsave(s);
3142}
3143
3144/*
3145 * "execute()" function
3146 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02003147 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003148execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003149{
3150 char_u *cmd = NULL;
3151 list_T *list = NULL;
3152 int save_msg_silent = msg_silent;
3153 int save_emsg_silent = emsg_silent;
3154 int save_emsg_noredir = emsg_noredir;
3155 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003156 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003157 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003158 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003159 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003160
3161 rettv->vval.v_string = NULL;
3162 rettv->v_type = VAR_STRING;
3163
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003164 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003165 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003166 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003167 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003168 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003169 return;
3170 ++list->lv_refcount;
3171 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003172 else if (argvars[arg_off].v_type == VAR_JOB
3173 || argvars[arg_off].v_type == VAR_CHANNEL)
3174 {
3175 emsg(_(e_inval_string));
3176 return;
3177 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003178 else
3179 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003180 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003181 if (cmd == NULL)
3182 return;
3183 }
3184
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003185 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003186 {
3187 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003188 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003189
3190 if (s == NULL)
3191 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003192 if (*s == NUL)
3193 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003194 if (STRNCMP(s, "silent", 6) == 0)
3195 ++msg_silent;
3196 if (STRCMP(s, "silent!") == 0)
3197 {
3198 emsg_silent = TRUE;
3199 emsg_noredir = TRUE;
3200 }
3201 }
3202 else
3203 ++msg_silent;
3204
3205 if (redir_execute)
3206 save_ga = redir_execute_ga;
3207 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3208 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003209 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003210 if (!echo_output)
3211 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003212
3213 if (cmd != NULL)
3214 do_cmdline_cmd(cmd);
3215 else
3216 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003217 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003218
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003219 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003220 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003221 do_cmdline(NULL, get_list_line, (void *)&item,
3222 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3223 --list->lv_refcount;
3224 }
3225
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003226 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003227 if (ga_grow(&redir_execute_ga, 1) == OK)
3228 {
3229 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3230 rettv->vval.v_string = redir_execute_ga.ga_data;
3231 }
3232 else
3233 {
3234 ga_clear(&redir_execute_ga);
3235 rettv->vval.v_string = NULL;
3236 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003237 msg_silent = save_msg_silent;
3238 emsg_silent = save_emsg_silent;
3239 emsg_noredir = save_emsg_noredir;
3240
3241 redir_execute = save_redir_execute;
3242 if (redir_execute)
3243 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003244 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003245
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003246 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003247 if (echo_output)
3248 // When not working silently: put it in column zero. A following
3249 // "echon" will overwrite the message, unavoidably.
3250 msg_col = 0;
3251 else
3252 // When working silently: Put it back where it was, since nothing
3253 // should have been written.
3254 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003255}
3256
3257/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003258 * "execute()" function
3259 */
3260 static void
3261f_execute(typval_T *argvars, typval_T *rettv)
3262{
3263 execute_common(argvars, rettv, 0);
3264}
3265
3266/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003267 * "exists()" function
3268 */
3269 static void
3270f_exists(typval_T *argvars, typval_T *rettv)
3271{
3272 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003273 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003274
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003275 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003276 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003277 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003278 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003279 if (mch_getenv(p + 1) != NULL)
3280 n = TRUE;
3281 else
3282 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003283 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003284 p = expand_env_save(p);
3285 if (p != NULL && *p != '$')
3286 n = TRUE;
3287 vim_free(p);
3288 }
3289 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003290 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003291 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003292 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003293 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003294 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003295 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003296 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003297 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003298 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003300 else if (*p == '?') // internal function only
3301 {
3302 n = has_internal_func_name(p + 1);
3303 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003304 else if (*p == ':')
3305 {
3306 n = cmd_exists(p + 1);
3307 }
3308 else if (*p == '#')
3309 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003310 if (p[1] == '#')
3311 n = autocmd_supported(p + 2);
3312 else
3313 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003314 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003315 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003316 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003317 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003318 }
3319
3320 rettv->vval.v_number = n;
3321}
3322
3323#ifdef FEAT_FLOAT
3324/*
3325 * "exp()" function
3326 */
3327 static void
3328f_exp(typval_T *argvars, typval_T *rettv)
3329{
3330 float_T f = 0.0;
3331
3332 rettv->v_type = VAR_FLOAT;
3333 if (get_float_arg(argvars, &f) == OK)
3334 rettv->vval.v_float = exp(f);
3335 else
3336 rettv->vval.v_float = 0.0;
3337}
3338#endif
3339
3340/*
3341 * "expand()" function
3342 */
3343 static void
3344f_expand(typval_T *argvars, typval_T *rettv)
3345{
3346 char_u *s;
3347 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003348 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003349 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3350 expand_T xpc;
3351 int error = FALSE;
3352 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003353#ifdef BACKSLASH_IN_FILENAME
3354 char_u *p_csl_save = p_csl;
3355
3356 // avoid using 'completeslash' here
3357 p_csl = empty_option;
3358#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003359
3360 rettv->v_type = VAR_STRING;
3361 if (argvars[1].v_type != VAR_UNKNOWN
3362 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003363 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003364 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003365 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003366
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003367 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003368 if (*s == '%' || *s == '#' || *s == '<')
3369 {
3370 ++emsg_off;
3371 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3372 --emsg_off;
3373 if (rettv->v_type == VAR_LIST)
3374 {
3375 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3376 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003377 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003378 }
3379 else
3380 rettv->vval.v_string = result;
3381 }
3382 else
3383 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003384 // When the optional second argument is non-zero, don't remove matches
3385 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003386 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003387 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003388 options |= WILD_KEEP_ALL;
3389 if (!error)
3390 {
3391 ExpandInit(&xpc);
3392 xpc.xp_context = EXPAND_FILES;
3393 if (p_wic)
3394 options += WILD_ICASE;
3395 if (rettv->v_type == VAR_STRING)
3396 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3397 options, WILD_ALL);
3398 else if (rettv_list_alloc(rettv) != FAIL)
3399 {
3400 int i;
3401
3402 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3403 for (i = 0; i < xpc.xp_numfiles; i++)
3404 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3405 ExpandCleanup(&xpc);
3406 }
3407 }
3408 else
3409 rettv->vval.v_string = NULL;
3410 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003411#ifdef BACKSLASH_IN_FILENAME
3412 p_csl = p_csl_save;
3413#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003414}
3415
3416/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003417 * "expandcmd()" function
3418 * Expand all the special characters in a command string.
3419 */
3420 static void
3421f_expandcmd(typval_T *argvars, typval_T *rettv)
3422{
3423 exarg_T eap;
3424 char_u *cmdstr;
3425 char *errormsg = NULL;
3426
3427 rettv->v_type = VAR_STRING;
3428 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3429
3430 memset(&eap, 0, sizeof(eap));
3431 eap.cmd = cmdstr;
3432 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003433 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003434 eap.usefilter = FALSE;
3435 eap.nextcmd = NULL;
3436 eap.cmdidx = CMD_USER;
3437
3438 expand_filename(&eap, &cmdstr, &errormsg);
3439 if (errormsg != NULL && *errormsg != NUL)
3440 emsg(errormsg);
3441
3442 rettv->vval.v_string = cmdstr;
3443}
3444
3445/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003446 * "feedkeys()" function
3447 */
3448 static void
3449f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3450{
3451 int remap = TRUE;
3452 int insert = FALSE;
3453 char_u *keys, *flags;
3454 char_u nbuf[NUMBUFLEN];
3455 int typed = FALSE;
3456 int execute = FALSE;
3457 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003458 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003459 char_u *keys_esc;
3460
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003461 // This is not allowed in the sandbox. If the commands would still be
3462 // executed in the sandbox it would be OK, but it probably happens later,
3463 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003464 if (check_secure())
3465 return;
3466
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003467 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003468
3469 if (argvars[1].v_type != VAR_UNKNOWN)
3470 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003471 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003472 for ( ; *flags != NUL; ++flags)
3473 {
3474 switch (*flags)
3475 {
3476 case 'n': remap = FALSE; break;
3477 case 'm': remap = TRUE; break;
3478 case 't': typed = TRUE; break;
3479 case 'i': insert = TRUE; break;
3480 case 'x': execute = TRUE; break;
3481 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003482 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003483 }
3484 }
3485 }
3486
3487 if (*keys != NUL || execute)
3488 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003489 // Need to escape K_SPECIAL and CSI before putting the string in the
3490 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003491 keys_esc = vim_strsave_escape_csi(keys);
3492 if (keys_esc != NULL)
3493 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003494 if (lowlevel)
3495 {
3496#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003497 int idx;
3498 int len = (int)STRLEN(keys);
3499
3500 for (idx = 0; idx < len; ++idx)
3501 {
3502 // if a CTRL-C was typed, set got_int, similar to what
3503 // happens in fill_input_buf()
3504 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3505 got_int = TRUE;
3506 add_to_input_buf(keys + idx, 1);
3507 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003508#else
3509 emsg(_("E980: lowlevel input not supported"));
3510#endif
3511 }
3512 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003513 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003514 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003515 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003516 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003517#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003518 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003519#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003520 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003521 typebuf_was_filled = TRUE;
3522 }
3523 vim_free(keys_esc);
3524
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003525 if (execute)
3526 {
3527 int save_msg_scroll = msg_scroll;
3528
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003529 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003530 msg_scroll = FALSE;
3531
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003532 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003533 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003534 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003535 ++in_feedkeys;
3536 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003537 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003538 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003539 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003540 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003541 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003542 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003543
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003544 msg_scroll |= save_msg_scroll;
3545 }
3546 }
3547 }
3548}
3549
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003550#ifdef FEAT_FLOAT
3551/*
3552 * "float2nr({float})" function
3553 */
3554 static void
3555f_float2nr(typval_T *argvars, typval_T *rettv)
3556{
3557 float_T f = 0.0;
3558
3559 if (get_float_arg(argvars, &f) == OK)
3560 {
Bram Moolenaar37184272020-05-23 19:30:05 +02003561 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003562 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02003563 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003564 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003565 else
3566 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003567 }
3568}
3569
3570/*
3571 * "floor({float})" function
3572 */
3573 static void
3574f_floor(typval_T *argvars, typval_T *rettv)
3575{
3576 float_T f = 0.0;
3577
3578 rettv->v_type = VAR_FLOAT;
3579 if (get_float_arg(argvars, &f) == OK)
3580 rettv->vval.v_float = floor(f);
3581 else
3582 rettv->vval.v_float = 0.0;
3583}
3584
3585/*
3586 * "fmod()" function
3587 */
3588 static void
3589f_fmod(typval_T *argvars, typval_T *rettv)
3590{
3591 float_T fx = 0.0, fy = 0.0;
3592
3593 rettv->v_type = VAR_FLOAT;
3594 if (get_float_arg(argvars, &fx) == OK
3595 && get_float_arg(&argvars[1], &fy) == OK)
3596 rettv->vval.v_float = fmod(fx, fy);
3597 else
3598 rettv->vval.v_float = 0.0;
3599}
3600#endif
3601
3602/*
3603 * "fnameescape({string})" function
3604 */
3605 static void
3606f_fnameescape(typval_T *argvars, typval_T *rettv)
3607{
3608 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003609 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003610 rettv->v_type = VAR_STRING;
3611}
3612
3613/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003614 * "foreground()" function
3615 */
3616 static void
3617f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3618{
3619#ifdef FEAT_GUI
3620 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003621 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003622 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003623 return;
3624 }
3625#endif
3626#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003627 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003628#endif
3629}
3630
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003631 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003632common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003633{
3634 char_u *s;
3635 char_u *name;
3636 int use_string = FALSE;
3637 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003638 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003639 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003640
3641 if (argvars[0].v_type == VAR_FUNC)
3642 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003643 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003644 s = argvars[0].vval.v_string;
3645 }
3646 else if (argvars[0].v_type == VAR_PARTIAL
3647 && argvars[0].vval.v_partial != NULL)
3648 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003649 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003650 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003651 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003652 }
3653 else
3654 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003655 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003656 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003657 use_string = TRUE;
3658 }
3659
Bram Moolenaar843b8842016-08-21 14:36:15 +02003660 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003661 {
3662 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003663 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003664 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3665 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003666 if (*name != NUL)
3667 s = NULL;
3668 }
3669
Bram Moolenaar843b8842016-08-21 14:36:15 +02003670 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3671 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003672 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003673 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003674 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003675 ? find_func(trans_name, is_global, NULL) == NULL
3676 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003677 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003678 else
3679 {
3680 int dict_idx = 0;
3681 int arg_idx = 0;
3682 list_T *list = NULL;
3683
3684 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3685 {
3686 char sid_buf[25];
3687 int off = *s == 's' ? 2 : 5;
3688
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003689 // Expand s: and <SID> into <SNR>nr_, so that the function can
3690 // also be called from another script. Using trans_function_name()
3691 // would also work, but some plugins depend on the name being
3692 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003693 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003694 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003695 if (name != NULL)
3696 {
3697 STRCPY(name, sid_buf);
3698 STRCAT(name, s + off);
3699 }
3700 }
3701 else
3702 name = vim_strsave(s);
3703
3704 if (argvars[1].v_type != VAR_UNKNOWN)
3705 {
3706 if (argvars[2].v_type != VAR_UNKNOWN)
3707 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003708 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003709 arg_idx = 1;
3710 dict_idx = 2;
3711 }
3712 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003713 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003714 dict_idx = 1;
3715 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003716 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003717 arg_idx = 1;
3718 if (dict_idx > 0)
3719 {
3720 if (argvars[dict_idx].v_type != VAR_DICT)
3721 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003722 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003723 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003724 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003725 }
3726 if (argvars[dict_idx].vval.v_dict == NULL)
3727 dict_idx = 0;
3728 }
3729 if (arg_idx > 0)
3730 {
3731 if (argvars[arg_idx].v_type != VAR_LIST)
3732 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003733 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003734 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003735 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003736 }
3737 list = argvars[arg_idx].vval.v_list;
3738 if (list == NULL || list->lv_len == 0)
3739 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003740 else if (list->lv_len > MAX_FUNC_ARGS)
3741 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003742 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003743 vim_free(name);
3744 goto theend;
3745 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003746 }
3747 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003748 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003749 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003750 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003751
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003752 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003753 if (pt == NULL)
3754 vim_free(name);
3755 else
3756 {
3757 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3758 {
3759 listitem_T *li;
3760 int i = 0;
3761 int arg_len = 0;
3762 int lv_len = 0;
3763
3764 if (arg_pt != NULL)
3765 arg_len = arg_pt->pt_argc;
3766 if (list != NULL)
3767 lv_len = list->lv_len;
3768 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003769 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003770 if (pt->pt_argv == NULL)
3771 {
3772 vim_free(pt);
3773 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003774 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003775 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003776 for (i = 0; i < arg_len; i++)
3777 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3778 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003779 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003780 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003781 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003782 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003783 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003784 }
3785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003786 // For "function(dict.func, [], dict)" and "func" is a partial
3787 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003788 if (dict_idx > 0)
3789 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003790 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003791 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3792 ++pt->pt_dict->dv_refcount;
3793 }
3794 else if (arg_pt != NULL)
3795 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003796 // If the dict was bound automatically the result is also
3797 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003798 pt->pt_dict = arg_pt->pt_dict;
3799 pt->pt_auto = arg_pt->pt_auto;
3800 if (pt->pt_dict != NULL)
3801 ++pt->pt_dict->dv_refcount;
3802 }
3803
3804 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003805 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3806 {
3807 pt->pt_func = arg_pt->pt_func;
3808 func_ptr_ref(pt->pt_func);
3809 vim_free(name);
3810 }
3811 else if (is_funcref)
3812 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003813 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003814 func_ptr_ref(pt->pt_func);
3815 vim_free(name);
3816 }
3817 else
3818 {
3819 pt->pt_name = name;
3820 func_ref(name);
3821 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003822 }
3823 rettv->v_type = VAR_PARTIAL;
3824 rettv->vval.v_partial = pt;
3825 }
3826 else
3827 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003828 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003829 rettv->v_type = VAR_FUNC;
3830 rettv->vval.v_string = name;
3831 func_ref(name);
3832 }
3833 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003834theend:
3835 vim_free(trans_name);
3836}
3837
3838/*
3839 * "funcref()" function
3840 */
3841 static void
3842f_funcref(typval_T *argvars, typval_T *rettv)
3843{
3844 common_function(argvars, rettv, TRUE);
3845}
3846
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003847 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003848ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003849{
3850 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3851 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003852 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003853}
3854
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003855/*
3856 * "function()" function
3857 */
3858 static void
3859f_function(typval_T *argvars, typval_T *rettv)
3860{
3861 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003862}
3863
3864/*
3865 * "garbagecollect()" function
3866 */
3867 static void
3868f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3869{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003870 // This is postponed until we are back at the toplevel, because we may be
3871 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003872 want_garbage_collect = TRUE;
3873
Bram Moolenaar2df47312020-09-05 17:30:44 +02003874 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003875 garbage_collect_at_exit = TRUE;
3876}
3877
3878/*
3879 * "get()" function
3880 */
3881 static void
3882f_get(typval_T *argvars, typval_T *rettv)
3883{
3884 listitem_T *li;
3885 list_T *l;
3886 dictitem_T *di;
3887 dict_T *d;
3888 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003889 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003890
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003891 if (argvars[0].v_type == VAR_BLOB)
3892 {
3893 int error = FALSE;
3894 int idx = tv_get_number_chk(&argvars[1], &error);
3895
3896 if (!error)
3897 {
3898 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003899 if (idx < 0)
3900 idx = blob_len(argvars[0].vval.v_blob) + idx;
3901 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3902 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003903 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003904 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003905 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003906 tv = rettv;
3907 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003908 }
3909 }
3910 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003911 {
3912 if ((l = argvars[0].vval.v_list) != NULL)
3913 {
3914 int error = FALSE;
3915
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003916 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003917 if (!error && li != NULL)
3918 tv = &li->li_tv;
3919 }
3920 }
3921 else if (argvars[0].v_type == VAR_DICT)
3922 {
3923 if ((d = argvars[0].vval.v_dict) != NULL)
3924 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003925 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003926 if (di != NULL)
3927 tv = &di->di_tv;
3928 }
3929 }
3930 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3931 {
3932 partial_T *pt;
3933 partial_T fref_pt;
3934
3935 if (argvars[0].v_type == VAR_PARTIAL)
3936 pt = argvars[0].vval.v_partial;
3937 else
3938 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003939 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003940 fref_pt.pt_name = argvars[0].vval.v_string;
3941 pt = &fref_pt;
3942 }
3943
3944 if (pt != NULL)
3945 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003946 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003947 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003948
3949 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3950 {
3951 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003952 n = partial_name(pt);
3953 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003954 rettv->vval.v_string = NULL;
3955 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003956 {
3957 rettv->vval.v_string = vim_strsave(n);
3958 if (rettv->v_type == VAR_FUNC)
3959 func_ref(rettv->vval.v_string);
3960 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003961 }
3962 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003963 {
3964 what_is_dict = TRUE;
3965 if (pt->pt_dict != NULL)
3966 rettv_dict_set(rettv, pt->pt_dict);
3967 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003968 else if (STRCMP(what, "args") == 0)
3969 {
3970 rettv->v_type = VAR_LIST;
3971 if (rettv_list_alloc(rettv) == OK)
3972 {
3973 int i;
3974
3975 for (i = 0; i < pt->pt_argc; ++i)
3976 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3977 }
3978 }
3979 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003980 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003981
3982 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3983 // third argument
3984 if (!what_is_dict)
3985 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003986 }
3987 }
3988 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003989 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003990
3991 if (tv == NULL)
3992 {
3993 if (argvars[2].v_type != VAR_UNKNOWN)
3994 copy_tv(&argvars[2], rettv);
3995 }
3996 else
3997 copy_tv(tv, rettv);
3998}
3999
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004000/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004001 * "getchangelist()" function
4002 */
4003 static void
4004f_getchangelist(typval_T *argvars, typval_T *rettv)
4005{
4006#ifdef FEAT_JUMPLIST
4007 buf_T *buf;
4008 int i;
4009 list_T *l;
4010 dict_T *d;
4011#endif
4012
4013 if (rettv_list_alloc(rettv) != OK)
4014 return;
4015
4016#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02004017 if (argvars[0].v_type == VAR_UNKNOWN)
4018 buf = curbuf;
4019 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02004020 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004021 if (buf == NULL)
4022 return;
4023
4024 l = list_alloc();
4025 if (l == NULL)
4026 return;
4027
4028 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4029 return;
4030 /*
4031 * The current window change list index tracks only the position in the
4032 * current buffer change list. For other buffers, use the change list
4033 * length as the current index.
4034 */
4035 list_append_number(rettv->vval.v_list,
4036 (varnumber_T)((buf == curwin->w_buffer)
4037 ? curwin->w_changelistidx : buf->b_changelistlen));
4038
4039 for (i = 0; i < buf->b_changelistlen; ++i)
4040 {
4041 if (buf->b_changelist[i].lnum == 0)
4042 continue;
4043 if ((d = dict_alloc()) == NULL)
4044 return;
4045 if (list_append_dict(l, d) == FAIL)
4046 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004047 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4048 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004049 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004050 }
4051#endif
4052}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004053
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004054 static void
4055getpos_both(
4056 typval_T *argvars,
4057 typval_T *rettv,
4058 int getcurpos,
4059 int charcol)
4060{
4061 pos_T *fp = NULL;
4062 pos_T pos;
4063 win_T *wp = curwin;
4064 list_T *l;
4065 int fnum = -1;
4066
4067 if (rettv_list_alloc(rettv) == OK)
4068 {
4069 l = rettv->vval.v_list;
4070 if (getcurpos)
4071 {
4072 if (argvars[0].v_type != VAR_UNKNOWN)
4073 {
4074 wp = find_win_by_nr_or_id(&argvars[0]);
4075 if (wp != NULL)
4076 fp = &wp->w_cursor;
4077 }
4078 else
4079 fp = &curwin->w_cursor;
4080 if (fp != NULL && charcol)
4081 {
4082 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01004083 pos.col =
4084 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004085 fp = &pos;
4086 }
4087 }
4088 else
4089 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
4090 if (fnum != -1)
4091 list_append_number(l, (varnumber_T)fnum);
4092 else
4093 list_append_number(l, (varnumber_T)0);
4094 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4095 : (varnumber_T)0);
4096 list_append_number(l, (fp != NULL)
4097 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4098 : (varnumber_T)0);
4099 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
4100 (varnumber_T)0);
4101 if (getcurpos)
4102 {
4103 int save_set_curswant = curwin->w_set_curswant;
4104 colnr_T save_curswant = curwin->w_curswant;
4105 colnr_T save_virtcol = curwin->w_virtcol;
4106
4107 if (wp == curwin)
4108 update_curswant();
4109 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
4110 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
4111
4112 // Do not change "curswant", as it is unexpected that a get
4113 // function has a side effect.
4114 if (wp == curwin && save_set_curswant)
4115 {
4116 curwin->w_set_curswant = save_set_curswant;
4117 curwin->w_curswant = save_curswant;
4118 curwin->w_virtcol = save_virtcol;
4119 curwin->w_valid &= ~VALID_VIRTCOL;
4120 }
4121 }
4122 }
4123 else
4124 rettv->vval.v_number = FALSE;
4125}
4126
4127/*
4128 * "getcharpos()" function
4129 */
4130 static void
4131f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
4132{
4133 getpos_both(argvars, rettv, FALSE, TRUE);
4134}
4135
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004136/*
4137 * "getcharsearch()" function
4138 */
4139 static void
4140f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4141{
4142 if (rettv_dict_alloc(rettv) != FAIL)
4143 {
4144 dict_T *dict = rettv->vval.v_dict;
4145
Bram Moolenaare0be1672018-07-08 16:50:37 +02004146 dict_add_string(dict, "char", last_csearch());
4147 dict_add_number(dict, "forward", last_csearch_forward());
4148 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004149 }
4150}
4151
4152/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004153 * "getenv()" function
4154 */
4155 static void
4156f_getenv(typval_T *argvars, typval_T *rettv)
4157{
4158 int mustfree = FALSE;
4159 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4160
4161 if (p == NULL)
4162 {
4163 rettv->v_type = VAR_SPECIAL;
4164 rettv->vval.v_number = VVAL_NULL;
4165 return;
4166 }
4167 if (!mustfree)
4168 p = vim_strsave(p);
4169 rettv->vval.v_string = p;
4170 rettv->v_type = VAR_STRING;
4171}
4172
4173/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004174 * "getfontname()" function
4175 */
4176 static void
4177f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4178{
4179 rettv->v_type = VAR_STRING;
4180 rettv->vval.v_string = NULL;
4181#ifdef FEAT_GUI
4182 if (gui.in_use)
4183 {
4184 GuiFont font;
4185 char_u *name = NULL;
4186
4187 if (argvars[0].v_type == VAR_UNKNOWN)
4188 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004189 // Get the "Normal" font. Either the name saved by
4190 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004191 font = gui.norm_font;
4192 name = hl_get_font_name();
4193 }
4194 else
4195 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004196 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004197 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004198 return;
4199 font = gui_mch_get_font(name, FALSE);
4200 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004201 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004202 }
4203 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4204 if (argvars[0].v_type != VAR_UNKNOWN)
4205 gui_mch_free_font(font);
4206 }
4207#endif
4208}
4209
4210/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004211 * "getjumplist()" function
4212 */
4213 static void
4214f_getjumplist(typval_T *argvars, typval_T *rettv)
4215{
4216#ifdef FEAT_JUMPLIST
4217 win_T *wp;
4218 int i;
4219 list_T *l;
4220 dict_T *d;
4221#endif
4222
4223 if (rettv_list_alloc(rettv) != OK)
4224 return;
4225
4226#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004227 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004228 if (wp == NULL)
4229 return;
4230
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004231 cleanup_jumplist(wp, TRUE);
4232
Bram Moolenaar4f505882018-02-10 21:06:32 +01004233 l = list_alloc();
4234 if (l == NULL)
4235 return;
4236
4237 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4238 return;
4239 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4240
4241 for (i = 0; i < wp->w_jumplistlen; ++i)
4242 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004243 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4244 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004245 if ((d = dict_alloc()) == NULL)
4246 return;
4247 if (list_append_dict(l, d) == FAIL)
4248 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004249 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4250 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004251 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004252 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004253 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004254 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004255 }
4256#endif
4257}
4258
4259/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004260 * "getpid()" function
4261 */
4262 static void
4263f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4264{
4265 rettv->vval.v_number = mch_get_pid();
4266}
4267
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004268/*
4269 * "getcurpos()" function
4270 */
4271 static void
4272f_getcurpos(typval_T *argvars, typval_T *rettv)
4273{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004274 getpos_both(argvars, rettv, TRUE, FALSE);
4275}
4276
4277 static void
4278f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4279{
4280 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004281}
4282
4283/*
4284 * "getpos(string)" function
4285 */
4286 static void
4287f_getpos(typval_T *argvars, typval_T *rettv)
4288{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004289 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004290}
4291
4292/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004293 * "getreg()" function
4294 */
4295 static void
4296f_getreg(typval_T *argvars, typval_T *rettv)
4297{
4298 char_u *strregname;
4299 int regname;
4300 int arg2 = FALSE;
4301 int return_list = FALSE;
4302 int error = FALSE;
4303
4304 if (argvars[0].v_type != VAR_UNKNOWN)
4305 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004306 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004307 if (strregname == NULL)
4308 error = TRUE;
4309 else if (in_vim9script() && STRLEN(strregname) > 1)
4310 {
4311 semsg(_(e_register_name_must_be_one_char_str), strregname);
4312 error = TRUE;
4313 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004314 if (argvars[1].v_type != VAR_UNKNOWN)
4315 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004316 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004317 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004318 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004319 }
4320 }
4321 else
4322 strregname = get_vim_var_str(VV_REG);
4323
4324 if (error)
4325 return;
4326
4327 regname = (strregname == NULL ? '"' : *strregname);
4328 if (regname == 0)
4329 regname = '"';
4330
4331 if (return_list)
4332 {
4333 rettv->v_type = VAR_LIST;
4334 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4335 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4336 if (rettv->vval.v_list == NULL)
4337 (void)rettv_list_alloc(rettv);
4338 else
4339 ++rettv->vval.v_list->lv_refcount;
4340 }
4341 else
4342 {
4343 rettv->v_type = VAR_STRING;
4344 rettv->vval.v_string = get_reg_contents(regname,
4345 arg2 ? GREG_EXPR_SRC : 0);
4346 }
4347}
4348
4349/*
4350 * "getregtype()" function
4351 */
4352 static void
4353f_getregtype(typval_T *argvars, typval_T *rettv)
4354{
4355 char_u *strregname;
4356 int regname;
4357 char_u buf[NUMBUFLEN + 2];
4358 long reglen = 0;
4359
4360 if (argvars[0].v_type != VAR_UNKNOWN)
4361 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004362 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004363 if (strregname != NULL && in_vim9script() && STRLEN(strregname) > 1)
4364 {
4365 semsg(_(e_register_name_must_be_one_char_str), strregname);
4366 strregname = NULL;
4367 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004368 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004369 {
4370 rettv->v_type = VAR_STRING;
4371 rettv->vval.v_string = NULL;
4372 return;
4373 }
4374 }
4375 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004376 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004377 strregname = get_vim_var_str(VV_REG);
4378
4379 regname = (strregname == NULL ? '"' : *strregname);
4380 if (regname == 0)
4381 regname = '"';
4382
4383 buf[0] = NUL;
4384 buf[1] = NUL;
4385 switch (get_reg_type(regname, &reglen))
4386 {
4387 case MLINE: buf[0] = 'V'; break;
4388 case MCHAR: buf[0] = 'v'; break;
4389 case MBLOCK:
4390 buf[0] = Ctrl_V;
4391 sprintf((char *)buf + 1, "%ld", reglen + 1);
4392 break;
4393 }
4394 rettv->v_type = VAR_STRING;
4395 rettv->vval.v_string = vim_strsave(buf);
4396}
4397
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004398/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004399 * "gettagstack()" function
4400 */
4401 static void
4402f_gettagstack(typval_T *argvars, typval_T *rettv)
4403{
4404 win_T *wp = curwin; // default is current window
4405
4406 if (rettv_dict_alloc(rettv) != OK)
4407 return;
4408
4409 if (argvars[0].v_type != VAR_UNKNOWN)
4410 {
4411 wp = find_win_by_nr_or_id(&argvars[0]);
4412 if (wp == NULL)
4413 return;
4414 }
4415
4416 get_tagstack(wp, rettv->vval.v_dict);
4417}
4418
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004419/*
4420 * "gettext()" function
4421 */
4422 static void
4423f_gettext(typval_T *argvars, typval_T *rettv)
4424{
4425 if (argvars[0].v_type != VAR_STRING
4426 || argvars[0].vval.v_string == NULL
4427 || *argvars[0].vval.v_string == NUL)
4428 {
4429 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4430 }
4431 else
4432 {
4433 rettv->v_type = VAR_STRING;
4434 rettv->vval.v_string = vim_strsave(
4435 (char_u *)_(argvars[0].vval.v_string));
4436 }
4437}
4438
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004439// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004440#include "version.h"
4441
4442/*
4443 * "has()" function
4444 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004445 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004446f_has(typval_T *argvars, typval_T *rettv)
4447{
4448 int i;
4449 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004450 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004451 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004452 typedef struct {
4453 char *name;
4454 short present;
4455 } has_item_T;
4456 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004457 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004458 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004459#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004460 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004461#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004462 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004463#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004464 },
4465 {"arp",
4466#if defined(AMIGA) && defined(FEAT_ARP)
4467 1
4468#else
4469 0
4470#endif
4471 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004472 {"haiku",
4473#ifdef __HAIKU__
4474 1
4475#else
4476 0
4477#endif
4478 },
4479 {"bsd",
4480#if defined(BSD) && !defined(MACOS_X)
4481 1
4482#else
4483 0
4484#endif
4485 },
4486 {"hpux",
4487#ifdef hpux
4488 1
4489#else
4490 0
4491#endif
4492 },
4493 {"linux",
4494#ifdef __linux__
4495 1
4496#else
4497 0
4498#endif
4499 },
4500 {"mac", // Mac OS X (and, once, Mac OS Classic)
4501#ifdef MACOS_X
4502 1
4503#else
4504 0
4505#endif
4506 },
4507 {"osx", // Mac OS X
4508#ifdef MACOS_X
4509 1
4510#else
4511 0
4512#endif
4513 },
4514 {"macunix", // Mac OS X, with the darwin feature
4515#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4516 1
4517#else
4518 0
4519#endif
4520 },
4521 {"osxdarwin", // synonym for macunix
4522#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4523 1
4524#else
4525 0
4526#endif
4527 },
4528 {"qnx",
4529#ifdef __QNX__
4530 1
4531#else
4532 0
4533#endif
4534 },
4535 {"sun",
4536#ifdef SUN_SYSTEM
4537 1
4538#else
4539 0
4540#endif
4541 },
4542 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004543#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004544 1
4545#else
4546 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004547#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004548 },
4549 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004550#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004551 1
4552#else
4553 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004554#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004555 },
4556 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004557#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004558 1
4559#else
4560 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004561#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004562 },
4563 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004564#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004565 1
4566#else
4567 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004568#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004569 },
4570 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004571#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004572 1
4573#else
4574 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004575#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004576 },
4577 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004578#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004579 1
4580#else
4581 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004582#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004583 },
4584 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004585#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004586 1
4587#else
4588 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004589#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004590 },
4591 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004592#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004593 1
4594#else
4595 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004596#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004597 },
4598 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004599#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004600 1
4601#else
4602 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004603#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004604 },
4605 {"autocmd", 1},
4606 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004607#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004608 1
4609#else
4610 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004611#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004612 },
4613 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004614#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004615 1
4616#else
4617 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004618#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004619 },
4620 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004621#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004622 1
4623#else
4624 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004625#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004626 },
4627 {"balloon_multiline",
4628#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4629 // MS-Windows requires runtime check, see below
4630 1
4631#else
4632 0
4633#endif
4634 },
4635 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004636#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004637 1
4638#else
4639 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004640#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004641 },
4642 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004643#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004644 1
4645#else
4646 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004647#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004648 },
4649 {"all_builtin_terms",
4650#if defined(ALL_BUILTIN_TCAPS)
4651 1
4652#else
4653 0
4654#endif
4655 },
4656 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004657#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004658 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004659 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004660 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004661#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004662 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004663#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004664 },
4665 {"byte_offset",
4666#ifdef FEAT_BYTEOFF
4667 1
4668#else
4669 0
4670#endif
4671 },
4672 {"channel",
4673#ifdef FEAT_JOB_CHANNEL
4674 1
4675#else
4676 0
4677#endif
4678 },
4679 {"cindent",
4680#ifdef FEAT_CINDENT
4681 1
4682#else
4683 0
4684#endif
4685 },
4686 {"clientserver",
4687#ifdef FEAT_CLIENTSERVER
4688 1
4689#else
4690 0
4691#endif
4692 },
4693 {"clipboard",
4694#ifdef FEAT_CLIPBOARD
4695 1
4696#else
4697 0
4698#endif
4699 },
4700 {"cmdline_compl", 1},
4701 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004702 {"cmdwin",
4703#ifdef FEAT_CMDWIN
4704 1
4705#else
4706 0
4707#endif
4708 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004709 {"comments", 1},
4710 {"conceal",
4711#ifdef FEAT_CONCEAL
4712 1
4713#else
4714 0
4715#endif
4716 },
4717 {"cryptv",
4718#ifdef FEAT_CRYPT
4719 1
4720#else
4721 0
4722#endif
4723 },
4724 {"crypt-blowfish",
4725#ifdef FEAT_CRYPT
4726 1
4727#else
4728 0
4729#endif
4730 },
4731 {"crypt-blowfish2",
4732#ifdef FEAT_CRYPT
4733 1
4734#else
4735 0
4736#endif
4737 },
4738 {"cscope",
4739#ifdef FEAT_CSCOPE
4740 1
4741#else
4742 0
4743#endif
4744 },
4745 {"cursorbind", 1},
4746 {"cursorshape",
4747#ifdef CURSOR_SHAPE
4748 1
4749#else
4750 0
4751#endif
4752 },
4753 {"debug",
4754#ifdef DEBUG
4755 1
4756#else
4757 0
4758#endif
4759 },
4760 {"dialog_con",
4761#ifdef FEAT_CON_DIALOG
4762 1
4763#else
4764 0
4765#endif
4766 },
4767 {"dialog_gui",
4768#ifdef FEAT_GUI_DIALOG
4769 1
4770#else
4771 0
4772#endif
4773 },
4774 {"diff",
4775#ifdef FEAT_DIFF
4776 1
4777#else
4778 0
4779#endif
4780 },
4781 {"digraphs",
4782#ifdef FEAT_DIGRAPHS
4783 1
4784#else
4785 0
4786#endif
4787 },
4788 {"directx",
4789#ifdef FEAT_DIRECTX
4790 1
4791#else
4792 0
4793#endif
4794 },
4795 {"dnd",
4796#ifdef FEAT_DND
4797 1
4798#else
4799 0
4800#endif
4801 },
4802 {"emacs_tags",
4803#ifdef FEAT_EMACS_TAGS
4804 1
4805#else
4806 0
4807#endif
4808 },
4809 {"eval", 1}, // always present, of course!
4810 {"ex_extra", 1}, // graduated feature
4811 {"extra_search",
4812#ifdef FEAT_SEARCH_EXTRA
4813 1
4814#else
4815 0
4816#endif
4817 },
4818 {"file_in_path",
4819#ifdef FEAT_SEARCHPATH
4820 1
4821#else
4822 0
4823#endif
4824 },
4825 {"filterpipe",
4826#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4827 1
4828#else
4829 0
4830#endif
4831 },
4832 {"find_in_path",
4833#ifdef FEAT_FIND_ID
4834 1
4835#else
4836 0
4837#endif
4838 },
4839 {"float",
4840#ifdef FEAT_FLOAT
4841 1
4842#else
4843 0
4844#endif
4845 },
4846 {"folding",
4847#ifdef FEAT_FOLDING
4848 1
4849#else
4850 0
4851#endif
4852 },
4853 {"footer",
4854#ifdef FEAT_FOOTER
4855 1
4856#else
4857 0
4858#endif
4859 },
4860 {"fork",
4861#if !defined(USE_SYSTEM) && defined(UNIX)
4862 1
4863#else
4864 0
4865#endif
4866 },
4867 {"gettext",
4868#ifdef FEAT_GETTEXT
4869 1
4870#else
4871 0
4872#endif
4873 },
4874 {"gui",
4875#ifdef FEAT_GUI
4876 1
4877#else
4878 0
4879#endif
4880 },
4881 {"gui_neXtaw",
4882#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4883 1
4884#else
4885 0
4886#endif
4887 },
4888 {"gui_athena",
4889#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4890 1
4891#else
4892 0
4893#endif
4894 },
4895 {"gui_gtk",
4896#ifdef FEAT_GUI_GTK
4897 1
4898#else
4899 0
4900#endif
4901 },
4902 {"gui_gtk2",
4903#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4904 1
4905#else
4906 0
4907#endif
4908 },
4909 {"gui_gtk3",
4910#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4911 1
4912#else
4913 0
4914#endif
4915 },
4916 {"gui_gnome",
4917#ifdef FEAT_GUI_GNOME
4918 1
4919#else
4920 0
4921#endif
4922 },
4923 {"gui_haiku",
4924#ifdef FEAT_GUI_HAIKU
4925 1
4926#else
4927 0
4928#endif
4929 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004930 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004931 {"gui_motif",
4932#ifdef FEAT_GUI_MOTIF
4933 1
4934#else
4935 0
4936#endif
4937 },
4938 {"gui_photon",
4939#ifdef FEAT_GUI_PHOTON
4940 1
4941#else
4942 0
4943#endif
4944 },
4945 {"gui_win32",
4946#ifdef FEAT_GUI_MSWIN
4947 1
4948#else
4949 0
4950#endif
4951 },
4952 {"iconv",
4953#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4954 1
4955#else
4956 0
4957#endif
4958 },
4959 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004960 {"ipv6",
4961#ifdef FEAT_IPV6
4962 1
4963#else
4964 0
4965#endif
4966 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004967 {"job",
4968#ifdef FEAT_JOB_CHANNEL
4969 1
4970#else
4971 0
4972#endif
4973 },
4974 {"jumplist",
4975#ifdef FEAT_JUMPLIST
4976 1
4977#else
4978 0
4979#endif
4980 },
4981 {"keymap",
4982#ifdef FEAT_KEYMAP
4983 1
4984#else
4985 0
4986#endif
4987 },
4988 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4989 {"langmap",
4990#ifdef FEAT_LANGMAP
4991 1
4992#else
4993 0
4994#endif
4995 },
4996 {"libcall",
4997#ifdef FEAT_LIBCALL
4998 1
4999#else
5000 0
5001#endif
5002 },
5003 {"linebreak",
5004#ifdef FEAT_LINEBREAK
5005 1
5006#else
5007 0
5008#endif
5009 },
5010 {"lispindent",
5011#ifdef FEAT_LISP
5012 1
5013#else
5014 0
5015#endif
5016 },
5017 {"listcmds", 1},
5018 {"localmap", 1},
5019 {"lua",
5020#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
5021 1
5022#else
5023 0
5024#endif
5025 },
5026 {"menu",
5027#ifdef FEAT_MENU
5028 1
5029#else
5030 0
5031#endif
5032 },
5033 {"mksession",
5034#ifdef FEAT_SESSION
5035 1
5036#else
5037 0
5038#endif
5039 },
5040 {"modify_fname", 1},
5041 {"mouse", 1},
5042 {"mouseshape",
5043#ifdef FEAT_MOUSESHAPE
5044 1
5045#else
5046 0
5047#endif
5048 },
5049 {"mouse_dec",
5050#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
5051 1
5052#else
5053 0
5054#endif
5055 },
5056 {"mouse_gpm",
5057#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
5058 1
5059#else
5060 0
5061#endif
5062 },
5063 {"mouse_jsbterm",
5064#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
5065 1
5066#else
5067 0
5068#endif
5069 },
5070 {"mouse_netterm",
5071#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
5072 1
5073#else
5074 0
5075#endif
5076 },
5077 {"mouse_pterm",
5078#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
5079 1
5080#else
5081 0
5082#endif
5083 },
5084 {"mouse_sgr",
5085#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5086 1
5087#else
5088 0
5089#endif
5090 },
5091 {"mouse_sysmouse",
5092#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
5093 1
5094#else
5095 0
5096#endif
5097 },
5098 {"mouse_urxvt",
5099#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
5100 1
5101#else
5102 0
5103#endif
5104 },
5105 {"mouse_xterm",
5106#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5107 1
5108#else
5109 0
5110#endif
5111 },
5112 {"multi_byte", 1},
5113 {"multi_byte_ime",
5114#ifdef FEAT_MBYTE_IME
5115 1
5116#else
5117 0
5118#endif
5119 },
5120 {"multi_lang",
5121#ifdef FEAT_MULTI_LANG
5122 1
5123#else
5124 0
5125#endif
5126 },
5127 {"mzscheme",
5128#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
5129 1
5130#else
5131 0
5132#endif
5133 },
5134 {"num64", 1},
5135 {"ole",
5136#ifdef FEAT_OLE
5137 1
5138#else
5139 0
5140#endif
5141 },
5142 {"packages",
5143#ifdef FEAT_EVAL
5144 1
5145#else
5146 0
5147#endif
5148 },
5149 {"path_extra",
5150#ifdef FEAT_PATH_EXTRA
5151 1
5152#else
5153 0
5154#endif
5155 },
5156 {"perl",
5157#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
5158 1
5159#else
5160 0
5161#endif
5162 },
5163 {"persistent_undo",
5164#ifdef FEAT_PERSISTENT_UNDO
5165 1
5166#else
5167 0
5168#endif
5169 },
5170 {"python_compiled",
5171#if defined(FEAT_PYTHON)
5172 1
5173#else
5174 0
5175#endif
5176 },
5177 {"python_dynamic",
5178#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
5179 1
5180#else
5181 0
5182#endif
5183 },
5184 {"python",
5185#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
5186 1
5187#else
5188 0
5189#endif
5190 },
5191 {"pythonx",
5192#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
5193 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
5194 1
5195#else
5196 0
5197#endif
5198 },
5199 {"python3_compiled",
5200#if defined(FEAT_PYTHON3)
5201 1
5202#else
5203 0
5204#endif
5205 },
5206 {"python3_dynamic",
5207#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
5208 1
5209#else
5210 0
5211#endif
5212 },
5213 {"python3",
5214#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
5215 1
5216#else
5217 0
5218#endif
5219 },
5220 {"popupwin",
5221#ifdef FEAT_PROP_POPUP
5222 1
5223#else
5224 0
5225#endif
5226 },
5227 {"postscript",
5228#ifdef FEAT_POSTSCRIPT
5229 1
5230#else
5231 0
5232#endif
5233 },
5234 {"printer",
5235#ifdef FEAT_PRINTER
5236 1
5237#else
5238 0
5239#endif
5240 },
5241 {"profile",
5242#ifdef FEAT_PROFILE
5243 1
5244#else
5245 0
5246#endif
5247 },
5248 {"reltime",
5249#ifdef FEAT_RELTIME
5250 1
5251#else
5252 0
5253#endif
5254 },
5255 {"quickfix",
5256#ifdef FEAT_QUICKFIX
5257 1
5258#else
5259 0
5260#endif
5261 },
5262 {"rightleft",
5263#ifdef FEAT_RIGHTLEFT
5264 1
5265#else
5266 0
5267#endif
5268 },
5269 {"ruby",
5270#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5271 1
5272#else
5273 0
5274#endif
5275 },
5276 {"scrollbind", 1},
5277 {"showcmd",
5278#ifdef FEAT_CMDL_INFO
5279 1
5280#else
5281 0
5282#endif
5283 },
5284 {"cmdline_info",
5285#ifdef FEAT_CMDL_INFO
5286 1
5287#else
5288 0
5289#endif
5290 },
5291 {"signs",
5292#ifdef FEAT_SIGNS
5293 1
5294#else
5295 0
5296#endif
5297 },
5298 {"smartindent",
5299#ifdef FEAT_SMARTINDENT
5300 1
5301#else
5302 0
5303#endif
5304 },
5305 {"startuptime",
5306#ifdef STARTUPTIME
5307 1
5308#else
5309 0
5310#endif
5311 },
5312 {"statusline",
5313#ifdef FEAT_STL_OPT
5314 1
5315#else
5316 0
5317#endif
5318 },
5319 {"netbeans_intg",
5320#ifdef FEAT_NETBEANS_INTG
5321 1
5322#else
5323 0
5324#endif
5325 },
5326 {"sound",
5327#ifdef FEAT_SOUND
5328 1
5329#else
5330 0
5331#endif
5332 },
5333 {"spell",
5334#ifdef FEAT_SPELL
5335 1
5336#else
5337 0
5338#endif
5339 },
5340 {"syntax",
5341#ifdef FEAT_SYN_HL
5342 1
5343#else
5344 0
5345#endif
5346 },
5347 {"system",
5348#if defined(USE_SYSTEM) || !defined(UNIX)
5349 1
5350#else
5351 0
5352#endif
5353 },
5354 {"tag_binary",
5355#ifdef FEAT_TAG_BINS
5356 1
5357#else
5358 0
5359#endif
5360 },
5361 {"tcl",
5362#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5363 1
5364#else
5365 0
5366#endif
5367 },
5368 {"termguicolors",
5369#ifdef FEAT_TERMGUICOLORS
5370 1
5371#else
5372 0
5373#endif
5374 },
5375 {"terminal",
5376#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5377 1
5378#else
5379 0
5380#endif
5381 },
5382 {"terminfo",
5383#ifdef TERMINFO
5384 1
5385#else
5386 0
5387#endif
5388 },
5389 {"termresponse",
5390#ifdef FEAT_TERMRESPONSE
5391 1
5392#else
5393 0
5394#endif
5395 },
5396 {"textobjects",
5397#ifdef FEAT_TEXTOBJ
5398 1
5399#else
5400 0
5401#endif
5402 },
5403 {"textprop",
5404#ifdef FEAT_PROP_POPUP
5405 1
5406#else
5407 0
5408#endif
5409 },
5410 {"tgetent",
5411#ifdef HAVE_TGETENT
5412 1
5413#else
5414 0
5415#endif
5416 },
5417 {"timers",
5418#ifdef FEAT_TIMERS
5419 1
5420#else
5421 0
5422#endif
5423 },
5424 {"title",
5425#ifdef FEAT_TITLE
5426 1
5427#else
5428 0
5429#endif
5430 },
5431 {"toolbar",
5432#ifdef FEAT_TOOLBAR
5433 1
5434#else
5435 0
5436#endif
5437 },
5438 {"unnamedplus",
5439#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5440 1
5441#else
5442 0
5443#endif
5444 },
5445 {"user-commands", 1}, // was accidentally included in 5.4
5446 {"user_commands", 1},
5447 {"vartabs",
5448#ifdef FEAT_VARTABS
5449 1
5450#else
5451 0
5452#endif
5453 },
5454 {"vertsplit", 1},
5455 {"viminfo",
5456#ifdef FEAT_VIMINFO
5457 1
5458#else
5459 0
5460#endif
5461 },
5462 {"vimscript-1", 1},
5463 {"vimscript-2", 1},
5464 {"vimscript-3", 1},
5465 {"vimscript-4", 1},
5466 {"virtualedit", 1},
5467 {"visual", 1},
5468 {"visualextra", 1},
5469 {"vreplace", 1},
5470 {"vtp",
5471#ifdef FEAT_VTP
5472 1
5473#else
5474 0
5475#endif
5476 },
5477 {"wildignore",
5478#ifdef FEAT_WILDIGN
5479 1
5480#else
5481 0
5482#endif
5483 },
5484 {"wildmenu",
5485#ifdef FEAT_WILDMENU
5486 1
5487#else
5488 0
5489#endif
5490 },
5491 {"windows", 1},
5492 {"winaltkeys",
5493#ifdef FEAT_WAK
5494 1
5495#else
5496 0
5497#endif
5498 },
5499 {"writebackup",
5500#ifdef FEAT_WRITEBACKUP
5501 1
5502#else
5503 0
5504#endif
5505 },
5506 {"xim",
5507#ifdef FEAT_XIM
5508 1
5509#else
5510 0
5511#endif
5512 },
5513 {"xfontset",
5514#ifdef FEAT_XFONTSET
5515 1
5516#else
5517 0
5518#endif
5519 },
5520 {"xpm",
5521#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5522 1
5523#else
5524 0
5525#endif
5526 },
5527 {"xpm_w32", // for backward compatibility
5528#ifdef FEAT_XPM_W32
5529 1
5530#else
5531 0
5532#endif
5533 },
5534 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005535#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005536 1
5537#else
5538 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005539#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005540 },
5541 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005542#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005543 1
5544#else
5545 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005546#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005547 },
5548 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005549#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005550 1
5551#else
5552 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005553#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005554 },
5555 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005556#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005557 1
5558#else
5559 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005560#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005561 },
5562 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005563#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005564 1
5565#else
5566 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005567#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005568 },
5569 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005570 };
5571
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005572 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005573 for (i = 0; has_list[i].name != NULL; ++i)
5574 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005575 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005576 x = TRUE;
5577 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005578 break;
5579 }
5580
Bram Moolenaar79296512020-03-22 16:17:14 +01005581 // features also in has_list[] but sometimes enabled at runtime
5582 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005583 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005584 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005585 {
5586 // intentionally empty
5587 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005588#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005589 else if (STRICMP(name, "balloon_multiline") == 0)
5590 n = multiline_balloon_available();
5591#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005592#ifdef VIMDLL
5593 else if (STRICMP(name, "filterpipe") == 0)
5594 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005595#endif
5596#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5597 else if (STRICMP(name, "iconv") == 0)
5598 n = iconv_enabled(FALSE);
5599#endif
5600#ifdef DYNAMIC_LUA
5601 else if (STRICMP(name, "lua") == 0)
5602 n = lua_enabled(FALSE);
5603#endif
5604#ifdef DYNAMIC_MZSCHEME
5605 else if (STRICMP(name, "mzscheme") == 0)
5606 n = mzscheme_enabled(FALSE);
5607#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005608#ifdef DYNAMIC_PERL
5609 else if (STRICMP(name, "perl") == 0)
5610 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005611#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005612#ifdef DYNAMIC_PYTHON
5613 else if (STRICMP(name, "python") == 0)
5614 n = python_enabled(FALSE);
5615#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005616#ifdef DYNAMIC_PYTHON3
5617 else if (STRICMP(name, "python3") == 0)
5618 n = python3_enabled(FALSE);
5619#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005620#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5621 else if (STRICMP(name, "pythonx") == 0)
5622 {
5623# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5624 if (p_pyx == 0)
5625 n = python3_enabled(FALSE) || python_enabled(FALSE);
5626 else if (p_pyx == 3)
5627 n = python3_enabled(FALSE);
5628 else if (p_pyx == 2)
5629 n = python_enabled(FALSE);
5630# elif defined(DYNAMIC_PYTHON)
5631 n = python_enabled(FALSE);
5632# elif defined(DYNAMIC_PYTHON3)
5633 n = python3_enabled(FALSE);
5634# endif
5635 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005636#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005637#ifdef DYNAMIC_RUBY
5638 else if (STRICMP(name, "ruby") == 0)
5639 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005640#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005641#ifdef DYNAMIC_TCL
5642 else if (STRICMP(name, "tcl") == 0)
5643 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005644#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005645#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005646 else if (STRICMP(name, "terminal") == 0)
5647 n = terminal_enabled();
5648#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005649 }
5650
Bram Moolenaar79296512020-03-22 16:17:14 +01005651 // features not in has_list[]
5652 if (x == FALSE)
5653 {
5654 if (STRNICMP(name, "patch", 5) == 0)
5655 {
5656 x = TRUE;
5657 if (name[5] == '-'
5658 && STRLEN(name) >= 11
5659 && vim_isdigit(name[6])
5660 && vim_isdigit(name[8])
5661 && vim_isdigit(name[10]))
5662 {
5663 int major = atoi((char *)name + 6);
5664 int minor = atoi((char *)name + 8);
5665
5666 // Expect "patch-9.9.01234".
5667 n = (major < VIM_VERSION_MAJOR
5668 || (major == VIM_VERSION_MAJOR
5669 && (minor < VIM_VERSION_MINOR
5670 || (minor == VIM_VERSION_MINOR
5671 && has_patch(atoi((char *)name + 10))))));
5672 }
5673 else
5674 n = has_patch(atoi((char *)name + 5));
5675 }
5676 else if (STRICMP(name, "vim_starting") == 0)
5677 {
5678 x = TRUE;
5679 n = (starting != 0);
5680 }
5681 else if (STRICMP(name, "ttyin") == 0)
5682 {
5683 x = TRUE;
5684 n = mch_input_isatty();
5685 }
5686 else if (STRICMP(name, "ttyout") == 0)
5687 {
5688 x = TRUE;
5689 n = stdout_isatty;
5690 }
5691 else if (STRICMP(name, "multi_byte_encoding") == 0)
5692 {
5693 x = TRUE;
5694 n = has_mbyte;
5695 }
5696 else if (STRICMP(name, "gui_running") == 0)
5697 {
5698 x = TRUE;
5699#ifdef FEAT_GUI
5700 n = (gui.in_use || gui.starting);
5701#endif
5702 }
5703 else if (STRICMP(name, "browse") == 0)
5704 {
5705 x = TRUE;
5706#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5707 n = gui.in_use; // gui_mch_browse() works when GUI is running
5708#endif
5709 }
5710 else if (STRICMP(name, "syntax_items") == 0)
5711 {
5712 x = TRUE;
5713#ifdef FEAT_SYN_HL
5714 n = syntax_present(curwin);
5715#endif
5716 }
5717 else if (STRICMP(name, "vcon") == 0)
5718 {
5719 x = TRUE;
5720#ifdef FEAT_VTP
5721 n = is_term_win32() && has_vtp_working();
5722#endif
5723 }
5724 else if (STRICMP(name, "netbeans_enabled") == 0)
5725 {
5726 x = TRUE;
5727#ifdef FEAT_NETBEANS_INTG
5728 n = netbeans_active();
5729#endif
5730 }
5731 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5732 {
5733 x = TRUE;
5734#ifdef FEAT_MOUSE_GPM
5735 n = gpm_enabled();
5736#endif
5737 }
5738 else if (STRICMP(name, "conpty") == 0)
5739 {
5740 x = TRUE;
5741#if defined(FEAT_TERMINAL) && defined(MSWIN)
5742 n = use_conpty();
5743#endif
5744 }
5745 else if (STRICMP(name, "clipboard_working") == 0)
5746 {
5747 x = TRUE;
5748#ifdef FEAT_CLIPBOARD
5749 n = clip_star.available;
5750#endif
5751 }
5752 }
5753
Bram Moolenaar04637e22020-09-05 18:45:29 +02005754 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005755 // return whether feature could ever be enabled
5756 rettv->vval.v_number = x;
5757 else
5758 // return whether feature is enabled
5759 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005760}
5761
5762/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005763 * Return TRUE if "feature" can change later.
5764 * Also when checking for the feature has side effects, such as loading a DLL.
5765 */
5766 int
5767dynamic_feature(char_u *feature)
5768{
5769 return (feature == NULL
5770#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5771 || STRICMP(feature, "balloon_multiline") == 0
5772#endif
5773#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5774 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5775#endif
5776#ifdef VIMDLL
5777 || STRICMP(feature, "filterpipe") == 0
5778#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005779#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005780 // this can only change on Unix where the ":gui" command could be
5781 // used.
5782 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5783#endif
5784#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5785 || STRICMP(feature, "iconv") == 0
5786#endif
5787#ifdef DYNAMIC_LUA
5788 || STRICMP(feature, "lua") == 0
5789#endif
5790#ifdef FEAT_MOUSE_GPM
5791 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5792#endif
5793#ifdef DYNAMIC_MZSCHEME
5794 || STRICMP(feature, "mzscheme") == 0
5795#endif
5796#ifdef FEAT_NETBEANS_INTG
5797 || STRICMP(feature, "netbeans_enabled") == 0
5798#endif
5799#ifdef DYNAMIC_PERL
5800 || STRICMP(feature, "perl") == 0
5801#endif
5802#ifdef DYNAMIC_PYTHON
5803 || STRICMP(feature, "python") == 0
5804#endif
5805#ifdef DYNAMIC_PYTHON3
5806 || STRICMP(feature, "python3") == 0
5807#endif
5808#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5809 || STRICMP(feature, "pythonx") == 0
5810#endif
5811#ifdef DYNAMIC_RUBY
5812 || STRICMP(feature, "ruby") == 0
5813#endif
5814#ifdef FEAT_SYN_HL
5815 || STRICMP(feature, "syntax_items") == 0
5816#endif
5817#ifdef DYNAMIC_TCL
5818 || STRICMP(feature, "tcl") == 0
5819#endif
5820 // once "starting" is zero it will stay that way
5821 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5822 || STRICMP(feature, "multi_byte_encoding") == 0
5823#if defined(FEAT_TERMINAL) && defined(MSWIN)
5824 || STRICMP(feature, "conpty") == 0
5825#endif
5826 );
5827}
5828
5829/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005830 * "haslocaldir()" function
5831 */
5832 static void
5833f_haslocaldir(typval_T *argvars, typval_T *rettv)
5834{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005835 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005836 win_T *wp = NULL;
5837
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005838 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5839
5840 // Check for window-local and tab-local directories
5841 if (wp != NULL && wp->w_localdir != NULL)
5842 rettv->vval.v_number = 1;
5843 else if (tp != NULL && tp->tp_localdir != NULL)
5844 rettv->vval.v_number = 2;
5845 else
5846 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005847}
5848
5849/*
5850 * "hasmapto()" function
5851 */
5852 static void
5853f_hasmapto(typval_T *argvars, typval_T *rettv)
5854{
5855 char_u *name;
5856 char_u *mode;
5857 char_u buf[NUMBUFLEN];
5858 int abbr = FALSE;
5859
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005860 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005861 if (argvars[1].v_type == VAR_UNKNOWN)
5862 mode = (char_u *)"nvo";
5863 else
5864 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005865 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005866 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005867 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005868 }
5869
5870 if (map_to_exists(name, mode, abbr))
5871 rettv->vval.v_number = TRUE;
5872 else
5873 rettv->vval.v_number = FALSE;
5874}
5875
5876/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005877 * "highlightID(name)" function
5878 */
5879 static void
5880f_hlID(typval_T *argvars, typval_T *rettv)
5881{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005882 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005883}
5884
5885/*
5886 * "highlight_exists()" function
5887 */
5888 static void
5889f_hlexists(typval_T *argvars, typval_T *rettv)
5890{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005891 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005892}
5893
5894/*
5895 * "hostname()" function
5896 */
5897 static void
5898f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5899{
5900 char_u hostname[256];
5901
5902 mch_get_host_name(hostname, 256);
5903 rettv->v_type = VAR_STRING;
5904 rettv->vval.v_string = vim_strsave(hostname);
5905}
5906
5907/*
5908 * iconv() function
5909 */
5910 static void
5911f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5912{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005913 char_u buf1[NUMBUFLEN];
5914 char_u buf2[NUMBUFLEN];
5915 char_u *from, *to, *str;
5916 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005917
5918 rettv->v_type = VAR_STRING;
5919 rettv->vval.v_string = NULL;
5920
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005921 str = tv_get_string(&argvars[0]);
5922 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5923 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005924 vimconv.vc_type = CONV_NONE;
5925 convert_setup(&vimconv, from, to);
5926
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005927 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005928 if (vimconv.vc_type == CONV_NONE)
5929 rettv->vval.v_string = vim_strsave(str);
5930 else
5931 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5932
5933 convert_setup(&vimconv, NULL, NULL);
5934 vim_free(from);
5935 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005936}
5937
5938/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005939 * "index()" function
5940 */
5941 static void
5942f_index(typval_T *argvars, typval_T *rettv)
5943{
5944 list_T *l;
5945 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005946 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005947 long idx = 0;
5948 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005949 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005950
5951 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005952 if (argvars[0].v_type == VAR_BLOB)
5953 {
5954 typval_T tv;
5955 int start = 0;
5956
5957 if (argvars[2].v_type != VAR_UNKNOWN)
5958 {
5959 start = tv_get_number_chk(&argvars[2], &error);
5960 if (error)
5961 return;
5962 }
5963 b = argvars[0].vval.v_blob;
5964 if (b == NULL)
5965 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005966 if (start < 0)
5967 {
5968 start = blob_len(b) + start;
5969 if (start < 0)
5970 start = 0;
5971 }
5972
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005973 for (idx = start; idx < blob_len(b); ++idx)
5974 {
5975 tv.v_type = VAR_NUMBER;
5976 tv.vval.v_number = blob_get(b, idx);
5977 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5978 {
5979 rettv->vval.v_number = idx;
5980 return;
5981 }
5982 }
5983 return;
5984 }
5985 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005986 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005987 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005988 return;
5989 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005990
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005991 l = argvars[0].vval.v_list;
5992 if (l != NULL)
5993 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005994 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005995 item = l->lv_first;
5996 if (argvars[2].v_type != VAR_UNKNOWN)
5997 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005998 // Start at specified item. Use the cached index that list_find()
5999 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006000 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006001 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006002 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02006003 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006004 if (error)
6005 item = NULL;
6006 }
6007
6008 for ( ; item != NULL; item = item->li_next, ++idx)
6009 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6010 {
6011 rettv->vval.v_number = idx;
6012 break;
6013 }
6014 }
6015}
6016
6017static int inputsecret_flag = 0;
6018
6019/*
6020 * "input()" function
6021 * Also handles inputsecret() when inputsecret is set.
6022 */
6023 static void
6024f_input(typval_T *argvars, typval_T *rettv)
6025{
6026 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6027}
6028
6029/*
6030 * "inputdialog()" function
6031 */
6032 static void
6033f_inputdialog(typval_T *argvars, typval_T *rettv)
6034{
6035#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006036 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006037 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6038 {
6039 char_u *message;
6040 char_u buf[NUMBUFLEN];
6041 char_u *defstr = (char_u *)"";
6042
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006043 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006044 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006045 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006046 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6047 else
6048 IObuff[0] = NUL;
6049 if (message != NULL && defstr != NULL
6050 && do_dialog(VIM_QUESTION, NULL, message,
6051 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6052 rettv->vval.v_string = vim_strsave(IObuff);
6053 else
6054 {
6055 if (message != NULL && defstr != NULL
6056 && argvars[1].v_type != VAR_UNKNOWN
6057 && argvars[2].v_type != VAR_UNKNOWN)
6058 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006059 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006060 else
6061 rettv->vval.v_string = NULL;
6062 }
6063 rettv->v_type = VAR_STRING;
6064 }
6065 else
6066#endif
6067 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6068}
6069
6070/*
6071 * "inputlist()" function
6072 */
6073 static void
6074f_inputlist(typval_T *argvars, typval_T *rettv)
6075{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006076 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006077 listitem_T *li;
6078 int selected;
6079 int mouse_used;
6080
6081#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006082 // While starting up, there is no place to enter text. When running tests
6083 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006084 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006085 return;
6086#endif
6087 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6088 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006089 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006090 return;
6091 }
6092
6093 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006094 msg_row = Rows - 1; // for when 'cmdheight' > 1
6095 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006096 msg_scroll = TRUE;
6097 msg_clr_eos();
6098
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006099 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006100 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006101 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006102 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006103 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006104 msg_putchar('\n');
6105 }
6106
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006107 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006108 selected = prompt_for_number(&mouse_used);
6109 if (mouse_used)
6110 selected -= lines_left;
6111
6112 rettv->vval.v_number = selected;
6113}
6114
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006115static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6116
6117/*
6118 * "inputrestore()" function
6119 */
6120 static void
6121f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6122{
6123 if (ga_userinput.ga_len > 0)
6124 {
6125 --ga_userinput.ga_len;
6126 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6127 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006128 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006129 }
6130 else if (p_verbose > 1)
6131 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006132 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006133 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006134 }
6135}
6136
6137/*
6138 * "inputsave()" function
6139 */
6140 static void
6141f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6142{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006143 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006144 if (ga_grow(&ga_userinput, 1) == OK)
6145 {
6146 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6147 + ga_userinput.ga_len);
6148 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006149 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006150 }
6151 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006152 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006153}
6154
6155/*
6156 * "inputsecret()" function
6157 */
6158 static void
6159f_inputsecret(typval_T *argvars, typval_T *rettv)
6160{
6161 ++cmdline_star;
6162 ++inputsecret_flag;
6163 f_input(argvars, rettv);
6164 --cmdline_star;
6165 --inputsecret_flag;
6166}
6167
6168/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01006169 * "interrupt()" function
6170 */
6171 static void
6172f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6173{
6174 got_int = TRUE;
6175}
6176
6177/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006178 * "invert(expr)" function
6179 */
6180 static void
6181f_invert(typval_T *argvars, typval_T *rettv)
6182{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006183 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006184}
6185
6186/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006187 * "islocked()" function
6188 */
6189 static void
6190f_islocked(typval_T *argvars, typval_T *rettv)
6191{
6192 lval_T lv;
6193 char_u *end;
6194 dictitem_T *di;
6195
6196 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006197 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006198 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006199 if (end != NULL && lv.ll_name != NULL)
6200 {
6201 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02006202 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006203 else
6204 {
6205 if (lv.ll_tv == NULL)
6206 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006207 di = find_var(lv.ll_name, NULL, TRUE);
6208 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006209 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006210 // Consider a variable locked when:
6211 // 1. the variable itself is locked
6212 // 2. the value of the variable is locked.
6213 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01006214 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6215 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006216 }
6217 }
6218 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006219 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006220 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006221 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006222 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006223 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006224 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6225 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006226 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006227 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6228 }
6229 }
6230
6231 clear_lval(&lv);
6232}
6233
6234#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6235/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006236 * "isinf()" function
6237 */
6238 static void
6239f_isinf(typval_T *argvars, typval_T *rettv)
6240{
6241 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6242 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6243}
6244
6245/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006246 * "isnan()" function
6247 */
6248 static void
6249f_isnan(typval_T *argvars, typval_T *rettv)
6250{
6251 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6252 && isnan(argvars[0].vval.v_float);
6253}
6254#endif
6255
6256/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006257 * "last_buffer_nr()" function.
6258 */
6259 static void
6260f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6261{
6262 int n = 0;
6263 buf_T *buf;
6264
Bram Moolenaar29323592016-07-24 22:04:11 +02006265 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006266 if (n < buf->b_fnum)
6267 n = buf->b_fnum;
6268
6269 rettv->vval.v_number = n;
6270}
6271
6272/*
6273 * "len()" function
6274 */
6275 static void
6276f_len(typval_T *argvars, typval_T *rettv)
6277{
6278 switch (argvars[0].v_type)
6279 {
6280 case VAR_STRING:
6281 case VAR_NUMBER:
6282 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006283 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006284 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006285 case VAR_BLOB:
6286 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6287 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006288 case VAR_LIST:
6289 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6290 break;
6291 case VAR_DICT:
6292 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6293 break;
6294 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006295 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006297 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006298 case VAR_SPECIAL:
6299 case VAR_FLOAT:
6300 case VAR_FUNC:
6301 case VAR_PARTIAL:
6302 case VAR_JOB:
6303 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006304 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006305 break;
6306 }
6307}
6308
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006309 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006310libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006311{
6312#ifdef FEAT_LIBCALL
6313 char_u *string_in;
6314 char_u **string_result;
6315 int nr_result;
6316#endif
6317
6318 rettv->v_type = type;
6319 if (type != VAR_NUMBER)
6320 rettv->vval.v_string = NULL;
6321
6322 if (check_restricted() || check_secure())
6323 return;
6324
6325#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006326 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006327 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6328 {
6329 string_in = NULL;
6330 if (argvars[2].v_type == VAR_STRING)
6331 string_in = argvars[2].vval.v_string;
6332 if (type == VAR_NUMBER)
6333 string_result = NULL;
6334 else
6335 string_result = &rettv->vval.v_string;
6336 if (mch_libcall(argvars[0].vval.v_string,
6337 argvars[1].vval.v_string,
6338 string_in,
6339 argvars[2].vval.v_number,
6340 string_result,
6341 &nr_result) == OK
6342 && type == VAR_NUMBER)
6343 rettv->vval.v_number = nr_result;
6344 }
6345#endif
6346}
6347
6348/*
6349 * "libcall()" function
6350 */
6351 static void
6352f_libcall(typval_T *argvars, typval_T *rettv)
6353{
6354 libcall_common(argvars, rettv, VAR_STRING);
6355}
6356
6357/*
6358 * "libcallnr()" function
6359 */
6360 static void
6361f_libcallnr(typval_T *argvars, typval_T *rettv)
6362{
6363 libcall_common(argvars, rettv, VAR_NUMBER);
6364}
6365
6366/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006367 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006368 */
6369 static void
6370f_line(typval_T *argvars, typval_T *rettv)
6371{
6372 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006373 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006374 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006375 int id;
6376 tabpage_T *tp;
6377 win_T *wp;
6378 win_T *save_curwin;
6379 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006380
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006381 if (argvars[1].v_type != VAR_UNKNOWN)
6382 {
6383 // use window specified in the second argument
6384 id = (int)tv_get_number(&argvars[1]);
6385 wp = win_id2wp_tp(id, &tp);
6386 if (wp != NULL && tp != NULL)
6387 {
6388 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6389 == OK)
6390 {
6391 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006392 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006393 }
6394 restore_win_noblock(save_curwin, save_curtab, TRUE);
6395 }
6396 }
6397 else
6398 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006399 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006400
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006401 if (fp != NULL)
6402 lnum = fp->lnum;
6403 rettv->vval.v_number = lnum;
6404}
6405
6406/*
6407 * "line2byte(lnum)" function
6408 */
6409 static void
6410f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6411{
6412#ifndef FEAT_BYTEOFF
6413 rettv->vval.v_number = -1;
6414#else
6415 linenr_T lnum;
6416
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006417 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006418 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6419 rettv->vval.v_number = -1;
6420 else
6421 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6422 if (rettv->vval.v_number >= 0)
6423 ++rettv->vval.v_number;
6424#endif
6425}
6426
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006427#ifdef FEAT_FLOAT
6428/*
6429 * "log()" function
6430 */
6431 static void
6432f_log(typval_T *argvars, typval_T *rettv)
6433{
6434 float_T f = 0.0;
6435
6436 rettv->v_type = VAR_FLOAT;
6437 if (get_float_arg(argvars, &f) == OK)
6438 rettv->vval.v_float = log(f);
6439 else
6440 rettv->vval.v_float = 0.0;
6441}
6442
6443/*
6444 * "log10()" function
6445 */
6446 static void
6447f_log10(typval_T *argvars, typval_T *rettv)
6448{
6449 float_T f = 0.0;
6450
6451 rettv->v_type = VAR_FLOAT;
6452 if (get_float_arg(argvars, &f) == OK)
6453 rettv->vval.v_float = log10(f);
6454 else
6455 rettv->vval.v_float = 0.0;
6456}
6457#endif
6458
6459#ifdef FEAT_LUA
6460/*
6461 * "luaeval()" function
6462 */
6463 static void
6464f_luaeval(typval_T *argvars, typval_T *rettv)
6465{
6466 char_u *str;
6467 char_u buf[NUMBUFLEN];
6468
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006469 if (check_restricted() || check_secure())
6470 return;
6471
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006472 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006473 do_luaeval(str, argvars + 1, rettv);
6474}
6475#endif
6476
6477/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006478 * "maparg()" function
6479 */
6480 static void
6481f_maparg(typval_T *argvars, typval_T *rettv)
6482{
6483 get_maparg(argvars, rettv, TRUE);
6484}
6485
6486/*
6487 * "mapcheck()" function
6488 */
6489 static void
6490f_mapcheck(typval_T *argvars, typval_T *rettv)
6491{
6492 get_maparg(argvars, rettv, FALSE);
6493}
6494
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006495typedef enum
6496{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006497 MATCH_END, // matchend()
6498 MATCH_MATCH, // match()
6499 MATCH_STR, // matchstr()
6500 MATCH_LIST, // matchlist()
6501 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006502} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006503
6504 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006505find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506{
6507 char_u *str = NULL;
6508 long len = 0;
6509 char_u *expr = NULL;
6510 char_u *pat;
6511 regmatch_T regmatch;
6512 char_u patbuf[NUMBUFLEN];
6513 char_u strbuf[NUMBUFLEN];
6514 char_u *save_cpo;
6515 long start = 0;
6516 long nth = 1;
6517 colnr_T startcol = 0;
6518 int match = 0;
6519 list_T *l = NULL;
6520 listitem_T *li = NULL;
6521 long idx = 0;
6522 char_u *tofree = NULL;
6523
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006524 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006525 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006526 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006527
6528 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006529 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006530 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006531 // type MATCH_LIST: return empty list when there are no matches.
6532 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006533 if (rettv_list_alloc(rettv) == FAIL)
6534 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006535 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006536 && (list_append_string(rettv->vval.v_list,
6537 (char_u *)"", 0) == FAIL
6538 || list_append_number(rettv->vval.v_list,
6539 (varnumber_T)-1) == FAIL
6540 || list_append_number(rettv->vval.v_list,
6541 (varnumber_T)-1) == FAIL
6542 || list_append_number(rettv->vval.v_list,
6543 (varnumber_T)-1) == FAIL))
6544 {
6545 list_free(rettv->vval.v_list);
6546 rettv->vval.v_list = NULL;
6547 goto theend;
6548 }
6549 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006550 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006551 {
6552 rettv->v_type = VAR_STRING;
6553 rettv->vval.v_string = NULL;
6554 }
6555
6556 if (argvars[0].v_type == VAR_LIST)
6557 {
6558 if ((l = argvars[0].vval.v_list) == NULL)
6559 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006560 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006561 li = l->lv_first;
6562 }
6563 else
6564 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006565 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006566 len = (long)STRLEN(str);
6567 }
6568
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006569 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006570 if (pat == NULL)
6571 goto theend;
6572
6573 if (argvars[2].v_type != VAR_UNKNOWN)
6574 {
6575 int error = FALSE;
6576
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006577 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006578 if (error)
6579 goto theend;
6580 if (l != NULL)
6581 {
6582 li = list_find(l, start);
6583 if (li == NULL)
6584 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006585 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006586 }
6587 else
6588 {
6589 if (start < 0)
6590 start = 0;
6591 if (start > len)
6592 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006593 // When "count" argument is there ignore matches before "start",
6594 // otherwise skip part of the string. Differs when pattern is "^"
6595 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006596 if (argvars[3].v_type != VAR_UNKNOWN)
6597 startcol = start;
6598 else
6599 {
6600 str += start;
6601 len -= start;
6602 }
6603 }
6604
6605 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006606 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006607 if (error)
6608 goto theend;
6609 }
6610
6611 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6612 if (regmatch.regprog != NULL)
6613 {
6614 regmatch.rm_ic = p_ic;
6615
6616 for (;;)
6617 {
6618 if (l != NULL)
6619 {
6620 if (li == NULL)
6621 {
6622 match = FALSE;
6623 break;
6624 }
6625 vim_free(tofree);
6626 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6627 if (str == NULL)
6628 break;
6629 }
6630
6631 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6632
6633 if (match && --nth <= 0)
6634 break;
6635 if (l == NULL && !match)
6636 break;
6637
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006638 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006639 if (l != NULL)
6640 {
6641 li = li->li_next;
6642 ++idx;
6643 }
6644 else
6645 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006646 startcol = (colnr_T)(regmatch.startp[0]
6647 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006648 if (startcol > (colnr_T)len
6649 || str + startcol <= regmatch.startp[0])
6650 {
6651 match = FALSE;
6652 break;
6653 }
6654 }
6655 }
6656
6657 if (match)
6658 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006659 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006660 {
6661 listitem_T *li1 = rettv->vval.v_list->lv_first;
6662 listitem_T *li2 = li1->li_next;
6663 listitem_T *li3 = li2->li_next;
6664 listitem_T *li4 = li3->li_next;
6665
6666 vim_free(li1->li_tv.vval.v_string);
6667 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006668 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006669 li3->li_tv.vval.v_number =
6670 (varnumber_T)(regmatch.startp[0] - expr);
6671 li4->li_tv.vval.v_number =
6672 (varnumber_T)(regmatch.endp[0] - expr);
6673 if (l != NULL)
6674 li2->li_tv.vval.v_number = (varnumber_T)idx;
6675 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006676 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006677 {
6678 int i;
6679
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006680 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006681 for (i = 0; i < NSUBEXP; ++i)
6682 {
6683 if (regmatch.endp[i] == NULL)
6684 {
6685 if (list_append_string(rettv->vval.v_list,
6686 (char_u *)"", 0) == FAIL)
6687 break;
6688 }
6689 else if (list_append_string(rettv->vval.v_list,
6690 regmatch.startp[i],
6691 (int)(regmatch.endp[i] - regmatch.startp[i]))
6692 == FAIL)
6693 break;
6694 }
6695 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006696 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006697 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006698 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006699 if (l != NULL)
6700 copy_tv(&li->li_tv, rettv);
6701 else
6702 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006703 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006704 }
6705 else if (l != NULL)
6706 rettv->vval.v_number = idx;
6707 else
6708 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006709 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006710 rettv->vval.v_number =
6711 (varnumber_T)(regmatch.startp[0] - str);
6712 else
6713 rettv->vval.v_number =
6714 (varnumber_T)(regmatch.endp[0] - str);
6715 rettv->vval.v_number += (varnumber_T)(str - expr);
6716 }
6717 }
6718 vim_regfree(regmatch.regprog);
6719 }
6720
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006721theend:
6722 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006723 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006724 listitem_remove(rettv->vval.v_list,
6725 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006726 vim_free(tofree);
6727 p_cpo = save_cpo;
6728}
6729
6730/*
6731 * "match()" function
6732 */
6733 static void
6734f_match(typval_T *argvars, typval_T *rettv)
6735{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006736 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006737}
6738
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006739/*
6740 * "matchend()" function
6741 */
6742 static void
6743f_matchend(typval_T *argvars, typval_T *rettv)
6744{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006745 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006746}
6747
6748/*
6749 * "matchlist()" function
6750 */
6751 static void
6752f_matchlist(typval_T *argvars, typval_T *rettv)
6753{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006754 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006755}
6756
6757/*
6758 * "matchstr()" function
6759 */
6760 static void
6761f_matchstr(typval_T *argvars, typval_T *rettv)
6762{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006763 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006764}
6765
6766/*
6767 * "matchstrpos()" function
6768 */
6769 static void
6770f_matchstrpos(typval_T *argvars, typval_T *rettv)
6771{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006772 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006773}
6774
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006775 static void
6776max_min(typval_T *argvars, typval_T *rettv, int domax)
6777{
6778 varnumber_T n = 0;
6779 varnumber_T i;
6780 int error = FALSE;
6781
6782 if (argvars[0].v_type == VAR_LIST)
6783 {
6784 list_T *l;
6785 listitem_T *li;
6786
6787 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006788 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006789 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006790 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006791 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006792 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6793 n = l->lv_u.nonmat.lv_start;
6794 else
6795 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6796 * l->lv_u.nonmat.lv_stride;
6797 }
6798 else
6799 {
6800 li = l->lv_first;
6801 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006802 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006803 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006804 if (error)
6805 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006806 for (;;)
6807 {
6808 li = li->li_next;
6809 if (li == NULL)
6810 break;
6811 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006812 if (error)
6813 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006814 if (domax ? i > n : i < n)
6815 n = i;
6816 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006817 }
6818 }
6819 }
6820 }
6821 else if (argvars[0].v_type == VAR_DICT)
6822 {
6823 dict_T *d;
6824 int first = TRUE;
6825 hashitem_T *hi;
6826 int todo;
6827
6828 d = argvars[0].vval.v_dict;
6829 if (d != NULL)
6830 {
6831 todo = (int)d->dv_hashtab.ht_used;
6832 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6833 {
6834 if (!HASHITEM_EMPTY(hi))
6835 {
6836 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006837 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006838 if (error)
6839 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006840 if (first)
6841 {
6842 n = i;
6843 first = FALSE;
6844 }
6845 else if (domax ? i > n : i < n)
6846 n = i;
6847 }
6848 }
6849 }
6850 }
6851 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006852 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006853
6854 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006855}
6856
6857/*
6858 * "max()" function
6859 */
6860 static void
6861f_max(typval_T *argvars, typval_T *rettv)
6862{
6863 max_min(argvars, rettv, TRUE);
6864}
6865
6866/*
6867 * "min()" function
6868 */
6869 static void
6870f_min(typval_T *argvars, typval_T *rettv)
6871{
6872 max_min(argvars, rettv, FALSE);
6873}
6874
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006875#if defined(FEAT_MZSCHEME) || defined(PROTO)
6876/*
6877 * "mzeval()" function
6878 */
6879 static void
6880f_mzeval(typval_T *argvars, typval_T *rettv)
6881{
6882 char_u *str;
6883 char_u buf[NUMBUFLEN];
6884
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006885 if (check_restricted() || check_secure())
6886 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006887 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006888 do_mzeval(str, rettv);
6889}
6890
6891 void
6892mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6893{
6894 typval_T argvars[3];
6895
6896 argvars[0].v_type = VAR_STRING;
6897 argvars[0].vval.v_string = name;
6898 copy_tv(args, &argvars[1]);
6899 argvars[2].v_type = VAR_UNKNOWN;
6900 f_call(argvars, rettv);
6901 clear_tv(&argvars[1]);
6902}
6903#endif
6904
6905/*
6906 * "nextnonblank()" function
6907 */
6908 static void
6909f_nextnonblank(typval_T *argvars, typval_T *rettv)
6910{
6911 linenr_T lnum;
6912
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006913 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006914 {
6915 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6916 {
6917 lnum = 0;
6918 break;
6919 }
6920 if (*skipwhite(ml_get(lnum)) != NUL)
6921 break;
6922 }
6923 rettv->vval.v_number = lnum;
6924}
6925
6926/*
6927 * "nr2char()" function
6928 */
6929 static void
6930f_nr2char(typval_T *argvars, typval_T *rettv)
6931{
6932 char_u buf[NUMBUFLEN];
6933
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006934 if (has_mbyte)
6935 {
6936 int utf8 = 0;
6937
6938 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006939 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006940 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006941 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006942 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006943 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006944 }
6945 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006946 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006947 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006948 buf[1] = NUL;
6949 }
6950 rettv->v_type = VAR_STRING;
6951 rettv->vval.v_string = vim_strsave(buf);
6952}
6953
6954/*
6955 * "or(expr, expr)" function
6956 */
6957 static void
6958f_or(typval_T *argvars, typval_T *rettv)
6959{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006960 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6961 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006962}
6963
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006964#ifdef FEAT_PERL
6965/*
6966 * "perleval()" function
6967 */
6968 static void
6969f_perleval(typval_T *argvars, typval_T *rettv)
6970{
6971 char_u *str;
6972 char_u buf[NUMBUFLEN];
6973
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006974 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006975 do_perleval(str, rettv);
6976}
6977#endif
6978
6979#ifdef FEAT_FLOAT
6980/*
6981 * "pow()" function
6982 */
6983 static void
6984f_pow(typval_T *argvars, typval_T *rettv)
6985{
6986 float_T fx = 0.0, fy = 0.0;
6987
6988 rettv->v_type = VAR_FLOAT;
6989 if (get_float_arg(argvars, &fx) == OK
6990 && get_float_arg(&argvars[1], &fy) == OK)
6991 rettv->vval.v_float = pow(fx, fy);
6992 else
6993 rettv->vval.v_float = 0.0;
6994}
6995#endif
6996
6997/*
6998 * "prevnonblank()" function
6999 */
7000 static void
7001f_prevnonblank(typval_T *argvars, typval_T *rettv)
7002{
7003 linenr_T lnum;
7004
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007005 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007006 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7007 lnum = 0;
7008 else
7009 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
7010 --lnum;
7011 rettv->vval.v_number = lnum;
7012}
7013
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007014// This dummy va_list is here because:
7015// - passing a NULL pointer doesn't work when va_list isn't a pointer
7016// - locally in the function results in a "used before set" warning
7017// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007018static va_list ap;
7019
7020/*
7021 * "printf()" function
7022 */
7023 static void
7024f_printf(typval_T *argvars, typval_T *rettv)
7025{
7026 char_u buf[NUMBUFLEN];
7027 int len;
7028 char_u *s;
7029 int saved_did_emsg = did_emsg;
7030 char *fmt;
7031
7032 rettv->v_type = VAR_STRING;
7033 rettv->vval.v_string = NULL;
7034
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007035 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007036 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007037 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007038 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007039 if (!did_emsg)
7040 {
7041 s = alloc(len + 1);
7042 if (s != NULL)
7043 {
7044 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007045 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
7046 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007047 }
7048 }
7049 did_emsg |= saved_did_emsg;
7050}
7051
7052/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007053 * "pum_getpos()" function
7054 */
7055 static void
7056f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7057{
7058 if (rettv_dict_alloc(rettv) != OK)
7059 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007060 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007061}
7062
7063/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007064 * "pumvisible()" function
7065 */
7066 static void
7067f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7068{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007069 if (pum_visible())
7070 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007071}
7072
7073#ifdef FEAT_PYTHON3
7074/*
7075 * "py3eval()" function
7076 */
7077 static void
7078f_py3eval(typval_T *argvars, typval_T *rettv)
7079{
7080 char_u *str;
7081 char_u buf[NUMBUFLEN];
7082
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007083 if (check_restricted() || check_secure())
7084 return;
7085
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007086 if (p_pyx == 0)
7087 p_pyx = 3;
7088
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007089 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007090 do_py3eval(str, rettv);
7091}
7092#endif
7093
7094#ifdef FEAT_PYTHON
7095/*
7096 * "pyeval()" function
7097 */
7098 static void
7099f_pyeval(typval_T *argvars, typval_T *rettv)
7100{
7101 char_u *str;
7102 char_u buf[NUMBUFLEN];
7103
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007104 if (check_restricted() || check_secure())
7105 return;
7106
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007107 if (p_pyx == 0)
7108 p_pyx = 2;
7109
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007110 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007111 do_pyeval(str, rettv);
7112}
7113#endif
7114
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007115#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
7116/*
7117 * "pyxeval()" function
7118 */
7119 static void
7120f_pyxeval(typval_T *argvars, typval_T *rettv)
7121{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007122 if (check_restricted() || check_secure())
7123 return;
7124
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007125# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
7126 init_pyxversion();
7127 if (p_pyx == 2)
7128 f_pyeval(argvars, rettv);
7129 else
7130 f_py3eval(argvars, rettv);
7131# elif defined(FEAT_PYTHON)
7132 f_pyeval(argvars, rettv);
7133# elif defined(FEAT_PYTHON3)
7134 f_py3eval(argvars, rettv);
7135# endif
7136}
7137#endif
7138
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007139static UINT32_T srand_seed_for_testing = 0;
7140static int srand_seed_for_testing_is_used = FALSE;
7141
7142 static void
7143f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
7144{
7145 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007146 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007147 else
7148 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007149 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
7150 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007151 }
7152}
7153
7154 static void
7155init_srand(UINT32_T *x)
7156{
7157#ifndef MSWIN
7158 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
7159#endif
7160
7161 if (srand_seed_for_testing_is_used)
7162 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007163 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007164 return;
7165 }
7166#ifndef MSWIN
7167 if (dev_urandom_state != FAIL)
7168 {
7169 int fd = open("/dev/urandom", O_RDONLY);
7170 struct {
7171 union {
7172 UINT32_T number;
7173 char bytes[sizeof(UINT32_T)];
7174 } contents;
7175 } buf;
7176
7177 // Attempt reading /dev/urandom.
7178 if (fd == -1)
7179 dev_urandom_state = FAIL;
7180 else
7181 {
7182 buf.contents.number = 0;
7183 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
7184 != sizeof(UINT32_T))
7185 dev_urandom_state = FAIL;
7186 else
7187 {
7188 dev_urandom_state = OK;
7189 *x = buf.contents.number;
7190 }
7191 close(fd);
7192 }
7193 }
7194 if (dev_urandom_state != OK)
7195 // Reading /dev/urandom doesn't work, fall back to time().
7196#endif
7197 *x = vim_time();
7198}
7199
7200#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
7201#define SPLITMIX32(x, z) ( \
7202 z = (x += 0x9e3779b9), \
7203 z = (z ^ (z >> 16)) * 0x85ebca6b, \
7204 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
7205 z ^ (z >> 16) \
7206 )
7207#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
7208 result = ROTL(y * 5, 7) * 9; \
7209 t = y << 9; \
7210 z ^= x; \
7211 w ^= y; \
7212 y ^= z, x ^= w; \
7213 z ^= t; \
7214 w = ROTL(w, 11);
7215
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007216/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007217 * "rand()" function
7218 */
7219 static void
7220f_rand(typval_T *argvars, typval_T *rettv)
7221{
7222 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007223 static UINT32_T gx, gy, gz, gw;
7224 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007225 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01007226 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007227
7228 if (argvars[0].v_type == VAR_UNKNOWN)
7229 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007230 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007231 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007232 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007233 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007234 init_srand(&x);
7235
7236 gx = SPLITMIX32(x, z);
7237 gy = SPLITMIX32(x, z);
7238 gz = SPLITMIX32(x, z);
7239 gw = SPLITMIX32(x, z);
7240 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007241 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007242
7243 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007244 }
7245 else if (argvars[0].v_type == VAR_LIST)
7246 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007247 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007248 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007249 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007250
7251 lx = list_find(l, 0L);
7252 ly = list_find(l, 1L);
7253 lz = list_find(l, 2L);
7254 lw = list_find(l, 3L);
7255 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
7256 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
7257 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
7258 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
7259 x = (UINT32_T)lx->li_tv.vval.v_number;
7260 y = (UINT32_T)ly->li_tv.vval.v_number;
7261 z = (UINT32_T)lz->li_tv.vval.v_number;
7262 w = (UINT32_T)lw->li_tv.vval.v_number;
7263
7264 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
7265
7266 lx->li_tv.vval.v_number = (varnumber_T)x;
7267 ly->li_tv.vval.v_number = (varnumber_T)y;
7268 lz->li_tv.vval.v_number = (varnumber_T)z;
7269 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007270 }
7271 else
7272 goto theend;
7273
7274 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007275 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007276 return;
7277
7278theend:
7279 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007280 rettv->v_type = VAR_NUMBER;
7281 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007282}
7283
7284/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007285 * "srand()" function
7286 */
7287 static void
7288f_srand(typval_T *argvars, typval_T *rettv)
7289{
7290 UINT32_T x = 0, z;
7291
7292 if (rettv_list_alloc(rettv) == FAIL)
7293 return;
7294 if (argvars[0].v_type == VAR_UNKNOWN)
7295 {
7296 init_srand(&x);
7297 }
7298 else
7299 {
7300 int error = FALSE;
7301
7302 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7303 if (error)
7304 return;
7305 }
7306
7307 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7308 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7309 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7310 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7311}
7312
7313#undef ROTL
7314#undef SPLITMIX32
7315#undef SHUFFLE_XOSHIRO128STARSTAR
7316
7317/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007318 * "range()" function
7319 */
7320 static void
7321f_range(typval_T *argvars, typval_T *rettv)
7322{
7323 varnumber_T start;
7324 varnumber_T end;
7325 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007326 int error = FALSE;
7327
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007328 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007329 if (argvars[1].v_type == VAR_UNKNOWN)
7330 {
7331 end = start - 1;
7332 start = 0;
7333 }
7334 else
7335 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007336 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007337 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007338 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007339 }
7340
7341 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007342 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007343 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007344 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007345 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007346 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007347 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007348 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007349 list_T *list = rettv->vval.v_list;
7350
7351 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007352 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353 // be called.
7354 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007355 list->lv_u.nonmat.lv_start = start;
7356 list->lv_u.nonmat.lv_end = end;
7357 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007358 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007359 }
7360}
7361
7362/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007363 * Materialize "list".
7364 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365 */
7366 void
7367range_list_materialize(list_T *list)
7368{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007369 varnumber_T start = list->lv_u.nonmat.lv_start;
7370 varnumber_T end = list->lv_u.nonmat.lv_end;
7371 int stride = list->lv_u.nonmat.lv_stride;
7372 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007373
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007374 list->lv_first = NULL;
7375 list->lv_u.mat.lv_last = NULL;
7376 list->lv_len = 0;
7377 list->lv_u.mat.lv_idx_item = NULL;
7378 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7379 if (list_append_number(list, (varnumber_T)i) == FAIL)
7380 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007381}
7382
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007383/*
7384 * "getreginfo()" function
7385 */
7386 static void
7387f_getreginfo(typval_T *argvars, typval_T *rettv)
7388{
7389 char_u *strregname;
7390 int regname;
7391 char_u buf[NUMBUFLEN + 2];
7392 long reglen = 0;
7393 dict_T *dict;
7394 list_T *list;
7395
7396 if (argvars[0].v_type != VAR_UNKNOWN)
7397 {
7398 strregname = tv_get_string_chk(&argvars[0]);
7399 if (strregname == NULL)
7400 return;
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007401 if (in_vim9script() && STRLEN(strregname) > 1)
7402 {
7403 semsg(_(e_register_name_must_be_one_char_str), strregname);
7404 return;
7405 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007406 }
7407 else
7408 strregname = get_vim_var_str(VV_REG);
7409
7410 regname = (strregname == NULL ? '"' : *strregname);
7411 if (regname == 0 || regname == '@')
7412 regname = '"';
7413
7414 if (rettv_dict_alloc(rettv) == FAIL)
7415 return;
7416 dict = rettv->vval.v_dict;
7417
7418 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7419 if (list == NULL)
7420 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007421 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007422
7423 buf[0] = NUL;
7424 buf[1] = NUL;
7425 switch (get_reg_type(regname, &reglen))
7426 {
7427 case MLINE: buf[0] = 'V'; break;
7428 case MCHAR: buf[0] = 'v'; break;
7429 case MBLOCK:
7430 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7431 reglen + 1);
7432 break;
7433 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007434 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007435
7436 buf[0] = get_register_name(get_unname_register());
7437 buf[1] = NUL;
7438 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007439 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007440 else
7441 {
7442 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7443
7444 if (item != NULL)
7445 {
7446 item->di_tv.v_type = VAR_SPECIAL;
7447 item->di_tv.vval.v_number = regname == buf[0]
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007448 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007449 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007450 }
7451 }
7452}
7453
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007454 static void
7455return_register(int regname, typval_T *rettv)
7456{
7457 char_u buf[2] = {0, 0};
7458
7459 buf[0] = (char_u)regname;
7460 rettv->v_type = VAR_STRING;
7461 rettv->vval.v_string = vim_strsave(buf);
7462}
7463
7464/*
7465 * "reg_executing()" function
7466 */
7467 static void
7468f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7469{
7470 return_register(reg_executing, rettv);
7471}
7472
7473/*
7474 * "reg_recording()" function
7475 */
7476 static void
7477f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7478{
7479 return_register(reg_recording, rettv);
7480}
7481
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007482/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007483 * "rename({from}, {to})" function
7484 */
7485 static void
7486f_rename(typval_T *argvars, typval_T *rettv)
7487{
7488 char_u buf[NUMBUFLEN];
7489
7490 if (check_restricted() || check_secure())
7491 rettv->vval.v_number = -1;
7492 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007493 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7494 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007495}
7496
7497/*
7498 * "repeat()" function
7499 */
7500 static void
7501f_repeat(typval_T *argvars, typval_T *rettv)
7502{
7503 char_u *p;
7504 int n;
7505 int slen;
7506 int len;
7507 char_u *r;
7508 int i;
7509
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007510 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007511 if (argvars[0].v_type == VAR_LIST)
7512 {
7513 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7514 while (n-- > 0)
7515 if (list_extend(rettv->vval.v_list,
7516 argvars[0].vval.v_list, NULL) == FAIL)
7517 break;
7518 }
7519 else
7520 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007521 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007522 rettv->v_type = VAR_STRING;
7523 rettv->vval.v_string = NULL;
7524
7525 slen = (int)STRLEN(p);
7526 len = slen * n;
7527 if (len <= 0)
7528 return;
7529
7530 r = alloc(len + 1);
7531 if (r != NULL)
7532 {
7533 for (i = 0; i < n; i++)
7534 mch_memmove(r + i * slen, p, (size_t)slen);
7535 r[len] = NUL;
7536 }
7537
7538 rettv->vval.v_string = r;
7539 }
7540}
7541
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007542#define SP_NOMOVE 0x01 // don't move cursor
7543#define SP_REPEAT 0x02 // repeat to find outer pair
7544#define SP_RETCOUNT 0x04 // return matchcount
7545#define SP_SETPCMARK 0x08 // set previous context mark
7546#define SP_START 0x10 // accept match at start position
7547#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7548#define SP_END 0x40 // leave cursor at end of match
7549#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007550
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007551/*
7552 * Get flags for a search function.
7553 * Possibly sets "p_ws".
7554 * Returns BACKWARD, FORWARD or zero (for an error).
7555 */
7556 static int
7557get_search_arg(typval_T *varp, int *flagsp)
7558{
7559 int dir = FORWARD;
7560 char_u *flags;
7561 char_u nbuf[NUMBUFLEN];
7562 int mask;
7563
7564 if (varp->v_type != VAR_UNKNOWN)
7565 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007566 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007567 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007568 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007569 while (*flags != NUL)
7570 {
7571 switch (*flags)
7572 {
7573 case 'b': dir = BACKWARD; break;
7574 case 'w': p_ws = TRUE; break;
7575 case 'W': p_ws = FALSE; break;
7576 default: mask = 0;
7577 if (flagsp != NULL)
7578 switch (*flags)
7579 {
7580 case 'c': mask = SP_START; break;
7581 case 'e': mask = SP_END; break;
7582 case 'm': mask = SP_RETCOUNT; break;
7583 case 'n': mask = SP_NOMOVE; break;
7584 case 'p': mask = SP_SUBPAT; break;
7585 case 'r': mask = SP_REPEAT; break;
7586 case 's': mask = SP_SETPCMARK; break;
7587 case 'z': mask = SP_COLUMN; break;
7588 }
7589 if (mask == 0)
7590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007591 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007592 dir = 0;
7593 }
7594 else
7595 *flagsp |= mask;
7596 }
7597 if (dir == 0)
7598 break;
7599 ++flags;
7600 }
7601 }
7602 return dir;
7603}
7604
7605/*
7606 * Shared by search() and searchpos() functions.
7607 */
7608 static int
7609search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7610{
7611 int flags;
7612 char_u *pat;
7613 pos_T pos;
7614 pos_T save_cursor;
7615 int save_p_ws = p_ws;
7616 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007617 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007618 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007619#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007620 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007621 long time_limit = 0;
7622#endif
7623 int options = SEARCH_KEEP;
7624 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007625 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007626 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007627 pos_T firstpos;
7628
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007629 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007630 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007631 if (dir == 0)
7632 goto theend;
7633 flags = *flagsp;
7634 if (flags & SP_START)
7635 options |= SEARCH_START;
7636 if (flags & SP_END)
7637 options |= SEARCH_END;
7638 if (flags & SP_COLUMN)
7639 options |= SEARCH_COL;
7640
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007641 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007642 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7643 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007644 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007645 if (lnum_stop < 0)
7646 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007647 if (argvars[3].v_type != VAR_UNKNOWN)
7648 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007649#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007650 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007651 if (time_limit < 0)
7652 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007653#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007654 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007655 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007656 }
7657
7658#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007659 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007660 profile_setlimit(time_limit, &tm);
7661#endif
7662
7663 /*
7664 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7665 * Check to make sure only those flags are set.
7666 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7667 * flags cannot be set. Check for that condition also.
7668 */
7669 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7670 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7671 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007672 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007673 goto theend;
7674 }
7675
7676 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007677 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007678 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007679 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7680#ifdef FEAT_RELTIME
7681 sia.sa_tm = &tm;
7682#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007683
7684 // Repeat until {skip} returns FALSE.
7685 for (;;)
7686 {
7687 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007688 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007689 // finding the first match again means there is no match where {skip}
7690 // evaluates to zero.
7691 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7692 subpatnum = FAIL;
7693
Bram Moolenaara9c01042020-06-07 14:50:50 +02007694 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007695 // didn't find it or no skip argument
7696 break;
7697 firstpos = pos;
7698
Bram Moolenaara9c01042020-06-07 14:50:50 +02007699 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007700 {
7701 int do_skip;
7702 int err;
7703 pos_T save_pos = curwin->w_cursor;
7704
7705 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007706 err = FALSE;
7707 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007708 curwin->w_cursor = save_pos;
7709 if (err)
7710 {
7711 // Evaluating {skip} caused an error, break here.
7712 subpatnum = FAIL;
7713 break;
7714 }
7715 if (!do_skip)
7716 break;
7717 }
7718 }
7719
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007720 if (subpatnum != FAIL)
7721 {
7722 if (flags & SP_SUBPAT)
7723 retval = subpatnum;
7724 else
7725 retval = pos.lnum;
7726 if (flags & SP_SETPCMARK)
7727 setpcmark();
7728 curwin->w_cursor = pos;
7729 if (match_pos != NULL)
7730 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007731 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007732 match_pos->lnum = pos.lnum;
7733 match_pos->col = pos.col + 1;
7734 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007735 // "/$" will put the cursor after the end of the line, may need to
7736 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007737 check_cursor();
7738 }
7739
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007740 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007741 if (flags & SP_NOMOVE)
7742 curwin->w_cursor = save_cursor;
7743 else
7744 curwin->w_set_curswant = TRUE;
7745theend:
7746 p_ws = save_p_ws;
7747
7748 return retval;
7749}
7750
7751#ifdef FEAT_FLOAT
7752
7753/*
7754 * round() is not in C90, use ceil() or floor() instead.
7755 */
7756 float_T
7757vim_round(float_T f)
7758{
7759 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
7760}
7761
7762/*
7763 * "round({float})" function
7764 */
7765 static void
7766f_round(typval_T *argvars, typval_T *rettv)
7767{
7768 float_T f = 0.0;
7769
7770 rettv->v_type = VAR_FLOAT;
7771 if (get_float_arg(argvars, &f) == OK)
7772 rettv->vval.v_float = vim_round(f);
7773 else
7774 rettv->vval.v_float = 0.0;
7775}
7776#endif
7777
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007778#ifdef FEAT_RUBY
7779/*
7780 * "rubyeval()" function
7781 */
7782 static void
7783f_rubyeval(typval_T *argvars, typval_T *rettv)
7784{
7785 char_u *str;
7786 char_u buf[NUMBUFLEN];
7787
7788 str = tv_get_string_buf(&argvars[0], buf);
7789 do_rubyeval(str, rettv);
7790}
7791#endif
7792
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007793/*
7794 * "screenattr()" function
7795 */
7796 static void
7797f_screenattr(typval_T *argvars, typval_T *rettv)
7798{
7799 int row;
7800 int col;
7801 int c;
7802
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007803 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7804 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007805 if (row < 0 || row >= screen_Rows
7806 || col < 0 || col >= screen_Columns)
7807 c = -1;
7808 else
7809 c = ScreenAttrs[LineOffset[row] + col];
7810 rettv->vval.v_number = c;
7811}
7812
7813/*
7814 * "screenchar()" function
7815 */
7816 static void
7817f_screenchar(typval_T *argvars, typval_T *rettv)
7818{
7819 int row;
7820 int col;
7821 int off;
7822 int c;
7823
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007824 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7825 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007826 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007827 c = -1;
7828 else
7829 {
7830 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007831 if (enc_utf8 && ScreenLinesUC[off] != 0)
7832 c = ScreenLinesUC[off];
7833 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007834 c = ScreenLines[off];
7835 }
7836 rettv->vval.v_number = c;
7837}
7838
7839/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007840 * "screenchars()" function
7841 */
7842 static void
7843f_screenchars(typval_T *argvars, typval_T *rettv)
7844{
7845 int row;
7846 int col;
7847 int off;
7848 int c;
7849 int i;
7850
7851 if (rettv_list_alloc(rettv) == FAIL)
7852 return;
7853 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7854 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7855 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7856 return;
7857
7858 off = LineOffset[row] + col;
7859 if (enc_utf8 && ScreenLinesUC[off] != 0)
7860 c = ScreenLinesUC[off];
7861 else
7862 c = ScreenLines[off];
7863 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7864
7865 if (enc_utf8)
7866
7867 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7868 list_append_number(rettv->vval.v_list,
7869 (varnumber_T)ScreenLinesC[i][off]);
7870}
7871
7872/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007873 * "screencol()" function
7874 *
7875 * First column is 1 to be consistent with virtcol().
7876 */
7877 static void
7878f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7879{
7880 rettv->vval.v_number = screen_screencol() + 1;
7881}
7882
7883/*
7884 * "screenrow()" function
7885 */
7886 static void
7887f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7888{
7889 rettv->vval.v_number = screen_screenrow() + 1;
7890}
7891
7892/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007893 * "screenstring()" function
7894 */
7895 static void
7896f_screenstring(typval_T *argvars, typval_T *rettv)
7897{
7898 int row;
7899 int col;
7900 int off;
7901 int c;
7902 int i;
7903 char_u buf[MB_MAXBYTES + 1];
7904 int buflen = 0;
7905
7906 rettv->vval.v_string = NULL;
7907 rettv->v_type = VAR_STRING;
7908
7909 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7910 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7911 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7912 return;
7913
7914 off = LineOffset[row] + col;
7915 if (enc_utf8 && ScreenLinesUC[off] != 0)
7916 c = ScreenLinesUC[off];
7917 else
7918 c = ScreenLines[off];
7919 buflen += mb_char2bytes(c, buf);
7920
Bram Moolenaarf1387282021-03-22 17:11:15 +01007921 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007922 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7923 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7924
7925 buf[buflen] = NUL;
7926 rettv->vval.v_string = vim_strsave(buf);
7927}
7928
7929/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007930 * "search()" function
7931 */
7932 static void
7933f_search(typval_T *argvars, typval_T *rettv)
7934{
7935 int flags = 0;
7936
7937 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7938}
7939
7940/*
7941 * "searchdecl()" function
7942 */
7943 static void
7944f_searchdecl(typval_T *argvars, typval_T *rettv)
7945{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007946 int locally = TRUE;
7947 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007948 int error = FALSE;
7949 char_u *name;
7950
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007951 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007952
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007953 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007954 if (argvars[1].v_type != VAR_UNKNOWN)
7955 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007956 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007957 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007958 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007959 }
7960 if (!error && name != NULL)
7961 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7962 locally, thisblock, SEARCH_KEEP) == FAIL;
7963}
7964
7965/*
7966 * Used by searchpair() and searchpairpos()
7967 */
7968 static int
7969searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7970{
7971 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007972 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007973 int save_p_ws = p_ws;
7974 int dir;
7975 int flags = 0;
7976 char_u nbuf1[NUMBUFLEN];
7977 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007978 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007979 long lnum_stop = 0;
7980 long time_limit = 0;
7981
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007982 // Get the three pattern arguments: start, middle, end. Will result in an
7983 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007984 spat = tv_get_string_chk(&argvars[0]);
7985 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7986 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007987 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007988 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007990 // Handle the optional fourth argument: flags
7991 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007992 if (dir == 0)
7993 goto theend;
7994
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007995 // Don't accept SP_END or SP_SUBPAT.
7996 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007997 if ((flags & (SP_END | SP_SUBPAT)) != 0
7998 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7999 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008000 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008001 goto theend;
8002 }
8003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008004 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008005 if (flags & SP_REPEAT)
8006 p_ws = FALSE;
8007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008008 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008009 if (argvars[3].v_type == VAR_UNKNOWN
8010 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01008011 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008012 else
8013 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02008014 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01008015 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02008016
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008017 if (argvars[5].v_type != VAR_UNKNOWN)
8018 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008019 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008020 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008022 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
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#ifdef FEAT_RELTIME
8026 if (argvars[6].v_type != VAR_UNKNOWN)
8027 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008028 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008029 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008030 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008031 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008032 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008033 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008034 }
8035#endif
8036 }
8037 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008038
8039 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
8040 match_pos, lnum_stop, time_limit);
8041
8042theend:
8043 p_ws = save_p_ws;
8044
8045 return retval;
8046}
8047
8048/*
8049 * "searchpair()" function
8050 */
8051 static void
8052f_searchpair(typval_T *argvars, typval_T *rettv)
8053{
8054 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
8055}
8056
8057/*
8058 * "searchpairpos()" function
8059 */
8060 static void
8061f_searchpairpos(typval_T *argvars, typval_T *rettv)
8062{
8063 pos_T match_pos;
8064 int lnum = 0;
8065 int col = 0;
8066
8067 if (rettv_list_alloc(rettv) == FAIL)
8068 return;
8069
8070 if (searchpair_cmn(argvars, &match_pos) > 0)
8071 {
8072 lnum = match_pos.lnum;
8073 col = match_pos.col;
8074 }
8075
8076 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8077 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8078}
8079
8080/*
8081 * Search for a start/middle/end thing.
8082 * Used by searchpair(), see its documentation for the details.
8083 * Returns 0 or -1 for no match,
8084 */
8085 long
8086do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008087 char_u *spat, // start pattern
8088 char_u *mpat, // middle pattern
8089 char_u *epat, // end pattern
8090 int dir, // BACKWARD or FORWARD
8091 typval_T *skip, // skip expression
8092 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008093 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008094 linenr_T lnum_stop, // stop at this line if not zero
8095 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008096{
8097 char_u *save_cpo;
8098 char_u *pat, *pat2 = NULL, *pat3 = NULL;
8099 long retval = 0;
8100 pos_T pos;
8101 pos_T firstpos;
8102 pos_T foundpos;
8103 pos_T save_cursor;
8104 pos_T save_pos;
8105 int n;
8106 int r;
8107 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01008108 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008109 int err;
8110 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008111#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008112 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008113#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008114
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008115 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008116 save_cpo = p_cpo;
8117 p_cpo = empty_option;
8118
8119#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008120 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008121 profile_setlimit(time_limit, &tm);
8122#endif
8123
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008124 // Make two search patterns: start/end (pat2, for in nested pairs) and
8125 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02008126 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
8127 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008128 if (pat2 == NULL || pat3 == NULL)
8129 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008130 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008131 if (*mpat == NUL)
8132 STRCPY(pat3, pat2);
8133 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008134 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008135 spat, epat, mpat);
8136 if (flags & SP_START)
8137 options |= SEARCH_START;
8138
Bram Moolenaar48570482017-10-30 21:48:41 +01008139 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02008140 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01008141
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008142 save_cursor = curwin->w_cursor;
8143 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008144 CLEAR_POS(&firstpos);
8145 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008146 pat = pat3;
8147 for (;;)
8148 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008149 searchit_arg_T sia;
8150
Bram Moolenaara80faa82020-04-12 19:37:17 +02008151 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008152 sia.sa_stop_lnum = lnum_stop;
8153#ifdef FEAT_RELTIME
8154 sia.sa_tm = &tm;
8155#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01008156 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008157 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008158 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008159 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008160 break;
8161
8162 if (firstpos.lnum == 0)
8163 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008164 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008166 // Found the same position again. Can happen with a pattern that
8167 // has "\zs" at the end and searching backwards. Advance one
8168 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008169 if (dir == BACKWARD)
8170 decl(&pos);
8171 else
8172 incl(&pos);
8173 }
8174 foundpos = pos;
8175
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008176 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008177 options &= ~SEARCH_START;
8178
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008179 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01008180 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008181 {
8182 save_pos = curwin->w_cursor;
8183 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01008184 err = FALSE;
8185 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008186 curwin->w_cursor = save_pos;
8187 if (err)
8188 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008189 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008190 curwin->w_cursor = save_cursor;
8191 retval = -1;
8192 break;
8193 }
8194 if (r)
8195 continue;
8196 }
8197
8198 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8199 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008200 // Found end when searching backwards or start when searching
8201 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008202 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008203 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008204 }
8205 else
8206 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008207 // Found end when searching forward or start when searching
8208 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008209 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008210 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008211 }
8212
8213 if (nest == 0)
8214 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008215 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008216 if (flags & SP_RETCOUNT)
8217 ++retval;
8218 else
8219 retval = pos.lnum;
8220 if (flags & SP_SETPCMARK)
8221 setpcmark();
8222 curwin->w_cursor = pos;
8223 if (!(flags & SP_REPEAT))
8224 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008225 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008226 }
8227 }
8228
8229 if (match_pos != NULL)
8230 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008231 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008232 match_pos->lnum = curwin->w_cursor.lnum;
8233 match_pos->col = curwin->w_cursor.col + 1;
8234 }
8235
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008236 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008237 if ((flags & SP_NOMOVE) || retval == 0)
8238 curwin->w_cursor = save_cursor;
8239
8240theend:
8241 vim_free(pat2);
8242 vim_free(pat3);
8243 if (p_cpo == empty_option)
8244 p_cpo = save_cpo;
8245 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008246 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008247 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008248 // If it's still empty it was changed and restored, need to restore in
8249 // the complicated way.
8250 if (*p_cpo == NUL)
8251 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008252 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008253 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008254
8255 return retval;
8256}
8257
8258/*
8259 * "searchpos()" function
8260 */
8261 static void
8262f_searchpos(typval_T *argvars, typval_T *rettv)
8263{
8264 pos_T match_pos;
8265 int lnum = 0;
8266 int col = 0;
8267 int n;
8268 int flags = 0;
8269
8270 if (rettv_list_alloc(rettv) == FAIL)
8271 return;
8272
8273 n = search_cmn(argvars, &match_pos, &flags);
8274 if (n > 0)
8275 {
8276 lnum = match_pos.lnum;
8277 col = match_pos.col;
8278 }
8279
8280 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8281 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8282 if (flags & SP_SUBPAT)
8283 list_append_number(rettv->vval.v_list, (varnumber_T)n);
8284}
8285
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008286/*
8287 * Set the cursor or mark position.
8288 * If 'charpos' is TRUE, then use the column number as a character offet.
8289 * Otherwise use the column number as a byte offset.
8290 */
8291 static void
8292set_position(typval_T *argvars, typval_T *rettv, int charpos)
8293{
8294 pos_T pos;
8295 int fnum;
8296 char_u *name;
8297 colnr_T curswant = -1;
8298
8299 rettv->vval.v_number = -1;
8300
8301 name = tv_get_string_chk(argvars);
8302 if (name != NULL)
8303 {
8304 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
8305 {
8306 if (pos.col != MAXCOL && --pos.col < 0)
8307 pos.col = 0;
8308 if ((name[0] == '.' && name[1] == NUL))
8309 {
8310 // set cursor; "fnum" is ignored
8311 curwin->w_cursor = pos;
8312 if (curswant >= 0)
8313 {
8314 curwin->w_curswant = curswant - 1;
8315 curwin->w_set_curswant = FALSE;
8316 }
8317 check_cursor();
8318 rettv->vval.v_number = 0;
8319 }
8320 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8321 {
8322 // set mark
8323 if (setmark_pos(name[1], &pos, fnum) == OK)
8324 rettv->vval.v_number = 0;
8325 }
8326 else
8327 emsg(_(e_invarg));
8328 }
8329 }
8330}
8331/*
8332 * "setcharpos()" function
8333 */
8334 static void
8335f_setcharpos(typval_T *argvars, typval_T *rettv)
8336{
8337 set_position(argvars, rettv, TRUE);
8338}
8339
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008340 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008341f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8342{
8343 dict_T *d;
8344 dictitem_T *di;
8345 char_u *csearch;
8346
8347 if (argvars[0].v_type != VAR_DICT)
8348 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008349 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008350 return;
8351 }
8352
8353 if ((d = argvars[0].vval.v_dict) != NULL)
8354 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008355 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008356 if (csearch != NULL)
8357 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008358 if (enc_utf8)
8359 {
8360 int pcc[MAX_MCO];
8361 int c = utfc_ptr2char(csearch, pcc);
8362
8363 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8364 }
8365 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008366 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008367 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008368 }
8369
8370 di = dict_find(d, (char_u *)"forward", -1);
8371 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008372 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008373 ? FORWARD : BACKWARD);
8374
8375 di = dict_find(d, (char_u *)"until", -1);
8376 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008377 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008378 }
8379}
8380
8381/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008382 * "setcursorcharpos" function
8383 */
8384 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008385f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008386{
8387 set_cursorpos(argvars, rettv, TRUE);
8388}
8389
8390/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008391 * "setenv()" function
8392 */
8393 static void
8394f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8395{
8396 char_u namebuf[NUMBUFLEN];
8397 char_u valbuf[NUMBUFLEN];
8398 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8399
8400 if (argvars[1].v_type == VAR_SPECIAL
8401 && argvars[1].vval.v_number == VVAL_NULL)
8402 vim_unsetenv(name);
8403 else
8404 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8405}
8406
8407/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008408 * "setfperm({fname}, {mode})" function
8409 */
8410 static void
8411f_setfperm(typval_T *argvars, typval_T *rettv)
8412{
8413 char_u *fname;
8414 char_u modebuf[NUMBUFLEN];
8415 char_u *mode_str;
8416 int i;
8417 int mask;
8418 int mode = 0;
8419
8420 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008421 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008422 if (fname == NULL)
8423 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008424 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008425 if (mode_str == NULL)
8426 return;
8427 if (STRLEN(mode_str) != 9)
8428 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008429 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008430 return;
8431 }
8432
8433 mask = 1;
8434 for (i = 8; i >= 0; --i)
8435 {
8436 if (mode_str[i] != '-')
8437 mode |= mask;
8438 mask = mask << 1;
8439 }
8440 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8441}
8442
8443/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008444 * "setpos()" function
8445 */
8446 static void
8447f_setpos(typval_T *argvars, typval_T *rettv)
8448{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008449 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008450}
8451
8452/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008453 * Translate a register type string to the yank type and block length
8454 */
8455 static int
8456get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8457{
8458 char_u *stropt = *pp;
8459 switch (*stropt)
8460 {
8461 case 'v': case 'c': // character-wise selection
8462 *yank_type = MCHAR;
8463 break;
8464 case 'V': case 'l': // line-wise selection
8465 *yank_type = MLINE;
8466 break;
8467 case 'b': case Ctrl_V: // block-wise selection
8468 *yank_type = MBLOCK;
8469 if (VIM_ISDIGIT(stropt[1]))
8470 {
8471 ++stropt;
8472 *block_len = getdigits(&stropt) - 1;
8473 --stropt;
8474 }
8475 break;
8476 default:
8477 return FAIL;
8478 }
8479 *pp = stropt;
8480 return OK;
8481}
8482
8483/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008484 * "setreg()" function
8485 */
8486 static void
8487f_setreg(typval_T *argvars, typval_T *rettv)
8488{
8489 int regname;
8490 char_u *strregname;
8491 char_u *stropt;
8492 char_u *strval;
8493 int append;
8494 char_u yank_type;
8495 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008496 typval_T *regcontents;
8497 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008498
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008499 pointreg = 0;
8500 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008501 block_len = -1;
8502 yank_type = MAUTO;
8503 append = FALSE;
8504
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008505 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008506 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008507
8508 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008509 return; // type error; errmsg already given
Bram Moolenaar418a29f2021-02-10 22:23:41 +01008510 if (in_vim9script() && STRLEN(strregname) > 1)
8511 {
8512 semsg(_(e_register_name_must_be_one_char_str), strregname);
8513 return;
8514 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008515 regname = *strregname;
8516 if (regname == 0 || regname == '@')
8517 regname = '"';
8518
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008519 if (argvars[1].v_type == VAR_DICT)
8520 {
8521 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008522 dictitem_T *di;
8523
8524 if (d == NULL || d->dv_hashtab.ht_used == 0)
8525 {
8526 // Empty dict, clear the register (like setreg(0, []))
8527 char_u *lstval[2] = {NULL, NULL};
8528 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8529 return;
8530 }
8531
8532 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008533 if (di != NULL)
8534 regcontents = &di->di_tv;
8535
8536 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8537 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008538 {
8539 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8540
8541 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008542 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008543 semsg(_(e_invargval), "value");
8544 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008545 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008546 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008547
8548 if (regname == '"')
8549 {
8550 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8551 if (stropt != NULL)
8552 {
8553 pointreg = *stropt;
8554 regname = pointreg;
8555 }
8556 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008557 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008558 pointreg = regname;
8559 }
8560 else
8561 regcontents = &argvars[1];
8562
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008563 if (argvars[2].v_type != VAR_UNKNOWN)
8564 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008565 if (yank_type != MAUTO)
8566 {
8567 semsg(_(e_toomanyarg), "setreg");
8568 return;
8569 }
8570
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008571 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008572 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008573 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008574 for (; *stropt != NUL; ++stropt)
8575 switch (*stropt)
8576 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008577 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008578 append = TRUE;
8579 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008580 default:
8581 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008582 }
8583 }
8584
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008585 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008586 {
8587 char_u **lstval;
8588 char_u **allocval;
8589 char_u buf[NUMBUFLEN];
8590 char_u **curval;
8591 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008592 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008593 listitem_T *li;
8594 int len;
8595
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008596 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008597 len = ll == NULL ? 0 : ll->lv_len;
8598
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008599 // First half: use for pointers to result lines; second half: use for
8600 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008601 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602 if (lstval == NULL)
8603 return;
8604 curval = lstval;
8605 allocval = lstval + len + 2;
8606 curallocval = allocval;
8607
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008608 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008609 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008610 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008611 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008612 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008613 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008614 if (strval == NULL)
8615 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008616 if (strval == buf)
8617 {
8618 // Need to make a copy, next tv_get_string_buf_chk() will
8619 // overwrite the string.
8620 strval = vim_strsave(buf);
8621 if (strval == NULL)
8622 goto free_lstval;
8623 *curallocval++ = strval;
8624 }
8625 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008626 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008627 }
8628 *curval++ = NULL;
8629
8630 write_reg_contents_lst(regname, lstval, -1,
8631 append, yank_type, block_len);
8632free_lstval:
8633 while (curallocval > allocval)
8634 vim_free(*--curallocval);
8635 vim_free(lstval);
8636 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008637 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008638 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008639 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008640 if (strval == NULL)
8641 return;
8642 write_reg_contents_ex(regname, strval, -1,
8643 append, yank_type, block_len);
8644 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008645 if (pointreg != 0)
8646 get_yank_register(pointreg, TRUE);
8647
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008648 rettv->vval.v_number = 0;
8649}
8650
8651/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008652 * "settagstack()" function
8653 */
8654 static void
8655f_settagstack(typval_T *argvars, typval_T *rettv)
8656{
8657 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8658 win_T *wp;
8659 dict_T *d;
8660 int action = 'r';
8661
8662 rettv->vval.v_number = -1;
8663
8664 // first argument: window number or id
8665 wp = find_win_by_nr_or_id(&argvars[0]);
8666 if (wp == NULL)
8667 return;
8668
8669 // second argument: dict with items to set in the tag stack
8670 if (argvars[1].v_type != VAR_DICT)
8671 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008672 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008673 return;
8674 }
8675 d = argvars[1].vval.v_dict;
8676 if (d == NULL)
8677 return;
8678
8679 // third argument: action - 'a' for append and 'r' for replace.
8680 // default is to replace the stack.
8681 if (argvars[2].v_type == VAR_UNKNOWN)
8682 action = 'r';
8683 else if (argvars[2].v_type == VAR_STRING)
8684 {
8685 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008686 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008687 if (actstr == NULL)
8688 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008689 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8690 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008691 action = *actstr;
8692 else
8693 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008694 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008695 return;
8696 }
8697 }
8698 else
8699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008700 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008701 return;
8702 }
8703
8704 if (set_tagstack(wp, d, action) == OK)
8705 rettv->vval.v_number = 0;
8706}
8707
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008708#ifdef FEAT_CRYPT
8709/*
8710 * "sha256({string})" function
8711 */
8712 static void
8713f_sha256(typval_T *argvars, typval_T *rettv)
8714{
8715 char_u *p;
8716
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008717 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008718 rettv->vval.v_string = vim_strsave(
8719 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8720 rettv->v_type = VAR_STRING;
8721}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008722#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723
8724/*
8725 * "shellescape({string})" function
8726 */
8727 static void
8728f_shellescape(typval_T *argvars, typval_T *rettv)
8729{
Bram Moolenaar20615522017-06-05 18:46:26 +02008730 int do_special = non_zero_arg(&argvars[1]);
8731
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008732 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008733 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008734 rettv->v_type = VAR_STRING;
8735}
8736
8737/*
8738 * shiftwidth() function
8739 */
8740 static void
8741f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8742{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008743 rettv->vval.v_number = 0;
8744
8745 if (argvars[0].v_type != VAR_UNKNOWN)
8746 {
8747 long col;
8748
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008749 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008750 if (col < 0)
8751 return; // type error; errmsg already given
8752#ifdef FEAT_VARTABS
8753 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8754 return;
8755#endif
8756 }
8757
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008758 rettv->vval.v_number = get_sw_value(curbuf);
8759}
8760
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008761#ifdef FEAT_FLOAT
8762/*
8763 * "sin()" function
8764 */
8765 static void
8766f_sin(typval_T *argvars, typval_T *rettv)
8767{
8768 float_T f = 0.0;
8769
8770 rettv->v_type = VAR_FLOAT;
8771 if (get_float_arg(argvars, &f) == OK)
8772 rettv->vval.v_float = sin(f);
8773 else
8774 rettv->vval.v_float = 0.0;
8775}
8776
8777/*
8778 * "sinh()" function
8779 */
8780 static void
8781f_sinh(typval_T *argvars, typval_T *rettv)
8782{
8783 float_T f = 0.0;
8784
8785 rettv->v_type = VAR_FLOAT;
8786 if (get_float_arg(argvars, &f) == OK)
8787 rettv->vval.v_float = sinh(f);
8788 else
8789 rettv->vval.v_float = 0.0;
8790}
8791#endif
8792
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008793/*
8794 * "soundfold({word})" function
8795 */
8796 static void
8797f_soundfold(typval_T *argvars, typval_T *rettv)
8798{
8799 char_u *s;
8800
8801 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008802 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008803#ifdef FEAT_SPELL
8804 rettv->vval.v_string = eval_soundfold(s);
8805#else
8806 rettv->vval.v_string = vim_strsave(s);
8807#endif
8808}
8809
8810/*
8811 * "spellbadword()" function
8812 */
8813 static void
8814f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8815{
8816 char_u *word = (char_u *)"";
8817 hlf_T attr = HLF_COUNT;
8818 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008819#ifdef FEAT_SPELL
8820 int wo_spell_save = curwin->w_p_spell;
8821
8822 if (!curwin->w_p_spell)
8823 {
8824 did_set_spelllang(curwin);
8825 curwin->w_p_spell = TRUE;
8826 }
8827
8828 if (*curwin->w_s->b_p_spl == NUL)
8829 {
8830 emsg(_(e_no_spell));
8831 curwin->w_p_spell = wo_spell_save;
8832 return;
8833 }
8834#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835
8836 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008837 {
8838#ifdef FEAT_SPELL
8839 curwin->w_p_spell = wo_spell_save;
8840#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008841 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008842 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008843
8844#ifdef FEAT_SPELL
8845 if (argvars[0].v_type == VAR_UNKNOWN)
8846 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008847 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008848 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8849 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008850 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008851 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008852 curwin->w_set_curswant = TRUE;
8853 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008854 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008855 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008856 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008857 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008858 int capcol = -1;
8859
8860 if (str != NULL)
8861 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008862 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008863 while (*str != NUL)
8864 {
8865 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8866 if (attr != HLF_COUNT)
8867 {
8868 word = str;
8869 break;
8870 }
8871 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008872 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008873 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008874 }
8875 }
8876 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008877 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008878#endif
8879
8880 list_append_string(rettv->vval.v_list, word, len);
8881 list_append_string(rettv->vval.v_list, (char_u *)(
8882 attr == HLF_SPB ? "bad" :
8883 attr == HLF_SPR ? "rare" :
8884 attr == HLF_SPL ? "local" :
8885 attr == HLF_SPC ? "caps" :
8886 ""), -1);
8887}
8888
8889/*
8890 * "spellsuggest()" function
8891 */
8892 static void
8893f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8894{
8895#ifdef FEAT_SPELL
8896 char_u *str;
8897 int typeerr = FALSE;
8898 int maxcount;
8899 garray_T ga;
8900 int i;
8901 listitem_T *li;
8902 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008903 int wo_spell_save = curwin->w_p_spell;
8904
8905 if (!curwin->w_p_spell)
8906 {
8907 did_set_spelllang(curwin);
8908 curwin->w_p_spell = TRUE;
8909 }
8910
8911 if (*curwin->w_s->b_p_spl == NUL)
8912 {
8913 emsg(_(e_no_spell));
8914 curwin->w_p_spell = wo_spell_save;
8915 return;
8916 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008917#endif
8918
8919 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008920 {
8921#ifdef FEAT_SPELL
8922 curwin->w_p_spell = wo_spell_save;
8923#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008925 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008926
8927#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008928 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008929 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008930 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008931 if (argvars[1].v_type != VAR_UNKNOWN)
8932 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008933 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008934 if (maxcount <= 0)
8935 return;
8936 if (argvars[2].v_type != VAR_UNKNOWN)
8937 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008938 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008939 if (typeerr)
8940 return;
8941 }
8942 }
8943 else
8944 maxcount = 25;
8945
8946 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8947
8948 for (i = 0; i < ga.ga_len; ++i)
8949 {
8950 str = ((char_u **)ga.ga_data)[i];
8951
8952 li = listitem_alloc();
8953 if (li == NULL)
8954 vim_free(str);
8955 else
8956 {
8957 li->li_tv.v_type = VAR_STRING;
8958 li->li_tv.v_lock = 0;
8959 li->li_tv.vval.v_string = str;
8960 list_append(rettv->vval.v_list, li);
8961 }
8962 }
8963 ga_clear(&ga);
8964 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008965 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008966#endif
8967}
8968
8969 static void
8970f_split(typval_T *argvars, typval_T *rettv)
8971{
8972 char_u *str;
8973 char_u *end;
8974 char_u *pat = NULL;
8975 regmatch_T regmatch;
8976 char_u patbuf[NUMBUFLEN];
8977 char_u *save_cpo;
8978 int match;
8979 colnr_T col = 0;
8980 int keepempty = FALSE;
8981 int typeerr = FALSE;
8982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008983 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008984 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008985 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008986
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008987 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008988 if (argvars[1].v_type != VAR_UNKNOWN)
8989 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008990 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008991 if (pat == NULL)
8992 typeerr = TRUE;
8993 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008994 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008995 }
8996 if (pat == NULL || *pat == NUL)
8997 pat = (char_u *)"[\\x01- ]\\+";
8998
8999 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02009000 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009001 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02009002 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009003
9004 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9005 if (regmatch.regprog != NULL)
9006 {
9007 regmatch.rm_ic = FALSE;
9008 while (*str != NUL || keepempty)
9009 {
9010 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009011 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009012 else
9013 match = vim_regexec_nl(&regmatch, str, col);
9014 if (match)
9015 end = regmatch.startp[0];
9016 else
9017 end = str + STRLEN(str);
9018 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
9019 && *str != NUL && match && end < regmatch.endp[0]))
9020 {
9021 if (list_append_string(rettv->vval.v_list, str,
9022 (int)(end - str)) == FAIL)
9023 break;
9024 }
9025 if (!match)
9026 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01009027 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009028 if (regmatch.endp[0] > str)
9029 col = 0;
9030 else
Bram Moolenaar13505972019-01-24 15:04:48 +01009031 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009032 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033 str = regmatch.endp[0];
9034 }
9035
9036 vim_regfree(regmatch.regprog);
9037 }
9038
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02009039theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009040 p_cpo = save_cpo;
9041}
9042
9043#ifdef FEAT_FLOAT
9044/*
9045 * "sqrt()" function
9046 */
9047 static void
9048f_sqrt(typval_T *argvars, typval_T *rettv)
9049{
9050 float_T f = 0.0;
9051
9052 rettv->v_type = VAR_FLOAT;
9053 if (get_float_arg(argvars, &f) == OK)
9054 rettv->vval.v_float = sqrt(f);
9055 else
9056 rettv->vval.v_float = 0.0;
9057}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009058#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009059
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009060#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01009061/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009062 * "str2float()" function
9063 */
9064 static void
9065f_str2float(typval_T *argvars, typval_T *rettv)
9066{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009067 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009068 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009069
Bram Moolenaar08243d22017-01-10 16:12:29 +01009070 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009071 p = skipwhite(p + 1);
9072 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01009073 if (isneg)
9074 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009075 rettv->v_type = VAR_FLOAT;
9076}
9077#endif
9078
9079/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02009080 * "str2list()" function
9081 */
9082 static void
9083f_str2list(typval_T *argvars, typval_T *rettv)
9084{
9085 char_u *p;
9086 int utf8 = FALSE;
9087
9088 if (rettv_list_alloc(rettv) == FAIL)
9089 return;
9090
9091 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02009092 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02009093
9094 p = tv_get_string(&argvars[0]);
9095
9096 if (has_mbyte || utf8)
9097 {
9098 int (*ptr2len)(char_u *);
9099 int (*ptr2char)(char_u *);
9100
9101 if (utf8 || enc_utf8)
9102 {
9103 ptr2len = utf_ptr2len;
9104 ptr2char = utf_ptr2char;
9105 }
9106 else
9107 {
9108 ptr2len = mb_ptr2len;
9109 ptr2char = mb_ptr2char;
9110 }
9111
9112 for ( ; *p != NUL; p += (*ptr2len)(p))
9113 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
9114 }
9115 else
9116 for ( ; *p != NUL; ++p)
9117 list_append_number(rettv->vval.v_list, *p);
9118}
9119
9120/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009121 * "str2nr()" function
9122 */
9123 static void
9124f_str2nr(typval_T *argvars, typval_T *rettv)
9125{
9126 int base = 10;
9127 char_u *p;
9128 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009129 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01009130 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009131
9132 if (argvars[1].v_type != VAR_UNKNOWN)
9133 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009134 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009135 if (base != 2 && base != 8 && base != 10 && base != 16)
9136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009137 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009138 return;
9139 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02009140 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009141 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009142 }
9143
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01009144 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009145 isneg = (*p == '-');
9146 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009147 p = skipwhite(p + 1);
9148 switch (base)
9149 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009150 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02009151 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009152 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009153 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02009154 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
9155 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01009156 if (isneg)
9157 rettv->vval.v_number = -n;
9158 else
9159 rettv->vval.v_number = n;
9160
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009161}
9162
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009163/*
9164 * "strgetchar()" function
9165 */
9166 static void
9167f_strgetchar(typval_T *argvars, typval_T *rettv)
9168{
9169 char_u *str;
9170 int len;
9171 int error = FALSE;
9172 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01009173 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009174
9175 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009176 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009177 if (str == NULL)
9178 return;
9179 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009180 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009181 if (error)
9182 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009183
Bram Moolenaar13505972019-01-24 15:04:48 +01009184 while (charidx >= 0 && byteidx < len)
9185 {
9186 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009187 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009188 rettv->vval.v_number = mb_ptr2char(str + byteidx);
9189 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009190 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009191 --charidx;
9192 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009193 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009194}
9195
9196/*
9197 * "stridx()" function
9198 */
9199 static void
9200f_stridx(typval_T *argvars, typval_T *rettv)
9201{
9202 char_u buf[NUMBUFLEN];
9203 char_u *needle;
9204 char_u *haystack;
9205 char_u *save_haystack;
9206 char_u *pos;
9207 int start_idx;
9208
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009209 needle = tv_get_string_chk(&argvars[1]);
9210 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009211 rettv->vval.v_number = -1;
9212 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009213 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009214
9215 if (argvars[2].v_type != VAR_UNKNOWN)
9216 {
9217 int error = FALSE;
9218
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009219 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009220 if (error || start_idx >= (int)STRLEN(haystack))
9221 return;
9222 if (start_idx >= 0)
9223 haystack += start_idx;
9224 }
9225
9226 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9227 if (pos != NULL)
9228 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
9229}
9230
9231/*
9232 * "string()" function
9233 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009234 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009235f_string(typval_T *argvars, typval_T *rettv)
9236{
9237 char_u *tofree;
9238 char_u numbuf[NUMBUFLEN];
9239
9240 rettv->v_type = VAR_STRING;
9241 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
9242 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009243 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009244 if (rettv->vval.v_string != NULL && tofree == NULL)
9245 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
9246}
9247
9248/*
9249 * "strlen()" function
9250 */
9251 static void
9252f_strlen(typval_T *argvars, typval_T *rettv)
9253{
9254 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009255 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009256}
9257
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01009258 static void
9259strchar_common(typval_T *argvars, typval_T *rettv, int skipcc)
9260{
9261 char_u *s = tv_get_string(&argvars[0]);
9262 varnumber_T len = 0;
9263 int (*func_mb_ptr2char_adv)(char_u **pp);
9264
9265 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
9266 while (*s != NUL)
9267 {
9268 func_mb_ptr2char_adv(&s);
9269 ++len;
9270 }
9271 rettv->vval.v_number = len;
9272}
9273
9274/*
9275 * "strcharlen()" function
9276 */
9277 static void
9278f_strcharlen(typval_T *argvars, typval_T *rettv)
9279{
9280 strchar_common(argvars, rettv, TRUE);
9281}
9282
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009283/*
9284 * "strchars()" function
9285 */
9286 static void
9287f_strchars(typval_T *argvars, typval_T *rettv)
9288{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009289 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009290
9291 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009292 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009293 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02009294 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009295 else
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01009296 strchar_common(argvars, rettv, skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009297}
9298
9299/*
9300 * "strdisplaywidth()" function
9301 */
9302 static void
9303f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
9304{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009305 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009306 int col = 0;
9307
9308 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009309 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009310
9311 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
9312}
9313
9314/*
9315 * "strwidth()" function
9316 */
9317 static void
9318f_strwidth(typval_T *argvars, typval_T *rettv)
9319{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009320 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009321
Bram Moolenaar13505972019-01-24 15:04:48 +01009322 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009323}
9324
9325/*
9326 * "strcharpart()" function
9327 */
9328 static void
9329f_strcharpart(typval_T *argvars, typval_T *rettv)
9330{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009331 char_u *p;
9332 int nchar;
9333 int nbyte = 0;
9334 int charlen;
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009335 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009336 int len = 0;
9337 int slen;
9338 int error = FALSE;
9339
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009340 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009341 slen = (int)STRLEN(p);
9342
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009343 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009344 if (!error)
9345 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009346 if (argvars[2].v_type != VAR_UNKNOWN
9347 && argvars[3].v_type != VAR_UNKNOWN)
9348 {
9349 skipcc = tv_get_bool(&argvars[3]);
9350 if (skipcc < 0 || skipcc > 1)
9351 {
9352 semsg(_(e_using_number_as_bool_nr), skipcc);
9353 return;
9354 }
9355 }
9356
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009357 if (nchar > 0)
9358 while (nchar > 0 && nbyte < slen)
9359 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009360 if (skipcc)
9361 nbyte += mb_ptr2len(p + nbyte);
9362 else
9363 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009364 --nchar;
9365 }
9366 else
9367 nbyte = nchar;
9368 if (argvars[2].v_type != VAR_UNKNOWN)
9369 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009370 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009371 while (charlen > 0 && nbyte + len < slen)
9372 {
9373 int off = nbyte + len;
9374
9375 if (off < 0)
9376 len += 1;
9377 else
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009378 {
9379 if (skipcc)
9380 len += mb_ptr2len(p + off);
9381 else
9382 len += MB_CPTR2LEN(p + off);
9383 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009384 --charlen;
9385 }
9386 }
9387 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009388 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009389 }
9390
9391 /*
9392 * Only return the overlap between the specified part and the actual
9393 * string.
9394 */
9395 if (nbyte < 0)
9396 {
9397 len += nbyte;
9398 nbyte = 0;
9399 }
9400 else if (nbyte > slen)
9401 nbyte = slen;
9402 if (len < 0)
9403 len = 0;
9404 else if (nbyte + len > slen)
9405 len = slen - nbyte;
9406
9407 rettv->v_type = VAR_STRING;
9408 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009409}
9410
9411/*
9412 * "strpart()" function
9413 */
9414 static void
9415f_strpart(typval_T *argvars, typval_T *rettv)
9416{
9417 char_u *p;
9418 int n;
9419 int len;
9420 int slen;
9421 int error = FALSE;
9422
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009423 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009424 slen = (int)STRLEN(p);
9425
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009426 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009427 if (error)
9428 len = 0;
9429 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009430 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009431 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009432 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009433
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009434 // Only return the overlap between the specified part and the actual
9435 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009436 if (n < 0)
9437 {
9438 len += n;
9439 n = 0;
9440 }
9441 else if (n > slen)
9442 n = slen;
9443 if (len < 0)
9444 len = 0;
9445 else if (n + len > slen)
9446 len = slen - n;
9447
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009448 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9449 {
9450 int off;
9451
9452 // length in characters
9453 for (off = n; off < slen && len > 0; --len)
9454 off += mb_ptr2len(p + off);
9455 len = off - n;
9456 }
9457
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009458 rettv->v_type = VAR_STRING;
9459 rettv->vval.v_string = vim_strnsave(p + n, len);
9460}
9461
9462/*
9463 * "strridx()" function
9464 */
9465 static void
9466f_strridx(typval_T *argvars, typval_T *rettv)
9467{
9468 char_u buf[NUMBUFLEN];
9469 char_u *needle;
9470 char_u *haystack;
9471 char_u *rest;
9472 char_u *lastmatch = NULL;
9473 int haystack_len, end_idx;
9474
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009475 needle = tv_get_string_chk(&argvars[1]);
9476 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009477
9478 rettv->vval.v_number = -1;
9479 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009480 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009481
9482 haystack_len = (int)STRLEN(haystack);
9483 if (argvars[2].v_type != VAR_UNKNOWN)
9484 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009485 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009486 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009487 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009488 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009489 }
9490 else
9491 end_idx = haystack_len;
9492
9493 if (*needle == NUL)
9494 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009495 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009496 lastmatch = haystack + end_idx;
9497 }
9498 else
9499 {
9500 for (rest = haystack; *rest != '\0'; ++rest)
9501 {
9502 rest = (char_u *)strstr((char *)rest, (char *)needle);
9503 if (rest == NULL || rest > haystack + end_idx)
9504 break;
9505 lastmatch = rest;
9506 }
9507 }
9508
9509 if (lastmatch == NULL)
9510 rettv->vval.v_number = -1;
9511 else
9512 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9513}
9514
9515/*
9516 * "strtrans()" function
9517 */
9518 static void
9519f_strtrans(typval_T *argvars, typval_T *rettv)
9520{
9521 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009522 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009523}
9524
9525/*
9526 * "submatch()" function
9527 */
9528 static void
9529f_submatch(typval_T *argvars, typval_T *rettv)
9530{
9531 int error = FALSE;
9532 int no;
9533 int retList = 0;
9534
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009535 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009536 if (error)
9537 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009538 if (no < 0 || no >= NSUBEXP)
9539 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009540 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009541 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009542 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009543 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009544 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009545 if (error)
9546 return;
9547
9548 if (retList == 0)
9549 {
9550 rettv->v_type = VAR_STRING;
9551 rettv->vval.v_string = reg_submatch(no);
9552 }
9553 else
9554 {
9555 rettv->v_type = VAR_LIST;
9556 rettv->vval.v_list = reg_submatch_list(no);
9557 }
9558}
9559
9560/*
9561 * "substitute()" function
9562 */
9563 static void
9564f_substitute(typval_T *argvars, typval_T *rettv)
9565{
9566 char_u patbuf[NUMBUFLEN];
9567 char_u subbuf[NUMBUFLEN];
9568 char_u flagsbuf[NUMBUFLEN];
9569
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009570 char_u *str = tv_get_string_chk(&argvars[0]);
9571 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009572 char_u *sub = NULL;
9573 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009574 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009575
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009576 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9577 expr = &argvars[2];
9578 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009579 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009580
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009581 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009582 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9583 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009584 rettv->vval.v_string = NULL;
9585 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009586 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009587}
9588
9589/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009590 * "swapinfo(swap_filename)" function
9591 */
9592 static void
9593f_swapinfo(typval_T *argvars, typval_T *rettv)
9594{
9595 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009596 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009597}
9598
9599/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009600 * "swapname(expr)" function
9601 */
9602 static void
9603f_swapname(typval_T *argvars, typval_T *rettv)
9604{
9605 buf_T *buf;
9606
9607 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009608 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009609 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9610 || buf->b_ml.ml_mfp->mf_fname == NULL)
9611 rettv->vval.v_string = NULL;
9612 else
9613 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9614}
9615
9616/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009617 * "synID(lnum, col, trans)" function
9618 */
9619 static void
9620f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9621{
9622 int id = 0;
9623#ifdef FEAT_SYN_HL
9624 linenr_T lnum;
9625 colnr_T col;
9626 int trans;
9627 int transerr = FALSE;
9628
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009629 lnum = tv_get_lnum(argvars); // -1 on type error
9630 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009631 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009632
9633 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9634 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9635 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9636#endif
9637
9638 rettv->vval.v_number = id;
9639}
9640
9641/*
9642 * "synIDattr(id, what [, mode])" function
9643 */
9644 static void
9645f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9646{
9647 char_u *p = NULL;
9648#ifdef FEAT_SYN_HL
9649 int id;
9650 char_u *what;
9651 char_u *mode;
9652 char_u modebuf[NUMBUFLEN];
9653 int modec;
9654
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009655 id = (int)tv_get_number(&argvars[0]);
9656 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009657 if (argvars[2].v_type != VAR_UNKNOWN)
9658 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009659 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009660 modec = TOLOWER_ASC(mode[0]);
9661 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009662 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009663 }
9664 else
9665 {
9666#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9667 if (USE_24BIT)
9668 modec = 'g';
9669 else
9670#endif
9671 if (t_colors > 1)
9672 modec = 'c';
9673 else
9674 modec = 't';
9675 }
9676
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009677 switch (TOLOWER_ASC(what[0]))
9678 {
9679 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009680 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009681 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009682 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009683 p = highlight_has_attr(id, HL_BOLD, modec);
9684 break;
9685
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009686 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009687 p = highlight_color(id, what, modec);
9688 break;
9689
9690 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009691 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009692 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009693 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009694 p = highlight_has_attr(id, HL_ITALIC, modec);
9695 break;
9696
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009697 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009698 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009699 break;
9700
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009701 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009702 p = highlight_has_attr(id, HL_INVERSE, modec);
9703 break;
9704
9705 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009706 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009707 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009708 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009709 else if (TOLOWER_ASC(what[1]) == 't' &&
9710 TOLOWER_ASC(what[2]) == 'r')
9711 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009712 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009713 p = highlight_has_attr(id, HL_STANDOUT, modec);
9714 break;
9715
9716 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009717 if (TOLOWER_ASC(what[1]) == 'l') // ul
9718 p = highlight_color(id, what, modec);
9719 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009720 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009721 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9722 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009723 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009724 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9725 break;
9726 }
9727
9728 if (p != NULL)
9729 p = vim_strsave(p);
9730#endif
9731 rettv->v_type = VAR_STRING;
9732 rettv->vval.v_string = p;
9733}
9734
9735/*
9736 * "synIDtrans(id)" function
9737 */
9738 static void
9739f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9740{
9741 int id;
9742
9743#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009744 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009745
9746 if (id > 0)
9747 id = syn_get_final_id(id);
9748 else
9749#endif
9750 id = 0;
9751
9752 rettv->vval.v_number = id;
9753}
9754
9755/*
9756 * "synconcealed(lnum, col)" function
9757 */
9758 static void
9759f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9760{
9761#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9762 linenr_T lnum;
9763 colnr_T col;
9764 int syntax_flags = 0;
9765 int cchar;
9766 int matchid = 0;
9767 char_u str[NUMBUFLEN];
9768#endif
9769
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009770 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009771
9772#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009773 lnum = tv_get_lnum(argvars); // -1 on type error
9774 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009775
Bram Moolenaara80faa82020-04-12 19:37:17 +02009776 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009777
9778 if (rettv_list_alloc(rettv) != FAIL)
9779 {
9780 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9781 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9782 && curwin->w_p_cole > 0)
9783 {
9784 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9785 syntax_flags = get_syntax_info(&matchid);
9786
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009787 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009788 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9789 {
9790 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009791 if (cchar == NUL && curwin->w_p_cole == 1)
Bram Moolenaareed9d462021-02-15 20:38:25 +01009792 cchar = (curwin->w_lcs_chars.conceal == NUL) ? ' '
9793 : curwin->w_lcs_chars.conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009794 if (cchar != NUL)
9795 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009796 if (has_mbyte)
9797 (*mb_char2bytes)(cchar, str);
9798 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009799 str[0] = cchar;
9800 }
9801 }
9802 }
9803
9804 list_append_number(rettv->vval.v_list,
9805 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009806 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009807 list_append_string(rettv->vval.v_list, str, -1);
9808 list_append_number(rettv->vval.v_list, matchid);
9809 }
9810#endif
9811}
9812
9813/*
9814 * "synstack(lnum, col)" function
9815 */
9816 static void
9817f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9818{
9819#ifdef FEAT_SYN_HL
9820 linenr_T lnum;
9821 colnr_T col;
9822 int i;
9823 int id;
9824#endif
9825
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009826 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009827
9828#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009829 lnum = tv_get_lnum(argvars); // -1 on type error
9830 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009831
9832 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9833 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9834 && rettv_list_alloc(rettv) != FAIL)
9835 {
9836 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9837 for (i = 0; ; ++i)
9838 {
9839 id = syn_get_stack_item(i);
9840 if (id < 0)
9841 break;
9842 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9843 break;
9844 }
9845 }
9846#endif
9847}
9848
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009849/*
9850 * "tabpagebuflist()" function
9851 */
9852 static void
9853f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9854{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009855 tabpage_T *tp;
9856 win_T *wp = NULL;
9857
9858 if (argvars[0].v_type == VAR_UNKNOWN)
9859 wp = firstwin;
9860 else
9861 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009862 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009863 if (tp != NULL)
9864 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9865 }
9866 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9867 {
9868 for (; wp != NULL; wp = wp->w_next)
9869 if (list_append_number(rettv->vval.v_list,
9870 wp->w_buffer->b_fnum) == FAIL)
9871 break;
9872 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009873}
9874
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009875/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009876 * "tagfiles()" function
9877 */
9878 static void
9879f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9880{
9881 char_u *fname;
9882 tagname_T tn;
9883 int first;
9884
9885 if (rettv_list_alloc(rettv) == FAIL)
9886 return;
9887 fname = alloc(MAXPATHL);
9888 if (fname == NULL)
9889 return;
9890
9891 for (first = TRUE; ; first = FALSE)
9892 if (get_tagfname(&tn, first, fname) == FAIL
9893 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9894 break;
9895 tagname_free(&tn);
9896 vim_free(fname);
9897}
9898
9899/*
9900 * "taglist()" function
9901 */
9902 static void
9903f_taglist(typval_T *argvars, typval_T *rettv)
9904{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009905 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009906 char_u *tag_pattern;
9907
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009908 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009909
9910 rettv->vval.v_number = FALSE;
9911 if (*tag_pattern == NUL)
9912 return;
9913
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009914 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009915 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009916 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009917 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009918}
9919
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009920#ifdef FEAT_FLOAT
9921/*
9922 * "tan()" function
9923 */
9924 static void
9925f_tan(typval_T *argvars, typval_T *rettv)
9926{
9927 float_T f = 0.0;
9928
9929 rettv->v_type = VAR_FLOAT;
9930 if (get_float_arg(argvars, &f) == OK)
9931 rettv->vval.v_float = tan(f);
9932 else
9933 rettv->vval.v_float = 0.0;
9934}
9935
9936/*
9937 * "tanh()" function
9938 */
9939 static void
9940f_tanh(typval_T *argvars, typval_T *rettv)
9941{
9942 float_T f = 0.0;
9943
9944 rettv->v_type = VAR_FLOAT;
9945 if (get_float_arg(argvars, &f) == OK)
9946 rettv->vval.v_float = tanh(f);
9947 else
9948 rettv->vval.v_float = 0.0;
9949}
9950#endif
9951
9952/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009953 * "tolower(string)" function
9954 */
9955 static void
9956f_tolower(typval_T *argvars, typval_T *rettv)
9957{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009958 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009959 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009960}
9961
9962/*
9963 * "toupper(string)" function
9964 */
9965 static void
9966f_toupper(typval_T *argvars, typval_T *rettv)
9967{
9968 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009969 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009970}
9971
9972/*
9973 * "tr(string, fromstr, tostr)" function
9974 */
9975 static void
9976f_tr(typval_T *argvars, typval_T *rettv)
9977{
9978 char_u *in_str;
9979 char_u *fromstr;
9980 char_u *tostr;
9981 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009982 int inlen;
9983 int fromlen;
9984 int tolen;
9985 int idx;
9986 char_u *cpstr;
9987 int cplen;
9988 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009989 char_u buf[NUMBUFLEN];
9990 char_u buf2[NUMBUFLEN];
9991 garray_T ga;
9992
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009993 in_str = tv_get_string(&argvars[0]);
9994 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9995 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009996
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009997 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009998 rettv->v_type = VAR_STRING;
9999 rettv->vval.v_string = NULL;
10000 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010001 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010002 ga_init2(&ga, (int)sizeof(char), 80);
10003
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010004 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010005 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010006 if (STRLEN(fromstr) != STRLEN(tostr))
10007 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010008error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010009 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010010 ga_clear(&ga);
10011 return;
10012 }
10013
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010014 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010015 while (*in_str != NUL)
10016 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010017 if (has_mbyte)
10018 {
10019 inlen = (*mb_ptr2len)(in_str);
10020 cpstr = in_str;
10021 cplen = inlen;
10022 idx = 0;
10023 for (p = fromstr; *p != NUL; p += fromlen)
10024 {
10025 fromlen = (*mb_ptr2len)(p);
10026 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
10027 {
10028 for (p = tostr; *p != NUL; p += tolen)
10029 {
10030 tolen = (*mb_ptr2len)(p);
10031 if (idx-- == 0)
10032 {
10033 cplen = tolen;
10034 cpstr = p;
10035 break;
10036 }
10037 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010038 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010039 goto error;
10040 break;
10041 }
10042 ++idx;
10043 }
10044
10045 if (first && cpstr == in_str)
10046 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010047 // Check that fromstr and tostr have the same number of
10048 // (multi-byte) characters. Done only once when a character
10049 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010050 first = FALSE;
10051 for (p = tostr; *p != NUL; p += tolen)
10052 {
10053 tolen = (*mb_ptr2len)(p);
10054 --idx;
10055 }
10056 if (idx != 0)
10057 goto error;
10058 }
10059
10060 (void)ga_grow(&ga, cplen);
10061 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
10062 ga.ga_len += cplen;
10063
10064 in_str += inlen;
10065 }
10066 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010067 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010068 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010069 p = vim_strchr(fromstr, *in_str);
10070 if (p != NULL)
10071 ga_append(&ga, tostr[p - fromstr]);
10072 else
10073 ga_append(&ga, *in_str);
10074 ++in_str;
10075 }
10076 }
10077
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010078 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010079 (void)ga_grow(&ga, 1);
10080 ga_append(&ga, NUL);
10081
10082 rettv->vval.v_string = ga.ga_data;
10083}
10084
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010085/*
10086 * "trim({expr})" function
10087 */
10088 static void
10089f_trim(typval_T *argvars, typval_T *rettv)
10090{
10091 char_u buf1[NUMBUFLEN];
10092 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010093 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010094 char_u *mask = NULL;
10095 char_u *tail;
10096 char_u *prev;
10097 char_u *p;
10098 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010099 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010100
10101 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010102 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010103 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010104 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010105
10106 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010107 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010108 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010109
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010110 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010111 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010112 int error = 0;
10113
10114 // leading or trailing characters to trim
10115 dir = (int)tv_get_number_chk(&argvars[2], &error);
10116 if (error)
10117 return;
10118 if (dir < 0 || dir > 2)
10119 {
10120 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
10121 return;
10122 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010123 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010124 }
10125
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010126 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010127 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010128 // Trim leading characters
10129 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010130 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010131 c1 = PTR2CHAR(head);
10132 if (mask == NULL)
10133 {
10134 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010135 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010136 }
10137 else
10138 {
10139 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10140 if (c1 == PTR2CHAR(p))
10141 break;
10142 if (*p == NUL)
10143 break;
10144 }
10145 MB_PTR_ADV(head);
10146 }
10147 }
10148
10149 tail = head + STRLEN(head);
10150 if (dir == 0 || dir == 2)
10151 {
10152 // Trim trailing characters
10153 for (; tail > head; tail = prev)
10154 {
10155 prev = tail;
10156 MB_PTR_BACK(head, prev);
10157 c1 = PTR2CHAR(prev);
10158 if (mask == NULL)
10159 {
10160 if (c1 > ' ' && c1 != 0xa0)
10161 break;
10162 }
10163 else
10164 {
10165 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10166 if (c1 == PTR2CHAR(p))
10167 break;
10168 if (*p == NUL)
10169 break;
10170 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010171 }
10172 }
Bram Moolenaardf44a272020-06-07 20:49:05 +020010173 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010174}
10175
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010176#ifdef FEAT_FLOAT
10177/*
10178 * "trunc({float})" function
10179 */
10180 static void
10181f_trunc(typval_T *argvars, typval_T *rettv)
10182{
10183 float_T f = 0.0;
10184
10185 rettv->v_type = VAR_FLOAT;
10186 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010187 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010188 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
10189 else
10190 rettv->vval.v_float = 0.0;
10191}
10192#endif
10193
10194/*
10195 * "type(expr)" function
10196 */
10197 static void
10198f_type(typval_T *argvars, typval_T *rettv)
10199{
10200 int n = -1;
10201
10202 switch (argvars[0].v_type)
10203 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010204 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
10205 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010206 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010207 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
10208 case VAR_LIST: n = VAR_TYPE_LIST; break;
10209 case VAR_DICT: n = VAR_TYPE_DICT; break;
10210 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
10211 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
10212 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020010213 case VAR_JOB: n = VAR_TYPE_JOB; break;
10214 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010215 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010216 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +020010217 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010218 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +010010219 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010220 n = -1;
10221 break;
10222 }
10223 rettv->vval.v_number = n;
10224}
10225
10226/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010227 * "virtcol(string)" function
10228 */
10229 static void
10230f_virtcol(typval_T *argvars, typval_T *rettv)
10231{
10232 colnr_T vcol = 0;
10233 pos_T *fp;
10234 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010235 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010236
Bram Moolenaar6f02b002021-01-10 20:22:54 +010010237 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010238 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
10239 && fnum == curbuf->b_fnum)
10240 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010241 // Limit the column to a valid value, getvvcol() doesn't check.
10242 if (fp->col < 0)
10243 fp->col = 0;
10244 else
10245 {
10246 len = (int)STRLEN(ml_get(fp->lnum));
10247 if (fp->col > len)
10248 fp->col = len;
10249 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010250 getvvcol(curwin, fp, NULL, NULL, &vcol);
10251 ++vcol;
10252 }
10253
10254 rettv->vval.v_number = vcol;
10255}
10256
10257/*
10258 * "visualmode()" function
10259 */
10260 static void
10261f_visualmode(typval_T *argvars, typval_T *rettv)
10262{
10263 char_u str[2];
10264
10265 rettv->v_type = VAR_STRING;
10266 str[0] = curbuf->b_visual_mode_eval;
10267 str[1] = NUL;
10268 rettv->vval.v_string = vim_strsave(str);
10269
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010270 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010271 if (non_zero_arg(&argvars[0]))
10272 curbuf->b_visual_mode_eval = NUL;
10273}
10274
10275/*
10276 * "wildmenumode()" function
10277 */
10278 static void
10279f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10280{
10281#ifdef FEAT_WILDMENU
10282 if (wild_menu_showing)
10283 rettv->vval.v_number = 1;
10284#endif
10285}
10286
10287/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +010010288 * "windowsversion()" function
10289 */
10290 static void
10291f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10292{
10293 rettv->v_type = VAR_STRING;
10294 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
10295}
10296
10297/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010298 * "wordcount()" function
10299 */
10300 static void
10301f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
10302{
10303 if (rettv_dict_alloc(rettv) == FAIL)
10304 return;
10305 cursor_pos_info(rettv->vval.v_dict);
10306}
10307
10308/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010309 * "xor(expr, expr)" function
10310 */
10311 static void
10312f_xor(typval_T *argvars, typval_T *rettv)
10313{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010314 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
10315 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010316}
10317
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010318#endif // FEAT_EVAL