blob: afc8977532e48a3034e36d5be01995c5f7d08585 [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 Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_confirm(typval_T *argvars, typval_T *rettv);
52static void f_copy(typval_T *argvars, typval_T *rettv);
53#ifdef FEAT_FLOAT
54static void f_cos(typval_T *argvars, typval_T *rettv);
55static void f_cosh(typval_T *argvars, typval_T *rettv);
56#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010058#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020059static void f_debugbreak(typval_T *argvars, typval_T *rettv);
60#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020061static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020062static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010063static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020064static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020065static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020066static void f_escape(typval_T *argvars, typval_T *rettv);
67static void f_eval(typval_T *argvars, typval_T *rettv);
68static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020069static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_exists(typval_T *argvars, typval_T *rettv);
71#ifdef FEAT_FLOAT
72static void f_exp(typval_T *argvars, typval_T *rettv);
73#endif
74static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020075static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020076static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020077#ifdef FEAT_FLOAT
78static void f_float2nr(typval_T *argvars, typval_T *rettv);
79static void f_floor(typval_T *argvars, typval_T *rettv);
80static void f_fmod(typval_T *argvars, typval_T *rettv);
81#endif
82static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020083static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020084static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020085static void f_function(typval_T *argvars, typval_T *rettv);
86static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
87static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010088static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020090static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010092static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020093static void f_getpid(typval_T *argvars, typval_T *rettv);
94static void f_getcurpos(typval_T *argvars, typval_T *rettv);
95static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +020097static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020098static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010099static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +0200100static void f_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200101static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
102static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200103static void f_hlID(typval_T *argvars, typval_T *rettv);
104static void f_hlexists(typval_T *argvars, typval_T *rettv);
105static void f_hostname(typval_T *argvars, typval_T *rettv);
106static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200107static void f_index(typval_T *argvars, typval_T *rettv);
108static void f_input(typval_T *argvars, typval_T *rettv);
109static void f_inputdialog(typval_T *argvars, typval_T *rettv);
110static void f_inputlist(typval_T *argvars, typval_T *rettv);
111static void f_inputrestore(typval_T *argvars, typval_T *rettv);
112static void f_inputsave(typval_T *argvars, typval_T *rettv);
113static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +0100114static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200115static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200116static void f_islocked(typval_T *argvars, typval_T *rettv);
117#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200118static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200119static void f_isnan(typval_T *argvars, typval_T *rettv);
120#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200121static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
122static void f_len(typval_T *argvars, typval_T *rettv);
123static void f_libcall(typval_T *argvars, typval_T *rettv);
124static void f_libcallnr(typval_T *argvars, typval_T *rettv);
125static void f_line(typval_T *argvars, typval_T *rettv);
126static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200127#ifdef FEAT_FLOAT
128static void f_log(typval_T *argvars, typval_T *rettv);
129static void f_log10(typval_T *argvars, typval_T *rettv);
130#endif
131#ifdef FEAT_LUA
132static void f_luaeval(typval_T *argvars, typval_T *rettv);
133#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200134static void f_maparg(typval_T *argvars, typval_T *rettv);
135static void f_mapcheck(typval_T *argvars, typval_T *rettv);
136static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200137static void f_matchend(typval_T *argvars, typval_T *rettv);
138static void f_matchlist(typval_T *argvars, typval_T *rettv);
139static void f_matchstr(typval_T *argvars, typval_T *rettv);
140static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
141static void f_max(typval_T *argvars, typval_T *rettv);
142static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200143#ifdef FEAT_MZSCHEME
144static void f_mzeval(typval_T *argvars, typval_T *rettv);
145#endif
146static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
147static void f_nr2char(typval_T *argvars, typval_T *rettv);
148static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200149#ifdef FEAT_PERL
150static void f_perleval(typval_T *argvars, typval_T *rettv);
151#endif
152#ifdef FEAT_FLOAT
153static void f_pow(typval_T *argvars, typval_T *rettv);
154#endif
155static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
156static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200157static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200158static void f_pumvisible(typval_T *argvars, typval_T *rettv);
159#ifdef FEAT_PYTHON3
160static void f_py3eval(typval_T *argvars, typval_T *rettv);
161#endif
162#ifdef FEAT_PYTHON
163static void f_pyeval(typval_T *argvars, typval_T *rettv);
164#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100165#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
166static void f_pyxeval(typval_T *argvars, typval_T *rettv);
167#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100168static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100169static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200170static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200171static void f_reg_executing(typval_T *argvars, typval_T *rettv);
172static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200173static void f_rename(typval_T *argvars, typval_T *rettv);
174static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200175#ifdef FEAT_FLOAT
176static void f_round(typval_T *argvars, typval_T *rettv);
177#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100178#ifdef FEAT_RUBY
179static void f_rubyeval(typval_T *argvars, typval_T *rettv);
180#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200181static void f_screenattr(typval_T *argvars, typval_T *rettv);
182static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100183static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200184static void f_screencol(typval_T *argvars, typval_T *rettv);
185static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100186static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200187static void f_search(typval_T *argvars, typval_T *rettv);
188static void f_searchdecl(typval_T *argvars, typval_T *rettv);
189static void f_searchpair(typval_T *argvars, typval_T *rettv);
190static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
191static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200193static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200195static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200196static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100197static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200198#ifdef FEAT_CRYPT
199static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200200#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_shellescape(typval_T *argvars, typval_T *rettv);
202static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200203#ifdef FEAT_FLOAT
204static void f_sin(typval_T *argvars, typval_T *rettv);
205static void f_sinh(typval_T *argvars, typval_T *rettv);
206#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200207static void f_soundfold(typval_T *argvars, typval_T *rettv);
208static void f_spellbadword(typval_T *argvars, typval_T *rettv);
209static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
210static void f_split(typval_T *argvars, typval_T *rettv);
211#ifdef FEAT_FLOAT
212static void f_sqrt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100213#endif
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100214static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100215#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200216static void f_str2float(typval_T *argvars, typval_T *rettv);
217#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200218static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200219static void f_str2nr(typval_T *argvars, typval_T *rettv);
220static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200221static void f_strgetchar(typval_T *argvars, typval_T *rettv);
222static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200223static void f_strlen(typval_T *argvars, typval_T *rettv);
224static void f_strcharpart(typval_T *argvars, typval_T *rettv);
225static void f_strpart(typval_T *argvars, typval_T *rettv);
226static void f_strridx(typval_T *argvars, typval_T *rettv);
227static void f_strtrans(typval_T *argvars, typval_T *rettv);
228static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
229static void f_strwidth(typval_T *argvars, typval_T *rettv);
230static void f_submatch(typval_T *argvars, typval_T *rettv);
231static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200232static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200233static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200234static void f_synID(typval_T *argvars, typval_T *rettv);
235static void f_synIDattr(typval_T *argvars, typval_T *rettv);
236static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
237static void f_synstack(typval_T *argvars, typval_T *rettv);
238static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200239static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200240static void f_taglist(typval_T *argvars, typval_T *rettv);
241static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200242#ifdef FEAT_FLOAT
243static void f_tan(typval_T *argvars, typval_T *rettv);
244static void f_tanh(typval_T *argvars, typval_T *rettv);
245#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200246static void f_tolower(typval_T *argvars, typval_T *rettv);
247static void f_toupper(typval_T *argvars, typval_T *rettv);
248static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100249static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200250#ifdef FEAT_FLOAT
251static void f_trunc(typval_T *argvars, typval_T *rettv);
252#endif
253static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200254static void f_virtcol(typval_T *argvars, typval_T *rettv);
255static void f_visualmode(typval_T *argvars, typval_T *rettv);
256static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100257static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200258static void f_wordcount(typval_T *argvars, typval_T *rettv);
259static void f_xor(typval_T *argvars, typval_T *rettv);
260
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100261
262 static type_T *
263ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
264{
265 return &t_void;
266}
267 static type_T *
268ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
269{
270 return &t_any;
271}
272 static type_T *
273ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
274{
275 return &t_number;
276}
277 static type_T *
278ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
279{
280 return &t_float;
281}
282 static type_T *
283ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
284{
285 return &t_string;
286}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200287 static type_T *
288ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100289{
290 return &t_list_any;
291}
292 static type_T *
293ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
294{
295 return &t_list_number;
296}
297 static type_T *
298ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
299{
300 return &t_list_string;
301}
302 static type_T *
303ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
304{
305 return &t_list_dict_any;
306}
307 static type_T *
308ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
309{
310 return &t_dict_any;
311}
312 static type_T *
313ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
314{
315 return &t_dict_number;
316}
317 static type_T *
318ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
319{
320 return &t_dict_string;
321}
322 static type_T *
323ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
324{
325 return &t_blob;
326}
327 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200328ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100329{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200330 return &t_func_any;
331}
332 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100333ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
334{
335 return &t_channel;
336}
337 static type_T *
338ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
339{
340 return &t_job;
341}
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100342
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200343 static type_T *
344ret_first_arg(int argcount, type_T **argtypes)
345{
346 if (argcount > 0)
347 return argtypes[0];
348 return &t_void;
349}
350
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200351/*
352 * Used for getqflist(): returns list if there is no argument, dict if there is
353 * one.
354 */
355 static type_T *
356ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
357{
358 if (argcount > 0)
359 return &t_dict_any;
360 return &t_list_dict_any;
361}
362
363/*
364 * Used for getloclist(): returns list if there is one argument, dict if there
365 * are two.
366 */
367 static type_T *
368ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
369{
370 if (argcount > 1)
371 return &t_dict_any;
372 return &t_list_dict_any;
373}
374
Bram Moolenaar846178a2020-07-05 17:04:13 +0200375 static type_T *
376ret_argv(int argcount, type_T **argtypes UNUSED)
377{
378 // argv() returns list of strings
379 if (argcount == 0)
380 return &t_list_string;
381
382 // argv(0) returns a string, but argv(-1] returns a list
383 return &t_any;
384}
385
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200386 static type_T *
387ret_remove(int argcount UNUSED, type_T **argtypes)
388{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200389 if (argtypes != NULL)
390 {
391 if (argtypes[0]->tt_type == VAR_LIST
392 || argtypes[0]->tt_type == VAR_DICT)
393 return argtypes[0]->tt_member;
394 if (argtypes[0]->tt_type == VAR_BLOB)
395 return &t_number;
396 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200397 return &t_any;
398}
399
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200400 static type_T *
401ret_getreg(int argcount, type_T **argtypes UNUSED)
402{
403 // Assume that if the third argument is passed it's non-zero
404 if (argcount == 3)
405 return &t_list_string;
406 return &t_string;
407}
408
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200409 static type_T *
410ret_maparg(int argcount, type_T **argtypes UNUSED)
411{
412 // Assume that if the fourth argument is passed it's non-zero
413 if (argcount == 4)
414 return &t_dict_any;
415 return &t_string;
416}
417
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100418static type_T *ret_f_function(int argcount, type_T **argtypes);
419
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200420/*
421 * Array with names and number of arguments of all internal functions
422 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
423 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200424typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200425{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200426 char *f_name; // function name
427 char f_min_argc; // minimal number of arguments
428 char f_max_argc; // maximal number of arguments
429 char f_argtype; // for method: FEARG_ values
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100430 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
431 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200432 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200433 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200434} funcentry_T;
435
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200436// values for f_argtype; zero means it cannot be used as a method
437#define FEARG_1 1 // base is the first argument
438#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200439#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200440#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200441#define FEARG_LAST 9 // base is the last argument
442
Bram Moolenaar15c47602020-03-26 22:16:48 +0100443#ifdef FEAT_FLOAT
444# define FLOAT_FUNC(name) name
445#else
446# define FLOAT_FUNC(name) NULL
447#endif
448#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
449# define MATH_FUNC(name) name
450#else
451# define MATH_FUNC(name) NULL
452#endif
453#ifdef FEAT_TIMERS
454# define TIMER_FUNC(name) name
455#else
456# define TIMER_FUNC(name) NULL
457#endif
458#ifdef FEAT_JOB_CHANNEL
459# define JOB_FUNC(name) name
460#else
461# define JOB_FUNC(name) NULL
462#endif
463#ifdef FEAT_PROP_POPUP
464# define PROP_FUNC(name) name
465#else
466# define PROP_FUNC(name) NULL
467#endif
468#ifdef FEAT_SIGNS
469# define SIGN_FUNC(name) name
470#else
471# define SIGN_FUNC(name) NULL
472#endif
473#ifdef FEAT_SOUND
474# define SOUND_FUNC(name) name
475#else
476# define SOUND_FUNC(name) NULL
477#endif
478#ifdef FEAT_TERMINAL
479# define TERM_FUNC(name) name
480#else
481# define TERM_FUNC(name) NULL
482#endif
483
Bram Moolenaarac92e252019-08-03 21:58:38 +0200484static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200485{
Bram Moolenaar15c47602020-03-26 22:16:48 +0100486 {"abs", 1, 1, FEARG_1, ret_any, FLOAT_FUNC(f_abs)},
487 {"acos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfce82b32020-07-05 16:07:21 +0200488 {"add", 2, 2, FEARG_1, ret_first_arg, f_add},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100489 {"and", 2, 2, FEARG_1, ret_number, f_and},
Bram Moolenaar389df252020-07-09 21:20:47 +0200490 {"append", 2, 2, FEARG_2, ret_number, f_append},
Bram Moolenaar92053ce2020-07-09 22:53:30 +0200491 {"appendbufline", 3, 3, FEARG_3, ret_number, f_appendbufline},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100492 {"argc", 0, 1, 0, ret_number, f_argc},
493 {"argidx", 0, 0, 0, ret_number, f_argidx},
494 {"arglistid", 0, 2, 0, ret_number, f_arglistid},
Bram Moolenaar846178a2020-07-05 17:04:13 +0200495 {"argv", 0, 2, 0, ret_argv, f_argv},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100496 {"asin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_asin)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100497 {"assert_beeps", 1, 2, FEARG_1, ret_number, f_assert_beeps},
498 {"assert_equal", 2, 3, FEARG_2, ret_number, f_assert_equal},
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200499 {"assert_equalfile", 2, 3, FEARG_1, ret_number, f_assert_equalfile},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100500 {"assert_exception", 1, 2, 0, ret_number, f_assert_exception},
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200501 {"assert_fails", 1, 5, FEARG_1, ret_number, f_assert_fails},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100502 {"assert_false", 1, 2, FEARG_1, ret_number, f_assert_false},
503 {"assert_inrange", 3, 4, FEARG_3, ret_number, f_assert_inrange},
504 {"assert_match", 2, 3, FEARG_2, ret_number, f_assert_match},
505 {"assert_notequal", 2, 3, FEARG_2, ret_number, f_assert_notequal},
506 {"assert_notmatch", 2, 3, FEARG_2, ret_number, f_assert_notmatch},
507 {"assert_report", 1, 1, FEARG_1, ret_number, f_assert_report},
508 {"assert_true", 1, 2, FEARG_1, ret_number, f_assert_true},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100509 {"atan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_atan)},
510 {"atan2", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_atan2)},
511 {"balloon_gettext", 0, 0, 0, ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100512#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100513 f_balloon_gettext
514#else
515 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100516#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100517 },
518 {"balloon_show", 1, 1, FEARG_1, ret_void,
519#ifdef FEAT_BEVAL
520 f_balloon_show
521#else
522 NULL
523#endif
524 },
525 {"balloon_split", 1, 1, FEARG_1, ret_list_string,
526#if defined(FEAT_BEVAL_TERM)
527 f_balloon_split
528#else
529 NULL
530#endif
531 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100532 {"browse", 4, 4, 0, ret_string, f_browse},
533 {"browsedir", 2, 2, 0, ret_string, f_browsedir},
534 {"bufadd", 1, 1, FEARG_1, ret_number, f_bufadd},
535 {"bufexists", 1, 1, FEARG_1, ret_number, f_bufexists},
536 {"buffer_exists", 1, 1, FEARG_1, ret_number, f_bufexists}, // obsolete
537 {"buffer_name", 0, 1, FEARG_1, ret_string, f_bufname}, // obsolete
538 {"buffer_number", 0, 1, FEARG_1, ret_number, f_bufnr}, // obsolete
539 {"buflisted", 1, 1, FEARG_1, ret_number, f_buflisted},
540 {"bufload", 1, 1, FEARG_1, ret_void, f_bufload},
541 {"bufloaded", 1, 1, FEARG_1, ret_number, f_bufloaded},
542 {"bufname", 0, 1, FEARG_1, ret_string, f_bufname},
543 {"bufnr", 0, 2, FEARG_1, ret_number, f_bufnr},
544 {"bufwinid", 1, 1, FEARG_1, ret_number, f_bufwinid},
545 {"bufwinnr", 1, 1, FEARG_1, ret_number, f_bufwinnr},
546 {"byte2line", 1, 1, FEARG_1, ret_number, f_byte2line},
547 {"byteidx", 2, 2, FEARG_1, ret_number, f_byteidx},
548 {"byteidxcomp", 2, 2, FEARG_1, ret_number, f_byteidxcomp},
549 {"call", 2, 3, FEARG_1, ret_any, f_call},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100550 {"ceil", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_ceil)},
551 {"ch_canread", 1, 1, FEARG_1, ret_number, JOB_FUNC(f_ch_canread)},
552 {"ch_close", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close)},
553 {"ch_close_in", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close_in)},
554 {"ch_evalexpr", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalexpr)},
555 {"ch_evalraw", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalraw)},
556 {"ch_getbufnr", 2, 2, FEARG_1, ret_number, JOB_FUNC(f_ch_getbufnr)},
557 {"ch_getjob", 1, 1, FEARG_1, ret_job, JOB_FUNC(f_ch_getjob)},
558 {"ch_info", 1, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_ch_info)},
559 {"ch_log", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_log)},
560 {"ch_logfile", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_logfile)},
561 {"ch_open", 1, 2, FEARG_1, ret_channel, JOB_FUNC(f_ch_open)},
562 {"ch_read", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_read)},
563 {"ch_readblob", 1, 2, FEARG_1, ret_blob, JOB_FUNC(f_ch_readblob)},
564 {"ch_readraw", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_readraw)},
565 {"ch_sendexpr", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendexpr)},
566 {"ch_sendraw", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendraw)},
567 {"ch_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_setoptions)},
568 {"ch_status", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_status)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100569 {"changenr", 0, 0, 0, ret_number, f_changenr},
570 {"char2nr", 1, 2, FEARG_1, ret_number, f_char2nr},
Bram Moolenaar4e4473c2020-08-28 22:24:57 +0200571 {"charclass", 1, 1, FEARG_1, ret_number, f_charclass},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100572 {"chdir", 1, 1, FEARG_1, ret_string, f_chdir},
573 {"cindent", 1, 1, FEARG_1, ret_number, f_cindent},
574 {"clearmatches", 0, 1, FEARG_1, ret_void, f_clearmatches},
575 {"col", 1, 1, FEARG_1, ret_number, f_col},
576 {"complete", 2, 2, FEARG_2, ret_void, f_complete},
577 {"complete_add", 1, 1, FEARG_1, ret_number, f_complete_add},
578 {"complete_check", 0, 0, 0, ret_number, f_complete_check},
579 {"complete_info", 0, 1, FEARG_1, ret_dict_any, f_complete_info},
580 {"confirm", 1, 4, FEARG_1, ret_number, f_confirm},
Bram Moolenaara66ba012020-07-05 18:41:08 +0200581 {"copy", 1, 1, FEARG_1, ret_first_arg, f_copy},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100582 {"cos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cos)},
583 {"cosh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cosh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100584 {"count", 2, 4, FEARG_1, ret_number, f_count},
585 {"cscope_connection",0,3, 0, ret_number, f_cscope_connection},
586 {"cursor", 1, 3, FEARG_1, ret_number, f_cursor},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100587 {"debugbreak", 1, 1, FEARG_1, ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100588#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100589 f_debugbreak
590#else
591 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200592#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100593 },
Bram Moolenaara66ba012020-07-05 18:41:08 +0200594 {"deepcopy", 1, 2, FEARG_1, ret_first_arg, f_deepcopy},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100595 {"delete", 1, 2, FEARG_1, ret_number, f_delete},
596 {"deletebufline", 2, 3, FEARG_1, ret_number, f_deletebufline},
597 {"did_filetype", 0, 0, 0, ret_number, f_did_filetype},
598 {"diff_filler", 1, 1, FEARG_1, ret_number, f_diff_filler},
599 {"diff_hlID", 2, 2, FEARG_1, ret_number, f_diff_hlID},
600 {"echoraw", 1, 1, FEARG_1, ret_number, f_echoraw},
601 {"empty", 1, 1, FEARG_1, ret_number, f_empty},
602 {"environ", 0, 0, 0, ret_dict_string, f_environ},
603 {"escape", 2, 2, FEARG_1, ret_string, f_escape},
604 {"eval", 1, 1, FEARG_1, ret_any, f_eval},
605 {"eventhandler", 0, 0, 0, ret_number, f_eventhandler},
606 {"executable", 1, 1, FEARG_1, ret_number, f_executable},
607 {"execute", 1, 2, FEARG_1, ret_string, f_execute},
608 {"exepath", 1, 1, FEARG_1, ret_string, f_exepath},
609 {"exists", 1, 1, FEARG_1, ret_number, f_exists},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100610 {"exp", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_exp)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100611 {"expand", 1, 3, FEARG_1, ret_any, f_expand},
612 {"expandcmd", 1, 1, FEARG_1, ret_string, f_expandcmd},
Bram Moolenaarb3c019c2020-07-05 20:08:39 +0200613 {"extend", 2, 3, FEARG_1, ret_first_arg, f_extend},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100614 {"feedkeys", 1, 2, FEARG_1, ret_void, f_feedkeys},
615 {"file_readable", 1, 1, FEARG_1, ret_number, f_filereadable}, // obsolete
616 {"filereadable", 1, 1, FEARG_1, ret_number, f_filereadable},
617 {"filewritable", 1, 1, FEARG_1, ret_number, f_filewritable},
Bram Moolenaar0d94ad62020-07-05 20:16:41 +0200618 {"filter", 2, 2, FEARG_1, ret_first_arg, f_filter},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100619 {"finddir", 1, 3, FEARG_1, ret_string, f_finddir},
620 {"findfile", 1, 3, FEARG_1, ret_string, f_findfile},
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200621 {"flatten", 1, 2, FEARG_1, ret_list_any, f_flatten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100622 {"float2nr", 1, 1, FEARG_1, ret_number, FLOAT_FUNC(f_float2nr)},
623 {"floor", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_floor)},
624 {"fmod", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_fmod)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100625 {"fnameescape", 1, 1, FEARG_1, ret_string, f_fnameescape},
626 {"fnamemodify", 2, 2, FEARG_1, ret_string, f_fnamemodify},
627 {"foldclosed", 1, 1, FEARG_1, ret_number, f_foldclosed},
628 {"foldclosedend", 1, 1, FEARG_1, ret_number, f_foldclosedend},
629 {"foldlevel", 1, 1, FEARG_1, ret_number, f_foldlevel},
630 {"foldtext", 0, 0, 0, ret_string, f_foldtext},
631 {"foldtextresult", 1, 1, FEARG_1, ret_string, f_foldtextresult},
632 {"foreground", 0, 0, 0, ret_void, f_foreground},
Bram Moolenaard77a8522020-04-03 21:59:57 +0200633 {"funcref", 1, 3, FEARG_1, ret_func_any, f_funcref},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100634 {"function", 1, 3, FEARG_1, ret_f_function, f_function},
635 {"garbagecollect", 0, 1, 0, ret_void, f_garbagecollect},
636 {"get", 2, 3, FEARG_1, ret_any, f_get},
Bram Moolenaar6434fc52020-07-18 22:24:22 +0200637 {"getbufinfo", 0, 1, FEARG_1, ret_list_dict_any, f_getbufinfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100638 {"getbufline", 2, 3, FEARG_1, ret_list_string, f_getbufline},
639 {"getbufvar", 2, 3, FEARG_1, ret_any, f_getbufvar},
640 {"getchangelist", 0, 1, FEARG_1, ret_list_any, f_getchangelist},
641 {"getchar", 0, 1, 0, ret_number, f_getchar},
642 {"getcharmod", 0, 0, 0, ret_number, f_getcharmod},
643 {"getcharsearch", 0, 0, 0, ret_dict_any, f_getcharsearch},
644 {"getcmdline", 0, 0, 0, ret_string, f_getcmdline},
645 {"getcmdpos", 0, 0, 0, ret_number, f_getcmdpos},
646 {"getcmdtype", 0, 0, 0, ret_string, f_getcmdtype},
647 {"getcmdwintype", 0, 0, 0, ret_string, f_getcmdwintype},
648 {"getcompletion", 2, 3, FEARG_1, ret_list_string, f_getcompletion},
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200649 {"getcurpos", 0, 1, FEARG_1, ret_list_number, f_getcurpos},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100650 {"getcwd", 0, 2, FEARG_1, ret_string, f_getcwd},
651 {"getenv", 1, 1, FEARG_1, ret_string, f_getenv},
652 {"getfontname", 0, 1, 0, ret_string, f_getfontname},
653 {"getfperm", 1, 1, FEARG_1, ret_string, f_getfperm},
654 {"getfsize", 1, 1, FEARG_1, ret_number, f_getfsize},
655 {"getftime", 1, 1, FEARG_1, ret_number, f_getftime},
656 {"getftype", 1, 1, FEARG_1, ret_string, f_getftype},
657 {"getimstatus", 0, 0, 0, ret_number, f_getimstatus},
658 {"getjumplist", 0, 2, FEARG_1, ret_list_any, f_getjumplist},
659 {"getline", 1, 2, FEARG_1, ret_f_getline, f_getline},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200660 {"getloclist", 1, 2, 0, ret_list_or_dict_1, f_getloclist},
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200661 {"getmarklist", 0, 1, FEARG_1, ret_list_dict_any, f_getmarklist},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100662 {"getmatches", 0, 1, 0, ret_list_dict_any, f_getmatches},
663 {"getmousepos", 0, 0, 0, ret_dict_number, f_getmousepos},
664 {"getpid", 0, 0, 0, ret_number, f_getpid},
665 {"getpos", 1, 1, FEARG_1, ret_list_number, f_getpos},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200666 {"getqflist", 0, 1, 0, ret_list_or_dict_0, f_getqflist},
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200667 {"getreg", 0, 3, FEARG_1, ret_getreg, f_getreg},
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200668 {"getreginfo", 0, 1, FEARG_1, ret_dict_any, f_getreginfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100669 {"getregtype", 0, 1, FEARG_1, ret_string, f_getregtype},
670 {"gettabinfo", 0, 1, FEARG_1, ret_list_dict_any, f_gettabinfo},
671 {"gettabvar", 2, 3, FEARG_1, ret_any, f_gettabvar},
672 {"gettabwinvar", 3, 4, FEARG_1, ret_any, f_gettabwinvar},
673 {"gettagstack", 0, 1, FEARG_1, ret_dict_any, f_gettagstack},
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +0200674 {"gettext", 1, 1, FEARG_1, ret_string, f_gettext},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100675 {"getwininfo", 0, 1, FEARG_1, ret_list_dict_any, f_getwininfo},
676 {"getwinpos", 0, 1, FEARG_1, ret_list_number, f_getwinpos},
677 {"getwinposx", 0, 0, 0, ret_number, f_getwinposx},
678 {"getwinposy", 0, 0, 0, ret_number, f_getwinposy},
679 {"getwinvar", 2, 3, FEARG_1, ret_any, f_getwinvar},
680 {"glob", 1, 4, FEARG_1, ret_any, f_glob},
681 {"glob2regpat", 1, 1, FEARG_1, ret_string, f_glob2regpat},
682 {"globpath", 2, 5, FEARG_2, ret_any, f_globpath},
Bram Moolenaar79296512020-03-22 16:17:14 +0100683 {"has", 1, 2, 0, ret_number, f_has},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100684 {"has_key", 2, 2, FEARG_1, ret_number, f_has_key},
685 {"haslocaldir", 0, 2, FEARG_1, ret_number, f_haslocaldir},
686 {"hasmapto", 1, 3, FEARG_1, ret_number, f_hasmapto},
687 {"highlightID", 1, 1, FEARG_1, ret_number, f_hlID}, // obsolete
688 {"highlight_exists",1, 1, FEARG_1, ret_number, f_hlexists}, // obsolete
689 {"histadd", 2, 2, FEARG_2, ret_number, f_histadd},
690 {"histdel", 1, 2, FEARG_1, ret_number, f_histdel},
691 {"histget", 1, 2, FEARG_1, ret_string, f_histget},
692 {"histnr", 1, 1, FEARG_1, ret_number, f_histnr},
693 {"hlID", 1, 1, FEARG_1, ret_number, f_hlID},
694 {"hlexists", 1, 1, FEARG_1, ret_number, f_hlexists},
695 {"hostname", 0, 0, 0, ret_string, f_hostname},
696 {"iconv", 3, 3, FEARG_1, ret_string, f_iconv},
697 {"indent", 1, 1, FEARG_1, ret_number, f_indent},
698 {"index", 2, 4, FEARG_1, ret_number, f_index},
699 {"input", 1, 3, FEARG_1, ret_string, f_input},
700 {"inputdialog", 1, 3, FEARG_1, ret_string, f_inputdialog},
701 {"inputlist", 1, 1, FEARG_1, ret_number, f_inputlist},
702 {"inputrestore", 0, 0, 0, ret_number, f_inputrestore},
703 {"inputsave", 0, 0, 0, ret_number, f_inputsave},
704 {"inputsecret", 1, 2, FEARG_1, ret_string, f_inputsecret},
Bram Moolenaar252e88a2020-07-05 20:47:18 +0200705 {"insert", 2, 3, FEARG_1, ret_first_arg, f_insert},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100706 {"interrupt", 0, 0, 0, ret_void, f_interrupt},
707 {"invert", 1, 1, FEARG_1, ret_number, f_invert},
708 {"isdirectory", 1, 1, FEARG_1, ret_number, f_isdirectory},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100709 {"isinf", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isinf)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100710 {"islocked", 1, 1, FEARG_1, ret_number, f_islocked},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100711 {"isnan", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isnan)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100712 {"items", 1, 1, FEARG_1, ret_list_any, f_items},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100713 {"job_getchannel", 1, 1, FEARG_1, ret_channel, JOB_FUNC(f_job_getchannel)},
714 {"job_info", 0, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_job_info)},
715 {"job_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_job_setoptions)},
716 {"job_start", 1, 2, FEARG_1, ret_job, JOB_FUNC(f_job_start)},
717 {"job_status", 1, 1, FEARG_1, ret_string, JOB_FUNC(f_job_status)},
718 {"job_stop", 1, 2, FEARG_1, ret_number, JOB_FUNC(f_job_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100719 {"join", 1, 2, FEARG_1, ret_string, f_join},
720 {"js_decode", 1, 1, FEARG_1, ret_any, f_js_decode},
721 {"js_encode", 1, 1, FEARG_1, ret_string, f_js_encode},
722 {"json_decode", 1, 1, FEARG_1, ret_any, f_json_decode},
723 {"json_encode", 1, 1, FEARG_1, ret_string, f_json_encode},
Bram Moolenaar32f335f2020-08-14 18:56:45 +0200724 {"keys", 1, 1, FEARG_1, ret_list_string, f_keys},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100725 {"last_buffer_nr", 0, 0, 0, ret_number, f_last_buffer_nr}, // obsolete
726 {"len", 1, 1, FEARG_1, ret_number, f_len},
727 {"libcall", 3, 3, FEARG_3, ret_string, f_libcall},
728 {"libcallnr", 3, 3, FEARG_3, ret_number, f_libcallnr},
729 {"line", 1, 2, FEARG_1, ret_number, f_line},
730 {"line2byte", 1, 1, FEARG_1, ret_number, f_line2byte},
731 {"lispindent", 1, 1, FEARG_1, ret_number, f_lispindent},
732 {"list2str", 1, 2, FEARG_1, ret_string, f_list2str},
733 {"listener_add", 1, 2, FEARG_2, ret_number, f_listener_add},
734 {"listener_flush", 0, 1, FEARG_1, ret_void, f_listener_flush},
735 {"listener_remove", 1, 1, FEARG_1, ret_number, f_listener_remove},
736 {"localtime", 0, 0, 0, ret_number, f_localtime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100737 {"log", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log)},
738 {"log10", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log10)},
739 {"luaeval", 1, 2, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200740#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +0100741 f_luaeval
742#else
743 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200744#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100745 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100746 {"map", 2, 2, FEARG_1, ret_any, f_map},
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200747 {"maparg", 1, 4, FEARG_1, ret_maparg, f_maparg},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100748 {"mapcheck", 1, 3, FEARG_1, ret_string, f_mapcheck},
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200749 {"mapset", 3, 3, FEARG_1, ret_void, f_mapset},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100750 {"match", 2, 4, FEARG_1, ret_any, f_match},
751 {"matchadd", 2, 5, FEARG_1, ret_number, f_matchadd},
752 {"matchaddpos", 2, 5, FEARG_1, ret_number, f_matchaddpos},
753 {"matcharg", 1, 1, FEARG_1, ret_list_string, f_matcharg},
754 {"matchdelete", 1, 2, FEARG_1, ret_number, f_matchdelete},
755 {"matchend", 2, 4, FEARG_1, ret_number, f_matchend},
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200756 {"matchfuzzy", 2, 3, FEARG_1, ret_list_string, f_matchfuzzy},
757 {"matchfuzzypos", 2, 3, FEARG_1, ret_list_any, f_matchfuzzypos},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100758 {"matchlist", 2, 4, FEARG_1, ret_list_string, f_matchlist},
759 {"matchstr", 2, 4, FEARG_1, ret_string, f_matchstr},
760 {"matchstrpos", 2, 4, FEARG_1, ret_list_any, f_matchstrpos},
761 {"max", 1, 1, FEARG_1, ret_any, f_max},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100762 {"menu_info", 1, 2, FEARG_1, ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100763#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +0100764 f_menu_info
765#else
766 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100767#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100768 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100769 {"min", 1, 1, FEARG_1, ret_any, f_min},
770 {"mkdir", 1, 3, FEARG_1, ret_number, f_mkdir},
771 {"mode", 0, 1, FEARG_1, ret_string, f_mode},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100772 {"mzeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200773#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100774 f_mzeval
775#else
776 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200777#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100778 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100779 {"nextnonblank", 1, 1, FEARG_1, ret_number, f_nextnonblank},
780 {"nr2char", 1, 2, FEARG_1, ret_string, f_nr2char},
781 {"or", 2, 2, FEARG_1, ret_number, f_or},
Bram Moolenaar6a33ef02020-09-25 22:42:48 +0200782 {"pathshorten", 1, 2, FEARG_1, ret_string, f_pathshorten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100783 {"perleval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200784#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100785 f_perleval
786#else
787 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200788#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100789 },
790 {"popup_atcursor", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_atcursor)},
791 {"popup_beval", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_beval)},
Bram Moolenaar03a9f842020-05-13 13:40:16 +0200792 {"popup_clear", 0, 1, 0, ret_void, PROP_FUNC(f_popup_clear)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100793 {"popup_close", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_close)},
794 {"popup_create", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_create)},
795 {"popup_dialog", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_dialog)},
796 {"popup_filter_menu", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_menu)},
797 {"popup_filter_yesno", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_yesno)},
798 {"popup_findinfo", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findinfo)},
799 {"popup_findpreview", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findpreview)},
800 {"popup_getoptions", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getoptions)},
801 {"popup_getpos", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getpos)},
802 {"popup_hide", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_hide)},
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200803 {"popup_list", 0, 0, 0, ret_list_number, PROP_FUNC(f_popup_list)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100804 {"popup_locate", 2, 2, 0, ret_number, PROP_FUNC(f_popup_locate)},
805 {"popup_menu", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_menu)},
806 {"popup_move", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_move)},
807 {"popup_notification", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_notification)},
808 {"popup_setoptions", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_setoptions)},
809 {"popup_settext", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_settext)},
810 {"popup_show", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_show)},
811 {"pow", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_pow)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100812 {"prevnonblank", 1, 1, FEARG_1, ret_number, f_prevnonblank},
813 {"printf", 1, 19, FEARG_2, ret_string, f_printf},
Bram Moolenaar077cc7a2020-09-04 16:35:35 +0200814 {"prompt_getprompt", 1, 1, FEARG_1, ret_string, JOB_FUNC(f_prompt_getprompt)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100815 {"prompt_setcallback", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setcallback)},
816 {"prompt_setinterrupt", 2, 2, FEARG_1,ret_void, JOB_FUNC(f_prompt_setinterrupt)},
817 {"prompt_setprompt", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setprompt)},
818 {"prop_add", 3, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_add)},
819 {"prop_clear", 1, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_clear)},
820 {"prop_find", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_find)},
821 {"prop_list", 1, 2, FEARG_1, ret_list_dict_any, PROP_FUNC(f_prop_list)},
822 {"prop_remove", 1, 3, FEARG_1, ret_number, PROP_FUNC(f_prop_remove)},
823 {"prop_type_add", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_add)},
824 {"prop_type_change", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_change)},
825 {"prop_type_delete", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_delete)},
826 {"prop_type_get", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_type_get)},
827 {"prop_type_list", 0, 1, FEARG_1, ret_list_string, PROP_FUNC(f_prop_type_list)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100828 {"pum_getpos", 0, 0, 0, ret_dict_number, f_pum_getpos},
829 {"pumvisible", 0, 0, 0, ret_number, f_pumvisible},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100830 {"py3eval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200831#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +0100832 f_py3eval
833#else
834 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200835#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100836 },
837 {"pyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200838#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +0100839 f_pyeval
840#else
841 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200842#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100843 },
844 {"pyxeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100845#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +0100846 f_pyxeval
847#else
848 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100849#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100850 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100851 {"rand", 0, 1, FEARG_1, ret_number, f_rand},
852 {"range", 1, 3, FEARG_1, ret_list_number, f_range},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200853 {"readdir", 1, 3, FEARG_1, ret_list_string, f_readdir},
854 {"readdirex", 1, 3, FEARG_1, ret_list_dict_any, f_readdirex},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100855 {"readfile", 1, 3, FEARG_1, ret_any, f_readfile},
Bram Moolenaar85629982020-06-01 18:39:20 +0200856 {"reduce", 2, 3, FEARG_1, ret_any, f_reduce},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100857 {"reg_executing", 0, 0, 0, ret_string, f_reg_executing},
858 {"reg_recording", 0, 0, 0, ret_string, f_reg_recording},
859 {"reltime", 0, 2, FEARG_1, ret_list_any, f_reltime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100860 {"reltimefloat", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_reltimefloat)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100861 {"reltimestr", 1, 1, FEARG_1, ret_string, f_reltimestr},
862 {"remote_expr", 2, 4, FEARG_1, ret_string, f_remote_expr},
863 {"remote_foreground", 1, 1, FEARG_1, ret_string, f_remote_foreground},
864 {"remote_peek", 1, 2, FEARG_1, ret_number, f_remote_peek},
865 {"remote_read", 1, 2, FEARG_1, ret_string, f_remote_read},
866 {"remote_send", 2, 3, FEARG_1, ret_string, f_remote_send},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200867 {"remote_startserver", 1, 1, FEARG_1, ret_void, f_remote_startserver},
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200868 {"remove", 2, 3, FEARG_1, ret_remove, f_remove},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100869 {"rename", 2, 2, FEARG_1, ret_number, f_rename},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200870 {"repeat", 2, 2, FEARG_1, ret_first_arg, f_repeat},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100871 {"resolve", 1, 1, FEARG_1, ret_string, f_resolve},
Bram Moolenaar67627352020-07-05 21:10:24 +0200872 {"reverse", 1, 1, FEARG_1, ret_first_arg, f_reverse},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100873 {"round", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_round)},
874 {"rubyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100875#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +0100876 f_rubyeval
877#else
878 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100879#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100880 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100881 {"screenattr", 2, 2, FEARG_1, ret_number, f_screenattr},
882 {"screenchar", 2, 2, FEARG_1, ret_number, f_screenchar},
883 {"screenchars", 2, 2, FEARG_1, ret_list_number, f_screenchars},
884 {"screencol", 0, 0, 0, ret_number, f_screencol},
885 {"screenpos", 3, 3, FEARG_1, ret_dict_number, f_screenpos},
886 {"screenrow", 0, 0, 0, ret_number, f_screenrow},
887 {"screenstring", 2, 2, FEARG_1, ret_string, f_screenstring},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200888 {"search", 1, 5, FEARG_1, ret_number, f_search},
Bram Moolenaare8f5ec02020-06-01 17:28:35 +0200889 {"searchcount", 0, 1, FEARG_1, ret_dict_any, f_searchcount},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100890 {"searchdecl", 1, 3, FEARG_1, ret_number, f_searchdecl},
891 {"searchpair", 3, 7, 0, ret_number, f_searchpair},
892 {"searchpairpos", 3, 7, 0, ret_list_number, f_searchpairpos},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200893 {"searchpos", 1, 5, FEARG_1, ret_list_number, f_searchpos},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100894 {"server2client", 2, 2, FEARG_1, ret_number, f_server2client},
895 {"serverlist", 0, 0, 0, ret_string, f_serverlist},
896 {"setbufline", 3, 3, FEARG_3, ret_number, f_setbufline},
897 {"setbufvar", 3, 3, FEARG_3, ret_void, f_setbufvar},
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200898 {"setcellwidths", 1, 1, FEARG_1, ret_void, f_setcellwidths},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100899 {"setcharsearch", 1, 1, FEARG_1, ret_void, f_setcharsearch},
900 {"setcmdpos", 1, 1, FEARG_1, ret_number, f_setcmdpos},
901 {"setenv", 2, 2, FEARG_2, ret_void, f_setenv},
902 {"setfperm", 2, 2, FEARG_1, ret_number, f_setfperm},
903 {"setline", 2, 2, FEARG_2, ret_number, f_setline},
904 {"setloclist", 2, 4, FEARG_2, ret_number, f_setloclist},
905 {"setmatches", 1, 2, FEARG_1, ret_number, f_setmatches},
906 {"setpos", 2, 2, FEARG_2, ret_number, f_setpos},
907 {"setqflist", 1, 3, FEARG_1, ret_number, f_setqflist},
908 {"setreg", 2, 3, FEARG_2, ret_number, f_setreg},
909 {"settabvar", 3, 3, FEARG_3, ret_void, f_settabvar},
910 {"settabwinvar", 4, 4, FEARG_4, ret_void, f_settabwinvar},
911 {"settagstack", 2, 3, FEARG_2, ret_number, f_settagstack},
912 {"setwinvar", 3, 3, FEARG_3, ret_void, f_setwinvar},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100913 {"sha256", 1, 1, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200914#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +0100915 f_sha256
916#else
917 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200918#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100919 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100920 {"shellescape", 1, 2, FEARG_1, ret_string, f_shellescape},
921 {"shiftwidth", 0, 1, FEARG_1, ret_number, f_shiftwidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100922 {"sign_define", 1, 2, FEARG_1, ret_any, SIGN_FUNC(f_sign_define)},
923 {"sign_getdefined", 0, 1, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
924 {"sign_getplaced", 0, 2, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
925 {"sign_jump", 3, 3, FEARG_1, ret_number, SIGN_FUNC(f_sign_jump)},
926 {"sign_place", 4, 5, FEARG_1, ret_number, SIGN_FUNC(f_sign_place)},
927 {"sign_placelist", 1, 1, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_placelist)},
928 {"sign_undefine", 0, 1, FEARG_1, ret_number, SIGN_FUNC(f_sign_undefine)},
929 {"sign_unplace", 1, 2, FEARG_1, ret_number, SIGN_FUNC(f_sign_unplace)},
930 {"sign_unplacelist", 1, 2, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200931 {"simplify", 1, 1, FEARG_1, ret_string, f_simplify},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100932 {"sin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sin)},
933 {"sinh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200934 {"sort", 1, 3, FEARG_1, ret_first_arg, f_sort},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100935 {"sound_clear", 0, 0, 0, ret_void, SOUND_FUNC(f_sound_clear)},
936 {"sound_playevent", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playevent)},
937 {"sound_playfile", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playfile)},
938 {"sound_stop", 1, 1, FEARG_1, ret_void, SOUND_FUNC(f_sound_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100939 {"soundfold", 1, 1, FEARG_1, ret_string, f_soundfold},
940 {"spellbadword", 0, 1, FEARG_1, ret_list_string, f_spellbadword},
941 {"spellsuggest", 1, 3, FEARG_1, ret_list_string, f_spellsuggest},
942 {"split", 1, 3, FEARG_1, ret_list_string, f_split},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100943 {"sqrt", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sqrt)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100944 {"srand", 0, 1, FEARG_1, ret_list_number, f_srand},
945 {"state", 0, 1, FEARG_1, ret_string, f_state},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100946 {"str2float", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_str2float)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100947 {"str2list", 1, 2, FEARG_1, ret_list_number, f_str2list},
948 {"str2nr", 1, 3, FEARG_1, ret_number, f_str2nr},
949 {"strcharpart", 2, 3, FEARG_1, ret_string, f_strcharpart},
950 {"strchars", 1, 2, FEARG_1, ret_number, f_strchars},
951 {"strdisplaywidth", 1, 2, FEARG_1, ret_number, f_strdisplaywidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100952 {"strftime", 1, 2, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200953#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100954 f_strftime
955#else
956 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200957#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100958 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100959 {"strgetchar", 2, 2, FEARG_1, ret_number, f_strgetchar},
960 {"stridx", 2, 3, FEARG_1, ret_number, f_stridx},
961 {"string", 1, 1, FEARG_1, ret_string, f_string},
962 {"strlen", 1, 1, FEARG_1, ret_number, f_strlen},
Bram Moolenaar6c53fca2020-08-23 17:34:46 +0200963 {"strpart", 2, 4, FEARG_1, ret_string, f_strpart},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100964 {"strptime", 2, 2, FEARG_1, ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +0100965#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100966 f_strptime
967#else
968 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +0100969#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100970 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100971 {"strridx", 2, 3, FEARG_1, ret_number, f_strridx},
972 {"strtrans", 1, 1, FEARG_1, ret_string, f_strtrans},
973 {"strwidth", 1, 1, FEARG_1, ret_number, f_strwidth},
974 {"submatch", 1, 2, FEARG_1, ret_string, f_submatch},
975 {"substitute", 4, 4, FEARG_1, ret_string, f_substitute},
976 {"swapinfo", 1, 1, FEARG_1, ret_dict_any, f_swapinfo},
977 {"swapname", 1, 1, FEARG_1, ret_string, f_swapname},
978 {"synID", 3, 3, 0, ret_number, f_synID},
979 {"synIDattr", 2, 3, FEARG_1, ret_string, f_synIDattr},
980 {"synIDtrans", 1, 1, FEARG_1, ret_number, f_synIDtrans},
981 {"synconcealed", 2, 2, 0, ret_list_any, f_synconcealed},
982 {"synstack", 2, 2, 0, ret_list_number, f_synstack},
983 {"system", 1, 2, FEARG_1, ret_string, f_system},
984 {"systemlist", 1, 2, FEARG_1, ret_list_string, f_systemlist},
985 {"tabpagebuflist", 0, 1, FEARG_1, ret_list_number, f_tabpagebuflist},
986 {"tabpagenr", 0, 1, 0, ret_number, f_tabpagenr},
987 {"tabpagewinnr", 1, 2, FEARG_1, ret_number, f_tabpagewinnr},
988 {"tagfiles", 0, 0, 0, ret_list_string, f_tagfiles},
989 {"taglist", 1, 2, FEARG_1, ret_list_dict_any, f_taglist},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100990 {"tan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tan)},
991 {"tanh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tanh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100992 {"tempname", 0, 0, 0, ret_string, f_tempname},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100993 {"term_dumpdiff", 2, 3, FEARG_1, ret_number, TERM_FUNC(f_term_dumpdiff)},
994 {"term_dumpload", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_dumpload)},
995 {"term_dumpwrite", 2, 3, FEARG_2, ret_void, TERM_FUNC(f_term_dumpwrite)},
996 {"term_getaltscreen", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getaltscreen)},
997 {"term_getansicolors", 1, 1, FEARG_1, ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100998#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100999 f_term_getansicolors
1000#else
1001 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001002#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001003 },
1004 {"term_getattr", 2, 2, FEARG_1, ret_number, TERM_FUNC(f_term_getattr)},
1005 {"term_getcursor", 1, 1, FEARG_1, ret_list_any, TERM_FUNC(f_term_getcursor)},
1006 {"term_getjob", 1, 1, FEARG_1, ret_job, TERM_FUNC(f_term_getjob)},
1007 {"term_getline", 2, 2, FEARG_1, ret_string, TERM_FUNC(f_term_getline)},
1008 {"term_getscrolled", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getscrolled)},
1009 {"term_getsize", 1, 1, FEARG_1, ret_list_number, TERM_FUNC(f_term_getsize)},
1010 {"term_getstatus", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_getstatus)},
1011 {"term_gettitle", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_gettitle)},
1012 {"term_gettty", 1, 2, FEARG_1, ret_string, TERM_FUNC(f_term_gettty)},
1013 {"term_list", 0, 0, 0, ret_list_number, TERM_FUNC(f_term_list)},
1014 {"term_scrape", 2, 2, FEARG_1, ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1015 {"term_sendkeys", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_sendkeys)},
1016 {"term_setansicolors", 2, 2, FEARG_1, ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001017#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001018 f_term_setansicolors
1019#else
1020 NULL
1021#endif
1022 },
1023 {"term_setapi", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setapi)},
1024 {"term_setkill", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setkill)},
1025 {"term_setrestore", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setrestore)},
1026 {"term_setsize", 3, 3, FEARG_1, ret_void, TERM_FUNC(f_term_setsize)},
1027 {"term_start", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_start)},
1028 {"term_wait", 1, 2, FEARG_1, ret_void, TERM_FUNC(f_term_wait)},
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001029 {"terminalprops", 0, 0, 0, ret_dict_string, f_terminalprops},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001030 {"test_alloc_fail", 3, 3, FEARG_1, ret_void, f_test_alloc_fail},
1031 {"test_autochdir", 0, 0, 0, ret_void, f_test_autochdir},
1032 {"test_feedinput", 1, 1, FEARG_1, ret_void, f_test_feedinput},
1033 {"test_garbagecollect_now", 0, 0, 0, ret_void, f_test_garbagecollect_now},
1034 {"test_garbagecollect_soon", 0, 0, 0, ret_void, f_test_garbagecollect_soon},
1035 {"test_getvalue", 1, 1, FEARG_1, ret_number, f_test_getvalue},
1036 {"test_ignore_error", 1, 1, FEARG_1, ret_void, f_test_ignore_error},
1037 {"test_null_blob", 0, 0, 0, ret_blob, f_test_null_blob},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001038 {"test_null_channel", 0, 0, 0, ret_channel, JOB_FUNC(f_test_null_channel)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001039 {"test_null_dict", 0, 0, 0, ret_dict_any, f_test_null_dict},
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001040 {"test_null_function", 0, 0, 0, ret_func_any, f_test_null_function},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001041 {"test_null_job", 0, 0, 0, ret_job, JOB_FUNC(f_test_null_job)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001042 {"test_null_list", 0, 0, 0, ret_list_any, f_test_null_list},
Bram Moolenaard77a8522020-04-03 21:59:57 +02001043 {"test_null_partial", 0, 0, 0, ret_func_any, f_test_null_partial},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001044 {"test_null_string", 0, 0, 0, ret_string, f_test_null_string},
1045 {"test_option_not_set", 1, 1, FEARG_1,ret_void, f_test_option_not_set},
1046 {"test_override", 2, 2, FEARG_2, ret_void, f_test_override},
1047 {"test_refcount", 1, 1, FEARG_1, ret_number, f_test_refcount},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001048 {"test_scrollbar", 3, 3, FEARG_2, ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001049#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001050 f_test_scrollbar
1051#else
1052 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001053#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001054 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001055 {"test_setmouse", 2, 2, 0, ret_void, f_test_setmouse},
1056 {"test_settime", 1, 1, FEARG_1, ret_void, f_test_settime},
1057 {"test_srand_seed", 0, 1, FEARG_1, ret_void, f_test_srand_seed},
1058 {"test_unknown", 0, 0, 0, ret_any, f_test_unknown},
Bram Moolenaar418f1df2020-08-12 21:34:49 +02001059 {"test_void", 0, 0, 0, ret_void, f_test_void},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001060 {"timer_info", 0, 1, FEARG_1, ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1061 {"timer_pause", 2, 2, FEARG_1, ret_void, TIMER_FUNC(f_timer_pause)},
1062 {"timer_start", 2, 3, FEARG_1, ret_number, TIMER_FUNC(f_timer_start)},
1063 {"timer_stop", 1, 1, FEARG_1, ret_void, TIMER_FUNC(f_timer_stop)},
1064 {"timer_stopall", 0, 0, 0, ret_void, TIMER_FUNC(f_timer_stopall)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001065 {"tolower", 1, 1, FEARG_1, ret_string, f_tolower},
1066 {"toupper", 1, 1, FEARG_1, ret_string, f_toupper},
1067 {"tr", 3, 3, FEARG_1, ret_string, f_tr},
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001068 {"trim", 1, 3, FEARG_1, ret_string, f_trim},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001069 {"trunc", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_trunc)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001070 {"type", 1, 1, FEARG_1, ret_number, f_type},
1071 {"undofile", 1, 1, FEARG_1, ret_string, f_undofile},
1072 {"undotree", 0, 0, 0, ret_dict_any, f_undotree},
1073 {"uniq", 1, 3, FEARG_1, ret_list_any, f_uniq},
1074 {"values", 1, 1, FEARG_1, ret_list_any, f_values},
1075 {"virtcol", 1, 1, FEARG_1, ret_number, f_virtcol},
1076 {"visualmode", 0, 1, 0, ret_string, f_visualmode},
1077 {"wildmenumode", 0, 0, 0, ret_number, f_wildmenumode},
1078 {"win_execute", 2, 3, FEARG_2, ret_string, f_win_execute},
1079 {"win_findbuf", 1, 1, FEARG_1, ret_list_number, f_win_findbuf},
1080 {"win_getid", 0, 2, FEARG_1, ret_number, f_win_getid},
1081 {"win_gettype", 0, 1, FEARG_1, ret_string, f_win_gettype},
1082 {"win_gotoid", 1, 1, FEARG_1, ret_number, f_win_gotoid},
1083 {"win_id2tabwin", 1, 1, FEARG_1, ret_list_number, f_win_id2tabwin},
1084 {"win_id2win", 1, 1, FEARG_1, ret_number, f_win_id2win},
1085 {"win_screenpos", 1, 1, FEARG_1, ret_list_number, f_win_screenpos},
1086 {"win_splitmove", 2, 3, FEARG_1, ret_number, f_win_splitmove},
1087 {"winbufnr", 1, 1, FEARG_1, ret_number, f_winbufnr},
1088 {"wincol", 0, 0, 0, ret_number, f_wincol},
1089 {"windowsversion", 0, 0, 0, ret_string, f_windowsversion},
1090 {"winheight", 1, 1, FEARG_1, ret_number, f_winheight},
1091 {"winlayout", 0, 1, FEARG_1, ret_list_any, f_winlayout},
1092 {"winline", 0, 0, 0, ret_number, f_winline},
1093 {"winnr", 0, 1, FEARG_1, ret_number, f_winnr},
1094 {"winrestcmd", 0, 0, 0, ret_string, f_winrestcmd},
1095 {"winrestview", 1, 1, FEARG_1, ret_void, f_winrestview},
1096 {"winsaveview", 0, 0, 0, ret_dict_any, f_winsaveview},
1097 {"winwidth", 1, 1, FEARG_1, ret_number, f_winwidth},
1098 {"wordcount", 0, 0, 0, ret_dict_number, f_wordcount},
1099 {"writefile", 2, 3, FEARG_1, ret_number, f_writefile},
1100 {"xor", 2, 2, FEARG_1, ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001101};
1102
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001103/*
1104 * Function given to ExpandGeneric() to obtain the list of internal
1105 * or user defined function names.
1106 */
1107 char_u *
1108get_function_name(expand_T *xp, int idx)
1109{
1110 static int intidx = -1;
1111 char_u *name;
1112
1113 if (idx == 0)
1114 intidx = -1;
1115 if (intidx < 0)
1116 {
1117 name = get_user_func_name(xp, idx);
1118 if (name != NULL)
1119 return name;
1120 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001121 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001122 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001123 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001124 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001125 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001126 STRCAT(IObuff, ")");
1127 return IObuff;
1128 }
1129
1130 return NULL;
1131}
1132
1133/*
1134 * Function given to ExpandGeneric() to obtain the list of internal or
1135 * user defined variable or function names.
1136 */
1137 char_u *
1138get_expr_name(expand_T *xp, int idx)
1139{
1140 static int intidx = -1;
1141 char_u *name;
1142
1143 if (idx == 0)
1144 intidx = -1;
1145 if (intidx < 0)
1146 {
1147 name = get_function_name(xp, idx);
1148 if (name != NULL)
1149 return name;
1150 }
1151 return get_user_var_name(xp, ++intidx);
1152}
1153
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001154/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001155 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001156 * Return index, or -1 if not found or "implemented" is TRUE and the function
1157 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001158 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001159 static int
1160find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001161{
1162 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001163 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001164 int cmp;
1165 int x;
1166
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001167 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001168
1169 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001170 while (first <= last)
1171 {
1172 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001173 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001174 if (cmp < 0)
1175 last = x - 1;
1176 else if (cmp > 0)
1177 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001178 else if (implemented && global_functions[x].f_func == NULL)
1179 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001180 else
1181 return x;
1182 }
1183 return -1;
1184}
1185
Bram Moolenaar15c47602020-03-26 22:16:48 +01001186/*
1187 * Find internal function "name" in table "global_functions".
1188 * Return index, or -1 if not found or the function is not implemented.
1189 */
1190 int
1191find_internal_func(char_u *name)
1192{
1193 return find_internal_func_opt(name, TRUE);
1194}
1195
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001196 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001197has_internal_func(char_u *name)
1198{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001199 return find_internal_func_opt(name, TRUE) >= 0;
1200}
1201
1202 static int
1203has_internal_func_name(char_u *name)
1204{
1205 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001206}
1207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208 char *
1209internal_func_name(int idx)
1210{
1211 return global_functions[idx].f_name;
1212}
1213
1214 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001215internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001217 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218}
1219
1220/*
1221 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02001222 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
1223 * first argument, 2 if method base is second argument, etc. 9 if method base
1224 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 */
1226 int
1227check_internal_func(int idx, int argcount)
1228{
1229 int res;
1230 char *name;
1231
1232 if (argcount < global_functions[idx].f_min_argc)
1233 res = FCERR_TOOFEW;
1234 else if (argcount > global_functions[idx].f_max_argc)
1235 res = FCERR_TOOMANY;
1236 else
Bram Moolenaar389df252020-07-09 21:20:47 +02001237 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238
1239 name = internal_func_name(idx);
1240 if (res == FCERR_TOOMANY)
1241 semsg(_(e_toomanyarg), name);
1242 else
1243 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02001244 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245}
1246
Bram Moolenaarac92e252019-08-03 21:58:38 +02001247 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001248call_internal_func(
1249 char_u *name,
1250 int argcount,
1251 typval_T *argvars,
1252 typval_T *rettv)
1253{
1254 int i;
1255
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001256 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001257 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001258 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001259 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001260 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001261 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001262 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001263 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001264 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001265 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001266}
1267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 void
1269call_internal_func_by_idx(
1270 int idx,
1271 typval_T *argvars,
1272 typval_T *rettv)
1273{
1274 global_functions[idx].f_func(argvars, rettv);
1275}
1276
Bram Moolenaarac92e252019-08-03 21:58:38 +02001277/*
1278 * Invoke a method for base->method().
1279 */
1280 int
1281call_internal_method(
1282 char_u *name,
1283 int argcount,
1284 typval_T *argvars,
1285 typval_T *rettv,
1286 typval_T *basetv)
1287{
1288 int i;
1289 int fi;
1290 typval_T argv[MAX_FUNC_ARGS + 1];
1291
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001292 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02001293 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001294 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02001295 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001296 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001297 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001298 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001299 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001300 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001301
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001302 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001303 {
1304 // base value goes last
1305 for (i = 0; i < argcount; ++i)
1306 argv[i] = argvars[i];
1307 argv[argcount] = *basetv;
1308 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001309 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001310 {
1311 // base value goes second
1312 argv[0] = argvars[0];
1313 argv[1] = *basetv;
1314 for (i = 1; i < argcount; ++i)
1315 argv[i + 1] = argvars[i];
1316 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02001317 else if (global_functions[fi].f_argtype == FEARG_3)
1318 {
1319 // base value goes third
1320 argv[0] = argvars[0];
1321 argv[1] = argvars[1];
1322 argv[2] = *basetv;
1323 for (i = 2; i < argcount; ++i)
1324 argv[i + 1] = argvars[i];
1325 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001326 else if (global_functions[fi].f_argtype == FEARG_4)
1327 {
1328 // base value goes fourth
1329 argv[0] = argvars[0];
1330 argv[1] = argvars[1];
1331 argv[2] = argvars[2];
1332 argv[3] = *basetv;
1333 for (i = 3; i < argcount; ++i)
1334 argv[i + 1] = argvars[i];
1335 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02001336 else
1337 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001338 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02001339 argv[0] = *basetv;
1340 for (i = 0; i < argcount; ++i)
1341 argv[i + 1] = argvars[i];
1342 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001343 argv[argcount + 1].v_type = VAR_UNKNOWN;
1344
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001345 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001346 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001347}
1348
1349/*
1350 * Return TRUE for a non-zero Number and a non-empty String.
1351 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001352 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001353non_zero_arg(typval_T *argvars)
1354{
1355 return ((argvars[0].v_type == VAR_NUMBER
1356 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001357 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001358 && argvars[0].vval.v_number == VVAL_TRUE)
1359 || (argvars[0].v_type == VAR_STRING
1360 && argvars[0].vval.v_string != NULL
1361 && *argvars[0].vval.v_string != NUL));
1362}
1363
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001364#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001365/*
1366 * Get the float value of "argvars[0]" into "f".
1367 * Returns FAIL when the argument is not a Number or Float.
1368 */
1369 static int
1370get_float_arg(typval_T *argvars, float_T *f)
1371{
1372 if (argvars[0].v_type == VAR_FLOAT)
1373 {
1374 *f = argvars[0].vval.v_float;
1375 return OK;
1376 }
1377 if (argvars[0].v_type == VAR_NUMBER)
1378 {
1379 *f = (float_T)argvars[0].vval.v_number;
1380 return OK;
1381 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001382 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001383 return FAIL;
1384}
1385
1386/*
1387 * "abs(expr)" function
1388 */
1389 static void
1390f_abs(typval_T *argvars, typval_T *rettv)
1391{
1392 if (argvars[0].v_type == VAR_FLOAT)
1393 {
1394 rettv->v_type = VAR_FLOAT;
1395 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1396 }
1397 else
1398 {
1399 varnumber_T n;
1400 int error = FALSE;
1401
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001402 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001403 if (error)
1404 rettv->vval.v_number = -1;
1405 else if (n > 0)
1406 rettv->vval.v_number = n;
1407 else
1408 rettv->vval.v_number = -n;
1409 }
1410}
1411
1412/*
1413 * "acos()" function
1414 */
1415 static void
1416f_acos(typval_T *argvars, typval_T *rettv)
1417{
1418 float_T f = 0.0;
1419
1420 rettv->v_type = VAR_FLOAT;
1421 if (get_float_arg(argvars, &f) == OK)
1422 rettv->vval.v_float = acos(f);
1423 else
1424 rettv->vval.v_float = 0.0;
1425}
1426#endif
1427
1428/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001429 * "and(expr, expr)" function
1430 */
1431 static void
1432f_and(typval_T *argvars, typval_T *rettv)
1433{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001434 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1435 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001436}
1437
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001438#ifdef FEAT_FLOAT
1439/*
1440 * "asin()" function
1441 */
1442 static void
1443f_asin(typval_T *argvars, typval_T *rettv)
1444{
1445 float_T f = 0.0;
1446
1447 rettv->v_type = VAR_FLOAT;
1448 if (get_float_arg(argvars, &f) == OK)
1449 rettv->vval.v_float = asin(f);
1450 else
1451 rettv->vval.v_float = 0.0;
1452}
1453
1454/*
1455 * "atan()" function
1456 */
1457 static void
1458f_atan(typval_T *argvars, typval_T *rettv)
1459{
1460 float_T f = 0.0;
1461
1462 rettv->v_type = VAR_FLOAT;
1463 if (get_float_arg(argvars, &f) == OK)
1464 rettv->vval.v_float = atan(f);
1465 else
1466 rettv->vval.v_float = 0.0;
1467}
1468
1469/*
1470 * "atan2()" function
1471 */
1472 static void
1473f_atan2(typval_T *argvars, typval_T *rettv)
1474{
1475 float_T fx = 0.0, fy = 0.0;
1476
1477 rettv->v_type = VAR_FLOAT;
1478 if (get_float_arg(argvars, &fx) == OK
1479 && get_float_arg(&argvars[1], &fy) == OK)
1480 rettv->vval.v_float = atan2(fx, fy);
1481 else
1482 rettv->vval.v_float = 0.0;
1483}
1484#endif
1485
1486/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001487 * "balloon_show()" function
1488 */
1489#ifdef FEAT_BEVAL
1490 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001491f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1492{
1493 rettv->v_type = VAR_STRING;
1494 if (balloonEval != NULL)
1495 {
1496 if (balloonEval->msg == NULL)
1497 rettv->vval.v_string = NULL;
1498 else
1499 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1500 }
1501}
1502
1503 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001504f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1505{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001506 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001507 {
1508 if (argvars[0].v_type == VAR_LIST
1509# ifdef FEAT_GUI
1510 && !gui.in_use
1511# endif
1512 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001513 {
1514 list_T *l = argvars[0].vval.v_list;
1515
1516 // empty list removes the balloon
1517 post_balloon(balloonEval, NULL,
1518 l == NULL || l->lv_len == 0 ? NULL : l);
1519 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001520 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001521 {
1522 char_u *mesg = tv_get_string_chk(&argvars[0]);
1523
1524 if (mesg != NULL)
1525 // empty string removes the balloon
1526 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1527 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001528 }
1529}
1530
Bram Moolenaar669a8282017-11-19 20:13:05 +01001531# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001532 static void
1533f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1534{
1535 if (rettv_list_alloc(rettv) == OK)
1536 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001537 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001538
1539 if (msg != NULL)
1540 {
1541 pumitem_T *array;
1542 int size = split_message(msg, &array);
1543 int i;
1544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001545 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001546 for (i = 1; i < size - 1; ++i)
1547 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001548 while (size > 0)
1549 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001550 vim_free(array);
1551 }
1552 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001553}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001554# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001555#endif
1556
1557/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001558 * Get the buffer from "arg" and give an error and return NULL if it is not
1559 * valid.
1560 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001561 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001562get_buf_arg(typval_T *arg)
1563{
1564 buf_T *buf;
1565
1566 ++emsg_off;
1567 buf = tv_get_buf(arg, FALSE);
1568 --emsg_off;
1569 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001570 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001571 return buf;
1572}
1573
1574/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001575 * "byte2line(byte)" function
1576 */
1577 static void
1578f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1579{
1580#ifndef FEAT_BYTEOFF
1581 rettv->vval.v_number = -1;
1582#else
1583 long boff = 0;
1584
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001585 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001586 if (boff < 0)
1587 rettv->vval.v_number = -1;
1588 else
1589 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1590 (linenr_T)0, &boff);
1591#endif
1592}
1593
1594 static void
1595byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1596{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001597 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001598 char_u *str;
1599 varnumber_T idx;
1600
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001601 str = tv_get_string_chk(&argvars[0]);
1602 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001603 rettv->vval.v_number = -1;
1604 if (str == NULL || idx < 0)
1605 return;
1606
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001607 t = str;
1608 for ( ; idx > 0; idx--)
1609 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001610 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001611 return;
1612 if (enc_utf8 && comp)
1613 t += utf_ptr2len(t);
1614 else
1615 t += (*mb_ptr2len)(t);
1616 }
1617 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001618}
1619
1620/*
1621 * "byteidx()" function
1622 */
1623 static void
1624f_byteidx(typval_T *argvars, typval_T *rettv)
1625{
1626 byteidx(argvars, rettv, FALSE);
1627}
1628
1629/*
1630 * "byteidxcomp()" function
1631 */
1632 static void
1633f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1634{
1635 byteidx(argvars, rettv, TRUE);
1636}
1637
1638/*
1639 * "call(func, arglist [, dict])" function
1640 */
1641 static void
1642f_call(typval_T *argvars, typval_T *rettv)
1643{
1644 char_u *func;
1645 partial_T *partial = NULL;
1646 dict_T *selfdict = NULL;
1647
1648 if (argvars[1].v_type != VAR_LIST)
1649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001650 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001651 return;
1652 }
1653 if (argvars[1].vval.v_list == NULL)
1654 return;
1655
1656 if (argvars[0].v_type == VAR_FUNC)
1657 func = argvars[0].vval.v_string;
1658 else if (argvars[0].v_type == VAR_PARTIAL)
1659 {
1660 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001661 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001662 }
1663 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001664 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001665 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001666 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001667
1668 if (argvars[2].v_type != VAR_UNKNOWN)
1669 {
1670 if (argvars[2].v_type != VAR_DICT)
1671 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001672 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001673 return;
1674 }
1675 selfdict = argvars[2].vval.v_dict;
1676 }
1677
1678 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1679}
1680
1681#ifdef FEAT_FLOAT
1682/*
1683 * "ceil({float})" function
1684 */
1685 static void
1686f_ceil(typval_T *argvars, typval_T *rettv)
1687{
1688 float_T f = 0.0;
1689
1690 rettv->v_type = VAR_FLOAT;
1691 if (get_float_arg(argvars, &f) == OK)
1692 rettv->vval.v_float = ceil(f);
1693 else
1694 rettv->vval.v_float = 0.0;
1695}
1696#endif
1697
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001698/*
1699 * "changenr()" function
1700 */
1701 static void
1702f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
1703{
1704 rettv->vval.v_number = curbuf->b_u_seq_cur;
1705}
1706
1707/*
1708 * "char2nr(string)" function
1709 */
1710 static void
1711f_char2nr(typval_T *argvars, typval_T *rettv)
1712{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001713 if (has_mbyte)
1714 {
1715 int utf8 = 0;
1716
1717 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02001718 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001719
1720 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01001721 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001722 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001723 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001724 }
1725 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001726 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001727}
1728
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001729 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01001730get_optional_window(typval_T *argvars, int idx)
1731{
1732 win_T *win = curwin;
1733
1734 if (argvars[idx].v_type != VAR_UNKNOWN)
1735 {
1736 win = find_win_by_nr_or_id(&argvars[idx]);
1737 if (win == NULL)
1738 {
1739 emsg(_(e_invalwindow));
1740 return NULL;
1741 }
1742 }
1743 return win;
1744}
1745
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001746/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001747 * "col(string)" function
1748 */
1749 static void
1750f_col(typval_T *argvars, typval_T *rettv)
1751{
1752 colnr_T col = 0;
1753 pos_T *fp;
1754 int fnum = curbuf->b_fnum;
1755
1756 fp = var2fpos(&argvars[0], FALSE, &fnum);
1757 if (fp != NULL && fnum == curbuf->b_fnum)
1758 {
1759 if (fp->col == MAXCOL)
1760 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001761 // '> can be MAXCOL, get the length of the line then
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001762 if (fp->lnum <= curbuf->b_ml.ml_line_count)
1763 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
1764 else
1765 col = MAXCOL;
1766 }
1767 else
1768 {
1769 col = fp->col + 1;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001770 // col(".") when the cursor is on the NUL at the end of the line
1771 // because of "coladd" can be seen as an extra column.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001772 if (virtual_active() && fp == &curwin->w_cursor)
1773 {
1774 char_u *p = ml_get_cursor();
1775
1776 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
1777 curwin->w_virtcol - curwin->w_cursor.coladd))
1778 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001779 int l;
1780
1781 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
1782 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001783 }
1784 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001785 }
1786 }
1787 rettv->vval.v_number = col;
1788}
1789
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001790/*
1791 * "confirm(message, buttons[, default [, type]])" function
1792 */
1793 static void
1794f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1795{
1796#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1797 char_u *message;
1798 char_u *buttons = NULL;
1799 char_u buf[NUMBUFLEN];
1800 char_u buf2[NUMBUFLEN];
1801 int def = 1;
1802 int type = VIM_GENERIC;
1803 char_u *typestr;
1804 int error = FALSE;
1805
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001806 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001807 if (message == NULL)
1808 error = TRUE;
1809 if (argvars[1].v_type != VAR_UNKNOWN)
1810 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001811 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001812 if (buttons == NULL)
1813 error = TRUE;
1814 if (argvars[2].v_type != VAR_UNKNOWN)
1815 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001816 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001817 if (argvars[3].v_type != VAR_UNKNOWN)
1818 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001819 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001820 if (typestr == NULL)
1821 error = TRUE;
1822 else
1823 {
1824 switch (TOUPPER_ASC(*typestr))
1825 {
1826 case 'E': type = VIM_ERROR; break;
1827 case 'Q': type = VIM_QUESTION; break;
1828 case 'I': type = VIM_INFO; break;
1829 case 'W': type = VIM_WARNING; break;
1830 case 'G': type = VIM_GENERIC; break;
1831 }
1832 }
1833 }
1834 }
1835 }
1836
1837 if (buttons == NULL || *buttons == NUL)
1838 buttons = (char_u *)_("&Ok");
1839
1840 if (!error)
1841 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
1842 def, NULL, FALSE);
1843#endif
1844}
1845
1846/*
1847 * "copy()" function
1848 */
1849 static void
1850f_copy(typval_T *argvars, typval_T *rettv)
1851{
1852 item_copy(&argvars[0], rettv, FALSE, 0);
1853}
1854
1855#ifdef FEAT_FLOAT
1856/*
1857 * "cos()" function
1858 */
1859 static void
1860f_cos(typval_T *argvars, typval_T *rettv)
1861{
1862 float_T f = 0.0;
1863
1864 rettv->v_type = VAR_FLOAT;
1865 if (get_float_arg(argvars, &f) == OK)
1866 rettv->vval.v_float = cos(f);
1867 else
1868 rettv->vval.v_float = 0.0;
1869}
1870
1871/*
1872 * "cosh()" function
1873 */
1874 static void
1875f_cosh(typval_T *argvars, typval_T *rettv)
1876{
1877 float_T f = 0.0;
1878
1879 rettv->v_type = VAR_FLOAT;
1880 if (get_float_arg(argvars, &f) == OK)
1881 rettv->vval.v_float = cosh(f);
1882 else
1883 rettv->vval.v_float = 0.0;
1884}
1885#endif
1886
1887/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001888 * "cursor(lnum, col)" function, or
1889 * "cursor(list)"
1890 *
1891 * Moves the cursor to the specified line and column.
1892 * Returns 0 when the position could be set, -1 otherwise.
1893 */
1894 static void
1895f_cursor(typval_T *argvars, typval_T *rettv)
1896{
1897 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001898 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001899 int set_curswant = TRUE;
1900
1901 rettv->vval.v_number = -1;
1902 if (argvars[1].v_type == VAR_UNKNOWN)
1903 {
1904 pos_T pos;
1905 colnr_T curswant = -1;
1906
1907 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
1908 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001909 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001910 return;
1911 }
1912 line = pos.lnum;
1913 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001914 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001915 if (curswant >= 0)
1916 {
1917 curwin->w_curswant = curswant - 1;
1918 set_curswant = FALSE;
1919 }
1920 }
1921 else
1922 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001923 line = tv_get_lnum(argvars);
1924 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001925 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001926 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001928 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001929 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001930 if (line > 0)
1931 curwin->w_cursor.lnum = line;
1932 if (col > 0)
1933 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001934 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001935
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001936 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001937 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001938 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001939 if (has_mbyte)
1940 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001941
1942 curwin->w_set_curswant = set_curswant;
1943 rettv->vval.v_number = 0;
1944}
1945
Bram Moolenaar4f974752019-02-17 17:44:42 +01001946#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001947/*
1948 * "debugbreak()" function
1949 */
1950 static void
1951f_debugbreak(typval_T *argvars, typval_T *rettv)
1952{
1953 int pid;
1954
1955 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001956 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001957 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001958 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001959 else
1960 {
1961 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
1962
1963 if (hProcess != NULL)
1964 {
1965 DebugBreakProcess(hProcess);
1966 CloseHandle(hProcess);
1967 rettv->vval.v_number = OK;
1968 }
1969 }
1970}
1971#endif
1972
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001973/*
1974 * "deepcopy()" function
1975 */
1976 static void
1977f_deepcopy(typval_T *argvars, typval_T *rettv)
1978{
1979 int noref = 0;
1980 int copyID;
1981
1982 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar44b4a242020-09-05 17:18:28 +02001983 noref = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001984 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001985 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001986 else
1987 {
1988 copyID = get_copyID();
1989 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
1990 }
1991}
1992
1993/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001994 * "did_filetype()" function
1995 */
1996 static void
1997f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1998{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001999 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002000}
2001
2002/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002003 * "echoraw({expr})" function
2004 */
2005 static void
2006f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2007{
2008 char_u *str = tv_get_string_chk(&argvars[0]);
2009
2010 if (str != NULL && *str != NUL)
2011 {
2012 out_str(str);
2013 out_flush();
2014 }
2015}
2016
2017/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002018 * "empty({expr})" function
2019 */
2020 static void
2021f_empty(typval_T *argvars, typval_T *rettv)
2022{
2023 int n = FALSE;
2024
2025 switch (argvars[0].v_type)
2026 {
2027 case VAR_STRING:
2028 case VAR_FUNC:
2029 n = argvars[0].vval.v_string == NULL
2030 || *argvars[0].vval.v_string == NUL;
2031 break;
2032 case VAR_PARTIAL:
2033 n = FALSE;
2034 break;
2035 case VAR_NUMBER:
2036 n = argvars[0].vval.v_number == 0;
2037 break;
2038 case VAR_FLOAT:
2039#ifdef FEAT_FLOAT
2040 n = argvars[0].vval.v_float == 0.0;
2041 break;
2042#endif
2043 case VAR_LIST:
2044 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002045 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002046 break;
2047 case VAR_DICT:
2048 n = argvars[0].vval.v_dict == NULL
2049 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2050 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002051 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002052 case VAR_SPECIAL:
2053 n = argvars[0].vval.v_number != VVAL_TRUE;
2054 break;
2055
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002056 case VAR_BLOB:
2057 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002058 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2059 break;
2060
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002061 case VAR_JOB:
2062#ifdef FEAT_JOB_CHANNEL
2063 n = argvars[0].vval.v_job == NULL
2064 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2065 break;
2066#endif
2067 case VAR_CHANNEL:
2068#ifdef FEAT_JOB_CHANNEL
2069 n = argvars[0].vval.v_channel == NULL
2070 || !channel_is_open(argvars[0].vval.v_channel);
2071 break;
2072#endif
2073 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002074 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002076 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002077 n = TRUE;
2078 break;
2079 }
2080
2081 rettv->vval.v_number = n;
2082}
2083
2084/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002085 * "environ()" function
2086 */
2087 static void
2088f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2089{
2090#if !defined(AMIGA)
2091 int i = 0;
2092 char_u *entry, *value;
2093# ifdef MSWIN
2094 extern wchar_t **_wenviron;
2095# else
2096 extern char **environ;
2097# endif
2098
2099 if (rettv_dict_alloc(rettv) != OK)
2100 return;
2101
2102# ifdef MSWIN
2103 if (*_wenviron == NULL)
2104 return;
2105# else
2106 if (*environ == NULL)
2107 return;
2108# endif
2109
2110 for (i = 0; ; ++i)
2111 {
2112# ifdef MSWIN
2113 short_u *p;
2114
2115 if ((p = (short_u *)_wenviron[i]) == NULL)
2116 return;
2117 entry = utf16_to_enc(p, NULL);
2118# else
2119 if ((entry = (char_u *)environ[i]) == NULL)
2120 return;
2121 entry = vim_strsave(entry);
2122# endif
2123 if (entry == NULL) // out of memory
2124 return;
2125 if ((value = vim_strchr(entry, '=')) == NULL)
2126 {
2127 vim_free(entry);
2128 continue;
2129 }
2130 *value++ = NUL;
2131 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2132 vim_free(entry);
2133 }
2134#endif
2135}
2136
2137/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002138 * "escape({string}, {chars})" function
2139 */
2140 static void
2141f_escape(typval_T *argvars, typval_T *rettv)
2142{
2143 char_u buf[NUMBUFLEN];
2144
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002145 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2146 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002147 rettv->v_type = VAR_STRING;
2148}
2149
2150/*
2151 * "eval()" function
2152 */
2153 static void
2154f_eval(typval_T *argvars, typval_T *rettv)
2155{
2156 char_u *s, *p;
2157
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002158 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002159 if (s != NULL)
2160 s = skipwhite(s);
2161
2162 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002163 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002164 {
2165 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002166 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002167 need_clr_eos = FALSE;
2168 rettv->v_type = VAR_NUMBER;
2169 rettv->vval.v_number = 0;
2170 }
2171 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002172 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002173}
2174
2175/*
2176 * "eventhandler()" function
2177 */
2178 static void
2179f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2180{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002181 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002182}
2183
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002184static garray_T redir_execute_ga;
2185
2186/*
2187 * Append "value[value_len]" to the execute() output.
2188 */
2189 void
2190execute_redir_str(char_u *value, int value_len)
2191{
2192 int len;
2193
2194 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002195 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002196 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002197 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002198 if (ga_grow(&redir_execute_ga, len) == OK)
2199 {
2200 mch_memmove((char *)redir_execute_ga.ga_data
2201 + redir_execute_ga.ga_len, value, len);
2202 redir_execute_ga.ga_len += len;
2203 }
2204}
2205
2206/*
2207 * Get next line from a list.
2208 * Called by do_cmdline() to get the next line.
2209 * Returns allocated string, or NULL for end of function.
2210 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002211 static char_u *
2212get_list_line(
2213 int c UNUSED,
2214 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002215 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02002216 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002217{
2218 listitem_T **p = (listitem_T **)cookie;
2219 listitem_T *item = *p;
2220 char_u buf[NUMBUFLEN];
2221 char_u *s;
2222
2223 if (item == NULL)
2224 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002225 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002226 *p = item->li_next;
2227 return s == NULL ? NULL : vim_strsave(s);
2228}
2229
2230/*
2231 * "execute()" function
2232 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002233 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002234execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002235{
2236 char_u *cmd = NULL;
2237 list_T *list = NULL;
2238 int save_msg_silent = msg_silent;
2239 int save_emsg_silent = emsg_silent;
2240 int save_emsg_noredir = emsg_noredir;
2241 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002242 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002243 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002244 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002245 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002246
2247 rettv->vval.v_string = NULL;
2248 rettv->v_type = VAR_STRING;
2249
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002250 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002251 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002252 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002253 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002254 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002255 return;
2256 ++list->lv_refcount;
2257 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002258 else if (argvars[arg_off].v_type == VAR_JOB
2259 || argvars[arg_off].v_type == VAR_CHANNEL)
2260 {
2261 emsg(_(e_inval_string));
2262 return;
2263 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002264 else
2265 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002266 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002267 if (cmd == NULL)
2268 return;
2269 }
2270
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002271 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002272 {
2273 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002274 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002275
2276 if (s == NULL)
2277 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002278 if (*s == NUL)
2279 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002280 if (STRNCMP(s, "silent", 6) == 0)
2281 ++msg_silent;
2282 if (STRCMP(s, "silent!") == 0)
2283 {
2284 emsg_silent = TRUE;
2285 emsg_noredir = TRUE;
2286 }
2287 }
2288 else
2289 ++msg_silent;
2290
2291 if (redir_execute)
2292 save_ga = redir_execute_ga;
2293 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2294 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002295 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002296 if (!echo_output)
2297 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002298
2299 if (cmd != NULL)
2300 do_cmdline_cmd(cmd);
2301 else
2302 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002303 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002304
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002305 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002306 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002307 do_cmdline(NULL, get_list_line, (void *)&item,
2308 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2309 --list->lv_refcount;
2310 }
2311
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002312 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01002313 if (ga_grow(&redir_execute_ga, 1) == OK)
2314 {
2315 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2316 rettv->vval.v_string = redir_execute_ga.ga_data;
2317 }
2318 else
2319 {
2320 ga_clear(&redir_execute_ga);
2321 rettv->vval.v_string = NULL;
2322 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002323 msg_silent = save_msg_silent;
2324 emsg_silent = save_emsg_silent;
2325 emsg_noredir = save_emsg_noredir;
2326
2327 redir_execute = save_redir_execute;
2328 if (redir_execute)
2329 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002330 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002331
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002332 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002333 if (echo_output)
2334 // When not working silently: put it in column zero. A following
2335 // "echon" will overwrite the message, unavoidably.
2336 msg_col = 0;
2337 else
2338 // When working silently: Put it back where it was, since nothing
2339 // should have been written.
2340 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002341}
2342
2343/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002344 * "execute()" function
2345 */
2346 static void
2347f_execute(typval_T *argvars, typval_T *rettv)
2348{
2349 execute_common(argvars, rettv, 0);
2350}
2351
2352/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002353 * "exists()" function
2354 */
2355 static void
2356f_exists(typval_T *argvars, typval_T *rettv)
2357{
2358 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002359 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002360
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002361 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002362 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002363 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002364 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002365 if (mch_getenv(p + 1) != NULL)
2366 n = TRUE;
2367 else
2368 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002369 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002370 p = expand_env_save(p);
2371 if (p != NULL && *p != '$')
2372 n = TRUE;
2373 vim_free(p);
2374 }
2375 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002376 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002377 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002378 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002379 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002380 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002381 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002382 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002383 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002384 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002385 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01002386 else if (*p == '?') // internal function only
2387 {
2388 n = has_internal_func_name(p + 1);
2389 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002390 else if (*p == ':')
2391 {
2392 n = cmd_exists(p + 1);
2393 }
2394 else if (*p == '#')
2395 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002396 if (p[1] == '#')
2397 n = autocmd_supported(p + 2);
2398 else
2399 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002400 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002401 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002402 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01002403 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002404 }
2405
2406 rettv->vval.v_number = n;
2407}
2408
2409#ifdef FEAT_FLOAT
2410/*
2411 * "exp()" function
2412 */
2413 static void
2414f_exp(typval_T *argvars, typval_T *rettv)
2415{
2416 float_T f = 0.0;
2417
2418 rettv->v_type = VAR_FLOAT;
2419 if (get_float_arg(argvars, &f) == OK)
2420 rettv->vval.v_float = exp(f);
2421 else
2422 rettv->vval.v_float = 0.0;
2423}
2424#endif
2425
2426/*
2427 * "expand()" function
2428 */
2429 static void
2430f_expand(typval_T *argvars, typval_T *rettv)
2431{
2432 char_u *s;
2433 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002434 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002435 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2436 expand_T xpc;
2437 int error = FALSE;
2438 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02002439#ifdef BACKSLASH_IN_FILENAME
2440 char_u *p_csl_save = p_csl;
2441
2442 // avoid using 'completeslash' here
2443 p_csl = empty_option;
2444#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002445
2446 rettv->v_type = VAR_STRING;
2447 if (argvars[1].v_type != VAR_UNKNOWN
2448 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02002449 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002450 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02002451 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002452
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002453 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002454 if (*s == '%' || *s == '#' || *s == '<')
2455 {
2456 ++emsg_off;
2457 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
2458 --emsg_off;
2459 if (rettv->v_type == VAR_LIST)
2460 {
2461 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
2462 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02002463 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002464 }
2465 else
2466 rettv->vval.v_string = result;
2467 }
2468 else
2469 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002470 // When the optional second argument is non-zero, don't remove matches
2471 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002472 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02002473 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002474 options |= WILD_KEEP_ALL;
2475 if (!error)
2476 {
2477 ExpandInit(&xpc);
2478 xpc.xp_context = EXPAND_FILES;
2479 if (p_wic)
2480 options += WILD_ICASE;
2481 if (rettv->v_type == VAR_STRING)
2482 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
2483 options, WILD_ALL);
2484 else if (rettv_list_alloc(rettv) != FAIL)
2485 {
2486 int i;
2487
2488 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
2489 for (i = 0; i < xpc.xp_numfiles; i++)
2490 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2491 ExpandCleanup(&xpc);
2492 }
2493 }
2494 else
2495 rettv->vval.v_string = NULL;
2496 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02002497#ifdef BACKSLASH_IN_FILENAME
2498 p_csl = p_csl_save;
2499#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002500}
2501
2502/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02002503 * "expandcmd()" function
2504 * Expand all the special characters in a command string.
2505 */
2506 static void
2507f_expandcmd(typval_T *argvars, typval_T *rettv)
2508{
2509 exarg_T eap;
2510 char_u *cmdstr;
2511 char *errormsg = NULL;
2512
2513 rettv->v_type = VAR_STRING;
2514 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
2515
2516 memset(&eap, 0, sizeof(eap));
2517 eap.cmd = cmdstr;
2518 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002519 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02002520 eap.usefilter = FALSE;
2521 eap.nextcmd = NULL;
2522 eap.cmdidx = CMD_USER;
2523
2524 expand_filename(&eap, &cmdstr, &errormsg);
2525 if (errormsg != NULL && *errormsg != NUL)
2526 emsg(errormsg);
2527
2528 rettv->vval.v_string = cmdstr;
2529}
2530
2531/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002532 * "feedkeys()" function
2533 */
2534 static void
2535f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
2536{
2537 int remap = TRUE;
2538 int insert = FALSE;
2539 char_u *keys, *flags;
2540 char_u nbuf[NUMBUFLEN];
2541 int typed = FALSE;
2542 int execute = FALSE;
2543 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002544 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002545 char_u *keys_esc;
2546
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002547 // This is not allowed in the sandbox. If the commands would still be
2548 // executed in the sandbox it would be OK, but it probably happens later,
2549 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002550 if (check_secure())
2551 return;
2552
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002553 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002554
2555 if (argvars[1].v_type != VAR_UNKNOWN)
2556 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002557 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002558 for ( ; *flags != NUL; ++flags)
2559 {
2560 switch (*flags)
2561 {
2562 case 'n': remap = FALSE; break;
2563 case 'm': remap = TRUE; break;
2564 case 't': typed = TRUE; break;
2565 case 'i': insert = TRUE; break;
2566 case 'x': execute = TRUE; break;
2567 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002568 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002569 }
2570 }
2571 }
2572
2573 if (*keys != NUL || execute)
2574 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002575 // Need to escape K_SPECIAL and CSI before putting the string in the
2576 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002577 keys_esc = vim_strsave_escape_csi(keys);
2578 if (keys_esc != NULL)
2579 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002580 if (lowlevel)
2581 {
2582#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002583 int idx;
2584 int len = (int)STRLEN(keys);
2585
2586 for (idx = 0; idx < len; ++idx)
2587 {
2588 // if a CTRL-C was typed, set got_int, similar to what
2589 // happens in fill_input_buf()
2590 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
2591 got_int = TRUE;
2592 add_to_input_buf(keys + idx, 1);
2593 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002594#else
2595 emsg(_("E980: lowlevel input not supported"));
2596#endif
2597 }
2598 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002599 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002600 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002601 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002602 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002603#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002604 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002605#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002606 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002607 typebuf_was_filled = TRUE;
2608 }
2609 vim_free(keys_esc);
2610
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002611 if (execute)
2612 {
2613 int save_msg_scroll = msg_scroll;
2614
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002615 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002616 msg_scroll = FALSE;
2617
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002618 if (!dangerous)
2619 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02002620 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002621 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02002622 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002623 --ex_normal_busy;
Bram Moolenaar189832b2020-09-23 12:29:11 +02002624#ifdef FEAT_PROP_POPUP
2625 if (ex_normal_busy == 0)
2626 ex_normal_busy_done = FALSE;
2627#endif
2628 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002629
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002630 msg_scroll |= save_msg_scroll;
2631 }
2632 }
2633 }
2634}
2635
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002636#ifdef FEAT_FLOAT
2637/*
2638 * "float2nr({float})" function
2639 */
2640 static void
2641f_float2nr(typval_T *argvars, typval_T *rettv)
2642{
2643 float_T f = 0.0;
2644
2645 if (get_float_arg(argvars, &f) == OK)
2646 {
Bram Moolenaar37184272020-05-23 19:30:05 +02002647 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002648 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02002649 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002650 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002651 else
2652 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002653 }
2654}
2655
2656/*
2657 * "floor({float})" function
2658 */
2659 static void
2660f_floor(typval_T *argvars, typval_T *rettv)
2661{
2662 float_T f = 0.0;
2663
2664 rettv->v_type = VAR_FLOAT;
2665 if (get_float_arg(argvars, &f) == OK)
2666 rettv->vval.v_float = floor(f);
2667 else
2668 rettv->vval.v_float = 0.0;
2669}
2670
2671/*
2672 * "fmod()" function
2673 */
2674 static void
2675f_fmod(typval_T *argvars, typval_T *rettv)
2676{
2677 float_T fx = 0.0, fy = 0.0;
2678
2679 rettv->v_type = VAR_FLOAT;
2680 if (get_float_arg(argvars, &fx) == OK
2681 && get_float_arg(&argvars[1], &fy) == OK)
2682 rettv->vval.v_float = fmod(fx, fy);
2683 else
2684 rettv->vval.v_float = 0.0;
2685}
2686#endif
2687
2688/*
2689 * "fnameescape({string})" function
2690 */
2691 static void
2692f_fnameescape(typval_T *argvars, typval_T *rettv)
2693{
2694 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002695 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002696 rettv->v_type = VAR_STRING;
2697}
2698
2699/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002700 * "foreground()" function
2701 */
2702 static void
2703f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2704{
2705#ifdef FEAT_GUI
2706 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002707 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002708 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002709 return;
2710 }
2711#endif
2712#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002713 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002714#endif
2715}
2716
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002717 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002718common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002719{
2720 char_u *s;
2721 char_u *name;
2722 int use_string = FALSE;
2723 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002724 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002725 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002726
2727 if (argvars[0].v_type == VAR_FUNC)
2728 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002729 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002730 s = argvars[0].vval.v_string;
2731 }
2732 else if (argvars[0].v_type == VAR_PARTIAL
2733 && argvars[0].vval.v_partial != NULL)
2734 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002735 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002736 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002737 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002738 }
2739 else
2740 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002741 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002742 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002743 use_string = TRUE;
2744 }
2745
Bram Moolenaar843b8842016-08-21 14:36:15 +02002746 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002747 {
2748 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002749 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002750 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
2751 if (*name != NUL)
2752 s = NULL;
2753 }
2754
Bram Moolenaar843b8842016-08-21 14:36:15 +02002755 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
2756 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002757 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002758 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002759 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002760 ? find_func(trans_name, is_global, NULL) == NULL
2761 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002762 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002763 else
2764 {
2765 int dict_idx = 0;
2766 int arg_idx = 0;
2767 list_T *list = NULL;
2768
2769 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
2770 {
2771 char sid_buf[25];
2772 int off = *s == 's' ? 2 : 5;
2773
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002774 // Expand s: and <SID> into <SNR>nr_, so that the function can
2775 // also be called from another script. Using trans_function_name()
2776 // would also work, but some plugins depend on the name being
2777 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002778 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02002779 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002780 if (name != NULL)
2781 {
2782 STRCPY(name, sid_buf);
2783 STRCAT(name, s + off);
2784 }
2785 }
2786 else
2787 name = vim_strsave(s);
2788
2789 if (argvars[1].v_type != VAR_UNKNOWN)
2790 {
2791 if (argvars[2].v_type != VAR_UNKNOWN)
2792 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002793 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002794 arg_idx = 1;
2795 dict_idx = 2;
2796 }
2797 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002798 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002799 dict_idx = 1;
2800 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002801 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002802 arg_idx = 1;
2803 if (dict_idx > 0)
2804 {
2805 if (argvars[dict_idx].v_type != VAR_DICT)
2806 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002807 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002808 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002809 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002810 }
2811 if (argvars[dict_idx].vval.v_dict == NULL)
2812 dict_idx = 0;
2813 }
2814 if (arg_idx > 0)
2815 {
2816 if (argvars[arg_idx].v_type != VAR_LIST)
2817 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002818 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002819 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002820 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002821 }
2822 list = argvars[arg_idx].vval.v_list;
2823 if (list == NULL || list->lv_len == 0)
2824 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002825 else if (list->lv_len > MAX_FUNC_ARGS)
2826 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01002827 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002828 vim_free(name);
2829 goto theend;
2830 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002831 }
2832 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002833 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002834 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002835 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002836
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002837 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002838 if (pt == NULL)
2839 vim_free(name);
2840 else
2841 {
2842 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
2843 {
2844 listitem_T *li;
2845 int i = 0;
2846 int arg_len = 0;
2847 int lv_len = 0;
2848
2849 if (arg_pt != NULL)
2850 arg_len = arg_pt->pt_argc;
2851 if (list != NULL)
2852 lv_len = list->lv_len;
2853 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002854 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002855 if (pt->pt_argv == NULL)
2856 {
2857 vim_free(pt);
2858 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002859 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002860 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002861 for (i = 0; i < arg_len; i++)
2862 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
2863 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002864 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002865 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002866 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002867 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002868 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002869 }
2870
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002871 // For "function(dict.func, [], dict)" and "func" is a partial
2872 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002873 if (dict_idx > 0)
2874 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002875 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002876 pt->pt_dict = argvars[dict_idx].vval.v_dict;
2877 ++pt->pt_dict->dv_refcount;
2878 }
2879 else if (arg_pt != NULL)
2880 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002881 // If the dict was bound automatically the result is also
2882 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002883 pt->pt_dict = arg_pt->pt_dict;
2884 pt->pt_auto = arg_pt->pt_auto;
2885 if (pt->pt_dict != NULL)
2886 ++pt->pt_dict->dv_refcount;
2887 }
2888
2889 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002890 if (arg_pt != NULL && arg_pt->pt_func != NULL)
2891 {
2892 pt->pt_func = arg_pt->pt_func;
2893 func_ptr_ref(pt->pt_func);
2894 vim_free(name);
2895 }
2896 else if (is_funcref)
2897 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002898 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002899 func_ptr_ref(pt->pt_func);
2900 vim_free(name);
2901 }
2902 else
2903 {
2904 pt->pt_name = name;
2905 func_ref(name);
2906 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002907 }
2908 rettv->v_type = VAR_PARTIAL;
2909 rettv->vval.v_partial = pt;
2910 }
2911 else
2912 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002913 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002914 rettv->v_type = VAR_FUNC;
2915 rettv->vval.v_string = name;
2916 func_ref(name);
2917 }
2918 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002919theend:
2920 vim_free(trans_name);
2921}
2922
2923/*
2924 * "funcref()" function
2925 */
2926 static void
2927f_funcref(typval_T *argvars, typval_T *rettv)
2928{
2929 common_function(argvars, rettv, TRUE);
2930}
2931
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002932 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002933ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002934{
2935 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
2936 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02002937 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002938}
2939
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002940/*
2941 * "function()" function
2942 */
2943 static void
2944f_function(typval_T *argvars, typval_T *rettv)
2945{
2946 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002947}
2948
2949/*
2950 * "garbagecollect()" function
2951 */
2952 static void
2953f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
2954{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002955 // This is postponed until we are back at the toplevel, because we may be
2956 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002957 want_garbage_collect = TRUE;
2958
Bram Moolenaar2df47312020-09-05 17:30:44 +02002959 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002960 garbage_collect_at_exit = TRUE;
2961}
2962
2963/*
2964 * "get()" function
2965 */
2966 static void
2967f_get(typval_T *argvars, typval_T *rettv)
2968{
2969 listitem_T *li;
2970 list_T *l;
2971 dictitem_T *di;
2972 dict_T *d;
2973 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02002974 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002975
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002976 if (argvars[0].v_type == VAR_BLOB)
2977 {
2978 int error = FALSE;
2979 int idx = tv_get_number_chk(&argvars[1], &error);
2980
2981 if (!error)
2982 {
2983 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002984 if (idx < 0)
2985 idx = blob_len(argvars[0].vval.v_blob) + idx;
2986 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
2987 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002988 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002989 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002990 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002991 tv = rettv;
2992 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002993 }
2994 }
2995 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002996 {
2997 if ((l = argvars[0].vval.v_list) != NULL)
2998 {
2999 int error = FALSE;
3000
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003001 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003002 if (!error && li != NULL)
3003 tv = &li->li_tv;
3004 }
3005 }
3006 else if (argvars[0].v_type == VAR_DICT)
3007 {
3008 if ((d = argvars[0].vval.v_dict) != NULL)
3009 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003010 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003011 if (di != NULL)
3012 tv = &di->di_tv;
3013 }
3014 }
3015 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3016 {
3017 partial_T *pt;
3018 partial_T fref_pt;
3019
3020 if (argvars[0].v_type == VAR_PARTIAL)
3021 pt = argvars[0].vval.v_partial;
3022 else
3023 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003024 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003025 fref_pt.pt_name = argvars[0].vval.v_string;
3026 pt = &fref_pt;
3027 }
3028
3029 if (pt != NULL)
3030 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003031 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003032 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033
3034 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3035 {
3036 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003037 n = partial_name(pt);
3038 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003039 rettv->vval.v_string = NULL;
3040 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003041 {
3042 rettv->vval.v_string = vim_strsave(n);
3043 if (rettv->v_type == VAR_FUNC)
3044 func_ref(rettv->vval.v_string);
3045 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003046 }
3047 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003048 {
3049 what_is_dict = TRUE;
3050 if (pt->pt_dict != NULL)
3051 rettv_dict_set(rettv, pt->pt_dict);
3052 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003053 else if (STRCMP(what, "args") == 0)
3054 {
3055 rettv->v_type = VAR_LIST;
3056 if (rettv_list_alloc(rettv) == OK)
3057 {
3058 int i;
3059
3060 for (i = 0; i < pt->pt_argc; ++i)
3061 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3062 }
3063 }
3064 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003065 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003066
3067 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3068 // third argument
3069 if (!what_is_dict)
3070 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003071 }
3072 }
3073 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003074 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003075
3076 if (tv == NULL)
3077 {
3078 if (argvars[2].v_type != VAR_UNKNOWN)
3079 copy_tv(&argvars[2], rettv);
3080 }
3081 else
3082 copy_tv(tv, rettv);
3083}
3084
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003085/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003086 * "getchangelist()" function
3087 */
3088 static void
3089f_getchangelist(typval_T *argvars, typval_T *rettv)
3090{
3091#ifdef FEAT_JUMPLIST
3092 buf_T *buf;
3093 int i;
3094 list_T *l;
3095 dict_T *d;
3096#endif
3097
3098 if (rettv_list_alloc(rettv) != OK)
3099 return;
3100
3101#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003102 if (argvars[0].v_type == VAR_UNKNOWN)
3103 buf = curbuf;
3104 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003105 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003106 if (buf == NULL)
3107 return;
3108
3109 l = list_alloc();
3110 if (l == NULL)
3111 return;
3112
3113 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3114 return;
3115 /*
3116 * The current window change list index tracks only the position in the
3117 * current buffer change list. For other buffers, use the change list
3118 * length as the current index.
3119 */
3120 list_append_number(rettv->vval.v_list,
3121 (varnumber_T)((buf == curwin->w_buffer)
3122 ? curwin->w_changelistidx : buf->b_changelistlen));
3123
3124 for (i = 0; i < buf->b_changelistlen; ++i)
3125 {
3126 if (buf->b_changelist[i].lnum == 0)
3127 continue;
3128 if ((d = dict_alloc()) == NULL)
3129 return;
3130 if (list_append_dict(l, d) == FAIL)
3131 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003132 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3133 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003134 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003135 }
3136#endif
3137}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003138
3139/*
3140 * "getcharsearch()" function
3141 */
3142 static void
3143f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3144{
3145 if (rettv_dict_alloc(rettv) != FAIL)
3146 {
3147 dict_T *dict = rettv->vval.v_dict;
3148
Bram Moolenaare0be1672018-07-08 16:50:37 +02003149 dict_add_string(dict, "char", last_csearch());
3150 dict_add_number(dict, "forward", last_csearch_forward());
3151 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003152 }
3153}
3154
3155/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003156 * "getenv()" function
3157 */
3158 static void
3159f_getenv(typval_T *argvars, typval_T *rettv)
3160{
3161 int mustfree = FALSE;
3162 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3163
3164 if (p == NULL)
3165 {
3166 rettv->v_type = VAR_SPECIAL;
3167 rettv->vval.v_number = VVAL_NULL;
3168 return;
3169 }
3170 if (!mustfree)
3171 p = vim_strsave(p);
3172 rettv->vval.v_string = p;
3173 rettv->v_type = VAR_STRING;
3174}
3175
3176/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003177 * "getfontname()" function
3178 */
3179 static void
3180f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3181{
3182 rettv->v_type = VAR_STRING;
3183 rettv->vval.v_string = NULL;
3184#ifdef FEAT_GUI
3185 if (gui.in_use)
3186 {
3187 GuiFont font;
3188 char_u *name = NULL;
3189
3190 if (argvars[0].v_type == VAR_UNKNOWN)
3191 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003192 // Get the "Normal" font. Either the name saved by
3193 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003194 font = gui.norm_font;
3195 name = hl_get_font_name();
3196 }
3197 else
3198 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003199 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003200 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003201 return;
3202 font = gui_mch_get_font(name, FALSE);
3203 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003204 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003205 }
3206 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3207 if (argvars[0].v_type != VAR_UNKNOWN)
3208 gui_mch_free_font(font);
3209 }
3210#endif
3211}
3212
3213/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003214 * "getjumplist()" function
3215 */
3216 static void
3217f_getjumplist(typval_T *argvars, typval_T *rettv)
3218{
3219#ifdef FEAT_JUMPLIST
3220 win_T *wp;
3221 int i;
3222 list_T *l;
3223 dict_T *d;
3224#endif
3225
3226 if (rettv_list_alloc(rettv) != OK)
3227 return;
3228
3229#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003230 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003231 if (wp == NULL)
3232 return;
3233
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003234 cleanup_jumplist(wp, TRUE);
3235
Bram Moolenaar4f505882018-02-10 21:06:32 +01003236 l = list_alloc();
3237 if (l == NULL)
3238 return;
3239
3240 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3241 return;
3242 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3243
3244 for (i = 0; i < wp->w_jumplistlen; ++i)
3245 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003246 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3247 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003248 if ((d = dict_alloc()) == NULL)
3249 return;
3250 if (list_append_dict(l, d) == FAIL)
3251 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003252 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3253 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003254 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003255 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003256 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003257 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003258 }
3259#endif
3260}
3261
3262/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003263 * "getpid()" function
3264 */
3265 static void
3266f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
3267{
3268 rettv->vval.v_number = mch_get_pid();
3269}
3270
3271 static void
3272getpos_both(
3273 typval_T *argvars,
3274 typval_T *rettv,
3275 int getcurpos)
3276{
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02003277 pos_T *fp = NULL;
3278 win_T *wp = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003279 list_T *l;
3280 int fnum = -1;
3281
3282 if (rettv_list_alloc(rettv) == OK)
3283 {
3284 l = rettv->vval.v_list;
3285 if (getcurpos)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02003286 {
3287 if (argvars[0].v_type != VAR_UNKNOWN)
3288 {
3289 wp = find_win_by_nr_or_id(&argvars[0]);
3290 if (wp != NULL)
3291 fp = &wp->w_cursor;
3292 }
3293 else
3294 fp = &curwin->w_cursor;
3295 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003296 else
3297 fp = var2fpos(&argvars[0], TRUE, &fnum);
3298 if (fnum != -1)
3299 list_append_number(l, (varnumber_T)fnum);
3300 else
3301 list_append_number(l, (varnumber_T)0);
3302 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3303 : (varnumber_T)0);
3304 list_append_number(l, (fp != NULL)
3305 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3306 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003307 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003308 (varnumber_T)0);
3309 if (getcurpos)
3310 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01003311 int save_set_curswant = curwin->w_set_curswant;
3312 colnr_T save_curswant = curwin->w_curswant;
3313 colnr_T save_virtcol = curwin->w_virtcol;
3314
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02003315 if (wp == curwin)
3316 update_curswant();
3317 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
3318 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01003319
3320 // Do not change "curswant", as it is unexpected that a get
3321 // function has a side effect.
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02003322 if (wp == curwin && save_set_curswant)
Bram Moolenaar19a66852019-03-07 11:25:32 +01003323 {
3324 curwin->w_set_curswant = save_set_curswant;
3325 curwin->w_curswant = save_curswant;
3326 curwin->w_virtcol = save_virtcol;
3327 curwin->w_valid &= ~VALID_VIRTCOL;
3328 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003329 }
3330 }
3331 else
3332 rettv->vval.v_number = FALSE;
3333}
3334
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003335/*
3336 * "getcurpos()" function
3337 */
3338 static void
3339f_getcurpos(typval_T *argvars, typval_T *rettv)
3340{
3341 getpos_both(argvars, rettv, TRUE);
3342}
3343
3344/*
3345 * "getpos(string)" function
3346 */
3347 static void
3348f_getpos(typval_T *argvars, typval_T *rettv)
3349{
3350 getpos_both(argvars, rettv, FALSE);
3351}
3352
3353/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003354 * "getreg()" function
3355 */
3356 static void
3357f_getreg(typval_T *argvars, typval_T *rettv)
3358{
3359 char_u *strregname;
3360 int regname;
3361 int arg2 = FALSE;
3362 int return_list = FALSE;
3363 int error = FALSE;
3364
3365 if (argvars[0].v_type != VAR_UNKNOWN)
3366 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003367 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003368 error = strregname == NULL;
3369 if (argvars[1].v_type != VAR_UNKNOWN)
3370 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02003371 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003372 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02003373 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003374 }
3375 }
3376 else
3377 strregname = get_vim_var_str(VV_REG);
3378
3379 if (error)
3380 return;
3381
3382 regname = (strregname == NULL ? '"' : *strregname);
3383 if (regname == 0)
3384 regname = '"';
3385
3386 if (return_list)
3387 {
3388 rettv->v_type = VAR_LIST;
3389 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
3390 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
3391 if (rettv->vval.v_list == NULL)
3392 (void)rettv_list_alloc(rettv);
3393 else
3394 ++rettv->vval.v_list->lv_refcount;
3395 }
3396 else
3397 {
3398 rettv->v_type = VAR_STRING;
3399 rettv->vval.v_string = get_reg_contents(regname,
3400 arg2 ? GREG_EXPR_SRC : 0);
3401 }
3402}
3403
3404/*
3405 * "getregtype()" function
3406 */
3407 static void
3408f_getregtype(typval_T *argvars, typval_T *rettv)
3409{
3410 char_u *strregname;
3411 int regname;
3412 char_u buf[NUMBUFLEN + 2];
3413 long reglen = 0;
3414
3415 if (argvars[0].v_type != VAR_UNKNOWN)
3416 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003417 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003418 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003419 {
3420 rettv->v_type = VAR_STRING;
3421 rettv->vval.v_string = NULL;
3422 return;
3423 }
3424 }
3425 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003426 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003427 strregname = get_vim_var_str(VV_REG);
3428
3429 regname = (strregname == NULL ? '"' : *strregname);
3430 if (regname == 0)
3431 regname = '"';
3432
3433 buf[0] = NUL;
3434 buf[1] = NUL;
3435 switch (get_reg_type(regname, &reglen))
3436 {
3437 case MLINE: buf[0] = 'V'; break;
3438 case MCHAR: buf[0] = 'v'; break;
3439 case MBLOCK:
3440 buf[0] = Ctrl_V;
3441 sprintf((char *)buf + 1, "%ld", reglen + 1);
3442 break;
3443 }
3444 rettv->v_type = VAR_STRING;
3445 rettv->vval.v_string = vim_strsave(buf);
3446}
3447
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003448/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01003449 * "gettagstack()" function
3450 */
3451 static void
3452f_gettagstack(typval_T *argvars, typval_T *rettv)
3453{
3454 win_T *wp = curwin; // default is current window
3455
3456 if (rettv_dict_alloc(rettv) != OK)
3457 return;
3458
3459 if (argvars[0].v_type != VAR_UNKNOWN)
3460 {
3461 wp = find_win_by_nr_or_id(&argvars[0]);
3462 if (wp == NULL)
3463 return;
3464 }
3465
3466 get_tagstack(wp, rettv->vval.v_dict);
3467}
3468
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02003469/*
3470 * "gettext()" function
3471 */
3472 static void
3473f_gettext(typval_T *argvars, typval_T *rettv)
3474{
3475 if (argvars[0].v_type != VAR_STRING
3476 || argvars[0].vval.v_string == NULL
3477 || *argvars[0].vval.v_string == NUL)
3478 {
3479 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
3480 }
3481 else
3482 {
3483 rettv->v_type = VAR_STRING;
3484 rettv->vval.v_string = vim_strsave(
3485 (char_u *)_(argvars[0].vval.v_string));
3486 }
3487}
3488
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003489// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003490#include "version.h"
3491
3492/*
3493 * "has()" function
3494 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003495 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003496f_has(typval_T *argvars, typval_T *rettv)
3497{
3498 int i;
3499 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01003500 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003501 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01003502 typedef struct {
3503 char *name;
3504 short present;
3505 } has_item_T;
3506 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003507 {
Bram Moolenaar79296512020-03-22 16:17:14 +01003508 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003509#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01003510 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003511#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003512 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003513#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003514 },
3515 {"arp",
3516#if defined(AMIGA) && defined(FEAT_ARP)
3517 1
3518#else
3519 0
3520#endif
3521 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003522 {"haiku",
3523#ifdef __HAIKU__
3524 1
3525#else
3526 0
3527#endif
3528 },
3529 {"bsd",
3530#if defined(BSD) && !defined(MACOS_X)
3531 1
3532#else
3533 0
3534#endif
3535 },
3536 {"hpux",
3537#ifdef hpux
3538 1
3539#else
3540 0
3541#endif
3542 },
3543 {"linux",
3544#ifdef __linux__
3545 1
3546#else
3547 0
3548#endif
3549 },
3550 {"mac", // Mac OS X (and, once, Mac OS Classic)
3551#ifdef MACOS_X
3552 1
3553#else
3554 0
3555#endif
3556 },
3557 {"osx", // Mac OS X
3558#ifdef MACOS_X
3559 1
3560#else
3561 0
3562#endif
3563 },
3564 {"macunix", // Mac OS X, with the darwin feature
3565#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3566 1
3567#else
3568 0
3569#endif
3570 },
3571 {"osxdarwin", // synonym for macunix
3572#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3573 1
3574#else
3575 0
3576#endif
3577 },
3578 {"qnx",
3579#ifdef __QNX__
3580 1
3581#else
3582 0
3583#endif
3584 },
3585 {"sun",
3586#ifdef SUN_SYSTEM
3587 1
3588#else
3589 0
3590#endif
3591 },
3592 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003593#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01003594 1
3595#else
3596 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003597#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003598 },
3599 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003600#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01003601 1
3602#else
3603 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003604#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003605 },
3606 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01003607#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01003608 1
3609#else
3610 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003611#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003612 },
3613 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01003614#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01003615 1
3616#else
3617 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003618#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003619 },
3620 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01003621#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01003622 1
3623#else
3624 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003625#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003626 },
3627 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003628#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003629 1
3630#else
3631 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003632#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003633 },
3634 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003635#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003636 1
3637#else
3638 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003639#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003640 },
3641 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003642#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01003643 1
3644#else
3645 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003646#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003647 },
3648 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003649#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003650 1
3651#else
3652 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003653#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003654 },
3655 {"autocmd", 1},
3656 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003657#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01003658 1
3659#else
3660 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003661#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003662 },
3663 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003664#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003665 1
3666#else
3667 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003668#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003669 },
3670 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003671#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01003672 1
3673#else
3674 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003675#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003676 },
3677 {"balloon_multiline",
3678#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
3679 // MS-Windows requires runtime check, see below
3680 1
3681#else
3682 0
3683#endif
3684 },
3685 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003686#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01003687 1
3688#else
3689 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003690#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003691 },
3692 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003693#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01003694 1
3695#else
3696 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003697#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003698 },
3699 {"all_builtin_terms",
3700#if defined(ALL_BUILTIN_TCAPS)
3701 1
3702#else
3703 0
3704#endif
3705 },
3706 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003707#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01003708 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003709 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01003710 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003711#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003712 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003713#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003714 },
3715 {"byte_offset",
3716#ifdef FEAT_BYTEOFF
3717 1
3718#else
3719 0
3720#endif
3721 },
3722 {"channel",
3723#ifdef FEAT_JOB_CHANNEL
3724 1
3725#else
3726 0
3727#endif
3728 },
3729 {"cindent",
3730#ifdef FEAT_CINDENT
3731 1
3732#else
3733 0
3734#endif
3735 },
3736 {"clientserver",
3737#ifdef FEAT_CLIENTSERVER
3738 1
3739#else
3740 0
3741#endif
3742 },
3743 {"clipboard",
3744#ifdef FEAT_CLIPBOARD
3745 1
3746#else
3747 0
3748#endif
3749 },
3750 {"cmdline_compl", 1},
3751 {"cmdline_hist", 1},
3752 {"comments", 1},
3753 {"conceal",
3754#ifdef FEAT_CONCEAL
3755 1
3756#else
3757 0
3758#endif
3759 },
3760 {"cryptv",
3761#ifdef FEAT_CRYPT
3762 1
3763#else
3764 0
3765#endif
3766 },
3767 {"crypt-blowfish",
3768#ifdef FEAT_CRYPT
3769 1
3770#else
3771 0
3772#endif
3773 },
3774 {"crypt-blowfish2",
3775#ifdef FEAT_CRYPT
3776 1
3777#else
3778 0
3779#endif
3780 },
3781 {"cscope",
3782#ifdef FEAT_CSCOPE
3783 1
3784#else
3785 0
3786#endif
3787 },
3788 {"cursorbind", 1},
3789 {"cursorshape",
3790#ifdef CURSOR_SHAPE
3791 1
3792#else
3793 0
3794#endif
3795 },
3796 {"debug",
3797#ifdef DEBUG
3798 1
3799#else
3800 0
3801#endif
3802 },
3803 {"dialog_con",
3804#ifdef FEAT_CON_DIALOG
3805 1
3806#else
3807 0
3808#endif
3809 },
3810 {"dialog_gui",
3811#ifdef FEAT_GUI_DIALOG
3812 1
3813#else
3814 0
3815#endif
3816 },
3817 {"diff",
3818#ifdef FEAT_DIFF
3819 1
3820#else
3821 0
3822#endif
3823 },
3824 {"digraphs",
3825#ifdef FEAT_DIGRAPHS
3826 1
3827#else
3828 0
3829#endif
3830 },
3831 {"directx",
3832#ifdef FEAT_DIRECTX
3833 1
3834#else
3835 0
3836#endif
3837 },
3838 {"dnd",
3839#ifdef FEAT_DND
3840 1
3841#else
3842 0
3843#endif
3844 },
3845 {"emacs_tags",
3846#ifdef FEAT_EMACS_TAGS
3847 1
3848#else
3849 0
3850#endif
3851 },
3852 {"eval", 1}, // always present, of course!
3853 {"ex_extra", 1}, // graduated feature
3854 {"extra_search",
3855#ifdef FEAT_SEARCH_EXTRA
3856 1
3857#else
3858 0
3859#endif
3860 },
3861 {"file_in_path",
3862#ifdef FEAT_SEARCHPATH
3863 1
3864#else
3865 0
3866#endif
3867 },
3868 {"filterpipe",
3869#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
3870 1
3871#else
3872 0
3873#endif
3874 },
3875 {"find_in_path",
3876#ifdef FEAT_FIND_ID
3877 1
3878#else
3879 0
3880#endif
3881 },
3882 {"float",
3883#ifdef FEAT_FLOAT
3884 1
3885#else
3886 0
3887#endif
3888 },
3889 {"folding",
3890#ifdef FEAT_FOLDING
3891 1
3892#else
3893 0
3894#endif
3895 },
3896 {"footer",
3897#ifdef FEAT_FOOTER
3898 1
3899#else
3900 0
3901#endif
3902 },
3903 {"fork",
3904#if !defined(USE_SYSTEM) && defined(UNIX)
3905 1
3906#else
3907 0
3908#endif
3909 },
3910 {"gettext",
3911#ifdef FEAT_GETTEXT
3912 1
3913#else
3914 0
3915#endif
3916 },
3917 {"gui",
3918#ifdef FEAT_GUI
3919 1
3920#else
3921 0
3922#endif
3923 },
3924 {"gui_neXtaw",
3925#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
3926 1
3927#else
3928 0
3929#endif
3930 },
3931 {"gui_athena",
3932#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
3933 1
3934#else
3935 0
3936#endif
3937 },
3938 {"gui_gtk",
3939#ifdef FEAT_GUI_GTK
3940 1
3941#else
3942 0
3943#endif
3944 },
3945 {"gui_gtk2",
3946#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
3947 1
3948#else
3949 0
3950#endif
3951 },
3952 {"gui_gtk3",
3953#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
3954 1
3955#else
3956 0
3957#endif
3958 },
3959 {"gui_gnome",
3960#ifdef FEAT_GUI_GNOME
3961 1
3962#else
3963 0
3964#endif
3965 },
3966 {"gui_haiku",
3967#ifdef FEAT_GUI_HAIKU
3968 1
3969#else
3970 0
3971#endif
3972 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02003973 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01003974 {"gui_motif",
3975#ifdef FEAT_GUI_MOTIF
3976 1
3977#else
3978 0
3979#endif
3980 },
3981 {"gui_photon",
3982#ifdef FEAT_GUI_PHOTON
3983 1
3984#else
3985 0
3986#endif
3987 },
3988 {"gui_win32",
3989#ifdef FEAT_GUI_MSWIN
3990 1
3991#else
3992 0
3993#endif
3994 },
3995 {"iconv",
3996#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
3997 1
3998#else
3999 0
4000#endif
4001 },
4002 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004003 {"ipv6",
4004#ifdef FEAT_IPV6
4005 1
4006#else
4007 0
4008#endif
4009 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004010 {"job",
4011#ifdef FEAT_JOB_CHANNEL
4012 1
4013#else
4014 0
4015#endif
4016 },
4017 {"jumplist",
4018#ifdef FEAT_JUMPLIST
4019 1
4020#else
4021 0
4022#endif
4023 },
4024 {"keymap",
4025#ifdef FEAT_KEYMAP
4026 1
4027#else
4028 0
4029#endif
4030 },
4031 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4032 {"langmap",
4033#ifdef FEAT_LANGMAP
4034 1
4035#else
4036 0
4037#endif
4038 },
4039 {"libcall",
4040#ifdef FEAT_LIBCALL
4041 1
4042#else
4043 0
4044#endif
4045 },
4046 {"linebreak",
4047#ifdef FEAT_LINEBREAK
4048 1
4049#else
4050 0
4051#endif
4052 },
4053 {"lispindent",
4054#ifdef FEAT_LISP
4055 1
4056#else
4057 0
4058#endif
4059 },
4060 {"listcmds", 1},
4061 {"localmap", 1},
4062 {"lua",
4063#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4064 1
4065#else
4066 0
4067#endif
4068 },
4069 {"menu",
4070#ifdef FEAT_MENU
4071 1
4072#else
4073 0
4074#endif
4075 },
4076 {"mksession",
4077#ifdef FEAT_SESSION
4078 1
4079#else
4080 0
4081#endif
4082 },
4083 {"modify_fname", 1},
4084 {"mouse", 1},
4085 {"mouseshape",
4086#ifdef FEAT_MOUSESHAPE
4087 1
4088#else
4089 0
4090#endif
4091 },
4092 {"mouse_dec",
4093#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4094 1
4095#else
4096 0
4097#endif
4098 },
4099 {"mouse_gpm",
4100#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4101 1
4102#else
4103 0
4104#endif
4105 },
4106 {"mouse_jsbterm",
4107#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4108 1
4109#else
4110 0
4111#endif
4112 },
4113 {"mouse_netterm",
4114#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4115 1
4116#else
4117 0
4118#endif
4119 },
4120 {"mouse_pterm",
4121#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4122 1
4123#else
4124 0
4125#endif
4126 },
4127 {"mouse_sgr",
4128#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4129 1
4130#else
4131 0
4132#endif
4133 },
4134 {"mouse_sysmouse",
4135#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4136 1
4137#else
4138 0
4139#endif
4140 },
4141 {"mouse_urxvt",
4142#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4143 1
4144#else
4145 0
4146#endif
4147 },
4148 {"mouse_xterm",
4149#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4150 1
4151#else
4152 0
4153#endif
4154 },
4155 {"multi_byte", 1},
4156 {"multi_byte_ime",
4157#ifdef FEAT_MBYTE_IME
4158 1
4159#else
4160 0
4161#endif
4162 },
4163 {"multi_lang",
4164#ifdef FEAT_MULTI_LANG
4165 1
4166#else
4167 0
4168#endif
4169 },
4170 {"mzscheme",
4171#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4172 1
4173#else
4174 0
4175#endif
4176 },
4177 {"num64", 1},
4178 {"ole",
4179#ifdef FEAT_OLE
4180 1
4181#else
4182 0
4183#endif
4184 },
4185 {"packages",
4186#ifdef FEAT_EVAL
4187 1
4188#else
4189 0
4190#endif
4191 },
4192 {"path_extra",
4193#ifdef FEAT_PATH_EXTRA
4194 1
4195#else
4196 0
4197#endif
4198 },
4199 {"perl",
4200#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4201 1
4202#else
4203 0
4204#endif
4205 },
4206 {"persistent_undo",
4207#ifdef FEAT_PERSISTENT_UNDO
4208 1
4209#else
4210 0
4211#endif
4212 },
4213 {"python_compiled",
4214#if defined(FEAT_PYTHON)
4215 1
4216#else
4217 0
4218#endif
4219 },
4220 {"python_dynamic",
4221#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4222 1
4223#else
4224 0
4225#endif
4226 },
4227 {"python",
4228#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4229 1
4230#else
4231 0
4232#endif
4233 },
4234 {"pythonx",
4235#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4236 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4237 1
4238#else
4239 0
4240#endif
4241 },
4242 {"python3_compiled",
4243#if defined(FEAT_PYTHON3)
4244 1
4245#else
4246 0
4247#endif
4248 },
4249 {"python3_dynamic",
4250#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4251 1
4252#else
4253 0
4254#endif
4255 },
4256 {"python3",
4257#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4258 1
4259#else
4260 0
4261#endif
4262 },
4263 {"popupwin",
4264#ifdef FEAT_PROP_POPUP
4265 1
4266#else
4267 0
4268#endif
4269 },
4270 {"postscript",
4271#ifdef FEAT_POSTSCRIPT
4272 1
4273#else
4274 0
4275#endif
4276 },
4277 {"printer",
4278#ifdef FEAT_PRINTER
4279 1
4280#else
4281 0
4282#endif
4283 },
4284 {"profile",
4285#ifdef FEAT_PROFILE
4286 1
4287#else
4288 0
4289#endif
4290 },
4291 {"reltime",
4292#ifdef FEAT_RELTIME
4293 1
4294#else
4295 0
4296#endif
4297 },
4298 {"quickfix",
4299#ifdef FEAT_QUICKFIX
4300 1
4301#else
4302 0
4303#endif
4304 },
4305 {"rightleft",
4306#ifdef FEAT_RIGHTLEFT
4307 1
4308#else
4309 0
4310#endif
4311 },
4312 {"ruby",
4313#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
4314 1
4315#else
4316 0
4317#endif
4318 },
4319 {"scrollbind", 1},
4320 {"showcmd",
4321#ifdef FEAT_CMDL_INFO
4322 1
4323#else
4324 0
4325#endif
4326 },
4327 {"cmdline_info",
4328#ifdef FEAT_CMDL_INFO
4329 1
4330#else
4331 0
4332#endif
4333 },
4334 {"signs",
4335#ifdef FEAT_SIGNS
4336 1
4337#else
4338 0
4339#endif
4340 },
4341 {"smartindent",
4342#ifdef FEAT_SMARTINDENT
4343 1
4344#else
4345 0
4346#endif
4347 },
4348 {"startuptime",
4349#ifdef STARTUPTIME
4350 1
4351#else
4352 0
4353#endif
4354 },
4355 {"statusline",
4356#ifdef FEAT_STL_OPT
4357 1
4358#else
4359 0
4360#endif
4361 },
4362 {"netbeans_intg",
4363#ifdef FEAT_NETBEANS_INTG
4364 1
4365#else
4366 0
4367#endif
4368 },
4369 {"sound",
4370#ifdef FEAT_SOUND
4371 1
4372#else
4373 0
4374#endif
4375 },
4376 {"spell",
4377#ifdef FEAT_SPELL
4378 1
4379#else
4380 0
4381#endif
4382 },
4383 {"syntax",
4384#ifdef FEAT_SYN_HL
4385 1
4386#else
4387 0
4388#endif
4389 },
4390 {"system",
4391#if defined(USE_SYSTEM) || !defined(UNIX)
4392 1
4393#else
4394 0
4395#endif
4396 },
4397 {"tag_binary",
4398#ifdef FEAT_TAG_BINS
4399 1
4400#else
4401 0
4402#endif
4403 },
4404 {"tcl",
4405#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
4406 1
4407#else
4408 0
4409#endif
4410 },
4411 {"termguicolors",
4412#ifdef FEAT_TERMGUICOLORS
4413 1
4414#else
4415 0
4416#endif
4417 },
4418 {"terminal",
4419#if defined(FEAT_TERMINAL) && !defined(MSWIN)
4420 1
4421#else
4422 0
4423#endif
4424 },
4425 {"terminfo",
4426#ifdef TERMINFO
4427 1
4428#else
4429 0
4430#endif
4431 },
4432 {"termresponse",
4433#ifdef FEAT_TERMRESPONSE
4434 1
4435#else
4436 0
4437#endif
4438 },
4439 {"textobjects",
4440#ifdef FEAT_TEXTOBJ
4441 1
4442#else
4443 0
4444#endif
4445 },
4446 {"textprop",
4447#ifdef FEAT_PROP_POPUP
4448 1
4449#else
4450 0
4451#endif
4452 },
4453 {"tgetent",
4454#ifdef HAVE_TGETENT
4455 1
4456#else
4457 0
4458#endif
4459 },
4460 {"timers",
4461#ifdef FEAT_TIMERS
4462 1
4463#else
4464 0
4465#endif
4466 },
4467 {"title",
4468#ifdef FEAT_TITLE
4469 1
4470#else
4471 0
4472#endif
4473 },
4474 {"toolbar",
4475#ifdef FEAT_TOOLBAR
4476 1
4477#else
4478 0
4479#endif
4480 },
4481 {"unnamedplus",
4482#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4483 1
4484#else
4485 0
4486#endif
4487 },
4488 {"user-commands", 1}, // was accidentally included in 5.4
4489 {"user_commands", 1},
4490 {"vartabs",
4491#ifdef FEAT_VARTABS
4492 1
4493#else
4494 0
4495#endif
4496 },
4497 {"vertsplit", 1},
4498 {"viminfo",
4499#ifdef FEAT_VIMINFO
4500 1
4501#else
4502 0
4503#endif
4504 },
4505 {"vimscript-1", 1},
4506 {"vimscript-2", 1},
4507 {"vimscript-3", 1},
4508 {"vimscript-4", 1},
4509 {"virtualedit", 1},
4510 {"visual", 1},
4511 {"visualextra", 1},
4512 {"vreplace", 1},
4513 {"vtp",
4514#ifdef FEAT_VTP
4515 1
4516#else
4517 0
4518#endif
4519 },
4520 {"wildignore",
4521#ifdef FEAT_WILDIGN
4522 1
4523#else
4524 0
4525#endif
4526 },
4527 {"wildmenu",
4528#ifdef FEAT_WILDMENU
4529 1
4530#else
4531 0
4532#endif
4533 },
4534 {"windows", 1},
4535 {"winaltkeys",
4536#ifdef FEAT_WAK
4537 1
4538#else
4539 0
4540#endif
4541 },
4542 {"writebackup",
4543#ifdef FEAT_WRITEBACKUP
4544 1
4545#else
4546 0
4547#endif
4548 },
4549 {"xim",
4550#ifdef FEAT_XIM
4551 1
4552#else
4553 0
4554#endif
4555 },
4556 {"xfontset",
4557#ifdef FEAT_XFONTSET
4558 1
4559#else
4560 0
4561#endif
4562 },
4563 {"xpm",
4564#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
4565 1
4566#else
4567 0
4568#endif
4569 },
4570 {"xpm_w32", // for backward compatibility
4571#ifdef FEAT_XPM_W32
4572 1
4573#else
4574 0
4575#endif
4576 },
4577 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004578#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01004579 1
4580#else
4581 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004582#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004583 },
4584 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004585#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01004586 1
4587#else
4588 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004589#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004590 },
4591 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004592#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01004593 1
4594#else
4595 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004596#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004597 },
4598 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004599#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01004600 1
4601#else
4602 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004603#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004604 },
4605 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004606#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01004607 1
4608#else
4609 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004610#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004611 },
4612 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004613 };
4614
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004615 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01004616 for (i = 0; has_list[i].name != NULL; ++i)
4617 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004618 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004619 x = TRUE;
4620 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004621 break;
4622 }
4623
Bram Moolenaar79296512020-03-22 16:17:14 +01004624 // features also in has_list[] but sometimes enabled at runtime
4625 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004626 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004627 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02004628 {
4629 // intentionally empty
4630 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004631#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004632 else if (STRICMP(name, "balloon_multiline") == 0)
4633 n = multiline_balloon_available();
4634#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004635#ifdef VIMDLL
4636 else if (STRICMP(name, "filterpipe") == 0)
4637 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004638#endif
4639#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
4640 else if (STRICMP(name, "iconv") == 0)
4641 n = iconv_enabled(FALSE);
4642#endif
4643#ifdef DYNAMIC_LUA
4644 else if (STRICMP(name, "lua") == 0)
4645 n = lua_enabled(FALSE);
4646#endif
4647#ifdef DYNAMIC_MZSCHEME
4648 else if (STRICMP(name, "mzscheme") == 0)
4649 n = mzscheme_enabled(FALSE);
4650#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004651#ifdef DYNAMIC_PERL
4652 else if (STRICMP(name, "perl") == 0)
4653 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004654#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004655#ifdef DYNAMIC_PYTHON
4656 else if (STRICMP(name, "python") == 0)
4657 n = python_enabled(FALSE);
4658#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004659#ifdef DYNAMIC_PYTHON3
4660 else if (STRICMP(name, "python3") == 0)
4661 n = python3_enabled(FALSE);
4662#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004663#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
4664 else if (STRICMP(name, "pythonx") == 0)
4665 {
4666# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
4667 if (p_pyx == 0)
4668 n = python3_enabled(FALSE) || python_enabled(FALSE);
4669 else if (p_pyx == 3)
4670 n = python3_enabled(FALSE);
4671 else if (p_pyx == 2)
4672 n = python_enabled(FALSE);
4673# elif defined(DYNAMIC_PYTHON)
4674 n = python_enabled(FALSE);
4675# elif defined(DYNAMIC_PYTHON3)
4676 n = python3_enabled(FALSE);
4677# endif
4678 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004679#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004680#ifdef DYNAMIC_RUBY
4681 else if (STRICMP(name, "ruby") == 0)
4682 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004683#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004684#ifdef DYNAMIC_TCL
4685 else if (STRICMP(name, "tcl") == 0)
4686 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02004687#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01004688#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02004689 else if (STRICMP(name, "terminal") == 0)
4690 n = terminal_enabled();
4691#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004692 }
4693
Bram Moolenaar79296512020-03-22 16:17:14 +01004694 // features not in has_list[]
4695 if (x == FALSE)
4696 {
4697 if (STRNICMP(name, "patch", 5) == 0)
4698 {
4699 x = TRUE;
4700 if (name[5] == '-'
4701 && STRLEN(name) >= 11
4702 && vim_isdigit(name[6])
4703 && vim_isdigit(name[8])
4704 && vim_isdigit(name[10]))
4705 {
4706 int major = atoi((char *)name + 6);
4707 int minor = atoi((char *)name + 8);
4708
4709 // Expect "patch-9.9.01234".
4710 n = (major < VIM_VERSION_MAJOR
4711 || (major == VIM_VERSION_MAJOR
4712 && (minor < VIM_VERSION_MINOR
4713 || (minor == VIM_VERSION_MINOR
4714 && has_patch(atoi((char *)name + 10))))));
4715 }
4716 else
4717 n = has_patch(atoi((char *)name + 5));
4718 }
4719 else if (STRICMP(name, "vim_starting") == 0)
4720 {
4721 x = TRUE;
4722 n = (starting != 0);
4723 }
4724 else if (STRICMP(name, "ttyin") == 0)
4725 {
4726 x = TRUE;
4727 n = mch_input_isatty();
4728 }
4729 else if (STRICMP(name, "ttyout") == 0)
4730 {
4731 x = TRUE;
4732 n = stdout_isatty;
4733 }
4734 else if (STRICMP(name, "multi_byte_encoding") == 0)
4735 {
4736 x = TRUE;
4737 n = has_mbyte;
4738 }
4739 else if (STRICMP(name, "gui_running") == 0)
4740 {
4741 x = TRUE;
4742#ifdef FEAT_GUI
4743 n = (gui.in_use || gui.starting);
4744#endif
4745 }
4746 else if (STRICMP(name, "browse") == 0)
4747 {
4748 x = TRUE;
4749#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
4750 n = gui.in_use; // gui_mch_browse() works when GUI is running
4751#endif
4752 }
4753 else if (STRICMP(name, "syntax_items") == 0)
4754 {
4755 x = TRUE;
4756#ifdef FEAT_SYN_HL
4757 n = syntax_present(curwin);
4758#endif
4759 }
4760 else if (STRICMP(name, "vcon") == 0)
4761 {
4762 x = TRUE;
4763#ifdef FEAT_VTP
4764 n = is_term_win32() && has_vtp_working();
4765#endif
4766 }
4767 else if (STRICMP(name, "netbeans_enabled") == 0)
4768 {
4769 x = TRUE;
4770#ifdef FEAT_NETBEANS_INTG
4771 n = netbeans_active();
4772#endif
4773 }
4774 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
4775 {
4776 x = TRUE;
4777#ifdef FEAT_MOUSE_GPM
4778 n = gpm_enabled();
4779#endif
4780 }
4781 else if (STRICMP(name, "conpty") == 0)
4782 {
4783 x = TRUE;
4784#if defined(FEAT_TERMINAL) && defined(MSWIN)
4785 n = use_conpty();
4786#endif
4787 }
4788 else if (STRICMP(name, "clipboard_working") == 0)
4789 {
4790 x = TRUE;
4791#ifdef FEAT_CLIPBOARD
4792 n = clip_star.available;
4793#endif
4794 }
4795 }
4796
Bram Moolenaar04637e22020-09-05 18:45:29 +02004797 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01004798 // return whether feature could ever be enabled
4799 rettv->vval.v_number = x;
4800 else
4801 // return whether feature is enabled
4802 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004803}
4804
4805/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004806 * "haslocaldir()" function
4807 */
4808 static void
4809f_haslocaldir(typval_T *argvars, typval_T *rettv)
4810{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004811 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004812 win_T *wp = NULL;
4813
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004814 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
4815
4816 // Check for window-local and tab-local directories
4817 if (wp != NULL && wp->w_localdir != NULL)
4818 rettv->vval.v_number = 1;
4819 else if (tp != NULL && tp->tp_localdir != NULL)
4820 rettv->vval.v_number = 2;
4821 else
4822 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004823}
4824
4825/*
4826 * "hasmapto()" function
4827 */
4828 static void
4829f_hasmapto(typval_T *argvars, typval_T *rettv)
4830{
4831 char_u *name;
4832 char_u *mode;
4833 char_u buf[NUMBUFLEN];
4834 int abbr = FALSE;
4835
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004836 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004837 if (argvars[1].v_type == VAR_UNKNOWN)
4838 mode = (char_u *)"nvo";
4839 else
4840 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004841 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004842 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02004843 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004844 }
4845
4846 if (map_to_exists(name, mode, abbr))
4847 rettv->vval.v_number = TRUE;
4848 else
4849 rettv->vval.v_number = FALSE;
4850}
4851
4852/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004853 * "highlightID(name)" function
4854 */
4855 static void
4856f_hlID(typval_T *argvars, typval_T *rettv)
4857{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004858 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004859}
4860
4861/*
4862 * "highlight_exists()" function
4863 */
4864 static void
4865f_hlexists(typval_T *argvars, typval_T *rettv)
4866{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004867 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004868}
4869
4870/*
4871 * "hostname()" function
4872 */
4873 static void
4874f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
4875{
4876 char_u hostname[256];
4877
4878 mch_get_host_name(hostname, 256);
4879 rettv->v_type = VAR_STRING;
4880 rettv->vval.v_string = vim_strsave(hostname);
4881}
4882
4883/*
4884 * iconv() function
4885 */
4886 static void
4887f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
4888{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004889 char_u buf1[NUMBUFLEN];
4890 char_u buf2[NUMBUFLEN];
4891 char_u *from, *to, *str;
4892 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004893
4894 rettv->v_type = VAR_STRING;
4895 rettv->vval.v_string = NULL;
4896
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004897 str = tv_get_string(&argvars[0]);
4898 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
4899 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004900 vimconv.vc_type = CONV_NONE;
4901 convert_setup(&vimconv, from, to);
4902
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004903 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004904 if (vimconv.vc_type == CONV_NONE)
4905 rettv->vval.v_string = vim_strsave(str);
4906 else
4907 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
4908
4909 convert_setup(&vimconv, NULL, NULL);
4910 vim_free(from);
4911 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004912}
4913
4914/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004915 * "index()" function
4916 */
4917 static void
4918f_index(typval_T *argvars, typval_T *rettv)
4919{
4920 list_T *l;
4921 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004922 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004923 long idx = 0;
4924 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004925 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004926
4927 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004928 if (argvars[0].v_type == VAR_BLOB)
4929 {
4930 typval_T tv;
4931 int start = 0;
4932
4933 if (argvars[2].v_type != VAR_UNKNOWN)
4934 {
4935 start = tv_get_number_chk(&argvars[2], &error);
4936 if (error)
4937 return;
4938 }
4939 b = argvars[0].vval.v_blob;
4940 if (b == NULL)
4941 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01004942 if (start < 0)
4943 {
4944 start = blob_len(b) + start;
4945 if (start < 0)
4946 start = 0;
4947 }
4948
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004949 for (idx = start; idx < blob_len(b); ++idx)
4950 {
4951 tv.v_type = VAR_NUMBER;
4952 tv.vval.v_number = blob_get(b, idx);
4953 if (tv_equal(&tv, &argvars[1], ic, FALSE))
4954 {
4955 rettv->vval.v_number = idx;
4956 return;
4957 }
4958 }
4959 return;
4960 }
4961 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004962 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004963 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004964 return;
4965 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004966
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004967 l = argvars[0].vval.v_list;
4968 if (l != NULL)
4969 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02004970 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004971 item = l->lv_first;
4972 if (argvars[2].v_type != VAR_UNKNOWN)
4973 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004974 // Start at specified item. Use the cached index that list_find()
4975 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004976 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004977 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004978 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02004979 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004980 if (error)
4981 item = NULL;
4982 }
4983
4984 for ( ; item != NULL; item = item->li_next, ++idx)
4985 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
4986 {
4987 rettv->vval.v_number = idx;
4988 break;
4989 }
4990 }
4991}
4992
4993static int inputsecret_flag = 0;
4994
4995/*
4996 * "input()" function
4997 * Also handles inputsecret() when inputsecret is set.
4998 */
4999 static void
5000f_input(typval_T *argvars, typval_T *rettv)
5001{
5002 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5003}
5004
5005/*
5006 * "inputdialog()" function
5007 */
5008 static void
5009f_inputdialog(typval_T *argvars, typval_T *rettv)
5010{
5011#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005012 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005013 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5014 {
5015 char_u *message;
5016 char_u buf[NUMBUFLEN];
5017 char_u *defstr = (char_u *)"";
5018
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005019 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005020 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005021 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005022 vim_strncpy(IObuff, defstr, IOSIZE - 1);
5023 else
5024 IObuff[0] = NUL;
5025 if (message != NULL && defstr != NULL
5026 && do_dialog(VIM_QUESTION, NULL, message,
5027 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
5028 rettv->vval.v_string = vim_strsave(IObuff);
5029 else
5030 {
5031 if (message != NULL && defstr != NULL
5032 && argvars[1].v_type != VAR_UNKNOWN
5033 && argvars[2].v_type != VAR_UNKNOWN)
5034 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005035 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005036 else
5037 rettv->vval.v_string = NULL;
5038 }
5039 rettv->v_type = VAR_STRING;
5040 }
5041 else
5042#endif
5043 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
5044}
5045
5046/*
5047 * "inputlist()" function
5048 */
5049 static void
5050f_inputlist(typval_T *argvars, typval_T *rettv)
5051{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005052 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005053 listitem_T *li;
5054 int selected;
5055 int mouse_used;
5056
5057#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005058 // While starting up, there is no place to enter text. When running tests
5059 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005060 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005061 return;
5062#endif
5063 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5064 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005065 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005066 return;
5067 }
5068
5069 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005070 msg_row = Rows - 1; // for when 'cmdheight' > 1
5071 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005072 msg_scroll = TRUE;
5073 msg_clr_eos();
5074
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005075 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005076 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005077 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005078 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005079 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005080 msg_putchar('\n');
5081 }
5082
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005083 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005084 selected = prompt_for_number(&mouse_used);
5085 if (mouse_used)
5086 selected -= lines_left;
5087
5088 rettv->vval.v_number = selected;
5089}
5090
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005091static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5092
5093/*
5094 * "inputrestore()" function
5095 */
5096 static void
5097f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5098{
5099 if (ga_userinput.ga_len > 0)
5100 {
5101 --ga_userinput.ga_len;
5102 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5103 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005104 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005105 }
5106 else if (p_verbose > 1)
5107 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005108 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005109 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005110 }
5111}
5112
5113/*
5114 * "inputsave()" function
5115 */
5116 static void
5117f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5118{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005119 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005120 if (ga_grow(&ga_userinput, 1) == OK)
5121 {
5122 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5123 + ga_userinput.ga_len);
5124 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005125 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005126 }
5127 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005128 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005129}
5130
5131/*
5132 * "inputsecret()" function
5133 */
5134 static void
5135f_inputsecret(typval_T *argvars, typval_T *rettv)
5136{
5137 ++cmdline_star;
5138 ++inputsecret_flag;
5139 f_input(argvars, rettv);
5140 --cmdline_star;
5141 --inputsecret_flag;
5142}
5143
5144/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005145 * "interrupt()" function
5146 */
5147 static void
5148f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5149{
5150 got_int = TRUE;
5151}
5152
5153/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005154 * "invert(expr)" function
5155 */
5156 static void
5157f_invert(typval_T *argvars, typval_T *rettv)
5158{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005159 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005160}
5161
5162/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005163 * "islocked()" function
5164 */
5165 static void
5166f_islocked(typval_T *argvars, typval_T *rettv)
5167{
5168 lval_T lv;
5169 char_u *end;
5170 dictitem_T *di;
5171
5172 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005173 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005174 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005175 if (end != NULL && lv.ll_name != NULL)
5176 {
5177 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005178 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005179 else
5180 {
5181 if (lv.ll_tv == NULL)
5182 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005183 di = find_var(lv.ll_name, NULL, TRUE);
5184 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005185 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005186 // Consider a variable locked when:
5187 // 1. the variable itself is locked
5188 // 2. the value of the variable is locked.
5189 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005190 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5191 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005192 }
5193 }
5194 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005195 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005196 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005197 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005198 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005199 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005200 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5201 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005202 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005203 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5204 }
5205 }
5206
5207 clear_lval(&lv);
5208}
5209
5210#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
5211/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02005212 * "isinf()" function
5213 */
5214 static void
5215f_isinf(typval_T *argvars, typval_T *rettv)
5216{
5217 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
5218 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
5219}
5220
5221/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005222 * "isnan()" function
5223 */
5224 static void
5225f_isnan(typval_T *argvars, typval_T *rettv)
5226{
5227 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
5228 && isnan(argvars[0].vval.v_float);
5229}
5230#endif
5231
5232/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005233 * "last_buffer_nr()" function.
5234 */
5235 static void
5236f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5237{
5238 int n = 0;
5239 buf_T *buf;
5240
Bram Moolenaar29323592016-07-24 22:04:11 +02005241 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005242 if (n < buf->b_fnum)
5243 n = buf->b_fnum;
5244
5245 rettv->vval.v_number = n;
5246}
5247
5248/*
5249 * "len()" function
5250 */
5251 static void
5252f_len(typval_T *argvars, typval_T *rettv)
5253{
5254 switch (argvars[0].v_type)
5255 {
5256 case VAR_STRING:
5257 case VAR_NUMBER:
5258 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005259 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005260 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005261 case VAR_BLOB:
5262 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
5263 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005264 case VAR_LIST:
5265 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
5266 break;
5267 case VAR_DICT:
5268 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
5269 break;
5270 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005271 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005272 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005273 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005274 case VAR_SPECIAL:
5275 case VAR_FLOAT:
5276 case VAR_FUNC:
5277 case VAR_PARTIAL:
5278 case VAR_JOB:
5279 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005280 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005281 break;
5282 }
5283}
5284
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005285 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01005286libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005287{
5288#ifdef FEAT_LIBCALL
5289 char_u *string_in;
5290 char_u **string_result;
5291 int nr_result;
5292#endif
5293
5294 rettv->v_type = type;
5295 if (type != VAR_NUMBER)
5296 rettv->vval.v_string = NULL;
5297
5298 if (check_restricted() || check_secure())
5299 return;
5300
5301#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005302 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005303 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
5304 {
5305 string_in = NULL;
5306 if (argvars[2].v_type == VAR_STRING)
5307 string_in = argvars[2].vval.v_string;
5308 if (type == VAR_NUMBER)
5309 string_result = NULL;
5310 else
5311 string_result = &rettv->vval.v_string;
5312 if (mch_libcall(argvars[0].vval.v_string,
5313 argvars[1].vval.v_string,
5314 string_in,
5315 argvars[2].vval.v_number,
5316 string_result,
5317 &nr_result) == OK
5318 && type == VAR_NUMBER)
5319 rettv->vval.v_number = nr_result;
5320 }
5321#endif
5322}
5323
5324/*
5325 * "libcall()" function
5326 */
5327 static void
5328f_libcall(typval_T *argvars, typval_T *rettv)
5329{
5330 libcall_common(argvars, rettv, VAR_STRING);
5331}
5332
5333/*
5334 * "libcallnr()" function
5335 */
5336 static void
5337f_libcallnr(typval_T *argvars, typval_T *rettv)
5338{
5339 libcall_common(argvars, rettv, VAR_NUMBER);
5340}
5341
5342/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005343 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005344 */
5345 static void
5346f_line(typval_T *argvars, typval_T *rettv)
5347{
5348 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005349 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005350 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005351 int id;
5352 tabpage_T *tp;
5353 win_T *wp;
5354 win_T *save_curwin;
5355 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005356
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005357 if (argvars[1].v_type != VAR_UNKNOWN)
5358 {
5359 // use window specified in the second argument
5360 id = (int)tv_get_number(&argvars[1]);
5361 wp = win_id2wp_tp(id, &tp);
5362 if (wp != NULL && tp != NULL)
5363 {
5364 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
5365 == OK)
5366 {
5367 check_cursor();
5368 fp = var2fpos(&argvars[0], TRUE, &fnum);
5369 }
5370 restore_win_noblock(save_curwin, save_curtab, TRUE);
5371 }
5372 }
5373 else
5374 // use current window
5375 fp = var2fpos(&argvars[0], TRUE, &fnum);
5376
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005377 if (fp != NULL)
5378 lnum = fp->lnum;
5379 rettv->vval.v_number = lnum;
5380}
5381
5382/*
5383 * "line2byte(lnum)" function
5384 */
5385 static void
5386f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
5387{
5388#ifndef FEAT_BYTEOFF
5389 rettv->vval.v_number = -1;
5390#else
5391 linenr_T lnum;
5392
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005393 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005394 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
5395 rettv->vval.v_number = -1;
5396 else
5397 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
5398 if (rettv->vval.v_number >= 0)
5399 ++rettv->vval.v_number;
5400#endif
5401}
5402
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005403#ifdef FEAT_FLOAT
5404/*
5405 * "log()" function
5406 */
5407 static void
5408f_log(typval_T *argvars, typval_T *rettv)
5409{
5410 float_T f = 0.0;
5411
5412 rettv->v_type = VAR_FLOAT;
5413 if (get_float_arg(argvars, &f) == OK)
5414 rettv->vval.v_float = log(f);
5415 else
5416 rettv->vval.v_float = 0.0;
5417}
5418
5419/*
5420 * "log10()" function
5421 */
5422 static void
5423f_log10(typval_T *argvars, typval_T *rettv)
5424{
5425 float_T f = 0.0;
5426
5427 rettv->v_type = VAR_FLOAT;
5428 if (get_float_arg(argvars, &f) == OK)
5429 rettv->vval.v_float = log10(f);
5430 else
5431 rettv->vval.v_float = 0.0;
5432}
5433#endif
5434
5435#ifdef FEAT_LUA
5436/*
5437 * "luaeval()" function
5438 */
5439 static void
5440f_luaeval(typval_T *argvars, typval_T *rettv)
5441{
5442 char_u *str;
5443 char_u buf[NUMBUFLEN];
5444
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005445 if (check_restricted() || check_secure())
5446 return;
5447
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005448 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005449 do_luaeval(str, argvars + 1, rettv);
5450}
5451#endif
5452
5453/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005454 * "maparg()" function
5455 */
5456 static void
5457f_maparg(typval_T *argvars, typval_T *rettv)
5458{
5459 get_maparg(argvars, rettv, TRUE);
5460}
5461
5462/*
5463 * "mapcheck()" function
5464 */
5465 static void
5466f_mapcheck(typval_T *argvars, typval_T *rettv)
5467{
5468 get_maparg(argvars, rettv, FALSE);
5469}
5470
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005471typedef enum
5472{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005473 MATCH_END, // matchend()
5474 MATCH_MATCH, // match()
5475 MATCH_STR, // matchstr()
5476 MATCH_LIST, // matchlist()
5477 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005478} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005479
5480 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005481find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005482{
5483 char_u *str = NULL;
5484 long len = 0;
5485 char_u *expr = NULL;
5486 char_u *pat;
5487 regmatch_T regmatch;
5488 char_u patbuf[NUMBUFLEN];
5489 char_u strbuf[NUMBUFLEN];
5490 char_u *save_cpo;
5491 long start = 0;
5492 long nth = 1;
5493 colnr_T startcol = 0;
5494 int match = 0;
5495 list_T *l = NULL;
5496 listitem_T *li = NULL;
5497 long idx = 0;
5498 char_u *tofree = NULL;
5499
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005500 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005501 save_cpo = p_cpo;
5502 p_cpo = (char_u *)"";
5503
5504 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005505 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005506 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005507 // type MATCH_LIST: return empty list when there are no matches.
5508 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005509 if (rettv_list_alloc(rettv) == FAIL)
5510 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005511 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005512 && (list_append_string(rettv->vval.v_list,
5513 (char_u *)"", 0) == FAIL
5514 || list_append_number(rettv->vval.v_list,
5515 (varnumber_T)-1) == FAIL
5516 || list_append_number(rettv->vval.v_list,
5517 (varnumber_T)-1) == FAIL
5518 || list_append_number(rettv->vval.v_list,
5519 (varnumber_T)-1) == FAIL))
5520 {
5521 list_free(rettv->vval.v_list);
5522 rettv->vval.v_list = NULL;
5523 goto theend;
5524 }
5525 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005526 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005527 {
5528 rettv->v_type = VAR_STRING;
5529 rettv->vval.v_string = NULL;
5530 }
5531
5532 if (argvars[0].v_type == VAR_LIST)
5533 {
5534 if ((l = argvars[0].vval.v_list) == NULL)
5535 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005536 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005537 li = l->lv_first;
5538 }
5539 else
5540 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005541 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005542 len = (long)STRLEN(str);
5543 }
5544
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005545 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005546 if (pat == NULL)
5547 goto theend;
5548
5549 if (argvars[2].v_type != VAR_UNKNOWN)
5550 {
5551 int error = FALSE;
5552
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005553 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005554 if (error)
5555 goto theend;
5556 if (l != NULL)
5557 {
5558 li = list_find(l, start);
5559 if (li == NULL)
5560 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005561 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005562 }
5563 else
5564 {
5565 if (start < 0)
5566 start = 0;
5567 if (start > len)
5568 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005569 // When "count" argument is there ignore matches before "start",
5570 // otherwise skip part of the string. Differs when pattern is "^"
5571 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005572 if (argvars[3].v_type != VAR_UNKNOWN)
5573 startcol = start;
5574 else
5575 {
5576 str += start;
5577 len -= start;
5578 }
5579 }
5580
5581 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005582 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005583 if (error)
5584 goto theend;
5585 }
5586
5587 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5588 if (regmatch.regprog != NULL)
5589 {
5590 regmatch.rm_ic = p_ic;
5591
5592 for (;;)
5593 {
5594 if (l != NULL)
5595 {
5596 if (li == NULL)
5597 {
5598 match = FALSE;
5599 break;
5600 }
5601 vim_free(tofree);
5602 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
5603 if (str == NULL)
5604 break;
5605 }
5606
5607 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
5608
5609 if (match && --nth <= 0)
5610 break;
5611 if (l == NULL && !match)
5612 break;
5613
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005614 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005615 if (l != NULL)
5616 {
5617 li = li->li_next;
5618 ++idx;
5619 }
5620 else
5621 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005622 startcol = (colnr_T)(regmatch.startp[0]
5623 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005624 if (startcol > (colnr_T)len
5625 || str + startcol <= regmatch.startp[0])
5626 {
5627 match = FALSE;
5628 break;
5629 }
5630 }
5631 }
5632
5633 if (match)
5634 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005635 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005636 {
5637 listitem_T *li1 = rettv->vval.v_list->lv_first;
5638 listitem_T *li2 = li1->li_next;
5639 listitem_T *li3 = li2->li_next;
5640 listitem_T *li4 = li3->li_next;
5641
5642 vim_free(li1->li_tv.vval.v_string);
5643 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005644 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005645 li3->li_tv.vval.v_number =
5646 (varnumber_T)(regmatch.startp[0] - expr);
5647 li4->li_tv.vval.v_number =
5648 (varnumber_T)(regmatch.endp[0] - expr);
5649 if (l != NULL)
5650 li2->li_tv.vval.v_number = (varnumber_T)idx;
5651 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005652 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005653 {
5654 int i;
5655
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005656 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005657 for (i = 0; i < NSUBEXP; ++i)
5658 {
5659 if (regmatch.endp[i] == NULL)
5660 {
5661 if (list_append_string(rettv->vval.v_list,
5662 (char_u *)"", 0) == FAIL)
5663 break;
5664 }
5665 else if (list_append_string(rettv->vval.v_list,
5666 regmatch.startp[i],
5667 (int)(regmatch.endp[i] - regmatch.startp[i]))
5668 == FAIL)
5669 break;
5670 }
5671 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005672 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005673 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005674 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005675 if (l != NULL)
5676 copy_tv(&li->li_tv, rettv);
5677 else
5678 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005679 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005680 }
5681 else if (l != NULL)
5682 rettv->vval.v_number = idx;
5683 else
5684 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005685 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005686 rettv->vval.v_number =
5687 (varnumber_T)(regmatch.startp[0] - str);
5688 else
5689 rettv->vval.v_number =
5690 (varnumber_T)(regmatch.endp[0] - str);
5691 rettv->vval.v_number += (varnumber_T)(str - expr);
5692 }
5693 }
5694 vim_regfree(regmatch.regprog);
5695 }
5696
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005697theend:
5698 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005699 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005700 listitem_remove(rettv->vval.v_list,
5701 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005702 vim_free(tofree);
5703 p_cpo = save_cpo;
5704}
5705
5706/*
5707 * "match()" function
5708 */
5709 static void
5710f_match(typval_T *argvars, typval_T *rettv)
5711{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005712 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005713}
5714
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005715/*
5716 * "matchend()" function
5717 */
5718 static void
5719f_matchend(typval_T *argvars, typval_T *rettv)
5720{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005721 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005722}
5723
5724/*
5725 * "matchlist()" function
5726 */
5727 static void
5728f_matchlist(typval_T *argvars, typval_T *rettv)
5729{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005730 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005731}
5732
5733/*
5734 * "matchstr()" function
5735 */
5736 static void
5737f_matchstr(typval_T *argvars, typval_T *rettv)
5738{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005739 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005740}
5741
5742/*
5743 * "matchstrpos()" function
5744 */
5745 static void
5746f_matchstrpos(typval_T *argvars, typval_T *rettv)
5747{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005748 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005749}
5750
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005751 static void
5752max_min(typval_T *argvars, typval_T *rettv, int domax)
5753{
5754 varnumber_T n = 0;
5755 varnumber_T i;
5756 int error = FALSE;
5757
5758 if (argvars[0].v_type == VAR_LIST)
5759 {
5760 list_T *l;
5761 listitem_T *li;
5762
5763 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005764 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005765 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005766 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005767 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005768 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
5769 n = l->lv_u.nonmat.lv_start;
5770 else
5771 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
5772 * l->lv_u.nonmat.lv_stride;
5773 }
5774 else
5775 {
5776 li = l->lv_first;
5777 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005778 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005779 n = tv_get_number_chk(&li->li_tv, &error);
5780 for (;;)
5781 {
5782 li = li->li_next;
5783 if (li == NULL)
5784 break;
5785 i = tv_get_number_chk(&li->li_tv, &error);
5786 if (domax ? i > n : i < n)
5787 n = i;
5788 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005789 }
5790 }
5791 }
5792 }
5793 else if (argvars[0].v_type == VAR_DICT)
5794 {
5795 dict_T *d;
5796 int first = TRUE;
5797 hashitem_T *hi;
5798 int todo;
5799
5800 d = argvars[0].vval.v_dict;
5801 if (d != NULL)
5802 {
5803 todo = (int)d->dv_hashtab.ht_used;
5804 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
5805 {
5806 if (!HASHITEM_EMPTY(hi))
5807 {
5808 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005809 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005810 if (first)
5811 {
5812 n = i;
5813 first = FALSE;
5814 }
5815 else if (domax ? i > n : i < n)
5816 n = i;
5817 }
5818 }
5819 }
5820 }
5821 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005822 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005823 rettv->vval.v_number = error ? 0 : n;
5824}
5825
5826/*
5827 * "max()" function
5828 */
5829 static void
5830f_max(typval_T *argvars, typval_T *rettv)
5831{
5832 max_min(argvars, rettv, TRUE);
5833}
5834
5835/*
5836 * "min()" function
5837 */
5838 static void
5839f_min(typval_T *argvars, typval_T *rettv)
5840{
5841 max_min(argvars, rettv, FALSE);
5842}
5843
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005844#if defined(FEAT_MZSCHEME) || defined(PROTO)
5845/*
5846 * "mzeval()" function
5847 */
5848 static void
5849f_mzeval(typval_T *argvars, typval_T *rettv)
5850{
5851 char_u *str;
5852 char_u buf[NUMBUFLEN];
5853
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005854 if (check_restricted() || check_secure())
5855 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005856 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005857 do_mzeval(str, rettv);
5858}
5859
5860 void
5861mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
5862{
5863 typval_T argvars[3];
5864
5865 argvars[0].v_type = VAR_STRING;
5866 argvars[0].vval.v_string = name;
5867 copy_tv(args, &argvars[1]);
5868 argvars[2].v_type = VAR_UNKNOWN;
5869 f_call(argvars, rettv);
5870 clear_tv(&argvars[1]);
5871}
5872#endif
5873
5874/*
5875 * "nextnonblank()" function
5876 */
5877 static void
5878f_nextnonblank(typval_T *argvars, typval_T *rettv)
5879{
5880 linenr_T lnum;
5881
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005882 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005883 {
5884 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
5885 {
5886 lnum = 0;
5887 break;
5888 }
5889 if (*skipwhite(ml_get(lnum)) != NUL)
5890 break;
5891 }
5892 rettv->vval.v_number = lnum;
5893}
5894
5895/*
5896 * "nr2char()" function
5897 */
5898 static void
5899f_nr2char(typval_T *argvars, typval_T *rettv)
5900{
5901 char_u buf[NUMBUFLEN];
5902
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005903 if (has_mbyte)
5904 {
5905 int utf8 = 0;
5906
5907 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02005908 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005909 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01005910 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005911 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005912 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005913 }
5914 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005915 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005916 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005917 buf[1] = NUL;
5918 }
5919 rettv->v_type = VAR_STRING;
5920 rettv->vval.v_string = vim_strsave(buf);
5921}
5922
5923/*
5924 * "or(expr, expr)" function
5925 */
5926 static void
5927f_or(typval_T *argvars, typval_T *rettv)
5928{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005929 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
5930 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005931}
5932
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005933#ifdef FEAT_PERL
5934/*
5935 * "perleval()" function
5936 */
5937 static void
5938f_perleval(typval_T *argvars, typval_T *rettv)
5939{
5940 char_u *str;
5941 char_u buf[NUMBUFLEN];
5942
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005943 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005944 do_perleval(str, rettv);
5945}
5946#endif
5947
5948#ifdef FEAT_FLOAT
5949/*
5950 * "pow()" function
5951 */
5952 static void
5953f_pow(typval_T *argvars, typval_T *rettv)
5954{
5955 float_T fx = 0.0, fy = 0.0;
5956
5957 rettv->v_type = VAR_FLOAT;
5958 if (get_float_arg(argvars, &fx) == OK
5959 && get_float_arg(&argvars[1], &fy) == OK)
5960 rettv->vval.v_float = pow(fx, fy);
5961 else
5962 rettv->vval.v_float = 0.0;
5963}
5964#endif
5965
5966/*
5967 * "prevnonblank()" function
5968 */
5969 static void
5970f_prevnonblank(typval_T *argvars, typval_T *rettv)
5971{
5972 linenr_T lnum;
5973
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005974 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005975 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5976 lnum = 0;
5977 else
5978 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
5979 --lnum;
5980 rettv->vval.v_number = lnum;
5981}
5982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005983// This dummy va_list is here because:
5984// - passing a NULL pointer doesn't work when va_list isn't a pointer
5985// - locally in the function results in a "used before set" warning
5986// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005987static va_list ap;
5988
5989/*
5990 * "printf()" function
5991 */
5992 static void
5993f_printf(typval_T *argvars, typval_T *rettv)
5994{
5995 char_u buf[NUMBUFLEN];
5996 int len;
5997 char_u *s;
5998 int saved_did_emsg = did_emsg;
5999 char *fmt;
6000
6001 rettv->v_type = VAR_STRING;
6002 rettv->vval.v_string = NULL;
6003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006004 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006005 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006006 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006007 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006008 if (!did_emsg)
6009 {
6010 s = alloc(len + 1);
6011 if (s != NULL)
6012 {
6013 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006014 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
6015 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006016 }
6017 }
6018 did_emsg |= saved_did_emsg;
6019}
6020
6021/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006022 * "pum_getpos()" function
6023 */
6024 static void
6025f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6026{
6027 if (rettv_dict_alloc(rettv) != OK)
6028 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006029 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006030}
6031
6032/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006033 * "pumvisible()" function
6034 */
6035 static void
6036f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6037{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006038 if (pum_visible())
6039 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006040}
6041
6042#ifdef FEAT_PYTHON3
6043/*
6044 * "py3eval()" function
6045 */
6046 static void
6047f_py3eval(typval_T *argvars, typval_T *rettv)
6048{
6049 char_u *str;
6050 char_u buf[NUMBUFLEN];
6051
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006052 if (check_restricted() || check_secure())
6053 return;
6054
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006055 if (p_pyx == 0)
6056 p_pyx = 3;
6057
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006058 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006059 do_py3eval(str, rettv);
6060}
6061#endif
6062
6063#ifdef FEAT_PYTHON
6064/*
6065 * "pyeval()" function
6066 */
6067 static void
6068f_pyeval(typval_T *argvars, typval_T *rettv)
6069{
6070 char_u *str;
6071 char_u buf[NUMBUFLEN];
6072
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006073 if (check_restricted() || check_secure())
6074 return;
6075
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006076 if (p_pyx == 0)
6077 p_pyx = 2;
6078
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006079 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006080 do_pyeval(str, rettv);
6081}
6082#endif
6083
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006084#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6085/*
6086 * "pyxeval()" function
6087 */
6088 static void
6089f_pyxeval(typval_T *argvars, typval_T *rettv)
6090{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006091 if (check_restricted() || check_secure())
6092 return;
6093
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006094# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6095 init_pyxversion();
6096 if (p_pyx == 2)
6097 f_pyeval(argvars, rettv);
6098 else
6099 f_py3eval(argvars, rettv);
6100# elif defined(FEAT_PYTHON)
6101 f_pyeval(argvars, rettv);
6102# elif defined(FEAT_PYTHON3)
6103 f_py3eval(argvars, rettv);
6104# endif
6105}
6106#endif
6107
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006108static UINT32_T srand_seed_for_testing = 0;
6109static int srand_seed_for_testing_is_used = FALSE;
6110
6111 static void
6112f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6113{
6114 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006115 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006116 else
6117 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006118 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6119 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006120 }
6121}
6122
6123 static void
6124init_srand(UINT32_T *x)
6125{
6126#ifndef MSWIN
6127 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6128#endif
6129
6130 if (srand_seed_for_testing_is_used)
6131 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006132 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006133 return;
6134 }
6135#ifndef MSWIN
6136 if (dev_urandom_state != FAIL)
6137 {
6138 int fd = open("/dev/urandom", O_RDONLY);
6139 struct {
6140 union {
6141 UINT32_T number;
6142 char bytes[sizeof(UINT32_T)];
6143 } contents;
6144 } buf;
6145
6146 // Attempt reading /dev/urandom.
6147 if (fd == -1)
6148 dev_urandom_state = FAIL;
6149 else
6150 {
6151 buf.contents.number = 0;
6152 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6153 != sizeof(UINT32_T))
6154 dev_urandom_state = FAIL;
6155 else
6156 {
6157 dev_urandom_state = OK;
6158 *x = buf.contents.number;
6159 }
6160 close(fd);
6161 }
6162 }
6163 if (dev_urandom_state != OK)
6164 // Reading /dev/urandom doesn't work, fall back to time().
6165#endif
6166 *x = vim_time();
6167}
6168
6169#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6170#define SPLITMIX32(x, z) ( \
6171 z = (x += 0x9e3779b9), \
6172 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6173 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6174 z ^ (z >> 16) \
6175 )
6176#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6177 result = ROTL(y * 5, 7) * 9; \
6178 t = y << 9; \
6179 z ^= x; \
6180 w ^= y; \
6181 y ^= z, x ^= w; \
6182 z ^= t; \
6183 w = ROTL(w, 11);
6184
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006185/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006186 * "rand()" function
6187 */
6188 static void
6189f_rand(typval_T *argvars, typval_T *rettv)
6190{
6191 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006192 static UINT32_T gx, gy, gz, gw;
6193 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006194 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006195 UINT32_T x, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006196
6197 if (argvars[0].v_type == VAR_UNKNOWN)
6198 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006199 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006200 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006201 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006202 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006203 init_srand(&x);
6204
6205 gx = SPLITMIX32(x, z);
6206 gy = SPLITMIX32(x, z);
6207 gz = SPLITMIX32(x, z);
6208 gw = SPLITMIX32(x, z);
6209 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006210 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006211
6212 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006213 }
6214 else if (argvars[0].v_type == VAR_LIST)
6215 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006216 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006217 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006218 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006219
6220 lx = list_find(l, 0L);
6221 ly = list_find(l, 1L);
6222 lz = list_find(l, 2L);
6223 lw = list_find(l, 3L);
6224 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6225 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6226 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6227 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6228 x = (UINT32_T)lx->li_tv.vval.v_number;
6229 y = (UINT32_T)ly->li_tv.vval.v_number;
6230 z = (UINT32_T)lz->li_tv.vval.v_number;
6231 w = (UINT32_T)lw->li_tv.vval.v_number;
6232
6233 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6234
6235 lx->li_tv.vval.v_number = (varnumber_T)x;
6236 ly->li_tv.vval.v_number = (varnumber_T)y;
6237 lz->li_tv.vval.v_number = (varnumber_T)z;
6238 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006239 }
6240 else
6241 goto theend;
6242
6243 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006244 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006245 return;
6246
6247theend:
6248 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006249 rettv->v_type = VAR_NUMBER;
6250 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006251}
6252
6253/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006254 * "srand()" function
6255 */
6256 static void
6257f_srand(typval_T *argvars, typval_T *rettv)
6258{
6259 UINT32_T x = 0, z;
6260
6261 if (rettv_list_alloc(rettv) == FAIL)
6262 return;
6263 if (argvars[0].v_type == VAR_UNKNOWN)
6264 {
6265 init_srand(&x);
6266 }
6267 else
6268 {
6269 int error = FALSE;
6270
6271 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6272 if (error)
6273 return;
6274 }
6275
6276 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6277 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6278 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6279 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6280}
6281
6282#undef ROTL
6283#undef SPLITMIX32
6284#undef SHUFFLE_XOSHIRO128STARSTAR
6285
6286/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006287 * "range()" function
6288 */
6289 static void
6290f_range(typval_T *argvars, typval_T *rettv)
6291{
6292 varnumber_T start;
6293 varnumber_T end;
6294 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006295 int error = FALSE;
6296
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006297 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006298 if (argvars[1].v_type == VAR_UNKNOWN)
6299 {
6300 end = start - 1;
6301 start = 0;
6302 }
6303 else
6304 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006305 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006306 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006307 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006308 }
6309
6310 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006311 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006312 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006313 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006314 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006315 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006317 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318 list_T *list = rettv->vval.v_list;
6319
6320 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006321 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 // be called.
6323 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006324 list->lv_u.nonmat.lv_start = start;
6325 list->lv_u.nonmat.lv_end = end;
6326 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006327 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006328 }
6329}
6330
6331/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006332 * Materialize "list".
6333 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006334 */
6335 void
6336range_list_materialize(list_T *list)
6337{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006338 varnumber_T start = list->lv_u.nonmat.lv_start;
6339 varnumber_T end = list->lv_u.nonmat.lv_end;
6340 int stride = list->lv_u.nonmat.lv_stride;
6341 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006342
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006343 list->lv_first = NULL;
6344 list->lv_u.mat.lv_last = NULL;
6345 list->lv_len = 0;
6346 list->lv_u.mat.lv_idx_item = NULL;
6347 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
6348 if (list_append_number(list, (varnumber_T)i) == FAIL)
6349 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006350}
6351
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006352/*
6353 * "getreginfo()" function
6354 */
6355 static void
6356f_getreginfo(typval_T *argvars, typval_T *rettv)
6357{
6358 char_u *strregname;
6359 int regname;
6360 char_u buf[NUMBUFLEN + 2];
6361 long reglen = 0;
6362 dict_T *dict;
6363 list_T *list;
6364
6365 if (argvars[0].v_type != VAR_UNKNOWN)
6366 {
6367 strregname = tv_get_string_chk(&argvars[0]);
6368 if (strregname == NULL)
6369 return;
6370 }
6371 else
6372 strregname = get_vim_var_str(VV_REG);
6373
6374 regname = (strregname == NULL ? '"' : *strregname);
6375 if (regname == 0 || regname == '@')
6376 regname = '"';
6377
6378 if (rettv_dict_alloc(rettv) == FAIL)
6379 return;
6380 dict = rettv->vval.v_dict;
6381
6382 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
6383 if (list == NULL)
6384 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02006385 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006386
6387 buf[0] = NUL;
6388 buf[1] = NUL;
6389 switch (get_reg_type(regname, &reglen))
6390 {
6391 case MLINE: buf[0] = 'V'; break;
6392 case MCHAR: buf[0] = 'v'; break;
6393 case MBLOCK:
6394 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
6395 reglen + 1);
6396 break;
6397 }
Bram Moolenaar91639192020-06-29 19:55:58 +02006398 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006399
6400 buf[0] = get_register_name(get_unname_register());
6401 buf[1] = NUL;
6402 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02006403 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006404 else
6405 {
6406 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
6407
6408 if (item != NULL)
6409 {
6410 item->di_tv.v_type = VAR_SPECIAL;
6411 item->di_tv.vval.v_number = regname == buf[0]
6412 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02006413 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006414 }
6415 }
6416}
6417
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02006418 static void
6419return_register(int regname, typval_T *rettv)
6420{
6421 char_u buf[2] = {0, 0};
6422
6423 buf[0] = (char_u)regname;
6424 rettv->v_type = VAR_STRING;
6425 rettv->vval.v_string = vim_strsave(buf);
6426}
6427
6428/*
6429 * "reg_executing()" function
6430 */
6431 static void
6432f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
6433{
6434 return_register(reg_executing, rettv);
6435}
6436
6437/*
6438 * "reg_recording()" function
6439 */
6440 static void
6441f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
6442{
6443 return_register(reg_recording, rettv);
6444}
6445
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01006446/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006447 * "rename({from}, {to})" function
6448 */
6449 static void
6450f_rename(typval_T *argvars, typval_T *rettv)
6451{
6452 char_u buf[NUMBUFLEN];
6453
6454 if (check_restricted() || check_secure())
6455 rettv->vval.v_number = -1;
6456 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006457 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
6458 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006459}
6460
6461/*
6462 * "repeat()" function
6463 */
6464 static void
6465f_repeat(typval_T *argvars, typval_T *rettv)
6466{
6467 char_u *p;
6468 int n;
6469 int slen;
6470 int len;
6471 char_u *r;
6472 int i;
6473
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006474 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006475 if (argvars[0].v_type == VAR_LIST)
6476 {
6477 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
6478 while (n-- > 0)
6479 if (list_extend(rettv->vval.v_list,
6480 argvars[0].vval.v_list, NULL) == FAIL)
6481 break;
6482 }
6483 else
6484 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006485 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006486 rettv->v_type = VAR_STRING;
6487 rettv->vval.v_string = NULL;
6488
6489 slen = (int)STRLEN(p);
6490 len = slen * n;
6491 if (len <= 0)
6492 return;
6493
6494 r = alloc(len + 1);
6495 if (r != NULL)
6496 {
6497 for (i = 0; i < n; i++)
6498 mch_memmove(r + i * slen, p, (size_t)slen);
6499 r[len] = NUL;
6500 }
6501
6502 rettv->vval.v_string = r;
6503 }
6504}
6505
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006506#define SP_NOMOVE 0x01 // don't move cursor
6507#define SP_REPEAT 0x02 // repeat to find outer pair
6508#define SP_RETCOUNT 0x04 // return matchcount
6509#define SP_SETPCMARK 0x08 // set previous context mark
6510#define SP_START 0x10 // accept match at start position
6511#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
6512#define SP_END 0x40 // leave cursor at end of match
6513#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006514
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006515/*
6516 * Get flags for a search function.
6517 * Possibly sets "p_ws".
6518 * Returns BACKWARD, FORWARD or zero (for an error).
6519 */
6520 static int
6521get_search_arg(typval_T *varp, int *flagsp)
6522{
6523 int dir = FORWARD;
6524 char_u *flags;
6525 char_u nbuf[NUMBUFLEN];
6526 int mask;
6527
6528 if (varp->v_type != VAR_UNKNOWN)
6529 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006530 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006531 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006532 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006533 while (*flags != NUL)
6534 {
6535 switch (*flags)
6536 {
6537 case 'b': dir = BACKWARD; break;
6538 case 'w': p_ws = TRUE; break;
6539 case 'W': p_ws = FALSE; break;
6540 default: mask = 0;
6541 if (flagsp != NULL)
6542 switch (*flags)
6543 {
6544 case 'c': mask = SP_START; break;
6545 case 'e': mask = SP_END; break;
6546 case 'm': mask = SP_RETCOUNT; break;
6547 case 'n': mask = SP_NOMOVE; break;
6548 case 'p': mask = SP_SUBPAT; break;
6549 case 'r': mask = SP_REPEAT; break;
6550 case 's': mask = SP_SETPCMARK; break;
6551 case 'z': mask = SP_COLUMN; break;
6552 }
6553 if (mask == 0)
6554 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006555 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006556 dir = 0;
6557 }
6558 else
6559 *flagsp |= mask;
6560 }
6561 if (dir == 0)
6562 break;
6563 ++flags;
6564 }
6565 }
6566 return dir;
6567}
6568
6569/*
6570 * Shared by search() and searchpos() functions.
6571 */
6572 static int
6573search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
6574{
6575 int flags;
6576 char_u *pat;
6577 pos_T pos;
6578 pos_T save_cursor;
6579 int save_p_ws = p_ws;
6580 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006581 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006582 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006583#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006584 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006585 long time_limit = 0;
6586#endif
6587 int options = SEARCH_KEEP;
6588 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006589 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006590 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006591 pos_T firstpos;
6592
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006593 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006594 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006595 if (dir == 0)
6596 goto theend;
6597 flags = *flagsp;
6598 if (flags & SP_START)
6599 options |= SEARCH_START;
6600 if (flags & SP_END)
6601 options |= SEARCH_END;
6602 if (flags & SP_COLUMN)
6603 options |= SEARCH_COL;
6604
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006605 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006606 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
6607 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006608 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006609 if (lnum_stop < 0)
6610 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006611 if (argvars[3].v_type != VAR_UNKNOWN)
6612 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006613#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006614 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006615 if (time_limit < 0)
6616 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006617#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02006618 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006619 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006620 }
6621
6622#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006623 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006624 profile_setlimit(time_limit, &tm);
6625#endif
6626
6627 /*
6628 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
6629 * Check to make sure only those flags are set.
6630 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
6631 * flags cannot be set. Check for that condition also.
6632 */
6633 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
6634 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6635 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006636 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006637 goto theend;
6638 }
6639
6640 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006641 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02006642 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006643 sia.sa_stop_lnum = (linenr_T)lnum_stop;
6644#ifdef FEAT_RELTIME
6645 sia.sa_tm = &tm;
6646#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006647
6648 // Repeat until {skip} returns FALSE.
6649 for (;;)
6650 {
6651 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006652 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006653 // finding the first match again means there is no match where {skip}
6654 // evaluates to zero.
6655 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
6656 subpatnum = FAIL;
6657
Bram Moolenaara9c01042020-06-07 14:50:50 +02006658 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006659 // didn't find it or no skip argument
6660 break;
6661 firstpos = pos;
6662
Bram Moolenaara9c01042020-06-07 14:50:50 +02006663 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006664 {
6665 int do_skip;
6666 int err;
6667 pos_T save_pos = curwin->w_cursor;
6668
6669 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006670 err = FALSE;
6671 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006672 curwin->w_cursor = save_pos;
6673 if (err)
6674 {
6675 // Evaluating {skip} caused an error, break here.
6676 subpatnum = FAIL;
6677 break;
6678 }
6679 if (!do_skip)
6680 break;
6681 }
6682 }
6683
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006684 if (subpatnum != FAIL)
6685 {
6686 if (flags & SP_SUBPAT)
6687 retval = subpatnum;
6688 else
6689 retval = pos.lnum;
6690 if (flags & SP_SETPCMARK)
6691 setpcmark();
6692 curwin->w_cursor = pos;
6693 if (match_pos != NULL)
6694 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006695 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006696 match_pos->lnum = pos.lnum;
6697 match_pos->col = pos.col + 1;
6698 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006699 // "/$" will put the cursor after the end of the line, may need to
6700 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006701 check_cursor();
6702 }
6703
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006704 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006705 if (flags & SP_NOMOVE)
6706 curwin->w_cursor = save_cursor;
6707 else
6708 curwin->w_set_curswant = TRUE;
6709theend:
6710 p_ws = save_p_ws;
6711
6712 return retval;
6713}
6714
6715#ifdef FEAT_FLOAT
6716
6717/*
6718 * round() is not in C90, use ceil() or floor() instead.
6719 */
6720 float_T
6721vim_round(float_T f)
6722{
6723 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
6724}
6725
6726/*
6727 * "round({float})" function
6728 */
6729 static void
6730f_round(typval_T *argvars, typval_T *rettv)
6731{
6732 float_T f = 0.0;
6733
6734 rettv->v_type = VAR_FLOAT;
6735 if (get_float_arg(argvars, &f) == OK)
6736 rettv->vval.v_float = vim_round(f);
6737 else
6738 rettv->vval.v_float = 0.0;
6739}
6740#endif
6741
Bram Moolenaare99be0e2019-03-26 22:51:09 +01006742#ifdef FEAT_RUBY
6743/*
6744 * "rubyeval()" function
6745 */
6746 static void
6747f_rubyeval(typval_T *argvars, typval_T *rettv)
6748{
6749 char_u *str;
6750 char_u buf[NUMBUFLEN];
6751
6752 str = tv_get_string_buf(&argvars[0], buf);
6753 do_rubyeval(str, rettv);
6754}
6755#endif
6756
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006757/*
6758 * "screenattr()" function
6759 */
6760 static void
6761f_screenattr(typval_T *argvars, typval_T *rettv)
6762{
6763 int row;
6764 int col;
6765 int c;
6766
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006767 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6768 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006769 if (row < 0 || row >= screen_Rows
6770 || col < 0 || col >= screen_Columns)
6771 c = -1;
6772 else
6773 c = ScreenAttrs[LineOffset[row] + col];
6774 rettv->vval.v_number = c;
6775}
6776
6777/*
6778 * "screenchar()" function
6779 */
6780 static void
6781f_screenchar(typval_T *argvars, typval_T *rettv)
6782{
6783 int row;
6784 int col;
6785 int off;
6786 int c;
6787
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006788 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6789 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006790 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006791 c = -1;
6792 else
6793 {
6794 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006795 if (enc_utf8 && ScreenLinesUC[off] != 0)
6796 c = ScreenLinesUC[off];
6797 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006798 c = ScreenLines[off];
6799 }
6800 rettv->vval.v_number = c;
6801}
6802
6803/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006804 * "screenchars()" function
6805 */
6806 static void
6807f_screenchars(typval_T *argvars, typval_T *rettv)
6808{
6809 int row;
6810 int col;
6811 int off;
6812 int c;
6813 int i;
6814
6815 if (rettv_list_alloc(rettv) == FAIL)
6816 return;
6817 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6818 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6819 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6820 return;
6821
6822 off = LineOffset[row] + col;
6823 if (enc_utf8 && ScreenLinesUC[off] != 0)
6824 c = ScreenLinesUC[off];
6825 else
6826 c = ScreenLines[off];
6827 list_append_number(rettv->vval.v_list, (varnumber_T)c);
6828
6829 if (enc_utf8)
6830
6831 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6832 list_append_number(rettv->vval.v_list,
6833 (varnumber_T)ScreenLinesC[i][off]);
6834}
6835
6836/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006837 * "screencol()" function
6838 *
6839 * First column is 1 to be consistent with virtcol().
6840 */
6841 static void
6842f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
6843{
6844 rettv->vval.v_number = screen_screencol() + 1;
6845}
6846
6847/*
6848 * "screenrow()" function
6849 */
6850 static void
6851f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
6852{
6853 rettv->vval.v_number = screen_screenrow() + 1;
6854}
6855
6856/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006857 * "screenstring()" function
6858 */
6859 static void
6860f_screenstring(typval_T *argvars, typval_T *rettv)
6861{
6862 int row;
6863 int col;
6864 int off;
6865 int c;
6866 int i;
6867 char_u buf[MB_MAXBYTES + 1];
6868 int buflen = 0;
6869
6870 rettv->vval.v_string = NULL;
6871 rettv->v_type = VAR_STRING;
6872
6873 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6874 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6875 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6876 return;
6877
6878 off = LineOffset[row] + col;
6879 if (enc_utf8 && ScreenLinesUC[off] != 0)
6880 c = ScreenLinesUC[off];
6881 else
6882 c = ScreenLines[off];
6883 buflen += mb_char2bytes(c, buf);
6884
6885 if (enc_utf8)
6886 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6887 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
6888
6889 buf[buflen] = NUL;
6890 rettv->vval.v_string = vim_strsave(buf);
6891}
6892
6893/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006894 * "search()" function
6895 */
6896 static void
6897f_search(typval_T *argvars, typval_T *rettv)
6898{
6899 int flags = 0;
6900
6901 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
6902}
6903
6904/*
6905 * "searchdecl()" function
6906 */
6907 static void
6908f_searchdecl(typval_T *argvars, typval_T *rettv)
6909{
Bram Moolenaar30788d32020-09-05 21:35:16 +02006910 int locally = TRUE;
6911 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006912 int error = FALSE;
6913 char_u *name;
6914
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006915 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006916
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006917 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006918 if (argvars[1].v_type != VAR_UNKNOWN)
6919 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02006920 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006921 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02006922 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923 }
6924 if (!error && name != NULL)
6925 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
6926 locally, thisblock, SEARCH_KEEP) == FAIL;
6927}
6928
6929/*
6930 * Used by searchpair() and searchpairpos()
6931 */
6932 static int
6933searchpair_cmn(typval_T *argvars, pos_T *match_pos)
6934{
6935 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01006936 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006937 int save_p_ws = p_ws;
6938 int dir;
6939 int flags = 0;
6940 char_u nbuf1[NUMBUFLEN];
6941 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006942 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006943 long lnum_stop = 0;
6944 long time_limit = 0;
6945
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006946 // Get the three pattern arguments: start, middle, end. Will result in an
6947 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006948 spat = tv_get_string_chk(&argvars[0]);
6949 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
6950 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006951 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006952 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006953
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006954 // Handle the optional fourth argument: flags
6955 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006956 if (dir == 0)
6957 goto theend;
6958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006959 // Don't accept SP_END or SP_SUBPAT.
6960 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006961 if ((flags & (SP_END | SP_SUBPAT)) != 0
6962 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6963 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006964 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006965 goto theend;
6966 }
6967
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006968 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006969 if (flags & SP_REPEAT)
6970 p_ws = FALSE;
6971
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006972 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006973 if (argvars[3].v_type == VAR_UNKNOWN
6974 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01006975 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006976 else
6977 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02006978 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01006979 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02006980
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006981 if (argvars[5].v_type != VAR_UNKNOWN)
6982 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006983 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006984 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006985 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006986 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006987 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006988 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006989#ifdef FEAT_RELTIME
6990 if (argvars[6].v_type != VAR_UNKNOWN)
6991 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006992 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006993 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006994 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006995 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006996 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006997 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006998 }
6999#endif
7000 }
7001 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007002
7003 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
7004 match_pos, lnum_stop, time_limit);
7005
7006theend:
7007 p_ws = save_p_ws;
7008
7009 return retval;
7010}
7011
7012/*
7013 * "searchpair()" function
7014 */
7015 static void
7016f_searchpair(typval_T *argvars, typval_T *rettv)
7017{
7018 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
7019}
7020
7021/*
7022 * "searchpairpos()" function
7023 */
7024 static void
7025f_searchpairpos(typval_T *argvars, typval_T *rettv)
7026{
7027 pos_T match_pos;
7028 int lnum = 0;
7029 int col = 0;
7030
7031 if (rettv_list_alloc(rettv) == FAIL)
7032 return;
7033
7034 if (searchpair_cmn(argvars, &match_pos) > 0)
7035 {
7036 lnum = match_pos.lnum;
7037 col = match_pos.col;
7038 }
7039
7040 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7041 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7042}
7043
7044/*
7045 * Search for a start/middle/end thing.
7046 * Used by searchpair(), see its documentation for the details.
7047 * Returns 0 or -1 for no match,
7048 */
7049 long
7050do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007051 char_u *spat, // start pattern
7052 char_u *mpat, // middle pattern
7053 char_u *epat, // end pattern
7054 int dir, // BACKWARD or FORWARD
7055 typval_T *skip, // skip expression
7056 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007057 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007058 linenr_T lnum_stop, // stop at this line if not zero
7059 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007060{
7061 char_u *save_cpo;
7062 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7063 long retval = 0;
7064 pos_T pos;
7065 pos_T firstpos;
7066 pos_T foundpos;
7067 pos_T save_cursor;
7068 pos_T save_pos;
7069 int n;
7070 int r;
7071 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007072 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007073 int err;
7074 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007075#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007076 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007077#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007078
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007079 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007080 save_cpo = p_cpo;
7081 p_cpo = empty_option;
7082
7083#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007084 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007085 profile_setlimit(time_limit, &tm);
7086#endif
7087
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007088 // Make two search patterns: start/end (pat2, for in nested pairs) and
7089 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007090 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7091 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007092 if (pat2 == NULL || pat3 == NULL)
7093 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007094 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007095 if (*mpat == NUL)
7096 STRCPY(pat3, pat2);
7097 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007098 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007099 spat, epat, mpat);
7100 if (flags & SP_START)
7101 options |= SEARCH_START;
7102
Bram Moolenaar48570482017-10-30 21:48:41 +01007103 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007104 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007105
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007106 save_cursor = curwin->w_cursor;
7107 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007108 CLEAR_POS(&firstpos);
7109 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007110 pat = pat3;
7111 for (;;)
7112 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007113 searchit_arg_T sia;
7114
Bram Moolenaara80faa82020-04-12 19:37:17 +02007115 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007116 sia.sa_stop_lnum = lnum_stop;
7117#ifdef FEAT_RELTIME
7118 sia.sa_tm = &tm;
7119#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007120 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007121 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007122 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007123 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007124 break;
7125
7126 if (firstpos.lnum == 0)
7127 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007128 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007129 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007130 // Found the same position again. Can happen with a pattern that
7131 // has "\zs" at the end and searching backwards. Advance one
7132 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007133 if (dir == BACKWARD)
7134 decl(&pos);
7135 else
7136 incl(&pos);
7137 }
7138 foundpos = pos;
7139
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007140 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007141 options &= ~SEARCH_START;
7142
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007143 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007144 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007145 {
7146 save_pos = curwin->w_cursor;
7147 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007148 err = FALSE;
7149 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007150 curwin->w_cursor = save_pos;
7151 if (err)
7152 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007153 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007154 curwin->w_cursor = save_cursor;
7155 retval = -1;
7156 break;
7157 }
7158 if (r)
7159 continue;
7160 }
7161
7162 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7163 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007164 // Found end when searching backwards or start when searching
7165 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007166 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007167 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007168 }
7169 else
7170 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007171 // Found end when searching forward or start when searching
7172 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007173 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007174 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007175 }
7176
7177 if (nest == 0)
7178 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007179 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007180 if (flags & SP_RETCOUNT)
7181 ++retval;
7182 else
7183 retval = pos.lnum;
7184 if (flags & SP_SETPCMARK)
7185 setpcmark();
7186 curwin->w_cursor = pos;
7187 if (!(flags & SP_REPEAT))
7188 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007189 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007190 }
7191 }
7192
7193 if (match_pos != NULL)
7194 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007195 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007196 match_pos->lnum = curwin->w_cursor.lnum;
7197 match_pos->col = curwin->w_cursor.col + 1;
7198 }
7199
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007200 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007201 if ((flags & SP_NOMOVE) || retval == 0)
7202 curwin->w_cursor = save_cursor;
7203
7204theend:
7205 vim_free(pat2);
7206 vim_free(pat3);
7207 if (p_cpo == empty_option)
7208 p_cpo = save_cpo;
7209 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007210 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007211 free_string_option(save_cpo);
7212
7213 return retval;
7214}
7215
7216/*
7217 * "searchpos()" function
7218 */
7219 static void
7220f_searchpos(typval_T *argvars, typval_T *rettv)
7221{
7222 pos_T match_pos;
7223 int lnum = 0;
7224 int col = 0;
7225 int n;
7226 int flags = 0;
7227
7228 if (rettv_list_alloc(rettv) == FAIL)
7229 return;
7230
7231 n = search_cmn(argvars, &match_pos, &flags);
7232 if (n > 0)
7233 {
7234 lnum = match_pos.lnum;
7235 col = match_pos.col;
7236 }
7237
7238 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7239 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7240 if (flags & SP_SUBPAT)
7241 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7242}
7243
7244 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7246{
7247 dict_T *d;
7248 dictitem_T *di;
7249 char_u *csearch;
7250
7251 if (argvars[0].v_type != VAR_DICT)
7252 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007253 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007254 return;
7255 }
7256
7257 if ((d = argvars[0].vval.v_dict) != NULL)
7258 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01007259 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007260 if (csearch != NULL)
7261 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007262 if (enc_utf8)
7263 {
7264 int pcc[MAX_MCO];
7265 int c = utfc_ptr2char(csearch, pcc);
7266
7267 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
7268 }
7269 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007270 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02007271 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007272 }
7273
7274 di = dict_find(d, (char_u *)"forward", -1);
7275 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007276 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007277 ? FORWARD : BACKWARD);
7278
7279 di = dict_find(d, (char_u *)"until", -1);
7280 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007281 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007282 }
7283}
7284
7285/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02007286 * "setenv()" function
7287 */
7288 static void
7289f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
7290{
7291 char_u namebuf[NUMBUFLEN];
7292 char_u valbuf[NUMBUFLEN];
7293 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
7294
7295 if (argvars[1].v_type == VAR_SPECIAL
7296 && argvars[1].vval.v_number == VVAL_NULL)
7297 vim_unsetenv(name);
7298 else
7299 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
7300}
7301
7302/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007303 * "setfperm({fname}, {mode})" function
7304 */
7305 static void
7306f_setfperm(typval_T *argvars, typval_T *rettv)
7307{
7308 char_u *fname;
7309 char_u modebuf[NUMBUFLEN];
7310 char_u *mode_str;
7311 int i;
7312 int mask;
7313 int mode = 0;
7314
7315 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007316 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007317 if (fname == NULL)
7318 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007319 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007320 if (mode_str == NULL)
7321 return;
7322 if (STRLEN(mode_str) != 9)
7323 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007324 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007325 return;
7326 }
7327
7328 mask = 1;
7329 for (i = 8; i >= 0; --i)
7330 {
7331 if (mode_str[i] != '-')
7332 mode |= mask;
7333 mask = mask << 1;
7334 }
7335 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
7336}
7337
7338/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007339 * "setpos()" function
7340 */
7341 static void
7342f_setpos(typval_T *argvars, typval_T *rettv)
7343{
7344 pos_T pos;
7345 int fnum;
7346 char_u *name;
7347 colnr_T curswant = -1;
7348
7349 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007350 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007351 if (name != NULL)
7352 {
7353 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
7354 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01007355 if (pos.col != MAXCOL && --pos.col < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007356 pos.col = 0;
7357 if (name[0] == '.' && name[1] == NUL)
7358 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007359 // set cursor; "fnum" is ignored
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007360 curwin->w_cursor = pos;
7361 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007362 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007363 curwin->w_curswant = curswant - 1;
7364 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007365 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007366 check_cursor();
7367 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007368 }
7369 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7370 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007371 // set mark
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007372 if (setmark_pos(name[1], &pos, fnum) == OK)
7373 rettv->vval.v_number = 0;
7374 }
7375 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007376 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007377 }
7378 }
7379}
7380
7381/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007382 * Translate a register type string to the yank type and block length
7383 */
7384 static int
7385get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
7386{
7387 char_u *stropt = *pp;
7388 switch (*stropt)
7389 {
7390 case 'v': case 'c': // character-wise selection
7391 *yank_type = MCHAR;
7392 break;
7393 case 'V': case 'l': // line-wise selection
7394 *yank_type = MLINE;
7395 break;
7396 case 'b': case Ctrl_V: // block-wise selection
7397 *yank_type = MBLOCK;
7398 if (VIM_ISDIGIT(stropt[1]))
7399 {
7400 ++stropt;
7401 *block_len = getdigits(&stropt) - 1;
7402 --stropt;
7403 }
7404 break;
7405 default:
7406 return FAIL;
7407 }
7408 *pp = stropt;
7409 return OK;
7410}
7411
7412/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007413 * "setreg()" function
7414 */
7415 static void
7416f_setreg(typval_T *argvars, typval_T *rettv)
7417{
7418 int regname;
7419 char_u *strregname;
7420 char_u *stropt;
7421 char_u *strval;
7422 int append;
7423 char_u yank_type;
7424 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007425 typval_T *regcontents;
7426 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007427
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007428 pointreg = 0;
7429 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007430 block_len = -1;
7431 yank_type = MAUTO;
7432 append = FALSE;
7433
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007434 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007435 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007436
7437 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007438 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007439 regname = *strregname;
7440 if (regname == 0 || regname == '@')
7441 regname = '"';
7442
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007443 if (argvars[1].v_type == VAR_DICT)
7444 {
7445 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007446 dictitem_T *di;
7447
7448 if (d == NULL || d->dv_hashtab.ht_used == 0)
7449 {
7450 // Empty dict, clear the register (like setreg(0, []))
7451 char_u *lstval[2] = {NULL, NULL};
7452 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
7453 return;
7454 }
7455
7456 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007457 if (di != NULL)
7458 regcontents = &di->di_tv;
7459
7460 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
7461 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007462 {
7463 int ret = get_yank_type(&stropt, &yank_type, &block_len);
7464
7465 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007466 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007467 semsg(_(e_invargval), "value");
7468 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007469 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007470 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007471
7472 if (regname == '"')
7473 {
7474 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
7475 if (stropt != NULL)
7476 {
7477 pointreg = *stropt;
7478 regname = pointreg;
7479 }
7480 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02007481 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007482 pointreg = regname;
7483 }
7484 else
7485 regcontents = &argvars[1];
7486
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007487 if (argvars[2].v_type != VAR_UNKNOWN)
7488 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007489 if (yank_type != MAUTO)
7490 {
7491 semsg(_(e_toomanyarg), "setreg");
7492 return;
7493 }
7494
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007495 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007496 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007497 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007498 for (; *stropt != NUL; ++stropt)
7499 switch (*stropt)
7500 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007501 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007502 append = TRUE;
7503 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007504 default:
7505 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007506 }
7507 }
7508
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007509 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007510 {
7511 char_u **lstval;
7512 char_u **allocval;
7513 char_u buf[NUMBUFLEN];
7514 char_u **curval;
7515 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007516 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007517 listitem_T *li;
7518 int len;
7519
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007520 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007521 len = ll == NULL ? 0 : ll->lv_len;
7522
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007523 // First half: use for pointers to result lines; second half: use for
7524 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007525 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007526 if (lstval == NULL)
7527 return;
7528 curval = lstval;
7529 allocval = lstval + len + 2;
7530 curallocval = allocval;
7531
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007532 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007533 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007534 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007535 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007536 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007537 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007538 if (strval == NULL)
7539 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007540 if (strval == buf)
7541 {
7542 // Need to make a copy, next tv_get_string_buf_chk() will
7543 // overwrite the string.
7544 strval = vim_strsave(buf);
7545 if (strval == NULL)
7546 goto free_lstval;
7547 *curallocval++ = strval;
7548 }
7549 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007550 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007551 }
7552 *curval++ = NULL;
7553
7554 write_reg_contents_lst(regname, lstval, -1,
7555 append, yank_type, block_len);
7556free_lstval:
7557 while (curallocval > allocval)
7558 vim_free(*--curallocval);
7559 vim_free(lstval);
7560 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007561 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007562 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007563 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007564 if (strval == NULL)
7565 return;
7566 write_reg_contents_ex(regname, strval, -1,
7567 append, yank_type, block_len);
7568 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007569 if (pointreg != 0)
7570 get_yank_register(pointreg, TRUE);
7571
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007572 rettv->vval.v_number = 0;
7573}
7574
7575/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007576 * "settagstack()" function
7577 */
7578 static void
7579f_settagstack(typval_T *argvars, typval_T *rettv)
7580{
7581 static char *e_invact2 = N_("E962: Invalid action: '%s'");
7582 win_T *wp;
7583 dict_T *d;
7584 int action = 'r';
7585
7586 rettv->vval.v_number = -1;
7587
7588 // first argument: window number or id
7589 wp = find_win_by_nr_or_id(&argvars[0]);
7590 if (wp == NULL)
7591 return;
7592
7593 // second argument: dict with items to set in the tag stack
7594 if (argvars[1].v_type != VAR_DICT)
7595 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007596 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007597 return;
7598 }
7599 d = argvars[1].vval.v_dict;
7600 if (d == NULL)
7601 return;
7602
7603 // third argument: action - 'a' for append and 'r' for replace.
7604 // default is to replace the stack.
7605 if (argvars[2].v_type == VAR_UNKNOWN)
7606 action = 'r';
7607 else if (argvars[2].v_type == VAR_STRING)
7608 {
7609 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007610 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007611 if (actstr == NULL)
7612 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01007613 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
7614 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007615 action = *actstr;
7616 else
7617 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007618 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007619 return;
7620 }
7621 }
7622 else
7623 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007624 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007625 return;
7626 }
7627
7628 if (set_tagstack(wp, d, action) == OK)
7629 rettv->vval.v_number = 0;
7630}
7631
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007632#ifdef FEAT_CRYPT
7633/*
7634 * "sha256({string})" function
7635 */
7636 static void
7637f_sha256(typval_T *argvars, typval_T *rettv)
7638{
7639 char_u *p;
7640
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007641 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007642 rettv->vval.v_string = vim_strsave(
7643 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
7644 rettv->v_type = VAR_STRING;
7645}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007646#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007647
7648/*
7649 * "shellescape({string})" function
7650 */
7651 static void
7652f_shellescape(typval_T *argvars, typval_T *rettv)
7653{
Bram Moolenaar20615522017-06-05 18:46:26 +02007654 int do_special = non_zero_arg(&argvars[1]);
7655
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007656 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007657 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007658 rettv->v_type = VAR_STRING;
7659}
7660
7661/*
7662 * shiftwidth() function
7663 */
7664 static void
7665f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
7666{
Bram Moolenaarf9514162018-11-22 03:08:29 +01007667 rettv->vval.v_number = 0;
7668
7669 if (argvars[0].v_type != VAR_UNKNOWN)
7670 {
7671 long col;
7672
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007673 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01007674 if (col < 0)
7675 return; // type error; errmsg already given
7676#ifdef FEAT_VARTABS
7677 rettv->vval.v_number = get_sw_value_col(curbuf, col);
7678 return;
7679#endif
7680 }
7681
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007682 rettv->vval.v_number = get_sw_value(curbuf);
7683}
7684
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007685#ifdef FEAT_FLOAT
7686/*
7687 * "sin()" function
7688 */
7689 static void
7690f_sin(typval_T *argvars, typval_T *rettv)
7691{
7692 float_T f = 0.0;
7693
7694 rettv->v_type = VAR_FLOAT;
7695 if (get_float_arg(argvars, &f) == OK)
7696 rettv->vval.v_float = sin(f);
7697 else
7698 rettv->vval.v_float = 0.0;
7699}
7700
7701/*
7702 * "sinh()" function
7703 */
7704 static void
7705f_sinh(typval_T *argvars, typval_T *rettv)
7706{
7707 float_T f = 0.0;
7708
7709 rettv->v_type = VAR_FLOAT;
7710 if (get_float_arg(argvars, &f) == OK)
7711 rettv->vval.v_float = sinh(f);
7712 else
7713 rettv->vval.v_float = 0.0;
7714}
7715#endif
7716
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007717/*
7718 * "soundfold({word})" function
7719 */
7720 static void
7721f_soundfold(typval_T *argvars, typval_T *rettv)
7722{
7723 char_u *s;
7724
7725 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007726 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007727#ifdef FEAT_SPELL
7728 rettv->vval.v_string = eval_soundfold(s);
7729#else
7730 rettv->vval.v_string = vim_strsave(s);
7731#endif
7732}
7733
7734/*
7735 * "spellbadword()" function
7736 */
7737 static void
7738f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
7739{
7740 char_u *word = (char_u *)"";
7741 hlf_T attr = HLF_COUNT;
7742 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007743#ifdef FEAT_SPELL
7744 int wo_spell_save = curwin->w_p_spell;
7745
7746 if (!curwin->w_p_spell)
7747 {
7748 did_set_spelllang(curwin);
7749 curwin->w_p_spell = TRUE;
7750 }
7751
7752 if (*curwin->w_s->b_p_spl == NUL)
7753 {
7754 emsg(_(e_no_spell));
7755 curwin->w_p_spell = wo_spell_save;
7756 return;
7757 }
7758#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007759
7760 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007761 {
7762#ifdef FEAT_SPELL
7763 curwin->w_p_spell = wo_spell_save;
7764#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007765 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007766 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007767
7768#ifdef FEAT_SPELL
7769 if (argvars[0].v_type == VAR_UNKNOWN)
7770 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007771 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007772 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
7773 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007774 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007775 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007776 curwin->w_set_curswant = TRUE;
7777 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007778 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007779 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007780 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007781 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007782 int capcol = -1;
7783
7784 if (str != NULL)
7785 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007786 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007787 while (*str != NUL)
7788 {
7789 len = spell_check(curwin, str, &attr, &capcol, FALSE);
7790 if (attr != HLF_COUNT)
7791 {
7792 word = str;
7793 break;
7794 }
7795 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02007796 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02007797 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007798 }
7799 }
7800 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007801 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007802#endif
7803
7804 list_append_string(rettv->vval.v_list, word, len);
7805 list_append_string(rettv->vval.v_list, (char_u *)(
7806 attr == HLF_SPB ? "bad" :
7807 attr == HLF_SPR ? "rare" :
7808 attr == HLF_SPL ? "local" :
7809 attr == HLF_SPC ? "caps" :
7810 ""), -1);
7811}
7812
7813/*
7814 * "spellsuggest()" function
7815 */
7816 static void
7817f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
7818{
7819#ifdef FEAT_SPELL
7820 char_u *str;
7821 int typeerr = FALSE;
7822 int maxcount;
7823 garray_T ga;
7824 int i;
7825 listitem_T *li;
7826 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007827 int wo_spell_save = curwin->w_p_spell;
7828
7829 if (!curwin->w_p_spell)
7830 {
7831 did_set_spelllang(curwin);
7832 curwin->w_p_spell = TRUE;
7833 }
7834
7835 if (*curwin->w_s->b_p_spl == NUL)
7836 {
7837 emsg(_(e_no_spell));
7838 curwin->w_p_spell = wo_spell_save;
7839 return;
7840 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007841#endif
7842
7843 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007844 {
7845#ifdef FEAT_SPELL
7846 curwin->w_p_spell = wo_spell_save;
7847#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007848 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007849 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007850
7851#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007852 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007853 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007854 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007855 if (argvars[1].v_type != VAR_UNKNOWN)
7856 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007857 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007858 if (maxcount <= 0)
7859 return;
7860 if (argvars[2].v_type != VAR_UNKNOWN)
7861 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02007862 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007863 if (typeerr)
7864 return;
7865 }
7866 }
7867 else
7868 maxcount = 25;
7869
7870 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
7871
7872 for (i = 0; i < ga.ga_len; ++i)
7873 {
7874 str = ((char_u **)ga.ga_data)[i];
7875
7876 li = listitem_alloc();
7877 if (li == NULL)
7878 vim_free(str);
7879 else
7880 {
7881 li->li_tv.v_type = VAR_STRING;
7882 li->li_tv.v_lock = 0;
7883 li->li_tv.vval.v_string = str;
7884 list_append(rettv->vval.v_list, li);
7885 }
7886 }
7887 ga_clear(&ga);
7888 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007889 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007890#endif
7891}
7892
7893 static void
7894f_split(typval_T *argvars, typval_T *rettv)
7895{
7896 char_u *str;
7897 char_u *end;
7898 char_u *pat = NULL;
7899 regmatch_T regmatch;
7900 char_u patbuf[NUMBUFLEN];
7901 char_u *save_cpo;
7902 int match;
7903 colnr_T col = 0;
7904 int keepempty = FALSE;
7905 int typeerr = FALSE;
7906
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007907 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007908 save_cpo = p_cpo;
7909 p_cpo = (char_u *)"";
7910
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007911 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007912 if (argvars[1].v_type != VAR_UNKNOWN)
7913 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007914 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007915 if (pat == NULL)
7916 typeerr = TRUE;
7917 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02007918 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007919 }
7920 if (pat == NULL || *pat == NUL)
7921 pat = (char_u *)"[\\x01- ]\\+";
7922
7923 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007924 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007925 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007926 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007927
7928 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7929 if (regmatch.regprog != NULL)
7930 {
7931 regmatch.rm_ic = FALSE;
7932 while (*str != NUL || keepempty)
7933 {
7934 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007935 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007936 else
7937 match = vim_regexec_nl(&regmatch, str, col);
7938 if (match)
7939 end = regmatch.startp[0];
7940 else
7941 end = str + STRLEN(str);
7942 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
7943 && *str != NUL && match && end < regmatch.endp[0]))
7944 {
7945 if (list_append_string(rettv->vval.v_list, str,
7946 (int)(end - str)) == FAIL)
7947 break;
7948 }
7949 if (!match)
7950 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01007951 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007952 if (regmatch.endp[0] > str)
7953 col = 0;
7954 else
Bram Moolenaar13505972019-01-24 15:04:48 +01007955 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007956 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007957 str = regmatch.endp[0];
7958 }
7959
7960 vim_regfree(regmatch.regprog);
7961 }
7962
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007963theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007964 p_cpo = save_cpo;
7965}
7966
7967#ifdef FEAT_FLOAT
7968/*
7969 * "sqrt()" function
7970 */
7971 static void
7972f_sqrt(typval_T *argvars, typval_T *rettv)
7973{
7974 float_T f = 0.0;
7975
7976 rettv->v_type = VAR_FLOAT;
7977 if (get_float_arg(argvars, &f) == OK)
7978 rettv->vval.v_float = sqrt(f);
7979 else
7980 rettv->vval.v_float = 0.0;
7981}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007982#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007983
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007984#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007985/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007986 * "str2float()" function
7987 */
7988 static void
7989f_str2float(typval_T *argvars, typval_T *rettv)
7990{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007991 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01007992 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007993
Bram Moolenaar08243d22017-01-10 16:12:29 +01007994 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007995 p = skipwhite(p + 1);
7996 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01007997 if (isneg)
7998 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007999 rettv->v_type = VAR_FLOAT;
8000}
8001#endif
8002
8003/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008004 * "str2list()" function
8005 */
8006 static void
8007f_str2list(typval_T *argvars, typval_T *rettv)
8008{
8009 char_u *p;
8010 int utf8 = FALSE;
8011
8012 if (rettv_list_alloc(rettv) == FAIL)
8013 return;
8014
8015 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02008016 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02008017
8018 p = tv_get_string(&argvars[0]);
8019
8020 if (has_mbyte || utf8)
8021 {
8022 int (*ptr2len)(char_u *);
8023 int (*ptr2char)(char_u *);
8024
8025 if (utf8 || enc_utf8)
8026 {
8027 ptr2len = utf_ptr2len;
8028 ptr2char = utf_ptr2char;
8029 }
8030 else
8031 {
8032 ptr2len = mb_ptr2len;
8033 ptr2char = mb_ptr2char;
8034 }
8035
8036 for ( ; *p != NUL; p += (*ptr2len)(p))
8037 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
8038 }
8039 else
8040 for ( ; *p != NUL; ++p)
8041 list_append_number(rettv->vval.v_list, *p);
8042}
8043
8044/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008045 * "str2nr()" function
8046 */
8047 static void
8048f_str2nr(typval_T *argvars, typval_T *rettv)
8049{
8050 int base = 10;
8051 char_u *p;
8052 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008053 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008054 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008055
8056 if (argvars[1].v_type != VAR_UNKNOWN)
8057 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008058 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008059 if (base != 2 && base != 8 && base != 10 && base != 16)
8060 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008061 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008062 return;
8063 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02008064 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008065 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008066 }
8067
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008068 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008069 isneg = (*p == '-');
8070 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008071 p = skipwhite(p + 1);
8072 switch (base)
8073 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008074 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008075 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008076 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008077 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008078 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8079 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008080 if (isneg)
8081 rettv->vval.v_number = -n;
8082 else
8083 rettv->vval.v_number = n;
8084
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008085}
8086
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008087/*
8088 * "strgetchar()" function
8089 */
8090 static void
8091f_strgetchar(typval_T *argvars, typval_T *rettv)
8092{
8093 char_u *str;
8094 int len;
8095 int error = FALSE;
8096 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008097 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008098
8099 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008100 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008101 if (str == NULL)
8102 return;
8103 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008104 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008105 if (error)
8106 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008107
Bram Moolenaar13505972019-01-24 15:04:48 +01008108 while (charidx >= 0 && byteidx < len)
8109 {
8110 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008111 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008112 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8113 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008114 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008115 --charidx;
8116 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008117 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118}
8119
8120/*
8121 * "stridx()" function
8122 */
8123 static void
8124f_stridx(typval_T *argvars, typval_T *rettv)
8125{
8126 char_u buf[NUMBUFLEN];
8127 char_u *needle;
8128 char_u *haystack;
8129 char_u *save_haystack;
8130 char_u *pos;
8131 int start_idx;
8132
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008133 needle = tv_get_string_chk(&argvars[1]);
8134 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008135 rettv->vval.v_number = -1;
8136 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008137 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008138
8139 if (argvars[2].v_type != VAR_UNKNOWN)
8140 {
8141 int error = FALSE;
8142
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008143 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008144 if (error || start_idx >= (int)STRLEN(haystack))
8145 return;
8146 if (start_idx >= 0)
8147 haystack += start_idx;
8148 }
8149
8150 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8151 if (pos != NULL)
8152 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8153}
8154
8155/*
8156 * "string()" function
8157 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008158 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008159f_string(typval_T *argvars, typval_T *rettv)
8160{
8161 char_u *tofree;
8162 char_u numbuf[NUMBUFLEN];
8163
8164 rettv->v_type = VAR_STRING;
8165 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8166 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008167 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008168 if (rettv->vval.v_string != NULL && tofree == NULL)
8169 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8170}
8171
8172/*
8173 * "strlen()" function
8174 */
8175 static void
8176f_strlen(typval_T *argvars, typval_T *rettv)
8177{
8178 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008179 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008180}
8181
8182/*
8183 * "strchars()" function
8184 */
8185 static void
8186f_strchars(typval_T *argvars, typval_T *rettv)
8187{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008188 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar3986b942020-09-06 16:09:04 +02008189 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008190 varnumber_T len = 0;
8191 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008192
8193 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008194 skipcc = (int)tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008195 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008196 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008197 else
8198 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008199 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8200 while (*s != NUL)
8201 {
8202 func_mb_ptr2char_adv(&s);
8203 ++len;
8204 }
8205 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008206 }
8207}
8208
8209/*
8210 * "strdisplaywidth()" function
8211 */
8212 static void
8213f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8214{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008215 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008216 int col = 0;
8217
8218 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008219 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008220
8221 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8222}
8223
8224/*
8225 * "strwidth()" function
8226 */
8227 static void
8228f_strwidth(typval_T *argvars, typval_T *rettv)
8229{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008230 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008231
Bram Moolenaar13505972019-01-24 15:04:48 +01008232 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008233}
8234
8235/*
8236 * "strcharpart()" function
8237 */
8238 static void
8239f_strcharpart(typval_T *argvars, typval_T *rettv)
8240{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008241 char_u *p;
8242 int nchar;
8243 int nbyte = 0;
8244 int charlen;
8245 int len = 0;
8246 int slen;
8247 int error = FALSE;
8248
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008249 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008250 slen = (int)STRLEN(p);
8251
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008252 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008253 if (!error)
8254 {
8255 if (nchar > 0)
8256 while (nchar > 0 && nbyte < slen)
8257 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008258 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008259 --nchar;
8260 }
8261 else
8262 nbyte = nchar;
8263 if (argvars[2].v_type != VAR_UNKNOWN)
8264 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008265 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008266 while (charlen > 0 && nbyte + len < slen)
8267 {
8268 int off = nbyte + len;
8269
8270 if (off < 0)
8271 len += 1;
8272 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008273 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008274 --charlen;
8275 }
8276 }
8277 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008278 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008279 }
8280
8281 /*
8282 * Only return the overlap between the specified part and the actual
8283 * string.
8284 */
8285 if (nbyte < 0)
8286 {
8287 len += nbyte;
8288 nbyte = 0;
8289 }
8290 else if (nbyte > slen)
8291 nbyte = slen;
8292 if (len < 0)
8293 len = 0;
8294 else if (nbyte + len > slen)
8295 len = slen - nbyte;
8296
8297 rettv->v_type = VAR_STRING;
8298 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008299}
8300
8301/*
8302 * "strpart()" function
8303 */
8304 static void
8305f_strpart(typval_T *argvars, typval_T *rettv)
8306{
8307 char_u *p;
8308 int n;
8309 int len;
8310 int slen;
8311 int error = FALSE;
8312
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008313 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008314 slen = (int)STRLEN(p);
8315
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008316 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008317 if (error)
8318 len = 0;
8319 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008320 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008321 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008322 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008323
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02008324 // Only return the overlap between the specified part and the actual
8325 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008326 if (n < 0)
8327 {
8328 len += n;
8329 n = 0;
8330 }
8331 else if (n > slen)
8332 n = slen;
8333 if (len < 0)
8334 len = 0;
8335 else if (n + len > slen)
8336 len = slen - n;
8337
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02008338 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
8339 {
8340 int off;
8341
8342 // length in characters
8343 for (off = n; off < slen && len > 0; --len)
8344 off += mb_ptr2len(p + off);
8345 len = off - n;
8346 }
8347
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008348 rettv->v_type = VAR_STRING;
8349 rettv->vval.v_string = vim_strnsave(p + n, len);
8350}
8351
8352/*
8353 * "strridx()" function
8354 */
8355 static void
8356f_strridx(typval_T *argvars, typval_T *rettv)
8357{
8358 char_u buf[NUMBUFLEN];
8359 char_u *needle;
8360 char_u *haystack;
8361 char_u *rest;
8362 char_u *lastmatch = NULL;
8363 int haystack_len, end_idx;
8364
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008365 needle = tv_get_string_chk(&argvars[1]);
8366 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008367
8368 rettv->vval.v_number = -1;
8369 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008370 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008371
8372 haystack_len = (int)STRLEN(haystack);
8373 if (argvars[2].v_type != VAR_UNKNOWN)
8374 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008375 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008376 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008377 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008378 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008379 }
8380 else
8381 end_idx = haystack_len;
8382
8383 if (*needle == NUL)
8384 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008385 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008386 lastmatch = haystack + end_idx;
8387 }
8388 else
8389 {
8390 for (rest = haystack; *rest != '\0'; ++rest)
8391 {
8392 rest = (char_u *)strstr((char *)rest, (char *)needle);
8393 if (rest == NULL || rest > haystack + end_idx)
8394 break;
8395 lastmatch = rest;
8396 }
8397 }
8398
8399 if (lastmatch == NULL)
8400 rettv->vval.v_number = -1;
8401 else
8402 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
8403}
8404
8405/*
8406 * "strtrans()" function
8407 */
8408 static void
8409f_strtrans(typval_T *argvars, typval_T *rettv)
8410{
8411 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008412 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413}
8414
8415/*
8416 * "submatch()" function
8417 */
8418 static void
8419f_submatch(typval_T *argvars, typval_T *rettv)
8420{
8421 int error = FALSE;
8422 int no;
8423 int retList = 0;
8424
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008425 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426 if (error)
8427 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008428 if (no < 0 || no >= NSUBEXP)
8429 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008430 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01008431 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008432 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008433 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02008434 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008435 if (error)
8436 return;
8437
8438 if (retList == 0)
8439 {
8440 rettv->v_type = VAR_STRING;
8441 rettv->vval.v_string = reg_submatch(no);
8442 }
8443 else
8444 {
8445 rettv->v_type = VAR_LIST;
8446 rettv->vval.v_list = reg_submatch_list(no);
8447 }
8448}
8449
8450/*
8451 * "substitute()" function
8452 */
8453 static void
8454f_substitute(typval_T *argvars, typval_T *rettv)
8455{
8456 char_u patbuf[NUMBUFLEN];
8457 char_u subbuf[NUMBUFLEN];
8458 char_u flagsbuf[NUMBUFLEN];
8459
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008460 char_u *str = tv_get_string_chk(&argvars[0]);
8461 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008462 char_u *sub = NULL;
8463 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008464 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008465
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008466 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
8467 expr = &argvars[2];
8468 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008469 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008470
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008471 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008472 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
8473 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008474 rettv->vval.v_string = NULL;
8475 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008476 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008477}
8478
8479/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008480 * "swapinfo(swap_filename)" function
8481 */
8482 static void
8483f_swapinfo(typval_T *argvars, typval_T *rettv)
8484{
8485 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008486 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008487}
8488
8489/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02008490 * "swapname(expr)" function
8491 */
8492 static void
8493f_swapname(typval_T *argvars, typval_T *rettv)
8494{
8495 buf_T *buf;
8496
8497 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008498 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02008499 if (buf == NULL || buf->b_ml.ml_mfp == NULL
8500 || buf->b_ml.ml_mfp->mf_fname == NULL)
8501 rettv->vval.v_string = NULL;
8502 else
8503 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
8504}
8505
8506/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008507 * "synID(lnum, col, trans)" function
8508 */
8509 static void
8510f_synID(typval_T *argvars UNUSED, typval_T *rettv)
8511{
8512 int id = 0;
8513#ifdef FEAT_SYN_HL
8514 linenr_T lnum;
8515 colnr_T col;
8516 int trans;
8517 int transerr = FALSE;
8518
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008519 lnum = tv_get_lnum(argvars); // -1 on type error
8520 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02008521 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008522
8523 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8524 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
8525 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
8526#endif
8527
8528 rettv->vval.v_number = id;
8529}
8530
8531/*
8532 * "synIDattr(id, what [, mode])" function
8533 */
8534 static void
8535f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
8536{
8537 char_u *p = NULL;
8538#ifdef FEAT_SYN_HL
8539 int id;
8540 char_u *what;
8541 char_u *mode;
8542 char_u modebuf[NUMBUFLEN];
8543 int modec;
8544
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008545 id = (int)tv_get_number(&argvars[0]);
8546 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008547 if (argvars[2].v_type != VAR_UNKNOWN)
8548 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008549 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008550 modec = TOLOWER_ASC(mode[0]);
8551 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008552 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008553 }
8554 else
8555 {
8556#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
8557 if (USE_24BIT)
8558 modec = 'g';
8559 else
8560#endif
8561 if (t_colors > 1)
8562 modec = 'c';
8563 else
8564 modec = 't';
8565 }
8566
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008567 switch (TOLOWER_ASC(what[0]))
8568 {
8569 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008570 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008571 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008572 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008573 p = highlight_has_attr(id, HL_BOLD, modec);
8574 break;
8575
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008576 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008577 p = highlight_color(id, what, modec);
8578 break;
8579
8580 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008581 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008582 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008583 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008584 p = highlight_has_attr(id, HL_ITALIC, modec);
8585 break;
8586
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008587 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008588 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008589 break;
8590
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008591 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008592 p = highlight_has_attr(id, HL_INVERSE, modec);
8593 break;
8594
8595 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008596 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008597 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008598 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008599 else if (TOLOWER_ASC(what[1]) == 't' &&
8600 TOLOWER_ASC(what[2]) == 'r')
8601 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008602 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008603 p = highlight_has_attr(id, HL_STANDOUT, modec);
8604 break;
8605
8606 case 'u':
8607 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008608 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008609 p = highlight_has_attr(id, HL_UNDERLINE, modec);
8610 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008611 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008612 p = highlight_has_attr(id, HL_UNDERCURL, modec);
8613 break;
8614 }
8615
8616 if (p != NULL)
8617 p = vim_strsave(p);
8618#endif
8619 rettv->v_type = VAR_STRING;
8620 rettv->vval.v_string = p;
8621}
8622
8623/*
8624 * "synIDtrans(id)" function
8625 */
8626 static void
8627f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
8628{
8629 int id;
8630
8631#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008632 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008633
8634 if (id > 0)
8635 id = syn_get_final_id(id);
8636 else
8637#endif
8638 id = 0;
8639
8640 rettv->vval.v_number = id;
8641}
8642
8643/*
8644 * "synconcealed(lnum, col)" function
8645 */
8646 static void
8647f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
8648{
8649#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
8650 linenr_T lnum;
8651 colnr_T col;
8652 int syntax_flags = 0;
8653 int cchar;
8654 int matchid = 0;
8655 char_u str[NUMBUFLEN];
8656#endif
8657
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008658 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008659
8660#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008661 lnum = tv_get_lnum(argvars); // -1 on type error
8662 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008663
Bram Moolenaara80faa82020-04-12 19:37:17 +02008664 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008665
8666 if (rettv_list_alloc(rettv) != FAIL)
8667 {
8668 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8669 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8670 && curwin->w_p_cole > 0)
8671 {
8672 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
8673 syntax_flags = get_syntax_info(&matchid);
8674
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008675 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008676 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
8677 {
8678 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02008679 if (cchar == NUL && curwin->w_p_cole == 1)
8680 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008681 if (cchar != NUL)
8682 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008683 if (has_mbyte)
8684 (*mb_char2bytes)(cchar, str);
8685 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008686 str[0] = cchar;
8687 }
8688 }
8689 }
8690
8691 list_append_number(rettv->vval.v_list,
8692 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008693 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008694 list_append_string(rettv->vval.v_list, str, -1);
8695 list_append_number(rettv->vval.v_list, matchid);
8696 }
8697#endif
8698}
8699
8700/*
8701 * "synstack(lnum, col)" function
8702 */
8703 static void
8704f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
8705{
8706#ifdef FEAT_SYN_HL
8707 linenr_T lnum;
8708 colnr_T col;
8709 int i;
8710 int id;
8711#endif
8712
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008713 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008714
8715#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008716 lnum = tv_get_lnum(argvars); // -1 on type error
8717 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008718
8719 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8720 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8721 && rettv_list_alloc(rettv) != FAIL)
8722 {
8723 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
8724 for (i = 0; ; ++i)
8725 {
8726 id = syn_get_stack_item(i);
8727 if (id < 0)
8728 break;
8729 if (list_append_number(rettv->vval.v_list, id) == FAIL)
8730 break;
8731 }
8732 }
8733#endif
8734}
8735
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008736/*
8737 * "tabpagebuflist()" function
8738 */
8739 static void
8740f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8741{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008742 tabpage_T *tp;
8743 win_T *wp = NULL;
8744
8745 if (argvars[0].v_type == VAR_UNKNOWN)
8746 wp = firstwin;
8747 else
8748 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008749 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008750 if (tp != NULL)
8751 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
8752 }
8753 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
8754 {
8755 for (; wp != NULL; wp = wp->w_next)
8756 if (list_append_number(rettv->vval.v_list,
8757 wp->w_buffer->b_fnum) == FAIL)
8758 break;
8759 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008760}
8761
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008762/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008763 * "tagfiles()" function
8764 */
8765 static void
8766f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
8767{
8768 char_u *fname;
8769 tagname_T tn;
8770 int first;
8771
8772 if (rettv_list_alloc(rettv) == FAIL)
8773 return;
8774 fname = alloc(MAXPATHL);
8775 if (fname == NULL)
8776 return;
8777
8778 for (first = TRUE; ; first = FALSE)
8779 if (get_tagfname(&tn, first, fname) == FAIL
8780 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
8781 break;
8782 tagname_free(&tn);
8783 vim_free(fname);
8784}
8785
8786/*
8787 * "taglist()" function
8788 */
8789 static void
8790f_taglist(typval_T *argvars, typval_T *rettv)
8791{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008792 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008793 char_u *tag_pattern;
8794
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008795 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796
8797 rettv->vval.v_number = FALSE;
8798 if (*tag_pattern == NUL)
8799 return;
8800
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008801 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008802 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008803 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008804 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008805}
8806
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008807#ifdef FEAT_FLOAT
8808/*
8809 * "tan()" function
8810 */
8811 static void
8812f_tan(typval_T *argvars, typval_T *rettv)
8813{
8814 float_T f = 0.0;
8815
8816 rettv->v_type = VAR_FLOAT;
8817 if (get_float_arg(argvars, &f) == OK)
8818 rettv->vval.v_float = tan(f);
8819 else
8820 rettv->vval.v_float = 0.0;
8821}
8822
8823/*
8824 * "tanh()" function
8825 */
8826 static void
8827f_tanh(typval_T *argvars, typval_T *rettv)
8828{
8829 float_T f = 0.0;
8830
8831 rettv->v_type = VAR_FLOAT;
8832 if (get_float_arg(argvars, &f) == OK)
8833 rettv->vval.v_float = tanh(f);
8834 else
8835 rettv->vval.v_float = 0.0;
8836}
8837#endif
8838
8839/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840 * "tolower(string)" function
8841 */
8842 static void
8843f_tolower(typval_T *argvars, typval_T *rettv)
8844{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008845 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008846 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008847}
8848
8849/*
8850 * "toupper(string)" function
8851 */
8852 static void
8853f_toupper(typval_T *argvars, typval_T *rettv)
8854{
8855 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008856 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008857}
8858
8859/*
8860 * "tr(string, fromstr, tostr)" function
8861 */
8862 static void
8863f_tr(typval_T *argvars, typval_T *rettv)
8864{
8865 char_u *in_str;
8866 char_u *fromstr;
8867 char_u *tostr;
8868 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008869 int inlen;
8870 int fromlen;
8871 int tolen;
8872 int idx;
8873 char_u *cpstr;
8874 int cplen;
8875 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876 char_u buf[NUMBUFLEN];
8877 char_u buf2[NUMBUFLEN];
8878 garray_T ga;
8879
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008880 in_str = tv_get_string(&argvars[0]);
8881 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
8882 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008884 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008885 rettv->v_type = VAR_STRING;
8886 rettv->vval.v_string = NULL;
8887 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008888 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008889 ga_init2(&ga, (int)sizeof(char), 80);
8890
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008891 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008892 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008893 if (STRLEN(fromstr) != STRLEN(tostr))
8894 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008895error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008896 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008897 ga_clear(&ga);
8898 return;
8899 }
8900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008901 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008902 while (*in_str != NUL)
8903 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008904 if (has_mbyte)
8905 {
8906 inlen = (*mb_ptr2len)(in_str);
8907 cpstr = in_str;
8908 cplen = inlen;
8909 idx = 0;
8910 for (p = fromstr; *p != NUL; p += fromlen)
8911 {
8912 fromlen = (*mb_ptr2len)(p);
8913 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
8914 {
8915 for (p = tostr; *p != NUL; p += tolen)
8916 {
8917 tolen = (*mb_ptr2len)(p);
8918 if (idx-- == 0)
8919 {
8920 cplen = tolen;
8921 cpstr = p;
8922 break;
8923 }
8924 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008925 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008926 goto error;
8927 break;
8928 }
8929 ++idx;
8930 }
8931
8932 if (first && cpstr == in_str)
8933 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008934 // Check that fromstr and tostr have the same number of
8935 // (multi-byte) characters. Done only once when a character
8936 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008937 first = FALSE;
8938 for (p = tostr; *p != NUL; p += tolen)
8939 {
8940 tolen = (*mb_ptr2len)(p);
8941 --idx;
8942 }
8943 if (idx != 0)
8944 goto error;
8945 }
8946
8947 (void)ga_grow(&ga, cplen);
8948 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
8949 ga.ga_len += cplen;
8950
8951 in_str += inlen;
8952 }
8953 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008954 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008955 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008956 p = vim_strchr(fromstr, *in_str);
8957 if (p != NULL)
8958 ga_append(&ga, tostr[p - fromstr]);
8959 else
8960 ga_append(&ga, *in_str);
8961 ++in_str;
8962 }
8963 }
8964
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008965 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008966 (void)ga_grow(&ga, 1);
8967 ga_append(&ga, NUL);
8968
8969 rettv->vval.v_string = ga.ga_data;
8970}
8971
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008972/*
8973 * "trim({expr})" function
8974 */
8975 static void
8976f_trim(typval_T *argvars, typval_T *rettv)
8977{
8978 char_u buf1[NUMBUFLEN];
8979 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008980 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008981 char_u *mask = NULL;
8982 char_u *tail;
8983 char_u *prev;
8984 char_u *p;
8985 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008986 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008987
8988 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008989 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008990 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008991 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008992
8993 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008994 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008995 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008996
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008997 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008998 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008999 int error = 0;
9000
9001 // leading or trailing characters to trim
9002 dir = (int)tv_get_number_chk(&argvars[2], &error);
9003 if (error)
9004 return;
9005 if (dir < 0 || dir > 2)
9006 {
9007 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
9008 return;
9009 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009010 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009011 }
9012
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009013 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009014 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009015 // Trim leading characters
9016 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009017 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009018 c1 = PTR2CHAR(head);
9019 if (mask == NULL)
9020 {
9021 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009022 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009023 }
9024 else
9025 {
9026 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9027 if (c1 == PTR2CHAR(p))
9028 break;
9029 if (*p == NUL)
9030 break;
9031 }
9032 MB_PTR_ADV(head);
9033 }
9034 }
9035
9036 tail = head + STRLEN(head);
9037 if (dir == 0 || dir == 2)
9038 {
9039 // Trim trailing characters
9040 for (; tail > head; tail = prev)
9041 {
9042 prev = tail;
9043 MB_PTR_BACK(head, prev);
9044 c1 = PTR2CHAR(prev);
9045 if (mask == NULL)
9046 {
9047 if (c1 > ' ' && c1 != 0xa0)
9048 break;
9049 }
9050 else
9051 {
9052 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9053 if (c1 == PTR2CHAR(p))
9054 break;
9055 if (*p == NUL)
9056 break;
9057 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009058 }
9059 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009060 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009061}
9062
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009063#ifdef FEAT_FLOAT
9064/*
9065 * "trunc({float})" function
9066 */
9067 static void
9068f_trunc(typval_T *argvars, typval_T *rettv)
9069{
9070 float_T f = 0.0;
9071
9072 rettv->v_type = VAR_FLOAT;
9073 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009074 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009075 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
9076 else
9077 rettv->vval.v_float = 0.0;
9078}
9079#endif
9080
9081/*
9082 * "type(expr)" function
9083 */
9084 static void
9085f_type(typval_T *argvars, typval_T *rettv)
9086{
9087 int n = -1;
9088
9089 switch (argvars[0].v_type)
9090 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009091 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9092 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009093 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009094 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9095 case VAR_LIST: n = VAR_TYPE_LIST; break;
9096 case VAR_DICT: n = VAR_TYPE_DICT; break;
9097 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9098 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9099 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009100 case VAR_JOB: n = VAR_TYPE_JOB; break;
9101 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009102 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009103 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009104 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009105 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009106 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009107 n = -1;
9108 break;
9109 }
9110 rettv->vval.v_number = n;
9111}
9112
9113/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009114 * "virtcol(string)" function
9115 */
9116 static void
9117f_virtcol(typval_T *argvars, typval_T *rettv)
9118{
9119 colnr_T vcol = 0;
9120 pos_T *fp;
9121 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009122 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009123
9124 fp = var2fpos(&argvars[0], FALSE, &fnum);
9125 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9126 && fnum == curbuf->b_fnum)
9127 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009128 // Limit the column to a valid value, getvvcol() doesn't check.
9129 if (fp->col < 0)
9130 fp->col = 0;
9131 else
9132 {
9133 len = (int)STRLEN(ml_get(fp->lnum));
9134 if (fp->col > len)
9135 fp->col = len;
9136 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009137 getvvcol(curwin, fp, NULL, NULL, &vcol);
9138 ++vcol;
9139 }
9140
9141 rettv->vval.v_number = vcol;
9142}
9143
9144/*
9145 * "visualmode()" function
9146 */
9147 static void
9148f_visualmode(typval_T *argvars, typval_T *rettv)
9149{
9150 char_u str[2];
9151
9152 rettv->v_type = VAR_STRING;
9153 str[0] = curbuf->b_visual_mode_eval;
9154 str[1] = NUL;
9155 rettv->vval.v_string = vim_strsave(str);
9156
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009157 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009158 if (non_zero_arg(&argvars[0]))
9159 curbuf->b_visual_mode_eval = NUL;
9160}
9161
9162/*
9163 * "wildmenumode()" function
9164 */
9165 static void
9166f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9167{
9168#ifdef FEAT_WILDMENU
9169 if (wild_menu_showing)
9170 rettv->vval.v_number = 1;
9171#endif
9172}
9173
9174/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009175 * "windowsversion()" function
9176 */
9177 static void
9178f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9179{
9180 rettv->v_type = VAR_STRING;
9181 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9182}
9183
9184/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009185 * "wordcount()" function
9186 */
9187 static void
9188f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9189{
9190 if (rettv_dict_alloc(rettv) == FAIL)
9191 return;
9192 cursor_pos_info(rettv->vval.v_dict);
9193}
9194
9195/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009196 * "xor(expr, expr)" function
9197 */
9198 static void
9199f_xor(typval_T *argvars, typval_T *rettv)
9200{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009201 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9202 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009203}
9204
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009205#endif // FEAT_EVAL