blob: 8ea48f9bd564134cfe000aff66463ad079aeafea [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 Moolenaar5b8cabf2021-04-02 18:55:57 +0200742 {"assert_nobeep", 1, 2, FEARG_1, NULL,
743 ret_number_bool, f_assert_nobeep},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200744 {"assert_notequal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100745 ret_number_bool, f_assert_notequal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200746 {"assert_notmatch", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100747 ret_number_bool, f_assert_notmatch},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200748 {"assert_report", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100749 ret_number_bool, f_assert_report},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200750 {"assert_true", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100751 ret_number_bool, f_assert_true},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200752 {"atan", 1, 1, FEARG_1, NULL,
753 ret_float, FLOAT_FUNC(f_atan)},
754 {"atan2", 2, 2, FEARG_1, NULL,
755 ret_float, FLOAT_FUNC(f_atan2)},
756 {"balloon_gettext", 0, 0, 0, NULL,
757 ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100758#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100759 f_balloon_gettext
760#else
761 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100762#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100763 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200764 {"balloon_show", 1, 1, FEARG_1, NULL,
765 ret_void,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100766#ifdef FEAT_BEVAL
767 f_balloon_show
768#else
769 NULL
770#endif
771 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200772 {"balloon_split", 1, 1, FEARG_1, NULL,
773 ret_list_string,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100774#if defined(FEAT_BEVAL_TERM)
775 f_balloon_split
776#else
777 NULL
778#endif
779 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200780 {"browse", 4, 4, 0, NULL,
781 ret_string, f_browse},
782 {"browsedir", 2, 2, 0, NULL,
783 ret_string, f_browsedir},
784 {"bufadd", 1, 1, FEARG_1, NULL,
785 ret_number, f_bufadd},
786 {"bufexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100787 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200788 {"buffer_exists", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100789 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200790 {"buffer_name", 0, 1, FEARG_1, NULL, // obsolete
791 ret_string, f_bufname},
792 {"buffer_number", 0, 1, FEARG_1, NULL, // obsolete
793 ret_number, f_bufnr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200794 {"buflisted", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100795 ret_number_bool, f_buflisted},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200796 {"bufload", 1, 1, FEARG_1, NULL,
797 ret_void, f_bufload},
798 {"bufloaded", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100799 ret_number_bool, f_bufloaded},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200800 {"bufname", 0, 1, FEARG_1, NULL,
801 ret_string, f_bufname},
802 {"bufnr", 0, 2, FEARG_1, NULL,
803 ret_number, f_bufnr},
804 {"bufwinid", 1, 1, FEARG_1, NULL,
805 ret_number, f_bufwinid},
806 {"bufwinnr", 1, 1, FEARG_1, NULL,
807 ret_number, f_bufwinnr},
808 {"byte2line", 1, 1, FEARG_1, NULL,
809 ret_number, f_byte2line},
810 {"byteidx", 2, 2, FEARG_1, NULL,
811 ret_number, f_byteidx},
812 {"byteidxcomp", 2, 2, FEARG_1, NULL,
813 ret_number, f_byteidxcomp},
814 {"call", 2, 3, FEARG_1, NULL,
815 ret_any, f_call},
816 {"ceil", 1, 1, FEARG_1, NULL,
817 ret_float, FLOAT_FUNC(f_ceil)},
818 {"ch_canread", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100819 ret_number_bool, JOB_FUNC(f_ch_canread)},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200820 {"ch_close", 1, 1, FEARG_1, NULL,
821 ret_void, JOB_FUNC(f_ch_close)},
822 {"ch_close_in", 1, 1, FEARG_1, NULL,
823 ret_void, JOB_FUNC(f_ch_close_in)},
824 {"ch_evalexpr", 2, 3, FEARG_1, NULL,
825 ret_any, JOB_FUNC(f_ch_evalexpr)},
826 {"ch_evalraw", 2, 3, FEARG_1, NULL,
827 ret_any, JOB_FUNC(f_ch_evalraw)},
828 {"ch_getbufnr", 2, 2, FEARG_1, NULL,
829 ret_number, JOB_FUNC(f_ch_getbufnr)},
830 {"ch_getjob", 1, 1, FEARG_1, NULL,
831 ret_job, JOB_FUNC(f_ch_getjob)},
832 {"ch_info", 1, 1, FEARG_1, NULL,
833 ret_dict_any, JOB_FUNC(f_ch_info)},
834 {"ch_log", 1, 2, FEARG_1, NULL,
835 ret_void, JOB_FUNC(f_ch_log)},
836 {"ch_logfile", 1, 2, FEARG_1, NULL,
837 ret_void, JOB_FUNC(f_ch_logfile)},
838 {"ch_open", 1, 2, FEARG_1, NULL,
839 ret_channel, JOB_FUNC(f_ch_open)},
840 {"ch_read", 1, 2, FEARG_1, NULL,
841 ret_string, JOB_FUNC(f_ch_read)},
842 {"ch_readblob", 1, 2, FEARG_1, NULL,
843 ret_blob, JOB_FUNC(f_ch_readblob)},
844 {"ch_readraw", 1, 2, FEARG_1, NULL,
845 ret_string, JOB_FUNC(f_ch_readraw)},
846 {"ch_sendexpr", 2, 3, FEARG_1, NULL,
847 ret_void, JOB_FUNC(f_ch_sendexpr)},
848 {"ch_sendraw", 2, 3, FEARG_1, NULL,
849 ret_void, JOB_FUNC(f_ch_sendraw)},
850 {"ch_setoptions", 2, 2, FEARG_1, NULL,
851 ret_void, JOB_FUNC(f_ch_setoptions)},
852 {"ch_status", 1, 2, FEARG_1, NULL,
853 ret_string, JOB_FUNC(f_ch_status)},
854 {"changenr", 0, 0, 0, NULL,
855 ret_number, f_changenr},
856 {"char2nr", 1, 2, FEARG_1, NULL,
857 ret_number, f_char2nr},
858 {"charclass", 1, 1, FEARG_1, NULL,
859 ret_number, f_charclass},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100860 {"charcol", 1, 1, FEARG_1, NULL,
861 ret_number, f_charcol},
Bram Moolenaar17793ef2020-12-28 12:56:58 +0100862 {"charidx", 2, 3, FEARG_1, NULL,
863 ret_number, f_charidx},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200864 {"chdir", 1, 1, FEARG_1, NULL,
865 ret_string, f_chdir},
866 {"cindent", 1, 1, FEARG_1, NULL,
867 ret_number, f_cindent},
868 {"clearmatches", 0, 1, FEARG_1, NULL,
869 ret_void, f_clearmatches},
870 {"col", 1, 1, FEARG_1, NULL,
871 ret_number, f_col},
872 {"complete", 2, 2, FEARG_2, NULL,
873 ret_void, f_complete},
874 {"complete_add", 1, 1, FEARG_1, NULL,
875 ret_number, f_complete_add},
876 {"complete_check", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100877 ret_number_bool, f_complete_check},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200878 {"complete_info", 0, 1, FEARG_1, NULL,
879 ret_dict_any, f_complete_info},
880 {"confirm", 1, 4, FEARG_1, NULL,
881 ret_number, f_confirm},
882 {"copy", 1, 1, FEARG_1, NULL,
883 ret_first_arg, f_copy},
884 {"cos", 1, 1, FEARG_1, NULL,
885 ret_float, FLOAT_FUNC(f_cos)},
886 {"cosh", 1, 1, FEARG_1, NULL,
887 ret_float, FLOAT_FUNC(f_cosh)},
888 {"count", 2, 4, FEARG_1, NULL,
889 ret_number, f_count},
890 {"cscope_connection",0,3, 0, NULL,
891 ret_number, f_cscope_connection},
892 {"cursor", 1, 3, FEARG_1, NULL,
893 ret_number, f_cursor},
894 {"debugbreak", 1, 1, FEARG_1, NULL,
895 ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100896#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100897 f_debugbreak
898#else
899 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200900#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100901 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200902 {"deepcopy", 1, 2, FEARG_1, NULL,
903 ret_first_arg, f_deepcopy},
904 {"delete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100905 ret_number_bool, f_delete},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200906 {"deletebufline", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100907 ret_number_bool, f_deletebufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200908 {"did_filetype", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100909 ret_number_bool, f_did_filetype},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200910 {"diff_filler", 1, 1, FEARG_1, NULL,
911 ret_number, f_diff_filler},
912 {"diff_hlID", 2, 2, FEARG_1, NULL,
913 ret_number, f_diff_hlID},
914 {"echoraw", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100915 ret_void, f_echoraw},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200916 {"empty", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100917 ret_number_bool, f_empty},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200918 {"environ", 0, 0, 0, NULL,
919 ret_dict_string, f_environ},
920 {"escape", 2, 2, FEARG_1, NULL,
921 ret_string, f_escape},
922 {"eval", 1, 1, FEARG_1, NULL,
923 ret_any, f_eval},
924 {"eventhandler", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100925 ret_number_bool, f_eventhandler},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200926 {"executable", 1, 1, FEARG_1, NULL,
927 ret_number, f_executable},
928 {"execute", 1, 2, FEARG_1, NULL,
929 ret_string, f_execute},
930 {"exepath", 1, 1, FEARG_1, NULL,
931 ret_string, f_exepath},
932 {"exists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100933 ret_number_bool, f_exists},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200934 {"exp", 1, 1, FEARG_1, NULL,
935 ret_float, FLOAT_FUNC(f_exp)},
936 {"expand", 1, 3, FEARG_1, NULL,
937 ret_any, f_expand},
938 {"expandcmd", 1, 1, FEARG_1, NULL,
939 ret_string, f_expandcmd},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100940 {"extend", 2, 3, FEARG_1, arg23_extend,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200941 ret_first_arg, f_extend},
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100942 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
943 ret_first_cont, f_extendnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200944 {"feedkeys", 1, 2, FEARG_1, NULL,
945 ret_void, f_feedkeys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200946 {"file_readable", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100947 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200948 {"filereadable", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100949 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200950 {"filewritable", 1, 1, FEARG_1, NULL,
951 ret_number, f_filewritable},
952 {"filter", 2, 2, FEARG_1, NULL,
953 ret_first_arg, f_filter},
954 {"finddir", 1, 3, FEARG_1, NULL,
955 ret_string, f_finddir},
956 {"findfile", 1, 3, FEARG_1, NULL,
957 ret_string, f_findfile},
958 {"flatten", 1, 2, FEARG_1, NULL,
959 ret_list_any, f_flatten},
Bram Moolenaar3b690062021-02-01 20:14:51 +0100960 {"flattennew", 1, 2, FEARG_1, NULL,
961 ret_list_any, f_flattennew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200962 {"float2nr", 1, 1, FEARG_1, NULL,
963 ret_number, FLOAT_FUNC(f_float2nr)},
964 {"floor", 1, 1, FEARG_1, NULL,
965 ret_float, FLOAT_FUNC(f_floor)},
966 {"fmod", 2, 2, FEARG_1, NULL,
967 ret_float, FLOAT_FUNC(f_fmod)},
968 {"fnameescape", 1, 1, FEARG_1, NULL,
969 ret_string, f_fnameescape},
970 {"fnamemodify", 2, 2, FEARG_1, NULL,
971 ret_string, f_fnamemodify},
972 {"foldclosed", 1, 1, FEARG_1, NULL,
973 ret_number, f_foldclosed},
974 {"foldclosedend", 1, 1, FEARG_1, NULL,
975 ret_number, f_foldclosedend},
976 {"foldlevel", 1, 1, FEARG_1, NULL,
977 ret_number, f_foldlevel},
978 {"foldtext", 0, 0, 0, NULL,
979 ret_string, f_foldtext},
980 {"foldtextresult", 1, 1, FEARG_1, NULL,
981 ret_string, f_foldtextresult},
982 {"foreground", 0, 0, 0, NULL,
983 ret_void, f_foreground},
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100984 {"fullcommand", 1, 1, FEARG_1, arg1_string,
985 ret_string, f_fullcommand},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200986 {"funcref", 1, 3, FEARG_1, NULL,
987 ret_func_any, f_funcref},
988 {"function", 1, 3, FEARG_1, NULL,
989 ret_f_function, f_function},
990 {"garbagecollect", 0, 1, 0, NULL,
991 ret_void, f_garbagecollect},
992 {"get", 2, 3, FEARG_1, NULL,
993 ret_any, f_get},
994 {"getbufinfo", 0, 1, FEARG_1, NULL,
995 ret_list_dict_any, f_getbufinfo},
996 {"getbufline", 2, 3, FEARG_1, NULL,
997 ret_list_string, f_getbufline},
998 {"getbufvar", 2, 3, FEARG_1, NULL,
999 ret_any, f_getbufvar},
1000 {"getchangelist", 0, 1, FEARG_1, NULL,
1001 ret_list_any, f_getchangelist},
1002 {"getchar", 0, 1, 0, NULL,
1003 ret_number, f_getchar},
1004 {"getcharmod", 0, 0, 0, NULL,
1005 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001006 {"getcharpos", 1, 1, FEARG_1, NULL,
1007 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001008 {"getcharsearch", 0, 0, 0, NULL,
1009 ret_dict_any, f_getcharsearch},
1010 {"getcmdline", 0, 0, 0, NULL,
1011 ret_string, f_getcmdline},
1012 {"getcmdpos", 0, 0, 0, NULL,
1013 ret_number, f_getcmdpos},
1014 {"getcmdtype", 0, 0, 0, NULL,
1015 ret_string, f_getcmdtype},
1016 {"getcmdwintype", 0, 0, 0, NULL,
1017 ret_string, f_getcmdwintype},
1018 {"getcompletion", 2, 3, FEARG_1, NULL,
1019 ret_list_string, f_getcompletion},
1020 {"getcurpos", 0, 1, FEARG_1, NULL,
1021 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001022 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
1023 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001024 {"getcwd", 0, 2, FEARG_1, NULL,
1025 ret_string, f_getcwd},
1026 {"getenv", 1, 1, FEARG_1, NULL,
Bram Moolenaar7ad67d12021-03-10 16:08:26 +01001027 ret_any, f_getenv},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001028 {"getfontname", 0, 1, 0, NULL,
1029 ret_string, f_getfontname},
1030 {"getfperm", 1, 1, FEARG_1, NULL,
1031 ret_string, f_getfperm},
1032 {"getfsize", 1, 1, FEARG_1, NULL,
1033 ret_number, f_getfsize},
1034 {"getftime", 1, 1, FEARG_1, NULL,
1035 ret_number, f_getftime},
1036 {"getftype", 1, 1, FEARG_1, NULL,
1037 ret_string, f_getftype},
1038 {"getimstatus", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001039 ret_number_bool, f_getimstatus},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001040 {"getjumplist", 0, 2, FEARG_1, NULL,
1041 ret_list_any, f_getjumplist},
1042 {"getline", 1, 2, FEARG_1, NULL,
1043 ret_f_getline, f_getline},
1044 {"getloclist", 1, 2, 0, NULL,
1045 ret_list_or_dict_1, f_getloclist},
1046 {"getmarklist", 0, 1, FEARG_1, NULL,
1047 ret_list_dict_any, f_getmarklist},
1048 {"getmatches", 0, 1, 0, NULL,
1049 ret_list_dict_any, f_getmatches},
1050 {"getmousepos", 0, 0, 0, NULL,
1051 ret_dict_number, f_getmousepos},
1052 {"getpid", 0, 0, 0, NULL,
1053 ret_number, f_getpid},
1054 {"getpos", 1, 1, FEARG_1, NULL,
1055 ret_list_number, f_getpos},
1056 {"getqflist", 0, 1, 0, NULL,
1057 ret_list_or_dict_0, f_getqflist},
1058 {"getreg", 0, 3, FEARG_1, NULL,
1059 ret_getreg, f_getreg},
1060 {"getreginfo", 0, 1, FEARG_1, NULL,
1061 ret_dict_any, f_getreginfo},
1062 {"getregtype", 0, 1, FEARG_1, NULL,
1063 ret_string, f_getregtype},
1064 {"gettabinfo", 0, 1, FEARG_1, NULL,
1065 ret_list_dict_any, f_gettabinfo},
1066 {"gettabvar", 2, 3, FEARG_1, NULL,
1067 ret_any, f_gettabvar},
1068 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1069 ret_any, f_gettabwinvar},
1070 {"gettagstack", 0, 1, FEARG_1, NULL,
1071 ret_dict_any, f_gettagstack},
1072 {"gettext", 1, 1, FEARG_1, NULL,
1073 ret_string, f_gettext},
1074 {"getwininfo", 0, 1, FEARG_1, NULL,
1075 ret_list_dict_any, f_getwininfo},
1076 {"getwinpos", 0, 1, FEARG_1, NULL,
1077 ret_list_number, f_getwinpos},
1078 {"getwinposx", 0, 0, 0, NULL,
1079 ret_number, f_getwinposx},
1080 {"getwinposy", 0, 0, 0, NULL,
1081 ret_number, f_getwinposy},
1082 {"getwinvar", 2, 3, FEARG_1, NULL,
1083 ret_any, f_getwinvar},
1084 {"glob", 1, 4, FEARG_1, NULL,
1085 ret_any, f_glob},
1086 {"glob2regpat", 1, 1, FEARG_1, NULL,
1087 ret_string, f_glob2regpat},
1088 {"globpath", 2, 5, FEARG_2, NULL,
1089 ret_any, f_globpath},
1090 {"has", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001091 ret_number_bool, f_has},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001092 {"has_key", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001093 ret_number_bool, f_has_key},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001094 {"haslocaldir", 0, 2, FEARG_1, NULL,
1095 ret_number, f_haslocaldir},
1096 {"hasmapto", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001097 ret_number_bool, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001098 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1099 ret_number, f_hlID},
1100 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001101 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001102 {"histadd", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001103 ret_number_bool, f_histadd},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001104 {"histdel", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001105 ret_number_bool, f_histdel},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001106 {"histget", 1, 2, FEARG_1, NULL,
1107 ret_string, f_histget},
1108 {"histnr", 1, 1, FEARG_1, NULL,
1109 ret_number, f_histnr},
1110 {"hlID", 1, 1, FEARG_1, NULL,
1111 ret_number, f_hlID},
1112 {"hlexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001113 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001114 {"hostname", 0, 0, 0, NULL,
1115 ret_string, f_hostname},
1116 {"iconv", 3, 3, FEARG_1, NULL,
1117 ret_string, f_iconv},
1118 {"indent", 1, 1, FEARG_1, NULL,
1119 ret_number, f_indent},
1120 {"index", 2, 4, FEARG_1, NULL,
1121 ret_number, f_index},
1122 {"input", 1, 3, FEARG_1, NULL,
1123 ret_string, f_input},
1124 {"inputdialog", 1, 3, FEARG_1, NULL,
1125 ret_string, f_inputdialog},
1126 {"inputlist", 1, 1, FEARG_1, NULL,
1127 ret_number, f_inputlist},
1128 {"inputrestore", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001129 ret_number_bool, f_inputrestore},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001130 {"inputsave", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001131 ret_number_bool, f_inputsave},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001132 {"inputsecret", 1, 2, FEARG_1, NULL,
1133 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001134 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001135 ret_first_arg, f_insert},
1136 {"interrupt", 0, 0, 0, NULL,
1137 ret_void, f_interrupt},
1138 {"invert", 1, 1, FEARG_1, NULL,
1139 ret_number, f_invert},
1140 {"isdirectory", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001141 ret_number_bool, f_isdirectory},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001142 {"isinf", 1, 1, FEARG_1, NULL,
1143 ret_number, MATH_FUNC(f_isinf)},
1144 {"islocked", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001145 ret_number_bool, f_islocked},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001146 {"isnan", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001147 ret_number_bool, MATH_FUNC(f_isnan)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001148 {"items", 1, 1, FEARG_1, NULL,
1149 ret_list_any, f_items},
1150 {"job_getchannel", 1, 1, FEARG_1, NULL,
1151 ret_channel, JOB_FUNC(f_job_getchannel)},
1152 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001153 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001154 {"job_setoptions", 2, 2, FEARG_1, NULL,
1155 ret_void, JOB_FUNC(f_job_setoptions)},
1156 {"job_start", 1, 2, FEARG_1, NULL,
1157 ret_job, JOB_FUNC(f_job_start)},
1158 {"job_status", 1, 1, FEARG_1, NULL,
1159 ret_string, JOB_FUNC(f_job_status)},
1160 {"job_stop", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001161 ret_number_bool, JOB_FUNC(f_job_stop)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001162 {"join", 1, 2, FEARG_1, NULL,
1163 ret_string, f_join},
1164 {"js_decode", 1, 1, FEARG_1, NULL,
1165 ret_any, f_js_decode},
1166 {"js_encode", 1, 1, FEARG_1, NULL,
1167 ret_string, f_js_encode},
1168 {"json_decode", 1, 1, FEARG_1, NULL,
1169 ret_any, f_json_decode},
1170 {"json_encode", 1, 1, FEARG_1, NULL,
1171 ret_string, f_json_encode},
1172 {"keys", 1, 1, FEARG_1, NULL,
1173 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001174 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1175 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001176 {"len", 1, 1, FEARG_1, NULL,
1177 ret_number, f_len},
1178 {"libcall", 3, 3, FEARG_3, NULL,
1179 ret_string, f_libcall},
1180 {"libcallnr", 3, 3, FEARG_3, NULL,
1181 ret_number, f_libcallnr},
1182 {"line", 1, 2, FEARG_1, NULL,
1183 ret_number, f_line},
1184 {"line2byte", 1, 1, FEARG_1, NULL,
1185 ret_number, f_line2byte},
1186 {"lispindent", 1, 1, FEARG_1, NULL,
1187 ret_number, f_lispindent},
1188 {"list2str", 1, 2, FEARG_1, NULL,
1189 ret_string, f_list2str},
1190 {"listener_add", 1, 2, FEARG_2, NULL,
1191 ret_number, f_listener_add},
1192 {"listener_flush", 0, 1, FEARG_1, NULL,
1193 ret_void, f_listener_flush},
1194 {"listener_remove", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001195 ret_number_bool, f_listener_remove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001196 {"localtime", 0, 0, 0, NULL,
1197 ret_number, f_localtime},
1198 {"log", 1, 1, FEARG_1, NULL,
1199 ret_float, FLOAT_FUNC(f_log)},
1200 {"log10", 1, 1, FEARG_1, NULL,
1201 ret_float, FLOAT_FUNC(f_log10)},
1202 {"luaeval", 1, 2, FEARG_1, NULL,
1203 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001204#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001205 f_luaeval
1206#else
1207 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001208#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001209 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001210 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001211 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001212 {"maparg", 1, 4, FEARG_1, NULL,
1213 ret_maparg, f_maparg},
1214 {"mapcheck", 1, 3, FEARG_1, NULL,
1215 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001216 {"mapnew", 2, 2, FEARG_1, NULL,
1217 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001218 {"mapset", 3, 3, FEARG_1, NULL,
1219 ret_void, f_mapset},
1220 {"match", 2, 4, FEARG_1, NULL,
1221 ret_any, f_match},
1222 {"matchadd", 2, 5, FEARG_1, NULL,
1223 ret_number, f_matchadd},
1224 {"matchaddpos", 2, 5, FEARG_1, NULL,
1225 ret_number, f_matchaddpos},
1226 {"matcharg", 1, 1, FEARG_1, NULL,
1227 ret_list_string, f_matcharg},
1228 {"matchdelete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001229 ret_number_bool, f_matchdelete},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001230 {"matchend", 2, 4, FEARG_1, NULL,
1231 ret_number, f_matchend},
1232 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1233 ret_list_string, f_matchfuzzy},
1234 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1235 ret_list_any, f_matchfuzzypos},
1236 {"matchlist", 2, 4, FEARG_1, NULL,
1237 ret_list_string, f_matchlist},
1238 {"matchstr", 2, 4, FEARG_1, NULL,
1239 ret_string, f_matchstr},
1240 {"matchstrpos", 2, 4, FEARG_1, NULL,
1241 ret_list_any, f_matchstrpos},
1242 {"max", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001243 ret_number, f_max},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001244 {"menu_info", 1, 2, FEARG_1, NULL,
1245 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001246#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001247 f_menu_info
1248#else
1249 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001250#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001251 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001252 {"min", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001253 ret_number, f_min},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001254 {"mkdir", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001255 ret_number_bool, f_mkdir},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001256 {"mode", 0, 1, FEARG_1, NULL,
1257 ret_string, f_mode},
1258 {"mzeval", 1, 1, FEARG_1, NULL,
1259 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001260#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001261 f_mzeval
1262#else
1263 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001264#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001265 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001266 {"nextnonblank", 1, 1, FEARG_1, NULL,
1267 ret_number, f_nextnonblank},
1268 {"nr2char", 1, 2, FEARG_1, NULL,
1269 ret_string, f_nr2char},
1270 {"or", 2, 2, FEARG_1, NULL,
1271 ret_number, f_or},
1272 {"pathshorten", 1, 2, FEARG_1, NULL,
1273 ret_string, f_pathshorten},
1274 {"perleval", 1, 1, FEARG_1, NULL,
1275 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001276#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001277 f_perleval
1278#else
1279 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001280#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001281 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001282 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1283 ret_number, PROP_FUNC(f_popup_atcursor)},
1284 {"popup_beval", 2, 2, FEARG_1, NULL,
1285 ret_number, PROP_FUNC(f_popup_beval)},
1286 {"popup_clear", 0, 1, 0, NULL,
1287 ret_void, PROP_FUNC(f_popup_clear)},
1288 {"popup_close", 1, 2, FEARG_1, NULL,
1289 ret_void, PROP_FUNC(f_popup_close)},
1290 {"popup_create", 2, 2, FEARG_1, NULL,
1291 ret_number, PROP_FUNC(f_popup_create)},
1292 {"popup_dialog", 2, 2, FEARG_1, NULL,
1293 ret_number, PROP_FUNC(f_popup_dialog)},
1294 {"popup_filter_menu", 2, 2, 0, NULL,
1295 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1296 {"popup_filter_yesno", 2, 2, 0, NULL,
1297 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1298 {"popup_findinfo", 0, 0, 0, NULL,
1299 ret_number, PROP_FUNC(f_popup_findinfo)},
1300 {"popup_findpreview", 0, 0, 0, NULL,
1301 ret_number, PROP_FUNC(f_popup_findpreview)},
1302 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1303 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1304 {"popup_getpos", 1, 1, FEARG_1, NULL,
1305 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1306 {"popup_hide", 1, 1, FEARG_1, NULL,
1307 ret_void, PROP_FUNC(f_popup_hide)},
1308 {"popup_list", 0, 0, 0, NULL,
1309 ret_list_number, PROP_FUNC(f_popup_list)},
1310 {"popup_locate", 2, 2, 0, NULL,
1311 ret_number, PROP_FUNC(f_popup_locate)},
1312 {"popup_menu", 2, 2, FEARG_1, NULL,
1313 ret_number, PROP_FUNC(f_popup_menu)},
1314 {"popup_move", 2, 2, FEARG_1, NULL,
1315 ret_void, PROP_FUNC(f_popup_move)},
1316 {"popup_notification", 2, 2, FEARG_1, NULL,
1317 ret_number, PROP_FUNC(f_popup_notification)},
1318 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1319 ret_void, PROP_FUNC(f_popup_setoptions)},
1320 {"popup_settext", 2, 2, FEARG_1, NULL,
1321 ret_void, PROP_FUNC(f_popup_settext)},
1322 {"popup_show", 1, 1, FEARG_1, NULL,
1323 ret_void, PROP_FUNC(f_popup_show)},
1324 {"pow", 2, 2, FEARG_1, NULL,
1325 ret_float, FLOAT_FUNC(f_pow)},
1326 {"prevnonblank", 1, 1, FEARG_1, NULL,
1327 ret_number, f_prevnonblank},
1328 {"printf", 1, 19, FEARG_2, NULL,
1329 ret_string, f_printf},
1330 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1331 ret_string, JOB_FUNC(f_prompt_getprompt)},
1332 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1333 ret_void, JOB_FUNC(f_prompt_setcallback)},
1334 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1335 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1336 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1337 ret_void, JOB_FUNC(f_prompt_setprompt)},
1338 {"prop_add", 3, 3, FEARG_1, NULL,
1339 ret_void, PROP_FUNC(f_prop_add)},
1340 {"prop_clear", 1, 3, FEARG_1, NULL,
1341 ret_void, PROP_FUNC(f_prop_clear)},
1342 {"prop_find", 1, 2, FEARG_1, NULL,
1343 ret_dict_any, PROP_FUNC(f_prop_find)},
1344 {"prop_list", 1, 2, FEARG_1, NULL,
1345 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1346 {"prop_remove", 1, 3, FEARG_1, NULL,
1347 ret_number, PROP_FUNC(f_prop_remove)},
1348 {"prop_type_add", 2, 2, FEARG_1, NULL,
1349 ret_void, PROP_FUNC(f_prop_type_add)},
1350 {"prop_type_change", 2, 2, FEARG_1, NULL,
1351 ret_void, PROP_FUNC(f_prop_type_change)},
1352 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1353 ret_void, PROP_FUNC(f_prop_type_delete)},
1354 {"prop_type_get", 1, 2, FEARG_1, NULL,
1355 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1356 {"prop_type_list", 0, 1, FEARG_1, NULL,
1357 ret_list_string, PROP_FUNC(f_prop_type_list)},
1358 {"pum_getpos", 0, 0, 0, NULL,
1359 ret_dict_number, f_pum_getpos},
1360 {"pumvisible", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001361 ret_number_bool, f_pumvisible},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001362 {"py3eval", 1, 1, FEARG_1, NULL,
1363 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001364#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001365 f_py3eval
1366#else
1367 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001368#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001369 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001370 {"pyeval", 1, 1, FEARG_1, NULL,
1371 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001372#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001373 f_pyeval
1374#else
1375 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001376#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001377 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001378 {"pyxeval", 1, 1, FEARG_1, NULL,
1379 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001380#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001381 f_pyxeval
1382#else
1383 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001384#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001385 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001386 {"rand", 0, 1, FEARG_1, NULL,
1387 ret_number, f_rand},
1388 {"range", 1, 3, FEARG_1, NULL,
1389 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001390 {"readblob", 1, 1, FEARG_1, NULL,
1391 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001392 {"readdir", 1, 3, FEARG_1, NULL,
1393 ret_list_string, f_readdir},
1394 {"readdirex", 1, 3, FEARG_1, NULL,
1395 ret_list_dict_any, f_readdirex},
1396 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001397 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001398 {"reduce", 2, 3, FEARG_1, NULL,
1399 ret_any, f_reduce},
1400 {"reg_executing", 0, 0, 0, NULL,
1401 ret_string, f_reg_executing},
1402 {"reg_recording", 0, 0, 0, NULL,
1403 ret_string, f_reg_recording},
1404 {"reltime", 0, 2, FEARG_1, NULL,
1405 ret_list_any, f_reltime},
1406 {"reltimefloat", 1, 1, FEARG_1, NULL,
1407 ret_float, FLOAT_FUNC(f_reltimefloat)},
1408 {"reltimestr", 1, 1, FEARG_1, NULL,
1409 ret_string, f_reltimestr},
1410 {"remote_expr", 2, 4, FEARG_1, NULL,
1411 ret_string, f_remote_expr},
1412 {"remote_foreground", 1, 1, FEARG_1, NULL,
1413 ret_string, f_remote_foreground},
1414 {"remote_peek", 1, 2, FEARG_1, NULL,
1415 ret_number, f_remote_peek},
1416 {"remote_read", 1, 2, FEARG_1, NULL,
1417 ret_string, f_remote_read},
1418 {"remote_send", 2, 3, FEARG_1, NULL,
1419 ret_string, f_remote_send},
1420 {"remote_startserver", 1, 1, FEARG_1, NULL,
1421 ret_void, f_remote_startserver},
1422 {"remove", 2, 3, FEARG_1, NULL,
1423 ret_remove, f_remove},
1424 {"rename", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001425 ret_number_bool, f_rename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001426 {"repeat", 2, 2, FEARG_1, NULL,
1427 ret_first_arg, f_repeat},
1428 {"resolve", 1, 1, FEARG_1, NULL,
1429 ret_string, f_resolve},
1430 {"reverse", 1, 1, FEARG_1, NULL,
1431 ret_first_arg, f_reverse},
1432 {"round", 1, 1, FEARG_1, NULL,
1433 ret_float, FLOAT_FUNC(f_round)},
1434 {"rubyeval", 1, 1, FEARG_1, NULL,
1435 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001436#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001437 f_rubyeval
1438#else
1439 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001440#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001441 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001442 {"screenattr", 2, 2, FEARG_1, NULL,
1443 ret_number, f_screenattr},
1444 {"screenchar", 2, 2, FEARG_1, NULL,
1445 ret_number, f_screenchar},
1446 {"screenchars", 2, 2, FEARG_1, NULL,
1447 ret_list_number, f_screenchars},
1448 {"screencol", 0, 0, 0, NULL,
1449 ret_number, f_screencol},
1450 {"screenpos", 3, 3, FEARG_1, NULL,
1451 ret_dict_number, f_screenpos},
1452 {"screenrow", 0, 0, 0, NULL,
1453 ret_number, f_screenrow},
1454 {"screenstring", 2, 2, FEARG_1, NULL,
1455 ret_string, f_screenstring},
1456 {"search", 1, 5, FEARG_1, NULL,
1457 ret_number, f_search},
1458 {"searchcount", 0, 1, FEARG_1, NULL,
1459 ret_dict_any, f_searchcount},
1460 {"searchdecl", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001461 ret_number_bool, f_searchdecl},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001462 {"searchpair", 3, 7, 0, NULL,
1463 ret_number, f_searchpair},
1464 {"searchpairpos", 3, 7, 0, NULL,
1465 ret_list_number, f_searchpairpos},
1466 {"searchpos", 1, 5, FEARG_1, NULL,
1467 ret_list_number, f_searchpos},
1468 {"server2client", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001469 ret_number_bool, f_server2client},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001470 {"serverlist", 0, 0, 0, NULL,
1471 ret_string, f_serverlist},
1472 {"setbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001473 ret_number_bool, f_setbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001474 {"setbufvar", 3, 3, FEARG_3, NULL,
1475 ret_void, f_setbufvar},
1476 {"setcellwidths", 1, 1, FEARG_1, NULL,
1477 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001478 {"setcharpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001479 ret_number_bool, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001480 {"setcharsearch", 1, 1, FEARG_1, NULL,
1481 ret_void, f_setcharsearch},
1482 {"setcmdpos", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001483 ret_number_bool, f_setcmdpos},
1484 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1485 ret_number_bool, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001486 {"setenv", 2, 2, FEARG_2, NULL,
1487 ret_void, f_setenv},
1488 {"setfperm", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001489 ret_number_bool, f_setfperm},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001490 {"setline", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001491 ret_number_bool, f_setline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001492 {"setloclist", 2, 4, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001493 ret_number_bool, f_setloclist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001494 {"setmatches", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001495 ret_number_bool, f_setmatches},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001496 {"setpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001497 ret_number_bool, f_setpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001498 {"setqflist", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001499 ret_number_bool, f_setqflist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001500 {"setreg", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001501 ret_number_bool, f_setreg},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001502 {"settabvar", 3, 3, FEARG_3, NULL,
1503 ret_void, f_settabvar},
1504 {"settabwinvar", 4, 4, FEARG_4, NULL,
1505 ret_void, f_settabwinvar},
1506 {"settagstack", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001507 ret_number_bool, f_settagstack},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001508 {"setwinvar", 3, 3, FEARG_3, NULL,
1509 ret_void, f_setwinvar},
1510 {"sha256", 1, 1, FEARG_1, NULL,
1511 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001512#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001513 f_sha256
1514#else
1515 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001516#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001517 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001518 {"shellescape", 1, 2, FEARG_1, NULL,
1519 ret_string, f_shellescape},
1520 {"shiftwidth", 0, 1, FEARG_1, NULL,
1521 ret_number, f_shiftwidth},
1522 {"sign_define", 1, 2, FEARG_1, NULL,
1523 ret_any, SIGN_FUNC(f_sign_define)},
1524 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1525 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1526 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1527 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1528 {"sign_jump", 3, 3, FEARG_1, NULL,
1529 ret_number, SIGN_FUNC(f_sign_jump)},
1530 {"sign_place", 4, 5, FEARG_1, NULL,
1531 ret_number, SIGN_FUNC(f_sign_place)},
1532 {"sign_placelist", 1, 1, FEARG_1, NULL,
1533 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1534 {"sign_undefine", 0, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001535 ret_number_bool, SIGN_FUNC(f_sign_undefine)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001536 {"sign_unplace", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001537 ret_number_bool, SIGN_FUNC(f_sign_unplace)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001538 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1539 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1540 {"simplify", 1, 1, FEARG_1, NULL,
1541 ret_string, f_simplify},
1542 {"sin", 1, 1, FEARG_1, NULL,
1543 ret_float, FLOAT_FUNC(f_sin)},
1544 {"sinh", 1, 1, FEARG_1, NULL,
1545 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001546 {"slice", 2, 3, FEARG_1, NULL,
1547 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001548 {"sort", 1, 3, FEARG_1, NULL,
1549 ret_first_arg, f_sort},
1550 {"sound_clear", 0, 0, 0, NULL,
1551 ret_void, SOUND_FUNC(f_sound_clear)},
1552 {"sound_playevent", 1, 2, FEARG_1, NULL,
1553 ret_number, SOUND_FUNC(f_sound_playevent)},
1554 {"sound_playfile", 1, 2, FEARG_1, NULL,
1555 ret_number, SOUND_FUNC(f_sound_playfile)},
1556 {"sound_stop", 1, 1, FEARG_1, NULL,
1557 ret_void, SOUND_FUNC(f_sound_stop)},
1558 {"soundfold", 1, 1, FEARG_1, NULL,
1559 ret_string, f_soundfold},
1560 {"spellbadword", 0, 1, FEARG_1, NULL,
1561 ret_list_string, f_spellbadword},
1562 {"spellsuggest", 1, 3, FEARG_1, NULL,
1563 ret_list_string, f_spellsuggest},
1564 {"split", 1, 3, FEARG_1, NULL,
1565 ret_list_string, f_split},
1566 {"sqrt", 1, 1, FEARG_1, NULL,
1567 ret_float, FLOAT_FUNC(f_sqrt)},
1568 {"srand", 0, 1, FEARG_1, NULL,
1569 ret_list_number, f_srand},
1570 {"state", 0, 1, FEARG_1, NULL,
1571 ret_string, f_state},
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001572 {"str2float", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001573 ret_float, FLOAT_FUNC(f_str2float)},
1574 {"str2list", 1, 2, FEARG_1, NULL,
1575 ret_list_number, f_str2list},
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001576 {"str2nr", 1, 3, FEARG_1, arg3_string_nr_bool,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001577 ret_number, f_str2nr},
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01001578 {"strcharlen", 1, 1, FEARG_1, NULL,
1579 ret_number, f_strcharlen},
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01001580 {"strcharpart", 2, 4, FEARG_1, NULL,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001581 ret_string, f_strcharpart},
1582 {"strchars", 1, 2, FEARG_1, NULL,
1583 ret_number, f_strchars},
1584 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1585 ret_number, f_strdisplaywidth},
1586 {"strftime", 1, 2, FEARG_1, NULL,
1587 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001588#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001589 f_strftime
1590#else
1591 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001592#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001593 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001594 {"strgetchar", 2, 2, FEARG_1, NULL,
1595 ret_number, f_strgetchar},
1596 {"stridx", 2, 3, FEARG_1, NULL,
1597 ret_number, f_stridx},
1598 {"string", 1, 1, FEARG_1, NULL,
1599 ret_string, f_string},
1600 {"strlen", 1, 1, FEARG_1, NULL,
1601 ret_number, f_strlen},
1602 {"strpart", 2, 4, FEARG_1, NULL,
1603 ret_string, f_strpart},
1604 {"strptime", 2, 2, FEARG_1, NULL,
1605 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001606#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001607 f_strptime
1608#else
1609 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001610#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001611 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001612 {"strridx", 2, 3, FEARG_1, NULL,
1613 ret_number, f_strridx},
1614 {"strtrans", 1, 1, FEARG_1, NULL,
1615 ret_string, f_strtrans},
1616 {"strwidth", 1, 1, FEARG_1, NULL,
1617 ret_number, f_strwidth},
1618 {"submatch", 1, 2, FEARG_1, NULL,
1619 ret_string, f_submatch},
1620 {"substitute", 4, 4, FEARG_1, NULL,
1621 ret_string, f_substitute},
1622 {"swapinfo", 1, 1, FEARG_1, NULL,
1623 ret_dict_any, f_swapinfo},
1624 {"swapname", 1, 1, FEARG_1, NULL,
1625 ret_string, f_swapname},
1626 {"synID", 3, 3, 0, NULL,
1627 ret_number, f_synID},
1628 {"synIDattr", 2, 3, FEARG_1, NULL,
1629 ret_string, f_synIDattr},
1630 {"synIDtrans", 1, 1, FEARG_1, NULL,
1631 ret_number, f_synIDtrans},
1632 {"synconcealed", 2, 2, 0, NULL,
1633 ret_list_any, f_synconcealed},
1634 {"synstack", 2, 2, 0, NULL,
1635 ret_list_number, f_synstack},
1636 {"system", 1, 2, FEARG_1, NULL,
1637 ret_string, f_system},
1638 {"systemlist", 1, 2, FEARG_1, NULL,
1639 ret_list_string, f_systemlist},
1640 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1641 ret_list_number, f_tabpagebuflist},
1642 {"tabpagenr", 0, 1, 0, NULL,
1643 ret_number, f_tabpagenr},
1644 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1645 ret_number, f_tabpagewinnr},
1646 {"tagfiles", 0, 0, 0, NULL,
1647 ret_list_string, f_tagfiles},
1648 {"taglist", 1, 2, FEARG_1, NULL,
1649 ret_list_dict_any, f_taglist},
1650 {"tan", 1, 1, FEARG_1, NULL,
1651 ret_float, FLOAT_FUNC(f_tan)},
1652 {"tanh", 1, 1, FEARG_1, NULL,
1653 ret_float, FLOAT_FUNC(f_tanh)},
1654 {"tempname", 0, 0, 0, NULL,
1655 ret_string, f_tempname},
1656 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1657 ret_number, TERM_FUNC(f_term_dumpdiff)},
1658 {"term_dumpload", 1, 2, FEARG_1, NULL,
1659 ret_number, TERM_FUNC(f_term_dumpload)},
1660 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1661 ret_void, TERM_FUNC(f_term_dumpwrite)},
1662 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1663 ret_number, TERM_FUNC(f_term_getaltscreen)},
1664 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1665 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001666#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001667 f_term_getansicolors
1668#else
1669 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001670#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001671 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001672 {"term_getattr", 2, 2, FEARG_1, NULL,
1673 ret_number, TERM_FUNC(f_term_getattr)},
1674 {"term_getcursor", 1, 1, FEARG_1, NULL,
1675 ret_list_any, TERM_FUNC(f_term_getcursor)},
1676 {"term_getjob", 1, 1, FEARG_1, NULL,
1677 ret_job, TERM_FUNC(f_term_getjob)},
1678 {"term_getline", 2, 2, FEARG_1, NULL,
1679 ret_string, TERM_FUNC(f_term_getline)},
1680 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1681 ret_number, TERM_FUNC(f_term_getscrolled)},
1682 {"term_getsize", 1, 1, FEARG_1, NULL,
1683 ret_list_number, TERM_FUNC(f_term_getsize)},
1684 {"term_getstatus", 1, 1, FEARG_1, NULL,
1685 ret_string, TERM_FUNC(f_term_getstatus)},
1686 {"term_gettitle", 1, 1, FEARG_1, NULL,
1687 ret_string, TERM_FUNC(f_term_gettitle)},
1688 {"term_gettty", 1, 2, FEARG_1, NULL,
1689 ret_string, TERM_FUNC(f_term_gettty)},
1690 {"term_list", 0, 0, 0, NULL,
1691 ret_list_number, TERM_FUNC(f_term_list)},
1692 {"term_scrape", 2, 2, FEARG_1, NULL,
1693 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1694 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1695 ret_void, TERM_FUNC(f_term_sendkeys)},
1696 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1697 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001698#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001699 f_term_setansicolors
1700#else
1701 NULL
1702#endif
1703 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001704 {"term_setapi", 2, 2, FEARG_1, NULL,
1705 ret_void, TERM_FUNC(f_term_setapi)},
1706 {"term_setkill", 2, 2, FEARG_1, NULL,
1707 ret_void, TERM_FUNC(f_term_setkill)},
1708 {"term_setrestore", 2, 2, FEARG_1, NULL,
1709 ret_void, TERM_FUNC(f_term_setrestore)},
1710 {"term_setsize", 3, 3, FEARG_1, NULL,
1711 ret_void, TERM_FUNC(f_term_setsize)},
1712 {"term_start", 1, 2, FEARG_1, NULL,
1713 ret_number, TERM_FUNC(f_term_start)},
1714 {"term_wait", 1, 2, FEARG_1, NULL,
1715 ret_void, TERM_FUNC(f_term_wait)},
1716 {"terminalprops", 0, 0, 0, NULL,
1717 ret_dict_string, f_terminalprops},
1718 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1719 ret_void, f_test_alloc_fail},
1720 {"test_autochdir", 0, 0, 0, NULL,
1721 ret_void, f_test_autochdir},
1722 {"test_feedinput", 1, 1, FEARG_1, NULL,
1723 ret_void, f_test_feedinput},
1724 {"test_garbagecollect_now", 0, 0, 0, NULL,
1725 ret_void, f_test_garbagecollect_now},
1726 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1727 ret_void, f_test_garbagecollect_soon},
1728 {"test_getvalue", 1, 1, FEARG_1, NULL,
1729 ret_number, f_test_getvalue},
1730 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1731 ret_void, f_test_ignore_error},
1732 {"test_null_blob", 0, 0, 0, NULL,
1733 ret_blob, f_test_null_blob},
1734 {"test_null_channel", 0, 0, 0, NULL,
1735 ret_channel, JOB_FUNC(f_test_null_channel)},
1736 {"test_null_dict", 0, 0, 0, NULL,
1737 ret_dict_any, f_test_null_dict},
1738 {"test_null_function", 0, 0, 0, NULL,
1739 ret_func_any, f_test_null_function},
1740 {"test_null_job", 0, 0, 0, NULL,
1741 ret_job, JOB_FUNC(f_test_null_job)},
1742 {"test_null_list", 0, 0, 0, NULL,
1743 ret_list_any, f_test_null_list},
1744 {"test_null_partial", 0, 0, 0, NULL,
1745 ret_func_any, f_test_null_partial},
1746 {"test_null_string", 0, 0, 0, NULL,
1747 ret_string, f_test_null_string},
1748 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1749 ret_void, f_test_option_not_set},
1750 {"test_override", 2, 2, FEARG_2, NULL,
1751 ret_void, f_test_override},
1752 {"test_refcount", 1, 1, FEARG_1, NULL,
1753 ret_number, f_test_refcount},
1754 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1755 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001756#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001757 f_test_scrollbar
1758#else
1759 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001760#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001761 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001762 {"test_setmouse", 2, 2, 0, NULL,
1763 ret_void, f_test_setmouse},
1764 {"test_settime", 1, 1, FEARG_1, NULL,
1765 ret_void, f_test_settime},
1766 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1767 ret_void, f_test_srand_seed},
1768 {"test_unknown", 0, 0, 0, NULL,
1769 ret_any, f_test_unknown},
1770 {"test_void", 0, 0, 0, NULL,
1771 ret_void, f_test_void},
1772 {"timer_info", 0, 1, FEARG_1, NULL,
1773 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1774 {"timer_pause", 2, 2, FEARG_1, NULL,
1775 ret_void, TIMER_FUNC(f_timer_pause)},
1776 {"timer_start", 2, 3, FEARG_1, NULL,
1777 ret_number, TIMER_FUNC(f_timer_start)},
1778 {"timer_stop", 1, 1, FEARG_1, NULL,
1779 ret_void, TIMER_FUNC(f_timer_stop)},
1780 {"timer_stopall", 0, 0, 0, NULL,
1781 ret_void, TIMER_FUNC(f_timer_stopall)},
1782 {"tolower", 1, 1, FEARG_1, NULL,
1783 ret_string, f_tolower},
1784 {"toupper", 1, 1, FEARG_1, NULL,
1785 ret_string, f_toupper},
1786 {"tr", 3, 3, FEARG_1, NULL,
1787 ret_string, f_tr},
1788 {"trim", 1, 3, FEARG_1, NULL,
1789 ret_string, f_trim},
1790 {"trunc", 1, 1, FEARG_1, NULL,
1791 ret_float, FLOAT_FUNC(f_trunc)},
1792 {"type", 1, 1, FEARG_1, NULL,
1793 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001794 {"typename", 1, 1, FEARG_1, NULL,
1795 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001796 {"undofile", 1, 1, FEARG_1, NULL,
1797 ret_string, f_undofile},
1798 {"undotree", 0, 0, 0, NULL,
1799 ret_dict_any, f_undotree},
1800 {"uniq", 1, 3, FEARG_1, NULL,
1801 ret_list_any, f_uniq},
1802 {"values", 1, 1, FEARG_1, NULL,
1803 ret_list_any, f_values},
1804 {"virtcol", 1, 1, FEARG_1, NULL,
1805 ret_number, f_virtcol},
1806 {"visualmode", 0, 1, 0, NULL,
1807 ret_string, f_visualmode},
1808 {"wildmenumode", 0, 0, 0, NULL,
1809 ret_number, f_wildmenumode},
1810 {"win_execute", 2, 3, FEARG_2, NULL,
1811 ret_string, f_win_execute},
1812 {"win_findbuf", 1, 1, FEARG_1, NULL,
1813 ret_list_number, f_win_findbuf},
1814 {"win_getid", 0, 2, FEARG_1, NULL,
1815 ret_number, f_win_getid},
1816 {"win_gettype", 0, 1, FEARG_1, NULL,
1817 ret_string, f_win_gettype},
1818 {"win_gotoid", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001819 ret_number_bool, f_win_gotoid},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001820 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1821 ret_list_number, f_win_id2tabwin},
1822 {"win_id2win", 1, 1, FEARG_1, NULL,
1823 ret_number, f_win_id2win},
1824 {"win_screenpos", 1, 1, FEARG_1, NULL,
1825 ret_list_number, f_win_screenpos},
1826 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001827 ret_number_bool, f_win_splitmove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001828 {"winbufnr", 1, 1, FEARG_1, NULL,
1829 ret_number, f_winbufnr},
1830 {"wincol", 0, 0, 0, NULL,
1831 ret_number, f_wincol},
1832 {"windowsversion", 0, 0, 0, NULL,
1833 ret_string, f_windowsversion},
1834 {"winheight", 1, 1, FEARG_1, NULL,
1835 ret_number, f_winheight},
1836 {"winlayout", 0, 1, FEARG_1, NULL,
1837 ret_list_any, f_winlayout},
1838 {"winline", 0, 0, 0, NULL,
1839 ret_number, f_winline},
1840 {"winnr", 0, 1, FEARG_1, NULL,
1841 ret_number, f_winnr},
1842 {"winrestcmd", 0, 0, 0, NULL,
1843 ret_string, f_winrestcmd},
1844 {"winrestview", 1, 1, FEARG_1, NULL,
1845 ret_void, f_winrestview},
1846 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001847 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001848 {"winwidth", 1, 1, FEARG_1, NULL,
1849 ret_number, f_winwidth},
1850 {"wordcount", 0, 0, 0, NULL,
1851 ret_dict_number, f_wordcount},
1852 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001853 ret_number_bool, f_writefile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001854 {"xor", 2, 2, FEARG_1, NULL,
1855 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001856};
1857
K.Takataeecf2b32021-06-02 14:56:39 +02001858#if defined(EBCDIC) || defined(PROTO)
1859/*
1860 * Compare funcentry_T by function name.
1861 */
1862 static int
1863compare_func_name(const void *s1, const void *s2)
1864{
1865 funcentry_T *p1 = (funcentry_T *)s1;
1866 funcentry_T *p2 = (funcentry_T *)s2;
1867
1868 return STRCMP(p1->f_name, p2->f_name);
1869}
1870
1871/*
1872 * Sort the function table by function name.
1873 * The sorting of the table above is ASCII dependent.
1874 * On machines using EBCDIC we have to sort it.
1875 */
1876 void
1877sortFunctions(void)
1878{
1879 size_t funcCnt = ARRAY_LENGTH(global_functions);
1880
1881 qsort(global_functions, funcCnt, sizeof(funcentry_T), compare_func_name);
1882}
1883#endif
1884
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001885/*
1886 * Function given to ExpandGeneric() to obtain the list of internal
1887 * or user defined function names.
1888 */
1889 char_u *
1890get_function_name(expand_T *xp, int idx)
1891{
1892 static int intidx = -1;
1893 char_u *name;
1894
1895 if (idx == 0)
1896 intidx = -1;
1897 if (intidx < 0)
1898 {
1899 name = get_user_func_name(xp, idx);
1900 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001901 {
1902 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1903 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001904 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001905 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001906 }
K.Takataeeec2542021-06-02 13:28:16 +02001907 if (++intidx < (int)ARRAY_LENGTH(global_functions))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001908 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001909 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001910 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001911 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001912 STRCAT(IObuff, ")");
1913 return IObuff;
1914 }
1915
1916 return NULL;
1917}
1918
1919/*
1920 * Function given to ExpandGeneric() to obtain the list of internal or
1921 * user defined variable or function names.
1922 */
1923 char_u *
1924get_expr_name(expand_T *xp, int idx)
1925{
1926 static int intidx = -1;
1927 char_u *name;
1928
1929 if (idx == 0)
1930 intidx = -1;
1931 if (intidx < 0)
1932 {
1933 name = get_function_name(xp, idx);
1934 if (name != NULL)
1935 return name;
1936 }
1937 return get_user_var_name(xp, ++intidx);
1938}
1939
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001940/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001941 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001942 * Return index, or -1 if not found or "implemented" is TRUE and the function
1943 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001944 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001945 static int
1946find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001947{
1948 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001949 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001950 int cmp;
1951 int x;
1952
K.Takataeeec2542021-06-02 13:28:16 +02001953 last = (int)ARRAY_LENGTH(global_functions) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001954
1955 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001956 while (first <= last)
1957 {
1958 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001959 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001960 if (cmp < 0)
1961 last = x - 1;
1962 else if (cmp > 0)
1963 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001964 else if (implemented && global_functions[x].f_func == NULL)
1965 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001966 else
1967 return x;
1968 }
1969 return -1;
1970}
1971
Bram Moolenaar15c47602020-03-26 22:16:48 +01001972/*
1973 * Find internal function "name" in table "global_functions".
1974 * Return index, or -1 if not found or the function is not implemented.
1975 */
1976 int
1977find_internal_func(char_u *name)
1978{
1979 return find_internal_func_opt(name, TRUE);
1980}
1981
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001982 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001983has_internal_func(char_u *name)
1984{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001985 return find_internal_func_opt(name, TRUE) >= 0;
1986}
1987
1988 static int
1989has_internal_func_name(char_u *name)
1990{
1991 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001992}
1993
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994 char *
1995internal_func_name(int idx)
1996{
1997 return global_functions[idx].f_name;
1998}
1999
Bram Moolenaar94738d82020-10-21 14:25:07 +02002000/*
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002001 * Check the argument types for builtin function "idx".
Bram Moolenaar94738d82020-10-21 14:25:07 +02002002 * Uses the list of types on the type stack: "types".
2003 * Return FAIL and gives an error message when a type is wrong.
2004 */
2005 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01002006internal_func_check_arg_types(
2007 type_T **types,
2008 int idx,
2009 int argcount,
2010 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02002011{
2012 argcheck_T *argchecks = global_functions[idx].f_argcheck;
2013 int i;
2014
2015 if (argchecks != NULL)
2016 {
2017 argcontext_T context;
2018
2019 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02002020 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01002021 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02002022 for (i = 0; i < argcount; ++i)
2023 if (argchecks[i] != NULL)
2024 {
2025 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02002026 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02002027 return FAIL;
2028 }
2029 }
2030 return OK;
2031}
2032
Bram Moolenaara1224cb2020-10-22 12:31:49 +02002033/*
2034 * Call the "f_retfunc" function to obtain the return type of function "idx".
2035 * "argtypes" is the list of argument types or NULL when there are no
2036 * arguments.
2037 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002039internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002041 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042}
2043
2044/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002045 * Return TRUE if "idx" is for the map() function.
2046 */
2047 int
2048internal_func_is_map(int idx)
2049{
2050 return global_functions[idx].f_func == f_map;
2051}
2052
2053/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002055 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2056 * first argument, 2 if method base is second argument, etc. 9 if method base
2057 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 */
2059 int
2060check_internal_func(int idx, int argcount)
2061{
2062 int res;
2063 char *name;
2064
2065 if (argcount < global_functions[idx].f_min_argc)
2066 res = FCERR_TOOFEW;
2067 else if (argcount > global_functions[idx].f_max_argc)
2068 res = FCERR_TOOMANY;
2069 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002070 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071
2072 name = internal_func_name(idx);
2073 if (res == FCERR_TOOMANY)
2074 semsg(_(e_toomanyarg), name);
2075 else
2076 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002077 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078}
2079
Bram Moolenaarac92e252019-08-03 21:58:38 +02002080 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002081call_internal_func(
2082 char_u *name,
2083 int argcount,
2084 typval_T *argvars,
2085 typval_T *rettv)
2086{
2087 int i;
2088
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002089 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002090 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002091 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002092 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002093 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002094 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002095 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002096 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002097 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002098 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002099}
2100
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101 void
2102call_internal_func_by_idx(
2103 int idx,
2104 typval_T *argvars,
2105 typval_T *rettv)
2106{
2107 global_functions[idx].f_func(argvars, rettv);
2108}
2109
Bram Moolenaarac92e252019-08-03 21:58:38 +02002110/*
2111 * Invoke a method for base->method().
2112 */
2113 int
2114call_internal_method(
2115 char_u *name,
2116 int argcount,
2117 typval_T *argvars,
2118 typval_T *rettv,
2119 typval_T *basetv)
2120{
2121 int i;
2122 int fi;
2123 typval_T argv[MAX_FUNC_ARGS + 1];
2124
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002125 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002126 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002127 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002128 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002129 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002130 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002131 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002132 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002133 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002134
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002135 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002136 {
2137 // base value goes last
2138 for (i = 0; i < argcount; ++i)
2139 argv[i] = argvars[i];
2140 argv[argcount] = *basetv;
2141 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002142 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002143 {
2144 // base value goes second
2145 argv[0] = argvars[0];
2146 argv[1] = *basetv;
2147 for (i = 1; i < argcount; ++i)
2148 argv[i + 1] = argvars[i];
2149 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002150 else if (global_functions[fi].f_argtype == FEARG_3)
2151 {
2152 // base value goes third
2153 argv[0] = argvars[0];
2154 argv[1] = argvars[1];
2155 argv[2] = *basetv;
2156 for (i = 2; i < argcount; ++i)
2157 argv[i + 1] = argvars[i];
2158 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002159 else if (global_functions[fi].f_argtype == FEARG_4)
2160 {
2161 // base value goes fourth
2162 argv[0] = argvars[0];
2163 argv[1] = argvars[1];
2164 argv[2] = argvars[2];
2165 argv[3] = *basetv;
2166 for (i = 3; i < argcount; ++i)
2167 argv[i + 1] = argvars[i];
2168 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002169 else
2170 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002171 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002172 argv[0] = *basetv;
2173 for (i = 0; i < argcount; ++i)
2174 argv[i + 1] = argvars[i];
2175 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002176 argv[argcount + 1].v_type = VAR_UNKNOWN;
2177
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002178 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002179 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002180}
2181
2182/*
2183 * Return TRUE for a non-zero Number and a non-empty String.
2184 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002185 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002186non_zero_arg(typval_T *argvars)
2187{
2188 return ((argvars[0].v_type == VAR_NUMBER
2189 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002190 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002191 && argvars[0].vval.v_number == VVAL_TRUE)
2192 || (argvars[0].v_type == VAR_STRING
2193 && argvars[0].vval.v_string != NULL
2194 && *argvars[0].vval.v_string != NUL));
2195}
2196
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002197#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002198/*
2199 * Get the float value of "argvars[0]" into "f".
2200 * Returns FAIL when the argument is not a Number or Float.
2201 */
2202 static int
2203get_float_arg(typval_T *argvars, float_T *f)
2204{
2205 if (argvars[0].v_type == VAR_FLOAT)
2206 {
2207 *f = argvars[0].vval.v_float;
2208 return OK;
2209 }
2210 if (argvars[0].v_type == VAR_NUMBER)
2211 {
2212 *f = (float_T)argvars[0].vval.v_number;
2213 return OK;
2214 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002215 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002216 return FAIL;
2217}
2218
2219/*
2220 * "abs(expr)" function
2221 */
2222 static void
2223f_abs(typval_T *argvars, typval_T *rettv)
2224{
2225 if (argvars[0].v_type == VAR_FLOAT)
2226 {
2227 rettv->v_type = VAR_FLOAT;
2228 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
2229 }
2230 else
2231 {
2232 varnumber_T n;
2233 int error = FALSE;
2234
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002235 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002236 if (error)
2237 rettv->vval.v_number = -1;
2238 else if (n > 0)
2239 rettv->vval.v_number = n;
2240 else
2241 rettv->vval.v_number = -n;
2242 }
2243}
2244
2245/*
2246 * "acos()" function
2247 */
2248 static void
2249f_acos(typval_T *argvars, typval_T *rettv)
2250{
2251 float_T f = 0.0;
2252
2253 rettv->v_type = VAR_FLOAT;
2254 if (get_float_arg(argvars, &f) == OK)
2255 rettv->vval.v_float = acos(f);
2256 else
2257 rettv->vval.v_float = 0.0;
2258}
2259#endif
2260
2261/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002262 * "and(expr, expr)" function
2263 */
2264 static void
2265f_and(typval_T *argvars, typval_T *rettv)
2266{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002267 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2268 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002269}
2270
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002271#ifdef FEAT_FLOAT
2272/*
2273 * "asin()" function
2274 */
2275 static void
2276f_asin(typval_T *argvars, typval_T *rettv)
2277{
2278 float_T f = 0.0;
2279
2280 rettv->v_type = VAR_FLOAT;
2281 if (get_float_arg(argvars, &f) == OK)
2282 rettv->vval.v_float = asin(f);
2283 else
2284 rettv->vval.v_float = 0.0;
2285}
2286
2287/*
2288 * "atan()" function
2289 */
2290 static void
2291f_atan(typval_T *argvars, typval_T *rettv)
2292{
2293 float_T f = 0.0;
2294
2295 rettv->v_type = VAR_FLOAT;
2296 if (get_float_arg(argvars, &f) == OK)
2297 rettv->vval.v_float = atan(f);
2298 else
2299 rettv->vval.v_float = 0.0;
2300}
2301
2302/*
2303 * "atan2()" function
2304 */
2305 static void
2306f_atan2(typval_T *argvars, typval_T *rettv)
2307{
2308 float_T fx = 0.0, fy = 0.0;
2309
2310 rettv->v_type = VAR_FLOAT;
2311 if (get_float_arg(argvars, &fx) == OK
2312 && get_float_arg(&argvars[1], &fy) == OK)
2313 rettv->vval.v_float = atan2(fx, fy);
2314 else
2315 rettv->vval.v_float = 0.0;
2316}
2317#endif
2318
2319/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002320 * "balloon_show()" function
2321 */
2322#ifdef FEAT_BEVAL
2323 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002324f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2325{
2326 rettv->v_type = VAR_STRING;
2327 if (balloonEval != NULL)
2328 {
2329 if (balloonEval->msg == NULL)
2330 rettv->vval.v_string = NULL;
2331 else
2332 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2333 }
2334}
2335
2336 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002337f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2338{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002339 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002340 {
2341 if (argvars[0].v_type == VAR_LIST
2342# ifdef FEAT_GUI
2343 && !gui.in_use
2344# endif
2345 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002346 {
2347 list_T *l = argvars[0].vval.v_list;
2348
2349 // empty list removes the balloon
2350 post_balloon(balloonEval, NULL,
2351 l == NULL || l->lv_len == 0 ? NULL : l);
2352 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002353 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002354 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002355 char_u *mesg;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002356
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002357 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2358 return;
2359
2360 mesg = tv_get_string_chk(&argvars[0]);
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002361 if (mesg != NULL)
2362 // empty string removes the balloon
2363 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2364 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002365 }
2366}
2367
Bram Moolenaar669a8282017-11-19 20:13:05 +01002368# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002369 static void
2370f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2371{
2372 if (rettv_list_alloc(rettv) == OK)
2373 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002374 char_u *msg;
Bram Moolenaar246fe032017-11-19 19:56:27 +01002375
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002376 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2377 return;
2378 msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002379 if (msg != NULL)
2380 {
2381 pumitem_T *array;
2382 int size = split_message(msg, &array);
2383 int i;
2384
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002385 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002386 for (i = 1; i < size - 1; ++i)
2387 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002388 while (size > 0)
2389 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002390 vim_free(array);
2391 }
2392 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002393}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002394# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002395#endif
2396
2397/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002398 * Get the buffer from "arg" and give an error and return NULL if it is not
2399 * valid.
2400 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002401 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002402get_buf_arg(typval_T *arg)
2403{
2404 buf_T *buf;
2405
2406 ++emsg_off;
2407 buf = tv_get_buf(arg, FALSE);
2408 --emsg_off;
2409 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002410 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002411 return buf;
2412}
2413
2414/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002415 * "byte2line(byte)" function
2416 */
2417 static void
2418f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2419{
2420#ifndef FEAT_BYTEOFF
2421 rettv->vval.v_number = -1;
2422#else
2423 long boff = 0;
2424
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002425 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002426 if (boff < 0)
2427 rettv->vval.v_number = -1;
2428 else
2429 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2430 (linenr_T)0, &boff);
2431#endif
2432}
2433
2434 static void
2435byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2436{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002437 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002438 char_u *str;
2439 varnumber_T idx;
2440
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002441 str = tv_get_string_chk(&argvars[0]);
2442 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002443 rettv->vval.v_number = -1;
2444 if (str == NULL || idx < 0)
2445 return;
2446
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002447 t = str;
2448 for ( ; idx > 0; idx--)
2449 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002450 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002451 return;
2452 if (enc_utf8 && comp)
2453 t += utf_ptr2len(t);
2454 else
2455 t += (*mb_ptr2len)(t);
2456 }
2457 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002458}
2459
2460/*
2461 * "byteidx()" function
2462 */
2463 static void
2464f_byteidx(typval_T *argvars, typval_T *rettv)
2465{
2466 byteidx(argvars, rettv, FALSE);
2467}
2468
2469/*
2470 * "byteidxcomp()" function
2471 */
2472 static void
2473f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2474{
2475 byteidx(argvars, rettv, TRUE);
2476}
2477
2478/*
2479 * "call(func, arglist [, dict])" function
2480 */
2481 static void
2482f_call(typval_T *argvars, typval_T *rettv)
2483{
2484 char_u *func;
2485 partial_T *partial = NULL;
2486 dict_T *selfdict = NULL;
2487
2488 if (argvars[1].v_type != VAR_LIST)
2489 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002490 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002491 return;
2492 }
2493 if (argvars[1].vval.v_list == NULL)
2494 return;
2495
2496 if (argvars[0].v_type == VAR_FUNC)
2497 func = argvars[0].vval.v_string;
2498 else if (argvars[0].v_type == VAR_PARTIAL)
2499 {
2500 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002501 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002502 }
2503 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002504 func = tv_get_string(&argvars[0]);
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002505 if (func == NULL || *func == NUL)
2506 return; // type error, empty name or null function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002507
2508 if (argvars[2].v_type != VAR_UNKNOWN)
2509 {
2510 if (argvars[2].v_type != VAR_DICT)
2511 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002512 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002513 return;
2514 }
2515 selfdict = argvars[2].vval.v_dict;
2516 }
2517
2518 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2519}
2520
2521#ifdef FEAT_FLOAT
2522/*
2523 * "ceil({float})" function
2524 */
2525 static void
2526f_ceil(typval_T *argvars, typval_T *rettv)
2527{
2528 float_T f = 0.0;
2529
2530 rettv->v_type = VAR_FLOAT;
2531 if (get_float_arg(argvars, &f) == OK)
2532 rettv->vval.v_float = ceil(f);
2533 else
2534 rettv->vval.v_float = 0.0;
2535}
2536#endif
2537
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002538/*
2539 * "changenr()" function
2540 */
2541 static void
2542f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2543{
2544 rettv->vval.v_number = curbuf->b_u_seq_cur;
2545}
2546
2547/*
2548 * "char2nr(string)" function
2549 */
2550 static void
2551f_char2nr(typval_T *argvars, typval_T *rettv)
2552{
Bram Moolenaarc5809432021-03-27 21:23:30 +01002553 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2554 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002555 if (has_mbyte)
2556 {
2557 int utf8 = 0;
2558
2559 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002560 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002561
2562 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002563 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002564 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002565 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002566 }
2567 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002568 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002569}
2570
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002571/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002572 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2573 * returns the character index of the column. Otherwise, returns the byte index
2574 * of the column.
2575 */
2576 static void
2577get_col(typval_T *argvars, typval_T *rettv, int charcol)
2578{
2579 colnr_T col = 0;
2580 pos_T *fp;
2581 int fnum = curbuf->b_fnum;
2582
2583 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2584 if (fp != NULL && fnum == curbuf->b_fnum)
2585 {
2586 if (fp->col == MAXCOL)
2587 {
2588 // '> can be MAXCOL, get the length of the line then
2589 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2590 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2591 else
2592 col = MAXCOL;
2593 }
2594 else
2595 {
2596 col = fp->col + 1;
2597 // col(".") when the cursor is on the NUL at the end of the line
2598 // because of "coladd" can be seen as an extra column.
2599 if (virtual_active() && fp == &curwin->w_cursor)
2600 {
2601 char_u *p = ml_get_cursor();
2602
2603 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2604 curwin->w_virtcol - curwin->w_cursor.coladd))
2605 {
2606 int l;
2607
2608 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2609 col += l;
2610 }
2611 }
2612 }
2613 }
2614 rettv->vval.v_number = col;
2615}
2616
2617/*
2618 * "charcol()" function
2619 */
2620 static void
2621f_charcol(typval_T *argvars, typval_T *rettv)
2622{
2623 get_col(argvars, rettv, TRUE);
2624}
2625
2626/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002627 * "charidx()" function
2628 */
2629 static void
2630f_charidx(typval_T *argvars, typval_T *rettv)
2631{
2632 char_u *str;
2633 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002634 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002635 char_u *p;
2636 int len;
2637 int (*ptr2len)(char_u *);
2638
2639 rettv->vval.v_number = -1;
2640
2641 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2642 || (argvars[2].v_type != VAR_UNKNOWN
2643 && argvars[2].v_type != VAR_NUMBER))
2644 {
2645 emsg(_(e_invarg));
2646 return;
2647 }
2648
2649 str = tv_get_string_chk(&argvars[0]);
2650 idx = tv_get_number_chk(&argvars[1], NULL);
2651 if (str == NULL || idx < 0)
2652 return;
2653
2654 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002655 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002656 if (countcc < 0 || countcc > 1)
2657 {
2658 semsg(_(e_using_number_as_bool_nr), countcc);
2659 return;
2660 }
2661
2662 if (enc_utf8 && countcc)
2663 ptr2len = utf_ptr2len;
2664 else
2665 ptr2len = mb_ptr2len;
2666
2667 for (p = str, len = 0; p <= str + idx; len++)
2668 {
2669 if (*p == NUL)
2670 return;
2671 p += ptr2len(p);
2672 }
2673
2674 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2675}
2676
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002677 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002678get_optional_window(typval_T *argvars, int idx)
2679{
2680 win_T *win = curwin;
2681
2682 if (argvars[idx].v_type != VAR_UNKNOWN)
2683 {
2684 win = find_win_by_nr_or_id(&argvars[idx]);
2685 if (win == NULL)
2686 {
2687 emsg(_(e_invalwindow));
2688 return NULL;
2689 }
2690 }
2691 return win;
2692}
2693
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002694/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002695 * "col(string)" function
2696 */
2697 static void
2698f_col(typval_T *argvars, typval_T *rettv)
2699{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002700 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002701}
2702
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002703/*
2704 * "confirm(message, buttons[, default [, type]])" function
2705 */
2706 static void
2707f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2708{
2709#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2710 char_u *message;
2711 char_u *buttons = NULL;
2712 char_u buf[NUMBUFLEN];
2713 char_u buf2[NUMBUFLEN];
2714 int def = 1;
2715 int type = VIM_GENERIC;
2716 char_u *typestr;
2717 int error = FALSE;
2718
Bram Moolenaarc5809432021-03-27 21:23:30 +01002719 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2720 return;
2721
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002722 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002723 if (message == NULL)
2724 error = TRUE;
2725 if (argvars[1].v_type != VAR_UNKNOWN)
2726 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002727 if (in_vim9script() && check_for_string_arg(argvars, 1) == FAIL)
2728 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002729 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002730 if (buttons == NULL)
2731 error = TRUE;
2732 if (argvars[2].v_type != VAR_UNKNOWN)
2733 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002734 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002735 if (argvars[3].v_type != VAR_UNKNOWN)
2736 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002737 if (in_vim9script() && check_for_string_arg(argvars, 3) == FAIL)
2738 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002739 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002740 if (typestr == NULL)
2741 error = TRUE;
2742 else
2743 {
2744 switch (TOUPPER_ASC(*typestr))
2745 {
2746 case 'E': type = VIM_ERROR; break;
2747 case 'Q': type = VIM_QUESTION; break;
2748 case 'I': type = VIM_INFO; break;
2749 case 'W': type = VIM_WARNING; break;
2750 case 'G': type = VIM_GENERIC; break;
2751 }
2752 }
2753 }
2754 }
2755 }
2756
2757 if (buttons == NULL || *buttons == NUL)
2758 buttons = (char_u *)_("&Ok");
2759
2760 if (!error)
2761 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2762 def, NULL, FALSE);
2763#endif
2764}
2765
2766/*
2767 * "copy()" function
2768 */
2769 static void
2770f_copy(typval_T *argvars, typval_T *rettv)
2771{
2772 item_copy(&argvars[0], rettv, FALSE, 0);
2773}
2774
2775#ifdef FEAT_FLOAT
2776/*
2777 * "cos()" function
2778 */
2779 static void
2780f_cos(typval_T *argvars, typval_T *rettv)
2781{
2782 float_T f = 0.0;
2783
2784 rettv->v_type = VAR_FLOAT;
2785 if (get_float_arg(argvars, &f) == OK)
2786 rettv->vval.v_float = cos(f);
2787 else
2788 rettv->vval.v_float = 0.0;
2789}
2790
2791/*
2792 * "cosh()" function
2793 */
2794 static void
2795f_cosh(typval_T *argvars, typval_T *rettv)
2796{
2797 float_T f = 0.0;
2798
2799 rettv->v_type = VAR_FLOAT;
2800 if (get_float_arg(argvars, &f) == OK)
2801 rettv->vval.v_float = cosh(f);
2802 else
2803 rettv->vval.v_float = 0.0;
2804}
2805#endif
2806
2807/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002808 * Set the cursor position.
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002809 * If 'charcol' is TRUE, then use the column number as a character offset.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002810 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002811 */
2812 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002813set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002814{
2815 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002816 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002817 int set_curswant = TRUE;
2818
2819 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002820 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002821 {
2822 pos_T pos;
2823 colnr_T curswant = -1;
2824
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002825 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002826 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002827 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002828 return;
2829 }
2830 line = pos.lnum;
2831 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002832 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002833 if (curswant >= 0)
2834 {
2835 curwin->w_curswant = curswant - 1;
2836 set_curswant = FALSE;
2837 }
2838 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002839 else if ((argvars[0].v_type == VAR_NUMBER ||
2840 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002841 && (argvars[1].v_type == VAR_NUMBER ||
2842 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002843 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002844 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002845 if (line < 0)
2846 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002847 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002848 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002849 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002850 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002851 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002852 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002853 else
2854 {
2855 emsg(_(e_invarg));
2856 return;
2857 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002858 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002859 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002860 if (line > 0)
2861 curwin->w_cursor.lnum = line;
2862 if (col > 0)
2863 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002864 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002865
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002866 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002867 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002868 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002869 if (has_mbyte)
2870 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002871
2872 curwin->w_set_curswant = set_curswant;
2873 rettv->vval.v_number = 0;
2874}
2875
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002876/*
2877 * "cursor(lnum, col)" function, or
2878 * "cursor(list)"
2879 *
2880 * Moves the cursor to the specified line and column.
2881 * Returns 0 when the position could be set, -1 otherwise.
2882 */
2883 static void
2884f_cursor(typval_T *argvars, typval_T *rettv)
2885{
2886 set_cursorpos(argvars, rettv, FALSE);
2887}
2888
Bram Moolenaar4f974752019-02-17 17:44:42 +01002889#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002890/*
2891 * "debugbreak()" function
2892 */
2893 static void
2894f_debugbreak(typval_T *argvars, typval_T *rettv)
2895{
2896 int pid;
2897
2898 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002899 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002900 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002901 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002902 else
2903 {
2904 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2905
2906 if (hProcess != NULL)
2907 {
2908 DebugBreakProcess(hProcess);
2909 CloseHandle(hProcess);
2910 rettv->vval.v_number = OK;
2911 }
2912 }
2913}
2914#endif
2915
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002916/*
2917 * "deepcopy()" function
2918 */
2919 static void
2920f_deepcopy(typval_T *argvars, typval_T *rettv)
2921{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002922 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002923 int copyID;
2924
2925 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002926 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002927 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002928 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002929 else
2930 {
2931 copyID = get_copyID();
2932 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2933 }
2934}
2935
2936/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002937 * "did_filetype()" function
2938 */
2939 static void
2940f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2941{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002942 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002943}
2944
2945/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002946 * "echoraw({expr})" function
2947 */
2948 static void
2949f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2950{
2951 char_u *str = tv_get_string_chk(&argvars[0]);
2952
2953 if (str != NULL && *str != NUL)
2954 {
2955 out_str(str);
2956 out_flush();
2957 }
2958}
2959
2960/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002961 * "empty({expr})" function
2962 */
2963 static void
2964f_empty(typval_T *argvars, typval_T *rettv)
2965{
2966 int n = FALSE;
2967
2968 switch (argvars[0].v_type)
2969 {
2970 case VAR_STRING:
2971 case VAR_FUNC:
2972 n = argvars[0].vval.v_string == NULL
2973 || *argvars[0].vval.v_string == NUL;
2974 break;
2975 case VAR_PARTIAL:
2976 n = FALSE;
2977 break;
2978 case VAR_NUMBER:
2979 n = argvars[0].vval.v_number == 0;
2980 break;
2981 case VAR_FLOAT:
2982#ifdef FEAT_FLOAT
2983 n = argvars[0].vval.v_float == 0.0;
2984 break;
2985#endif
2986 case VAR_LIST:
2987 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002988 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002989 break;
2990 case VAR_DICT:
2991 n = argvars[0].vval.v_dict == NULL
2992 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2993 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002994 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002995 case VAR_SPECIAL:
2996 n = argvars[0].vval.v_number != VVAL_TRUE;
2997 break;
2998
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002999 case VAR_BLOB:
3000 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003001 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3002 break;
3003
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003004 case VAR_JOB:
3005#ifdef FEAT_JOB_CHANNEL
3006 n = argvars[0].vval.v_job == NULL
3007 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3008 break;
3009#endif
3010 case VAR_CHANNEL:
3011#ifdef FEAT_JOB_CHANNEL
3012 n = argvars[0].vval.v_channel == NULL
3013 || !channel_is_open(argvars[0].vval.v_channel);
3014 break;
3015#endif
3016 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003017 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003019 case VAR_INSTR:
Bram Moolenaardd589232020-02-29 17:38:12 +01003020 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003021 n = TRUE;
3022 break;
3023 }
3024
3025 rettv->vval.v_number = n;
3026}
3027
3028/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003029 * "environ()" function
3030 */
3031 static void
3032f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3033{
3034#if !defined(AMIGA)
3035 int i = 0;
3036 char_u *entry, *value;
3037# ifdef MSWIN
3038 extern wchar_t **_wenviron;
3039# else
3040 extern char **environ;
3041# endif
3042
3043 if (rettv_dict_alloc(rettv) != OK)
3044 return;
3045
3046# ifdef MSWIN
3047 if (*_wenviron == NULL)
3048 return;
3049# else
3050 if (*environ == NULL)
3051 return;
3052# endif
3053
3054 for (i = 0; ; ++i)
3055 {
3056# ifdef MSWIN
3057 short_u *p;
3058
3059 if ((p = (short_u *)_wenviron[i]) == NULL)
3060 return;
3061 entry = utf16_to_enc(p, NULL);
3062# else
3063 if ((entry = (char_u *)environ[i]) == NULL)
3064 return;
3065 entry = vim_strsave(entry);
3066# endif
3067 if (entry == NULL) // out of memory
3068 return;
3069 if ((value = vim_strchr(entry, '=')) == NULL)
3070 {
3071 vim_free(entry);
3072 continue;
3073 }
3074 *value++ = NUL;
3075 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3076 vim_free(entry);
3077 }
3078#endif
3079}
3080
3081/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003082 * "escape({string}, {chars})" function
3083 */
3084 static void
3085f_escape(typval_T *argvars, typval_T *rettv)
3086{
3087 char_u buf[NUMBUFLEN];
3088
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003089 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3090 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003091 rettv->v_type = VAR_STRING;
3092}
3093
3094/*
3095 * "eval()" function
3096 */
3097 static void
3098f_eval(typval_T *argvars, typval_T *rettv)
3099{
3100 char_u *s, *p;
3101
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003102 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003103 if (s != NULL)
3104 s = skipwhite(s);
3105
3106 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003107 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003108 {
3109 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003110 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003111 need_clr_eos = FALSE;
3112 rettv->v_type = VAR_NUMBER;
3113 rettv->vval.v_number = 0;
3114 }
3115 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003116 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003117}
3118
3119/*
3120 * "eventhandler()" function
3121 */
3122 static void
3123f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3124{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003125 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003126}
3127
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003128static garray_T redir_execute_ga;
3129
3130/*
3131 * Append "value[value_len]" to the execute() output.
3132 */
3133 void
3134execute_redir_str(char_u *value, int value_len)
3135{
3136 int len;
3137
3138 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003139 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003140 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003141 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003142 if (ga_grow(&redir_execute_ga, len) == OK)
3143 {
3144 mch_memmove((char *)redir_execute_ga.ga_data
3145 + redir_execute_ga.ga_len, value, len);
3146 redir_execute_ga.ga_len += len;
3147 }
3148}
3149
3150/*
3151 * Get next line from a list.
3152 * Called by do_cmdline() to get the next line.
3153 * Returns allocated string, or NULL for end of function.
3154 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003155 static char_u *
3156get_list_line(
3157 int c UNUSED,
3158 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003159 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02003160 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003161{
3162 listitem_T **p = (listitem_T **)cookie;
3163 listitem_T *item = *p;
3164 char_u buf[NUMBUFLEN];
3165 char_u *s;
3166
3167 if (item == NULL)
3168 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003169 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003170 *p = item->li_next;
3171 return s == NULL ? NULL : vim_strsave(s);
3172}
3173
3174/*
3175 * "execute()" function
3176 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02003177 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003178execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003179{
3180 char_u *cmd = NULL;
3181 list_T *list = NULL;
3182 int save_msg_silent = msg_silent;
3183 int save_emsg_silent = emsg_silent;
3184 int save_emsg_noredir = emsg_noredir;
3185 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003186 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003187 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003188 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003189 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003190
3191 rettv->vval.v_string = NULL;
3192 rettv->v_type = VAR_STRING;
3193
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003194 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003195 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003196 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003197 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003198 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003199 return;
3200 ++list->lv_refcount;
3201 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003202 else if (argvars[arg_off].v_type == VAR_JOB
3203 || argvars[arg_off].v_type == VAR_CHANNEL)
3204 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02003205 semsg(_(e_using_invalid_value_as_string_str),
3206 vartype_name(argvars[arg_off].v_type));
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003207 return;
3208 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003209 else
3210 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003211 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003212 if (cmd == NULL)
3213 return;
3214 }
3215
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003216 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003217 {
3218 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003219 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003220
3221 if (s == NULL)
3222 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003223 if (*s == NUL)
3224 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003225 if (STRNCMP(s, "silent", 6) == 0)
3226 ++msg_silent;
3227 if (STRCMP(s, "silent!") == 0)
3228 {
3229 emsg_silent = TRUE;
3230 emsg_noredir = TRUE;
3231 }
3232 }
3233 else
3234 ++msg_silent;
3235
3236 if (redir_execute)
3237 save_ga = redir_execute_ga;
3238 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3239 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003240 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003241 if (!echo_output)
3242 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003243
3244 if (cmd != NULL)
3245 do_cmdline_cmd(cmd);
3246 else
3247 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003248 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003249
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003250 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003251 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003252 do_cmdline(NULL, get_list_line, (void *)&item,
3253 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3254 --list->lv_refcount;
3255 }
3256
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003257 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003258 if (ga_grow(&redir_execute_ga, 1) == OK)
3259 {
3260 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3261 rettv->vval.v_string = redir_execute_ga.ga_data;
3262 }
3263 else
3264 {
3265 ga_clear(&redir_execute_ga);
3266 rettv->vval.v_string = NULL;
3267 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003268 msg_silent = save_msg_silent;
3269 emsg_silent = save_emsg_silent;
3270 emsg_noredir = save_emsg_noredir;
3271
3272 redir_execute = save_redir_execute;
3273 if (redir_execute)
3274 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003275 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003276
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003277 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003278 if (echo_output)
3279 // When not working silently: put it in column zero. A following
3280 // "echon" will overwrite the message, unavoidably.
3281 msg_col = 0;
3282 else
3283 // When working silently: Put it back where it was, since nothing
3284 // should have been written.
3285 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003286}
3287
3288/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003289 * "execute()" function
3290 */
3291 static void
3292f_execute(typval_T *argvars, typval_T *rettv)
3293{
3294 execute_common(argvars, rettv, 0);
3295}
3296
3297/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003298 * "exists()" function
3299 */
3300 static void
3301f_exists(typval_T *argvars, typval_T *rettv)
3302{
3303 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003304 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003305
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003306 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003307 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003308 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003309 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003310 if (mch_getenv(p + 1) != NULL)
3311 n = TRUE;
3312 else
3313 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003314 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003315 p = expand_env_save(p);
3316 if (p != NULL && *p != '$')
3317 n = TRUE;
3318 vim_free(p);
3319 }
3320 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003321 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003322 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003323 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003324 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003325 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003326 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003327 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003328 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003329 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003330 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003331 else if (*p == '?') // internal function only
3332 {
3333 n = has_internal_func_name(p + 1);
3334 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003335 else if (*p == ':')
3336 {
3337 n = cmd_exists(p + 1);
3338 }
3339 else if (*p == '#')
3340 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003341 if (p[1] == '#')
3342 n = autocmd_supported(p + 2);
3343 else
3344 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003345 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003346 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003347 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003348 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003349 }
3350
3351 rettv->vval.v_number = n;
3352}
3353
3354#ifdef FEAT_FLOAT
3355/*
3356 * "exp()" function
3357 */
3358 static void
3359f_exp(typval_T *argvars, typval_T *rettv)
3360{
3361 float_T f = 0.0;
3362
3363 rettv->v_type = VAR_FLOAT;
3364 if (get_float_arg(argvars, &f) == OK)
3365 rettv->vval.v_float = exp(f);
3366 else
3367 rettv->vval.v_float = 0.0;
3368}
3369#endif
3370
3371/*
3372 * "expand()" function
3373 */
3374 static void
3375f_expand(typval_T *argvars, typval_T *rettv)
3376{
3377 char_u *s;
3378 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003379 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003380 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3381 expand_T xpc;
3382 int error = FALSE;
3383 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003384#ifdef BACKSLASH_IN_FILENAME
3385 char_u *p_csl_save = p_csl;
3386
3387 // avoid using 'completeslash' here
3388 p_csl = empty_option;
3389#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003390
3391 rettv->v_type = VAR_STRING;
3392 if (argvars[1].v_type != VAR_UNKNOWN
3393 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003394 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003395 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003396 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003397
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003398 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003399 if (*s == '%' || *s == '#' || *s == '<')
3400 {
3401 ++emsg_off;
3402 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3403 --emsg_off;
3404 if (rettv->v_type == VAR_LIST)
3405 {
3406 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3407 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003408 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003409 }
3410 else
3411 rettv->vval.v_string = result;
3412 }
3413 else
3414 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003415 // When the optional second argument is non-zero, don't remove matches
3416 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003417 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003418 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003419 options |= WILD_KEEP_ALL;
3420 if (!error)
3421 {
3422 ExpandInit(&xpc);
3423 xpc.xp_context = EXPAND_FILES;
3424 if (p_wic)
3425 options += WILD_ICASE;
3426 if (rettv->v_type == VAR_STRING)
3427 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3428 options, WILD_ALL);
3429 else if (rettv_list_alloc(rettv) != FAIL)
3430 {
3431 int i;
3432
3433 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3434 for (i = 0; i < xpc.xp_numfiles; i++)
3435 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3436 ExpandCleanup(&xpc);
3437 }
3438 }
3439 else
3440 rettv->vval.v_string = NULL;
3441 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003442#ifdef BACKSLASH_IN_FILENAME
3443 p_csl = p_csl_save;
3444#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003445}
3446
3447/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003448 * "expandcmd()" function
3449 * Expand all the special characters in a command string.
3450 */
3451 static void
3452f_expandcmd(typval_T *argvars, typval_T *rettv)
3453{
3454 exarg_T eap;
3455 char_u *cmdstr;
3456 char *errormsg = NULL;
3457
3458 rettv->v_type = VAR_STRING;
3459 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3460
3461 memset(&eap, 0, sizeof(eap));
3462 eap.cmd = cmdstr;
3463 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003464 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003465 eap.usefilter = FALSE;
3466 eap.nextcmd = NULL;
3467 eap.cmdidx = CMD_USER;
3468
3469 expand_filename(&eap, &cmdstr, &errormsg);
3470 if (errormsg != NULL && *errormsg != NUL)
3471 emsg(errormsg);
3472
3473 rettv->vval.v_string = cmdstr;
3474}
3475
3476/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003477 * "feedkeys()" function
3478 */
3479 static void
3480f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3481{
3482 int remap = TRUE;
3483 int insert = FALSE;
3484 char_u *keys, *flags;
3485 char_u nbuf[NUMBUFLEN];
3486 int typed = FALSE;
3487 int execute = FALSE;
3488 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003489 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003490 char_u *keys_esc;
3491
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003492 // This is not allowed in the sandbox. If the commands would still be
3493 // executed in the sandbox it would be OK, but it probably happens later,
3494 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003495 if (check_secure())
3496 return;
3497
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003498 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003499
3500 if (argvars[1].v_type != VAR_UNKNOWN)
3501 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003502 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003503 for ( ; *flags != NUL; ++flags)
3504 {
3505 switch (*flags)
3506 {
3507 case 'n': remap = FALSE; break;
3508 case 'm': remap = TRUE; break;
3509 case 't': typed = TRUE; break;
3510 case 'i': insert = TRUE; break;
3511 case 'x': execute = TRUE; break;
3512 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003513 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003514 }
3515 }
3516 }
3517
3518 if (*keys != NUL || execute)
3519 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003520 // Need to escape K_SPECIAL and CSI before putting the string in the
3521 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003522 keys_esc = vim_strsave_escape_csi(keys);
3523 if (keys_esc != NULL)
3524 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003525 if (lowlevel)
3526 {
3527#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003528 int idx;
3529 int len = (int)STRLEN(keys);
3530
3531 for (idx = 0; idx < len; ++idx)
3532 {
3533 // if a CTRL-C was typed, set got_int, similar to what
3534 // happens in fill_input_buf()
3535 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3536 got_int = TRUE;
3537 add_to_input_buf(keys + idx, 1);
3538 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003539#else
3540 emsg(_("E980: lowlevel input not supported"));
3541#endif
3542 }
3543 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003544 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003545 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003546 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003547 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003548#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003549 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003550#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003551 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003552 typebuf_was_filled = TRUE;
3553 }
3554 vim_free(keys_esc);
3555
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003556 if (execute)
3557 {
3558 int save_msg_scroll = msg_scroll;
3559
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003560 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561 msg_scroll = FALSE;
3562
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003563 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003564 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003565 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003566 ++in_feedkeys;
3567 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003568 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003569 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003570 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003571 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003572 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003573 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003574
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003575 msg_scroll |= save_msg_scroll;
3576 }
3577 }
3578 }
3579}
3580
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003581#ifdef FEAT_FLOAT
3582/*
3583 * "float2nr({float})" function
3584 */
3585 static void
3586f_float2nr(typval_T *argvars, typval_T *rettv)
3587{
3588 float_T f = 0.0;
3589
3590 if (get_float_arg(argvars, &f) == OK)
3591 {
Bram Moolenaar37184272020-05-23 19:30:05 +02003592 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003593 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02003594 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003595 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003596 else
3597 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003598 }
3599}
3600
3601/*
3602 * "floor({float})" function
3603 */
3604 static void
3605f_floor(typval_T *argvars, typval_T *rettv)
3606{
3607 float_T f = 0.0;
3608
3609 rettv->v_type = VAR_FLOAT;
3610 if (get_float_arg(argvars, &f) == OK)
3611 rettv->vval.v_float = floor(f);
3612 else
3613 rettv->vval.v_float = 0.0;
3614}
3615
3616/*
3617 * "fmod()" function
3618 */
3619 static void
3620f_fmod(typval_T *argvars, typval_T *rettv)
3621{
3622 float_T fx = 0.0, fy = 0.0;
3623
3624 rettv->v_type = VAR_FLOAT;
3625 if (get_float_arg(argvars, &fx) == OK
3626 && get_float_arg(&argvars[1], &fy) == OK)
3627 rettv->vval.v_float = fmod(fx, fy);
3628 else
3629 rettv->vval.v_float = 0.0;
3630}
3631#endif
3632
3633/*
3634 * "fnameescape({string})" function
3635 */
3636 static void
3637f_fnameescape(typval_T *argvars, typval_T *rettv)
3638{
3639 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003640 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003641 rettv->v_type = VAR_STRING;
3642}
3643
3644/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003645 * "foreground()" function
3646 */
3647 static void
3648f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3649{
3650#ifdef FEAT_GUI
3651 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003652 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003653 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003654 return;
3655 }
3656#endif
3657#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003658 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003659#endif
3660}
3661
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003662 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003663common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003664{
3665 char_u *s;
3666 char_u *name;
3667 int use_string = FALSE;
3668 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003669 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003670 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003671
3672 if (argvars[0].v_type == VAR_FUNC)
3673 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003674 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003675 s = argvars[0].vval.v_string;
3676 }
3677 else if (argvars[0].v_type == VAR_PARTIAL
3678 && argvars[0].vval.v_partial != NULL)
3679 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003680 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003681 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003682 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003683 }
3684 else
3685 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003686 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003687 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003688 use_string = TRUE;
3689 }
3690
Bram Moolenaar843b8842016-08-21 14:36:15 +02003691 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003692 {
3693 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003694 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003695 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3696 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003697 if (*name != NUL)
3698 s = NULL;
3699 }
3700
Bram Moolenaar843b8842016-08-21 14:36:15 +02003701 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3702 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003703 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003704 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003705 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003706 ? find_func(trans_name, is_global, NULL) == NULL
3707 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003708 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003709 else
3710 {
3711 int dict_idx = 0;
3712 int arg_idx = 0;
3713 list_T *list = NULL;
3714
3715 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3716 {
3717 char sid_buf[25];
3718 int off = *s == 's' ? 2 : 5;
3719
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003720 // Expand s: and <SID> into <SNR>nr_, so that the function can
3721 // also be called from another script. Using trans_function_name()
3722 // would also work, but some plugins depend on the name being
3723 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003724 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003725 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003726 if (name != NULL)
3727 {
3728 STRCPY(name, sid_buf);
3729 STRCAT(name, s + off);
3730 }
3731 }
3732 else
3733 name = vim_strsave(s);
3734
3735 if (argvars[1].v_type != VAR_UNKNOWN)
3736 {
3737 if (argvars[2].v_type != VAR_UNKNOWN)
3738 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003739 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003740 arg_idx = 1;
3741 dict_idx = 2;
3742 }
3743 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003744 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003745 dict_idx = 1;
3746 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003747 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003748 arg_idx = 1;
3749 if (dict_idx > 0)
3750 {
3751 if (argvars[dict_idx].v_type != VAR_DICT)
3752 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003753 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003754 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003755 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003756 }
3757 if (argvars[dict_idx].vval.v_dict == NULL)
3758 dict_idx = 0;
3759 }
3760 if (arg_idx > 0)
3761 {
3762 if (argvars[arg_idx].v_type != VAR_LIST)
3763 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003764 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003765 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003766 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003767 }
3768 list = argvars[arg_idx].vval.v_list;
3769 if (list == NULL || list->lv_len == 0)
3770 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003771 else if (list->lv_len > MAX_FUNC_ARGS)
3772 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003773 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003774 vim_free(name);
3775 goto theend;
3776 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003777 }
3778 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003779 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003780 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003781 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003782
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003783 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003784 if (pt == NULL)
3785 vim_free(name);
3786 else
3787 {
3788 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3789 {
3790 listitem_T *li;
3791 int i = 0;
3792 int arg_len = 0;
3793 int lv_len = 0;
3794
3795 if (arg_pt != NULL)
3796 arg_len = arg_pt->pt_argc;
3797 if (list != NULL)
3798 lv_len = list->lv_len;
3799 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003800 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003801 if (pt->pt_argv == NULL)
3802 {
3803 vim_free(pt);
3804 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003805 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003806 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003807 for (i = 0; i < arg_len; i++)
3808 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3809 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003810 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003811 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003812 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003813 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003814 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003815 }
3816
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003817 // For "function(dict.func, [], dict)" and "func" is a partial
3818 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003819 if (dict_idx > 0)
3820 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003821 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003822 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3823 ++pt->pt_dict->dv_refcount;
3824 }
3825 else if (arg_pt != NULL)
3826 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003827 // If the dict was bound automatically the result is also
3828 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003829 pt->pt_dict = arg_pt->pt_dict;
3830 pt->pt_auto = arg_pt->pt_auto;
3831 if (pt->pt_dict != NULL)
3832 ++pt->pt_dict->dv_refcount;
3833 }
3834
3835 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003836 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3837 {
3838 pt->pt_func = arg_pt->pt_func;
3839 func_ptr_ref(pt->pt_func);
3840 vim_free(name);
3841 }
3842 else if (is_funcref)
3843 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003844 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003845 func_ptr_ref(pt->pt_func);
3846 vim_free(name);
3847 }
3848 else
3849 {
3850 pt->pt_name = name;
3851 func_ref(name);
3852 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003853 }
3854 rettv->v_type = VAR_PARTIAL;
3855 rettv->vval.v_partial = pt;
3856 }
3857 else
3858 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003859 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003860 rettv->v_type = VAR_FUNC;
3861 rettv->vval.v_string = name;
3862 func_ref(name);
3863 }
3864 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003865theend:
3866 vim_free(trans_name);
3867}
3868
3869/*
3870 * "funcref()" function
3871 */
3872 static void
3873f_funcref(typval_T *argvars, typval_T *rettv)
3874{
3875 common_function(argvars, rettv, TRUE);
3876}
3877
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003878 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003879ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003880{
3881 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3882 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003883 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003884}
3885
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003886/*
3887 * "function()" function
3888 */
3889 static void
3890f_function(typval_T *argvars, typval_T *rettv)
3891{
3892 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003893}
3894
3895/*
3896 * "garbagecollect()" function
3897 */
3898 static void
3899f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3900{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003901 // This is postponed until we are back at the toplevel, because we may be
3902 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003903 want_garbage_collect = TRUE;
3904
Bram Moolenaar2df47312020-09-05 17:30:44 +02003905 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003906 garbage_collect_at_exit = TRUE;
3907}
3908
3909/*
3910 * "get()" function
3911 */
3912 static void
3913f_get(typval_T *argvars, typval_T *rettv)
3914{
3915 listitem_T *li;
3916 list_T *l;
3917 dictitem_T *di;
3918 dict_T *d;
3919 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003920 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003921
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003922 if (argvars[0].v_type == VAR_BLOB)
3923 {
3924 int error = FALSE;
3925 int idx = tv_get_number_chk(&argvars[1], &error);
3926
3927 if (!error)
3928 {
3929 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003930 if (idx < 0)
3931 idx = blob_len(argvars[0].vval.v_blob) + idx;
3932 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3933 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003934 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003935 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003936 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003937 tv = rettv;
3938 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003939 }
3940 }
3941 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003942 {
3943 if ((l = argvars[0].vval.v_list) != NULL)
3944 {
3945 int error = FALSE;
3946
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003947 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003948 if (!error && li != NULL)
3949 tv = &li->li_tv;
3950 }
3951 }
3952 else if (argvars[0].v_type == VAR_DICT)
3953 {
3954 if ((d = argvars[0].vval.v_dict) != NULL)
3955 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003956 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003957 if (di != NULL)
3958 tv = &di->di_tv;
3959 }
3960 }
3961 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3962 {
3963 partial_T *pt;
3964 partial_T fref_pt;
3965
3966 if (argvars[0].v_type == VAR_PARTIAL)
3967 pt = argvars[0].vval.v_partial;
3968 else
3969 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003970 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003971 fref_pt.pt_name = argvars[0].vval.v_string;
3972 pt = &fref_pt;
3973 }
3974
3975 if (pt != NULL)
3976 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003977 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003978 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003979
3980 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3981 {
3982 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003983 n = partial_name(pt);
3984 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003985 rettv->vval.v_string = NULL;
3986 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003987 {
3988 rettv->vval.v_string = vim_strsave(n);
3989 if (rettv->v_type == VAR_FUNC)
3990 func_ref(rettv->vval.v_string);
3991 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003992 }
3993 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003994 {
3995 what_is_dict = TRUE;
3996 if (pt->pt_dict != NULL)
3997 rettv_dict_set(rettv, pt->pt_dict);
3998 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003999 else if (STRCMP(what, "args") == 0)
4000 {
4001 rettv->v_type = VAR_LIST;
4002 if (rettv_list_alloc(rettv) == OK)
4003 {
4004 int i;
4005
4006 for (i = 0; i < pt->pt_argc; ++i)
4007 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4008 }
4009 }
4010 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004011 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02004012
4013 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
4014 // third argument
4015 if (!what_is_dict)
4016 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004017 }
4018 }
4019 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004020 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004021
4022 if (tv == NULL)
4023 {
4024 if (argvars[2].v_type != VAR_UNKNOWN)
4025 copy_tv(&argvars[2], rettv);
4026 }
4027 else
4028 copy_tv(tv, rettv);
4029}
4030
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004031/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004032 * "getchangelist()" function
4033 */
4034 static void
4035f_getchangelist(typval_T *argvars, typval_T *rettv)
4036{
4037#ifdef FEAT_JUMPLIST
4038 buf_T *buf;
4039 int i;
4040 list_T *l;
4041 dict_T *d;
4042#endif
4043
4044 if (rettv_list_alloc(rettv) != OK)
4045 return;
4046
4047#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02004048 if (argvars[0].v_type == VAR_UNKNOWN)
4049 buf = curbuf;
4050 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02004051 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004052 if (buf == NULL)
4053 return;
4054
4055 l = list_alloc();
4056 if (l == NULL)
4057 return;
4058
4059 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4060 return;
4061 /*
4062 * The current window change list index tracks only the position in the
4063 * current buffer change list. For other buffers, use the change list
4064 * length as the current index.
4065 */
4066 list_append_number(rettv->vval.v_list,
4067 (varnumber_T)((buf == curwin->w_buffer)
4068 ? curwin->w_changelistidx : buf->b_changelistlen));
4069
4070 for (i = 0; i < buf->b_changelistlen; ++i)
4071 {
4072 if (buf->b_changelist[i].lnum == 0)
4073 continue;
4074 if ((d = dict_alloc()) == NULL)
4075 return;
4076 if (list_append_dict(l, d) == FAIL)
4077 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004078 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4079 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004080 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004081 }
4082#endif
4083}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004084
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004085 static void
4086getpos_both(
4087 typval_T *argvars,
4088 typval_T *rettv,
4089 int getcurpos,
4090 int charcol)
4091{
4092 pos_T *fp = NULL;
4093 pos_T pos;
4094 win_T *wp = curwin;
4095 list_T *l;
4096 int fnum = -1;
4097
4098 if (rettv_list_alloc(rettv) == OK)
4099 {
4100 l = rettv->vval.v_list;
4101 if (getcurpos)
4102 {
4103 if (argvars[0].v_type != VAR_UNKNOWN)
4104 {
4105 wp = find_win_by_nr_or_id(&argvars[0]);
4106 if (wp != NULL)
4107 fp = &wp->w_cursor;
4108 }
4109 else
4110 fp = &curwin->w_cursor;
4111 if (fp != NULL && charcol)
4112 {
4113 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01004114 pos.col =
4115 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004116 fp = &pos;
4117 }
4118 }
4119 else
4120 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
4121 if (fnum != -1)
4122 list_append_number(l, (varnumber_T)fnum);
4123 else
4124 list_append_number(l, (varnumber_T)0);
4125 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4126 : (varnumber_T)0);
4127 list_append_number(l, (fp != NULL)
4128 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4129 : (varnumber_T)0);
4130 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
4131 (varnumber_T)0);
4132 if (getcurpos)
4133 {
4134 int save_set_curswant = curwin->w_set_curswant;
4135 colnr_T save_curswant = curwin->w_curswant;
4136 colnr_T save_virtcol = curwin->w_virtcol;
4137
4138 if (wp == curwin)
4139 update_curswant();
4140 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
4141 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
4142
4143 // Do not change "curswant", as it is unexpected that a get
4144 // function has a side effect.
4145 if (wp == curwin && save_set_curswant)
4146 {
4147 curwin->w_set_curswant = save_set_curswant;
4148 curwin->w_curswant = save_curswant;
4149 curwin->w_virtcol = save_virtcol;
4150 curwin->w_valid &= ~VALID_VIRTCOL;
4151 }
4152 }
4153 }
4154 else
4155 rettv->vval.v_number = FALSE;
4156}
4157
4158/*
4159 * "getcharpos()" function
4160 */
4161 static void
4162f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
4163{
4164 getpos_both(argvars, rettv, FALSE, TRUE);
4165}
4166
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004167/*
4168 * "getcharsearch()" function
4169 */
4170 static void
4171f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4172{
4173 if (rettv_dict_alloc(rettv) != FAIL)
4174 {
4175 dict_T *dict = rettv->vval.v_dict;
4176
Bram Moolenaare0be1672018-07-08 16:50:37 +02004177 dict_add_string(dict, "char", last_csearch());
4178 dict_add_number(dict, "forward", last_csearch_forward());
4179 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004180 }
4181}
4182
4183/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004184 * "getenv()" function
4185 */
4186 static void
4187f_getenv(typval_T *argvars, typval_T *rettv)
4188{
4189 int mustfree = FALSE;
4190 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4191
4192 if (p == NULL)
4193 {
4194 rettv->v_type = VAR_SPECIAL;
4195 rettv->vval.v_number = VVAL_NULL;
4196 return;
4197 }
4198 if (!mustfree)
4199 p = vim_strsave(p);
4200 rettv->vval.v_string = p;
4201 rettv->v_type = VAR_STRING;
4202}
4203
4204/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004205 * "getfontname()" function
4206 */
4207 static void
4208f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4209{
4210 rettv->v_type = VAR_STRING;
4211 rettv->vval.v_string = NULL;
4212#ifdef FEAT_GUI
4213 if (gui.in_use)
4214 {
4215 GuiFont font;
4216 char_u *name = NULL;
4217
4218 if (argvars[0].v_type == VAR_UNKNOWN)
4219 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004220 // Get the "Normal" font. Either the name saved by
4221 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004222 font = gui.norm_font;
4223 name = hl_get_font_name();
4224 }
4225 else
4226 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004227 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004228 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004229 return;
4230 font = gui_mch_get_font(name, FALSE);
4231 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004232 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004233 }
4234 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4235 if (argvars[0].v_type != VAR_UNKNOWN)
4236 gui_mch_free_font(font);
4237 }
4238#endif
4239}
4240
4241/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004242 * "getjumplist()" function
4243 */
4244 static void
4245f_getjumplist(typval_T *argvars, typval_T *rettv)
4246{
4247#ifdef FEAT_JUMPLIST
4248 win_T *wp;
4249 int i;
4250 list_T *l;
4251 dict_T *d;
4252#endif
4253
4254 if (rettv_list_alloc(rettv) != OK)
4255 return;
4256
4257#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004258 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004259 if (wp == NULL)
4260 return;
4261
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004262 cleanup_jumplist(wp, TRUE);
4263
Bram Moolenaar4f505882018-02-10 21:06:32 +01004264 l = list_alloc();
4265 if (l == NULL)
4266 return;
4267
4268 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4269 return;
4270 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4271
4272 for (i = 0; i < wp->w_jumplistlen; ++i)
4273 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004274 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4275 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004276 if ((d = dict_alloc()) == NULL)
4277 return;
4278 if (list_append_dict(l, d) == FAIL)
4279 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004280 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4281 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004282 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004283 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004284 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004285 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004286 }
4287#endif
4288}
4289
4290/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004291 * "getpid()" function
4292 */
4293 static void
4294f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4295{
4296 rettv->vval.v_number = mch_get_pid();
4297}
4298
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004299/*
4300 * "getcurpos()" function
4301 */
4302 static void
4303f_getcurpos(typval_T *argvars, typval_T *rettv)
4304{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004305 getpos_both(argvars, rettv, TRUE, FALSE);
4306}
4307
4308 static void
4309f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4310{
4311 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004312}
4313
4314/*
4315 * "getpos(string)" function
4316 */
4317 static void
4318f_getpos(typval_T *argvars, typval_T *rettv)
4319{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004320 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004321}
4322
4323/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004324 * "getreg()" function
4325 */
4326 static void
4327f_getreg(typval_T *argvars, typval_T *rettv)
4328{
4329 char_u *strregname;
4330 int regname;
4331 int arg2 = FALSE;
4332 int return_list = FALSE;
4333 int error = FALSE;
4334
4335 if (argvars[0].v_type != VAR_UNKNOWN)
4336 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004337 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004338 if (strregname == NULL)
4339 error = TRUE;
4340 else if (in_vim9script() && STRLEN(strregname) > 1)
4341 {
4342 semsg(_(e_register_name_must_be_one_char_str), strregname);
4343 error = TRUE;
4344 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004345 if (argvars[1].v_type != VAR_UNKNOWN)
4346 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004347 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004348 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004349 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004350 }
4351 }
4352 else
4353 strregname = get_vim_var_str(VV_REG);
4354
4355 if (error)
4356 return;
4357
4358 regname = (strregname == NULL ? '"' : *strregname);
4359 if (regname == 0)
4360 regname = '"';
4361
4362 if (return_list)
4363 {
4364 rettv->v_type = VAR_LIST;
4365 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4366 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4367 if (rettv->vval.v_list == NULL)
4368 (void)rettv_list_alloc(rettv);
4369 else
4370 ++rettv->vval.v_list->lv_refcount;
4371 }
4372 else
4373 {
4374 rettv->v_type = VAR_STRING;
4375 rettv->vval.v_string = get_reg_contents(regname,
4376 arg2 ? GREG_EXPR_SRC : 0);
4377 }
4378}
4379
4380/*
4381 * "getregtype()" function
4382 */
4383 static void
4384f_getregtype(typval_T *argvars, typval_T *rettv)
4385{
4386 char_u *strregname;
4387 int regname;
4388 char_u buf[NUMBUFLEN + 2];
4389 long reglen = 0;
4390
4391 if (argvars[0].v_type != VAR_UNKNOWN)
4392 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004393 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004394 if (strregname != NULL && in_vim9script() && STRLEN(strregname) > 1)
4395 {
4396 semsg(_(e_register_name_must_be_one_char_str), strregname);
4397 strregname = NULL;
4398 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004399 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004400 {
4401 rettv->v_type = VAR_STRING;
4402 rettv->vval.v_string = NULL;
4403 return;
4404 }
4405 }
4406 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004407 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004408 strregname = get_vim_var_str(VV_REG);
4409
4410 regname = (strregname == NULL ? '"' : *strregname);
4411 if (regname == 0)
4412 regname = '"';
4413
4414 buf[0] = NUL;
4415 buf[1] = NUL;
4416 switch (get_reg_type(regname, &reglen))
4417 {
4418 case MLINE: buf[0] = 'V'; break;
4419 case MCHAR: buf[0] = 'v'; break;
4420 case MBLOCK:
4421 buf[0] = Ctrl_V;
4422 sprintf((char *)buf + 1, "%ld", reglen + 1);
4423 break;
4424 }
4425 rettv->v_type = VAR_STRING;
4426 rettv->vval.v_string = vim_strsave(buf);
4427}
4428
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004429/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004430 * "gettagstack()" function
4431 */
4432 static void
4433f_gettagstack(typval_T *argvars, typval_T *rettv)
4434{
4435 win_T *wp = curwin; // default is current window
4436
4437 if (rettv_dict_alloc(rettv) != OK)
4438 return;
4439
4440 if (argvars[0].v_type != VAR_UNKNOWN)
4441 {
4442 wp = find_win_by_nr_or_id(&argvars[0]);
4443 if (wp == NULL)
4444 return;
4445 }
4446
4447 get_tagstack(wp, rettv->vval.v_dict);
4448}
4449
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004450/*
4451 * "gettext()" function
4452 */
4453 static void
4454f_gettext(typval_T *argvars, typval_T *rettv)
4455{
4456 if (argvars[0].v_type != VAR_STRING
4457 || argvars[0].vval.v_string == NULL
4458 || *argvars[0].vval.v_string == NUL)
4459 {
4460 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4461 }
4462 else
4463 {
4464 rettv->v_type = VAR_STRING;
4465 rettv->vval.v_string = vim_strsave(
4466 (char_u *)_(argvars[0].vval.v_string));
4467 }
4468}
4469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004470// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004471#include "version.h"
4472
4473/*
4474 * "has()" function
4475 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004476 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004477f_has(typval_T *argvars, typval_T *rettv)
4478{
4479 int i;
4480 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004481 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004482 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004483 typedef struct {
4484 char *name;
4485 short present;
4486 } has_item_T;
4487 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004488 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004489 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004490#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004491 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004492#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004493 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004494#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004495 },
4496 {"arp",
4497#if defined(AMIGA) && defined(FEAT_ARP)
4498 1
4499#else
4500 0
4501#endif
4502 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004503 {"haiku",
4504#ifdef __HAIKU__
4505 1
4506#else
4507 0
4508#endif
4509 },
4510 {"bsd",
4511#if defined(BSD) && !defined(MACOS_X)
4512 1
4513#else
4514 0
4515#endif
4516 },
4517 {"hpux",
4518#ifdef hpux
4519 1
4520#else
4521 0
4522#endif
4523 },
4524 {"linux",
4525#ifdef __linux__
4526 1
4527#else
4528 0
4529#endif
4530 },
4531 {"mac", // Mac OS X (and, once, Mac OS Classic)
4532#ifdef MACOS_X
4533 1
4534#else
4535 0
4536#endif
4537 },
4538 {"osx", // Mac OS X
4539#ifdef MACOS_X
4540 1
4541#else
4542 0
4543#endif
4544 },
4545 {"macunix", // Mac OS X, with the darwin feature
4546#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4547 1
4548#else
4549 0
4550#endif
4551 },
4552 {"osxdarwin", // synonym for macunix
4553#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4554 1
4555#else
4556 0
4557#endif
4558 },
4559 {"qnx",
4560#ifdef __QNX__
4561 1
4562#else
4563 0
4564#endif
4565 },
4566 {"sun",
4567#ifdef SUN_SYSTEM
4568 1
4569#else
4570 0
4571#endif
4572 },
4573 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004574#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004575 1
4576#else
4577 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004578#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004579 },
4580 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004581#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004582 1
4583#else
4584 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004585#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004586 },
4587 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004588#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004589 1
4590#else
4591 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004592#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004593 },
4594 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004595#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004596 1
4597#else
4598 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004599#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004600 },
4601 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004602#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004603 1
4604#else
4605 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004606#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004607 },
4608 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004609#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004610 1
4611#else
4612 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004613#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004614 },
4615 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004616#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004617 1
4618#else
4619 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004620#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004621 },
4622 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004623#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004624 1
4625#else
4626 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004627#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004628 },
4629 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004630#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004631 1
4632#else
4633 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004634#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004635 },
4636 {"autocmd", 1},
4637 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004638#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004639 1
4640#else
4641 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004642#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004643 },
4644 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004645#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004646 1
4647#else
4648 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004649#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004650 },
4651 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004652#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004653 1
4654#else
4655 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004656#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004657 },
4658 {"balloon_multiline",
4659#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4660 // MS-Windows requires runtime check, see below
4661 1
4662#else
4663 0
4664#endif
4665 },
4666 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004667#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004668 1
4669#else
4670 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004671#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004672 },
4673 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004674#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004675 1
4676#else
4677 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004678#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004679 },
4680 {"all_builtin_terms",
4681#if defined(ALL_BUILTIN_TCAPS)
4682 1
4683#else
4684 0
4685#endif
4686 },
4687 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004688#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004689 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004690 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004691 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004692#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004693 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004694#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004695 },
4696 {"byte_offset",
4697#ifdef FEAT_BYTEOFF
4698 1
4699#else
4700 0
4701#endif
4702 },
4703 {"channel",
4704#ifdef FEAT_JOB_CHANNEL
4705 1
4706#else
4707 0
4708#endif
4709 },
4710 {"cindent",
4711#ifdef FEAT_CINDENT
4712 1
4713#else
4714 0
4715#endif
4716 },
4717 {"clientserver",
4718#ifdef FEAT_CLIENTSERVER
4719 1
4720#else
4721 0
4722#endif
4723 },
4724 {"clipboard",
4725#ifdef FEAT_CLIPBOARD
4726 1
4727#else
4728 0
4729#endif
4730 },
4731 {"cmdline_compl", 1},
4732 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004733 {"cmdwin",
4734#ifdef FEAT_CMDWIN
4735 1
4736#else
4737 0
4738#endif
4739 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004740 {"comments", 1},
4741 {"conceal",
4742#ifdef FEAT_CONCEAL
4743 1
4744#else
4745 0
4746#endif
4747 },
4748 {"cryptv",
4749#ifdef FEAT_CRYPT
4750 1
4751#else
4752 0
4753#endif
4754 },
4755 {"crypt-blowfish",
4756#ifdef FEAT_CRYPT
4757 1
4758#else
4759 0
4760#endif
4761 },
4762 {"crypt-blowfish2",
4763#ifdef FEAT_CRYPT
4764 1
4765#else
4766 0
4767#endif
4768 },
4769 {"cscope",
4770#ifdef FEAT_CSCOPE
4771 1
4772#else
4773 0
4774#endif
4775 },
4776 {"cursorbind", 1},
4777 {"cursorshape",
4778#ifdef CURSOR_SHAPE
4779 1
4780#else
4781 0
4782#endif
4783 },
4784 {"debug",
4785#ifdef DEBUG
4786 1
4787#else
4788 0
4789#endif
4790 },
4791 {"dialog_con",
4792#ifdef FEAT_CON_DIALOG
4793 1
4794#else
4795 0
4796#endif
4797 },
4798 {"dialog_gui",
4799#ifdef FEAT_GUI_DIALOG
4800 1
4801#else
4802 0
4803#endif
4804 },
4805 {"diff",
4806#ifdef FEAT_DIFF
4807 1
4808#else
4809 0
4810#endif
4811 },
4812 {"digraphs",
4813#ifdef FEAT_DIGRAPHS
4814 1
4815#else
4816 0
4817#endif
4818 },
4819 {"directx",
4820#ifdef FEAT_DIRECTX
4821 1
4822#else
4823 0
4824#endif
4825 },
4826 {"dnd",
4827#ifdef FEAT_DND
4828 1
4829#else
4830 0
4831#endif
4832 },
4833 {"emacs_tags",
4834#ifdef FEAT_EMACS_TAGS
4835 1
4836#else
4837 0
4838#endif
4839 },
4840 {"eval", 1}, // always present, of course!
4841 {"ex_extra", 1}, // graduated feature
4842 {"extra_search",
4843#ifdef FEAT_SEARCH_EXTRA
4844 1
4845#else
4846 0
4847#endif
4848 },
4849 {"file_in_path",
4850#ifdef FEAT_SEARCHPATH
4851 1
4852#else
4853 0
4854#endif
4855 },
4856 {"filterpipe",
4857#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4858 1
4859#else
4860 0
4861#endif
4862 },
4863 {"find_in_path",
4864#ifdef FEAT_FIND_ID
4865 1
4866#else
4867 0
4868#endif
4869 },
4870 {"float",
4871#ifdef FEAT_FLOAT
4872 1
4873#else
4874 0
4875#endif
4876 },
4877 {"folding",
4878#ifdef FEAT_FOLDING
4879 1
4880#else
4881 0
4882#endif
4883 },
4884 {"footer",
4885#ifdef FEAT_FOOTER
4886 1
4887#else
4888 0
4889#endif
4890 },
4891 {"fork",
4892#if !defined(USE_SYSTEM) && defined(UNIX)
4893 1
4894#else
4895 0
4896#endif
4897 },
4898 {"gettext",
4899#ifdef FEAT_GETTEXT
4900 1
4901#else
4902 0
4903#endif
4904 },
4905 {"gui",
4906#ifdef FEAT_GUI
4907 1
4908#else
4909 0
4910#endif
4911 },
4912 {"gui_neXtaw",
4913#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4914 1
4915#else
4916 0
4917#endif
4918 },
4919 {"gui_athena",
4920#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4921 1
4922#else
4923 0
4924#endif
4925 },
4926 {"gui_gtk",
4927#ifdef FEAT_GUI_GTK
4928 1
4929#else
4930 0
4931#endif
4932 },
4933 {"gui_gtk2",
4934#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4935 1
4936#else
4937 0
4938#endif
4939 },
4940 {"gui_gtk3",
4941#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4942 1
4943#else
4944 0
4945#endif
4946 },
4947 {"gui_gnome",
4948#ifdef FEAT_GUI_GNOME
4949 1
4950#else
4951 0
4952#endif
4953 },
4954 {"gui_haiku",
4955#ifdef FEAT_GUI_HAIKU
4956 1
4957#else
4958 0
4959#endif
4960 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004961 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004962 {"gui_motif",
4963#ifdef FEAT_GUI_MOTIF
4964 1
4965#else
4966 0
4967#endif
4968 },
4969 {"gui_photon",
4970#ifdef FEAT_GUI_PHOTON
4971 1
4972#else
4973 0
4974#endif
4975 },
4976 {"gui_win32",
4977#ifdef FEAT_GUI_MSWIN
4978 1
4979#else
4980 0
4981#endif
4982 },
4983 {"iconv",
4984#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4985 1
4986#else
4987 0
4988#endif
4989 },
4990 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004991 {"ipv6",
4992#ifdef FEAT_IPV6
4993 1
4994#else
4995 0
4996#endif
4997 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004998 {"job",
4999#ifdef FEAT_JOB_CHANNEL
5000 1
5001#else
5002 0
5003#endif
5004 },
5005 {"jumplist",
5006#ifdef FEAT_JUMPLIST
5007 1
5008#else
5009 0
5010#endif
5011 },
5012 {"keymap",
5013#ifdef FEAT_KEYMAP
5014 1
5015#else
5016 0
5017#endif
5018 },
5019 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
5020 {"langmap",
5021#ifdef FEAT_LANGMAP
5022 1
5023#else
5024 0
5025#endif
5026 },
5027 {"libcall",
5028#ifdef FEAT_LIBCALL
5029 1
5030#else
5031 0
5032#endif
5033 },
5034 {"linebreak",
5035#ifdef FEAT_LINEBREAK
5036 1
5037#else
5038 0
5039#endif
5040 },
5041 {"lispindent",
5042#ifdef FEAT_LISP
5043 1
5044#else
5045 0
5046#endif
5047 },
5048 {"listcmds", 1},
5049 {"localmap", 1},
5050 {"lua",
5051#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
5052 1
5053#else
5054 0
5055#endif
5056 },
5057 {"menu",
5058#ifdef FEAT_MENU
5059 1
5060#else
5061 0
5062#endif
5063 },
5064 {"mksession",
5065#ifdef FEAT_SESSION
5066 1
5067#else
5068 0
5069#endif
5070 },
5071 {"modify_fname", 1},
5072 {"mouse", 1},
5073 {"mouseshape",
5074#ifdef FEAT_MOUSESHAPE
5075 1
5076#else
5077 0
5078#endif
5079 },
5080 {"mouse_dec",
5081#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
5082 1
5083#else
5084 0
5085#endif
5086 },
5087 {"mouse_gpm",
5088#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
5089 1
5090#else
5091 0
5092#endif
5093 },
5094 {"mouse_jsbterm",
5095#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
5096 1
5097#else
5098 0
5099#endif
5100 },
5101 {"mouse_netterm",
5102#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
5103 1
5104#else
5105 0
5106#endif
5107 },
5108 {"mouse_pterm",
5109#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
5110 1
5111#else
5112 0
5113#endif
5114 },
5115 {"mouse_sgr",
5116#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5117 1
5118#else
5119 0
5120#endif
5121 },
5122 {"mouse_sysmouse",
5123#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
5124 1
5125#else
5126 0
5127#endif
5128 },
5129 {"mouse_urxvt",
5130#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
5131 1
5132#else
5133 0
5134#endif
5135 },
5136 {"mouse_xterm",
5137#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5138 1
5139#else
5140 0
5141#endif
5142 },
5143 {"multi_byte", 1},
5144 {"multi_byte_ime",
5145#ifdef FEAT_MBYTE_IME
5146 1
5147#else
5148 0
5149#endif
5150 },
5151 {"multi_lang",
5152#ifdef FEAT_MULTI_LANG
5153 1
5154#else
5155 0
5156#endif
5157 },
5158 {"mzscheme",
5159#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
5160 1
5161#else
5162 0
5163#endif
5164 },
5165 {"num64", 1},
5166 {"ole",
5167#ifdef FEAT_OLE
5168 1
5169#else
5170 0
5171#endif
5172 },
5173 {"packages",
5174#ifdef FEAT_EVAL
5175 1
5176#else
5177 0
5178#endif
5179 },
5180 {"path_extra",
5181#ifdef FEAT_PATH_EXTRA
5182 1
5183#else
5184 0
5185#endif
5186 },
5187 {"perl",
5188#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
5189 1
5190#else
5191 0
5192#endif
5193 },
5194 {"persistent_undo",
5195#ifdef FEAT_PERSISTENT_UNDO
5196 1
5197#else
5198 0
5199#endif
5200 },
5201 {"python_compiled",
5202#if defined(FEAT_PYTHON)
5203 1
5204#else
5205 0
5206#endif
5207 },
5208 {"python_dynamic",
5209#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
5210 1
5211#else
5212 0
5213#endif
5214 },
5215 {"python",
5216#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
5217 1
5218#else
5219 0
5220#endif
5221 },
5222 {"pythonx",
5223#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
5224 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
5225 1
5226#else
5227 0
5228#endif
5229 },
5230 {"python3_compiled",
5231#if defined(FEAT_PYTHON3)
5232 1
5233#else
5234 0
5235#endif
5236 },
5237 {"python3_dynamic",
5238#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
5239 1
5240#else
5241 0
5242#endif
5243 },
5244 {"python3",
5245#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
5246 1
5247#else
5248 0
5249#endif
5250 },
5251 {"popupwin",
5252#ifdef FEAT_PROP_POPUP
5253 1
5254#else
5255 0
5256#endif
5257 },
5258 {"postscript",
5259#ifdef FEAT_POSTSCRIPT
5260 1
5261#else
5262 0
5263#endif
5264 },
5265 {"printer",
5266#ifdef FEAT_PRINTER
5267 1
5268#else
5269 0
5270#endif
5271 },
5272 {"profile",
5273#ifdef FEAT_PROFILE
5274 1
5275#else
5276 0
5277#endif
5278 },
5279 {"reltime",
5280#ifdef FEAT_RELTIME
5281 1
5282#else
5283 0
5284#endif
5285 },
5286 {"quickfix",
5287#ifdef FEAT_QUICKFIX
5288 1
5289#else
5290 0
5291#endif
5292 },
5293 {"rightleft",
5294#ifdef FEAT_RIGHTLEFT
5295 1
5296#else
5297 0
5298#endif
5299 },
5300 {"ruby",
5301#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5302 1
5303#else
5304 0
5305#endif
5306 },
5307 {"scrollbind", 1},
5308 {"showcmd",
5309#ifdef FEAT_CMDL_INFO
5310 1
5311#else
5312 0
5313#endif
5314 },
5315 {"cmdline_info",
5316#ifdef FEAT_CMDL_INFO
5317 1
5318#else
5319 0
5320#endif
5321 },
5322 {"signs",
5323#ifdef FEAT_SIGNS
5324 1
5325#else
5326 0
5327#endif
5328 },
5329 {"smartindent",
5330#ifdef FEAT_SMARTINDENT
5331 1
5332#else
5333 0
5334#endif
5335 },
5336 {"startuptime",
5337#ifdef STARTUPTIME
5338 1
5339#else
5340 0
5341#endif
5342 },
5343 {"statusline",
5344#ifdef FEAT_STL_OPT
5345 1
5346#else
5347 0
5348#endif
5349 },
5350 {"netbeans_intg",
5351#ifdef FEAT_NETBEANS_INTG
5352 1
5353#else
5354 0
5355#endif
5356 },
5357 {"sound",
5358#ifdef FEAT_SOUND
5359 1
5360#else
5361 0
5362#endif
5363 },
5364 {"spell",
5365#ifdef FEAT_SPELL
5366 1
5367#else
5368 0
5369#endif
5370 },
5371 {"syntax",
5372#ifdef FEAT_SYN_HL
5373 1
5374#else
5375 0
5376#endif
5377 },
5378 {"system",
5379#if defined(USE_SYSTEM) || !defined(UNIX)
5380 1
5381#else
5382 0
5383#endif
5384 },
5385 {"tag_binary",
5386#ifdef FEAT_TAG_BINS
5387 1
5388#else
5389 0
5390#endif
5391 },
5392 {"tcl",
5393#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5394 1
5395#else
5396 0
5397#endif
5398 },
5399 {"termguicolors",
5400#ifdef FEAT_TERMGUICOLORS
5401 1
5402#else
5403 0
5404#endif
5405 },
5406 {"terminal",
5407#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5408 1
5409#else
5410 0
5411#endif
5412 },
5413 {"terminfo",
5414#ifdef TERMINFO
5415 1
5416#else
5417 0
5418#endif
5419 },
5420 {"termresponse",
5421#ifdef FEAT_TERMRESPONSE
5422 1
5423#else
5424 0
5425#endif
5426 },
5427 {"textobjects",
5428#ifdef FEAT_TEXTOBJ
5429 1
5430#else
5431 0
5432#endif
5433 },
5434 {"textprop",
5435#ifdef FEAT_PROP_POPUP
5436 1
5437#else
5438 0
5439#endif
5440 },
5441 {"tgetent",
5442#ifdef HAVE_TGETENT
5443 1
5444#else
5445 0
5446#endif
5447 },
5448 {"timers",
5449#ifdef FEAT_TIMERS
5450 1
5451#else
5452 0
5453#endif
5454 },
5455 {"title",
5456#ifdef FEAT_TITLE
5457 1
5458#else
5459 0
5460#endif
5461 },
5462 {"toolbar",
5463#ifdef FEAT_TOOLBAR
5464 1
5465#else
5466 0
5467#endif
5468 },
5469 {"unnamedplus",
5470#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5471 1
5472#else
5473 0
5474#endif
5475 },
5476 {"user-commands", 1}, // was accidentally included in 5.4
5477 {"user_commands", 1},
5478 {"vartabs",
5479#ifdef FEAT_VARTABS
5480 1
5481#else
5482 0
5483#endif
5484 },
5485 {"vertsplit", 1},
5486 {"viminfo",
5487#ifdef FEAT_VIMINFO
5488 1
5489#else
5490 0
5491#endif
5492 },
5493 {"vimscript-1", 1},
5494 {"vimscript-2", 1},
5495 {"vimscript-3", 1},
5496 {"vimscript-4", 1},
5497 {"virtualedit", 1},
5498 {"visual", 1},
5499 {"visualextra", 1},
5500 {"vreplace", 1},
5501 {"vtp",
5502#ifdef FEAT_VTP
5503 1
5504#else
5505 0
5506#endif
5507 },
5508 {"wildignore",
5509#ifdef FEAT_WILDIGN
5510 1
5511#else
5512 0
5513#endif
5514 },
5515 {"wildmenu",
5516#ifdef FEAT_WILDMENU
5517 1
5518#else
5519 0
5520#endif
5521 },
5522 {"windows", 1},
5523 {"winaltkeys",
5524#ifdef FEAT_WAK
5525 1
5526#else
5527 0
5528#endif
5529 },
5530 {"writebackup",
5531#ifdef FEAT_WRITEBACKUP
5532 1
5533#else
5534 0
5535#endif
5536 },
5537 {"xim",
5538#ifdef FEAT_XIM
5539 1
5540#else
5541 0
5542#endif
5543 },
5544 {"xfontset",
5545#ifdef FEAT_XFONTSET
5546 1
5547#else
5548 0
5549#endif
5550 },
5551 {"xpm",
5552#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5553 1
5554#else
5555 0
5556#endif
5557 },
5558 {"xpm_w32", // for backward compatibility
5559#ifdef FEAT_XPM_W32
5560 1
5561#else
5562 0
5563#endif
5564 },
5565 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005566#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005567 1
5568#else
5569 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005570#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005571 },
5572 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005573#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005574 1
5575#else
5576 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005577#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005578 },
5579 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005580#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005581 1
5582#else
5583 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005584#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005585 },
5586 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005587#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005588 1
5589#else
5590 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005591#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005592 },
5593 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005594#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005595 1
5596#else
5597 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005598#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005599 },
5600 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005601 };
5602
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005603 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005604 for (i = 0; has_list[i].name != NULL; ++i)
5605 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005606 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005607 x = TRUE;
5608 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005609 break;
5610 }
5611
Bram Moolenaar79296512020-03-22 16:17:14 +01005612 // features also in has_list[] but sometimes enabled at runtime
5613 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005614 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005615 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005616 {
5617 // intentionally empty
5618 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005619#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005620 else if (STRICMP(name, "balloon_multiline") == 0)
5621 n = multiline_balloon_available();
5622#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005623#ifdef VIMDLL
5624 else if (STRICMP(name, "filterpipe") == 0)
5625 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005626#endif
5627#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5628 else if (STRICMP(name, "iconv") == 0)
5629 n = iconv_enabled(FALSE);
5630#endif
5631#ifdef DYNAMIC_LUA
5632 else if (STRICMP(name, "lua") == 0)
5633 n = lua_enabled(FALSE);
5634#endif
5635#ifdef DYNAMIC_MZSCHEME
5636 else if (STRICMP(name, "mzscheme") == 0)
5637 n = mzscheme_enabled(FALSE);
5638#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005639#ifdef DYNAMIC_PERL
5640 else if (STRICMP(name, "perl") == 0)
5641 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005642#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005643#ifdef DYNAMIC_PYTHON
5644 else if (STRICMP(name, "python") == 0)
5645 n = python_enabled(FALSE);
5646#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005647#ifdef DYNAMIC_PYTHON3
5648 else if (STRICMP(name, "python3") == 0)
5649 n = python3_enabled(FALSE);
5650#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005651#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5652 else if (STRICMP(name, "pythonx") == 0)
5653 {
5654# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5655 if (p_pyx == 0)
5656 n = python3_enabled(FALSE) || python_enabled(FALSE);
5657 else if (p_pyx == 3)
5658 n = python3_enabled(FALSE);
5659 else if (p_pyx == 2)
5660 n = python_enabled(FALSE);
5661# elif defined(DYNAMIC_PYTHON)
5662 n = python_enabled(FALSE);
5663# elif defined(DYNAMIC_PYTHON3)
5664 n = python3_enabled(FALSE);
5665# endif
5666 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005667#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005668#ifdef DYNAMIC_RUBY
5669 else if (STRICMP(name, "ruby") == 0)
5670 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005671#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005672#ifdef DYNAMIC_TCL
5673 else if (STRICMP(name, "tcl") == 0)
5674 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005675#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005676#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005677 else if (STRICMP(name, "terminal") == 0)
5678 n = terminal_enabled();
5679#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005680 }
5681
Bram Moolenaar79296512020-03-22 16:17:14 +01005682 // features not in has_list[]
5683 if (x == FALSE)
5684 {
5685 if (STRNICMP(name, "patch", 5) == 0)
5686 {
5687 x = TRUE;
5688 if (name[5] == '-'
5689 && STRLEN(name) >= 11
5690 && vim_isdigit(name[6])
5691 && vim_isdigit(name[8])
5692 && vim_isdigit(name[10]))
5693 {
5694 int major = atoi((char *)name + 6);
5695 int minor = atoi((char *)name + 8);
5696
5697 // Expect "patch-9.9.01234".
5698 n = (major < VIM_VERSION_MAJOR
5699 || (major == VIM_VERSION_MAJOR
5700 && (minor < VIM_VERSION_MINOR
5701 || (minor == VIM_VERSION_MINOR
5702 && has_patch(atoi((char *)name + 10))))));
5703 }
5704 else
5705 n = has_patch(atoi((char *)name + 5));
5706 }
5707 else if (STRICMP(name, "vim_starting") == 0)
5708 {
5709 x = TRUE;
5710 n = (starting != 0);
5711 }
5712 else if (STRICMP(name, "ttyin") == 0)
5713 {
5714 x = TRUE;
5715 n = mch_input_isatty();
5716 }
5717 else if (STRICMP(name, "ttyout") == 0)
5718 {
5719 x = TRUE;
5720 n = stdout_isatty;
5721 }
5722 else if (STRICMP(name, "multi_byte_encoding") == 0)
5723 {
5724 x = TRUE;
5725 n = has_mbyte;
5726 }
5727 else if (STRICMP(name, "gui_running") == 0)
5728 {
5729 x = TRUE;
5730#ifdef FEAT_GUI
5731 n = (gui.in_use || gui.starting);
5732#endif
5733 }
5734 else if (STRICMP(name, "browse") == 0)
5735 {
5736 x = TRUE;
5737#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5738 n = gui.in_use; // gui_mch_browse() works when GUI is running
5739#endif
5740 }
5741 else if (STRICMP(name, "syntax_items") == 0)
5742 {
5743 x = TRUE;
5744#ifdef FEAT_SYN_HL
5745 n = syntax_present(curwin);
5746#endif
5747 }
5748 else if (STRICMP(name, "vcon") == 0)
5749 {
5750 x = TRUE;
5751#ifdef FEAT_VTP
5752 n = is_term_win32() && has_vtp_working();
5753#endif
5754 }
5755 else if (STRICMP(name, "netbeans_enabled") == 0)
5756 {
5757 x = TRUE;
5758#ifdef FEAT_NETBEANS_INTG
5759 n = netbeans_active();
5760#endif
5761 }
5762 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5763 {
5764 x = TRUE;
5765#ifdef FEAT_MOUSE_GPM
5766 n = gpm_enabled();
5767#endif
5768 }
5769 else if (STRICMP(name, "conpty") == 0)
5770 {
5771 x = TRUE;
5772#if defined(FEAT_TERMINAL) && defined(MSWIN)
5773 n = use_conpty();
5774#endif
5775 }
5776 else if (STRICMP(name, "clipboard_working") == 0)
5777 {
5778 x = TRUE;
5779#ifdef FEAT_CLIPBOARD
5780 n = clip_star.available;
5781#endif
5782 }
5783 }
5784
Bram Moolenaar04637e22020-09-05 18:45:29 +02005785 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005786 // return whether feature could ever be enabled
5787 rettv->vval.v_number = x;
5788 else
5789 // return whether feature is enabled
5790 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005791}
5792
5793/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005794 * Return TRUE if "feature" can change later.
5795 * Also when checking for the feature has side effects, such as loading a DLL.
5796 */
5797 int
5798dynamic_feature(char_u *feature)
5799{
5800 return (feature == NULL
5801#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5802 || STRICMP(feature, "balloon_multiline") == 0
5803#endif
5804#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5805 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5806#endif
5807#ifdef VIMDLL
5808 || STRICMP(feature, "filterpipe") == 0
5809#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005810#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005811 // this can only change on Unix where the ":gui" command could be
5812 // used.
5813 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5814#endif
5815#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5816 || STRICMP(feature, "iconv") == 0
5817#endif
5818#ifdef DYNAMIC_LUA
5819 || STRICMP(feature, "lua") == 0
5820#endif
5821#ifdef FEAT_MOUSE_GPM
5822 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5823#endif
5824#ifdef DYNAMIC_MZSCHEME
5825 || STRICMP(feature, "mzscheme") == 0
5826#endif
5827#ifdef FEAT_NETBEANS_INTG
5828 || STRICMP(feature, "netbeans_enabled") == 0
5829#endif
5830#ifdef DYNAMIC_PERL
5831 || STRICMP(feature, "perl") == 0
5832#endif
5833#ifdef DYNAMIC_PYTHON
5834 || STRICMP(feature, "python") == 0
5835#endif
5836#ifdef DYNAMIC_PYTHON3
5837 || STRICMP(feature, "python3") == 0
5838#endif
5839#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5840 || STRICMP(feature, "pythonx") == 0
5841#endif
5842#ifdef DYNAMIC_RUBY
5843 || STRICMP(feature, "ruby") == 0
5844#endif
5845#ifdef FEAT_SYN_HL
5846 || STRICMP(feature, "syntax_items") == 0
5847#endif
5848#ifdef DYNAMIC_TCL
5849 || STRICMP(feature, "tcl") == 0
5850#endif
5851 // once "starting" is zero it will stay that way
5852 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5853 || STRICMP(feature, "multi_byte_encoding") == 0
5854#if defined(FEAT_TERMINAL) && defined(MSWIN)
5855 || STRICMP(feature, "conpty") == 0
5856#endif
5857 );
5858}
5859
5860/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005861 * "haslocaldir()" function
5862 */
5863 static void
5864f_haslocaldir(typval_T *argvars, typval_T *rettv)
5865{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005866 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005867 win_T *wp = NULL;
5868
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005869 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5870
5871 // Check for window-local and tab-local directories
5872 if (wp != NULL && wp->w_localdir != NULL)
5873 rettv->vval.v_number = 1;
5874 else if (tp != NULL && tp->tp_localdir != NULL)
5875 rettv->vval.v_number = 2;
5876 else
5877 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005878}
5879
5880/*
5881 * "hasmapto()" function
5882 */
5883 static void
5884f_hasmapto(typval_T *argvars, typval_T *rettv)
5885{
5886 char_u *name;
5887 char_u *mode;
5888 char_u buf[NUMBUFLEN];
5889 int abbr = FALSE;
5890
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005891 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005892 if (argvars[1].v_type == VAR_UNKNOWN)
5893 mode = (char_u *)"nvo";
5894 else
5895 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005896 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005897 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005898 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005899 }
5900
5901 if (map_to_exists(name, mode, abbr))
5902 rettv->vval.v_number = TRUE;
5903 else
5904 rettv->vval.v_number = FALSE;
5905}
5906
5907/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005908 * "highlightID(name)" function
5909 */
5910 static void
5911f_hlID(typval_T *argvars, typval_T *rettv)
5912{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005913 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005914}
5915
5916/*
5917 * "highlight_exists()" function
5918 */
5919 static void
5920f_hlexists(typval_T *argvars, typval_T *rettv)
5921{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005922 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005923}
5924
5925/*
5926 * "hostname()" function
5927 */
5928 static void
5929f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5930{
5931 char_u hostname[256];
5932
5933 mch_get_host_name(hostname, 256);
5934 rettv->v_type = VAR_STRING;
5935 rettv->vval.v_string = vim_strsave(hostname);
5936}
5937
5938/*
5939 * iconv() function
5940 */
5941 static void
5942f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5943{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005944 char_u buf1[NUMBUFLEN];
5945 char_u buf2[NUMBUFLEN];
5946 char_u *from, *to, *str;
5947 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005948
5949 rettv->v_type = VAR_STRING;
5950 rettv->vval.v_string = NULL;
5951
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005952 str = tv_get_string(&argvars[0]);
5953 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5954 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005955 vimconv.vc_type = CONV_NONE;
5956 convert_setup(&vimconv, from, to);
5957
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005958 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005959 if (vimconv.vc_type == CONV_NONE)
5960 rettv->vval.v_string = vim_strsave(str);
5961 else
5962 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5963
5964 convert_setup(&vimconv, NULL, NULL);
5965 vim_free(from);
5966 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005967}
5968
5969/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005970 * "index()" function
5971 */
5972 static void
5973f_index(typval_T *argvars, typval_T *rettv)
5974{
5975 list_T *l;
5976 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005977 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005978 long idx = 0;
5979 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005980 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005981
5982 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005983 if (argvars[0].v_type == VAR_BLOB)
5984 {
5985 typval_T tv;
5986 int start = 0;
5987
5988 if (argvars[2].v_type != VAR_UNKNOWN)
5989 {
5990 start = tv_get_number_chk(&argvars[2], &error);
5991 if (error)
5992 return;
5993 }
5994 b = argvars[0].vval.v_blob;
5995 if (b == NULL)
5996 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005997 if (start < 0)
5998 {
5999 start = blob_len(b) + start;
6000 if (start < 0)
6001 start = 0;
6002 }
6003
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006004 for (idx = start; idx < blob_len(b); ++idx)
6005 {
6006 tv.v_type = VAR_NUMBER;
6007 tv.vval.v_number = blob_get(b, idx);
6008 if (tv_equal(&tv, &argvars[1], ic, FALSE))
6009 {
6010 rettv->vval.v_number = idx;
6011 return;
6012 }
6013 }
6014 return;
6015 }
6016 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006017 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01006018 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006019 return;
6020 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006021
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006022 l = argvars[0].vval.v_list;
6023 if (l != NULL)
6024 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006025 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006026 item = l->lv_first;
6027 if (argvars[2].v_type != VAR_UNKNOWN)
6028 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006029 // Start at specified item. Use the cached index that list_find()
6030 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006031 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006032 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006033 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02006034 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006035 if (error)
6036 item = NULL;
6037 }
6038
6039 for ( ; item != NULL; item = item->li_next, ++idx)
6040 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6041 {
6042 rettv->vval.v_number = idx;
6043 break;
6044 }
6045 }
6046}
6047
6048static int inputsecret_flag = 0;
6049
6050/*
6051 * "input()" function
6052 * Also handles inputsecret() when inputsecret is set.
6053 */
6054 static void
6055f_input(typval_T *argvars, typval_T *rettv)
6056{
6057 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6058}
6059
6060/*
6061 * "inputdialog()" function
6062 */
6063 static void
6064f_inputdialog(typval_T *argvars, typval_T *rettv)
6065{
6066#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006067 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006068 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6069 {
6070 char_u *message;
6071 char_u buf[NUMBUFLEN];
6072 char_u *defstr = (char_u *)"";
6073
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006074 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006075 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006076 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006077 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6078 else
6079 IObuff[0] = NUL;
6080 if (message != NULL && defstr != NULL
6081 && do_dialog(VIM_QUESTION, NULL, message,
6082 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6083 rettv->vval.v_string = vim_strsave(IObuff);
6084 else
6085 {
6086 if (message != NULL && defstr != NULL
6087 && argvars[1].v_type != VAR_UNKNOWN
6088 && argvars[2].v_type != VAR_UNKNOWN)
6089 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006090 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006091 else
6092 rettv->vval.v_string = NULL;
6093 }
6094 rettv->v_type = VAR_STRING;
6095 }
6096 else
6097#endif
6098 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6099}
6100
6101/*
6102 * "inputlist()" function
6103 */
6104 static void
6105f_inputlist(typval_T *argvars, typval_T *rettv)
6106{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006107 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006108 listitem_T *li;
6109 int selected;
6110 int mouse_used;
6111
6112#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006113 // While starting up, there is no place to enter text. When running tests
6114 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006115 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006116 return;
6117#endif
6118 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6119 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006120 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006121 return;
6122 }
6123
6124 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006125 msg_row = Rows - 1; // for when 'cmdheight' > 1
6126 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006127 msg_scroll = TRUE;
6128 msg_clr_eos();
6129
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006130 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006131 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006132 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006133 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006134 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006135 msg_putchar('\n');
6136 }
6137
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006138 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006139 selected = prompt_for_number(&mouse_used);
6140 if (mouse_used)
6141 selected -= lines_left;
6142
6143 rettv->vval.v_number = selected;
6144}
6145
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006146static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6147
6148/*
6149 * "inputrestore()" function
6150 */
6151 static void
6152f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6153{
6154 if (ga_userinput.ga_len > 0)
6155 {
6156 --ga_userinput.ga_len;
6157 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6158 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006159 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006160 }
6161 else if (p_verbose > 1)
6162 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006163 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006164 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006165 }
6166}
6167
6168/*
6169 * "inputsave()" function
6170 */
6171 static void
6172f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6173{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006174 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006175 if (ga_grow(&ga_userinput, 1) == OK)
6176 {
6177 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6178 + ga_userinput.ga_len);
6179 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006180 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006181 }
6182 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006183 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006184}
6185
6186/*
6187 * "inputsecret()" function
6188 */
6189 static void
6190f_inputsecret(typval_T *argvars, typval_T *rettv)
6191{
6192 ++cmdline_star;
6193 ++inputsecret_flag;
6194 f_input(argvars, rettv);
6195 --cmdline_star;
6196 --inputsecret_flag;
6197}
6198
6199/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01006200 * "interrupt()" function
6201 */
6202 static void
6203f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6204{
6205 got_int = TRUE;
6206}
6207
6208/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006209 * "invert(expr)" function
6210 */
6211 static void
6212f_invert(typval_T *argvars, typval_T *rettv)
6213{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006214 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006215}
6216
6217/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006218 * "islocked()" function
6219 */
6220 static void
6221f_islocked(typval_T *argvars, typval_T *rettv)
6222{
6223 lval_T lv;
6224 char_u *end;
6225 dictitem_T *di;
6226
6227 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006228 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006229 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006230 if (end != NULL && lv.ll_name != NULL)
6231 {
6232 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02006233 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006234 else
6235 {
6236 if (lv.ll_tv == NULL)
6237 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006238 di = find_var(lv.ll_name, NULL, TRUE);
6239 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006240 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006241 // Consider a variable locked when:
6242 // 1. the variable itself is locked
6243 // 2. the value of the variable is locked.
6244 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01006245 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6246 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247 }
6248 }
6249 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006250 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006251 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006252 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006253 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006254 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006255 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6256 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006257 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006258 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6259 }
6260 }
6261
6262 clear_lval(&lv);
6263}
6264
6265#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6266/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006267 * "isinf()" function
6268 */
6269 static void
6270f_isinf(typval_T *argvars, typval_T *rettv)
6271{
6272 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6273 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6274}
6275
6276/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006277 * "isnan()" function
6278 */
6279 static void
6280f_isnan(typval_T *argvars, typval_T *rettv)
6281{
6282 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6283 && isnan(argvars[0].vval.v_float);
6284}
6285#endif
6286
6287/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006288 * "last_buffer_nr()" function.
6289 */
6290 static void
6291f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6292{
6293 int n = 0;
6294 buf_T *buf;
6295
Bram Moolenaar29323592016-07-24 22:04:11 +02006296 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006297 if (n < buf->b_fnum)
6298 n = buf->b_fnum;
6299
6300 rettv->vval.v_number = n;
6301}
6302
6303/*
6304 * "len()" function
6305 */
6306 static void
6307f_len(typval_T *argvars, typval_T *rettv)
6308{
6309 switch (argvars[0].v_type)
6310 {
6311 case VAR_STRING:
6312 case VAR_NUMBER:
6313 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006314 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006315 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006316 case VAR_BLOB:
6317 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6318 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006319 case VAR_LIST:
6320 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6321 break;
6322 case VAR_DICT:
6323 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6324 break;
6325 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006326 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006327 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006328 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006329 case VAR_SPECIAL:
6330 case VAR_FLOAT:
6331 case VAR_FUNC:
6332 case VAR_PARTIAL:
6333 case VAR_JOB:
6334 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006335 case VAR_INSTR:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006336 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006337 break;
6338 }
6339}
6340
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006341 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006342libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006343{
6344#ifdef FEAT_LIBCALL
6345 char_u *string_in;
6346 char_u **string_result;
6347 int nr_result;
6348#endif
6349
6350 rettv->v_type = type;
6351 if (type != VAR_NUMBER)
6352 rettv->vval.v_string = NULL;
6353
6354 if (check_restricted() || check_secure())
6355 return;
6356
6357#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006358 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006359 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6360 {
6361 string_in = NULL;
6362 if (argvars[2].v_type == VAR_STRING)
6363 string_in = argvars[2].vval.v_string;
6364 if (type == VAR_NUMBER)
6365 string_result = NULL;
6366 else
6367 string_result = &rettv->vval.v_string;
6368 if (mch_libcall(argvars[0].vval.v_string,
6369 argvars[1].vval.v_string,
6370 string_in,
6371 argvars[2].vval.v_number,
6372 string_result,
6373 &nr_result) == OK
6374 && type == VAR_NUMBER)
6375 rettv->vval.v_number = nr_result;
6376 }
6377#endif
6378}
6379
6380/*
6381 * "libcall()" function
6382 */
6383 static void
6384f_libcall(typval_T *argvars, typval_T *rettv)
6385{
6386 libcall_common(argvars, rettv, VAR_STRING);
6387}
6388
6389/*
6390 * "libcallnr()" function
6391 */
6392 static void
6393f_libcallnr(typval_T *argvars, typval_T *rettv)
6394{
6395 libcall_common(argvars, rettv, VAR_NUMBER);
6396}
6397
6398/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006399 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006400 */
6401 static void
6402f_line(typval_T *argvars, typval_T *rettv)
6403{
6404 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006405 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006406 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006407 int id;
6408 tabpage_T *tp;
6409 win_T *wp;
6410 win_T *save_curwin;
6411 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006412
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006413 if (argvars[1].v_type != VAR_UNKNOWN)
6414 {
6415 // use window specified in the second argument
6416 id = (int)tv_get_number(&argvars[1]);
6417 wp = win_id2wp_tp(id, &tp);
6418 if (wp != NULL && tp != NULL)
6419 {
6420 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6421 == OK)
6422 {
6423 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006424 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006425 }
6426 restore_win_noblock(save_curwin, save_curtab, TRUE);
6427 }
6428 }
6429 else
6430 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006431 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006432
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006433 if (fp != NULL)
6434 lnum = fp->lnum;
6435 rettv->vval.v_number = lnum;
6436}
6437
6438/*
6439 * "line2byte(lnum)" function
6440 */
6441 static void
6442f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6443{
6444#ifndef FEAT_BYTEOFF
6445 rettv->vval.v_number = -1;
6446#else
6447 linenr_T lnum;
6448
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006449 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006450 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6451 rettv->vval.v_number = -1;
6452 else
6453 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6454 if (rettv->vval.v_number >= 0)
6455 ++rettv->vval.v_number;
6456#endif
6457}
6458
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006459#ifdef FEAT_FLOAT
6460/*
6461 * "log()" function
6462 */
6463 static void
6464f_log(typval_T *argvars, typval_T *rettv)
6465{
6466 float_T f = 0.0;
6467
6468 rettv->v_type = VAR_FLOAT;
6469 if (get_float_arg(argvars, &f) == OK)
6470 rettv->vval.v_float = log(f);
6471 else
6472 rettv->vval.v_float = 0.0;
6473}
6474
6475/*
6476 * "log10()" function
6477 */
6478 static void
6479f_log10(typval_T *argvars, typval_T *rettv)
6480{
6481 float_T f = 0.0;
6482
6483 rettv->v_type = VAR_FLOAT;
6484 if (get_float_arg(argvars, &f) == OK)
6485 rettv->vval.v_float = log10(f);
6486 else
6487 rettv->vval.v_float = 0.0;
6488}
6489#endif
6490
6491#ifdef FEAT_LUA
6492/*
6493 * "luaeval()" function
6494 */
6495 static void
6496f_luaeval(typval_T *argvars, typval_T *rettv)
6497{
6498 char_u *str;
6499 char_u buf[NUMBUFLEN];
6500
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006501 if (check_restricted() || check_secure())
6502 return;
6503
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006504 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006505 do_luaeval(str, argvars + 1, rettv);
6506}
6507#endif
6508
6509/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006510 * "maparg()" function
6511 */
6512 static void
6513f_maparg(typval_T *argvars, typval_T *rettv)
6514{
6515 get_maparg(argvars, rettv, TRUE);
6516}
6517
6518/*
6519 * "mapcheck()" function
6520 */
6521 static void
6522f_mapcheck(typval_T *argvars, typval_T *rettv)
6523{
6524 get_maparg(argvars, rettv, FALSE);
6525}
6526
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006527typedef enum
6528{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006529 MATCH_END, // matchend()
6530 MATCH_MATCH, // match()
6531 MATCH_STR, // matchstr()
6532 MATCH_LIST, // matchlist()
6533 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006534} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006535
6536 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006537find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006538{
6539 char_u *str = NULL;
6540 long len = 0;
6541 char_u *expr = NULL;
6542 char_u *pat;
6543 regmatch_T regmatch;
6544 char_u patbuf[NUMBUFLEN];
6545 char_u strbuf[NUMBUFLEN];
6546 char_u *save_cpo;
6547 long start = 0;
6548 long nth = 1;
6549 colnr_T startcol = 0;
6550 int match = 0;
6551 list_T *l = NULL;
6552 listitem_T *li = NULL;
6553 long idx = 0;
6554 char_u *tofree = NULL;
6555
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006556 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006557 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006558 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006559
6560 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006561 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006562 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006563 // type MATCH_LIST: return empty list when there are no matches.
6564 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006565 if (rettv_list_alloc(rettv) == FAIL)
6566 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006567 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006568 && (list_append_string(rettv->vval.v_list,
6569 (char_u *)"", 0) == FAIL
6570 || list_append_number(rettv->vval.v_list,
6571 (varnumber_T)-1) == FAIL
6572 || list_append_number(rettv->vval.v_list,
6573 (varnumber_T)-1) == FAIL
6574 || list_append_number(rettv->vval.v_list,
6575 (varnumber_T)-1) == FAIL))
6576 {
6577 list_free(rettv->vval.v_list);
6578 rettv->vval.v_list = NULL;
6579 goto theend;
6580 }
6581 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006582 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006583 {
6584 rettv->v_type = VAR_STRING;
6585 rettv->vval.v_string = NULL;
6586 }
6587
6588 if (argvars[0].v_type == VAR_LIST)
6589 {
6590 if ((l = argvars[0].vval.v_list) == NULL)
6591 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006592 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006593 li = l->lv_first;
6594 }
6595 else
6596 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006597 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006598 len = (long)STRLEN(str);
6599 }
6600
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006601 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006602 if (pat == NULL)
6603 goto theend;
6604
6605 if (argvars[2].v_type != VAR_UNKNOWN)
6606 {
6607 int error = FALSE;
6608
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006609 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006610 if (error)
6611 goto theend;
6612 if (l != NULL)
6613 {
6614 li = list_find(l, start);
6615 if (li == NULL)
6616 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006617 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006618 }
6619 else
6620 {
6621 if (start < 0)
6622 start = 0;
6623 if (start > len)
6624 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006625 // When "count" argument is there ignore matches before "start",
6626 // otherwise skip part of the string. Differs when pattern is "^"
6627 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006628 if (argvars[3].v_type != VAR_UNKNOWN)
6629 startcol = start;
6630 else
6631 {
6632 str += start;
6633 len -= start;
6634 }
6635 }
6636
6637 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006638 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006639 if (error)
6640 goto theend;
6641 }
6642
6643 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6644 if (regmatch.regprog != NULL)
6645 {
6646 regmatch.rm_ic = p_ic;
6647
6648 for (;;)
6649 {
6650 if (l != NULL)
6651 {
6652 if (li == NULL)
6653 {
6654 match = FALSE;
6655 break;
6656 }
6657 vim_free(tofree);
6658 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6659 if (str == NULL)
6660 break;
6661 }
6662
6663 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6664
6665 if (match && --nth <= 0)
6666 break;
6667 if (l == NULL && !match)
6668 break;
6669
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006670 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006671 if (l != NULL)
6672 {
6673 li = li->li_next;
6674 ++idx;
6675 }
6676 else
6677 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006678 startcol = (colnr_T)(regmatch.startp[0]
6679 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006680 if (startcol > (colnr_T)len
6681 || str + startcol <= regmatch.startp[0])
6682 {
6683 match = FALSE;
6684 break;
6685 }
6686 }
6687 }
6688
6689 if (match)
6690 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006691 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006692 {
6693 listitem_T *li1 = rettv->vval.v_list->lv_first;
6694 listitem_T *li2 = li1->li_next;
6695 listitem_T *li3 = li2->li_next;
6696 listitem_T *li4 = li3->li_next;
6697
6698 vim_free(li1->li_tv.vval.v_string);
6699 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006700 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006701 li3->li_tv.vval.v_number =
6702 (varnumber_T)(regmatch.startp[0] - expr);
6703 li4->li_tv.vval.v_number =
6704 (varnumber_T)(regmatch.endp[0] - expr);
6705 if (l != NULL)
6706 li2->li_tv.vval.v_number = (varnumber_T)idx;
6707 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006708 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006709 {
6710 int i;
6711
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006712 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006713 for (i = 0; i < NSUBEXP; ++i)
6714 {
6715 if (regmatch.endp[i] == NULL)
6716 {
6717 if (list_append_string(rettv->vval.v_list,
6718 (char_u *)"", 0) == FAIL)
6719 break;
6720 }
6721 else if (list_append_string(rettv->vval.v_list,
6722 regmatch.startp[i],
6723 (int)(regmatch.endp[i] - regmatch.startp[i]))
6724 == FAIL)
6725 break;
6726 }
6727 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006728 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006729 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006730 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006731 if (l != NULL)
6732 copy_tv(&li->li_tv, rettv);
6733 else
6734 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006735 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006736 }
6737 else if (l != NULL)
6738 rettv->vval.v_number = idx;
6739 else
6740 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006741 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006742 rettv->vval.v_number =
6743 (varnumber_T)(regmatch.startp[0] - str);
6744 else
6745 rettv->vval.v_number =
6746 (varnumber_T)(regmatch.endp[0] - str);
6747 rettv->vval.v_number += (varnumber_T)(str - expr);
6748 }
6749 }
6750 vim_regfree(regmatch.regprog);
6751 }
6752
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006753theend:
6754 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006755 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006756 listitem_remove(rettv->vval.v_list,
6757 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006758 vim_free(tofree);
6759 p_cpo = save_cpo;
6760}
6761
6762/*
6763 * "match()" function
6764 */
6765 static void
6766f_match(typval_T *argvars, typval_T *rettv)
6767{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006768 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006769}
6770
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006771/*
6772 * "matchend()" function
6773 */
6774 static void
6775f_matchend(typval_T *argvars, typval_T *rettv)
6776{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006777 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006778}
6779
6780/*
6781 * "matchlist()" function
6782 */
6783 static void
6784f_matchlist(typval_T *argvars, typval_T *rettv)
6785{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006786 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006787}
6788
6789/*
6790 * "matchstr()" function
6791 */
6792 static void
6793f_matchstr(typval_T *argvars, typval_T *rettv)
6794{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006795 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006796}
6797
6798/*
6799 * "matchstrpos()" function
6800 */
6801 static void
6802f_matchstrpos(typval_T *argvars, typval_T *rettv)
6803{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006804 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006805}
6806
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006807 static void
6808max_min(typval_T *argvars, typval_T *rettv, int domax)
6809{
6810 varnumber_T n = 0;
6811 varnumber_T i;
6812 int error = FALSE;
6813
6814 if (argvars[0].v_type == VAR_LIST)
6815 {
6816 list_T *l;
6817 listitem_T *li;
6818
6819 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006820 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006821 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006822 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006823 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006824 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6825 n = l->lv_u.nonmat.lv_start;
6826 else
6827 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6828 * l->lv_u.nonmat.lv_stride;
6829 }
6830 else
6831 {
6832 li = l->lv_first;
6833 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006834 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006835 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006836 if (error)
6837 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006838 for (;;)
6839 {
6840 li = li->li_next;
6841 if (li == NULL)
6842 break;
6843 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006844 if (error)
6845 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006846 if (domax ? i > n : i < n)
6847 n = i;
6848 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006849 }
6850 }
6851 }
6852 }
6853 else if (argvars[0].v_type == VAR_DICT)
6854 {
6855 dict_T *d;
6856 int first = TRUE;
6857 hashitem_T *hi;
6858 int todo;
6859
6860 d = argvars[0].vval.v_dict;
6861 if (d != NULL)
6862 {
6863 todo = (int)d->dv_hashtab.ht_used;
6864 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6865 {
6866 if (!HASHITEM_EMPTY(hi))
6867 {
6868 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006869 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006870 if (error)
6871 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006872 if (first)
6873 {
6874 n = i;
6875 first = FALSE;
6876 }
6877 else if (domax ? i > n : i < n)
6878 n = i;
6879 }
6880 }
6881 }
6882 }
6883 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006884 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006885
6886 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006887}
6888
6889/*
6890 * "max()" function
6891 */
6892 static void
6893f_max(typval_T *argvars, typval_T *rettv)
6894{
6895 max_min(argvars, rettv, TRUE);
6896}
6897
6898/*
6899 * "min()" function
6900 */
6901 static void
6902f_min(typval_T *argvars, typval_T *rettv)
6903{
6904 max_min(argvars, rettv, FALSE);
6905}
6906
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006907#if defined(FEAT_MZSCHEME) || defined(PROTO)
6908/*
6909 * "mzeval()" function
6910 */
6911 static void
6912f_mzeval(typval_T *argvars, typval_T *rettv)
6913{
6914 char_u *str;
6915 char_u buf[NUMBUFLEN];
6916
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006917 if (check_restricted() || check_secure())
6918 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006919 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006920 do_mzeval(str, rettv);
6921}
6922
6923 void
6924mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6925{
6926 typval_T argvars[3];
6927
6928 argvars[0].v_type = VAR_STRING;
6929 argvars[0].vval.v_string = name;
6930 copy_tv(args, &argvars[1]);
6931 argvars[2].v_type = VAR_UNKNOWN;
6932 f_call(argvars, rettv);
6933 clear_tv(&argvars[1]);
6934}
6935#endif
6936
6937/*
6938 * "nextnonblank()" function
6939 */
6940 static void
6941f_nextnonblank(typval_T *argvars, typval_T *rettv)
6942{
6943 linenr_T lnum;
6944
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006945 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006946 {
6947 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6948 {
6949 lnum = 0;
6950 break;
6951 }
6952 if (*skipwhite(ml_get(lnum)) != NUL)
6953 break;
6954 }
6955 rettv->vval.v_number = lnum;
6956}
6957
6958/*
6959 * "nr2char()" function
6960 */
6961 static void
6962f_nr2char(typval_T *argvars, typval_T *rettv)
6963{
6964 char_u buf[NUMBUFLEN];
6965
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006966 if (has_mbyte)
6967 {
6968 int utf8 = 0;
6969
6970 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006971 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006972 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006973 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006974 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006975 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006976 }
6977 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006978 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006979 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006980 buf[1] = NUL;
6981 }
6982 rettv->v_type = VAR_STRING;
6983 rettv->vval.v_string = vim_strsave(buf);
6984}
6985
6986/*
6987 * "or(expr, expr)" function
6988 */
6989 static void
6990f_or(typval_T *argvars, typval_T *rettv)
6991{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006992 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6993 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006994}
6995
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006996#ifdef FEAT_PERL
6997/*
6998 * "perleval()" function
6999 */
7000 static void
7001f_perleval(typval_T *argvars, typval_T *rettv)
7002{
7003 char_u *str;
7004 char_u buf[NUMBUFLEN];
7005
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007006 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007007 do_perleval(str, rettv);
7008}
7009#endif
7010
7011#ifdef FEAT_FLOAT
7012/*
7013 * "pow()" function
7014 */
7015 static void
7016f_pow(typval_T *argvars, typval_T *rettv)
7017{
7018 float_T fx = 0.0, fy = 0.0;
7019
7020 rettv->v_type = VAR_FLOAT;
7021 if (get_float_arg(argvars, &fx) == OK
7022 && get_float_arg(&argvars[1], &fy) == OK)
7023 rettv->vval.v_float = pow(fx, fy);
7024 else
7025 rettv->vval.v_float = 0.0;
7026}
7027#endif
7028
7029/*
7030 * "prevnonblank()" function
7031 */
7032 static void
7033f_prevnonblank(typval_T *argvars, typval_T *rettv)
7034{
7035 linenr_T lnum;
7036
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007037 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007038 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7039 lnum = 0;
7040 else
7041 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
7042 --lnum;
7043 rettv->vval.v_number = lnum;
7044}
7045
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007046// This dummy va_list is here because:
7047// - passing a NULL pointer doesn't work when va_list isn't a pointer
7048// - locally in the function results in a "used before set" warning
7049// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007050static va_list ap;
7051
7052/*
7053 * "printf()" function
7054 */
7055 static void
7056f_printf(typval_T *argvars, typval_T *rettv)
7057{
7058 char_u buf[NUMBUFLEN];
7059 int len;
7060 char_u *s;
7061 int saved_did_emsg = did_emsg;
7062 char *fmt;
7063
7064 rettv->v_type = VAR_STRING;
7065 rettv->vval.v_string = NULL;
7066
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007067 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007068 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007069 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007070 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007071 if (!did_emsg)
7072 {
7073 s = alloc(len + 1);
7074 if (s != NULL)
7075 {
7076 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007077 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
7078 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007079 }
7080 }
7081 did_emsg |= saved_did_emsg;
7082}
7083
7084/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007085 * "pum_getpos()" function
7086 */
7087 static void
7088f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7089{
7090 if (rettv_dict_alloc(rettv) != OK)
7091 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007092 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007093}
7094
7095/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007096 * "pumvisible()" function
7097 */
7098 static void
7099f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7100{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007101 if (pum_visible())
7102 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007103}
7104
7105#ifdef FEAT_PYTHON3
7106/*
7107 * "py3eval()" function
7108 */
7109 static void
7110f_py3eval(typval_T *argvars, typval_T *rettv)
7111{
7112 char_u *str;
7113 char_u buf[NUMBUFLEN];
7114
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007115 if (check_restricted() || check_secure())
7116 return;
7117
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007118 if (p_pyx == 0)
7119 p_pyx = 3;
7120
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007121 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007122 do_py3eval(str, rettv);
7123}
7124#endif
7125
7126#ifdef FEAT_PYTHON
7127/*
7128 * "pyeval()" function
7129 */
7130 static void
7131f_pyeval(typval_T *argvars, typval_T *rettv)
7132{
7133 char_u *str;
7134 char_u buf[NUMBUFLEN];
7135
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007136 if (check_restricted() || check_secure())
7137 return;
7138
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007139 if (p_pyx == 0)
7140 p_pyx = 2;
7141
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007142 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007143 do_pyeval(str, rettv);
7144}
7145#endif
7146
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007147#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
7148/*
7149 * "pyxeval()" function
7150 */
7151 static void
7152f_pyxeval(typval_T *argvars, typval_T *rettv)
7153{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007154 if (check_restricted() || check_secure())
7155 return;
7156
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007157# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
7158 init_pyxversion();
7159 if (p_pyx == 2)
7160 f_pyeval(argvars, rettv);
7161 else
7162 f_py3eval(argvars, rettv);
7163# elif defined(FEAT_PYTHON)
7164 f_pyeval(argvars, rettv);
7165# elif defined(FEAT_PYTHON3)
7166 f_py3eval(argvars, rettv);
7167# endif
7168}
7169#endif
7170
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007171static UINT32_T srand_seed_for_testing = 0;
7172static int srand_seed_for_testing_is_used = FALSE;
7173
7174 static void
7175f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
7176{
7177 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007178 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007179 else
7180 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007181 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
7182 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007183 }
7184}
7185
7186 static void
7187init_srand(UINT32_T *x)
7188{
7189#ifndef MSWIN
7190 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
7191#endif
7192
7193 if (srand_seed_for_testing_is_used)
7194 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007195 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007196 return;
7197 }
7198#ifndef MSWIN
7199 if (dev_urandom_state != FAIL)
7200 {
7201 int fd = open("/dev/urandom", O_RDONLY);
7202 struct {
7203 union {
7204 UINT32_T number;
7205 char bytes[sizeof(UINT32_T)];
7206 } contents;
7207 } buf;
7208
7209 // Attempt reading /dev/urandom.
7210 if (fd == -1)
7211 dev_urandom_state = FAIL;
7212 else
7213 {
7214 buf.contents.number = 0;
7215 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
7216 != sizeof(UINT32_T))
7217 dev_urandom_state = FAIL;
7218 else
7219 {
7220 dev_urandom_state = OK;
7221 *x = buf.contents.number;
7222 }
7223 close(fd);
7224 }
7225 }
7226 if (dev_urandom_state != OK)
7227 // Reading /dev/urandom doesn't work, fall back to time().
7228#endif
7229 *x = vim_time();
7230}
7231
7232#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
7233#define SPLITMIX32(x, z) ( \
7234 z = (x += 0x9e3779b9), \
7235 z = (z ^ (z >> 16)) * 0x85ebca6b, \
7236 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
7237 z ^ (z >> 16) \
7238 )
7239#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
7240 result = ROTL(y * 5, 7) * 9; \
7241 t = y << 9; \
7242 z ^= x; \
7243 w ^= y; \
7244 y ^= z, x ^= w; \
7245 z ^= t; \
7246 w = ROTL(w, 11);
7247
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007248/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007249 * "rand()" function
7250 */
7251 static void
7252f_rand(typval_T *argvars, typval_T *rettv)
7253{
7254 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007255 static UINT32_T gx, gy, gz, gw;
7256 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007257 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01007258 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007259
7260 if (argvars[0].v_type == VAR_UNKNOWN)
7261 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007262 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007263 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007264 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007265 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007266 init_srand(&x);
7267
7268 gx = SPLITMIX32(x, z);
7269 gy = SPLITMIX32(x, z);
7270 gz = SPLITMIX32(x, z);
7271 gw = SPLITMIX32(x, z);
7272 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007273 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007274
7275 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007276 }
7277 else if (argvars[0].v_type == VAR_LIST)
7278 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007279 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007280 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007281 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007282
7283 lx = list_find(l, 0L);
7284 ly = list_find(l, 1L);
7285 lz = list_find(l, 2L);
7286 lw = list_find(l, 3L);
7287 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
7288 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
7289 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
7290 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
7291 x = (UINT32_T)lx->li_tv.vval.v_number;
7292 y = (UINT32_T)ly->li_tv.vval.v_number;
7293 z = (UINT32_T)lz->li_tv.vval.v_number;
7294 w = (UINT32_T)lw->li_tv.vval.v_number;
7295
7296 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
7297
7298 lx->li_tv.vval.v_number = (varnumber_T)x;
7299 ly->li_tv.vval.v_number = (varnumber_T)y;
7300 lz->li_tv.vval.v_number = (varnumber_T)z;
7301 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007302 }
7303 else
7304 goto theend;
7305
7306 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007307 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007308 return;
7309
7310theend:
7311 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007312 rettv->v_type = VAR_NUMBER;
7313 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007314}
7315
7316/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007317 * "srand()" function
7318 */
7319 static void
7320f_srand(typval_T *argvars, typval_T *rettv)
7321{
7322 UINT32_T x = 0, z;
7323
7324 if (rettv_list_alloc(rettv) == FAIL)
7325 return;
7326 if (argvars[0].v_type == VAR_UNKNOWN)
7327 {
7328 init_srand(&x);
7329 }
7330 else
7331 {
7332 int error = FALSE;
7333
7334 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7335 if (error)
7336 return;
7337 }
7338
7339 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7340 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7341 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7342 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7343}
7344
7345#undef ROTL
7346#undef SPLITMIX32
7347#undef SHUFFLE_XOSHIRO128STARSTAR
7348
7349/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007350 * "range()" function
7351 */
7352 static void
7353f_range(typval_T *argvars, typval_T *rettv)
7354{
7355 varnumber_T start;
7356 varnumber_T end;
7357 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007358 int error = FALSE;
7359
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007360 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007361 if (argvars[1].v_type == VAR_UNKNOWN)
7362 {
7363 end = start - 1;
7364 start = 0;
7365 }
7366 else
7367 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007368 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007369 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007370 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007371 }
7372
7373 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007374 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007375 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007376 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007377 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007378 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007380 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381 list_T *list = rettv->vval.v_list;
7382
7383 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007384 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007385 // be called.
7386 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007387 list->lv_u.nonmat.lv_start = start;
7388 list->lv_u.nonmat.lv_end = end;
7389 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007390 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007391 }
7392}
7393
7394/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007395 * Materialize "list".
7396 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397 */
7398 void
7399range_list_materialize(list_T *list)
7400{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007401 varnumber_T start = list->lv_u.nonmat.lv_start;
7402 varnumber_T end = list->lv_u.nonmat.lv_end;
7403 int stride = list->lv_u.nonmat.lv_stride;
7404 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007405
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007406 list->lv_first = NULL;
7407 list->lv_u.mat.lv_last = NULL;
7408 list->lv_len = 0;
7409 list->lv_u.mat.lv_idx_item = NULL;
7410 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7411 if (list_append_number(list, (varnumber_T)i) == FAIL)
7412 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007413}
7414
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007415/*
7416 * "getreginfo()" function
7417 */
7418 static void
7419f_getreginfo(typval_T *argvars, typval_T *rettv)
7420{
7421 char_u *strregname;
7422 int regname;
7423 char_u buf[NUMBUFLEN + 2];
7424 long reglen = 0;
7425 dict_T *dict;
7426 list_T *list;
7427
7428 if (argvars[0].v_type != VAR_UNKNOWN)
7429 {
7430 strregname = tv_get_string_chk(&argvars[0]);
7431 if (strregname == NULL)
7432 return;
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007433 if (in_vim9script() && STRLEN(strregname) > 1)
7434 {
7435 semsg(_(e_register_name_must_be_one_char_str), strregname);
7436 return;
7437 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007438 }
7439 else
7440 strregname = get_vim_var_str(VV_REG);
7441
7442 regname = (strregname == NULL ? '"' : *strregname);
7443 if (regname == 0 || regname == '@')
7444 regname = '"';
7445
7446 if (rettv_dict_alloc(rettv) == FAIL)
7447 return;
7448 dict = rettv->vval.v_dict;
7449
7450 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7451 if (list == NULL)
7452 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007453 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007454
7455 buf[0] = NUL;
7456 buf[1] = NUL;
7457 switch (get_reg_type(regname, &reglen))
7458 {
7459 case MLINE: buf[0] = 'V'; break;
7460 case MCHAR: buf[0] = 'v'; break;
7461 case MBLOCK:
7462 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7463 reglen + 1);
7464 break;
7465 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007466 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007467
7468 buf[0] = get_register_name(get_unname_register());
7469 buf[1] = NUL;
7470 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007471 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007472 else
7473 {
7474 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7475
7476 if (item != NULL)
7477 {
7478 item->di_tv.v_type = VAR_SPECIAL;
7479 item->di_tv.vval.v_number = regname == buf[0]
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007480 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007481 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007482 }
7483 }
7484}
7485
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007486 static void
7487return_register(int regname, typval_T *rettv)
7488{
7489 char_u buf[2] = {0, 0};
7490
7491 buf[0] = (char_u)regname;
7492 rettv->v_type = VAR_STRING;
7493 rettv->vval.v_string = vim_strsave(buf);
7494}
7495
7496/*
7497 * "reg_executing()" function
7498 */
7499 static void
7500f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7501{
7502 return_register(reg_executing, rettv);
7503}
7504
7505/*
7506 * "reg_recording()" function
7507 */
7508 static void
7509f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7510{
7511 return_register(reg_recording, rettv);
7512}
7513
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007514/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007515 * "rename({from}, {to})" function
7516 */
7517 static void
7518f_rename(typval_T *argvars, typval_T *rettv)
7519{
7520 char_u buf[NUMBUFLEN];
7521
7522 if (check_restricted() || check_secure())
7523 rettv->vval.v_number = -1;
7524 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007525 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7526 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007527}
7528
7529/*
7530 * "repeat()" function
7531 */
7532 static void
7533f_repeat(typval_T *argvars, typval_T *rettv)
7534{
7535 char_u *p;
7536 int n;
7537 int slen;
7538 int len;
7539 char_u *r;
7540 int i;
7541
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007542 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007543 if (argvars[0].v_type == VAR_LIST)
7544 {
7545 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7546 while (n-- > 0)
7547 if (list_extend(rettv->vval.v_list,
7548 argvars[0].vval.v_list, NULL) == FAIL)
7549 break;
7550 }
7551 else
7552 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007553 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007554 rettv->v_type = VAR_STRING;
7555 rettv->vval.v_string = NULL;
7556
7557 slen = (int)STRLEN(p);
7558 len = slen * n;
7559 if (len <= 0)
7560 return;
7561
7562 r = alloc(len + 1);
7563 if (r != NULL)
7564 {
7565 for (i = 0; i < n; i++)
7566 mch_memmove(r + i * slen, p, (size_t)slen);
7567 r[len] = NUL;
7568 }
7569
7570 rettv->vval.v_string = r;
7571 }
7572}
7573
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007574#define SP_NOMOVE 0x01 // don't move cursor
7575#define SP_REPEAT 0x02 // repeat to find outer pair
7576#define SP_RETCOUNT 0x04 // return matchcount
7577#define SP_SETPCMARK 0x08 // set previous context mark
7578#define SP_START 0x10 // accept match at start position
7579#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7580#define SP_END 0x40 // leave cursor at end of match
7581#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007582
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007583/*
7584 * Get flags for a search function.
7585 * Possibly sets "p_ws".
7586 * Returns BACKWARD, FORWARD or zero (for an error).
7587 */
7588 static int
7589get_search_arg(typval_T *varp, int *flagsp)
7590{
7591 int dir = FORWARD;
7592 char_u *flags;
7593 char_u nbuf[NUMBUFLEN];
7594 int mask;
7595
7596 if (varp->v_type != VAR_UNKNOWN)
7597 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007598 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007599 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007600 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007601 while (*flags != NUL)
7602 {
7603 switch (*flags)
7604 {
7605 case 'b': dir = BACKWARD; break;
7606 case 'w': p_ws = TRUE; break;
7607 case 'W': p_ws = FALSE; break;
7608 default: mask = 0;
7609 if (flagsp != NULL)
7610 switch (*flags)
7611 {
7612 case 'c': mask = SP_START; break;
7613 case 'e': mask = SP_END; break;
7614 case 'm': mask = SP_RETCOUNT; break;
7615 case 'n': mask = SP_NOMOVE; break;
7616 case 'p': mask = SP_SUBPAT; break;
7617 case 'r': mask = SP_REPEAT; break;
7618 case 's': mask = SP_SETPCMARK; break;
7619 case 'z': mask = SP_COLUMN; break;
7620 }
7621 if (mask == 0)
7622 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007623 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007624 dir = 0;
7625 }
7626 else
7627 *flagsp |= mask;
7628 }
7629 if (dir == 0)
7630 break;
7631 ++flags;
7632 }
7633 }
7634 return dir;
7635}
7636
7637/*
7638 * Shared by search() and searchpos() functions.
7639 */
7640 static int
7641search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7642{
7643 int flags;
7644 char_u *pat;
7645 pos_T pos;
7646 pos_T save_cursor;
7647 int save_p_ws = p_ws;
7648 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007649 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007650 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007651#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007652 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007653 long time_limit = 0;
7654#endif
7655 int options = SEARCH_KEEP;
7656 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007657 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007658 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007659 pos_T firstpos;
7660
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007661 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007662 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007663 if (dir == 0)
7664 goto theend;
7665 flags = *flagsp;
7666 if (flags & SP_START)
7667 options |= SEARCH_START;
7668 if (flags & SP_END)
7669 options |= SEARCH_END;
7670 if (flags & SP_COLUMN)
7671 options |= SEARCH_COL;
7672
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007673 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007674 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7675 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007676 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007677 if (lnum_stop < 0)
7678 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007679 if (argvars[3].v_type != VAR_UNKNOWN)
7680 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007681#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007682 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007683 if (time_limit < 0)
7684 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007685#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007686 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007687 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007688 }
7689
7690#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007691 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007692 profile_setlimit(time_limit, &tm);
7693#endif
7694
7695 /*
7696 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7697 * Check to make sure only those flags are set.
7698 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7699 * flags cannot be set. Check for that condition also.
7700 */
7701 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7702 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7703 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007704 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007705 goto theend;
7706 }
7707
7708 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007709 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007710 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007711 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7712#ifdef FEAT_RELTIME
7713 sia.sa_tm = &tm;
7714#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007715
7716 // Repeat until {skip} returns FALSE.
7717 for (;;)
7718 {
7719 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007720 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007721 // finding the first match again means there is no match where {skip}
7722 // evaluates to zero.
7723 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7724 subpatnum = FAIL;
7725
Bram Moolenaara9c01042020-06-07 14:50:50 +02007726 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007727 // didn't find it or no skip argument
7728 break;
7729 firstpos = pos;
7730
Bram Moolenaara9c01042020-06-07 14:50:50 +02007731 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007732 {
7733 int do_skip;
7734 int err;
7735 pos_T save_pos = curwin->w_cursor;
7736
7737 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007738 err = FALSE;
7739 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007740 curwin->w_cursor = save_pos;
7741 if (err)
7742 {
7743 // Evaluating {skip} caused an error, break here.
7744 subpatnum = FAIL;
7745 break;
7746 }
7747 if (!do_skip)
7748 break;
7749 }
7750 }
7751
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007752 if (subpatnum != FAIL)
7753 {
7754 if (flags & SP_SUBPAT)
7755 retval = subpatnum;
7756 else
7757 retval = pos.lnum;
7758 if (flags & SP_SETPCMARK)
7759 setpcmark();
7760 curwin->w_cursor = pos;
7761 if (match_pos != NULL)
7762 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007763 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007764 match_pos->lnum = pos.lnum;
7765 match_pos->col = pos.col + 1;
7766 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007767 // "/$" will put the cursor after the end of the line, may need to
7768 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007769 check_cursor();
7770 }
7771
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007772 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007773 if (flags & SP_NOMOVE)
7774 curwin->w_cursor = save_cursor;
7775 else
7776 curwin->w_set_curswant = TRUE;
7777theend:
7778 p_ws = save_p_ws;
7779
7780 return retval;
7781}
7782
7783#ifdef FEAT_FLOAT
7784
7785/*
7786 * round() is not in C90, use ceil() or floor() instead.
7787 */
7788 float_T
7789vim_round(float_T f)
7790{
7791 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
7792}
7793
7794/*
7795 * "round({float})" function
7796 */
7797 static void
7798f_round(typval_T *argvars, typval_T *rettv)
7799{
7800 float_T f = 0.0;
7801
7802 rettv->v_type = VAR_FLOAT;
7803 if (get_float_arg(argvars, &f) == OK)
7804 rettv->vval.v_float = vim_round(f);
7805 else
7806 rettv->vval.v_float = 0.0;
7807}
7808#endif
7809
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007810#ifdef FEAT_RUBY
7811/*
7812 * "rubyeval()" function
7813 */
7814 static void
7815f_rubyeval(typval_T *argvars, typval_T *rettv)
7816{
7817 char_u *str;
7818 char_u buf[NUMBUFLEN];
7819
7820 str = tv_get_string_buf(&argvars[0], buf);
7821 do_rubyeval(str, rettv);
7822}
7823#endif
7824
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007825/*
7826 * "screenattr()" function
7827 */
7828 static void
7829f_screenattr(typval_T *argvars, typval_T *rettv)
7830{
7831 int row;
7832 int col;
7833 int c;
7834
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007835 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7836 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007837 if (row < 0 || row >= screen_Rows
7838 || col < 0 || col >= screen_Columns)
7839 c = -1;
7840 else
7841 c = ScreenAttrs[LineOffset[row] + col];
7842 rettv->vval.v_number = c;
7843}
7844
7845/*
7846 * "screenchar()" function
7847 */
7848 static void
7849f_screenchar(typval_T *argvars, typval_T *rettv)
7850{
7851 int row;
7852 int col;
7853 int off;
7854 int c;
7855
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007856 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7857 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007858 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007859 c = -1;
7860 else
7861 {
7862 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007863 if (enc_utf8 && ScreenLinesUC[off] != 0)
7864 c = ScreenLinesUC[off];
7865 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007866 c = ScreenLines[off];
7867 }
7868 rettv->vval.v_number = c;
7869}
7870
7871/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007872 * "screenchars()" function
7873 */
7874 static void
7875f_screenchars(typval_T *argvars, typval_T *rettv)
7876{
7877 int row;
7878 int col;
7879 int off;
7880 int c;
7881 int i;
7882
7883 if (rettv_list_alloc(rettv) == FAIL)
7884 return;
7885 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7886 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7887 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7888 return;
7889
7890 off = LineOffset[row] + col;
7891 if (enc_utf8 && ScreenLinesUC[off] != 0)
7892 c = ScreenLinesUC[off];
7893 else
7894 c = ScreenLines[off];
7895 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7896
7897 if (enc_utf8)
7898
7899 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7900 list_append_number(rettv->vval.v_list,
7901 (varnumber_T)ScreenLinesC[i][off]);
7902}
7903
7904/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007905 * "screencol()" function
7906 *
7907 * First column is 1 to be consistent with virtcol().
7908 */
7909 static void
7910f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7911{
7912 rettv->vval.v_number = screen_screencol() + 1;
7913}
7914
7915/*
7916 * "screenrow()" function
7917 */
7918 static void
7919f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7920{
7921 rettv->vval.v_number = screen_screenrow() + 1;
7922}
7923
7924/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007925 * "screenstring()" function
7926 */
7927 static void
7928f_screenstring(typval_T *argvars, typval_T *rettv)
7929{
7930 int row;
7931 int col;
7932 int off;
7933 int c;
7934 int i;
7935 char_u buf[MB_MAXBYTES + 1];
7936 int buflen = 0;
7937
7938 rettv->vval.v_string = NULL;
7939 rettv->v_type = VAR_STRING;
7940
7941 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7942 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7943 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7944 return;
7945
7946 off = LineOffset[row] + col;
7947 if (enc_utf8 && ScreenLinesUC[off] != 0)
7948 c = ScreenLinesUC[off];
7949 else
7950 c = ScreenLines[off];
7951 buflen += mb_char2bytes(c, buf);
7952
Bram Moolenaarf1387282021-03-22 17:11:15 +01007953 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007954 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7955 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7956
7957 buf[buflen] = NUL;
7958 rettv->vval.v_string = vim_strsave(buf);
7959}
7960
7961/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007962 * "search()" function
7963 */
7964 static void
7965f_search(typval_T *argvars, typval_T *rettv)
7966{
7967 int flags = 0;
7968
7969 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7970}
7971
7972/*
7973 * "searchdecl()" function
7974 */
7975 static void
7976f_searchdecl(typval_T *argvars, typval_T *rettv)
7977{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007978 int locally = TRUE;
7979 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007980 int error = FALSE;
7981 char_u *name;
7982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007983 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007984
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007985 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007986 if (argvars[1].v_type != VAR_UNKNOWN)
7987 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007988 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007989 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007990 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007991 }
7992 if (!error && name != NULL)
7993 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7994 locally, thisblock, SEARCH_KEEP) == FAIL;
7995}
7996
7997/*
7998 * Used by searchpair() and searchpairpos()
7999 */
8000 static int
8001searchpair_cmn(typval_T *argvars, pos_T *match_pos)
8002{
8003 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01008004 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008005 int save_p_ws = p_ws;
8006 int dir;
8007 int flags = 0;
8008 char_u nbuf1[NUMBUFLEN];
8009 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008010 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008011 long lnum_stop = 0;
8012 long time_limit = 0;
8013
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008014 // Get the three pattern arguments: start, middle, end. Will result in an
8015 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008016 spat = tv_get_string_chk(&argvars[0]);
8017 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
8018 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008019 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008020 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008021
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008022 // Handle the optional fourth argument: flags
8023 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008024 if (dir == 0)
8025 goto theend;
8026
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008027 // Don't accept SP_END or SP_SUBPAT.
8028 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008029 if ((flags & (SP_END | SP_SUBPAT)) != 0
8030 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
8031 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008032 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008033 goto theend;
8034 }
8035
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008036 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008037 if (flags & SP_REPEAT)
8038 p_ws = FALSE;
8039
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008040 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008041 if (argvars[3].v_type == VAR_UNKNOWN
8042 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01008043 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008044 else
8045 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02008046 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01008047 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02008048
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008049 if (argvars[5].v_type != VAR_UNKNOWN)
8050 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008051 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008052 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008053 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008054 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008055 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008056 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008057#ifdef FEAT_RELTIME
8058 if (argvars[6].v_type != VAR_UNKNOWN)
8059 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008060 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008061 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008062 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008063 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008064 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02008065 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008066 }
8067#endif
8068 }
8069 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008070
8071 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
8072 match_pos, lnum_stop, time_limit);
8073
8074theend:
8075 p_ws = save_p_ws;
8076
8077 return retval;
8078}
8079
8080/*
8081 * "searchpair()" function
8082 */
8083 static void
8084f_searchpair(typval_T *argvars, typval_T *rettv)
8085{
8086 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
8087}
8088
8089/*
8090 * "searchpairpos()" function
8091 */
8092 static void
8093f_searchpairpos(typval_T *argvars, typval_T *rettv)
8094{
8095 pos_T match_pos;
8096 int lnum = 0;
8097 int col = 0;
8098
8099 if (rettv_list_alloc(rettv) == FAIL)
8100 return;
8101
8102 if (searchpair_cmn(argvars, &match_pos) > 0)
8103 {
8104 lnum = match_pos.lnum;
8105 col = match_pos.col;
8106 }
8107
8108 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8109 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8110}
8111
8112/*
8113 * Search for a start/middle/end thing.
8114 * Used by searchpair(), see its documentation for the details.
8115 * Returns 0 or -1 for no match,
8116 */
8117 long
8118do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008119 char_u *spat, // start pattern
8120 char_u *mpat, // middle pattern
8121 char_u *epat, // end pattern
8122 int dir, // BACKWARD or FORWARD
8123 typval_T *skip, // skip expression
8124 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008125 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008126 linenr_T lnum_stop, // stop at this line if not zero
8127 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008128{
8129 char_u *save_cpo;
8130 char_u *pat, *pat2 = NULL, *pat3 = NULL;
8131 long retval = 0;
8132 pos_T pos;
8133 pos_T firstpos;
8134 pos_T foundpos;
8135 pos_T save_cursor;
8136 pos_T save_pos;
8137 int n;
8138 int r;
8139 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01008140 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008141 int err;
8142 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008143#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008144 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008145#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008146
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008147 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008148 save_cpo = p_cpo;
8149 p_cpo = empty_option;
8150
8151#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008152 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008153 profile_setlimit(time_limit, &tm);
8154#endif
8155
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008156 // Make two search patterns: start/end (pat2, for in nested pairs) and
8157 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02008158 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
8159 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008160 if (pat2 == NULL || pat3 == NULL)
8161 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008162 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008163 if (*mpat == NUL)
8164 STRCPY(pat3, pat2);
8165 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008166 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008167 spat, epat, mpat);
8168 if (flags & SP_START)
8169 options |= SEARCH_START;
8170
Bram Moolenaar48570482017-10-30 21:48:41 +01008171 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02008172 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01008173
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008174 save_cursor = curwin->w_cursor;
8175 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008176 CLEAR_POS(&firstpos);
8177 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008178 pat = pat3;
8179 for (;;)
8180 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008181 searchit_arg_T sia;
8182
Bram Moolenaara80faa82020-04-12 19:37:17 +02008183 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008184 sia.sa_stop_lnum = lnum_stop;
8185#ifdef FEAT_RELTIME
8186 sia.sa_tm = &tm;
8187#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01008188 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008189 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008190 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008191 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008192 break;
8193
8194 if (firstpos.lnum == 0)
8195 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008196 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008197 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008198 // Found the same position again. Can happen with a pattern that
8199 // has "\zs" at the end and searching backwards. Advance one
8200 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008201 if (dir == BACKWARD)
8202 decl(&pos);
8203 else
8204 incl(&pos);
8205 }
8206 foundpos = pos;
8207
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008208 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008209 options &= ~SEARCH_START;
8210
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008211 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01008212 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008213 {
8214 save_pos = curwin->w_cursor;
8215 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01008216 err = FALSE;
8217 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008218 curwin->w_cursor = save_pos;
8219 if (err)
8220 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008221 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008222 curwin->w_cursor = save_cursor;
8223 retval = -1;
8224 break;
8225 }
8226 if (r)
8227 continue;
8228 }
8229
8230 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8231 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008232 // Found end when searching backwards or start when searching
8233 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008234 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008235 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008236 }
8237 else
8238 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008239 // Found end when searching forward or start when searching
8240 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008241 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008242 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008243 }
8244
8245 if (nest == 0)
8246 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008247 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008248 if (flags & SP_RETCOUNT)
8249 ++retval;
8250 else
8251 retval = pos.lnum;
8252 if (flags & SP_SETPCMARK)
8253 setpcmark();
8254 curwin->w_cursor = pos;
8255 if (!(flags & SP_REPEAT))
8256 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008257 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008258 }
8259 }
8260
8261 if (match_pos != NULL)
8262 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008263 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008264 match_pos->lnum = curwin->w_cursor.lnum;
8265 match_pos->col = curwin->w_cursor.col + 1;
8266 }
8267
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008268 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008269 if ((flags & SP_NOMOVE) || retval == 0)
8270 curwin->w_cursor = save_cursor;
8271
8272theend:
8273 vim_free(pat2);
8274 vim_free(pat3);
8275 if (p_cpo == empty_option)
8276 p_cpo = save_cpo;
8277 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008278 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008279 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008280 // If it's still empty it was changed and restored, need to restore in
8281 // the complicated way.
8282 if (*p_cpo == NUL)
8283 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008284 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008285 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008286
8287 return retval;
8288}
8289
8290/*
8291 * "searchpos()" function
8292 */
8293 static void
8294f_searchpos(typval_T *argvars, typval_T *rettv)
8295{
8296 pos_T match_pos;
8297 int lnum = 0;
8298 int col = 0;
8299 int n;
8300 int flags = 0;
8301
8302 if (rettv_list_alloc(rettv) == FAIL)
8303 return;
8304
8305 n = search_cmn(argvars, &match_pos, &flags);
8306 if (n > 0)
8307 {
8308 lnum = match_pos.lnum;
8309 col = match_pos.col;
8310 }
8311
8312 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8313 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8314 if (flags & SP_SUBPAT)
8315 list_append_number(rettv->vval.v_list, (varnumber_T)n);
8316}
8317
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008318/*
8319 * Set the cursor or mark position.
8320 * If 'charpos' is TRUE, then use the column number as a character offet.
8321 * Otherwise use the column number as a byte offset.
8322 */
8323 static void
8324set_position(typval_T *argvars, typval_T *rettv, int charpos)
8325{
8326 pos_T pos;
8327 int fnum;
8328 char_u *name;
8329 colnr_T curswant = -1;
8330
8331 rettv->vval.v_number = -1;
8332
8333 name = tv_get_string_chk(argvars);
8334 if (name != NULL)
8335 {
8336 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
8337 {
8338 if (pos.col != MAXCOL && --pos.col < 0)
8339 pos.col = 0;
8340 if ((name[0] == '.' && name[1] == NUL))
8341 {
8342 // set cursor; "fnum" is ignored
8343 curwin->w_cursor = pos;
8344 if (curswant >= 0)
8345 {
8346 curwin->w_curswant = curswant - 1;
8347 curwin->w_set_curswant = FALSE;
8348 }
8349 check_cursor();
8350 rettv->vval.v_number = 0;
8351 }
8352 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8353 {
8354 // set mark
8355 if (setmark_pos(name[1], &pos, fnum) == OK)
8356 rettv->vval.v_number = 0;
8357 }
8358 else
8359 emsg(_(e_invarg));
8360 }
8361 }
8362}
8363/*
8364 * "setcharpos()" function
8365 */
8366 static void
8367f_setcharpos(typval_T *argvars, typval_T *rettv)
8368{
8369 set_position(argvars, rettv, TRUE);
8370}
8371
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008372 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008373f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8374{
8375 dict_T *d;
8376 dictitem_T *di;
8377 char_u *csearch;
8378
8379 if (argvars[0].v_type != VAR_DICT)
8380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008381 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008382 return;
8383 }
8384
8385 if ((d = argvars[0].vval.v_dict) != NULL)
8386 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008387 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008388 if (csearch != NULL)
8389 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008390 if (enc_utf8)
8391 {
8392 int pcc[MAX_MCO];
8393 int c = utfc_ptr2char(csearch, pcc);
8394
8395 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8396 }
8397 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008398 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008399 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008400 }
8401
8402 di = dict_find(d, (char_u *)"forward", -1);
8403 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008404 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008405 ? FORWARD : BACKWARD);
8406
8407 di = dict_find(d, (char_u *)"until", -1);
8408 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008409 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008410 }
8411}
8412
8413/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008414 * "setcursorcharpos" function
8415 */
8416 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008417f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008418{
8419 set_cursorpos(argvars, rettv, TRUE);
8420}
8421
8422/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008423 * "setenv()" function
8424 */
8425 static void
8426f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8427{
8428 char_u namebuf[NUMBUFLEN];
8429 char_u valbuf[NUMBUFLEN];
8430 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8431
8432 if (argvars[1].v_type == VAR_SPECIAL
8433 && argvars[1].vval.v_number == VVAL_NULL)
8434 vim_unsetenv(name);
8435 else
8436 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8437}
8438
8439/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008440 * "setfperm({fname}, {mode})" function
8441 */
8442 static void
8443f_setfperm(typval_T *argvars, typval_T *rettv)
8444{
8445 char_u *fname;
8446 char_u modebuf[NUMBUFLEN];
8447 char_u *mode_str;
8448 int i;
8449 int mask;
8450 int mode = 0;
8451
8452 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008453 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008454 if (fname == NULL)
8455 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008456 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008457 if (mode_str == NULL)
8458 return;
8459 if (STRLEN(mode_str) != 9)
8460 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008461 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008462 return;
8463 }
8464
8465 mask = 1;
8466 for (i = 8; i >= 0; --i)
8467 {
8468 if (mode_str[i] != '-')
8469 mode |= mask;
8470 mask = mask << 1;
8471 }
8472 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8473}
8474
8475/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008476 * "setpos()" function
8477 */
8478 static void
8479f_setpos(typval_T *argvars, typval_T *rettv)
8480{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008481 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008482}
8483
8484/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008485 * Translate a register type string to the yank type and block length
8486 */
8487 static int
8488get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8489{
8490 char_u *stropt = *pp;
8491 switch (*stropt)
8492 {
8493 case 'v': case 'c': // character-wise selection
8494 *yank_type = MCHAR;
8495 break;
8496 case 'V': case 'l': // line-wise selection
8497 *yank_type = MLINE;
8498 break;
8499 case 'b': case Ctrl_V: // block-wise selection
8500 *yank_type = MBLOCK;
8501 if (VIM_ISDIGIT(stropt[1]))
8502 {
8503 ++stropt;
8504 *block_len = getdigits(&stropt) - 1;
8505 --stropt;
8506 }
8507 break;
8508 default:
8509 return FAIL;
8510 }
8511 *pp = stropt;
8512 return OK;
8513}
8514
8515/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008516 * "setreg()" function
8517 */
8518 static void
8519f_setreg(typval_T *argvars, typval_T *rettv)
8520{
8521 int regname;
8522 char_u *strregname;
8523 char_u *stropt;
8524 char_u *strval;
8525 int append;
8526 char_u yank_type;
8527 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008528 typval_T *regcontents;
8529 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008530
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008531 pointreg = 0;
8532 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008533 block_len = -1;
8534 yank_type = MAUTO;
8535 append = FALSE;
8536
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008537 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008538 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008539
8540 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008541 return; // type error; errmsg already given
Bram Moolenaar418a29f2021-02-10 22:23:41 +01008542 if (in_vim9script() && STRLEN(strregname) > 1)
8543 {
8544 semsg(_(e_register_name_must_be_one_char_str), strregname);
8545 return;
8546 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008547 regname = *strregname;
8548 if (regname == 0 || regname == '@')
8549 regname = '"';
8550
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008551 if (argvars[1].v_type == VAR_DICT)
8552 {
8553 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008554 dictitem_T *di;
8555
8556 if (d == NULL || d->dv_hashtab.ht_used == 0)
8557 {
8558 // Empty dict, clear the register (like setreg(0, []))
8559 char_u *lstval[2] = {NULL, NULL};
8560 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8561 return;
8562 }
8563
8564 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008565 if (di != NULL)
8566 regcontents = &di->di_tv;
8567
8568 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8569 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008570 {
8571 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8572
8573 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008574 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008575 semsg(_(e_invargval), "value");
8576 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008577 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008578 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008579
8580 if (regname == '"')
8581 {
8582 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8583 if (stropt != NULL)
8584 {
8585 pointreg = *stropt;
8586 regname = pointreg;
8587 }
8588 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008589 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008590 pointreg = regname;
8591 }
8592 else
8593 regcontents = &argvars[1];
8594
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008595 if (argvars[2].v_type != VAR_UNKNOWN)
8596 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008597 if (yank_type != MAUTO)
8598 {
8599 semsg(_(e_toomanyarg), "setreg");
8600 return;
8601 }
8602
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008603 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008604 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008605 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008606 for (; *stropt != NUL; ++stropt)
8607 switch (*stropt)
8608 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008609 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008610 append = TRUE;
8611 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008612 default:
8613 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008614 }
8615 }
8616
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008617 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008618 {
8619 char_u **lstval;
8620 char_u **allocval;
8621 char_u buf[NUMBUFLEN];
8622 char_u **curval;
8623 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008624 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008625 listitem_T *li;
8626 int len;
8627
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008628 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008629 len = ll == NULL ? 0 : ll->lv_len;
8630
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008631 // First half: use for pointers to result lines; second half: use for
8632 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008633 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008634 if (lstval == NULL)
8635 return;
8636 curval = lstval;
8637 allocval = lstval + len + 2;
8638 curallocval = allocval;
8639
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008640 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008641 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008642 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008643 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008644 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008645 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008646 if (strval == NULL)
8647 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008648 if (strval == buf)
8649 {
8650 // Need to make a copy, next tv_get_string_buf_chk() will
8651 // overwrite the string.
8652 strval = vim_strsave(buf);
8653 if (strval == NULL)
8654 goto free_lstval;
8655 *curallocval++ = strval;
8656 }
8657 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008658 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008659 }
8660 *curval++ = NULL;
8661
8662 write_reg_contents_lst(regname, lstval, -1,
8663 append, yank_type, block_len);
8664free_lstval:
8665 while (curallocval > allocval)
8666 vim_free(*--curallocval);
8667 vim_free(lstval);
8668 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008669 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008670 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008671 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008672 if (strval == NULL)
8673 return;
8674 write_reg_contents_ex(regname, strval, -1,
8675 append, yank_type, block_len);
8676 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008677 if (pointreg != 0)
8678 get_yank_register(pointreg, TRUE);
8679
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008680 rettv->vval.v_number = 0;
8681}
8682
8683/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008684 * "settagstack()" function
8685 */
8686 static void
8687f_settagstack(typval_T *argvars, typval_T *rettv)
8688{
8689 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8690 win_T *wp;
8691 dict_T *d;
8692 int action = 'r';
8693
8694 rettv->vval.v_number = -1;
8695
8696 // first argument: window number or id
8697 wp = find_win_by_nr_or_id(&argvars[0]);
8698 if (wp == NULL)
8699 return;
8700
8701 // second argument: dict with items to set in the tag stack
8702 if (argvars[1].v_type != VAR_DICT)
8703 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008704 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008705 return;
8706 }
8707 d = argvars[1].vval.v_dict;
8708 if (d == NULL)
8709 return;
8710
8711 // third argument: action - 'a' for append and 'r' for replace.
8712 // default is to replace the stack.
8713 if (argvars[2].v_type == VAR_UNKNOWN)
8714 action = 'r';
8715 else if (argvars[2].v_type == VAR_STRING)
8716 {
8717 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008718 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008719 if (actstr == NULL)
8720 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008721 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8722 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008723 action = *actstr;
8724 else
8725 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008726 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008727 return;
8728 }
8729 }
8730 else
8731 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008732 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008733 return;
8734 }
8735
8736 if (set_tagstack(wp, d, action) == OK)
8737 rettv->vval.v_number = 0;
8738}
8739
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008740#ifdef FEAT_CRYPT
8741/*
8742 * "sha256({string})" function
8743 */
8744 static void
8745f_sha256(typval_T *argvars, typval_T *rettv)
8746{
8747 char_u *p;
8748
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008749 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008750 rettv->vval.v_string = vim_strsave(
8751 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8752 rettv->v_type = VAR_STRING;
8753}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008754#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008755
8756/*
8757 * "shellescape({string})" function
8758 */
8759 static void
8760f_shellescape(typval_T *argvars, typval_T *rettv)
8761{
Bram Moolenaar20615522017-06-05 18:46:26 +02008762 int do_special = non_zero_arg(&argvars[1]);
8763
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008764 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008765 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008766 rettv->v_type = VAR_STRING;
8767}
8768
8769/*
8770 * shiftwidth() function
8771 */
8772 static void
8773f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8774{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008775 rettv->vval.v_number = 0;
8776
8777 if (argvars[0].v_type != VAR_UNKNOWN)
8778 {
8779 long col;
8780
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008781 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008782 if (col < 0)
8783 return; // type error; errmsg already given
8784#ifdef FEAT_VARTABS
8785 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8786 return;
8787#endif
8788 }
8789
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008790 rettv->vval.v_number = get_sw_value(curbuf);
8791}
8792
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008793#ifdef FEAT_FLOAT
8794/*
8795 * "sin()" function
8796 */
8797 static void
8798f_sin(typval_T *argvars, typval_T *rettv)
8799{
8800 float_T f = 0.0;
8801
8802 rettv->v_type = VAR_FLOAT;
8803 if (get_float_arg(argvars, &f) == OK)
8804 rettv->vval.v_float = sin(f);
8805 else
8806 rettv->vval.v_float = 0.0;
8807}
8808
8809/*
8810 * "sinh()" function
8811 */
8812 static void
8813f_sinh(typval_T *argvars, typval_T *rettv)
8814{
8815 float_T f = 0.0;
8816
8817 rettv->v_type = VAR_FLOAT;
8818 if (get_float_arg(argvars, &f) == OK)
8819 rettv->vval.v_float = sinh(f);
8820 else
8821 rettv->vval.v_float = 0.0;
8822}
8823#endif
8824
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008825/*
8826 * "soundfold({word})" function
8827 */
8828 static void
8829f_soundfold(typval_T *argvars, typval_T *rettv)
8830{
8831 char_u *s;
8832
8833 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008834 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835#ifdef FEAT_SPELL
8836 rettv->vval.v_string = eval_soundfold(s);
8837#else
8838 rettv->vval.v_string = vim_strsave(s);
8839#endif
8840}
8841
8842/*
8843 * "spellbadword()" function
8844 */
8845 static void
8846f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8847{
8848 char_u *word = (char_u *)"";
8849 hlf_T attr = HLF_COUNT;
8850 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008851#ifdef FEAT_SPELL
8852 int wo_spell_save = curwin->w_p_spell;
8853
8854 if (!curwin->w_p_spell)
8855 {
8856 did_set_spelllang(curwin);
8857 curwin->w_p_spell = TRUE;
8858 }
8859
8860 if (*curwin->w_s->b_p_spl == NUL)
8861 {
8862 emsg(_(e_no_spell));
8863 curwin->w_p_spell = wo_spell_save;
8864 return;
8865 }
8866#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008867
8868 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008869 {
8870#ifdef FEAT_SPELL
8871 curwin->w_p_spell = wo_spell_save;
8872#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008873 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008874 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875
8876#ifdef FEAT_SPELL
8877 if (argvars[0].v_type == VAR_UNKNOWN)
8878 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008879 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008880 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8881 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008882 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008883 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008884 curwin->w_set_curswant = TRUE;
8885 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008886 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008887 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008888 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008889 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008890 int capcol = -1;
8891
8892 if (str != NULL)
8893 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008894 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008895 while (*str != NUL)
8896 {
8897 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8898 if (attr != HLF_COUNT)
8899 {
8900 word = str;
8901 break;
8902 }
8903 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008904 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008905 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008906 }
8907 }
8908 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008909 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008910#endif
8911
8912 list_append_string(rettv->vval.v_list, word, len);
8913 list_append_string(rettv->vval.v_list, (char_u *)(
8914 attr == HLF_SPB ? "bad" :
8915 attr == HLF_SPR ? "rare" :
8916 attr == HLF_SPL ? "local" :
8917 attr == HLF_SPC ? "caps" :
8918 ""), -1);
8919}
8920
8921/*
8922 * "spellsuggest()" function
8923 */
8924 static void
8925f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8926{
8927#ifdef FEAT_SPELL
8928 char_u *str;
8929 int typeerr = FALSE;
8930 int maxcount;
8931 garray_T ga;
8932 int i;
8933 listitem_T *li;
8934 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008935 int wo_spell_save = curwin->w_p_spell;
8936
8937 if (!curwin->w_p_spell)
8938 {
8939 did_set_spelllang(curwin);
8940 curwin->w_p_spell = TRUE;
8941 }
8942
8943 if (*curwin->w_s->b_p_spl == NUL)
8944 {
8945 emsg(_(e_no_spell));
8946 curwin->w_p_spell = wo_spell_save;
8947 return;
8948 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008949#endif
8950
8951 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008952 {
8953#ifdef FEAT_SPELL
8954 curwin->w_p_spell = wo_spell_save;
8955#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008956 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008957 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008958
8959#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008960 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008961 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008962 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008963 if (argvars[1].v_type != VAR_UNKNOWN)
8964 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008965 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008966 if (maxcount <= 0)
8967 return;
8968 if (argvars[2].v_type != VAR_UNKNOWN)
8969 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008970 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008971 if (typeerr)
8972 return;
8973 }
8974 }
8975 else
8976 maxcount = 25;
8977
8978 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8979
8980 for (i = 0; i < ga.ga_len; ++i)
8981 {
8982 str = ((char_u **)ga.ga_data)[i];
8983
8984 li = listitem_alloc();
8985 if (li == NULL)
8986 vim_free(str);
8987 else
8988 {
8989 li->li_tv.v_type = VAR_STRING;
8990 li->li_tv.v_lock = 0;
8991 li->li_tv.vval.v_string = str;
8992 list_append(rettv->vval.v_list, li);
8993 }
8994 }
8995 ga_clear(&ga);
8996 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008997 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008998#endif
8999}
9000
9001 static void
9002f_split(typval_T *argvars, typval_T *rettv)
9003{
9004 char_u *str;
9005 char_u *end;
9006 char_u *pat = NULL;
9007 regmatch_T regmatch;
9008 char_u patbuf[NUMBUFLEN];
9009 char_u *save_cpo;
9010 int match;
9011 colnr_T col = 0;
9012 int keepempty = FALSE;
9013 int typeerr = FALSE;
9014
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009015 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009016 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01009017 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009018
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009019 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009020 if (argvars[1].v_type != VAR_UNKNOWN)
9021 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009022 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009023 if (pat == NULL)
9024 typeerr = TRUE;
9025 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02009026 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009027 }
9028 if (pat == NULL || *pat == NUL)
9029 pat = (char_u *)"[\\x01- ]\\+";
9030
9031 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02009032 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02009034 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009035
9036 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9037 if (regmatch.regprog != NULL)
9038 {
9039 regmatch.rm_ic = FALSE;
9040 while (*str != NUL || keepempty)
9041 {
9042 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009043 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009044 else
9045 match = vim_regexec_nl(&regmatch, str, col);
9046 if (match)
9047 end = regmatch.startp[0];
9048 else
9049 end = str + STRLEN(str);
9050 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
9051 && *str != NUL && match && end < regmatch.endp[0]))
9052 {
9053 if (list_append_string(rettv->vval.v_list, str,
9054 (int)(end - str)) == FAIL)
9055 break;
9056 }
9057 if (!match)
9058 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01009059 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009060 if (regmatch.endp[0] > str)
9061 col = 0;
9062 else
Bram Moolenaar13505972019-01-24 15:04:48 +01009063 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009064 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009065 str = regmatch.endp[0];
9066 }
9067
9068 vim_regfree(regmatch.regprog);
9069 }
9070
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02009071theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009072 p_cpo = save_cpo;
9073}
9074
9075#ifdef FEAT_FLOAT
9076/*
9077 * "sqrt()" function
9078 */
9079 static void
9080f_sqrt(typval_T *argvars, typval_T *rettv)
9081{
9082 float_T f = 0.0;
9083
9084 rettv->v_type = VAR_FLOAT;
9085 if (get_float_arg(argvars, &f) == OK)
9086 rettv->vval.v_float = sqrt(f);
9087 else
9088 rettv->vval.v_float = 0.0;
9089}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009090#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009091
Bram Moolenaar0387cae2019-11-29 21:07:58 +01009092#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01009093/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009094 * "str2float()" function
9095 */
9096 static void
9097f_str2float(typval_T *argvars, typval_T *rettv)
9098{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009099 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009100 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009101
Bram Moolenaar08243d22017-01-10 16:12:29 +01009102 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009103 p = skipwhite(p + 1);
9104 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01009105 if (isneg)
9106 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009107 rettv->v_type = VAR_FLOAT;
9108}
9109#endif
9110
9111/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02009112 * "str2list()" function
9113 */
9114 static void
9115f_str2list(typval_T *argvars, typval_T *rettv)
9116{
9117 char_u *p;
9118 int utf8 = FALSE;
9119
9120 if (rettv_list_alloc(rettv) == FAIL)
9121 return;
9122
9123 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02009124 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02009125
9126 p = tv_get_string(&argvars[0]);
9127
9128 if (has_mbyte || utf8)
9129 {
9130 int (*ptr2len)(char_u *);
9131 int (*ptr2char)(char_u *);
9132
9133 if (utf8 || enc_utf8)
9134 {
9135 ptr2len = utf_ptr2len;
9136 ptr2char = utf_ptr2char;
9137 }
9138 else
9139 {
9140 ptr2len = mb_ptr2len;
9141 ptr2char = mb_ptr2char;
9142 }
9143
9144 for ( ; *p != NUL; p += (*ptr2len)(p))
9145 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
9146 }
9147 else
9148 for ( ; *p != NUL; ++p)
9149 list_append_number(rettv->vval.v_list, *p);
9150}
9151
9152/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009153 * "str2nr()" function
9154 */
9155 static void
9156f_str2nr(typval_T *argvars, typval_T *rettv)
9157{
9158 int base = 10;
9159 char_u *p;
9160 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009161 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01009162 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009163
9164 if (argvars[1].v_type != VAR_UNKNOWN)
9165 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009166 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009167 if (base != 2 && base != 8 && base != 10 && base != 16)
9168 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009169 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009170 return;
9171 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02009172 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009173 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009174 }
9175
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01009176 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009177 isneg = (*p == '-');
9178 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009179 p = skipwhite(p + 1);
9180 switch (base)
9181 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009182 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02009183 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009184 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009185 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02009186 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
9187 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01009188 if (isneg)
9189 rettv->vval.v_number = -n;
9190 else
9191 rettv->vval.v_number = n;
9192
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009193}
9194
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009195/*
9196 * "strgetchar()" function
9197 */
9198 static void
9199f_strgetchar(typval_T *argvars, typval_T *rettv)
9200{
9201 char_u *str;
9202 int len;
9203 int error = FALSE;
9204 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01009205 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009206
9207 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009208 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009209 if (str == NULL)
9210 return;
9211 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009212 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009213 if (error)
9214 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009215
Bram Moolenaar13505972019-01-24 15:04:48 +01009216 while (charidx >= 0 && byteidx < len)
9217 {
9218 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009219 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009220 rettv->vval.v_number = mb_ptr2char(str + byteidx);
9221 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009222 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009223 --charidx;
9224 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009225 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009226}
9227
9228/*
9229 * "stridx()" function
9230 */
9231 static void
9232f_stridx(typval_T *argvars, typval_T *rettv)
9233{
9234 char_u buf[NUMBUFLEN];
9235 char_u *needle;
9236 char_u *haystack;
9237 char_u *save_haystack;
9238 char_u *pos;
9239 int start_idx;
9240
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009241 needle = tv_get_string_chk(&argvars[1]);
9242 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009243 rettv->vval.v_number = -1;
9244 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009245 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009246
9247 if (argvars[2].v_type != VAR_UNKNOWN)
9248 {
9249 int error = FALSE;
9250
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009251 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009252 if (error || start_idx >= (int)STRLEN(haystack))
9253 return;
9254 if (start_idx >= 0)
9255 haystack += start_idx;
9256 }
9257
9258 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9259 if (pos != NULL)
9260 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
9261}
9262
9263/*
9264 * "string()" function
9265 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009266 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009267f_string(typval_T *argvars, typval_T *rettv)
9268{
9269 char_u *tofree;
9270 char_u numbuf[NUMBUFLEN];
9271
9272 rettv->v_type = VAR_STRING;
9273 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
9274 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009275 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009276 if (rettv->vval.v_string != NULL && tofree == NULL)
9277 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
9278}
9279
9280/*
9281 * "strlen()" function
9282 */
9283 static void
9284f_strlen(typval_T *argvars, typval_T *rettv)
9285{
9286 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009287 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009288}
9289
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01009290 static void
9291strchar_common(typval_T *argvars, typval_T *rettv, int skipcc)
9292{
9293 char_u *s = tv_get_string(&argvars[0]);
9294 varnumber_T len = 0;
9295 int (*func_mb_ptr2char_adv)(char_u **pp);
9296
9297 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
9298 while (*s != NUL)
9299 {
9300 func_mb_ptr2char_adv(&s);
9301 ++len;
9302 }
9303 rettv->vval.v_number = len;
9304}
9305
9306/*
9307 * "strcharlen()" function
9308 */
9309 static void
9310f_strcharlen(typval_T *argvars, typval_T *rettv)
9311{
9312 strchar_common(argvars, rettv, TRUE);
9313}
9314
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009315/*
9316 * "strchars()" function
9317 */
9318 static void
9319f_strchars(typval_T *argvars, typval_T *rettv)
9320{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009321 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009322
9323 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01009324 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009325 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02009326 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009327 else
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01009328 strchar_common(argvars, rettv, skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009329}
9330
9331/*
9332 * "strdisplaywidth()" function
9333 */
9334 static void
9335f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
9336{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009337 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009338 int col = 0;
9339
9340 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009341 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009342
9343 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
9344}
9345
9346/*
9347 * "strwidth()" function
9348 */
9349 static void
9350f_strwidth(typval_T *argvars, typval_T *rettv)
9351{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009352 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009353
Bram Moolenaar13505972019-01-24 15:04:48 +01009354 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009355}
9356
9357/*
9358 * "strcharpart()" function
9359 */
9360 static void
9361f_strcharpart(typval_T *argvars, typval_T *rettv)
9362{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009363 char_u *p;
9364 int nchar;
9365 int nbyte = 0;
9366 int charlen;
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009367 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009368 int len = 0;
9369 int slen;
9370 int error = FALSE;
9371
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009372 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009373 slen = (int)STRLEN(p);
9374
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009375 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009376 if (!error)
9377 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009378 if (argvars[2].v_type != VAR_UNKNOWN
9379 && argvars[3].v_type != VAR_UNKNOWN)
9380 {
9381 skipcc = tv_get_bool(&argvars[3]);
9382 if (skipcc < 0 || skipcc > 1)
9383 {
9384 semsg(_(e_using_number_as_bool_nr), skipcc);
9385 return;
9386 }
9387 }
9388
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009389 if (nchar > 0)
9390 while (nchar > 0 && nbyte < slen)
9391 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009392 if (skipcc)
9393 nbyte += mb_ptr2len(p + nbyte);
9394 else
9395 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009396 --nchar;
9397 }
9398 else
9399 nbyte = nchar;
9400 if (argvars[2].v_type != VAR_UNKNOWN)
9401 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009402 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009403 while (charlen > 0 && nbyte + len < slen)
9404 {
9405 int off = nbyte + len;
9406
9407 if (off < 0)
9408 len += 1;
9409 else
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01009410 {
9411 if (skipcc)
9412 len += mb_ptr2len(p + off);
9413 else
9414 len += MB_CPTR2LEN(p + off);
9415 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009416 --charlen;
9417 }
9418 }
9419 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009420 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009421 }
9422
9423 /*
9424 * Only return the overlap between the specified part and the actual
9425 * string.
9426 */
9427 if (nbyte < 0)
9428 {
9429 len += nbyte;
9430 nbyte = 0;
9431 }
9432 else if (nbyte > slen)
9433 nbyte = slen;
9434 if (len < 0)
9435 len = 0;
9436 else if (nbyte + len > slen)
9437 len = slen - nbyte;
9438
9439 rettv->v_type = VAR_STRING;
9440 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009441}
9442
9443/*
9444 * "strpart()" function
9445 */
9446 static void
9447f_strpart(typval_T *argvars, typval_T *rettv)
9448{
9449 char_u *p;
9450 int n;
9451 int len;
9452 int slen;
9453 int error = FALSE;
9454
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009455 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009456 slen = (int)STRLEN(p);
9457
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009458 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009459 if (error)
9460 len = 0;
9461 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009462 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009463 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009464 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009465
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009466 // Only return the overlap between the specified part and the actual
9467 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009468 if (n < 0)
9469 {
9470 len += n;
9471 n = 0;
9472 }
9473 else if (n > slen)
9474 n = slen;
9475 if (len < 0)
9476 len = 0;
9477 else if (n + len > slen)
9478 len = slen - n;
9479
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009480 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9481 {
9482 int off;
9483
9484 // length in characters
9485 for (off = n; off < slen && len > 0; --len)
9486 off += mb_ptr2len(p + off);
9487 len = off - n;
9488 }
9489
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009490 rettv->v_type = VAR_STRING;
9491 rettv->vval.v_string = vim_strnsave(p + n, len);
9492}
9493
9494/*
9495 * "strridx()" function
9496 */
9497 static void
9498f_strridx(typval_T *argvars, typval_T *rettv)
9499{
9500 char_u buf[NUMBUFLEN];
9501 char_u *needle;
9502 char_u *haystack;
9503 char_u *rest;
9504 char_u *lastmatch = NULL;
9505 int haystack_len, end_idx;
9506
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009507 needle = tv_get_string_chk(&argvars[1]);
9508 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009509
9510 rettv->vval.v_number = -1;
9511 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009512 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009513
9514 haystack_len = (int)STRLEN(haystack);
9515 if (argvars[2].v_type != VAR_UNKNOWN)
9516 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009517 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009518 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009519 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009520 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009521 }
9522 else
9523 end_idx = haystack_len;
9524
9525 if (*needle == NUL)
9526 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009527 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009528 lastmatch = haystack + end_idx;
9529 }
9530 else
9531 {
9532 for (rest = haystack; *rest != '\0'; ++rest)
9533 {
9534 rest = (char_u *)strstr((char *)rest, (char *)needle);
9535 if (rest == NULL || rest > haystack + end_idx)
9536 break;
9537 lastmatch = rest;
9538 }
9539 }
9540
9541 if (lastmatch == NULL)
9542 rettv->vval.v_number = -1;
9543 else
9544 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9545}
9546
9547/*
9548 * "strtrans()" function
9549 */
9550 static void
9551f_strtrans(typval_T *argvars, typval_T *rettv)
9552{
9553 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009554 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009555}
9556
9557/*
9558 * "submatch()" function
9559 */
9560 static void
9561f_submatch(typval_T *argvars, typval_T *rettv)
9562{
9563 int error = FALSE;
9564 int no;
9565 int retList = 0;
9566
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009567 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009568 if (error)
9569 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009570 if (no < 0 || no >= NSUBEXP)
9571 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009572 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009573 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009574 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009575 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009576 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009577 if (error)
9578 return;
9579
9580 if (retList == 0)
9581 {
9582 rettv->v_type = VAR_STRING;
9583 rettv->vval.v_string = reg_submatch(no);
9584 }
9585 else
9586 {
9587 rettv->v_type = VAR_LIST;
9588 rettv->vval.v_list = reg_submatch_list(no);
9589 }
9590}
9591
9592/*
9593 * "substitute()" function
9594 */
9595 static void
9596f_substitute(typval_T *argvars, typval_T *rettv)
9597{
9598 char_u patbuf[NUMBUFLEN];
9599 char_u subbuf[NUMBUFLEN];
9600 char_u flagsbuf[NUMBUFLEN];
9601
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009602 char_u *str = tv_get_string_chk(&argvars[0]);
9603 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009604 char_u *sub = NULL;
9605 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009606 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009607
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009608 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9609 expr = &argvars[2];
9610 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009611 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009612
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009613 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009614 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9615 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009616 rettv->vval.v_string = NULL;
9617 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009618 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009619}
9620
9621/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009622 * "swapinfo(swap_filename)" function
9623 */
9624 static void
9625f_swapinfo(typval_T *argvars, typval_T *rettv)
9626{
9627 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009628 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009629}
9630
9631/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009632 * "swapname(expr)" function
9633 */
9634 static void
9635f_swapname(typval_T *argvars, typval_T *rettv)
9636{
9637 buf_T *buf;
9638
9639 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009640 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009641 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9642 || buf->b_ml.ml_mfp->mf_fname == NULL)
9643 rettv->vval.v_string = NULL;
9644 else
9645 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9646}
9647
9648/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009649 * "synID(lnum, col, trans)" function
9650 */
9651 static void
9652f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9653{
9654 int id = 0;
9655#ifdef FEAT_SYN_HL
9656 linenr_T lnum;
9657 colnr_T col;
9658 int trans;
9659 int transerr = FALSE;
9660
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009661 lnum = tv_get_lnum(argvars); // -1 on type error
9662 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009663 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009664
9665 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9666 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9667 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9668#endif
9669
9670 rettv->vval.v_number = id;
9671}
9672
9673/*
9674 * "synIDattr(id, what [, mode])" function
9675 */
9676 static void
9677f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9678{
9679 char_u *p = NULL;
9680#ifdef FEAT_SYN_HL
9681 int id;
9682 char_u *what;
9683 char_u *mode;
9684 char_u modebuf[NUMBUFLEN];
9685 int modec;
9686
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009687 id = (int)tv_get_number(&argvars[0]);
9688 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009689 if (argvars[2].v_type != VAR_UNKNOWN)
9690 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009691 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009692 modec = TOLOWER_ASC(mode[0]);
9693 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009694 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009695 }
9696 else
9697 {
9698#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9699 if (USE_24BIT)
9700 modec = 'g';
9701 else
9702#endif
9703 if (t_colors > 1)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02009704 modec = 'c';
9705 else
9706 modec = 't';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009707 }
9708
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009709 switch (TOLOWER_ASC(what[0]))
9710 {
9711 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009712 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009713 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009714 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009715 p = highlight_has_attr(id, HL_BOLD, modec);
9716 break;
9717
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009718 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009719 p = highlight_color(id, what, modec);
9720 break;
9721
9722 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009723 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009724 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009725 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009726 p = highlight_has_attr(id, HL_ITALIC, modec);
9727 break;
9728
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009729 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009730 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009731 break;
9732
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009733 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009734 p = highlight_has_attr(id, HL_INVERSE, modec);
9735 break;
9736
9737 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009738 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009739 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009740 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009741 else if (TOLOWER_ASC(what[1]) == 't' &&
9742 TOLOWER_ASC(what[2]) == 'r')
9743 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009744 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009745 p = highlight_has_attr(id, HL_STANDOUT, modec);
9746 break;
9747
9748 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009749 if (TOLOWER_ASC(what[1]) == 'l') // ul
9750 p = highlight_color(id, what, modec);
9751 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009752 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009753 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9754 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009755 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009756 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9757 break;
9758 }
9759
9760 if (p != NULL)
9761 p = vim_strsave(p);
9762#endif
9763 rettv->v_type = VAR_STRING;
9764 rettv->vval.v_string = p;
9765}
9766
9767/*
9768 * "synIDtrans(id)" function
9769 */
9770 static void
9771f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9772{
9773 int id;
9774
9775#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009776 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009777
9778 if (id > 0)
9779 id = syn_get_final_id(id);
9780 else
9781#endif
9782 id = 0;
9783
9784 rettv->vval.v_number = id;
9785}
9786
9787/*
9788 * "synconcealed(lnum, col)" function
9789 */
9790 static void
9791f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9792{
9793#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9794 linenr_T lnum;
9795 colnr_T col;
9796 int syntax_flags = 0;
9797 int cchar;
9798 int matchid = 0;
9799 char_u str[NUMBUFLEN];
9800#endif
9801
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009802 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009803
9804#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009805 lnum = tv_get_lnum(argvars); // -1 on type error
9806 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009807
Bram Moolenaara80faa82020-04-12 19:37:17 +02009808 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009809
9810 if (rettv_list_alloc(rettv) != FAIL)
9811 {
9812 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9813 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9814 && curwin->w_p_cole > 0)
9815 {
9816 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9817 syntax_flags = get_syntax_info(&matchid);
9818
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009819 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009820 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9821 {
9822 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009823 if (cchar == NUL && curwin->w_p_cole == 1)
Bram Moolenaareed9d462021-02-15 20:38:25 +01009824 cchar = (curwin->w_lcs_chars.conceal == NUL) ? ' '
9825 : curwin->w_lcs_chars.conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009826 if (cchar != NUL)
9827 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009828 if (has_mbyte)
9829 (*mb_char2bytes)(cchar, str);
9830 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009831 str[0] = cchar;
9832 }
9833 }
9834 }
9835
9836 list_append_number(rettv->vval.v_list,
9837 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009838 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009839 list_append_string(rettv->vval.v_list, str, -1);
9840 list_append_number(rettv->vval.v_list, matchid);
9841 }
9842#endif
9843}
9844
9845/*
9846 * "synstack(lnum, col)" function
9847 */
9848 static void
9849f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9850{
9851#ifdef FEAT_SYN_HL
9852 linenr_T lnum;
9853 colnr_T col;
9854 int i;
9855 int id;
9856#endif
9857
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009858 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009859
9860#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009861 lnum = tv_get_lnum(argvars); // -1 on type error
9862 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009863
9864 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9865 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9866 && rettv_list_alloc(rettv) != FAIL)
9867 {
9868 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9869 for (i = 0; ; ++i)
9870 {
9871 id = syn_get_stack_item(i);
9872 if (id < 0)
9873 break;
9874 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9875 break;
9876 }
9877 }
9878#endif
9879}
9880
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009881/*
9882 * "tabpagebuflist()" function
9883 */
9884 static void
9885f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9886{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009887 tabpage_T *tp;
9888 win_T *wp = NULL;
9889
9890 if (argvars[0].v_type == VAR_UNKNOWN)
9891 wp = firstwin;
9892 else
9893 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009894 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009895 if (tp != NULL)
9896 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9897 }
9898 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9899 {
9900 for (; wp != NULL; wp = wp->w_next)
9901 if (list_append_number(rettv->vval.v_list,
9902 wp->w_buffer->b_fnum) == FAIL)
9903 break;
9904 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009905}
9906
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009907/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009908 * "tagfiles()" function
9909 */
9910 static void
9911f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9912{
9913 char_u *fname;
9914 tagname_T tn;
9915 int first;
9916
9917 if (rettv_list_alloc(rettv) == FAIL)
9918 return;
9919 fname = alloc(MAXPATHL);
9920 if (fname == NULL)
9921 return;
9922
9923 for (first = TRUE; ; first = FALSE)
9924 if (get_tagfname(&tn, first, fname) == FAIL
9925 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9926 break;
9927 tagname_free(&tn);
9928 vim_free(fname);
9929}
9930
9931/*
9932 * "taglist()" function
9933 */
9934 static void
9935f_taglist(typval_T *argvars, typval_T *rettv)
9936{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009937 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009938 char_u *tag_pattern;
9939
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009940 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009941
9942 rettv->vval.v_number = FALSE;
9943 if (*tag_pattern == NUL)
9944 return;
9945
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009946 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009947 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009948 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009949 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009950}
9951
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009952#ifdef FEAT_FLOAT
9953/*
9954 * "tan()" function
9955 */
9956 static void
9957f_tan(typval_T *argvars, typval_T *rettv)
9958{
9959 float_T f = 0.0;
9960
9961 rettv->v_type = VAR_FLOAT;
9962 if (get_float_arg(argvars, &f) == OK)
9963 rettv->vval.v_float = tan(f);
9964 else
9965 rettv->vval.v_float = 0.0;
9966}
9967
9968/*
9969 * "tanh()" function
9970 */
9971 static void
9972f_tanh(typval_T *argvars, typval_T *rettv)
9973{
9974 float_T f = 0.0;
9975
9976 rettv->v_type = VAR_FLOAT;
9977 if (get_float_arg(argvars, &f) == OK)
9978 rettv->vval.v_float = tanh(f);
9979 else
9980 rettv->vval.v_float = 0.0;
9981}
9982#endif
9983
9984/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009985 * "tolower(string)" function
9986 */
9987 static void
9988f_tolower(typval_T *argvars, typval_T *rettv)
9989{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009990 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009991 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009992}
9993
9994/*
9995 * "toupper(string)" function
9996 */
9997 static void
9998f_toupper(typval_T *argvars, typval_T *rettv)
9999{
10000 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010001 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010002}
10003
10004/*
10005 * "tr(string, fromstr, tostr)" function
10006 */
10007 static void
10008f_tr(typval_T *argvars, typval_T *rettv)
10009{
10010 char_u *in_str;
10011 char_u *fromstr;
10012 char_u *tostr;
10013 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010014 int inlen;
10015 int fromlen;
10016 int tolen;
10017 int idx;
10018 char_u *cpstr;
10019 int cplen;
10020 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010021 char_u buf[NUMBUFLEN];
10022 char_u buf2[NUMBUFLEN];
10023 garray_T ga;
10024
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010025 in_str = tv_get_string(&argvars[0]);
10026 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
10027 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010028
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010029 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010030 rettv->v_type = VAR_STRING;
10031 rettv->vval.v_string = NULL;
10032 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010033 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010034 ga_init2(&ga, (int)sizeof(char), 80);
10035
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010036 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010037 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010038 if (STRLEN(fromstr) != STRLEN(tostr))
10039 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010040error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010041 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010042 ga_clear(&ga);
10043 return;
10044 }
10045
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010046 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010047 while (*in_str != NUL)
10048 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010049 if (has_mbyte)
10050 {
10051 inlen = (*mb_ptr2len)(in_str);
10052 cpstr = in_str;
10053 cplen = inlen;
10054 idx = 0;
10055 for (p = fromstr; *p != NUL; p += fromlen)
10056 {
10057 fromlen = (*mb_ptr2len)(p);
10058 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
10059 {
10060 for (p = tostr; *p != NUL; p += tolen)
10061 {
10062 tolen = (*mb_ptr2len)(p);
10063 if (idx-- == 0)
10064 {
10065 cplen = tolen;
10066 cpstr = p;
10067 break;
10068 }
10069 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010070 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010071 goto error;
10072 break;
10073 }
10074 ++idx;
10075 }
10076
10077 if (first && cpstr == in_str)
10078 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010079 // Check that fromstr and tostr have the same number of
10080 // (multi-byte) characters. Done only once when a character
10081 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010082 first = FALSE;
10083 for (p = tostr; *p != NUL; p += tolen)
10084 {
10085 tolen = (*mb_ptr2len)(p);
10086 --idx;
10087 }
10088 if (idx != 0)
10089 goto error;
10090 }
10091
10092 (void)ga_grow(&ga, cplen);
10093 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
10094 ga.ga_len += cplen;
10095
10096 in_str += inlen;
10097 }
10098 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010099 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010100 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010101 p = vim_strchr(fromstr, *in_str);
10102 if (p != NULL)
10103 ga_append(&ga, tostr[p - fromstr]);
10104 else
10105 ga_append(&ga, *in_str);
10106 ++in_str;
10107 }
10108 }
10109
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010110 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010111 (void)ga_grow(&ga, 1);
10112 ga_append(&ga, NUL);
10113
10114 rettv->vval.v_string = ga.ga_data;
10115}
10116
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010117/*
10118 * "trim({expr})" function
10119 */
10120 static void
10121f_trim(typval_T *argvars, typval_T *rettv)
10122{
10123 char_u buf1[NUMBUFLEN];
10124 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010125 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010126 char_u *mask = NULL;
10127 char_u *tail;
10128 char_u *prev;
10129 char_u *p;
10130 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010131 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010132
10133 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010134 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010135 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010136 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010137
10138 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010139 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010140 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010141
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010142 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010143 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010144 int error = 0;
10145
10146 // leading or trailing characters to trim
10147 dir = (int)tv_get_number_chk(&argvars[2], &error);
10148 if (error)
10149 return;
10150 if (dir < 0 || dir > 2)
10151 {
10152 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
10153 return;
10154 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010155 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010156 }
10157
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010158 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010159 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010160 // Trim leading characters
10161 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010162 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010163 c1 = PTR2CHAR(head);
10164 if (mask == NULL)
10165 {
10166 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010167 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010168 }
10169 else
10170 {
10171 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10172 if (c1 == PTR2CHAR(p))
10173 break;
10174 if (*p == NUL)
10175 break;
10176 }
10177 MB_PTR_ADV(head);
10178 }
10179 }
10180
10181 tail = head + STRLEN(head);
10182 if (dir == 0 || dir == 2)
10183 {
10184 // Trim trailing characters
10185 for (; tail > head; tail = prev)
10186 {
10187 prev = tail;
10188 MB_PTR_BACK(head, prev);
10189 c1 = PTR2CHAR(prev);
10190 if (mask == NULL)
10191 {
10192 if (c1 > ' ' && c1 != 0xa0)
10193 break;
10194 }
10195 else
10196 {
10197 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10198 if (c1 == PTR2CHAR(p))
10199 break;
10200 if (*p == NUL)
10201 break;
10202 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010203 }
10204 }
Bram Moolenaardf44a272020-06-07 20:49:05 +020010205 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010206}
10207
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010208#ifdef FEAT_FLOAT
10209/*
10210 * "trunc({float})" function
10211 */
10212 static void
10213f_trunc(typval_T *argvars, typval_T *rettv)
10214{
10215 float_T f = 0.0;
10216
10217 rettv->v_type = VAR_FLOAT;
10218 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010219 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010220 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
10221 else
10222 rettv->vval.v_float = 0.0;
10223}
10224#endif
10225
10226/*
10227 * "type(expr)" function
10228 */
10229 static void
10230f_type(typval_T *argvars, typval_T *rettv)
10231{
10232 int n = -1;
10233
10234 switch (argvars[0].v_type)
10235 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010236 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
10237 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010238 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010239 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
10240 case VAR_LIST: n = VAR_TYPE_LIST; break;
10241 case VAR_DICT: n = VAR_TYPE_DICT; break;
10242 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
10243 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
10244 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020010245 case VAR_JOB: n = VAR_TYPE_JOB; break;
10246 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010247 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +020010248 case VAR_INSTR: n = VAR_TYPE_INSTR; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010249 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +020010250 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010251 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +010010252 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010253 n = -1;
10254 break;
10255 }
10256 rettv->vval.v_number = n;
10257}
10258
10259/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010260 * "virtcol(string)" function
10261 */
10262 static void
10263f_virtcol(typval_T *argvars, typval_T *rettv)
10264{
10265 colnr_T vcol = 0;
10266 pos_T *fp;
10267 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010268 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010269
Bram Moolenaar6f02b002021-01-10 20:22:54 +010010270 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010271 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
10272 && fnum == curbuf->b_fnum)
10273 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010274 // Limit the column to a valid value, getvvcol() doesn't check.
10275 if (fp->col < 0)
10276 fp->col = 0;
10277 else
10278 {
10279 len = (int)STRLEN(ml_get(fp->lnum));
10280 if (fp->col > len)
10281 fp->col = len;
10282 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010283 getvvcol(curwin, fp, NULL, NULL, &vcol);
10284 ++vcol;
10285 }
10286
10287 rettv->vval.v_number = vcol;
10288}
10289
10290/*
10291 * "visualmode()" function
10292 */
10293 static void
10294f_visualmode(typval_T *argvars, typval_T *rettv)
10295{
10296 char_u str[2];
10297
10298 rettv->v_type = VAR_STRING;
10299 str[0] = curbuf->b_visual_mode_eval;
10300 str[1] = NUL;
10301 rettv->vval.v_string = vim_strsave(str);
10302
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010303 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010304 if (non_zero_arg(&argvars[0]))
10305 curbuf->b_visual_mode_eval = NUL;
10306}
10307
10308/*
10309 * "wildmenumode()" function
10310 */
10311 static void
10312f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10313{
10314#ifdef FEAT_WILDMENU
10315 if (wild_menu_showing)
10316 rettv->vval.v_number = 1;
10317#endif
10318}
10319
10320/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +010010321 * "windowsversion()" function
10322 */
10323 static void
10324f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10325{
10326 rettv->v_type = VAR_STRING;
10327 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
10328}
10329
10330/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010331 * "wordcount()" function
10332 */
10333 static void
10334f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
10335{
10336 if (rettv_dict_alloc(rettv) == FAIL)
10337 return;
10338 cursor_pos_info(rettv->vval.v_dict);
10339}
10340
10341/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010342 * "xor(expr, expr)" function
10343 */
10344 static void
10345f_xor(typval_T *argvars, typval_T *rettv)
10346{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010347 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
10348 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010349}
10350
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010351#endif // FEAT_EVAL