blob: 61cca01f8a5c951885b6bde9f45de63a43816515 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020023#ifdef FEAT_FLOAT
24static void f_abs(typval_T *argvars, typval_T *rettv);
25static void f_acos(typval_T *argvars, typval_T *rettv);
26#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020027static void f_and(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028#ifdef FEAT_FLOAT
29static void f_asin(typval_T *argvars, typval_T *rettv);
30static void f_atan(typval_T *argvars, typval_T *rettv);
31static void f_atan2(typval_T *argvars, typval_T *rettv);
32#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010033#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020034static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010035static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010036# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010037static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010038# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010039#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_byte2line(typval_T *argvars, typval_T *rettv);
41static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
42static void f_byteidx(typval_T *argvars, typval_T *rettv);
43static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
44static void f_call(typval_T *argvars, typval_T *rettv);
45#ifdef FEAT_FLOAT
46static void f_ceil(typval_T *argvars, typval_T *rettv);
47#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_changenr(typval_T *argvars, typval_T *rettv);
49static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010050static void f_charcol(typval_T *argvars, typval_T *rettv);
Bram Moolenaar17793ef2020-12-28 12:56:58 +010051static void f_charidx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020053static void f_confirm(typval_T *argvars, typval_T *rettv);
54static void f_copy(typval_T *argvars, typval_T *rettv);
55#ifdef FEAT_FLOAT
56static void f_cos(typval_T *argvars, typval_T *rettv);
57static void f_cosh(typval_T *argvars, typval_T *rettv);
58#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010060#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020061static void f_debugbreak(typval_T *argvars, typval_T *rettv);
62#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020063static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020064static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010065static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020066static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020067static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_escape(typval_T *argvars, typval_T *rettv);
69static void f_eval(typval_T *argvars, typval_T *rettv);
70static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_exists(typval_T *argvars, typval_T *rettv);
73#ifdef FEAT_FLOAT
74static void f_exp(typval_T *argvars, typval_T *rettv);
75#endif
76static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020077static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020078static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020079#ifdef FEAT_FLOAT
80static void f_float2nr(typval_T *argvars, typval_T *rettv);
81static void f_floor(typval_T *argvars, typval_T *rettv);
82static void f_fmod(typval_T *argvars, typval_T *rettv);
83#endif
84static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020085static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020086static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_function(typval_T *argvars, typval_T *rettv);
88static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
89static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010090static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010091static void f_getcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020092static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020093static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020094static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010095static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_getpid(typval_T *argvars, typval_T *rettv);
97static void f_getcurpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010098static void f_getcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020099static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200100static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200101static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100103static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +0200104static void f_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200105static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
106static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200107static void f_hlID(typval_T *argvars, typval_T *rettv);
108static void f_hlexists(typval_T *argvars, typval_T *rettv);
109static void f_hostname(typval_T *argvars, typval_T *rettv);
110static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200111static void f_index(typval_T *argvars, typval_T *rettv);
112static void f_input(typval_T *argvars, typval_T *rettv);
113static void f_inputdialog(typval_T *argvars, typval_T *rettv);
114static void f_inputlist(typval_T *argvars, typval_T *rettv);
115static void f_inputrestore(typval_T *argvars, typval_T *rettv);
116static void f_inputsave(typval_T *argvars, typval_T *rettv);
117static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +0100118static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200119static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120static void f_islocked(typval_T *argvars, typval_T *rettv);
121#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200122static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200123static void f_isnan(typval_T *argvars, typval_T *rettv);
124#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200125static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
126static void f_len(typval_T *argvars, typval_T *rettv);
127static void f_libcall(typval_T *argvars, typval_T *rettv);
128static void f_libcallnr(typval_T *argvars, typval_T *rettv);
129static void f_line(typval_T *argvars, typval_T *rettv);
130static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200131#ifdef FEAT_FLOAT
132static void f_log(typval_T *argvars, typval_T *rettv);
133static void f_log10(typval_T *argvars, typval_T *rettv);
134#endif
135#ifdef FEAT_LUA
136static void f_luaeval(typval_T *argvars, typval_T *rettv);
137#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200138static void f_maparg(typval_T *argvars, typval_T *rettv);
139static void f_mapcheck(typval_T *argvars, typval_T *rettv);
140static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200141static void f_matchend(typval_T *argvars, typval_T *rettv);
142static void f_matchlist(typval_T *argvars, typval_T *rettv);
143static void f_matchstr(typval_T *argvars, typval_T *rettv);
144static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
145static void f_max(typval_T *argvars, typval_T *rettv);
146static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200147#ifdef FEAT_MZSCHEME
148static void f_mzeval(typval_T *argvars, typval_T *rettv);
149#endif
150static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
151static void f_nr2char(typval_T *argvars, typval_T *rettv);
152static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200153#ifdef FEAT_PERL
154static void f_perleval(typval_T *argvars, typval_T *rettv);
155#endif
156#ifdef FEAT_FLOAT
157static void f_pow(typval_T *argvars, typval_T *rettv);
158#endif
159static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
160static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200161static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200162static void f_pumvisible(typval_T *argvars, typval_T *rettv);
163#ifdef FEAT_PYTHON3
164static void f_py3eval(typval_T *argvars, typval_T *rettv);
165#endif
166#ifdef FEAT_PYTHON
167static void f_pyeval(typval_T *argvars, typval_T *rettv);
168#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100169#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
170static void f_pyxeval(typval_T *argvars, typval_T *rettv);
171#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100172static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100173static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200175static void f_reg_executing(typval_T *argvars, typval_T *rettv);
176static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_rename(typval_T *argvars, typval_T *rettv);
178static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200179#ifdef FEAT_FLOAT
180static void f_round(typval_T *argvars, typval_T *rettv);
181#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100182#ifdef FEAT_RUBY
183static void f_rubyeval(typval_T *argvars, typval_T *rettv);
184#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200185static void f_screenattr(typval_T *argvars, typval_T *rettv);
186static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100187static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200188static void f_screencol(typval_T *argvars, typval_T *rettv);
189static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100190static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200191static void f_search(typval_T *argvars, typval_T *rettv);
192static void f_searchdecl(typval_T *argvars, typval_T *rettv);
193static void f_searchpair(typval_T *argvars, typval_T *rettv);
194static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
195static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100196static void f_setcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100198static void f_setcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200199static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100203static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200204#ifdef FEAT_CRYPT
205static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200206#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200207static void f_shellescape(typval_T *argvars, typval_T *rettv);
208static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200209#ifdef FEAT_FLOAT
210static void f_sin(typval_T *argvars, typval_T *rettv);
211static void f_sinh(typval_T *argvars, typval_T *rettv);
212#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200213static void f_soundfold(typval_T *argvars, typval_T *rettv);
214static void f_spellbadword(typval_T *argvars, typval_T *rettv);
215static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
216static void f_split(typval_T *argvars, typval_T *rettv);
217#ifdef FEAT_FLOAT
218static void f_sqrt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100219#endif
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100220static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100221#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200222static void f_str2float(typval_T *argvars, typval_T *rettv);
223#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200224static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200225static void f_str2nr(typval_T *argvars, typval_T *rettv);
226static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200227static void f_strgetchar(typval_T *argvars, typval_T *rettv);
228static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200229static void f_strlen(typval_T *argvars, typval_T *rettv);
230static void f_strcharpart(typval_T *argvars, typval_T *rettv);
231static void f_strpart(typval_T *argvars, typval_T *rettv);
232static void f_strridx(typval_T *argvars, typval_T *rettv);
233static void f_strtrans(typval_T *argvars, typval_T *rettv);
234static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
235static void f_strwidth(typval_T *argvars, typval_T *rettv);
236static void f_submatch(typval_T *argvars, typval_T *rettv);
237static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200238static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200239static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200240static void f_synID(typval_T *argvars, typval_T *rettv);
241static void f_synIDattr(typval_T *argvars, typval_T *rettv);
242static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
243static void f_synstack(typval_T *argvars, typval_T *rettv);
244static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200246static void f_taglist(typval_T *argvars, typval_T *rettv);
247static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200248#ifdef FEAT_FLOAT
249static void f_tan(typval_T *argvars, typval_T *rettv);
250static void f_tanh(typval_T *argvars, typval_T *rettv);
251#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200252static void f_tolower(typval_T *argvars, typval_T *rettv);
253static void f_toupper(typval_T *argvars, typval_T *rettv);
254static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100255static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200256#ifdef FEAT_FLOAT
257static void f_trunc(typval_T *argvars, typval_T *rettv);
258#endif
259static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200260static void f_virtcol(typval_T *argvars, typval_T *rettv);
261static void f_visualmode(typval_T *argvars, typval_T *rettv);
262static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100263static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200264static void f_wordcount(typval_T *argvars, typval_T *rettv);
265static void f_xor(typval_T *argvars, typval_T *rettv);
266
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100267
Bram Moolenaar94738d82020-10-21 14:25:07 +0200268/*
269 * Functions that check the argument type of a builtin function.
270 * Each function returns FAIL and gives an error message if the type is wrong.
271 */
272
273// Context passed to an arg_ function.
274typedef struct {
Bram Moolenaarca174532020-10-21 16:42:22 +0200275 int arg_count; // actual argument count
276 type_T **arg_types; // list of argument types
277 int arg_idx; // current argument index (first arg is zero)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100278 cctx_T *arg_cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +0200279} argcontext_T;
280
281// A function to check one argument type. The first argument is the type to
282// check. If needed, other argument types can be obtained with the context.
283// E.g. if "arg_idx" is 1, then (type - 1) is the first argument type.
284typedef int (*argcheck_T)(type_T *, argcontext_T *);
285
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100286/*
Bram Moolenaar351ead02021-01-16 16:07:01 +0100287 * Call need_type() to check an argument type.
288 */
289 static int
290check_arg_type(
291 type_T *expected,
292 type_T *actual,
293 argcontext_T *context)
294{
295 // TODO: would be useful to know if "actual" is a constant and pass it to
296 // need_type() to get a compile time error if possible.
297 return need_type(actual, expected,
298 context->arg_idx - context->arg_count, context->arg_idx + 1,
299 context->arg_cctx, FALSE, FALSE);
300}
301
302/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100303 * Check "type" is a float or a number.
304 */
Bram Moolenaar94738d82020-10-21 14:25:07 +0200305 static int
306arg_float_or_nr(type_T *type, argcontext_T *context)
307{
Bram Moolenaarca174532020-10-21 16:42:22 +0200308 if (type->tt_type == VAR_ANY
309 || type->tt_type == VAR_FLOAT || type->tt_type == VAR_NUMBER)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200310 return OK;
311 arg_type_mismatch(&t_number, type, context->arg_idx + 1);
312 return FAIL;
313}
314
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100315/*
316 * Check "type" is a number.
317 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200318 static int
319arg_number(type_T *type, argcontext_T *context)
320{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100321 return check_arg_type(&t_number, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200322}
323
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100324/*
325 * Check "type" is a string.
326 */
327 static int
328arg_string(type_T *type, argcontext_T *context)
329{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100330 return check_arg_type(&t_string, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100331}
332
333/*
334 * Check "type" is a list or a blob.
335 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200336 static int
337arg_list_or_blob(type_T *type, argcontext_T *context)
338{
339 if (type->tt_type == VAR_ANY
340 || type->tt_type == VAR_LIST || type->tt_type == VAR_BLOB)
341 return OK;
342 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
343 return FAIL;
344}
345
346/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100347 * Check "type" is a list or a dict.
348 */
349 static int
350arg_list_or_dict(type_T *type, argcontext_T *context)
351{
352 if (type->tt_type == VAR_ANY
353 || type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
354 return OK;
355 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
356 return FAIL;
357}
358
359/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100360 * Check "type" is the same type as the previous argument.
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100361 * Must not be used for the first argcheck_T entry.
362 */
363 static int
364arg_same_as_prev(type_T *type, argcontext_T *context)
365{
366 type_T *prev_type = context->arg_types[context->arg_idx - 1];
367
Bram Moolenaar351ead02021-01-16 16:07:01 +0100368 return check_arg_type(prev_type, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100369}
370
371/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100372 * Check "type" is the same basic type as the previous argument, checks list or
373 * dict vs other type, but not member type.
374 * Must not be used for the first argcheck_T entry.
375 */
376 static int
377arg_same_struct_as_prev(type_T *type, argcontext_T *context)
378{
379 type_T *prev_type = context->arg_types[context->arg_idx - 1];
380
381 if (prev_type->tt_type != context->arg_types[context->arg_idx]->tt_type)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100382 return check_arg_type(prev_type, type, context);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100383 return OK;
384}
385
386/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100387 * Check "type" is an item of the list or blob of the previous arg.
Bram Moolenaarca174532020-10-21 16:42:22 +0200388 * Must not be used for the first argcheck_T entry.
389 */
390 static int
391arg_item_of_prev(type_T *type, argcontext_T *context)
392{
393 type_T *prev_type = context->arg_types[context->arg_idx - 1];
394 type_T *expected;
395
396 if (prev_type->tt_type == VAR_LIST)
397 expected = prev_type->tt_member;
398 else if (prev_type->tt_type == VAR_BLOB)
399 expected = &t_number;
400 else
401 // probably VAR_ANY, can't check
402 return OK;
403
Bram Moolenaar351ead02021-01-16 16:07:01 +0100404 return check_arg_type(expected, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200405}
406
Bram Moolenaar94738d82020-10-21 14:25:07 +0200407/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100408 * Check "type" which is the third argument of extend().
409 */
410 static int
411arg_extend3(type_T *type, argcontext_T *context)
412{
413 type_T *first_type = context->arg_types[context->arg_idx - 2];
414
415 if (first_type->tt_type == VAR_LIST)
416 return arg_number(type, context);
417 if (first_type->tt_type == VAR_DICT)
418 return arg_string(type, context);
419 return OK;
420}
421
422
423/*
Bram Moolenaar94738d82020-10-21 14:25:07 +0200424 * Lists of functions that check the argument types of a builtin function.
425 */
426argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100427argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev};
428argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3};
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100429argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
Bram Moolenaarca174532020-10-21 16:42:22 +0200430argcheck_T arg3_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200431
432/*
433 * Functions that return the return type of a builtin function.
Bram Moolenaara1224cb2020-10-22 12:31:49 +0200434 * Note that "argtypes" is NULL if "argcount" is zero.
Bram Moolenaar94738d82020-10-21 14:25:07 +0200435 */
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100436 static type_T *
437ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
438{
439 return &t_void;
440}
441 static type_T *
442ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
443{
444 return &t_any;
445}
446 static type_T *
Bram Moolenaar403dc312020-10-17 19:29:51 +0200447ret_bool(int argcount UNUSED, type_T **argtypes UNUSED)
448{
449 return &t_bool;
450}
451 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100452ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
453{
454 return &t_number;
455}
456 static type_T *
457ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
458{
459 return &t_float;
460}
461 static type_T *
462ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
463{
464 return &t_string;
465}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200466 static type_T *
467ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100468{
469 return &t_list_any;
470}
471 static type_T *
472ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
473{
474 return &t_list_number;
475}
476 static type_T *
477ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
478{
479 return &t_list_string;
480}
481 static type_T *
482ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
483{
484 return &t_list_dict_any;
485}
486 static type_T *
487ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
488{
489 return &t_dict_any;
490}
491 static type_T *
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100492ret_job_info(int argcount, type_T **argtypes UNUSED)
493{
494 if (argcount == 0)
495 return &t_list_job;
496 return &t_dict_any;
497}
498 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100499ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
500{
501 return &t_dict_number;
502}
503 static type_T *
504ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
505{
506 return &t_dict_string;
507}
508 static type_T *
509ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
510{
511 return &t_blob;
512}
513 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200514ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100515{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200516 return &t_func_any;
517}
518 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100519ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
520{
521 return &t_channel;
522}
523 static type_T *
524ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
525{
526 return &t_job;
527}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200528 static type_T *
529ret_first_arg(int argcount, type_T **argtypes)
530{
531 if (argcount > 0)
532 return argtypes[0];
533 return &t_void;
534}
Bram Moolenaarea696852020-11-09 18:31:39 +0100535// for map(): returns first argument but item type may differ
536 static type_T *
537ret_first_cont(int argcount UNUSED, type_T **argtypes)
538{
539 if (argtypes[0]->tt_type == VAR_LIST)
540 return &t_list_any;
541 if (argtypes[0]->tt_type == VAR_DICT)
542 return &t_dict_any;
543 if (argtypes[0]->tt_type == VAR_BLOB)
544 return argtypes[0];
545 return &t_any;
546}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200547
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200548/*
549 * Used for getqflist(): returns list if there is no argument, dict if there is
550 * one.
551 */
552 static type_T *
553ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
554{
555 if (argcount > 0)
556 return &t_dict_any;
557 return &t_list_dict_any;
558}
559
560/*
561 * Used for getloclist(): returns list if there is one argument, dict if there
562 * are two.
563 */
564 static type_T *
565ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
566{
567 if (argcount > 1)
568 return &t_dict_any;
569 return &t_list_dict_any;
570}
571
Bram Moolenaar846178a2020-07-05 17:04:13 +0200572 static type_T *
573ret_argv(int argcount, type_T **argtypes UNUSED)
574{
575 // argv() returns list of strings
576 if (argcount == 0)
577 return &t_list_string;
578
579 // argv(0) returns a string, but argv(-1] returns a list
580 return &t_any;
581}
582
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200583 static type_T *
584ret_remove(int argcount UNUSED, type_T **argtypes)
585{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200586 if (argtypes != NULL)
587 {
588 if (argtypes[0]->tt_type == VAR_LIST
589 || argtypes[0]->tt_type == VAR_DICT)
590 return argtypes[0]->tt_member;
591 if (argtypes[0]->tt_type == VAR_BLOB)
592 return &t_number;
593 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200594 return &t_any;
595}
596
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200597 static type_T *
598ret_getreg(int argcount, type_T **argtypes UNUSED)
599{
600 // Assume that if the third argument is passed it's non-zero
601 if (argcount == 3)
602 return &t_list_string;
603 return &t_string;
604}
605
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200606 static type_T *
607ret_maparg(int argcount, type_T **argtypes UNUSED)
608{
609 // Assume that if the fourth argument is passed it's non-zero
610 if (argcount == 4)
611 return &t_dict_any;
612 return &t_string;
613}
614
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100615static type_T *ret_f_function(int argcount, type_T **argtypes);
616
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200617/*
618 * Array with names and number of arguments of all internal functions
619 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
620 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200621typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200622{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200623 char *f_name; // function name
624 char f_min_argc; // minimal number of arguments
625 char f_max_argc; // maximal number of arguments
626 char f_argtype; // for method: FEARG_ values
Bram Moolenaar94738d82020-10-21 14:25:07 +0200627 argcheck_T *f_argcheck; // list of functions to check argument types
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100628 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
629 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200630 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200631 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200632} funcentry_T;
633
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200634// values for f_argtype; zero means it cannot be used as a method
635#define FEARG_1 1 // base is the first argument
636#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200637#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200638#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200639#define FEARG_LAST 9 // base is the last argument
640
Bram Moolenaar15c47602020-03-26 22:16:48 +0100641#ifdef FEAT_FLOAT
642# define FLOAT_FUNC(name) name
643#else
644# define FLOAT_FUNC(name) NULL
645#endif
646#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
647# define MATH_FUNC(name) name
648#else
649# define MATH_FUNC(name) NULL
650#endif
651#ifdef FEAT_TIMERS
652# define TIMER_FUNC(name) name
653#else
654# define TIMER_FUNC(name) NULL
655#endif
656#ifdef FEAT_JOB_CHANNEL
657# define JOB_FUNC(name) name
658#else
659# define JOB_FUNC(name) NULL
660#endif
661#ifdef FEAT_PROP_POPUP
662# define PROP_FUNC(name) name
663#else
664# define PROP_FUNC(name) NULL
665#endif
666#ifdef FEAT_SIGNS
667# define SIGN_FUNC(name) name
668#else
669# define SIGN_FUNC(name) NULL
670#endif
671#ifdef FEAT_SOUND
672# define SOUND_FUNC(name) name
673#else
674# define SOUND_FUNC(name) NULL
675#endif
676#ifdef FEAT_TERMINAL
677# define TERM_FUNC(name) name
678#else
679# define TERM_FUNC(name) NULL
680#endif
681
Bram Moolenaarac92e252019-08-03 21:58:38 +0200682static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200683{
Bram Moolenaar94738d82020-10-21 14:25:07 +0200684 {"abs", 1, 1, FEARG_1, arg1_float_or_nr,
685 ret_any, FLOAT_FUNC(f_abs)},
686 {"acos", 1, 1, FEARG_1, NULL,
687 ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100688 {"add", 2, 2, FEARG_1, NULL /* arg2_listblob_item */,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200689 ret_first_arg, f_add},
690 {"and", 2, 2, FEARG_1, NULL,
691 ret_number, f_and},
692 {"append", 2, 2, FEARG_2, NULL,
693 ret_number, f_append},
694 {"appendbufline", 3, 3, FEARG_3, NULL,
695 ret_number, f_appendbufline},
696 {"argc", 0, 1, 0, NULL,
697 ret_number, f_argc},
698 {"argidx", 0, 0, 0, NULL,
699 ret_number, f_argidx},
700 {"arglistid", 0, 2, 0, NULL,
701 ret_number, f_arglistid},
702 {"argv", 0, 2, 0, NULL,
703 ret_argv, f_argv},
704 {"asin", 1, 1, FEARG_1, NULL,
705 ret_float, FLOAT_FUNC(f_asin)},
706 {"assert_beeps", 1, 2, FEARG_1, NULL,
707 ret_number, f_assert_beeps},
708 {"assert_equal", 2, 3, FEARG_2, NULL,
709 ret_number, f_assert_equal},
710 {"assert_equalfile", 2, 3, FEARG_1, NULL,
711 ret_number, f_assert_equalfile},
712 {"assert_exception", 1, 2, 0, NULL,
713 ret_number, f_assert_exception},
714 {"assert_fails", 1, 5, FEARG_1, NULL,
715 ret_number, f_assert_fails},
716 {"assert_false", 1, 2, FEARG_1, NULL,
717 ret_number, f_assert_false},
718 {"assert_inrange", 3, 4, FEARG_3, NULL,
719 ret_number, f_assert_inrange},
720 {"assert_match", 2, 3, FEARG_2, NULL,
721 ret_number, f_assert_match},
722 {"assert_notequal", 2, 3, FEARG_2, NULL,
723 ret_number, f_assert_notequal},
724 {"assert_notmatch", 2, 3, FEARG_2, NULL,
725 ret_number, f_assert_notmatch},
726 {"assert_report", 1, 1, FEARG_1, NULL,
727 ret_number, f_assert_report},
728 {"assert_true", 1, 2, FEARG_1, NULL,
729 ret_number, f_assert_true},
730 {"atan", 1, 1, FEARG_1, NULL,
731 ret_float, FLOAT_FUNC(f_atan)},
732 {"atan2", 2, 2, FEARG_1, NULL,
733 ret_float, FLOAT_FUNC(f_atan2)},
734 {"balloon_gettext", 0, 0, 0, NULL,
735 ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100736#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100737 f_balloon_gettext
738#else
739 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100740#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100741 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200742 {"balloon_show", 1, 1, FEARG_1, NULL,
743 ret_void,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100744#ifdef FEAT_BEVAL
745 f_balloon_show
746#else
747 NULL
748#endif
749 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200750 {"balloon_split", 1, 1, FEARG_1, NULL,
751 ret_list_string,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100752#if defined(FEAT_BEVAL_TERM)
753 f_balloon_split
754#else
755 NULL
756#endif
757 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200758 {"browse", 4, 4, 0, NULL,
759 ret_string, f_browse},
760 {"browsedir", 2, 2, 0, NULL,
761 ret_string, f_browsedir},
762 {"bufadd", 1, 1, FEARG_1, NULL,
763 ret_number, f_bufadd},
764 {"bufexists", 1, 1, FEARG_1, NULL,
765 ret_number, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200766 {"buffer_exists", 1, 1, FEARG_1, NULL, // obsolete
767 ret_number, f_bufexists},
768 {"buffer_name", 0, 1, FEARG_1, NULL, // obsolete
769 ret_string, f_bufname},
770 {"buffer_number", 0, 1, FEARG_1, NULL, // obsolete
771 ret_number, f_bufnr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200772 {"buflisted", 1, 1, FEARG_1, NULL,
773 ret_number, f_buflisted},
774 {"bufload", 1, 1, FEARG_1, NULL,
775 ret_void, f_bufload},
776 {"bufloaded", 1, 1, FEARG_1, NULL,
777 ret_number, f_bufloaded},
778 {"bufname", 0, 1, FEARG_1, NULL,
779 ret_string, f_bufname},
780 {"bufnr", 0, 2, FEARG_1, NULL,
781 ret_number, f_bufnr},
782 {"bufwinid", 1, 1, FEARG_1, NULL,
783 ret_number, f_bufwinid},
784 {"bufwinnr", 1, 1, FEARG_1, NULL,
785 ret_number, f_bufwinnr},
786 {"byte2line", 1, 1, FEARG_1, NULL,
787 ret_number, f_byte2line},
788 {"byteidx", 2, 2, FEARG_1, NULL,
789 ret_number, f_byteidx},
790 {"byteidxcomp", 2, 2, FEARG_1, NULL,
791 ret_number, f_byteidxcomp},
792 {"call", 2, 3, FEARG_1, NULL,
793 ret_any, f_call},
794 {"ceil", 1, 1, FEARG_1, NULL,
795 ret_float, FLOAT_FUNC(f_ceil)},
796 {"ch_canread", 1, 1, FEARG_1, NULL,
797 ret_number, JOB_FUNC(f_ch_canread)},
798 {"ch_close", 1, 1, FEARG_1, NULL,
799 ret_void, JOB_FUNC(f_ch_close)},
800 {"ch_close_in", 1, 1, FEARG_1, NULL,
801 ret_void, JOB_FUNC(f_ch_close_in)},
802 {"ch_evalexpr", 2, 3, FEARG_1, NULL,
803 ret_any, JOB_FUNC(f_ch_evalexpr)},
804 {"ch_evalraw", 2, 3, FEARG_1, NULL,
805 ret_any, JOB_FUNC(f_ch_evalraw)},
806 {"ch_getbufnr", 2, 2, FEARG_1, NULL,
807 ret_number, JOB_FUNC(f_ch_getbufnr)},
808 {"ch_getjob", 1, 1, FEARG_1, NULL,
809 ret_job, JOB_FUNC(f_ch_getjob)},
810 {"ch_info", 1, 1, FEARG_1, NULL,
811 ret_dict_any, JOB_FUNC(f_ch_info)},
812 {"ch_log", 1, 2, FEARG_1, NULL,
813 ret_void, JOB_FUNC(f_ch_log)},
814 {"ch_logfile", 1, 2, FEARG_1, NULL,
815 ret_void, JOB_FUNC(f_ch_logfile)},
816 {"ch_open", 1, 2, FEARG_1, NULL,
817 ret_channel, JOB_FUNC(f_ch_open)},
818 {"ch_read", 1, 2, FEARG_1, NULL,
819 ret_string, JOB_FUNC(f_ch_read)},
820 {"ch_readblob", 1, 2, FEARG_1, NULL,
821 ret_blob, JOB_FUNC(f_ch_readblob)},
822 {"ch_readraw", 1, 2, FEARG_1, NULL,
823 ret_string, JOB_FUNC(f_ch_readraw)},
824 {"ch_sendexpr", 2, 3, FEARG_1, NULL,
825 ret_void, JOB_FUNC(f_ch_sendexpr)},
826 {"ch_sendraw", 2, 3, FEARG_1, NULL,
827 ret_void, JOB_FUNC(f_ch_sendraw)},
828 {"ch_setoptions", 2, 2, FEARG_1, NULL,
829 ret_void, JOB_FUNC(f_ch_setoptions)},
830 {"ch_status", 1, 2, FEARG_1, NULL,
831 ret_string, JOB_FUNC(f_ch_status)},
832 {"changenr", 0, 0, 0, NULL,
833 ret_number, f_changenr},
834 {"char2nr", 1, 2, FEARG_1, NULL,
835 ret_number, f_char2nr},
836 {"charclass", 1, 1, FEARG_1, NULL,
837 ret_number, f_charclass},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100838 {"charcol", 1, 1, FEARG_1, NULL,
839 ret_number, f_charcol},
Bram Moolenaar17793ef2020-12-28 12:56:58 +0100840 {"charidx", 2, 3, FEARG_1, NULL,
841 ret_number, f_charidx},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200842 {"chdir", 1, 1, FEARG_1, NULL,
843 ret_string, f_chdir},
844 {"cindent", 1, 1, FEARG_1, NULL,
845 ret_number, f_cindent},
846 {"clearmatches", 0, 1, FEARG_1, NULL,
847 ret_void, f_clearmatches},
848 {"col", 1, 1, FEARG_1, NULL,
849 ret_number, f_col},
850 {"complete", 2, 2, FEARG_2, NULL,
851 ret_void, f_complete},
852 {"complete_add", 1, 1, FEARG_1, NULL,
853 ret_number, f_complete_add},
854 {"complete_check", 0, 0, 0, NULL,
855 ret_number, f_complete_check},
856 {"complete_info", 0, 1, FEARG_1, NULL,
857 ret_dict_any, f_complete_info},
858 {"confirm", 1, 4, FEARG_1, NULL,
859 ret_number, f_confirm},
860 {"copy", 1, 1, FEARG_1, NULL,
861 ret_first_arg, f_copy},
862 {"cos", 1, 1, FEARG_1, NULL,
863 ret_float, FLOAT_FUNC(f_cos)},
864 {"cosh", 1, 1, FEARG_1, NULL,
865 ret_float, FLOAT_FUNC(f_cosh)},
866 {"count", 2, 4, FEARG_1, NULL,
867 ret_number, f_count},
868 {"cscope_connection",0,3, 0, NULL,
869 ret_number, f_cscope_connection},
870 {"cursor", 1, 3, FEARG_1, NULL,
871 ret_number, f_cursor},
872 {"debugbreak", 1, 1, FEARG_1, NULL,
873 ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100874#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100875 f_debugbreak
876#else
877 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200878#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100879 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200880 {"deepcopy", 1, 2, FEARG_1, NULL,
881 ret_first_arg, f_deepcopy},
882 {"delete", 1, 2, FEARG_1, NULL,
883 ret_number, f_delete},
884 {"deletebufline", 2, 3, FEARG_1, NULL,
885 ret_number, f_deletebufline},
886 {"did_filetype", 0, 0, 0, NULL,
887 ret_number, f_did_filetype},
888 {"diff_filler", 1, 1, FEARG_1, NULL,
889 ret_number, f_diff_filler},
890 {"diff_hlID", 2, 2, FEARG_1, NULL,
891 ret_number, f_diff_hlID},
892 {"echoraw", 1, 1, FEARG_1, NULL,
893 ret_number, f_echoraw},
894 {"empty", 1, 1, FEARG_1, NULL,
895 ret_number, f_empty},
896 {"environ", 0, 0, 0, NULL,
897 ret_dict_string, f_environ},
898 {"escape", 2, 2, FEARG_1, NULL,
899 ret_string, f_escape},
900 {"eval", 1, 1, FEARG_1, NULL,
901 ret_any, f_eval},
902 {"eventhandler", 0, 0, 0, NULL,
903 ret_number, f_eventhandler},
904 {"executable", 1, 1, FEARG_1, NULL,
905 ret_number, f_executable},
906 {"execute", 1, 2, FEARG_1, NULL,
907 ret_string, f_execute},
908 {"exepath", 1, 1, FEARG_1, NULL,
909 ret_string, f_exepath},
910 {"exists", 1, 1, FEARG_1, NULL,
911 ret_number, f_exists},
912 {"exp", 1, 1, FEARG_1, NULL,
913 ret_float, FLOAT_FUNC(f_exp)},
914 {"expand", 1, 3, FEARG_1, NULL,
915 ret_any, f_expand},
916 {"expandcmd", 1, 1, FEARG_1, NULL,
917 ret_string, f_expandcmd},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100918 {"extend", 2, 3, FEARG_1, arg23_extend,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200919 ret_first_arg, f_extend},
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100920 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
921 ret_first_cont, f_extendnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200922 {"feedkeys", 1, 2, FEARG_1, NULL,
923 ret_void, f_feedkeys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200924 {"file_readable", 1, 1, FEARG_1, NULL, // obsolete
925 ret_number, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200926 {"filereadable", 1, 1, FEARG_1, NULL,
927 ret_number, f_filereadable},
928 {"filewritable", 1, 1, FEARG_1, NULL,
929 ret_number, f_filewritable},
930 {"filter", 2, 2, FEARG_1, NULL,
931 ret_first_arg, f_filter},
932 {"finddir", 1, 3, FEARG_1, NULL,
933 ret_string, f_finddir},
934 {"findfile", 1, 3, FEARG_1, NULL,
935 ret_string, f_findfile},
936 {"flatten", 1, 2, FEARG_1, NULL,
937 ret_list_any, f_flatten},
938 {"float2nr", 1, 1, FEARG_1, NULL,
939 ret_number, FLOAT_FUNC(f_float2nr)},
940 {"floor", 1, 1, FEARG_1, NULL,
941 ret_float, FLOAT_FUNC(f_floor)},
942 {"fmod", 2, 2, FEARG_1, NULL,
943 ret_float, FLOAT_FUNC(f_fmod)},
944 {"fnameescape", 1, 1, FEARG_1, NULL,
945 ret_string, f_fnameescape},
946 {"fnamemodify", 2, 2, FEARG_1, NULL,
947 ret_string, f_fnamemodify},
948 {"foldclosed", 1, 1, FEARG_1, NULL,
949 ret_number, f_foldclosed},
950 {"foldclosedend", 1, 1, FEARG_1, NULL,
951 ret_number, f_foldclosedend},
952 {"foldlevel", 1, 1, FEARG_1, NULL,
953 ret_number, f_foldlevel},
954 {"foldtext", 0, 0, 0, NULL,
955 ret_string, f_foldtext},
956 {"foldtextresult", 1, 1, FEARG_1, NULL,
957 ret_string, f_foldtextresult},
958 {"foreground", 0, 0, 0, NULL,
959 ret_void, f_foreground},
960 {"funcref", 1, 3, FEARG_1, NULL,
961 ret_func_any, f_funcref},
962 {"function", 1, 3, FEARG_1, NULL,
963 ret_f_function, f_function},
964 {"garbagecollect", 0, 1, 0, NULL,
965 ret_void, f_garbagecollect},
966 {"get", 2, 3, FEARG_1, NULL,
967 ret_any, f_get},
968 {"getbufinfo", 0, 1, FEARG_1, NULL,
969 ret_list_dict_any, f_getbufinfo},
970 {"getbufline", 2, 3, FEARG_1, NULL,
971 ret_list_string, f_getbufline},
972 {"getbufvar", 2, 3, FEARG_1, NULL,
973 ret_any, f_getbufvar},
974 {"getchangelist", 0, 1, FEARG_1, NULL,
975 ret_list_any, f_getchangelist},
976 {"getchar", 0, 1, 0, NULL,
977 ret_number, f_getchar},
978 {"getcharmod", 0, 0, 0, NULL,
979 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100980 {"getcharpos", 1, 1, FEARG_1, NULL,
981 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200982 {"getcharsearch", 0, 0, 0, NULL,
983 ret_dict_any, f_getcharsearch},
984 {"getcmdline", 0, 0, 0, NULL,
985 ret_string, f_getcmdline},
986 {"getcmdpos", 0, 0, 0, NULL,
987 ret_number, f_getcmdpos},
988 {"getcmdtype", 0, 0, 0, NULL,
989 ret_string, f_getcmdtype},
990 {"getcmdwintype", 0, 0, 0, NULL,
991 ret_string, f_getcmdwintype},
992 {"getcompletion", 2, 3, FEARG_1, NULL,
993 ret_list_string, f_getcompletion},
994 {"getcurpos", 0, 1, FEARG_1, NULL,
995 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100996 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
997 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200998 {"getcwd", 0, 2, FEARG_1, NULL,
999 ret_string, f_getcwd},
1000 {"getenv", 1, 1, FEARG_1, NULL,
1001 ret_string, f_getenv},
1002 {"getfontname", 0, 1, 0, NULL,
1003 ret_string, f_getfontname},
1004 {"getfperm", 1, 1, FEARG_1, NULL,
1005 ret_string, f_getfperm},
1006 {"getfsize", 1, 1, FEARG_1, NULL,
1007 ret_number, f_getfsize},
1008 {"getftime", 1, 1, FEARG_1, NULL,
1009 ret_number, f_getftime},
1010 {"getftype", 1, 1, FEARG_1, NULL,
1011 ret_string, f_getftype},
1012 {"getimstatus", 0, 0, 0, NULL,
1013 ret_number, f_getimstatus},
1014 {"getjumplist", 0, 2, FEARG_1, NULL,
1015 ret_list_any, f_getjumplist},
1016 {"getline", 1, 2, FEARG_1, NULL,
1017 ret_f_getline, f_getline},
1018 {"getloclist", 1, 2, 0, NULL,
1019 ret_list_or_dict_1, f_getloclist},
1020 {"getmarklist", 0, 1, FEARG_1, NULL,
1021 ret_list_dict_any, f_getmarklist},
1022 {"getmatches", 0, 1, 0, NULL,
1023 ret_list_dict_any, f_getmatches},
1024 {"getmousepos", 0, 0, 0, NULL,
1025 ret_dict_number, f_getmousepos},
1026 {"getpid", 0, 0, 0, NULL,
1027 ret_number, f_getpid},
1028 {"getpos", 1, 1, FEARG_1, NULL,
1029 ret_list_number, f_getpos},
1030 {"getqflist", 0, 1, 0, NULL,
1031 ret_list_or_dict_0, f_getqflist},
1032 {"getreg", 0, 3, FEARG_1, NULL,
1033 ret_getreg, f_getreg},
1034 {"getreginfo", 0, 1, FEARG_1, NULL,
1035 ret_dict_any, f_getreginfo},
1036 {"getregtype", 0, 1, FEARG_1, NULL,
1037 ret_string, f_getregtype},
1038 {"gettabinfo", 0, 1, FEARG_1, NULL,
1039 ret_list_dict_any, f_gettabinfo},
1040 {"gettabvar", 2, 3, FEARG_1, NULL,
1041 ret_any, f_gettabvar},
1042 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1043 ret_any, f_gettabwinvar},
1044 {"gettagstack", 0, 1, FEARG_1, NULL,
1045 ret_dict_any, f_gettagstack},
1046 {"gettext", 1, 1, FEARG_1, NULL,
1047 ret_string, f_gettext},
1048 {"getwininfo", 0, 1, FEARG_1, NULL,
1049 ret_list_dict_any, f_getwininfo},
1050 {"getwinpos", 0, 1, FEARG_1, NULL,
1051 ret_list_number, f_getwinpos},
1052 {"getwinposx", 0, 0, 0, NULL,
1053 ret_number, f_getwinposx},
1054 {"getwinposy", 0, 0, 0, NULL,
1055 ret_number, f_getwinposy},
1056 {"getwinvar", 2, 3, FEARG_1, NULL,
1057 ret_any, f_getwinvar},
1058 {"glob", 1, 4, FEARG_1, NULL,
1059 ret_any, f_glob},
1060 {"glob2regpat", 1, 1, FEARG_1, NULL,
1061 ret_string, f_glob2regpat},
1062 {"globpath", 2, 5, FEARG_2, NULL,
1063 ret_any, f_globpath},
1064 {"has", 1, 2, 0, NULL,
1065 ret_number, f_has},
1066 {"has_key", 2, 2, FEARG_1, NULL,
1067 ret_number, f_has_key},
1068 {"haslocaldir", 0, 2, FEARG_1, NULL,
1069 ret_number, f_haslocaldir},
1070 {"hasmapto", 1, 3, FEARG_1, NULL,
1071 ret_number, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001072 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1073 ret_number, f_hlID},
1074 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
1075 ret_number, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001076 {"histadd", 2, 2, FEARG_2, NULL,
1077 ret_number, f_histadd},
1078 {"histdel", 1, 2, FEARG_1, NULL,
1079 ret_number, f_histdel},
1080 {"histget", 1, 2, FEARG_1, NULL,
1081 ret_string, f_histget},
1082 {"histnr", 1, 1, FEARG_1, NULL,
1083 ret_number, f_histnr},
1084 {"hlID", 1, 1, FEARG_1, NULL,
1085 ret_number, f_hlID},
1086 {"hlexists", 1, 1, FEARG_1, NULL,
1087 ret_number, f_hlexists},
1088 {"hostname", 0, 0, 0, NULL,
1089 ret_string, f_hostname},
1090 {"iconv", 3, 3, FEARG_1, NULL,
1091 ret_string, f_iconv},
1092 {"indent", 1, 1, FEARG_1, NULL,
1093 ret_number, f_indent},
1094 {"index", 2, 4, FEARG_1, NULL,
1095 ret_number, f_index},
1096 {"input", 1, 3, FEARG_1, NULL,
1097 ret_string, f_input},
1098 {"inputdialog", 1, 3, FEARG_1, NULL,
1099 ret_string, f_inputdialog},
1100 {"inputlist", 1, 1, FEARG_1, NULL,
1101 ret_number, f_inputlist},
1102 {"inputrestore", 0, 0, 0, NULL,
1103 ret_number, f_inputrestore},
1104 {"inputsave", 0, 0, 0, NULL,
1105 ret_number, f_inputsave},
1106 {"inputsecret", 1, 2, FEARG_1, NULL,
1107 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001108 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001109 ret_first_arg, f_insert},
1110 {"interrupt", 0, 0, 0, NULL,
1111 ret_void, f_interrupt},
1112 {"invert", 1, 1, FEARG_1, NULL,
1113 ret_number, f_invert},
1114 {"isdirectory", 1, 1, FEARG_1, NULL,
1115 ret_number, f_isdirectory},
1116 {"isinf", 1, 1, FEARG_1, NULL,
1117 ret_number, MATH_FUNC(f_isinf)},
1118 {"islocked", 1, 1, FEARG_1, NULL,
1119 ret_number, f_islocked},
1120 {"isnan", 1, 1, FEARG_1, NULL,
1121 ret_number, MATH_FUNC(f_isnan)},
1122 {"items", 1, 1, FEARG_1, NULL,
1123 ret_list_any, f_items},
1124 {"job_getchannel", 1, 1, FEARG_1, NULL,
1125 ret_channel, JOB_FUNC(f_job_getchannel)},
1126 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001127 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001128 {"job_setoptions", 2, 2, FEARG_1, NULL,
1129 ret_void, JOB_FUNC(f_job_setoptions)},
1130 {"job_start", 1, 2, FEARG_1, NULL,
1131 ret_job, JOB_FUNC(f_job_start)},
1132 {"job_status", 1, 1, FEARG_1, NULL,
1133 ret_string, JOB_FUNC(f_job_status)},
1134 {"job_stop", 1, 2, FEARG_1, NULL,
1135 ret_number, JOB_FUNC(f_job_stop)},
1136 {"join", 1, 2, FEARG_1, NULL,
1137 ret_string, f_join},
1138 {"js_decode", 1, 1, FEARG_1, NULL,
1139 ret_any, f_js_decode},
1140 {"js_encode", 1, 1, FEARG_1, NULL,
1141 ret_string, f_js_encode},
1142 {"json_decode", 1, 1, FEARG_1, NULL,
1143 ret_any, f_json_decode},
1144 {"json_encode", 1, 1, FEARG_1, NULL,
1145 ret_string, f_json_encode},
1146 {"keys", 1, 1, FEARG_1, NULL,
1147 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001148 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1149 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001150 {"len", 1, 1, FEARG_1, NULL,
1151 ret_number, f_len},
1152 {"libcall", 3, 3, FEARG_3, NULL,
1153 ret_string, f_libcall},
1154 {"libcallnr", 3, 3, FEARG_3, NULL,
1155 ret_number, f_libcallnr},
1156 {"line", 1, 2, FEARG_1, NULL,
1157 ret_number, f_line},
1158 {"line2byte", 1, 1, FEARG_1, NULL,
1159 ret_number, f_line2byte},
1160 {"lispindent", 1, 1, FEARG_1, NULL,
1161 ret_number, f_lispindent},
1162 {"list2str", 1, 2, FEARG_1, NULL,
1163 ret_string, f_list2str},
1164 {"listener_add", 1, 2, FEARG_2, NULL,
1165 ret_number, f_listener_add},
1166 {"listener_flush", 0, 1, FEARG_1, NULL,
1167 ret_void, f_listener_flush},
1168 {"listener_remove", 1, 1, FEARG_1, NULL,
1169 ret_number, f_listener_remove},
1170 {"localtime", 0, 0, 0, NULL,
1171 ret_number, f_localtime},
1172 {"log", 1, 1, FEARG_1, NULL,
1173 ret_float, FLOAT_FUNC(f_log)},
1174 {"log10", 1, 1, FEARG_1, NULL,
1175 ret_float, FLOAT_FUNC(f_log10)},
1176 {"luaeval", 1, 2, FEARG_1, NULL,
1177 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001178#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001179 f_luaeval
1180#else
1181 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001182#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001183 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001184 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001185 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001186 {"maparg", 1, 4, FEARG_1, NULL,
1187 ret_maparg, f_maparg},
1188 {"mapcheck", 1, 3, FEARG_1, NULL,
1189 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001190 {"mapnew", 2, 2, FEARG_1, NULL,
1191 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001192 {"mapset", 3, 3, FEARG_1, NULL,
1193 ret_void, f_mapset},
1194 {"match", 2, 4, FEARG_1, NULL,
1195 ret_any, f_match},
1196 {"matchadd", 2, 5, FEARG_1, NULL,
1197 ret_number, f_matchadd},
1198 {"matchaddpos", 2, 5, FEARG_1, NULL,
1199 ret_number, f_matchaddpos},
1200 {"matcharg", 1, 1, FEARG_1, NULL,
1201 ret_list_string, f_matcharg},
1202 {"matchdelete", 1, 2, FEARG_1, NULL,
1203 ret_number, f_matchdelete},
1204 {"matchend", 2, 4, FEARG_1, NULL,
1205 ret_number, f_matchend},
1206 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1207 ret_list_string, f_matchfuzzy},
1208 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1209 ret_list_any, f_matchfuzzypos},
1210 {"matchlist", 2, 4, FEARG_1, NULL,
1211 ret_list_string, f_matchlist},
1212 {"matchstr", 2, 4, FEARG_1, NULL,
1213 ret_string, f_matchstr},
1214 {"matchstrpos", 2, 4, FEARG_1, NULL,
1215 ret_list_any, f_matchstrpos},
1216 {"max", 1, 1, FEARG_1, NULL,
1217 ret_any, f_max},
1218 {"menu_info", 1, 2, FEARG_1, NULL,
1219 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001220#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001221 f_menu_info
1222#else
1223 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001224#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001225 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001226 {"min", 1, 1, FEARG_1, NULL,
1227 ret_any, f_min},
1228 {"mkdir", 1, 3, FEARG_1, NULL,
1229 ret_number, f_mkdir},
1230 {"mode", 0, 1, FEARG_1, NULL,
1231 ret_string, f_mode},
1232 {"mzeval", 1, 1, FEARG_1, NULL,
1233 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001234#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001235 f_mzeval
1236#else
1237 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001238#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001239 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001240 {"nextnonblank", 1, 1, FEARG_1, NULL,
1241 ret_number, f_nextnonblank},
1242 {"nr2char", 1, 2, FEARG_1, NULL,
1243 ret_string, f_nr2char},
1244 {"or", 2, 2, FEARG_1, NULL,
1245 ret_number, f_or},
1246 {"pathshorten", 1, 2, FEARG_1, NULL,
1247 ret_string, f_pathshorten},
1248 {"perleval", 1, 1, FEARG_1, NULL,
1249 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001250#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001251 f_perleval
1252#else
1253 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001254#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001255 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001256 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1257 ret_number, PROP_FUNC(f_popup_atcursor)},
1258 {"popup_beval", 2, 2, FEARG_1, NULL,
1259 ret_number, PROP_FUNC(f_popup_beval)},
1260 {"popup_clear", 0, 1, 0, NULL,
1261 ret_void, PROP_FUNC(f_popup_clear)},
1262 {"popup_close", 1, 2, FEARG_1, NULL,
1263 ret_void, PROP_FUNC(f_popup_close)},
1264 {"popup_create", 2, 2, FEARG_1, NULL,
1265 ret_number, PROP_FUNC(f_popup_create)},
1266 {"popup_dialog", 2, 2, FEARG_1, NULL,
1267 ret_number, PROP_FUNC(f_popup_dialog)},
1268 {"popup_filter_menu", 2, 2, 0, NULL,
1269 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1270 {"popup_filter_yesno", 2, 2, 0, NULL,
1271 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1272 {"popup_findinfo", 0, 0, 0, NULL,
1273 ret_number, PROP_FUNC(f_popup_findinfo)},
1274 {"popup_findpreview", 0, 0, 0, NULL,
1275 ret_number, PROP_FUNC(f_popup_findpreview)},
1276 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1277 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1278 {"popup_getpos", 1, 1, FEARG_1, NULL,
1279 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1280 {"popup_hide", 1, 1, FEARG_1, NULL,
1281 ret_void, PROP_FUNC(f_popup_hide)},
1282 {"popup_list", 0, 0, 0, NULL,
1283 ret_list_number, PROP_FUNC(f_popup_list)},
1284 {"popup_locate", 2, 2, 0, NULL,
1285 ret_number, PROP_FUNC(f_popup_locate)},
1286 {"popup_menu", 2, 2, FEARG_1, NULL,
1287 ret_number, PROP_FUNC(f_popup_menu)},
1288 {"popup_move", 2, 2, FEARG_1, NULL,
1289 ret_void, PROP_FUNC(f_popup_move)},
1290 {"popup_notification", 2, 2, FEARG_1, NULL,
1291 ret_number, PROP_FUNC(f_popup_notification)},
1292 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1293 ret_void, PROP_FUNC(f_popup_setoptions)},
1294 {"popup_settext", 2, 2, FEARG_1, NULL,
1295 ret_void, PROP_FUNC(f_popup_settext)},
1296 {"popup_show", 1, 1, FEARG_1, NULL,
1297 ret_void, PROP_FUNC(f_popup_show)},
1298 {"pow", 2, 2, FEARG_1, NULL,
1299 ret_float, FLOAT_FUNC(f_pow)},
1300 {"prevnonblank", 1, 1, FEARG_1, NULL,
1301 ret_number, f_prevnonblank},
1302 {"printf", 1, 19, FEARG_2, NULL,
1303 ret_string, f_printf},
1304 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1305 ret_string, JOB_FUNC(f_prompt_getprompt)},
1306 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1307 ret_void, JOB_FUNC(f_prompt_setcallback)},
1308 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1309 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1310 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1311 ret_void, JOB_FUNC(f_prompt_setprompt)},
1312 {"prop_add", 3, 3, FEARG_1, NULL,
1313 ret_void, PROP_FUNC(f_prop_add)},
1314 {"prop_clear", 1, 3, FEARG_1, NULL,
1315 ret_void, PROP_FUNC(f_prop_clear)},
1316 {"prop_find", 1, 2, FEARG_1, NULL,
1317 ret_dict_any, PROP_FUNC(f_prop_find)},
1318 {"prop_list", 1, 2, FEARG_1, NULL,
1319 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1320 {"prop_remove", 1, 3, FEARG_1, NULL,
1321 ret_number, PROP_FUNC(f_prop_remove)},
1322 {"prop_type_add", 2, 2, FEARG_1, NULL,
1323 ret_void, PROP_FUNC(f_prop_type_add)},
1324 {"prop_type_change", 2, 2, FEARG_1, NULL,
1325 ret_void, PROP_FUNC(f_prop_type_change)},
1326 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1327 ret_void, PROP_FUNC(f_prop_type_delete)},
1328 {"prop_type_get", 1, 2, FEARG_1, NULL,
1329 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1330 {"prop_type_list", 0, 1, FEARG_1, NULL,
1331 ret_list_string, PROP_FUNC(f_prop_type_list)},
1332 {"pum_getpos", 0, 0, 0, NULL,
1333 ret_dict_number, f_pum_getpos},
1334 {"pumvisible", 0, 0, 0, NULL,
1335 ret_number, f_pumvisible},
1336 {"py3eval", 1, 1, FEARG_1, NULL,
1337 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001338#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001339 f_py3eval
1340#else
1341 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001342#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001343 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001344 {"pyeval", 1, 1, FEARG_1, NULL,
1345 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001346#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001347 f_pyeval
1348#else
1349 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001350#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001351 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001352 {"pyxeval", 1, 1, FEARG_1, NULL,
1353 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001354#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001355 f_pyxeval
1356#else
1357 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001358#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001359 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001360 {"rand", 0, 1, FEARG_1, NULL,
1361 ret_number, f_rand},
1362 {"range", 1, 3, FEARG_1, NULL,
1363 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001364 {"readblob", 1, 1, FEARG_1, NULL,
1365 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001366 {"readdir", 1, 3, FEARG_1, NULL,
1367 ret_list_string, f_readdir},
1368 {"readdirex", 1, 3, FEARG_1, NULL,
1369 ret_list_dict_any, f_readdirex},
1370 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001371 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001372 {"reduce", 2, 3, FEARG_1, NULL,
1373 ret_any, f_reduce},
1374 {"reg_executing", 0, 0, 0, NULL,
1375 ret_string, f_reg_executing},
1376 {"reg_recording", 0, 0, 0, NULL,
1377 ret_string, f_reg_recording},
1378 {"reltime", 0, 2, FEARG_1, NULL,
1379 ret_list_any, f_reltime},
1380 {"reltimefloat", 1, 1, FEARG_1, NULL,
1381 ret_float, FLOAT_FUNC(f_reltimefloat)},
1382 {"reltimestr", 1, 1, FEARG_1, NULL,
1383 ret_string, f_reltimestr},
1384 {"remote_expr", 2, 4, FEARG_1, NULL,
1385 ret_string, f_remote_expr},
1386 {"remote_foreground", 1, 1, FEARG_1, NULL,
1387 ret_string, f_remote_foreground},
1388 {"remote_peek", 1, 2, FEARG_1, NULL,
1389 ret_number, f_remote_peek},
1390 {"remote_read", 1, 2, FEARG_1, NULL,
1391 ret_string, f_remote_read},
1392 {"remote_send", 2, 3, FEARG_1, NULL,
1393 ret_string, f_remote_send},
1394 {"remote_startserver", 1, 1, FEARG_1, NULL,
1395 ret_void, f_remote_startserver},
1396 {"remove", 2, 3, FEARG_1, NULL,
1397 ret_remove, f_remove},
1398 {"rename", 2, 2, FEARG_1, NULL,
1399 ret_number, f_rename},
1400 {"repeat", 2, 2, FEARG_1, NULL,
1401 ret_first_arg, f_repeat},
1402 {"resolve", 1, 1, FEARG_1, NULL,
1403 ret_string, f_resolve},
1404 {"reverse", 1, 1, FEARG_1, NULL,
1405 ret_first_arg, f_reverse},
1406 {"round", 1, 1, FEARG_1, NULL,
1407 ret_float, FLOAT_FUNC(f_round)},
1408 {"rubyeval", 1, 1, FEARG_1, NULL,
1409 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001410#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001411 f_rubyeval
1412#else
1413 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001414#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001415 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001416 {"screenattr", 2, 2, FEARG_1, NULL,
1417 ret_number, f_screenattr},
1418 {"screenchar", 2, 2, FEARG_1, NULL,
1419 ret_number, f_screenchar},
1420 {"screenchars", 2, 2, FEARG_1, NULL,
1421 ret_list_number, f_screenchars},
1422 {"screencol", 0, 0, 0, NULL,
1423 ret_number, f_screencol},
1424 {"screenpos", 3, 3, FEARG_1, NULL,
1425 ret_dict_number, f_screenpos},
1426 {"screenrow", 0, 0, 0, NULL,
1427 ret_number, f_screenrow},
1428 {"screenstring", 2, 2, FEARG_1, NULL,
1429 ret_string, f_screenstring},
1430 {"search", 1, 5, FEARG_1, NULL,
1431 ret_number, f_search},
1432 {"searchcount", 0, 1, FEARG_1, NULL,
1433 ret_dict_any, f_searchcount},
1434 {"searchdecl", 1, 3, FEARG_1, NULL,
1435 ret_number, f_searchdecl},
1436 {"searchpair", 3, 7, 0, NULL,
1437 ret_number, f_searchpair},
1438 {"searchpairpos", 3, 7, 0, NULL,
1439 ret_list_number, f_searchpairpos},
1440 {"searchpos", 1, 5, FEARG_1, NULL,
1441 ret_list_number, f_searchpos},
1442 {"server2client", 2, 2, FEARG_1, NULL,
1443 ret_number, f_server2client},
1444 {"serverlist", 0, 0, 0, NULL,
1445 ret_string, f_serverlist},
1446 {"setbufline", 3, 3, FEARG_3, NULL,
1447 ret_number, f_setbufline},
1448 {"setbufvar", 3, 3, FEARG_3, NULL,
1449 ret_void, f_setbufvar},
1450 {"setcellwidths", 1, 1, FEARG_1, NULL,
1451 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001452 {"setcharpos", 2, 2, FEARG_2, NULL,
1453 ret_number, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001454 {"setcharsearch", 1, 1, FEARG_1, NULL,
1455 ret_void, f_setcharsearch},
1456 {"setcmdpos", 1, 1, FEARG_1, NULL,
1457 ret_number, f_setcmdpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001458 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1459 ret_number, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001460 {"setenv", 2, 2, FEARG_2, NULL,
1461 ret_void, f_setenv},
1462 {"setfperm", 2, 2, FEARG_1, NULL,
1463 ret_number, f_setfperm},
1464 {"setline", 2, 2, FEARG_2, NULL,
1465 ret_number, f_setline},
1466 {"setloclist", 2, 4, FEARG_2, NULL,
1467 ret_number, f_setloclist},
1468 {"setmatches", 1, 2, FEARG_1, NULL,
1469 ret_number, f_setmatches},
1470 {"setpos", 2, 2, FEARG_2, NULL,
1471 ret_number, f_setpos},
1472 {"setqflist", 1, 3, FEARG_1, NULL,
1473 ret_number, f_setqflist},
1474 {"setreg", 2, 3, FEARG_2, NULL,
1475 ret_number, f_setreg},
1476 {"settabvar", 3, 3, FEARG_3, NULL,
1477 ret_void, f_settabvar},
1478 {"settabwinvar", 4, 4, FEARG_4, NULL,
1479 ret_void, f_settabwinvar},
1480 {"settagstack", 2, 3, FEARG_2, NULL,
1481 ret_number, f_settagstack},
1482 {"setwinvar", 3, 3, FEARG_3, NULL,
1483 ret_void, f_setwinvar},
1484 {"sha256", 1, 1, FEARG_1, NULL,
1485 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001486#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001487 f_sha256
1488#else
1489 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001490#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001491 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001492 {"shellescape", 1, 2, FEARG_1, NULL,
1493 ret_string, f_shellescape},
1494 {"shiftwidth", 0, 1, FEARG_1, NULL,
1495 ret_number, f_shiftwidth},
1496 {"sign_define", 1, 2, FEARG_1, NULL,
1497 ret_any, SIGN_FUNC(f_sign_define)},
1498 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1499 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1500 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1501 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1502 {"sign_jump", 3, 3, FEARG_1, NULL,
1503 ret_number, SIGN_FUNC(f_sign_jump)},
1504 {"sign_place", 4, 5, FEARG_1, NULL,
1505 ret_number, SIGN_FUNC(f_sign_place)},
1506 {"sign_placelist", 1, 1, FEARG_1, NULL,
1507 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1508 {"sign_undefine", 0, 1, FEARG_1, NULL,
1509 ret_number, SIGN_FUNC(f_sign_undefine)},
1510 {"sign_unplace", 1, 2, FEARG_1, NULL,
1511 ret_number, SIGN_FUNC(f_sign_unplace)},
1512 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1513 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1514 {"simplify", 1, 1, FEARG_1, NULL,
1515 ret_string, f_simplify},
1516 {"sin", 1, 1, FEARG_1, NULL,
1517 ret_float, FLOAT_FUNC(f_sin)},
1518 {"sinh", 1, 1, FEARG_1, NULL,
1519 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001520 {"slice", 2, 3, FEARG_1, NULL,
1521 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001522 {"sort", 1, 3, FEARG_1, NULL,
1523 ret_first_arg, f_sort},
1524 {"sound_clear", 0, 0, 0, NULL,
1525 ret_void, SOUND_FUNC(f_sound_clear)},
1526 {"sound_playevent", 1, 2, FEARG_1, NULL,
1527 ret_number, SOUND_FUNC(f_sound_playevent)},
1528 {"sound_playfile", 1, 2, FEARG_1, NULL,
1529 ret_number, SOUND_FUNC(f_sound_playfile)},
1530 {"sound_stop", 1, 1, FEARG_1, NULL,
1531 ret_void, SOUND_FUNC(f_sound_stop)},
1532 {"soundfold", 1, 1, FEARG_1, NULL,
1533 ret_string, f_soundfold},
1534 {"spellbadword", 0, 1, FEARG_1, NULL,
1535 ret_list_string, f_spellbadword},
1536 {"spellsuggest", 1, 3, FEARG_1, NULL,
1537 ret_list_string, f_spellsuggest},
1538 {"split", 1, 3, FEARG_1, NULL,
1539 ret_list_string, f_split},
1540 {"sqrt", 1, 1, FEARG_1, NULL,
1541 ret_float, FLOAT_FUNC(f_sqrt)},
1542 {"srand", 0, 1, FEARG_1, NULL,
1543 ret_list_number, f_srand},
1544 {"state", 0, 1, FEARG_1, NULL,
1545 ret_string, f_state},
1546 {"str2float", 1, 1, FEARG_1, NULL,
1547 ret_float, FLOAT_FUNC(f_str2float)},
1548 {"str2list", 1, 2, FEARG_1, NULL,
1549 ret_list_number, f_str2list},
1550 {"str2nr", 1, 3, FEARG_1, NULL,
1551 ret_number, f_str2nr},
1552 {"strcharpart", 2, 3, FEARG_1, NULL,
1553 ret_string, f_strcharpart},
1554 {"strchars", 1, 2, FEARG_1, NULL,
1555 ret_number, f_strchars},
1556 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1557 ret_number, f_strdisplaywidth},
1558 {"strftime", 1, 2, FEARG_1, NULL,
1559 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001560#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001561 f_strftime
1562#else
1563 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001564#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001565 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001566 {"strgetchar", 2, 2, FEARG_1, NULL,
1567 ret_number, f_strgetchar},
1568 {"stridx", 2, 3, FEARG_1, NULL,
1569 ret_number, f_stridx},
1570 {"string", 1, 1, FEARG_1, NULL,
1571 ret_string, f_string},
1572 {"strlen", 1, 1, FEARG_1, NULL,
1573 ret_number, f_strlen},
1574 {"strpart", 2, 4, FEARG_1, NULL,
1575 ret_string, f_strpart},
1576 {"strptime", 2, 2, FEARG_1, NULL,
1577 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001578#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001579 f_strptime
1580#else
1581 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001582#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001583 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001584 {"strridx", 2, 3, FEARG_1, NULL,
1585 ret_number, f_strridx},
1586 {"strtrans", 1, 1, FEARG_1, NULL,
1587 ret_string, f_strtrans},
1588 {"strwidth", 1, 1, FEARG_1, NULL,
1589 ret_number, f_strwidth},
1590 {"submatch", 1, 2, FEARG_1, NULL,
1591 ret_string, f_submatch},
1592 {"substitute", 4, 4, FEARG_1, NULL,
1593 ret_string, f_substitute},
1594 {"swapinfo", 1, 1, FEARG_1, NULL,
1595 ret_dict_any, f_swapinfo},
1596 {"swapname", 1, 1, FEARG_1, NULL,
1597 ret_string, f_swapname},
1598 {"synID", 3, 3, 0, NULL,
1599 ret_number, f_synID},
1600 {"synIDattr", 2, 3, FEARG_1, NULL,
1601 ret_string, f_synIDattr},
1602 {"synIDtrans", 1, 1, FEARG_1, NULL,
1603 ret_number, f_synIDtrans},
1604 {"synconcealed", 2, 2, 0, NULL,
1605 ret_list_any, f_synconcealed},
1606 {"synstack", 2, 2, 0, NULL,
1607 ret_list_number, f_synstack},
1608 {"system", 1, 2, FEARG_1, NULL,
1609 ret_string, f_system},
1610 {"systemlist", 1, 2, FEARG_1, NULL,
1611 ret_list_string, f_systemlist},
1612 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1613 ret_list_number, f_tabpagebuflist},
1614 {"tabpagenr", 0, 1, 0, NULL,
1615 ret_number, f_tabpagenr},
1616 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1617 ret_number, f_tabpagewinnr},
1618 {"tagfiles", 0, 0, 0, NULL,
1619 ret_list_string, f_tagfiles},
1620 {"taglist", 1, 2, FEARG_1, NULL,
1621 ret_list_dict_any, f_taglist},
1622 {"tan", 1, 1, FEARG_1, NULL,
1623 ret_float, FLOAT_FUNC(f_tan)},
1624 {"tanh", 1, 1, FEARG_1, NULL,
1625 ret_float, FLOAT_FUNC(f_tanh)},
1626 {"tempname", 0, 0, 0, NULL,
1627 ret_string, f_tempname},
1628 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1629 ret_number, TERM_FUNC(f_term_dumpdiff)},
1630 {"term_dumpload", 1, 2, FEARG_1, NULL,
1631 ret_number, TERM_FUNC(f_term_dumpload)},
1632 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1633 ret_void, TERM_FUNC(f_term_dumpwrite)},
1634 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1635 ret_number, TERM_FUNC(f_term_getaltscreen)},
1636 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1637 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001638#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001639 f_term_getansicolors
1640#else
1641 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001642#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001643 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001644 {"term_getattr", 2, 2, FEARG_1, NULL,
1645 ret_number, TERM_FUNC(f_term_getattr)},
1646 {"term_getcursor", 1, 1, FEARG_1, NULL,
1647 ret_list_any, TERM_FUNC(f_term_getcursor)},
1648 {"term_getjob", 1, 1, FEARG_1, NULL,
1649 ret_job, TERM_FUNC(f_term_getjob)},
1650 {"term_getline", 2, 2, FEARG_1, NULL,
1651 ret_string, TERM_FUNC(f_term_getline)},
1652 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1653 ret_number, TERM_FUNC(f_term_getscrolled)},
1654 {"term_getsize", 1, 1, FEARG_1, NULL,
1655 ret_list_number, TERM_FUNC(f_term_getsize)},
1656 {"term_getstatus", 1, 1, FEARG_1, NULL,
1657 ret_string, TERM_FUNC(f_term_getstatus)},
1658 {"term_gettitle", 1, 1, FEARG_1, NULL,
1659 ret_string, TERM_FUNC(f_term_gettitle)},
1660 {"term_gettty", 1, 2, FEARG_1, NULL,
1661 ret_string, TERM_FUNC(f_term_gettty)},
1662 {"term_list", 0, 0, 0, NULL,
1663 ret_list_number, TERM_FUNC(f_term_list)},
1664 {"term_scrape", 2, 2, FEARG_1, NULL,
1665 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1666 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1667 ret_void, TERM_FUNC(f_term_sendkeys)},
1668 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1669 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001670#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001671 f_term_setansicolors
1672#else
1673 NULL
1674#endif
1675 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001676 {"term_setapi", 2, 2, FEARG_1, NULL,
1677 ret_void, TERM_FUNC(f_term_setapi)},
1678 {"term_setkill", 2, 2, FEARG_1, NULL,
1679 ret_void, TERM_FUNC(f_term_setkill)},
1680 {"term_setrestore", 2, 2, FEARG_1, NULL,
1681 ret_void, TERM_FUNC(f_term_setrestore)},
1682 {"term_setsize", 3, 3, FEARG_1, NULL,
1683 ret_void, TERM_FUNC(f_term_setsize)},
1684 {"term_start", 1, 2, FEARG_1, NULL,
1685 ret_number, TERM_FUNC(f_term_start)},
1686 {"term_wait", 1, 2, FEARG_1, NULL,
1687 ret_void, TERM_FUNC(f_term_wait)},
1688 {"terminalprops", 0, 0, 0, NULL,
1689 ret_dict_string, f_terminalprops},
1690 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1691 ret_void, f_test_alloc_fail},
1692 {"test_autochdir", 0, 0, 0, NULL,
1693 ret_void, f_test_autochdir},
1694 {"test_feedinput", 1, 1, FEARG_1, NULL,
1695 ret_void, f_test_feedinput},
1696 {"test_garbagecollect_now", 0, 0, 0, NULL,
1697 ret_void, f_test_garbagecollect_now},
1698 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1699 ret_void, f_test_garbagecollect_soon},
1700 {"test_getvalue", 1, 1, FEARG_1, NULL,
1701 ret_number, f_test_getvalue},
1702 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1703 ret_void, f_test_ignore_error},
1704 {"test_null_blob", 0, 0, 0, NULL,
1705 ret_blob, f_test_null_blob},
1706 {"test_null_channel", 0, 0, 0, NULL,
1707 ret_channel, JOB_FUNC(f_test_null_channel)},
1708 {"test_null_dict", 0, 0, 0, NULL,
1709 ret_dict_any, f_test_null_dict},
1710 {"test_null_function", 0, 0, 0, NULL,
1711 ret_func_any, f_test_null_function},
1712 {"test_null_job", 0, 0, 0, NULL,
1713 ret_job, JOB_FUNC(f_test_null_job)},
1714 {"test_null_list", 0, 0, 0, NULL,
1715 ret_list_any, f_test_null_list},
1716 {"test_null_partial", 0, 0, 0, NULL,
1717 ret_func_any, f_test_null_partial},
1718 {"test_null_string", 0, 0, 0, NULL,
1719 ret_string, f_test_null_string},
1720 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1721 ret_void, f_test_option_not_set},
1722 {"test_override", 2, 2, FEARG_2, NULL,
1723 ret_void, f_test_override},
1724 {"test_refcount", 1, 1, FEARG_1, NULL,
1725 ret_number, f_test_refcount},
1726 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1727 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001728#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001729 f_test_scrollbar
1730#else
1731 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001732#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001733 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001734 {"test_setmouse", 2, 2, 0, NULL,
1735 ret_void, f_test_setmouse},
1736 {"test_settime", 1, 1, FEARG_1, NULL,
1737 ret_void, f_test_settime},
1738 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1739 ret_void, f_test_srand_seed},
1740 {"test_unknown", 0, 0, 0, NULL,
1741 ret_any, f_test_unknown},
1742 {"test_void", 0, 0, 0, NULL,
1743 ret_void, f_test_void},
1744 {"timer_info", 0, 1, FEARG_1, NULL,
1745 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1746 {"timer_pause", 2, 2, FEARG_1, NULL,
1747 ret_void, TIMER_FUNC(f_timer_pause)},
1748 {"timer_start", 2, 3, FEARG_1, NULL,
1749 ret_number, TIMER_FUNC(f_timer_start)},
1750 {"timer_stop", 1, 1, FEARG_1, NULL,
1751 ret_void, TIMER_FUNC(f_timer_stop)},
1752 {"timer_stopall", 0, 0, 0, NULL,
1753 ret_void, TIMER_FUNC(f_timer_stopall)},
1754 {"tolower", 1, 1, FEARG_1, NULL,
1755 ret_string, f_tolower},
1756 {"toupper", 1, 1, FEARG_1, NULL,
1757 ret_string, f_toupper},
1758 {"tr", 3, 3, FEARG_1, NULL,
1759 ret_string, f_tr},
1760 {"trim", 1, 3, FEARG_1, NULL,
1761 ret_string, f_trim},
1762 {"trunc", 1, 1, FEARG_1, NULL,
1763 ret_float, FLOAT_FUNC(f_trunc)},
1764 {"type", 1, 1, FEARG_1, NULL,
1765 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001766 {"typename", 1, 1, FEARG_1, NULL,
1767 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001768 {"undofile", 1, 1, FEARG_1, NULL,
1769 ret_string, f_undofile},
1770 {"undotree", 0, 0, 0, NULL,
1771 ret_dict_any, f_undotree},
1772 {"uniq", 1, 3, FEARG_1, NULL,
1773 ret_list_any, f_uniq},
1774 {"values", 1, 1, FEARG_1, NULL,
1775 ret_list_any, f_values},
1776 {"virtcol", 1, 1, FEARG_1, NULL,
1777 ret_number, f_virtcol},
1778 {"visualmode", 0, 1, 0, NULL,
1779 ret_string, f_visualmode},
1780 {"wildmenumode", 0, 0, 0, NULL,
1781 ret_number, f_wildmenumode},
1782 {"win_execute", 2, 3, FEARG_2, NULL,
1783 ret_string, f_win_execute},
1784 {"win_findbuf", 1, 1, FEARG_1, NULL,
1785 ret_list_number, f_win_findbuf},
1786 {"win_getid", 0, 2, FEARG_1, NULL,
1787 ret_number, f_win_getid},
1788 {"win_gettype", 0, 1, FEARG_1, NULL,
1789 ret_string, f_win_gettype},
1790 {"win_gotoid", 1, 1, FEARG_1, NULL,
1791 ret_number, f_win_gotoid},
1792 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1793 ret_list_number, f_win_id2tabwin},
1794 {"win_id2win", 1, 1, FEARG_1, NULL,
1795 ret_number, f_win_id2win},
1796 {"win_screenpos", 1, 1, FEARG_1, NULL,
1797 ret_list_number, f_win_screenpos},
1798 {"win_splitmove", 2, 3, FEARG_1, NULL,
1799 ret_number, f_win_splitmove},
1800 {"winbufnr", 1, 1, FEARG_1, NULL,
1801 ret_number, f_winbufnr},
1802 {"wincol", 0, 0, 0, NULL,
1803 ret_number, f_wincol},
1804 {"windowsversion", 0, 0, 0, NULL,
1805 ret_string, f_windowsversion},
1806 {"winheight", 1, 1, FEARG_1, NULL,
1807 ret_number, f_winheight},
1808 {"winlayout", 0, 1, FEARG_1, NULL,
1809 ret_list_any, f_winlayout},
1810 {"winline", 0, 0, 0, NULL,
1811 ret_number, f_winline},
1812 {"winnr", 0, 1, FEARG_1, NULL,
1813 ret_number, f_winnr},
1814 {"winrestcmd", 0, 0, 0, NULL,
1815 ret_string, f_winrestcmd},
1816 {"winrestview", 1, 1, FEARG_1, NULL,
1817 ret_void, f_winrestview},
1818 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001819 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001820 {"winwidth", 1, 1, FEARG_1, NULL,
1821 ret_number, f_winwidth},
1822 {"wordcount", 0, 0, 0, NULL,
1823 ret_dict_number, f_wordcount},
1824 {"writefile", 2, 3, FEARG_1, NULL,
1825 ret_number, f_writefile},
1826 {"xor", 2, 2, FEARG_1, NULL,
1827 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001828};
1829
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001830/*
1831 * Function given to ExpandGeneric() to obtain the list of internal
1832 * or user defined function names.
1833 */
1834 char_u *
1835get_function_name(expand_T *xp, int idx)
1836{
1837 static int intidx = -1;
1838 char_u *name;
1839
1840 if (idx == 0)
1841 intidx = -1;
1842 if (intidx < 0)
1843 {
1844 name = get_user_func_name(xp, idx);
1845 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001846 {
1847 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1848 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001849 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001850 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001851 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001852 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001853 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001854 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001855 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001856 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001857 STRCAT(IObuff, ")");
1858 return IObuff;
1859 }
1860
1861 return NULL;
1862}
1863
1864/*
1865 * Function given to ExpandGeneric() to obtain the list of internal or
1866 * user defined variable or function names.
1867 */
1868 char_u *
1869get_expr_name(expand_T *xp, int idx)
1870{
1871 static int intidx = -1;
1872 char_u *name;
1873
1874 if (idx == 0)
1875 intidx = -1;
1876 if (intidx < 0)
1877 {
1878 name = get_function_name(xp, idx);
1879 if (name != NULL)
1880 return name;
1881 }
1882 return get_user_var_name(xp, ++intidx);
1883}
1884
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001885/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001886 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001887 * Return index, or -1 if not found or "implemented" is TRUE and the function
1888 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001889 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001890 static int
1891find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001892{
1893 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001894 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001895 int cmp;
1896 int x;
1897
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001898 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001899
1900 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001901 while (first <= last)
1902 {
1903 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001904 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001905 if (cmp < 0)
1906 last = x - 1;
1907 else if (cmp > 0)
1908 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001909 else if (implemented && global_functions[x].f_func == NULL)
1910 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001911 else
1912 return x;
1913 }
1914 return -1;
1915}
1916
Bram Moolenaar15c47602020-03-26 22:16:48 +01001917/*
1918 * Find internal function "name" in table "global_functions".
1919 * Return index, or -1 if not found or the function is not implemented.
1920 */
1921 int
1922find_internal_func(char_u *name)
1923{
1924 return find_internal_func_opt(name, TRUE);
1925}
1926
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001928has_internal_func(char_u *name)
1929{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001930 return find_internal_func_opt(name, TRUE) >= 0;
1931}
1932
1933 static int
1934has_internal_func_name(char_u *name)
1935{
1936 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001937}
1938
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 char *
1940internal_func_name(int idx)
1941{
1942 return global_functions[idx].f_name;
1943}
1944
Bram Moolenaar94738d82020-10-21 14:25:07 +02001945/*
1946 * Check the argument types for builting function "idx".
1947 * Uses the list of types on the type stack: "types".
1948 * Return FAIL and gives an error message when a type is wrong.
1949 */
1950 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001951internal_func_check_arg_types(
1952 type_T **types,
1953 int idx,
1954 int argcount,
1955 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001956{
1957 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1958 int i;
1959
1960 if (argchecks != NULL)
1961 {
1962 argcontext_T context;
1963
1964 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001965 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001966 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001967 for (i = 0; i < argcount; ++i)
1968 if (argchecks[i] != NULL)
1969 {
1970 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001971 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001972 return FAIL;
1973 }
1974 }
1975 return OK;
1976}
1977
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001978/*
1979 * Call the "f_retfunc" function to obtain the return type of function "idx".
1980 * "argtypes" is the list of argument types or NULL when there are no
1981 * arguments.
1982 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001984internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001986 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987}
1988
1989/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001990 * Return TRUE if "idx" is for the map() function.
1991 */
1992 int
1993internal_func_is_map(int idx)
1994{
1995 return global_functions[idx].f_func == f_map;
1996}
1997
1998/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002000 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2001 * first argument, 2 if method base is second argument, etc. 9 if method base
2002 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 */
2004 int
2005check_internal_func(int idx, int argcount)
2006{
2007 int res;
2008 char *name;
2009
2010 if (argcount < global_functions[idx].f_min_argc)
2011 res = FCERR_TOOFEW;
2012 else if (argcount > global_functions[idx].f_max_argc)
2013 res = FCERR_TOOMANY;
2014 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002015 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016
2017 name = internal_func_name(idx);
2018 if (res == FCERR_TOOMANY)
2019 semsg(_(e_toomanyarg), name);
2020 else
2021 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002022 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023}
2024
Bram Moolenaarac92e252019-08-03 21:58:38 +02002025 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002026call_internal_func(
2027 char_u *name,
2028 int argcount,
2029 typval_T *argvars,
2030 typval_T *rettv)
2031{
2032 int i;
2033
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002034 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002035 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002036 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002037 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002038 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002039 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002040 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002041 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002042 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002043 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002044}
2045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 void
2047call_internal_func_by_idx(
2048 int idx,
2049 typval_T *argvars,
2050 typval_T *rettv)
2051{
2052 global_functions[idx].f_func(argvars, rettv);
2053}
2054
Bram Moolenaarac92e252019-08-03 21:58:38 +02002055/*
2056 * Invoke a method for base->method().
2057 */
2058 int
2059call_internal_method(
2060 char_u *name,
2061 int argcount,
2062 typval_T *argvars,
2063 typval_T *rettv,
2064 typval_T *basetv)
2065{
2066 int i;
2067 int fi;
2068 typval_T argv[MAX_FUNC_ARGS + 1];
2069
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002070 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002071 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002072 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002073 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002074 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002075 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002076 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002077 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002078 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002079
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002080 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002081 {
2082 // base value goes last
2083 for (i = 0; i < argcount; ++i)
2084 argv[i] = argvars[i];
2085 argv[argcount] = *basetv;
2086 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002087 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002088 {
2089 // base value goes second
2090 argv[0] = argvars[0];
2091 argv[1] = *basetv;
2092 for (i = 1; i < argcount; ++i)
2093 argv[i + 1] = argvars[i];
2094 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002095 else if (global_functions[fi].f_argtype == FEARG_3)
2096 {
2097 // base value goes third
2098 argv[0] = argvars[0];
2099 argv[1] = argvars[1];
2100 argv[2] = *basetv;
2101 for (i = 2; i < argcount; ++i)
2102 argv[i + 1] = argvars[i];
2103 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002104 else if (global_functions[fi].f_argtype == FEARG_4)
2105 {
2106 // base value goes fourth
2107 argv[0] = argvars[0];
2108 argv[1] = argvars[1];
2109 argv[2] = argvars[2];
2110 argv[3] = *basetv;
2111 for (i = 3; i < argcount; ++i)
2112 argv[i + 1] = argvars[i];
2113 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002114 else
2115 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002116 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002117 argv[0] = *basetv;
2118 for (i = 0; i < argcount; ++i)
2119 argv[i + 1] = argvars[i];
2120 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002121 argv[argcount + 1].v_type = VAR_UNKNOWN;
2122
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002123 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002124 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002125}
2126
2127/*
2128 * Return TRUE for a non-zero Number and a non-empty String.
2129 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002130 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002131non_zero_arg(typval_T *argvars)
2132{
2133 return ((argvars[0].v_type == VAR_NUMBER
2134 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002135 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002136 && argvars[0].vval.v_number == VVAL_TRUE)
2137 || (argvars[0].v_type == VAR_STRING
2138 && argvars[0].vval.v_string != NULL
2139 && *argvars[0].vval.v_string != NUL));
2140}
2141
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002142#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002143/*
2144 * Get the float value of "argvars[0]" into "f".
2145 * Returns FAIL when the argument is not a Number or Float.
2146 */
2147 static int
2148get_float_arg(typval_T *argvars, float_T *f)
2149{
2150 if (argvars[0].v_type == VAR_FLOAT)
2151 {
2152 *f = argvars[0].vval.v_float;
2153 return OK;
2154 }
2155 if (argvars[0].v_type == VAR_NUMBER)
2156 {
2157 *f = (float_T)argvars[0].vval.v_number;
2158 return OK;
2159 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002160 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002161 return FAIL;
2162}
2163
2164/*
2165 * "abs(expr)" function
2166 */
2167 static void
2168f_abs(typval_T *argvars, typval_T *rettv)
2169{
2170 if (argvars[0].v_type == VAR_FLOAT)
2171 {
2172 rettv->v_type = VAR_FLOAT;
2173 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
2174 }
2175 else
2176 {
2177 varnumber_T n;
2178 int error = FALSE;
2179
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002180 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002181 if (error)
2182 rettv->vval.v_number = -1;
2183 else if (n > 0)
2184 rettv->vval.v_number = n;
2185 else
2186 rettv->vval.v_number = -n;
2187 }
2188}
2189
2190/*
2191 * "acos()" function
2192 */
2193 static void
2194f_acos(typval_T *argvars, typval_T *rettv)
2195{
2196 float_T f = 0.0;
2197
2198 rettv->v_type = VAR_FLOAT;
2199 if (get_float_arg(argvars, &f) == OK)
2200 rettv->vval.v_float = acos(f);
2201 else
2202 rettv->vval.v_float = 0.0;
2203}
2204#endif
2205
2206/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002207 * "and(expr, expr)" function
2208 */
2209 static void
2210f_and(typval_T *argvars, typval_T *rettv)
2211{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002212 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2213 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002214}
2215
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002216#ifdef FEAT_FLOAT
2217/*
2218 * "asin()" function
2219 */
2220 static void
2221f_asin(typval_T *argvars, typval_T *rettv)
2222{
2223 float_T f = 0.0;
2224
2225 rettv->v_type = VAR_FLOAT;
2226 if (get_float_arg(argvars, &f) == OK)
2227 rettv->vval.v_float = asin(f);
2228 else
2229 rettv->vval.v_float = 0.0;
2230}
2231
2232/*
2233 * "atan()" function
2234 */
2235 static void
2236f_atan(typval_T *argvars, typval_T *rettv)
2237{
2238 float_T f = 0.0;
2239
2240 rettv->v_type = VAR_FLOAT;
2241 if (get_float_arg(argvars, &f) == OK)
2242 rettv->vval.v_float = atan(f);
2243 else
2244 rettv->vval.v_float = 0.0;
2245}
2246
2247/*
2248 * "atan2()" function
2249 */
2250 static void
2251f_atan2(typval_T *argvars, typval_T *rettv)
2252{
2253 float_T fx = 0.0, fy = 0.0;
2254
2255 rettv->v_type = VAR_FLOAT;
2256 if (get_float_arg(argvars, &fx) == OK
2257 && get_float_arg(&argvars[1], &fy) == OK)
2258 rettv->vval.v_float = atan2(fx, fy);
2259 else
2260 rettv->vval.v_float = 0.0;
2261}
2262#endif
2263
2264/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002265 * "balloon_show()" function
2266 */
2267#ifdef FEAT_BEVAL
2268 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002269f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2270{
2271 rettv->v_type = VAR_STRING;
2272 if (balloonEval != NULL)
2273 {
2274 if (balloonEval->msg == NULL)
2275 rettv->vval.v_string = NULL;
2276 else
2277 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2278 }
2279}
2280
2281 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002282f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2283{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002284 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002285 {
2286 if (argvars[0].v_type == VAR_LIST
2287# ifdef FEAT_GUI
2288 && !gui.in_use
2289# endif
2290 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002291 {
2292 list_T *l = argvars[0].vval.v_list;
2293
2294 // empty list removes the balloon
2295 post_balloon(balloonEval, NULL,
2296 l == NULL || l->lv_len == 0 ? NULL : l);
2297 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002298 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002299 {
2300 char_u *mesg = tv_get_string_chk(&argvars[0]);
2301
2302 if (mesg != NULL)
2303 // empty string removes the balloon
2304 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2305 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002306 }
2307}
2308
Bram Moolenaar669a8282017-11-19 20:13:05 +01002309# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002310 static void
2311f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2312{
2313 if (rettv_list_alloc(rettv) == OK)
2314 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002315 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002316
2317 if (msg != NULL)
2318 {
2319 pumitem_T *array;
2320 int size = split_message(msg, &array);
2321 int i;
2322
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002323 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002324 for (i = 1; i < size - 1; ++i)
2325 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002326 while (size > 0)
2327 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002328 vim_free(array);
2329 }
2330 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002331}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002332# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002333#endif
2334
2335/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002336 * Get the buffer from "arg" and give an error and return NULL if it is not
2337 * valid.
2338 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002339 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002340get_buf_arg(typval_T *arg)
2341{
2342 buf_T *buf;
2343
2344 ++emsg_off;
2345 buf = tv_get_buf(arg, FALSE);
2346 --emsg_off;
2347 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002348 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002349 return buf;
2350}
2351
2352/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002353 * "byte2line(byte)" function
2354 */
2355 static void
2356f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2357{
2358#ifndef FEAT_BYTEOFF
2359 rettv->vval.v_number = -1;
2360#else
2361 long boff = 0;
2362
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002363 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002364 if (boff < 0)
2365 rettv->vval.v_number = -1;
2366 else
2367 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2368 (linenr_T)0, &boff);
2369#endif
2370}
2371
2372 static void
2373byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2374{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002375 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002376 char_u *str;
2377 varnumber_T idx;
2378
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002379 str = tv_get_string_chk(&argvars[0]);
2380 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002381 rettv->vval.v_number = -1;
2382 if (str == NULL || idx < 0)
2383 return;
2384
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002385 t = str;
2386 for ( ; idx > 0; idx--)
2387 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002388 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002389 return;
2390 if (enc_utf8 && comp)
2391 t += utf_ptr2len(t);
2392 else
2393 t += (*mb_ptr2len)(t);
2394 }
2395 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002396}
2397
2398/*
2399 * "byteidx()" function
2400 */
2401 static void
2402f_byteidx(typval_T *argvars, typval_T *rettv)
2403{
2404 byteidx(argvars, rettv, FALSE);
2405}
2406
2407/*
2408 * "byteidxcomp()" function
2409 */
2410 static void
2411f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2412{
2413 byteidx(argvars, rettv, TRUE);
2414}
2415
2416/*
2417 * "call(func, arglist [, dict])" function
2418 */
2419 static void
2420f_call(typval_T *argvars, typval_T *rettv)
2421{
2422 char_u *func;
2423 partial_T *partial = NULL;
2424 dict_T *selfdict = NULL;
2425
2426 if (argvars[1].v_type != VAR_LIST)
2427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002428 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002429 return;
2430 }
2431 if (argvars[1].vval.v_list == NULL)
2432 return;
2433
2434 if (argvars[0].v_type == VAR_FUNC)
2435 func = argvars[0].vval.v_string;
2436 else if (argvars[0].v_type == VAR_PARTIAL)
2437 {
2438 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002439 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002440 }
2441 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002442 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002443 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002444 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002445
2446 if (argvars[2].v_type != VAR_UNKNOWN)
2447 {
2448 if (argvars[2].v_type != VAR_DICT)
2449 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002450 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002451 return;
2452 }
2453 selfdict = argvars[2].vval.v_dict;
2454 }
2455
2456 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2457}
2458
2459#ifdef FEAT_FLOAT
2460/*
2461 * "ceil({float})" function
2462 */
2463 static void
2464f_ceil(typval_T *argvars, typval_T *rettv)
2465{
2466 float_T f = 0.0;
2467
2468 rettv->v_type = VAR_FLOAT;
2469 if (get_float_arg(argvars, &f) == OK)
2470 rettv->vval.v_float = ceil(f);
2471 else
2472 rettv->vval.v_float = 0.0;
2473}
2474#endif
2475
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002476/*
2477 * "changenr()" function
2478 */
2479 static void
2480f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2481{
2482 rettv->vval.v_number = curbuf->b_u_seq_cur;
2483}
2484
2485/*
2486 * "char2nr(string)" function
2487 */
2488 static void
2489f_char2nr(typval_T *argvars, typval_T *rettv)
2490{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002491 if (has_mbyte)
2492 {
2493 int utf8 = 0;
2494
2495 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002496 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002497
2498 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002499 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002500 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002501 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002502 }
2503 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002504 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002505}
2506
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002507/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002508 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2509 * returns the character index of the column. Otherwise, returns the byte index
2510 * of the column.
2511 */
2512 static void
2513get_col(typval_T *argvars, typval_T *rettv, int charcol)
2514{
2515 colnr_T col = 0;
2516 pos_T *fp;
2517 int fnum = curbuf->b_fnum;
2518
2519 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2520 if (fp != NULL && fnum == curbuf->b_fnum)
2521 {
2522 if (fp->col == MAXCOL)
2523 {
2524 // '> can be MAXCOL, get the length of the line then
2525 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2526 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2527 else
2528 col = MAXCOL;
2529 }
2530 else
2531 {
2532 col = fp->col + 1;
2533 // col(".") when the cursor is on the NUL at the end of the line
2534 // because of "coladd" can be seen as an extra column.
2535 if (virtual_active() && fp == &curwin->w_cursor)
2536 {
2537 char_u *p = ml_get_cursor();
2538
2539 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2540 curwin->w_virtcol - curwin->w_cursor.coladd))
2541 {
2542 int l;
2543
2544 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2545 col += l;
2546 }
2547 }
2548 }
2549 }
2550 rettv->vval.v_number = col;
2551}
2552
2553/*
2554 * "charcol()" function
2555 */
2556 static void
2557f_charcol(typval_T *argvars, typval_T *rettv)
2558{
2559 get_col(argvars, rettv, TRUE);
2560}
2561
2562/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002563 * "charidx()" function
2564 */
2565 static void
2566f_charidx(typval_T *argvars, typval_T *rettv)
2567{
2568 char_u *str;
2569 varnumber_T idx;
2570 int countcc = FALSE;
2571 char_u *p;
2572 int len;
2573 int (*ptr2len)(char_u *);
2574
2575 rettv->vval.v_number = -1;
2576
2577 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2578 || (argvars[2].v_type != VAR_UNKNOWN
2579 && argvars[2].v_type != VAR_NUMBER))
2580 {
2581 emsg(_(e_invarg));
2582 return;
2583 }
2584
2585 str = tv_get_string_chk(&argvars[0]);
2586 idx = tv_get_number_chk(&argvars[1], NULL);
2587 if (str == NULL || idx < 0)
2588 return;
2589
2590 if (argvars[2].v_type != VAR_UNKNOWN)
2591 countcc = (int)tv_get_bool(&argvars[2]);
2592 if (countcc < 0 || countcc > 1)
2593 {
2594 semsg(_(e_using_number_as_bool_nr), countcc);
2595 return;
2596 }
2597
2598 if (enc_utf8 && countcc)
2599 ptr2len = utf_ptr2len;
2600 else
2601 ptr2len = mb_ptr2len;
2602
2603 for (p = str, len = 0; p <= str + idx; len++)
2604 {
2605 if (*p == NUL)
2606 return;
2607 p += ptr2len(p);
2608 }
2609
2610 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2611}
2612
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002613 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002614get_optional_window(typval_T *argvars, int idx)
2615{
2616 win_T *win = curwin;
2617
2618 if (argvars[idx].v_type != VAR_UNKNOWN)
2619 {
2620 win = find_win_by_nr_or_id(&argvars[idx]);
2621 if (win == NULL)
2622 {
2623 emsg(_(e_invalwindow));
2624 return NULL;
2625 }
2626 }
2627 return win;
2628}
2629
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002630/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002631 * "col(string)" function
2632 */
2633 static void
2634f_col(typval_T *argvars, typval_T *rettv)
2635{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002636 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002637}
2638
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002639/*
2640 * "confirm(message, buttons[, default [, type]])" function
2641 */
2642 static void
2643f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2644{
2645#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2646 char_u *message;
2647 char_u *buttons = NULL;
2648 char_u buf[NUMBUFLEN];
2649 char_u buf2[NUMBUFLEN];
2650 int def = 1;
2651 int type = VIM_GENERIC;
2652 char_u *typestr;
2653 int error = FALSE;
2654
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002655 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002656 if (message == NULL)
2657 error = TRUE;
2658 if (argvars[1].v_type != VAR_UNKNOWN)
2659 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002660 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002661 if (buttons == NULL)
2662 error = TRUE;
2663 if (argvars[2].v_type != VAR_UNKNOWN)
2664 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002665 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002666 if (argvars[3].v_type != VAR_UNKNOWN)
2667 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002668 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002669 if (typestr == NULL)
2670 error = TRUE;
2671 else
2672 {
2673 switch (TOUPPER_ASC(*typestr))
2674 {
2675 case 'E': type = VIM_ERROR; break;
2676 case 'Q': type = VIM_QUESTION; break;
2677 case 'I': type = VIM_INFO; break;
2678 case 'W': type = VIM_WARNING; break;
2679 case 'G': type = VIM_GENERIC; break;
2680 }
2681 }
2682 }
2683 }
2684 }
2685
2686 if (buttons == NULL || *buttons == NUL)
2687 buttons = (char_u *)_("&Ok");
2688
2689 if (!error)
2690 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2691 def, NULL, FALSE);
2692#endif
2693}
2694
2695/*
2696 * "copy()" function
2697 */
2698 static void
2699f_copy(typval_T *argvars, typval_T *rettv)
2700{
2701 item_copy(&argvars[0], rettv, FALSE, 0);
2702}
2703
2704#ifdef FEAT_FLOAT
2705/*
2706 * "cos()" function
2707 */
2708 static void
2709f_cos(typval_T *argvars, typval_T *rettv)
2710{
2711 float_T f = 0.0;
2712
2713 rettv->v_type = VAR_FLOAT;
2714 if (get_float_arg(argvars, &f) == OK)
2715 rettv->vval.v_float = cos(f);
2716 else
2717 rettv->vval.v_float = 0.0;
2718}
2719
2720/*
2721 * "cosh()" function
2722 */
2723 static void
2724f_cosh(typval_T *argvars, typval_T *rettv)
2725{
2726 float_T f = 0.0;
2727
2728 rettv->v_type = VAR_FLOAT;
2729 if (get_float_arg(argvars, &f) == OK)
2730 rettv->vval.v_float = cosh(f);
2731 else
2732 rettv->vval.v_float = 0.0;
2733}
2734#endif
2735
2736/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002737 * Set the cursor position.
2738 * If 'charcol' is TRUE, then use the column number as a character offet.
2739 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002740 */
2741 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002742set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002743{
2744 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002745 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002746 int set_curswant = TRUE;
2747
2748 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002749 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002750 {
2751 pos_T pos;
2752 colnr_T curswant = -1;
2753
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002754 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002755 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002756 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002757 return;
2758 }
2759 line = pos.lnum;
2760 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002761 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002762 if (curswant >= 0)
2763 {
2764 curwin->w_curswant = curswant - 1;
2765 set_curswant = FALSE;
2766 }
2767 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002768 else if ((argvars[0].v_type == VAR_NUMBER ||
2769 argvars[0].v_type == VAR_STRING)
2770 && argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002771 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002772 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002773 if (line < 0)
2774 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002775 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002776 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002777 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002778 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002779 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002780 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002781 else
2782 {
2783 emsg(_(e_invarg));
2784 return;
2785 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002786 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002787 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002788 if (line > 0)
2789 curwin->w_cursor.lnum = line;
2790 if (col > 0)
2791 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002792 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002793
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002794 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002795 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002796 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002797 if (has_mbyte)
2798 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002799
2800 curwin->w_set_curswant = set_curswant;
2801 rettv->vval.v_number = 0;
2802}
2803
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002804/*
2805 * "cursor(lnum, col)" function, or
2806 * "cursor(list)"
2807 *
2808 * Moves the cursor to the specified line and column.
2809 * Returns 0 when the position could be set, -1 otherwise.
2810 */
2811 static void
2812f_cursor(typval_T *argvars, typval_T *rettv)
2813{
2814 set_cursorpos(argvars, rettv, FALSE);
2815}
2816
Bram Moolenaar4f974752019-02-17 17:44:42 +01002817#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002818/*
2819 * "debugbreak()" function
2820 */
2821 static void
2822f_debugbreak(typval_T *argvars, typval_T *rettv)
2823{
2824 int pid;
2825
2826 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002827 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002828 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002829 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002830 else
2831 {
2832 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2833
2834 if (hProcess != NULL)
2835 {
2836 DebugBreakProcess(hProcess);
2837 CloseHandle(hProcess);
2838 rettv->vval.v_number = OK;
2839 }
2840 }
2841}
2842#endif
2843
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002844/*
2845 * "deepcopy()" function
2846 */
2847 static void
2848f_deepcopy(typval_T *argvars, typval_T *rettv)
2849{
2850 int noref = 0;
2851 int copyID;
2852
2853 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar44b4a242020-09-05 17:18:28 +02002854 noref = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002855 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002856 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002857 else
2858 {
2859 copyID = get_copyID();
2860 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2861 }
2862}
2863
2864/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002865 * "did_filetype()" function
2866 */
2867 static void
2868f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2869{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002870 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002871}
2872
2873/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002874 * "echoraw({expr})" function
2875 */
2876 static void
2877f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2878{
2879 char_u *str = tv_get_string_chk(&argvars[0]);
2880
2881 if (str != NULL && *str != NUL)
2882 {
2883 out_str(str);
2884 out_flush();
2885 }
2886}
2887
2888/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002889 * "empty({expr})" function
2890 */
2891 static void
2892f_empty(typval_T *argvars, typval_T *rettv)
2893{
2894 int n = FALSE;
2895
2896 switch (argvars[0].v_type)
2897 {
2898 case VAR_STRING:
2899 case VAR_FUNC:
2900 n = argvars[0].vval.v_string == NULL
2901 || *argvars[0].vval.v_string == NUL;
2902 break;
2903 case VAR_PARTIAL:
2904 n = FALSE;
2905 break;
2906 case VAR_NUMBER:
2907 n = argvars[0].vval.v_number == 0;
2908 break;
2909 case VAR_FLOAT:
2910#ifdef FEAT_FLOAT
2911 n = argvars[0].vval.v_float == 0.0;
2912 break;
2913#endif
2914 case VAR_LIST:
2915 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002916 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002917 break;
2918 case VAR_DICT:
2919 n = argvars[0].vval.v_dict == NULL
2920 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2921 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002922 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002923 case VAR_SPECIAL:
2924 n = argvars[0].vval.v_number != VVAL_TRUE;
2925 break;
2926
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002927 case VAR_BLOB:
2928 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002929 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2930 break;
2931
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002932 case VAR_JOB:
2933#ifdef FEAT_JOB_CHANNEL
2934 n = argvars[0].vval.v_job == NULL
2935 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2936 break;
2937#endif
2938 case VAR_CHANNEL:
2939#ifdef FEAT_JOB_CHANNEL
2940 n = argvars[0].vval.v_channel == NULL
2941 || !channel_is_open(argvars[0].vval.v_channel);
2942 break;
2943#endif
2944 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002945 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002947 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002948 n = TRUE;
2949 break;
2950 }
2951
2952 rettv->vval.v_number = n;
2953}
2954
2955/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002956 * "environ()" function
2957 */
2958 static void
2959f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2960{
2961#if !defined(AMIGA)
2962 int i = 0;
2963 char_u *entry, *value;
2964# ifdef MSWIN
2965 extern wchar_t **_wenviron;
2966# else
2967 extern char **environ;
2968# endif
2969
2970 if (rettv_dict_alloc(rettv) != OK)
2971 return;
2972
2973# ifdef MSWIN
2974 if (*_wenviron == NULL)
2975 return;
2976# else
2977 if (*environ == NULL)
2978 return;
2979# endif
2980
2981 for (i = 0; ; ++i)
2982 {
2983# ifdef MSWIN
2984 short_u *p;
2985
2986 if ((p = (short_u *)_wenviron[i]) == NULL)
2987 return;
2988 entry = utf16_to_enc(p, NULL);
2989# else
2990 if ((entry = (char_u *)environ[i]) == NULL)
2991 return;
2992 entry = vim_strsave(entry);
2993# endif
2994 if (entry == NULL) // out of memory
2995 return;
2996 if ((value = vim_strchr(entry, '=')) == NULL)
2997 {
2998 vim_free(entry);
2999 continue;
3000 }
3001 *value++ = NUL;
3002 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3003 vim_free(entry);
3004 }
3005#endif
3006}
3007
3008/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003009 * "escape({string}, {chars})" function
3010 */
3011 static void
3012f_escape(typval_T *argvars, typval_T *rettv)
3013{
3014 char_u buf[NUMBUFLEN];
3015
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003016 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3017 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003018 rettv->v_type = VAR_STRING;
3019}
3020
3021/*
3022 * "eval()" function
3023 */
3024 static void
3025f_eval(typval_T *argvars, typval_T *rettv)
3026{
3027 char_u *s, *p;
3028
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003029 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003030 if (s != NULL)
3031 s = skipwhite(s);
3032
3033 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003034 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003035 {
3036 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003037 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003038 need_clr_eos = FALSE;
3039 rettv->v_type = VAR_NUMBER;
3040 rettv->vval.v_number = 0;
3041 }
3042 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003043 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003044}
3045
3046/*
3047 * "eventhandler()" function
3048 */
3049 static void
3050f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3051{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003052 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003053}
3054
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003055static garray_T redir_execute_ga;
3056
3057/*
3058 * Append "value[value_len]" to the execute() output.
3059 */
3060 void
3061execute_redir_str(char_u *value, int value_len)
3062{
3063 int len;
3064
3065 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003066 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003067 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003068 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003069 if (ga_grow(&redir_execute_ga, len) == OK)
3070 {
3071 mch_memmove((char *)redir_execute_ga.ga_data
3072 + redir_execute_ga.ga_len, value, len);
3073 redir_execute_ga.ga_len += len;
3074 }
3075}
3076
3077/*
3078 * Get next line from a list.
3079 * Called by do_cmdline() to get the next line.
3080 * Returns allocated string, or NULL for end of function.
3081 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003082 static char_u *
3083get_list_line(
3084 int c UNUSED,
3085 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003086 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02003087 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003088{
3089 listitem_T **p = (listitem_T **)cookie;
3090 listitem_T *item = *p;
3091 char_u buf[NUMBUFLEN];
3092 char_u *s;
3093
3094 if (item == NULL)
3095 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003096 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003097 *p = item->li_next;
3098 return s == NULL ? NULL : vim_strsave(s);
3099}
3100
3101/*
3102 * "execute()" function
3103 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02003104 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003105execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003106{
3107 char_u *cmd = NULL;
3108 list_T *list = NULL;
3109 int save_msg_silent = msg_silent;
3110 int save_emsg_silent = emsg_silent;
3111 int save_emsg_noredir = emsg_noredir;
3112 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003113 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003114 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003115 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003116 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003117
3118 rettv->vval.v_string = NULL;
3119 rettv->v_type = VAR_STRING;
3120
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003121 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003122 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003123 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003124 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003125 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003126 return;
3127 ++list->lv_refcount;
3128 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003129 else if (argvars[arg_off].v_type == VAR_JOB
3130 || argvars[arg_off].v_type == VAR_CHANNEL)
3131 {
3132 emsg(_(e_inval_string));
3133 return;
3134 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003135 else
3136 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003137 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003138 if (cmd == NULL)
3139 return;
3140 }
3141
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003142 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003143 {
3144 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003145 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003146
3147 if (s == NULL)
3148 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003149 if (*s == NUL)
3150 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003151 if (STRNCMP(s, "silent", 6) == 0)
3152 ++msg_silent;
3153 if (STRCMP(s, "silent!") == 0)
3154 {
3155 emsg_silent = TRUE;
3156 emsg_noredir = TRUE;
3157 }
3158 }
3159 else
3160 ++msg_silent;
3161
3162 if (redir_execute)
3163 save_ga = redir_execute_ga;
3164 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3165 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003166 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003167 if (!echo_output)
3168 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003169
3170 if (cmd != NULL)
3171 do_cmdline_cmd(cmd);
3172 else
3173 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003174 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003175
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003176 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003177 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003178 do_cmdline(NULL, get_list_line, (void *)&item,
3179 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3180 --list->lv_refcount;
3181 }
3182
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003183 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003184 if (ga_grow(&redir_execute_ga, 1) == OK)
3185 {
3186 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3187 rettv->vval.v_string = redir_execute_ga.ga_data;
3188 }
3189 else
3190 {
3191 ga_clear(&redir_execute_ga);
3192 rettv->vval.v_string = NULL;
3193 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003194 msg_silent = save_msg_silent;
3195 emsg_silent = save_emsg_silent;
3196 emsg_noredir = save_emsg_noredir;
3197
3198 redir_execute = save_redir_execute;
3199 if (redir_execute)
3200 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003201 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003202
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003203 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003204 if (echo_output)
3205 // When not working silently: put it in column zero. A following
3206 // "echon" will overwrite the message, unavoidably.
3207 msg_col = 0;
3208 else
3209 // When working silently: Put it back where it was, since nothing
3210 // should have been written.
3211 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003212}
3213
3214/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003215 * "execute()" function
3216 */
3217 static void
3218f_execute(typval_T *argvars, typval_T *rettv)
3219{
3220 execute_common(argvars, rettv, 0);
3221}
3222
3223/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003224 * "exists()" function
3225 */
3226 static void
3227f_exists(typval_T *argvars, typval_T *rettv)
3228{
3229 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003230 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003231
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003232 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003233 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003234 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003235 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003236 if (mch_getenv(p + 1) != NULL)
3237 n = TRUE;
3238 else
3239 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003240 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003241 p = expand_env_save(p);
3242 if (p != NULL && *p != '$')
3243 n = TRUE;
3244 vim_free(p);
3245 }
3246 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003247 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003248 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003249 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003250 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003251 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003252 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003253 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003254 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003255 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003256 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003257 else if (*p == '?') // internal function only
3258 {
3259 n = has_internal_func_name(p + 1);
3260 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003261 else if (*p == ':')
3262 {
3263 n = cmd_exists(p + 1);
3264 }
3265 else if (*p == '#')
3266 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003267 if (p[1] == '#')
3268 n = autocmd_supported(p + 2);
3269 else
3270 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003271 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003272 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003273 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003274 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003275 }
3276
3277 rettv->vval.v_number = n;
3278}
3279
3280#ifdef FEAT_FLOAT
3281/*
3282 * "exp()" function
3283 */
3284 static void
3285f_exp(typval_T *argvars, typval_T *rettv)
3286{
3287 float_T f = 0.0;
3288
3289 rettv->v_type = VAR_FLOAT;
3290 if (get_float_arg(argvars, &f) == OK)
3291 rettv->vval.v_float = exp(f);
3292 else
3293 rettv->vval.v_float = 0.0;
3294}
3295#endif
3296
3297/*
3298 * "expand()" function
3299 */
3300 static void
3301f_expand(typval_T *argvars, typval_T *rettv)
3302{
3303 char_u *s;
3304 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003305 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003306 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3307 expand_T xpc;
3308 int error = FALSE;
3309 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003310#ifdef BACKSLASH_IN_FILENAME
3311 char_u *p_csl_save = p_csl;
3312
3313 // avoid using 'completeslash' here
3314 p_csl = empty_option;
3315#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003316
3317 rettv->v_type = VAR_STRING;
3318 if (argvars[1].v_type != VAR_UNKNOWN
3319 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003320 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003321 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003322 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003323
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003324 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003325 if (*s == '%' || *s == '#' || *s == '<')
3326 {
3327 ++emsg_off;
3328 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3329 --emsg_off;
3330 if (rettv->v_type == VAR_LIST)
3331 {
3332 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3333 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003334 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003335 }
3336 else
3337 rettv->vval.v_string = result;
3338 }
3339 else
3340 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003341 // When the optional second argument is non-zero, don't remove matches
3342 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003343 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003344 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003345 options |= WILD_KEEP_ALL;
3346 if (!error)
3347 {
3348 ExpandInit(&xpc);
3349 xpc.xp_context = EXPAND_FILES;
3350 if (p_wic)
3351 options += WILD_ICASE;
3352 if (rettv->v_type == VAR_STRING)
3353 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3354 options, WILD_ALL);
3355 else if (rettv_list_alloc(rettv) != FAIL)
3356 {
3357 int i;
3358
3359 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3360 for (i = 0; i < xpc.xp_numfiles; i++)
3361 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3362 ExpandCleanup(&xpc);
3363 }
3364 }
3365 else
3366 rettv->vval.v_string = NULL;
3367 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003368#ifdef BACKSLASH_IN_FILENAME
3369 p_csl = p_csl_save;
3370#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003371}
3372
3373/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003374 * "expandcmd()" function
3375 * Expand all the special characters in a command string.
3376 */
3377 static void
3378f_expandcmd(typval_T *argvars, typval_T *rettv)
3379{
3380 exarg_T eap;
3381 char_u *cmdstr;
3382 char *errormsg = NULL;
3383
3384 rettv->v_type = VAR_STRING;
3385 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3386
3387 memset(&eap, 0, sizeof(eap));
3388 eap.cmd = cmdstr;
3389 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003390 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003391 eap.usefilter = FALSE;
3392 eap.nextcmd = NULL;
3393 eap.cmdidx = CMD_USER;
3394
3395 expand_filename(&eap, &cmdstr, &errormsg);
3396 if (errormsg != NULL && *errormsg != NUL)
3397 emsg(errormsg);
3398
3399 rettv->vval.v_string = cmdstr;
3400}
3401
3402/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003403 * "feedkeys()" function
3404 */
3405 static void
3406f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3407{
3408 int remap = TRUE;
3409 int insert = FALSE;
3410 char_u *keys, *flags;
3411 char_u nbuf[NUMBUFLEN];
3412 int typed = FALSE;
3413 int execute = FALSE;
3414 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003415 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003416 char_u *keys_esc;
3417
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003418 // This is not allowed in the sandbox. If the commands would still be
3419 // executed in the sandbox it would be OK, but it probably happens later,
3420 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003421 if (check_secure())
3422 return;
3423
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003424 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003425
3426 if (argvars[1].v_type != VAR_UNKNOWN)
3427 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003428 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003429 for ( ; *flags != NUL; ++flags)
3430 {
3431 switch (*flags)
3432 {
3433 case 'n': remap = FALSE; break;
3434 case 'm': remap = TRUE; break;
3435 case 't': typed = TRUE; break;
3436 case 'i': insert = TRUE; break;
3437 case 'x': execute = TRUE; break;
3438 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003439 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003440 }
3441 }
3442 }
3443
3444 if (*keys != NUL || execute)
3445 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003446 // Need to escape K_SPECIAL and CSI before putting the string in the
3447 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003448 keys_esc = vim_strsave_escape_csi(keys);
3449 if (keys_esc != NULL)
3450 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003451 if (lowlevel)
3452 {
3453#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003454 int idx;
3455 int len = (int)STRLEN(keys);
3456
3457 for (idx = 0; idx < len; ++idx)
3458 {
3459 // if a CTRL-C was typed, set got_int, similar to what
3460 // happens in fill_input_buf()
3461 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3462 got_int = TRUE;
3463 add_to_input_buf(keys + idx, 1);
3464 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003465#else
3466 emsg(_("E980: lowlevel input not supported"));
3467#endif
3468 }
3469 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003470 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003471 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003472 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003473 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003474#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003475 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003476#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003477 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003478 typebuf_was_filled = TRUE;
3479 }
3480 vim_free(keys_esc);
3481
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003482 if (execute)
3483 {
3484 int save_msg_scroll = msg_scroll;
3485
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003486 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003487 msg_scroll = FALSE;
3488
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003489 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003490 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003491 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003492 ++in_feedkeys;
3493 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003494 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003495 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003496 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003497 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003498 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003499 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003500
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003501 msg_scroll |= save_msg_scroll;
3502 }
3503 }
3504 }
3505}
3506
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003507#ifdef FEAT_FLOAT
3508/*
3509 * "float2nr({float})" function
3510 */
3511 static void
3512f_float2nr(typval_T *argvars, typval_T *rettv)
3513{
3514 float_T f = 0.0;
3515
3516 if (get_float_arg(argvars, &f) == OK)
3517 {
Bram Moolenaar37184272020-05-23 19:30:05 +02003518 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003519 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02003520 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003521 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003522 else
3523 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003524 }
3525}
3526
3527/*
3528 * "floor({float})" function
3529 */
3530 static void
3531f_floor(typval_T *argvars, typval_T *rettv)
3532{
3533 float_T f = 0.0;
3534
3535 rettv->v_type = VAR_FLOAT;
3536 if (get_float_arg(argvars, &f) == OK)
3537 rettv->vval.v_float = floor(f);
3538 else
3539 rettv->vval.v_float = 0.0;
3540}
3541
3542/*
3543 * "fmod()" function
3544 */
3545 static void
3546f_fmod(typval_T *argvars, typval_T *rettv)
3547{
3548 float_T fx = 0.0, fy = 0.0;
3549
3550 rettv->v_type = VAR_FLOAT;
3551 if (get_float_arg(argvars, &fx) == OK
3552 && get_float_arg(&argvars[1], &fy) == OK)
3553 rettv->vval.v_float = fmod(fx, fy);
3554 else
3555 rettv->vval.v_float = 0.0;
3556}
3557#endif
3558
3559/*
3560 * "fnameescape({string})" function
3561 */
3562 static void
3563f_fnameescape(typval_T *argvars, typval_T *rettv)
3564{
3565 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003566 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003567 rettv->v_type = VAR_STRING;
3568}
3569
3570/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003571 * "foreground()" function
3572 */
3573 static void
3574f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3575{
3576#ifdef FEAT_GUI
3577 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003578 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003579 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003580 return;
3581 }
3582#endif
3583#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003584 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003585#endif
3586}
3587
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003588 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003589common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003590{
3591 char_u *s;
3592 char_u *name;
3593 int use_string = FALSE;
3594 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003595 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003596 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003597
3598 if (argvars[0].v_type == VAR_FUNC)
3599 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003600 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003601 s = argvars[0].vval.v_string;
3602 }
3603 else if (argvars[0].v_type == VAR_PARTIAL
3604 && argvars[0].vval.v_partial != NULL)
3605 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003606 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003607 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003608 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003609 }
3610 else
3611 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003612 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003613 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003614 use_string = TRUE;
3615 }
3616
Bram Moolenaar843b8842016-08-21 14:36:15 +02003617 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003618 {
3619 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003620 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003621 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3622 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003623 if (*name != NUL)
3624 s = NULL;
3625 }
3626
Bram Moolenaar843b8842016-08-21 14:36:15 +02003627 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3628 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003629 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003630 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003631 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003632 ? find_func(trans_name, is_global, NULL) == NULL
3633 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003634 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003635 else
3636 {
3637 int dict_idx = 0;
3638 int arg_idx = 0;
3639 list_T *list = NULL;
3640
3641 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3642 {
3643 char sid_buf[25];
3644 int off = *s == 's' ? 2 : 5;
3645
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003646 // Expand s: and <SID> into <SNR>nr_, so that the function can
3647 // also be called from another script. Using trans_function_name()
3648 // would also work, but some plugins depend on the name being
3649 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003650 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003651 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003652 if (name != NULL)
3653 {
3654 STRCPY(name, sid_buf);
3655 STRCAT(name, s + off);
3656 }
3657 }
3658 else
3659 name = vim_strsave(s);
3660
3661 if (argvars[1].v_type != VAR_UNKNOWN)
3662 {
3663 if (argvars[2].v_type != VAR_UNKNOWN)
3664 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003665 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003666 arg_idx = 1;
3667 dict_idx = 2;
3668 }
3669 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003670 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003671 dict_idx = 1;
3672 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003673 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003674 arg_idx = 1;
3675 if (dict_idx > 0)
3676 {
3677 if (argvars[dict_idx].v_type != VAR_DICT)
3678 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003679 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003680 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003681 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003682 }
3683 if (argvars[dict_idx].vval.v_dict == NULL)
3684 dict_idx = 0;
3685 }
3686 if (arg_idx > 0)
3687 {
3688 if (argvars[arg_idx].v_type != VAR_LIST)
3689 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003690 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003691 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003692 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003693 }
3694 list = argvars[arg_idx].vval.v_list;
3695 if (list == NULL || list->lv_len == 0)
3696 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003697 else if (list->lv_len > MAX_FUNC_ARGS)
3698 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003699 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003700 vim_free(name);
3701 goto theend;
3702 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003703 }
3704 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003705 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003706 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003707 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003708
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003709 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003710 if (pt == NULL)
3711 vim_free(name);
3712 else
3713 {
3714 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3715 {
3716 listitem_T *li;
3717 int i = 0;
3718 int arg_len = 0;
3719 int lv_len = 0;
3720
3721 if (arg_pt != NULL)
3722 arg_len = arg_pt->pt_argc;
3723 if (list != NULL)
3724 lv_len = list->lv_len;
3725 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003726 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003727 if (pt->pt_argv == NULL)
3728 {
3729 vim_free(pt);
3730 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003731 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003732 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003733 for (i = 0; i < arg_len; i++)
3734 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3735 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003736 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003737 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003738 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003739 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003740 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003741 }
3742
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003743 // For "function(dict.func, [], dict)" and "func" is a partial
3744 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003745 if (dict_idx > 0)
3746 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003747 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003748 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3749 ++pt->pt_dict->dv_refcount;
3750 }
3751 else if (arg_pt != NULL)
3752 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003753 // If the dict was bound automatically the result is also
3754 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003755 pt->pt_dict = arg_pt->pt_dict;
3756 pt->pt_auto = arg_pt->pt_auto;
3757 if (pt->pt_dict != NULL)
3758 ++pt->pt_dict->dv_refcount;
3759 }
3760
3761 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003762 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3763 {
3764 pt->pt_func = arg_pt->pt_func;
3765 func_ptr_ref(pt->pt_func);
3766 vim_free(name);
3767 }
3768 else if (is_funcref)
3769 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003770 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003771 func_ptr_ref(pt->pt_func);
3772 vim_free(name);
3773 }
3774 else
3775 {
3776 pt->pt_name = name;
3777 func_ref(name);
3778 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003779 }
3780 rettv->v_type = VAR_PARTIAL;
3781 rettv->vval.v_partial = pt;
3782 }
3783 else
3784 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003785 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003786 rettv->v_type = VAR_FUNC;
3787 rettv->vval.v_string = name;
3788 func_ref(name);
3789 }
3790 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003791theend:
3792 vim_free(trans_name);
3793}
3794
3795/*
3796 * "funcref()" function
3797 */
3798 static void
3799f_funcref(typval_T *argvars, typval_T *rettv)
3800{
3801 common_function(argvars, rettv, TRUE);
3802}
3803
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003804 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003805ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003806{
3807 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3808 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003809 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003810}
3811
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003812/*
3813 * "function()" function
3814 */
3815 static void
3816f_function(typval_T *argvars, typval_T *rettv)
3817{
3818 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003819}
3820
3821/*
3822 * "garbagecollect()" function
3823 */
3824 static void
3825f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3826{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003827 // This is postponed until we are back at the toplevel, because we may be
3828 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003829 want_garbage_collect = TRUE;
3830
Bram Moolenaar2df47312020-09-05 17:30:44 +02003831 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003832 garbage_collect_at_exit = TRUE;
3833}
3834
3835/*
3836 * "get()" function
3837 */
3838 static void
3839f_get(typval_T *argvars, typval_T *rettv)
3840{
3841 listitem_T *li;
3842 list_T *l;
3843 dictitem_T *di;
3844 dict_T *d;
3845 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003846 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003847
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003848 if (argvars[0].v_type == VAR_BLOB)
3849 {
3850 int error = FALSE;
3851 int idx = tv_get_number_chk(&argvars[1], &error);
3852
3853 if (!error)
3854 {
3855 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003856 if (idx < 0)
3857 idx = blob_len(argvars[0].vval.v_blob) + idx;
3858 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3859 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003860 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003861 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003862 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003863 tv = rettv;
3864 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003865 }
3866 }
3867 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003868 {
3869 if ((l = argvars[0].vval.v_list) != NULL)
3870 {
3871 int error = FALSE;
3872
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003873 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003874 if (!error && li != NULL)
3875 tv = &li->li_tv;
3876 }
3877 }
3878 else if (argvars[0].v_type == VAR_DICT)
3879 {
3880 if ((d = argvars[0].vval.v_dict) != NULL)
3881 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003882 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003883 if (di != NULL)
3884 tv = &di->di_tv;
3885 }
3886 }
3887 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3888 {
3889 partial_T *pt;
3890 partial_T fref_pt;
3891
3892 if (argvars[0].v_type == VAR_PARTIAL)
3893 pt = argvars[0].vval.v_partial;
3894 else
3895 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003896 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003897 fref_pt.pt_name = argvars[0].vval.v_string;
3898 pt = &fref_pt;
3899 }
3900
3901 if (pt != NULL)
3902 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003903 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003904 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003905
3906 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3907 {
3908 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003909 n = partial_name(pt);
3910 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003911 rettv->vval.v_string = NULL;
3912 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003913 {
3914 rettv->vval.v_string = vim_strsave(n);
3915 if (rettv->v_type == VAR_FUNC)
3916 func_ref(rettv->vval.v_string);
3917 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003918 }
3919 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003920 {
3921 what_is_dict = TRUE;
3922 if (pt->pt_dict != NULL)
3923 rettv_dict_set(rettv, pt->pt_dict);
3924 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003925 else if (STRCMP(what, "args") == 0)
3926 {
3927 rettv->v_type = VAR_LIST;
3928 if (rettv_list_alloc(rettv) == OK)
3929 {
3930 int i;
3931
3932 for (i = 0; i < pt->pt_argc; ++i)
3933 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3934 }
3935 }
3936 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003937 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003938
3939 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3940 // third argument
3941 if (!what_is_dict)
3942 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003943 }
3944 }
3945 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003946 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003947
3948 if (tv == NULL)
3949 {
3950 if (argvars[2].v_type != VAR_UNKNOWN)
3951 copy_tv(&argvars[2], rettv);
3952 }
3953 else
3954 copy_tv(tv, rettv);
3955}
3956
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003957/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003958 * "getchangelist()" function
3959 */
3960 static void
3961f_getchangelist(typval_T *argvars, typval_T *rettv)
3962{
3963#ifdef FEAT_JUMPLIST
3964 buf_T *buf;
3965 int i;
3966 list_T *l;
3967 dict_T *d;
3968#endif
3969
3970 if (rettv_list_alloc(rettv) != OK)
3971 return;
3972
3973#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003974 if (argvars[0].v_type == VAR_UNKNOWN)
3975 buf = curbuf;
3976 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003977 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003978 if (buf == NULL)
3979 return;
3980
3981 l = list_alloc();
3982 if (l == NULL)
3983 return;
3984
3985 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3986 return;
3987 /*
3988 * The current window change list index tracks only the position in the
3989 * current buffer change list. For other buffers, use the change list
3990 * length as the current index.
3991 */
3992 list_append_number(rettv->vval.v_list,
3993 (varnumber_T)((buf == curwin->w_buffer)
3994 ? curwin->w_changelistidx : buf->b_changelistlen));
3995
3996 for (i = 0; i < buf->b_changelistlen; ++i)
3997 {
3998 if (buf->b_changelist[i].lnum == 0)
3999 continue;
4000 if ((d = dict_alloc()) == NULL)
4001 return;
4002 if (list_append_dict(l, d) == FAIL)
4003 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004004 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4005 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004006 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004007 }
4008#endif
4009}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004010
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004011 static void
4012getpos_both(
4013 typval_T *argvars,
4014 typval_T *rettv,
4015 int getcurpos,
4016 int charcol)
4017{
4018 pos_T *fp = NULL;
4019 pos_T pos;
4020 win_T *wp = curwin;
4021 list_T *l;
4022 int fnum = -1;
4023
4024 if (rettv_list_alloc(rettv) == OK)
4025 {
4026 l = rettv->vval.v_list;
4027 if (getcurpos)
4028 {
4029 if (argvars[0].v_type != VAR_UNKNOWN)
4030 {
4031 wp = find_win_by_nr_or_id(&argvars[0]);
4032 if (wp != NULL)
4033 fp = &wp->w_cursor;
4034 }
4035 else
4036 fp = &curwin->w_cursor;
4037 if (fp != NULL && charcol)
4038 {
4039 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01004040 pos.col =
4041 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004042 fp = &pos;
4043 }
4044 }
4045 else
4046 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
4047 if (fnum != -1)
4048 list_append_number(l, (varnumber_T)fnum);
4049 else
4050 list_append_number(l, (varnumber_T)0);
4051 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4052 : (varnumber_T)0);
4053 list_append_number(l, (fp != NULL)
4054 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4055 : (varnumber_T)0);
4056 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
4057 (varnumber_T)0);
4058 if (getcurpos)
4059 {
4060 int save_set_curswant = curwin->w_set_curswant;
4061 colnr_T save_curswant = curwin->w_curswant;
4062 colnr_T save_virtcol = curwin->w_virtcol;
4063
4064 if (wp == curwin)
4065 update_curswant();
4066 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
4067 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
4068
4069 // Do not change "curswant", as it is unexpected that a get
4070 // function has a side effect.
4071 if (wp == curwin && save_set_curswant)
4072 {
4073 curwin->w_set_curswant = save_set_curswant;
4074 curwin->w_curswant = save_curswant;
4075 curwin->w_virtcol = save_virtcol;
4076 curwin->w_valid &= ~VALID_VIRTCOL;
4077 }
4078 }
4079 }
4080 else
4081 rettv->vval.v_number = FALSE;
4082}
4083
4084/*
4085 * "getcharpos()" function
4086 */
4087 static void
4088f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
4089{
4090 getpos_both(argvars, rettv, FALSE, TRUE);
4091}
4092
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004093/*
4094 * "getcharsearch()" function
4095 */
4096 static void
4097f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4098{
4099 if (rettv_dict_alloc(rettv) != FAIL)
4100 {
4101 dict_T *dict = rettv->vval.v_dict;
4102
Bram Moolenaare0be1672018-07-08 16:50:37 +02004103 dict_add_string(dict, "char", last_csearch());
4104 dict_add_number(dict, "forward", last_csearch_forward());
4105 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004106 }
4107}
4108
4109/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004110 * "getenv()" function
4111 */
4112 static void
4113f_getenv(typval_T *argvars, typval_T *rettv)
4114{
4115 int mustfree = FALSE;
4116 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4117
4118 if (p == NULL)
4119 {
4120 rettv->v_type = VAR_SPECIAL;
4121 rettv->vval.v_number = VVAL_NULL;
4122 return;
4123 }
4124 if (!mustfree)
4125 p = vim_strsave(p);
4126 rettv->vval.v_string = p;
4127 rettv->v_type = VAR_STRING;
4128}
4129
4130/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004131 * "getfontname()" function
4132 */
4133 static void
4134f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4135{
4136 rettv->v_type = VAR_STRING;
4137 rettv->vval.v_string = NULL;
4138#ifdef FEAT_GUI
4139 if (gui.in_use)
4140 {
4141 GuiFont font;
4142 char_u *name = NULL;
4143
4144 if (argvars[0].v_type == VAR_UNKNOWN)
4145 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004146 // Get the "Normal" font. Either the name saved by
4147 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004148 font = gui.norm_font;
4149 name = hl_get_font_name();
4150 }
4151 else
4152 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004153 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004154 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004155 return;
4156 font = gui_mch_get_font(name, FALSE);
4157 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004158 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004159 }
4160 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4161 if (argvars[0].v_type != VAR_UNKNOWN)
4162 gui_mch_free_font(font);
4163 }
4164#endif
4165}
4166
4167/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004168 * "getjumplist()" function
4169 */
4170 static void
4171f_getjumplist(typval_T *argvars, typval_T *rettv)
4172{
4173#ifdef FEAT_JUMPLIST
4174 win_T *wp;
4175 int i;
4176 list_T *l;
4177 dict_T *d;
4178#endif
4179
4180 if (rettv_list_alloc(rettv) != OK)
4181 return;
4182
4183#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004184 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004185 if (wp == NULL)
4186 return;
4187
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004188 cleanup_jumplist(wp, TRUE);
4189
Bram Moolenaar4f505882018-02-10 21:06:32 +01004190 l = list_alloc();
4191 if (l == NULL)
4192 return;
4193
4194 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4195 return;
4196 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4197
4198 for (i = 0; i < wp->w_jumplistlen; ++i)
4199 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004200 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4201 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004202 if ((d = dict_alloc()) == NULL)
4203 return;
4204 if (list_append_dict(l, d) == FAIL)
4205 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004206 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4207 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004208 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004209 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004210 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004211 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004212 }
4213#endif
4214}
4215
4216/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004217 * "getpid()" function
4218 */
4219 static void
4220f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4221{
4222 rettv->vval.v_number = mch_get_pid();
4223}
4224
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004225/*
4226 * "getcurpos()" function
4227 */
4228 static void
4229f_getcurpos(typval_T *argvars, typval_T *rettv)
4230{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004231 getpos_both(argvars, rettv, TRUE, FALSE);
4232}
4233
4234 static void
4235f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4236{
4237 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004238}
4239
4240/*
4241 * "getpos(string)" function
4242 */
4243 static void
4244f_getpos(typval_T *argvars, typval_T *rettv)
4245{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004246 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004247}
4248
4249/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004250 * "getreg()" function
4251 */
4252 static void
4253f_getreg(typval_T *argvars, typval_T *rettv)
4254{
4255 char_u *strregname;
4256 int regname;
4257 int arg2 = FALSE;
4258 int return_list = FALSE;
4259 int error = FALSE;
4260
4261 if (argvars[0].v_type != VAR_UNKNOWN)
4262 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004263 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004264 error = strregname == NULL;
4265 if (argvars[1].v_type != VAR_UNKNOWN)
4266 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004267 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004268 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004269 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004270 }
4271 }
4272 else
4273 strregname = get_vim_var_str(VV_REG);
4274
4275 if (error)
4276 return;
4277
4278 regname = (strregname == NULL ? '"' : *strregname);
4279 if (regname == 0)
4280 regname = '"';
4281
4282 if (return_list)
4283 {
4284 rettv->v_type = VAR_LIST;
4285 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4286 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4287 if (rettv->vval.v_list == NULL)
4288 (void)rettv_list_alloc(rettv);
4289 else
4290 ++rettv->vval.v_list->lv_refcount;
4291 }
4292 else
4293 {
4294 rettv->v_type = VAR_STRING;
4295 rettv->vval.v_string = get_reg_contents(regname,
4296 arg2 ? GREG_EXPR_SRC : 0);
4297 }
4298}
4299
4300/*
4301 * "getregtype()" function
4302 */
4303 static void
4304f_getregtype(typval_T *argvars, typval_T *rettv)
4305{
4306 char_u *strregname;
4307 int regname;
4308 char_u buf[NUMBUFLEN + 2];
4309 long reglen = 0;
4310
4311 if (argvars[0].v_type != VAR_UNKNOWN)
4312 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004313 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004314 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004315 {
4316 rettv->v_type = VAR_STRING;
4317 rettv->vval.v_string = NULL;
4318 return;
4319 }
4320 }
4321 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004322 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004323 strregname = get_vim_var_str(VV_REG);
4324
4325 regname = (strregname == NULL ? '"' : *strregname);
4326 if (regname == 0)
4327 regname = '"';
4328
4329 buf[0] = NUL;
4330 buf[1] = NUL;
4331 switch (get_reg_type(regname, &reglen))
4332 {
4333 case MLINE: buf[0] = 'V'; break;
4334 case MCHAR: buf[0] = 'v'; break;
4335 case MBLOCK:
4336 buf[0] = Ctrl_V;
4337 sprintf((char *)buf + 1, "%ld", reglen + 1);
4338 break;
4339 }
4340 rettv->v_type = VAR_STRING;
4341 rettv->vval.v_string = vim_strsave(buf);
4342}
4343
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004344/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004345 * "gettagstack()" function
4346 */
4347 static void
4348f_gettagstack(typval_T *argvars, typval_T *rettv)
4349{
4350 win_T *wp = curwin; // default is current window
4351
4352 if (rettv_dict_alloc(rettv) != OK)
4353 return;
4354
4355 if (argvars[0].v_type != VAR_UNKNOWN)
4356 {
4357 wp = find_win_by_nr_or_id(&argvars[0]);
4358 if (wp == NULL)
4359 return;
4360 }
4361
4362 get_tagstack(wp, rettv->vval.v_dict);
4363}
4364
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004365/*
4366 * "gettext()" function
4367 */
4368 static void
4369f_gettext(typval_T *argvars, typval_T *rettv)
4370{
4371 if (argvars[0].v_type != VAR_STRING
4372 || argvars[0].vval.v_string == NULL
4373 || *argvars[0].vval.v_string == NUL)
4374 {
4375 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4376 }
4377 else
4378 {
4379 rettv->v_type = VAR_STRING;
4380 rettv->vval.v_string = vim_strsave(
4381 (char_u *)_(argvars[0].vval.v_string));
4382 }
4383}
4384
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004385// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004386#include "version.h"
4387
4388/*
4389 * "has()" function
4390 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004391 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004392f_has(typval_T *argvars, typval_T *rettv)
4393{
4394 int i;
4395 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004396 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004397 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004398 typedef struct {
4399 char *name;
4400 short present;
4401 } has_item_T;
4402 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004403 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004404 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004405#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004406 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004407#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004408 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004409#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004410 },
4411 {"arp",
4412#if defined(AMIGA) && defined(FEAT_ARP)
4413 1
4414#else
4415 0
4416#endif
4417 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004418 {"haiku",
4419#ifdef __HAIKU__
4420 1
4421#else
4422 0
4423#endif
4424 },
4425 {"bsd",
4426#if defined(BSD) && !defined(MACOS_X)
4427 1
4428#else
4429 0
4430#endif
4431 },
4432 {"hpux",
4433#ifdef hpux
4434 1
4435#else
4436 0
4437#endif
4438 },
4439 {"linux",
4440#ifdef __linux__
4441 1
4442#else
4443 0
4444#endif
4445 },
4446 {"mac", // Mac OS X (and, once, Mac OS Classic)
4447#ifdef MACOS_X
4448 1
4449#else
4450 0
4451#endif
4452 },
4453 {"osx", // Mac OS X
4454#ifdef MACOS_X
4455 1
4456#else
4457 0
4458#endif
4459 },
4460 {"macunix", // Mac OS X, with the darwin feature
4461#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4462 1
4463#else
4464 0
4465#endif
4466 },
4467 {"osxdarwin", // synonym for macunix
4468#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4469 1
4470#else
4471 0
4472#endif
4473 },
4474 {"qnx",
4475#ifdef __QNX__
4476 1
4477#else
4478 0
4479#endif
4480 },
4481 {"sun",
4482#ifdef SUN_SYSTEM
4483 1
4484#else
4485 0
4486#endif
4487 },
4488 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004489#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004490 1
4491#else
4492 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004493#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004494 },
4495 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004496#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004497 1
4498#else
4499 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004500#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004501 },
4502 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004503#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004504 1
4505#else
4506 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004507#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004508 },
4509 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004510#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004511 1
4512#else
4513 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004514#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004515 },
4516 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004517#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004518 1
4519#else
4520 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004521#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004522 },
4523 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004524#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004525 1
4526#else
4527 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004528#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004529 },
4530 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004531#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004532 1
4533#else
4534 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004535#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004536 },
4537 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004538#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004539 1
4540#else
4541 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004542#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004543 },
4544 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004545#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004546 1
4547#else
4548 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004549#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004550 },
4551 {"autocmd", 1},
4552 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004553#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004554 1
4555#else
4556 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004557#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004558 },
4559 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004560#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004561 1
4562#else
4563 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004564#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004565 },
4566 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004567#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004568 1
4569#else
4570 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004571#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004572 },
4573 {"balloon_multiline",
4574#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4575 // MS-Windows requires runtime check, see below
4576 1
4577#else
4578 0
4579#endif
4580 },
4581 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004582#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004583 1
4584#else
4585 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004586#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004587 },
4588 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004589#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004590 1
4591#else
4592 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004593#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004594 },
4595 {"all_builtin_terms",
4596#if defined(ALL_BUILTIN_TCAPS)
4597 1
4598#else
4599 0
4600#endif
4601 },
4602 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004603#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004604 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004605 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004606 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004607#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004608 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004609#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004610 },
4611 {"byte_offset",
4612#ifdef FEAT_BYTEOFF
4613 1
4614#else
4615 0
4616#endif
4617 },
4618 {"channel",
4619#ifdef FEAT_JOB_CHANNEL
4620 1
4621#else
4622 0
4623#endif
4624 },
4625 {"cindent",
4626#ifdef FEAT_CINDENT
4627 1
4628#else
4629 0
4630#endif
4631 },
4632 {"clientserver",
4633#ifdef FEAT_CLIENTSERVER
4634 1
4635#else
4636 0
4637#endif
4638 },
4639 {"clipboard",
4640#ifdef FEAT_CLIPBOARD
4641 1
4642#else
4643 0
4644#endif
4645 },
4646 {"cmdline_compl", 1},
4647 {"cmdline_hist", 1},
4648 {"comments", 1},
4649 {"conceal",
4650#ifdef FEAT_CONCEAL
4651 1
4652#else
4653 0
4654#endif
4655 },
4656 {"cryptv",
4657#ifdef FEAT_CRYPT
4658 1
4659#else
4660 0
4661#endif
4662 },
4663 {"crypt-blowfish",
4664#ifdef FEAT_CRYPT
4665 1
4666#else
4667 0
4668#endif
4669 },
4670 {"crypt-blowfish2",
4671#ifdef FEAT_CRYPT
4672 1
4673#else
4674 0
4675#endif
4676 },
4677 {"cscope",
4678#ifdef FEAT_CSCOPE
4679 1
4680#else
4681 0
4682#endif
4683 },
4684 {"cursorbind", 1},
4685 {"cursorshape",
4686#ifdef CURSOR_SHAPE
4687 1
4688#else
4689 0
4690#endif
4691 },
4692 {"debug",
4693#ifdef DEBUG
4694 1
4695#else
4696 0
4697#endif
4698 },
4699 {"dialog_con",
4700#ifdef FEAT_CON_DIALOG
4701 1
4702#else
4703 0
4704#endif
4705 },
4706 {"dialog_gui",
4707#ifdef FEAT_GUI_DIALOG
4708 1
4709#else
4710 0
4711#endif
4712 },
4713 {"diff",
4714#ifdef FEAT_DIFF
4715 1
4716#else
4717 0
4718#endif
4719 },
4720 {"digraphs",
4721#ifdef FEAT_DIGRAPHS
4722 1
4723#else
4724 0
4725#endif
4726 },
4727 {"directx",
4728#ifdef FEAT_DIRECTX
4729 1
4730#else
4731 0
4732#endif
4733 },
4734 {"dnd",
4735#ifdef FEAT_DND
4736 1
4737#else
4738 0
4739#endif
4740 },
4741 {"emacs_tags",
4742#ifdef FEAT_EMACS_TAGS
4743 1
4744#else
4745 0
4746#endif
4747 },
4748 {"eval", 1}, // always present, of course!
4749 {"ex_extra", 1}, // graduated feature
4750 {"extra_search",
4751#ifdef FEAT_SEARCH_EXTRA
4752 1
4753#else
4754 0
4755#endif
4756 },
4757 {"file_in_path",
4758#ifdef FEAT_SEARCHPATH
4759 1
4760#else
4761 0
4762#endif
4763 },
4764 {"filterpipe",
4765#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4766 1
4767#else
4768 0
4769#endif
4770 },
4771 {"find_in_path",
4772#ifdef FEAT_FIND_ID
4773 1
4774#else
4775 0
4776#endif
4777 },
4778 {"float",
4779#ifdef FEAT_FLOAT
4780 1
4781#else
4782 0
4783#endif
4784 },
4785 {"folding",
4786#ifdef FEAT_FOLDING
4787 1
4788#else
4789 0
4790#endif
4791 },
4792 {"footer",
4793#ifdef FEAT_FOOTER
4794 1
4795#else
4796 0
4797#endif
4798 },
4799 {"fork",
4800#if !defined(USE_SYSTEM) && defined(UNIX)
4801 1
4802#else
4803 0
4804#endif
4805 },
4806 {"gettext",
4807#ifdef FEAT_GETTEXT
4808 1
4809#else
4810 0
4811#endif
4812 },
4813 {"gui",
4814#ifdef FEAT_GUI
4815 1
4816#else
4817 0
4818#endif
4819 },
4820 {"gui_neXtaw",
4821#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4822 1
4823#else
4824 0
4825#endif
4826 },
4827 {"gui_athena",
4828#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4829 1
4830#else
4831 0
4832#endif
4833 },
4834 {"gui_gtk",
4835#ifdef FEAT_GUI_GTK
4836 1
4837#else
4838 0
4839#endif
4840 },
4841 {"gui_gtk2",
4842#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4843 1
4844#else
4845 0
4846#endif
4847 },
4848 {"gui_gtk3",
4849#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4850 1
4851#else
4852 0
4853#endif
4854 },
4855 {"gui_gnome",
4856#ifdef FEAT_GUI_GNOME
4857 1
4858#else
4859 0
4860#endif
4861 },
4862 {"gui_haiku",
4863#ifdef FEAT_GUI_HAIKU
4864 1
4865#else
4866 0
4867#endif
4868 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004869 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004870 {"gui_motif",
4871#ifdef FEAT_GUI_MOTIF
4872 1
4873#else
4874 0
4875#endif
4876 },
4877 {"gui_photon",
4878#ifdef FEAT_GUI_PHOTON
4879 1
4880#else
4881 0
4882#endif
4883 },
4884 {"gui_win32",
4885#ifdef FEAT_GUI_MSWIN
4886 1
4887#else
4888 0
4889#endif
4890 },
4891 {"iconv",
4892#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4893 1
4894#else
4895 0
4896#endif
4897 },
4898 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004899 {"ipv6",
4900#ifdef FEAT_IPV6
4901 1
4902#else
4903 0
4904#endif
4905 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004906 {"job",
4907#ifdef FEAT_JOB_CHANNEL
4908 1
4909#else
4910 0
4911#endif
4912 },
4913 {"jumplist",
4914#ifdef FEAT_JUMPLIST
4915 1
4916#else
4917 0
4918#endif
4919 },
4920 {"keymap",
4921#ifdef FEAT_KEYMAP
4922 1
4923#else
4924 0
4925#endif
4926 },
4927 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4928 {"langmap",
4929#ifdef FEAT_LANGMAP
4930 1
4931#else
4932 0
4933#endif
4934 },
4935 {"libcall",
4936#ifdef FEAT_LIBCALL
4937 1
4938#else
4939 0
4940#endif
4941 },
4942 {"linebreak",
4943#ifdef FEAT_LINEBREAK
4944 1
4945#else
4946 0
4947#endif
4948 },
4949 {"lispindent",
4950#ifdef FEAT_LISP
4951 1
4952#else
4953 0
4954#endif
4955 },
4956 {"listcmds", 1},
4957 {"localmap", 1},
4958 {"lua",
4959#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4960 1
4961#else
4962 0
4963#endif
4964 },
4965 {"menu",
4966#ifdef FEAT_MENU
4967 1
4968#else
4969 0
4970#endif
4971 },
4972 {"mksession",
4973#ifdef FEAT_SESSION
4974 1
4975#else
4976 0
4977#endif
4978 },
4979 {"modify_fname", 1},
4980 {"mouse", 1},
4981 {"mouseshape",
4982#ifdef FEAT_MOUSESHAPE
4983 1
4984#else
4985 0
4986#endif
4987 },
4988 {"mouse_dec",
4989#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4990 1
4991#else
4992 0
4993#endif
4994 },
4995 {"mouse_gpm",
4996#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4997 1
4998#else
4999 0
5000#endif
5001 },
5002 {"mouse_jsbterm",
5003#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
5004 1
5005#else
5006 0
5007#endif
5008 },
5009 {"mouse_netterm",
5010#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
5011 1
5012#else
5013 0
5014#endif
5015 },
5016 {"mouse_pterm",
5017#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
5018 1
5019#else
5020 0
5021#endif
5022 },
5023 {"mouse_sgr",
5024#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5025 1
5026#else
5027 0
5028#endif
5029 },
5030 {"mouse_sysmouse",
5031#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
5032 1
5033#else
5034 0
5035#endif
5036 },
5037 {"mouse_urxvt",
5038#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
5039 1
5040#else
5041 0
5042#endif
5043 },
5044 {"mouse_xterm",
5045#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
5046 1
5047#else
5048 0
5049#endif
5050 },
5051 {"multi_byte", 1},
5052 {"multi_byte_ime",
5053#ifdef FEAT_MBYTE_IME
5054 1
5055#else
5056 0
5057#endif
5058 },
5059 {"multi_lang",
5060#ifdef FEAT_MULTI_LANG
5061 1
5062#else
5063 0
5064#endif
5065 },
5066 {"mzscheme",
5067#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
5068 1
5069#else
5070 0
5071#endif
5072 },
5073 {"num64", 1},
5074 {"ole",
5075#ifdef FEAT_OLE
5076 1
5077#else
5078 0
5079#endif
5080 },
5081 {"packages",
5082#ifdef FEAT_EVAL
5083 1
5084#else
5085 0
5086#endif
5087 },
5088 {"path_extra",
5089#ifdef FEAT_PATH_EXTRA
5090 1
5091#else
5092 0
5093#endif
5094 },
5095 {"perl",
5096#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
5097 1
5098#else
5099 0
5100#endif
5101 },
5102 {"persistent_undo",
5103#ifdef FEAT_PERSISTENT_UNDO
5104 1
5105#else
5106 0
5107#endif
5108 },
5109 {"python_compiled",
5110#if defined(FEAT_PYTHON)
5111 1
5112#else
5113 0
5114#endif
5115 },
5116 {"python_dynamic",
5117#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
5118 1
5119#else
5120 0
5121#endif
5122 },
5123 {"python",
5124#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
5125 1
5126#else
5127 0
5128#endif
5129 },
5130 {"pythonx",
5131#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
5132 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
5133 1
5134#else
5135 0
5136#endif
5137 },
5138 {"python3_compiled",
5139#if defined(FEAT_PYTHON3)
5140 1
5141#else
5142 0
5143#endif
5144 },
5145 {"python3_dynamic",
5146#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
5147 1
5148#else
5149 0
5150#endif
5151 },
5152 {"python3",
5153#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
5154 1
5155#else
5156 0
5157#endif
5158 },
5159 {"popupwin",
5160#ifdef FEAT_PROP_POPUP
5161 1
5162#else
5163 0
5164#endif
5165 },
5166 {"postscript",
5167#ifdef FEAT_POSTSCRIPT
5168 1
5169#else
5170 0
5171#endif
5172 },
5173 {"printer",
5174#ifdef FEAT_PRINTER
5175 1
5176#else
5177 0
5178#endif
5179 },
5180 {"profile",
5181#ifdef FEAT_PROFILE
5182 1
5183#else
5184 0
5185#endif
5186 },
5187 {"reltime",
5188#ifdef FEAT_RELTIME
5189 1
5190#else
5191 0
5192#endif
5193 },
5194 {"quickfix",
5195#ifdef FEAT_QUICKFIX
5196 1
5197#else
5198 0
5199#endif
5200 },
5201 {"rightleft",
5202#ifdef FEAT_RIGHTLEFT
5203 1
5204#else
5205 0
5206#endif
5207 },
5208 {"ruby",
5209#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5210 1
5211#else
5212 0
5213#endif
5214 },
5215 {"scrollbind", 1},
5216 {"showcmd",
5217#ifdef FEAT_CMDL_INFO
5218 1
5219#else
5220 0
5221#endif
5222 },
5223 {"cmdline_info",
5224#ifdef FEAT_CMDL_INFO
5225 1
5226#else
5227 0
5228#endif
5229 },
5230 {"signs",
5231#ifdef FEAT_SIGNS
5232 1
5233#else
5234 0
5235#endif
5236 },
5237 {"smartindent",
5238#ifdef FEAT_SMARTINDENT
5239 1
5240#else
5241 0
5242#endif
5243 },
5244 {"startuptime",
5245#ifdef STARTUPTIME
5246 1
5247#else
5248 0
5249#endif
5250 },
5251 {"statusline",
5252#ifdef FEAT_STL_OPT
5253 1
5254#else
5255 0
5256#endif
5257 },
5258 {"netbeans_intg",
5259#ifdef FEAT_NETBEANS_INTG
5260 1
5261#else
5262 0
5263#endif
5264 },
5265 {"sound",
5266#ifdef FEAT_SOUND
5267 1
5268#else
5269 0
5270#endif
5271 },
5272 {"spell",
5273#ifdef FEAT_SPELL
5274 1
5275#else
5276 0
5277#endif
5278 },
5279 {"syntax",
5280#ifdef FEAT_SYN_HL
5281 1
5282#else
5283 0
5284#endif
5285 },
5286 {"system",
5287#if defined(USE_SYSTEM) || !defined(UNIX)
5288 1
5289#else
5290 0
5291#endif
5292 },
5293 {"tag_binary",
5294#ifdef FEAT_TAG_BINS
5295 1
5296#else
5297 0
5298#endif
5299 },
5300 {"tcl",
5301#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5302 1
5303#else
5304 0
5305#endif
5306 },
5307 {"termguicolors",
5308#ifdef FEAT_TERMGUICOLORS
5309 1
5310#else
5311 0
5312#endif
5313 },
5314 {"terminal",
5315#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5316 1
5317#else
5318 0
5319#endif
5320 },
5321 {"terminfo",
5322#ifdef TERMINFO
5323 1
5324#else
5325 0
5326#endif
5327 },
5328 {"termresponse",
5329#ifdef FEAT_TERMRESPONSE
5330 1
5331#else
5332 0
5333#endif
5334 },
5335 {"textobjects",
5336#ifdef FEAT_TEXTOBJ
5337 1
5338#else
5339 0
5340#endif
5341 },
5342 {"textprop",
5343#ifdef FEAT_PROP_POPUP
5344 1
5345#else
5346 0
5347#endif
5348 },
5349 {"tgetent",
5350#ifdef HAVE_TGETENT
5351 1
5352#else
5353 0
5354#endif
5355 },
5356 {"timers",
5357#ifdef FEAT_TIMERS
5358 1
5359#else
5360 0
5361#endif
5362 },
5363 {"title",
5364#ifdef FEAT_TITLE
5365 1
5366#else
5367 0
5368#endif
5369 },
5370 {"toolbar",
5371#ifdef FEAT_TOOLBAR
5372 1
5373#else
5374 0
5375#endif
5376 },
5377 {"unnamedplus",
5378#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5379 1
5380#else
5381 0
5382#endif
5383 },
5384 {"user-commands", 1}, // was accidentally included in 5.4
5385 {"user_commands", 1},
5386 {"vartabs",
5387#ifdef FEAT_VARTABS
5388 1
5389#else
5390 0
5391#endif
5392 },
5393 {"vertsplit", 1},
5394 {"viminfo",
5395#ifdef FEAT_VIMINFO
5396 1
5397#else
5398 0
5399#endif
5400 },
5401 {"vimscript-1", 1},
5402 {"vimscript-2", 1},
5403 {"vimscript-3", 1},
5404 {"vimscript-4", 1},
5405 {"virtualedit", 1},
5406 {"visual", 1},
5407 {"visualextra", 1},
5408 {"vreplace", 1},
5409 {"vtp",
5410#ifdef FEAT_VTP
5411 1
5412#else
5413 0
5414#endif
5415 },
5416 {"wildignore",
5417#ifdef FEAT_WILDIGN
5418 1
5419#else
5420 0
5421#endif
5422 },
5423 {"wildmenu",
5424#ifdef FEAT_WILDMENU
5425 1
5426#else
5427 0
5428#endif
5429 },
5430 {"windows", 1},
5431 {"winaltkeys",
5432#ifdef FEAT_WAK
5433 1
5434#else
5435 0
5436#endif
5437 },
5438 {"writebackup",
5439#ifdef FEAT_WRITEBACKUP
5440 1
5441#else
5442 0
5443#endif
5444 },
5445 {"xim",
5446#ifdef FEAT_XIM
5447 1
5448#else
5449 0
5450#endif
5451 },
5452 {"xfontset",
5453#ifdef FEAT_XFONTSET
5454 1
5455#else
5456 0
5457#endif
5458 },
5459 {"xpm",
5460#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5461 1
5462#else
5463 0
5464#endif
5465 },
5466 {"xpm_w32", // for backward compatibility
5467#ifdef FEAT_XPM_W32
5468 1
5469#else
5470 0
5471#endif
5472 },
5473 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005474#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005475 1
5476#else
5477 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005478#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005479 },
5480 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005481#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005482 1
5483#else
5484 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005485#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005486 },
5487 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005488#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005489 1
5490#else
5491 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005492#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005493 },
5494 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005495#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005496 1
5497#else
5498 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005499#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005500 },
5501 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005502#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005503 1
5504#else
5505 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005506#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005507 },
5508 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005509 };
5510
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005511 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005512 for (i = 0; has_list[i].name != NULL; ++i)
5513 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005514 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005515 x = TRUE;
5516 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005517 break;
5518 }
5519
Bram Moolenaar79296512020-03-22 16:17:14 +01005520 // features also in has_list[] but sometimes enabled at runtime
5521 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005522 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005523 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005524 {
5525 // intentionally empty
5526 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005527#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005528 else if (STRICMP(name, "balloon_multiline") == 0)
5529 n = multiline_balloon_available();
5530#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005531#ifdef VIMDLL
5532 else if (STRICMP(name, "filterpipe") == 0)
5533 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005534#endif
5535#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5536 else if (STRICMP(name, "iconv") == 0)
5537 n = iconv_enabled(FALSE);
5538#endif
5539#ifdef DYNAMIC_LUA
5540 else if (STRICMP(name, "lua") == 0)
5541 n = lua_enabled(FALSE);
5542#endif
5543#ifdef DYNAMIC_MZSCHEME
5544 else if (STRICMP(name, "mzscheme") == 0)
5545 n = mzscheme_enabled(FALSE);
5546#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005547#ifdef DYNAMIC_PERL
5548 else if (STRICMP(name, "perl") == 0)
5549 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005550#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005551#ifdef DYNAMIC_PYTHON
5552 else if (STRICMP(name, "python") == 0)
5553 n = python_enabled(FALSE);
5554#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005555#ifdef DYNAMIC_PYTHON3
5556 else if (STRICMP(name, "python3") == 0)
5557 n = python3_enabled(FALSE);
5558#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005559#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5560 else if (STRICMP(name, "pythonx") == 0)
5561 {
5562# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5563 if (p_pyx == 0)
5564 n = python3_enabled(FALSE) || python_enabled(FALSE);
5565 else if (p_pyx == 3)
5566 n = python3_enabled(FALSE);
5567 else if (p_pyx == 2)
5568 n = python_enabled(FALSE);
5569# elif defined(DYNAMIC_PYTHON)
5570 n = python_enabled(FALSE);
5571# elif defined(DYNAMIC_PYTHON3)
5572 n = python3_enabled(FALSE);
5573# endif
5574 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005575#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005576#ifdef DYNAMIC_RUBY
5577 else if (STRICMP(name, "ruby") == 0)
5578 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005579#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005580#ifdef DYNAMIC_TCL
5581 else if (STRICMP(name, "tcl") == 0)
5582 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005583#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005584#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005585 else if (STRICMP(name, "terminal") == 0)
5586 n = terminal_enabled();
5587#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005588 }
5589
Bram Moolenaar79296512020-03-22 16:17:14 +01005590 // features not in has_list[]
5591 if (x == FALSE)
5592 {
5593 if (STRNICMP(name, "patch", 5) == 0)
5594 {
5595 x = TRUE;
5596 if (name[5] == '-'
5597 && STRLEN(name) >= 11
5598 && vim_isdigit(name[6])
5599 && vim_isdigit(name[8])
5600 && vim_isdigit(name[10]))
5601 {
5602 int major = atoi((char *)name + 6);
5603 int minor = atoi((char *)name + 8);
5604
5605 // Expect "patch-9.9.01234".
5606 n = (major < VIM_VERSION_MAJOR
5607 || (major == VIM_VERSION_MAJOR
5608 && (minor < VIM_VERSION_MINOR
5609 || (minor == VIM_VERSION_MINOR
5610 && has_patch(atoi((char *)name + 10))))));
5611 }
5612 else
5613 n = has_patch(atoi((char *)name + 5));
5614 }
5615 else if (STRICMP(name, "vim_starting") == 0)
5616 {
5617 x = TRUE;
5618 n = (starting != 0);
5619 }
5620 else if (STRICMP(name, "ttyin") == 0)
5621 {
5622 x = TRUE;
5623 n = mch_input_isatty();
5624 }
5625 else if (STRICMP(name, "ttyout") == 0)
5626 {
5627 x = TRUE;
5628 n = stdout_isatty;
5629 }
5630 else if (STRICMP(name, "multi_byte_encoding") == 0)
5631 {
5632 x = TRUE;
5633 n = has_mbyte;
5634 }
5635 else if (STRICMP(name, "gui_running") == 0)
5636 {
5637 x = TRUE;
5638#ifdef FEAT_GUI
5639 n = (gui.in_use || gui.starting);
5640#endif
5641 }
5642 else if (STRICMP(name, "browse") == 0)
5643 {
5644 x = TRUE;
5645#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5646 n = gui.in_use; // gui_mch_browse() works when GUI is running
5647#endif
5648 }
5649 else if (STRICMP(name, "syntax_items") == 0)
5650 {
5651 x = TRUE;
5652#ifdef FEAT_SYN_HL
5653 n = syntax_present(curwin);
5654#endif
5655 }
5656 else if (STRICMP(name, "vcon") == 0)
5657 {
5658 x = TRUE;
5659#ifdef FEAT_VTP
5660 n = is_term_win32() && has_vtp_working();
5661#endif
5662 }
5663 else if (STRICMP(name, "netbeans_enabled") == 0)
5664 {
5665 x = TRUE;
5666#ifdef FEAT_NETBEANS_INTG
5667 n = netbeans_active();
5668#endif
5669 }
5670 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5671 {
5672 x = TRUE;
5673#ifdef FEAT_MOUSE_GPM
5674 n = gpm_enabled();
5675#endif
5676 }
5677 else if (STRICMP(name, "conpty") == 0)
5678 {
5679 x = TRUE;
5680#if defined(FEAT_TERMINAL) && defined(MSWIN)
5681 n = use_conpty();
5682#endif
5683 }
5684 else if (STRICMP(name, "clipboard_working") == 0)
5685 {
5686 x = TRUE;
5687#ifdef FEAT_CLIPBOARD
5688 n = clip_star.available;
5689#endif
5690 }
5691 }
5692
Bram Moolenaar04637e22020-09-05 18:45:29 +02005693 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005694 // return whether feature could ever be enabled
5695 rettv->vval.v_number = x;
5696 else
5697 // return whether feature is enabled
5698 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005699}
5700
5701/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005702 * Return TRUE if "feature" can change later.
5703 * Also when checking for the feature has side effects, such as loading a DLL.
5704 */
5705 int
5706dynamic_feature(char_u *feature)
5707{
5708 return (feature == NULL
5709#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5710 || STRICMP(feature, "balloon_multiline") == 0
5711#endif
5712#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5713 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5714#endif
5715#ifdef VIMDLL
5716 || STRICMP(feature, "filterpipe") == 0
5717#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005718#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005719 // this can only change on Unix where the ":gui" command could be
5720 // used.
5721 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5722#endif
5723#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5724 || STRICMP(feature, "iconv") == 0
5725#endif
5726#ifdef DYNAMIC_LUA
5727 || STRICMP(feature, "lua") == 0
5728#endif
5729#ifdef FEAT_MOUSE_GPM
5730 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5731#endif
5732#ifdef DYNAMIC_MZSCHEME
5733 || STRICMP(feature, "mzscheme") == 0
5734#endif
5735#ifdef FEAT_NETBEANS_INTG
5736 || STRICMP(feature, "netbeans_enabled") == 0
5737#endif
5738#ifdef DYNAMIC_PERL
5739 || STRICMP(feature, "perl") == 0
5740#endif
5741#ifdef DYNAMIC_PYTHON
5742 || STRICMP(feature, "python") == 0
5743#endif
5744#ifdef DYNAMIC_PYTHON3
5745 || STRICMP(feature, "python3") == 0
5746#endif
5747#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5748 || STRICMP(feature, "pythonx") == 0
5749#endif
5750#ifdef DYNAMIC_RUBY
5751 || STRICMP(feature, "ruby") == 0
5752#endif
5753#ifdef FEAT_SYN_HL
5754 || STRICMP(feature, "syntax_items") == 0
5755#endif
5756#ifdef DYNAMIC_TCL
5757 || STRICMP(feature, "tcl") == 0
5758#endif
5759 // once "starting" is zero it will stay that way
5760 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5761 || STRICMP(feature, "multi_byte_encoding") == 0
5762#if defined(FEAT_TERMINAL) && defined(MSWIN)
5763 || STRICMP(feature, "conpty") == 0
5764#endif
5765 );
5766}
5767
5768/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005769 * "haslocaldir()" function
5770 */
5771 static void
5772f_haslocaldir(typval_T *argvars, typval_T *rettv)
5773{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005774 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005775 win_T *wp = NULL;
5776
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005777 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5778
5779 // Check for window-local and tab-local directories
5780 if (wp != NULL && wp->w_localdir != NULL)
5781 rettv->vval.v_number = 1;
5782 else if (tp != NULL && tp->tp_localdir != NULL)
5783 rettv->vval.v_number = 2;
5784 else
5785 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005786}
5787
5788/*
5789 * "hasmapto()" function
5790 */
5791 static void
5792f_hasmapto(typval_T *argvars, typval_T *rettv)
5793{
5794 char_u *name;
5795 char_u *mode;
5796 char_u buf[NUMBUFLEN];
5797 int abbr = FALSE;
5798
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005799 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005800 if (argvars[1].v_type == VAR_UNKNOWN)
5801 mode = (char_u *)"nvo";
5802 else
5803 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005804 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005805 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005806 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005807 }
5808
5809 if (map_to_exists(name, mode, abbr))
5810 rettv->vval.v_number = TRUE;
5811 else
5812 rettv->vval.v_number = FALSE;
5813}
5814
5815/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005816 * "highlightID(name)" function
5817 */
5818 static void
5819f_hlID(typval_T *argvars, typval_T *rettv)
5820{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005821 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005822}
5823
5824/*
5825 * "highlight_exists()" function
5826 */
5827 static void
5828f_hlexists(typval_T *argvars, typval_T *rettv)
5829{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005830 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005831}
5832
5833/*
5834 * "hostname()" function
5835 */
5836 static void
5837f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5838{
5839 char_u hostname[256];
5840
5841 mch_get_host_name(hostname, 256);
5842 rettv->v_type = VAR_STRING;
5843 rettv->vval.v_string = vim_strsave(hostname);
5844}
5845
5846/*
5847 * iconv() function
5848 */
5849 static void
5850f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5851{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005852 char_u buf1[NUMBUFLEN];
5853 char_u buf2[NUMBUFLEN];
5854 char_u *from, *to, *str;
5855 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005856
5857 rettv->v_type = VAR_STRING;
5858 rettv->vval.v_string = NULL;
5859
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005860 str = tv_get_string(&argvars[0]);
5861 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5862 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005863 vimconv.vc_type = CONV_NONE;
5864 convert_setup(&vimconv, from, to);
5865
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005866 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005867 if (vimconv.vc_type == CONV_NONE)
5868 rettv->vval.v_string = vim_strsave(str);
5869 else
5870 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5871
5872 convert_setup(&vimconv, NULL, NULL);
5873 vim_free(from);
5874 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005875}
5876
5877/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005878 * "index()" function
5879 */
5880 static void
5881f_index(typval_T *argvars, typval_T *rettv)
5882{
5883 list_T *l;
5884 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005885 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005886 long idx = 0;
5887 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005888 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005889
5890 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005891 if (argvars[0].v_type == VAR_BLOB)
5892 {
5893 typval_T tv;
5894 int start = 0;
5895
5896 if (argvars[2].v_type != VAR_UNKNOWN)
5897 {
5898 start = tv_get_number_chk(&argvars[2], &error);
5899 if (error)
5900 return;
5901 }
5902 b = argvars[0].vval.v_blob;
5903 if (b == NULL)
5904 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005905 if (start < 0)
5906 {
5907 start = blob_len(b) + start;
5908 if (start < 0)
5909 start = 0;
5910 }
5911
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005912 for (idx = start; idx < blob_len(b); ++idx)
5913 {
5914 tv.v_type = VAR_NUMBER;
5915 tv.vval.v_number = blob_get(b, idx);
5916 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5917 {
5918 rettv->vval.v_number = idx;
5919 return;
5920 }
5921 }
5922 return;
5923 }
5924 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005925 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005926 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005927 return;
5928 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005929
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005930 l = argvars[0].vval.v_list;
5931 if (l != NULL)
5932 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005933 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005934 item = l->lv_first;
5935 if (argvars[2].v_type != VAR_UNKNOWN)
5936 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005937 // Start at specified item. Use the cached index that list_find()
5938 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005939 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005940 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005941 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005942 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005943 if (error)
5944 item = NULL;
5945 }
5946
5947 for ( ; item != NULL; item = item->li_next, ++idx)
5948 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5949 {
5950 rettv->vval.v_number = idx;
5951 break;
5952 }
5953 }
5954}
5955
5956static int inputsecret_flag = 0;
5957
5958/*
5959 * "input()" function
5960 * Also handles inputsecret() when inputsecret is set.
5961 */
5962 static void
5963f_input(typval_T *argvars, typval_T *rettv)
5964{
5965 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5966}
5967
5968/*
5969 * "inputdialog()" function
5970 */
5971 static void
5972f_inputdialog(typval_T *argvars, typval_T *rettv)
5973{
5974#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005975 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005976 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5977 {
5978 char_u *message;
5979 char_u buf[NUMBUFLEN];
5980 char_u *defstr = (char_u *)"";
5981
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005982 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005983 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005984 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005985 vim_strncpy(IObuff, defstr, IOSIZE - 1);
5986 else
5987 IObuff[0] = NUL;
5988 if (message != NULL && defstr != NULL
5989 && do_dialog(VIM_QUESTION, NULL, message,
5990 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
5991 rettv->vval.v_string = vim_strsave(IObuff);
5992 else
5993 {
5994 if (message != NULL && defstr != NULL
5995 && argvars[1].v_type != VAR_UNKNOWN
5996 && argvars[2].v_type != VAR_UNKNOWN)
5997 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005998 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005999 else
6000 rettv->vval.v_string = NULL;
6001 }
6002 rettv->v_type = VAR_STRING;
6003 }
6004 else
6005#endif
6006 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6007}
6008
6009/*
6010 * "inputlist()" function
6011 */
6012 static void
6013f_inputlist(typval_T *argvars, typval_T *rettv)
6014{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006015 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006016 listitem_T *li;
6017 int selected;
6018 int mouse_used;
6019
6020#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006021 // While starting up, there is no place to enter text. When running tests
6022 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006023 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006024 return;
6025#endif
6026 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6027 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006028 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006029 return;
6030 }
6031
6032 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006033 msg_row = Rows - 1; // for when 'cmdheight' > 1
6034 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006035 msg_scroll = TRUE;
6036 msg_clr_eos();
6037
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006038 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006039 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006040 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006041 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006042 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006043 msg_putchar('\n');
6044 }
6045
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006046 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006047 selected = prompt_for_number(&mouse_used);
6048 if (mouse_used)
6049 selected -= lines_left;
6050
6051 rettv->vval.v_number = selected;
6052}
6053
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006054static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6055
6056/*
6057 * "inputrestore()" function
6058 */
6059 static void
6060f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6061{
6062 if (ga_userinput.ga_len > 0)
6063 {
6064 --ga_userinput.ga_len;
6065 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6066 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006067 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006068 }
6069 else if (p_verbose > 1)
6070 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006071 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006072 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006073 }
6074}
6075
6076/*
6077 * "inputsave()" function
6078 */
6079 static void
6080f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6081{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006082 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006083 if (ga_grow(&ga_userinput, 1) == OK)
6084 {
6085 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6086 + ga_userinput.ga_len);
6087 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006088 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006089 }
6090 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006091 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006092}
6093
6094/*
6095 * "inputsecret()" function
6096 */
6097 static void
6098f_inputsecret(typval_T *argvars, typval_T *rettv)
6099{
6100 ++cmdline_star;
6101 ++inputsecret_flag;
6102 f_input(argvars, rettv);
6103 --cmdline_star;
6104 --inputsecret_flag;
6105}
6106
6107/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01006108 * "interrupt()" function
6109 */
6110 static void
6111f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6112{
6113 got_int = TRUE;
6114}
6115
6116/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006117 * "invert(expr)" function
6118 */
6119 static void
6120f_invert(typval_T *argvars, typval_T *rettv)
6121{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006122 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006123}
6124
6125/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006126 * "islocked()" function
6127 */
6128 static void
6129f_islocked(typval_T *argvars, typval_T *rettv)
6130{
6131 lval_T lv;
6132 char_u *end;
6133 dictitem_T *di;
6134
6135 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006136 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006137 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006138 if (end != NULL && lv.ll_name != NULL)
6139 {
6140 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02006141 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006142 else
6143 {
6144 if (lv.ll_tv == NULL)
6145 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006146 di = find_var(lv.ll_name, NULL, TRUE);
6147 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006148 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006149 // Consider a variable locked when:
6150 // 1. the variable itself is locked
6151 // 2. the value of the variable is locked.
6152 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01006153 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6154 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006155 }
6156 }
6157 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006158 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006159 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006160 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006161 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006162 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006163 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6164 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006165 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006166 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6167 }
6168 }
6169
6170 clear_lval(&lv);
6171}
6172
6173#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6174/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006175 * "isinf()" function
6176 */
6177 static void
6178f_isinf(typval_T *argvars, typval_T *rettv)
6179{
6180 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6181 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6182}
6183
6184/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006185 * "isnan()" function
6186 */
6187 static void
6188f_isnan(typval_T *argvars, typval_T *rettv)
6189{
6190 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6191 && isnan(argvars[0].vval.v_float);
6192}
6193#endif
6194
6195/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006196 * "last_buffer_nr()" function.
6197 */
6198 static void
6199f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6200{
6201 int n = 0;
6202 buf_T *buf;
6203
Bram Moolenaar29323592016-07-24 22:04:11 +02006204 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006205 if (n < buf->b_fnum)
6206 n = buf->b_fnum;
6207
6208 rettv->vval.v_number = n;
6209}
6210
6211/*
6212 * "len()" function
6213 */
6214 static void
6215f_len(typval_T *argvars, typval_T *rettv)
6216{
6217 switch (argvars[0].v_type)
6218 {
6219 case VAR_STRING:
6220 case VAR_NUMBER:
6221 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006222 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006223 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006224 case VAR_BLOB:
6225 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6226 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006227 case VAR_LIST:
6228 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6229 break;
6230 case VAR_DICT:
6231 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6232 break;
6233 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006234 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006235 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006236 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006237 case VAR_SPECIAL:
6238 case VAR_FLOAT:
6239 case VAR_FUNC:
6240 case VAR_PARTIAL:
6241 case VAR_JOB:
6242 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006243 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006244 break;
6245 }
6246}
6247
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006248 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006249libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006250{
6251#ifdef FEAT_LIBCALL
6252 char_u *string_in;
6253 char_u **string_result;
6254 int nr_result;
6255#endif
6256
6257 rettv->v_type = type;
6258 if (type != VAR_NUMBER)
6259 rettv->vval.v_string = NULL;
6260
6261 if (check_restricted() || check_secure())
6262 return;
6263
6264#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006265 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006266 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6267 {
6268 string_in = NULL;
6269 if (argvars[2].v_type == VAR_STRING)
6270 string_in = argvars[2].vval.v_string;
6271 if (type == VAR_NUMBER)
6272 string_result = NULL;
6273 else
6274 string_result = &rettv->vval.v_string;
6275 if (mch_libcall(argvars[0].vval.v_string,
6276 argvars[1].vval.v_string,
6277 string_in,
6278 argvars[2].vval.v_number,
6279 string_result,
6280 &nr_result) == OK
6281 && type == VAR_NUMBER)
6282 rettv->vval.v_number = nr_result;
6283 }
6284#endif
6285}
6286
6287/*
6288 * "libcall()" function
6289 */
6290 static void
6291f_libcall(typval_T *argvars, typval_T *rettv)
6292{
6293 libcall_common(argvars, rettv, VAR_STRING);
6294}
6295
6296/*
6297 * "libcallnr()" function
6298 */
6299 static void
6300f_libcallnr(typval_T *argvars, typval_T *rettv)
6301{
6302 libcall_common(argvars, rettv, VAR_NUMBER);
6303}
6304
6305/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006306 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006307 */
6308 static void
6309f_line(typval_T *argvars, typval_T *rettv)
6310{
6311 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006312 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006313 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006314 int id;
6315 tabpage_T *tp;
6316 win_T *wp;
6317 win_T *save_curwin;
6318 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006319
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006320 if (argvars[1].v_type != VAR_UNKNOWN)
6321 {
6322 // use window specified in the second argument
6323 id = (int)tv_get_number(&argvars[1]);
6324 wp = win_id2wp_tp(id, &tp);
6325 if (wp != NULL && tp != NULL)
6326 {
6327 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6328 == OK)
6329 {
6330 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006331 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006332 }
6333 restore_win_noblock(save_curwin, save_curtab, TRUE);
6334 }
6335 }
6336 else
6337 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006338 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006339
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006340 if (fp != NULL)
6341 lnum = fp->lnum;
6342 rettv->vval.v_number = lnum;
6343}
6344
6345/*
6346 * "line2byte(lnum)" function
6347 */
6348 static void
6349f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6350{
6351#ifndef FEAT_BYTEOFF
6352 rettv->vval.v_number = -1;
6353#else
6354 linenr_T lnum;
6355
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006356 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006357 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6358 rettv->vval.v_number = -1;
6359 else
6360 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6361 if (rettv->vval.v_number >= 0)
6362 ++rettv->vval.v_number;
6363#endif
6364}
6365
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006366#ifdef FEAT_FLOAT
6367/*
6368 * "log()" function
6369 */
6370 static void
6371f_log(typval_T *argvars, typval_T *rettv)
6372{
6373 float_T f = 0.0;
6374
6375 rettv->v_type = VAR_FLOAT;
6376 if (get_float_arg(argvars, &f) == OK)
6377 rettv->vval.v_float = log(f);
6378 else
6379 rettv->vval.v_float = 0.0;
6380}
6381
6382/*
6383 * "log10()" function
6384 */
6385 static void
6386f_log10(typval_T *argvars, typval_T *rettv)
6387{
6388 float_T f = 0.0;
6389
6390 rettv->v_type = VAR_FLOAT;
6391 if (get_float_arg(argvars, &f) == OK)
6392 rettv->vval.v_float = log10(f);
6393 else
6394 rettv->vval.v_float = 0.0;
6395}
6396#endif
6397
6398#ifdef FEAT_LUA
6399/*
6400 * "luaeval()" function
6401 */
6402 static void
6403f_luaeval(typval_T *argvars, typval_T *rettv)
6404{
6405 char_u *str;
6406 char_u buf[NUMBUFLEN];
6407
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006408 if (check_restricted() || check_secure())
6409 return;
6410
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006411 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006412 do_luaeval(str, argvars + 1, rettv);
6413}
6414#endif
6415
6416/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006417 * "maparg()" function
6418 */
6419 static void
6420f_maparg(typval_T *argvars, typval_T *rettv)
6421{
6422 get_maparg(argvars, rettv, TRUE);
6423}
6424
6425/*
6426 * "mapcheck()" function
6427 */
6428 static void
6429f_mapcheck(typval_T *argvars, typval_T *rettv)
6430{
6431 get_maparg(argvars, rettv, FALSE);
6432}
6433
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006434typedef enum
6435{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006436 MATCH_END, // matchend()
6437 MATCH_MATCH, // match()
6438 MATCH_STR, // matchstr()
6439 MATCH_LIST, // matchlist()
6440 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006441} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006442
6443 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006444find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006445{
6446 char_u *str = NULL;
6447 long len = 0;
6448 char_u *expr = NULL;
6449 char_u *pat;
6450 regmatch_T regmatch;
6451 char_u patbuf[NUMBUFLEN];
6452 char_u strbuf[NUMBUFLEN];
6453 char_u *save_cpo;
6454 long start = 0;
6455 long nth = 1;
6456 colnr_T startcol = 0;
6457 int match = 0;
6458 list_T *l = NULL;
6459 listitem_T *li = NULL;
6460 long idx = 0;
6461 char_u *tofree = NULL;
6462
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006463 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006464 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006465 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006466
6467 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006468 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006469 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006470 // type MATCH_LIST: return empty list when there are no matches.
6471 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006472 if (rettv_list_alloc(rettv) == FAIL)
6473 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006474 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006475 && (list_append_string(rettv->vval.v_list,
6476 (char_u *)"", 0) == FAIL
6477 || list_append_number(rettv->vval.v_list,
6478 (varnumber_T)-1) == FAIL
6479 || list_append_number(rettv->vval.v_list,
6480 (varnumber_T)-1) == FAIL
6481 || list_append_number(rettv->vval.v_list,
6482 (varnumber_T)-1) == FAIL))
6483 {
6484 list_free(rettv->vval.v_list);
6485 rettv->vval.v_list = NULL;
6486 goto theend;
6487 }
6488 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006489 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006490 {
6491 rettv->v_type = VAR_STRING;
6492 rettv->vval.v_string = NULL;
6493 }
6494
6495 if (argvars[0].v_type == VAR_LIST)
6496 {
6497 if ((l = argvars[0].vval.v_list) == NULL)
6498 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006499 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006500 li = l->lv_first;
6501 }
6502 else
6503 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006504 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006505 len = (long)STRLEN(str);
6506 }
6507
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006508 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006509 if (pat == NULL)
6510 goto theend;
6511
6512 if (argvars[2].v_type != VAR_UNKNOWN)
6513 {
6514 int error = FALSE;
6515
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006516 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006517 if (error)
6518 goto theend;
6519 if (l != NULL)
6520 {
6521 li = list_find(l, start);
6522 if (li == NULL)
6523 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006524 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006525 }
6526 else
6527 {
6528 if (start < 0)
6529 start = 0;
6530 if (start > len)
6531 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006532 // When "count" argument is there ignore matches before "start",
6533 // otherwise skip part of the string. Differs when pattern is "^"
6534 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006535 if (argvars[3].v_type != VAR_UNKNOWN)
6536 startcol = start;
6537 else
6538 {
6539 str += start;
6540 len -= start;
6541 }
6542 }
6543
6544 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006545 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006546 if (error)
6547 goto theend;
6548 }
6549
6550 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6551 if (regmatch.regprog != NULL)
6552 {
6553 regmatch.rm_ic = p_ic;
6554
6555 for (;;)
6556 {
6557 if (l != NULL)
6558 {
6559 if (li == NULL)
6560 {
6561 match = FALSE;
6562 break;
6563 }
6564 vim_free(tofree);
6565 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6566 if (str == NULL)
6567 break;
6568 }
6569
6570 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6571
6572 if (match && --nth <= 0)
6573 break;
6574 if (l == NULL && !match)
6575 break;
6576
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006577 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006578 if (l != NULL)
6579 {
6580 li = li->li_next;
6581 ++idx;
6582 }
6583 else
6584 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006585 startcol = (colnr_T)(regmatch.startp[0]
6586 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006587 if (startcol > (colnr_T)len
6588 || str + startcol <= regmatch.startp[0])
6589 {
6590 match = FALSE;
6591 break;
6592 }
6593 }
6594 }
6595
6596 if (match)
6597 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006598 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006599 {
6600 listitem_T *li1 = rettv->vval.v_list->lv_first;
6601 listitem_T *li2 = li1->li_next;
6602 listitem_T *li3 = li2->li_next;
6603 listitem_T *li4 = li3->li_next;
6604
6605 vim_free(li1->li_tv.vval.v_string);
6606 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006607 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006608 li3->li_tv.vval.v_number =
6609 (varnumber_T)(regmatch.startp[0] - expr);
6610 li4->li_tv.vval.v_number =
6611 (varnumber_T)(regmatch.endp[0] - expr);
6612 if (l != NULL)
6613 li2->li_tv.vval.v_number = (varnumber_T)idx;
6614 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006615 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006616 {
6617 int i;
6618
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006619 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006620 for (i = 0; i < NSUBEXP; ++i)
6621 {
6622 if (regmatch.endp[i] == NULL)
6623 {
6624 if (list_append_string(rettv->vval.v_list,
6625 (char_u *)"", 0) == FAIL)
6626 break;
6627 }
6628 else if (list_append_string(rettv->vval.v_list,
6629 regmatch.startp[i],
6630 (int)(regmatch.endp[i] - regmatch.startp[i]))
6631 == FAIL)
6632 break;
6633 }
6634 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006635 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006636 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006637 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006638 if (l != NULL)
6639 copy_tv(&li->li_tv, rettv);
6640 else
6641 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006642 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006643 }
6644 else if (l != NULL)
6645 rettv->vval.v_number = idx;
6646 else
6647 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006648 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006649 rettv->vval.v_number =
6650 (varnumber_T)(regmatch.startp[0] - str);
6651 else
6652 rettv->vval.v_number =
6653 (varnumber_T)(regmatch.endp[0] - str);
6654 rettv->vval.v_number += (varnumber_T)(str - expr);
6655 }
6656 }
6657 vim_regfree(regmatch.regprog);
6658 }
6659
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006660theend:
6661 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006662 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006663 listitem_remove(rettv->vval.v_list,
6664 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006665 vim_free(tofree);
6666 p_cpo = save_cpo;
6667}
6668
6669/*
6670 * "match()" function
6671 */
6672 static void
6673f_match(typval_T *argvars, typval_T *rettv)
6674{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006675 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006676}
6677
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006678/*
6679 * "matchend()" function
6680 */
6681 static void
6682f_matchend(typval_T *argvars, typval_T *rettv)
6683{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006684 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006685}
6686
6687/*
6688 * "matchlist()" function
6689 */
6690 static void
6691f_matchlist(typval_T *argvars, typval_T *rettv)
6692{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006693 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694}
6695
6696/*
6697 * "matchstr()" function
6698 */
6699 static void
6700f_matchstr(typval_T *argvars, typval_T *rettv)
6701{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006702 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006703}
6704
6705/*
6706 * "matchstrpos()" function
6707 */
6708 static void
6709f_matchstrpos(typval_T *argvars, typval_T *rettv)
6710{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006711 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006712}
6713
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006714 static void
6715max_min(typval_T *argvars, typval_T *rettv, int domax)
6716{
6717 varnumber_T n = 0;
6718 varnumber_T i;
6719 int error = FALSE;
6720
6721 if (argvars[0].v_type == VAR_LIST)
6722 {
6723 list_T *l;
6724 listitem_T *li;
6725
6726 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006727 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006728 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006729 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006730 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006731 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6732 n = l->lv_u.nonmat.lv_start;
6733 else
6734 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6735 * l->lv_u.nonmat.lv_stride;
6736 }
6737 else
6738 {
6739 li = l->lv_first;
6740 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006741 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006742 n = tv_get_number_chk(&li->li_tv, &error);
6743 for (;;)
6744 {
6745 li = li->li_next;
6746 if (li == NULL)
6747 break;
6748 i = tv_get_number_chk(&li->li_tv, &error);
6749 if (domax ? i > n : i < n)
6750 n = i;
6751 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006752 }
6753 }
6754 }
6755 }
6756 else if (argvars[0].v_type == VAR_DICT)
6757 {
6758 dict_T *d;
6759 int first = TRUE;
6760 hashitem_T *hi;
6761 int todo;
6762
6763 d = argvars[0].vval.v_dict;
6764 if (d != NULL)
6765 {
6766 todo = (int)d->dv_hashtab.ht_used;
6767 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6768 {
6769 if (!HASHITEM_EMPTY(hi))
6770 {
6771 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006772 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006773 if (first)
6774 {
6775 n = i;
6776 first = FALSE;
6777 }
6778 else if (domax ? i > n : i < n)
6779 n = i;
6780 }
6781 }
6782 }
6783 }
6784 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006785 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006786 rettv->vval.v_number = error ? 0 : n;
6787}
6788
6789/*
6790 * "max()" function
6791 */
6792 static void
6793f_max(typval_T *argvars, typval_T *rettv)
6794{
6795 max_min(argvars, rettv, TRUE);
6796}
6797
6798/*
6799 * "min()" function
6800 */
6801 static void
6802f_min(typval_T *argvars, typval_T *rettv)
6803{
6804 max_min(argvars, rettv, FALSE);
6805}
6806
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006807#if defined(FEAT_MZSCHEME) || defined(PROTO)
6808/*
6809 * "mzeval()" function
6810 */
6811 static void
6812f_mzeval(typval_T *argvars, typval_T *rettv)
6813{
6814 char_u *str;
6815 char_u buf[NUMBUFLEN];
6816
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006817 if (check_restricted() || check_secure())
6818 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006819 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006820 do_mzeval(str, rettv);
6821}
6822
6823 void
6824mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6825{
6826 typval_T argvars[3];
6827
6828 argvars[0].v_type = VAR_STRING;
6829 argvars[0].vval.v_string = name;
6830 copy_tv(args, &argvars[1]);
6831 argvars[2].v_type = VAR_UNKNOWN;
6832 f_call(argvars, rettv);
6833 clear_tv(&argvars[1]);
6834}
6835#endif
6836
6837/*
6838 * "nextnonblank()" function
6839 */
6840 static void
6841f_nextnonblank(typval_T *argvars, typval_T *rettv)
6842{
6843 linenr_T lnum;
6844
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006845 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006846 {
6847 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6848 {
6849 lnum = 0;
6850 break;
6851 }
6852 if (*skipwhite(ml_get(lnum)) != NUL)
6853 break;
6854 }
6855 rettv->vval.v_number = lnum;
6856}
6857
6858/*
6859 * "nr2char()" function
6860 */
6861 static void
6862f_nr2char(typval_T *argvars, typval_T *rettv)
6863{
6864 char_u buf[NUMBUFLEN];
6865
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006866 if (has_mbyte)
6867 {
6868 int utf8 = 0;
6869
6870 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006871 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006872 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006873 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006874 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006875 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006876 }
6877 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006878 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006879 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006880 buf[1] = NUL;
6881 }
6882 rettv->v_type = VAR_STRING;
6883 rettv->vval.v_string = vim_strsave(buf);
6884}
6885
6886/*
6887 * "or(expr, expr)" function
6888 */
6889 static void
6890f_or(typval_T *argvars, typval_T *rettv)
6891{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006892 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6893 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006894}
6895
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006896#ifdef FEAT_PERL
6897/*
6898 * "perleval()" function
6899 */
6900 static void
6901f_perleval(typval_T *argvars, typval_T *rettv)
6902{
6903 char_u *str;
6904 char_u buf[NUMBUFLEN];
6905
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006906 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006907 do_perleval(str, rettv);
6908}
6909#endif
6910
6911#ifdef FEAT_FLOAT
6912/*
6913 * "pow()" function
6914 */
6915 static void
6916f_pow(typval_T *argvars, typval_T *rettv)
6917{
6918 float_T fx = 0.0, fy = 0.0;
6919
6920 rettv->v_type = VAR_FLOAT;
6921 if (get_float_arg(argvars, &fx) == OK
6922 && get_float_arg(&argvars[1], &fy) == OK)
6923 rettv->vval.v_float = pow(fx, fy);
6924 else
6925 rettv->vval.v_float = 0.0;
6926}
6927#endif
6928
6929/*
6930 * "prevnonblank()" function
6931 */
6932 static void
6933f_prevnonblank(typval_T *argvars, typval_T *rettv)
6934{
6935 linenr_T lnum;
6936
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006937 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006938 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6939 lnum = 0;
6940 else
6941 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6942 --lnum;
6943 rettv->vval.v_number = lnum;
6944}
6945
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006946// This dummy va_list is here because:
6947// - passing a NULL pointer doesn't work when va_list isn't a pointer
6948// - locally in the function results in a "used before set" warning
6949// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006950static va_list ap;
6951
6952/*
6953 * "printf()" function
6954 */
6955 static void
6956f_printf(typval_T *argvars, typval_T *rettv)
6957{
6958 char_u buf[NUMBUFLEN];
6959 int len;
6960 char_u *s;
6961 int saved_did_emsg = did_emsg;
6962 char *fmt;
6963
6964 rettv->v_type = VAR_STRING;
6965 rettv->vval.v_string = NULL;
6966
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006967 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006968 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006969 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006970 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006971 if (!did_emsg)
6972 {
6973 s = alloc(len + 1);
6974 if (s != NULL)
6975 {
6976 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006977 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
6978 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006979 }
6980 }
6981 did_emsg |= saved_did_emsg;
6982}
6983
6984/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006985 * "pum_getpos()" function
6986 */
6987 static void
6988f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6989{
6990 if (rettv_dict_alloc(rettv) != OK)
6991 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006992 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006993}
6994
6995/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006996 * "pumvisible()" function
6997 */
6998 static void
6999f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7000{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007001 if (pum_visible())
7002 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007003}
7004
7005#ifdef FEAT_PYTHON3
7006/*
7007 * "py3eval()" function
7008 */
7009 static void
7010f_py3eval(typval_T *argvars, typval_T *rettv)
7011{
7012 char_u *str;
7013 char_u buf[NUMBUFLEN];
7014
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007015 if (check_restricted() || check_secure())
7016 return;
7017
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007018 if (p_pyx == 0)
7019 p_pyx = 3;
7020
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007021 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007022 do_py3eval(str, rettv);
7023}
7024#endif
7025
7026#ifdef FEAT_PYTHON
7027/*
7028 * "pyeval()" function
7029 */
7030 static void
7031f_pyeval(typval_T *argvars, typval_T *rettv)
7032{
7033 char_u *str;
7034 char_u buf[NUMBUFLEN];
7035
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007036 if (check_restricted() || check_secure())
7037 return;
7038
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007039 if (p_pyx == 0)
7040 p_pyx = 2;
7041
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007042 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007043 do_pyeval(str, rettv);
7044}
7045#endif
7046
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007047#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
7048/*
7049 * "pyxeval()" function
7050 */
7051 static void
7052f_pyxeval(typval_T *argvars, typval_T *rettv)
7053{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007054 if (check_restricted() || check_secure())
7055 return;
7056
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007057# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
7058 init_pyxversion();
7059 if (p_pyx == 2)
7060 f_pyeval(argvars, rettv);
7061 else
7062 f_py3eval(argvars, rettv);
7063# elif defined(FEAT_PYTHON)
7064 f_pyeval(argvars, rettv);
7065# elif defined(FEAT_PYTHON3)
7066 f_py3eval(argvars, rettv);
7067# endif
7068}
7069#endif
7070
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007071static UINT32_T srand_seed_for_testing = 0;
7072static int srand_seed_for_testing_is_used = FALSE;
7073
7074 static void
7075f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
7076{
7077 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007078 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007079 else
7080 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007081 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
7082 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007083 }
7084}
7085
7086 static void
7087init_srand(UINT32_T *x)
7088{
7089#ifndef MSWIN
7090 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
7091#endif
7092
7093 if (srand_seed_for_testing_is_used)
7094 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007095 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007096 return;
7097 }
7098#ifndef MSWIN
7099 if (dev_urandom_state != FAIL)
7100 {
7101 int fd = open("/dev/urandom", O_RDONLY);
7102 struct {
7103 union {
7104 UINT32_T number;
7105 char bytes[sizeof(UINT32_T)];
7106 } contents;
7107 } buf;
7108
7109 // Attempt reading /dev/urandom.
7110 if (fd == -1)
7111 dev_urandom_state = FAIL;
7112 else
7113 {
7114 buf.contents.number = 0;
7115 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
7116 != sizeof(UINT32_T))
7117 dev_urandom_state = FAIL;
7118 else
7119 {
7120 dev_urandom_state = OK;
7121 *x = buf.contents.number;
7122 }
7123 close(fd);
7124 }
7125 }
7126 if (dev_urandom_state != OK)
7127 // Reading /dev/urandom doesn't work, fall back to time().
7128#endif
7129 *x = vim_time();
7130}
7131
7132#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
7133#define SPLITMIX32(x, z) ( \
7134 z = (x += 0x9e3779b9), \
7135 z = (z ^ (z >> 16)) * 0x85ebca6b, \
7136 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
7137 z ^ (z >> 16) \
7138 )
7139#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
7140 result = ROTL(y * 5, 7) * 9; \
7141 t = y << 9; \
7142 z ^= x; \
7143 w ^= y; \
7144 y ^= z, x ^= w; \
7145 z ^= t; \
7146 w = ROTL(w, 11);
7147
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007148/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007149 * "rand()" function
7150 */
7151 static void
7152f_rand(typval_T *argvars, typval_T *rettv)
7153{
7154 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007155 static UINT32_T gx, gy, gz, gw;
7156 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007157 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01007158 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007159
7160 if (argvars[0].v_type == VAR_UNKNOWN)
7161 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007162 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007163 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007164 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007165 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007166 init_srand(&x);
7167
7168 gx = SPLITMIX32(x, z);
7169 gy = SPLITMIX32(x, z);
7170 gz = SPLITMIX32(x, z);
7171 gw = SPLITMIX32(x, z);
7172 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007173 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007174
7175 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007176 }
7177 else if (argvars[0].v_type == VAR_LIST)
7178 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007179 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007180 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007181 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007182
7183 lx = list_find(l, 0L);
7184 ly = list_find(l, 1L);
7185 lz = list_find(l, 2L);
7186 lw = list_find(l, 3L);
7187 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
7188 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
7189 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
7190 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
7191 x = (UINT32_T)lx->li_tv.vval.v_number;
7192 y = (UINT32_T)ly->li_tv.vval.v_number;
7193 z = (UINT32_T)lz->li_tv.vval.v_number;
7194 w = (UINT32_T)lw->li_tv.vval.v_number;
7195
7196 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
7197
7198 lx->li_tv.vval.v_number = (varnumber_T)x;
7199 ly->li_tv.vval.v_number = (varnumber_T)y;
7200 lz->li_tv.vval.v_number = (varnumber_T)z;
7201 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007202 }
7203 else
7204 goto theend;
7205
7206 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007207 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007208 return;
7209
7210theend:
7211 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01007212 rettv->v_type = VAR_NUMBER;
7213 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007214}
7215
7216/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007217 * "srand()" function
7218 */
7219 static void
7220f_srand(typval_T *argvars, typval_T *rettv)
7221{
7222 UINT32_T x = 0, z;
7223
7224 if (rettv_list_alloc(rettv) == FAIL)
7225 return;
7226 if (argvars[0].v_type == VAR_UNKNOWN)
7227 {
7228 init_srand(&x);
7229 }
7230 else
7231 {
7232 int error = FALSE;
7233
7234 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7235 if (error)
7236 return;
7237 }
7238
7239 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7240 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7241 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7242 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7243}
7244
7245#undef ROTL
7246#undef SPLITMIX32
7247#undef SHUFFLE_XOSHIRO128STARSTAR
7248
7249/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007250 * "range()" function
7251 */
7252 static void
7253f_range(typval_T *argvars, typval_T *rettv)
7254{
7255 varnumber_T start;
7256 varnumber_T end;
7257 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007258 int error = FALSE;
7259
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007260 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007261 if (argvars[1].v_type == VAR_UNKNOWN)
7262 {
7263 end = start - 1;
7264 start = 0;
7265 }
7266 else
7267 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007268 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007269 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007270 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007271 }
7272
7273 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007274 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007275 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007276 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007277 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007278 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007279 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007280 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281 list_T *list = rettv->vval.v_list;
7282
7283 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007284 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007285 // be called.
7286 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007287 list->lv_u.nonmat.lv_start = start;
7288 list->lv_u.nonmat.lv_end = end;
7289 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007290 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007291 }
7292}
7293
7294/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007295 * Materialize "list".
7296 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007297 */
7298 void
7299range_list_materialize(list_T *list)
7300{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007301 varnumber_T start = list->lv_u.nonmat.lv_start;
7302 varnumber_T end = list->lv_u.nonmat.lv_end;
7303 int stride = list->lv_u.nonmat.lv_stride;
7304 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007305
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007306 list->lv_first = NULL;
7307 list->lv_u.mat.lv_last = NULL;
7308 list->lv_len = 0;
7309 list->lv_u.mat.lv_idx_item = NULL;
7310 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7311 if (list_append_number(list, (varnumber_T)i) == FAIL)
7312 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007313}
7314
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007315/*
7316 * "getreginfo()" function
7317 */
7318 static void
7319f_getreginfo(typval_T *argvars, typval_T *rettv)
7320{
7321 char_u *strregname;
7322 int regname;
7323 char_u buf[NUMBUFLEN + 2];
7324 long reglen = 0;
7325 dict_T *dict;
7326 list_T *list;
7327
7328 if (argvars[0].v_type != VAR_UNKNOWN)
7329 {
7330 strregname = tv_get_string_chk(&argvars[0]);
7331 if (strregname == NULL)
7332 return;
7333 }
7334 else
7335 strregname = get_vim_var_str(VV_REG);
7336
7337 regname = (strregname == NULL ? '"' : *strregname);
7338 if (regname == 0 || regname == '@')
7339 regname = '"';
7340
7341 if (rettv_dict_alloc(rettv) == FAIL)
7342 return;
7343 dict = rettv->vval.v_dict;
7344
7345 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7346 if (list == NULL)
7347 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007348 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007349
7350 buf[0] = NUL;
7351 buf[1] = NUL;
7352 switch (get_reg_type(regname, &reglen))
7353 {
7354 case MLINE: buf[0] = 'V'; break;
7355 case MCHAR: buf[0] = 'v'; break;
7356 case MBLOCK:
7357 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7358 reglen + 1);
7359 break;
7360 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007361 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007362
7363 buf[0] = get_register_name(get_unname_register());
7364 buf[1] = NUL;
7365 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007366 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007367 else
7368 {
7369 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7370
7371 if (item != NULL)
7372 {
7373 item->di_tv.v_type = VAR_SPECIAL;
7374 item->di_tv.vval.v_number = regname == buf[0]
7375 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007376 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007377 }
7378 }
7379}
7380
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007381 static void
7382return_register(int regname, typval_T *rettv)
7383{
7384 char_u buf[2] = {0, 0};
7385
7386 buf[0] = (char_u)regname;
7387 rettv->v_type = VAR_STRING;
7388 rettv->vval.v_string = vim_strsave(buf);
7389}
7390
7391/*
7392 * "reg_executing()" function
7393 */
7394 static void
7395f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7396{
7397 return_register(reg_executing, rettv);
7398}
7399
7400/*
7401 * "reg_recording()" function
7402 */
7403 static void
7404f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7405{
7406 return_register(reg_recording, rettv);
7407}
7408
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007409/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007410 * "rename({from}, {to})" function
7411 */
7412 static void
7413f_rename(typval_T *argvars, typval_T *rettv)
7414{
7415 char_u buf[NUMBUFLEN];
7416
7417 if (check_restricted() || check_secure())
7418 rettv->vval.v_number = -1;
7419 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007420 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7421 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007422}
7423
7424/*
7425 * "repeat()" function
7426 */
7427 static void
7428f_repeat(typval_T *argvars, typval_T *rettv)
7429{
7430 char_u *p;
7431 int n;
7432 int slen;
7433 int len;
7434 char_u *r;
7435 int i;
7436
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007437 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007438 if (argvars[0].v_type == VAR_LIST)
7439 {
7440 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7441 while (n-- > 0)
7442 if (list_extend(rettv->vval.v_list,
7443 argvars[0].vval.v_list, NULL) == FAIL)
7444 break;
7445 }
7446 else
7447 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007448 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007449 rettv->v_type = VAR_STRING;
7450 rettv->vval.v_string = NULL;
7451
7452 slen = (int)STRLEN(p);
7453 len = slen * n;
7454 if (len <= 0)
7455 return;
7456
7457 r = alloc(len + 1);
7458 if (r != NULL)
7459 {
7460 for (i = 0; i < n; i++)
7461 mch_memmove(r + i * slen, p, (size_t)slen);
7462 r[len] = NUL;
7463 }
7464
7465 rettv->vval.v_string = r;
7466 }
7467}
7468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007469#define SP_NOMOVE 0x01 // don't move cursor
7470#define SP_REPEAT 0x02 // repeat to find outer pair
7471#define SP_RETCOUNT 0x04 // return matchcount
7472#define SP_SETPCMARK 0x08 // set previous context mark
7473#define SP_START 0x10 // accept match at start position
7474#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7475#define SP_END 0x40 // leave cursor at end of match
7476#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007477
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007478/*
7479 * Get flags for a search function.
7480 * Possibly sets "p_ws".
7481 * Returns BACKWARD, FORWARD or zero (for an error).
7482 */
7483 static int
7484get_search_arg(typval_T *varp, int *flagsp)
7485{
7486 int dir = FORWARD;
7487 char_u *flags;
7488 char_u nbuf[NUMBUFLEN];
7489 int mask;
7490
7491 if (varp->v_type != VAR_UNKNOWN)
7492 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007493 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007494 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007495 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007496 while (*flags != NUL)
7497 {
7498 switch (*flags)
7499 {
7500 case 'b': dir = BACKWARD; break;
7501 case 'w': p_ws = TRUE; break;
7502 case 'W': p_ws = FALSE; break;
7503 default: mask = 0;
7504 if (flagsp != NULL)
7505 switch (*flags)
7506 {
7507 case 'c': mask = SP_START; break;
7508 case 'e': mask = SP_END; break;
7509 case 'm': mask = SP_RETCOUNT; break;
7510 case 'n': mask = SP_NOMOVE; break;
7511 case 'p': mask = SP_SUBPAT; break;
7512 case 'r': mask = SP_REPEAT; break;
7513 case 's': mask = SP_SETPCMARK; break;
7514 case 'z': mask = SP_COLUMN; break;
7515 }
7516 if (mask == 0)
7517 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007518 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007519 dir = 0;
7520 }
7521 else
7522 *flagsp |= mask;
7523 }
7524 if (dir == 0)
7525 break;
7526 ++flags;
7527 }
7528 }
7529 return dir;
7530}
7531
7532/*
7533 * Shared by search() and searchpos() functions.
7534 */
7535 static int
7536search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7537{
7538 int flags;
7539 char_u *pat;
7540 pos_T pos;
7541 pos_T save_cursor;
7542 int save_p_ws = p_ws;
7543 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007544 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007545 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007546#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007547 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007548 long time_limit = 0;
7549#endif
7550 int options = SEARCH_KEEP;
7551 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007552 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007553 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007554 pos_T firstpos;
7555
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007556 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007557 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007558 if (dir == 0)
7559 goto theend;
7560 flags = *flagsp;
7561 if (flags & SP_START)
7562 options |= SEARCH_START;
7563 if (flags & SP_END)
7564 options |= SEARCH_END;
7565 if (flags & SP_COLUMN)
7566 options |= SEARCH_COL;
7567
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007568 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007569 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7570 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007571 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007572 if (lnum_stop < 0)
7573 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007574 if (argvars[3].v_type != VAR_UNKNOWN)
7575 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007576#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007577 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007578 if (time_limit < 0)
7579 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007580#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007581 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007582 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007583 }
7584
7585#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007586 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007587 profile_setlimit(time_limit, &tm);
7588#endif
7589
7590 /*
7591 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7592 * Check to make sure only those flags are set.
7593 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7594 * flags cannot be set. Check for that condition also.
7595 */
7596 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7597 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7598 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007599 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007600 goto theend;
7601 }
7602
7603 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007604 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007605 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007606 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7607#ifdef FEAT_RELTIME
7608 sia.sa_tm = &tm;
7609#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007610
7611 // Repeat until {skip} returns FALSE.
7612 for (;;)
7613 {
7614 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007615 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007616 // finding the first match again means there is no match where {skip}
7617 // evaluates to zero.
7618 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7619 subpatnum = FAIL;
7620
Bram Moolenaara9c01042020-06-07 14:50:50 +02007621 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007622 // didn't find it or no skip argument
7623 break;
7624 firstpos = pos;
7625
Bram Moolenaara9c01042020-06-07 14:50:50 +02007626 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007627 {
7628 int do_skip;
7629 int err;
7630 pos_T save_pos = curwin->w_cursor;
7631
7632 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007633 err = FALSE;
7634 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007635 curwin->w_cursor = save_pos;
7636 if (err)
7637 {
7638 // Evaluating {skip} caused an error, break here.
7639 subpatnum = FAIL;
7640 break;
7641 }
7642 if (!do_skip)
7643 break;
7644 }
7645 }
7646
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007647 if (subpatnum != FAIL)
7648 {
7649 if (flags & SP_SUBPAT)
7650 retval = subpatnum;
7651 else
7652 retval = pos.lnum;
7653 if (flags & SP_SETPCMARK)
7654 setpcmark();
7655 curwin->w_cursor = pos;
7656 if (match_pos != NULL)
7657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007658 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007659 match_pos->lnum = pos.lnum;
7660 match_pos->col = pos.col + 1;
7661 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007662 // "/$" will put the cursor after the end of the line, may need to
7663 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007664 check_cursor();
7665 }
7666
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007667 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007668 if (flags & SP_NOMOVE)
7669 curwin->w_cursor = save_cursor;
7670 else
7671 curwin->w_set_curswant = TRUE;
7672theend:
7673 p_ws = save_p_ws;
7674
7675 return retval;
7676}
7677
7678#ifdef FEAT_FLOAT
7679
7680/*
7681 * round() is not in C90, use ceil() or floor() instead.
7682 */
7683 float_T
7684vim_round(float_T f)
7685{
7686 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
7687}
7688
7689/*
7690 * "round({float})" function
7691 */
7692 static void
7693f_round(typval_T *argvars, typval_T *rettv)
7694{
7695 float_T f = 0.0;
7696
7697 rettv->v_type = VAR_FLOAT;
7698 if (get_float_arg(argvars, &f) == OK)
7699 rettv->vval.v_float = vim_round(f);
7700 else
7701 rettv->vval.v_float = 0.0;
7702}
7703#endif
7704
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007705#ifdef FEAT_RUBY
7706/*
7707 * "rubyeval()" function
7708 */
7709 static void
7710f_rubyeval(typval_T *argvars, typval_T *rettv)
7711{
7712 char_u *str;
7713 char_u buf[NUMBUFLEN];
7714
7715 str = tv_get_string_buf(&argvars[0], buf);
7716 do_rubyeval(str, rettv);
7717}
7718#endif
7719
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007720/*
7721 * "screenattr()" function
7722 */
7723 static void
7724f_screenattr(typval_T *argvars, typval_T *rettv)
7725{
7726 int row;
7727 int col;
7728 int c;
7729
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007730 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7731 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007732 if (row < 0 || row >= screen_Rows
7733 || col < 0 || col >= screen_Columns)
7734 c = -1;
7735 else
7736 c = ScreenAttrs[LineOffset[row] + col];
7737 rettv->vval.v_number = c;
7738}
7739
7740/*
7741 * "screenchar()" function
7742 */
7743 static void
7744f_screenchar(typval_T *argvars, typval_T *rettv)
7745{
7746 int row;
7747 int col;
7748 int off;
7749 int c;
7750
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007751 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7752 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007753 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007754 c = -1;
7755 else
7756 {
7757 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007758 if (enc_utf8 && ScreenLinesUC[off] != 0)
7759 c = ScreenLinesUC[off];
7760 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007761 c = ScreenLines[off];
7762 }
7763 rettv->vval.v_number = c;
7764}
7765
7766/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007767 * "screenchars()" function
7768 */
7769 static void
7770f_screenchars(typval_T *argvars, typval_T *rettv)
7771{
7772 int row;
7773 int col;
7774 int off;
7775 int c;
7776 int i;
7777
7778 if (rettv_list_alloc(rettv) == FAIL)
7779 return;
7780 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7781 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7782 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7783 return;
7784
7785 off = LineOffset[row] + col;
7786 if (enc_utf8 && ScreenLinesUC[off] != 0)
7787 c = ScreenLinesUC[off];
7788 else
7789 c = ScreenLines[off];
7790 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7791
7792 if (enc_utf8)
7793
7794 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7795 list_append_number(rettv->vval.v_list,
7796 (varnumber_T)ScreenLinesC[i][off]);
7797}
7798
7799/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007800 * "screencol()" function
7801 *
7802 * First column is 1 to be consistent with virtcol().
7803 */
7804 static void
7805f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7806{
7807 rettv->vval.v_number = screen_screencol() + 1;
7808}
7809
7810/*
7811 * "screenrow()" function
7812 */
7813 static void
7814f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7815{
7816 rettv->vval.v_number = screen_screenrow() + 1;
7817}
7818
7819/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007820 * "screenstring()" function
7821 */
7822 static void
7823f_screenstring(typval_T *argvars, typval_T *rettv)
7824{
7825 int row;
7826 int col;
7827 int off;
7828 int c;
7829 int i;
7830 char_u buf[MB_MAXBYTES + 1];
7831 int buflen = 0;
7832
7833 rettv->vval.v_string = NULL;
7834 rettv->v_type = VAR_STRING;
7835
7836 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7837 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7838 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7839 return;
7840
7841 off = LineOffset[row] + col;
7842 if (enc_utf8 && ScreenLinesUC[off] != 0)
7843 c = ScreenLinesUC[off];
7844 else
7845 c = ScreenLines[off];
7846 buflen += mb_char2bytes(c, buf);
7847
7848 if (enc_utf8)
7849 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7850 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7851
7852 buf[buflen] = NUL;
7853 rettv->vval.v_string = vim_strsave(buf);
7854}
7855
7856/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007857 * "search()" function
7858 */
7859 static void
7860f_search(typval_T *argvars, typval_T *rettv)
7861{
7862 int flags = 0;
7863
7864 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7865}
7866
7867/*
7868 * "searchdecl()" function
7869 */
7870 static void
7871f_searchdecl(typval_T *argvars, typval_T *rettv)
7872{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007873 int locally = TRUE;
7874 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007875 int error = FALSE;
7876 char_u *name;
7877
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007878 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007879
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007880 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007881 if (argvars[1].v_type != VAR_UNKNOWN)
7882 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007883 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007884 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007885 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007886 }
7887 if (!error && name != NULL)
7888 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7889 locally, thisblock, SEARCH_KEEP) == FAIL;
7890}
7891
7892/*
7893 * Used by searchpair() and searchpairpos()
7894 */
7895 static int
7896searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7897{
7898 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007899 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007900 int save_p_ws = p_ws;
7901 int dir;
7902 int flags = 0;
7903 char_u nbuf1[NUMBUFLEN];
7904 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007905 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007906 long lnum_stop = 0;
7907 long time_limit = 0;
7908
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007909 // Get the three pattern arguments: start, middle, end. Will result in an
7910 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007911 spat = tv_get_string_chk(&argvars[0]);
7912 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7913 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007914 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007915 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007916
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007917 // Handle the optional fourth argument: flags
7918 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007919 if (dir == 0)
7920 goto theend;
7921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007922 // Don't accept SP_END or SP_SUBPAT.
7923 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007924 if ((flags & (SP_END | SP_SUBPAT)) != 0
7925 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7926 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007927 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007928 goto theend;
7929 }
7930
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007931 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007932 if (flags & SP_REPEAT)
7933 p_ws = FALSE;
7934
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007935 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007936 if (argvars[3].v_type == VAR_UNKNOWN
7937 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007938 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007939 else
7940 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007941 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007942 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007943
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007944 if (argvars[5].v_type != VAR_UNKNOWN)
7945 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007946 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007947 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007948 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007949 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007950 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007951 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007952#ifdef FEAT_RELTIME
7953 if (argvars[6].v_type != VAR_UNKNOWN)
7954 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007955 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007956 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007957 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007958 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007959 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007960 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007961 }
7962#endif
7963 }
7964 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007965
7966 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
7967 match_pos, lnum_stop, time_limit);
7968
7969theend:
7970 p_ws = save_p_ws;
7971
7972 return retval;
7973}
7974
7975/*
7976 * "searchpair()" function
7977 */
7978 static void
7979f_searchpair(typval_T *argvars, typval_T *rettv)
7980{
7981 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
7982}
7983
7984/*
7985 * "searchpairpos()" function
7986 */
7987 static void
7988f_searchpairpos(typval_T *argvars, typval_T *rettv)
7989{
7990 pos_T match_pos;
7991 int lnum = 0;
7992 int col = 0;
7993
7994 if (rettv_list_alloc(rettv) == FAIL)
7995 return;
7996
7997 if (searchpair_cmn(argvars, &match_pos) > 0)
7998 {
7999 lnum = match_pos.lnum;
8000 col = match_pos.col;
8001 }
8002
8003 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8004 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8005}
8006
8007/*
8008 * Search for a start/middle/end thing.
8009 * Used by searchpair(), see its documentation for the details.
8010 * Returns 0 or -1 for no match,
8011 */
8012 long
8013do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008014 char_u *spat, // start pattern
8015 char_u *mpat, // middle pattern
8016 char_u *epat, // end pattern
8017 int dir, // BACKWARD or FORWARD
8018 typval_T *skip, // skip expression
8019 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008020 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008021 linenr_T lnum_stop, // stop at this line if not zero
8022 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008023{
8024 char_u *save_cpo;
8025 char_u *pat, *pat2 = NULL, *pat3 = NULL;
8026 long retval = 0;
8027 pos_T pos;
8028 pos_T firstpos;
8029 pos_T foundpos;
8030 pos_T save_cursor;
8031 pos_T save_pos;
8032 int n;
8033 int r;
8034 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01008035 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036 int err;
8037 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008038#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008039 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008040#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008041
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008042 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008043 save_cpo = p_cpo;
8044 p_cpo = empty_option;
8045
8046#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008047 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008048 profile_setlimit(time_limit, &tm);
8049#endif
8050
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008051 // Make two search patterns: start/end (pat2, for in nested pairs) and
8052 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02008053 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
8054 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008055 if (pat2 == NULL || pat3 == NULL)
8056 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008057 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008058 if (*mpat == NUL)
8059 STRCPY(pat3, pat2);
8060 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01008061 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008062 spat, epat, mpat);
8063 if (flags & SP_START)
8064 options |= SEARCH_START;
8065
Bram Moolenaar48570482017-10-30 21:48:41 +01008066 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02008067 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01008068
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008069 save_cursor = curwin->w_cursor;
8070 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008071 CLEAR_POS(&firstpos);
8072 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008073 pat = pat3;
8074 for (;;)
8075 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008076 searchit_arg_T sia;
8077
Bram Moolenaara80faa82020-04-12 19:37:17 +02008078 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008079 sia.sa_stop_lnum = lnum_stop;
8080#ifdef FEAT_RELTIME
8081 sia.sa_tm = &tm;
8082#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01008083 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02008084 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008085 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008086 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008087 break;
8088
8089 if (firstpos.lnum == 0)
8090 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008091 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008092 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008093 // Found the same position again. Can happen with a pattern that
8094 // has "\zs" at the end and searching backwards. Advance one
8095 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008096 if (dir == BACKWARD)
8097 decl(&pos);
8098 else
8099 incl(&pos);
8100 }
8101 foundpos = pos;
8102
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008103 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008104 options &= ~SEARCH_START;
8105
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008106 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01008107 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008108 {
8109 save_pos = curwin->w_cursor;
8110 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01008111 err = FALSE;
8112 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008113 curwin->w_cursor = save_pos;
8114 if (err)
8115 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008116 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008117 curwin->w_cursor = save_cursor;
8118 retval = -1;
8119 break;
8120 }
8121 if (r)
8122 continue;
8123 }
8124
8125 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8126 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008127 // Found end when searching backwards or start when searching
8128 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008129 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008130 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008131 }
8132 else
8133 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008134 // Found end when searching forward or start when searching
8135 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008136 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008137 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008138 }
8139
8140 if (nest == 0)
8141 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008142 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008143 if (flags & SP_RETCOUNT)
8144 ++retval;
8145 else
8146 retval = pos.lnum;
8147 if (flags & SP_SETPCMARK)
8148 setpcmark();
8149 curwin->w_cursor = pos;
8150 if (!(flags & SP_REPEAT))
8151 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008152 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008153 }
8154 }
8155
8156 if (match_pos != NULL)
8157 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008158 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008159 match_pos->lnum = curwin->w_cursor.lnum;
8160 match_pos->col = curwin->w_cursor.col + 1;
8161 }
8162
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008163 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008164 if ((flags & SP_NOMOVE) || retval == 0)
8165 curwin->w_cursor = save_cursor;
8166
8167theend:
8168 vim_free(pat2);
8169 vim_free(pat3);
8170 if (p_cpo == empty_option)
8171 p_cpo = save_cpo;
8172 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008173 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008174 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008175 // If it's still empty it was changed and restored, need to restore in
8176 // the complicated way.
8177 if (*p_cpo == NUL)
8178 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008179 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008180 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008181
8182 return retval;
8183}
8184
8185/*
8186 * "searchpos()" function
8187 */
8188 static void
8189f_searchpos(typval_T *argvars, typval_T *rettv)
8190{
8191 pos_T match_pos;
8192 int lnum = 0;
8193 int col = 0;
8194 int n;
8195 int flags = 0;
8196
8197 if (rettv_list_alloc(rettv) == FAIL)
8198 return;
8199
8200 n = search_cmn(argvars, &match_pos, &flags);
8201 if (n > 0)
8202 {
8203 lnum = match_pos.lnum;
8204 col = match_pos.col;
8205 }
8206
8207 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8208 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8209 if (flags & SP_SUBPAT)
8210 list_append_number(rettv->vval.v_list, (varnumber_T)n);
8211}
8212
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008213/*
8214 * Set the cursor or mark position.
8215 * If 'charpos' is TRUE, then use the column number as a character offet.
8216 * Otherwise use the column number as a byte offset.
8217 */
8218 static void
8219set_position(typval_T *argvars, typval_T *rettv, int charpos)
8220{
8221 pos_T pos;
8222 int fnum;
8223 char_u *name;
8224 colnr_T curswant = -1;
8225
8226 rettv->vval.v_number = -1;
8227
8228 name = tv_get_string_chk(argvars);
8229 if (name != NULL)
8230 {
8231 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
8232 {
8233 if (pos.col != MAXCOL && --pos.col < 0)
8234 pos.col = 0;
8235 if ((name[0] == '.' && name[1] == NUL))
8236 {
8237 // set cursor; "fnum" is ignored
8238 curwin->w_cursor = pos;
8239 if (curswant >= 0)
8240 {
8241 curwin->w_curswant = curswant - 1;
8242 curwin->w_set_curswant = FALSE;
8243 }
8244 check_cursor();
8245 rettv->vval.v_number = 0;
8246 }
8247 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8248 {
8249 // set mark
8250 if (setmark_pos(name[1], &pos, fnum) == OK)
8251 rettv->vval.v_number = 0;
8252 }
8253 else
8254 emsg(_(e_invarg));
8255 }
8256 }
8257}
8258/*
8259 * "setcharpos()" function
8260 */
8261 static void
8262f_setcharpos(typval_T *argvars, typval_T *rettv)
8263{
8264 set_position(argvars, rettv, TRUE);
8265}
8266
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008267 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008268f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8269{
8270 dict_T *d;
8271 dictitem_T *di;
8272 char_u *csearch;
8273
8274 if (argvars[0].v_type != VAR_DICT)
8275 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008276 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008277 return;
8278 }
8279
8280 if ((d = argvars[0].vval.v_dict) != NULL)
8281 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008282 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008283 if (csearch != NULL)
8284 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008285 if (enc_utf8)
8286 {
8287 int pcc[MAX_MCO];
8288 int c = utfc_ptr2char(csearch, pcc);
8289
8290 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8291 }
8292 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008293 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008294 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008295 }
8296
8297 di = dict_find(d, (char_u *)"forward", -1);
8298 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008299 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008300 ? FORWARD : BACKWARD);
8301
8302 di = dict_find(d, (char_u *)"until", -1);
8303 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008304 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008305 }
8306}
8307
8308/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008309 * "setcursorcharpos" function
8310 */
8311 static void
8312f_setcursorcharpos(typval_T *argvars, typval_T *rettv UNUSED)
8313{
8314 set_cursorpos(argvars, rettv, TRUE);
8315}
8316
8317/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008318 * "setenv()" function
8319 */
8320 static void
8321f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8322{
8323 char_u namebuf[NUMBUFLEN];
8324 char_u valbuf[NUMBUFLEN];
8325 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8326
8327 if (argvars[1].v_type == VAR_SPECIAL
8328 && argvars[1].vval.v_number == VVAL_NULL)
8329 vim_unsetenv(name);
8330 else
8331 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8332}
8333
8334/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008335 * "setfperm({fname}, {mode})" function
8336 */
8337 static void
8338f_setfperm(typval_T *argvars, typval_T *rettv)
8339{
8340 char_u *fname;
8341 char_u modebuf[NUMBUFLEN];
8342 char_u *mode_str;
8343 int i;
8344 int mask;
8345 int mode = 0;
8346
8347 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008348 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008349 if (fname == NULL)
8350 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008351 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008352 if (mode_str == NULL)
8353 return;
8354 if (STRLEN(mode_str) != 9)
8355 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008356 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008357 return;
8358 }
8359
8360 mask = 1;
8361 for (i = 8; i >= 0; --i)
8362 {
8363 if (mode_str[i] != '-')
8364 mode |= mask;
8365 mask = mask << 1;
8366 }
8367 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8368}
8369
8370/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008371 * "setpos()" function
8372 */
8373 static void
8374f_setpos(typval_T *argvars, typval_T *rettv)
8375{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008376 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008377}
8378
8379/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008380 * Translate a register type string to the yank type and block length
8381 */
8382 static int
8383get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8384{
8385 char_u *stropt = *pp;
8386 switch (*stropt)
8387 {
8388 case 'v': case 'c': // character-wise selection
8389 *yank_type = MCHAR;
8390 break;
8391 case 'V': case 'l': // line-wise selection
8392 *yank_type = MLINE;
8393 break;
8394 case 'b': case Ctrl_V: // block-wise selection
8395 *yank_type = MBLOCK;
8396 if (VIM_ISDIGIT(stropt[1]))
8397 {
8398 ++stropt;
8399 *block_len = getdigits(&stropt) - 1;
8400 --stropt;
8401 }
8402 break;
8403 default:
8404 return FAIL;
8405 }
8406 *pp = stropt;
8407 return OK;
8408}
8409
8410/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008411 * "setreg()" function
8412 */
8413 static void
8414f_setreg(typval_T *argvars, typval_T *rettv)
8415{
8416 int regname;
8417 char_u *strregname;
8418 char_u *stropt;
8419 char_u *strval;
8420 int append;
8421 char_u yank_type;
8422 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008423 typval_T *regcontents;
8424 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008425
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008426 pointreg = 0;
8427 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008428 block_len = -1;
8429 yank_type = MAUTO;
8430 append = FALSE;
8431
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008432 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008433 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008434
8435 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008436 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008437 regname = *strregname;
8438 if (regname == 0 || regname == '@')
8439 regname = '"';
8440
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008441 if (argvars[1].v_type == VAR_DICT)
8442 {
8443 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008444 dictitem_T *di;
8445
8446 if (d == NULL || d->dv_hashtab.ht_used == 0)
8447 {
8448 // Empty dict, clear the register (like setreg(0, []))
8449 char_u *lstval[2] = {NULL, NULL};
8450 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8451 return;
8452 }
8453
8454 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008455 if (di != NULL)
8456 regcontents = &di->di_tv;
8457
8458 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8459 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008460 {
8461 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8462
8463 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008464 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008465 semsg(_(e_invargval), "value");
8466 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008467 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008468 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008469
8470 if (regname == '"')
8471 {
8472 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8473 if (stropt != NULL)
8474 {
8475 pointreg = *stropt;
8476 regname = pointreg;
8477 }
8478 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008479 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008480 pointreg = regname;
8481 }
8482 else
8483 regcontents = &argvars[1];
8484
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008485 if (argvars[2].v_type != VAR_UNKNOWN)
8486 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008487 if (yank_type != MAUTO)
8488 {
8489 semsg(_(e_toomanyarg), "setreg");
8490 return;
8491 }
8492
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008493 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008494 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008495 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008496 for (; *stropt != NUL; ++stropt)
8497 switch (*stropt)
8498 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008499 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008500 append = TRUE;
8501 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008502 default:
8503 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008504 }
8505 }
8506
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008507 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008508 {
8509 char_u **lstval;
8510 char_u **allocval;
8511 char_u buf[NUMBUFLEN];
8512 char_u **curval;
8513 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008514 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008515 listitem_T *li;
8516 int len;
8517
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008518 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008519 len = ll == NULL ? 0 : ll->lv_len;
8520
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008521 // First half: use for pointers to result lines; second half: use for
8522 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008523 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008524 if (lstval == NULL)
8525 return;
8526 curval = lstval;
8527 allocval = lstval + len + 2;
8528 curallocval = allocval;
8529
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008530 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008531 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008532 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008533 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008534 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008535 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008536 if (strval == NULL)
8537 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008538 if (strval == buf)
8539 {
8540 // Need to make a copy, next tv_get_string_buf_chk() will
8541 // overwrite the string.
8542 strval = vim_strsave(buf);
8543 if (strval == NULL)
8544 goto free_lstval;
8545 *curallocval++ = strval;
8546 }
8547 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008548 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008549 }
8550 *curval++ = NULL;
8551
8552 write_reg_contents_lst(regname, lstval, -1,
8553 append, yank_type, block_len);
8554free_lstval:
8555 while (curallocval > allocval)
8556 vim_free(*--curallocval);
8557 vim_free(lstval);
8558 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008559 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008560 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008561 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562 if (strval == NULL)
8563 return;
8564 write_reg_contents_ex(regname, strval, -1,
8565 append, yank_type, block_len);
8566 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008567 if (pointreg != 0)
8568 get_yank_register(pointreg, TRUE);
8569
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008570 rettv->vval.v_number = 0;
8571}
8572
8573/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008574 * "settagstack()" function
8575 */
8576 static void
8577f_settagstack(typval_T *argvars, typval_T *rettv)
8578{
8579 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8580 win_T *wp;
8581 dict_T *d;
8582 int action = 'r';
8583
8584 rettv->vval.v_number = -1;
8585
8586 // first argument: window number or id
8587 wp = find_win_by_nr_or_id(&argvars[0]);
8588 if (wp == NULL)
8589 return;
8590
8591 // second argument: dict with items to set in the tag stack
8592 if (argvars[1].v_type != VAR_DICT)
8593 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008594 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008595 return;
8596 }
8597 d = argvars[1].vval.v_dict;
8598 if (d == NULL)
8599 return;
8600
8601 // third argument: action - 'a' for append and 'r' for replace.
8602 // default is to replace the stack.
8603 if (argvars[2].v_type == VAR_UNKNOWN)
8604 action = 'r';
8605 else if (argvars[2].v_type == VAR_STRING)
8606 {
8607 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008608 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008609 if (actstr == NULL)
8610 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008611 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8612 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008613 action = *actstr;
8614 else
8615 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008616 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008617 return;
8618 }
8619 }
8620 else
8621 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008622 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008623 return;
8624 }
8625
8626 if (set_tagstack(wp, d, action) == OK)
8627 rettv->vval.v_number = 0;
8628}
8629
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008630#ifdef FEAT_CRYPT
8631/*
8632 * "sha256({string})" function
8633 */
8634 static void
8635f_sha256(typval_T *argvars, typval_T *rettv)
8636{
8637 char_u *p;
8638
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008639 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008640 rettv->vval.v_string = vim_strsave(
8641 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8642 rettv->v_type = VAR_STRING;
8643}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008644#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645
8646/*
8647 * "shellescape({string})" function
8648 */
8649 static void
8650f_shellescape(typval_T *argvars, typval_T *rettv)
8651{
Bram Moolenaar20615522017-06-05 18:46:26 +02008652 int do_special = non_zero_arg(&argvars[1]);
8653
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008654 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008655 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008656 rettv->v_type = VAR_STRING;
8657}
8658
8659/*
8660 * shiftwidth() function
8661 */
8662 static void
8663f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8664{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008665 rettv->vval.v_number = 0;
8666
8667 if (argvars[0].v_type != VAR_UNKNOWN)
8668 {
8669 long col;
8670
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008671 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008672 if (col < 0)
8673 return; // type error; errmsg already given
8674#ifdef FEAT_VARTABS
8675 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8676 return;
8677#endif
8678 }
8679
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008680 rettv->vval.v_number = get_sw_value(curbuf);
8681}
8682
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008683#ifdef FEAT_FLOAT
8684/*
8685 * "sin()" function
8686 */
8687 static void
8688f_sin(typval_T *argvars, typval_T *rettv)
8689{
8690 float_T f = 0.0;
8691
8692 rettv->v_type = VAR_FLOAT;
8693 if (get_float_arg(argvars, &f) == OK)
8694 rettv->vval.v_float = sin(f);
8695 else
8696 rettv->vval.v_float = 0.0;
8697}
8698
8699/*
8700 * "sinh()" function
8701 */
8702 static void
8703f_sinh(typval_T *argvars, typval_T *rettv)
8704{
8705 float_T f = 0.0;
8706
8707 rettv->v_type = VAR_FLOAT;
8708 if (get_float_arg(argvars, &f) == OK)
8709 rettv->vval.v_float = sinh(f);
8710 else
8711 rettv->vval.v_float = 0.0;
8712}
8713#endif
8714
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008715/*
8716 * "soundfold({word})" function
8717 */
8718 static void
8719f_soundfold(typval_T *argvars, typval_T *rettv)
8720{
8721 char_u *s;
8722
8723 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008724 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008725#ifdef FEAT_SPELL
8726 rettv->vval.v_string = eval_soundfold(s);
8727#else
8728 rettv->vval.v_string = vim_strsave(s);
8729#endif
8730}
8731
8732/*
8733 * "spellbadword()" function
8734 */
8735 static void
8736f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8737{
8738 char_u *word = (char_u *)"";
8739 hlf_T attr = HLF_COUNT;
8740 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008741#ifdef FEAT_SPELL
8742 int wo_spell_save = curwin->w_p_spell;
8743
8744 if (!curwin->w_p_spell)
8745 {
8746 did_set_spelllang(curwin);
8747 curwin->w_p_spell = TRUE;
8748 }
8749
8750 if (*curwin->w_s->b_p_spl == NUL)
8751 {
8752 emsg(_(e_no_spell));
8753 curwin->w_p_spell = wo_spell_save;
8754 return;
8755 }
8756#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757
8758 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008759 {
8760#ifdef FEAT_SPELL
8761 curwin->w_p_spell = wo_spell_save;
8762#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008763 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008764 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008765
8766#ifdef FEAT_SPELL
8767 if (argvars[0].v_type == VAR_UNKNOWN)
8768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008769 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8771 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008772 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008773 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008774 curwin->w_set_curswant = TRUE;
8775 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008777 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008778 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008779 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008780 int capcol = -1;
8781
8782 if (str != NULL)
8783 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008784 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008785 while (*str != NUL)
8786 {
8787 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8788 if (attr != HLF_COUNT)
8789 {
8790 word = str;
8791 break;
8792 }
8793 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008794 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008795 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796 }
8797 }
8798 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008799 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008800#endif
8801
8802 list_append_string(rettv->vval.v_list, word, len);
8803 list_append_string(rettv->vval.v_list, (char_u *)(
8804 attr == HLF_SPB ? "bad" :
8805 attr == HLF_SPR ? "rare" :
8806 attr == HLF_SPL ? "local" :
8807 attr == HLF_SPC ? "caps" :
8808 ""), -1);
8809}
8810
8811/*
8812 * "spellsuggest()" function
8813 */
8814 static void
8815f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8816{
8817#ifdef FEAT_SPELL
8818 char_u *str;
8819 int typeerr = FALSE;
8820 int maxcount;
8821 garray_T ga;
8822 int i;
8823 listitem_T *li;
8824 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008825 int wo_spell_save = curwin->w_p_spell;
8826
8827 if (!curwin->w_p_spell)
8828 {
8829 did_set_spelllang(curwin);
8830 curwin->w_p_spell = TRUE;
8831 }
8832
8833 if (*curwin->w_s->b_p_spl == NUL)
8834 {
8835 emsg(_(e_no_spell));
8836 curwin->w_p_spell = wo_spell_save;
8837 return;
8838 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008839#endif
8840
8841 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008842 {
8843#ifdef FEAT_SPELL
8844 curwin->w_p_spell = wo_spell_save;
8845#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008846 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008847 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008848
8849#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008850 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008851 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008852 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008853 if (argvars[1].v_type != VAR_UNKNOWN)
8854 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008855 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008856 if (maxcount <= 0)
8857 return;
8858 if (argvars[2].v_type != VAR_UNKNOWN)
8859 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008860 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008861 if (typeerr)
8862 return;
8863 }
8864 }
8865 else
8866 maxcount = 25;
8867
8868 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8869
8870 for (i = 0; i < ga.ga_len; ++i)
8871 {
8872 str = ((char_u **)ga.ga_data)[i];
8873
8874 li = listitem_alloc();
8875 if (li == NULL)
8876 vim_free(str);
8877 else
8878 {
8879 li->li_tv.v_type = VAR_STRING;
8880 li->li_tv.v_lock = 0;
8881 li->li_tv.vval.v_string = str;
8882 list_append(rettv->vval.v_list, li);
8883 }
8884 }
8885 ga_clear(&ga);
8886 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008887 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008888#endif
8889}
8890
8891 static void
8892f_split(typval_T *argvars, typval_T *rettv)
8893{
8894 char_u *str;
8895 char_u *end;
8896 char_u *pat = NULL;
8897 regmatch_T regmatch;
8898 char_u patbuf[NUMBUFLEN];
8899 char_u *save_cpo;
8900 int match;
8901 colnr_T col = 0;
8902 int keepempty = FALSE;
8903 int typeerr = FALSE;
8904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008905 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008906 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008907 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008908
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008909 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008910 if (argvars[1].v_type != VAR_UNKNOWN)
8911 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008912 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008913 if (pat == NULL)
8914 typeerr = TRUE;
8915 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008916 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008917 }
8918 if (pat == NULL || *pat == NUL)
8919 pat = (char_u *)"[\\x01- ]\\+";
8920
8921 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008922 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008923 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008924 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008925
8926 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8927 if (regmatch.regprog != NULL)
8928 {
8929 regmatch.rm_ic = FALSE;
8930 while (*str != NUL || keepempty)
8931 {
8932 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008933 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008934 else
8935 match = vim_regexec_nl(&regmatch, str, col);
8936 if (match)
8937 end = regmatch.startp[0];
8938 else
8939 end = str + STRLEN(str);
8940 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
8941 && *str != NUL && match && end < regmatch.endp[0]))
8942 {
8943 if (list_append_string(rettv->vval.v_list, str,
8944 (int)(end - str)) == FAIL)
8945 break;
8946 }
8947 if (!match)
8948 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01008949 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008950 if (regmatch.endp[0] > str)
8951 col = 0;
8952 else
Bram Moolenaar13505972019-01-24 15:04:48 +01008953 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008954 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008955 str = regmatch.endp[0];
8956 }
8957
8958 vim_regfree(regmatch.regprog);
8959 }
8960
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008961theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008962 p_cpo = save_cpo;
8963}
8964
8965#ifdef FEAT_FLOAT
8966/*
8967 * "sqrt()" function
8968 */
8969 static void
8970f_sqrt(typval_T *argvars, typval_T *rettv)
8971{
8972 float_T f = 0.0;
8973
8974 rettv->v_type = VAR_FLOAT;
8975 if (get_float_arg(argvars, &f) == OK)
8976 rettv->vval.v_float = sqrt(f);
8977 else
8978 rettv->vval.v_float = 0.0;
8979}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01008980#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008981
Bram Moolenaar0387cae2019-11-29 21:07:58 +01008982#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01008983/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008984 * "str2float()" function
8985 */
8986 static void
8987f_str2float(typval_T *argvars, typval_T *rettv)
8988{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008989 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008990 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008991
Bram Moolenaar08243d22017-01-10 16:12:29 +01008992 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008993 p = skipwhite(p + 1);
8994 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01008995 if (isneg)
8996 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008997 rettv->v_type = VAR_FLOAT;
8998}
8999#endif
9000
9001/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02009002 * "str2list()" function
9003 */
9004 static void
9005f_str2list(typval_T *argvars, typval_T *rettv)
9006{
9007 char_u *p;
9008 int utf8 = FALSE;
9009
9010 if (rettv_list_alloc(rettv) == FAIL)
9011 return;
9012
9013 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02009014 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02009015
9016 p = tv_get_string(&argvars[0]);
9017
9018 if (has_mbyte || utf8)
9019 {
9020 int (*ptr2len)(char_u *);
9021 int (*ptr2char)(char_u *);
9022
9023 if (utf8 || enc_utf8)
9024 {
9025 ptr2len = utf_ptr2len;
9026 ptr2char = utf_ptr2char;
9027 }
9028 else
9029 {
9030 ptr2len = mb_ptr2len;
9031 ptr2char = mb_ptr2char;
9032 }
9033
9034 for ( ; *p != NUL; p += (*ptr2len)(p))
9035 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
9036 }
9037 else
9038 for ( ; *p != NUL; ++p)
9039 list_append_number(rettv->vval.v_list, *p);
9040}
9041
9042/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009043 * "str2nr()" function
9044 */
9045 static void
9046f_str2nr(typval_T *argvars, typval_T *rettv)
9047{
9048 int base = 10;
9049 char_u *p;
9050 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009051 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01009052 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009053
9054 if (argvars[1].v_type != VAR_UNKNOWN)
9055 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009056 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009057 if (base != 2 && base != 8 && base != 10 && base != 16)
9058 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009059 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009060 return;
9061 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02009062 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009063 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009064 }
9065
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009066 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01009067 isneg = (*p == '-');
9068 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009069 p = skipwhite(p + 1);
9070 switch (base)
9071 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009072 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02009073 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02009074 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009075 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02009076 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
9077 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01009078 if (isneg)
9079 rettv->vval.v_number = -n;
9080 else
9081 rettv->vval.v_number = n;
9082
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009083}
9084
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009085/*
9086 * "strgetchar()" function
9087 */
9088 static void
9089f_strgetchar(typval_T *argvars, typval_T *rettv)
9090{
9091 char_u *str;
9092 int len;
9093 int error = FALSE;
9094 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01009095 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009096
9097 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009098 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009099 if (str == NULL)
9100 return;
9101 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009102 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009103 if (error)
9104 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009105
Bram Moolenaar13505972019-01-24 15:04:48 +01009106 while (charidx >= 0 && byteidx < len)
9107 {
9108 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009109 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009110 rettv->vval.v_number = mb_ptr2char(str + byteidx);
9111 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009113 --charidx;
9114 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009115 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009116}
9117
9118/*
9119 * "stridx()" function
9120 */
9121 static void
9122f_stridx(typval_T *argvars, typval_T *rettv)
9123{
9124 char_u buf[NUMBUFLEN];
9125 char_u *needle;
9126 char_u *haystack;
9127 char_u *save_haystack;
9128 char_u *pos;
9129 int start_idx;
9130
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009131 needle = tv_get_string_chk(&argvars[1]);
9132 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009133 rettv->vval.v_number = -1;
9134 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009135 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009136
9137 if (argvars[2].v_type != VAR_UNKNOWN)
9138 {
9139 int error = FALSE;
9140
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009141 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009142 if (error || start_idx >= (int)STRLEN(haystack))
9143 return;
9144 if (start_idx >= 0)
9145 haystack += start_idx;
9146 }
9147
9148 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9149 if (pos != NULL)
9150 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
9151}
9152
9153/*
9154 * "string()" function
9155 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009156 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009157f_string(typval_T *argvars, typval_T *rettv)
9158{
9159 char_u *tofree;
9160 char_u numbuf[NUMBUFLEN];
9161
9162 rettv->v_type = VAR_STRING;
9163 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
9164 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009165 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009166 if (rettv->vval.v_string != NULL && tofree == NULL)
9167 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
9168}
9169
9170/*
9171 * "strlen()" function
9172 */
9173 static void
9174f_strlen(typval_T *argvars, typval_T *rettv)
9175{
9176 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009177 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009178}
9179
9180/*
9181 * "strchars()" function
9182 */
9183 static void
9184f_strchars(typval_T *argvars, typval_T *rettv)
9185{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009186 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar3986b942020-09-06 16:09:04 +02009187 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009188 varnumber_T len = 0;
9189 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009190
9191 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02009192 skipcc = (int)tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009193 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02009194 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009195 else
9196 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009197 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
9198 while (*s != NUL)
9199 {
9200 func_mb_ptr2char_adv(&s);
9201 ++len;
9202 }
9203 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009204 }
9205}
9206
9207/*
9208 * "strdisplaywidth()" function
9209 */
9210 static void
9211f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
9212{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009213 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009214 int col = 0;
9215
9216 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009217 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009218
9219 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
9220}
9221
9222/*
9223 * "strwidth()" function
9224 */
9225 static void
9226f_strwidth(typval_T *argvars, typval_T *rettv)
9227{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009228 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009229
Bram Moolenaar13505972019-01-24 15:04:48 +01009230 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009231}
9232
9233/*
9234 * "strcharpart()" function
9235 */
9236 static void
9237f_strcharpart(typval_T *argvars, typval_T *rettv)
9238{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009239 char_u *p;
9240 int nchar;
9241 int nbyte = 0;
9242 int charlen;
9243 int len = 0;
9244 int slen;
9245 int error = FALSE;
9246
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009247 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009248 slen = (int)STRLEN(p);
9249
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009250 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009251 if (!error)
9252 {
9253 if (nchar > 0)
9254 while (nchar > 0 && nbyte < slen)
9255 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009256 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009257 --nchar;
9258 }
9259 else
9260 nbyte = nchar;
9261 if (argvars[2].v_type != VAR_UNKNOWN)
9262 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009263 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009264 while (charlen > 0 && nbyte + len < slen)
9265 {
9266 int off = nbyte + len;
9267
9268 if (off < 0)
9269 len += 1;
9270 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02009271 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009272 --charlen;
9273 }
9274 }
9275 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009276 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009277 }
9278
9279 /*
9280 * Only return the overlap between the specified part and the actual
9281 * string.
9282 */
9283 if (nbyte < 0)
9284 {
9285 len += nbyte;
9286 nbyte = 0;
9287 }
9288 else if (nbyte > slen)
9289 nbyte = slen;
9290 if (len < 0)
9291 len = 0;
9292 else if (nbyte + len > slen)
9293 len = slen - nbyte;
9294
9295 rettv->v_type = VAR_STRING;
9296 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009297}
9298
9299/*
9300 * "strpart()" function
9301 */
9302 static void
9303f_strpart(typval_T *argvars, typval_T *rettv)
9304{
9305 char_u *p;
9306 int n;
9307 int len;
9308 int slen;
9309 int error = FALSE;
9310
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009311 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009312 slen = (int)STRLEN(p);
9313
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009314 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009315 if (error)
9316 len = 0;
9317 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009318 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009319 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009320 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009321
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009322 // Only return the overlap between the specified part and the actual
9323 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009324 if (n < 0)
9325 {
9326 len += n;
9327 n = 0;
9328 }
9329 else if (n > slen)
9330 n = slen;
9331 if (len < 0)
9332 len = 0;
9333 else if (n + len > slen)
9334 len = slen - n;
9335
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009336 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9337 {
9338 int off;
9339
9340 // length in characters
9341 for (off = n; off < slen && len > 0; --len)
9342 off += mb_ptr2len(p + off);
9343 len = off - n;
9344 }
9345
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009346 rettv->v_type = VAR_STRING;
9347 rettv->vval.v_string = vim_strnsave(p + n, len);
9348}
9349
9350/*
9351 * "strridx()" function
9352 */
9353 static void
9354f_strridx(typval_T *argvars, typval_T *rettv)
9355{
9356 char_u buf[NUMBUFLEN];
9357 char_u *needle;
9358 char_u *haystack;
9359 char_u *rest;
9360 char_u *lastmatch = NULL;
9361 int haystack_len, end_idx;
9362
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009363 needle = tv_get_string_chk(&argvars[1]);
9364 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009365
9366 rettv->vval.v_number = -1;
9367 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009368 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009369
9370 haystack_len = (int)STRLEN(haystack);
9371 if (argvars[2].v_type != VAR_UNKNOWN)
9372 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009373 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009374 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009375 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009376 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009377 }
9378 else
9379 end_idx = haystack_len;
9380
9381 if (*needle == NUL)
9382 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009383 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009384 lastmatch = haystack + end_idx;
9385 }
9386 else
9387 {
9388 for (rest = haystack; *rest != '\0'; ++rest)
9389 {
9390 rest = (char_u *)strstr((char *)rest, (char *)needle);
9391 if (rest == NULL || rest > haystack + end_idx)
9392 break;
9393 lastmatch = rest;
9394 }
9395 }
9396
9397 if (lastmatch == NULL)
9398 rettv->vval.v_number = -1;
9399 else
9400 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9401}
9402
9403/*
9404 * "strtrans()" function
9405 */
9406 static void
9407f_strtrans(typval_T *argvars, typval_T *rettv)
9408{
9409 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009410 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009411}
9412
9413/*
9414 * "submatch()" function
9415 */
9416 static void
9417f_submatch(typval_T *argvars, typval_T *rettv)
9418{
9419 int error = FALSE;
9420 int no;
9421 int retList = 0;
9422
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009423 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009424 if (error)
9425 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009426 if (no < 0 || no >= NSUBEXP)
9427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009428 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009429 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009430 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009431 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009432 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009433 if (error)
9434 return;
9435
9436 if (retList == 0)
9437 {
9438 rettv->v_type = VAR_STRING;
9439 rettv->vval.v_string = reg_submatch(no);
9440 }
9441 else
9442 {
9443 rettv->v_type = VAR_LIST;
9444 rettv->vval.v_list = reg_submatch_list(no);
9445 }
9446}
9447
9448/*
9449 * "substitute()" function
9450 */
9451 static void
9452f_substitute(typval_T *argvars, typval_T *rettv)
9453{
9454 char_u patbuf[NUMBUFLEN];
9455 char_u subbuf[NUMBUFLEN];
9456 char_u flagsbuf[NUMBUFLEN];
9457
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009458 char_u *str = tv_get_string_chk(&argvars[0]);
9459 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009460 char_u *sub = NULL;
9461 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009462 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009463
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009464 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9465 expr = &argvars[2];
9466 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009467 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009468
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009469 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009470 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9471 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009472 rettv->vval.v_string = NULL;
9473 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009474 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009475}
9476
9477/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009478 * "swapinfo(swap_filename)" function
9479 */
9480 static void
9481f_swapinfo(typval_T *argvars, typval_T *rettv)
9482{
9483 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009484 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009485}
9486
9487/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009488 * "swapname(expr)" function
9489 */
9490 static void
9491f_swapname(typval_T *argvars, typval_T *rettv)
9492{
9493 buf_T *buf;
9494
9495 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009496 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009497 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9498 || buf->b_ml.ml_mfp->mf_fname == NULL)
9499 rettv->vval.v_string = NULL;
9500 else
9501 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9502}
9503
9504/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009505 * "synID(lnum, col, trans)" function
9506 */
9507 static void
9508f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9509{
9510 int id = 0;
9511#ifdef FEAT_SYN_HL
9512 linenr_T lnum;
9513 colnr_T col;
9514 int trans;
9515 int transerr = FALSE;
9516
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009517 lnum = tv_get_lnum(argvars); // -1 on type error
9518 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009519 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009520
9521 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9522 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9523 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9524#endif
9525
9526 rettv->vval.v_number = id;
9527}
9528
9529/*
9530 * "synIDattr(id, what [, mode])" function
9531 */
9532 static void
9533f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9534{
9535 char_u *p = NULL;
9536#ifdef FEAT_SYN_HL
9537 int id;
9538 char_u *what;
9539 char_u *mode;
9540 char_u modebuf[NUMBUFLEN];
9541 int modec;
9542
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009543 id = (int)tv_get_number(&argvars[0]);
9544 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009545 if (argvars[2].v_type != VAR_UNKNOWN)
9546 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009547 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009548 modec = TOLOWER_ASC(mode[0]);
9549 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009550 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009551 }
9552 else
9553 {
9554#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9555 if (USE_24BIT)
9556 modec = 'g';
9557 else
9558#endif
9559 if (t_colors > 1)
9560 modec = 'c';
9561 else
9562 modec = 't';
9563 }
9564
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009565 switch (TOLOWER_ASC(what[0]))
9566 {
9567 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009568 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009569 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009570 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009571 p = highlight_has_attr(id, HL_BOLD, modec);
9572 break;
9573
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009574 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009575 p = highlight_color(id, what, modec);
9576 break;
9577
9578 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009579 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009580 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009581 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009582 p = highlight_has_attr(id, HL_ITALIC, modec);
9583 break;
9584
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009585 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009586 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009587 break;
9588
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009589 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009590 p = highlight_has_attr(id, HL_INVERSE, modec);
9591 break;
9592
9593 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009594 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009595 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009596 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009597 else if (TOLOWER_ASC(what[1]) == 't' &&
9598 TOLOWER_ASC(what[2]) == 'r')
9599 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009600 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009601 p = highlight_has_attr(id, HL_STANDOUT, modec);
9602 break;
9603
9604 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009605 if (TOLOWER_ASC(what[1]) == 'l') // ul
9606 p = highlight_color(id, what, modec);
9607 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009608 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009609 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9610 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009611 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009612 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9613 break;
9614 }
9615
9616 if (p != NULL)
9617 p = vim_strsave(p);
9618#endif
9619 rettv->v_type = VAR_STRING;
9620 rettv->vval.v_string = p;
9621}
9622
9623/*
9624 * "synIDtrans(id)" function
9625 */
9626 static void
9627f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9628{
9629 int id;
9630
9631#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009632 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009633
9634 if (id > 0)
9635 id = syn_get_final_id(id);
9636 else
9637#endif
9638 id = 0;
9639
9640 rettv->vval.v_number = id;
9641}
9642
9643/*
9644 * "synconcealed(lnum, col)" function
9645 */
9646 static void
9647f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9648{
9649#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9650 linenr_T lnum;
9651 colnr_T col;
9652 int syntax_flags = 0;
9653 int cchar;
9654 int matchid = 0;
9655 char_u str[NUMBUFLEN];
9656#endif
9657
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009658 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009659
9660#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009661 lnum = tv_get_lnum(argvars); // -1 on type error
9662 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009663
Bram Moolenaara80faa82020-04-12 19:37:17 +02009664 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009665
9666 if (rettv_list_alloc(rettv) != FAIL)
9667 {
9668 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9669 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9670 && curwin->w_p_cole > 0)
9671 {
9672 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9673 syntax_flags = get_syntax_info(&matchid);
9674
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009675 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009676 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9677 {
9678 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009679 if (cchar == NUL && curwin->w_p_cole == 1)
9680 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009681 if (cchar != NUL)
9682 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009683 if (has_mbyte)
9684 (*mb_char2bytes)(cchar, str);
9685 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009686 str[0] = cchar;
9687 }
9688 }
9689 }
9690
9691 list_append_number(rettv->vval.v_list,
9692 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009693 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009694 list_append_string(rettv->vval.v_list, str, -1);
9695 list_append_number(rettv->vval.v_list, matchid);
9696 }
9697#endif
9698}
9699
9700/*
9701 * "synstack(lnum, col)" function
9702 */
9703 static void
9704f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9705{
9706#ifdef FEAT_SYN_HL
9707 linenr_T lnum;
9708 colnr_T col;
9709 int i;
9710 int id;
9711#endif
9712
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009713 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009714
9715#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009716 lnum = tv_get_lnum(argvars); // -1 on type error
9717 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009718
9719 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9720 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9721 && rettv_list_alloc(rettv) != FAIL)
9722 {
9723 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9724 for (i = 0; ; ++i)
9725 {
9726 id = syn_get_stack_item(i);
9727 if (id < 0)
9728 break;
9729 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9730 break;
9731 }
9732 }
9733#endif
9734}
9735
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009736/*
9737 * "tabpagebuflist()" function
9738 */
9739 static void
9740f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9741{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009742 tabpage_T *tp;
9743 win_T *wp = NULL;
9744
9745 if (argvars[0].v_type == VAR_UNKNOWN)
9746 wp = firstwin;
9747 else
9748 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009749 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009750 if (tp != NULL)
9751 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9752 }
9753 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9754 {
9755 for (; wp != NULL; wp = wp->w_next)
9756 if (list_append_number(rettv->vval.v_list,
9757 wp->w_buffer->b_fnum) == FAIL)
9758 break;
9759 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009760}
9761
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009762/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009763 * "tagfiles()" function
9764 */
9765 static void
9766f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9767{
9768 char_u *fname;
9769 tagname_T tn;
9770 int first;
9771
9772 if (rettv_list_alloc(rettv) == FAIL)
9773 return;
9774 fname = alloc(MAXPATHL);
9775 if (fname == NULL)
9776 return;
9777
9778 for (first = TRUE; ; first = FALSE)
9779 if (get_tagfname(&tn, first, fname) == FAIL
9780 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9781 break;
9782 tagname_free(&tn);
9783 vim_free(fname);
9784}
9785
9786/*
9787 * "taglist()" function
9788 */
9789 static void
9790f_taglist(typval_T *argvars, typval_T *rettv)
9791{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009792 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009793 char_u *tag_pattern;
9794
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009795 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009796
9797 rettv->vval.v_number = FALSE;
9798 if (*tag_pattern == NUL)
9799 return;
9800
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009801 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009802 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009803 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009804 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009805}
9806
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009807#ifdef FEAT_FLOAT
9808/*
9809 * "tan()" function
9810 */
9811 static void
9812f_tan(typval_T *argvars, typval_T *rettv)
9813{
9814 float_T f = 0.0;
9815
9816 rettv->v_type = VAR_FLOAT;
9817 if (get_float_arg(argvars, &f) == OK)
9818 rettv->vval.v_float = tan(f);
9819 else
9820 rettv->vval.v_float = 0.0;
9821}
9822
9823/*
9824 * "tanh()" function
9825 */
9826 static void
9827f_tanh(typval_T *argvars, typval_T *rettv)
9828{
9829 float_T f = 0.0;
9830
9831 rettv->v_type = VAR_FLOAT;
9832 if (get_float_arg(argvars, &f) == OK)
9833 rettv->vval.v_float = tanh(f);
9834 else
9835 rettv->vval.v_float = 0.0;
9836}
9837#endif
9838
9839/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009840 * "tolower(string)" function
9841 */
9842 static void
9843f_tolower(typval_T *argvars, typval_T *rettv)
9844{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009845 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009846 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009847}
9848
9849/*
9850 * "toupper(string)" function
9851 */
9852 static void
9853f_toupper(typval_T *argvars, typval_T *rettv)
9854{
9855 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009856 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009857}
9858
9859/*
9860 * "tr(string, fromstr, tostr)" function
9861 */
9862 static void
9863f_tr(typval_T *argvars, typval_T *rettv)
9864{
9865 char_u *in_str;
9866 char_u *fromstr;
9867 char_u *tostr;
9868 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009869 int inlen;
9870 int fromlen;
9871 int tolen;
9872 int idx;
9873 char_u *cpstr;
9874 int cplen;
9875 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009876 char_u buf[NUMBUFLEN];
9877 char_u buf2[NUMBUFLEN];
9878 garray_T ga;
9879
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009880 in_str = tv_get_string(&argvars[0]);
9881 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9882 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009884 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009885 rettv->v_type = VAR_STRING;
9886 rettv->vval.v_string = NULL;
9887 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009888 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009889 ga_init2(&ga, (int)sizeof(char), 80);
9890
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009891 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009892 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009893 if (STRLEN(fromstr) != STRLEN(tostr))
9894 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009895error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009896 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009897 ga_clear(&ga);
9898 return;
9899 }
9900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009901 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009902 while (*in_str != NUL)
9903 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009904 if (has_mbyte)
9905 {
9906 inlen = (*mb_ptr2len)(in_str);
9907 cpstr = in_str;
9908 cplen = inlen;
9909 idx = 0;
9910 for (p = fromstr; *p != NUL; p += fromlen)
9911 {
9912 fromlen = (*mb_ptr2len)(p);
9913 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9914 {
9915 for (p = tostr; *p != NUL; p += tolen)
9916 {
9917 tolen = (*mb_ptr2len)(p);
9918 if (idx-- == 0)
9919 {
9920 cplen = tolen;
9921 cpstr = p;
9922 break;
9923 }
9924 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009925 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009926 goto error;
9927 break;
9928 }
9929 ++idx;
9930 }
9931
9932 if (first && cpstr == in_str)
9933 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009934 // Check that fromstr and tostr have the same number of
9935 // (multi-byte) characters. Done only once when a character
9936 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009937 first = FALSE;
9938 for (p = tostr; *p != NUL; p += tolen)
9939 {
9940 tolen = (*mb_ptr2len)(p);
9941 --idx;
9942 }
9943 if (idx != 0)
9944 goto error;
9945 }
9946
9947 (void)ga_grow(&ga, cplen);
9948 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
9949 ga.ga_len += cplen;
9950
9951 in_str += inlen;
9952 }
9953 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009954 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009955 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009956 p = vim_strchr(fromstr, *in_str);
9957 if (p != NULL)
9958 ga_append(&ga, tostr[p - fromstr]);
9959 else
9960 ga_append(&ga, *in_str);
9961 ++in_str;
9962 }
9963 }
9964
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009965 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009966 (void)ga_grow(&ga, 1);
9967 ga_append(&ga, NUL);
9968
9969 rettv->vval.v_string = ga.ga_data;
9970}
9971
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009972/*
9973 * "trim({expr})" function
9974 */
9975 static void
9976f_trim(typval_T *argvars, typval_T *rettv)
9977{
9978 char_u buf1[NUMBUFLEN];
9979 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009980 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009981 char_u *mask = NULL;
9982 char_u *tail;
9983 char_u *prev;
9984 char_u *p;
9985 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009986 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009987
9988 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009989 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009990 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009991 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009992
9993 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009994 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009995 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009996
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009997 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009998 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009999 int error = 0;
10000
10001 // leading or trailing characters to trim
10002 dir = (int)tv_get_number_chk(&argvars[2], &error);
10003 if (error)
10004 return;
10005 if (dir < 0 || dir > 2)
10006 {
10007 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
10008 return;
10009 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010010 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010011 }
10012
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010013 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010014 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010015 // Trim leading characters
10016 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010017 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010018 c1 = PTR2CHAR(head);
10019 if (mask == NULL)
10020 {
10021 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010022 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +020010023 }
10024 else
10025 {
10026 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10027 if (c1 == PTR2CHAR(p))
10028 break;
10029 if (*p == NUL)
10030 break;
10031 }
10032 MB_PTR_ADV(head);
10033 }
10034 }
10035
10036 tail = head + STRLEN(head);
10037 if (dir == 0 || dir == 2)
10038 {
10039 // Trim trailing characters
10040 for (; tail > head; tail = prev)
10041 {
10042 prev = tail;
10043 MB_PTR_BACK(head, prev);
10044 c1 = PTR2CHAR(prev);
10045 if (mask == NULL)
10046 {
10047 if (c1 > ' ' && c1 != 0xa0)
10048 break;
10049 }
10050 else
10051 {
10052 for (p = mask; *p != NUL; MB_PTR_ADV(p))
10053 if (c1 == PTR2CHAR(p))
10054 break;
10055 if (*p == NUL)
10056 break;
10057 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010058 }
10059 }
Bram Moolenaardf44a272020-06-07 20:49:05 +020010060 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010010061}
10062
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010063#ifdef FEAT_FLOAT
10064/*
10065 * "trunc({float})" function
10066 */
10067 static void
10068f_trunc(typval_T *argvars, typval_T *rettv)
10069{
10070 float_T f = 0.0;
10071
10072 rettv->v_type = VAR_FLOAT;
10073 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010074 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010075 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
10076 else
10077 rettv->vval.v_float = 0.0;
10078}
10079#endif
10080
10081/*
10082 * "type(expr)" function
10083 */
10084 static void
10085f_type(typval_T *argvars, typval_T *rettv)
10086{
10087 int n = -1;
10088
10089 switch (argvars[0].v_type)
10090 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010091 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
10092 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010093 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +010010094 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
10095 case VAR_LIST: n = VAR_TYPE_LIST; break;
10096 case VAR_DICT: n = VAR_TYPE_DICT; break;
10097 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
10098 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
10099 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020010100 case VAR_JOB: n = VAR_TYPE_JOB; break;
10101 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010102 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010103 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +020010104 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010105 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +010010106 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010107 n = -1;
10108 break;
10109 }
10110 rettv->vval.v_number = n;
10111}
10112
10113/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010114 * "virtcol(string)" function
10115 */
10116 static void
10117f_virtcol(typval_T *argvars, typval_T *rettv)
10118{
10119 colnr_T vcol = 0;
10120 pos_T *fp;
10121 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010122 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010123
Bram Moolenaar6f02b002021-01-10 20:22:54 +010010124 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010125 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
10126 && fnum == curbuf->b_fnum)
10127 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010010128 // Limit the column to a valid value, getvvcol() doesn't check.
10129 if (fp->col < 0)
10130 fp->col = 0;
10131 else
10132 {
10133 len = (int)STRLEN(ml_get(fp->lnum));
10134 if (fp->col > len)
10135 fp->col = len;
10136 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010137 getvvcol(curwin, fp, NULL, NULL, &vcol);
10138 ++vcol;
10139 }
10140
10141 rettv->vval.v_number = vcol;
10142}
10143
10144/*
10145 * "visualmode()" function
10146 */
10147 static void
10148f_visualmode(typval_T *argvars, typval_T *rettv)
10149{
10150 char_u str[2];
10151
10152 rettv->v_type = VAR_STRING;
10153 str[0] = curbuf->b_visual_mode_eval;
10154 str[1] = NUL;
10155 rettv->vval.v_string = vim_strsave(str);
10156
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010157 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010158 if (non_zero_arg(&argvars[0]))
10159 curbuf->b_visual_mode_eval = NUL;
10160}
10161
10162/*
10163 * "wildmenumode()" function
10164 */
10165 static void
10166f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10167{
10168#ifdef FEAT_WILDMENU
10169 if (wild_menu_showing)
10170 rettv->vval.v_number = 1;
10171#endif
10172}
10173
10174/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +010010175 * "windowsversion()" function
10176 */
10177 static void
10178f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10179{
10180 rettv->v_type = VAR_STRING;
10181 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
10182}
10183
10184/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010185 * "wordcount()" function
10186 */
10187 static void
10188f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
10189{
10190 if (rettv_dict_alloc(rettv) == FAIL)
10191 return;
10192 cursor_pos_info(rettv->vval.v_dict);
10193}
10194
10195/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010196 * "xor(expr, expr)" function
10197 */
10198 static void
10199f_xor(typval_T *argvars, typval_T *rettv)
10200{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010201 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
10202 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010203}
10204
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010010205#endif // FEAT_EVAL