blob: c9acd4136beab39390318d31c70088d4e28fb9fb [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020023#ifdef FEAT_FLOAT
24static void f_abs(typval_T *argvars, typval_T *rettv);
25static void f_acos(typval_T *argvars, typval_T *rettv);
26#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020027static void f_and(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028#ifdef FEAT_FLOAT
29static void f_asin(typval_T *argvars, typval_T *rettv);
30static void f_atan(typval_T *argvars, typval_T *rettv);
31static void f_atan2(typval_T *argvars, typval_T *rettv);
32#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010033#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020034static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010035static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010036# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010037static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010038# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010039#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_byte2line(typval_T *argvars, typval_T *rettv);
41static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
42static void f_byteidx(typval_T *argvars, typval_T *rettv);
43static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
44static void f_call(typval_T *argvars, typval_T *rettv);
45#ifdef FEAT_FLOAT
46static void f_ceil(typval_T *argvars, typval_T *rettv);
47#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_changenr(typval_T *argvars, typval_T *rettv);
49static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010050static void f_charcol(typval_T *argvars, typval_T *rettv);
Bram Moolenaar17793ef2020-12-28 12:56:58 +010051static void f_charidx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020053static void f_confirm(typval_T *argvars, typval_T *rettv);
54static void f_copy(typval_T *argvars, typval_T *rettv);
55#ifdef FEAT_FLOAT
56static void f_cos(typval_T *argvars, typval_T *rettv);
57static void f_cosh(typval_T *argvars, typval_T *rettv);
58#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010060#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020061static void f_debugbreak(typval_T *argvars, typval_T *rettv);
62#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020063static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020064static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010065static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020066static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020067static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_escape(typval_T *argvars, typval_T *rettv);
69static void f_eval(typval_T *argvars, typval_T *rettv);
70static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_exists(typval_T *argvars, typval_T *rettv);
73#ifdef FEAT_FLOAT
74static void f_exp(typval_T *argvars, typval_T *rettv);
75#endif
76static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020077static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020078static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020079#ifdef FEAT_FLOAT
80static void f_float2nr(typval_T *argvars, typval_T *rettv);
81static void f_floor(typval_T *argvars, typval_T *rettv);
82static void f_fmod(typval_T *argvars, typval_T *rettv);
83#endif
84static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020085static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020086static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_function(typval_T *argvars, typval_T *rettv);
88static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
89static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010090static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010091static void f_getcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020092static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020093static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020094static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010095static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_getpid(typval_T *argvars, typval_T *rettv);
97static void f_getcurpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010098static void f_getcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020099static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200100static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200101static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100103static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +0200104static void f_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200105static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
106static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200107static void f_hlID(typval_T *argvars, typval_T *rettv);
108static void f_hlexists(typval_T *argvars, typval_T *rettv);
109static void f_hostname(typval_T *argvars, typval_T *rettv);
110static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200111static void f_index(typval_T *argvars, typval_T *rettv);
112static void f_input(typval_T *argvars, typval_T *rettv);
113static void f_inputdialog(typval_T *argvars, typval_T *rettv);
114static void f_inputlist(typval_T *argvars, typval_T *rettv);
115static void f_inputrestore(typval_T *argvars, typval_T *rettv);
116static void f_inputsave(typval_T *argvars, typval_T *rettv);
117static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +0100118static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200119static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120static void f_islocked(typval_T *argvars, typval_T *rettv);
121#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200122static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200123static void f_isnan(typval_T *argvars, typval_T *rettv);
124#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200125static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
126static void f_len(typval_T *argvars, typval_T *rettv);
127static void f_libcall(typval_T *argvars, typval_T *rettv);
128static void f_libcallnr(typval_T *argvars, typval_T *rettv);
129static void f_line(typval_T *argvars, typval_T *rettv);
130static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200131#ifdef FEAT_FLOAT
132static void f_log(typval_T *argvars, typval_T *rettv);
133static void f_log10(typval_T *argvars, typval_T *rettv);
134#endif
135#ifdef FEAT_LUA
136static void f_luaeval(typval_T *argvars, typval_T *rettv);
137#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200138static void f_maparg(typval_T *argvars, typval_T *rettv);
139static void f_mapcheck(typval_T *argvars, typval_T *rettv);
140static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200141static void f_matchend(typval_T *argvars, typval_T *rettv);
142static void f_matchlist(typval_T *argvars, typval_T *rettv);
143static void f_matchstr(typval_T *argvars, typval_T *rettv);
144static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
145static void f_max(typval_T *argvars, typval_T *rettv);
146static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200147#ifdef FEAT_MZSCHEME
148static void f_mzeval(typval_T *argvars, typval_T *rettv);
149#endif
150static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
151static void f_nr2char(typval_T *argvars, typval_T *rettv);
152static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200153#ifdef FEAT_PERL
154static void f_perleval(typval_T *argvars, typval_T *rettv);
155#endif
156#ifdef FEAT_FLOAT
157static void f_pow(typval_T *argvars, typval_T *rettv);
158#endif
159static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
160static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200161static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200162static void f_pumvisible(typval_T *argvars, typval_T *rettv);
163#ifdef FEAT_PYTHON3
164static void f_py3eval(typval_T *argvars, typval_T *rettv);
165#endif
166#ifdef FEAT_PYTHON
167static void f_pyeval(typval_T *argvars, typval_T *rettv);
168#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100169#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
170static void f_pyxeval(typval_T *argvars, typval_T *rettv);
171#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100172static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100173static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200175static void f_reg_executing(typval_T *argvars, typval_T *rettv);
176static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_rename(typval_T *argvars, typval_T *rettv);
178static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200179#ifdef FEAT_FLOAT
180static void f_round(typval_T *argvars, typval_T *rettv);
181#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100182#ifdef FEAT_RUBY
183static void f_rubyeval(typval_T *argvars, typval_T *rettv);
184#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200185static void f_screenattr(typval_T *argvars, typval_T *rettv);
186static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100187static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200188static void f_screencol(typval_T *argvars, typval_T *rettv);
189static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100190static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200191static void f_search(typval_T *argvars, typval_T *rettv);
192static void f_searchdecl(typval_T *argvars, typval_T *rettv);
193static void f_searchpair(typval_T *argvars, typval_T *rettv);
194static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
195static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100196static void f_setcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100198static void f_setcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200199static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100203static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200204#ifdef FEAT_CRYPT
205static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200206#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200207static void f_shellescape(typval_T *argvars, typval_T *rettv);
208static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200209#ifdef FEAT_FLOAT
210static void f_sin(typval_T *argvars, typval_T *rettv);
211static void f_sinh(typval_T *argvars, typval_T *rettv);
212#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200213static void f_soundfold(typval_T *argvars, typval_T *rettv);
214static void f_spellbadword(typval_T *argvars, typval_T *rettv);
215static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
216static void f_split(typval_T *argvars, typval_T *rettv);
217#ifdef FEAT_FLOAT
218static void f_sqrt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100219#endif
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100220static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100221#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200222static void f_str2float(typval_T *argvars, typval_T *rettv);
223#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200224static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200225static void f_str2nr(typval_T *argvars, typval_T *rettv);
226static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200227static void f_strgetchar(typval_T *argvars, typval_T *rettv);
228static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200229static void f_strlen(typval_T *argvars, typval_T *rettv);
230static void f_strcharpart(typval_T *argvars, typval_T *rettv);
231static void f_strpart(typval_T *argvars, typval_T *rettv);
232static void f_strridx(typval_T *argvars, typval_T *rettv);
233static void f_strtrans(typval_T *argvars, typval_T *rettv);
234static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
235static void f_strwidth(typval_T *argvars, typval_T *rettv);
236static void f_submatch(typval_T *argvars, typval_T *rettv);
237static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200238static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200239static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200240static void f_synID(typval_T *argvars, typval_T *rettv);
241static void f_synIDattr(typval_T *argvars, typval_T *rettv);
242static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
243static void f_synstack(typval_T *argvars, typval_T *rettv);
244static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200246static void f_taglist(typval_T *argvars, typval_T *rettv);
247static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200248#ifdef FEAT_FLOAT
249static void f_tan(typval_T *argvars, typval_T *rettv);
250static void f_tanh(typval_T *argvars, typval_T *rettv);
251#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200252static void f_tolower(typval_T *argvars, typval_T *rettv);
253static void f_toupper(typval_T *argvars, typval_T *rettv);
254static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100255static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200256#ifdef FEAT_FLOAT
257static void f_trunc(typval_T *argvars, typval_T *rettv);
258#endif
259static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200260static void f_virtcol(typval_T *argvars, typval_T *rettv);
261static void f_visualmode(typval_T *argvars, typval_T *rettv);
262static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100263static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200264static void f_wordcount(typval_T *argvars, typval_T *rettv);
265static void f_xor(typval_T *argvars, typval_T *rettv);
266
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100267
Bram Moolenaar94738d82020-10-21 14:25:07 +0200268/*
269 * Functions that check the argument type of a builtin function.
270 * Each function returns FAIL and gives an error message if the type is wrong.
271 */
272
273// Context passed to an arg_ function.
274typedef struct {
Bram Moolenaarca174532020-10-21 16:42:22 +0200275 int arg_count; // actual argument count
276 type_T **arg_types; // list of argument types
277 int arg_idx; // current argument index (first arg is zero)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100278 cctx_T *arg_cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +0200279} argcontext_T;
280
281// A function to check one argument type. The first argument is the type to
282// check. If needed, other argument types can be obtained with the context.
283// E.g. if "arg_idx" is 1, then (type - 1) is the first argument type.
284typedef int (*argcheck_T)(type_T *, argcontext_T *);
285
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100286/*
Bram Moolenaar351ead02021-01-16 16:07:01 +0100287 * Call need_type() to check an argument type.
288 */
289 static int
290check_arg_type(
291 type_T *expected,
292 type_T *actual,
293 argcontext_T *context)
294{
295 // TODO: would be useful to know if "actual" is a constant and pass it to
296 // need_type() to get a compile time error if possible.
297 return need_type(actual, expected,
298 context->arg_idx - context->arg_count, context->arg_idx + 1,
299 context->arg_cctx, FALSE, FALSE);
300}
301
302/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100303 * Check "type" is a float or a number.
304 */
Bram Moolenaar94738d82020-10-21 14:25:07 +0200305 static int
306arg_float_or_nr(type_T *type, argcontext_T *context)
307{
Bram Moolenaarca174532020-10-21 16:42:22 +0200308 if (type->tt_type == VAR_ANY
309 || type->tt_type == VAR_FLOAT || type->tt_type == VAR_NUMBER)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200310 return OK;
311 arg_type_mismatch(&t_number, type, context->arg_idx + 1);
312 return FAIL;
313}
314
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100315/*
316 * Check "type" is a number.
317 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200318 static int
319arg_number(type_T *type, argcontext_T *context)
320{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100321 return check_arg_type(&t_number, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200322}
323
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100324/*
325 * Check "type" is a string.
326 */
327 static int
328arg_string(type_T *type, argcontext_T *context)
329{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100330 return check_arg_type(&t_string, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100331}
332
333/*
334 * Check "type" is a list or a blob.
335 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200336 static int
337arg_list_or_blob(type_T *type, argcontext_T *context)
338{
339 if (type->tt_type == VAR_ANY
340 || type->tt_type == VAR_LIST || type->tt_type == VAR_BLOB)
341 return OK;
342 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
343 return FAIL;
344}
345
346/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100347 * Check "type" is a list or a dict.
348 */
349 static int
350arg_list_or_dict(type_T *type, argcontext_T *context)
351{
352 if (type->tt_type == VAR_ANY
353 || type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
354 return OK;
355 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
356 return FAIL;
357}
358
359/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100360 * Check "type" is the same type as the previous argument.
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100361 * Must not be used for the first argcheck_T entry.
362 */
363 static int
364arg_same_as_prev(type_T *type, argcontext_T *context)
365{
366 type_T *prev_type = context->arg_types[context->arg_idx - 1];
367
Bram Moolenaar351ead02021-01-16 16:07:01 +0100368 return check_arg_type(prev_type, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100369}
370
371/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100372 * Check "type" is the same basic type as the previous argument, checks list or
373 * dict vs other type, but not member type.
374 * Must not be used for the first argcheck_T entry.
375 */
376 static int
377arg_same_struct_as_prev(type_T *type, argcontext_T *context)
378{
379 type_T *prev_type = context->arg_types[context->arg_idx - 1];
380
381 if (prev_type->tt_type != context->arg_types[context->arg_idx]->tt_type)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100382 return check_arg_type(prev_type, type, context);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100383 return OK;
384}
385
386/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100387 * Check "type" is an item of the list or blob of the previous arg.
Bram Moolenaarca174532020-10-21 16:42:22 +0200388 * Must not be used for the first argcheck_T entry.
389 */
390 static int
391arg_item_of_prev(type_T *type, argcontext_T *context)
392{
393 type_T *prev_type = context->arg_types[context->arg_idx - 1];
394 type_T *expected;
395
396 if (prev_type->tt_type == VAR_LIST)
397 expected = prev_type->tt_member;
398 else if (prev_type->tt_type == VAR_BLOB)
399 expected = &t_number;
400 else
401 // probably VAR_ANY, can't check
402 return OK;
403
Bram Moolenaar351ead02021-01-16 16:07:01 +0100404 return check_arg_type(expected, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200405}
406
Bram Moolenaar94738d82020-10-21 14:25:07 +0200407/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100408 * Check "type" which is the third argument of extend().
409 */
410 static int
411arg_extend3(type_T *type, argcontext_T *context)
412{
413 type_T *first_type = context->arg_types[context->arg_idx - 2];
414
415 if (first_type->tt_type == VAR_LIST)
416 return arg_number(type, context);
417 if (first_type->tt_type == VAR_DICT)
418 return arg_string(type, context);
419 return OK;
420}
421
422
423/*
Bram Moolenaar94738d82020-10-21 14:25:07 +0200424 * Lists of functions that check the argument types of a builtin function.
425 */
426argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100427argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev};
428argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3};
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100429argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
Bram Moolenaarca174532020-10-21 16:42:22 +0200430argcheck_T arg3_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200431
432/*
433 * Functions that return the return type of a builtin function.
Bram Moolenaara1224cb2020-10-22 12:31:49 +0200434 * Note that "argtypes" is NULL if "argcount" is zero.
Bram Moolenaar94738d82020-10-21 14:25:07 +0200435 */
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100436 static type_T *
437ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
438{
439 return &t_void;
440}
441 static type_T *
442ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
443{
444 return &t_any;
445}
446 static type_T *
Bram Moolenaar403dc312020-10-17 19:29:51 +0200447ret_bool(int argcount UNUSED, type_T **argtypes UNUSED)
448{
449 return &t_bool;
450}
451 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100452ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
453{
454 return &t_number;
455}
456 static type_T *
457ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
458{
459 return &t_float;
460}
461 static type_T *
462ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
463{
464 return &t_string;
465}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200466 static type_T *
467ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100468{
469 return &t_list_any;
470}
471 static type_T *
472ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
473{
474 return &t_list_number;
475}
476 static type_T *
477ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
478{
479 return &t_list_string;
480}
481 static type_T *
482ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
483{
484 return &t_list_dict_any;
485}
486 static type_T *
487ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
488{
489 return &t_dict_any;
490}
491 static type_T *
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100492ret_job_info(int argcount, type_T **argtypes UNUSED)
493{
494 if (argcount == 0)
495 return &t_list_job;
496 return &t_dict_any;
497}
498 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100499ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
500{
501 return &t_dict_number;
502}
503 static type_T *
504ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
505{
506 return &t_dict_string;
507}
508 static type_T *
509ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
510{
511 return &t_blob;
512}
513 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200514ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100515{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200516 return &t_func_any;
517}
518 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100519ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
520{
521 return &t_channel;
522}
523 static type_T *
524ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
525{
526 return &t_job;
527}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200528 static type_T *
529ret_first_arg(int argcount, type_T **argtypes)
530{
531 if (argcount > 0)
532 return argtypes[0];
533 return &t_void;
534}
Bram Moolenaarea696852020-11-09 18:31:39 +0100535// for map(): returns first argument but item type may differ
536 static type_T *
537ret_first_cont(int argcount UNUSED, type_T **argtypes)
538{
539 if (argtypes[0]->tt_type == VAR_LIST)
540 return &t_list_any;
541 if (argtypes[0]->tt_type == VAR_DICT)
542 return &t_dict_any;
543 if (argtypes[0]->tt_type == VAR_BLOB)
544 return argtypes[0];
545 return &t_any;
546}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200547
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200548/*
549 * Used for getqflist(): returns list if there is no argument, dict if there is
550 * one.
551 */
552 static type_T *
553ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
554{
555 if (argcount > 0)
556 return &t_dict_any;
557 return &t_list_dict_any;
558}
559
560/*
561 * Used for getloclist(): returns list if there is one argument, dict if there
562 * are two.
563 */
564 static type_T *
565ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
566{
567 if (argcount > 1)
568 return &t_dict_any;
569 return &t_list_dict_any;
570}
571
Bram Moolenaar846178a2020-07-05 17:04:13 +0200572 static type_T *
573ret_argv(int argcount, type_T **argtypes UNUSED)
574{
575 // argv() returns list of strings
576 if (argcount == 0)
577 return &t_list_string;
578
579 // argv(0) returns a string, but argv(-1] returns a list
580 return &t_any;
581}
582
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200583 static type_T *
584ret_remove(int argcount UNUSED, type_T **argtypes)
585{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200586 if (argtypes != NULL)
587 {
588 if (argtypes[0]->tt_type == VAR_LIST
589 || argtypes[0]->tt_type == VAR_DICT)
590 return argtypes[0]->tt_member;
591 if (argtypes[0]->tt_type == VAR_BLOB)
592 return &t_number;
593 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200594 return &t_any;
595}
596
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200597 static type_T *
598ret_getreg(int argcount, type_T **argtypes UNUSED)
599{
600 // Assume that if the third argument is passed it's non-zero
601 if (argcount == 3)
602 return &t_list_string;
603 return &t_string;
604}
605
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200606 static type_T *
607ret_maparg(int argcount, type_T **argtypes UNUSED)
608{
609 // Assume that if the fourth argument is passed it's non-zero
610 if (argcount == 4)
611 return &t_dict_any;
612 return &t_string;
613}
614
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100615static type_T *ret_f_function(int argcount, type_T **argtypes);
616
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200617/*
618 * Array with names and number of arguments of all internal functions
619 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
620 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200621typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200622{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200623 char *f_name; // function name
624 char f_min_argc; // minimal number of arguments
625 char f_max_argc; // maximal number of arguments
626 char f_argtype; // for method: FEARG_ values
Bram Moolenaar94738d82020-10-21 14:25:07 +0200627 argcheck_T *f_argcheck; // list of functions to check argument types
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100628 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
629 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200630 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200631 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200632} funcentry_T;
633
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200634// values for f_argtype; zero means it cannot be used as a method
635#define FEARG_1 1 // base is the first argument
636#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200637#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200638#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200639#define FEARG_LAST 9 // base is the last argument
640
Bram Moolenaar15c47602020-03-26 22:16:48 +0100641#ifdef FEAT_FLOAT
642# define FLOAT_FUNC(name) name
643#else
644# define FLOAT_FUNC(name) NULL
645#endif
646#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
647# define MATH_FUNC(name) name
648#else
649# define MATH_FUNC(name) NULL
650#endif
651#ifdef FEAT_TIMERS
652# define TIMER_FUNC(name) name
653#else
654# define TIMER_FUNC(name) NULL
655#endif
656#ifdef FEAT_JOB_CHANNEL
657# define JOB_FUNC(name) name
658#else
659# define JOB_FUNC(name) NULL
660#endif
661#ifdef FEAT_PROP_POPUP
662# define PROP_FUNC(name) name
663#else
664# define PROP_FUNC(name) NULL
665#endif
666#ifdef FEAT_SIGNS
667# define SIGN_FUNC(name) name
668#else
669# define SIGN_FUNC(name) NULL
670#endif
671#ifdef FEAT_SOUND
672# define SOUND_FUNC(name) name
673#else
674# define SOUND_FUNC(name) NULL
675#endif
676#ifdef FEAT_TERMINAL
677# define TERM_FUNC(name) name
678#else
679# define TERM_FUNC(name) NULL
680#endif
681
Bram Moolenaarac92e252019-08-03 21:58:38 +0200682static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200683{
Bram Moolenaar94738d82020-10-21 14:25:07 +0200684 {"abs", 1, 1, FEARG_1, arg1_float_or_nr,
685 ret_any, FLOAT_FUNC(f_abs)},
686 {"acos", 1, 1, FEARG_1, NULL,
687 ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100688 {"add", 2, 2, FEARG_1, NULL /* arg2_listblob_item */,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200689 ret_first_arg, f_add},
690 {"and", 2, 2, FEARG_1, NULL,
691 ret_number, f_and},
692 {"append", 2, 2, FEARG_2, NULL,
693 ret_number, f_append},
694 {"appendbufline", 3, 3, FEARG_3, NULL,
695 ret_number, f_appendbufline},
696 {"argc", 0, 1, 0, NULL,
697 ret_number, f_argc},
698 {"argidx", 0, 0, 0, NULL,
699 ret_number, f_argidx},
700 {"arglistid", 0, 2, 0, NULL,
701 ret_number, f_arglistid},
702 {"argv", 0, 2, 0, NULL,
703 ret_argv, f_argv},
704 {"asin", 1, 1, FEARG_1, NULL,
705 ret_float, FLOAT_FUNC(f_asin)},
706 {"assert_beeps", 1, 2, FEARG_1, NULL,
707 ret_number, f_assert_beeps},
708 {"assert_equal", 2, 3, FEARG_2, NULL,
709 ret_number, f_assert_equal},
710 {"assert_equalfile", 2, 3, FEARG_1, NULL,
711 ret_number, f_assert_equalfile},
712 {"assert_exception", 1, 2, 0, NULL,
713 ret_number, f_assert_exception},
714 {"assert_fails", 1, 5, FEARG_1, NULL,
715 ret_number, f_assert_fails},
716 {"assert_false", 1, 2, FEARG_1, NULL,
717 ret_number, f_assert_false},
718 {"assert_inrange", 3, 4, FEARG_3, NULL,
719 ret_number, f_assert_inrange},
720 {"assert_match", 2, 3, FEARG_2, NULL,
721 ret_number, f_assert_match},
722 {"assert_notequal", 2, 3, FEARG_2, NULL,
723 ret_number, f_assert_notequal},
724 {"assert_notmatch", 2, 3, FEARG_2, NULL,
725 ret_number, f_assert_notmatch},
726 {"assert_report", 1, 1, FEARG_1, NULL,
727 ret_number, f_assert_report},
728 {"assert_true", 1, 2, FEARG_1, NULL,
729 ret_number, f_assert_true},
730 {"atan", 1, 1, FEARG_1, NULL,
731 ret_float, FLOAT_FUNC(f_atan)},
732 {"atan2", 2, 2, FEARG_1, NULL,
733 ret_float, FLOAT_FUNC(f_atan2)},
734 {"balloon_gettext", 0, 0, 0, NULL,
735 ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100736#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100737 f_balloon_gettext
738#else
739 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100740#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100741 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200742 {"balloon_show", 1, 1, FEARG_1, NULL,
743 ret_void,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100744#ifdef FEAT_BEVAL
745 f_balloon_show
746#else
747 NULL
748#endif
749 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200750 {"balloon_split", 1, 1, FEARG_1, NULL,
751 ret_list_string,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100752#if defined(FEAT_BEVAL_TERM)
753 f_balloon_split
754#else
755 NULL
756#endif
757 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200758 {"browse", 4, 4, 0, NULL,
759 ret_string, f_browse},
760 {"browsedir", 2, 2, 0, NULL,
761 ret_string, f_browsedir},
762 {"bufadd", 1, 1, FEARG_1, NULL,
763 ret_number, f_bufadd},
764 {"bufexists", 1, 1, FEARG_1, NULL,
765 ret_number, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200766 {"buffer_exists", 1, 1, FEARG_1, NULL, // obsolete
767 ret_number, f_bufexists},
768 {"buffer_name", 0, 1, FEARG_1, NULL, // obsolete
769 ret_string, f_bufname},
770 {"buffer_number", 0, 1, FEARG_1, NULL, // obsolete
771 ret_number, f_bufnr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200772 {"buflisted", 1, 1, FEARG_1, NULL,
773 ret_number, f_buflisted},
774 {"bufload", 1, 1, FEARG_1, NULL,
775 ret_void, f_bufload},
776 {"bufloaded", 1, 1, FEARG_1, NULL,
777 ret_number, f_bufloaded},
778 {"bufname", 0, 1, FEARG_1, NULL,
779 ret_string, f_bufname},
780 {"bufnr", 0, 2, FEARG_1, NULL,
781 ret_number, f_bufnr},
782 {"bufwinid", 1, 1, FEARG_1, NULL,
783 ret_number, f_bufwinid},
784 {"bufwinnr", 1, 1, FEARG_1, NULL,
785 ret_number, f_bufwinnr},
786 {"byte2line", 1, 1, FEARG_1, NULL,
787 ret_number, f_byte2line},
788 {"byteidx", 2, 2, FEARG_1, NULL,
789 ret_number, f_byteidx},
790 {"byteidxcomp", 2, 2, FEARG_1, NULL,
791 ret_number, f_byteidxcomp},
792 {"call", 2, 3, FEARG_1, NULL,
793 ret_any, f_call},
794 {"ceil", 1, 1, FEARG_1, NULL,
795 ret_float, FLOAT_FUNC(f_ceil)},
796 {"ch_canread", 1, 1, FEARG_1, NULL,
797 ret_number, JOB_FUNC(f_ch_canread)},
798 {"ch_close", 1, 1, FEARG_1, NULL,
799 ret_void, JOB_FUNC(f_ch_close)},
800 {"ch_close_in", 1, 1, FEARG_1, NULL,
801 ret_void, JOB_FUNC(f_ch_close_in)},
802 {"ch_evalexpr", 2, 3, FEARG_1, NULL,
803 ret_any, JOB_FUNC(f_ch_evalexpr)},
804 {"ch_evalraw", 2, 3, FEARG_1, NULL,
805 ret_any, JOB_FUNC(f_ch_evalraw)},
806 {"ch_getbufnr", 2, 2, FEARG_1, NULL,
807 ret_number, JOB_FUNC(f_ch_getbufnr)},
808 {"ch_getjob", 1, 1, FEARG_1, NULL,
809 ret_job, JOB_FUNC(f_ch_getjob)},
810 {"ch_info", 1, 1, FEARG_1, NULL,
811 ret_dict_any, JOB_FUNC(f_ch_info)},
812 {"ch_log", 1, 2, FEARG_1, NULL,
813 ret_void, JOB_FUNC(f_ch_log)},
814 {"ch_logfile", 1, 2, FEARG_1, NULL,
815 ret_void, JOB_FUNC(f_ch_logfile)},
816 {"ch_open", 1, 2, FEARG_1, NULL,
817 ret_channel, JOB_FUNC(f_ch_open)},
818 {"ch_read", 1, 2, FEARG_1, NULL,
819 ret_string, JOB_FUNC(f_ch_read)},
820 {"ch_readblob", 1, 2, FEARG_1, NULL,
821 ret_blob, JOB_FUNC(f_ch_readblob)},
822 {"ch_readraw", 1, 2, FEARG_1, NULL,
823 ret_string, JOB_FUNC(f_ch_readraw)},
824 {"ch_sendexpr", 2, 3, FEARG_1, NULL,
825 ret_void, JOB_FUNC(f_ch_sendexpr)},
826 {"ch_sendraw", 2, 3, FEARG_1, NULL,
827 ret_void, JOB_FUNC(f_ch_sendraw)},
828 {"ch_setoptions", 2, 2, FEARG_1, NULL,
829 ret_void, JOB_FUNC(f_ch_setoptions)},
830 {"ch_status", 1, 2, FEARG_1, NULL,
831 ret_string, JOB_FUNC(f_ch_status)},
832 {"changenr", 0, 0, 0, NULL,
833 ret_number, f_changenr},
834 {"char2nr", 1, 2, FEARG_1, NULL,
835 ret_number, f_char2nr},
836 {"charclass", 1, 1, FEARG_1, NULL,
837 ret_number, f_charclass},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100838 {"charcol", 1, 1, FEARG_1, NULL,
839 ret_number, f_charcol},
Bram Moolenaar17793ef2020-12-28 12:56:58 +0100840 {"charidx", 2, 3, FEARG_1, NULL,
841 ret_number, f_charidx},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200842 {"chdir", 1, 1, FEARG_1, NULL,
843 ret_string, f_chdir},
844 {"cindent", 1, 1, FEARG_1, NULL,
845 ret_number, f_cindent},
846 {"clearmatches", 0, 1, FEARG_1, NULL,
847 ret_void, f_clearmatches},
848 {"col", 1, 1, FEARG_1, NULL,
849 ret_number, f_col},
850 {"complete", 2, 2, FEARG_2, NULL,
851 ret_void, f_complete},
852 {"complete_add", 1, 1, FEARG_1, NULL,
853 ret_number, f_complete_add},
854 {"complete_check", 0, 0, 0, NULL,
855 ret_number, f_complete_check},
856 {"complete_info", 0, 1, FEARG_1, NULL,
857 ret_dict_any, f_complete_info},
858 {"confirm", 1, 4, FEARG_1, NULL,
859 ret_number, f_confirm},
860 {"copy", 1, 1, FEARG_1, NULL,
861 ret_first_arg, f_copy},
862 {"cos", 1, 1, FEARG_1, NULL,
863 ret_float, FLOAT_FUNC(f_cos)},
864 {"cosh", 1, 1, FEARG_1, NULL,
865 ret_float, FLOAT_FUNC(f_cosh)},
866 {"count", 2, 4, FEARG_1, NULL,
867 ret_number, f_count},
868 {"cscope_connection",0,3, 0, NULL,
869 ret_number, f_cscope_connection},
870 {"cursor", 1, 3, FEARG_1, NULL,
871 ret_number, f_cursor},
872 {"debugbreak", 1, 1, FEARG_1, NULL,
873 ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100874#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100875 f_debugbreak
876#else
877 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200878#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100879 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200880 {"deepcopy", 1, 2, FEARG_1, NULL,
881 ret_first_arg, f_deepcopy},
882 {"delete", 1, 2, FEARG_1, NULL,
883 ret_number, f_delete},
884 {"deletebufline", 2, 3, FEARG_1, NULL,
885 ret_number, f_deletebufline},
886 {"did_filetype", 0, 0, 0, NULL,
887 ret_number, f_did_filetype},
888 {"diff_filler", 1, 1, FEARG_1, NULL,
889 ret_number, f_diff_filler},
890 {"diff_hlID", 2, 2, FEARG_1, NULL,
891 ret_number, f_diff_hlID},
892 {"echoraw", 1, 1, FEARG_1, NULL,
893 ret_number, f_echoraw},
894 {"empty", 1, 1, FEARG_1, NULL,
895 ret_number, f_empty},
896 {"environ", 0, 0, 0, NULL,
897 ret_dict_string, f_environ},
898 {"escape", 2, 2, FEARG_1, NULL,
899 ret_string, f_escape},
900 {"eval", 1, 1, FEARG_1, NULL,
901 ret_any, f_eval},
902 {"eventhandler", 0, 0, 0, NULL,
903 ret_number, f_eventhandler},
904 {"executable", 1, 1, FEARG_1, NULL,
905 ret_number, f_executable},
906 {"execute", 1, 2, FEARG_1, NULL,
907 ret_string, f_execute},
908 {"exepath", 1, 1, FEARG_1, NULL,
909 ret_string, f_exepath},
910 {"exists", 1, 1, FEARG_1, NULL,
911 ret_number, f_exists},
912 {"exp", 1, 1, FEARG_1, NULL,
913 ret_float, FLOAT_FUNC(f_exp)},
914 {"expand", 1, 3, FEARG_1, NULL,
915 ret_any, f_expand},
916 {"expandcmd", 1, 1, FEARG_1, NULL,
917 ret_string, f_expandcmd},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100918 {"extend", 2, 3, FEARG_1, arg23_extend,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200919 ret_first_arg, f_extend},
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100920 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
921 ret_first_cont, f_extendnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200922 {"feedkeys", 1, 2, FEARG_1, NULL,
923 ret_void, f_feedkeys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200924 {"file_readable", 1, 1, FEARG_1, NULL, // obsolete
925 ret_number, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200926 {"filereadable", 1, 1, FEARG_1, NULL,
927 ret_number, f_filereadable},
928 {"filewritable", 1, 1, FEARG_1, NULL,
929 ret_number, f_filewritable},
930 {"filter", 2, 2, FEARG_1, NULL,
931 ret_first_arg, f_filter},
932 {"finddir", 1, 3, FEARG_1, NULL,
933 ret_string, f_finddir},
934 {"findfile", 1, 3, FEARG_1, NULL,
935 ret_string, f_findfile},
936 {"flatten", 1, 2, FEARG_1, NULL,
937 ret_list_any, f_flatten},
938 {"float2nr", 1, 1, FEARG_1, NULL,
939 ret_number, FLOAT_FUNC(f_float2nr)},
940 {"floor", 1, 1, FEARG_1, NULL,
941 ret_float, FLOAT_FUNC(f_floor)},
942 {"fmod", 2, 2, FEARG_1, NULL,
943 ret_float, FLOAT_FUNC(f_fmod)},
944 {"fnameescape", 1, 1, FEARG_1, NULL,
945 ret_string, f_fnameescape},
946 {"fnamemodify", 2, 2, FEARG_1, NULL,
947 ret_string, f_fnamemodify},
948 {"foldclosed", 1, 1, FEARG_1, NULL,
949 ret_number, f_foldclosed},
950 {"foldclosedend", 1, 1, FEARG_1, NULL,
951 ret_number, f_foldclosedend},
952 {"foldlevel", 1, 1, FEARG_1, NULL,
953 ret_number, f_foldlevel},
954 {"foldtext", 0, 0, 0, NULL,
955 ret_string, f_foldtext},
956 {"foldtextresult", 1, 1, FEARG_1, NULL,
957 ret_string, f_foldtextresult},
958 {"foreground", 0, 0, 0, NULL,
959 ret_void, f_foreground},
960 {"funcref", 1, 3, FEARG_1, NULL,
961 ret_func_any, f_funcref},
962 {"function", 1, 3, FEARG_1, NULL,
963 ret_f_function, f_function},
964 {"garbagecollect", 0, 1, 0, NULL,
965 ret_void, f_garbagecollect},
966 {"get", 2, 3, FEARG_1, NULL,
967 ret_any, f_get},
968 {"getbufinfo", 0, 1, FEARG_1, NULL,
969 ret_list_dict_any, f_getbufinfo},
970 {"getbufline", 2, 3, FEARG_1, NULL,
971 ret_list_string, f_getbufline},
972 {"getbufvar", 2, 3, FEARG_1, NULL,
973 ret_any, f_getbufvar},
974 {"getchangelist", 0, 1, FEARG_1, NULL,
975 ret_list_any, f_getchangelist},
976 {"getchar", 0, 1, 0, NULL,
977 ret_number, f_getchar},
978 {"getcharmod", 0, 0, 0, NULL,
979 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100980 {"getcharpos", 1, 1, FEARG_1, NULL,
981 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200982 {"getcharsearch", 0, 0, 0, NULL,
983 ret_dict_any, f_getcharsearch},
984 {"getcmdline", 0, 0, 0, NULL,
985 ret_string, f_getcmdline},
986 {"getcmdpos", 0, 0, 0, NULL,
987 ret_number, f_getcmdpos},
988 {"getcmdtype", 0, 0, 0, NULL,
989 ret_string, f_getcmdtype},
990 {"getcmdwintype", 0, 0, 0, NULL,
991 ret_string, f_getcmdwintype},
992 {"getcompletion", 2, 3, FEARG_1, NULL,
993 ret_list_string, f_getcompletion},
994 {"getcurpos", 0, 1, FEARG_1, NULL,
995 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100996 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
997 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200998 {"getcwd", 0, 2, FEARG_1, NULL,
999 ret_string, f_getcwd},
1000 {"getenv", 1, 1, FEARG_1, NULL,
1001 ret_string, f_getenv},
1002 {"getfontname", 0, 1, 0, NULL,
1003 ret_string, f_getfontname},
1004 {"getfperm", 1, 1, FEARG_1, NULL,
1005 ret_string, f_getfperm},
1006 {"getfsize", 1, 1, FEARG_1, NULL,
1007 ret_number, f_getfsize},
1008 {"getftime", 1, 1, FEARG_1, NULL,
1009 ret_number, f_getftime},
1010 {"getftype", 1, 1, FEARG_1, NULL,
1011 ret_string, f_getftype},
1012 {"getimstatus", 0, 0, 0, NULL,
1013 ret_number, f_getimstatus},
1014 {"getjumplist", 0, 2, FEARG_1, NULL,
1015 ret_list_any, f_getjumplist},
1016 {"getline", 1, 2, FEARG_1, NULL,
1017 ret_f_getline, f_getline},
1018 {"getloclist", 1, 2, 0, NULL,
1019 ret_list_or_dict_1, f_getloclist},
1020 {"getmarklist", 0, 1, FEARG_1, NULL,
1021 ret_list_dict_any, f_getmarklist},
1022 {"getmatches", 0, 1, 0, NULL,
1023 ret_list_dict_any, f_getmatches},
1024 {"getmousepos", 0, 0, 0, NULL,
1025 ret_dict_number, f_getmousepos},
1026 {"getpid", 0, 0, 0, NULL,
1027 ret_number, f_getpid},
1028 {"getpos", 1, 1, FEARG_1, NULL,
1029 ret_list_number, f_getpos},
1030 {"getqflist", 0, 1, 0, NULL,
1031 ret_list_or_dict_0, f_getqflist},
1032 {"getreg", 0, 3, FEARG_1, NULL,
1033 ret_getreg, f_getreg},
1034 {"getreginfo", 0, 1, FEARG_1, NULL,
1035 ret_dict_any, f_getreginfo},
1036 {"getregtype", 0, 1, FEARG_1, NULL,
1037 ret_string, f_getregtype},
1038 {"gettabinfo", 0, 1, FEARG_1, NULL,
1039 ret_list_dict_any, f_gettabinfo},
1040 {"gettabvar", 2, 3, FEARG_1, NULL,
1041 ret_any, f_gettabvar},
1042 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1043 ret_any, f_gettabwinvar},
1044 {"gettagstack", 0, 1, FEARG_1, NULL,
1045 ret_dict_any, f_gettagstack},
1046 {"gettext", 1, 1, FEARG_1, NULL,
1047 ret_string, f_gettext},
1048 {"getwininfo", 0, 1, FEARG_1, NULL,
1049 ret_list_dict_any, f_getwininfo},
1050 {"getwinpos", 0, 1, FEARG_1, NULL,
1051 ret_list_number, f_getwinpos},
1052 {"getwinposx", 0, 0, 0, NULL,
1053 ret_number, f_getwinposx},
1054 {"getwinposy", 0, 0, 0, NULL,
1055 ret_number, f_getwinposy},
1056 {"getwinvar", 2, 3, FEARG_1, NULL,
1057 ret_any, f_getwinvar},
1058 {"glob", 1, 4, FEARG_1, NULL,
1059 ret_any, f_glob},
1060 {"glob2regpat", 1, 1, FEARG_1, NULL,
1061 ret_string, f_glob2regpat},
1062 {"globpath", 2, 5, FEARG_2, NULL,
1063 ret_any, f_globpath},
1064 {"has", 1, 2, 0, NULL,
1065 ret_number, f_has},
1066 {"has_key", 2, 2, FEARG_1, NULL,
1067 ret_number, f_has_key},
1068 {"haslocaldir", 0, 2, FEARG_1, NULL,
1069 ret_number, f_haslocaldir},
1070 {"hasmapto", 1, 3, FEARG_1, NULL,
1071 ret_number, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001072 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1073 ret_number, f_hlID},
1074 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
1075 ret_number, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001076 {"histadd", 2, 2, FEARG_2, NULL,
1077 ret_number, f_histadd},
1078 {"histdel", 1, 2, FEARG_1, NULL,
1079 ret_number, f_histdel},
1080 {"histget", 1, 2, FEARG_1, NULL,
1081 ret_string, f_histget},
1082 {"histnr", 1, 1, FEARG_1, NULL,
1083 ret_number, f_histnr},
1084 {"hlID", 1, 1, FEARG_1, NULL,
1085 ret_number, f_hlID},
1086 {"hlexists", 1, 1, FEARG_1, NULL,
1087 ret_number, f_hlexists},
1088 {"hostname", 0, 0, 0, NULL,
1089 ret_string, f_hostname},
1090 {"iconv", 3, 3, FEARG_1, NULL,
1091 ret_string, f_iconv},
1092 {"indent", 1, 1, FEARG_1, NULL,
1093 ret_number, f_indent},
1094 {"index", 2, 4, FEARG_1, NULL,
1095 ret_number, f_index},
1096 {"input", 1, 3, FEARG_1, NULL,
1097 ret_string, f_input},
1098 {"inputdialog", 1, 3, FEARG_1, NULL,
1099 ret_string, f_inputdialog},
1100 {"inputlist", 1, 1, FEARG_1, NULL,
1101 ret_number, f_inputlist},
1102 {"inputrestore", 0, 0, 0, NULL,
1103 ret_number, f_inputrestore},
1104 {"inputsave", 0, 0, 0, NULL,
1105 ret_number, f_inputsave},
1106 {"inputsecret", 1, 2, FEARG_1, NULL,
1107 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001108 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001109 ret_first_arg, f_insert},
1110 {"interrupt", 0, 0, 0, NULL,
1111 ret_void, f_interrupt},
1112 {"invert", 1, 1, FEARG_1, NULL,
1113 ret_number, f_invert},
1114 {"isdirectory", 1, 1, FEARG_1, NULL,
1115 ret_number, f_isdirectory},
1116 {"isinf", 1, 1, FEARG_1, NULL,
1117 ret_number, MATH_FUNC(f_isinf)},
1118 {"islocked", 1, 1, FEARG_1, NULL,
1119 ret_number, f_islocked},
1120 {"isnan", 1, 1, FEARG_1, NULL,
1121 ret_number, MATH_FUNC(f_isnan)},
1122 {"items", 1, 1, FEARG_1, NULL,
1123 ret_list_any, f_items},
1124 {"job_getchannel", 1, 1, FEARG_1, NULL,
1125 ret_channel, JOB_FUNC(f_job_getchannel)},
1126 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001127 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001128 {"job_setoptions", 2, 2, FEARG_1, NULL,
1129 ret_void, JOB_FUNC(f_job_setoptions)},
1130 {"job_start", 1, 2, FEARG_1, NULL,
1131 ret_job, JOB_FUNC(f_job_start)},
1132 {"job_status", 1, 1, FEARG_1, NULL,
1133 ret_string, JOB_FUNC(f_job_status)},
1134 {"job_stop", 1, 2, FEARG_1, NULL,
1135 ret_number, JOB_FUNC(f_job_stop)},
1136 {"join", 1, 2, FEARG_1, NULL,
1137 ret_string, f_join},
1138 {"js_decode", 1, 1, FEARG_1, NULL,
1139 ret_any, f_js_decode},
1140 {"js_encode", 1, 1, FEARG_1, NULL,
1141 ret_string, f_js_encode},
1142 {"json_decode", 1, 1, FEARG_1, NULL,
1143 ret_any, f_json_decode},
1144 {"json_encode", 1, 1, FEARG_1, NULL,
1145 ret_string, f_json_encode},
1146 {"keys", 1, 1, FEARG_1, NULL,
1147 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001148 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1149 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001150 {"len", 1, 1, FEARG_1, NULL,
1151 ret_number, f_len},
1152 {"libcall", 3, 3, FEARG_3, NULL,
1153 ret_string, f_libcall},
1154 {"libcallnr", 3, 3, FEARG_3, NULL,
1155 ret_number, f_libcallnr},
1156 {"line", 1, 2, FEARG_1, NULL,
1157 ret_number, f_line},
1158 {"line2byte", 1, 1, FEARG_1, NULL,
1159 ret_number, f_line2byte},
1160 {"lispindent", 1, 1, FEARG_1, NULL,
1161 ret_number, f_lispindent},
1162 {"list2str", 1, 2, FEARG_1, NULL,
1163 ret_string, f_list2str},
1164 {"listener_add", 1, 2, FEARG_2, NULL,
1165 ret_number, f_listener_add},
1166 {"listener_flush", 0, 1, FEARG_1, NULL,
1167 ret_void, f_listener_flush},
1168 {"listener_remove", 1, 1, FEARG_1, NULL,
1169 ret_number, f_listener_remove},
1170 {"localtime", 0, 0, 0, NULL,
1171 ret_number, f_localtime},
1172 {"log", 1, 1, FEARG_1, NULL,
1173 ret_float, FLOAT_FUNC(f_log)},
1174 {"log10", 1, 1, FEARG_1, NULL,
1175 ret_float, FLOAT_FUNC(f_log10)},
1176 {"luaeval", 1, 2, FEARG_1, NULL,
1177 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001178#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001179 f_luaeval
1180#else
1181 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001182#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001183 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001184 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001185 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001186 {"maparg", 1, 4, FEARG_1, NULL,
1187 ret_maparg, f_maparg},
1188 {"mapcheck", 1, 3, FEARG_1, NULL,
1189 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001190 {"mapnew", 2, 2, FEARG_1, NULL,
1191 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001192 {"mapset", 3, 3, FEARG_1, NULL,
1193 ret_void, f_mapset},
1194 {"match", 2, 4, FEARG_1, NULL,
1195 ret_any, f_match},
1196 {"matchadd", 2, 5, FEARG_1, NULL,
1197 ret_number, f_matchadd},
1198 {"matchaddpos", 2, 5, FEARG_1, NULL,
1199 ret_number, f_matchaddpos},
1200 {"matcharg", 1, 1, FEARG_1, NULL,
1201 ret_list_string, f_matcharg},
1202 {"matchdelete", 1, 2, FEARG_1, NULL,
1203 ret_number, f_matchdelete},
1204 {"matchend", 2, 4, FEARG_1, NULL,
1205 ret_number, f_matchend},
1206 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1207 ret_list_string, f_matchfuzzy},
1208 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1209 ret_list_any, f_matchfuzzypos},
1210 {"matchlist", 2, 4, FEARG_1, NULL,
1211 ret_list_string, f_matchlist},
1212 {"matchstr", 2, 4, FEARG_1, NULL,
1213 ret_string, f_matchstr},
1214 {"matchstrpos", 2, 4, FEARG_1, NULL,
1215 ret_list_any, f_matchstrpos},
1216 {"max", 1, 1, FEARG_1, NULL,
1217 ret_any, f_max},
1218 {"menu_info", 1, 2, FEARG_1, NULL,
1219 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001220#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001221 f_menu_info
1222#else
1223 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001224#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001225 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001226 {"min", 1, 1, FEARG_1, NULL,
1227 ret_any, f_min},
1228 {"mkdir", 1, 3, FEARG_1, NULL,
1229 ret_number, f_mkdir},
1230 {"mode", 0, 1, FEARG_1, NULL,
1231 ret_string, f_mode},
1232 {"mzeval", 1, 1, FEARG_1, NULL,
1233 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001234#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001235 f_mzeval
1236#else
1237 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001238#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001239 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001240 {"nextnonblank", 1, 1, FEARG_1, NULL,
1241 ret_number, f_nextnonblank},
1242 {"nr2char", 1, 2, FEARG_1, NULL,
1243 ret_string, f_nr2char},
1244 {"or", 2, 2, FEARG_1, NULL,
1245 ret_number, f_or},
1246 {"pathshorten", 1, 2, FEARG_1, NULL,
1247 ret_string, f_pathshorten},
1248 {"perleval", 1, 1, FEARG_1, NULL,
1249 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001250#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001251 f_perleval
1252#else
1253 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001254#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001255 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001256 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1257 ret_number, PROP_FUNC(f_popup_atcursor)},
1258 {"popup_beval", 2, 2, FEARG_1, NULL,
1259 ret_number, PROP_FUNC(f_popup_beval)},
1260 {"popup_clear", 0, 1, 0, NULL,
1261 ret_void, PROP_FUNC(f_popup_clear)},
1262 {"popup_close", 1, 2, FEARG_1, NULL,
1263 ret_void, PROP_FUNC(f_popup_close)},
1264 {"popup_create", 2, 2, FEARG_1, NULL,
1265 ret_number, PROP_FUNC(f_popup_create)},
1266 {"popup_dialog", 2, 2, FEARG_1, NULL,
1267 ret_number, PROP_FUNC(f_popup_dialog)},
1268 {"popup_filter_menu", 2, 2, 0, NULL,
1269 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1270 {"popup_filter_yesno", 2, 2, 0, NULL,
1271 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1272 {"popup_findinfo", 0, 0, 0, NULL,
1273 ret_number, PROP_FUNC(f_popup_findinfo)},
1274 {"popup_findpreview", 0, 0, 0, NULL,
1275 ret_number, PROP_FUNC(f_popup_findpreview)},
1276 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1277 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1278 {"popup_getpos", 1, 1, FEARG_1, NULL,
1279 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1280 {"popup_hide", 1, 1, FEARG_1, NULL,
1281 ret_void, PROP_FUNC(f_popup_hide)},
1282 {"popup_list", 0, 0, 0, NULL,
1283 ret_list_number, PROP_FUNC(f_popup_list)},
1284 {"popup_locate", 2, 2, 0, NULL,
1285 ret_number, PROP_FUNC(f_popup_locate)},
1286 {"popup_menu", 2, 2, FEARG_1, NULL,
1287 ret_number, PROP_FUNC(f_popup_menu)},
1288 {"popup_move", 2, 2, FEARG_1, NULL,
1289 ret_void, PROP_FUNC(f_popup_move)},
1290 {"popup_notification", 2, 2, FEARG_1, NULL,
1291 ret_number, PROP_FUNC(f_popup_notification)},
1292 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1293 ret_void, PROP_FUNC(f_popup_setoptions)},
1294 {"popup_settext", 2, 2, FEARG_1, NULL,
1295 ret_void, PROP_FUNC(f_popup_settext)},
1296 {"popup_show", 1, 1, FEARG_1, NULL,
1297 ret_void, PROP_FUNC(f_popup_show)},
1298 {"pow", 2, 2, FEARG_1, NULL,
1299 ret_float, FLOAT_FUNC(f_pow)},
1300 {"prevnonblank", 1, 1, FEARG_1, NULL,
1301 ret_number, f_prevnonblank},
1302 {"printf", 1, 19, FEARG_2, NULL,
1303 ret_string, f_printf},
1304 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1305 ret_string, JOB_FUNC(f_prompt_getprompt)},
1306 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1307 ret_void, JOB_FUNC(f_prompt_setcallback)},
1308 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1309 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1310 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1311 ret_void, JOB_FUNC(f_prompt_setprompt)},
1312 {"prop_add", 3, 3, FEARG_1, NULL,
1313 ret_void, PROP_FUNC(f_prop_add)},
1314 {"prop_clear", 1, 3, FEARG_1, NULL,
1315 ret_void, PROP_FUNC(f_prop_clear)},
1316 {"prop_find", 1, 2, FEARG_1, NULL,
1317 ret_dict_any, PROP_FUNC(f_prop_find)},
1318 {"prop_list", 1, 2, FEARG_1, NULL,
1319 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1320 {"prop_remove", 1, 3, FEARG_1, NULL,
1321 ret_number, PROP_FUNC(f_prop_remove)},
1322 {"prop_type_add", 2, 2, FEARG_1, NULL,
1323 ret_void, PROP_FUNC(f_prop_type_add)},
1324 {"prop_type_change", 2, 2, FEARG_1, NULL,
1325 ret_void, PROP_FUNC(f_prop_type_change)},
1326 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1327 ret_void, PROP_FUNC(f_prop_type_delete)},
1328 {"prop_type_get", 1, 2, FEARG_1, NULL,
1329 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1330 {"prop_type_list", 0, 1, FEARG_1, NULL,
1331 ret_list_string, PROP_FUNC(f_prop_type_list)},
1332 {"pum_getpos", 0, 0, 0, NULL,
1333 ret_dict_number, f_pum_getpos},
1334 {"pumvisible", 0, 0, 0, NULL,
1335 ret_number, f_pumvisible},
1336 {"py3eval", 1, 1, FEARG_1, NULL,
1337 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001338#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001339 f_py3eval
1340#else
1341 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001342#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001343 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001344 {"pyeval", 1, 1, FEARG_1, NULL,
1345 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001346#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001347 f_pyeval
1348#else
1349 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001350#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001351 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001352 {"pyxeval", 1, 1, FEARG_1, NULL,
1353 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001354#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001355 f_pyxeval
1356#else
1357 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001358#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001359 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001360 {"rand", 0, 1, FEARG_1, NULL,
1361 ret_number, f_rand},
1362 {"range", 1, 3, FEARG_1, NULL,
1363 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001364 {"readblob", 1, 1, FEARG_1, NULL,
1365 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001366 {"readdir", 1, 3, FEARG_1, NULL,
1367 ret_list_string, f_readdir},
1368 {"readdirex", 1, 3, FEARG_1, NULL,
1369 ret_list_dict_any, f_readdirex},
1370 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001371 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001372 {"reduce", 2, 3, FEARG_1, NULL,
1373 ret_any, f_reduce},
1374 {"reg_executing", 0, 0, 0, NULL,
1375 ret_string, f_reg_executing},
1376 {"reg_recording", 0, 0, 0, NULL,
1377 ret_string, f_reg_recording},
1378 {"reltime", 0, 2, FEARG_1, NULL,
1379 ret_list_any, f_reltime},
1380 {"reltimefloat", 1, 1, FEARG_1, NULL,
1381 ret_float, FLOAT_FUNC(f_reltimefloat)},
1382 {"reltimestr", 1, 1, FEARG_1, NULL,
1383 ret_string, f_reltimestr},
1384 {"remote_expr", 2, 4, FEARG_1, NULL,
1385 ret_string, f_remote_expr},
1386 {"remote_foreground", 1, 1, FEARG_1, NULL,
1387 ret_string, f_remote_foreground},
1388 {"remote_peek", 1, 2, FEARG_1, NULL,
1389 ret_number, f_remote_peek},
1390 {"remote_read", 1, 2, FEARG_1, NULL,
1391 ret_string, f_remote_read},
1392 {"remote_send", 2, 3, FEARG_1, NULL,
1393 ret_string, f_remote_send},
1394 {"remote_startserver", 1, 1, FEARG_1, NULL,
1395 ret_void, f_remote_startserver},
1396 {"remove", 2, 3, FEARG_1, NULL,
1397 ret_remove, f_remove},
1398 {"rename", 2, 2, FEARG_1, NULL,
1399 ret_number, f_rename},
1400 {"repeat", 2, 2, FEARG_1, NULL,
1401 ret_first_arg, f_repeat},
1402 {"resolve", 1, 1, FEARG_1, NULL,
1403 ret_string, f_resolve},
1404 {"reverse", 1, 1, FEARG_1, NULL,
1405 ret_first_arg, f_reverse},
1406 {"round", 1, 1, FEARG_1, NULL,
1407 ret_float, FLOAT_FUNC(f_round)},
1408 {"rubyeval", 1, 1, FEARG_1, NULL,
1409 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001410#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001411 f_rubyeval
1412#else
1413 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001414#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001415 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001416 {"screenattr", 2, 2, FEARG_1, NULL,
1417 ret_number, f_screenattr},
1418 {"screenchar", 2, 2, FEARG_1, NULL,
1419 ret_number, f_screenchar},
1420 {"screenchars", 2, 2, FEARG_1, NULL,
1421 ret_list_number, f_screenchars},
1422 {"screencol", 0, 0, 0, NULL,
1423 ret_number, f_screencol},
1424 {"screenpos", 3, 3, FEARG_1, NULL,
1425 ret_dict_number, f_screenpos},
1426 {"screenrow", 0, 0, 0, NULL,
1427 ret_number, f_screenrow},
1428 {"screenstring", 2, 2, FEARG_1, NULL,
1429 ret_string, f_screenstring},
1430 {"search", 1, 5, FEARG_1, NULL,
1431 ret_number, f_search},
1432 {"searchcount", 0, 1, FEARG_1, NULL,
1433 ret_dict_any, f_searchcount},
1434 {"searchdecl", 1, 3, FEARG_1, NULL,
1435 ret_number, f_searchdecl},
1436 {"searchpair", 3, 7, 0, NULL,
1437 ret_number, f_searchpair},
1438 {"searchpairpos", 3, 7, 0, NULL,
1439 ret_list_number, f_searchpairpos},
1440 {"searchpos", 1, 5, FEARG_1, NULL,
1441 ret_list_number, f_searchpos},
1442 {"server2client", 2, 2, FEARG_1, NULL,
1443 ret_number, f_server2client},
1444 {"serverlist", 0, 0, 0, NULL,
1445 ret_string, f_serverlist},
1446 {"setbufline", 3, 3, FEARG_3, NULL,
1447 ret_number, f_setbufline},
1448 {"setbufvar", 3, 3, FEARG_3, NULL,
1449 ret_void, f_setbufvar},
1450 {"setcellwidths", 1, 1, FEARG_1, NULL,
1451 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001452 {"setcharpos", 2, 2, FEARG_2, NULL,
1453 ret_number, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001454 {"setcharsearch", 1, 1, FEARG_1, NULL,
1455 ret_void, f_setcharsearch},
1456 {"setcmdpos", 1, 1, FEARG_1, NULL,
1457 ret_number, f_setcmdpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001458 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1459 ret_number, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001460 {"setenv", 2, 2, FEARG_2, NULL,
1461 ret_void, f_setenv},
1462 {"setfperm", 2, 2, FEARG_1, NULL,
1463 ret_number, f_setfperm},
1464 {"setline", 2, 2, FEARG_2, NULL,
1465 ret_number, f_setline},
1466 {"setloclist", 2, 4, FEARG_2, NULL,
1467 ret_number, f_setloclist},
1468 {"setmatches", 1, 2, FEARG_1, NULL,
1469 ret_number, f_setmatches},
1470 {"setpos", 2, 2, FEARG_2, NULL,
1471 ret_number, f_setpos},
1472 {"setqflist", 1, 3, FEARG_1, NULL,
1473 ret_number, f_setqflist},
1474 {"setreg", 2, 3, FEARG_2, NULL,
1475 ret_number, f_setreg},
1476 {"settabvar", 3, 3, FEARG_3, NULL,
1477 ret_void, f_settabvar},
1478 {"settabwinvar", 4, 4, FEARG_4, NULL,
1479 ret_void, f_settabwinvar},
1480 {"settagstack", 2, 3, FEARG_2, NULL,
1481 ret_number, f_settagstack},
1482 {"setwinvar", 3, 3, FEARG_3, NULL,
1483 ret_void, f_setwinvar},
1484 {"sha256", 1, 1, FEARG_1, NULL,
1485 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001486#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001487 f_sha256
1488#else
1489 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001490#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001491 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001492 {"shellescape", 1, 2, FEARG_1, NULL,
1493 ret_string, f_shellescape},
1494 {"shiftwidth", 0, 1, FEARG_1, NULL,
1495 ret_number, f_shiftwidth},
1496 {"sign_define", 1, 2, FEARG_1, NULL,
1497 ret_any, SIGN_FUNC(f_sign_define)},
1498 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1499 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1500 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1501 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1502 {"sign_jump", 3, 3, FEARG_1, NULL,
1503 ret_number, SIGN_FUNC(f_sign_jump)},
1504 {"sign_place", 4, 5, FEARG_1, NULL,
1505 ret_number, SIGN_FUNC(f_sign_place)},
1506 {"sign_placelist", 1, 1, FEARG_1, NULL,
1507 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1508 {"sign_undefine", 0, 1, FEARG_1, NULL,
1509 ret_number, SIGN_FUNC(f_sign_undefine)},
1510 {"sign_unplace", 1, 2, FEARG_1, NULL,
1511 ret_number, SIGN_FUNC(f_sign_unplace)},
1512 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1513 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1514 {"simplify", 1, 1, FEARG_1, NULL,
1515 ret_string, f_simplify},
1516 {"sin", 1, 1, FEARG_1, NULL,
1517 ret_float, FLOAT_FUNC(f_sin)},
1518 {"sinh", 1, 1, FEARG_1, NULL,
1519 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001520 {"slice", 2, 3, FEARG_1, NULL,
1521 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001522 {"sort", 1, 3, FEARG_1, NULL,
1523 ret_first_arg, f_sort},
1524 {"sound_clear", 0, 0, 0, NULL,
1525 ret_void, SOUND_FUNC(f_sound_clear)},
1526 {"sound_playevent", 1, 2, FEARG_1, NULL,
1527 ret_number, SOUND_FUNC(f_sound_playevent)},
1528 {"sound_playfile", 1, 2, FEARG_1, NULL,
1529 ret_number, SOUND_FUNC(f_sound_playfile)},
1530 {"sound_stop", 1, 1, FEARG_1, NULL,
1531 ret_void, SOUND_FUNC(f_sound_stop)},
1532 {"soundfold", 1, 1, FEARG_1, NULL,
1533 ret_string, f_soundfold},
1534 {"spellbadword", 0, 1, FEARG_1, NULL,
1535 ret_list_string, f_spellbadword},
1536 {"spellsuggest", 1, 3, FEARG_1, NULL,
1537 ret_list_string, f_spellsuggest},
1538 {"split", 1, 3, FEARG_1, NULL,
1539 ret_list_string, f_split},
1540 {"sqrt", 1, 1, FEARG_1, NULL,
1541 ret_float, FLOAT_FUNC(f_sqrt)},
1542 {"srand", 0, 1, FEARG_1, NULL,
1543 ret_list_number, f_srand},
1544 {"state", 0, 1, FEARG_1, NULL,
1545 ret_string, f_state},
1546 {"str2float", 1, 1, FEARG_1, NULL,
1547 ret_float, FLOAT_FUNC(f_str2float)},
1548 {"str2list", 1, 2, FEARG_1, NULL,
1549 ret_list_number, f_str2list},
1550 {"str2nr", 1, 3, FEARG_1, NULL,
1551 ret_number, f_str2nr},
1552 {"strcharpart", 2, 3, FEARG_1, NULL,
1553 ret_string, f_strcharpart},
1554 {"strchars", 1, 2, FEARG_1, NULL,
1555 ret_number, f_strchars},
1556 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1557 ret_number, f_strdisplaywidth},
1558 {"strftime", 1, 2, FEARG_1, NULL,
1559 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001560#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001561 f_strftime
1562#else
1563 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001564#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001565 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001566 {"strgetchar", 2, 2, FEARG_1, NULL,
1567 ret_number, f_strgetchar},
1568 {"stridx", 2, 3, FEARG_1, NULL,
1569 ret_number, f_stridx},
1570 {"string", 1, 1, FEARG_1, NULL,
1571 ret_string, f_string},
1572 {"strlen", 1, 1, FEARG_1, NULL,
1573 ret_number, f_strlen},
1574 {"strpart", 2, 4, FEARG_1, NULL,
1575 ret_string, f_strpart},
1576 {"strptime", 2, 2, FEARG_1, NULL,
1577 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001578#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001579 f_strptime
1580#else
1581 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001582#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001583 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001584 {"strridx", 2, 3, FEARG_1, NULL,
1585 ret_number, f_strridx},
1586 {"strtrans", 1, 1, FEARG_1, NULL,
1587 ret_string, f_strtrans},
1588 {"strwidth", 1, 1, FEARG_1, NULL,
1589 ret_number, f_strwidth},
1590 {"submatch", 1, 2, FEARG_1, NULL,
1591 ret_string, f_submatch},
1592 {"substitute", 4, 4, FEARG_1, NULL,
1593 ret_string, f_substitute},
1594 {"swapinfo", 1, 1, FEARG_1, NULL,
1595 ret_dict_any, f_swapinfo},
1596 {"swapname", 1, 1, FEARG_1, NULL,
1597 ret_string, f_swapname},
1598 {"synID", 3, 3, 0, NULL,
1599 ret_number, f_synID},
1600 {"synIDattr", 2, 3, FEARG_1, NULL,
1601 ret_string, f_synIDattr},
1602 {"synIDtrans", 1, 1, FEARG_1, NULL,
1603 ret_number, f_synIDtrans},
1604 {"synconcealed", 2, 2, 0, NULL,
1605 ret_list_any, f_synconcealed},
1606 {"synstack", 2, 2, 0, NULL,
1607 ret_list_number, f_synstack},
1608 {"system", 1, 2, FEARG_1, NULL,
1609 ret_string, f_system},
1610 {"systemlist", 1, 2, FEARG_1, NULL,
1611 ret_list_string, f_systemlist},
1612 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1613 ret_list_number, f_tabpagebuflist},
1614 {"tabpagenr", 0, 1, 0, NULL,
1615 ret_number, f_tabpagenr},
1616 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1617 ret_number, f_tabpagewinnr},
1618 {"tagfiles", 0, 0, 0, NULL,
1619 ret_list_string, f_tagfiles},
1620 {"taglist", 1, 2, FEARG_1, NULL,
1621 ret_list_dict_any, f_taglist},
1622 {"tan", 1, 1, FEARG_1, NULL,
1623 ret_float, FLOAT_FUNC(f_tan)},
1624 {"tanh", 1, 1, FEARG_1, NULL,
1625 ret_float, FLOAT_FUNC(f_tanh)},
1626 {"tempname", 0, 0, 0, NULL,
1627 ret_string, f_tempname},
1628 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1629 ret_number, TERM_FUNC(f_term_dumpdiff)},
1630 {"term_dumpload", 1, 2, FEARG_1, NULL,
1631 ret_number, TERM_FUNC(f_term_dumpload)},
1632 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1633 ret_void, TERM_FUNC(f_term_dumpwrite)},
1634 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1635 ret_number, TERM_FUNC(f_term_getaltscreen)},
1636 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1637 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001638#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001639 f_term_getansicolors
1640#else
1641 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001642#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001643 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001644 {"term_getattr", 2, 2, FEARG_1, NULL,
1645 ret_number, TERM_FUNC(f_term_getattr)},
1646 {"term_getcursor", 1, 1, FEARG_1, NULL,
1647 ret_list_any, TERM_FUNC(f_term_getcursor)},
1648 {"term_getjob", 1, 1, FEARG_1, NULL,
1649 ret_job, TERM_FUNC(f_term_getjob)},
1650 {"term_getline", 2, 2, FEARG_1, NULL,
1651 ret_string, TERM_FUNC(f_term_getline)},
1652 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1653 ret_number, TERM_FUNC(f_term_getscrolled)},
1654 {"term_getsize", 1, 1, FEARG_1, NULL,
1655 ret_list_number, TERM_FUNC(f_term_getsize)},
1656 {"term_getstatus", 1, 1, FEARG_1, NULL,
1657 ret_string, TERM_FUNC(f_term_getstatus)},
1658 {"term_gettitle", 1, 1, FEARG_1, NULL,
1659 ret_string, TERM_FUNC(f_term_gettitle)},
1660 {"term_gettty", 1, 2, FEARG_1, NULL,
1661 ret_string, TERM_FUNC(f_term_gettty)},
1662 {"term_list", 0, 0, 0, NULL,
1663 ret_list_number, TERM_FUNC(f_term_list)},
1664 {"term_scrape", 2, 2, FEARG_1, NULL,
1665 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1666 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1667 ret_void, TERM_FUNC(f_term_sendkeys)},
1668 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1669 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001670#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001671 f_term_setansicolors
1672#else
1673 NULL
1674#endif
1675 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001676 {"term_setapi", 2, 2, FEARG_1, NULL,
1677 ret_void, TERM_FUNC(f_term_setapi)},
1678 {"term_setkill", 2, 2, FEARG_1, NULL,
1679 ret_void, TERM_FUNC(f_term_setkill)},
1680 {"term_setrestore", 2, 2, FEARG_1, NULL,
1681 ret_void, TERM_FUNC(f_term_setrestore)},
1682 {"term_setsize", 3, 3, FEARG_1, NULL,
1683 ret_void, TERM_FUNC(f_term_setsize)},
1684 {"term_start", 1, 2, FEARG_1, NULL,
1685 ret_number, TERM_FUNC(f_term_start)},
1686 {"term_wait", 1, 2, FEARG_1, NULL,
1687 ret_void, TERM_FUNC(f_term_wait)},
1688 {"terminalprops", 0, 0, 0, NULL,
1689 ret_dict_string, f_terminalprops},
1690 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1691 ret_void, f_test_alloc_fail},
1692 {"test_autochdir", 0, 0, 0, NULL,
1693 ret_void, f_test_autochdir},
1694 {"test_feedinput", 1, 1, FEARG_1, NULL,
1695 ret_void, f_test_feedinput},
1696 {"test_garbagecollect_now", 0, 0, 0, NULL,
1697 ret_void, f_test_garbagecollect_now},
1698 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1699 ret_void, f_test_garbagecollect_soon},
1700 {"test_getvalue", 1, 1, FEARG_1, NULL,
1701 ret_number, f_test_getvalue},
1702 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1703 ret_void, f_test_ignore_error},
1704 {"test_null_blob", 0, 0, 0, NULL,
1705 ret_blob, f_test_null_blob},
1706 {"test_null_channel", 0, 0, 0, NULL,
1707 ret_channel, JOB_FUNC(f_test_null_channel)},
1708 {"test_null_dict", 0, 0, 0, NULL,
1709 ret_dict_any, f_test_null_dict},
1710 {"test_null_function", 0, 0, 0, NULL,
1711 ret_func_any, f_test_null_function},
1712 {"test_null_job", 0, 0, 0, NULL,
1713 ret_job, JOB_FUNC(f_test_null_job)},
1714 {"test_null_list", 0, 0, 0, NULL,
1715 ret_list_any, f_test_null_list},
1716 {"test_null_partial", 0, 0, 0, NULL,
1717 ret_func_any, f_test_null_partial},
1718 {"test_null_string", 0, 0, 0, NULL,
1719 ret_string, f_test_null_string},
1720 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1721 ret_void, f_test_option_not_set},
1722 {"test_override", 2, 2, FEARG_2, NULL,
1723 ret_void, f_test_override},
1724 {"test_refcount", 1, 1, FEARG_1, NULL,
1725 ret_number, f_test_refcount},
1726 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1727 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001728#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001729 f_test_scrollbar
1730#else
1731 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001732#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001733 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001734 {"test_setmouse", 2, 2, 0, NULL,
1735 ret_void, f_test_setmouse},
1736 {"test_settime", 1, 1, FEARG_1, NULL,
1737 ret_void, f_test_settime},
1738 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1739 ret_void, f_test_srand_seed},
1740 {"test_unknown", 0, 0, 0, NULL,
1741 ret_any, f_test_unknown},
1742 {"test_void", 0, 0, 0, NULL,
1743 ret_void, f_test_void},
1744 {"timer_info", 0, 1, FEARG_1, NULL,
1745 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1746 {"timer_pause", 2, 2, FEARG_1, NULL,
1747 ret_void, TIMER_FUNC(f_timer_pause)},
1748 {"timer_start", 2, 3, FEARG_1, NULL,
1749 ret_number, TIMER_FUNC(f_timer_start)},
1750 {"timer_stop", 1, 1, FEARG_1, NULL,
1751 ret_void, TIMER_FUNC(f_timer_stop)},
1752 {"timer_stopall", 0, 0, 0, NULL,
1753 ret_void, TIMER_FUNC(f_timer_stopall)},
1754 {"tolower", 1, 1, FEARG_1, NULL,
1755 ret_string, f_tolower},
1756 {"toupper", 1, 1, FEARG_1, NULL,
1757 ret_string, f_toupper},
1758 {"tr", 3, 3, FEARG_1, NULL,
1759 ret_string, f_tr},
1760 {"trim", 1, 3, FEARG_1, NULL,
1761 ret_string, f_trim},
1762 {"trunc", 1, 1, FEARG_1, NULL,
1763 ret_float, FLOAT_FUNC(f_trunc)},
1764 {"type", 1, 1, FEARG_1, NULL,
1765 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001766 {"typename", 1, 1, FEARG_1, NULL,
1767 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001768 {"undofile", 1, 1, FEARG_1, NULL,
1769 ret_string, f_undofile},
1770 {"undotree", 0, 0, 0, NULL,
1771 ret_dict_any, f_undotree},
1772 {"uniq", 1, 3, FEARG_1, NULL,
1773 ret_list_any, f_uniq},
1774 {"values", 1, 1, FEARG_1, NULL,
1775 ret_list_any, f_values},
1776 {"virtcol", 1, 1, FEARG_1, NULL,
1777 ret_number, f_virtcol},
1778 {"visualmode", 0, 1, 0, NULL,
1779 ret_string, f_visualmode},
1780 {"wildmenumode", 0, 0, 0, NULL,
1781 ret_number, f_wildmenumode},
1782 {"win_execute", 2, 3, FEARG_2, NULL,
1783 ret_string, f_win_execute},
1784 {"win_findbuf", 1, 1, FEARG_1, NULL,
1785 ret_list_number, f_win_findbuf},
1786 {"win_getid", 0, 2, FEARG_1, NULL,
1787 ret_number, f_win_getid},
1788 {"win_gettype", 0, 1, FEARG_1, NULL,
1789 ret_string, f_win_gettype},
1790 {"win_gotoid", 1, 1, FEARG_1, NULL,
1791 ret_number, f_win_gotoid},
1792 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1793 ret_list_number, f_win_id2tabwin},
1794 {"win_id2win", 1, 1, FEARG_1, NULL,
1795 ret_number, f_win_id2win},
1796 {"win_screenpos", 1, 1, FEARG_1, NULL,
1797 ret_list_number, f_win_screenpos},
1798 {"win_splitmove", 2, 3, FEARG_1, NULL,
1799 ret_number, f_win_splitmove},
1800 {"winbufnr", 1, 1, FEARG_1, NULL,
1801 ret_number, f_winbufnr},
1802 {"wincol", 0, 0, 0, NULL,
1803 ret_number, f_wincol},
1804 {"windowsversion", 0, 0, 0, NULL,
1805 ret_string, f_windowsversion},
1806 {"winheight", 1, 1, FEARG_1, NULL,
1807 ret_number, f_winheight},
1808 {"winlayout", 0, 1, FEARG_1, NULL,
1809 ret_list_any, f_winlayout},
1810 {"winline", 0, 0, 0, NULL,
1811 ret_number, f_winline},
1812 {"winnr", 0, 1, FEARG_1, NULL,
1813 ret_number, f_winnr},
1814 {"winrestcmd", 0, 0, 0, NULL,
1815 ret_string, f_winrestcmd},
1816 {"winrestview", 1, 1, FEARG_1, NULL,
1817 ret_void, f_winrestview},
1818 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001819 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001820 {"winwidth", 1, 1, FEARG_1, NULL,
1821 ret_number, f_winwidth},
1822 {"wordcount", 0, 0, 0, NULL,
1823 ret_dict_number, f_wordcount},
1824 {"writefile", 2, 3, FEARG_1, NULL,
1825 ret_number, f_writefile},
1826 {"xor", 2, 2, FEARG_1, NULL,
1827 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001828};
1829
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001830/*
1831 * Function given to ExpandGeneric() to obtain the list of internal
1832 * or user defined function names.
1833 */
1834 char_u *
1835get_function_name(expand_T *xp, int idx)
1836{
1837 static int intidx = -1;
1838 char_u *name;
1839
1840 if (idx == 0)
1841 intidx = -1;
1842 if (intidx < 0)
1843 {
1844 name = get_user_func_name(xp, idx);
1845 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001846 {
1847 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1848 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001849 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001850 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001851 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001852 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001853 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001854 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001855 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001856 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001857 STRCAT(IObuff, ")");
1858 return IObuff;
1859 }
1860
1861 return NULL;
1862}
1863
1864/*
1865 * Function given to ExpandGeneric() to obtain the list of internal or
1866 * user defined variable or function names.
1867 */
1868 char_u *
1869get_expr_name(expand_T *xp, int idx)
1870{
1871 static int intidx = -1;
1872 char_u *name;
1873
1874 if (idx == 0)
1875 intidx = -1;
1876 if (intidx < 0)
1877 {
1878 name = get_function_name(xp, idx);
1879 if (name != NULL)
1880 return name;
1881 }
1882 return get_user_var_name(xp, ++intidx);
1883}
1884
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001885/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001886 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001887 * Return index, or -1 if not found or "implemented" is TRUE and the function
1888 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001889 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001890 static int
1891find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001892{
1893 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001894 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001895 int cmp;
1896 int x;
1897
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001898 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001899
1900 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001901 while (first <= last)
1902 {
1903 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001904 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001905 if (cmp < 0)
1906 last = x - 1;
1907 else if (cmp > 0)
1908 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001909 else if (implemented && global_functions[x].f_func == NULL)
1910 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001911 else
1912 return x;
1913 }
1914 return -1;
1915}
1916
Bram Moolenaar15c47602020-03-26 22:16:48 +01001917/*
1918 * Find internal function "name" in table "global_functions".
1919 * Return index, or -1 if not found or the function is not implemented.
1920 */
1921 int
1922find_internal_func(char_u *name)
1923{
1924 return find_internal_func_opt(name, TRUE);
1925}
1926
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001928has_internal_func(char_u *name)
1929{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001930 return find_internal_func_opt(name, TRUE) >= 0;
1931}
1932
1933 static int
1934has_internal_func_name(char_u *name)
1935{
1936 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001937}
1938
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 char *
1940internal_func_name(int idx)
1941{
1942 return global_functions[idx].f_name;
1943}
1944
Bram Moolenaar94738d82020-10-21 14:25:07 +02001945/*
1946 * Check the argument types for builting function "idx".
1947 * Uses the list of types on the type stack: "types".
1948 * Return FAIL and gives an error message when a type is wrong.
1949 */
1950 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001951internal_func_check_arg_types(
1952 type_T **types,
1953 int idx,
1954 int argcount,
1955 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001956{
1957 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1958 int i;
1959
1960 if (argchecks != NULL)
1961 {
1962 argcontext_T context;
1963
1964 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001965 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001966 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001967 for (i = 0; i < argcount; ++i)
1968 if (argchecks[i] != NULL)
1969 {
1970 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001971 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001972 return FAIL;
1973 }
1974 }
1975 return OK;
1976}
1977
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001978/*
1979 * Call the "f_retfunc" function to obtain the return type of function "idx".
1980 * "argtypes" is the list of argument types or NULL when there are no
1981 * arguments.
1982 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001984internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001986 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987}
1988
1989/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001990 * Return TRUE if "idx" is for the map() function.
1991 */
1992 int
1993internal_func_is_map(int idx)
1994{
1995 return global_functions[idx].f_func == f_map;
1996}
1997
1998/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002000 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2001 * first argument, 2 if method base is second argument, etc. 9 if method base
2002 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 */
2004 int
2005check_internal_func(int idx, int argcount)
2006{
2007 int res;
2008 char *name;
2009
2010 if (argcount < global_functions[idx].f_min_argc)
2011 res = FCERR_TOOFEW;
2012 else if (argcount > global_functions[idx].f_max_argc)
2013 res = FCERR_TOOMANY;
2014 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002015 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016
2017 name = internal_func_name(idx);
2018 if (res == FCERR_TOOMANY)
2019 semsg(_(e_toomanyarg), name);
2020 else
2021 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002022 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023}
2024
Bram Moolenaarac92e252019-08-03 21:58:38 +02002025 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002026call_internal_func(
2027 char_u *name,
2028 int argcount,
2029 typval_T *argvars,
2030 typval_T *rettv)
2031{
2032 int i;
2033
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002034 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002035 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002036 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002037 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002038 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002039 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002040 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002041 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002042 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002043 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002044}
2045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 void
2047call_internal_func_by_idx(
2048 int idx,
2049 typval_T *argvars,
2050 typval_T *rettv)
2051{
2052 global_functions[idx].f_func(argvars, rettv);
2053}
2054
Bram Moolenaarac92e252019-08-03 21:58:38 +02002055/*
2056 * Invoke a method for base->method().
2057 */
2058 int
2059call_internal_method(
2060 char_u *name,
2061 int argcount,
2062 typval_T *argvars,
2063 typval_T *rettv,
2064 typval_T *basetv)
2065{
2066 int i;
2067 int fi;
2068 typval_T argv[MAX_FUNC_ARGS + 1];
2069
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002070 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002071 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002072 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002073 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002074 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002075 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002076 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002077 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002078 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002079
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002080 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002081 {
2082 // base value goes last
2083 for (i = 0; i < argcount; ++i)
2084 argv[i] = argvars[i];
2085 argv[argcount] = *basetv;
2086 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002087 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002088 {
2089 // base value goes second
2090 argv[0] = argvars[0];
2091 argv[1] = *basetv;
2092 for (i = 1; i < argcount; ++i)
2093 argv[i + 1] = argvars[i];
2094 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002095 else if (global_functions[fi].f_argtype == FEARG_3)
2096 {
2097 // base value goes third
2098 argv[0] = argvars[0];
2099 argv[1] = argvars[1];
2100 argv[2] = *basetv;
2101 for (i = 2; i < argcount; ++i)
2102 argv[i + 1] = argvars[i];
2103 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002104 else if (global_functions[fi].f_argtype == FEARG_4)
2105 {
2106 // base value goes fourth
2107 argv[0] = argvars[0];
2108 argv[1] = argvars[1];
2109 argv[2] = argvars[2];
2110 argv[3] = *basetv;
2111 for (i = 3; i < argcount; ++i)
2112 argv[i + 1] = argvars[i];
2113 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002114 else
2115 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002116 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002117 argv[0] = *basetv;
2118 for (i = 0; i < argcount; ++i)
2119 argv[i + 1] = argvars[i];
2120 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002121 argv[argcount + 1].v_type = VAR_UNKNOWN;
2122
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002123 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002124 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002125}
2126
2127/*
2128 * Return TRUE for a non-zero Number and a non-empty String.
2129 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002130 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002131non_zero_arg(typval_T *argvars)
2132{
2133 return ((argvars[0].v_type == VAR_NUMBER
2134 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002135 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002136 && argvars[0].vval.v_number == VVAL_TRUE)
2137 || (argvars[0].v_type == VAR_STRING
2138 && argvars[0].vval.v_string != NULL
2139 && *argvars[0].vval.v_string != NUL));
2140}
2141
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002142#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002143/*
2144 * Get the float value of "argvars[0]" into "f".
2145 * Returns FAIL when the argument is not a Number or Float.
2146 */
2147 static int
2148get_float_arg(typval_T *argvars, float_T *f)
2149{
2150 if (argvars[0].v_type == VAR_FLOAT)
2151 {
2152 *f = argvars[0].vval.v_float;
2153 return OK;
2154 }
2155 if (argvars[0].v_type == VAR_NUMBER)
2156 {
2157 *f = (float_T)argvars[0].vval.v_number;
2158 return OK;
2159 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002160 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002161 return FAIL;
2162}
2163
2164/*
2165 * "abs(expr)" function
2166 */
2167 static void
2168f_abs(typval_T *argvars, typval_T *rettv)
2169{
2170 if (argvars[0].v_type == VAR_FLOAT)
2171 {
2172 rettv->v_type = VAR_FLOAT;
2173 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
2174 }
2175 else
2176 {
2177 varnumber_T n;
2178 int error = FALSE;
2179
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002180 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002181 if (error)
2182 rettv->vval.v_number = -1;
2183 else if (n > 0)
2184 rettv->vval.v_number = n;
2185 else
2186 rettv->vval.v_number = -n;
2187 }
2188}
2189
2190/*
2191 * "acos()" function
2192 */
2193 static void
2194f_acos(typval_T *argvars, typval_T *rettv)
2195{
2196 float_T f = 0.0;
2197
2198 rettv->v_type = VAR_FLOAT;
2199 if (get_float_arg(argvars, &f) == OK)
2200 rettv->vval.v_float = acos(f);
2201 else
2202 rettv->vval.v_float = 0.0;
2203}
2204#endif
2205
2206/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002207 * "and(expr, expr)" function
2208 */
2209 static void
2210f_and(typval_T *argvars, typval_T *rettv)
2211{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002212 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2213 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002214}
2215
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002216#ifdef FEAT_FLOAT
2217/*
2218 * "asin()" function
2219 */
2220 static void
2221f_asin(typval_T *argvars, typval_T *rettv)
2222{
2223 float_T f = 0.0;
2224
2225 rettv->v_type = VAR_FLOAT;
2226 if (get_float_arg(argvars, &f) == OK)
2227 rettv->vval.v_float = asin(f);
2228 else
2229 rettv->vval.v_float = 0.0;
2230}
2231
2232/*
2233 * "atan()" function
2234 */
2235 static void
2236f_atan(typval_T *argvars, typval_T *rettv)
2237{
2238 float_T f = 0.0;
2239
2240 rettv->v_type = VAR_FLOAT;
2241 if (get_float_arg(argvars, &f) == OK)
2242 rettv->vval.v_float = atan(f);
2243 else
2244 rettv->vval.v_float = 0.0;
2245}
2246
2247/*
2248 * "atan2()" function
2249 */
2250 static void
2251f_atan2(typval_T *argvars, typval_T *rettv)
2252{
2253 float_T fx = 0.0, fy = 0.0;
2254
2255 rettv->v_type = VAR_FLOAT;
2256 if (get_float_arg(argvars, &fx) == OK
2257 && get_float_arg(&argvars[1], &fy) == OK)
2258 rettv->vval.v_float = atan2(fx, fy);
2259 else
2260 rettv->vval.v_float = 0.0;
2261}
2262#endif
2263
2264/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002265 * "balloon_show()" function
2266 */
2267#ifdef FEAT_BEVAL
2268 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002269f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2270{
2271 rettv->v_type = VAR_STRING;
2272 if (balloonEval != NULL)
2273 {
2274 if (balloonEval->msg == NULL)
2275 rettv->vval.v_string = NULL;
2276 else
2277 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2278 }
2279}
2280
2281 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002282f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2283{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002284 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002285 {
2286 if (argvars[0].v_type == VAR_LIST
2287# ifdef FEAT_GUI
2288 && !gui.in_use
2289# endif
2290 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002291 {
2292 list_T *l = argvars[0].vval.v_list;
2293
2294 // empty list removes the balloon
2295 post_balloon(balloonEval, NULL,
2296 l == NULL || l->lv_len == 0 ? NULL : l);
2297 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002298 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002299 {
2300 char_u *mesg = tv_get_string_chk(&argvars[0]);
2301
2302 if (mesg != NULL)
2303 // empty string removes the balloon
2304 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2305 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002306 }
2307}
2308
Bram Moolenaar669a8282017-11-19 20:13:05 +01002309# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002310 static void
2311f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2312{
2313 if (rettv_list_alloc(rettv) == OK)
2314 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002315 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002316
2317 if (msg != NULL)
2318 {
2319 pumitem_T *array;
2320 int size = split_message(msg, &array);
2321 int i;
2322
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002323 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002324 for (i = 1; i < size - 1; ++i)
2325 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002326 while (size > 0)
2327 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002328 vim_free(array);
2329 }
2330 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002331}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002332# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002333#endif
2334
2335/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002336 * Get the buffer from "arg" and give an error and return NULL if it is not
2337 * valid.
2338 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002339 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002340get_buf_arg(typval_T *arg)
2341{
2342 buf_T *buf;
2343
2344 ++emsg_off;
2345 buf = tv_get_buf(arg, FALSE);
2346 --emsg_off;
2347 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002348 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002349 return buf;
2350}
2351
2352/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002353 * "byte2line(byte)" function
2354 */
2355 static void
2356f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2357{
2358#ifndef FEAT_BYTEOFF
2359 rettv->vval.v_number = -1;
2360#else
2361 long boff = 0;
2362
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002363 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002364 if (boff < 0)
2365 rettv->vval.v_number = -1;
2366 else
2367 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2368 (linenr_T)0, &boff);
2369#endif
2370}
2371
2372 static void
2373byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2374{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002375 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002376 char_u *str;
2377 varnumber_T idx;
2378
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002379 str = tv_get_string_chk(&argvars[0]);
2380 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002381 rettv->vval.v_number = -1;
2382 if (str == NULL || idx < 0)
2383 return;
2384
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002385 t = str;
2386 for ( ; idx > 0; idx--)
2387 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002388 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002389 return;
2390 if (enc_utf8 && comp)
2391 t += utf_ptr2len(t);
2392 else
2393 t += (*mb_ptr2len)(t);
2394 }
2395 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002396}
2397
2398/*
2399 * "byteidx()" function
2400 */
2401 static void
2402f_byteidx(typval_T *argvars, typval_T *rettv)
2403{
2404 byteidx(argvars, rettv, FALSE);
2405}
2406
2407/*
2408 * "byteidxcomp()" function
2409 */
2410 static void
2411f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2412{
2413 byteidx(argvars, rettv, TRUE);
2414}
2415
2416/*
2417 * "call(func, arglist [, dict])" function
2418 */
2419 static void
2420f_call(typval_T *argvars, typval_T *rettv)
2421{
2422 char_u *func;
2423 partial_T *partial = NULL;
2424 dict_T *selfdict = NULL;
2425
2426 if (argvars[1].v_type != VAR_LIST)
2427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002428 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002429 return;
2430 }
2431 if (argvars[1].vval.v_list == NULL)
2432 return;
2433
2434 if (argvars[0].v_type == VAR_FUNC)
2435 func = argvars[0].vval.v_string;
2436 else if (argvars[0].v_type == VAR_PARTIAL)
2437 {
2438 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002439 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002440 }
2441 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002442 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002443 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002444 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002445
2446 if (argvars[2].v_type != VAR_UNKNOWN)
2447 {
2448 if (argvars[2].v_type != VAR_DICT)
2449 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002450 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002451 return;
2452 }
2453 selfdict = argvars[2].vval.v_dict;
2454 }
2455
2456 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2457}
2458
2459#ifdef FEAT_FLOAT
2460/*
2461 * "ceil({float})" function
2462 */
2463 static void
2464f_ceil(typval_T *argvars, typval_T *rettv)
2465{
2466 float_T f = 0.0;
2467
2468 rettv->v_type = VAR_FLOAT;
2469 if (get_float_arg(argvars, &f) == OK)
2470 rettv->vval.v_float = ceil(f);
2471 else
2472 rettv->vval.v_float = 0.0;
2473}
2474#endif
2475
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002476/*
2477 * "changenr()" function
2478 */
2479 static void
2480f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2481{
2482 rettv->vval.v_number = curbuf->b_u_seq_cur;
2483}
2484
2485/*
2486 * "char2nr(string)" function
2487 */
2488 static void
2489f_char2nr(typval_T *argvars, typval_T *rettv)
2490{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002491 if (has_mbyte)
2492 {
2493 int utf8 = 0;
2494
2495 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002496 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002497
2498 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002499 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002500 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002501 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002502 }
2503 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002504 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002505}
2506
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002507/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002508 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2509 * returns the character index of the column. Otherwise, returns the byte index
2510 * of the column.
2511 */
2512 static void
2513get_col(typval_T *argvars, typval_T *rettv, int charcol)
2514{
2515 colnr_T col = 0;
2516 pos_T *fp;
2517 int fnum = curbuf->b_fnum;
2518
2519 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2520 if (fp != NULL && fnum == curbuf->b_fnum)
2521 {
2522 if (fp->col == MAXCOL)
2523 {
2524 // '> can be MAXCOL, get the length of the line then
2525 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2526 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2527 else
2528 col = MAXCOL;
2529 }
2530 else
2531 {
2532 col = fp->col + 1;
2533 // col(".") when the cursor is on the NUL at the end of the line
2534 // because of "coladd" can be seen as an extra column.
2535 if (virtual_active() && fp == &curwin->w_cursor)
2536 {
2537 char_u *p = ml_get_cursor();
2538
2539 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2540 curwin->w_virtcol - curwin->w_cursor.coladd))
2541 {
2542 int l;
2543
2544 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2545 col += l;
2546 }
2547 }
2548 }
2549 }
2550 rettv->vval.v_number = col;
2551}
2552
2553/*
2554 * "charcol()" function
2555 */
2556 static void
2557f_charcol(typval_T *argvars, typval_T *rettv)
2558{
2559 get_col(argvars, rettv, TRUE);
2560}
2561
2562/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002563 * "charidx()" function
2564 */
2565 static void
2566f_charidx(typval_T *argvars, typval_T *rettv)
2567{
2568 char_u *str;
2569 varnumber_T idx;
2570 int countcc = FALSE;
2571 char_u *p;
2572 int len;
2573 int (*ptr2len)(char_u *);
2574
2575 rettv->vval.v_number = -1;
2576
2577 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2578 || (argvars[2].v_type != VAR_UNKNOWN
2579 && argvars[2].v_type != VAR_NUMBER))
2580 {
2581 emsg(_(e_invarg));
2582 return;
2583 }
2584
2585 str = tv_get_string_chk(&argvars[0]);
2586 idx = tv_get_number_chk(&argvars[1], NULL);
2587 if (str == NULL || idx < 0)
2588 return;
2589
2590 if (argvars[2].v_type != VAR_UNKNOWN)
2591 countcc = (int)tv_get_bool(&argvars[2]);
2592 if (countcc < 0 || countcc > 1)
2593 {
2594 semsg(_(e_using_number_as_bool_nr), countcc);
2595 return;
2596 }
2597
2598 if (enc_utf8 && countcc)
2599 ptr2len = utf_ptr2len;
2600 else
2601 ptr2len = mb_ptr2len;
2602
2603 for (p = str, len = 0; p <= str + idx; len++)
2604 {
2605 if (*p == NUL)
2606 return;
2607 p += ptr2len(p);
2608 }
2609
2610 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2611}
2612
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002613 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002614get_optional_window(typval_T *argvars, int idx)
2615{
2616 win_T *win = curwin;
2617
2618 if (argvars[idx].v_type != VAR_UNKNOWN)
2619 {
2620 win = find_win_by_nr_or_id(&argvars[idx]);
2621 if (win == NULL)
2622 {
2623 emsg(_(e_invalwindow));
2624 return NULL;
2625 }
2626 }
2627 return win;
2628}
2629
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002630/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002631 * "col(string)" function
2632 */
2633 static void
2634f_col(typval_T *argvars, typval_T *rettv)
2635{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002636 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002637}
2638
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002639/*
2640 * "confirm(message, buttons[, default [, type]])" function
2641 */
2642 static void
2643f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2644{
2645#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2646 char_u *message;
2647 char_u *buttons = NULL;
2648 char_u buf[NUMBUFLEN];
2649 char_u buf2[NUMBUFLEN];
2650 int def = 1;
2651 int type = VIM_GENERIC;
2652 char_u *typestr;
2653 int error = FALSE;
2654
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002655 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002656 if (message == NULL)
2657 error = TRUE;
2658 if (argvars[1].v_type != VAR_UNKNOWN)
2659 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002660 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002661 if (buttons == NULL)
2662 error = TRUE;
2663 if (argvars[2].v_type != VAR_UNKNOWN)
2664 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002665 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002666 if (argvars[3].v_type != VAR_UNKNOWN)
2667 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002668 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002669 if (typestr == NULL)
2670 error = TRUE;
2671 else
2672 {
2673 switch (TOUPPER_ASC(*typestr))
2674 {
2675 case 'E': type = VIM_ERROR; break;
2676 case 'Q': type = VIM_QUESTION; break;
2677 case 'I': type = VIM_INFO; break;
2678 case 'W': type = VIM_WARNING; break;
2679 case 'G': type = VIM_GENERIC; break;
2680 }
2681 }
2682 }
2683 }
2684 }
2685
2686 if (buttons == NULL || *buttons == NUL)
2687 buttons = (char_u *)_("&Ok");
2688
2689 if (!error)
2690 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2691 def, NULL, FALSE);
2692#endif
2693}
2694
2695/*
2696 * "copy()" function
2697 */
2698 static void
2699f_copy(typval_T *argvars, typval_T *rettv)
2700{
2701 item_copy(&argvars[0], rettv, FALSE, 0);
2702}
2703
2704#ifdef FEAT_FLOAT
2705/*
2706 * "cos()" function
2707 */
2708 static void
2709f_cos(typval_T *argvars, typval_T *rettv)
2710{
2711 float_T f = 0.0;
2712
2713 rettv->v_type = VAR_FLOAT;
2714 if (get_float_arg(argvars, &f) == OK)
2715 rettv->vval.v_float = cos(f);
2716 else
2717 rettv->vval.v_float = 0.0;
2718}
2719
2720/*
2721 * "cosh()" function
2722 */
2723 static void
2724f_cosh(typval_T *argvars, typval_T *rettv)
2725{
2726 float_T f = 0.0;
2727
2728 rettv->v_type = VAR_FLOAT;
2729 if (get_float_arg(argvars, &f) == OK)
2730 rettv->vval.v_float = cosh(f);
2731 else
2732 rettv->vval.v_float = 0.0;
2733}
2734#endif
2735
2736/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002737 * Set the cursor position.
2738 * If 'charcol' is TRUE, then use the column number as a character offet.
2739 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002740 */
2741 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002742set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002743{
2744 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002745 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002746 int set_curswant = TRUE;
2747
2748 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002749 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002750 {
2751 pos_T pos;
2752 colnr_T curswant = -1;
2753
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002754 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002755 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002756 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002757 return;
2758 }
2759 line = pos.lnum;
2760 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002761 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002762 if (curswant >= 0)
2763 {
2764 curwin->w_curswant = curswant - 1;
2765 set_curswant = FALSE;
2766 }
2767 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002768 else if ((argvars[0].v_type == VAR_NUMBER ||
2769 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002770 && (argvars[1].v_type == VAR_NUMBER ||
2771 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002772 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002773 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002774 if (line < 0)
2775 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002776 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002777 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002778 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002779 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002780 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002781 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002782 else
2783 {
2784 emsg(_(e_invarg));
2785 return;
2786 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002787 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002788 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002789 if (line > 0)
2790 curwin->w_cursor.lnum = line;
2791 if (col > 0)
2792 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002793 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002794
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002795 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002796 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002797 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002798 if (has_mbyte)
2799 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002800
2801 curwin->w_set_curswant = set_curswant;
2802 rettv->vval.v_number = 0;
2803}
2804
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002805/*
2806 * "cursor(lnum, col)" function, or
2807 * "cursor(list)"
2808 *
2809 * Moves the cursor to the specified line and column.
2810 * Returns 0 when the position could be set, -1 otherwise.
2811 */
2812 static void
2813f_cursor(typval_T *argvars, typval_T *rettv)
2814{
2815 set_cursorpos(argvars, rettv, FALSE);
2816}
2817
Bram Moolenaar4f974752019-02-17 17:44:42 +01002818#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002819/*
2820 * "debugbreak()" function
2821 */
2822 static void
2823f_debugbreak(typval_T *argvars, typval_T *rettv)
2824{
2825 int pid;
2826
2827 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002828 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002829 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002830 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002831 else
2832 {
2833 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2834
2835 if (hProcess != NULL)
2836 {
2837 DebugBreakProcess(hProcess);
2838 CloseHandle(hProcess);
2839 rettv->vval.v_number = OK;
2840 }
2841 }
2842}
2843#endif
2844
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002845/*
2846 * "deepcopy()" function
2847 */
2848 static void
2849f_deepcopy(typval_T *argvars, typval_T *rettv)
2850{
2851 int noref = 0;
2852 int copyID;
2853
2854 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar44b4a242020-09-05 17:18:28 +02002855 noref = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002856 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002857 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002858 else
2859 {
2860 copyID = get_copyID();
2861 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2862 }
2863}
2864
2865/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002866 * "did_filetype()" function
2867 */
2868 static void
2869f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2870{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002871 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002872}
2873
2874/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002875 * "echoraw({expr})" function
2876 */
2877 static void
2878f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2879{
2880 char_u *str = tv_get_string_chk(&argvars[0]);
2881
2882 if (str != NULL && *str != NUL)
2883 {
2884 out_str(str);
2885 out_flush();
2886 }
2887}
2888
2889/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002890 * "empty({expr})" function
2891 */
2892 static void
2893f_empty(typval_T *argvars, typval_T *rettv)
2894{
2895 int n = FALSE;
2896
2897 switch (argvars[0].v_type)
2898 {
2899 case VAR_STRING:
2900 case VAR_FUNC:
2901 n = argvars[0].vval.v_string == NULL
2902 || *argvars[0].vval.v_string == NUL;
2903 break;
2904 case VAR_PARTIAL:
2905 n = FALSE;
2906 break;
2907 case VAR_NUMBER:
2908 n = argvars[0].vval.v_number == 0;
2909 break;
2910 case VAR_FLOAT:
2911#ifdef FEAT_FLOAT
2912 n = argvars[0].vval.v_float == 0.0;
2913 break;
2914#endif
2915 case VAR_LIST:
2916 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002917 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002918 break;
2919 case VAR_DICT:
2920 n = argvars[0].vval.v_dict == NULL
2921 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2922 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002923 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002924 case VAR_SPECIAL:
2925 n = argvars[0].vval.v_number != VVAL_TRUE;
2926 break;
2927
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002928 case VAR_BLOB:
2929 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002930 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2931 break;
2932
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002933 case VAR_JOB:
2934#ifdef FEAT_JOB_CHANNEL
2935 n = argvars[0].vval.v_job == NULL
2936 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2937 break;
2938#endif
2939 case VAR_CHANNEL:
2940#ifdef FEAT_JOB_CHANNEL
2941 n = argvars[0].vval.v_channel == NULL
2942 || !channel_is_open(argvars[0].vval.v_channel);
2943 break;
2944#endif
2945 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002946 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002948 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002949 n = TRUE;
2950 break;
2951 }
2952
2953 rettv->vval.v_number = n;
2954}
2955
2956/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002957 * "environ()" function
2958 */
2959 static void
2960f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2961{
2962#if !defined(AMIGA)
2963 int i = 0;
2964 char_u *entry, *value;
2965# ifdef MSWIN
2966 extern wchar_t **_wenviron;
2967# else
2968 extern char **environ;
2969# endif
2970
2971 if (rettv_dict_alloc(rettv) != OK)
2972 return;
2973
2974# ifdef MSWIN
2975 if (*_wenviron == NULL)
2976 return;
2977# else
2978 if (*environ == NULL)
2979 return;
2980# endif
2981
2982 for (i = 0; ; ++i)
2983 {
2984# ifdef MSWIN
2985 short_u *p;
2986
2987 if ((p = (short_u *)_wenviron[i]) == NULL)
2988 return;
2989 entry = utf16_to_enc(p, NULL);
2990# else
2991 if ((entry = (char_u *)environ[i]) == NULL)
2992 return;
2993 entry = vim_strsave(entry);
2994# endif
2995 if (entry == NULL) // out of memory
2996 return;
2997 if ((value = vim_strchr(entry, '=')) == NULL)
2998 {
2999 vim_free(entry);
3000 continue;
3001 }
3002 *value++ = NUL;
3003 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3004 vim_free(entry);
3005 }
3006#endif
3007}
3008
3009/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003010 * "escape({string}, {chars})" function
3011 */
3012 static void
3013f_escape(typval_T *argvars, typval_T *rettv)
3014{
3015 char_u buf[NUMBUFLEN];
3016
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003017 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3018 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003019 rettv->v_type = VAR_STRING;
3020}
3021
3022/*
3023 * "eval()" function
3024 */
3025 static void
3026f_eval(typval_T *argvars, typval_T *rettv)
3027{
3028 char_u *s, *p;
3029
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003030 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003031 if (s != NULL)
3032 s = skipwhite(s);
3033
3034 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003035 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003036 {
3037 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003038 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003039 need_clr_eos = FALSE;
3040 rettv->v_type = VAR_NUMBER;
3041 rettv->vval.v_number = 0;
3042 }
3043 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003044 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003045}
3046
3047/*
3048 * "eventhandler()" function
3049 */
3050 static void
3051f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3052{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003053 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003054}
3055
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003056static garray_T redir_execute_ga;
3057
3058/*
3059 * Append "value[value_len]" to the execute() output.
3060 */
3061 void
3062execute_redir_str(char_u *value, int value_len)
3063{
3064 int len;
3065
3066 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003067 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003068 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003069 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003070 if (ga_grow(&redir_execute_ga, len) == OK)
3071 {
3072 mch_memmove((char *)redir_execute_ga.ga_data
3073 + redir_execute_ga.ga_len, value, len);
3074 redir_execute_ga.ga_len += len;
3075 }
3076}
3077
3078/*
3079 * Get next line from a list.
3080 * Called by do_cmdline() to get the next line.
3081 * Returns allocated string, or NULL for end of function.
3082 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003083 static char_u *
3084get_list_line(
3085 int c UNUSED,
3086 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003087 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02003088 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003089{
3090 listitem_T **p = (listitem_T **)cookie;
3091 listitem_T *item = *p;
3092 char_u buf[NUMBUFLEN];
3093 char_u *s;
3094
3095 if (item == NULL)
3096 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003097 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003098 *p = item->li_next;
3099 return s == NULL ? NULL : vim_strsave(s);
3100}
3101
3102/*
3103 * "execute()" function
3104 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02003105 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003106execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003107{
3108 char_u *cmd = NULL;
3109 list_T *list = NULL;
3110 int save_msg_silent = msg_silent;
3111 int save_emsg_silent = emsg_silent;
3112 int save_emsg_noredir = emsg_noredir;
3113 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003114 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003115 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003116 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003117 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003118
3119 rettv->vval.v_string = NULL;
3120 rettv->v_type = VAR_STRING;
3121
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003122 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003123 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003124 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003125 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003126 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003127 return;
3128 ++list->lv_refcount;
3129 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003130 else if (argvars[arg_off].v_type == VAR_JOB
3131 || argvars[arg_off].v_type == VAR_CHANNEL)
3132 {
3133 emsg(_(e_inval_string));
3134 return;
3135 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003136 else
3137 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003138 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003139 if (cmd == NULL)
3140 return;
3141 }
3142
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003143 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003144 {
3145 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003146 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003147
3148 if (s == NULL)
3149 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003150 if (*s == NUL)
3151 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003152 if (STRNCMP(s, "silent", 6) == 0)
3153 ++msg_silent;
3154 if (STRCMP(s, "silent!") == 0)
3155 {
3156 emsg_silent = TRUE;
3157 emsg_noredir = TRUE;
3158 }
3159 }
3160 else
3161 ++msg_silent;
3162
3163 if (redir_execute)
3164 save_ga = redir_execute_ga;
3165 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3166 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003167 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003168 if (!echo_output)
3169 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003170
3171 if (cmd != NULL)
3172 do_cmdline_cmd(cmd);
3173 else
3174 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003175 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003176
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003177 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003178 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003179 do_cmdline(NULL, get_list_line, (void *)&item,
3180 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3181 --list->lv_refcount;
3182 }
3183
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003184 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003185 if (ga_grow(&redir_execute_ga, 1) == OK)
3186 {
3187 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3188 rettv->vval.v_string = redir_execute_ga.ga_data;
3189 }
3190 else
3191 {
3192 ga_clear(&redir_execute_ga);
3193 rettv->vval.v_string = NULL;
3194 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003195 msg_silent = save_msg_silent;
3196 emsg_silent = save_emsg_silent;
3197 emsg_noredir = save_emsg_noredir;
3198
3199 redir_execute = save_redir_execute;
3200 if (redir_execute)
3201 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003202 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003203
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003204 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003205 if (echo_output)
3206 // When not working silently: put it in column zero. A following
3207 // "echon" will overwrite the message, unavoidably.
3208 msg_col = 0;
3209 else
3210 // When working silently: Put it back where it was, since nothing
3211 // should have been written.
3212 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003213}
3214
3215/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003216 * "execute()" function
3217 */
3218 static void
3219f_execute(typval_T *argvars, typval_T *rettv)
3220{
3221 execute_common(argvars, rettv, 0);
3222}
3223
3224/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003225 * "exists()" function
3226 */
3227 static void
3228f_exists(typval_T *argvars, typval_T *rettv)
3229{
3230 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003231 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003232
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003233 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003234 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003235 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003236 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003237 if (mch_getenv(p + 1) != NULL)
3238 n = TRUE;
3239 else
3240 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003241 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003242 p = expand_env_save(p);
3243 if (p != NULL && *p != '$')
3244 n = TRUE;
3245 vim_free(p);
3246 }
3247 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003248 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003249 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003250 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003251 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003252 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003253 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003254 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003255 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003256 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003257 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003258 else if (*p == '?') // internal function only
3259 {
3260 n = has_internal_func_name(p + 1);
3261 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003262 else if (*p == ':')
3263 {
3264 n = cmd_exists(p + 1);
3265 }
3266 else if (*p == '#')
3267 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003268 if (p[1] == '#')
3269 n = autocmd_supported(p + 2);
3270 else
3271 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003272 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003273 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003274 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003275 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003276 }
3277
3278 rettv->vval.v_number = n;
3279}
3280
3281#ifdef FEAT_FLOAT
3282/*
3283 * "exp()" function
3284 */
3285 static void
3286f_exp(typval_T *argvars, typval_T *rettv)
3287{
3288 float_T f = 0.0;
3289
3290 rettv->v_type = VAR_FLOAT;
3291 if (get_float_arg(argvars, &f) == OK)
3292 rettv->vval.v_float = exp(f);
3293 else
3294 rettv->vval.v_float = 0.0;
3295}
3296#endif
3297
3298/*
3299 * "expand()" function
3300 */
3301 static void
3302f_expand(typval_T *argvars, typval_T *rettv)
3303{
3304 char_u *s;
3305 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003306 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003307 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3308 expand_T xpc;
3309 int error = FALSE;
3310 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003311#ifdef BACKSLASH_IN_FILENAME
3312 char_u *p_csl_save = p_csl;
3313
3314 // avoid using 'completeslash' here
3315 p_csl = empty_option;
3316#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003317
3318 rettv->v_type = VAR_STRING;
3319 if (argvars[1].v_type != VAR_UNKNOWN
3320 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003321 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003322 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003323 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003324
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003325 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003326 if (*s == '%' || *s == '#' || *s == '<')
3327 {
3328 ++emsg_off;
3329 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3330 --emsg_off;
3331 if (rettv->v_type == VAR_LIST)
3332 {
3333 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3334 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003335 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003336 }
3337 else
3338 rettv->vval.v_string = result;
3339 }
3340 else
3341 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003342 // When the optional second argument is non-zero, don't remove matches
3343 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003344 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003345 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003346 options |= WILD_KEEP_ALL;
3347 if (!error)
3348 {
3349 ExpandInit(&xpc);
3350 xpc.xp_context = EXPAND_FILES;
3351 if (p_wic)
3352 options += WILD_ICASE;
3353 if (rettv->v_type == VAR_STRING)
3354 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3355 options, WILD_ALL);
3356 else if (rettv_list_alloc(rettv) != FAIL)
3357 {
3358 int i;
3359
3360 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3361 for (i = 0; i < xpc.xp_numfiles; i++)
3362 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3363 ExpandCleanup(&xpc);
3364 }
3365 }
3366 else
3367 rettv->vval.v_string = NULL;
3368 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003369#ifdef BACKSLASH_IN_FILENAME
3370 p_csl = p_csl_save;
3371#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003372}
3373
3374/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003375 * "expandcmd()" function
3376 * Expand all the special characters in a command string.
3377 */
3378 static void
3379f_expandcmd(typval_T *argvars, typval_T *rettv)
3380{
3381 exarg_T eap;
3382 char_u *cmdstr;
3383 char *errormsg = NULL;
3384
3385 rettv->v_type = VAR_STRING;
3386 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3387
3388 memset(&eap, 0, sizeof(eap));
3389 eap.cmd = cmdstr;
3390 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003391 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003392 eap.usefilter = FALSE;
3393 eap.nextcmd = NULL;
3394 eap.cmdidx = CMD_USER;
3395
3396 expand_filename(&eap, &cmdstr, &errormsg);
3397 if (errormsg != NULL && *errormsg != NUL)
3398 emsg(errormsg);
3399
3400 rettv->vval.v_string = cmdstr;
3401}
3402
3403/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003404 * "feedkeys()" function
3405 */
3406 static void
3407f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3408{
3409 int remap = TRUE;
3410 int insert = FALSE;
3411 char_u *keys, *flags;
3412 char_u nbuf[NUMBUFLEN];
3413 int typed = FALSE;
3414 int execute = FALSE;
3415 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003416 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003417 char_u *keys_esc;
3418
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003419 // This is not allowed in the sandbox. If the commands would still be
3420 // executed in the sandbox it would be OK, but it probably happens later,
3421 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003422 if (check_secure())
3423 return;
3424
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003425 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003426
3427 if (argvars[1].v_type != VAR_UNKNOWN)
3428 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003429 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003430 for ( ; *flags != NUL; ++flags)
3431 {
3432 switch (*flags)
3433 {
3434 case 'n': remap = FALSE; break;
3435 case 'm': remap = TRUE; break;
3436 case 't': typed = TRUE; break;
3437 case 'i': insert = TRUE; break;
3438 case 'x': execute = TRUE; break;
3439 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003440 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003441 }
3442 }
3443 }
3444
3445 if (*keys != NUL || execute)
3446 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003447 // Need to escape K_SPECIAL and CSI before putting the string in the
3448 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003449 keys_esc = vim_strsave_escape_csi(keys);
3450 if (keys_esc != NULL)
3451 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003452 if (lowlevel)
3453 {
3454#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003455 int idx;
3456 int len = (int)STRLEN(keys);
3457
3458 for (idx = 0; idx < len; ++idx)
3459 {
3460 // if a CTRL-C was typed, set got_int, similar to what
3461 // happens in fill_input_buf()
3462 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3463 got_int = TRUE;
3464 add_to_input_buf(keys + idx, 1);
3465 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003466#else
3467 emsg(_("E980: lowlevel input not supported"));
3468#endif
3469 }
3470 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003471 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003472 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003473 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003474 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003475#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003476 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003477#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003478 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003479 typebuf_was_filled = TRUE;
3480 }
3481 vim_free(keys_esc);
3482
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003483 if (execute)
3484 {
3485 int save_msg_scroll = msg_scroll;
3486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003487 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003488 msg_scroll = FALSE;
3489
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003490 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003491 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003492 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003493 ++in_feedkeys;
3494 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003495 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003496 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003497 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003498 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003499 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003500 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003501
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003502 msg_scroll |= save_msg_scroll;
3503 }
3504 }
3505 }
3506}
3507
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003508#ifdef FEAT_FLOAT
3509/*
3510 * "float2nr({float})" function
3511 */
3512 static void
3513f_float2nr(typval_T *argvars, typval_T *rettv)
3514{
3515 float_T f = 0.0;
3516
3517 if (get_float_arg(argvars, &f) == OK)
3518 {
Bram Moolenaar37184272020-05-23 19:30:05 +02003519 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003520 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02003521 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003522 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003523 else
3524 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003525 }
3526}
3527
3528/*
3529 * "floor({float})" function
3530 */
3531 static void
3532f_floor(typval_T *argvars, typval_T *rettv)
3533{
3534 float_T f = 0.0;
3535
3536 rettv->v_type = VAR_FLOAT;
3537 if (get_float_arg(argvars, &f) == OK)
3538 rettv->vval.v_float = floor(f);
3539 else
3540 rettv->vval.v_float = 0.0;
3541}
3542
3543/*
3544 * "fmod()" function
3545 */
3546 static void
3547f_fmod(typval_T *argvars, typval_T *rettv)
3548{
3549 float_T fx = 0.0, fy = 0.0;
3550
3551 rettv->v_type = VAR_FLOAT;
3552 if (get_float_arg(argvars, &fx) == OK
3553 && get_float_arg(&argvars[1], &fy) == OK)
3554 rettv->vval.v_float = fmod(fx, fy);
3555 else
3556 rettv->vval.v_float = 0.0;
3557}
3558#endif
3559
3560/*
3561 * "fnameescape({string})" function
3562 */
3563 static void
3564f_fnameescape(typval_T *argvars, typval_T *rettv)
3565{
3566 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003567 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003568 rettv->v_type = VAR_STRING;
3569}
3570
3571/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003572 * "foreground()" function
3573 */
3574 static void
3575f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3576{
3577#ifdef FEAT_GUI
3578 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003579 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003580 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003581 return;
3582 }
3583#endif
3584#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003585 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003586#endif
3587}
3588
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003589 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003590common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003591{
3592 char_u *s;
3593 char_u *name;
3594 int use_string = FALSE;
3595 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003596 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003597 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003598
3599 if (argvars[0].v_type == VAR_FUNC)
3600 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003601 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003602 s = argvars[0].vval.v_string;
3603 }
3604 else if (argvars[0].v_type == VAR_PARTIAL
3605 && argvars[0].vval.v_partial != NULL)
3606 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003607 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003608 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003609 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003610 }
3611 else
3612 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003613 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003614 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003615 use_string = TRUE;
3616 }
3617
Bram Moolenaar843b8842016-08-21 14:36:15 +02003618 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003619 {
3620 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003621 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003622 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3623 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003624 if (*name != NUL)
3625 s = NULL;
3626 }
3627
Bram Moolenaar843b8842016-08-21 14:36:15 +02003628 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3629 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003630 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003631 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003632 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003633 ? find_func(trans_name, is_global, NULL) == NULL
3634 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003635 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003636 else
3637 {
3638 int dict_idx = 0;
3639 int arg_idx = 0;
3640 list_T *list = NULL;
3641
3642 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3643 {
3644 char sid_buf[25];
3645 int off = *s == 's' ? 2 : 5;
3646
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003647 // Expand s: and <SID> into <SNR>nr_, so that the function can
3648 // also be called from another script. Using trans_function_name()
3649 // would also work, but some plugins depend on the name being
3650 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003651 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003652 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003653 if (name != NULL)
3654 {
3655 STRCPY(name, sid_buf);
3656 STRCAT(name, s + off);
3657 }
3658 }
3659 else
3660 name = vim_strsave(s);
3661
3662 if (argvars[1].v_type != VAR_UNKNOWN)
3663 {
3664 if (argvars[2].v_type != VAR_UNKNOWN)
3665 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003666 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003667 arg_idx = 1;
3668 dict_idx = 2;
3669 }
3670 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003671 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003672 dict_idx = 1;
3673 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003674 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003675 arg_idx = 1;
3676 if (dict_idx > 0)
3677 {
3678 if (argvars[dict_idx].v_type != VAR_DICT)
3679 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003680 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003681 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003682 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003683 }
3684 if (argvars[dict_idx].vval.v_dict == NULL)
3685 dict_idx = 0;
3686 }
3687 if (arg_idx > 0)
3688 {
3689 if (argvars[arg_idx].v_type != VAR_LIST)
3690 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003691 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003692 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003693 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003694 }
3695 list = argvars[arg_idx].vval.v_list;
3696 if (list == NULL || list->lv_len == 0)
3697 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003698 else if (list->lv_len > MAX_FUNC_ARGS)
3699 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003700 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003701 vim_free(name);
3702 goto theend;
3703 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003704 }
3705 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003706 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003707 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003708 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003709
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003710 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003711 if (pt == NULL)
3712 vim_free(name);
3713 else
3714 {
3715 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3716 {
3717 listitem_T *li;
3718 int i = 0;
3719 int arg_len = 0;
3720 int lv_len = 0;
3721
3722 if (arg_pt != NULL)
3723 arg_len = arg_pt->pt_argc;
3724 if (list != NULL)
3725 lv_len = list->lv_len;
3726 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003727 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003728 if (pt->pt_argv == NULL)
3729 {
3730 vim_free(pt);
3731 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003732 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003733 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003734 for (i = 0; i < arg_len; i++)
3735 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3736 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003737 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003738 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003739 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003740 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003741 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003742 }
3743
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003744 // For "function(dict.func, [], dict)" and "func" is a partial
3745 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003746 if (dict_idx > 0)
3747 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003748 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003749 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3750 ++pt->pt_dict->dv_refcount;
3751 }
3752 else if (arg_pt != NULL)
3753 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003754 // If the dict was bound automatically the result is also
3755 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003756 pt->pt_dict = arg_pt->pt_dict;
3757 pt->pt_auto = arg_pt->pt_auto;
3758 if (pt->pt_dict != NULL)
3759 ++pt->pt_dict->dv_refcount;
3760 }
3761
3762 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003763 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3764 {
3765 pt->pt_func = arg_pt->pt_func;
3766 func_ptr_ref(pt->pt_func);
3767 vim_free(name);
3768 }
3769 else if (is_funcref)
3770 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003771 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003772 func_ptr_ref(pt->pt_func);
3773 vim_free(name);
3774 }
3775 else
3776 {
3777 pt->pt_name = name;
3778 func_ref(name);
3779 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003780 }
3781 rettv->v_type = VAR_PARTIAL;
3782 rettv->vval.v_partial = pt;
3783 }
3784 else
3785 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003786 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003787 rettv->v_type = VAR_FUNC;
3788 rettv->vval.v_string = name;
3789 func_ref(name);
3790 }
3791 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003792theend:
3793 vim_free(trans_name);
3794}
3795
3796/*
3797 * "funcref()" function
3798 */
3799 static void
3800f_funcref(typval_T *argvars, typval_T *rettv)
3801{
3802 common_function(argvars, rettv, TRUE);
3803}
3804
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003805 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003806ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003807{
3808 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3809 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003810 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003811}
3812
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003813/*
3814 * "function()" function
3815 */
3816 static void
3817f_function(typval_T *argvars, typval_T *rettv)
3818{
3819 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003820}
3821
3822/*
3823 * "garbagecollect()" function
3824 */
3825 static void
3826f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3827{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003828 // This is postponed until we are back at the toplevel, because we may be
3829 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003830 want_garbage_collect = TRUE;
3831
Bram Moolenaar2df47312020-09-05 17:30:44 +02003832 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003833 garbage_collect_at_exit = TRUE;
3834}
3835
3836/*
3837 * "get()" function
3838 */
3839 static void
3840f_get(typval_T *argvars, typval_T *rettv)
3841{
3842 listitem_T *li;
3843 list_T *l;
3844 dictitem_T *di;
3845 dict_T *d;
3846 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003847 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003848
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003849 if (argvars[0].v_type == VAR_BLOB)
3850 {
3851 int error = FALSE;
3852 int idx = tv_get_number_chk(&argvars[1], &error);
3853
3854 if (!error)
3855 {
3856 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003857 if (idx < 0)
3858 idx = blob_len(argvars[0].vval.v_blob) + idx;
3859 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3860 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003861 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003862 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003863 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003864 tv = rettv;
3865 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003866 }
3867 }
3868 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003869 {
3870 if ((l = argvars[0].vval.v_list) != NULL)
3871 {
3872 int error = FALSE;
3873
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003874 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003875 if (!error && li != NULL)
3876 tv = &li->li_tv;
3877 }
3878 }
3879 else if (argvars[0].v_type == VAR_DICT)
3880 {
3881 if ((d = argvars[0].vval.v_dict) != NULL)
3882 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003883 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003884 if (di != NULL)
3885 tv = &di->di_tv;
3886 }
3887 }
3888 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3889 {
3890 partial_T *pt;
3891 partial_T fref_pt;
3892
3893 if (argvars[0].v_type == VAR_PARTIAL)
3894 pt = argvars[0].vval.v_partial;
3895 else
3896 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003897 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003898 fref_pt.pt_name = argvars[0].vval.v_string;
3899 pt = &fref_pt;
3900 }
3901
3902 if (pt != NULL)
3903 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003904 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003905 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003906
3907 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3908 {
3909 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003910 n = partial_name(pt);
3911 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003912 rettv->vval.v_string = NULL;
3913 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003914 {
3915 rettv->vval.v_string = vim_strsave(n);
3916 if (rettv->v_type == VAR_FUNC)
3917 func_ref(rettv->vval.v_string);
3918 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003919 }
3920 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003921 {
3922 what_is_dict = TRUE;
3923 if (pt->pt_dict != NULL)
3924 rettv_dict_set(rettv, pt->pt_dict);
3925 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003926 else if (STRCMP(what, "args") == 0)
3927 {
3928 rettv->v_type = VAR_LIST;
3929 if (rettv_list_alloc(rettv) == OK)
3930 {
3931 int i;
3932
3933 for (i = 0; i < pt->pt_argc; ++i)
3934 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3935 }
3936 }
3937 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003938 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003939
3940 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3941 // third argument
3942 if (!what_is_dict)
3943 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003944 }
3945 }
3946 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003947 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003948
3949 if (tv == NULL)
3950 {
3951 if (argvars[2].v_type != VAR_UNKNOWN)
3952 copy_tv(&argvars[2], rettv);
3953 }
3954 else
3955 copy_tv(tv, rettv);
3956}
3957
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003958/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003959 * "getchangelist()" function
3960 */
3961 static void
3962f_getchangelist(typval_T *argvars, typval_T *rettv)
3963{
3964#ifdef FEAT_JUMPLIST
3965 buf_T *buf;
3966 int i;
3967 list_T *l;
3968 dict_T *d;
3969#endif
3970
3971 if (rettv_list_alloc(rettv) != OK)
3972 return;
3973
3974#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003975 if (argvars[0].v_type == VAR_UNKNOWN)
3976 buf = curbuf;
3977 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003978 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003979 if (buf == NULL)
3980 return;
3981
3982 l = list_alloc();
3983 if (l == NULL)
3984 return;
3985
3986 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3987 return;
3988 /*
3989 * The current window change list index tracks only the position in the
3990 * current buffer change list. For other buffers, use the change list
3991 * length as the current index.
3992 */
3993 list_append_number(rettv->vval.v_list,
3994 (varnumber_T)((buf == curwin->w_buffer)
3995 ? curwin->w_changelistidx : buf->b_changelistlen));
3996
3997 for (i = 0; i < buf->b_changelistlen; ++i)
3998 {
3999 if (buf->b_changelist[i].lnum == 0)
4000 continue;
4001 if ((d = dict_alloc()) == NULL)
4002 return;
4003 if (list_append_dict(l, d) == FAIL)
4004 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004005 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4006 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004007 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004008 }
4009#endif
4010}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004011
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004012 static void
4013getpos_both(
4014 typval_T *argvars,
4015 typval_T *rettv,
4016 int getcurpos,
4017 int charcol)
4018{
4019 pos_T *fp = NULL;
4020 pos_T pos;
4021 win_T *wp = curwin;
4022 list_T *l;
4023 int fnum = -1;
4024
4025 if (rettv_list_alloc(rettv) == OK)
4026 {
4027 l = rettv->vval.v_list;
4028 if (getcurpos)
4029 {
4030 if (argvars[0].v_type != VAR_UNKNOWN)
4031 {
4032 wp = find_win_by_nr_or_id(&argvars[0]);
4033 if (wp != NULL)
4034 fp = &wp->w_cursor;
4035 }
4036 else
4037 fp = &curwin->w_cursor;
4038 if (fp != NULL && charcol)
4039 {
4040 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01004041 pos.col =
4042 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004043 fp = &pos;
4044 }
4045 }
4046 else
4047 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
4048 if (fnum != -1)
4049 list_append_number(l, (varnumber_T)fnum);
4050 else
4051 list_append_number(l, (varnumber_T)0);
4052 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4053 : (varnumber_T)0);
4054 list_append_number(l, (fp != NULL)
4055 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4056 : (varnumber_T)0);
4057 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
4058 (varnumber_T)0);
4059 if (getcurpos)
4060 {
4061 int save_set_curswant = curwin->w_set_curswant;
4062 colnr_T save_curswant = curwin->w_curswant;
4063 colnr_T save_virtcol = curwin->w_virtcol;
4064
4065 if (wp == curwin)
4066 update_curswant();
4067 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
4068 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
4069
4070 // Do not change "curswant", as it is unexpected that a get
4071 // function has a side effect.
4072 if (wp == curwin && save_set_curswant)
4073 {
4074 curwin->w_set_curswant = save_set_curswant;
4075 curwin->w_curswant = save_curswant;
4076 curwin->w_virtcol = save_virtcol;
4077 curwin->w_valid &= ~VALID_VIRTCOL;
4078 }
4079 }
4080 }
4081 else
4082 rettv->vval.v_number = FALSE;
4083}
4084
4085/*
4086 * "getcharpos()" function
4087 */
4088 static void
4089f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
4090{
4091 getpos_both(argvars, rettv, FALSE, TRUE);
4092}
4093
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004094/*
4095 * "getcharsearch()" function
4096 */
4097 static void
4098f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4099{
4100 if (rettv_dict_alloc(rettv) != FAIL)
4101 {
4102 dict_T *dict = rettv->vval.v_dict;
4103
Bram Moolenaare0be1672018-07-08 16:50:37 +02004104 dict_add_string(dict, "char", last_csearch());
4105 dict_add_number(dict, "forward", last_csearch_forward());
4106 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004107 }
4108}
4109
4110/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004111 * "getenv()" function
4112 */
4113 static void
4114f_getenv(typval_T *argvars, typval_T *rettv)
4115{
4116 int mustfree = FALSE;
4117 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4118
4119 if (p == NULL)
4120 {
4121 rettv->v_type = VAR_SPECIAL;
4122 rettv->vval.v_number = VVAL_NULL;
4123 return;
4124 }
4125 if (!mustfree)
4126 p = vim_strsave(p);
4127 rettv->vval.v_string = p;
4128 rettv->v_type = VAR_STRING;
4129}
4130
4131/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004132 * "getfontname()" function
4133 */
4134 static void
4135f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4136{
4137 rettv->v_type = VAR_STRING;
4138 rettv->vval.v_string = NULL;
4139#ifdef FEAT_GUI
4140 if (gui.in_use)
4141 {
4142 GuiFont font;
4143 char_u *name = NULL;
4144
4145 if (argvars[0].v_type == VAR_UNKNOWN)
4146 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004147 // Get the "Normal" font. Either the name saved by
4148 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004149 font = gui.norm_font;
4150 name = hl_get_font_name();
4151 }
4152 else
4153 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004154 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004155 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004156 return;
4157 font = gui_mch_get_font(name, FALSE);
4158 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004159 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004160 }
4161 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4162 if (argvars[0].v_type != VAR_UNKNOWN)
4163 gui_mch_free_font(font);
4164 }
4165#endif
4166}
4167
4168/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004169 * "getjumplist()" function
4170 */
4171 static void
4172f_getjumplist(typval_T *argvars, typval_T *rettv)
4173{
4174#ifdef FEAT_JUMPLIST
4175 win_T *wp;
4176 int i;
4177 list_T *l;
4178 dict_T *d;
4179#endif
4180
4181 if (rettv_list_alloc(rettv) != OK)
4182 return;
4183
4184#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004185 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004186 if (wp == NULL)
4187 return;
4188
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004189 cleanup_jumplist(wp, TRUE);
4190
Bram Moolenaar4f505882018-02-10 21:06:32 +01004191 l = list_alloc();
4192 if (l == NULL)
4193 return;
4194
4195 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4196 return;
4197 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4198
4199 for (i = 0; i < wp->w_jumplistlen; ++i)
4200 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004201 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4202 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004203 if ((d = dict_alloc()) == NULL)
4204 return;
4205 if (list_append_dict(l, d) == FAIL)
4206 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004207 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4208 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004209 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004210 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004211 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004212 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004213 }
4214#endif
4215}
4216
4217/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004218 * "getpid()" function
4219 */
4220 static void
4221f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4222{
4223 rettv->vval.v_number = mch_get_pid();
4224}
4225
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004226/*
4227 * "getcurpos()" function
4228 */
4229 static void
4230f_getcurpos(typval_T *argvars, typval_T *rettv)
4231{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004232 getpos_both(argvars, rettv, TRUE, FALSE);
4233}
4234
4235 static void
4236f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4237{
4238 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004239}
4240
4241/*
4242 * "getpos(string)" function
4243 */
4244 static void
4245f_getpos(typval_T *argvars, typval_T *rettv)
4246{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004247 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004248}
4249
4250/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004251 * "getreg()" function
4252 */
4253 static void
4254f_getreg(typval_T *argvars, typval_T *rettv)
4255{
4256 char_u *strregname;
4257 int regname;
4258 int arg2 = FALSE;
4259 int return_list = FALSE;
4260 int error = FALSE;
4261
4262 if (argvars[0].v_type != VAR_UNKNOWN)
4263 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004264 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004265 error = strregname == NULL;
4266 if (argvars[1].v_type != VAR_UNKNOWN)
4267 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004268 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004269 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004270 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004271 }
4272 }
4273 else
4274 strregname = get_vim_var_str(VV_REG);
4275
4276 if (error)
4277 return;
4278
4279 regname = (strregname == NULL ? '"' : *strregname);
4280 if (regname == 0)
4281 regname = '"';
4282
4283 if (return_list)
4284 {
4285 rettv->v_type = VAR_LIST;
4286 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4287 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4288 if (rettv->vval.v_list == NULL)
4289 (void)rettv_list_alloc(rettv);
4290 else
4291 ++rettv->vval.v_list->lv_refcount;
4292 }
4293 else
4294 {
4295 rettv->v_type = VAR_STRING;
4296 rettv->vval.v_string = get_reg_contents(regname,
4297 arg2 ? GREG_EXPR_SRC : 0);
4298 }
4299}
4300
4301/*
4302 * "getregtype()" function
4303 */
4304 static void
4305f_getregtype(typval_T *argvars, typval_T *rettv)
4306{
4307 char_u *strregname;
4308 int regname;
4309 char_u buf[NUMBUFLEN + 2];
4310 long reglen = 0;
4311
4312 if (argvars[0].v_type != VAR_UNKNOWN)
4313 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004314 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004315 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004316 {
4317 rettv->v_type = VAR_STRING;
4318 rettv->vval.v_string = NULL;
4319 return;
4320 }
4321 }
4322 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004323 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004324 strregname = get_vim_var_str(VV_REG);
4325
4326 regname = (strregname == NULL ? '"' : *strregname);
4327 if (regname == 0)
4328 regname = '"';
4329
4330 buf[0] = NUL;
4331 buf[1] = NUL;
4332 switch (get_reg_type(regname, &reglen))
4333 {
4334 case MLINE: buf[0] = 'V'; break;
4335 case MCHAR: buf[0] = 'v'; break;
4336 case MBLOCK:
4337 buf[0] = Ctrl_V;
4338 sprintf((char *)buf + 1, "%ld", reglen + 1);
4339 break;
4340 }
4341 rettv->v_type = VAR_STRING;
4342 rettv->vval.v_string = vim_strsave(buf);
4343}
4344
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004345/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004346 * "gettagstack()" function
4347 */
4348 static void
4349f_gettagstack(typval_T *argvars, typval_T *rettv)
4350{
4351 win_T *wp = curwin; // default is current window
4352
4353 if (rettv_dict_alloc(rettv) != OK)
4354 return;
4355
4356 if (argvars[0].v_type != VAR_UNKNOWN)
4357 {
4358 wp = find_win_by_nr_or_id(&argvars[0]);
4359 if (wp == NULL)
4360 return;
4361 }
4362
4363 get_tagstack(wp, rettv->vval.v_dict);
4364}
4365
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004366/*
4367 * "gettext()" function
4368 */
4369 static void
4370f_gettext(typval_T *argvars, typval_T *rettv)
4371{
4372 if (argvars[0].v_type != VAR_STRING
4373 || argvars[0].vval.v_string == NULL
4374 || *argvars[0].vval.v_string == NUL)
4375 {
4376 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4377 }
4378 else
4379 {
4380 rettv->v_type = VAR_STRING;
4381 rettv->vval.v_string = vim_strsave(
4382 (char_u *)_(argvars[0].vval.v_string));
4383 }
4384}
4385
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004386// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004387#include "version.h"
4388
4389/*
4390 * "has()" function
4391 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004392 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004393f_has(typval_T *argvars, typval_T *rettv)
4394{
4395 int i;
4396 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004397 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004398 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004399 typedef struct {
4400 char *name;
4401 short present;
4402 } has_item_T;
4403 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004404 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004405 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004406#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004407 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004408#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004409 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004410#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004411 },
4412 {"arp",
4413#if defined(AMIGA) && defined(FEAT_ARP)
4414 1
4415#else
4416 0
4417#endif
4418 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004419 {"haiku",
4420#ifdef __HAIKU__
4421 1
4422#else
4423 0
4424#endif
4425 },
4426 {"bsd",
4427#if defined(BSD) && !defined(MACOS_X)
4428 1
4429#else
4430 0
4431#endif
4432 },
4433 {"hpux",
4434#ifdef hpux
4435 1
4436#else
4437 0
4438#endif
4439 },
4440 {"linux",
4441#ifdef __linux__
4442 1
4443#else
4444 0
4445#endif
4446 },
4447 {"mac", // Mac OS X (and, once, Mac OS Classic)
4448#ifdef MACOS_X
4449 1
4450#else
4451 0
4452#endif
4453 },
4454 {"osx", // Mac OS X
4455#ifdef MACOS_X
4456 1
4457#else
4458 0
4459#endif
4460 },
4461 {"macunix", // Mac OS X, with the darwin feature
4462#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4463 1
4464#else
4465 0
4466#endif
4467 },
4468 {"osxdarwin", // synonym for macunix
4469#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4470 1
4471#else
4472 0
4473#endif
4474 },
4475 {"qnx",
4476#ifdef __QNX__
4477 1
4478#else
4479 0
4480#endif
4481 },
4482 {"sun",
4483#ifdef SUN_SYSTEM
4484 1
4485#else
4486 0
4487#endif
4488 },
4489 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004490#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004491 1
4492#else
4493 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004494#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004495 },
4496 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004497#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004498 1
4499#else
4500 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004501#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004502 },
4503 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004504#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004505 1
4506#else
4507 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004508#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004509 },
4510 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004511#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004512 1
4513#else
4514 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004515#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004516 },
4517 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004518#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004519 1
4520#else
4521 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004522#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004523 },
4524 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004525#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004526 1
4527#else
4528 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004529#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004530 },
4531 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004532#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004533 1
4534#else
4535 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004536#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004537 },
4538 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004539#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004540 1
4541#else
4542 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004543#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004544 },
4545 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004546#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004547 1
4548#else
4549 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004550#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004551 },
4552 {"autocmd", 1},
4553 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004554#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004555 1
4556#else
4557 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004558#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004559 },
4560 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004561#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004562 1
4563#else
4564 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004565#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004566 },
4567 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004568#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004569 1
4570#else
4571 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004572#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004573 },
4574 {"balloon_multiline",
4575#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4576 // MS-Windows requires runtime check, see below
4577 1
4578#else
4579 0
4580#endif
4581 },
4582 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004583#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004584 1
4585#else
4586 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004587#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004588 },
4589 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004590#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004591 1
4592#else
4593 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004594#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004595 },
4596 {"all_builtin_terms",
4597#if defined(ALL_BUILTIN_TCAPS)
4598 1
4599#else
4600 0
4601#endif
4602 },
4603 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004604#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004605 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004606 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004607 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004608#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004609 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004610#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004611 },
4612 {"byte_offset",
4613#ifdef FEAT_BYTEOFF
4614 1
4615#else
4616 0
4617#endif
4618 },
4619 {"channel",
4620#ifdef FEAT_JOB_CHANNEL
4621 1
4622#else
4623 0
4624#endif
4625 },
4626 {"cindent",
4627#ifdef FEAT_CINDENT
4628 1
4629#else
4630 0
4631#endif
4632 },
4633 {"clientserver",
4634#ifdef FEAT_CLIENTSERVER
4635 1
4636#else
4637 0
4638#endif
4639 },
4640 {"clipboard",
4641#ifdef FEAT_CLIPBOARD
4642 1
4643#else
4644 0
4645#endif
4646 },
4647 {"cmdline_compl", 1},
4648 {"cmdline_hist", 1},
4649 {"comments", 1},
4650 {"conceal",
4651#ifdef FEAT_CONCEAL
4652 1
4653#else
4654 0
4655#endif
4656 },
4657 {"cryptv",
4658#ifdef FEAT_CRYPT
4659 1
4660#else
4661 0
4662#endif
4663 },
4664 {"crypt-blowfish",
4665#ifdef FEAT_CRYPT
4666 1
4667#else
4668 0
4669#endif
4670 },
4671 {"crypt-blowfish2",
4672#ifdef FEAT_CRYPT
4673 1
4674#else
4675 0
4676#endif
4677 },
4678 {"cscope",
4679#ifdef FEAT_CSCOPE
4680 1
4681#else
4682 0
4683#endif
4684 },
4685 {"cursorbind", 1},
4686 {"cursorshape",
4687#ifdef CURSOR_SHAPE
4688 1
4689#else
4690 0
4691#endif
4692 },
4693 {"debug",
4694#ifdef DEBUG
4695 1
4696#else
4697 0
4698#endif
4699 },
4700 {"dialog_con",
4701#ifdef FEAT_CON_DIALOG
4702 1
4703#else
4704 0
4705#endif
4706 },
4707 {"dialog_gui",
4708#ifdef FEAT_GUI_DIALOG
4709 1
4710#else
4711 0
4712#endif
4713 },
4714 {"diff",
4715#ifdef FEAT_DIFF
4716 1
4717#else
4718 0
4719#endif
4720 },
4721 {"digraphs",
4722#ifdef FEAT_DIGRAPHS
4723 1
4724#else
4725 0
4726#endif
4727 },
4728 {"directx",
4729#ifdef FEAT_DIRECTX
4730 1
4731#else
4732 0
4733#endif
4734 },
4735 {"dnd",
4736#ifdef FEAT_DND
4737 1
4738#else
4739 0
4740#endif
4741 },
4742 {"emacs_tags",
4743#ifdef FEAT_EMACS_TAGS
4744 1
4745#else
4746 0
4747#endif
4748 },
4749 {"eval", 1}, // always present, of course!
4750 {"ex_extra", 1}, // graduated feature
4751 {"extra_search",
4752#ifdef FEAT_SEARCH_EXTRA
4753 1
4754#else
4755 0
4756#endif
4757 },
4758 {"file_in_path",
4759#ifdef FEAT_SEARCHPATH
4760 1
4761#else
4762 0
4763#endif
4764 },
4765 {"filterpipe",
4766#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4767 1
4768#else
4769 0
4770#endif
4771 },
4772 {"find_in_path",
4773#ifdef FEAT_FIND_ID
4774 1
4775#else
4776 0
4777#endif
4778 },
4779 {"float",
4780#ifdef FEAT_FLOAT
4781 1
4782#else
4783 0
4784#endif
4785 },
4786 {"folding",
4787#ifdef FEAT_FOLDING
4788 1
4789#else
4790 0
4791#endif
4792 },
4793 {"footer",
4794#ifdef FEAT_FOOTER
4795 1
4796#else
4797 0
4798#endif
4799 },
4800 {"fork",
4801#if !defined(USE_SYSTEM) && defined(UNIX)
4802 1
4803#else
4804 0
4805#endif
4806 },
4807 {"gettext",
4808#ifdef FEAT_GETTEXT
4809 1
4810#else
4811 0
4812#endif
4813 },
4814 {"gui",
4815#ifdef FEAT_GUI
4816 1
4817#else
4818 0
4819#endif
4820 },
4821 {"gui_neXtaw",
4822#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4823 1
4824#else
4825 0
4826#endif
4827 },
4828 {"gui_athena",
4829#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4830 1
4831#else
4832 0
4833#endif
4834 },
4835 {"gui_gtk",
4836#ifdef FEAT_GUI_GTK
4837 1
4838#else
4839 0
4840#endif
4841 },
4842 {"gui_gtk2",
4843#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4844 1
4845#else
4846 0
4847#endif
4848 },
4849 {"gui_gtk3",
4850#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4851 1
4852#else
4853 0
4854#endif
4855 },
4856 {"gui_gnome",
4857#ifdef FEAT_GUI_GNOME
4858 1
4859#else
4860 0
4861#endif
4862 },
4863 {"gui_haiku",
4864#ifdef FEAT_GUI_HAIKU
4865 1
4866#else
4867 0
4868#endif
4869 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004870 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004871 {"gui_motif",
4872#ifdef FEAT_GUI_MOTIF
4873 1
4874#else
4875 0
4876#endif
4877 },
4878 {"gui_photon",
4879#ifdef FEAT_GUI_PHOTON
4880 1
4881#else
4882 0
4883#endif
4884 },
4885 {"gui_win32",
4886#ifdef FEAT_GUI_MSWIN
4887 1
4888#else
4889 0
4890#endif
4891 },
4892 {"iconv",
4893#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4894 1
4895#else
4896 0
4897#endif
4898 },
4899 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004900 {"ipv6",
4901#ifdef FEAT_IPV6
4902 1
4903#else
4904 0
4905#endif
4906 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004907 {"job",
4908#ifdef FEAT_JOB_CHANNEL
4909 1
4910#else
4911 0
4912#endif
4913 },
4914 {"jumplist",
4915#ifdef FEAT_JUMPLIST
4916 1
4917#else
4918 0
4919#endif
4920 },
4921 {"keymap",
4922#ifdef FEAT_KEYMAP
4923 1
4924#else
4925 0
4926#endif
4927 },
4928 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4929 {"langmap",
4930#ifdef FEAT_LANGMAP
4931 1
4932#else
4933 0
4934#endif
4935 },
4936 {"libcall",
4937#ifdef FEAT_LIBCALL
4938 1
4939#else
4940 0
4941#endif
4942 },
4943 {"linebreak",
4944#ifdef FEAT_LINEBREAK
4945 1
4946#else
4947 0
4948#endif
4949 },
4950 {"lispindent",
4951#ifdef FEAT_LISP
4952 1
4953#else
4954 0
4955#endif
4956 },
4957 {"listcmds", 1},
4958 {"localmap", 1},
4959 {"lua",
4960#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4961 1
4962#else
4963 0
4964#endif
4965 },
4966 {"menu",
4967#ifdef FEAT_MENU
4968 1
4969#else
4970 0
4971#endif
4972 },
4973 {"mksession",
4974#ifdef FEAT_SESSION
4975 1
4976#else
4977 0
4978#endif
4979 },
4980 {"modify_fname", 1},
4981 {"mouse", 1},
4982 {"mouseshape",
4983#ifdef FEAT_MOUSESHAPE
4984 1
4985#else
4986 0
4987#endif
4988 },
4989 {"mouse_dec",
4990#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4991 1
4992#else
4993 0
4994#endif
4995 },
4996 {"mouse_gpm",
4997#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4998 1
4999#else
5000 0
5001#endif
5002 },
5003 {"mouse_jsbterm",
5004#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
5005 1
5006#else
5007 0
5008#endif
5009 },
5010 {"mouse_netterm",
5011#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
5012 1
5013#else
5014 0
5015#endif
5016 },
5017 {"mouse_pterm",
5018#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
5019 1
5020#else
5021 0
5022#endif
5023 },
5024 {"mouse_sgr",
5025#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5026 1
5027#else
5028 0
5029#endif
5030 },
5031 {"mouse_sysmouse",
5032#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
5033 1
5034#else
5035 0
5036#endif
5037 },
5038 {"mouse_urxvt",
5039#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
5040 1
5041#else
5042 0
5043#endif
5044 },
5045 {"mouse_xterm",
5046#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5047 1
5048#else
5049 0
5050#endif
5051 },
5052 {"multi_byte", 1},
5053 {"multi_byte_ime",
5054#ifdef FEAT_MBYTE_IME
5055 1
5056#else
5057 0
5058#endif
5059 },
5060 {"multi_lang",
5061#ifdef FEAT_MULTI_LANG
5062 1
5063#else
5064 0
5065#endif
5066 },
5067 {"mzscheme",
5068#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
5069 1
5070#else
5071 0
5072#endif
5073 },
5074 {"num64", 1},
5075 {"ole",
5076#ifdef FEAT_OLE
5077 1
5078#else
5079 0
5080#endif
5081 },
5082 {"packages",
5083#ifdef FEAT_EVAL
5084 1
5085#else
5086 0
5087#endif
5088 },
5089 {"path_extra",
5090#ifdef FEAT_PATH_EXTRA
5091 1
5092#else
5093 0
5094#endif
5095 },
5096 {"perl",
5097#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
5098 1
5099#else
5100 0
5101#endif
5102 },
5103 {"persistent_undo",
5104#ifdef FEAT_PERSISTENT_UNDO
5105 1
5106#else
5107 0
5108#endif
5109 },
5110 {"python_compiled",
5111#if defined(FEAT_PYTHON)
5112 1
5113#else
5114 0
5115#endif
5116 },
5117 {"python_dynamic",
5118#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
5119 1
5120#else
5121 0
5122#endif
5123 },
5124 {"python",
5125#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
5126 1
5127#else
5128 0
5129#endif
5130 },
5131 {"pythonx",
5132#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
5133 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
5134 1
5135#else
5136 0
5137#endif
5138 },
5139 {"python3_compiled",
5140#if defined(FEAT_PYTHON3)
5141 1
5142#else
5143 0
5144#endif
5145 },
5146 {"python3_dynamic",
5147#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
5148 1
5149#else
5150 0
5151#endif
5152 },
5153 {"python3",
5154#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
5155 1
5156#else
5157 0
5158#endif
5159 },
5160 {"popupwin",
5161#ifdef FEAT_PROP_POPUP
5162 1
5163#else
5164 0
5165#endif
5166 },
5167 {"postscript",
5168#ifdef FEAT_POSTSCRIPT
5169 1
5170#else
5171 0
5172#endif
5173 },
5174 {"printer",
5175#ifdef FEAT_PRINTER
5176 1
5177#else
5178 0
5179#endif
5180 },
5181 {"profile",
5182#ifdef FEAT_PROFILE
5183 1
5184#else
5185 0
5186#endif
5187 },
5188 {"reltime",
5189#ifdef FEAT_RELTIME
5190 1
5191#else
5192 0
5193#endif
5194 },
5195 {"quickfix",
5196#ifdef FEAT_QUICKFIX
5197 1
5198#else
5199 0
5200#endif
5201 },
5202 {"rightleft",
5203#ifdef FEAT_RIGHTLEFT
5204 1
5205#else
5206 0
5207#endif
5208 },
5209 {"ruby",
5210#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5211 1
5212#else
5213 0
5214#endif
5215 },
5216 {"scrollbind", 1},
5217 {"showcmd",
5218#ifdef FEAT_CMDL_INFO
5219 1
5220#else
5221 0
5222#endif
5223 },
5224 {"cmdline_info",
5225#ifdef FEAT_CMDL_INFO
5226 1
5227#else
5228 0
5229#endif
5230 },
5231 {"signs",
5232#ifdef FEAT_SIGNS
5233 1
5234#else
5235 0
5236#endif
5237 },
5238 {"smartindent",
5239#ifdef FEAT_SMARTINDENT
5240 1
5241#else
5242 0
5243#endif
5244 },
5245 {"startuptime",
5246#ifdef STARTUPTIME
5247 1
5248#else
5249 0
5250#endif
5251 },
5252 {"statusline",
5253#ifdef FEAT_STL_OPT
5254 1
5255#else
5256 0
5257#endif
5258 },
5259 {"netbeans_intg",
5260#ifdef FEAT_NETBEANS_INTG
5261 1
5262#else
5263 0
5264#endif
5265 },
5266 {"sound",
5267#ifdef FEAT_SOUND
5268 1
5269#else
5270 0
5271#endif
5272 },
5273 {"spell",
5274#ifdef FEAT_SPELL
5275 1
5276#else
5277 0
5278#endif
5279 },
5280 {"syntax",
5281#ifdef FEAT_SYN_HL
5282 1
5283#else
5284 0
5285#endif
5286 },
5287 {"system",
5288#if defined(USE_SYSTEM) || !defined(UNIX)
5289 1
5290#else
5291 0
5292#endif
5293 },
5294 {"tag_binary",
5295#ifdef FEAT_TAG_BINS
5296 1
5297#else
5298 0
5299#endif
5300 },
5301 {"tcl",
5302#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5303 1
5304#else
5305 0
5306#endif
5307 },
5308 {"termguicolors",
5309#ifdef FEAT_TERMGUICOLORS
5310 1
5311#else
5312 0
5313#endif
5314 },
5315 {"terminal",
5316#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5317 1
5318#else
5319 0
5320#endif
5321 },
5322 {"terminfo",
5323#ifdef TERMINFO
5324 1
5325#else
5326 0
5327#endif
5328 },
5329 {"termresponse",
5330#ifdef FEAT_TERMRESPONSE
5331 1
5332#else
5333 0
5334#endif
5335 },
5336 {"textobjects",
5337#ifdef FEAT_TEXTOBJ
5338 1
5339#else
5340 0
5341#endif
5342 },
5343 {"textprop",
5344#ifdef FEAT_PROP_POPUP
5345 1
5346#else
5347 0
5348#endif
5349 },
5350 {"tgetent",
5351#ifdef HAVE_TGETENT
5352 1
5353#else
5354 0
5355#endif
5356 },
5357 {"timers",
5358#ifdef FEAT_TIMERS
5359 1
5360#else
5361 0
5362#endif
5363 },
5364 {"title",
5365#ifdef FEAT_TITLE
5366 1
5367#else
5368 0
5369#endif
5370 },
5371 {"toolbar",
5372#ifdef FEAT_TOOLBAR
5373 1
5374#else
5375 0
5376#endif
5377 },
5378 {"unnamedplus",
5379#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5380 1
5381#else
5382 0
5383#endif
5384 },
5385 {"user-commands", 1}, // was accidentally included in 5.4
5386 {"user_commands", 1},
5387 {"vartabs",
5388#ifdef FEAT_VARTABS
5389 1
5390#else
5391 0
5392#endif
5393 },
5394 {"vertsplit", 1},
5395 {"viminfo",
5396#ifdef FEAT_VIMINFO
5397 1
5398#else
5399 0
5400#endif
5401 },
5402 {"vimscript-1", 1},
5403 {"vimscript-2", 1},
5404 {"vimscript-3", 1},
5405 {"vimscript-4", 1},
5406 {"virtualedit", 1},
5407 {"visual", 1},
5408 {"visualextra", 1},
5409 {"vreplace", 1},
5410 {"vtp",
5411#ifdef FEAT_VTP
5412 1
5413#else
5414 0
5415#endif
5416 },
5417 {"wildignore",
5418#ifdef FEAT_WILDIGN
5419 1
5420#else
5421 0
5422#endif
5423 },
5424 {"wildmenu",
5425#ifdef FEAT_WILDMENU
5426 1
5427#else
5428 0
5429#endif
5430 },
5431 {"windows", 1},
5432 {"winaltkeys",
5433#ifdef FEAT_WAK
5434 1
5435#else
5436 0
5437#endif
5438 },
5439 {"writebackup",
5440#ifdef FEAT_WRITEBACKUP
5441 1
5442#else
5443 0
5444#endif
5445 },
5446 {"xim",
5447#ifdef FEAT_XIM
5448 1
5449#else
5450 0
5451#endif
5452 },
5453 {"xfontset",
5454#ifdef FEAT_XFONTSET
5455 1
5456#else
5457 0
5458#endif
5459 },
5460 {"xpm",
5461#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5462 1
5463#else
5464 0
5465#endif
5466 },
5467 {"xpm_w32", // for backward compatibility
5468#ifdef FEAT_XPM_W32
5469 1
5470#else
5471 0
5472#endif
5473 },
5474 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005475#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005476 1
5477#else
5478 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005479#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005480 },
5481 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005482#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005483 1
5484#else
5485 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005486#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005487 },
5488 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005489#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005490 1
5491#else
5492 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005493#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005494 },
5495 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005496#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005497 1
5498#else
5499 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005500#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005501 },
5502 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005503#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005504 1
5505#else
5506 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005507#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005508 },
5509 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005510 };
5511
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005512 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005513 for (i = 0; has_list[i].name != NULL; ++i)
5514 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005515 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005516 x = TRUE;
5517 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005518 break;
5519 }
5520
Bram Moolenaar79296512020-03-22 16:17:14 +01005521 // features also in has_list[] but sometimes enabled at runtime
5522 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005523 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005524 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005525 {
5526 // intentionally empty
5527 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005528#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005529 else if (STRICMP(name, "balloon_multiline") == 0)
5530 n = multiline_balloon_available();
5531#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005532#ifdef VIMDLL
5533 else if (STRICMP(name, "filterpipe") == 0)
5534 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005535#endif
5536#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5537 else if (STRICMP(name, "iconv") == 0)
5538 n = iconv_enabled(FALSE);
5539#endif
5540#ifdef DYNAMIC_LUA
5541 else if (STRICMP(name, "lua") == 0)
5542 n = lua_enabled(FALSE);
5543#endif
5544#ifdef DYNAMIC_MZSCHEME
5545 else if (STRICMP(name, "mzscheme") == 0)
5546 n = mzscheme_enabled(FALSE);
5547#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005548#ifdef DYNAMIC_PERL
5549 else if (STRICMP(name, "perl") == 0)
5550 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005551#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005552#ifdef DYNAMIC_PYTHON
5553 else if (STRICMP(name, "python") == 0)
5554 n = python_enabled(FALSE);
5555#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005556#ifdef DYNAMIC_PYTHON3
5557 else if (STRICMP(name, "python3") == 0)
5558 n = python3_enabled(FALSE);
5559#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005560#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5561 else if (STRICMP(name, "pythonx") == 0)
5562 {
5563# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5564 if (p_pyx == 0)
5565 n = python3_enabled(FALSE) || python_enabled(FALSE);
5566 else if (p_pyx == 3)
5567 n = python3_enabled(FALSE);
5568 else if (p_pyx == 2)
5569 n = python_enabled(FALSE);
5570# elif defined(DYNAMIC_PYTHON)
5571 n = python_enabled(FALSE);
5572# elif defined(DYNAMIC_PYTHON3)
5573 n = python3_enabled(FALSE);
5574# endif
5575 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005576#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005577#ifdef DYNAMIC_RUBY
5578 else if (STRICMP(name, "ruby") == 0)
5579 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005580#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005581#ifdef DYNAMIC_TCL
5582 else if (STRICMP(name, "tcl") == 0)
5583 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005584#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005585#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005586 else if (STRICMP(name, "terminal") == 0)
5587 n = terminal_enabled();
5588#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005589 }
5590
Bram Moolenaar79296512020-03-22 16:17:14 +01005591 // features not in has_list[]
5592 if (x == FALSE)
5593 {
5594 if (STRNICMP(name, "patch", 5) == 0)
5595 {
5596 x = TRUE;
5597 if (name[5] == '-'
5598 && STRLEN(name) >= 11
5599 && vim_isdigit(name[6])
5600 && vim_isdigit(name[8])
5601 && vim_isdigit(name[10]))
5602 {
5603 int major = atoi((char *)name + 6);
5604 int minor = atoi((char *)name + 8);
5605
5606 // Expect "patch-9.9.01234".
5607 n = (major < VIM_VERSION_MAJOR
5608 || (major == VIM_VERSION_MAJOR
5609 && (minor < VIM_VERSION_MINOR
5610 || (minor == VIM_VERSION_MINOR
5611 && has_patch(atoi((char *)name + 10))))));
5612 }
5613 else
5614 n = has_patch(atoi((char *)name + 5));
5615 }
5616 else if (STRICMP(name, "vim_starting") == 0)
5617 {
5618 x = TRUE;
5619 n = (starting != 0);
5620 }
5621 else if (STRICMP(name, "ttyin") == 0)
5622 {
5623 x = TRUE;
5624 n = mch_input_isatty();
5625 }
5626 else if (STRICMP(name, "ttyout") == 0)
5627 {
5628 x = TRUE;
5629 n = stdout_isatty;
5630 }
5631 else if (STRICMP(name, "multi_byte_encoding") == 0)
5632 {
5633 x = TRUE;
5634 n = has_mbyte;
5635 }
5636 else if (STRICMP(name, "gui_running") == 0)
5637 {
5638 x = TRUE;
5639#ifdef FEAT_GUI
5640 n = (gui.in_use || gui.starting);
5641#endif
5642 }
5643 else if (STRICMP(name, "browse") == 0)
5644 {
5645 x = TRUE;
5646#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5647 n = gui.in_use; // gui_mch_browse() works when GUI is running
5648#endif
5649 }
5650 else if (STRICMP(name, "syntax_items") == 0)
5651 {
5652 x = TRUE;
5653#ifdef FEAT_SYN_HL
5654 n = syntax_present(curwin);
5655#endif
5656 }
5657 else if (STRICMP(name, "vcon") == 0)
5658 {
5659 x = TRUE;
5660#ifdef FEAT_VTP
5661 n = is_term_win32() && has_vtp_working();
5662#endif
5663 }
5664 else if (STRICMP(name, "netbeans_enabled") == 0)
5665 {
5666 x = TRUE;
5667#ifdef FEAT_NETBEANS_INTG
5668 n = netbeans_active();
5669#endif
5670 }
5671 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5672 {
5673 x = TRUE;
5674#ifdef FEAT_MOUSE_GPM
5675 n = gpm_enabled();
5676#endif
5677 }
5678 else if (STRICMP(name, "conpty") == 0)
5679 {
5680 x = TRUE;
5681#if defined(FEAT_TERMINAL) && defined(MSWIN)
5682 n = use_conpty();
5683#endif
5684 }
5685 else if (STRICMP(name, "clipboard_working") == 0)
5686 {
5687 x = TRUE;
5688#ifdef FEAT_CLIPBOARD
5689 n = clip_star.available;
5690#endif
5691 }
5692 }
5693
Bram Moolenaar04637e22020-09-05 18:45:29 +02005694 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005695 // return whether feature could ever be enabled
5696 rettv->vval.v_number = x;
5697 else
5698 // return whether feature is enabled
5699 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005700}
5701
5702/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005703 * Return TRUE if "feature" can change later.
5704 * Also when checking for the feature has side effects, such as loading a DLL.
5705 */
5706 int
5707dynamic_feature(char_u *feature)
5708{
5709 return (feature == NULL
5710#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5711 || STRICMP(feature, "balloon_multiline") == 0
5712#endif
5713#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5714 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5715#endif
5716#ifdef VIMDLL
5717 || STRICMP(feature, "filterpipe") == 0
5718#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005719#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005720 // this can only change on Unix where the ":gui" command could be
5721 // used.
5722 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5723#endif
5724#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5725 || STRICMP(feature, "iconv") == 0
5726#endif
5727#ifdef DYNAMIC_LUA
5728 || STRICMP(feature, "lua") == 0
5729#endif
5730#ifdef FEAT_MOUSE_GPM
5731 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5732#endif
5733#ifdef DYNAMIC_MZSCHEME
5734 || STRICMP(feature, "mzscheme") == 0
5735#endif
5736#ifdef FEAT_NETBEANS_INTG
5737 || STRICMP(feature, "netbeans_enabled") == 0
5738#endif
5739#ifdef DYNAMIC_PERL
5740 || STRICMP(feature, "perl") == 0
5741#endif
5742#ifdef DYNAMIC_PYTHON
5743 || STRICMP(feature, "python") == 0
5744#endif
5745#ifdef DYNAMIC_PYTHON3
5746 || STRICMP(feature, "python3") == 0
5747#endif
5748#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5749 || STRICMP(feature, "pythonx") == 0
5750#endif
5751#ifdef DYNAMIC_RUBY
5752 || STRICMP(feature, "ruby") == 0
5753#endif
5754#ifdef FEAT_SYN_HL
5755 || STRICMP(feature, "syntax_items") == 0
5756#endif
5757#ifdef DYNAMIC_TCL
5758 || STRICMP(feature, "tcl") == 0
5759#endif
5760 // once "starting" is zero it will stay that way
5761 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5762 || STRICMP(feature, "multi_byte_encoding") == 0
5763#if defined(FEAT_TERMINAL) && defined(MSWIN)
5764 || STRICMP(feature, "conpty") == 0
5765#endif
5766 );
5767}
5768
5769/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005770 * "haslocaldir()" function
5771 */
5772 static void
5773f_haslocaldir(typval_T *argvars, typval_T *rettv)
5774{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005775 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005776 win_T *wp = NULL;
5777
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005778 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5779
5780 // Check for window-local and tab-local directories
5781 if (wp != NULL && wp->w_localdir != NULL)
5782 rettv->vval.v_number = 1;
5783 else if (tp != NULL && tp->tp_localdir != NULL)
5784 rettv->vval.v_number = 2;
5785 else
5786 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005787}
5788
5789/*
5790 * "hasmapto()" function
5791 */
5792 static void
5793f_hasmapto(typval_T *argvars, typval_T *rettv)
5794{
5795 char_u *name;
5796 char_u *mode;
5797 char_u buf[NUMBUFLEN];
5798 int abbr = FALSE;
5799
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005800 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005801 if (argvars[1].v_type == VAR_UNKNOWN)
5802 mode = (char_u *)"nvo";
5803 else
5804 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005805 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005806 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005807 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005808 }
5809
5810 if (map_to_exists(name, mode, abbr))
5811 rettv->vval.v_number = TRUE;
5812 else
5813 rettv->vval.v_number = FALSE;
5814}
5815
5816/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005817 * "highlightID(name)" function
5818 */
5819 static void
5820f_hlID(typval_T *argvars, typval_T *rettv)
5821{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005822 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005823}
5824
5825/*
5826 * "highlight_exists()" function
5827 */
5828 static void
5829f_hlexists(typval_T *argvars, typval_T *rettv)
5830{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005831 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005832}
5833
5834/*
5835 * "hostname()" function
5836 */
5837 static void
5838f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5839{
5840 char_u hostname[256];
5841
5842 mch_get_host_name(hostname, 256);
5843 rettv->v_type = VAR_STRING;
5844 rettv->vval.v_string = vim_strsave(hostname);
5845}
5846
5847/*
5848 * iconv() function
5849 */
5850 static void
5851f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5852{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005853 char_u buf1[NUMBUFLEN];
5854 char_u buf2[NUMBUFLEN];
5855 char_u *from, *to, *str;
5856 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005857
5858 rettv->v_type = VAR_STRING;
5859 rettv->vval.v_string = NULL;
5860
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005861 str = tv_get_string(&argvars[0]);
5862 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5863 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005864 vimconv.vc_type = CONV_NONE;
5865 convert_setup(&vimconv, from, to);
5866
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005867 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005868 if (vimconv.vc_type == CONV_NONE)
5869 rettv->vval.v_string = vim_strsave(str);
5870 else
5871 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5872
5873 convert_setup(&vimconv, NULL, NULL);
5874 vim_free(from);
5875 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005876}
5877
5878/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005879 * "index()" function
5880 */
5881 static void
5882f_index(typval_T *argvars, typval_T *rettv)
5883{
5884 list_T *l;
5885 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005886 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005887 long idx = 0;
5888 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005889 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005890
5891 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005892 if (argvars[0].v_type == VAR_BLOB)
5893 {
5894 typval_T tv;
5895 int start = 0;
5896
5897 if (argvars[2].v_type != VAR_UNKNOWN)
5898 {
5899 start = tv_get_number_chk(&argvars[2], &error);
5900 if (error)
5901 return;
5902 }
5903 b = argvars[0].vval.v_blob;
5904 if (b == NULL)
5905 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005906 if (start < 0)
5907 {
5908 start = blob_len(b) + start;
5909 if (start < 0)
5910 start = 0;
5911 }
5912
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005913 for (idx = start; idx < blob_len(b); ++idx)
5914 {
5915 tv.v_type = VAR_NUMBER;
5916 tv.vval.v_number = blob_get(b, idx);
5917 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5918 {
5919 rettv->vval.v_number = idx;
5920 return;
5921 }
5922 }
5923 return;
5924 }
5925 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005926 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005927 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005928 return;
5929 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005930
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005931 l = argvars[0].vval.v_list;
5932 if (l != NULL)
5933 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005934 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005935 item = l->lv_first;
5936 if (argvars[2].v_type != VAR_UNKNOWN)
5937 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005938 // Start at specified item. Use the cached index that list_find()
5939 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005940 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005941 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005942 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005943 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005944 if (error)
5945 item = NULL;
5946 }
5947
5948 for ( ; item != NULL; item = item->li_next, ++idx)
5949 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5950 {
5951 rettv->vval.v_number = idx;
5952 break;
5953 }
5954 }
5955}
5956
5957static int inputsecret_flag = 0;
5958
5959/*
5960 * "input()" function
5961 * Also handles inputsecret() when inputsecret is set.
5962 */
5963 static void
5964f_input(typval_T *argvars, typval_T *rettv)
5965{
5966 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5967}
5968
5969/*
5970 * "inputdialog()" function
5971 */
5972 static void
5973f_inputdialog(typval_T *argvars, typval_T *rettv)
5974{
5975#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005976 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005977 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5978 {
5979 char_u *message;
5980 char_u buf[NUMBUFLEN];
5981 char_u *defstr = (char_u *)"";
5982
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005983 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005984 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005985 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005986 vim_strncpy(IObuff, defstr, IOSIZE - 1);
5987 else
5988 IObuff[0] = NUL;
5989 if (message != NULL && defstr != NULL
5990 && do_dialog(VIM_QUESTION, NULL, message,
5991 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
5992 rettv->vval.v_string = vim_strsave(IObuff);
5993 else
5994 {
5995 if (message != NULL && defstr != NULL
5996 && argvars[1].v_type != VAR_UNKNOWN
5997 && argvars[2].v_type != VAR_UNKNOWN)
5998 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005999 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006000 else
6001 rettv->vval.v_string = NULL;
6002 }
6003 rettv->v_type = VAR_STRING;
6004 }
6005 else
6006#endif
6007 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6008}
6009
6010/*
6011 * "inputlist()" function
6012 */
6013 static void
6014f_inputlist(typval_T *argvars, typval_T *rettv)
6015{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006016 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006017 listitem_T *li;
6018 int selected;
6019 int mouse_used;
6020
6021#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006022 // While starting up, there is no place to enter text. When running tests
6023 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006024 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006025 return;
6026#endif
6027 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6028 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006029 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006030 return;
6031 }
6032
6033 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006034 msg_row = Rows - 1; // for when 'cmdheight' > 1
6035 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006036 msg_scroll = TRUE;
6037 msg_clr_eos();
6038
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006039 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006040 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006041 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006042 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006043 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006044 msg_putchar('\n');
6045 }
6046
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006047 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006048 selected = prompt_for_number(&mouse_used);
6049 if (mouse_used)
6050 selected -= lines_left;
6051
6052 rettv->vval.v_number = selected;
6053}
6054
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006055static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6056
6057/*
6058 * "inputrestore()" function
6059 */
6060 static void
6061f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6062{
6063 if (ga_userinput.ga_len > 0)
6064 {
6065 --ga_userinput.ga_len;
6066 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6067 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006068 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006069 }
6070 else if (p_verbose > 1)
6071 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006072 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006073 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006074 }
6075}
6076
6077/*
6078 * "inputsave()" function
6079 */
6080 static void
6081f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6082{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006083 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006084 if (ga_grow(&ga_userinput, 1) == OK)
6085 {
6086 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6087 + ga_userinput.ga_len);
6088 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006089 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006090 }
6091 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006092 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006093}
6094
6095/*
6096 * "inputsecret()" function
6097 */
6098 static void
6099f_inputsecret(typval_T *argvars, typval_T *rettv)
6100{
6101 ++cmdline_star;
6102 ++inputsecret_flag;
6103 f_input(argvars, rettv);
6104 --cmdline_star;
6105 --inputsecret_flag;
6106}
6107
6108/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01006109 * "interrupt()" function
6110 */
6111 static void
6112f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6113{
6114 got_int = TRUE;
6115}
6116
6117/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006118 * "invert(expr)" function
6119 */
6120 static void
6121f_invert(typval_T *argvars, typval_T *rettv)
6122{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006123 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006124}
6125
6126/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006127 * "islocked()" function
6128 */
6129 static void
6130f_islocked(typval_T *argvars, typval_T *rettv)
6131{
6132 lval_T lv;
6133 char_u *end;
6134 dictitem_T *di;
6135
6136 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006137 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006138 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006139 if (end != NULL && lv.ll_name != NULL)
6140 {
6141 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02006142 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006143 else
6144 {
6145 if (lv.ll_tv == NULL)
6146 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006147 di = find_var(lv.ll_name, NULL, TRUE);
6148 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006149 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006150 // Consider a variable locked when:
6151 // 1. the variable itself is locked
6152 // 2. the value of the variable is locked.
6153 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01006154 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6155 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006156 }
6157 }
6158 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006159 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006160 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006161 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006162 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006163 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006164 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6165 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006166 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006167 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6168 }
6169 }
6170
6171 clear_lval(&lv);
6172}
6173
6174#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6175/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006176 * "isinf()" function
6177 */
6178 static void
6179f_isinf(typval_T *argvars, typval_T *rettv)
6180{
6181 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6182 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6183}
6184
6185/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006186 * "isnan()" function
6187 */
6188 static void
6189f_isnan(typval_T *argvars, typval_T *rettv)
6190{
6191 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6192 && isnan(argvars[0].vval.v_float);
6193}
6194#endif
6195
6196/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006197 * "last_buffer_nr()" function.
6198 */
6199 static void
6200f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6201{
6202 int n = 0;
6203 buf_T *buf;
6204
Bram Moolenaar29323592016-07-24 22:04:11 +02006205 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006206 if (n < buf->b_fnum)
6207 n = buf->b_fnum;
6208
6209 rettv->vval.v_number = n;
6210}
6211
6212/*
6213 * "len()" function
6214 */
6215 static void
6216f_len(typval_T *argvars, typval_T *rettv)
6217{
6218 switch (argvars[0].v_type)
6219 {
6220 case VAR_STRING:
6221 case VAR_NUMBER:
6222 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006223 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006224 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006225 case VAR_BLOB:
6226 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6227 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006228 case VAR_LIST:
6229 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6230 break;
6231 case VAR_DICT:
6232 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6233 break;
6234 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006235 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006236 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006237 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006238 case VAR_SPECIAL:
6239 case VAR_FLOAT:
6240 case VAR_FUNC:
6241 case VAR_PARTIAL:
6242 case VAR_JOB:
6243 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006244 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006245 break;
6246 }
6247}
6248
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006249 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006250libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006251{
6252#ifdef FEAT_LIBCALL
6253 char_u *string_in;
6254 char_u **string_result;
6255 int nr_result;
6256#endif
6257
6258 rettv->v_type = type;
6259 if (type != VAR_NUMBER)
6260 rettv->vval.v_string = NULL;
6261
6262 if (check_restricted() || check_secure())
6263 return;
6264
6265#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006266 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006267 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6268 {
6269 string_in = NULL;
6270 if (argvars[2].v_type == VAR_STRING)
6271 string_in = argvars[2].vval.v_string;
6272 if (type == VAR_NUMBER)
6273 string_result = NULL;
6274 else
6275 string_result = &rettv->vval.v_string;
6276 if (mch_libcall(argvars[0].vval.v_string,
6277 argvars[1].vval.v_string,
6278 string_in,
6279 argvars[2].vval.v_number,
6280 string_result,
6281 &nr_result) == OK
6282 && type == VAR_NUMBER)
6283 rettv->vval.v_number = nr_result;
6284 }
6285#endif
6286}
6287
6288/*
6289 * "libcall()" function
6290 */
6291 static void
6292f_libcall(typval_T *argvars, typval_T *rettv)
6293{
6294 libcall_common(argvars, rettv, VAR_STRING);
6295}
6296
6297/*
6298 * "libcallnr()" function
6299 */
6300 static void
6301f_libcallnr(typval_T *argvars, typval_T *rettv)
6302{
6303 libcall_common(argvars, rettv, VAR_NUMBER);
6304}
6305
6306/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006307 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006308 */
6309 static void
6310f_line(typval_T *argvars, typval_T *rettv)
6311{
6312 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006313 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006314 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006315 int id;
6316 tabpage_T *tp;
6317 win_T *wp;
6318 win_T *save_curwin;
6319 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006320
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006321 if (argvars[1].v_type != VAR_UNKNOWN)
6322 {
6323 // use window specified in the second argument
6324 id = (int)tv_get_number(&argvars[1]);
6325 wp = win_id2wp_tp(id, &tp);
6326 if (wp != NULL && tp != NULL)
6327 {
6328 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6329 == OK)
6330 {
6331 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006332 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006333 }
6334 restore_win_noblock(save_curwin, save_curtab, TRUE);
6335 }
6336 }
6337 else
6338 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006339 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006340
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006341 if (fp != NULL)
6342 lnum = fp->lnum;
6343 rettv->vval.v_number = lnum;
6344}
6345
6346/*
6347 * "line2byte(lnum)" function
6348 */
6349 static void
6350f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6351{
6352#ifndef FEAT_BYTEOFF
6353 rettv->vval.v_number = -1;
6354#else
6355 linenr_T lnum;
6356
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006357 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006358 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6359 rettv->vval.v_number = -1;
6360 else
6361 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6362 if (rettv->vval.v_number >= 0)
6363 ++rettv->vval.v_number;
6364#endif
6365}
6366
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006367#ifdef FEAT_FLOAT
6368/*
6369 * "log()" function
6370 */
6371 static void
6372f_log(typval_T *argvars, typval_T *rettv)
6373{
6374 float_T f = 0.0;
6375
6376 rettv->v_type = VAR_FLOAT;
6377 if (get_float_arg(argvars, &f) == OK)
6378 rettv->vval.v_float = log(f);
6379 else
6380 rettv->vval.v_float = 0.0;
6381}
6382
6383/*
6384 * "log10()" function
6385 */
6386 static void
6387f_log10(typval_T *argvars, typval_T *rettv)
6388{
6389 float_T f = 0.0;
6390
6391 rettv->v_type = VAR_FLOAT;
6392 if (get_float_arg(argvars, &f) == OK)
6393 rettv->vval.v_float = log10(f);
6394 else
6395 rettv->vval.v_float = 0.0;
6396}
6397#endif
6398
6399#ifdef FEAT_LUA
6400/*
6401 * "luaeval()" function
6402 */
6403 static void
6404f_luaeval(typval_T *argvars, typval_T *rettv)
6405{
6406 char_u *str;
6407 char_u buf[NUMBUFLEN];
6408
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006409 if (check_restricted() || check_secure())
6410 return;
6411
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006412 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006413 do_luaeval(str, argvars + 1, rettv);
6414}
6415#endif
6416
6417/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006418 * "maparg()" function
6419 */
6420 static void
6421f_maparg(typval_T *argvars, typval_T *rettv)
6422{
6423 get_maparg(argvars, rettv, TRUE);
6424}
6425
6426/*
6427 * "mapcheck()" function
6428 */
6429 static void
6430f_mapcheck(typval_T *argvars, typval_T *rettv)
6431{
6432 get_maparg(argvars, rettv, FALSE);
6433}
6434
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006435typedef enum
6436{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006437 MATCH_END, // matchend()
6438 MATCH_MATCH, // match()
6439 MATCH_STR, // matchstr()
6440 MATCH_LIST, // matchlist()
6441 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006442} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006443
6444 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006445find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006446{
6447 char_u *str = NULL;
6448 long len = 0;
6449 char_u *expr = NULL;
6450 char_u *pat;
6451 regmatch_T regmatch;
6452 char_u patbuf[NUMBUFLEN];
6453 char_u strbuf[NUMBUFLEN];
6454 char_u *save_cpo;
6455 long start = 0;
6456 long nth = 1;
6457 colnr_T startcol = 0;
6458 int match = 0;
6459 list_T *l = NULL;
6460 listitem_T *li = NULL;
6461 long idx = 0;
6462 char_u *tofree = NULL;
6463
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006464 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006465 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006466 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006467
6468 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006469 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006470 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006471 // type MATCH_LIST: return empty list when there are no matches.
6472 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006473 if (rettv_list_alloc(rettv) == FAIL)
6474 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006475 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006476 && (list_append_string(rettv->vval.v_list,
6477 (char_u *)"", 0) == FAIL
6478 || list_append_number(rettv->vval.v_list,
6479 (varnumber_T)-1) == FAIL
6480 || list_append_number(rettv->vval.v_list,
6481 (varnumber_T)-1) == FAIL
6482 || list_append_number(rettv->vval.v_list,
6483 (varnumber_T)-1) == FAIL))
6484 {
6485 list_free(rettv->vval.v_list);
6486 rettv->vval.v_list = NULL;
6487 goto theend;
6488 }
6489 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006490 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006491 {
6492 rettv->v_type = VAR_STRING;
6493 rettv->vval.v_string = NULL;
6494 }
6495
6496 if (argvars[0].v_type == VAR_LIST)
6497 {
6498 if ((l = argvars[0].vval.v_list) == NULL)
6499 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006500 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006501 li = l->lv_first;
6502 }
6503 else
6504 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006505 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506 len = (long)STRLEN(str);
6507 }
6508
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006509 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006510 if (pat == NULL)
6511 goto theend;
6512
6513 if (argvars[2].v_type != VAR_UNKNOWN)
6514 {
6515 int error = FALSE;
6516
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006517 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006518 if (error)
6519 goto theend;
6520 if (l != NULL)
6521 {
6522 li = list_find(l, start);
6523 if (li == NULL)
6524 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006525 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006526 }
6527 else
6528 {
6529 if (start < 0)
6530 start = 0;
6531 if (start > len)
6532 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006533 // When "count" argument is there ignore matches before "start",
6534 // otherwise skip part of the string. Differs when pattern is "^"
6535 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006536 if (argvars[3].v_type != VAR_UNKNOWN)
6537 startcol = start;
6538 else
6539 {
6540 str += start;
6541 len -= start;
6542 }
6543 }
6544
6545 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006546 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006547 if (error)
6548 goto theend;
6549 }
6550
6551 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6552 if (regmatch.regprog != NULL)
6553 {
6554 regmatch.rm_ic = p_ic;
6555
6556 for (;;)
6557 {
6558 if (l != NULL)
6559 {
6560 if (li == NULL)
6561 {
6562 match = FALSE;
6563 break;
6564 }
6565 vim_free(tofree);
6566 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6567 if (str == NULL)
6568 break;
6569 }
6570
6571 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6572
6573 if (match && --nth <= 0)
6574 break;
6575 if (l == NULL && !match)
6576 break;
6577
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006578 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006579 if (l != NULL)
6580 {
6581 li = li->li_next;
6582 ++idx;
6583 }
6584 else
6585 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006586 startcol = (colnr_T)(regmatch.startp[0]
6587 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006588 if (startcol > (colnr_T)len
6589 || str + startcol <= regmatch.startp[0])
6590 {
6591 match = FALSE;
6592 break;
6593 }
6594 }
6595 }
6596
6597 if (match)
6598 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006599 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006600 {
6601 listitem_T *li1 = rettv->vval.v_list->lv_first;
6602 listitem_T *li2 = li1->li_next;
6603 listitem_T *li3 = li2->li_next;
6604 listitem_T *li4 = li3->li_next;
6605
6606 vim_free(li1->li_tv.vval.v_string);
6607 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006608 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006609 li3->li_tv.vval.v_number =
6610 (varnumber_T)(regmatch.startp[0] - expr);
6611 li4->li_tv.vval.v_number =
6612 (varnumber_T)(regmatch.endp[0] - expr);
6613 if (l != NULL)
6614 li2->li_tv.vval.v_number = (varnumber_T)idx;
6615 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006616 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006617 {
6618 int i;
6619
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006620 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006621 for (i = 0; i < NSUBEXP; ++i)
6622 {
6623 if (regmatch.endp[i] == NULL)
6624 {
6625 if (list_append_string(rettv->vval.v_list,
6626 (char_u *)"", 0) == FAIL)
6627 break;
6628 }
6629 else if (list_append_string(rettv->vval.v_list,
6630 regmatch.startp[i],
6631 (int)(regmatch.endp[i] - regmatch.startp[i]))
6632 == FAIL)
6633 break;
6634 }
6635 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006636 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006637 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006638 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006639 if (l != NULL)
6640 copy_tv(&li->li_tv, rettv);
6641 else
6642 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006643 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006644 }
6645 else if (l != NULL)
6646 rettv->vval.v_number = idx;
6647 else
6648 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006649 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006650 rettv->vval.v_number =
6651 (varnumber_T)(regmatch.startp[0] - str);
6652 else
6653 rettv->vval.v_number =
6654 (varnumber_T)(regmatch.endp[0] - str);
6655 rettv->vval.v_number += (varnumber_T)(str - expr);
6656 }
6657 }
6658 vim_regfree(regmatch.regprog);
6659 }
6660
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006661theend:
6662 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006663 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006664 listitem_remove(rettv->vval.v_list,
6665 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006666 vim_free(tofree);
6667 p_cpo = save_cpo;
6668}
6669
6670/*
6671 * "match()" function
6672 */
6673 static void
6674f_match(typval_T *argvars, typval_T *rettv)
6675{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006676 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006677}
6678
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006679/*
6680 * "matchend()" function
6681 */
6682 static void
6683f_matchend(typval_T *argvars, typval_T *rettv)
6684{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006685 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006686}
6687
6688/*
6689 * "matchlist()" function
6690 */
6691 static void
6692f_matchlist(typval_T *argvars, typval_T *rettv)
6693{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006694 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006695}
6696
6697/*
6698 * "matchstr()" function
6699 */
6700 static void
6701f_matchstr(typval_T *argvars, typval_T *rettv)
6702{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006703 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006704}
6705
6706/*
6707 * "matchstrpos()" function
6708 */
6709 static void
6710f_matchstrpos(typval_T *argvars, typval_T *rettv)
6711{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006712 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006713}
6714
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006715 static void
6716max_min(typval_T *argvars, typval_T *rettv, int domax)
6717{
6718 varnumber_T n = 0;
6719 varnumber_T i;
6720 int error = FALSE;
6721
6722 if (argvars[0].v_type == VAR_LIST)
6723 {
6724 list_T *l;
6725 listitem_T *li;
6726
6727 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006728 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006729 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006730 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006731 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006732 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6733 n = l->lv_u.nonmat.lv_start;
6734 else
6735 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6736 * l->lv_u.nonmat.lv_stride;
6737 }
6738 else
6739 {
6740 li = l->lv_first;
6741 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006742 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006743 n = tv_get_number_chk(&li->li_tv, &error);
6744 for (;;)
6745 {
6746 li = li->li_next;
6747 if (li == NULL)
6748 break;
6749 i = tv_get_number_chk(&li->li_tv, &error);
6750 if (domax ? i > n : i < n)
6751 n = i;
6752 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006753 }
6754 }
6755 }
6756 }
6757 else if (argvars[0].v_type == VAR_DICT)
6758 {
6759 dict_T *d;
6760 int first = TRUE;
6761 hashitem_T *hi;
6762 int todo;
6763
6764 d = argvars[0].vval.v_dict;
6765 if (d != NULL)
6766 {
6767 todo = (int)d->dv_hashtab.ht_used;
6768 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6769 {
6770 if (!HASHITEM_EMPTY(hi))
6771 {
6772 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006773 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006774 if (first)
6775 {
6776 n = i;
6777 first = FALSE;
6778 }
6779 else if (domax ? i > n : i < n)
6780 n = i;
6781 }
6782 }
6783 }
6784 }
6785 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006786 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006787 rettv->vval.v_number = error ? 0 : n;
6788}
6789
6790/*
6791 * "max()" function
6792 */
6793 static void
6794f_max(typval_T *argvars, typval_T *rettv)
6795{
6796 max_min(argvars, rettv, TRUE);
6797}
6798
6799/*
6800 * "min()" function
6801 */
6802 static void
6803f_min(typval_T *argvars, typval_T *rettv)
6804{
6805 max_min(argvars, rettv, FALSE);
6806}
6807
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006808#if defined(FEAT_MZSCHEME) || defined(PROTO)
6809/*
6810 * "mzeval()" function
6811 */
6812 static void
6813f_mzeval(typval_T *argvars, typval_T *rettv)
6814{
6815 char_u *str;
6816 char_u buf[NUMBUFLEN];
6817
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006818 if (check_restricted() || check_secure())
6819 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006820 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006821 do_mzeval(str, rettv);
6822}
6823
6824 void
6825mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6826{
6827 typval_T argvars[3];
6828
6829 argvars[0].v_type = VAR_STRING;
6830 argvars[0].vval.v_string = name;
6831 copy_tv(args, &argvars[1]);
6832 argvars[2].v_type = VAR_UNKNOWN;
6833 f_call(argvars, rettv);
6834 clear_tv(&argvars[1]);
6835}
6836#endif
6837
6838/*
6839 * "nextnonblank()" function
6840 */
6841 static void
6842f_nextnonblank(typval_T *argvars, typval_T *rettv)
6843{
6844 linenr_T lnum;
6845
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006846 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006847 {
6848 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6849 {
6850 lnum = 0;
6851 break;
6852 }
6853 if (*skipwhite(ml_get(lnum)) != NUL)
6854 break;
6855 }
6856 rettv->vval.v_number = lnum;
6857}
6858
6859/*
6860 * "nr2char()" function
6861 */
6862 static void
6863f_nr2char(typval_T *argvars, typval_T *rettv)
6864{
6865 char_u buf[NUMBUFLEN];
6866
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006867 if (has_mbyte)
6868 {
6869 int utf8 = 0;
6870
6871 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006872 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006873 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006874 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006875 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006876 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006877 }
6878 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006879 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006880 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006881 buf[1] = NUL;
6882 }
6883 rettv->v_type = VAR_STRING;
6884 rettv->vval.v_string = vim_strsave(buf);
6885}
6886
6887/*
6888 * "or(expr, expr)" function
6889 */
6890 static void
6891f_or(typval_T *argvars, typval_T *rettv)
6892{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006893 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6894 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006895}
6896
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006897#ifdef FEAT_PERL
6898/*
6899 * "perleval()" function
6900 */
6901 static void
6902f_perleval(typval_T *argvars, typval_T *rettv)
6903{
6904 char_u *str;
6905 char_u buf[NUMBUFLEN];
6906
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006907 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006908 do_perleval(str, rettv);
6909}
6910#endif
6911
6912#ifdef FEAT_FLOAT
6913/*
6914 * "pow()" function
6915 */
6916 static void
6917f_pow(typval_T *argvars, typval_T *rettv)
6918{
6919 float_T fx = 0.0, fy = 0.0;
6920
6921 rettv->v_type = VAR_FLOAT;
6922 if (get_float_arg(argvars, &fx) == OK
6923 && get_float_arg(&argvars[1], &fy) == OK)
6924 rettv->vval.v_float = pow(fx, fy);
6925 else
6926 rettv->vval.v_float = 0.0;
6927}
6928#endif
6929
6930/*
6931 * "prevnonblank()" function
6932 */
6933 static void
6934f_prevnonblank(typval_T *argvars, typval_T *rettv)
6935{
6936 linenr_T lnum;
6937
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006938 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006939 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6940 lnum = 0;
6941 else
6942 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6943 --lnum;
6944 rettv->vval.v_number = lnum;
6945}
6946
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006947// This dummy va_list is here because:
6948// - passing a NULL pointer doesn't work when va_list isn't a pointer
6949// - locally in the function results in a "used before set" warning
6950// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006951static va_list ap;
6952
6953/*
6954 * "printf()" function
6955 */
6956 static void
6957f_printf(typval_T *argvars, typval_T *rettv)
6958{
6959 char_u buf[NUMBUFLEN];
6960 int len;
6961 char_u *s;
6962 int saved_did_emsg = did_emsg;
6963 char *fmt;
6964
6965 rettv->v_type = VAR_STRING;
6966 rettv->vval.v_string = NULL;
6967
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006968 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006969 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006970 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006971 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006972 if (!did_emsg)
6973 {
6974 s = alloc(len + 1);
6975 if (s != NULL)
6976 {
6977 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006978 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
6979 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006980 }
6981 }
6982 did_emsg |= saved_did_emsg;
6983}
6984
6985/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006986 * "pum_getpos()" function
6987 */
6988 static void
6989f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6990{
6991 if (rettv_dict_alloc(rettv) != OK)
6992 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006993 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006994}
6995
6996/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006997 * "pumvisible()" function
6998 */
6999 static void
7000f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7001{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007002 if (pum_visible())
7003 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007004}
7005
7006#ifdef FEAT_PYTHON3
7007/*
7008 * "py3eval()" function
7009 */
7010 static void
7011f_py3eval(typval_T *argvars, typval_T *rettv)
7012{
7013 char_u *str;
7014 char_u buf[NUMBUFLEN];
7015
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007016 if (check_restricted() || check_secure())
7017 return;
7018
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007019 if (p_pyx == 0)
7020 p_pyx = 3;
7021
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007022 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007023 do_py3eval(str, rettv);
7024}
7025#endif
7026
7027#ifdef FEAT_PYTHON
7028/*
7029 * "pyeval()" function
7030 */
7031 static void
7032f_pyeval(typval_T *argvars, typval_T *rettv)
7033{
7034 char_u *str;
7035 char_u buf[NUMBUFLEN];
7036
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007037 if (check_restricted() || check_secure())
7038 return;
7039
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007040 if (p_pyx == 0)
7041 p_pyx = 2;
7042
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007043 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007044 do_pyeval(str, rettv);
7045}
7046#endif
7047
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007048#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
7049/*
7050 * "pyxeval()" function
7051 */
7052 static void
7053f_pyxeval(typval_T *argvars, typval_T *rettv)
7054{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007055 if (check_restricted() || check_secure())
7056 return;
7057
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007058# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
7059 init_pyxversion();
7060 if (p_pyx == 2)
7061 f_pyeval(argvars, rettv);
7062 else
7063 f_py3eval(argvars, rettv);
7064# elif defined(FEAT_PYTHON)
7065 f_pyeval(argvars, rettv);
7066# elif defined(FEAT_PYTHON3)
7067 f_py3eval(argvars, rettv);
7068# endif
7069}
7070#endif
7071
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007072static UINT32_T srand_seed_for_testing = 0;
7073static int srand_seed_for_testing_is_used = FALSE;
7074
7075 static void
7076f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
7077{
7078 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007079 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007080 else
7081 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007082 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
7083 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007084 }
7085}
7086
7087 static void
7088init_srand(UINT32_T *x)
7089{
7090#ifndef MSWIN
7091 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
7092#endif
7093
7094 if (srand_seed_for_testing_is_used)
7095 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007096 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007097 return;
7098 }
7099#ifndef MSWIN
7100 if (dev_urandom_state != FAIL)
7101 {
7102 int fd = open("/dev/urandom", O_RDONLY);
7103 struct {
7104 union {
7105 UINT32_T number;
7106 char bytes[sizeof(UINT32_T)];
7107 } contents;
7108 } buf;
7109
7110 // Attempt reading /dev/urandom.
7111 if (fd == -1)
7112 dev_urandom_state = FAIL;
7113 else
7114 {
7115 buf.contents.number = 0;
7116 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
7117 != sizeof(UINT32_T))
7118 dev_urandom_state = FAIL;
7119 else
7120 {
7121 dev_urandom_state = OK;
7122 *x = buf.contents.number;
7123 }
7124 close(fd);
7125 }
7126 }
7127 if (dev_urandom_state != OK)
7128 // Reading /dev/urandom doesn't work, fall back to time().
7129#endif
7130 *x = vim_time();
7131}
7132
7133#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
7134#define SPLITMIX32(x, z) ( \
7135 z = (x += 0x9e3779b9), \
7136 z = (z ^ (z >> 16)) * 0x85ebca6b, \
7137 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
7138 z ^ (z >> 16) \
7139 )
7140#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
7141 result = ROTL(y * 5, 7) * 9; \
7142 t = y << 9; \
7143 z ^= x; \
7144 w ^= y; \
7145 y ^= z, x ^= w; \
7146 z ^= t; \
7147 w = ROTL(w, 11);
7148
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007149/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007150 * "rand()" function
7151 */
7152 static void
7153f_rand(typval_T *argvars, typval_T *rettv)
7154{
7155 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007156 static UINT32_T gx, gy, gz, gw;
7157 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007158 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01007159 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007160
7161 if (argvars[0].v_type == VAR_UNKNOWN)
7162 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007163 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007164 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007165 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007166 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007167 init_srand(&x);
7168
7169 gx = SPLITMIX32(x, z);
7170 gy = SPLITMIX32(x, z);
7171 gz = SPLITMIX32(x, z);
7172 gw = SPLITMIX32(x, z);
7173 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007174 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007175
7176 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007177 }
7178 else if (argvars[0].v_type == VAR_LIST)
7179 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007180 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007181 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007182 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007183
7184 lx = list_find(l, 0L);
7185 ly = list_find(l, 1L);
7186 lz = list_find(l, 2L);
7187 lw = list_find(l, 3L);
7188 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
7189 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
7190 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
7191 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
7192 x = (UINT32_T)lx->li_tv.vval.v_number;
7193 y = (UINT32_T)ly->li_tv.vval.v_number;
7194 z = (UINT32_T)lz->li_tv.vval.v_number;
7195 w = (UINT32_T)lw->li_tv.vval.v_number;
7196
7197 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
7198
7199 lx->li_tv.vval.v_number = (varnumber_T)x;
7200 ly->li_tv.vval.v_number = (varnumber_T)y;
7201 lz->li_tv.vval.v_number = (varnumber_T)z;
7202 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007203 }
7204 else
7205 goto theend;
7206
7207 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007208 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007209 return;
7210
7211theend:
7212 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007213 rettv->v_type = VAR_NUMBER;
7214 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007215}
7216
7217/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007218 * "srand()" function
7219 */
7220 static void
7221f_srand(typval_T *argvars, typval_T *rettv)
7222{
7223 UINT32_T x = 0, z;
7224
7225 if (rettv_list_alloc(rettv) == FAIL)
7226 return;
7227 if (argvars[0].v_type == VAR_UNKNOWN)
7228 {
7229 init_srand(&x);
7230 }
7231 else
7232 {
7233 int error = FALSE;
7234
7235 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7236 if (error)
7237 return;
7238 }
7239
7240 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7241 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7242 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7243 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7244}
7245
7246#undef ROTL
7247#undef SPLITMIX32
7248#undef SHUFFLE_XOSHIRO128STARSTAR
7249
7250/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007251 * "range()" function
7252 */
7253 static void
7254f_range(typval_T *argvars, typval_T *rettv)
7255{
7256 varnumber_T start;
7257 varnumber_T end;
7258 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007259 int error = FALSE;
7260
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007261 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007262 if (argvars[1].v_type == VAR_UNKNOWN)
7263 {
7264 end = start - 1;
7265 start = 0;
7266 }
7267 else
7268 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007269 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007270 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007271 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007272 }
7273
7274 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007275 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007276 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007277 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007278 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007279 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007280 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007281 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007282 list_T *list = rettv->vval.v_list;
7283
7284 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007285 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007286 // be called.
7287 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007288 list->lv_u.nonmat.lv_start = start;
7289 list->lv_u.nonmat.lv_end = end;
7290 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007291 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007292 }
7293}
7294
7295/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007296 * Materialize "list".
7297 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007298 */
7299 void
7300range_list_materialize(list_T *list)
7301{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007302 varnumber_T start = list->lv_u.nonmat.lv_start;
7303 varnumber_T end = list->lv_u.nonmat.lv_end;
7304 int stride = list->lv_u.nonmat.lv_stride;
7305 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007306
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007307 list->lv_first = NULL;
7308 list->lv_u.mat.lv_last = NULL;
7309 list->lv_len = 0;
7310 list->lv_u.mat.lv_idx_item = NULL;
7311 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7312 if (list_append_number(list, (varnumber_T)i) == FAIL)
7313 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007314}
7315
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007316/*
7317 * "getreginfo()" function
7318 */
7319 static void
7320f_getreginfo(typval_T *argvars, typval_T *rettv)
7321{
7322 char_u *strregname;
7323 int regname;
7324 char_u buf[NUMBUFLEN + 2];
7325 long reglen = 0;
7326 dict_T *dict;
7327 list_T *list;
7328
7329 if (argvars[0].v_type != VAR_UNKNOWN)
7330 {
7331 strregname = tv_get_string_chk(&argvars[0]);
7332 if (strregname == NULL)
7333 return;
7334 }
7335 else
7336 strregname = get_vim_var_str(VV_REG);
7337
7338 regname = (strregname == NULL ? '"' : *strregname);
7339 if (regname == 0 || regname == '@')
7340 regname = '"';
7341
7342 if (rettv_dict_alloc(rettv) == FAIL)
7343 return;
7344 dict = rettv->vval.v_dict;
7345
7346 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7347 if (list == NULL)
7348 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007349 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007350
7351 buf[0] = NUL;
7352 buf[1] = NUL;
7353 switch (get_reg_type(regname, &reglen))
7354 {
7355 case MLINE: buf[0] = 'V'; break;
7356 case MCHAR: buf[0] = 'v'; break;
7357 case MBLOCK:
7358 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7359 reglen + 1);
7360 break;
7361 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007362 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007363
7364 buf[0] = get_register_name(get_unname_register());
7365 buf[1] = NUL;
7366 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007367 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007368 else
7369 {
7370 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7371
7372 if (item != NULL)
7373 {
7374 item->di_tv.v_type = VAR_SPECIAL;
7375 item->di_tv.vval.v_number = regname == buf[0]
7376 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007377 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007378 }
7379 }
7380}
7381
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007382 static void
7383return_register(int regname, typval_T *rettv)
7384{
7385 char_u buf[2] = {0, 0};
7386
7387 buf[0] = (char_u)regname;
7388 rettv->v_type = VAR_STRING;
7389 rettv->vval.v_string = vim_strsave(buf);
7390}
7391
7392/*
7393 * "reg_executing()" function
7394 */
7395 static void
7396f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7397{
7398 return_register(reg_executing, rettv);
7399}
7400
7401/*
7402 * "reg_recording()" function
7403 */
7404 static void
7405f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7406{
7407 return_register(reg_recording, rettv);
7408}
7409
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007410/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007411 * "rename({from}, {to})" function
7412 */
7413 static void
7414f_rename(typval_T *argvars, typval_T *rettv)
7415{
7416 char_u buf[NUMBUFLEN];
7417
7418 if (check_restricted() || check_secure())
7419 rettv->vval.v_number = -1;
7420 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007421 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7422 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007423}
7424
7425/*
7426 * "repeat()" function
7427 */
7428 static void
7429f_repeat(typval_T *argvars, typval_T *rettv)
7430{
7431 char_u *p;
7432 int n;
7433 int slen;
7434 int len;
7435 char_u *r;
7436 int i;
7437
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007438 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007439 if (argvars[0].v_type == VAR_LIST)
7440 {
7441 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7442 while (n-- > 0)
7443 if (list_extend(rettv->vval.v_list,
7444 argvars[0].vval.v_list, NULL) == FAIL)
7445 break;
7446 }
7447 else
7448 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007449 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007450 rettv->v_type = VAR_STRING;
7451 rettv->vval.v_string = NULL;
7452
7453 slen = (int)STRLEN(p);
7454 len = slen * n;
7455 if (len <= 0)
7456 return;
7457
7458 r = alloc(len + 1);
7459 if (r != NULL)
7460 {
7461 for (i = 0; i < n; i++)
7462 mch_memmove(r + i * slen, p, (size_t)slen);
7463 r[len] = NUL;
7464 }
7465
7466 rettv->vval.v_string = r;
7467 }
7468}
7469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007470#define SP_NOMOVE 0x01 // don't move cursor
7471#define SP_REPEAT 0x02 // repeat to find outer pair
7472#define SP_RETCOUNT 0x04 // return matchcount
7473#define SP_SETPCMARK 0x08 // set previous context mark
7474#define SP_START 0x10 // accept match at start position
7475#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7476#define SP_END 0x40 // leave cursor at end of match
7477#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007478
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007479/*
7480 * Get flags for a search function.
7481 * Possibly sets "p_ws".
7482 * Returns BACKWARD, FORWARD or zero (for an error).
7483 */
7484 static int
7485get_search_arg(typval_T *varp, int *flagsp)
7486{
7487 int dir = FORWARD;
7488 char_u *flags;
7489 char_u nbuf[NUMBUFLEN];
7490 int mask;
7491
7492 if (varp->v_type != VAR_UNKNOWN)
7493 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007494 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007495 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007496 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007497 while (*flags != NUL)
7498 {
7499 switch (*flags)
7500 {
7501 case 'b': dir = BACKWARD; break;
7502 case 'w': p_ws = TRUE; break;
7503 case 'W': p_ws = FALSE; break;
7504 default: mask = 0;
7505 if (flagsp != NULL)
7506 switch (*flags)
7507 {
7508 case 'c': mask = SP_START; break;
7509 case 'e': mask = SP_END; break;
7510 case 'm': mask = SP_RETCOUNT; break;
7511 case 'n': mask = SP_NOMOVE; break;
7512 case 'p': mask = SP_SUBPAT; break;
7513 case 'r': mask = SP_REPEAT; break;
7514 case 's': mask = SP_SETPCMARK; break;
7515 case 'z': mask = SP_COLUMN; break;
7516 }
7517 if (mask == 0)
7518 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007519 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007520 dir = 0;
7521 }
7522 else
7523 *flagsp |= mask;
7524 }
7525 if (dir == 0)
7526 break;
7527 ++flags;
7528 }
7529 }
7530 return dir;
7531}
7532
7533/*
7534 * Shared by search() and searchpos() functions.
7535 */
7536 static int
7537search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7538{
7539 int flags;
7540 char_u *pat;
7541 pos_T pos;
7542 pos_T save_cursor;
7543 int save_p_ws = p_ws;
7544 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007545 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007546 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007547#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007548 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007549 long time_limit = 0;
7550#endif
7551 int options = SEARCH_KEEP;
7552 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007553 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007554 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007555 pos_T firstpos;
7556
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007557 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007558 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007559 if (dir == 0)
7560 goto theend;
7561 flags = *flagsp;
7562 if (flags & SP_START)
7563 options |= SEARCH_START;
7564 if (flags & SP_END)
7565 options |= SEARCH_END;
7566 if (flags & SP_COLUMN)
7567 options |= SEARCH_COL;
7568
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007569 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007570 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7571 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007572 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007573 if (lnum_stop < 0)
7574 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007575 if (argvars[3].v_type != VAR_UNKNOWN)
7576 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007577#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007578 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007579 if (time_limit < 0)
7580 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007581#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007582 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007583 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007584 }
7585
7586#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007587 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007588 profile_setlimit(time_limit, &tm);
7589#endif
7590
7591 /*
7592 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7593 * Check to make sure only those flags are set.
7594 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7595 * flags cannot be set. Check for that condition also.
7596 */
7597 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7598 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7599 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007600 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007601 goto theend;
7602 }
7603
7604 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007605 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007606 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007607 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7608#ifdef FEAT_RELTIME
7609 sia.sa_tm = &tm;
7610#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007611
7612 // Repeat until {skip} returns FALSE.
7613 for (;;)
7614 {
7615 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007616 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007617 // finding the first match again means there is no match where {skip}
7618 // evaluates to zero.
7619 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7620 subpatnum = FAIL;
7621
Bram Moolenaara9c01042020-06-07 14:50:50 +02007622 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007623 // didn't find it or no skip argument
7624 break;
7625 firstpos = pos;
7626
Bram Moolenaara9c01042020-06-07 14:50:50 +02007627 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007628 {
7629 int do_skip;
7630 int err;
7631 pos_T save_pos = curwin->w_cursor;
7632
7633 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007634 err = FALSE;
7635 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007636 curwin->w_cursor = save_pos;
7637 if (err)
7638 {
7639 // Evaluating {skip} caused an error, break here.
7640 subpatnum = FAIL;
7641 break;
7642 }
7643 if (!do_skip)
7644 break;
7645 }
7646 }
7647
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007648 if (subpatnum != FAIL)
7649 {
7650 if (flags & SP_SUBPAT)
7651 retval = subpatnum;
7652 else
7653 retval = pos.lnum;
7654 if (flags & SP_SETPCMARK)
7655 setpcmark();
7656 curwin->w_cursor = pos;
7657 if (match_pos != NULL)
7658 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007659 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007660 match_pos->lnum = pos.lnum;
7661 match_pos->col = pos.col + 1;
7662 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007663 // "/$" will put the cursor after the end of the line, may need to
7664 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007665 check_cursor();
7666 }
7667
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007668 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007669 if (flags & SP_NOMOVE)
7670 curwin->w_cursor = save_cursor;
7671 else
7672 curwin->w_set_curswant = TRUE;
7673theend:
7674 p_ws = save_p_ws;
7675
7676 return retval;
7677}
7678
7679#ifdef FEAT_FLOAT
7680
7681/*
7682 * round() is not in C90, use ceil() or floor() instead.
7683 */
7684 float_T
7685vim_round(float_T f)
7686{
7687 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
7688}
7689
7690/*
7691 * "round({float})" function
7692 */
7693 static void
7694f_round(typval_T *argvars, typval_T *rettv)
7695{
7696 float_T f = 0.0;
7697
7698 rettv->v_type = VAR_FLOAT;
7699 if (get_float_arg(argvars, &f) == OK)
7700 rettv->vval.v_float = vim_round(f);
7701 else
7702 rettv->vval.v_float = 0.0;
7703}
7704#endif
7705
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007706#ifdef FEAT_RUBY
7707/*
7708 * "rubyeval()" function
7709 */
7710 static void
7711f_rubyeval(typval_T *argvars, typval_T *rettv)
7712{
7713 char_u *str;
7714 char_u buf[NUMBUFLEN];
7715
7716 str = tv_get_string_buf(&argvars[0], buf);
7717 do_rubyeval(str, rettv);
7718}
7719#endif
7720
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007721/*
7722 * "screenattr()" function
7723 */
7724 static void
7725f_screenattr(typval_T *argvars, typval_T *rettv)
7726{
7727 int row;
7728 int col;
7729 int c;
7730
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007731 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7732 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007733 if (row < 0 || row >= screen_Rows
7734 || col < 0 || col >= screen_Columns)
7735 c = -1;
7736 else
7737 c = ScreenAttrs[LineOffset[row] + col];
7738 rettv->vval.v_number = c;
7739}
7740
7741/*
7742 * "screenchar()" function
7743 */
7744 static void
7745f_screenchar(typval_T *argvars, typval_T *rettv)
7746{
7747 int row;
7748 int col;
7749 int off;
7750 int c;
7751
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007752 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7753 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007754 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007755 c = -1;
7756 else
7757 {
7758 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007759 if (enc_utf8 && ScreenLinesUC[off] != 0)
7760 c = ScreenLinesUC[off];
7761 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007762 c = ScreenLines[off];
7763 }
7764 rettv->vval.v_number = c;
7765}
7766
7767/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007768 * "screenchars()" function
7769 */
7770 static void
7771f_screenchars(typval_T *argvars, typval_T *rettv)
7772{
7773 int row;
7774 int col;
7775 int off;
7776 int c;
7777 int i;
7778
7779 if (rettv_list_alloc(rettv) == FAIL)
7780 return;
7781 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7782 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7783 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7784 return;
7785
7786 off = LineOffset[row] + col;
7787 if (enc_utf8 && ScreenLinesUC[off] != 0)
7788 c = ScreenLinesUC[off];
7789 else
7790 c = ScreenLines[off];
7791 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7792
7793 if (enc_utf8)
7794
7795 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7796 list_append_number(rettv->vval.v_list,
7797 (varnumber_T)ScreenLinesC[i][off]);
7798}
7799
7800/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007801 * "screencol()" function
7802 *
7803 * First column is 1 to be consistent with virtcol().
7804 */
7805 static void
7806f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7807{
7808 rettv->vval.v_number = screen_screencol() + 1;
7809}
7810
7811/*
7812 * "screenrow()" function
7813 */
7814 static void
7815f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7816{
7817 rettv->vval.v_number = screen_screenrow() + 1;
7818}
7819
7820/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007821 * "screenstring()" function
7822 */
7823 static void
7824f_screenstring(typval_T *argvars, typval_T *rettv)
7825{
7826 int row;
7827 int col;
7828 int off;
7829 int c;
7830 int i;
7831 char_u buf[MB_MAXBYTES + 1];
7832 int buflen = 0;
7833
7834 rettv->vval.v_string = NULL;
7835 rettv->v_type = VAR_STRING;
7836
7837 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7838 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7839 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7840 return;
7841
7842 off = LineOffset[row] + col;
7843 if (enc_utf8 && ScreenLinesUC[off] != 0)
7844 c = ScreenLinesUC[off];
7845 else
7846 c = ScreenLines[off];
7847 buflen += mb_char2bytes(c, buf);
7848
7849 if (enc_utf8)
7850 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7851 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7852
7853 buf[buflen] = NUL;
7854 rettv->vval.v_string = vim_strsave(buf);
7855}
7856
7857/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007858 * "search()" function
7859 */
7860 static void
7861f_search(typval_T *argvars, typval_T *rettv)
7862{
7863 int flags = 0;
7864
7865 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7866}
7867
7868/*
7869 * "searchdecl()" function
7870 */
7871 static void
7872f_searchdecl(typval_T *argvars, typval_T *rettv)
7873{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007874 int locally = TRUE;
7875 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007876 int error = FALSE;
7877 char_u *name;
7878
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007879 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007880
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007881 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007882 if (argvars[1].v_type != VAR_UNKNOWN)
7883 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007884 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007885 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007886 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007887 }
7888 if (!error && name != NULL)
7889 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7890 locally, thisblock, SEARCH_KEEP) == FAIL;
7891}
7892
7893/*
7894 * Used by searchpair() and searchpairpos()
7895 */
7896 static int
7897searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7898{
7899 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007900 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007901 int save_p_ws = p_ws;
7902 int dir;
7903 int flags = 0;
7904 char_u nbuf1[NUMBUFLEN];
7905 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007906 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007907 long lnum_stop = 0;
7908 long time_limit = 0;
7909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007910 // Get the three pattern arguments: start, middle, end. Will result in an
7911 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007912 spat = tv_get_string_chk(&argvars[0]);
7913 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7914 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007915 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007916 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007917
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007918 // Handle the optional fourth argument: flags
7919 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007920 if (dir == 0)
7921 goto theend;
7922
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007923 // Don't accept SP_END or SP_SUBPAT.
7924 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007925 if ((flags & (SP_END | SP_SUBPAT)) != 0
7926 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7927 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007928 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007929 goto theend;
7930 }
7931
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007932 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007933 if (flags & SP_REPEAT)
7934 p_ws = FALSE;
7935
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007936 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007937 if (argvars[3].v_type == VAR_UNKNOWN
7938 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007939 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007940 else
7941 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007942 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007943 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007944
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007945 if (argvars[5].v_type != VAR_UNKNOWN)
7946 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007947 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007948 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007949 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007950 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007951 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007952 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007953#ifdef FEAT_RELTIME
7954 if (argvars[6].v_type != VAR_UNKNOWN)
7955 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007956 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007957 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007958 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007959 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007960 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007961 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007962 }
7963#endif
7964 }
7965 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007966
7967 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
7968 match_pos, lnum_stop, time_limit);
7969
7970theend:
7971 p_ws = save_p_ws;
7972
7973 return retval;
7974}
7975
7976/*
7977 * "searchpair()" function
7978 */
7979 static void
7980f_searchpair(typval_T *argvars, typval_T *rettv)
7981{
7982 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
7983}
7984
7985/*
7986 * "searchpairpos()" function
7987 */
7988 static void
7989f_searchpairpos(typval_T *argvars, typval_T *rettv)
7990{
7991 pos_T match_pos;
7992 int lnum = 0;
7993 int col = 0;
7994
7995 if (rettv_list_alloc(rettv) == FAIL)
7996 return;
7997
7998 if (searchpair_cmn(argvars, &match_pos) > 0)
7999 {
8000 lnum = match_pos.lnum;
8001 col = match_pos.col;
8002 }
8003
8004 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8005 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8006}
8007
8008/*
8009 * Search for a start/middle/end thing.
8010 * Used by searchpair(), see its documentation for the details.
8011 * Returns 0 or -1 for no match,
8012 */
8013 long
8014do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008015 char_u *spat, // start pattern
8016 char_u *mpat, // middle pattern
8017 char_u *epat, // end pattern
8018 int dir, // BACKWARD or FORWARD
8019 typval_T *skip, // skip expression
8020 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008021 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008022 linenr_T lnum_stop, // stop at this line if not zero
8023 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008024{
8025 char_u *save_cpo;
8026 char_u *pat, *pat2 = NULL, *pat3 = NULL;
8027 long retval = 0;
8028 pos_T pos;
8029 pos_T firstpos;
8030 pos_T foundpos;
8031 pos_T save_cursor;
8032 pos_T save_pos;
8033 int n;
8034 int r;
8035 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01008036 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008037 int err;
8038 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008039#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008040 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008041#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008042
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008043 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008044 save_cpo = p_cpo;
8045 p_cpo = empty_option;
8046
8047#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008048 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008049 profile_setlimit(time_limit, &tm);
8050#endif
8051
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008052 // Make two search patterns: start/end (pat2, for in nested pairs) and
8053 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02008054 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
8055 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008056 if (pat2 == NULL || pat3 == NULL)
8057 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008058 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008059 if (*mpat == NUL)
8060 STRCPY(pat3, pat2);
8061 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008062 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008063 spat, epat, mpat);
8064 if (flags & SP_START)
8065 options |= SEARCH_START;
8066
Bram Moolenaar48570482017-10-30 21:48:41 +01008067 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02008068 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01008069
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008070 save_cursor = curwin->w_cursor;
8071 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008072 CLEAR_POS(&firstpos);
8073 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008074 pat = pat3;
8075 for (;;)
8076 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008077 searchit_arg_T sia;
8078
Bram Moolenaara80faa82020-04-12 19:37:17 +02008079 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008080 sia.sa_stop_lnum = lnum_stop;
8081#ifdef FEAT_RELTIME
8082 sia.sa_tm = &tm;
8083#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01008084 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008085 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008086 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008087 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008088 break;
8089
8090 if (firstpos.lnum == 0)
8091 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008092 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008093 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008094 // Found the same position again. Can happen with a pattern that
8095 // has "\zs" at the end and searching backwards. Advance one
8096 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008097 if (dir == BACKWARD)
8098 decl(&pos);
8099 else
8100 incl(&pos);
8101 }
8102 foundpos = pos;
8103
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008104 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008105 options &= ~SEARCH_START;
8106
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008107 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01008108 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008109 {
8110 save_pos = curwin->w_cursor;
8111 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01008112 err = FALSE;
8113 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008114 curwin->w_cursor = save_pos;
8115 if (err)
8116 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008117 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118 curwin->w_cursor = save_cursor;
8119 retval = -1;
8120 break;
8121 }
8122 if (r)
8123 continue;
8124 }
8125
8126 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8127 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008128 // Found end when searching backwards or start when searching
8129 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008130 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008131 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008132 }
8133 else
8134 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008135 // Found end when searching forward or start when searching
8136 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008137 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008138 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008139 }
8140
8141 if (nest == 0)
8142 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008143 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008144 if (flags & SP_RETCOUNT)
8145 ++retval;
8146 else
8147 retval = pos.lnum;
8148 if (flags & SP_SETPCMARK)
8149 setpcmark();
8150 curwin->w_cursor = pos;
8151 if (!(flags & SP_REPEAT))
8152 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008153 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008154 }
8155 }
8156
8157 if (match_pos != NULL)
8158 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008159 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008160 match_pos->lnum = curwin->w_cursor.lnum;
8161 match_pos->col = curwin->w_cursor.col + 1;
8162 }
8163
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008164 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165 if ((flags & SP_NOMOVE) || retval == 0)
8166 curwin->w_cursor = save_cursor;
8167
8168theend:
8169 vim_free(pat2);
8170 vim_free(pat3);
8171 if (p_cpo == empty_option)
8172 p_cpo = save_cpo;
8173 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008174 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008175 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008176 // If it's still empty it was changed and restored, need to restore in
8177 // the complicated way.
8178 if (*p_cpo == NUL)
8179 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008180 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008181 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008182
8183 return retval;
8184}
8185
8186/*
8187 * "searchpos()" function
8188 */
8189 static void
8190f_searchpos(typval_T *argvars, typval_T *rettv)
8191{
8192 pos_T match_pos;
8193 int lnum = 0;
8194 int col = 0;
8195 int n;
8196 int flags = 0;
8197
8198 if (rettv_list_alloc(rettv) == FAIL)
8199 return;
8200
8201 n = search_cmn(argvars, &match_pos, &flags);
8202 if (n > 0)
8203 {
8204 lnum = match_pos.lnum;
8205 col = match_pos.col;
8206 }
8207
8208 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8209 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8210 if (flags & SP_SUBPAT)
8211 list_append_number(rettv->vval.v_list, (varnumber_T)n);
8212}
8213
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008214/*
8215 * Set the cursor or mark position.
8216 * If 'charpos' is TRUE, then use the column number as a character offet.
8217 * Otherwise use the column number as a byte offset.
8218 */
8219 static void
8220set_position(typval_T *argvars, typval_T *rettv, int charpos)
8221{
8222 pos_T pos;
8223 int fnum;
8224 char_u *name;
8225 colnr_T curswant = -1;
8226
8227 rettv->vval.v_number = -1;
8228
8229 name = tv_get_string_chk(argvars);
8230 if (name != NULL)
8231 {
8232 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
8233 {
8234 if (pos.col != MAXCOL && --pos.col < 0)
8235 pos.col = 0;
8236 if ((name[0] == '.' && name[1] == NUL))
8237 {
8238 // set cursor; "fnum" is ignored
8239 curwin->w_cursor = pos;
8240 if (curswant >= 0)
8241 {
8242 curwin->w_curswant = curswant - 1;
8243 curwin->w_set_curswant = FALSE;
8244 }
8245 check_cursor();
8246 rettv->vval.v_number = 0;
8247 }
8248 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8249 {
8250 // set mark
8251 if (setmark_pos(name[1], &pos, fnum) == OK)
8252 rettv->vval.v_number = 0;
8253 }
8254 else
8255 emsg(_(e_invarg));
8256 }
8257 }
8258}
8259/*
8260 * "setcharpos()" function
8261 */
8262 static void
8263f_setcharpos(typval_T *argvars, typval_T *rettv)
8264{
8265 set_position(argvars, rettv, TRUE);
8266}
8267
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008268 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008269f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8270{
8271 dict_T *d;
8272 dictitem_T *di;
8273 char_u *csearch;
8274
8275 if (argvars[0].v_type != VAR_DICT)
8276 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008277 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008278 return;
8279 }
8280
8281 if ((d = argvars[0].vval.v_dict) != NULL)
8282 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008283 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008284 if (csearch != NULL)
8285 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008286 if (enc_utf8)
8287 {
8288 int pcc[MAX_MCO];
8289 int c = utfc_ptr2char(csearch, pcc);
8290
8291 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8292 }
8293 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008294 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008295 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008296 }
8297
8298 di = dict_find(d, (char_u *)"forward", -1);
8299 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008300 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008301 ? FORWARD : BACKWARD);
8302
8303 di = dict_find(d, (char_u *)"until", -1);
8304 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008305 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008306 }
8307}
8308
8309/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008310 * "setcursorcharpos" function
8311 */
8312 static void
8313f_setcursorcharpos(typval_T *argvars, typval_T *rettv UNUSED)
8314{
8315 set_cursorpos(argvars, rettv, TRUE);
8316}
8317
8318/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008319 * "setenv()" function
8320 */
8321 static void
8322f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8323{
8324 char_u namebuf[NUMBUFLEN];
8325 char_u valbuf[NUMBUFLEN];
8326 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8327
8328 if (argvars[1].v_type == VAR_SPECIAL
8329 && argvars[1].vval.v_number == VVAL_NULL)
8330 vim_unsetenv(name);
8331 else
8332 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8333}
8334
8335/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008336 * "setfperm({fname}, {mode})" function
8337 */
8338 static void
8339f_setfperm(typval_T *argvars, typval_T *rettv)
8340{
8341 char_u *fname;
8342 char_u modebuf[NUMBUFLEN];
8343 char_u *mode_str;
8344 int i;
8345 int mask;
8346 int mode = 0;
8347
8348 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008349 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008350 if (fname == NULL)
8351 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008352 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008353 if (mode_str == NULL)
8354 return;
8355 if (STRLEN(mode_str) != 9)
8356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008357 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008358 return;
8359 }
8360
8361 mask = 1;
8362 for (i = 8; i >= 0; --i)
8363 {
8364 if (mode_str[i] != '-')
8365 mode |= mask;
8366 mask = mask << 1;
8367 }
8368 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8369}
8370
8371/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008372 * "setpos()" function
8373 */
8374 static void
8375f_setpos(typval_T *argvars, typval_T *rettv)
8376{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008377 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008378}
8379
8380/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008381 * Translate a register type string to the yank type and block length
8382 */
8383 static int
8384get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8385{
8386 char_u *stropt = *pp;
8387 switch (*stropt)
8388 {
8389 case 'v': case 'c': // character-wise selection
8390 *yank_type = MCHAR;
8391 break;
8392 case 'V': case 'l': // line-wise selection
8393 *yank_type = MLINE;
8394 break;
8395 case 'b': case Ctrl_V: // block-wise selection
8396 *yank_type = MBLOCK;
8397 if (VIM_ISDIGIT(stropt[1]))
8398 {
8399 ++stropt;
8400 *block_len = getdigits(&stropt) - 1;
8401 --stropt;
8402 }
8403 break;
8404 default:
8405 return FAIL;
8406 }
8407 *pp = stropt;
8408 return OK;
8409}
8410
8411/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008412 * "setreg()" function
8413 */
8414 static void
8415f_setreg(typval_T *argvars, typval_T *rettv)
8416{
8417 int regname;
8418 char_u *strregname;
8419 char_u *stropt;
8420 char_u *strval;
8421 int append;
8422 char_u yank_type;
8423 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008424 typval_T *regcontents;
8425 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008427 pointreg = 0;
8428 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008429 block_len = -1;
8430 yank_type = MAUTO;
8431 append = FALSE;
8432
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008433 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008434 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008435
8436 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008437 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008438 regname = *strregname;
8439 if (regname == 0 || regname == '@')
8440 regname = '"';
8441
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008442 if (argvars[1].v_type == VAR_DICT)
8443 {
8444 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008445 dictitem_T *di;
8446
8447 if (d == NULL || d->dv_hashtab.ht_used == 0)
8448 {
8449 // Empty dict, clear the register (like setreg(0, []))
8450 char_u *lstval[2] = {NULL, NULL};
8451 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8452 return;
8453 }
8454
8455 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008456 if (di != NULL)
8457 regcontents = &di->di_tv;
8458
8459 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8460 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008461 {
8462 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8463
8464 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008465 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008466 semsg(_(e_invargval), "value");
8467 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008468 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008469 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008470
8471 if (regname == '"')
8472 {
8473 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8474 if (stropt != NULL)
8475 {
8476 pointreg = *stropt;
8477 regname = pointreg;
8478 }
8479 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008480 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008481 pointreg = regname;
8482 }
8483 else
8484 regcontents = &argvars[1];
8485
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008486 if (argvars[2].v_type != VAR_UNKNOWN)
8487 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008488 if (yank_type != MAUTO)
8489 {
8490 semsg(_(e_toomanyarg), "setreg");
8491 return;
8492 }
8493
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008494 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008495 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008496 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008497 for (; *stropt != NUL; ++stropt)
8498 switch (*stropt)
8499 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008500 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008501 append = TRUE;
8502 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008503 default:
8504 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008505 }
8506 }
8507
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008508 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008509 {
8510 char_u **lstval;
8511 char_u **allocval;
8512 char_u buf[NUMBUFLEN];
8513 char_u **curval;
8514 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008515 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008516 listitem_T *li;
8517 int len;
8518
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008519 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008520 len = ll == NULL ? 0 : ll->lv_len;
8521
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008522 // First half: use for pointers to result lines; second half: use for
8523 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008524 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008525 if (lstval == NULL)
8526 return;
8527 curval = lstval;
8528 allocval = lstval + len + 2;
8529 curallocval = allocval;
8530
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008531 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008533 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008534 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008535 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008536 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008537 if (strval == NULL)
8538 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008539 if (strval == buf)
8540 {
8541 // Need to make a copy, next tv_get_string_buf_chk() will
8542 // overwrite the string.
8543 strval = vim_strsave(buf);
8544 if (strval == NULL)
8545 goto free_lstval;
8546 *curallocval++ = strval;
8547 }
8548 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008549 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008550 }
8551 *curval++ = NULL;
8552
8553 write_reg_contents_lst(regname, lstval, -1,
8554 append, yank_type, block_len);
8555free_lstval:
8556 while (curallocval > allocval)
8557 vim_free(*--curallocval);
8558 vim_free(lstval);
8559 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008560 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008561 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008562 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008563 if (strval == NULL)
8564 return;
8565 write_reg_contents_ex(regname, strval, -1,
8566 append, yank_type, block_len);
8567 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008568 if (pointreg != 0)
8569 get_yank_register(pointreg, TRUE);
8570
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008571 rettv->vval.v_number = 0;
8572}
8573
8574/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008575 * "settagstack()" function
8576 */
8577 static void
8578f_settagstack(typval_T *argvars, typval_T *rettv)
8579{
8580 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8581 win_T *wp;
8582 dict_T *d;
8583 int action = 'r';
8584
8585 rettv->vval.v_number = -1;
8586
8587 // first argument: window number or id
8588 wp = find_win_by_nr_or_id(&argvars[0]);
8589 if (wp == NULL)
8590 return;
8591
8592 // second argument: dict with items to set in the tag stack
8593 if (argvars[1].v_type != VAR_DICT)
8594 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008595 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008596 return;
8597 }
8598 d = argvars[1].vval.v_dict;
8599 if (d == NULL)
8600 return;
8601
8602 // third argument: action - 'a' for append and 'r' for replace.
8603 // default is to replace the stack.
8604 if (argvars[2].v_type == VAR_UNKNOWN)
8605 action = 'r';
8606 else if (argvars[2].v_type == VAR_STRING)
8607 {
8608 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008609 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008610 if (actstr == NULL)
8611 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008612 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8613 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008614 action = *actstr;
8615 else
8616 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008617 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008618 return;
8619 }
8620 }
8621 else
8622 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008623 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008624 return;
8625 }
8626
8627 if (set_tagstack(wp, d, action) == OK)
8628 rettv->vval.v_number = 0;
8629}
8630
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008631#ifdef FEAT_CRYPT
8632/*
8633 * "sha256({string})" function
8634 */
8635 static void
8636f_sha256(typval_T *argvars, typval_T *rettv)
8637{
8638 char_u *p;
8639
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008640 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008641 rettv->vval.v_string = vim_strsave(
8642 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8643 rettv->v_type = VAR_STRING;
8644}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008645#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008646
8647/*
8648 * "shellescape({string})" function
8649 */
8650 static void
8651f_shellescape(typval_T *argvars, typval_T *rettv)
8652{
Bram Moolenaar20615522017-06-05 18:46:26 +02008653 int do_special = non_zero_arg(&argvars[1]);
8654
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008655 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008656 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008657 rettv->v_type = VAR_STRING;
8658}
8659
8660/*
8661 * shiftwidth() function
8662 */
8663 static void
8664f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8665{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008666 rettv->vval.v_number = 0;
8667
8668 if (argvars[0].v_type != VAR_UNKNOWN)
8669 {
8670 long col;
8671
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008672 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008673 if (col < 0)
8674 return; // type error; errmsg already given
8675#ifdef FEAT_VARTABS
8676 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8677 return;
8678#endif
8679 }
8680
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008681 rettv->vval.v_number = get_sw_value(curbuf);
8682}
8683
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008684#ifdef FEAT_FLOAT
8685/*
8686 * "sin()" function
8687 */
8688 static void
8689f_sin(typval_T *argvars, typval_T *rettv)
8690{
8691 float_T f = 0.0;
8692
8693 rettv->v_type = VAR_FLOAT;
8694 if (get_float_arg(argvars, &f) == OK)
8695 rettv->vval.v_float = sin(f);
8696 else
8697 rettv->vval.v_float = 0.0;
8698}
8699
8700/*
8701 * "sinh()" function
8702 */
8703 static void
8704f_sinh(typval_T *argvars, typval_T *rettv)
8705{
8706 float_T f = 0.0;
8707
8708 rettv->v_type = VAR_FLOAT;
8709 if (get_float_arg(argvars, &f) == OK)
8710 rettv->vval.v_float = sinh(f);
8711 else
8712 rettv->vval.v_float = 0.0;
8713}
8714#endif
8715
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008716/*
8717 * "soundfold({word})" function
8718 */
8719 static void
8720f_soundfold(typval_T *argvars, typval_T *rettv)
8721{
8722 char_u *s;
8723
8724 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008725 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008726#ifdef FEAT_SPELL
8727 rettv->vval.v_string = eval_soundfold(s);
8728#else
8729 rettv->vval.v_string = vim_strsave(s);
8730#endif
8731}
8732
8733/*
8734 * "spellbadword()" function
8735 */
8736 static void
8737f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8738{
8739 char_u *word = (char_u *)"";
8740 hlf_T attr = HLF_COUNT;
8741 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008742#ifdef FEAT_SPELL
8743 int wo_spell_save = curwin->w_p_spell;
8744
8745 if (!curwin->w_p_spell)
8746 {
8747 did_set_spelllang(curwin);
8748 curwin->w_p_spell = TRUE;
8749 }
8750
8751 if (*curwin->w_s->b_p_spl == NUL)
8752 {
8753 emsg(_(e_no_spell));
8754 curwin->w_p_spell = wo_spell_save;
8755 return;
8756 }
8757#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008758
8759 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008760 {
8761#ifdef FEAT_SPELL
8762 curwin->w_p_spell = wo_spell_save;
8763#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008764 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008765 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008766
8767#ifdef FEAT_SPELL
8768 if (argvars[0].v_type == VAR_UNKNOWN)
8769 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008770 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008771 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8772 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008773 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008774 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008775 curwin->w_set_curswant = TRUE;
8776 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008777 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008778 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008779 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008780 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008781 int capcol = -1;
8782
8783 if (str != NULL)
8784 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008785 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008786 while (*str != NUL)
8787 {
8788 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8789 if (attr != HLF_COUNT)
8790 {
8791 word = str;
8792 break;
8793 }
8794 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008795 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008796 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008797 }
8798 }
8799 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008800 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008801#endif
8802
8803 list_append_string(rettv->vval.v_list, word, len);
8804 list_append_string(rettv->vval.v_list, (char_u *)(
8805 attr == HLF_SPB ? "bad" :
8806 attr == HLF_SPR ? "rare" :
8807 attr == HLF_SPL ? "local" :
8808 attr == HLF_SPC ? "caps" :
8809 ""), -1);
8810}
8811
8812/*
8813 * "spellsuggest()" function
8814 */
8815 static void
8816f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8817{
8818#ifdef FEAT_SPELL
8819 char_u *str;
8820 int typeerr = FALSE;
8821 int maxcount;
8822 garray_T ga;
8823 int i;
8824 listitem_T *li;
8825 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008826 int wo_spell_save = curwin->w_p_spell;
8827
8828 if (!curwin->w_p_spell)
8829 {
8830 did_set_spelllang(curwin);
8831 curwin->w_p_spell = TRUE;
8832 }
8833
8834 if (*curwin->w_s->b_p_spl == NUL)
8835 {
8836 emsg(_(e_no_spell));
8837 curwin->w_p_spell = wo_spell_save;
8838 return;
8839 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840#endif
8841
8842 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008843 {
8844#ifdef FEAT_SPELL
8845 curwin->w_p_spell = wo_spell_save;
8846#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008847 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008848 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008849
8850#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008851 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008852 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008853 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008854 if (argvars[1].v_type != VAR_UNKNOWN)
8855 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008856 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008857 if (maxcount <= 0)
8858 return;
8859 if (argvars[2].v_type != VAR_UNKNOWN)
8860 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008861 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008862 if (typeerr)
8863 return;
8864 }
8865 }
8866 else
8867 maxcount = 25;
8868
8869 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8870
8871 for (i = 0; i < ga.ga_len; ++i)
8872 {
8873 str = ((char_u **)ga.ga_data)[i];
8874
8875 li = listitem_alloc();
8876 if (li == NULL)
8877 vim_free(str);
8878 else
8879 {
8880 li->li_tv.v_type = VAR_STRING;
8881 li->li_tv.v_lock = 0;
8882 li->li_tv.vval.v_string = str;
8883 list_append(rettv->vval.v_list, li);
8884 }
8885 }
8886 ga_clear(&ga);
8887 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008888 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008889#endif
8890}
8891
8892 static void
8893f_split(typval_T *argvars, typval_T *rettv)
8894{
8895 char_u *str;
8896 char_u *end;
8897 char_u *pat = NULL;
8898 regmatch_T regmatch;
8899 char_u patbuf[NUMBUFLEN];
8900 char_u *save_cpo;
8901 int match;
8902 colnr_T col = 0;
8903 int keepempty = FALSE;
8904 int typeerr = FALSE;
8905
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008906 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008907 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008908 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008909
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008910 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008911 if (argvars[1].v_type != VAR_UNKNOWN)
8912 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008913 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008914 if (pat == NULL)
8915 typeerr = TRUE;
8916 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008917 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008918 }
8919 if (pat == NULL || *pat == NUL)
8920 pat = (char_u *)"[\\x01- ]\\+";
8921
8922 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008923 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008925 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008926
8927 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8928 if (regmatch.regprog != NULL)
8929 {
8930 regmatch.rm_ic = FALSE;
8931 while (*str != NUL || keepempty)
8932 {
8933 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008934 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008935 else
8936 match = vim_regexec_nl(&regmatch, str, col);
8937 if (match)
8938 end = regmatch.startp[0];
8939 else
8940 end = str + STRLEN(str);
8941 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
8942 && *str != NUL && match && end < regmatch.endp[0]))
8943 {
8944 if (list_append_string(rettv->vval.v_list, str,
8945 (int)(end - str)) == FAIL)
8946 break;
8947 }
8948 if (!match)
8949 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01008950 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008951 if (regmatch.endp[0] > str)
8952 col = 0;
8953 else
Bram Moolenaar13505972019-01-24 15:04:48 +01008954 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008955 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008956 str = regmatch.endp[0];
8957 }
8958
8959 vim_regfree(regmatch.regprog);
8960 }
8961
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008962theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008963 p_cpo = save_cpo;
8964}
8965
8966#ifdef FEAT_FLOAT
8967/*
8968 * "sqrt()" function
8969 */
8970 static void
8971f_sqrt(typval_T *argvars, typval_T *rettv)
8972{
8973 float_T f = 0.0;
8974
8975 rettv->v_type = VAR_FLOAT;
8976 if (get_float_arg(argvars, &f) == OK)
8977 rettv->vval.v_float = sqrt(f);
8978 else
8979 rettv->vval.v_float = 0.0;
8980}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01008981#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008982
Bram Moolenaar0387cae2019-11-29 21:07:58 +01008983#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01008984/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008985 * "str2float()" function
8986 */
8987 static void
8988f_str2float(typval_T *argvars, typval_T *rettv)
8989{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008990 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008991 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008992
Bram Moolenaar08243d22017-01-10 16:12:29 +01008993 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008994 p = skipwhite(p + 1);
8995 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01008996 if (isneg)
8997 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008998 rettv->v_type = VAR_FLOAT;
8999}
9000#endif
9001
9002/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02009003 * "str2list()" function
9004 */
9005 static void
9006f_str2list(typval_T *argvars, typval_T *rettv)
9007{
9008 char_u *p;
9009 int utf8 = FALSE;
9010
9011 if (rettv_list_alloc(rettv) == FAIL)
9012 return;
9013
9014 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02009015 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02009016
9017 p = tv_get_string(&argvars[0]);
9018
9019 if (has_mbyte || utf8)
9020 {
9021 int (*ptr2len)(char_u *);
9022 int (*ptr2char)(char_u *);
9023
9024 if (utf8 || enc_utf8)
9025 {
9026 ptr2len = utf_ptr2len;
9027 ptr2char = utf_ptr2char;
9028 }
9029 else
9030 {
9031 ptr2len = mb_ptr2len;
9032 ptr2char = mb_ptr2char;
9033 }
9034
9035 for ( ; *p != NUL; p += (*ptr2len)(p))
9036 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
9037 }
9038 else
9039 for ( ; *p != NUL; ++p)
9040 list_append_number(rettv->vval.v_list, *p);
9041}
9042
9043/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009044 * "str2nr()" function
9045 */
9046 static void
9047f_str2nr(typval_T *argvars, typval_T *rettv)
9048{
9049 int base = 10;
9050 char_u *p;
9051 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009052 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01009053 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009054
9055 if (argvars[1].v_type != VAR_UNKNOWN)
9056 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009057 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009058 if (base != 2 && base != 8 && base != 10 && base != 16)
9059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009060 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009061 return;
9062 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02009063 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009064 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009065 }
9066
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009067 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009068 isneg = (*p == '-');
9069 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009070 p = skipwhite(p + 1);
9071 switch (base)
9072 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009073 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02009074 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009075 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009076 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02009077 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
9078 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01009079 if (isneg)
9080 rettv->vval.v_number = -n;
9081 else
9082 rettv->vval.v_number = n;
9083
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009084}
9085
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009086/*
9087 * "strgetchar()" function
9088 */
9089 static void
9090f_strgetchar(typval_T *argvars, typval_T *rettv)
9091{
9092 char_u *str;
9093 int len;
9094 int error = FALSE;
9095 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01009096 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009097
9098 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009099 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009100 if (str == NULL)
9101 return;
9102 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009103 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009104 if (error)
9105 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009106
Bram Moolenaar13505972019-01-24 15:04:48 +01009107 while (charidx >= 0 && byteidx < len)
9108 {
9109 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009110 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009111 rettv->vval.v_number = mb_ptr2char(str + byteidx);
9112 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009113 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009114 --charidx;
9115 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009116 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009117}
9118
9119/*
9120 * "stridx()" function
9121 */
9122 static void
9123f_stridx(typval_T *argvars, typval_T *rettv)
9124{
9125 char_u buf[NUMBUFLEN];
9126 char_u *needle;
9127 char_u *haystack;
9128 char_u *save_haystack;
9129 char_u *pos;
9130 int start_idx;
9131
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009132 needle = tv_get_string_chk(&argvars[1]);
9133 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009134 rettv->vval.v_number = -1;
9135 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009136 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009137
9138 if (argvars[2].v_type != VAR_UNKNOWN)
9139 {
9140 int error = FALSE;
9141
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009142 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009143 if (error || start_idx >= (int)STRLEN(haystack))
9144 return;
9145 if (start_idx >= 0)
9146 haystack += start_idx;
9147 }
9148
9149 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9150 if (pos != NULL)
9151 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
9152}
9153
9154/*
9155 * "string()" function
9156 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009157 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009158f_string(typval_T *argvars, typval_T *rettv)
9159{
9160 char_u *tofree;
9161 char_u numbuf[NUMBUFLEN];
9162
9163 rettv->v_type = VAR_STRING;
9164 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
9165 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009166 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009167 if (rettv->vval.v_string != NULL && tofree == NULL)
9168 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
9169}
9170
9171/*
9172 * "strlen()" function
9173 */
9174 static void
9175f_strlen(typval_T *argvars, typval_T *rettv)
9176{
9177 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009178 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009179}
9180
9181/*
9182 * "strchars()" function
9183 */
9184 static void
9185f_strchars(typval_T *argvars, typval_T *rettv)
9186{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009187 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar3986b942020-09-06 16:09:04 +02009188 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009189 varnumber_T len = 0;
9190 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009191
9192 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02009193 skipcc = (int)tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009194 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02009195 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009196 else
9197 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009198 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
9199 while (*s != NUL)
9200 {
9201 func_mb_ptr2char_adv(&s);
9202 ++len;
9203 }
9204 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009205 }
9206}
9207
9208/*
9209 * "strdisplaywidth()" function
9210 */
9211 static void
9212f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
9213{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009214 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009215 int col = 0;
9216
9217 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009218 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009219
9220 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
9221}
9222
9223/*
9224 * "strwidth()" function
9225 */
9226 static void
9227f_strwidth(typval_T *argvars, typval_T *rettv)
9228{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009229 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009230
Bram Moolenaar13505972019-01-24 15:04:48 +01009231 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009232}
9233
9234/*
9235 * "strcharpart()" function
9236 */
9237 static void
9238f_strcharpart(typval_T *argvars, typval_T *rettv)
9239{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009240 char_u *p;
9241 int nchar;
9242 int nbyte = 0;
9243 int charlen;
9244 int len = 0;
9245 int slen;
9246 int error = FALSE;
9247
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009248 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009249 slen = (int)STRLEN(p);
9250
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009251 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009252 if (!error)
9253 {
9254 if (nchar > 0)
9255 while (nchar > 0 && nbyte < slen)
9256 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009257 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009258 --nchar;
9259 }
9260 else
9261 nbyte = nchar;
9262 if (argvars[2].v_type != VAR_UNKNOWN)
9263 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009264 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009265 while (charlen > 0 && nbyte + len < slen)
9266 {
9267 int off = nbyte + len;
9268
9269 if (off < 0)
9270 len += 1;
9271 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009272 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009273 --charlen;
9274 }
9275 }
9276 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009277 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009278 }
9279
9280 /*
9281 * Only return the overlap between the specified part and the actual
9282 * string.
9283 */
9284 if (nbyte < 0)
9285 {
9286 len += nbyte;
9287 nbyte = 0;
9288 }
9289 else if (nbyte > slen)
9290 nbyte = slen;
9291 if (len < 0)
9292 len = 0;
9293 else if (nbyte + len > slen)
9294 len = slen - nbyte;
9295
9296 rettv->v_type = VAR_STRING;
9297 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009298}
9299
9300/*
9301 * "strpart()" function
9302 */
9303 static void
9304f_strpart(typval_T *argvars, typval_T *rettv)
9305{
9306 char_u *p;
9307 int n;
9308 int len;
9309 int slen;
9310 int error = FALSE;
9311
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009312 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009313 slen = (int)STRLEN(p);
9314
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009315 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009316 if (error)
9317 len = 0;
9318 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009319 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009320 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009321 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009322
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009323 // Only return the overlap between the specified part and the actual
9324 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009325 if (n < 0)
9326 {
9327 len += n;
9328 n = 0;
9329 }
9330 else if (n > slen)
9331 n = slen;
9332 if (len < 0)
9333 len = 0;
9334 else if (n + len > slen)
9335 len = slen - n;
9336
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009337 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9338 {
9339 int off;
9340
9341 // length in characters
9342 for (off = n; off < slen && len > 0; --len)
9343 off += mb_ptr2len(p + off);
9344 len = off - n;
9345 }
9346
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009347 rettv->v_type = VAR_STRING;
9348 rettv->vval.v_string = vim_strnsave(p + n, len);
9349}
9350
9351/*
9352 * "strridx()" function
9353 */
9354 static void
9355f_strridx(typval_T *argvars, typval_T *rettv)
9356{
9357 char_u buf[NUMBUFLEN];
9358 char_u *needle;
9359 char_u *haystack;
9360 char_u *rest;
9361 char_u *lastmatch = NULL;
9362 int haystack_len, end_idx;
9363
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009364 needle = tv_get_string_chk(&argvars[1]);
9365 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009366
9367 rettv->vval.v_number = -1;
9368 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009369 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009370
9371 haystack_len = (int)STRLEN(haystack);
9372 if (argvars[2].v_type != VAR_UNKNOWN)
9373 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009374 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009375 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009376 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009377 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009378 }
9379 else
9380 end_idx = haystack_len;
9381
9382 if (*needle == NUL)
9383 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009384 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009385 lastmatch = haystack + end_idx;
9386 }
9387 else
9388 {
9389 for (rest = haystack; *rest != '\0'; ++rest)
9390 {
9391 rest = (char_u *)strstr((char *)rest, (char *)needle);
9392 if (rest == NULL || rest > haystack + end_idx)
9393 break;
9394 lastmatch = rest;
9395 }
9396 }
9397
9398 if (lastmatch == NULL)
9399 rettv->vval.v_number = -1;
9400 else
9401 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9402}
9403
9404/*
9405 * "strtrans()" function
9406 */
9407 static void
9408f_strtrans(typval_T *argvars, typval_T *rettv)
9409{
9410 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009411 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009412}
9413
9414/*
9415 * "submatch()" function
9416 */
9417 static void
9418f_submatch(typval_T *argvars, typval_T *rettv)
9419{
9420 int error = FALSE;
9421 int no;
9422 int retList = 0;
9423
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009424 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009425 if (error)
9426 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009427 if (no < 0 || no >= NSUBEXP)
9428 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009429 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009430 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009431 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009432 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009433 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009434 if (error)
9435 return;
9436
9437 if (retList == 0)
9438 {
9439 rettv->v_type = VAR_STRING;
9440 rettv->vval.v_string = reg_submatch(no);
9441 }
9442 else
9443 {
9444 rettv->v_type = VAR_LIST;
9445 rettv->vval.v_list = reg_submatch_list(no);
9446 }
9447}
9448
9449/*
9450 * "substitute()" function
9451 */
9452 static void
9453f_substitute(typval_T *argvars, typval_T *rettv)
9454{
9455 char_u patbuf[NUMBUFLEN];
9456 char_u subbuf[NUMBUFLEN];
9457 char_u flagsbuf[NUMBUFLEN];
9458
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009459 char_u *str = tv_get_string_chk(&argvars[0]);
9460 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009461 char_u *sub = NULL;
9462 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009463 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009464
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009465 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9466 expr = &argvars[2];
9467 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009468 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009469
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009470 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009471 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9472 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009473 rettv->vval.v_string = NULL;
9474 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009475 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009476}
9477
9478/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009479 * "swapinfo(swap_filename)" function
9480 */
9481 static void
9482f_swapinfo(typval_T *argvars, typval_T *rettv)
9483{
9484 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009485 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009486}
9487
9488/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009489 * "swapname(expr)" function
9490 */
9491 static void
9492f_swapname(typval_T *argvars, typval_T *rettv)
9493{
9494 buf_T *buf;
9495
9496 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009497 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009498 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9499 || buf->b_ml.ml_mfp->mf_fname == NULL)
9500 rettv->vval.v_string = NULL;
9501 else
9502 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9503}
9504
9505/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009506 * "synID(lnum, col, trans)" function
9507 */
9508 static void
9509f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9510{
9511 int id = 0;
9512#ifdef FEAT_SYN_HL
9513 linenr_T lnum;
9514 colnr_T col;
9515 int trans;
9516 int transerr = FALSE;
9517
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009518 lnum = tv_get_lnum(argvars); // -1 on type error
9519 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009520 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009521
9522 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9523 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9524 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9525#endif
9526
9527 rettv->vval.v_number = id;
9528}
9529
9530/*
9531 * "synIDattr(id, what [, mode])" function
9532 */
9533 static void
9534f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9535{
9536 char_u *p = NULL;
9537#ifdef FEAT_SYN_HL
9538 int id;
9539 char_u *what;
9540 char_u *mode;
9541 char_u modebuf[NUMBUFLEN];
9542 int modec;
9543
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009544 id = (int)tv_get_number(&argvars[0]);
9545 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009546 if (argvars[2].v_type != VAR_UNKNOWN)
9547 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009548 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009549 modec = TOLOWER_ASC(mode[0]);
9550 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009551 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009552 }
9553 else
9554 {
9555#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9556 if (USE_24BIT)
9557 modec = 'g';
9558 else
9559#endif
9560 if (t_colors > 1)
9561 modec = 'c';
9562 else
9563 modec = 't';
9564 }
9565
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009566 switch (TOLOWER_ASC(what[0]))
9567 {
9568 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009569 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009570 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009571 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009572 p = highlight_has_attr(id, HL_BOLD, modec);
9573 break;
9574
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009575 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009576 p = highlight_color(id, what, modec);
9577 break;
9578
9579 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009580 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009581 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009582 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009583 p = highlight_has_attr(id, HL_ITALIC, modec);
9584 break;
9585
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009586 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009587 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009588 break;
9589
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009590 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009591 p = highlight_has_attr(id, HL_INVERSE, modec);
9592 break;
9593
9594 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009595 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009596 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009597 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009598 else if (TOLOWER_ASC(what[1]) == 't' &&
9599 TOLOWER_ASC(what[2]) == 'r')
9600 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009601 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009602 p = highlight_has_attr(id, HL_STANDOUT, modec);
9603 break;
9604
9605 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009606 if (TOLOWER_ASC(what[1]) == 'l') // ul
9607 p = highlight_color(id, what, modec);
9608 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009609 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009610 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9611 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009612 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009613 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9614 break;
9615 }
9616
9617 if (p != NULL)
9618 p = vim_strsave(p);
9619#endif
9620 rettv->v_type = VAR_STRING;
9621 rettv->vval.v_string = p;
9622}
9623
9624/*
9625 * "synIDtrans(id)" function
9626 */
9627 static void
9628f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9629{
9630 int id;
9631
9632#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009633 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009634
9635 if (id > 0)
9636 id = syn_get_final_id(id);
9637 else
9638#endif
9639 id = 0;
9640
9641 rettv->vval.v_number = id;
9642}
9643
9644/*
9645 * "synconcealed(lnum, col)" function
9646 */
9647 static void
9648f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9649{
9650#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9651 linenr_T lnum;
9652 colnr_T col;
9653 int syntax_flags = 0;
9654 int cchar;
9655 int matchid = 0;
9656 char_u str[NUMBUFLEN];
9657#endif
9658
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009659 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009660
9661#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009662 lnum = tv_get_lnum(argvars); // -1 on type error
9663 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009664
Bram Moolenaara80faa82020-04-12 19:37:17 +02009665 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009666
9667 if (rettv_list_alloc(rettv) != FAIL)
9668 {
9669 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9670 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9671 && curwin->w_p_cole > 0)
9672 {
9673 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9674 syntax_flags = get_syntax_info(&matchid);
9675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009676 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009677 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9678 {
9679 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009680 if (cchar == NUL && curwin->w_p_cole == 1)
9681 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009682 if (cchar != NUL)
9683 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009684 if (has_mbyte)
9685 (*mb_char2bytes)(cchar, str);
9686 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009687 str[0] = cchar;
9688 }
9689 }
9690 }
9691
9692 list_append_number(rettv->vval.v_list,
9693 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009694 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009695 list_append_string(rettv->vval.v_list, str, -1);
9696 list_append_number(rettv->vval.v_list, matchid);
9697 }
9698#endif
9699}
9700
9701/*
9702 * "synstack(lnum, col)" function
9703 */
9704 static void
9705f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9706{
9707#ifdef FEAT_SYN_HL
9708 linenr_T lnum;
9709 colnr_T col;
9710 int i;
9711 int id;
9712#endif
9713
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009714 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009715
9716#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009717 lnum = tv_get_lnum(argvars); // -1 on type error
9718 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009719
9720 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9721 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9722 && rettv_list_alloc(rettv) != FAIL)
9723 {
9724 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9725 for (i = 0; ; ++i)
9726 {
9727 id = syn_get_stack_item(i);
9728 if (id < 0)
9729 break;
9730 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9731 break;
9732 }
9733 }
9734#endif
9735}
9736
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009737/*
9738 * "tabpagebuflist()" function
9739 */
9740 static void
9741f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9742{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009743 tabpage_T *tp;
9744 win_T *wp = NULL;
9745
9746 if (argvars[0].v_type == VAR_UNKNOWN)
9747 wp = firstwin;
9748 else
9749 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009750 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009751 if (tp != NULL)
9752 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9753 }
9754 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9755 {
9756 for (; wp != NULL; wp = wp->w_next)
9757 if (list_append_number(rettv->vval.v_list,
9758 wp->w_buffer->b_fnum) == FAIL)
9759 break;
9760 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009761}
9762
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009763/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009764 * "tagfiles()" function
9765 */
9766 static void
9767f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9768{
9769 char_u *fname;
9770 tagname_T tn;
9771 int first;
9772
9773 if (rettv_list_alloc(rettv) == FAIL)
9774 return;
9775 fname = alloc(MAXPATHL);
9776 if (fname == NULL)
9777 return;
9778
9779 for (first = TRUE; ; first = FALSE)
9780 if (get_tagfname(&tn, first, fname) == FAIL
9781 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9782 break;
9783 tagname_free(&tn);
9784 vim_free(fname);
9785}
9786
9787/*
9788 * "taglist()" function
9789 */
9790 static void
9791f_taglist(typval_T *argvars, typval_T *rettv)
9792{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009793 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009794 char_u *tag_pattern;
9795
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009796 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009797
9798 rettv->vval.v_number = FALSE;
9799 if (*tag_pattern == NUL)
9800 return;
9801
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009802 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009803 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009804 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009805 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009806}
9807
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009808#ifdef FEAT_FLOAT
9809/*
9810 * "tan()" function
9811 */
9812 static void
9813f_tan(typval_T *argvars, typval_T *rettv)
9814{
9815 float_T f = 0.0;
9816
9817 rettv->v_type = VAR_FLOAT;
9818 if (get_float_arg(argvars, &f) == OK)
9819 rettv->vval.v_float = tan(f);
9820 else
9821 rettv->vval.v_float = 0.0;
9822}
9823
9824/*
9825 * "tanh()" function
9826 */
9827 static void
9828f_tanh(typval_T *argvars, typval_T *rettv)
9829{
9830 float_T f = 0.0;
9831
9832 rettv->v_type = VAR_FLOAT;
9833 if (get_float_arg(argvars, &f) == OK)
9834 rettv->vval.v_float = tanh(f);
9835 else
9836 rettv->vval.v_float = 0.0;
9837}
9838#endif
9839
9840/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009841 * "tolower(string)" function
9842 */
9843 static void
9844f_tolower(typval_T *argvars, typval_T *rettv)
9845{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009846 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009847 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009848}
9849
9850/*
9851 * "toupper(string)" function
9852 */
9853 static void
9854f_toupper(typval_T *argvars, typval_T *rettv)
9855{
9856 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009857 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009858}
9859
9860/*
9861 * "tr(string, fromstr, tostr)" function
9862 */
9863 static void
9864f_tr(typval_T *argvars, typval_T *rettv)
9865{
9866 char_u *in_str;
9867 char_u *fromstr;
9868 char_u *tostr;
9869 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009870 int inlen;
9871 int fromlen;
9872 int tolen;
9873 int idx;
9874 char_u *cpstr;
9875 int cplen;
9876 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009877 char_u buf[NUMBUFLEN];
9878 char_u buf2[NUMBUFLEN];
9879 garray_T ga;
9880
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009881 in_str = tv_get_string(&argvars[0]);
9882 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9883 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009884
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009885 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009886 rettv->v_type = VAR_STRING;
9887 rettv->vval.v_string = NULL;
9888 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009889 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009890 ga_init2(&ga, (int)sizeof(char), 80);
9891
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009892 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009893 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009894 if (STRLEN(fromstr) != STRLEN(tostr))
9895 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009896error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009897 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009898 ga_clear(&ga);
9899 return;
9900 }
9901
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009902 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009903 while (*in_str != NUL)
9904 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009905 if (has_mbyte)
9906 {
9907 inlen = (*mb_ptr2len)(in_str);
9908 cpstr = in_str;
9909 cplen = inlen;
9910 idx = 0;
9911 for (p = fromstr; *p != NUL; p += fromlen)
9912 {
9913 fromlen = (*mb_ptr2len)(p);
9914 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9915 {
9916 for (p = tostr; *p != NUL; p += tolen)
9917 {
9918 tolen = (*mb_ptr2len)(p);
9919 if (idx-- == 0)
9920 {
9921 cplen = tolen;
9922 cpstr = p;
9923 break;
9924 }
9925 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009926 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009927 goto error;
9928 break;
9929 }
9930 ++idx;
9931 }
9932
9933 if (first && cpstr == in_str)
9934 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009935 // Check that fromstr and tostr have the same number of
9936 // (multi-byte) characters. Done only once when a character
9937 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009938 first = FALSE;
9939 for (p = tostr; *p != NUL; p += tolen)
9940 {
9941 tolen = (*mb_ptr2len)(p);
9942 --idx;
9943 }
9944 if (idx != 0)
9945 goto error;
9946 }
9947
9948 (void)ga_grow(&ga, cplen);
9949 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
9950 ga.ga_len += cplen;
9951
9952 in_str += inlen;
9953 }
9954 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009955 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009956 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009957 p = vim_strchr(fromstr, *in_str);
9958 if (p != NULL)
9959 ga_append(&ga, tostr[p - fromstr]);
9960 else
9961 ga_append(&ga, *in_str);
9962 ++in_str;
9963 }
9964 }
9965
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009966 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009967 (void)ga_grow(&ga, 1);
9968 ga_append(&ga, NUL);
9969
9970 rettv->vval.v_string = ga.ga_data;
9971}
9972
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009973/*
9974 * "trim({expr})" function
9975 */
9976 static void
9977f_trim(typval_T *argvars, typval_T *rettv)
9978{
9979 char_u buf1[NUMBUFLEN];
9980 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009981 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009982 char_u *mask = NULL;
9983 char_u *tail;
9984 char_u *prev;
9985 char_u *p;
9986 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009987 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009988
9989 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009990 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009991 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009992 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009993
9994 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009995 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009996 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009997
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009998 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009999 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010000 int error = 0;
10001
10002 // leading or trailing characters to trim
10003 dir = (int)tv_get_number_chk(&argvars[2], &error);
10004 if (error)
10005 return;
10006 if (dir < 0 || dir > 2)
10007 {
10008 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
10009 return;
10010 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010011 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010012 }
10013
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010014 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010015 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010016 // Trim leading characters
10017 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010018 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010019 c1 = PTR2CHAR(head);
10020 if (mask == NULL)
10021 {
10022 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010023 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010024 }
10025 else
10026 {
10027 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10028 if (c1 == PTR2CHAR(p))
10029 break;
10030 if (*p == NUL)
10031 break;
10032 }
10033 MB_PTR_ADV(head);
10034 }
10035 }
10036
10037 tail = head + STRLEN(head);
10038 if (dir == 0 || dir == 2)
10039 {
10040 // Trim trailing characters
10041 for (; tail > head; tail = prev)
10042 {
10043 prev = tail;
10044 MB_PTR_BACK(head, prev);
10045 c1 = PTR2CHAR(prev);
10046 if (mask == NULL)
10047 {
10048 if (c1 > ' ' && c1 != 0xa0)
10049 break;
10050 }
10051 else
10052 {
10053 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10054 if (c1 == PTR2CHAR(p))
10055 break;
10056 if (*p == NUL)
10057 break;
10058 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010059 }
10060 }
Bram Moolenaardf44a272020-06-07 20:49:05 +020010061 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010062}
10063
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010064#ifdef FEAT_FLOAT
10065/*
10066 * "trunc({float})" function
10067 */
10068 static void
10069f_trunc(typval_T *argvars, typval_T *rettv)
10070{
10071 float_T f = 0.0;
10072
10073 rettv->v_type = VAR_FLOAT;
10074 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010075 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010076 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
10077 else
10078 rettv->vval.v_float = 0.0;
10079}
10080#endif
10081
10082/*
10083 * "type(expr)" function
10084 */
10085 static void
10086f_type(typval_T *argvars, typval_T *rettv)
10087{
10088 int n = -1;
10089
10090 switch (argvars[0].v_type)
10091 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010092 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
10093 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010094 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010095 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
10096 case VAR_LIST: n = VAR_TYPE_LIST; break;
10097 case VAR_DICT: n = VAR_TYPE_DICT; break;
10098 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
10099 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
10100 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020010101 case VAR_JOB: n = VAR_TYPE_JOB; break;
10102 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010103 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010104 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +020010105 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010106 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +010010107 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010108 n = -1;
10109 break;
10110 }
10111 rettv->vval.v_number = n;
10112}
10113
10114/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010115 * "virtcol(string)" function
10116 */
10117 static void
10118f_virtcol(typval_T *argvars, typval_T *rettv)
10119{
10120 colnr_T vcol = 0;
10121 pos_T *fp;
10122 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010123 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010124
Bram Moolenaar6f02b002021-01-10 20:22:54 +010010125 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010126 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
10127 && fnum == curbuf->b_fnum)
10128 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010129 // Limit the column to a valid value, getvvcol() doesn't check.
10130 if (fp->col < 0)
10131 fp->col = 0;
10132 else
10133 {
10134 len = (int)STRLEN(ml_get(fp->lnum));
10135 if (fp->col > len)
10136 fp->col = len;
10137 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010138 getvvcol(curwin, fp, NULL, NULL, &vcol);
10139 ++vcol;
10140 }
10141
10142 rettv->vval.v_number = vcol;
10143}
10144
10145/*
10146 * "visualmode()" function
10147 */
10148 static void
10149f_visualmode(typval_T *argvars, typval_T *rettv)
10150{
10151 char_u str[2];
10152
10153 rettv->v_type = VAR_STRING;
10154 str[0] = curbuf->b_visual_mode_eval;
10155 str[1] = NUL;
10156 rettv->vval.v_string = vim_strsave(str);
10157
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010158 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010159 if (non_zero_arg(&argvars[0]))
10160 curbuf->b_visual_mode_eval = NUL;
10161}
10162
10163/*
10164 * "wildmenumode()" function
10165 */
10166 static void
10167f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10168{
10169#ifdef FEAT_WILDMENU
10170 if (wild_menu_showing)
10171 rettv->vval.v_number = 1;
10172#endif
10173}
10174
10175/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +010010176 * "windowsversion()" function
10177 */
10178 static void
10179f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10180{
10181 rettv->v_type = VAR_STRING;
10182 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
10183}
10184
10185/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010186 * "wordcount()" function
10187 */
10188 static void
10189f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
10190{
10191 if (rettv_dict_alloc(rettv) == FAIL)
10192 return;
10193 cursor_pos_info(rettv->vval.v_dict);
10194}
10195
10196/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010197 * "xor(expr, expr)" function
10198 */
10199 static void
10200f_xor(typval_T *argvars, typval_T *rettv)
10201{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010202 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
10203 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010204}
10205
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010206#endif // FEAT_EVAL