blob: 6db93b86ea136dcf5bd9379e41f32bd20b58a7ba [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020023#ifdef FEAT_FLOAT
24static void f_abs(typval_T *argvars, typval_T *rettv);
25static void f_acos(typval_T *argvars, typval_T *rettv);
26#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020027static void f_and(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028#ifdef FEAT_FLOAT
29static void f_asin(typval_T *argvars, typval_T *rettv);
30static void f_atan(typval_T *argvars, typval_T *rettv);
31static void f_atan2(typval_T *argvars, typval_T *rettv);
32#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010033#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020034static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010035static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010036# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010037static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010038# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010039#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_byte2line(typval_T *argvars, typval_T *rettv);
41static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
42static void f_byteidx(typval_T *argvars, typval_T *rettv);
43static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
44static void f_call(typval_T *argvars, typval_T *rettv);
45#ifdef FEAT_FLOAT
46static void f_ceil(typval_T *argvars, typval_T *rettv);
47#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_changenr(typval_T *argvars, typval_T *rettv);
49static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010050static void f_charcol(typval_T *argvars, typval_T *rettv);
Bram Moolenaar17793ef2020-12-28 12:56:58 +010051static void f_charidx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020053static void f_confirm(typval_T *argvars, typval_T *rettv);
54static void f_copy(typval_T *argvars, typval_T *rettv);
55#ifdef FEAT_FLOAT
56static void f_cos(typval_T *argvars, typval_T *rettv);
57static void f_cosh(typval_T *argvars, typval_T *rettv);
58#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010060#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020061static void f_debugbreak(typval_T *argvars, typval_T *rettv);
62#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020063static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020064static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010065static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020066static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020067static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_escape(typval_T *argvars, typval_T *rettv);
69static void f_eval(typval_T *argvars, typval_T *rettv);
70static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_exists(typval_T *argvars, typval_T *rettv);
73#ifdef FEAT_FLOAT
74static void f_exp(typval_T *argvars, typval_T *rettv);
75#endif
76static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020077static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020078static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020079#ifdef FEAT_FLOAT
80static void f_float2nr(typval_T *argvars, typval_T *rettv);
81static void f_floor(typval_T *argvars, typval_T *rettv);
82static void f_fmod(typval_T *argvars, typval_T *rettv);
83#endif
84static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020085static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020086static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_function(typval_T *argvars, typval_T *rettv);
88static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
89static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010090static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010091static void f_getcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020092static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020093static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020094static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010095static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_getpid(typval_T *argvars, typval_T *rettv);
97static void f_getcurpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010098static void f_getcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020099static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200100static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200101static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100103static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +0200104static void f_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200105static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
106static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200107static void f_hlID(typval_T *argvars, typval_T *rettv);
108static void f_hlexists(typval_T *argvars, typval_T *rettv);
109static void f_hostname(typval_T *argvars, typval_T *rettv);
110static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200111static void f_index(typval_T *argvars, typval_T *rettv);
112static void f_input(typval_T *argvars, typval_T *rettv);
113static void f_inputdialog(typval_T *argvars, typval_T *rettv);
114static void f_inputlist(typval_T *argvars, typval_T *rettv);
115static void f_inputrestore(typval_T *argvars, typval_T *rettv);
116static void f_inputsave(typval_T *argvars, typval_T *rettv);
117static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +0100118static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200119static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120static void f_islocked(typval_T *argvars, typval_T *rettv);
121#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200122static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200123static void f_isnan(typval_T *argvars, typval_T *rettv);
124#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200125static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
126static void f_len(typval_T *argvars, typval_T *rettv);
127static void f_libcall(typval_T *argvars, typval_T *rettv);
128static void f_libcallnr(typval_T *argvars, typval_T *rettv);
129static void f_line(typval_T *argvars, typval_T *rettv);
130static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200131#ifdef FEAT_FLOAT
132static void f_log(typval_T *argvars, typval_T *rettv);
133static void f_log10(typval_T *argvars, typval_T *rettv);
134#endif
135#ifdef FEAT_LUA
136static void f_luaeval(typval_T *argvars, typval_T *rettv);
137#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200138static void f_maparg(typval_T *argvars, typval_T *rettv);
139static void f_mapcheck(typval_T *argvars, typval_T *rettv);
140static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200141static void f_matchend(typval_T *argvars, typval_T *rettv);
142static void f_matchlist(typval_T *argvars, typval_T *rettv);
143static void f_matchstr(typval_T *argvars, typval_T *rettv);
144static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
145static void f_max(typval_T *argvars, typval_T *rettv);
146static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200147#ifdef FEAT_MZSCHEME
148static void f_mzeval(typval_T *argvars, typval_T *rettv);
149#endif
150static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
151static void f_nr2char(typval_T *argvars, typval_T *rettv);
152static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200153#ifdef FEAT_PERL
154static void f_perleval(typval_T *argvars, typval_T *rettv);
155#endif
156#ifdef FEAT_FLOAT
157static void f_pow(typval_T *argvars, typval_T *rettv);
158#endif
159static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
160static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200161static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200162static void f_pumvisible(typval_T *argvars, typval_T *rettv);
163#ifdef FEAT_PYTHON3
164static void f_py3eval(typval_T *argvars, typval_T *rettv);
165#endif
166#ifdef FEAT_PYTHON
167static void f_pyeval(typval_T *argvars, typval_T *rettv);
168#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100169#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
170static void f_pyxeval(typval_T *argvars, typval_T *rettv);
171#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100172static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100173static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200175static void f_reg_executing(typval_T *argvars, typval_T *rettv);
176static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_rename(typval_T *argvars, typval_T *rettv);
178static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200179#ifdef FEAT_FLOAT
180static void f_round(typval_T *argvars, typval_T *rettv);
181#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100182#ifdef FEAT_RUBY
183static void f_rubyeval(typval_T *argvars, typval_T *rettv);
184#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200185static void f_screenattr(typval_T *argvars, typval_T *rettv);
186static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100187static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200188static void f_screencol(typval_T *argvars, typval_T *rettv);
189static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100190static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200191static void f_search(typval_T *argvars, typval_T *rettv);
192static void f_searchdecl(typval_T *argvars, typval_T *rettv);
193static void f_searchpair(typval_T *argvars, typval_T *rettv);
194static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
195static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100196static void f_setcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100198static void f_setcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200199static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100203static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200204#ifdef FEAT_CRYPT
205static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200206#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200207static void f_shellescape(typval_T *argvars, typval_T *rettv);
208static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200209#ifdef FEAT_FLOAT
210static void f_sin(typval_T *argvars, typval_T *rettv);
211static void f_sinh(typval_T *argvars, typval_T *rettv);
212#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200213static void f_soundfold(typval_T *argvars, typval_T *rettv);
214static void f_spellbadword(typval_T *argvars, typval_T *rettv);
215static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
216static void f_split(typval_T *argvars, typval_T *rettv);
217#ifdef FEAT_FLOAT
218static void f_sqrt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100219#endif
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100220static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100221#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200222static void f_str2float(typval_T *argvars, typval_T *rettv);
223#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200224static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200225static void f_str2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar70ce8a12021-03-14 19:02:09 +0100226static void f_strcharlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200227static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200228static void f_strgetchar(typval_T *argvars, typval_T *rettv);
229static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200230static void f_strlen(typval_T *argvars, typval_T *rettv);
231static void f_strcharpart(typval_T *argvars, typval_T *rettv);
232static void f_strpart(typval_T *argvars, typval_T *rettv);
233static void f_strridx(typval_T *argvars, typval_T *rettv);
234static void f_strtrans(typval_T *argvars, typval_T *rettv);
235static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
236static void f_strwidth(typval_T *argvars, typval_T *rettv);
237static void f_submatch(typval_T *argvars, typval_T *rettv);
238static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200239static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200240static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200241static void f_synID(typval_T *argvars, typval_T *rettv);
242static void f_synIDattr(typval_T *argvars, typval_T *rettv);
243static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
244static void f_synstack(typval_T *argvars, typval_T *rettv);
245static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200246static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200247static void f_taglist(typval_T *argvars, typval_T *rettv);
248static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200249#ifdef FEAT_FLOAT
250static void f_tan(typval_T *argvars, typval_T *rettv);
251static void f_tanh(typval_T *argvars, typval_T *rettv);
252#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200253static void f_tolower(typval_T *argvars, typval_T *rettv);
254static void f_toupper(typval_T *argvars, typval_T *rettv);
255static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100256static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200257#ifdef FEAT_FLOAT
258static void f_trunc(typval_T *argvars, typval_T *rettv);
259#endif
260static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200261static void f_virtcol(typval_T *argvars, typval_T *rettv);
262static void f_visualmode(typval_T *argvars, typval_T *rettv);
263static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100264static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200265static void f_wordcount(typval_T *argvars, typval_T *rettv);
266static void f_xor(typval_T *argvars, typval_T *rettv);
267
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100268
Bram Moolenaar94738d82020-10-21 14:25:07 +0200269/*
270 * Functions that check the argument type of a builtin function.
271 * Each function returns FAIL and gives an error message if the type is wrong.
272 */
273
274// Context passed to an arg_ function.
275typedef struct {
Bram Moolenaarca174532020-10-21 16:42:22 +0200276 int arg_count; // actual argument count
277 type_T **arg_types; // list of argument types
278 int arg_idx; // current argument index (first arg is zero)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100279 cctx_T *arg_cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +0200280} argcontext_T;
281
282// A function to check one argument type. The first argument is the type to
283// check. If needed, other argument types can be obtained with the context.
284// E.g. if "arg_idx" is 1, then (type - 1) is the first argument type.
285typedef int (*argcheck_T)(type_T *, argcontext_T *);
286
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100287/*
Bram Moolenaar351ead02021-01-16 16:07:01 +0100288 * Call need_type() to check an argument type.
289 */
290 static int
291check_arg_type(
292 type_T *expected,
293 type_T *actual,
294 argcontext_T *context)
295{
296 // TODO: would be useful to know if "actual" is a constant and pass it to
297 // need_type() to get a compile time error if possible.
298 return need_type(actual, expected,
299 context->arg_idx - context->arg_count, context->arg_idx + 1,
300 context->arg_cctx, FALSE, FALSE);
301}
302
303/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100304 * Check "type" is a float or a number.
305 */
Bram Moolenaar94738d82020-10-21 14:25:07 +0200306 static int
307arg_float_or_nr(type_T *type, argcontext_T *context)
308{
Bram Moolenaarca174532020-10-21 16:42:22 +0200309 if (type->tt_type == VAR_ANY
310 || type->tt_type == VAR_FLOAT || type->tt_type == VAR_NUMBER)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200311 return OK;
312 arg_type_mismatch(&t_number, type, context->arg_idx + 1);
313 return FAIL;
314}
315
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100316/*
317 * Check "type" is a number.
318 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200319 static int
320arg_number(type_T *type, argcontext_T *context)
321{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100322 return check_arg_type(&t_number, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200323}
324
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100325/*
326 * Check "type" is a string.
327 */
328 static int
329arg_string(type_T *type, argcontext_T *context)
330{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100331 return check_arg_type(&t_string, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100332}
333
334/*
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100335 * Check "type" is a bool or number 0 or 1.
336 */
337 static int
338arg_bool(type_T *type, argcontext_T *context)
339{
340 if (type->tt_type == VAR_ANY
341 || type->tt_type == VAR_NUMBER || type->tt_type == VAR_BOOL)
342 return OK;
343 return check_arg_type(&t_bool, type, context);
344}
345
346/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100347 * Check "type" is a list or a blob.
348 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200349 static int
350arg_list_or_blob(type_T *type, argcontext_T *context)
351{
352 if (type->tt_type == VAR_ANY
353 || type->tt_type == VAR_LIST || type->tt_type == VAR_BLOB)
354 return OK;
355 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
356 return FAIL;
357}
358
359/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100360 * Check "type" is a list or a dict.
361 */
362 static int
363arg_list_or_dict(type_T *type, argcontext_T *context)
364{
365 if (type->tt_type == VAR_ANY
366 || type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
367 return OK;
368 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
369 return FAIL;
370}
371
372/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100373 * Check "type" is the same type as the previous argument.
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100374 * Must not be used for the first argcheck_T entry.
375 */
376 static int
377arg_same_as_prev(type_T *type, argcontext_T *context)
378{
379 type_T *prev_type = context->arg_types[context->arg_idx - 1];
380
Bram Moolenaar351ead02021-01-16 16:07:01 +0100381 return check_arg_type(prev_type, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100382}
383
384/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100385 * Check "type" is the same basic type as the previous argument, checks list or
386 * dict vs other type, but not member type.
387 * Must not be used for the first argcheck_T entry.
388 */
389 static int
390arg_same_struct_as_prev(type_T *type, argcontext_T *context)
391{
392 type_T *prev_type = context->arg_types[context->arg_idx - 1];
393
394 if (prev_type->tt_type != context->arg_types[context->arg_idx]->tt_type)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100395 return check_arg_type(prev_type, type, context);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100396 return OK;
397}
398
399/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100400 * Check "type" is an item of the list or blob of the previous arg.
Bram Moolenaarca174532020-10-21 16:42:22 +0200401 * Must not be used for the first argcheck_T entry.
402 */
403 static int
404arg_item_of_prev(type_T *type, argcontext_T *context)
405{
406 type_T *prev_type = context->arg_types[context->arg_idx - 1];
407 type_T *expected;
408
409 if (prev_type->tt_type == VAR_LIST)
410 expected = prev_type->tt_member;
411 else if (prev_type->tt_type == VAR_BLOB)
412 expected = &t_number;
413 else
414 // probably VAR_ANY, can't check
415 return OK;
416
Bram Moolenaar351ead02021-01-16 16:07:01 +0100417 return check_arg_type(expected, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200418}
419
Bram Moolenaar94738d82020-10-21 14:25:07 +0200420/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100421 * Check "type" which is the third argument of extend().
422 */
423 static int
424arg_extend3(type_T *type, argcontext_T *context)
425{
426 type_T *first_type = context->arg_types[context->arg_idx - 2];
427
428 if (first_type->tt_type == VAR_LIST)
429 return arg_number(type, context);
430 if (first_type->tt_type == VAR_DICT)
431 return arg_string(type, context);
432 return OK;
433}
434
435
436/*
Bram Moolenaar94738d82020-10-21 14:25:07 +0200437 * Lists of functions that check the argument types of a builtin function.
438 */
Bram Moolenaar80ad3e22021-01-31 20:48:58 +0100439argcheck_T arg1_string[] = {arg_string};
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100440argcheck_T arg3_string_nr_bool[] = {arg_string, arg_number, arg_bool};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200441argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100442argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev};
443argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3};
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100444argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
Bram Moolenaarca174532020-10-21 16:42:22 +0200445argcheck_T arg3_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200446
447/*
448 * Functions that return the return type of a builtin function.
Bram Moolenaara1224cb2020-10-22 12:31:49 +0200449 * Note that "argtypes" is NULL if "argcount" is zero.
Bram Moolenaar94738d82020-10-21 14:25:07 +0200450 */
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100451 static type_T *
452ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
453{
454 return &t_void;
455}
456 static type_T *
457ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
458{
459 return &t_any;
460}
461 static type_T *
Bram Moolenaar403dc312020-10-17 19:29:51 +0200462ret_bool(int argcount UNUSED, type_T **argtypes UNUSED)
463{
464 return &t_bool;
465}
466 static type_T *
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100467ret_number_bool(int argcount UNUSED, type_T **argtypes UNUSED)
468{
469 return &t_number_bool;
470}
471 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100472ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
473{
474 return &t_number;
475}
476 static type_T *
477ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
478{
479 return &t_float;
480}
481 static type_T *
482ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
483{
484 return &t_string;
485}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200486 static type_T *
487ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100488{
489 return &t_list_any;
490}
491 static type_T *
492ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
493{
494 return &t_list_number;
495}
496 static type_T *
497ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
498{
499 return &t_list_string;
500}
501 static type_T *
502ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
503{
504 return &t_list_dict_any;
505}
506 static type_T *
507ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
508{
509 return &t_dict_any;
510}
511 static type_T *
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100512ret_job_info(int argcount, type_T **argtypes UNUSED)
513{
514 if (argcount == 0)
515 return &t_list_job;
516 return &t_dict_any;
517}
518 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100519ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
520{
521 return &t_dict_number;
522}
523 static type_T *
524ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
525{
526 return &t_dict_string;
527}
528 static type_T *
529ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
530{
531 return &t_blob;
532}
533 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200534ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100535{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200536 return &t_func_any;
537}
538 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100539ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
540{
541 return &t_channel;
542}
543 static type_T *
544ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
545{
546 return &t_job;
547}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200548 static type_T *
549ret_first_arg(int argcount, type_T **argtypes)
550{
551 if (argcount > 0)
552 return argtypes[0];
553 return &t_void;
554}
Bram Moolenaarea696852020-11-09 18:31:39 +0100555// for map(): returns first argument but item type may differ
556 static type_T *
557ret_first_cont(int argcount UNUSED, type_T **argtypes)
558{
559 if (argtypes[0]->tt_type == VAR_LIST)
560 return &t_list_any;
561 if (argtypes[0]->tt_type == VAR_DICT)
562 return &t_dict_any;
563 if (argtypes[0]->tt_type == VAR_BLOB)
564 return argtypes[0];
565 return &t_any;
566}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200567
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200568/*
569 * Used for getqflist(): returns list if there is no argument, dict if there is
570 * one.
571 */
572 static type_T *
573ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
574{
575 if (argcount > 0)
576 return &t_dict_any;
577 return &t_list_dict_any;
578}
579
580/*
581 * Used for getloclist(): returns list if there is one argument, dict if there
582 * are two.
583 */
584 static type_T *
585ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
586{
587 if (argcount > 1)
588 return &t_dict_any;
589 return &t_list_dict_any;
590}
591
Bram Moolenaar846178a2020-07-05 17:04:13 +0200592 static type_T *
593ret_argv(int argcount, type_T **argtypes UNUSED)
594{
595 // argv() returns list of strings
596 if (argcount == 0)
597 return &t_list_string;
598
599 // argv(0) returns a string, but argv(-1] returns a list
600 return &t_any;
601}
602
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200603 static type_T *
604ret_remove(int argcount UNUSED, type_T **argtypes)
605{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200606 if (argtypes != NULL)
607 {
608 if (argtypes[0]->tt_type == VAR_LIST
609 || argtypes[0]->tt_type == VAR_DICT)
610 return argtypes[0]->tt_member;
611 if (argtypes[0]->tt_type == VAR_BLOB)
612 return &t_number;
613 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200614 return &t_any;
615}
616
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200617 static type_T *
618ret_getreg(int argcount, type_T **argtypes UNUSED)
619{
620 // Assume that if the third argument is passed it's non-zero
621 if (argcount == 3)
622 return &t_list_string;
623 return &t_string;
624}
625
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200626 static type_T *
627ret_maparg(int argcount, type_T **argtypes UNUSED)
628{
629 // Assume that if the fourth argument is passed it's non-zero
630 if (argcount == 4)
631 return &t_dict_any;
632 return &t_string;
633}
634
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100635static type_T *ret_f_function(int argcount, type_T **argtypes);
636
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200637/*
638 * Array with names and number of arguments of all internal functions
639 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
640 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200641typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200642{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200643 char *f_name; // function name
644 char f_min_argc; // minimal number of arguments
645 char f_max_argc; // maximal number of arguments
646 char f_argtype; // for method: FEARG_ values
Bram Moolenaar94738d82020-10-21 14:25:07 +0200647 argcheck_T *f_argcheck; // list of functions to check argument types
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100648 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
649 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200650 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200651 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200652} funcentry_T;
653
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200654// values for f_argtype; zero means it cannot be used as a method
655#define FEARG_1 1 // base is the first argument
656#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200657#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200658#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200659#define FEARG_LAST 9 // base is the last argument
660
Bram Moolenaar15c47602020-03-26 22:16:48 +0100661#ifdef FEAT_FLOAT
662# define FLOAT_FUNC(name) name
663#else
664# define FLOAT_FUNC(name) NULL
665#endif
666#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
667# define MATH_FUNC(name) name
668#else
669# define MATH_FUNC(name) NULL
670#endif
671#ifdef FEAT_TIMERS
672# define TIMER_FUNC(name) name
673#else
674# define TIMER_FUNC(name) NULL
675#endif
676#ifdef FEAT_JOB_CHANNEL
677# define JOB_FUNC(name) name
678#else
679# define JOB_FUNC(name) NULL
680#endif
681#ifdef FEAT_PROP_POPUP
682# define PROP_FUNC(name) name
683#else
684# define PROP_FUNC(name) NULL
685#endif
686#ifdef FEAT_SIGNS
687# define SIGN_FUNC(name) name
688#else
689# define SIGN_FUNC(name) NULL
690#endif
691#ifdef FEAT_SOUND
692# define SOUND_FUNC(name) name
693#else
694# define SOUND_FUNC(name) NULL
695#endif
696#ifdef FEAT_TERMINAL
697# define TERM_FUNC(name) name
698#else
699# define TERM_FUNC(name) NULL
700#endif
701
Bram Moolenaarac92e252019-08-03 21:58:38 +0200702static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200703{
Bram Moolenaar94738d82020-10-21 14:25:07 +0200704 {"abs", 1, 1, FEARG_1, arg1_float_or_nr,
705 ret_any, FLOAT_FUNC(f_abs)},
706 {"acos", 1, 1, FEARG_1, NULL,
707 ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100708 {"add", 2, 2, FEARG_1, NULL /* arg2_listblob_item */,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200709 ret_first_arg, f_add},
710 {"and", 2, 2, FEARG_1, NULL,
711 ret_number, f_and},
712 {"append", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100713 ret_number_bool, f_append},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200714 {"appendbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100715 ret_number_bool, f_appendbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200716 {"argc", 0, 1, 0, NULL,
717 ret_number, f_argc},
718 {"argidx", 0, 0, 0, NULL,
719 ret_number, f_argidx},
720 {"arglistid", 0, 2, 0, NULL,
721 ret_number, f_arglistid},
722 {"argv", 0, 2, 0, NULL,
723 ret_argv, f_argv},
724 {"asin", 1, 1, FEARG_1, NULL,
725 ret_float, FLOAT_FUNC(f_asin)},
726 {"assert_beeps", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100727 ret_number_bool, f_assert_beeps},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200728 {"assert_equal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100729 ret_number_bool, f_assert_equal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200730 {"assert_equalfile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100731 ret_number_bool, f_assert_equalfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200732 {"assert_exception", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100733 ret_number_bool, f_assert_exception},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200734 {"assert_fails", 1, 5, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100735 ret_number_bool, f_assert_fails},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200736 {"assert_false", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100737 ret_number_bool, f_assert_false},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200738 {"assert_inrange", 3, 4, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100739 ret_number_bool, f_assert_inrange},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200740 {"assert_match", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100741 ret_number_bool, f_assert_match},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200742 {"assert_notequal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100743 ret_number_bool, f_assert_notequal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200744 {"assert_notmatch", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100745 ret_number_bool, f_assert_notmatch},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200746 {"assert_report", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100747 ret_number_bool, f_assert_report},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200748 {"assert_true", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100749 ret_number_bool, f_assert_true},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200750 {"atan", 1, 1, FEARG_1, NULL,
751 ret_float, FLOAT_FUNC(f_atan)},
752 {"atan2", 2, 2, FEARG_1, NULL,
753 ret_float, FLOAT_FUNC(f_atan2)},
754 {"balloon_gettext", 0, 0, 0, NULL,
755 ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100756#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100757 f_balloon_gettext
758#else
759 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100760#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100761 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200762 {"balloon_show", 1, 1, FEARG_1, NULL,
763 ret_void,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100764#ifdef FEAT_BEVAL
765 f_balloon_show
766#else
767 NULL
768#endif
769 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200770 {"balloon_split", 1, 1, FEARG_1, NULL,
771 ret_list_string,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100772#if defined(FEAT_BEVAL_TERM)
773 f_balloon_split
774#else
775 NULL
776#endif
777 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200778 {"browse", 4, 4, 0, NULL,
779 ret_string, f_browse},
780 {"browsedir", 2, 2, 0, NULL,
781 ret_string, f_browsedir},
782 {"bufadd", 1, 1, FEARG_1, NULL,
783 ret_number, f_bufadd},
784 {"bufexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100785 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200786 {"buffer_exists", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100787 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200788 {"buffer_name", 0, 1, FEARG_1, NULL, // obsolete
789 ret_string, f_bufname},
790 {"buffer_number", 0, 1, FEARG_1, NULL, // obsolete
791 ret_number, f_bufnr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200792 {"buflisted", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100793 ret_number_bool, f_buflisted},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200794 {"bufload", 1, 1, FEARG_1, NULL,
795 ret_void, f_bufload},
796 {"bufloaded", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100797 ret_number_bool, f_bufloaded},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200798 {"bufname", 0, 1, FEARG_1, NULL,
799 ret_string, f_bufname},
800 {"bufnr", 0, 2, FEARG_1, NULL,
801 ret_number, f_bufnr},
802 {"bufwinid", 1, 1, FEARG_1, NULL,
803 ret_number, f_bufwinid},
804 {"bufwinnr", 1, 1, FEARG_1, NULL,
805 ret_number, f_bufwinnr},
806 {"byte2line", 1, 1, FEARG_1, NULL,
807 ret_number, f_byte2line},
808 {"byteidx", 2, 2, FEARG_1, NULL,
809 ret_number, f_byteidx},
810 {"byteidxcomp", 2, 2, FEARG_1, NULL,
811 ret_number, f_byteidxcomp},
812 {"call", 2, 3, FEARG_1, NULL,
813 ret_any, f_call},
814 {"ceil", 1, 1, FEARG_1, NULL,
815 ret_float, FLOAT_FUNC(f_ceil)},
816 {"ch_canread", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100817 ret_number_bool, JOB_FUNC(f_ch_canread)},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200818 {"ch_close", 1, 1, FEARG_1, NULL,
819 ret_void, JOB_FUNC(f_ch_close)},
820 {"ch_close_in", 1, 1, FEARG_1, NULL,
821 ret_void, JOB_FUNC(f_ch_close_in)},
822 {"ch_evalexpr", 2, 3, FEARG_1, NULL,
823 ret_any, JOB_FUNC(f_ch_evalexpr)},
824 {"ch_evalraw", 2, 3, FEARG_1, NULL,
825 ret_any, JOB_FUNC(f_ch_evalraw)},
826 {"ch_getbufnr", 2, 2, FEARG_1, NULL,
827 ret_number, JOB_FUNC(f_ch_getbufnr)},
828 {"ch_getjob", 1, 1, FEARG_1, NULL,
829 ret_job, JOB_FUNC(f_ch_getjob)},
830 {"ch_info", 1, 1, FEARG_1, NULL,
831 ret_dict_any, JOB_FUNC(f_ch_info)},
832 {"ch_log", 1, 2, FEARG_1, NULL,
833 ret_void, JOB_FUNC(f_ch_log)},
834 {"ch_logfile", 1, 2, FEARG_1, NULL,
835 ret_void, JOB_FUNC(f_ch_logfile)},
836 {"ch_open", 1, 2, FEARG_1, NULL,
837 ret_channel, JOB_FUNC(f_ch_open)},
838 {"ch_read", 1, 2, FEARG_1, NULL,
839 ret_string, JOB_FUNC(f_ch_read)},
840 {"ch_readblob", 1, 2, FEARG_1, NULL,
841 ret_blob, JOB_FUNC(f_ch_readblob)},
842 {"ch_readraw", 1, 2, FEARG_1, NULL,
843 ret_string, JOB_FUNC(f_ch_readraw)},
844 {"ch_sendexpr", 2, 3, FEARG_1, NULL,
845 ret_void, JOB_FUNC(f_ch_sendexpr)},
846 {"ch_sendraw", 2, 3, FEARG_1, NULL,
847 ret_void, JOB_FUNC(f_ch_sendraw)},
848 {"ch_setoptions", 2, 2, FEARG_1, NULL,
849 ret_void, JOB_FUNC(f_ch_setoptions)},
850 {"ch_status", 1, 2, FEARG_1, NULL,
851 ret_string, JOB_FUNC(f_ch_status)},
852 {"changenr", 0, 0, 0, NULL,
853 ret_number, f_changenr},
854 {"char2nr", 1, 2, FEARG_1, NULL,
855 ret_number, f_char2nr},
856 {"charclass", 1, 1, FEARG_1, NULL,
857 ret_number, f_charclass},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100858 {"charcol", 1, 1, FEARG_1, NULL,
859 ret_number, f_charcol},
Bram Moolenaar17793ef2020-12-28 12:56:58 +0100860 {"charidx", 2, 3, FEARG_1, NULL,
861 ret_number, f_charidx},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200862 {"chdir", 1, 1, FEARG_1, NULL,
863 ret_string, f_chdir},
864 {"cindent", 1, 1, FEARG_1, NULL,
865 ret_number, f_cindent},
866 {"clearmatches", 0, 1, FEARG_1, NULL,
867 ret_void, f_clearmatches},
868 {"col", 1, 1, FEARG_1, NULL,
869 ret_number, f_col},
870 {"complete", 2, 2, FEARG_2, NULL,
871 ret_void, f_complete},
872 {"complete_add", 1, 1, FEARG_1, NULL,
873 ret_number, f_complete_add},
874 {"complete_check", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100875 ret_number_bool, f_complete_check},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200876 {"complete_info", 0, 1, FEARG_1, NULL,
877 ret_dict_any, f_complete_info},
878 {"confirm", 1, 4, FEARG_1, NULL,
879 ret_number, f_confirm},
880 {"copy", 1, 1, FEARG_1, NULL,
881 ret_first_arg, f_copy},
882 {"cos", 1, 1, FEARG_1, NULL,
883 ret_float, FLOAT_FUNC(f_cos)},
884 {"cosh", 1, 1, FEARG_1, NULL,
885 ret_float, FLOAT_FUNC(f_cosh)},
886 {"count", 2, 4, FEARG_1, NULL,
887 ret_number, f_count},
888 {"cscope_connection",0,3, 0, NULL,
889 ret_number, f_cscope_connection},
890 {"cursor", 1, 3, FEARG_1, NULL,
891 ret_number, f_cursor},
892 {"debugbreak", 1, 1, FEARG_1, NULL,
893 ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100894#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100895 f_debugbreak
896#else
897 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200898#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100899 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200900 {"deepcopy", 1, 2, FEARG_1, NULL,
901 ret_first_arg, f_deepcopy},
902 {"delete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100903 ret_number_bool, f_delete},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200904 {"deletebufline", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100905 ret_number_bool, f_deletebufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200906 {"did_filetype", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100907 ret_number_bool, f_did_filetype},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200908 {"diff_filler", 1, 1, FEARG_1, NULL,
909 ret_number, f_diff_filler},
910 {"diff_hlID", 2, 2, FEARG_1, NULL,
911 ret_number, f_diff_hlID},
912 {"echoraw", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100913 ret_void, f_echoraw},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200914 {"empty", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100915 ret_number_bool, f_empty},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200916 {"environ", 0, 0, 0, NULL,
917 ret_dict_string, f_environ},
918 {"escape", 2, 2, FEARG_1, NULL,
919 ret_string, f_escape},
920 {"eval", 1, 1, FEARG_1, NULL,
921 ret_any, f_eval},
922 {"eventhandler", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100923 ret_number_bool, f_eventhandler},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200924 {"executable", 1, 1, FEARG_1, NULL,
925 ret_number, f_executable},
926 {"execute", 1, 2, FEARG_1, NULL,
927 ret_string, f_execute},
928 {"exepath", 1, 1, FEARG_1, NULL,
929 ret_string, f_exepath},
930 {"exists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100931 ret_number_bool, f_exists},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200932 {"exp", 1, 1, FEARG_1, NULL,
933 ret_float, FLOAT_FUNC(f_exp)},
934 {"expand", 1, 3, FEARG_1, NULL,
935 ret_any, f_expand},
936 {"expandcmd", 1, 1, FEARG_1, NULL,
937 ret_string, f_expandcmd},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100938 {"extend", 2, 3, FEARG_1, arg23_extend,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200939 ret_first_arg, f_extend},
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100940 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
941 ret_first_cont, f_extendnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200942 {"feedkeys", 1, 2, FEARG_1, NULL,
943 ret_void, f_feedkeys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200944 {"file_readable", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100945 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200946 {"filereadable", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100947 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200948 {"filewritable", 1, 1, FEARG_1, NULL,
949 ret_number, f_filewritable},
950 {"filter", 2, 2, FEARG_1, NULL,
951 ret_first_arg, f_filter},
952 {"finddir", 1, 3, FEARG_1, NULL,
953 ret_string, f_finddir},
954 {"findfile", 1, 3, FEARG_1, NULL,
955 ret_string, f_findfile},
956 {"flatten", 1, 2, FEARG_1, NULL,
957 ret_list_any, f_flatten},
Bram Moolenaar3b690062021-02-01 20:14:51 +0100958 {"flattennew", 1, 2, FEARG_1, NULL,
959 ret_list_any, f_flattennew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200960 {"float2nr", 1, 1, FEARG_1, NULL,
961 ret_number, FLOAT_FUNC(f_float2nr)},
962 {"floor", 1, 1, FEARG_1, NULL,
963 ret_float, FLOAT_FUNC(f_floor)},
964 {"fmod", 2, 2, FEARG_1, NULL,
965 ret_float, FLOAT_FUNC(f_fmod)},
966 {"fnameescape", 1, 1, FEARG_1, NULL,
967 ret_string, f_fnameescape},
968 {"fnamemodify", 2, 2, FEARG_1, NULL,
969 ret_string, f_fnamemodify},
970 {"foldclosed", 1, 1, FEARG_1, NULL,
971 ret_number, f_foldclosed},
972 {"foldclosedend", 1, 1, FEARG_1, NULL,
973 ret_number, f_foldclosedend},
974 {"foldlevel", 1, 1, FEARG_1, NULL,
975 ret_number, f_foldlevel},
976 {"foldtext", 0, 0, 0, NULL,
977 ret_string, f_foldtext},
978 {"foldtextresult", 1, 1, FEARG_1, NULL,
979 ret_string, f_foldtextresult},
980 {"foreground", 0, 0, 0, NULL,
981 ret_void, f_foreground},
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100982 {"fullcommand", 1, 1, FEARG_1, arg1_string,
983 ret_string, f_fullcommand},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200984 {"funcref", 1, 3, FEARG_1, NULL,
985 ret_func_any, f_funcref},
986 {"function", 1, 3, FEARG_1, NULL,
987 ret_f_function, f_function},
988 {"garbagecollect", 0, 1, 0, NULL,
989 ret_void, f_garbagecollect},
990 {"get", 2, 3, FEARG_1, NULL,
991 ret_any, f_get},
992 {"getbufinfo", 0, 1, FEARG_1, NULL,
993 ret_list_dict_any, f_getbufinfo},
994 {"getbufline", 2, 3, FEARG_1, NULL,
995 ret_list_string, f_getbufline},
996 {"getbufvar", 2, 3, FEARG_1, NULL,
997 ret_any, f_getbufvar},
998 {"getchangelist", 0, 1, FEARG_1, NULL,
999 ret_list_any, f_getchangelist},
1000 {"getchar", 0, 1, 0, NULL,
1001 ret_number, f_getchar},
1002 {"getcharmod", 0, 0, 0, NULL,
1003 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001004 {"getcharpos", 1, 1, FEARG_1, NULL,
1005 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001006 {"getcharsearch", 0, 0, 0, NULL,
1007 ret_dict_any, f_getcharsearch},
1008 {"getcmdline", 0, 0, 0, NULL,
1009 ret_string, f_getcmdline},
1010 {"getcmdpos", 0, 0, 0, NULL,
1011 ret_number, f_getcmdpos},
1012 {"getcmdtype", 0, 0, 0, NULL,
1013 ret_string, f_getcmdtype},
1014 {"getcmdwintype", 0, 0, 0, NULL,
1015 ret_string, f_getcmdwintype},
1016 {"getcompletion", 2, 3, FEARG_1, NULL,
1017 ret_list_string, f_getcompletion},
1018 {"getcurpos", 0, 1, FEARG_1, NULL,
1019 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001020 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
1021 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001022 {"getcwd", 0, 2, FEARG_1, NULL,
1023 ret_string, f_getcwd},
1024 {"getenv", 1, 1, FEARG_1, NULL,
Bram Moolenaar7ad67d12021-03-10 16:08:26 +01001025 ret_any, f_getenv},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001026 {"getfontname", 0, 1, 0, NULL,
1027 ret_string, f_getfontname},
1028 {"getfperm", 1, 1, FEARG_1, NULL,
1029 ret_string, f_getfperm},
1030 {"getfsize", 1, 1, FEARG_1, NULL,
1031 ret_number, f_getfsize},
1032 {"getftime", 1, 1, FEARG_1, NULL,
1033 ret_number, f_getftime},
1034 {"getftype", 1, 1, FEARG_1, NULL,
1035 ret_string, f_getftype},
1036 {"getimstatus", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001037 ret_number_bool, f_getimstatus},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001038 {"getjumplist", 0, 2, FEARG_1, NULL,
1039 ret_list_any, f_getjumplist},
1040 {"getline", 1, 2, FEARG_1, NULL,
1041 ret_f_getline, f_getline},
1042 {"getloclist", 1, 2, 0, NULL,
1043 ret_list_or_dict_1, f_getloclist},
1044 {"getmarklist", 0, 1, FEARG_1, NULL,
1045 ret_list_dict_any, f_getmarklist},
1046 {"getmatches", 0, 1, 0, NULL,
1047 ret_list_dict_any, f_getmatches},
1048 {"getmousepos", 0, 0, 0, NULL,
1049 ret_dict_number, f_getmousepos},
1050 {"getpid", 0, 0, 0, NULL,
1051 ret_number, f_getpid},
1052 {"getpos", 1, 1, FEARG_1, NULL,
1053 ret_list_number, f_getpos},
1054 {"getqflist", 0, 1, 0, NULL,
1055 ret_list_or_dict_0, f_getqflist},
1056 {"getreg", 0, 3, FEARG_1, NULL,
1057 ret_getreg, f_getreg},
1058 {"getreginfo", 0, 1, FEARG_1, NULL,
1059 ret_dict_any, f_getreginfo},
1060 {"getregtype", 0, 1, FEARG_1, NULL,
1061 ret_string, f_getregtype},
1062 {"gettabinfo", 0, 1, FEARG_1, NULL,
1063 ret_list_dict_any, f_gettabinfo},
1064 {"gettabvar", 2, 3, FEARG_1, NULL,
1065 ret_any, f_gettabvar},
1066 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1067 ret_any, f_gettabwinvar},
1068 {"gettagstack", 0, 1, FEARG_1, NULL,
1069 ret_dict_any, f_gettagstack},
1070 {"gettext", 1, 1, FEARG_1, NULL,
1071 ret_string, f_gettext},
1072 {"getwininfo", 0, 1, FEARG_1, NULL,
1073 ret_list_dict_any, f_getwininfo},
1074 {"getwinpos", 0, 1, FEARG_1, NULL,
1075 ret_list_number, f_getwinpos},
1076 {"getwinposx", 0, 0, 0, NULL,
1077 ret_number, f_getwinposx},
1078 {"getwinposy", 0, 0, 0, NULL,
1079 ret_number, f_getwinposy},
1080 {"getwinvar", 2, 3, FEARG_1, NULL,
1081 ret_any, f_getwinvar},
1082 {"glob", 1, 4, FEARG_1, NULL,
1083 ret_any, f_glob},
1084 {"glob2regpat", 1, 1, FEARG_1, NULL,
1085 ret_string, f_glob2regpat},
1086 {"globpath", 2, 5, FEARG_2, NULL,
1087 ret_any, f_globpath},
1088 {"has", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001089 ret_number_bool, f_has},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001090 {"has_key", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001091 ret_number_bool, f_has_key},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001092 {"haslocaldir", 0, 2, FEARG_1, NULL,
1093 ret_number, f_haslocaldir},
1094 {"hasmapto", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001095 ret_number_bool, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001096 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1097 ret_number, f_hlID},
1098 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001099 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001100 {"histadd", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001101 ret_number_bool, f_histadd},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001102 {"histdel", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001103 ret_number_bool, f_histdel},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001104 {"histget", 1, 2, FEARG_1, NULL,
1105 ret_string, f_histget},
1106 {"histnr", 1, 1, FEARG_1, NULL,
1107 ret_number, f_histnr},
1108 {"hlID", 1, 1, FEARG_1, NULL,
1109 ret_number, f_hlID},
1110 {"hlexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001111 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001112 {"hostname", 0, 0, 0, NULL,
1113 ret_string, f_hostname},
1114 {"iconv", 3, 3, FEARG_1, NULL,
1115 ret_string, f_iconv},
1116 {"indent", 1, 1, FEARG_1, NULL,
1117 ret_number, f_indent},
1118 {"index", 2, 4, FEARG_1, NULL,
1119 ret_number, f_index},
1120 {"input", 1, 3, FEARG_1, NULL,
1121 ret_string, f_input},
1122 {"inputdialog", 1, 3, FEARG_1, NULL,
1123 ret_string, f_inputdialog},
1124 {"inputlist", 1, 1, FEARG_1, NULL,
1125 ret_number, f_inputlist},
1126 {"inputrestore", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001127 ret_number_bool, f_inputrestore},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001128 {"inputsave", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001129 ret_number_bool, f_inputsave},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001130 {"inputsecret", 1, 2, FEARG_1, NULL,
1131 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001132 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001133 ret_first_arg, f_insert},
1134 {"interrupt", 0, 0, 0, NULL,
1135 ret_void, f_interrupt},
1136 {"invert", 1, 1, FEARG_1, NULL,
1137 ret_number, f_invert},
1138 {"isdirectory", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001139 ret_number_bool, f_isdirectory},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001140 {"isinf", 1, 1, FEARG_1, NULL,
1141 ret_number, MATH_FUNC(f_isinf)},
1142 {"islocked", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001143 ret_number_bool, f_islocked},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001144 {"isnan", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001145 ret_number_bool, MATH_FUNC(f_isnan)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001146 {"items", 1, 1, FEARG_1, NULL,
1147 ret_list_any, f_items},
1148 {"job_getchannel", 1, 1, FEARG_1, NULL,
1149 ret_channel, JOB_FUNC(f_job_getchannel)},
1150 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001151 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001152 {"job_setoptions", 2, 2, FEARG_1, NULL,
1153 ret_void, JOB_FUNC(f_job_setoptions)},
1154 {"job_start", 1, 2, FEARG_1, NULL,
1155 ret_job, JOB_FUNC(f_job_start)},
1156 {"job_status", 1, 1, FEARG_1, NULL,
1157 ret_string, JOB_FUNC(f_job_status)},
1158 {"job_stop", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001159 ret_number_bool, JOB_FUNC(f_job_stop)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001160 {"join", 1, 2, FEARG_1, NULL,
1161 ret_string, f_join},
1162 {"js_decode", 1, 1, FEARG_1, NULL,
1163 ret_any, f_js_decode},
1164 {"js_encode", 1, 1, FEARG_1, NULL,
1165 ret_string, f_js_encode},
1166 {"json_decode", 1, 1, FEARG_1, NULL,
1167 ret_any, f_json_decode},
1168 {"json_encode", 1, 1, FEARG_1, NULL,
1169 ret_string, f_json_encode},
1170 {"keys", 1, 1, FEARG_1, NULL,
1171 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001172 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1173 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001174 {"len", 1, 1, FEARG_1, NULL,
1175 ret_number, f_len},
1176 {"libcall", 3, 3, FEARG_3, NULL,
1177 ret_string, f_libcall},
1178 {"libcallnr", 3, 3, FEARG_3, NULL,
1179 ret_number, f_libcallnr},
1180 {"line", 1, 2, FEARG_1, NULL,
1181 ret_number, f_line},
1182 {"line2byte", 1, 1, FEARG_1, NULL,
1183 ret_number, f_line2byte},
1184 {"lispindent", 1, 1, FEARG_1, NULL,
1185 ret_number, f_lispindent},
1186 {"list2str", 1, 2, FEARG_1, NULL,
1187 ret_string, f_list2str},
1188 {"listener_add", 1, 2, FEARG_2, NULL,
1189 ret_number, f_listener_add},
1190 {"listener_flush", 0, 1, FEARG_1, NULL,
1191 ret_void, f_listener_flush},
1192 {"listener_remove", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001193 ret_number_bool, f_listener_remove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001194 {"localtime", 0, 0, 0, NULL,
1195 ret_number, f_localtime},
1196 {"log", 1, 1, FEARG_1, NULL,
1197 ret_float, FLOAT_FUNC(f_log)},
1198 {"log10", 1, 1, FEARG_1, NULL,
1199 ret_float, FLOAT_FUNC(f_log10)},
1200 {"luaeval", 1, 2, FEARG_1, NULL,
1201 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001202#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001203 f_luaeval
1204#else
1205 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001206#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001207 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001208 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001209 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001210 {"maparg", 1, 4, FEARG_1, NULL,
1211 ret_maparg, f_maparg},
1212 {"mapcheck", 1, 3, FEARG_1, NULL,
1213 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001214 {"mapnew", 2, 2, FEARG_1, NULL,
1215 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001216 {"mapset", 3, 3, FEARG_1, NULL,
1217 ret_void, f_mapset},
1218 {"match", 2, 4, FEARG_1, NULL,
1219 ret_any, f_match},
1220 {"matchadd", 2, 5, FEARG_1, NULL,
1221 ret_number, f_matchadd},
1222 {"matchaddpos", 2, 5, FEARG_1, NULL,
1223 ret_number, f_matchaddpos},
1224 {"matcharg", 1, 1, FEARG_1, NULL,
1225 ret_list_string, f_matcharg},
1226 {"matchdelete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001227 ret_number_bool, f_matchdelete},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001228 {"matchend", 2, 4, FEARG_1, NULL,
1229 ret_number, f_matchend},
1230 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1231 ret_list_string, f_matchfuzzy},
1232 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1233 ret_list_any, f_matchfuzzypos},
1234 {"matchlist", 2, 4, FEARG_1, NULL,
1235 ret_list_string, f_matchlist},
1236 {"matchstr", 2, 4, FEARG_1, NULL,
1237 ret_string, f_matchstr},
1238 {"matchstrpos", 2, 4, FEARG_1, NULL,
1239 ret_list_any, f_matchstrpos},
1240 {"max", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001241 ret_number, f_max},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001242 {"menu_info", 1, 2, FEARG_1, NULL,
1243 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001244#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001245 f_menu_info
1246#else
1247 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001248#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001249 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001250 {"min", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001251 ret_number, f_min},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001252 {"mkdir", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001253 ret_number_bool, f_mkdir},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001254 {"mode", 0, 1, FEARG_1, NULL,
1255 ret_string, f_mode},
1256 {"mzeval", 1, 1, FEARG_1, NULL,
1257 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001258#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001259 f_mzeval
1260#else
1261 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001262#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001263 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001264 {"nextnonblank", 1, 1, FEARG_1, NULL,
1265 ret_number, f_nextnonblank},
1266 {"nr2char", 1, 2, FEARG_1, NULL,
1267 ret_string, f_nr2char},
1268 {"or", 2, 2, FEARG_1, NULL,
1269 ret_number, f_or},
1270 {"pathshorten", 1, 2, FEARG_1, NULL,
1271 ret_string, f_pathshorten},
1272 {"perleval", 1, 1, FEARG_1, NULL,
1273 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001274#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001275 f_perleval
1276#else
1277 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001278#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001279 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001280 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1281 ret_number, PROP_FUNC(f_popup_atcursor)},
1282 {"popup_beval", 2, 2, FEARG_1, NULL,
1283 ret_number, PROP_FUNC(f_popup_beval)},
1284 {"popup_clear", 0, 1, 0, NULL,
1285 ret_void, PROP_FUNC(f_popup_clear)},
1286 {"popup_close", 1, 2, FEARG_1, NULL,
1287 ret_void, PROP_FUNC(f_popup_close)},
1288 {"popup_create", 2, 2, FEARG_1, NULL,
1289 ret_number, PROP_FUNC(f_popup_create)},
1290 {"popup_dialog", 2, 2, FEARG_1, NULL,
1291 ret_number, PROP_FUNC(f_popup_dialog)},
1292 {"popup_filter_menu", 2, 2, 0, NULL,
1293 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1294 {"popup_filter_yesno", 2, 2, 0, NULL,
1295 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1296 {"popup_findinfo", 0, 0, 0, NULL,
1297 ret_number, PROP_FUNC(f_popup_findinfo)},
1298 {"popup_findpreview", 0, 0, 0, NULL,
1299 ret_number, PROP_FUNC(f_popup_findpreview)},
1300 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1301 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1302 {"popup_getpos", 1, 1, FEARG_1, NULL,
1303 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1304 {"popup_hide", 1, 1, FEARG_1, NULL,
1305 ret_void, PROP_FUNC(f_popup_hide)},
1306 {"popup_list", 0, 0, 0, NULL,
1307 ret_list_number, PROP_FUNC(f_popup_list)},
1308 {"popup_locate", 2, 2, 0, NULL,
1309 ret_number, PROP_FUNC(f_popup_locate)},
1310 {"popup_menu", 2, 2, FEARG_1, NULL,
1311 ret_number, PROP_FUNC(f_popup_menu)},
1312 {"popup_move", 2, 2, FEARG_1, NULL,
1313 ret_void, PROP_FUNC(f_popup_move)},
1314 {"popup_notification", 2, 2, FEARG_1, NULL,
1315 ret_number, PROP_FUNC(f_popup_notification)},
1316 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1317 ret_void, PROP_FUNC(f_popup_setoptions)},
1318 {"popup_settext", 2, 2, FEARG_1, NULL,
1319 ret_void, PROP_FUNC(f_popup_settext)},
1320 {"popup_show", 1, 1, FEARG_1, NULL,
1321 ret_void, PROP_FUNC(f_popup_show)},
1322 {"pow", 2, 2, FEARG_1, NULL,
1323 ret_float, FLOAT_FUNC(f_pow)},
1324 {"prevnonblank", 1, 1, FEARG_1, NULL,
1325 ret_number, f_prevnonblank},
1326 {"printf", 1, 19, FEARG_2, NULL,
1327 ret_string, f_printf},
1328 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1329 ret_string, JOB_FUNC(f_prompt_getprompt)},
1330 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1331 ret_void, JOB_FUNC(f_prompt_setcallback)},
1332 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1333 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1334 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1335 ret_void, JOB_FUNC(f_prompt_setprompt)},
1336 {"prop_add", 3, 3, FEARG_1, NULL,
1337 ret_void, PROP_FUNC(f_prop_add)},
1338 {"prop_clear", 1, 3, FEARG_1, NULL,
1339 ret_void, PROP_FUNC(f_prop_clear)},
1340 {"prop_find", 1, 2, FEARG_1, NULL,
1341 ret_dict_any, PROP_FUNC(f_prop_find)},
1342 {"prop_list", 1, 2, FEARG_1, NULL,
1343 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1344 {"prop_remove", 1, 3, FEARG_1, NULL,
1345 ret_number, PROP_FUNC(f_prop_remove)},
1346 {"prop_type_add", 2, 2, FEARG_1, NULL,
1347 ret_void, PROP_FUNC(f_prop_type_add)},
1348 {"prop_type_change", 2, 2, FEARG_1, NULL,
1349 ret_void, PROP_FUNC(f_prop_type_change)},
1350 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1351 ret_void, PROP_FUNC(f_prop_type_delete)},
1352 {"prop_type_get", 1, 2, FEARG_1, NULL,
1353 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1354 {"prop_type_list", 0, 1, FEARG_1, NULL,
1355 ret_list_string, PROP_FUNC(f_prop_type_list)},
1356 {"pum_getpos", 0, 0, 0, NULL,
1357 ret_dict_number, f_pum_getpos},
1358 {"pumvisible", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001359 ret_number_bool, f_pumvisible},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001360 {"py3eval", 1, 1, FEARG_1, NULL,
1361 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001362#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001363 f_py3eval
1364#else
1365 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001366#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001367 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001368 {"pyeval", 1, 1, FEARG_1, NULL,
1369 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001370#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001371 f_pyeval
1372#else
1373 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001374#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001375 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001376 {"pyxeval", 1, 1, FEARG_1, NULL,
1377 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001378#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001379 f_pyxeval
1380#else
1381 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001382#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001383 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001384 {"rand", 0, 1, FEARG_1, NULL,
1385 ret_number, f_rand},
1386 {"range", 1, 3, FEARG_1, NULL,
1387 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001388 {"readblob", 1, 1, FEARG_1, NULL,
1389 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001390 {"readdir", 1, 3, FEARG_1, NULL,
1391 ret_list_string, f_readdir},
1392 {"readdirex", 1, 3, FEARG_1, NULL,
1393 ret_list_dict_any, f_readdirex},
1394 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001395 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001396 {"reduce", 2, 3, FEARG_1, NULL,
1397 ret_any, f_reduce},
1398 {"reg_executing", 0, 0, 0, NULL,
1399 ret_string, f_reg_executing},
1400 {"reg_recording", 0, 0, 0, NULL,
1401 ret_string, f_reg_recording},
1402 {"reltime", 0, 2, FEARG_1, NULL,
1403 ret_list_any, f_reltime},
1404 {"reltimefloat", 1, 1, FEARG_1, NULL,
1405 ret_float, FLOAT_FUNC(f_reltimefloat)},
1406 {"reltimestr", 1, 1, FEARG_1, NULL,
1407 ret_string, f_reltimestr},
1408 {"remote_expr", 2, 4, FEARG_1, NULL,
1409 ret_string, f_remote_expr},
1410 {"remote_foreground", 1, 1, FEARG_1, NULL,
1411 ret_string, f_remote_foreground},
1412 {"remote_peek", 1, 2, FEARG_1, NULL,
1413 ret_number, f_remote_peek},
1414 {"remote_read", 1, 2, FEARG_1, NULL,
1415 ret_string, f_remote_read},
1416 {"remote_send", 2, 3, FEARG_1, NULL,
1417 ret_string, f_remote_send},
1418 {"remote_startserver", 1, 1, FEARG_1, NULL,
1419 ret_void, f_remote_startserver},
1420 {"remove", 2, 3, FEARG_1, NULL,
1421 ret_remove, f_remove},
1422 {"rename", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001423 ret_number_bool, f_rename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001424 {"repeat", 2, 2, FEARG_1, NULL,
1425 ret_first_arg, f_repeat},
1426 {"resolve", 1, 1, FEARG_1, NULL,
1427 ret_string, f_resolve},
1428 {"reverse", 1, 1, FEARG_1, NULL,
1429 ret_first_arg, f_reverse},
1430 {"round", 1, 1, FEARG_1, NULL,
1431 ret_float, FLOAT_FUNC(f_round)},
1432 {"rubyeval", 1, 1, FEARG_1, NULL,
1433 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001434#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001435 f_rubyeval
1436#else
1437 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001438#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001439 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001440 {"screenattr", 2, 2, FEARG_1, NULL,
1441 ret_number, f_screenattr},
1442 {"screenchar", 2, 2, FEARG_1, NULL,
1443 ret_number, f_screenchar},
1444 {"screenchars", 2, 2, FEARG_1, NULL,
1445 ret_list_number, f_screenchars},
1446 {"screencol", 0, 0, 0, NULL,
1447 ret_number, f_screencol},
1448 {"screenpos", 3, 3, FEARG_1, NULL,
1449 ret_dict_number, f_screenpos},
1450 {"screenrow", 0, 0, 0, NULL,
1451 ret_number, f_screenrow},
1452 {"screenstring", 2, 2, FEARG_1, NULL,
1453 ret_string, f_screenstring},
1454 {"search", 1, 5, FEARG_1, NULL,
1455 ret_number, f_search},
1456 {"searchcount", 0, 1, FEARG_1, NULL,
1457 ret_dict_any, f_searchcount},
1458 {"searchdecl", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001459 ret_number_bool, f_searchdecl},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001460 {"searchpair", 3, 7, 0, NULL,
1461 ret_number, f_searchpair},
1462 {"searchpairpos", 3, 7, 0, NULL,
1463 ret_list_number, f_searchpairpos},
1464 {"searchpos", 1, 5, FEARG_1, NULL,
1465 ret_list_number, f_searchpos},
1466 {"server2client", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001467 ret_number_bool, f_server2client},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001468 {"serverlist", 0, 0, 0, NULL,
1469 ret_string, f_serverlist},
1470 {"setbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001471 ret_number_bool, f_setbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001472 {"setbufvar", 3, 3, FEARG_3, NULL,
1473 ret_void, f_setbufvar},
1474 {"setcellwidths", 1, 1, FEARG_1, NULL,
1475 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001476 {"setcharpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001477 ret_number_bool, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001478 {"setcharsearch", 1, 1, FEARG_1, NULL,
1479 ret_void, f_setcharsearch},
1480 {"setcmdpos", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001481 ret_number_bool, f_setcmdpos},
1482 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1483 ret_number_bool, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001484 {"setenv", 2, 2, FEARG_2, NULL,
1485 ret_void, f_setenv},
1486 {"setfperm", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001487 ret_number_bool, f_setfperm},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001488 {"setline", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001489 ret_number_bool, f_setline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001490 {"setloclist", 2, 4, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001491 ret_number_bool, f_setloclist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001492 {"setmatches", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001493 ret_number_bool, f_setmatches},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001494 {"setpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001495 ret_number_bool, f_setpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001496 {"setqflist", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001497 ret_number_bool, f_setqflist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001498 {"setreg", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001499 ret_number_bool, f_setreg},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001500 {"settabvar", 3, 3, FEARG_3, NULL,
1501 ret_void, f_settabvar},
1502 {"settabwinvar", 4, 4, FEARG_4, NULL,
1503 ret_void, f_settabwinvar},
1504 {"settagstack", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001505 ret_number_bool, f_settagstack},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001506 {"setwinvar", 3, 3, FEARG_3, NULL,
1507 ret_void, f_setwinvar},
1508 {"sha256", 1, 1, FEARG_1, NULL,
1509 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001510#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001511 f_sha256
1512#else
1513 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001514#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001515 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001516 {"shellescape", 1, 2, FEARG_1, NULL,
1517 ret_string, f_shellescape},
1518 {"shiftwidth", 0, 1, FEARG_1, NULL,
1519 ret_number, f_shiftwidth},
1520 {"sign_define", 1, 2, FEARG_1, NULL,
1521 ret_any, SIGN_FUNC(f_sign_define)},
1522 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1523 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1524 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1525 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1526 {"sign_jump", 3, 3, FEARG_1, NULL,
1527 ret_number, SIGN_FUNC(f_sign_jump)},
1528 {"sign_place", 4, 5, FEARG_1, NULL,
1529 ret_number, SIGN_FUNC(f_sign_place)},
1530 {"sign_placelist", 1, 1, FEARG_1, NULL,
1531 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1532 {"sign_undefine", 0, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001533 ret_number_bool, SIGN_FUNC(f_sign_undefine)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001534 {"sign_unplace", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001535 ret_number_bool, SIGN_FUNC(f_sign_unplace)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001536 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1537 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1538 {"simplify", 1, 1, FEARG_1, NULL,
1539 ret_string, f_simplify},
1540 {"sin", 1, 1, FEARG_1, NULL,
1541 ret_float, FLOAT_FUNC(f_sin)},
1542 {"sinh", 1, 1, FEARG_1, NULL,
1543 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001544 {"slice", 2, 3, FEARG_1, NULL,
1545 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001546 {"sort", 1, 3, FEARG_1, NULL,
1547 ret_first_arg, f_sort},
1548 {"sound_clear", 0, 0, 0, NULL,
1549 ret_void, SOUND_FUNC(f_sound_clear)},
1550 {"sound_playevent", 1, 2, FEARG_1, NULL,
1551 ret_number, SOUND_FUNC(f_sound_playevent)},
1552 {"sound_playfile", 1, 2, FEARG_1, NULL,
1553 ret_number, SOUND_FUNC(f_sound_playfile)},
1554 {"sound_stop", 1, 1, FEARG_1, NULL,
1555 ret_void, SOUND_FUNC(f_sound_stop)},
1556 {"soundfold", 1, 1, FEARG_1, NULL,
1557 ret_string, f_soundfold},
1558 {"spellbadword", 0, 1, FEARG_1, NULL,
1559 ret_list_string, f_spellbadword},
1560 {"spellsuggest", 1, 3, FEARG_1, NULL,
1561 ret_list_string, f_spellsuggest},
1562 {"split", 1, 3, FEARG_1, NULL,
1563 ret_list_string, f_split},
1564 {"sqrt", 1, 1, FEARG_1, NULL,
1565 ret_float, FLOAT_FUNC(f_sqrt)},
1566 {"srand", 0, 1, FEARG_1, NULL,
1567 ret_list_number, f_srand},
1568 {"state", 0, 1, FEARG_1, NULL,
1569 ret_string, f_state},
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001570 {"str2float", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001571 ret_float, FLOAT_FUNC(f_str2float)},
1572 {"str2list", 1, 2, FEARG_1, NULL,
1573 ret_list_number, f_str2list},
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001574 {"str2nr", 1, 3, FEARG_1, arg3_string_nr_bool,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001575 ret_number, f_str2nr},
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01001576 {"strcharlen", 1, 1, FEARG_1, NULL,
1577 ret_number, f_strcharlen},
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01001578 {"strcharpart", 2, 4, FEARG_1, NULL,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001579 ret_string, f_strcharpart},
1580 {"strchars", 1, 2, FEARG_1, NULL,
1581 ret_number, f_strchars},
1582 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1583 ret_number, f_strdisplaywidth},
1584 {"strftime", 1, 2, FEARG_1, NULL,
1585 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001586#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001587 f_strftime
1588#else
1589 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001590#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001591 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001592 {"strgetchar", 2, 2, FEARG_1, NULL,
1593 ret_number, f_strgetchar},
1594 {"stridx", 2, 3, FEARG_1, NULL,
1595 ret_number, f_stridx},
1596 {"string", 1, 1, FEARG_1, NULL,
1597 ret_string, f_string},
1598 {"strlen", 1, 1, FEARG_1, NULL,
1599 ret_number, f_strlen},
1600 {"strpart", 2, 4, FEARG_1, NULL,
1601 ret_string, f_strpart},
1602 {"strptime", 2, 2, FEARG_1, NULL,
1603 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001604#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001605 f_strptime
1606#else
1607 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001608#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001609 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001610 {"strridx", 2, 3, FEARG_1, NULL,
1611 ret_number, f_strridx},
1612 {"strtrans", 1, 1, FEARG_1, NULL,
1613 ret_string, f_strtrans},
1614 {"strwidth", 1, 1, FEARG_1, NULL,
1615 ret_number, f_strwidth},
1616 {"submatch", 1, 2, FEARG_1, NULL,
1617 ret_string, f_submatch},
1618 {"substitute", 4, 4, FEARG_1, NULL,
1619 ret_string, f_substitute},
1620 {"swapinfo", 1, 1, FEARG_1, NULL,
1621 ret_dict_any, f_swapinfo},
1622 {"swapname", 1, 1, FEARG_1, NULL,
1623 ret_string, f_swapname},
1624 {"synID", 3, 3, 0, NULL,
1625 ret_number, f_synID},
1626 {"synIDattr", 2, 3, FEARG_1, NULL,
1627 ret_string, f_synIDattr},
1628 {"synIDtrans", 1, 1, FEARG_1, NULL,
1629 ret_number, f_synIDtrans},
1630 {"synconcealed", 2, 2, 0, NULL,
1631 ret_list_any, f_synconcealed},
1632 {"synstack", 2, 2, 0, NULL,
1633 ret_list_number, f_synstack},
1634 {"system", 1, 2, FEARG_1, NULL,
1635 ret_string, f_system},
1636 {"systemlist", 1, 2, FEARG_1, NULL,
1637 ret_list_string, f_systemlist},
1638 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1639 ret_list_number, f_tabpagebuflist},
1640 {"tabpagenr", 0, 1, 0, NULL,
1641 ret_number, f_tabpagenr},
1642 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1643 ret_number, f_tabpagewinnr},
1644 {"tagfiles", 0, 0, 0, NULL,
1645 ret_list_string, f_tagfiles},
1646 {"taglist", 1, 2, FEARG_1, NULL,
1647 ret_list_dict_any, f_taglist},
1648 {"tan", 1, 1, FEARG_1, NULL,
1649 ret_float, FLOAT_FUNC(f_tan)},
1650 {"tanh", 1, 1, FEARG_1, NULL,
1651 ret_float, FLOAT_FUNC(f_tanh)},
1652 {"tempname", 0, 0, 0, NULL,
1653 ret_string, f_tempname},
1654 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1655 ret_number, TERM_FUNC(f_term_dumpdiff)},
1656 {"term_dumpload", 1, 2, FEARG_1, NULL,
1657 ret_number, TERM_FUNC(f_term_dumpload)},
1658 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1659 ret_void, TERM_FUNC(f_term_dumpwrite)},
1660 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1661 ret_number, TERM_FUNC(f_term_getaltscreen)},
1662 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1663 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001664#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001665 f_term_getansicolors
1666#else
1667 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001668#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001669 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001670 {"term_getattr", 2, 2, FEARG_1, NULL,
1671 ret_number, TERM_FUNC(f_term_getattr)},
1672 {"term_getcursor", 1, 1, FEARG_1, NULL,
1673 ret_list_any, TERM_FUNC(f_term_getcursor)},
1674 {"term_getjob", 1, 1, FEARG_1, NULL,
1675 ret_job, TERM_FUNC(f_term_getjob)},
1676 {"term_getline", 2, 2, FEARG_1, NULL,
1677 ret_string, TERM_FUNC(f_term_getline)},
1678 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1679 ret_number, TERM_FUNC(f_term_getscrolled)},
1680 {"term_getsize", 1, 1, FEARG_1, NULL,
1681 ret_list_number, TERM_FUNC(f_term_getsize)},
1682 {"term_getstatus", 1, 1, FEARG_1, NULL,
1683 ret_string, TERM_FUNC(f_term_getstatus)},
1684 {"term_gettitle", 1, 1, FEARG_1, NULL,
1685 ret_string, TERM_FUNC(f_term_gettitle)},
1686 {"term_gettty", 1, 2, FEARG_1, NULL,
1687 ret_string, TERM_FUNC(f_term_gettty)},
1688 {"term_list", 0, 0, 0, NULL,
1689 ret_list_number, TERM_FUNC(f_term_list)},
1690 {"term_scrape", 2, 2, FEARG_1, NULL,
1691 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1692 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1693 ret_void, TERM_FUNC(f_term_sendkeys)},
1694 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1695 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001696#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001697 f_term_setansicolors
1698#else
1699 NULL
1700#endif
1701 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001702 {"term_setapi", 2, 2, FEARG_1, NULL,
1703 ret_void, TERM_FUNC(f_term_setapi)},
1704 {"term_setkill", 2, 2, FEARG_1, NULL,
1705 ret_void, TERM_FUNC(f_term_setkill)},
1706 {"term_setrestore", 2, 2, FEARG_1, NULL,
1707 ret_void, TERM_FUNC(f_term_setrestore)},
1708 {"term_setsize", 3, 3, FEARG_1, NULL,
1709 ret_void, TERM_FUNC(f_term_setsize)},
1710 {"term_start", 1, 2, FEARG_1, NULL,
1711 ret_number, TERM_FUNC(f_term_start)},
1712 {"term_wait", 1, 2, FEARG_1, NULL,
1713 ret_void, TERM_FUNC(f_term_wait)},
1714 {"terminalprops", 0, 0, 0, NULL,
1715 ret_dict_string, f_terminalprops},
1716 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1717 ret_void, f_test_alloc_fail},
1718 {"test_autochdir", 0, 0, 0, NULL,
1719 ret_void, f_test_autochdir},
1720 {"test_feedinput", 1, 1, FEARG_1, NULL,
1721 ret_void, f_test_feedinput},
1722 {"test_garbagecollect_now", 0, 0, 0, NULL,
1723 ret_void, f_test_garbagecollect_now},
1724 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1725 ret_void, f_test_garbagecollect_soon},
1726 {"test_getvalue", 1, 1, FEARG_1, NULL,
1727 ret_number, f_test_getvalue},
1728 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1729 ret_void, f_test_ignore_error},
1730 {"test_null_blob", 0, 0, 0, NULL,
1731 ret_blob, f_test_null_blob},
1732 {"test_null_channel", 0, 0, 0, NULL,
1733 ret_channel, JOB_FUNC(f_test_null_channel)},
1734 {"test_null_dict", 0, 0, 0, NULL,
1735 ret_dict_any, f_test_null_dict},
1736 {"test_null_function", 0, 0, 0, NULL,
1737 ret_func_any, f_test_null_function},
1738 {"test_null_job", 0, 0, 0, NULL,
1739 ret_job, JOB_FUNC(f_test_null_job)},
1740 {"test_null_list", 0, 0, 0, NULL,
1741 ret_list_any, f_test_null_list},
1742 {"test_null_partial", 0, 0, 0, NULL,
1743 ret_func_any, f_test_null_partial},
1744 {"test_null_string", 0, 0, 0, NULL,
1745 ret_string, f_test_null_string},
1746 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1747 ret_void, f_test_option_not_set},
1748 {"test_override", 2, 2, FEARG_2, NULL,
1749 ret_void, f_test_override},
1750 {"test_refcount", 1, 1, FEARG_1, NULL,
1751 ret_number, f_test_refcount},
1752 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1753 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001754#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001755 f_test_scrollbar
1756#else
1757 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001758#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001759 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001760 {"test_setmouse", 2, 2, 0, NULL,
1761 ret_void, f_test_setmouse},
1762 {"test_settime", 1, 1, FEARG_1, NULL,
1763 ret_void, f_test_settime},
1764 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1765 ret_void, f_test_srand_seed},
1766 {"test_unknown", 0, 0, 0, NULL,
1767 ret_any, f_test_unknown},
1768 {"test_void", 0, 0, 0, NULL,
1769 ret_void, f_test_void},
1770 {"timer_info", 0, 1, FEARG_1, NULL,
1771 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1772 {"timer_pause", 2, 2, FEARG_1, NULL,
1773 ret_void, TIMER_FUNC(f_timer_pause)},
1774 {"timer_start", 2, 3, FEARG_1, NULL,
1775 ret_number, TIMER_FUNC(f_timer_start)},
1776 {"timer_stop", 1, 1, FEARG_1, NULL,
1777 ret_void, TIMER_FUNC(f_timer_stop)},
1778 {"timer_stopall", 0, 0, 0, NULL,
1779 ret_void, TIMER_FUNC(f_timer_stopall)},
1780 {"tolower", 1, 1, FEARG_1, NULL,
1781 ret_string, f_tolower},
1782 {"toupper", 1, 1, FEARG_1, NULL,
1783 ret_string, f_toupper},
1784 {"tr", 3, 3, FEARG_1, NULL,
1785 ret_string, f_tr},
1786 {"trim", 1, 3, FEARG_1, NULL,
1787 ret_string, f_trim},
1788 {"trunc", 1, 1, FEARG_1, NULL,
1789 ret_float, FLOAT_FUNC(f_trunc)},
1790 {"type", 1, 1, FEARG_1, NULL,
1791 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001792 {"typename", 1, 1, FEARG_1, NULL,
1793 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001794 {"undofile", 1, 1, FEARG_1, NULL,
1795 ret_string, f_undofile},
1796 {"undotree", 0, 0, 0, NULL,
1797 ret_dict_any, f_undotree},
1798 {"uniq", 1, 3, FEARG_1, NULL,
1799 ret_list_any, f_uniq},
1800 {"values", 1, 1, FEARG_1, NULL,
1801 ret_list_any, f_values},
1802 {"virtcol", 1, 1, FEARG_1, NULL,
1803 ret_number, f_virtcol},
1804 {"visualmode", 0, 1, 0, NULL,
1805 ret_string, f_visualmode},
1806 {"wildmenumode", 0, 0, 0, NULL,
1807 ret_number, f_wildmenumode},
1808 {"win_execute", 2, 3, FEARG_2, NULL,
1809 ret_string, f_win_execute},
1810 {"win_findbuf", 1, 1, FEARG_1, NULL,
1811 ret_list_number, f_win_findbuf},
1812 {"win_getid", 0, 2, FEARG_1, NULL,
1813 ret_number, f_win_getid},
1814 {"win_gettype", 0, 1, FEARG_1, NULL,
1815 ret_string, f_win_gettype},
1816 {"win_gotoid", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001817 ret_number_bool, f_win_gotoid},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001818 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1819 ret_list_number, f_win_id2tabwin},
1820 {"win_id2win", 1, 1, FEARG_1, NULL,
1821 ret_number, f_win_id2win},
1822 {"win_screenpos", 1, 1, FEARG_1, NULL,
1823 ret_list_number, f_win_screenpos},
1824 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001825 ret_number_bool, f_win_splitmove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001826 {"winbufnr", 1, 1, FEARG_1, NULL,
1827 ret_number, f_winbufnr},
1828 {"wincol", 0, 0, 0, NULL,
1829 ret_number, f_wincol},
1830 {"windowsversion", 0, 0, 0, NULL,
1831 ret_string, f_windowsversion},
1832 {"winheight", 1, 1, FEARG_1, NULL,
1833 ret_number, f_winheight},
1834 {"winlayout", 0, 1, FEARG_1, NULL,
1835 ret_list_any, f_winlayout},
1836 {"winline", 0, 0, 0, NULL,
1837 ret_number, f_winline},
1838 {"winnr", 0, 1, FEARG_1, NULL,
1839 ret_number, f_winnr},
1840 {"winrestcmd", 0, 0, 0, NULL,
1841 ret_string, f_winrestcmd},
1842 {"winrestview", 1, 1, FEARG_1, NULL,
1843 ret_void, f_winrestview},
1844 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001845 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001846 {"winwidth", 1, 1, FEARG_1, NULL,
1847 ret_number, f_winwidth},
1848 {"wordcount", 0, 0, 0, NULL,
1849 ret_dict_number, f_wordcount},
1850 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001851 ret_number_bool, f_writefile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001852 {"xor", 2, 2, FEARG_1, NULL,
1853 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001854};
1855
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001856/*
1857 * Function given to ExpandGeneric() to obtain the list of internal
1858 * or user defined function names.
1859 */
1860 char_u *
1861get_function_name(expand_T *xp, int idx)
1862{
1863 static int intidx = -1;
1864 char_u *name;
1865
1866 if (idx == 0)
1867 intidx = -1;
1868 if (intidx < 0)
1869 {
1870 name = get_user_func_name(xp, idx);
1871 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001872 {
1873 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1874 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001875 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001876 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001877 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001878 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001879 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001880 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001881 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001882 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001883 STRCAT(IObuff, ")");
1884 return IObuff;
1885 }
1886
1887 return NULL;
1888}
1889
1890/*
1891 * Function given to ExpandGeneric() to obtain the list of internal or
1892 * user defined variable or function names.
1893 */
1894 char_u *
1895get_expr_name(expand_T *xp, int idx)
1896{
1897 static int intidx = -1;
1898 char_u *name;
1899
1900 if (idx == 0)
1901 intidx = -1;
1902 if (intidx < 0)
1903 {
1904 name = get_function_name(xp, idx);
1905 if (name != NULL)
1906 return name;
1907 }
1908 return get_user_var_name(xp, ++intidx);
1909}
1910
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001911/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001912 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001913 * Return index, or -1 if not found or "implemented" is TRUE and the function
1914 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001915 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001916 static int
1917find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001918{
1919 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001920 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001921 int cmp;
1922 int x;
1923
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001924 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001925
1926 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927 while (first <= last)
1928 {
1929 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001930 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001931 if (cmp < 0)
1932 last = x - 1;
1933 else if (cmp > 0)
1934 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001935 else if (implemented && global_functions[x].f_func == NULL)
1936 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001937 else
1938 return x;
1939 }
1940 return -1;
1941}
1942
Bram Moolenaar15c47602020-03-26 22:16:48 +01001943/*
1944 * Find internal function "name" in table "global_functions".
1945 * Return index, or -1 if not found or the function is not implemented.
1946 */
1947 int
1948find_internal_func(char_u *name)
1949{
1950 return find_internal_func_opt(name, TRUE);
1951}
1952
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001953 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001954has_internal_func(char_u *name)
1955{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001956 return find_internal_func_opt(name, TRUE) >= 0;
1957}
1958
1959 static int
1960has_internal_func_name(char_u *name)
1961{
1962 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001963}
1964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 char *
1966internal_func_name(int idx)
1967{
1968 return global_functions[idx].f_name;
1969}
1970
Bram Moolenaar94738d82020-10-21 14:25:07 +02001971/*
1972 * Check the argument types for builting function "idx".
1973 * Uses the list of types on the type stack: "types".
1974 * Return FAIL and gives an error message when a type is wrong.
1975 */
1976 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001977internal_func_check_arg_types(
1978 type_T **types,
1979 int idx,
1980 int argcount,
1981 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001982{
1983 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1984 int i;
1985
1986 if (argchecks != NULL)
1987 {
1988 argcontext_T context;
1989
1990 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001991 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001992 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001993 for (i = 0; i < argcount; ++i)
1994 if (argchecks[i] != NULL)
1995 {
1996 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001997 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001998 return FAIL;
1999 }
2000 }
2001 return OK;
2002}
2003
Bram Moolenaara1224cb2020-10-22 12:31:49 +02002004/*
2005 * Call the "f_retfunc" function to obtain the return type of function "idx".
2006 * "argtypes" is the list of argument types or NULL when there are no
2007 * arguments.
2008 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002010internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002012 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013}
2014
2015/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002016 * Return TRUE if "idx" is for the map() function.
2017 */
2018 int
2019internal_func_is_map(int idx)
2020{
2021 return global_functions[idx].f_func == f_map;
2022}
2023
2024/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002026 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2027 * first argument, 2 if method base is second argument, etc. 9 if method base
2028 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 */
2030 int
2031check_internal_func(int idx, int argcount)
2032{
2033 int res;
2034 char *name;
2035
2036 if (argcount < global_functions[idx].f_min_argc)
2037 res = FCERR_TOOFEW;
2038 else if (argcount > global_functions[idx].f_max_argc)
2039 res = FCERR_TOOMANY;
2040 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002041 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042
2043 name = internal_func_name(idx);
2044 if (res == FCERR_TOOMANY)
2045 semsg(_(e_toomanyarg), name);
2046 else
2047 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002048 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049}
2050
Bram Moolenaarac92e252019-08-03 21:58:38 +02002051 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002052call_internal_func(
2053 char_u *name,
2054 int argcount,
2055 typval_T *argvars,
2056 typval_T *rettv)
2057{
2058 int i;
2059
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002060 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002061 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002062 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002063 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002064 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002065 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002066 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002067 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002068 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002069 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002070}
2071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 void
2073call_internal_func_by_idx(
2074 int idx,
2075 typval_T *argvars,
2076 typval_T *rettv)
2077{
2078 global_functions[idx].f_func(argvars, rettv);
2079}
2080
Bram Moolenaarac92e252019-08-03 21:58:38 +02002081/*
2082 * Invoke a method for base->method().
2083 */
2084 int
2085call_internal_method(
2086 char_u *name,
2087 int argcount,
2088 typval_T *argvars,
2089 typval_T *rettv,
2090 typval_T *basetv)
2091{
2092 int i;
2093 int fi;
2094 typval_T argv[MAX_FUNC_ARGS + 1];
2095
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002096 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002097 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002098 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002099 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002100 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002101 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002102 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002103 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002104 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002105
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002106 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002107 {
2108 // base value goes last
2109 for (i = 0; i < argcount; ++i)
2110 argv[i] = argvars[i];
2111 argv[argcount] = *basetv;
2112 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002113 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002114 {
2115 // base value goes second
2116 argv[0] = argvars[0];
2117 argv[1] = *basetv;
2118 for (i = 1; i < argcount; ++i)
2119 argv[i + 1] = argvars[i];
2120 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002121 else if (global_functions[fi].f_argtype == FEARG_3)
2122 {
2123 // base value goes third
2124 argv[0] = argvars[0];
2125 argv[1] = argvars[1];
2126 argv[2] = *basetv;
2127 for (i = 2; i < argcount; ++i)
2128 argv[i + 1] = argvars[i];
2129 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002130 else if (global_functions[fi].f_argtype == FEARG_4)
2131 {
2132 // base value goes fourth
2133 argv[0] = argvars[0];
2134 argv[1] = argvars[1];
2135 argv[2] = argvars[2];
2136 argv[3] = *basetv;
2137 for (i = 3; i < argcount; ++i)
2138 argv[i + 1] = argvars[i];
2139 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002140 else
2141 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002142 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002143 argv[0] = *basetv;
2144 for (i = 0; i < argcount; ++i)
2145 argv[i + 1] = argvars[i];
2146 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002147 argv[argcount + 1].v_type = VAR_UNKNOWN;
2148
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002149 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002150 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002151}
2152
2153/*
2154 * Return TRUE for a non-zero Number and a non-empty String.
2155 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002156 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157non_zero_arg(typval_T *argvars)
2158{
2159 return ((argvars[0].v_type == VAR_NUMBER
2160 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002161 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002162 && argvars[0].vval.v_number == VVAL_TRUE)
2163 || (argvars[0].v_type == VAR_STRING
2164 && argvars[0].vval.v_string != NULL
2165 && *argvars[0].vval.v_string != NUL));
2166}
2167
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002168#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002169/*
2170 * Get the float value of "argvars[0]" into "f".
2171 * Returns FAIL when the argument is not a Number or Float.
2172 */
2173 static int
2174get_float_arg(typval_T *argvars, float_T *f)
2175{
2176 if (argvars[0].v_type == VAR_FLOAT)
2177 {
2178 *f = argvars[0].vval.v_float;
2179 return OK;
2180 }
2181 if (argvars[0].v_type == VAR_NUMBER)
2182 {
2183 *f = (float_T)argvars[0].vval.v_number;
2184 return OK;
2185 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002186 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002187 return FAIL;
2188}
2189
2190/*
2191 * "abs(expr)" function
2192 */
2193 static void
2194f_abs(typval_T *argvars, typval_T *rettv)
2195{
2196 if (argvars[0].v_type == VAR_FLOAT)
2197 {
2198 rettv->v_type = VAR_FLOAT;
2199 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
2200 }
2201 else
2202 {
2203 varnumber_T n;
2204 int error = FALSE;
2205
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002206 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002207 if (error)
2208 rettv->vval.v_number = -1;
2209 else if (n > 0)
2210 rettv->vval.v_number = n;
2211 else
2212 rettv->vval.v_number = -n;
2213 }
2214}
2215
2216/*
2217 * "acos()" function
2218 */
2219 static void
2220f_acos(typval_T *argvars, typval_T *rettv)
2221{
2222 float_T f = 0.0;
2223
2224 rettv->v_type = VAR_FLOAT;
2225 if (get_float_arg(argvars, &f) == OK)
2226 rettv->vval.v_float = acos(f);
2227 else
2228 rettv->vval.v_float = 0.0;
2229}
2230#endif
2231
2232/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002233 * "and(expr, expr)" function
2234 */
2235 static void
2236f_and(typval_T *argvars, typval_T *rettv)
2237{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002238 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2239 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002240}
2241
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002242#ifdef FEAT_FLOAT
2243/*
2244 * "asin()" function
2245 */
2246 static void
2247f_asin(typval_T *argvars, typval_T *rettv)
2248{
2249 float_T f = 0.0;
2250
2251 rettv->v_type = VAR_FLOAT;
2252 if (get_float_arg(argvars, &f) == OK)
2253 rettv->vval.v_float = asin(f);
2254 else
2255 rettv->vval.v_float = 0.0;
2256}
2257
2258/*
2259 * "atan()" function
2260 */
2261 static void
2262f_atan(typval_T *argvars, typval_T *rettv)
2263{
2264 float_T f = 0.0;
2265
2266 rettv->v_type = VAR_FLOAT;
2267 if (get_float_arg(argvars, &f) == OK)
2268 rettv->vval.v_float = atan(f);
2269 else
2270 rettv->vval.v_float = 0.0;
2271}
2272
2273/*
2274 * "atan2()" function
2275 */
2276 static void
2277f_atan2(typval_T *argvars, typval_T *rettv)
2278{
2279 float_T fx = 0.0, fy = 0.0;
2280
2281 rettv->v_type = VAR_FLOAT;
2282 if (get_float_arg(argvars, &fx) == OK
2283 && get_float_arg(&argvars[1], &fy) == OK)
2284 rettv->vval.v_float = atan2(fx, fy);
2285 else
2286 rettv->vval.v_float = 0.0;
2287}
2288#endif
2289
2290/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002291 * "balloon_show()" function
2292 */
2293#ifdef FEAT_BEVAL
2294 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002295f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2296{
2297 rettv->v_type = VAR_STRING;
2298 if (balloonEval != NULL)
2299 {
2300 if (balloonEval->msg == NULL)
2301 rettv->vval.v_string = NULL;
2302 else
2303 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2304 }
2305}
2306
2307 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002308f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2309{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002310 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002311 {
2312 if (argvars[0].v_type == VAR_LIST
2313# ifdef FEAT_GUI
2314 && !gui.in_use
2315# endif
2316 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002317 {
2318 list_T *l = argvars[0].vval.v_list;
2319
2320 // empty list removes the balloon
2321 post_balloon(balloonEval, NULL,
2322 l == NULL || l->lv_len == 0 ? NULL : l);
2323 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002324 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002325 {
2326 char_u *mesg = tv_get_string_chk(&argvars[0]);
2327
2328 if (mesg != NULL)
2329 // empty string removes the balloon
2330 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2331 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002332 }
2333}
2334
Bram Moolenaar669a8282017-11-19 20:13:05 +01002335# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002336 static void
2337f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2338{
2339 if (rettv_list_alloc(rettv) == OK)
2340 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002341 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002342
2343 if (msg != NULL)
2344 {
2345 pumitem_T *array;
2346 int size = split_message(msg, &array);
2347 int i;
2348
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002349 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002350 for (i = 1; i < size - 1; ++i)
2351 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002352 while (size > 0)
2353 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002354 vim_free(array);
2355 }
2356 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002357}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002358# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002359#endif
2360
2361/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002362 * Get the buffer from "arg" and give an error and return NULL if it is not
2363 * valid.
2364 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002365 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002366get_buf_arg(typval_T *arg)
2367{
2368 buf_T *buf;
2369
2370 ++emsg_off;
2371 buf = tv_get_buf(arg, FALSE);
2372 --emsg_off;
2373 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002374 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002375 return buf;
2376}
2377
2378/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002379 * "byte2line(byte)" function
2380 */
2381 static void
2382f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2383{
2384#ifndef FEAT_BYTEOFF
2385 rettv->vval.v_number = -1;
2386#else
2387 long boff = 0;
2388
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002389 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002390 if (boff < 0)
2391 rettv->vval.v_number = -1;
2392 else
2393 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2394 (linenr_T)0, &boff);
2395#endif
2396}
2397
2398 static void
2399byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2400{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002401 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002402 char_u *str;
2403 varnumber_T idx;
2404
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002405 str = tv_get_string_chk(&argvars[0]);
2406 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002407 rettv->vval.v_number = -1;
2408 if (str == NULL || idx < 0)
2409 return;
2410
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002411 t = str;
2412 for ( ; idx > 0; idx--)
2413 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002414 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002415 return;
2416 if (enc_utf8 && comp)
2417 t += utf_ptr2len(t);
2418 else
2419 t += (*mb_ptr2len)(t);
2420 }
2421 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002422}
2423
2424/*
2425 * "byteidx()" function
2426 */
2427 static void
2428f_byteidx(typval_T *argvars, typval_T *rettv)
2429{
2430 byteidx(argvars, rettv, FALSE);
2431}
2432
2433/*
2434 * "byteidxcomp()" function
2435 */
2436 static void
2437f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2438{
2439 byteidx(argvars, rettv, TRUE);
2440}
2441
2442/*
2443 * "call(func, arglist [, dict])" function
2444 */
2445 static void
2446f_call(typval_T *argvars, typval_T *rettv)
2447{
2448 char_u *func;
2449 partial_T *partial = NULL;
2450 dict_T *selfdict = NULL;
2451
2452 if (argvars[1].v_type != VAR_LIST)
2453 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002454 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002455 return;
2456 }
2457 if (argvars[1].vval.v_list == NULL)
2458 return;
2459
2460 if (argvars[0].v_type == VAR_FUNC)
2461 func = argvars[0].vval.v_string;
2462 else if (argvars[0].v_type == VAR_PARTIAL)
2463 {
2464 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002465 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002466 }
2467 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002468 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002469 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002470 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002471
2472 if (argvars[2].v_type != VAR_UNKNOWN)
2473 {
2474 if (argvars[2].v_type != VAR_DICT)
2475 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002476 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002477 return;
2478 }
2479 selfdict = argvars[2].vval.v_dict;
2480 }
2481
2482 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2483}
2484
2485#ifdef FEAT_FLOAT
2486/*
2487 * "ceil({float})" function
2488 */
2489 static void
2490f_ceil(typval_T *argvars, typval_T *rettv)
2491{
2492 float_T f = 0.0;
2493
2494 rettv->v_type = VAR_FLOAT;
2495 if (get_float_arg(argvars, &f) == OK)
2496 rettv->vval.v_float = ceil(f);
2497 else
2498 rettv->vval.v_float = 0.0;
2499}
2500#endif
2501
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002502/*
2503 * "changenr()" function
2504 */
2505 static void
2506f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2507{
2508 rettv->vval.v_number = curbuf->b_u_seq_cur;
2509}
2510
2511/*
2512 * "char2nr(string)" function
2513 */
2514 static void
2515f_char2nr(typval_T *argvars, typval_T *rettv)
2516{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002517 if (has_mbyte)
2518 {
2519 int utf8 = 0;
2520
2521 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002522 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002523
2524 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002525 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002526 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002527 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002528 }
2529 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002530 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002531}
2532
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002533/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002534 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2535 * returns the character index of the column. Otherwise, returns the byte index
2536 * of the column.
2537 */
2538 static void
2539get_col(typval_T *argvars, typval_T *rettv, int charcol)
2540{
2541 colnr_T col = 0;
2542 pos_T *fp;
2543 int fnum = curbuf->b_fnum;
2544
2545 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2546 if (fp != NULL && fnum == curbuf->b_fnum)
2547 {
2548 if (fp->col == MAXCOL)
2549 {
2550 // '> can be MAXCOL, get the length of the line then
2551 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2552 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2553 else
2554 col = MAXCOL;
2555 }
2556 else
2557 {
2558 col = fp->col + 1;
2559 // col(".") when the cursor is on the NUL at the end of the line
2560 // because of "coladd" can be seen as an extra column.
2561 if (virtual_active() && fp == &curwin->w_cursor)
2562 {
2563 char_u *p = ml_get_cursor();
2564
2565 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2566 curwin->w_virtcol - curwin->w_cursor.coladd))
2567 {
2568 int l;
2569
2570 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2571 col += l;
2572 }
2573 }
2574 }
2575 }
2576 rettv->vval.v_number = col;
2577}
2578
2579/*
2580 * "charcol()" function
2581 */
2582 static void
2583f_charcol(typval_T *argvars, typval_T *rettv)
2584{
2585 get_col(argvars, rettv, TRUE);
2586}
2587
2588/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002589 * "charidx()" function
2590 */
2591 static void
2592f_charidx(typval_T *argvars, typval_T *rettv)
2593{
2594 char_u *str;
2595 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002596 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002597 char_u *p;
2598 int len;
2599 int (*ptr2len)(char_u *);
2600
2601 rettv->vval.v_number = -1;
2602
2603 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2604 || (argvars[2].v_type != VAR_UNKNOWN
2605 && argvars[2].v_type != VAR_NUMBER))
2606 {
2607 emsg(_(e_invarg));
2608 return;
2609 }
2610
2611 str = tv_get_string_chk(&argvars[0]);
2612 idx = tv_get_number_chk(&argvars[1], NULL);
2613 if (str == NULL || idx < 0)
2614 return;
2615
2616 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002617 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002618 if (countcc < 0 || countcc > 1)
2619 {
2620 semsg(_(e_using_number_as_bool_nr), countcc);
2621 return;
2622 }
2623
2624 if (enc_utf8 && countcc)
2625 ptr2len = utf_ptr2len;
2626 else
2627 ptr2len = mb_ptr2len;
2628
2629 for (p = str, len = 0; p <= str + idx; len++)
2630 {
2631 if (*p == NUL)
2632 return;
2633 p += ptr2len(p);
2634 }
2635
2636 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2637}
2638
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002639 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002640get_optional_window(typval_T *argvars, int idx)
2641{
2642 win_T *win = curwin;
2643
2644 if (argvars[idx].v_type != VAR_UNKNOWN)
2645 {
2646 win = find_win_by_nr_or_id(&argvars[idx]);
2647 if (win == NULL)
2648 {
2649 emsg(_(e_invalwindow));
2650 return NULL;
2651 }
2652 }
2653 return win;
2654}
2655
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002656/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002657 * "col(string)" function
2658 */
2659 static void
2660f_col(typval_T *argvars, typval_T *rettv)
2661{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002662 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002663}
2664
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002665/*
2666 * "confirm(message, buttons[, default [, type]])" function
2667 */
2668 static void
2669f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2670{
2671#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2672 char_u *message;
2673 char_u *buttons = NULL;
2674 char_u buf[NUMBUFLEN];
2675 char_u buf2[NUMBUFLEN];
2676 int def = 1;
2677 int type = VIM_GENERIC;
2678 char_u *typestr;
2679 int error = FALSE;
2680
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002681 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002682 if (message == NULL)
2683 error = TRUE;
2684 if (argvars[1].v_type != VAR_UNKNOWN)
2685 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002686 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002687 if (buttons == NULL)
2688 error = TRUE;
2689 if (argvars[2].v_type != VAR_UNKNOWN)
2690 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002691 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002692 if (argvars[3].v_type != VAR_UNKNOWN)
2693 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002694 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002695 if (typestr == NULL)
2696 error = TRUE;
2697 else
2698 {
2699 switch (TOUPPER_ASC(*typestr))
2700 {
2701 case 'E': type = VIM_ERROR; break;
2702 case 'Q': type = VIM_QUESTION; break;
2703 case 'I': type = VIM_INFO; break;
2704 case 'W': type = VIM_WARNING; break;
2705 case 'G': type = VIM_GENERIC; break;
2706 }
2707 }
2708 }
2709 }
2710 }
2711
2712 if (buttons == NULL || *buttons == NUL)
2713 buttons = (char_u *)_("&Ok");
2714
2715 if (!error)
2716 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2717 def, NULL, FALSE);
2718#endif
2719}
2720
2721/*
2722 * "copy()" function
2723 */
2724 static void
2725f_copy(typval_T *argvars, typval_T *rettv)
2726{
2727 item_copy(&argvars[0], rettv, FALSE, 0);
2728}
2729
2730#ifdef FEAT_FLOAT
2731/*
2732 * "cos()" function
2733 */
2734 static void
2735f_cos(typval_T *argvars, typval_T *rettv)
2736{
2737 float_T f = 0.0;
2738
2739 rettv->v_type = VAR_FLOAT;
2740 if (get_float_arg(argvars, &f) == OK)
2741 rettv->vval.v_float = cos(f);
2742 else
2743 rettv->vval.v_float = 0.0;
2744}
2745
2746/*
2747 * "cosh()" function
2748 */
2749 static void
2750f_cosh(typval_T *argvars, typval_T *rettv)
2751{
2752 float_T f = 0.0;
2753
2754 rettv->v_type = VAR_FLOAT;
2755 if (get_float_arg(argvars, &f) == OK)
2756 rettv->vval.v_float = cosh(f);
2757 else
2758 rettv->vval.v_float = 0.0;
2759}
2760#endif
2761
2762/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002763 * Set the cursor position.
2764 * If 'charcol' is TRUE, then use the column number as a character offet.
2765 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002766 */
2767 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002768set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002769{
2770 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002771 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002772 int set_curswant = TRUE;
2773
2774 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002775 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002776 {
2777 pos_T pos;
2778 colnr_T curswant = -1;
2779
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002780 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002781 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002782 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002783 return;
2784 }
2785 line = pos.lnum;
2786 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002787 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002788 if (curswant >= 0)
2789 {
2790 curwin->w_curswant = curswant - 1;
2791 set_curswant = FALSE;
2792 }
2793 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002794 else if ((argvars[0].v_type == VAR_NUMBER ||
2795 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002796 && (argvars[1].v_type == VAR_NUMBER ||
2797 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002798 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002799 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002800 if (line < 0)
2801 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002802 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002803 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002804 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002805 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002806 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002807 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002808 else
2809 {
2810 emsg(_(e_invarg));
2811 return;
2812 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002813 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002814 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002815 if (line > 0)
2816 curwin->w_cursor.lnum = line;
2817 if (col > 0)
2818 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002819 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002820
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002821 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002822 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002823 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002824 if (has_mbyte)
2825 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002826
2827 curwin->w_set_curswant = set_curswant;
2828 rettv->vval.v_number = 0;
2829}
2830
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002831/*
2832 * "cursor(lnum, col)" function, or
2833 * "cursor(list)"
2834 *
2835 * Moves the cursor to the specified line and column.
2836 * Returns 0 when the position could be set, -1 otherwise.
2837 */
2838 static void
2839f_cursor(typval_T *argvars, typval_T *rettv)
2840{
2841 set_cursorpos(argvars, rettv, FALSE);
2842}
2843
Bram Moolenaar4f974752019-02-17 17:44:42 +01002844#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002845/*
2846 * "debugbreak()" function
2847 */
2848 static void
2849f_debugbreak(typval_T *argvars, typval_T *rettv)
2850{
2851 int pid;
2852
2853 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002854 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002855 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002856 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002857 else
2858 {
2859 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2860
2861 if (hProcess != NULL)
2862 {
2863 DebugBreakProcess(hProcess);
2864 CloseHandle(hProcess);
2865 rettv->vval.v_number = OK;
2866 }
2867 }
2868}
2869#endif
2870
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002871/*
2872 * "deepcopy()" function
2873 */
2874 static void
2875f_deepcopy(typval_T *argvars, typval_T *rettv)
2876{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002877 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002878 int copyID;
2879
2880 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002881 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002882 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002883 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002884 else
2885 {
2886 copyID = get_copyID();
2887 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2888 }
2889}
2890
2891/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002892 * "did_filetype()" function
2893 */
2894 static void
2895f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2896{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002897 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002898}
2899
2900/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002901 * "echoraw({expr})" function
2902 */
2903 static void
2904f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2905{
2906 char_u *str = tv_get_string_chk(&argvars[0]);
2907
2908 if (str != NULL && *str != NUL)
2909 {
2910 out_str(str);
2911 out_flush();
2912 }
2913}
2914
2915/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002916 * "empty({expr})" function
2917 */
2918 static void
2919f_empty(typval_T *argvars, typval_T *rettv)
2920{
2921 int n = FALSE;
2922
2923 switch (argvars[0].v_type)
2924 {
2925 case VAR_STRING:
2926 case VAR_FUNC:
2927 n = argvars[0].vval.v_string == NULL
2928 || *argvars[0].vval.v_string == NUL;
2929 break;
2930 case VAR_PARTIAL:
2931 n = FALSE;
2932 break;
2933 case VAR_NUMBER:
2934 n = argvars[0].vval.v_number == 0;
2935 break;
2936 case VAR_FLOAT:
2937#ifdef FEAT_FLOAT
2938 n = argvars[0].vval.v_float == 0.0;
2939 break;
2940#endif
2941 case VAR_LIST:
2942 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002943 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002944 break;
2945 case VAR_DICT:
2946 n = argvars[0].vval.v_dict == NULL
2947 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2948 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002949 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002950 case VAR_SPECIAL:
2951 n = argvars[0].vval.v_number != VVAL_TRUE;
2952 break;
2953
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002954 case VAR_BLOB:
2955 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002956 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2957 break;
2958
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002959 case VAR_JOB:
2960#ifdef FEAT_JOB_CHANNEL
2961 n = argvars[0].vval.v_job == NULL
2962 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2963 break;
2964#endif
2965 case VAR_CHANNEL:
2966#ifdef FEAT_JOB_CHANNEL
2967 n = argvars[0].vval.v_channel == NULL
2968 || !channel_is_open(argvars[0].vval.v_channel);
2969 break;
2970#endif
2971 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002972 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002974 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002975 n = TRUE;
2976 break;
2977 }
2978
2979 rettv->vval.v_number = n;
2980}
2981
2982/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002983 * "environ()" function
2984 */
2985 static void
2986f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2987{
2988#if !defined(AMIGA)
2989 int i = 0;
2990 char_u *entry, *value;
2991# ifdef MSWIN
2992 extern wchar_t **_wenviron;
2993# else
2994 extern char **environ;
2995# endif
2996
2997 if (rettv_dict_alloc(rettv) != OK)
2998 return;
2999
3000# ifdef MSWIN
3001 if (*_wenviron == NULL)
3002 return;
3003# else
3004 if (*environ == NULL)
3005 return;
3006# endif
3007
3008 for (i = 0; ; ++i)
3009 {
3010# ifdef MSWIN
3011 short_u *p;
3012
3013 if ((p = (short_u *)_wenviron[i]) == NULL)
3014 return;
3015 entry = utf16_to_enc(p, NULL);
3016# else
3017 if ((entry = (char_u *)environ[i]) == NULL)
3018 return;
3019 entry = vim_strsave(entry);
3020# endif
3021 if (entry == NULL) // out of memory
3022 return;
3023 if ((value = vim_strchr(entry, '=')) == NULL)
3024 {
3025 vim_free(entry);
3026 continue;
3027 }
3028 *value++ = NUL;
3029 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3030 vim_free(entry);
3031 }
3032#endif
3033}
3034
3035/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003036 * "escape({string}, {chars})" function
3037 */
3038 static void
3039f_escape(typval_T *argvars, typval_T *rettv)
3040{
3041 char_u buf[NUMBUFLEN];
3042
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003043 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3044 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003045 rettv->v_type = VAR_STRING;
3046}
3047
3048/*
3049 * "eval()" function
3050 */
3051 static void
3052f_eval(typval_T *argvars, typval_T *rettv)
3053{
3054 char_u *s, *p;
3055
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003056 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003057 if (s != NULL)
3058 s = skipwhite(s);
3059
3060 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003061 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003062 {
3063 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003064 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003065 need_clr_eos = FALSE;
3066 rettv->v_type = VAR_NUMBER;
3067 rettv->vval.v_number = 0;
3068 }
3069 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003070 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003071}
3072
3073/*
3074 * "eventhandler()" function
3075 */
3076 static void
3077f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3078{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003079 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003080}
3081
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003082static garray_T redir_execute_ga;
3083
3084/*
3085 * Append "value[value_len]" to the execute() output.
3086 */
3087 void
3088execute_redir_str(char_u *value, int value_len)
3089{
3090 int len;
3091
3092 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003093 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003094 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003095 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003096 if (ga_grow(&redir_execute_ga, len) == OK)
3097 {
3098 mch_memmove((char *)redir_execute_ga.ga_data
3099 + redir_execute_ga.ga_len, value, len);
3100 redir_execute_ga.ga_len += len;
3101 }
3102}
3103
3104/*
3105 * Get next line from a list.
3106 * Called by do_cmdline() to get the next line.
3107 * Returns allocated string, or NULL for end of function.
3108 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003109 static char_u *
3110get_list_line(
3111 int c UNUSED,
3112 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003113 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02003114 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003115{
3116 listitem_T **p = (listitem_T **)cookie;
3117 listitem_T *item = *p;
3118 char_u buf[NUMBUFLEN];
3119 char_u *s;
3120
3121 if (item == NULL)
3122 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003123 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003124 *p = item->li_next;
3125 return s == NULL ? NULL : vim_strsave(s);
3126}
3127
3128/*
3129 * "execute()" function
3130 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02003131 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003132execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003133{
3134 char_u *cmd = NULL;
3135 list_T *list = NULL;
3136 int save_msg_silent = msg_silent;
3137 int save_emsg_silent = emsg_silent;
3138 int save_emsg_noredir = emsg_noredir;
3139 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003140 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003141 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003142 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003143 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003144
3145 rettv->vval.v_string = NULL;
3146 rettv->v_type = VAR_STRING;
3147
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003148 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003149 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003150 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003151 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003152 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003153 return;
3154 ++list->lv_refcount;
3155 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003156 else if (argvars[arg_off].v_type == VAR_JOB
3157 || argvars[arg_off].v_type == VAR_CHANNEL)
3158 {
3159 emsg(_(e_inval_string));
3160 return;
3161 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003162 else
3163 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003164 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003165 if (cmd == NULL)
3166 return;
3167 }
3168
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003169 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003170 {
3171 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003172 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003173
3174 if (s == NULL)
3175 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003176 if (*s == NUL)
3177 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003178 if (STRNCMP(s, "silent", 6) == 0)
3179 ++msg_silent;
3180 if (STRCMP(s, "silent!") == 0)
3181 {
3182 emsg_silent = TRUE;
3183 emsg_noredir = TRUE;
3184 }
3185 }
3186 else
3187 ++msg_silent;
3188
3189 if (redir_execute)
3190 save_ga = redir_execute_ga;
3191 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3192 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003193 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003194 if (!echo_output)
3195 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003196
3197 if (cmd != NULL)
3198 do_cmdline_cmd(cmd);
3199 else
3200 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003201 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003202
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003203 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003204 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003205 do_cmdline(NULL, get_list_line, (void *)&item,
3206 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3207 --list->lv_refcount;
3208 }
3209
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003210 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003211 if (ga_grow(&redir_execute_ga, 1) == OK)
3212 {
3213 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3214 rettv->vval.v_string = redir_execute_ga.ga_data;
3215 }
3216 else
3217 {
3218 ga_clear(&redir_execute_ga);
3219 rettv->vval.v_string = NULL;
3220 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003221 msg_silent = save_msg_silent;
3222 emsg_silent = save_emsg_silent;
3223 emsg_noredir = save_emsg_noredir;
3224
3225 redir_execute = save_redir_execute;
3226 if (redir_execute)
3227 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003228 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003229
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003230 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003231 if (echo_output)
3232 // When not working silently: put it in column zero. A following
3233 // "echon" will overwrite the message, unavoidably.
3234 msg_col = 0;
3235 else
3236 // When working silently: Put it back where it was, since nothing
3237 // should have been written.
3238 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003239}
3240
3241/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003242 * "execute()" function
3243 */
3244 static void
3245f_execute(typval_T *argvars, typval_T *rettv)
3246{
3247 execute_common(argvars, rettv, 0);
3248}
3249
3250/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003251 * "exists()" function
3252 */
3253 static void
3254f_exists(typval_T *argvars, typval_T *rettv)
3255{
3256 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003257 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003258
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003259 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003260 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003261 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003262 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003263 if (mch_getenv(p + 1) != NULL)
3264 n = TRUE;
3265 else
3266 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003267 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003268 p = expand_env_save(p);
3269 if (p != NULL && *p != '$')
3270 n = TRUE;
3271 vim_free(p);
3272 }
3273 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003274 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003275 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003276 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003277 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003278 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003279 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003280 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003281 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003282 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003283 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003284 else if (*p == '?') // internal function only
3285 {
3286 n = has_internal_func_name(p + 1);
3287 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003288 else if (*p == ':')
3289 {
3290 n = cmd_exists(p + 1);
3291 }
3292 else if (*p == '#')
3293 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003294 if (p[1] == '#')
3295 n = autocmd_supported(p + 2);
3296 else
3297 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003298 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003299 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003300 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003301 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003302 }
3303
3304 rettv->vval.v_number = n;
3305}
3306
3307#ifdef FEAT_FLOAT
3308/*
3309 * "exp()" function
3310 */
3311 static void
3312f_exp(typval_T *argvars, typval_T *rettv)
3313{
3314 float_T f = 0.0;
3315
3316 rettv->v_type = VAR_FLOAT;
3317 if (get_float_arg(argvars, &f) == OK)
3318 rettv->vval.v_float = exp(f);
3319 else
3320 rettv->vval.v_float = 0.0;
3321}
3322#endif
3323
3324/*
3325 * "expand()" function
3326 */
3327 static void
3328f_expand(typval_T *argvars, typval_T *rettv)
3329{
3330 char_u *s;
3331 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003332 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003333 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3334 expand_T xpc;
3335 int error = FALSE;
3336 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003337#ifdef BACKSLASH_IN_FILENAME
3338 char_u *p_csl_save = p_csl;
3339
3340 // avoid using 'completeslash' here
3341 p_csl = empty_option;
3342#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003343
3344 rettv->v_type = VAR_STRING;
3345 if (argvars[1].v_type != VAR_UNKNOWN
3346 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003347 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003348 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003349 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003350
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003351 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003352 if (*s == '%' || *s == '#' || *s == '<')
3353 {
3354 ++emsg_off;
3355 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3356 --emsg_off;
3357 if (rettv->v_type == VAR_LIST)
3358 {
3359 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3360 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003361 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003362 }
3363 else
3364 rettv->vval.v_string = result;
3365 }
3366 else
3367 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003368 // When the optional second argument is non-zero, don't remove matches
3369 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003370 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003371 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003372 options |= WILD_KEEP_ALL;
3373 if (!error)
3374 {
3375 ExpandInit(&xpc);
3376 xpc.xp_context = EXPAND_FILES;
3377 if (p_wic)
3378 options += WILD_ICASE;
3379 if (rettv->v_type == VAR_STRING)
3380 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3381 options, WILD_ALL);
3382 else if (rettv_list_alloc(rettv) != FAIL)
3383 {
3384 int i;
3385
3386 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3387 for (i = 0; i < xpc.xp_numfiles; i++)
3388 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3389 ExpandCleanup(&xpc);
3390 }
3391 }
3392 else
3393 rettv->vval.v_string = NULL;
3394 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003395#ifdef BACKSLASH_IN_FILENAME
3396 p_csl = p_csl_save;
3397#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003398}
3399
3400/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003401 * "expandcmd()" function
3402 * Expand all the special characters in a command string.
3403 */
3404 static void
3405f_expandcmd(typval_T *argvars, typval_T *rettv)
3406{
3407 exarg_T eap;
3408 char_u *cmdstr;
3409 char *errormsg = NULL;
3410
3411 rettv->v_type = VAR_STRING;
3412 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3413
3414 memset(&eap, 0, sizeof(eap));
3415 eap.cmd = cmdstr;
3416 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003417 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003418 eap.usefilter = FALSE;
3419 eap.nextcmd = NULL;
3420 eap.cmdidx = CMD_USER;
3421
3422 expand_filename(&eap, &cmdstr, &errormsg);
3423 if (errormsg != NULL && *errormsg != NUL)
3424 emsg(errormsg);
3425
3426 rettv->vval.v_string = cmdstr;
3427}
3428
3429/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003430 * "feedkeys()" function
3431 */
3432 static void
3433f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3434{
3435 int remap = TRUE;
3436 int insert = FALSE;
3437 char_u *keys, *flags;
3438 char_u nbuf[NUMBUFLEN];
3439 int typed = FALSE;
3440 int execute = FALSE;
3441 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003442 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003443 char_u *keys_esc;
3444
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003445 // This is not allowed in the sandbox. If the commands would still be
3446 // executed in the sandbox it would be OK, but it probably happens later,
3447 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003448 if (check_secure())
3449 return;
3450
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003451 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003452
3453 if (argvars[1].v_type != VAR_UNKNOWN)
3454 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003455 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003456 for ( ; *flags != NUL; ++flags)
3457 {
3458 switch (*flags)
3459 {
3460 case 'n': remap = FALSE; break;
3461 case 'm': remap = TRUE; break;
3462 case 't': typed = TRUE; break;
3463 case 'i': insert = TRUE; break;
3464 case 'x': execute = TRUE; break;
3465 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003466 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003467 }
3468 }
3469 }
3470
3471 if (*keys != NUL || execute)
3472 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003473 // Need to escape K_SPECIAL and CSI before putting the string in the
3474 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003475 keys_esc = vim_strsave_escape_csi(keys);
3476 if (keys_esc != NULL)
3477 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003478 if (lowlevel)
3479 {
3480#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003481 int idx;
3482 int len = (int)STRLEN(keys);
3483
3484 for (idx = 0; idx < len; ++idx)
3485 {
3486 // if a CTRL-C was typed, set got_int, similar to what
3487 // happens in fill_input_buf()
3488 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3489 got_int = TRUE;
3490 add_to_input_buf(keys + idx, 1);
3491 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003492#else
3493 emsg(_("E980: lowlevel input not supported"));
3494#endif
3495 }
3496 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003497 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003498 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003499 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003500 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003501#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003502 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003503#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003504 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003505 typebuf_was_filled = TRUE;
3506 }
3507 vim_free(keys_esc);
3508
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003509 if (execute)
3510 {
3511 int save_msg_scroll = msg_scroll;
3512
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003513 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003514 msg_scroll = FALSE;
3515
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003516 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003517 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003518 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003519 ++in_feedkeys;
3520 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003521 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003522 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003523 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003524 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003525 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003526 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003527
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003528 msg_scroll |= save_msg_scroll;
3529 }
3530 }
3531 }
3532}
3533
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003534#ifdef FEAT_FLOAT
3535/*
3536 * "float2nr({float})" function
3537 */
3538 static void
3539f_float2nr(typval_T *argvars, typval_T *rettv)
3540{
3541 float_T f = 0.0;
3542
3543 if (get_float_arg(argvars, &f) == OK)
3544 {
Bram Moolenaar37184272020-05-23 19:30:05 +02003545 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003546 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02003547 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003548 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003549 else
3550 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003551 }
3552}
3553
3554/*
3555 * "floor({float})" function
3556 */
3557 static void
3558f_floor(typval_T *argvars, typval_T *rettv)
3559{
3560 float_T f = 0.0;
3561
3562 rettv->v_type = VAR_FLOAT;
3563 if (get_float_arg(argvars, &f) == OK)
3564 rettv->vval.v_float = floor(f);
3565 else
3566 rettv->vval.v_float = 0.0;
3567}
3568
3569/*
3570 * "fmod()" function
3571 */
3572 static void
3573f_fmod(typval_T *argvars, typval_T *rettv)
3574{
3575 float_T fx = 0.0, fy = 0.0;
3576
3577 rettv->v_type = VAR_FLOAT;
3578 if (get_float_arg(argvars, &fx) == OK
3579 && get_float_arg(&argvars[1], &fy) == OK)
3580 rettv->vval.v_float = fmod(fx, fy);
3581 else
3582 rettv->vval.v_float = 0.0;
3583}
3584#endif
3585
3586/*
3587 * "fnameescape({string})" function
3588 */
3589 static void
3590f_fnameescape(typval_T *argvars, typval_T *rettv)
3591{
3592 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003593 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003594 rettv->v_type = VAR_STRING;
3595}
3596
3597/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003598 * "foreground()" function
3599 */
3600 static void
3601f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3602{
3603#ifdef FEAT_GUI
3604 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003605 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003606 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003607 return;
3608 }
3609#endif
3610#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003611 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003612#endif
3613}
3614
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003615 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003616common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003617{
3618 char_u *s;
3619 char_u *name;
3620 int use_string = FALSE;
3621 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003622 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003623 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003624
3625 if (argvars[0].v_type == VAR_FUNC)
3626 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003627 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003628 s = argvars[0].vval.v_string;
3629 }
3630 else if (argvars[0].v_type == VAR_PARTIAL
3631 && argvars[0].vval.v_partial != NULL)
3632 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003633 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003634 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003635 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003636 }
3637 else
3638 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003639 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003640 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003641 use_string = TRUE;
3642 }
3643
Bram Moolenaar843b8842016-08-21 14:36:15 +02003644 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003645 {
3646 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003647 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003648 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3649 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003650 if (*name != NUL)
3651 s = NULL;
3652 }
3653
Bram Moolenaar843b8842016-08-21 14:36:15 +02003654 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3655 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003656 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003657 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003658 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003659 ? find_func(trans_name, is_global, NULL) == NULL
3660 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003661 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003662 else
3663 {
3664 int dict_idx = 0;
3665 int arg_idx = 0;
3666 list_T *list = NULL;
3667
3668 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3669 {
3670 char sid_buf[25];
3671 int off = *s == 's' ? 2 : 5;
3672
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003673 // Expand s: and <SID> into <SNR>nr_, so that the function can
3674 // also be called from another script. Using trans_function_name()
3675 // would also work, but some plugins depend on the name being
3676 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003677 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003678 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003679 if (name != NULL)
3680 {
3681 STRCPY(name, sid_buf);
3682 STRCAT(name, s + off);
3683 }
3684 }
3685 else
3686 name = vim_strsave(s);
3687
3688 if (argvars[1].v_type != VAR_UNKNOWN)
3689 {
3690 if (argvars[2].v_type != VAR_UNKNOWN)
3691 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003692 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003693 arg_idx = 1;
3694 dict_idx = 2;
3695 }
3696 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003697 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003698 dict_idx = 1;
3699 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003700 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003701 arg_idx = 1;
3702 if (dict_idx > 0)
3703 {
3704 if (argvars[dict_idx].v_type != VAR_DICT)
3705 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003706 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003707 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003708 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003709 }
3710 if (argvars[dict_idx].vval.v_dict == NULL)
3711 dict_idx = 0;
3712 }
3713 if (arg_idx > 0)
3714 {
3715 if (argvars[arg_idx].v_type != VAR_LIST)
3716 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003717 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003718 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003719 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003720 }
3721 list = argvars[arg_idx].vval.v_list;
3722 if (list == NULL || list->lv_len == 0)
3723 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003724 else if (list->lv_len > MAX_FUNC_ARGS)
3725 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003726 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003727 vim_free(name);
3728 goto theend;
3729 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003730 }
3731 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003732 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003733 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003734 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003735
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003736 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003737 if (pt == NULL)
3738 vim_free(name);
3739 else
3740 {
3741 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3742 {
3743 listitem_T *li;
3744 int i = 0;
3745 int arg_len = 0;
3746 int lv_len = 0;
3747
3748 if (arg_pt != NULL)
3749 arg_len = arg_pt->pt_argc;
3750 if (list != NULL)
3751 lv_len = list->lv_len;
3752 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003753 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003754 if (pt->pt_argv == NULL)
3755 {
3756 vim_free(pt);
3757 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003758 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003759 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003760 for (i = 0; i < arg_len; i++)
3761 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3762 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003763 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003764 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003765 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003766 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003767 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003768 }
3769
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003770 // For "function(dict.func, [], dict)" and "func" is a partial
3771 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003772 if (dict_idx > 0)
3773 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003774 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003775 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3776 ++pt->pt_dict->dv_refcount;
3777 }
3778 else if (arg_pt != NULL)
3779 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003780 // If the dict was bound automatically the result is also
3781 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003782 pt->pt_dict = arg_pt->pt_dict;
3783 pt->pt_auto = arg_pt->pt_auto;
3784 if (pt->pt_dict != NULL)
3785 ++pt->pt_dict->dv_refcount;
3786 }
3787
3788 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003789 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3790 {
3791 pt->pt_func = arg_pt->pt_func;
3792 func_ptr_ref(pt->pt_func);
3793 vim_free(name);
3794 }
3795 else if (is_funcref)
3796 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003797 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003798 func_ptr_ref(pt->pt_func);
3799 vim_free(name);
3800 }
3801 else
3802 {
3803 pt->pt_name = name;
3804 func_ref(name);
3805 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003806 }
3807 rettv->v_type = VAR_PARTIAL;
3808 rettv->vval.v_partial = pt;
3809 }
3810 else
3811 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003812 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003813 rettv->v_type = VAR_FUNC;
3814 rettv->vval.v_string = name;
3815 func_ref(name);
3816 }
3817 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003818theend:
3819 vim_free(trans_name);
3820}
3821
3822/*
3823 * "funcref()" function
3824 */
3825 static void
3826f_funcref(typval_T *argvars, typval_T *rettv)
3827{
3828 common_function(argvars, rettv, TRUE);
3829}
3830
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003831 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003832ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003833{
3834 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3835 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003836 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003837}
3838
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003839/*
3840 * "function()" function
3841 */
3842 static void
3843f_function(typval_T *argvars, typval_T *rettv)
3844{
3845 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003846}
3847
3848/*
3849 * "garbagecollect()" function
3850 */
3851 static void
3852f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3853{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003854 // This is postponed until we are back at the toplevel, because we may be
3855 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003856 want_garbage_collect = TRUE;
3857
Bram Moolenaar2df47312020-09-05 17:30:44 +02003858 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003859 garbage_collect_at_exit = TRUE;
3860}
3861
3862/*
3863 * "get()" function
3864 */
3865 static void
3866f_get(typval_T *argvars, typval_T *rettv)
3867{
3868 listitem_T *li;
3869 list_T *l;
3870 dictitem_T *di;
3871 dict_T *d;
3872 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003873 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003874
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003875 if (argvars[0].v_type == VAR_BLOB)
3876 {
3877 int error = FALSE;
3878 int idx = tv_get_number_chk(&argvars[1], &error);
3879
3880 if (!error)
3881 {
3882 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003883 if (idx < 0)
3884 idx = blob_len(argvars[0].vval.v_blob) + idx;
3885 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3886 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003887 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003888 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003889 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003890 tv = rettv;
3891 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003892 }
3893 }
3894 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003895 {
3896 if ((l = argvars[0].vval.v_list) != NULL)
3897 {
3898 int error = FALSE;
3899
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003900 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003901 if (!error && li != NULL)
3902 tv = &li->li_tv;
3903 }
3904 }
3905 else if (argvars[0].v_type == VAR_DICT)
3906 {
3907 if ((d = argvars[0].vval.v_dict) != NULL)
3908 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003909 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003910 if (di != NULL)
3911 tv = &di->di_tv;
3912 }
3913 }
3914 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3915 {
3916 partial_T *pt;
3917 partial_T fref_pt;
3918
3919 if (argvars[0].v_type == VAR_PARTIAL)
3920 pt = argvars[0].vval.v_partial;
3921 else
3922 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003923 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003924 fref_pt.pt_name = argvars[0].vval.v_string;
3925 pt = &fref_pt;
3926 }
3927
3928 if (pt != NULL)
3929 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003930 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003931 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003932
3933 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3934 {
3935 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003936 n = partial_name(pt);
3937 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003938 rettv->vval.v_string = NULL;
3939 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003940 {
3941 rettv->vval.v_string = vim_strsave(n);
3942 if (rettv->v_type == VAR_FUNC)
3943 func_ref(rettv->vval.v_string);
3944 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003945 }
3946 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003947 {
3948 what_is_dict = TRUE;
3949 if (pt->pt_dict != NULL)
3950 rettv_dict_set(rettv, pt->pt_dict);
3951 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003952 else if (STRCMP(what, "args") == 0)
3953 {
3954 rettv->v_type = VAR_LIST;
3955 if (rettv_list_alloc(rettv) == OK)
3956 {
3957 int i;
3958
3959 for (i = 0; i < pt->pt_argc; ++i)
3960 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3961 }
3962 }
3963 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003964 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003965
3966 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3967 // third argument
3968 if (!what_is_dict)
3969 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003970 }
3971 }
3972 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003973 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003974
3975 if (tv == NULL)
3976 {
3977 if (argvars[2].v_type != VAR_UNKNOWN)
3978 copy_tv(&argvars[2], rettv);
3979 }
3980 else
3981 copy_tv(tv, rettv);
3982}
3983
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003984/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003985 * "getchangelist()" function
3986 */
3987 static void
3988f_getchangelist(typval_T *argvars, typval_T *rettv)
3989{
3990#ifdef FEAT_JUMPLIST
3991 buf_T *buf;
3992 int i;
3993 list_T *l;
3994 dict_T *d;
3995#endif
3996
3997 if (rettv_list_alloc(rettv) != OK)
3998 return;
3999
4000#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02004001 if (argvars[0].v_type == VAR_UNKNOWN)
4002 buf = curbuf;
4003 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02004004 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004005 if (buf == NULL)
4006 return;
4007
4008 l = list_alloc();
4009 if (l == NULL)
4010 return;
4011
4012 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4013 return;
4014 /*
4015 * The current window change list index tracks only the position in the
4016 * current buffer change list. For other buffers, use the change list
4017 * length as the current index.
4018 */
4019 list_append_number(rettv->vval.v_list,
4020 (varnumber_T)((buf == curwin->w_buffer)
4021 ? curwin->w_changelistidx : buf->b_changelistlen));
4022
4023 for (i = 0; i < buf->b_changelistlen; ++i)
4024 {
4025 if (buf->b_changelist[i].lnum == 0)
4026 continue;
4027 if ((d = dict_alloc()) == NULL)
4028 return;
4029 if (list_append_dict(l, d) == FAIL)
4030 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004031 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4032 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004033 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004034 }
4035#endif
4036}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004037
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004038 static void
4039getpos_both(
4040 typval_T *argvars,
4041 typval_T *rettv,
4042 int getcurpos,
4043 int charcol)
4044{
4045 pos_T *fp = NULL;
4046 pos_T pos;
4047 win_T *wp = curwin;
4048 list_T *l;
4049 int fnum = -1;
4050
4051 if (rettv_list_alloc(rettv) == OK)
4052 {
4053 l = rettv->vval.v_list;
4054 if (getcurpos)
4055 {
4056 if (argvars[0].v_type != VAR_UNKNOWN)
4057 {
4058 wp = find_win_by_nr_or_id(&argvars[0]);
4059 if (wp != NULL)
4060 fp = &wp->w_cursor;
4061 }
4062 else
4063 fp = &curwin->w_cursor;
4064 if (fp != NULL && charcol)
4065 {
4066 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01004067 pos.col =
4068 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004069 fp = &pos;
4070 }
4071 }
4072 else
4073 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
4074 if (fnum != -1)
4075 list_append_number(l, (varnumber_T)fnum);
4076 else
4077 list_append_number(l, (varnumber_T)0);
4078 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4079 : (varnumber_T)0);
4080 list_append_number(l, (fp != NULL)
4081 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4082 : (varnumber_T)0);
4083 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
4084 (varnumber_T)0);
4085 if (getcurpos)
4086 {
4087 int save_set_curswant = curwin->w_set_curswant;
4088 colnr_T save_curswant = curwin->w_curswant;
4089 colnr_T save_virtcol = curwin->w_virtcol;
4090
4091 if (wp == curwin)
4092 update_curswant();
4093 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
4094 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
4095
4096 // Do not change "curswant", as it is unexpected that a get
4097 // function has a side effect.
4098 if (wp == curwin && save_set_curswant)
4099 {
4100 curwin->w_set_curswant = save_set_curswant;
4101 curwin->w_curswant = save_curswant;
4102 curwin->w_virtcol = save_virtcol;
4103 curwin->w_valid &= ~VALID_VIRTCOL;
4104 }
4105 }
4106 }
4107 else
4108 rettv->vval.v_number = FALSE;
4109}
4110
4111/*
4112 * "getcharpos()" function
4113 */
4114 static void
4115f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
4116{
4117 getpos_both(argvars, rettv, FALSE, TRUE);
4118}
4119
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004120/*
4121 * "getcharsearch()" function
4122 */
4123 static void
4124f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4125{
4126 if (rettv_dict_alloc(rettv) != FAIL)
4127 {
4128 dict_T *dict = rettv->vval.v_dict;
4129
Bram Moolenaare0be1672018-07-08 16:50:37 +02004130 dict_add_string(dict, "char", last_csearch());
4131 dict_add_number(dict, "forward", last_csearch_forward());
4132 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004133 }
4134}
4135
4136/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004137 * "getenv()" function
4138 */
4139 static void
4140f_getenv(typval_T *argvars, typval_T *rettv)
4141{
4142 int mustfree = FALSE;
4143 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4144
4145 if (p == NULL)
4146 {
4147 rettv->v_type = VAR_SPECIAL;
4148 rettv->vval.v_number = VVAL_NULL;
4149 return;
4150 }
4151 if (!mustfree)
4152 p = vim_strsave(p);
4153 rettv->vval.v_string = p;
4154 rettv->v_type = VAR_STRING;
4155}
4156
4157/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004158 * "getfontname()" function
4159 */
4160 static void
4161f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4162{
4163 rettv->v_type = VAR_STRING;
4164 rettv->vval.v_string = NULL;
4165#ifdef FEAT_GUI
4166 if (gui.in_use)
4167 {
4168 GuiFont font;
4169 char_u *name = NULL;
4170
4171 if (argvars[0].v_type == VAR_UNKNOWN)
4172 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004173 // Get the "Normal" font. Either the name saved by
4174 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004175 font = gui.norm_font;
4176 name = hl_get_font_name();
4177 }
4178 else
4179 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004180 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004181 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004182 return;
4183 font = gui_mch_get_font(name, FALSE);
4184 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004185 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004186 }
4187 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4188 if (argvars[0].v_type != VAR_UNKNOWN)
4189 gui_mch_free_font(font);
4190 }
4191#endif
4192}
4193
4194/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004195 * "getjumplist()" function
4196 */
4197 static void
4198f_getjumplist(typval_T *argvars, typval_T *rettv)
4199{
4200#ifdef FEAT_JUMPLIST
4201 win_T *wp;
4202 int i;
4203 list_T *l;
4204 dict_T *d;
4205#endif
4206
4207 if (rettv_list_alloc(rettv) != OK)
4208 return;
4209
4210#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004211 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004212 if (wp == NULL)
4213 return;
4214
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004215 cleanup_jumplist(wp, TRUE);
4216
Bram Moolenaar4f505882018-02-10 21:06:32 +01004217 l = list_alloc();
4218 if (l == NULL)
4219 return;
4220
4221 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4222 return;
4223 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4224
4225 for (i = 0; i < wp->w_jumplistlen; ++i)
4226 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004227 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4228 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004229 if ((d = dict_alloc()) == NULL)
4230 return;
4231 if (list_append_dict(l, d) == FAIL)
4232 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004233 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4234 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004235 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004236 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004237 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004238 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004239 }
4240#endif
4241}
4242
4243/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004244 * "getpid()" function
4245 */
4246 static void
4247f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4248{
4249 rettv->vval.v_number = mch_get_pid();
4250}
4251
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004252/*
4253 * "getcurpos()" function
4254 */
4255 static void
4256f_getcurpos(typval_T *argvars, typval_T *rettv)
4257{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004258 getpos_both(argvars, rettv, TRUE, FALSE);
4259}
4260
4261 static void
4262f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4263{
4264 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004265}
4266
4267/*
4268 * "getpos(string)" function
4269 */
4270 static void
4271f_getpos(typval_T *argvars, typval_T *rettv)
4272{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004273 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004274}
4275
4276/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004277 * "getreg()" function
4278 */
4279 static void
4280f_getreg(typval_T *argvars, typval_T *rettv)
4281{
4282 char_u *strregname;
4283 int regname;
4284 int arg2 = FALSE;
4285 int return_list = FALSE;
4286 int error = FALSE;
4287
4288 if (argvars[0].v_type != VAR_UNKNOWN)
4289 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004290 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004291 if (strregname == NULL)
4292 error = TRUE;
4293 else if (in_vim9script() && STRLEN(strregname) > 1)
4294 {
4295 semsg(_(e_register_name_must_be_one_char_str), strregname);
4296 error = TRUE;
4297 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004298 if (argvars[1].v_type != VAR_UNKNOWN)
4299 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004300 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004301 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004302 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004303 }
4304 }
4305 else
4306 strregname = get_vim_var_str(VV_REG);
4307
4308 if (error)
4309 return;
4310
4311 regname = (strregname == NULL ? '"' : *strregname);
4312 if (regname == 0)
4313 regname = '"';
4314
4315 if (return_list)
4316 {
4317 rettv->v_type = VAR_LIST;
4318 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4319 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4320 if (rettv->vval.v_list == NULL)
4321 (void)rettv_list_alloc(rettv);
4322 else
4323 ++rettv->vval.v_list->lv_refcount;
4324 }
4325 else
4326 {
4327 rettv->v_type = VAR_STRING;
4328 rettv->vval.v_string = get_reg_contents(regname,
4329 arg2 ? GREG_EXPR_SRC : 0);
4330 }
4331}
4332
4333/*
4334 * "getregtype()" function
4335 */
4336 static void
4337f_getregtype(typval_T *argvars, typval_T *rettv)
4338{
4339 char_u *strregname;
4340 int regname;
4341 char_u buf[NUMBUFLEN + 2];
4342 long reglen = 0;
4343
4344 if (argvars[0].v_type != VAR_UNKNOWN)
4345 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004346 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004347 if (strregname != NULL && in_vim9script() && STRLEN(strregname) > 1)
4348 {
4349 semsg(_(e_register_name_must_be_one_char_str), strregname);
4350 strregname = NULL;
4351 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004352 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004353 {
4354 rettv->v_type = VAR_STRING;
4355 rettv->vval.v_string = NULL;
4356 return;
4357 }
4358 }
4359 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004360 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004361 strregname = get_vim_var_str(VV_REG);
4362
4363 regname = (strregname == NULL ? '"' : *strregname);
4364 if (regname == 0)
4365 regname = '"';
4366
4367 buf[0] = NUL;
4368 buf[1] = NUL;
4369 switch (get_reg_type(regname, &reglen))
4370 {
4371 case MLINE: buf[0] = 'V'; break;
4372 case MCHAR: buf[0] = 'v'; break;
4373 case MBLOCK:
4374 buf[0] = Ctrl_V;
4375 sprintf((char *)buf + 1, "%ld", reglen + 1);
4376 break;
4377 }
4378 rettv->v_type = VAR_STRING;
4379 rettv->vval.v_string = vim_strsave(buf);
4380}
4381
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004382/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004383 * "gettagstack()" function
4384 */
4385 static void
4386f_gettagstack(typval_T *argvars, typval_T *rettv)
4387{
4388 win_T *wp = curwin; // default is current window
4389
4390 if (rettv_dict_alloc(rettv) != OK)
4391 return;
4392
4393 if (argvars[0].v_type != VAR_UNKNOWN)
4394 {
4395 wp = find_win_by_nr_or_id(&argvars[0]);
4396 if (wp == NULL)
4397 return;
4398 }
4399
4400 get_tagstack(wp, rettv->vval.v_dict);
4401}
4402
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004403/*
4404 * "gettext()" function
4405 */
4406 static void
4407f_gettext(typval_T *argvars, typval_T *rettv)
4408{
4409 if (argvars[0].v_type != VAR_STRING
4410 || argvars[0].vval.v_string == NULL
4411 || *argvars[0].vval.v_string == NUL)
4412 {
4413 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4414 }
4415 else
4416 {
4417 rettv->v_type = VAR_STRING;
4418 rettv->vval.v_string = vim_strsave(
4419 (char_u *)_(argvars[0].vval.v_string));
4420 }
4421}
4422
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004423// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004424#include "version.h"
4425
4426/*
4427 * "has()" function
4428 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004429 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004430f_has(typval_T *argvars, typval_T *rettv)
4431{
4432 int i;
4433 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004434 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004435 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004436 typedef struct {
4437 char *name;
4438 short present;
4439 } has_item_T;
4440 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004441 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004442 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004443#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004444 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004445#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004446 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004447#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004448 },
4449 {"arp",
4450#if defined(AMIGA) && defined(FEAT_ARP)
4451 1
4452#else
4453 0
4454#endif
4455 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004456 {"haiku",
4457#ifdef __HAIKU__
4458 1
4459#else
4460 0
4461#endif
4462 },
4463 {"bsd",
4464#if defined(BSD) && !defined(MACOS_X)
4465 1
4466#else
4467 0
4468#endif
4469 },
4470 {"hpux",
4471#ifdef hpux
4472 1
4473#else
4474 0
4475#endif
4476 },
4477 {"linux",
4478#ifdef __linux__
4479 1
4480#else
4481 0
4482#endif
4483 },
4484 {"mac", // Mac OS X (and, once, Mac OS Classic)
4485#ifdef MACOS_X
4486 1
4487#else
4488 0
4489#endif
4490 },
4491 {"osx", // Mac OS X
4492#ifdef MACOS_X
4493 1
4494#else
4495 0
4496#endif
4497 },
4498 {"macunix", // Mac OS X, with the darwin feature
4499#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4500 1
4501#else
4502 0
4503#endif
4504 },
4505 {"osxdarwin", // synonym for macunix
4506#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4507 1
4508#else
4509 0
4510#endif
4511 },
4512 {"qnx",
4513#ifdef __QNX__
4514 1
4515#else
4516 0
4517#endif
4518 },
4519 {"sun",
4520#ifdef SUN_SYSTEM
4521 1
4522#else
4523 0
4524#endif
4525 },
4526 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004527#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004528 1
4529#else
4530 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004531#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004532 },
4533 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004534#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004535 1
4536#else
4537 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004538#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004539 },
4540 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004541#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004542 1
4543#else
4544 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004545#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004546 },
4547 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004548#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004549 1
4550#else
4551 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004552#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004553 },
4554 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004555#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004556 1
4557#else
4558 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004559#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004560 },
4561 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004562#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004563 1
4564#else
4565 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004566#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004567 },
4568 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004569#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004570 1
4571#else
4572 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004573#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004574 },
4575 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004576#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004577 1
4578#else
4579 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004580#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004581 },
4582 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004583#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004584 1
4585#else
4586 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004587#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004588 },
4589 {"autocmd", 1},
4590 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004591#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004592 1
4593#else
4594 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004595#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004596 },
4597 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004598#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004599 1
4600#else
4601 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004602#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004603 },
4604 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004605#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004606 1
4607#else
4608 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004609#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004610 },
4611 {"balloon_multiline",
4612#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4613 // MS-Windows requires runtime check, see below
4614 1
4615#else
4616 0
4617#endif
4618 },
4619 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004620#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004621 1
4622#else
4623 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004624#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004625 },
4626 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004627#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004628 1
4629#else
4630 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004631#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004632 },
4633 {"all_builtin_terms",
4634#if defined(ALL_BUILTIN_TCAPS)
4635 1
4636#else
4637 0
4638#endif
4639 },
4640 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004641#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004642 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004643 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004644 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004645#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004646 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004647#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004648 },
4649 {"byte_offset",
4650#ifdef FEAT_BYTEOFF
4651 1
4652#else
4653 0
4654#endif
4655 },
4656 {"channel",
4657#ifdef FEAT_JOB_CHANNEL
4658 1
4659#else
4660 0
4661#endif
4662 },
4663 {"cindent",
4664#ifdef FEAT_CINDENT
4665 1
4666#else
4667 0
4668#endif
4669 },
4670 {"clientserver",
4671#ifdef FEAT_CLIENTSERVER
4672 1
4673#else
4674 0
4675#endif
4676 },
4677 {"clipboard",
4678#ifdef FEAT_CLIPBOARD
4679 1
4680#else
4681 0
4682#endif
4683 },
4684 {"cmdline_compl", 1},
4685 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004686 {"cmdwin",
4687#ifdef FEAT_CMDWIN
4688 1
4689#else
4690 0
4691#endif
4692 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004693 {"comments", 1},
4694 {"conceal",
4695#ifdef FEAT_CONCEAL
4696 1
4697#else
4698 0
4699#endif
4700 },
4701 {"cryptv",
4702#ifdef FEAT_CRYPT
4703 1
4704#else
4705 0
4706#endif
4707 },
4708 {"crypt-blowfish",
4709#ifdef FEAT_CRYPT
4710 1
4711#else
4712 0
4713#endif
4714 },
4715 {"crypt-blowfish2",
4716#ifdef FEAT_CRYPT
4717 1
4718#else
4719 0
4720#endif
4721 },
4722 {"cscope",
4723#ifdef FEAT_CSCOPE
4724 1
4725#else
4726 0
4727#endif
4728 },
4729 {"cursorbind", 1},
4730 {"cursorshape",
4731#ifdef CURSOR_SHAPE
4732 1
4733#else
4734 0
4735#endif
4736 },
4737 {"debug",
4738#ifdef DEBUG
4739 1
4740#else
4741 0
4742#endif
4743 },
4744 {"dialog_con",
4745#ifdef FEAT_CON_DIALOG
4746 1
4747#else
4748 0
4749#endif
4750 },
4751 {"dialog_gui",
4752#ifdef FEAT_GUI_DIALOG
4753 1
4754#else
4755 0
4756#endif
4757 },
4758 {"diff",
4759#ifdef FEAT_DIFF
4760 1
4761#else
4762 0
4763#endif
4764 },
4765 {"digraphs",
4766#ifdef FEAT_DIGRAPHS
4767 1
4768#else
4769 0
4770#endif
4771 },
4772 {"directx",
4773#ifdef FEAT_DIRECTX
4774 1
4775#else
4776 0
4777#endif
4778 },
4779 {"dnd",
4780#ifdef FEAT_DND
4781 1
4782#else
4783 0
4784#endif
4785 },
4786 {"emacs_tags",
4787#ifdef FEAT_EMACS_TAGS
4788 1
4789#else
4790 0
4791#endif
4792 },
4793 {"eval", 1}, // always present, of course!
4794 {"ex_extra", 1}, // graduated feature
4795 {"extra_search",
4796#ifdef FEAT_SEARCH_EXTRA
4797 1
4798#else
4799 0
4800#endif
4801 },
4802 {"file_in_path",
4803#ifdef FEAT_SEARCHPATH
4804 1
4805#else
4806 0
4807#endif
4808 },
4809 {"filterpipe",
4810#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4811 1
4812#else
4813 0
4814#endif
4815 },
4816 {"find_in_path",
4817#ifdef FEAT_FIND_ID
4818 1
4819#else
4820 0
4821#endif
4822 },
4823 {"float",
4824#ifdef FEAT_FLOAT
4825 1
4826#else
4827 0
4828#endif
4829 },
4830 {"folding",
4831#ifdef FEAT_FOLDING
4832 1
4833#else
4834 0
4835#endif
4836 },
4837 {"footer",
4838#ifdef FEAT_FOOTER
4839 1
4840#else
4841 0
4842#endif
4843 },
4844 {"fork",
4845#if !defined(USE_SYSTEM) && defined(UNIX)
4846 1
4847#else
4848 0
4849#endif
4850 },
4851 {"gettext",
4852#ifdef FEAT_GETTEXT
4853 1
4854#else
4855 0
4856#endif
4857 },
4858 {"gui",
4859#ifdef FEAT_GUI
4860 1
4861#else
4862 0
4863#endif
4864 },
4865 {"gui_neXtaw",
4866#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4867 1
4868#else
4869 0
4870#endif
4871 },
4872 {"gui_athena",
4873#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4874 1
4875#else
4876 0
4877#endif
4878 },
4879 {"gui_gtk",
4880#ifdef FEAT_GUI_GTK
4881 1
4882#else
4883 0
4884#endif
4885 },
4886 {"gui_gtk2",
4887#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4888 1
4889#else
4890 0
4891#endif
4892 },
4893 {"gui_gtk3",
4894#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4895 1
4896#else
4897 0
4898#endif
4899 },
4900 {"gui_gnome",
4901#ifdef FEAT_GUI_GNOME
4902 1
4903#else
4904 0
4905#endif
4906 },
4907 {"gui_haiku",
4908#ifdef FEAT_GUI_HAIKU
4909 1
4910#else
4911 0
4912#endif
4913 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004914 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004915 {"gui_motif",
4916#ifdef FEAT_GUI_MOTIF
4917 1
4918#else
4919 0
4920#endif
4921 },
4922 {"gui_photon",
4923#ifdef FEAT_GUI_PHOTON
4924 1
4925#else
4926 0
4927#endif
4928 },
4929 {"gui_win32",
4930#ifdef FEAT_GUI_MSWIN
4931 1
4932#else
4933 0
4934#endif
4935 },
4936 {"iconv",
4937#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4938 1
4939#else
4940 0
4941#endif
4942 },
4943 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004944 {"ipv6",
4945#ifdef FEAT_IPV6
4946 1
4947#else
4948 0
4949#endif
4950 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004951 {"job",
4952#ifdef FEAT_JOB_CHANNEL
4953 1
4954#else
4955 0
4956#endif
4957 },
4958 {"jumplist",
4959#ifdef FEAT_JUMPLIST
4960 1
4961#else
4962 0
4963#endif
4964 },
4965 {"keymap",
4966#ifdef FEAT_KEYMAP
4967 1
4968#else
4969 0
4970#endif
4971 },
4972 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4973 {"langmap",
4974#ifdef FEAT_LANGMAP
4975 1
4976#else
4977 0
4978#endif
4979 },
4980 {"libcall",
4981#ifdef FEAT_LIBCALL
4982 1
4983#else
4984 0
4985#endif
4986 },
4987 {"linebreak",
4988#ifdef FEAT_LINEBREAK
4989 1
4990#else
4991 0
4992#endif
4993 },
4994 {"lispindent",
4995#ifdef FEAT_LISP
4996 1
4997#else
4998 0
4999#endif
5000 },
5001 {"listcmds", 1},
5002 {"localmap", 1},
5003 {"lua",
5004#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
5005 1
5006#else
5007 0
5008#endif
5009 },
5010 {"menu",
5011#ifdef FEAT_MENU
5012 1
5013#else
5014 0
5015#endif
5016 },
5017 {"mksession",
5018#ifdef FEAT_SESSION
5019 1
5020#else
5021 0
5022#endif
5023 },
5024 {"modify_fname", 1},
5025 {"mouse", 1},
5026 {"mouseshape",
5027#ifdef FEAT_MOUSESHAPE
5028 1
5029#else
5030 0
5031#endif
5032 },
5033 {"mouse_dec",
5034#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
5035 1
5036#else
5037 0
5038#endif
5039 },
5040 {"mouse_gpm",
5041#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
5042 1
5043#else
5044 0
5045#endif
5046 },
5047 {"mouse_jsbterm",
5048#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
5049 1
5050#else
5051 0
5052#endif
5053 },
5054 {"mouse_netterm",
5055#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
5056 1
5057#else
5058 0
5059#endif
5060 },
5061 {"mouse_pterm",
5062#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
5063 1
5064#else
5065 0
5066#endif
5067 },
5068 {"mouse_sgr",
5069#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5070 1
5071#else
5072 0
5073#endif
5074 },
5075 {"mouse_sysmouse",
5076#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
5077 1
5078#else
5079 0
5080#endif
5081 },
5082 {"mouse_urxvt",
5083#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
5084 1
5085#else
5086 0
5087#endif
5088 },
5089 {"mouse_xterm",
5090#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5091 1
5092#else
5093 0
5094#endif
5095 },
5096 {"multi_byte", 1},
5097 {"multi_byte_ime",
5098#ifdef FEAT_MBYTE_IME
5099 1
5100#else
5101 0
5102#endif
5103 },
5104 {"multi_lang",
5105#ifdef FEAT_MULTI_LANG
5106 1
5107#else
5108 0
5109#endif
5110 },
5111 {"mzscheme",
5112#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
5113 1
5114#else
5115 0
5116#endif
5117 },
5118 {"num64", 1},
5119 {"ole",
5120#ifdef FEAT_OLE
5121 1
5122#else
5123 0
5124#endif
5125 },
5126 {"packages",
5127#ifdef FEAT_EVAL
5128 1
5129#else
5130 0
5131#endif
5132 },
5133 {"path_extra",
5134#ifdef FEAT_PATH_EXTRA
5135 1
5136#else
5137 0
5138#endif
5139 },
5140 {"perl",
5141#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
5142 1
5143#else
5144 0
5145#endif
5146 },
5147 {"persistent_undo",
5148#ifdef FEAT_PERSISTENT_UNDO
5149 1
5150#else
5151 0
5152#endif
5153 },
5154 {"python_compiled",
5155#if defined(FEAT_PYTHON)
5156 1
5157#else
5158 0
5159#endif
5160 },
5161 {"python_dynamic",
5162#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
5163 1
5164#else
5165 0
5166#endif
5167 },
5168 {"python",
5169#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
5170 1
5171#else
5172 0
5173#endif
5174 },
5175 {"pythonx",
5176#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
5177 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
5178 1
5179#else
5180 0
5181#endif
5182 },
5183 {"python3_compiled",
5184#if defined(FEAT_PYTHON3)
5185 1
5186#else
5187 0
5188#endif
5189 },
5190 {"python3_dynamic",
5191#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
5192 1
5193#else
5194 0
5195#endif
5196 },
5197 {"python3",
5198#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
5199 1
5200#else
5201 0
5202#endif
5203 },
5204 {"popupwin",
5205#ifdef FEAT_PROP_POPUP
5206 1
5207#else
5208 0
5209#endif
5210 },
5211 {"postscript",
5212#ifdef FEAT_POSTSCRIPT
5213 1
5214#else
5215 0
5216#endif
5217 },
5218 {"printer",
5219#ifdef FEAT_PRINTER
5220 1
5221#else
5222 0
5223#endif
5224 },
5225 {"profile",
5226#ifdef FEAT_PROFILE
5227 1
5228#else
5229 0
5230#endif
5231 },
5232 {"reltime",
5233#ifdef FEAT_RELTIME
5234 1
5235#else
5236 0
5237#endif
5238 },
5239 {"quickfix",
5240#ifdef FEAT_QUICKFIX
5241 1
5242#else
5243 0
5244#endif
5245 },
5246 {"rightleft",
5247#ifdef FEAT_RIGHTLEFT
5248 1
5249#else
5250 0
5251#endif
5252 },
5253 {"ruby",
5254#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5255 1
5256#else
5257 0
5258#endif
5259 },
5260 {"scrollbind", 1},
5261 {"showcmd",
5262#ifdef FEAT_CMDL_INFO
5263 1
5264#else
5265 0
5266#endif
5267 },
5268 {"cmdline_info",
5269#ifdef FEAT_CMDL_INFO
5270 1
5271#else
5272 0
5273#endif
5274 },
5275 {"signs",
5276#ifdef FEAT_SIGNS
5277 1
5278#else
5279 0
5280#endif
5281 },
5282 {"smartindent",
5283#ifdef FEAT_SMARTINDENT
5284 1
5285#else
5286 0
5287#endif
5288 },
5289 {"startuptime",
5290#ifdef STARTUPTIME
5291 1
5292#else
5293 0
5294#endif
5295 },
5296 {"statusline",
5297#ifdef FEAT_STL_OPT
5298 1
5299#else
5300 0
5301#endif
5302 },
5303 {"netbeans_intg",
5304#ifdef FEAT_NETBEANS_INTG
5305 1
5306#else
5307 0
5308#endif
5309 },
5310 {"sound",
5311#ifdef FEAT_SOUND
5312 1
5313#else
5314 0
5315#endif
5316 },
5317 {"spell",
5318#ifdef FEAT_SPELL
5319 1
5320#else
5321 0
5322#endif
5323 },
5324 {"syntax",
5325#ifdef FEAT_SYN_HL
5326 1
5327#else
5328 0
5329#endif
5330 },
5331 {"system",
5332#if defined(USE_SYSTEM) || !defined(UNIX)
5333 1
5334#else
5335 0
5336#endif
5337 },
5338 {"tag_binary",
5339#ifdef FEAT_TAG_BINS
5340 1
5341#else
5342 0
5343#endif
5344 },
5345 {"tcl",
5346#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5347 1
5348#else
5349 0
5350#endif
5351 },
5352 {"termguicolors",
5353#ifdef FEAT_TERMGUICOLORS
5354 1
5355#else
5356 0
5357#endif
5358 },
5359 {"terminal",
5360#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5361 1
5362#else
5363 0
5364#endif
5365 },
5366 {"terminfo",
5367#ifdef TERMINFO
5368 1
5369#else
5370 0
5371#endif
5372 },
5373 {"termresponse",
5374#ifdef FEAT_TERMRESPONSE
5375 1
5376#else
5377 0
5378#endif
5379 },
5380 {"textobjects",
5381#ifdef FEAT_TEXTOBJ
5382 1
5383#else
5384 0
5385#endif
5386 },
5387 {"textprop",
5388#ifdef FEAT_PROP_POPUP
5389 1
5390#else
5391 0
5392#endif
5393 },
5394 {"tgetent",
5395#ifdef HAVE_TGETENT
5396 1
5397#else
5398 0
5399#endif
5400 },
5401 {"timers",
5402#ifdef FEAT_TIMERS
5403 1
5404#else
5405 0
5406#endif
5407 },
5408 {"title",
5409#ifdef FEAT_TITLE
5410 1
5411#else
5412 0
5413#endif
5414 },
5415 {"toolbar",
5416#ifdef FEAT_TOOLBAR
5417 1
5418#else
5419 0
5420#endif
5421 },
5422 {"unnamedplus",
5423#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5424 1
5425#else
5426 0
5427#endif
5428 },
5429 {"user-commands", 1}, // was accidentally included in 5.4
5430 {"user_commands", 1},
5431 {"vartabs",
5432#ifdef FEAT_VARTABS
5433 1
5434#else
5435 0
5436#endif
5437 },
5438 {"vertsplit", 1},
5439 {"viminfo",
5440#ifdef FEAT_VIMINFO
5441 1
5442#else
5443 0
5444#endif
5445 },
5446 {"vimscript-1", 1},
5447 {"vimscript-2", 1},
5448 {"vimscript-3", 1},
5449 {"vimscript-4", 1},
5450 {"virtualedit", 1},
5451 {"visual", 1},
5452 {"visualextra", 1},
5453 {"vreplace", 1},
5454 {"vtp",
5455#ifdef FEAT_VTP
5456 1
5457#else
5458 0
5459#endif
5460 },
5461 {"wildignore",
5462#ifdef FEAT_WILDIGN
5463 1
5464#else
5465 0
5466#endif
5467 },
5468 {"wildmenu",
5469#ifdef FEAT_WILDMENU
5470 1
5471#else
5472 0
5473#endif
5474 },
5475 {"windows", 1},
5476 {"winaltkeys",
5477#ifdef FEAT_WAK
5478 1
5479#else
5480 0
5481#endif
5482 },
5483 {"writebackup",
5484#ifdef FEAT_WRITEBACKUP
5485 1
5486#else
5487 0
5488#endif
5489 },
5490 {"xim",
5491#ifdef FEAT_XIM
5492 1
5493#else
5494 0
5495#endif
5496 },
5497 {"xfontset",
5498#ifdef FEAT_XFONTSET
5499 1
5500#else
5501 0
5502#endif
5503 },
5504 {"xpm",
5505#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5506 1
5507#else
5508 0
5509#endif
5510 },
5511 {"xpm_w32", // for backward compatibility
5512#ifdef FEAT_XPM_W32
5513 1
5514#else
5515 0
5516#endif
5517 },
5518 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005519#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005520 1
5521#else
5522 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005523#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005524 },
5525 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005526#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005527 1
5528#else
5529 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005530#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005531 },
5532 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005533#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005534 1
5535#else
5536 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005537#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005538 },
5539 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005540#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005541 1
5542#else
5543 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005544#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005545 },
5546 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005547#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005548 1
5549#else
5550 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005551#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005552 },
5553 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005554 };
5555
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005556 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005557 for (i = 0; has_list[i].name != NULL; ++i)
5558 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005559 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005560 x = TRUE;
5561 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005562 break;
5563 }
5564
Bram Moolenaar79296512020-03-22 16:17:14 +01005565 // features also in has_list[] but sometimes enabled at runtime
5566 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005567 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005568 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005569 {
5570 // intentionally empty
5571 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005572#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005573 else if (STRICMP(name, "balloon_multiline") == 0)
5574 n = multiline_balloon_available();
5575#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005576#ifdef VIMDLL
5577 else if (STRICMP(name, "filterpipe") == 0)
5578 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005579#endif
5580#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5581 else if (STRICMP(name, "iconv") == 0)
5582 n = iconv_enabled(FALSE);
5583#endif
5584#ifdef DYNAMIC_LUA
5585 else if (STRICMP(name, "lua") == 0)
5586 n = lua_enabled(FALSE);
5587#endif
5588#ifdef DYNAMIC_MZSCHEME
5589 else if (STRICMP(name, "mzscheme") == 0)
5590 n = mzscheme_enabled(FALSE);
5591#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005592#ifdef DYNAMIC_PERL
5593 else if (STRICMP(name, "perl") == 0)
5594 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005595#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005596#ifdef DYNAMIC_PYTHON
5597 else if (STRICMP(name, "python") == 0)
5598 n = python_enabled(FALSE);
5599#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005600#ifdef DYNAMIC_PYTHON3
5601 else if (STRICMP(name, "python3") == 0)
5602 n = python3_enabled(FALSE);
5603#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005604#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5605 else if (STRICMP(name, "pythonx") == 0)
5606 {
5607# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5608 if (p_pyx == 0)
5609 n = python3_enabled(FALSE) || python_enabled(FALSE);
5610 else if (p_pyx == 3)
5611 n = python3_enabled(FALSE);
5612 else if (p_pyx == 2)
5613 n = python_enabled(FALSE);
5614# elif defined(DYNAMIC_PYTHON)
5615 n = python_enabled(FALSE);
5616# elif defined(DYNAMIC_PYTHON3)
5617 n = python3_enabled(FALSE);
5618# endif
5619 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005620#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005621#ifdef DYNAMIC_RUBY
5622 else if (STRICMP(name, "ruby") == 0)
5623 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005624#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005625#ifdef DYNAMIC_TCL
5626 else if (STRICMP(name, "tcl") == 0)
5627 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005628#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005629#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005630 else if (STRICMP(name, "terminal") == 0)
5631 n = terminal_enabled();
5632#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005633 }
5634
Bram Moolenaar79296512020-03-22 16:17:14 +01005635 // features not in has_list[]
5636 if (x == FALSE)
5637 {
5638 if (STRNICMP(name, "patch", 5) == 0)
5639 {
5640 x = TRUE;
5641 if (name[5] == '-'
5642 && STRLEN(name) >= 11
5643 && vim_isdigit(name[6])
5644 && vim_isdigit(name[8])
5645 && vim_isdigit(name[10]))
5646 {
5647 int major = atoi((char *)name + 6);
5648 int minor = atoi((char *)name + 8);
5649
5650 // Expect "patch-9.9.01234".
5651 n = (major < VIM_VERSION_MAJOR
5652 || (major == VIM_VERSION_MAJOR
5653 && (minor < VIM_VERSION_MINOR
5654 || (minor == VIM_VERSION_MINOR
5655 && has_patch(atoi((char *)name + 10))))));
5656 }
5657 else
5658 n = has_patch(atoi((char *)name + 5));
5659 }
5660 else if (STRICMP(name, "vim_starting") == 0)
5661 {
5662 x = TRUE;
5663 n = (starting != 0);
5664 }
5665 else if (STRICMP(name, "ttyin") == 0)
5666 {
5667 x = TRUE;
5668 n = mch_input_isatty();
5669 }
5670 else if (STRICMP(name, "ttyout") == 0)
5671 {
5672 x = TRUE;
5673 n = stdout_isatty;
5674 }
5675 else if (STRICMP(name, "multi_byte_encoding") == 0)
5676 {
5677 x = TRUE;
5678 n = has_mbyte;
5679 }
5680 else if (STRICMP(name, "gui_running") == 0)
5681 {
5682 x = TRUE;
5683#ifdef FEAT_GUI
5684 n = (gui.in_use || gui.starting);
5685#endif
5686 }
5687 else if (STRICMP(name, "browse") == 0)
5688 {
5689 x = TRUE;
5690#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5691 n = gui.in_use; // gui_mch_browse() works when GUI is running
5692#endif
5693 }
5694 else if (STRICMP(name, "syntax_items") == 0)
5695 {
5696 x = TRUE;
5697#ifdef FEAT_SYN_HL
5698 n = syntax_present(curwin);
5699#endif
5700 }
5701 else if (STRICMP(name, "vcon") == 0)
5702 {
5703 x = TRUE;
5704#ifdef FEAT_VTP
5705 n = is_term_win32() && has_vtp_working();
5706#endif
5707 }
5708 else if (STRICMP(name, "netbeans_enabled") == 0)
5709 {
5710 x = TRUE;
5711#ifdef FEAT_NETBEANS_INTG
5712 n = netbeans_active();
5713#endif
5714 }
5715 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5716 {
5717 x = TRUE;
5718#ifdef FEAT_MOUSE_GPM
5719 n = gpm_enabled();
5720#endif
5721 }
5722 else if (STRICMP(name, "conpty") == 0)
5723 {
5724 x = TRUE;
5725#if defined(FEAT_TERMINAL) && defined(MSWIN)
5726 n = use_conpty();
5727#endif
5728 }
5729 else if (STRICMP(name, "clipboard_working") == 0)
5730 {
5731 x = TRUE;
5732#ifdef FEAT_CLIPBOARD
5733 n = clip_star.available;
5734#endif
5735 }
5736 }
5737
Bram Moolenaar04637e22020-09-05 18:45:29 +02005738 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005739 // return whether feature could ever be enabled
5740 rettv->vval.v_number = x;
5741 else
5742 // return whether feature is enabled
5743 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005744}
5745
5746/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005747 * Return TRUE if "feature" can change later.
5748 * Also when checking for the feature has side effects, such as loading a DLL.
5749 */
5750 int
5751dynamic_feature(char_u *feature)
5752{
5753 return (feature == NULL
5754#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5755 || STRICMP(feature, "balloon_multiline") == 0
5756#endif
5757#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5758 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5759#endif
5760#ifdef VIMDLL
5761 || STRICMP(feature, "filterpipe") == 0
5762#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005763#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005764 // this can only change on Unix where the ":gui" command could be
5765 // used.
5766 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5767#endif
5768#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5769 || STRICMP(feature, "iconv") == 0
5770#endif
5771#ifdef DYNAMIC_LUA
5772 || STRICMP(feature, "lua") == 0
5773#endif
5774#ifdef FEAT_MOUSE_GPM
5775 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5776#endif
5777#ifdef DYNAMIC_MZSCHEME
5778 || STRICMP(feature, "mzscheme") == 0
5779#endif
5780#ifdef FEAT_NETBEANS_INTG
5781 || STRICMP(feature, "netbeans_enabled") == 0
5782#endif
5783#ifdef DYNAMIC_PERL
5784 || STRICMP(feature, "perl") == 0
5785#endif
5786#ifdef DYNAMIC_PYTHON
5787 || STRICMP(feature, "python") == 0
5788#endif
5789#ifdef DYNAMIC_PYTHON3
5790 || STRICMP(feature, "python3") == 0
5791#endif
5792#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5793 || STRICMP(feature, "pythonx") == 0
5794#endif
5795#ifdef DYNAMIC_RUBY
5796 || STRICMP(feature, "ruby") == 0
5797#endif
5798#ifdef FEAT_SYN_HL
5799 || STRICMP(feature, "syntax_items") == 0
5800#endif
5801#ifdef DYNAMIC_TCL
5802 || STRICMP(feature, "tcl") == 0
5803#endif
5804 // once "starting" is zero it will stay that way
5805 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5806 || STRICMP(feature, "multi_byte_encoding") == 0
5807#if defined(FEAT_TERMINAL) && defined(MSWIN)
5808 || STRICMP(feature, "conpty") == 0
5809#endif
5810 );
5811}
5812
5813/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005814 * "haslocaldir()" function
5815 */
5816 static void
5817f_haslocaldir(typval_T *argvars, typval_T *rettv)
5818{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005819 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005820 win_T *wp = NULL;
5821
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005822 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5823
5824 // Check for window-local and tab-local directories
5825 if (wp != NULL && wp->w_localdir != NULL)
5826 rettv->vval.v_number = 1;
5827 else if (tp != NULL && tp->tp_localdir != NULL)
5828 rettv->vval.v_number = 2;
5829 else
5830 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005831}
5832
5833/*
5834 * "hasmapto()" function
5835 */
5836 static void
5837f_hasmapto(typval_T *argvars, typval_T *rettv)
5838{
5839 char_u *name;
5840 char_u *mode;
5841 char_u buf[NUMBUFLEN];
5842 int abbr = FALSE;
5843
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005844 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005845 if (argvars[1].v_type == VAR_UNKNOWN)
5846 mode = (char_u *)"nvo";
5847 else
5848 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005849 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005850 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005851 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005852 }
5853
5854 if (map_to_exists(name, mode, abbr))
5855 rettv->vval.v_number = TRUE;
5856 else
5857 rettv->vval.v_number = FALSE;
5858}
5859
5860/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005861 * "highlightID(name)" function
5862 */
5863 static void
5864f_hlID(typval_T *argvars, typval_T *rettv)
5865{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005866 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005867}
5868
5869/*
5870 * "highlight_exists()" function
5871 */
5872 static void
5873f_hlexists(typval_T *argvars, typval_T *rettv)
5874{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005875 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005876}
5877
5878/*
5879 * "hostname()" function
5880 */
5881 static void
5882f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5883{
5884 char_u hostname[256];
5885
5886 mch_get_host_name(hostname, 256);
5887 rettv->v_type = VAR_STRING;
5888 rettv->vval.v_string = vim_strsave(hostname);
5889}
5890
5891/*
5892 * iconv() function
5893 */
5894 static void
5895f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5896{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005897 char_u buf1[NUMBUFLEN];
5898 char_u buf2[NUMBUFLEN];
5899 char_u *from, *to, *str;
5900 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005901
5902 rettv->v_type = VAR_STRING;
5903 rettv->vval.v_string = NULL;
5904
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005905 str = tv_get_string(&argvars[0]);
5906 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5907 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005908 vimconv.vc_type = CONV_NONE;
5909 convert_setup(&vimconv, from, to);
5910
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005911 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005912 if (vimconv.vc_type == CONV_NONE)
5913 rettv->vval.v_string = vim_strsave(str);
5914 else
5915 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5916
5917 convert_setup(&vimconv, NULL, NULL);
5918 vim_free(from);
5919 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005920}
5921
5922/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005923 * "index()" function
5924 */
5925 static void
5926f_index(typval_T *argvars, typval_T *rettv)
5927{
5928 list_T *l;
5929 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005930 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005931 long idx = 0;
5932 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005933 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005934
5935 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005936 if (argvars[0].v_type == VAR_BLOB)
5937 {
5938 typval_T tv;
5939 int start = 0;
5940
5941 if (argvars[2].v_type != VAR_UNKNOWN)
5942 {
5943 start = tv_get_number_chk(&argvars[2], &error);
5944 if (error)
5945 return;
5946 }
5947 b = argvars[0].vval.v_blob;
5948 if (b == NULL)
5949 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005950 if (start < 0)
5951 {
5952 start = blob_len(b) + start;
5953 if (start < 0)
5954 start = 0;
5955 }
5956
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005957 for (idx = start; idx < blob_len(b); ++idx)
5958 {
5959 tv.v_type = VAR_NUMBER;
5960 tv.vval.v_number = blob_get(b, idx);
5961 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5962 {
5963 rettv->vval.v_number = idx;
5964 return;
5965 }
5966 }
5967 return;
5968 }
5969 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005970 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005971 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005972 return;
5973 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005974
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005975 l = argvars[0].vval.v_list;
5976 if (l != NULL)
5977 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005978 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005979 item = l->lv_first;
5980 if (argvars[2].v_type != VAR_UNKNOWN)
5981 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005982 // Start at specified item. Use the cached index that list_find()
5983 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005984 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005985 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005986 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005987 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005988 if (error)
5989 item = NULL;
5990 }
5991
5992 for ( ; item != NULL; item = item->li_next, ++idx)
5993 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5994 {
5995 rettv->vval.v_number = idx;
5996 break;
5997 }
5998 }
5999}
6000
6001static int inputsecret_flag = 0;
6002
6003/*
6004 * "input()" function
6005 * Also handles inputsecret() when inputsecret is set.
6006 */
6007 static void
6008f_input(typval_T *argvars, typval_T *rettv)
6009{
6010 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6011}
6012
6013/*
6014 * "inputdialog()" function
6015 */
6016 static void
6017f_inputdialog(typval_T *argvars, typval_T *rettv)
6018{
6019#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006020 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006021 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6022 {
6023 char_u *message;
6024 char_u buf[NUMBUFLEN];
6025 char_u *defstr = (char_u *)"";
6026
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006027 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006028 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006029 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006030 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6031 else
6032 IObuff[0] = NUL;
6033 if (message != NULL && defstr != NULL
6034 && do_dialog(VIM_QUESTION, NULL, message,
6035 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6036 rettv->vval.v_string = vim_strsave(IObuff);
6037 else
6038 {
6039 if (message != NULL && defstr != NULL
6040 && argvars[1].v_type != VAR_UNKNOWN
6041 && argvars[2].v_type != VAR_UNKNOWN)
6042 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006043 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006044 else
6045 rettv->vval.v_string = NULL;
6046 }
6047 rettv->v_type = VAR_STRING;
6048 }
6049 else
6050#endif
6051 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6052}
6053
6054/*
6055 * "inputlist()" function
6056 */
6057 static void
6058f_inputlist(typval_T *argvars, typval_T *rettv)
6059{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006060 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006061 listitem_T *li;
6062 int selected;
6063 int mouse_used;
6064
6065#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006066 // While starting up, there is no place to enter text. When running tests
6067 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006068 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006069 return;
6070#endif
6071 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006073 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006074 return;
6075 }
6076
6077 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006078 msg_row = Rows - 1; // for when 'cmdheight' > 1
6079 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006080 msg_scroll = TRUE;
6081 msg_clr_eos();
6082
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006083 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006084 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006085 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006086 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006087 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006088 msg_putchar('\n');
6089 }
6090
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006091 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006092 selected = prompt_for_number(&mouse_used);
6093 if (mouse_used)
6094 selected -= lines_left;
6095
6096 rettv->vval.v_number = selected;
6097}
6098
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006099static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6100
6101/*
6102 * "inputrestore()" function
6103 */
6104 static void
6105f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6106{
6107 if (ga_userinput.ga_len > 0)
6108 {
6109 --ga_userinput.ga_len;
6110 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6111 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006112 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006113 }
6114 else if (p_verbose > 1)
6115 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006116 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006117 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006118 }
6119}
6120
6121/*
6122 * "inputsave()" function
6123 */
6124 static void
6125f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6126{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006127 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006128 if (ga_grow(&ga_userinput, 1) == OK)
6129 {
6130 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6131 + ga_userinput.ga_len);
6132 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006133 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006134 }
6135 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006136 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006137}
6138
6139/*
6140 * "inputsecret()" function
6141 */
6142 static void
6143f_inputsecret(typval_T *argvars, typval_T *rettv)
6144{
6145 ++cmdline_star;
6146 ++inputsecret_flag;
6147 f_input(argvars, rettv);
6148 --cmdline_star;
6149 --inputsecret_flag;
6150}
6151
6152/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01006153 * "interrupt()" function
6154 */
6155 static void
6156f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6157{
6158 got_int = TRUE;
6159}
6160
6161/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006162 * "invert(expr)" function
6163 */
6164 static void
6165f_invert(typval_T *argvars, typval_T *rettv)
6166{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006167 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006168}
6169
6170/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006171 * "islocked()" function
6172 */
6173 static void
6174f_islocked(typval_T *argvars, typval_T *rettv)
6175{
6176 lval_T lv;
6177 char_u *end;
6178 dictitem_T *di;
6179
6180 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006181 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006182 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006183 if (end != NULL && lv.ll_name != NULL)
6184 {
6185 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02006186 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006187 else
6188 {
6189 if (lv.ll_tv == NULL)
6190 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006191 di = find_var(lv.ll_name, NULL, TRUE);
6192 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006193 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006194 // Consider a variable locked when:
6195 // 1. the variable itself is locked
6196 // 2. the value of the variable is locked.
6197 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01006198 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6199 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006200 }
6201 }
6202 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006203 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006204 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006205 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006206 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006207 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006208 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6209 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006210 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006211 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6212 }
6213 }
6214
6215 clear_lval(&lv);
6216}
6217
6218#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6219/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006220 * "isinf()" function
6221 */
6222 static void
6223f_isinf(typval_T *argvars, typval_T *rettv)
6224{
6225 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6226 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6227}
6228
6229/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006230 * "isnan()" function
6231 */
6232 static void
6233f_isnan(typval_T *argvars, typval_T *rettv)
6234{
6235 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6236 && isnan(argvars[0].vval.v_float);
6237}
6238#endif
6239
6240/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006241 * "last_buffer_nr()" function.
6242 */
6243 static void
6244f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6245{
6246 int n = 0;
6247 buf_T *buf;
6248
Bram Moolenaar29323592016-07-24 22:04:11 +02006249 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006250 if (n < buf->b_fnum)
6251 n = buf->b_fnum;
6252
6253 rettv->vval.v_number = n;
6254}
6255
6256/*
6257 * "len()" function
6258 */
6259 static void
6260f_len(typval_T *argvars, typval_T *rettv)
6261{
6262 switch (argvars[0].v_type)
6263 {
6264 case VAR_STRING:
6265 case VAR_NUMBER:
6266 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006267 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006268 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006269 case VAR_BLOB:
6270 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6271 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006272 case VAR_LIST:
6273 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6274 break;
6275 case VAR_DICT:
6276 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6277 break;
6278 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006279 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006280 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006281 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006282 case VAR_SPECIAL:
6283 case VAR_FLOAT:
6284 case VAR_FUNC:
6285 case VAR_PARTIAL:
6286 case VAR_JOB:
6287 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006288 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006289 break;
6290 }
6291}
6292
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006293 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006294libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006295{
6296#ifdef FEAT_LIBCALL
6297 char_u *string_in;
6298 char_u **string_result;
6299 int nr_result;
6300#endif
6301
6302 rettv->v_type = type;
6303 if (type != VAR_NUMBER)
6304 rettv->vval.v_string = NULL;
6305
6306 if (check_restricted() || check_secure())
6307 return;
6308
6309#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006310 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006311 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6312 {
6313 string_in = NULL;
6314 if (argvars[2].v_type == VAR_STRING)
6315 string_in = argvars[2].vval.v_string;
6316 if (type == VAR_NUMBER)
6317 string_result = NULL;
6318 else
6319 string_result = &rettv->vval.v_string;
6320 if (mch_libcall(argvars[0].vval.v_string,
6321 argvars[1].vval.v_string,
6322 string_in,
6323 argvars[2].vval.v_number,
6324 string_result,
6325 &nr_result) == OK
6326 && type == VAR_NUMBER)
6327 rettv->vval.v_number = nr_result;
6328 }
6329#endif
6330}
6331
6332/*
6333 * "libcall()" function
6334 */
6335 static void
6336f_libcall(typval_T *argvars, typval_T *rettv)
6337{
6338 libcall_common(argvars, rettv, VAR_STRING);
6339}
6340
6341/*
6342 * "libcallnr()" function
6343 */
6344 static void
6345f_libcallnr(typval_T *argvars, typval_T *rettv)
6346{
6347 libcall_common(argvars, rettv, VAR_NUMBER);
6348}
6349
6350/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006351 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006352 */
6353 static void
6354f_line(typval_T *argvars, typval_T *rettv)
6355{
6356 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006357 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006358 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006359 int id;
6360 tabpage_T *tp;
6361 win_T *wp;
6362 win_T *save_curwin;
6363 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006364
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006365 if (argvars[1].v_type != VAR_UNKNOWN)
6366 {
6367 // use window specified in the second argument
6368 id = (int)tv_get_number(&argvars[1]);
6369 wp = win_id2wp_tp(id, &tp);
6370 if (wp != NULL && tp != NULL)
6371 {
6372 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6373 == OK)
6374 {
6375 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006376 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006377 }
6378 restore_win_noblock(save_curwin, save_curtab, TRUE);
6379 }
6380 }
6381 else
6382 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006383 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006384
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006385 if (fp != NULL)
6386 lnum = fp->lnum;
6387 rettv->vval.v_number = lnum;
6388}
6389
6390/*
6391 * "line2byte(lnum)" function
6392 */
6393 static void
6394f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6395{
6396#ifndef FEAT_BYTEOFF
6397 rettv->vval.v_number = -1;
6398#else
6399 linenr_T lnum;
6400
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006401 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006402 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6403 rettv->vval.v_number = -1;
6404 else
6405 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6406 if (rettv->vval.v_number >= 0)
6407 ++rettv->vval.v_number;
6408#endif
6409}
6410
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006411#ifdef FEAT_FLOAT
6412/*
6413 * "log()" function
6414 */
6415 static void
6416f_log(typval_T *argvars, typval_T *rettv)
6417{
6418 float_T f = 0.0;
6419
6420 rettv->v_type = VAR_FLOAT;
6421 if (get_float_arg(argvars, &f) == OK)
6422 rettv->vval.v_float = log(f);
6423 else
6424 rettv->vval.v_float = 0.0;
6425}
6426
6427/*
6428 * "log10()" function
6429 */
6430 static void
6431f_log10(typval_T *argvars, typval_T *rettv)
6432{
6433 float_T f = 0.0;
6434
6435 rettv->v_type = VAR_FLOAT;
6436 if (get_float_arg(argvars, &f) == OK)
6437 rettv->vval.v_float = log10(f);
6438 else
6439 rettv->vval.v_float = 0.0;
6440}
6441#endif
6442
6443#ifdef FEAT_LUA
6444/*
6445 * "luaeval()" function
6446 */
6447 static void
6448f_luaeval(typval_T *argvars, typval_T *rettv)
6449{
6450 char_u *str;
6451 char_u buf[NUMBUFLEN];
6452
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006453 if (check_restricted() || check_secure())
6454 return;
6455
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006456 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006457 do_luaeval(str, argvars + 1, rettv);
6458}
6459#endif
6460
6461/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006462 * "maparg()" function
6463 */
6464 static void
6465f_maparg(typval_T *argvars, typval_T *rettv)
6466{
6467 get_maparg(argvars, rettv, TRUE);
6468}
6469
6470/*
6471 * "mapcheck()" function
6472 */
6473 static void
6474f_mapcheck(typval_T *argvars, typval_T *rettv)
6475{
6476 get_maparg(argvars, rettv, FALSE);
6477}
6478
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006479typedef enum
6480{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006481 MATCH_END, // matchend()
6482 MATCH_MATCH, // match()
6483 MATCH_STR, // matchstr()
6484 MATCH_LIST, // matchlist()
6485 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006486} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006487
6488 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006489find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006490{
6491 char_u *str = NULL;
6492 long len = 0;
6493 char_u *expr = NULL;
6494 char_u *pat;
6495 regmatch_T regmatch;
6496 char_u patbuf[NUMBUFLEN];
6497 char_u strbuf[NUMBUFLEN];
6498 char_u *save_cpo;
6499 long start = 0;
6500 long nth = 1;
6501 colnr_T startcol = 0;
6502 int match = 0;
6503 list_T *l = NULL;
6504 listitem_T *li = NULL;
6505 long idx = 0;
6506 char_u *tofree = NULL;
6507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006508 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006509 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006510 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006511
6512 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006513 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006514 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006515 // type MATCH_LIST: return empty list when there are no matches.
6516 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006517 if (rettv_list_alloc(rettv) == FAIL)
6518 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006519 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006520 && (list_append_string(rettv->vval.v_list,
6521 (char_u *)"", 0) == FAIL
6522 || list_append_number(rettv->vval.v_list,
6523 (varnumber_T)-1) == FAIL
6524 || list_append_number(rettv->vval.v_list,
6525 (varnumber_T)-1) == FAIL
6526 || list_append_number(rettv->vval.v_list,
6527 (varnumber_T)-1) == FAIL))
6528 {
6529 list_free(rettv->vval.v_list);
6530 rettv->vval.v_list = NULL;
6531 goto theend;
6532 }
6533 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006534 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006535 {
6536 rettv->v_type = VAR_STRING;
6537 rettv->vval.v_string = NULL;
6538 }
6539
6540 if (argvars[0].v_type == VAR_LIST)
6541 {
6542 if ((l = argvars[0].vval.v_list) == NULL)
6543 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006544 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006545 li = l->lv_first;
6546 }
6547 else
6548 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006549 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006550 len = (long)STRLEN(str);
6551 }
6552
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006553 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006554 if (pat == NULL)
6555 goto theend;
6556
6557 if (argvars[2].v_type != VAR_UNKNOWN)
6558 {
6559 int error = FALSE;
6560
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006561 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006562 if (error)
6563 goto theend;
6564 if (l != NULL)
6565 {
6566 li = list_find(l, start);
6567 if (li == NULL)
6568 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006569 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006570 }
6571 else
6572 {
6573 if (start < 0)
6574 start = 0;
6575 if (start > len)
6576 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006577 // When "count" argument is there ignore matches before "start",
6578 // otherwise skip part of the string. Differs when pattern is "^"
6579 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006580 if (argvars[3].v_type != VAR_UNKNOWN)
6581 startcol = start;
6582 else
6583 {
6584 str += start;
6585 len -= start;
6586 }
6587 }
6588
6589 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006590 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006591 if (error)
6592 goto theend;
6593 }
6594
6595 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6596 if (regmatch.regprog != NULL)
6597 {
6598 regmatch.rm_ic = p_ic;
6599
6600 for (;;)
6601 {
6602 if (l != NULL)
6603 {
6604 if (li == NULL)
6605 {
6606 match = FALSE;
6607 break;
6608 }
6609 vim_free(tofree);
6610 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6611 if (str == NULL)
6612 break;
6613 }
6614
6615 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6616
6617 if (match && --nth <= 0)
6618 break;
6619 if (l == NULL && !match)
6620 break;
6621
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006622 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006623 if (l != NULL)
6624 {
6625 li = li->li_next;
6626 ++idx;
6627 }
6628 else
6629 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006630 startcol = (colnr_T)(regmatch.startp[0]
6631 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006632 if (startcol > (colnr_T)len
6633 || str + startcol <= regmatch.startp[0])
6634 {
6635 match = FALSE;
6636 break;
6637 }
6638 }
6639 }
6640
6641 if (match)
6642 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006643 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006644 {
6645 listitem_T *li1 = rettv->vval.v_list->lv_first;
6646 listitem_T *li2 = li1->li_next;
6647 listitem_T *li3 = li2->li_next;
6648 listitem_T *li4 = li3->li_next;
6649
6650 vim_free(li1->li_tv.vval.v_string);
6651 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006652 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006653 li3->li_tv.vval.v_number =
6654 (varnumber_T)(regmatch.startp[0] - expr);
6655 li4->li_tv.vval.v_number =
6656 (varnumber_T)(regmatch.endp[0] - expr);
6657 if (l != NULL)
6658 li2->li_tv.vval.v_number = (varnumber_T)idx;
6659 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006660 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006661 {
6662 int i;
6663
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006664 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006665 for (i = 0; i < NSUBEXP; ++i)
6666 {
6667 if (regmatch.endp[i] == NULL)
6668 {
6669 if (list_append_string(rettv->vval.v_list,
6670 (char_u *)"", 0) == FAIL)
6671 break;
6672 }
6673 else if (list_append_string(rettv->vval.v_list,
6674 regmatch.startp[i],
6675 (int)(regmatch.endp[i] - regmatch.startp[i]))
6676 == FAIL)
6677 break;
6678 }
6679 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006680 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006681 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006682 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006683 if (l != NULL)
6684 copy_tv(&li->li_tv, rettv);
6685 else
6686 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006687 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006688 }
6689 else if (l != NULL)
6690 rettv->vval.v_number = idx;
6691 else
6692 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006693 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694 rettv->vval.v_number =
6695 (varnumber_T)(regmatch.startp[0] - str);
6696 else
6697 rettv->vval.v_number =
6698 (varnumber_T)(regmatch.endp[0] - str);
6699 rettv->vval.v_number += (varnumber_T)(str - expr);
6700 }
6701 }
6702 vim_regfree(regmatch.regprog);
6703 }
6704
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006705theend:
6706 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006707 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006708 listitem_remove(rettv->vval.v_list,
6709 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006710 vim_free(tofree);
6711 p_cpo = save_cpo;
6712}
6713
6714/*
6715 * "match()" function
6716 */
6717 static void
6718f_match(typval_T *argvars, typval_T *rettv)
6719{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006720 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006721}
6722
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006723/*
6724 * "matchend()" function
6725 */
6726 static void
6727f_matchend(typval_T *argvars, typval_T *rettv)
6728{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006729 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006730}
6731
6732/*
6733 * "matchlist()" function
6734 */
6735 static void
6736f_matchlist(typval_T *argvars, typval_T *rettv)
6737{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006738 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006739}
6740
6741/*
6742 * "matchstr()" function
6743 */
6744 static void
6745f_matchstr(typval_T *argvars, typval_T *rettv)
6746{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006747 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006748}
6749
6750/*
6751 * "matchstrpos()" function
6752 */
6753 static void
6754f_matchstrpos(typval_T *argvars, typval_T *rettv)
6755{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006756 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006757}
6758
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006759 static void
6760max_min(typval_T *argvars, typval_T *rettv, int domax)
6761{
6762 varnumber_T n = 0;
6763 varnumber_T i;
6764 int error = FALSE;
6765
6766 if (argvars[0].v_type == VAR_LIST)
6767 {
6768 list_T *l;
6769 listitem_T *li;
6770
6771 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006772 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006773 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006774 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006775 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006776 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6777 n = l->lv_u.nonmat.lv_start;
6778 else
6779 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6780 * l->lv_u.nonmat.lv_stride;
6781 }
6782 else
6783 {
6784 li = l->lv_first;
6785 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006786 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006787 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006788 if (error)
6789 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006790 for (;;)
6791 {
6792 li = li->li_next;
6793 if (li == NULL)
6794 break;
6795 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006796 if (error)
6797 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006798 if (domax ? i > n : i < n)
6799 n = i;
6800 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006801 }
6802 }
6803 }
6804 }
6805 else if (argvars[0].v_type == VAR_DICT)
6806 {
6807 dict_T *d;
6808 int first = TRUE;
6809 hashitem_T *hi;
6810 int todo;
6811
6812 d = argvars[0].vval.v_dict;
6813 if (d != NULL)
6814 {
6815 todo = (int)d->dv_hashtab.ht_used;
6816 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6817 {
6818 if (!HASHITEM_EMPTY(hi))
6819 {
6820 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006821 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006822 if (error)
6823 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006824 if (first)
6825 {
6826 n = i;
6827 first = FALSE;
6828 }
6829 else if (domax ? i > n : i < n)
6830 n = i;
6831 }
6832 }
6833 }
6834 }
6835 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006836 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006837
6838 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006839}
6840
6841/*
6842 * "max()" function
6843 */
6844 static void
6845f_max(typval_T *argvars, typval_T *rettv)
6846{
6847 max_min(argvars, rettv, TRUE);
6848}
6849
6850/*
6851 * "min()" function
6852 */
6853 static void
6854f_min(typval_T *argvars, typval_T *rettv)
6855{
6856 max_min(argvars, rettv, FALSE);
6857}
6858
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006859#if defined(FEAT_MZSCHEME) || defined(PROTO)
6860/*
6861 * "mzeval()" function
6862 */
6863 static void
6864f_mzeval(typval_T *argvars, typval_T *rettv)
6865{
6866 char_u *str;
6867 char_u buf[NUMBUFLEN];
6868
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006869 if (check_restricted() || check_secure())
6870 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006871 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006872 do_mzeval(str, rettv);
6873}
6874
6875 void
6876mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6877{
6878 typval_T argvars[3];
6879
6880 argvars[0].v_type = VAR_STRING;
6881 argvars[0].vval.v_string = name;
6882 copy_tv(args, &argvars[1]);
6883 argvars[2].v_type = VAR_UNKNOWN;
6884 f_call(argvars, rettv);
6885 clear_tv(&argvars[1]);
6886}
6887#endif
6888
6889/*
6890 * "nextnonblank()" function
6891 */
6892 static void
6893f_nextnonblank(typval_T *argvars, typval_T *rettv)
6894{
6895 linenr_T lnum;
6896
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006897 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006898 {
6899 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6900 {
6901 lnum = 0;
6902 break;
6903 }
6904 if (*skipwhite(ml_get(lnum)) != NUL)
6905 break;
6906 }
6907 rettv->vval.v_number = lnum;
6908}
6909
6910/*
6911 * "nr2char()" function
6912 */
6913 static void
6914f_nr2char(typval_T *argvars, typval_T *rettv)
6915{
6916 char_u buf[NUMBUFLEN];
6917
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006918 if (has_mbyte)
6919 {
6920 int utf8 = 0;
6921
6922 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006923 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006924 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006925 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006926 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006927 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006928 }
6929 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006930 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006931 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006932 buf[1] = NUL;
6933 }
6934 rettv->v_type = VAR_STRING;
6935 rettv->vval.v_string = vim_strsave(buf);
6936}
6937
6938/*
6939 * "or(expr, expr)" function
6940 */
6941 static void
6942f_or(typval_T *argvars, typval_T *rettv)
6943{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006944 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6945 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006946}
6947
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006948#ifdef FEAT_PERL
6949/*
6950 * "perleval()" function
6951 */
6952 static void
6953f_perleval(typval_T *argvars, typval_T *rettv)
6954{
6955 char_u *str;
6956 char_u buf[NUMBUFLEN];
6957
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006958 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006959 do_perleval(str, rettv);
6960}
6961#endif
6962
6963#ifdef FEAT_FLOAT
6964/*
6965 * "pow()" function
6966 */
6967 static void
6968f_pow(typval_T *argvars, typval_T *rettv)
6969{
6970 float_T fx = 0.0, fy = 0.0;
6971
6972 rettv->v_type = VAR_FLOAT;
6973 if (get_float_arg(argvars, &fx) == OK
6974 && get_float_arg(&argvars[1], &fy) == OK)
6975 rettv->vval.v_float = pow(fx, fy);
6976 else
6977 rettv->vval.v_float = 0.0;
6978}
6979#endif
6980
6981/*
6982 * "prevnonblank()" function
6983 */
6984 static void
6985f_prevnonblank(typval_T *argvars, typval_T *rettv)
6986{
6987 linenr_T lnum;
6988
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006989 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006990 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6991 lnum = 0;
6992 else
6993 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6994 --lnum;
6995 rettv->vval.v_number = lnum;
6996}
6997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006998// This dummy va_list is here because:
6999// - passing a NULL pointer doesn't work when va_list isn't a pointer
7000// - locally in the function results in a "used before set" warning
7001// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007002static va_list ap;
7003
7004/*
7005 * "printf()" function
7006 */
7007 static void
7008f_printf(typval_T *argvars, typval_T *rettv)
7009{
7010 char_u buf[NUMBUFLEN];
7011 int len;
7012 char_u *s;
7013 int saved_did_emsg = did_emsg;
7014 char *fmt;
7015
7016 rettv->v_type = VAR_STRING;
7017 rettv->vval.v_string = NULL;
7018
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007019 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007020 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007021 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007022 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007023 if (!did_emsg)
7024 {
7025 s = alloc(len + 1);
7026 if (s != NULL)
7027 {
7028 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007029 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
7030 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007031 }
7032 }
7033 did_emsg |= saved_did_emsg;
7034}
7035
7036/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007037 * "pum_getpos()" function
7038 */
7039 static void
7040f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7041{
7042 if (rettv_dict_alloc(rettv) != OK)
7043 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007044 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007045}
7046
7047/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007048 * "pumvisible()" function
7049 */
7050 static void
7051f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7052{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007053 if (pum_visible())
7054 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007055}
7056
7057#ifdef FEAT_PYTHON3
7058/*
7059 * "py3eval()" function
7060 */
7061 static void
7062f_py3eval(typval_T *argvars, typval_T *rettv)
7063{
7064 char_u *str;
7065 char_u buf[NUMBUFLEN];
7066
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007067 if (check_restricted() || check_secure())
7068 return;
7069
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007070 if (p_pyx == 0)
7071 p_pyx = 3;
7072
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007073 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007074 do_py3eval(str, rettv);
7075}
7076#endif
7077
7078#ifdef FEAT_PYTHON
7079/*
7080 * "pyeval()" function
7081 */
7082 static void
7083f_pyeval(typval_T *argvars, typval_T *rettv)
7084{
7085 char_u *str;
7086 char_u buf[NUMBUFLEN];
7087
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007088 if (check_restricted() || check_secure())
7089 return;
7090
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007091 if (p_pyx == 0)
7092 p_pyx = 2;
7093
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007094 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007095 do_pyeval(str, rettv);
7096}
7097#endif
7098
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007099#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
7100/*
7101 * "pyxeval()" function
7102 */
7103 static void
7104f_pyxeval(typval_T *argvars, typval_T *rettv)
7105{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007106 if (check_restricted() || check_secure())
7107 return;
7108
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007109# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
7110 init_pyxversion();
7111 if (p_pyx == 2)
7112 f_pyeval(argvars, rettv);
7113 else
7114 f_py3eval(argvars, rettv);
7115# elif defined(FEAT_PYTHON)
7116 f_pyeval(argvars, rettv);
7117# elif defined(FEAT_PYTHON3)
7118 f_py3eval(argvars, rettv);
7119# endif
7120}
7121#endif
7122
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007123static UINT32_T srand_seed_for_testing = 0;
7124static int srand_seed_for_testing_is_used = FALSE;
7125
7126 static void
7127f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
7128{
7129 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007130 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007131 else
7132 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007133 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
7134 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007135 }
7136}
7137
7138 static void
7139init_srand(UINT32_T *x)
7140{
7141#ifndef MSWIN
7142 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
7143#endif
7144
7145 if (srand_seed_for_testing_is_used)
7146 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007147 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007148 return;
7149 }
7150#ifndef MSWIN
7151 if (dev_urandom_state != FAIL)
7152 {
7153 int fd = open("/dev/urandom", O_RDONLY);
7154 struct {
7155 union {
7156 UINT32_T number;
7157 char bytes[sizeof(UINT32_T)];
7158 } contents;
7159 } buf;
7160
7161 // Attempt reading /dev/urandom.
7162 if (fd == -1)
7163 dev_urandom_state = FAIL;
7164 else
7165 {
7166 buf.contents.number = 0;
7167 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
7168 != sizeof(UINT32_T))
7169 dev_urandom_state = FAIL;
7170 else
7171 {
7172 dev_urandom_state = OK;
7173 *x = buf.contents.number;
7174 }
7175 close(fd);
7176 }
7177 }
7178 if (dev_urandom_state != OK)
7179 // Reading /dev/urandom doesn't work, fall back to time().
7180#endif
7181 *x = vim_time();
7182}
7183
7184#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
7185#define SPLITMIX32(x, z) ( \
7186 z = (x += 0x9e3779b9), \
7187 z = (z ^ (z >> 16)) * 0x85ebca6b, \
7188 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
7189 z ^ (z >> 16) \
7190 )
7191#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
7192 result = ROTL(y * 5, 7) * 9; \
7193 t = y << 9; \
7194 z ^= x; \
7195 w ^= y; \
7196 y ^= z, x ^= w; \
7197 z ^= t; \
7198 w = ROTL(w, 11);
7199
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007200/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007201 * "rand()" function
7202 */
7203 static void
7204f_rand(typval_T *argvars, typval_T *rettv)
7205{
7206 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007207 static UINT32_T gx, gy, gz, gw;
7208 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007209 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01007210 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007211
7212 if (argvars[0].v_type == VAR_UNKNOWN)
7213 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007214 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007215 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007216 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007217 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007218 init_srand(&x);
7219
7220 gx = SPLITMIX32(x, z);
7221 gy = SPLITMIX32(x, z);
7222 gz = SPLITMIX32(x, z);
7223 gw = SPLITMIX32(x, z);
7224 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007225 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007226
7227 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007228 }
7229 else if (argvars[0].v_type == VAR_LIST)
7230 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007231 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007232 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007233 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007234
7235 lx = list_find(l, 0L);
7236 ly = list_find(l, 1L);
7237 lz = list_find(l, 2L);
7238 lw = list_find(l, 3L);
7239 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
7240 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
7241 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
7242 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
7243 x = (UINT32_T)lx->li_tv.vval.v_number;
7244 y = (UINT32_T)ly->li_tv.vval.v_number;
7245 z = (UINT32_T)lz->li_tv.vval.v_number;
7246 w = (UINT32_T)lw->li_tv.vval.v_number;
7247
7248 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
7249
7250 lx->li_tv.vval.v_number = (varnumber_T)x;
7251 ly->li_tv.vval.v_number = (varnumber_T)y;
7252 lz->li_tv.vval.v_number = (varnumber_T)z;
7253 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007254 }
7255 else
7256 goto theend;
7257
7258 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007259 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007260 return;
7261
7262theend:
7263 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007264 rettv->v_type = VAR_NUMBER;
7265 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007266}
7267
7268/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007269 * "srand()" function
7270 */
7271 static void
7272f_srand(typval_T *argvars, typval_T *rettv)
7273{
7274 UINT32_T x = 0, z;
7275
7276 if (rettv_list_alloc(rettv) == FAIL)
7277 return;
7278 if (argvars[0].v_type == VAR_UNKNOWN)
7279 {
7280 init_srand(&x);
7281 }
7282 else
7283 {
7284 int error = FALSE;
7285
7286 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7287 if (error)
7288 return;
7289 }
7290
7291 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7292 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7293 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7294 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7295}
7296
7297#undef ROTL
7298#undef SPLITMIX32
7299#undef SHUFFLE_XOSHIRO128STARSTAR
7300
7301/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007302 * "range()" function
7303 */
7304 static void
7305f_range(typval_T *argvars, typval_T *rettv)
7306{
7307 varnumber_T start;
7308 varnumber_T end;
7309 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007310 int error = FALSE;
7311
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007312 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007313 if (argvars[1].v_type == VAR_UNKNOWN)
7314 {
7315 end = start - 1;
7316 start = 0;
7317 }
7318 else
7319 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007320 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007321 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007322 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007323 }
7324
7325 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007326 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007327 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007328 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007329 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007330 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007331 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007332 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007333 list_T *list = rettv->vval.v_list;
7334
7335 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007336 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007337 // be called.
7338 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007339 list->lv_u.nonmat.lv_start = start;
7340 list->lv_u.nonmat.lv_end = end;
7341 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007342 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007343 }
7344}
7345
7346/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007347 * Materialize "list".
7348 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007349 */
7350 void
7351range_list_materialize(list_T *list)
7352{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007353 varnumber_T start = list->lv_u.nonmat.lv_start;
7354 varnumber_T end = list->lv_u.nonmat.lv_end;
7355 int stride = list->lv_u.nonmat.lv_stride;
7356 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007358 list->lv_first = NULL;
7359 list->lv_u.mat.lv_last = NULL;
7360 list->lv_len = 0;
7361 list->lv_u.mat.lv_idx_item = NULL;
7362 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7363 if (list_append_number(list, (varnumber_T)i) == FAIL)
7364 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007365}
7366
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007367/*
7368 * "getreginfo()" function
7369 */
7370 static void
7371f_getreginfo(typval_T *argvars, typval_T *rettv)
7372{
7373 char_u *strregname;
7374 int regname;
7375 char_u buf[NUMBUFLEN + 2];
7376 long reglen = 0;
7377 dict_T *dict;
7378 list_T *list;
7379
7380 if (argvars[0].v_type != VAR_UNKNOWN)
7381 {
7382 strregname = tv_get_string_chk(&argvars[0]);
7383 if (strregname == NULL)
7384 return;
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007385 if (in_vim9script() && STRLEN(strregname) > 1)
7386 {
7387 semsg(_(e_register_name_must_be_one_char_str), strregname);
7388 return;
7389 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007390 }
7391 else
7392 strregname = get_vim_var_str(VV_REG);
7393
7394 regname = (strregname == NULL ? '"' : *strregname);
7395 if (regname == 0 || regname == '@')
7396 regname = '"';
7397
7398 if (rettv_dict_alloc(rettv) == FAIL)
7399 return;
7400 dict = rettv->vval.v_dict;
7401
7402 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7403 if (list == NULL)
7404 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007405 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007406
7407 buf[0] = NUL;
7408 buf[1] = NUL;
7409 switch (get_reg_type(regname, &reglen))
7410 {
7411 case MLINE: buf[0] = 'V'; break;
7412 case MCHAR: buf[0] = 'v'; break;
7413 case MBLOCK:
7414 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7415 reglen + 1);
7416 break;
7417 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007418 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007419
7420 buf[0] = get_register_name(get_unname_register());
7421 buf[1] = NUL;
7422 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007423 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007424 else
7425 {
7426 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7427
7428 if (item != NULL)
7429 {
7430 item->di_tv.v_type = VAR_SPECIAL;
7431 item->di_tv.vval.v_number = regname == buf[0]
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007432 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007433 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007434 }
7435 }
7436}
7437
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007438 static void
7439return_register(int regname, typval_T *rettv)
7440{
7441 char_u buf[2] = {0, 0};
7442
7443 buf[0] = (char_u)regname;
7444 rettv->v_type = VAR_STRING;
7445 rettv->vval.v_string = vim_strsave(buf);
7446}
7447
7448/*
7449 * "reg_executing()" function
7450 */
7451 static void
7452f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7453{
7454 return_register(reg_executing, rettv);
7455}
7456
7457/*
7458 * "reg_recording()" function
7459 */
7460 static void
7461f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7462{
7463 return_register(reg_recording, rettv);
7464}
7465
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007466/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007467 * "rename({from}, {to})" function
7468 */
7469 static void
7470f_rename(typval_T *argvars, typval_T *rettv)
7471{
7472 char_u buf[NUMBUFLEN];
7473
7474 if (check_restricted() || check_secure())
7475 rettv->vval.v_number = -1;
7476 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007477 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7478 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007479}
7480
7481/*
7482 * "repeat()" function
7483 */
7484 static void
7485f_repeat(typval_T *argvars, typval_T *rettv)
7486{
7487 char_u *p;
7488 int n;
7489 int slen;
7490 int len;
7491 char_u *r;
7492 int i;
7493
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007494 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007495 if (argvars[0].v_type == VAR_LIST)
7496 {
7497 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7498 while (n-- > 0)
7499 if (list_extend(rettv->vval.v_list,
7500 argvars[0].vval.v_list, NULL) == FAIL)
7501 break;
7502 }
7503 else
7504 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007505 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007506 rettv->v_type = VAR_STRING;
7507 rettv->vval.v_string = NULL;
7508
7509 slen = (int)STRLEN(p);
7510 len = slen * n;
7511 if (len <= 0)
7512 return;
7513
7514 r = alloc(len + 1);
7515 if (r != NULL)
7516 {
7517 for (i = 0; i < n; i++)
7518 mch_memmove(r + i * slen, p, (size_t)slen);
7519 r[len] = NUL;
7520 }
7521
7522 rettv->vval.v_string = r;
7523 }
7524}
7525
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007526#define SP_NOMOVE 0x01 // don't move cursor
7527#define SP_REPEAT 0x02 // repeat to find outer pair
7528#define SP_RETCOUNT 0x04 // return matchcount
7529#define SP_SETPCMARK 0x08 // set previous context mark
7530#define SP_START 0x10 // accept match at start position
7531#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7532#define SP_END 0x40 // leave cursor at end of match
7533#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007534
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007535/*
7536 * Get flags for a search function.
7537 * Possibly sets "p_ws".
7538 * Returns BACKWARD, FORWARD or zero (for an error).
7539 */
7540 static int
7541get_search_arg(typval_T *varp, int *flagsp)
7542{
7543 int dir = FORWARD;
7544 char_u *flags;
7545 char_u nbuf[NUMBUFLEN];
7546 int mask;
7547
7548 if (varp->v_type != VAR_UNKNOWN)
7549 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007550 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007551 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007552 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007553 while (*flags != NUL)
7554 {
7555 switch (*flags)
7556 {
7557 case 'b': dir = BACKWARD; break;
7558 case 'w': p_ws = TRUE; break;
7559 case 'W': p_ws = FALSE; break;
7560 default: mask = 0;
7561 if (flagsp != NULL)
7562 switch (*flags)
7563 {
7564 case 'c': mask = SP_START; break;
7565 case 'e': mask = SP_END; break;
7566 case 'm': mask = SP_RETCOUNT; break;
7567 case 'n': mask = SP_NOMOVE; break;
7568 case 'p': mask = SP_SUBPAT; break;
7569 case 'r': mask = SP_REPEAT; break;
7570 case 's': mask = SP_SETPCMARK; break;
7571 case 'z': mask = SP_COLUMN; break;
7572 }
7573 if (mask == 0)
7574 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007575 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007576 dir = 0;
7577 }
7578 else
7579 *flagsp |= mask;
7580 }
7581 if (dir == 0)
7582 break;
7583 ++flags;
7584 }
7585 }
7586 return dir;
7587}
7588
7589/*
7590 * Shared by search() and searchpos() functions.
7591 */
7592 static int
7593search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7594{
7595 int flags;
7596 char_u *pat;
7597 pos_T pos;
7598 pos_T save_cursor;
7599 int save_p_ws = p_ws;
7600 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007601 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007602 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007604 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007605 long time_limit = 0;
7606#endif
7607 int options = SEARCH_KEEP;
7608 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007609 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007610 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007611 pos_T firstpos;
7612
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007613 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007614 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007615 if (dir == 0)
7616 goto theend;
7617 flags = *flagsp;
7618 if (flags & SP_START)
7619 options |= SEARCH_START;
7620 if (flags & SP_END)
7621 options |= SEARCH_END;
7622 if (flags & SP_COLUMN)
7623 options |= SEARCH_COL;
7624
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007625 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007626 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7627 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007628 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007629 if (lnum_stop < 0)
7630 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007631 if (argvars[3].v_type != VAR_UNKNOWN)
7632 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007633#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007634 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007635 if (time_limit < 0)
7636 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007637#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007638 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007639 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007640 }
7641
7642#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007643 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007644 profile_setlimit(time_limit, &tm);
7645#endif
7646
7647 /*
7648 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7649 * Check to make sure only those flags are set.
7650 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7651 * flags cannot be set. Check for that condition also.
7652 */
7653 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7654 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7655 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007656 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007657 goto theend;
7658 }
7659
7660 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007661 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007662 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007663 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7664#ifdef FEAT_RELTIME
7665 sia.sa_tm = &tm;
7666#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007667
7668 // Repeat until {skip} returns FALSE.
7669 for (;;)
7670 {
7671 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007672 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007673 // finding the first match again means there is no match where {skip}
7674 // evaluates to zero.
7675 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7676 subpatnum = FAIL;
7677
Bram Moolenaara9c01042020-06-07 14:50:50 +02007678 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007679 // didn't find it or no skip argument
7680 break;
7681 firstpos = pos;
7682
Bram Moolenaara9c01042020-06-07 14:50:50 +02007683 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007684 {
7685 int do_skip;
7686 int err;
7687 pos_T save_pos = curwin->w_cursor;
7688
7689 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007690 err = FALSE;
7691 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007692 curwin->w_cursor = save_pos;
7693 if (err)
7694 {
7695 // Evaluating {skip} caused an error, break here.
7696 subpatnum = FAIL;
7697 break;
7698 }
7699 if (!do_skip)
7700 break;
7701 }
7702 }
7703
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007704 if (subpatnum != FAIL)
7705 {
7706 if (flags & SP_SUBPAT)
7707 retval = subpatnum;
7708 else
7709 retval = pos.lnum;
7710 if (flags & SP_SETPCMARK)
7711 setpcmark();
7712 curwin->w_cursor = pos;
7713 if (match_pos != NULL)
7714 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007715 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007716 match_pos->lnum = pos.lnum;
7717 match_pos->col = pos.col + 1;
7718 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007719 // "/$" will put the cursor after the end of the line, may need to
7720 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007721 check_cursor();
7722 }
7723
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007724 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007725 if (flags & SP_NOMOVE)
7726 curwin->w_cursor = save_cursor;
7727 else
7728 curwin->w_set_curswant = TRUE;
7729theend:
7730 p_ws = save_p_ws;
7731
7732 return retval;
7733}
7734
7735#ifdef FEAT_FLOAT
7736
7737/*
7738 * round() is not in C90, use ceil() or floor() instead.
7739 */
7740 float_T
7741vim_round(float_T f)
7742{
7743 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
7744}
7745
7746/*
7747 * "round({float})" function
7748 */
7749 static void
7750f_round(typval_T *argvars, typval_T *rettv)
7751{
7752 float_T f = 0.0;
7753
7754 rettv->v_type = VAR_FLOAT;
7755 if (get_float_arg(argvars, &f) == OK)
7756 rettv->vval.v_float = vim_round(f);
7757 else
7758 rettv->vval.v_float = 0.0;
7759}
7760#endif
7761
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007762#ifdef FEAT_RUBY
7763/*
7764 * "rubyeval()" function
7765 */
7766 static void
7767f_rubyeval(typval_T *argvars, typval_T *rettv)
7768{
7769 char_u *str;
7770 char_u buf[NUMBUFLEN];
7771
7772 str = tv_get_string_buf(&argvars[0], buf);
7773 do_rubyeval(str, rettv);
7774}
7775#endif
7776
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007777/*
7778 * "screenattr()" function
7779 */
7780 static void
7781f_screenattr(typval_T *argvars, typval_T *rettv)
7782{
7783 int row;
7784 int col;
7785 int c;
7786
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007787 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7788 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007789 if (row < 0 || row >= screen_Rows
7790 || col < 0 || col >= screen_Columns)
7791 c = -1;
7792 else
7793 c = ScreenAttrs[LineOffset[row] + col];
7794 rettv->vval.v_number = c;
7795}
7796
7797/*
7798 * "screenchar()" function
7799 */
7800 static void
7801f_screenchar(typval_T *argvars, typval_T *rettv)
7802{
7803 int row;
7804 int col;
7805 int off;
7806 int c;
7807
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007808 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7809 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007810 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007811 c = -1;
7812 else
7813 {
7814 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007815 if (enc_utf8 && ScreenLinesUC[off] != 0)
7816 c = ScreenLinesUC[off];
7817 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007818 c = ScreenLines[off];
7819 }
7820 rettv->vval.v_number = c;
7821}
7822
7823/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007824 * "screenchars()" function
7825 */
7826 static void
7827f_screenchars(typval_T *argvars, typval_T *rettv)
7828{
7829 int row;
7830 int col;
7831 int off;
7832 int c;
7833 int i;
7834
7835 if (rettv_list_alloc(rettv) == FAIL)
7836 return;
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 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7848
7849 if (enc_utf8)
7850
7851 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7852 list_append_number(rettv->vval.v_list,
7853 (varnumber_T)ScreenLinesC[i][off]);
7854}
7855
7856/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007857 * "screencol()" function
7858 *
7859 * First column is 1 to be consistent with virtcol().
7860 */
7861 static void
7862f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7863{
7864 rettv->vval.v_number = screen_screencol() + 1;
7865}
7866
7867/*
7868 * "screenrow()" function
7869 */
7870 static void
7871f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7872{
7873 rettv->vval.v_number = screen_screenrow() + 1;
7874}
7875
7876/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007877 * "screenstring()" function
7878 */
7879 static void
7880f_screenstring(typval_T *argvars, typval_T *rettv)
7881{
7882 int row;
7883 int col;
7884 int off;
7885 int c;
7886 int i;
7887 char_u buf[MB_MAXBYTES + 1];
7888 int buflen = 0;
7889
7890 rettv->vval.v_string = NULL;
7891 rettv->v_type = VAR_STRING;
7892
7893 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7894 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7895 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7896 return;
7897
7898 off = LineOffset[row] + col;
7899 if (enc_utf8 && ScreenLinesUC[off] != 0)
7900 c = ScreenLinesUC[off];
7901 else
7902 c = ScreenLines[off];
7903 buflen += mb_char2bytes(c, buf);
7904
Bram Moolenaarf1387282021-03-22 17:11:15 +01007905 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007906 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7907 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7908
7909 buf[buflen] = NUL;
7910 rettv->vval.v_string = vim_strsave(buf);
7911}
7912
7913/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007914 * "search()" function
7915 */
7916 static void
7917f_search(typval_T *argvars, typval_T *rettv)
7918{
7919 int flags = 0;
7920
7921 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7922}
7923
7924/*
7925 * "searchdecl()" function
7926 */
7927 static void
7928f_searchdecl(typval_T *argvars, typval_T *rettv)
7929{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007930 int locally = TRUE;
7931 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007932 int error = FALSE;
7933 char_u *name;
7934
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007935 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007936
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007937 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007938 if (argvars[1].v_type != VAR_UNKNOWN)
7939 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007940 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007941 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007942 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007943 }
7944 if (!error && name != NULL)
7945 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7946 locally, thisblock, SEARCH_KEEP) == FAIL;
7947}
7948
7949/*
7950 * Used by searchpair() and searchpairpos()
7951 */
7952 static int
7953searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7954{
7955 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007956 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007957 int save_p_ws = p_ws;
7958 int dir;
7959 int flags = 0;
7960 char_u nbuf1[NUMBUFLEN];
7961 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007962 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007963 long lnum_stop = 0;
7964 long time_limit = 0;
7965
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007966 // Get the three pattern arguments: start, middle, end. Will result in an
7967 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007968 spat = tv_get_string_chk(&argvars[0]);
7969 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7970 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007971 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007972 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007973
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007974 // Handle the optional fourth argument: flags
7975 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007976 if (dir == 0)
7977 goto theend;
7978
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007979 // Don't accept SP_END or SP_SUBPAT.
7980 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007981 if ((flags & (SP_END | SP_SUBPAT)) != 0
7982 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7983 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007984 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007985 goto theend;
7986 }
7987
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007988 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007989 if (flags & SP_REPEAT)
7990 p_ws = FALSE;
7991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007992 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007993 if (argvars[3].v_type == VAR_UNKNOWN
7994 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007995 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007996 else
7997 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007998 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007999 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02008000
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008001 if (argvars[5].v_type != VAR_UNKNOWN)
8002 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008003 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008004 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008005 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008006 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008007 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008008 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008009#ifdef FEAT_RELTIME
8010 if (argvars[6].v_type != VAR_UNKNOWN)
8011 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008012 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008013 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008014 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008015 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008016 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008017 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008018 }
8019#endif
8020 }
8021 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008022
8023 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
8024 match_pos, lnum_stop, time_limit);
8025
8026theend:
8027 p_ws = save_p_ws;
8028
8029 return retval;
8030}
8031
8032/*
8033 * "searchpair()" function
8034 */
8035 static void
8036f_searchpair(typval_T *argvars, typval_T *rettv)
8037{
8038 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
8039}
8040
8041/*
8042 * "searchpairpos()" function
8043 */
8044 static void
8045f_searchpairpos(typval_T *argvars, typval_T *rettv)
8046{
8047 pos_T match_pos;
8048 int lnum = 0;
8049 int col = 0;
8050
8051 if (rettv_list_alloc(rettv) == FAIL)
8052 return;
8053
8054 if (searchpair_cmn(argvars, &match_pos) > 0)
8055 {
8056 lnum = match_pos.lnum;
8057 col = match_pos.col;
8058 }
8059
8060 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8061 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8062}
8063
8064/*
8065 * Search for a start/middle/end thing.
8066 * Used by searchpair(), see its documentation for the details.
8067 * Returns 0 or -1 for no match,
8068 */
8069 long
8070do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008071 char_u *spat, // start pattern
8072 char_u *mpat, // middle pattern
8073 char_u *epat, // end pattern
8074 int dir, // BACKWARD or FORWARD
8075 typval_T *skip, // skip expression
8076 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008077 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008078 linenr_T lnum_stop, // stop at this line if not zero
8079 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008080{
8081 char_u *save_cpo;
8082 char_u *pat, *pat2 = NULL, *pat3 = NULL;
8083 long retval = 0;
8084 pos_T pos;
8085 pos_T firstpos;
8086 pos_T foundpos;
8087 pos_T save_cursor;
8088 pos_T save_pos;
8089 int n;
8090 int r;
8091 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01008092 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008093 int err;
8094 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008095#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008096 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008097#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008098
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008099 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008100 save_cpo = p_cpo;
8101 p_cpo = empty_option;
8102
8103#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008104 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008105 profile_setlimit(time_limit, &tm);
8106#endif
8107
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008108 // Make two search patterns: start/end (pat2, for in nested pairs) and
8109 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02008110 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
8111 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008112 if (pat2 == NULL || pat3 == NULL)
8113 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008114 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008115 if (*mpat == NUL)
8116 STRCPY(pat3, pat2);
8117 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008118 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008119 spat, epat, mpat);
8120 if (flags & SP_START)
8121 options |= SEARCH_START;
8122
Bram Moolenaar48570482017-10-30 21:48:41 +01008123 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02008124 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01008125
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008126 save_cursor = curwin->w_cursor;
8127 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008128 CLEAR_POS(&firstpos);
8129 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008130 pat = pat3;
8131 for (;;)
8132 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008133 searchit_arg_T sia;
8134
Bram Moolenaara80faa82020-04-12 19:37:17 +02008135 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008136 sia.sa_stop_lnum = lnum_stop;
8137#ifdef FEAT_RELTIME
8138 sia.sa_tm = &tm;
8139#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01008140 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008141 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008142 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008143 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008144 break;
8145
8146 if (firstpos.lnum == 0)
8147 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008148 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008149 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008150 // Found the same position again. Can happen with a pattern that
8151 // has "\zs" at the end and searching backwards. Advance one
8152 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008153 if (dir == BACKWARD)
8154 decl(&pos);
8155 else
8156 incl(&pos);
8157 }
8158 foundpos = pos;
8159
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008160 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008161 options &= ~SEARCH_START;
8162
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008163 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01008164 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165 {
8166 save_pos = curwin->w_cursor;
8167 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01008168 err = FALSE;
8169 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008170 curwin->w_cursor = save_pos;
8171 if (err)
8172 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008173 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008174 curwin->w_cursor = save_cursor;
8175 retval = -1;
8176 break;
8177 }
8178 if (r)
8179 continue;
8180 }
8181
8182 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8183 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008184 // Found end when searching backwards or start when searching
8185 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008186 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008187 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008188 }
8189 else
8190 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008191 // Found end when searching forward or start when searching
8192 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008193 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008194 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008195 }
8196
8197 if (nest == 0)
8198 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008199 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008200 if (flags & SP_RETCOUNT)
8201 ++retval;
8202 else
8203 retval = pos.lnum;
8204 if (flags & SP_SETPCMARK)
8205 setpcmark();
8206 curwin->w_cursor = pos;
8207 if (!(flags & SP_REPEAT))
8208 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008209 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008210 }
8211 }
8212
8213 if (match_pos != NULL)
8214 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008215 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008216 match_pos->lnum = curwin->w_cursor.lnum;
8217 match_pos->col = curwin->w_cursor.col + 1;
8218 }
8219
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008220 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008221 if ((flags & SP_NOMOVE) || retval == 0)
8222 curwin->w_cursor = save_cursor;
8223
8224theend:
8225 vim_free(pat2);
8226 vim_free(pat3);
8227 if (p_cpo == empty_option)
8228 p_cpo = save_cpo;
8229 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008230 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008231 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008232 // If it's still empty it was changed and restored, need to restore in
8233 // the complicated way.
8234 if (*p_cpo == NUL)
8235 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008236 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008237 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008238
8239 return retval;
8240}
8241
8242/*
8243 * "searchpos()" function
8244 */
8245 static void
8246f_searchpos(typval_T *argvars, typval_T *rettv)
8247{
8248 pos_T match_pos;
8249 int lnum = 0;
8250 int col = 0;
8251 int n;
8252 int flags = 0;
8253
8254 if (rettv_list_alloc(rettv) == FAIL)
8255 return;
8256
8257 n = search_cmn(argvars, &match_pos, &flags);
8258 if (n > 0)
8259 {
8260 lnum = match_pos.lnum;
8261 col = match_pos.col;
8262 }
8263
8264 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8265 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8266 if (flags & SP_SUBPAT)
8267 list_append_number(rettv->vval.v_list, (varnumber_T)n);
8268}
8269
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008270/*
8271 * Set the cursor or mark position.
8272 * If 'charpos' is TRUE, then use the column number as a character offet.
8273 * Otherwise use the column number as a byte offset.
8274 */
8275 static void
8276set_position(typval_T *argvars, typval_T *rettv, int charpos)
8277{
8278 pos_T pos;
8279 int fnum;
8280 char_u *name;
8281 colnr_T curswant = -1;
8282
8283 rettv->vval.v_number = -1;
8284
8285 name = tv_get_string_chk(argvars);
8286 if (name != NULL)
8287 {
8288 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
8289 {
8290 if (pos.col != MAXCOL && --pos.col < 0)
8291 pos.col = 0;
8292 if ((name[0] == '.' && name[1] == NUL))
8293 {
8294 // set cursor; "fnum" is ignored
8295 curwin->w_cursor = pos;
8296 if (curswant >= 0)
8297 {
8298 curwin->w_curswant = curswant - 1;
8299 curwin->w_set_curswant = FALSE;
8300 }
8301 check_cursor();
8302 rettv->vval.v_number = 0;
8303 }
8304 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8305 {
8306 // set mark
8307 if (setmark_pos(name[1], &pos, fnum) == OK)
8308 rettv->vval.v_number = 0;
8309 }
8310 else
8311 emsg(_(e_invarg));
8312 }
8313 }
8314}
8315/*
8316 * "setcharpos()" function
8317 */
8318 static void
8319f_setcharpos(typval_T *argvars, typval_T *rettv)
8320{
8321 set_position(argvars, rettv, TRUE);
8322}
8323
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008324 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008325f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8326{
8327 dict_T *d;
8328 dictitem_T *di;
8329 char_u *csearch;
8330
8331 if (argvars[0].v_type != VAR_DICT)
8332 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008333 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008334 return;
8335 }
8336
8337 if ((d = argvars[0].vval.v_dict) != NULL)
8338 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008339 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008340 if (csearch != NULL)
8341 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008342 if (enc_utf8)
8343 {
8344 int pcc[MAX_MCO];
8345 int c = utfc_ptr2char(csearch, pcc);
8346
8347 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8348 }
8349 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008350 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008351 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008352 }
8353
8354 di = dict_find(d, (char_u *)"forward", -1);
8355 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008356 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008357 ? FORWARD : BACKWARD);
8358
8359 di = dict_find(d, (char_u *)"until", -1);
8360 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008361 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362 }
8363}
8364
8365/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008366 * "setcursorcharpos" function
8367 */
8368 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008369f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008370{
8371 set_cursorpos(argvars, rettv, TRUE);
8372}
8373
8374/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008375 * "setenv()" function
8376 */
8377 static void
8378f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8379{
8380 char_u namebuf[NUMBUFLEN];
8381 char_u valbuf[NUMBUFLEN];
8382 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8383
8384 if (argvars[1].v_type == VAR_SPECIAL
8385 && argvars[1].vval.v_number == VVAL_NULL)
8386 vim_unsetenv(name);
8387 else
8388 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8389}
8390
8391/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008392 * "setfperm({fname}, {mode})" function
8393 */
8394 static void
8395f_setfperm(typval_T *argvars, typval_T *rettv)
8396{
8397 char_u *fname;
8398 char_u modebuf[NUMBUFLEN];
8399 char_u *mode_str;
8400 int i;
8401 int mask;
8402 int mode = 0;
8403
8404 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008405 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008406 if (fname == NULL)
8407 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008408 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008409 if (mode_str == NULL)
8410 return;
8411 if (STRLEN(mode_str) != 9)
8412 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008413 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008414 return;
8415 }
8416
8417 mask = 1;
8418 for (i = 8; i >= 0; --i)
8419 {
8420 if (mode_str[i] != '-')
8421 mode |= mask;
8422 mask = mask << 1;
8423 }
8424 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8425}
8426
8427/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008428 * "setpos()" function
8429 */
8430 static void
8431f_setpos(typval_T *argvars, typval_T *rettv)
8432{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008433 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008434}
8435
8436/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008437 * Translate a register type string to the yank type and block length
8438 */
8439 static int
8440get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8441{
8442 char_u *stropt = *pp;
8443 switch (*stropt)
8444 {
8445 case 'v': case 'c': // character-wise selection
8446 *yank_type = MCHAR;
8447 break;
8448 case 'V': case 'l': // line-wise selection
8449 *yank_type = MLINE;
8450 break;
8451 case 'b': case Ctrl_V: // block-wise selection
8452 *yank_type = MBLOCK;
8453 if (VIM_ISDIGIT(stropt[1]))
8454 {
8455 ++stropt;
8456 *block_len = getdigits(&stropt) - 1;
8457 --stropt;
8458 }
8459 break;
8460 default:
8461 return FAIL;
8462 }
8463 *pp = stropt;
8464 return OK;
8465}
8466
8467/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008468 * "setreg()" function
8469 */
8470 static void
8471f_setreg(typval_T *argvars, typval_T *rettv)
8472{
8473 int regname;
8474 char_u *strregname;
8475 char_u *stropt;
8476 char_u *strval;
8477 int append;
8478 char_u yank_type;
8479 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008480 typval_T *regcontents;
8481 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008482
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008483 pointreg = 0;
8484 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008485 block_len = -1;
8486 yank_type = MAUTO;
8487 append = FALSE;
8488
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008489 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008490 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008491
8492 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008493 return; // type error; errmsg already given
Bram Moolenaar418a29f2021-02-10 22:23:41 +01008494 if (in_vim9script() && STRLEN(strregname) > 1)
8495 {
8496 semsg(_(e_register_name_must_be_one_char_str), strregname);
8497 return;
8498 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008499 regname = *strregname;
8500 if (regname == 0 || regname == '@')
8501 regname = '"';
8502
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008503 if (argvars[1].v_type == VAR_DICT)
8504 {
8505 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008506 dictitem_T *di;
8507
8508 if (d == NULL || d->dv_hashtab.ht_used == 0)
8509 {
8510 // Empty dict, clear the register (like setreg(0, []))
8511 char_u *lstval[2] = {NULL, NULL};
8512 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8513 return;
8514 }
8515
8516 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008517 if (di != NULL)
8518 regcontents = &di->di_tv;
8519
8520 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8521 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008522 {
8523 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8524
8525 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008526 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008527 semsg(_(e_invargval), "value");
8528 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008529 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008530 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008531
8532 if (regname == '"')
8533 {
8534 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8535 if (stropt != NULL)
8536 {
8537 pointreg = *stropt;
8538 regname = pointreg;
8539 }
8540 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008541 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008542 pointreg = regname;
8543 }
8544 else
8545 regcontents = &argvars[1];
8546
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008547 if (argvars[2].v_type != VAR_UNKNOWN)
8548 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008549 if (yank_type != MAUTO)
8550 {
8551 semsg(_(e_toomanyarg), "setreg");
8552 return;
8553 }
8554
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008555 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008556 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008557 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008558 for (; *stropt != NUL; ++stropt)
8559 switch (*stropt)
8560 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008561 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562 append = TRUE;
8563 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008564 default:
8565 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008566 }
8567 }
8568
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008569 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008570 {
8571 char_u **lstval;
8572 char_u **allocval;
8573 char_u buf[NUMBUFLEN];
8574 char_u **curval;
8575 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008576 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008577 listitem_T *li;
8578 int len;
8579
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008580 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008581 len = ll == NULL ? 0 : ll->lv_len;
8582
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008583 // First half: use for pointers to result lines; second half: use for
8584 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008585 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008586 if (lstval == NULL)
8587 return;
8588 curval = lstval;
8589 allocval = lstval + len + 2;
8590 curallocval = allocval;
8591
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008592 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008593 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008594 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008595 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008596 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008597 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008598 if (strval == NULL)
8599 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008600 if (strval == buf)
8601 {
8602 // Need to make a copy, next tv_get_string_buf_chk() will
8603 // overwrite the string.
8604 strval = vim_strsave(buf);
8605 if (strval == NULL)
8606 goto free_lstval;
8607 *curallocval++ = strval;
8608 }
8609 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008610 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008611 }
8612 *curval++ = NULL;
8613
8614 write_reg_contents_lst(regname, lstval, -1,
8615 append, yank_type, block_len);
8616free_lstval:
8617 while (curallocval > allocval)
8618 vim_free(*--curallocval);
8619 vim_free(lstval);
8620 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008621 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008622 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008623 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008624 if (strval == NULL)
8625 return;
8626 write_reg_contents_ex(regname, strval, -1,
8627 append, yank_type, block_len);
8628 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008629 if (pointreg != 0)
8630 get_yank_register(pointreg, TRUE);
8631
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008632 rettv->vval.v_number = 0;
8633}
8634
8635/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008636 * "settagstack()" function
8637 */
8638 static void
8639f_settagstack(typval_T *argvars, typval_T *rettv)
8640{
8641 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8642 win_T *wp;
8643 dict_T *d;
8644 int action = 'r';
8645
8646 rettv->vval.v_number = -1;
8647
8648 // first argument: window number or id
8649 wp = find_win_by_nr_or_id(&argvars[0]);
8650 if (wp == NULL)
8651 return;
8652
8653 // second argument: dict with items to set in the tag stack
8654 if (argvars[1].v_type != VAR_DICT)
8655 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008656 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008657 return;
8658 }
8659 d = argvars[1].vval.v_dict;
8660 if (d == NULL)
8661 return;
8662
8663 // third argument: action - 'a' for append and 'r' for replace.
8664 // default is to replace the stack.
8665 if (argvars[2].v_type == VAR_UNKNOWN)
8666 action = 'r';
8667 else if (argvars[2].v_type == VAR_STRING)
8668 {
8669 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008670 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008671 if (actstr == NULL)
8672 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008673 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8674 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008675 action = *actstr;
8676 else
8677 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008678 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008679 return;
8680 }
8681 }
8682 else
8683 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008684 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008685 return;
8686 }
8687
8688 if (set_tagstack(wp, d, action) == OK)
8689 rettv->vval.v_number = 0;
8690}
8691
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008692#ifdef FEAT_CRYPT
8693/*
8694 * "sha256({string})" function
8695 */
8696 static void
8697f_sha256(typval_T *argvars, typval_T *rettv)
8698{
8699 char_u *p;
8700
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008701 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008702 rettv->vval.v_string = vim_strsave(
8703 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8704 rettv->v_type = VAR_STRING;
8705}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008706#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008707
8708/*
8709 * "shellescape({string})" function
8710 */
8711 static void
8712f_shellescape(typval_T *argvars, typval_T *rettv)
8713{
Bram Moolenaar20615522017-06-05 18:46:26 +02008714 int do_special = non_zero_arg(&argvars[1]);
8715
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008716 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008717 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008718 rettv->v_type = VAR_STRING;
8719}
8720
8721/*
8722 * shiftwidth() function
8723 */
8724 static void
8725f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8726{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008727 rettv->vval.v_number = 0;
8728
8729 if (argvars[0].v_type != VAR_UNKNOWN)
8730 {
8731 long col;
8732
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008733 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008734 if (col < 0)
8735 return; // type error; errmsg already given
8736#ifdef FEAT_VARTABS
8737 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8738 return;
8739#endif
8740 }
8741
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008742 rettv->vval.v_number = get_sw_value(curbuf);
8743}
8744
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008745#ifdef FEAT_FLOAT
8746/*
8747 * "sin()" function
8748 */
8749 static void
8750f_sin(typval_T *argvars, typval_T *rettv)
8751{
8752 float_T f = 0.0;
8753
8754 rettv->v_type = VAR_FLOAT;
8755 if (get_float_arg(argvars, &f) == OK)
8756 rettv->vval.v_float = sin(f);
8757 else
8758 rettv->vval.v_float = 0.0;
8759}
8760
8761/*
8762 * "sinh()" function
8763 */
8764 static void
8765f_sinh(typval_T *argvars, typval_T *rettv)
8766{
8767 float_T f = 0.0;
8768
8769 rettv->v_type = VAR_FLOAT;
8770 if (get_float_arg(argvars, &f) == OK)
8771 rettv->vval.v_float = sinh(f);
8772 else
8773 rettv->vval.v_float = 0.0;
8774}
8775#endif
8776
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008777/*
8778 * "soundfold({word})" function
8779 */
8780 static void
8781f_soundfold(typval_T *argvars, typval_T *rettv)
8782{
8783 char_u *s;
8784
8785 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008786 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008787#ifdef FEAT_SPELL
8788 rettv->vval.v_string = eval_soundfold(s);
8789#else
8790 rettv->vval.v_string = vim_strsave(s);
8791#endif
8792}
8793
8794/*
8795 * "spellbadword()" function
8796 */
8797 static void
8798f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8799{
8800 char_u *word = (char_u *)"";
8801 hlf_T attr = HLF_COUNT;
8802 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008803#ifdef FEAT_SPELL
8804 int wo_spell_save = curwin->w_p_spell;
8805
8806 if (!curwin->w_p_spell)
8807 {
8808 did_set_spelllang(curwin);
8809 curwin->w_p_spell = TRUE;
8810 }
8811
8812 if (*curwin->w_s->b_p_spl == NUL)
8813 {
8814 emsg(_(e_no_spell));
8815 curwin->w_p_spell = wo_spell_save;
8816 return;
8817 }
8818#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008819
8820 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008821 {
8822#ifdef FEAT_SPELL
8823 curwin->w_p_spell = wo_spell_save;
8824#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008825 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008826 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008827
8828#ifdef FEAT_SPELL
8829 if (argvars[0].v_type == VAR_UNKNOWN)
8830 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008831 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008832 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8833 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008834 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008836 curwin->w_set_curswant = TRUE;
8837 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008838 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008839 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008841 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008842 int capcol = -1;
8843
8844 if (str != NULL)
8845 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008846 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008847 while (*str != NUL)
8848 {
8849 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8850 if (attr != HLF_COUNT)
8851 {
8852 word = str;
8853 break;
8854 }
8855 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008856 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008857 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008858 }
8859 }
8860 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008861 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008862#endif
8863
8864 list_append_string(rettv->vval.v_list, word, len);
8865 list_append_string(rettv->vval.v_list, (char_u *)(
8866 attr == HLF_SPB ? "bad" :
8867 attr == HLF_SPR ? "rare" :
8868 attr == HLF_SPL ? "local" :
8869 attr == HLF_SPC ? "caps" :
8870 ""), -1);
8871}
8872
8873/*
8874 * "spellsuggest()" function
8875 */
8876 static void
8877f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8878{
8879#ifdef FEAT_SPELL
8880 char_u *str;
8881 int typeerr = FALSE;
8882 int maxcount;
8883 garray_T ga;
8884 int i;
8885 listitem_T *li;
8886 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008887 int wo_spell_save = curwin->w_p_spell;
8888
8889 if (!curwin->w_p_spell)
8890 {
8891 did_set_spelllang(curwin);
8892 curwin->w_p_spell = TRUE;
8893 }
8894
8895 if (*curwin->w_s->b_p_spl == NUL)
8896 {
8897 emsg(_(e_no_spell));
8898 curwin->w_p_spell = wo_spell_save;
8899 return;
8900 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008901#endif
8902
8903 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008904 {
8905#ifdef FEAT_SPELL
8906 curwin->w_p_spell = wo_spell_save;
8907#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008908 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008909 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008910
8911#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008912 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008913 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008914 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008915 if (argvars[1].v_type != VAR_UNKNOWN)
8916 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008917 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008918 if (maxcount <= 0)
8919 return;
8920 if (argvars[2].v_type != VAR_UNKNOWN)
8921 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008922 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008923 if (typeerr)
8924 return;
8925 }
8926 }
8927 else
8928 maxcount = 25;
8929
8930 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8931
8932 for (i = 0; i < ga.ga_len; ++i)
8933 {
8934 str = ((char_u **)ga.ga_data)[i];
8935
8936 li = listitem_alloc();
8937 if (li == NULL)
8938 vim_free(str);
8939 else
8940 {
8941 li->li_tv.v_type = VAR_STRING;
8942 li->li_tv.v_lock = 0;
8943 li->li_tv.vval.v_string = str;
8944 list_append(rettv->vval.v_list, li);
8945 }
8946 }
8947 ga_clear(&ga);
8948 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008949 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008950#endif
8951}
8952
8953 static void
8954f_split(typval_T *argvars, typval_T *rettv)
8955{
8956 char_u *str;
8957 char_u *end;
8958 char_u *pat = NULL;
8959 regmatch_T regmatch;
8960 char_u patbuf[NUMBUFLEN];
8961 char_u *save_cpo;
8962 int match;
8963 colnr_T col = 0;
8964 int keepempty = FALSE;
8965 int typeerr = FALSE;
8966
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008967 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008968 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008969 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008970
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008971 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008972 if (argvars[1].v_type != VAR_UNKNOWN)
8973 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008974 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008975 if (pat == NULL)
8976 typeerr = TRUE;
8977 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008978 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008979 }
8980 if (pat == NULL || *pat == NUL)
8981 pat = (char_u *)"[\\x01- ]\\+";
8982
8983 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008984 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008985 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008986 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008987
8988 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8989 if (regmatch.regprog != NULL)
8990 {
8991 regmatch.rm_ic = FALSE;
8992 while (*str != NUL || keepempty)
8993 {
8994 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008995 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008996 else
8997 match = vim_regexec_nl(&regmatch, str, col);
8998 if (match)
8999 end = regmatch.startp[0];
9000 else
9001 end = str + STRLEN(str);
9002 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
9003 && *str != NUL && match && end < regmatch.endp[0]))
9004 {
9005 if (list_append_string(rettv->vval.v_list, str,
9006 (int)(end - str)) == FAIL)
9007 break;
9008 }
9009 if (!match)
9010 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01009011 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009012 if (regmatch.endp[0] > str)
9013 col = 0;
9014 else
Bram Moolenaar13505972019-01-24 15:04:48 +01009015 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009016 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009017 str = regmatch.endp[0];
9018 }
9019
9020 vim_regfree(regmatch.regprog);
9021 }
9022
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02009023theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009024 p_cpo = save_cpo;
9025}
9026
9027#ifdef FEAT_FLOAT
9028/*
9029 * "sqrt()" function
9030 */
9031 static void
9032f_sqrt(typval_T *argvars, typval_T *rettv)
9033{
9034 float_T f = 0.0;
9035
9036 rettv->v_type = VAR_FLOAT;
9037 if (get_float_arg(argvars, &f) == OK)
9038 rettv->vval.v_float = sqrt(f);
9039 else
9040 rettv->vval.v_float = 0.0;
9041}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009042#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009043
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009044#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01009045/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009046 * "str2float()" function
9047 */
9048 static void
9049f_str2float(typval_T *argvars, typval_T *rettv)
9050{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009051 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009052 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009053
Bram Moolenaar08243d22017-01-10 16:12:29 +01009054 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009055 p = skipwhite(p + 1);
9056 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01009057 if (isneg)
9058 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009059 rettv->v_type = VAR_FLOAT;
9060}
9061#endif
9062
9063/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02009064 * "str2list()" function
9065 */
9066 static void
9067f_str2list(typval_T *argvars, typval_T *rettv)
9068{
9069 char_u *p;
9070 int utf8 = FALSE;
9071
9072 if (rettv_list_alloc(rettv) == FAIL)
9073 return;
9074
9075 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02009076 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02009077
9078 p = tv_get_string(&argvars[0]);
9079
9080 if (has_mbyte || utf8)
9081 {
9082 int (*ptr2len)(char_u *);
9083 int (*ptr2char)(char_u *);
9084
9085 if (utf8 || enc_utf8)
9086 {
9087 ptr2len = utf_ptr2len;
9088 ptr2char = utf_ptr2char;
9089 }
9090 else
9091 {
9092 ptr2len = mb_ptr2len;
9093 ptr2char = mb_ptr2char;
9094 }
9095
9096 for ( ; *p != NUL; p += (*ptr2len)(p))
9097 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
9098 }
9099 else
9100 for ( ; *p != NUL; ++p)
9101 list_append_number(rettv->vval.v_list, *p);
9102}
9103
9104/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009105 * "str2nr()" function
9106 */
9107 static void
9108f_str2nr(typval_T *argvars, typval_T *rettv)
9109{
9110 int base = 10;
9111 char_u *p;
9112 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009113 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01009114 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009115
9116 if (argvars[1].v_type != VAR_UNKNOWN)
9117 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009118 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009119 if (base != 2 && base != 8 && base != 10 && base != 16)
9120 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009121 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009122 return;
9123 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02009124 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009125 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009126 }
9127
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01009128 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009129 isneg = (*p == '-');
9130 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009131 p = skipwhite(p + 1);
9132 switch (base)
9133 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009134 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02009135 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009136 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009137 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02009138 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
9139 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01009140 if (isneg)
9141 rettv->vval.v_number = -n;
9142 else
9143 rettv->vval.v_number = n;
9144
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009145}
9146
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009147/*
9148 * "strgetchar()" function
9149 */
9150 static void
9151f_strgetchar(typval_T *argvars, typval_T *rettv)
9152{
9153 char_u *str;
9154 int len;
9155 int error = FALSE;
9156 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01009157 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009158
9159 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009160 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009161 if (str == NULL)
9162 return;
9163 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009164 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009165 if (error)
9166 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009167
Bram Moolenaar13505972019-01-24 15:04:48 +01009168 while (charidx >= 0 && byteidx < len)
9169 {
9170 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009171 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009172 rettv->vval.v_number = mb_ptr2char(str + byteidx);
9173 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009174 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009175 --charidx;
9176 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009177 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009178}
9179
9180/*
9181 * "stridx()" function
9182 */
9183 static void
9184f_stridx(typval_T *argvars, typval_T *rettv)
9185{
9186 char_u buf[NUMBUFLEN];
9187 char_u *needle;
9188 char_u *haystack;
9189 char_u *save_haystack;
9190 char_u *pos;
9191 int start_idx;
9192
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009193 needle = tv_get_string_chk(&argvars[1]);
9194 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009195 rettv->vval.v_number = -1;
9196 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009197 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009198
9199 if (argvars[2].v_type != VAR_UNKNOWN)
9200 {
9201 int error = FALSE;
9202
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009203 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009204 if (error || start_idx >= (int)STRLEN(haystack))
9205 return;
9206 if (start_idx >= 0)
9207 haystack += start_idx;
9208 }
9209
9210 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9211 if (pos != NULL)
9212 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
9213}
9214
9215/*
9216 * "string()" function
9217 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009218 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009219f_string(typval_T *argvars, typval_T *rettv)
9220{
9221 char_u *tofree;
9222 char_u numbuf[NUMBUFLEN];
9223
9224 rettv->v_type = VAR_STRING;
9225 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
9226 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009227 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009228 if (rettv->vval.v_string != NULL && tofree == NULL)
9229 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
9230}
9231
9232/*
9233 * "strlen()" function
9234 */
9235 static void
9236f_strlen(typval_T *argvars, typval_T *rettv)
9237{
9238 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009239 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009240}
9241
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01009242 static void
9243strchar_common(typval_T *argvars, typval_T *rettv, int skipcc)
9244{
9245 char_u *s = tv_get_string(&argvars[0]);
9246 varnumber_T len = 0;
9247 int (*func_mb_ptr2char_adv)(char_u **pp);
9248
9249 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
9250 while (*s != NUL)
9251 {
9252 func_mb_ptr2char_adv(&s);
9253 ++len;
9254 }
9255 rettv->vval.v_number = len;
9256}
9257
9258/*
9259 * "strcharlen()" function
9260 */
9261 static void
9262f_strcharlen(typval_T *argvars, typval_T *rettv)
9263{
9264 strchar_common(argvars, rettv, TRUE);
9265}
9266
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009267/*
9268 * "strchars()" function
9269 */
9270 static void
9271f_strchars(typval_T *argvars, typval_T *rettv)
9272{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009273 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009274
9275 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009276 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009277 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02009278 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009279 else
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01009280 strchar_common(argvars, rettv, skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009281}
9282
9283/*
9284 * "strdisplaywidth()" function
9285 */
9286 static void
9287f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
9288{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009289 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009290 int col = 0;
9291
9292 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009293 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009294
9295 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
9296}
9297
9298/*
9299 * "strwidth()" function
9300 */
9301 static void
9302f_strwidth(typval_T *argvars, typval_T *rettv)
9303{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009304 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009305
Bram Moolenaar13505972019-01-24 15:04:48 +01009306 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009307}
9308
9309/*
9310 * "strcharpart()" function
9311 */
9312 static void
9313f_strcharpart(typval_T *argvars, typval_T *rettv)
9314{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009315 char_u *p;
9316 int nchar;
9317 int nbyte = 0;
9318 int charlen;
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009319 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009320 int len = 0;
9321 int slen;
9322 int error = FALSE;
9323
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009324 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009325 slen = (int)STRLEN(p);
9326
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009327 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009328 if (!error)
9329 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009330 if (argvars[2].v_type != VAR_UNKNOWN
9331 && argvars[3].v_type != VAR_UNKNOWN)
9332 {
9333 skipcc = tv_get_bool(&argvars[3]);
9334 if (skipcc < 0 || skipcc > 1)
9335 {
9336 semsg(_(e_using_number_as_bool_nr), skipcc);
9337 return;
9338 }
9339 }
9340
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009341 if (nchar > 0)
9342 while (nchar > 0 && nbyte < slen)
9343 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009344 if (skipcc)
9345 nbyte += mb_ptr2len(p + nbyte);
9346 else
9347 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009348 --nchar;
9349 }
9350 else
9351 nbyte = nchar;
9352 if (argvars[2].v_type != VAR_UNKNOWN)
9353 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009354 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009355 while (charlen > 0 && nbyte + len < slen)
9356 {
9357 int off = nbyte + len;
9358
9359 if (off < 0)
9360 len += 1;
9361 else
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009362 {
9363 if (skipcc)
9364 len += mb_ptr2len(p + off);
9365 else
9366 len += MB_CPTR2LEN(p + off);
9367 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009368 --charlen;
9369 }
9370 }
9371 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009372 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009373 }
9374
9375 /*
9376 * Only return the overlap between the specified part and the actual
9377 * string.
9378 */
9379 if (nbyte < 0)
9380 {
9381 len += nbyte;
9382 nbyte = 0;
9383 }
9384 else if (nbyte > slen)
9385 nbyte = slen;
9386 if (len < 0)
9387 len = 0;
9388 else if (nbyte + len > slen)
9389 len = slen - nbyte;
9390
9391 rettv->v_type = VAR_STRING;
9392 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009393}
9394
9395/*
9396 * "strpart()" function
9397 */
9398 static void
9399f_strpart(typval_T *argvars, typval_T *rettv)
9400{
9401 char_u *p;
9402 int n;
9403 int len;
9404 int slen;
9405 int error = FALSE;
9406
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009407 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009408 slen = (int)STRLEN(p);
9409
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009410 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009411 if (error)
9412 len = 0;
9413 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009414 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009415 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009416 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009417
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009418 // Only return the overlap between the specified part and the actual
9419 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009420 if (n < 0)
9421 {
9422 len += n;
9423 n = 0;
9424 }
9425 else if (n > slen)
9426 n = slen;
9427 if (len < 0)
9428 len = 0;
9429 else if (n + len > slen)
9430 len = slen - n;
9431
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009432 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9433 {
9434 int off;
9435
9436 // length in characters
9437 for (off = n; off < slen && len > 0; --len)
9438 off += mb_ptr2len(p + off);
9439 len = off - n;
9440 }
9441
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009442 rettv->v_type = VAR_STRING;
9443 rettv->vval.v_string = vim_strnsave(p + n, len);
9444}
9445
9446/*
9447 * "strridx()" function
9448 */
9449 static void
9450f_strridx(typval_T *argvars, typval_T *rettv)
9451{
9452 char_u buf[NUMBUFLEN];
9453 char_u *needle;
9454 char_u *haystack;
9455 char_u *rest;
9456 char_u *lastmatch = NULL;
9457 int haystack_len, end_idx;
9458
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009459 needle = tv_get_string_chk(&argvars[1]);
9460 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009461
9462 rettv->vval.v_number = -1;
9463 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009464 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009465
9466 haystack_len = (int)STRLEN(haystack);
9467 if (argvars[2].v_type != VAR_UNKNOWN)
9468 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009469 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009470 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009471 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009472 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009473 }
9474 else
9475 end_idx = haystack_len;
9476
9477 if (*needle == NUL)
9478 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009479 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009480 lastmatch = haystack + end_idx;
9481 }
9482 else
9483 {
9484 for (rest = haystack; *rest != '\0'; ++rest)
9485 {
9486 rest = (char_u *)strstr((char *)rest, (char *)needle);
9487 if (rest == NULL || rest > haystack + end_idx)
9488 break;
9489 lastmatch = rest;
9490 }
9491 }
9492
9493 if (lastmatch == NULL)
9494 rettv->vval.v_number = -1;
9495 else
9496 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9497}
9498
9499/*
9500 * "strtrans()" function
9501 */
9502 static void
9503f_strtrans(typval_T *argvars, typval_T *rettv)
9504{
9505 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009506 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009507}
9508
9509/*
9510 * "submatch()" function
9511 */
9512 static void
9513f_submatch(typval_T *argvars, typval_T *rettv)
9514{
9515 int error = FALSE;
9516 int no;
9517 int retList = 0;
9518
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009519 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009520 if (error)
9521 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009522 if (no < 0 || no >= NSUBEXP)
9523 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009524 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009525 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009526 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009527 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009528 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009529 if (error)
9530 return;
9531
9532 if (retList == 0)
9533 {
9534 rettv->v_type = VAR_STRING;
9535 rettv->vval.v_string = reg_submatch(no);
9536 }
9537 else
9538 {
9539 rettv->v_type = VAR_LIST;
9540 rettv->vval.v_list = reg_submatch_list(no);
9541 }
9542}
9543
9544/*
9545 * "substitute()" function
9546 */
9547 static void
9548f_substitute(typval_T *argvars, typval_T *rettv)
9549{
9550 char_u patbuf[NUMBUFLEN];
9551 char_u subbuf[NUMBUFLEN];
9552 char_u flagsbuf[NUMBUFLEN];
9553
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009554 char_u *str = tv_get_string_chk(&argvars[0]);
9555 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009556 char_u *sub = NULL;
9557 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009558 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009559
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009560 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9561 expr = &argvars[2];
9562 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009563 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009564
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009565 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009566 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9567 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009568 rettv->vval.v_string = NULL;
9569 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009570 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009571}
9572
9573/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009574 * "swapinfo(swap_filename)" function
9575 */
9576 static void
9577f_swapinfo(typval_T *argvars, typval_T *rettv)
9578{
9579 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009580 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009581}
9582
9583/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009584 * "swapname(expr)" function
9585 */
9586 static void
9587f_swapname(typval_T *argvars, typval_T *rettv)
9588{
9589 buf_T *buf;
9590
9591 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009592 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009593 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9594 || buf->b_ml.ml_mfp->mf_fname == NULL)
9595 rettv->vval.v_string = NULL;
9596 else
9597 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9598}
9599
9600/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009601 * "synID(lnum, col, trans)" function
9602 */
9603 static void
9604f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9605{
9606 int id = 0;
9607#ifdef FEAT_SYN_HL
9608 linenr_T lnum;
9609 colnr_T col;
9610 int trans;
9611 int transerr = FALSE;
9612
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009613 lnum = tv_get_lnum(argvars); // -1 on type error
9614 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009615 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009616
9617 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9618 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9619 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9620#endif
9621
9622 rettv->vval.v_number = id;
9623}
9624
9625/*
9626 * "synIDattr(id, what [, mode])" function
9627 */
9628 static void
9629f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9630{
9631 char_u *p = NULL;
9632#ifdef FEAT_SYN_HL
9633 int id;
9634 char_u *what;
9635 char_u *mode;
9636 char_u modebuf[NUMBUFLEN];
9637 int modec;
9638
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009639 id = (int)tv_get_number(&argvars[0]);
9640 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009641 if (argvars[2].v_type != VAR_UNKNOWN)
9642 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009643 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009644 modec = TOLOWER_ASC(mode[0]);
9645 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009646 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009647 }
9648 else
9649 {
9650#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9651 if (USE_24BIT)
9652 modec = 'g';
9653 else
9654#endif
9655 if (t_colors > 1)
9656 modec = 'c';
9657 else
9658 modec = 't';
9659 }
9660
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009661 switch (TOLOWER_ASC(what[0]))
9662 {
9663 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009664 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009665 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009666 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009667 p = highlight_has_attr(id, HL_BOLD, modec);
9668 break;
9669
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009670 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009671 p = highlight_color(id, what, modec);
9672 break;
9673
9674 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009675 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009676 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009677 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009678 p = highlight_has_attr(id, HL_ITALIC, modec);
9679 break;
9680
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009681 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009682 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009683 break;
9684
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009685 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009686 p = highlight_has_attr(id, HL_INVERSE, modec);
9687 break;
9688
9689 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009690 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009691 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009692 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009693 else if (TOLOWER_ASC(what[1]) == 't' &&
9694 TOLOWER_ASC(what[2]) == 'r')
9695 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009696 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009697 p = highlight_has_attr(id, HL_STANDOUT, modec);
9698 break;
9699
9700 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009701 if (TOLOWER_ASC(what[1]) == 'l') // ul
9702 p = highlight_color(id, what, modec);
9703 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009704 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009705 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9706 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009707 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009708 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9709 break;
9710 }
9711
9712 if (p != NULL)
9713 p = vim_strsave(p);
9714#endif
9715 rettv->v_type = VAR_STRING;
9716 rettv->vval.v_string = p;
9717}
9718
9719/*
9720 * "synIDtrans(id)" function
9721 */
9722 static void
9723f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9724{
9725 int id;
9726
9727#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009728 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009729
9730 if (id > 0)
9731 id = syn_get_final_id(id);
9732 else
9733#endif
9734 id = 0;
9735
9736 rettv->vval.v_number = id;
9737}
9738
9739/*
9740 * "synconcealed(lnum, col)" function
9741 */
9742 static void
9743f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9744{
9745#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9746 linenr_T lnum;
9747 colnr_T col;
9748 int syntax_flags = 0;
9749 int cchar;
9750 int matchid = 0;
9751 char_u str[NUMBUFLEN];
9752#endif
9753
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009754 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009755
9756#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009757 lnum = tv_get_lnum(argvars); // -1 on type error
9758 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009759
Bram Moolenaara80faa82020-04-12 19:37:17 +02009760 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009761
9762 if (rettv_list_alloc(rettv) != FAIL)
9763 {
9764 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9765 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9766 && curwin->w_p_cole > 0)
9767 {
9768 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9769 syntax_flags = get_syntax_info(&matchid);
9770
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009771 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009772 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9773 {
9774 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009775 if (cchar == NUL && curwin->w_p_cole == 1)
Bram Moolenaareed9d462021-02-15 20:38:25 +01009776 cchar = (curwin->w_lcs_chars.conceal == NUL) ? ' '
9777 : curwin->w_lcs_chars.conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009778 if (cchar != NUL)
9779 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009780 if (has_mbyte)
9781 (*mb_char2bytes)(cchar, str);
9782 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009783 str[0] = cchar;
9784 }
9785 }
9786 }
9787
9788 list_append_number(rettv->vval.v_list,
9789 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009790 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009791 list_append_string(rettv->vval.v_list, str, -1);
9792 list_append_number(rettv->vval.v_list, matchid);
9793 }
9794#endif
9795}
9796
9797/*
9798 * "synstack(lnum, col)" function
9799 */
9800 static void
9801f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9802{
9803#ifdef FEAT_SYN_HL
9804 linenr_T lnum;
9805 colnr_T col;
9806 int i;
9807 int id;
9808#endif
9809
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009810 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009811
9812#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009813 lnum = tv_get_lnum(argvars); // -1 on type error
9814 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009815
9816 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9817 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9818 && rettv_list_alloc(rettv) != FAIL)
9819 {
9820 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9821 for (i = 0; ; ++i)
9822 {
9823 id = syn_get_stack_item(i);
9824 if (id < 0)
9825 break;
9826 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9827 break;
9828 }
9829 }
9830#endif
9831}
9832
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009833/*
9834 * "tabpagebuflist()" function
9835 */
9836 static void
9837f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9838{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009839 tabpage_T *tp;
9840 win_T *wp = NULL;
9841
9842 if (argvars[0].v_type == VAR_UNKNOWN)
9843 wp = firstwin;
9844 else
9845 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009846 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009847 if (tp != NULL)
9848 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9849 }
9850 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9851 {
9852 for (; wp != NULL; wp = wp->w_next)
9853 if (list_append_number(rettv->vval.v_list,
9854 wp->w_buffer->b_fnum) == FAIL)
9855 break;
9856 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009857}
9858
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009859/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009860 * "tagfiles()" function
9861 */
9862 static void
9863f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9864{
9865 char_u *fname;
9866 tagname_T tn;
9867 int first;
9868
9869 if (rettv_list_alloc(rettv) == FAIL)
9870 return;
9871 fname = alloc(MAXPATHL);
9872 if (fname == NULL)
9873 return;
9874
9875 for (first = TRUE; ; first = FALSE)
9876 if (get_tagfname(&tn, first, fname) == FAIL
9877 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9878 break;
9879 tagname_free(&tn);
9880 vim_free(fname);
9881}
9882
9883/*
9884 * "taglist()" function
9885 */
9886 static void
9887f_taglist(typval_T *argvars, typval_T *rettv)
9888{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009889 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009890 char_u *tag_pattern;
9891
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009892 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009893
9894 rettv->vval.v_number = FALSE;
9895 if (*tag_pattern == NUL)
9896 return;
9897
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009898 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009899 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009900 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009901 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009902}
9903
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009904#ifdef FEAT_FLOAT
9905/*
9906 * "tan()" function
9907 */
9908 static void
9909f_tan(typval_T *argvars, typval_T *rettv)
9910{
9911 float_T f = 0.0;
9912
9913 rettv->v_type = VAR_FLOAT;
9914 if (get_float_arg(argvars, &f) == OK)
9915 rettv->vval.v_float = tan(f);
9916 else
9917 rettv->vval.v_float = 0.0;
9918}
9919
9920/*
9921 * "tanh()" function
9922 */
9923 static void
9924f_tanh(typval_T *argvars, typval_T *rettv)
9925{
9926 float_T f = 0.0;
9927
9928 rettv->v_type = VAR_FLOAT;
9929 if (get_float_arg(argvars, &f) == OK)
9930 rettv->vval.v_float = tanh(f);
9931 else
9932 rettv->vval.v_float = 0.0;
9933}
9934#endif
9935
9936/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009937 * "tolower(string)" function
9938 */
9939 static void
9940f_tolower(typval_T *argvars, typval_T *rettv)
9941{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009942 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009943 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009944}
9945
9946/*
9947 * "toupper(string)" function
9948 */
9949 static void
9950f_toupper(typval_T *argvars, typval_T *rettv)
9951{
9952 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009953 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009954}
9955
9956/*
9957 * "tr(string, fromstr, tostr)" function
9958 */
9959 static void
9960f_tr(typval_T *argvars, typval_T *rettv)
9961{
9962 char_u *in_str;
9963 char_u *fromstr;
9964 char_u *tostr;
9965 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009966 int inlen;
9967 int fromlen;
9968 int tolen;
9969 int idx;
9970 char_u *cpstr;
9971 int cplen;
9972 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009973 char_u buf[NUMBUFLEN];
9974 char_u buf2[NUMBUFLEN];
9975 garray_T ga;
9976
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009977 in_str = tv_get_string(&argvars[0]);
9978 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9979 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009980
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009981 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009982 rettv->v_type = VAR_STRING;
9983 rettv->vval.v_string = NULL;
9984 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009985 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009986 ga_init2(&ga, (int)sizeof(char), 80);
9987
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009988 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009989 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009990 if (STRLEN(fromstr) != STRLEN(tostr))
9991 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009992error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009993 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009994 ga_clear(&ga);
9995 return;
9996 }
9997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009998 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009999 while (*in_str != NUL)
10000 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010001 if (has_mbyte)
10002 {
10003 inlen = (*mb_ptr2len)(in_str);
10004 cpstr = in_str;
10005 cplen = inlen;
10006 idx = 0;
10007 for (p = fromstr; *p != NUL; p += fromlen)
10008 {
10009 fromlen = (*mb_ptr2len)(p);
10010 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
10011 {
10012 for (p = tostr; *p != NUL; p += tolen)
10013 {
10014 tolen = (*mb_ptr2len)(p);
10015 if (idx-- == 0)
10016 {
10017 cplen = tolen;
10018 cpstr = p;
10019 break;
10020 }
10021 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010022 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010023 goto error;
10024 break;
10025 }
10026 ++idx;
10027 }
10028
10029 if (first && cpstr == in_str)
10030 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010031 // Check that fromstr and tostr have the same number of
10032 // (multi-byte) characters. Done only once when a character
10033 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010034 first = FALSE;
10035 for (p = tostr; *p != NUL; p += tolen)
10036 {
10037 tolen = (*mb_ptr2len)(p);
10038 --idx;
10039 }
10040 if (idx != 0)
10041 goto error;
10042 }
10043
10044 (void)ga_grow(&ga, cplen);
10045 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
10046 ga.ga_len += cplen;
10047
10048 in_str += inlen;
10049 }
10050 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010051 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010052 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010053 p = vim_strchr(fromstr, *in_str);
10054 if (p != NULL)
10055 ga_append(&ga, tostr[p - fromstr]);
10056 else
10057 ga_append(&ga, *in_str);
10058 ++in_str;
10059 }
10060 }
10061
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010062 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010063 (void)ga_grow(&ga, 1);
10064 ga_append(&ga, NUL);
10065
10066 rettv->vval.v_string = ga.ga_data;
10067}
10068
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010069/*
10070 * "trim({expr})" function
10071 */
10072 static void
10073f_trim(typval_T *argvars, typval_T *rettv)
10074{
10075 char_u buf1[NUMBUFLEN];
10076 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010077 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010078 char_u *mask = NULL;
10079 char_u *tail;
10080 char_u *prev;
10081 char_u *p;
10082 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010083 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010084
10085 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010086 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010087 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010088 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010089
10090 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010091 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010092 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010093
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010094 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010095 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010096 int error = 0;
10097
10098 // leading or trailing characters to trim
10099 dir = (int)tv_get_number_chk(&argvars[2], &error);
10100 if (error)
10101 return;
10102 if (dir < 0 || dir > 2)
10103 {
10104 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
10105 return;
10106 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010107 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010108 }
10109
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010110 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010111 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010112 // Trim leading characters
10113 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010114 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010115 c1 = PTR2CHAR(head);
10116 if (mask == NULL)
10117 {
10118 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010119 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010120 }
10121 else
10122 {
10123 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10124 if (c1 == PTR2CHAR(p))
10125 break;
10126 if (*p == NUL)
10127 break;
10128 }
10129 MB_PTR_ADV(head);
10130 }
10131 }
10132
10133 tail = head + STRLEN(head);
10134 if (dir == 0 || dir == 2)
10135 {
10136 // Trim trailing characters
10137 for (; tail > head; tail = prev)
10138 {
10139 prev = tail;
10140 MB_PTR_BACK(head, prev);
10141 c1 = PTR2CHAR(prev);
10142 if (mask == NULL)
10143 {
10144 if (c1 > ' ' && c1 != 0xa0)
10145 break;
10146 }
10147 else
10148 {
10149 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10150 if (c1 == PTR2CHAR(p))
10151 break;
10152 if (*p == NUL)
10153 break;
10154 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010155 }
10156 }
Bram Moolenaardf44a272020-06-07 20:49:05 +020010157 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010158}
10159
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010160#ifdef FEAT_FLOAT
10161/*
10162 * "trunc({float})" function
10163 */
10164 static void
10165f_trunc(typval_T *argvars, typval_T *rettv)
10166{
10167 float_T f = 0.0;
10168
10169 rettv->v_type = VAR_FLOAT;
10170 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010171 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010172 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
10173 else
10174 rettv->vval.v_float = 0.0;
10175}
10176#endif
10177
10178/*
10179 * "type(expr)" function
10180 */
10181 static void
10182f_type(typval_T *argvars, typval_T *rettv)
10183{
10184 int n = -1;
10185
10186 switch (argvars[0].v_type)
10187 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010188 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
10189 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010190 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010191 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
10192 case VAR_LIST: n = VAR_TYPE_LIST; break;
10193 case VAR_DICT: n = VAR_TYPE_DICT; break;
10194 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
10195 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
10196 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020010197 case VAR_JOB: n = VAR_TYPE_JOB; break;
10198 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010199 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010200 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +020010201 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010202 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +010010203 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010204 n = -1;
10205 break;
10206 }
10207 rettv->vval.v_number = n;
10208}
10209
10210/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010211 * "virtcol(string)" function
10212 */
10213 static void
10214f_virtcol(typval_T *argvars, typval_T *rettv)
10215{
10216 colnr_T vcol = 0;
10217 pos_T *fp;
10218 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010219 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010220
Bram Moolenaar6f02b002021-01-10 20:22:54 +010010221 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010222 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
10223 && fnum == curbuf->b_fnum)
10224 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010225 // Limit the column to a valid value, getvvcol() doesn't check.
10226 if (fp->col < 0)
10227 fp->col = 0;
10228 else
10229 {
10230 len = (int)STRLEN(ml_get(fp->lnum));
10231 if (fp->col > len)
10232 fp->col = len;
10233 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010234 getvvcol(curwin, fp, NULL, NULL, &vcol);
10235 ++vcol;
10236 }
10237
10238 rettv->vval.v_number = vcol;
10239}
10240
10241/*
10242 * "visualmode()" function
10243 */
10244 static void
10245f_visualmode(typval_T *argvars, typval_T *rettv)
10246{
10247 char_u str[2];
10248
10249 rettv->v_type = VAR_STRING;
10250 str[0] = curbuf->b_visual_mode_eval;
10251 str[1] = NUL;
10252 rettv->vval.v_string = vim_strsave(str);
10253
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010254 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010255 if (non_zero_arg(&argvars[0]))
10256 curbuf->b_visual_mode_eval = NUL;
10257}
10258
10259/*
10260 * "wildmenumode()" function
10261 */
10262 static void
10263f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10264{
10265#ifdef FEAT_WILDMENU
10266 if (wild_menu_showing)
10267 rettv->vval.v_number = 1;
10268#endif
10269}
10270
10271/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +010010272 * "windowsversion()" function
10273 */
10274 static void
10275f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10276{
10277 rettv->v_type = VAR_STRING;
10278 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
10279}
10280
10281/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010282 * "wordcount()" function
10283 */
10284 static void
10285f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
10286{
10287 if (rettv_dict_alloc(rettv) == FAIL)
10288 return;
10289 cursor_pos_info(rettv->vval.v_dict);
10290}
10291
10292/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010293 * "xor(expr, expr)" function
10294 */
10295 static void
10296f_xor(typval_T *argvars, typval_T *rettv)
10297{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010298 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
10299 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010300}
10301
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010302#endif // FEAT_EVAL